diff --git a/dist/lib.d.ts b/dist/lib.d.ts index 0dceb5e..4ad5820 100644 --- a/dist/lib.d.ts +++ b/dist/lib.d.ts @@ -13,11 +13,19 @@ declare module "json2pbf" { Columnar = 2, Row = 3 } + export enum UnpackMethod { + Generic = 1, + Columnar = 2, + Row = 3 + } export interface PackOptions { pbf?: typeof Pbf; method?: PackMethod; columns?: Record; } - export function pack(val: any, options?: PackOptions): ArrayBuffer; - export function unpack(arr: ArrayBuffer): any; + export function packJson(val: any, options?: PackOptions): ArrayBuffer; + export interface UnpackOptions { + method?: UnpackMethod; + } + export function unpackJson(arr: ArrayBuffer, options?: UnpackOptions): any; } diff --git a/dist/lib.mjs b/dist/lib.mjs index a9d4759..850e3d0 100644 --- a/dist/lib.mjs +++ b/dist/lib.mjs @@ -1,32 +1,32 @@ -function U(t) { +function H(t) { return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t; } -var v = {}; +var N = {}; /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh */ -v.read = function(t, e, i, n, r) { - var s, o, f = r * 8 - n - 1, a = (1 << f) - 1, x = a >> 1, u = -7, d = i ? r - 1 : 0, l = i ? -1 : 1, w = t[e + d]; - for (d += l, s = w & (1 << -u) - 1, w >>= -u, u += f; u > 0; s = s * 256 + t[e + d], d += l, u -= 8) +N.read = function(t, e, i, n, r) { + var s, o, f = r * 8 - n - 1, u = (1 << f) - 1, x = u >> 1, a = -7, c = i ? r - 1 : 0, F = i ? -1 : 1, d = t[e + c]; + for (c += F, s = d & (1 << -a) - 1, d >>= -a, a += f; a > 0; s = s * 256 + t[e + c], c += F, a -= 8) ; - for (o = s & (1 << -u) - 1, s >>= -u, u += n; u > 0; o = o * 256 + t[e + d], d += l, u -= 8) + for (o = s & (1 << -a) - 1, s >>= -a, a += n; a > 0; o = o * 256 + t[e + c], c += F, a -= 8) ; if (s === 0) s = 1 - x; else { - if (s === a) - return o ? NaN : (w ? -1 : 1) * (1 / 0); + if (s === u) + return o ? NaN : (d ? -1 : 1) * (1 / 0); o = o + Math.pow(2, n), s = s - x; } - return (w ? -1 : 1) * o * Math.pow(2, s - n); + return (d ? -1 : 1) * o * Math.pow(2, s - n); }; -v.write = function(t, e, i, n, r, s) { - var o, f, a, x = s * 8 - r - 1, u = (1 << x) - 1, d = u >> 1, l = r === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0, w = n ? 0 : s - 1, c = n ? 1 : -1, F = e < 0 || e === 0 && 1 / e < 0 ? 1 : 0; - for (e = Math.abs(e), isNaN(e) || e === 1 / 0 ? (f = isNaN(e) ? 1 : 0, o = u) : (o = Math.floor(Math.log(e) / Math.LN2), e * (a = Math.pow(2, -o)) < 1 && (o--, a *= 2), o + d >= 1 ? e += l / a : e += l * Math.pow(2, 1 - d), e * a >= 2 && (o++, a /= 2), o + d >= u ? (f = 0, o = u) : o + d >= 1 ? (f = (e * a - 1) * Math.pow(2, r), o = o + d) : (f = e * Math.pow(2, d - 1) * Math.pow(2, r), o = 0)); r >= 8; t[i + w] = f & 255, w += c, f /= 256, r -= 8) +N.write = function(t, e, i, n, r, s) { + var o, f, u, x = s * 8 - r - 1, a = (1 << x) - 1, c = a >> 1, F = r === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0, d = n ? 0 : s - 1, w = n ? 1 : -1, g = e < 0 || e === 0 && 1 / e < 0 ? 1 : 0; + for (e = Math.abs(e), isNaN(e) || e === 1 / 0 ? (f = isNaN(e) ? 1 : 0, o = a) : (o = Math.floor(Math.log(e) / Math.LN2), e * (u = Math.pow(2, -o)) < 1 && (o--, u *= 2), o + c >= 1 ? e += F / u : e += F * Math.pow(2, 1 - c), e * u >= 2 && (o++, u /= 2), o + c >= a ? (f = 0, o = a) : o + c >= 1 ? (f = (e * u - 1) * Math.pow(2, r), o = o + c) : (f = e * Math.pow(2, c - 1) * Math.pow(2, r), o = 0)); r >= 8; t[i + d] = f & 255, d += w, f /= 256, r -= 8) ; - for (o = o << r | f, x += r; x > 0; t[i + w] = o & 255, w += c, o /= 256, x -= 8) + for (o = o << r | f, x += r; x > 0; t[i + d] = o & 255, d += w, o /= 256, x -= 8) ; - t[i + w - c] |= F * 128; + t[i + d - w] |= g * 128; }; -var $ = h, S = v; +var U = h, M = N; function h(t) { this.buf = ArrayBuffer.isView && ArrayBuffer.isView(t) ? t : new Uint8Array(t || 0), this.pos = 0, this.type = 0, this.length = this.buf.length; } @@ -34,7 +34,7 @@ h.Varint = 0; h.Fixed64 = 1; h.Bytes = 2; h.Fixed32 = 5; -var D = 65536 * 65536, N = 1 / D, G = 12, I = typeof TextDecoder > "u" ? null : new TextDecoder("utf8"); +var E = 65536 * 65536, A = 1 / E, K = 12, I = typeof TextDecoder > "u" ? null : new TextDecoder("utf8"); h.prototype = { destroy: function() { this.buf = null; @@ -51,33 +51,33 @@ h.prototype = { return this.readFields(t, e, this.readVarint() + this.pos); }, readFixed32: function() { - var t = M(this.buf, this.pos); + var t = D(this.buf, this.pos); return this.pos += 4, t; }, readSFixed32: function() { - var t = A(this.buf, this.pos); + var t = _(this.buf, this.pos); return this.pos += 4, t; }, // 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed) readFixed64: function() { - var t = M(this.buf, this.pos) + M(this.buf, this.pos + 4) * D; + var t = D(this.buf, this.pos) + D(this.buf, this.pos + 4) * E; return this.pos += 8, t; }, readSFixed64: function() { - var t = M(this.buf, this.pos) + A(this.buf, this.pos + 4) * D; + var t = D(this.buf, this.pos) + _(this.buf, this.pos + 4) * E; return this.pos += 8, t; }, readFloat: function() { - var t = S.read(this.buf, this.pos, !0, 23, 4); + var t = M.read(this.buf, this.pos, !0, 23, 4); return this.pos += 4, t; }, readDouble: function() { - var t = S.read(this.buf, this.pos, !0, 52, 8); + var t = M.read(this.buf, this.pos, !0, 52, 8); return this.pos += 8, t; }, readVarint: function(t) { var e = this.buf, i, n; - return n = e[this.pos++], i = n & 127, n < 128 || (n = e[this.pos++], i |= (n & 127) << 7, n < 128) || (n = e[this.pos++], i |= (n & 127) << 14, n < 128) || (n = e[this.pos++], i |= (n & 127) << 21, n < 128) ? i : (n = e[this.pos], i |= (n & 15) << 28, H(i, t, this)); + return n = e[this.pos++], i = n & 127, n < 128 || (n = e[this.pos++], i |= (n & 127) << 7, n < 128) || (n = e[this.pos++], i |= (n & 127) << 14, n < 128) || (n = e[this.pos++], i |= (n & 127) << 21, n < 128) ? i : (n = e[this.pos], i |= (n & 15) << 28, X(i, t, this)); }, readVarint64: function() { return this.readVarint(!0); @@ -91,7 +91,7 @@ h.prototype = { }, readString: function() { var t = this.readVarint() + this.pos, e = this.pos; - return this.pos = t, t - e >= G && I ? rt(this.buf, e, t) : it(this.buf, e, t); + return this.pos = t, t - e >= K && I ? st(this.buf, e, t) : nt(this.buf, e, t); }, readBytes: function() { var t = this.readVarint() + this.pos, e = this.buf.subarray(this.pos, t); @@ -100,55 +100,55 @@ h.prototype = { // verbose for performance reasons; doesn't affect gzipped size readPackedVarint: function(t, e) { if (this.type !== h.Bytes) return t.push(this.readVarint(e)); - var i = p(this); + var i = y(this); for (t = t || []; this.pos < i; ) t.push(this.readVarint(e)); return t; }, readPackedSVarint: function(t) { if (this.type !== h.Bytes) return t.push(this.readSVarint()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readSVarint()); return t; }, readPackedBoolean: function(t) { if (this.type !== h.Bytes) return t.push(this.readBoolean()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readBoolean()); return t; }, readPackedFloat: function(t) { if (this.type !== h.Bytes) return t.push(this.readFloat()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readFloat()); return t; }, readPackedDouble: function(t) { if (this.type !== h.Bytes) return t.push(this.readDouble()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readDouble()); return t; }, readPackedFixed32: function(t) { if (this.type !== h.Bytes) return t.push(this.readFixed32()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readFixed32()); return t; }, readPackedSFixed32: function(t) { if (this.type !== h.Bytes) return t.push(this.readSFixed32()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readSFixed32()); return t; }, readPackedFixed64: function(t) { if (this.type !== h.Bytes) return t.push(this.readFixed64()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readFixed64()); return t; }, readPackedSFixed64: function(t) { if (this.type !== h.Bytes) return t.push(this.readSFixed64()); - var e = p(this); + var e = y(this); for (t = t || []; this.pos < e; ) t.push(this.readSFixed64()); return t; }, @@ -182,14 +182,14 @@ h.prototype = { this.realloc(4), k(this.buf, t, this.pos), this.pos += 4; }, writeFixed64: function(t) { - this.realloc(8), k(this.buf, t & -1, this.pos), k(this.buf, Math.floor(t * N), this.pos + 4), this.pos += 8; + this.realloc(8), k(this.buf, t & -1, this.pos), k(this.buf, Math.floor(t * A), this.pos + 4), this.pos += 8; }, writeSFixed64: function(t) { - this.realloc(8), k(this.buf, t & -1, this.pos), k(this.buf, Math.floor(t * N), this.pos + 4), this.pos += 8; + this.realloc(8), k(this.buf, t & -1, this.pos), k(this.buf, Math.floor(t * A), this.pos + 4), this.pos += 8; }, writeVarint: function(t) { if (t = +t || 0, t > 268435455 || t < 0) { - K(t, this); + z(t, this); return; } this.realloc(4), this.buf[this.pos++] = t & 127 | (t > 127 ? 128 : 0), !(t <= 127) && (this.buf[this.pos++] = (t >>>= 7) & 127 | (t > 127 ? 128 : 0), !(t <= 127) && (this.buf[this.pos++] = (t >>>= 7) & 127 | (t > 127 ? 128 : 0), !(t <= 127) && (this.buf[this.pos++] = t >>> 7 & 127))); @@ -203,15 +203,15 @@ h.prototype = { writeString: function(t) { t = String(t), this.realloc(t.length * 4), this.pos++; var e = this.pos; - this.pos = nt(this.buf, t, this.pos); + this.pos = ot(this.buf, t, this.pos); var i = this.pos - e; - i >= 128 && j(e, i, this), this.pos = e - 1, this.writeVarint(i), this.pos += i; + i >= 128 && O(e, i, this), this.pos = e - 1, this.writeVarint(i), this.pos += i; }, writeFloat: function(t) { - this.realloc(4), S.write(this.buf, t, this.pos, !0, 23, 4), this.pos += 4; + this.realloc(4), M.write(this.buf, t, this.pos, !0, 23, 4), this.pos += 4; }, writeDouble: function(t) { - this.realloc(8), S.write(this.buf, t, this.pos, !0, 52, 8), this.pos += 8; + this.realloc(8), M.write(this.buf, t, this.pos, !0, 52, 8), this.pos += 8; }, writeBytes: function(t) { var e = t.length; @@ -223,37 +223,37 @@ h.prototype = { var i = this.pos; t(e, this); var n = this.pos - i; - n >= 128 && j(i, n, this), this.pos = i - 1, this.writeVarint(n), this.pos += n; + n >= 128 && O(i, n, this), this.pos = i - 1, this.writeVarint(n), this.pos += n; }, writeMessage: function(t, e, i) { this.writeTag(t, h.Bytes), this.writeRawMessage(e, i); }, writePackedVarint: function(t, e) { - e.length && this.writeMessage(t, L, e); + e.length && this.writeMessage(t, W, e); }, writePackedSVarint: function(t, e) { - e.length && this.writeMessage(t, Q, e); + e.length && this.writeMessage(t, Y, e); }, writePackedBoolean: function(t, e) { - e.length && this.writeMessage(t, Z, e); + e.length && this.writeMessage(t, J, e); }, writePackedFloat: function(t, e) { - e.length && this.writeMessage(t, W, e); + e.length && this.writeMessage(t, Z, e); }, writePackedDouble: function(t, e) { - e.length && this.writeMessage(t, Y, e); + e.length && this.writeMessage(t, q, e); }, writePackedFixed32: function(t, e) { - e.length && this.writeMessage(t, q, e); + e.length && this.writeMessage(t, tt, e); }, writePackedSFixed32: function(t, e) { - e.length && this.writeMessage(t, J, e); + e.length && this.writeMessage(t, et, e); }, writePackedFixed64: function(t, e) { - e.length && this.writeMessage(t, tt, e); + e.length && this.writeMessage(t, it, e); }, writePackedSFixed64: function(t, e) { - e.length && this.writeMessage(t, et, e); + e.length && this.writeMessage(t, rt, e); }, writeBytesField: function(t, e) { this.writeTag(t, h.Bytes), this.writeBytes(e); @@ -289,84 +289,84 @@ h.prototype = { this.writeVarintField(t, !!e); } }; -function H(t, e, i) { +function X(t, e, i) { var n = i.buf, r, s; if (s = n[i.pos++], r = (s & 112) >> 4, s < 128 || (s = n[i.pos++], r |= (s & 127) << 3, s < 128) || (s = n[i.pos++], r |= (s & 127) << 10, s < 128) || (s = n[i.pos++], r |= (s & 127) << 17, s < 128) || (s = n[i.pos++], r |= (s & 127) << 24, s < 128) || (s = n[i.pos++], r |= (s & 1) << 31, s < 128)) return B(t, r, e); throw new Error("Expected varint not more than 10 bytes"); } -function p(t) { +function y(t) { return t.type === h.Bytes ? t.readVarint() + t.pos : t.pos + 1; } function B(t, e, i) { return i ? e * 4294967296 + (t >>> 0) : (e >>> 0) * 4294967296 + (t >>> 0); } -function K(t, e) { +function z(t, e) { var i, n; if (t >= 0 ? (i = t % 4294967296 | 0, n = t / 4294967296 | 0) : (i = ~(-t % 4294967296), n = ~(-t / 4294967296), i ^ 4294967295 ? i = i + 1 | 0 : (i = 0, n = n + 1 | 0)), t >= 18446744073709552e3 || t < -18446744073709552e3) throw new Error("Given varint doesn't fit into 10 bytes"); - e.realloc(10), X(i, n, e), z(n, e); + e.realloc(10), L(i, n, e), Q(n, e); } -function X(t, e, i) { +function L(t, e, i) { i.buf[i.pos++] = t & 127 | 128, t >>>= 7, i.buf[i.pos++] = t & 127 | 128, t >>>= 7, i.buf[i.pos++] = t & 127 | 128, t >>>= 7, i.buf[i.pos++] = t & 127 | 128, t >>>= 7, i.buf[i.pos] = t & 127; } -function z(t, e) { +function Q(t, e) { var i = (t & 7) << 4; e.buf[e.pos++] |= i | ((t >>>= 3) ? 128 : 0), t && (e.buf[e.pos++] = t & 127 | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = t & 127 | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = t & 127 | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = t & 127 | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = t & 127))))); } -function j(t, e, i) { +function O(t, e, i) { var n = e <= 16383 ? 1 : e <= 2097151 ? 2 : e <= 268435455 ? 3 : Math.floor(Math.log(e) / (Math.LN2 * 7)); i.realloc(n); for (var r = i.pos - 1; r >= t; r--) i.buf[r + n] = i.buf[r]; } -function L(t, e) { +function W(t, e) { for (var i = 0; i < t.length; i++) e.writeVarint(t[i]); } -function Q(t, e) { +function Y(t, e) { for (var i = 0; i < t.length; i++) e.writeSVarint(t[i]); } -function W(t, e) { +function Z(t, e) { for (var i = 0; i < t.length; i++) e.writeFloat(t[i]); } -function Y(t, e) { +function q(t, e) { for (var i = 0; i < t.length; i++) e.writeDouble(t[i]); } -function Z(t, e) { +function J(t, e) { for (var i = 0; i < t.length; i++) e.writeBoolean(t[i]); } -function q(t, e) { +function tt(t, e) { for (var i = 0; i < t.length; i++) e.writeFixed32(t[i]); } -function J(t, e) { +function et(t, e) { for (var i = 0; i < t.length; i++) e.writeSFixed32(t[i]); } -function tt(t, e) { +function it(t, e) { for (var i = 0; i < t.length; i++) e.writeFixed64(t[i]); } -function et(t, e) { +function rt(t, e) { for (var i = 0; i < t.length; i++) e.writeSFixed64(t[i]); } -function M(t, e) { +function D(t, e) { return (t[e] | t[e + 1] << 8 | t[e + 2] << 16) + t[e + 3] * 16777216; } function k(t, e, i) { t[i] = e, t[i + 1] = e >>> 8, t[i + 2] = e >>> 16, t[i + 3] = e >>> 24; } -function A(t, e) { +function _(t, e) { return (t[e] | t[e + 1] << 8 | t[e + 2] << 16) + (t[e + 3] << 24); } -function it(t, e, i) { +function nt(t, e, i) { for (var n = "", r = e; r < i; ) { var s = t[r], o = null, f = s > 239 ? 4 : s > 223 ? 3 : s > 191 ? 2 : 1; if (r + f > i) break; - var a, x, u; - f === 1 ? s < 128 && (o = s) : f === 2 ? (a = t[r + 1], (a & 192) === 128 && (o = (s & 31) << 6 | a & 63, o <= 127 && (o = null))) : f === 3 ? (a = t[r + 1], x = t[r + 2], (a & 192) === 128 && (x & 192) === 128 && (o = (s & 15) << 12 | (a & 63) << 6 | x & 63, (o <= 2047 || o >= 55296 && o <= 57343) && (o = null))) : f === 4 && (a = t[r + 1], x = t[r + 2], u = t[r + 3], (a & 192) === 128 && (x & 192) === 128 && (u & 192) === 128 && (o = (s & 15) << 18 | (a & 63) << 12 | (x & 63) << 6 | u & 63, (o <= 65535 || o >= 1114112) && (o = null))), o === null ? (o = 65533, f = 1) : o > 65535 && (o -= 65536, n += String.fromCharCode(o >>> 10 & 1023 | 55296), o = 56320 | o & 1023), n += String.fromCharCode(o), r += f; + var u, x, a; + f === 1 ? s < 128 && (o = s) : f === 2 ? (u = t[r + 1], (u & 192) === 128 && (o = (s & 31) << 6 | u & 63, o <= 127 && (o = null))) : f === 3 ? (u = t[r + 1], x = t[r + 2], (u & 192) === 128 && (x & 192) === 128 && (o = (s & 15) << 12 | (u & 63) << 6 | x & 63, (o <= 2047 || o >= 55296 && o <= 57343) && (o = null))) : f === 4 && (u = t[r + 1], x = t[r + 2], a = t[r + 3], (u & 192) === 128 && (x & 192) === 128 && (a & 192) === 128 && (o = (s & 15) << 18 | (u & 63) << 12 | (x & 63) << 6 | a & 63, (o <= 65535 || o >= 1114112) && (o = null))), o === null ? (o = 65533, f = 1) : o > 65535 && (o -= 65536, n += String.fromCharCode(o >>> 10 & 1023 | 55296), o = 56320 | o & 1023), n += String.fromCharCode(o), r += f; } return n; } -function rt(t, e, i) { +function st(t, e, i) { return I.decode(t.subarray(e, i)); } -function nt(t, e, i) { +function ot(t, e, i) { for (var n = 0, r, s; n < e.length; n++) { if (r = e.charCodeAt(n), r > 55295 && r < 57344) if (s) @@ -384,16 +384,16 @@ function nt(t, e, i) { } return i; } -const b = /* @__PURE__ */ U($), R = 1; -var st = /* @__PURE__ */ ((t) => (t[t.Boolean = 1] = "Boolean", t[t.Number = 2] = "Number", t[t.String = 3] = "String", t[t.Object = 4] = "Object", t[t.Array = 5] = "Array", t[t.Null = 6] = "Null", t))(st || {}), ot = /* @__PURE__ */ ((t) => (t[t.Generic = 1] = "Generic", t[t.Columnar = 2] = "Columnar", t[t.Row = 3] = "Row", t))(ot || {}); -function ht(t, e) { +const G = /* @__PURE__ */ H(U), $ = 1; +var ht = /* @__PURE__ */ ((t) => (t[t.Boolean = 1] = "Boolean", t[t.Number = 2] = "Number", t[t.String = 3] = "String", t[t.Object = 4] = "Object", t[t.Array = 5] = "Array", t[t.Null = 6] = "Null", t))(ht || {}), ft = /* @__PURE__ */ ((t) => (t[t.Generic = 1] = "Generic", t[t.Columnar = 2] = "Columnar", t[t.Row = 3] = "Row", t))(ft || {}), at = /* @__PURE__ */ ((t) => (t[t.Generic = 1] = "Generic", t[t.Columnar = 2] = "Columnar", t[t.Row = 3] = "Row", t))(at || {}); +function ut(t, e) { let i = t.stringMap[e]; return i === void 0 && (i = t.strings.length, t.strings.push(e), t.stringMap[e] = i), i; } -function ft(t, e, i) { +function dt(t, e, i) { t.writeVarint(e << 3 | i); } -const C = { +const P = { 1: ({ pbf: t }, e) => { t.writeBoolean(e); }, @@ -408,37 +408,37 @@ const C = { return; const { pbf: i } = t, n = Object.keys(e); i.writeVarint(n.length), n.forEach((r) => { - P(e[r], t, ht(t, r)); + v(e[r], t, ut(t, r)); }); }, 5: (t, e) => { const { pbf: i } = t, n = e.length; i.writeVarint(n); for (let r = 0; r < n; r++) - P(e[r], t); + v(e[r], t); }, 6: (t, e) => { } -}, E = { +}, m = { 1: ({ pbf: t }) => t.readBoolean(), 2: ({ pbf: t }) => t.readDouble(), 3: ({ pbf: t }) => t.readString(), 5: (t) => { const { pbf: e } = t, i = e.readVarint(), n = new Array(i); for (let r = 0; r < i; r++) - n[r] = m(t, e.readVarint()); + n[r] = T(t, e.readVarint()); return n; }, 4: (t) => { const { pbf: e } = t, i = e.readVarint(), n = {}; for (let s = 0; s < i; s++) { var r = e.readVarint(); - n[t.keys[r >> 3]] = m(t, r & 7); + n[t.keys[r >> 3]] = T(t, r & 7); } return n; }, 6: (t) => null -}, at = { +}, xt = { boolean: 1, number: 2, string: 3, @@ -447,31 +447,31 @@ const C = { null: 6 /* Null */ }; -function P(t, e, i = 0) { +function v(t, e, i = 0) { const { pbf: n } = e; let r = typeof t; t === null && (r = "null"), Array.isArray(t) && (r = "array"); - const s = at[r], o = C[s]; + const s = xt[r], o = P[s]; if (!o) throw new Error(`Type ${typeof t} is not supported`); - return ft(n, i, s), o(e, t), n; + return dt(n, i, s), o(e, t), n; } -function m(t, e) { +function T(t, e) { const i = t.pbf; if (i.pos < i.length) { - const n = E[e]; + const n = m[e]; if (!n) throw new Error(`Type ${e} is not supported`); return n(t); } } -function O(t, e) { +function R(t, e) { const i = Object.keys(t).length; e.writeVarint(i); for (const n in t) e.writeString(n), e.writeVarint(t[n]); } -function _(t) { +function b(t) { const e = t.readVarint(), i = []; for (let n = 0; n < e; n++) { const r = t.readString(), s = t.readVarint(); @@ -479,44 +479,44 @@ function _(t) { } return i; } -function ut(t, e) { - var a; - const i = (e == null ? void 0 : e.pbf) ?? new b(), n = (e == null ? void 0 : e.method) ?? 1, r = e == null ? void 0 : e.columns; - i.writeFixed32(R << 24 | n << 16); +function ct(t, e) { + var u; + const i = (e == null ? void 0 : e.pbf) ?? new G(), n = (e == null ? void 0 : e.method) ?? 1, r = e == null ? void 0 : e.columns; + i.writeFixed32($ << 24 | n << 16); const s = [], f = { pbf: i, strings: s, stringMap: {} }; switch (n) { case 1: const x = i.pos; - i.writeFixed32(0), P(t, f); - const u = i.pos; - i.pos = x, i.writeFixed32(u), i.pos = u, i.writeVarint(s.length); - for (const c of s) - i.writeString(c); + i.writeFixed32(0), v(t, f); + const a = i.pos; + i.pos = x, i.writeFixed32(a), i.pos = a, i.writeVarint(s.length); + for (const w of s) + i.writeString(w); break; case 2: if (!r) throw new Error("No columns"); - const d = Object.keys(t)[0], l = (a = t == null ? void 0 : t[d]) == null ? void 0 : a.length; - if (!l) + const c = Object.keys(t)[0], F = (u = t == null ? void 0 : t[c]) == null ? void 0 : u.length; + if (!F) throw new Error("Cannot determine columnar data length"); - i.writeFixed32(l), O(r, i); - for (const c in r) { - const F = r[c], y = C[F], V = t[c]; - for (const g of V) - y(f, g); + i.writeFixed32(F), R(r, i); + for (const w in r) { + const g = r[w], p = P[g], l = t[w]; + for (const S of l) + p(f, S); } break; case 3: if (!r) throw new Error("No columns"); - const w = t == null ? void 0 : t.length; - if (!w) + const d = t == null ? void 0 : t.length; + if (!d) throw new Error("Cannot determine columnar data length"); - i.writeFixed32(w), O(r, i); - for (const c in r) { - const F = r[c], y = C[F]; - for (const V of t) - y(f, V[c]); + i.writeFixed32(d), R(r, i); + for (const w in r) { + const g = r[w], p = P[g]; + for (const l of t) + p(f, l[w]); } break; default: @@ -524,54 +524,57 @@ function ut(t, e) { } return i.buf.slice(0, i.pos).buffer; } -function dt(t) { +function wt(t, e) { if (!t || !t.byteLength || t.byteLength < 4) throw new Error("Bad array or insufficient array length."); - const e = new b(t), i = e.readFixed32(), n = i >> 24 & 255; - if (n > R) - throw new Error(`Version ${n} is not supported`); - const r = i >> 16 & 255; - switch (r) { + const i = new G(t), n = i.readFixed32(), r = n >> 24 & 255; + if (r > $) + throw new Error(`Version ${r} is not supported`); + const s = n >> 16 & 255, o = (e == null ? void 0 : e.method) ?? 1; + if (s === 1 && o !== 1) + throw new Error("Packed with PackMethod.Generic cannot be unpacked with anything than UnpackMethod.Generic"); + switch (o) { case 1: - const s = e.readFixed32(); - if (s >= t.byteLength) + const f = i.readFixed32(); + if (f >= t.byteLength) return; - const o = e.pos; - e.pos = s; - const f = e.readVarint(), a = new Array(f); - for (let d = 0; d < f; d++) - a[d] = e.readString(); - e.pos = o; - const x = e.readVarint(); - return m({ keys: a, pbf: e }, x); + const u = i.pos; + i.pos = f; + const x = i.readVarint(), a = new Array(x); + for (let d = 0; d < x; d++) + a[d] = i.readString(); + i.pos = u; + const c = i.readVarint(); + return T({ keys: a, pbf: i }, c); case 2: { - const d = { keys: [], pbf: e }, l = e.readFixed32(), w = _(e), c = {}; - for (const { key: F, type: y } of w) { - const V = E[y], g = []; - for (let T = 0; T < l; T++) - g.push(V(d)); - c[F] = g; + const d = { keys: [], pbf: i }, w = i.readFixed32(), g = b(i), p = {}; + for (const { key: l, type: S } of g) { + const C = m[S], V = []; + for (let j = 0; j < w; j++) + V.push(C(d)); + p[l] = V; } - return c; + return p; } case 3: { - const d = { keys: [], pbf: e }, l = e.readFixed32(), w = _(e), c = new Array(l); - for (let F = 0; F < l; F++) - c[F] = {}; - for (const { key: F, type: y } of w) { - const V = E[y]; - for (let g = 0; g < l; g++) - c[g][F] = V(d); + const d = { keys: [], pbf: i }, w = i.readFixed32(), g = b(i), p = new Array(w); + for (let l = 0; l < w; l++) + p[l] = {}; + for (const { key: l, type: S } of g) { + const C = m[S]; + for (let V = 0; V < w; V++) + p[V][l] = C(d); } - return c; + return p; } default: - throw new Error(`Method ${r} is not supported`); + throw new Error(`Method ${o} is not supported`); } } export { - st as JsonType, - ot as PackMethod, - ut as pack, - dt as unpack + ht as JsonType, + ft as PackMethod, + at as UnpackMethod, + ct as packJson, + wt as unpackJson }; diff --git a/package.json b/package.json index 1eba1f5..3019072 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json2pbf", - "version": "1.0.1", + "version": "1.0.2", "description": "Simple JSON <-> protobuf codec", "module": "./dist/lib.mjs", "types": "./dist/lib.d.ts", diff --git a/src/json2pbf.ts b/src/json2pbf.ts index 9c54644..71e1076 100644 --- a/src/json2pbf.ts +++ b/src/json2pbf.ts @@ -17,6 +17,12 @@ export enum PackMethod { Row = 3 } +export enum UnpackMethod { + Generic = 1, + Columnar = 2, + Row = 3, +} + interface PackCtx { pbf: typeof Pbf; stringMap: { [str: string]: number }; @@ -132,47 +138,8 @@ function fromPbf(ctx: UnpackCtx, type: JsonType) { throw new Error(`Type ${type} is not supported`); } return decoder(ctx); - - // switch (type) { - // case JsonType.Array: { - // const len = pbf.readVarint(); - // const arr = new Array(len); - // for (let i = 0; i < len; i++) { - // arr[i] = fromPbf(ctx, pbf.readVarint()); - // } - // return arr; - // } - // case JsonType.Boolean: { - // return pbf.readBoolean(); - // } - // case JsonType.Null: { - // return null; - // } - // case JsonType.Number: { - // return pbf.readDouble(); - // } - // case JsonType.Object: { - // const len = pbf.readVarint(); - // const obj = {}; - // for (let i = 0; i < len; i++) { - // var val = pbf.readVarint(); - // obj[ctx.keys[val >> 3]] = fromPbf(ctx, val & 0x7); - // } - // return obj; - // } - // case JsonType.String: { - // return pbf.readString(); - // } - // default: - // return undefined; - // } } } -export interface PackOptions { - pbf?: typeof Pbf, - method?: PackMethod, - columns?: Record -} function writeColumns(columns: Record, pbf: typeof Pbf) { const length = Object.keys(columns).length; pbf.writeVarint(length); @@ -193,7 +160,13 @@ function readColumns(pbf: typeof Pbf) { return columns; } -export function pack(val: any, options?: PackOptions): ArrayBuffer { +export interface PackOptions { + pbf?: typeof Pbf, + method?: PackMethod, + columns?: Record +} + +export function packJson(val: any, options?: PackOptions): ArrayBuffer { const pbf = options?.pbf ?? new Pbf(); const method = options?.method ?? PackMethod.Generic; const columns = options?.columns; @@ -265,7 +238,11 @@ export function pack(val: any, options?: PackOptions): ArrayBuffer { return pbf.buf.slice(0, pbf.pos).buffer; } -export function unpack(arr: ArrayBuffer) { +export interface UnpackOptions { + method?: UnpackMethod, +} + +export function unpackJson(arr: ArrayBuffer, options?: UnpackOptions) { if (!arr || !arr.byteLength || arr.byteLength < 4) { throw new Error('Bad array or insufficient array length.') } @@ -276,9 +253,15 @@ export function unpack(arr: ArrayBuffer) { if (version > VERSION) { throw new Error(`Version ${version} is not supported`); } - const method = (header >> 16) & 0xff; - switch (method) { - case PackMethod.Generic: + const packMethod: PackMethod = (header >> 16) & 0xff; + const unpackMethod: UnpackMethod = options?.method ?? UnpackMethod.Generic; + + if (packMethod === PackMethod.Generic && unpackMethod !== UnpackMethod.Generic) { + throw new Error('Packed with PackMethod.Generic cannot be unpacked with anything than UnpackMethod.Generic'); + } + + switch (unpackMethod) { + case UnpackMethod.Generic: const keysOffset = pbf.readFixed32(); if (keysOffset >= arr.byteLength) { return; @@ -296,7 +279,7 @@ export function unpack(arr: ArrayBuffer) { const startType = pbf.readVarint(); const ctx: UnpackCtx = { keys, pbf }; return fromPbf(ctx, startType); - case PackMethod.Columnar: { + case UnpackMethod.Columnar: { const ctx: UnpackCtx = { keys: [], pbf }; const len = pbf.readFixed32(); const columns = readColumns(pbf); @@ -311,7 +294,7 @@ export function unpack(arr: ArrayBuffer) { } return result; } - case PackMethod.Row: { + case UnpackMethod.Row: { const ctx: UnpackCtx = { keys: [], pbf }; const len = pbf.readFixed32(); const columns = readColumns(pbf); @@ -328,8 +311,6 @@ export function unpack(arr: ArrayBuffer) { return result; } default: - throw new Error(`Method ${method} is not supported`); - + throw new Error(`Method ${unpackMethod} is not supported`); } - }