mapshaper 0.6.16 → 0.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mapshaper.js +1934 -92
- package/package.json +95 -87
- package/www/mapshaper-gui.js +11 -9
- package/www/mapshaper.js +1934 -92
- package/www/modules.js +2 -7543
package/mapshaper.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
(function () {
|
|
2
2
|
|
|
3
|
-
var VERSION = "0.6.
|
|
3
|
+
var VERSION = "0.6.18";
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
var utils = /*#__PURE__*/Object.freeze({
|
|
@@ -7154,6 +7154,1907 @@
|
|
|
7154
7154
|
getOutputFileBase: getOutputFileBase
|
|
7155
7155
|
});
|
|
7156
7156
|
|
|
7157
|
+
// Integer Utility
|
|
7158
|
+
var UINT32_MAX = 4294967295;
|
|
7159
|
+
// DataView extension to handle int64 / uint64,
|
|
7160
|
+
// where the actual range is 53-bits integer (a.k.a. safe integer)
|
|
7161
|
+
function setUint64(view, offset, value) {
|
|
7162
|
+
var high = value / 4294967296;
|
|
7163
|
+
var low = value; // high bits are truncated by DataView
|
|
7164
|
+
view.setUint32(offset, high);
|
|
7165
|
+
view.setUint32(offset + 4, low);
|
|
7166
|
+
}
|
|
7167
|
+
function setInt64(view, offset, value) {
|
|
7168
|
+
var high = Math.floor(value / 4294967296);
|
|
7169
|
+
var low = value; // high bits are truncated by DataView
|
|
7170
|
+
view.setUint32(offset, high);
|
|
7171
|
+
view.setUint32(offset + 4, low);
|
|
7172
|
+
}
|
|
7173
|
+
function getInt64(view, offset) {
|
|
7174
|
+
var high = view.getInt32(offset);
|
|
7175
|
+
var low = view.getUint32(offset + 4);
|
|
7176
|
+
return high * 4294967296 + low;
|
|
7177
|
+
}
|
|
7178
|
+
function getUint64(view, offset) {
|
|
7179
|
+
var high = view.getUint32(offset);
|
|
7180
|
+
var low = view.getUint32(offset + 4);
|
|
7181
|
+
return high * 4294967296 + low;
|
|
7182
|
+
}
|
|
7183
|
+
|
|
7184
|
+
var _a$1, _b$1, _c;
|
|
7185
|
+
var TEXT_ENCODING_AVAILABLE = (typeof process === "undefined" || ((_a$1 = process === null || process === void 0 ? void 0 : process.env) === null || _a$1 === void 0 ? void 0 : _a$1["TEXT_ENCODING"]) !== "never") &&
|
|
7186
|
+
typeof TextEncoder !== "undefined" &&
|
|
7187
|
+
typeof TextDecoder !== "undefined";
|
|
7188
|
+
function utf8Count(str) {
|
|
7189
|
+
var strLength = str.length;
|
|
7190
|
+
var byteLength = 0;
|
|
7191
|
+
var pos = 0;
|
|
7192
|
+
while (pos < strLength) {
|
|
7193
|
+
var value = str.charCodeAt(pos++);
|
|
7194
|
+
if ((value & 0xffffff80) === 0) {
|
|
7195
|
+
// 1-byte
|
|
7196
|
+
byteLength++;
|
|
7197
|
+
continue;
|
|
7198
|
+
}
|
|
7199
|
+
else if ((value & 0xfffff800) === 0) {
|
|
7200
|
+
// 2-bytes
|
|
7201
|
+
byteLength += 2;
|
|
7202
|
+
}
|
|
7203
|
+
else {
|
|
7204
|
+
// handle surrogate pair
|
|
7205
|
+
if (value >= 0xd800 && value <= 0xdbff) {
|
|
7206
|
+
// high surrogate
|
|
7207
|
+
if (pos < strLength) {
|
|
7208
|
+
var extra = str.charCodeAt(pos);
|
|
7209
|
+
if ((extra & 0xfc00) === 0xdc00) {
|
|
7210
|
+
++pos;
|
|
7211
|
+
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
|
|
7212
|
+
}
|
|
7213
|
+
}
|
|
7214
|
+
}
|
|
7215
|
+
if ((value & 0xffff0000) === 0) {
|
|
7216
|
+
// 3-byte
|
|
7217
|
+
byteLength += 3;
|
|
7218
|
+
}
|
|
7219
|
+
else {
|
|
7220
|
+
// 4-byte
|
|
7221
|
+
byteLength += 4;
|
|
7222
|
+
}
|
|
7223
|
+
}
|
|
7224
|
+
}
|
|
7225
|
+
return byteLength;
|
|
7226
|
+
}
|
|
7227
|
+
function utf8EncodeJs(str, output, outputOffset) {
|
|
7228
|
+
var strLength = str.length;
|
|
7229
|
+
var offset = outputOffset;
|
|
7230
|
+
var pos = 0;
|
|
7231
|
+
while (pos < strLength) {
|
|
7232
|
+
var value = str.charCodeAt(pos++);
|
|
7233
|
+
if ((value & 0xffffff80) === 0) {
|
|
7234
|
+
// 1-byte
|
|
7235
|
+
output[offset++] = value;
|
|
7236
|
+
continue;
|
|
7237
|
+
}
|
|
7238
|
+
else if ((value & 0xfffff800) === 0) {
|
|
7239
|
+
// 2-bytes
|
|
7240
|
+
output[offset++] = ((value >> 6) & 0x1f) | 0xc0;
|
|
7241
|
+
}
|
|
7242
|
+
else {
|
|
7243
|
+
// handle surrogate pair
|
|
7244
|
+
if (value >= 0xd800 && value <= 0xdbff) {
|
|
7245
|
+
// high surrogate
|
|
7246
|
+
if (pos < strLength) {
|
|
7247
|
+
var extra = str.charCodeAt(pos);
|
|
7248
|
+
if ((extra & 0xfc00) === 0xdc00) {
|
|
7249
|
+
++pos;
|
|
7250
|
+
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
|
|
7251
|
+
}
|
|
7252
|
+
}
|
|
7253
|
+
}
|
|
7254
|
+
if ((value & 0xffff0000) === 0) {
|
|
7255
|
+
// 3-byte
|
|
7256
|
+
output[offset++] = ((value >> 12) & 0x0f) | 0xe0;
|
|
7257
|
+
output[offset++] = ((value >> 6) & 0x3f) | 0x80;
|
|
7258
|
+
}
|
|
7259
|
+
else {
|
|
7260
|
+
// 4-byte
|
|
7261
|
+
output[offset++] = ((value >> 18) & 0x07) | 0xf0;
|
|
7262
|
+
output[offset++] = ((value >> 12) & 0x3f) | 0x80;
|
|
7263
|
+
output[offset++] = ((value >> 6) & 0x3f) | 0x80;
|
|
7264
|
+
}
|
|
7265
|
+
}
|
|
7266
|
+
output[offset++] = (value & 0x3f) | 0x80;
|
|
7267
|
+
}
|
|
7268
|
+
}
|
|
7269
|
+
var sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined;
|
|
7270
|
+
var TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
|
|
7271
|
+
? UINT32_MAX
|
|
7272
|
+
: typeof process !== "undefined" && ((_b$1 = process === null || process === void 0 ? void 0 : process.env) === null || _b$1 === void 0 ? void 0 : _b$1["TEXT_ENCODING"]) !== "force"
|
|
7273
|
+
? 200
|
|
7274
|
+
: 0;
|
|
7275
|
+
function utf8EncodeTEencode(str, output, outputOffset) {
|
|
7276
|
+
output.set(sharedTextEncoder.encode(str), outputOffset);
|
|
7277
|
+
}
|
|
7278
|
+
function utf8EncodeTEencodeInto(str, output, outputOffset) {
|
|
7279
|
+
sharedTextEncoder.encodeInto(str, output.subarray(outputOffset));
|
|
7280
|
+
}
|
|
7281
|
+
var utf8EncodeTE = (sharedTextEncoder === null || sharedTextEncoder === void 0 ? void 0 : sharedTextEncoder.encodeInto) ? utf8EncodeTEencodeInto : utf8EncodeTEencode;
|
|
7282
|
+
var CHUNK_SIZE = 4096;
|
|
7283
|
+
function utf8DecodeJs(bytes, inputOffset, byteLength) {
|
|
7284
|
+
var offset = inputOffset;
|
|
7285
|
+
var end = offset + byteLength;
|
|
7286
|
+
var units = [];
|
|
7287
|
+
var result = "";
|
|
7288
|
+
while (offset < end) {
|
|
7289
|
+
var byte1 = bytes[offset++];
|
|
7290
|
+
if ((byte1 & 0x80) === 0) {
|
|
7291
|
+
// 1 byte
|
|
7292
|
+
units.push(byte1);
|
|
7293
|
+
}
|
|
7294
|
+
else if ((byte1 & 0xe0) === 0xc0) {
|
|
7295
|
+
// 2 bytes
|
|
7296
|
+
var byte2 = bytes[offset++] & 0x3f;
|
|
7297
|
+
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
7298
|
+
}
|
|
7299
|
+
else if ((byte1 & 0xf0) === 0xe0) {
|
|
7300
|
+
// 3 bytes
|
|
7301
|
+
var byte2 = bytes[offset++] & 0x3f;
|
|
7302
|
+
var byte3 = bytes[offset++] & 0x3f;
|
|
7303
|
+
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
7304
|
+
}
|
|
7305
|
+
else if ((byte1 & 0xf8) === 0xf0) {
|
|
7306
|
+
// 4 bytes
|
|
7307
|
+
var byte2 = bytes[offset++] & 0x3f;
|
|
7308
|
+
var byte3 = bytes[offset++] & 0x3f;
|
|
7309
|
+
var byte4 = bytes[offset++] & 0x3f;
|
|
7310
|
+
var unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
7311
|
+
if (unit > 0xffff) {
|
|
7312
|
+
unit -= 0x10000;
|
|
7313
|
+
units.push(((unit >>> 10) & 0x3ff) | 0xd800);
|
|
7314
|
+
unit = 0xdc00 | (unit & 0x3ff);
|
|
7315
|
+
}
|
|
7316
|
+
units.push(unit);
|
|
7317
|
+
}
|
|
7318
|
+
else {
|
|
7319
|
+
units.push(byte1);
|
|
7320
|
+
}
|
|
7321
|
+
if (units.length >= CHUNK_SIZE) {
|
|
7322
|
+
result += String.fromCharCode.apply(String, units);
|
|
7323
|
+
units.length = 0;
|
|
7324
|
+
}
|
|
7325
|
+
}
|
|
7326
|
+
if (units.length > 0) {
|
|
7327
|
+
result += String.fromCharCode.apply(String, units);
|
|
7328
|
+
}
|
|
7329
|
+
return result;
|
|
7330
|
+
}
|
|
7331
|
+
var sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null;
|
|
7332
|
+
var TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
|
|
7333
|
+
? UINT32_MAX
|
|
7334
|
+
: typeof process !== "undefined" && ((_c = process === null || process === void 0 ? void 0 : process.env) === null || _c === void 0 ? void 0 : _c["TEXT_DECODER"]) !== "force"
|
|
7335
|
+
? 200
|
|
7336
|
+
: 0;
|
|
7337
|
+
function utf8DecodeTD(bytes, inputOffset, byteLength) {
|
|
7338
|
+
var stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
|
|
7339
|
+
return sharedTextDecoder.decode(stringBytes);
|
|
7340
|
+
}
|
|
7341
|
+
|
|
7342
|
+
/**
|
|
7343
|
+
* ExtData is used to handle Extension Types that are not registered to ExtensionCodec.
|
|
7344
|
+
*/
|
|
7345
|
+
var ExtData = /** @class */ (function () {
|
|
7346
|
+
function ExtData(type, data) {
|
|
7347
|
+
this.type = type;
|
|
7348
|
+
this.data = data;
|
|
7349
|
+
}
|
|
7350
|
+
return ExtData;
|
|
7351
|
+
}());
|
|
7352
|
+
|
|
7353
|
+
var __extends = (null && null.__extends) || (function () {
|
|
7354
|
+
var extendStatics = function (d, b) {
|
|
7355
|
+
extendStatics = Object.setPrototypeOf ||
|
|
7356
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
7357
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7358
|
+
return extendStatics(d, b);
|
|
7359
|
+
};
|
|
7360
|
+
return function (d, b) {
|
|
7361
|
+
if (typeof b !== "function" && b !== null)
|
|
7362
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
7363
|
+
extendStatics(d, b);
|
|
7364
|
+
function __() { this.constructor = d; }
|
|
7365
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
7366
|
+
};
|
|
7367
|
+
})();
|
|
7368
|
+
var DecodeError = /** @class */ (function (_super) {
|
|
7369
|
+
__extends(DecodeError, _super);
|
|
7370
|
+
function DecodeError(message) {
|
|
7371
|
+
var _this = _super.call(this, message) || this;
|
|
7372
|
+
// fix the prototype chain in a cross-platform way
|
|
7373
|
+
var proto = Object.create(DecodeError.prototype);
|
|
7374
|
+
Object.setPrototypeOf(_this, proto);
|
|
7375
|
+
Object.defineProperty(_this, "name", {
|
|
7376
|
+
configurable: true,
|
|
7377
|
+
enumerable: false,
|
|
7378
|
+
value: DecodeError.name,
|
|
7379
|
+
});
|
|
7380
|
+
return _this;
|
|
7381
|
+
}
|
|
7382
|
+
return DecodeError;
|
|
7383
|
+
}(Error));
|
|
7384
|
+
|
|
7385
|
+
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
|
|
7386
|
+
var EXT_TIMESTAMP = -1;
|
|
7387
|
+
var TIMESTAMP32_MAX_SEC = 0x100000000 - 1; // 32-bit unsigned int
|
|
7388
|
+
var TIMESTAMP64_MAX_SEC = 0x400000000 - 1; // 34-bit unsigned int
|
|
7389
|
+
function encodeTimeSpecToTimestamp(_a) {
|
|
7390
|
+
var sec = _a.sec, nsec = _a.nsec;
|
|
7391
|
+
if (sec >= 0 && nsec >= 0 && sec <= TIMESTAMP64_MAX_SEC) {
|
|
7392
|
+
// Here sec >= 0 && nsec >= 0
|
|
7393
|
+
if (nsec === 0 && sec <= TIMESTAMP32_MAX_SEC) {
|
|
7394
|
+
// timestamp 32 = { sec32 (unsigned) }
|
|
7395
|
+
var rv = new Uint8Array(4);
|
|
7396
|
+
var view = new DataView(rv.buffer);
|
|
7397
|
+
view.setUint32(0, sec);
|
|
7398
|
+
return rv;
|
|
7399
|
+
}
|
|
7400
|
+
else {
|
|
7401
|
+
// timestamp 64 = { nsec30 (unsigned), sec34 (unsigned) }
|
|
7402
|
+
var secHigh = sec / 0x100000000;
|
|
7403
|
+
var secLow = sec & 0xffffffff;
|
|
7404
|
+
var rv = new Uint8Array(8);
|
|
7405
|
+
var view = new DataView(rv.buffer);
|
|
7406
|
+
// nsec30 | secHigh2
|
|
7407
|
+
view.setUint32(0, (nsec << 2) | (secHigh & 0x3));
|
|
7408
|
+
// secLow32
|
|
7409
|
+
view.setUint32(4, secLow);
|
|
7410
|
+
return rv;
|
|
7411
|
+
}
|
|
7412
|
+
}
|
|
7413
|
+
else {
|
|
7414
|
+
// timestamp 96 = { nsec32 (unsigned), sec64 (signed) }
|
|
7415
|
+
var rv = new Uint8Array(12);
|
|
7416
|
+
var view = new DataView(rv.buffer);
|
|
7417
|
+
view.setUint32(0, nsec);
|
|
7418
|
+
setInt64(view, 4, sec);
|
|
7419
|
+
return rv;
|
|
7420
|
+
}
|
|
7421
|
+
}
|
|
7422
|
+
function encodeDateToTimeSpec(date) {
|
|
7423
|
+
var msec = date.getTime();
|
|
7424
|
+
var sec = Math.floor(msec / 1e3);
|
|
7425
|
+
var nsec = (msec - sec * 1e3) * 1e6;
|
|
7426
|
+
// Normalizes { sec, nsec } to ensure nsec is unsigned.
|
|
7427
|
+
var nsecInSec = Math.floor(nsec / 1e9);
|
|
7428
|
+
return {
|
|
7429
|
+
sec: sec + nsecInSec,
|
|
7430
|
+
nsec: nsec - nsecInSec * 1e9,
|
|
7431
|
+
};
|
|
7432
|
+
}
|
|
7433
|
+
function encodeTimestampExtension(object) {
|
|
7434
|
+
if (object instanceof Date) {
|
|
7435
|
+
var timeSpec = encodeDateToTimeSpec(object);
|
|
7436
|
+
return encodeTimeSpecToTimestamp(timeSpec);
|
|
7437
|
+
}
|
|
7438
|
+
else {
|
|
7439
|
+
return null;
|
|
7440
|
+
}
|
|
7441
|
+
}
|
|
7442
|
+
function decodeTimestampToTimeSpec(data) {
|
|
7443
|
+
var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
7444
|
+
// data may be 32, 64, or 96 bits
|
|
7445
|
+
switch (data.byteLength) {
|
|
7446
|
+
case 4: {
|
|
7447
|
+
// timestamp 32 = { sec32 }
|
|
7448
|
+
var sec = view.getUint32(0);
|
|
7449
|
+
var nsec = 0;
|
|
7450
|
+
return { sec: sec, nsec: nsec };
|
|
7451
|
+
}
|
|
7452
|
+
case 8: {
|
|
7453
|
+
// timestamp 64 = { nsec30, sec34 }
|
|
7454
|
+
var nsec30AndSecHigh2 = view.getUint32(0);
|
|
7455
|
+
var secLow32 = view.getUint32(4);
|
|
7456
|
+
var sec = (nsec30AndSecHigh2 & 0x3) * 0x100000000 + secLow32;
|
|
7457
|
+
var nsec = nsec30AndSecHigh2 >>> 2;
|
|
7458
|
+
return { sec: sec, nsec: nsec };
|
|
7459
|
+
}
|
|
7460
|
+
case 12: {
|
|
7461
|
+
// timestamp 96 = { nsec32 (unsigned), sec64 (signed) }
|
|
7462
|
+
var sec = getInt64(view, 4);
|
|
7463
|
+
var nsec = view.getUint32(0);
|
|
7464
|
+
return { sec: sec, nsec: nsec };
|
|
7465
|
+
}
|
|
7466
|
+
default:
|
|
7467
|
+
throw new DecodeError("Unrecognized data size for timestamp (expected 4, 8, or 12): ".concat(data.length));
|
|
7468
|
+
}
|
|
7469
|
+
}
|
|
7470
|
+
function decodeTimestampExtension(data) {
|
|
7471
|
+
var timeSpec = decodeTimestampToTimeSpec(data);
|
|
7472
|
+
return new Date(timeSpec.sec * 1e3 + timeSpec.nsec / 1e6);
|
|
7473
|
+
}
|
|
7474
|
+
var timestampExtension = {
|
|
7475
|
+
type: EXT_TIMESTAMP,
|
|
7476
|
+
encode: encodeTimestampExtension,
|
|
7477
|
+
decode: decodeTimestampExtension,
|
|
7478
|
+
};
|
|
7479
|
+
|
|
7480
|
+
// ExtensionCodec to handle MessagePack extensions
|
|
7481
|
+
var ExtensionCodec = /** @class */ (function () {
|
|
7482
|
+
function ExtensionCodec() {
|
|
7483
|
+
// built-in extensions
|
|
7484
|
+
this.builtInEncoders = [];
|
|
7485
|
+
this.builtInDecoders = [];
|
|
7486
|
+
// custom extensions
|
|
7487
|
+
this.encoders = [];
|
|
7488
|
+
this.decoders = [];
|
|
7489
|
+
this.register(timestampExtension);
|
|
7490
|
+
}
|
|
7491
|
+
ExtensionCodec.prototype.register = function (_a) {
|
|
7492
|
+
var type = _a.type, encode = _a.encode, decode = _a.decode;
|
|
7493
|
+
if (type >= 0) {
|
|
7494
|
+
// custom extensions
|
|
7495
|
+
this.encoders[type] = encode;
|
|
7496
|
+
this.decoders[type] = decode;
|
|
7497
|
+
}
|
|
7498
|
+
else {
|
|
7499
|
+
// built-in extensions
|
|
7500
|
+
var index = 1 + type;
|
|
7501
|
+
this.builtInEncoders[index] = encode;
|
|
7502
|
+
this.builtInDecoders[index] = decode;
|
|
7503
|
+
}
|
|
7504
|
+
};
|
|
7505
|
+
ExtensionCodec.prototype.tryToEncode = function (object, context) {
|
|
7506
|
+
// built-in extensions
|
|
7507
|
+
for (var i = 0; i < this.builtInEncoders.length; i++) {
|
|
7508
|
+
var encodeExt = this.builtInEncoders[i];
|
|
7509
|
+
if (encodeExt != null) {
|
|
7510
|
+
var data = encodeExt(object, context);
|
|
7511
|
+
if (data != null) {
|
|
7512
|
+
var type = -1 - i;
|
|
7513
|
+
return new ExtData(type, data);
|
|
7514
|
+
}
|
|
7515
|
+
}
|
|
7516
|
+
}
|
|
7517
|
+
// custom extensions
|
|
7518
|
+
for (var i = 0; i < this.encoders.length; i++) {
|
|
7519
|
+
var encodeExt = this.encoders[i];
|
|
7520
|
+
if (encodeExt != null) {
|
|
7521
|
+
var data = encodeExt(object, context);
|
|
7522
|
+
if (data != null) {
|
|
7523
|
+
var type = i;
|
|
7524
|
+
return new ExtData(type, data);
|
|
7525
|
+
}
|
|
7526
|
+
}
|
|
7527
|
+
}
|
|
7528
|
+
if (object instanceof ExtData) {
|
|
7529
|
+
// to keep ExtData as is
|
|
7530
|
+
return object;
|
|
7531
|
+
}
|
|
7532
|
+
return null;
|
|
7533
|
+
};
|
|
7534
|
+
ExtensionCodec.prototype.decode = function (data, type, context) {
|
|
7535
|
+
var decodeExt = type < 0 ? this.builtInDecoders[-1 - type] : this.decoders[type];
|
|
7536
|
+
if (decodeExt) {
|
|
7537
|
+
return decodeExt(data, type, context);
|
|
7538
|
+
}
|
|
7539
|
+
else {
|
|
7540
|
+
// decode() does not fail, returns ExtData instead.
|
|
7541
|
+
return new ExtData(type, data);
|
|
7542
|
+
}
|
|
7543
|
+
};
|
|
7544
|
+
ExtensionCodec.defaultCodec = new ExtensionCodec();
|
|
7545
|
+
return ExtensionCodec;
|
|
7546
|
+
}());
|
|
7547
|
+
|
|
7548
|
+
function ensureUint8Array(buffer) {
|
|
7549
|
+
if (buffer instanceof Uint8Array) {
|
|
7550
|
+
return buffer;
|
|
7551
|
+
}
|
|
7552
|
+
else if (ArrayBuffer.isView(buffer)) {
|
|
7553
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
7554
|
+
}
|
|
7555
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
7556
|
+
return new Uint8Array(buffer);
|
|
7557
|
+
}
|
|
7558
|
+
else {
|
|
7559
|
+
// ArrayLike<number>
|
|
7560
|
+
return Uint8Array.from(buffer);
|
|
7561
|
+
}
|
|
7562
|
+
}
|
|
7563
|
+
function createDataView(buffer) {
|
|
7564
|
+
if (buffer instanceof ArrayBuffer) {
|
|
7565
|
+
return new DataView(buffer);
|
|
7566
|
+
}
|
|
7567
|
+
var bufferView = ensureUint8Array(buffer);
|
|
7568
|
+
return new DataView(bufferView.buffer, bufferView.byteOffset, bufferView.byteLength);
|
|
7569
|
+
}
|
|
7570
|
+
|
|
7571
|
+
var DEFAULT_MAX_DEPTH = 100;
|
|
7572
|
+
var DEFAULT_INITIAL_BUFFER_SIZE = 2048;
|
|
7573
|
+
var Encoder = /** @class */ (function () {
|
|
7574
|
+
function Encoder(extensionCodec, context, maxDepth, initialBufferSize, sortKeys, forceFloat32, ignoreUndefined, forceIntegerToFloat) {
|
|
7575
|
+
if (extensionCodec === void 0) { extensionCodec = ExtensionCodec.defaultCodec; }
|
|
7576
|
+
if (context === void 0) { context = undefined; }
|
|
7577
|
+
if (maxDepth === void 0) { maxDepth = DEFAULT_MAX_DEPTH; }
|
|
7578
|
+
if (initialBufferSize === void 0) { initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE; }
|
|
7579
|
+
if (sortKeys === void 0) { sortKeys = false; }
|
|
7580
|
+
if (forceFloat32 === void 0) { forceFloat32 = false; }
|
|
7581
|
+
if (ignoreUndefined === void 0) { ignoreUndefined = false; }
|
|
7582
|
+
if (forceIntegerToFloat === void 0) { forceIntegerToFloat = false; }
|
|
7583
|
+
this.extensionCodec = extensionCodec;
|
|
7584
|
+
this.context = context;
|
|
7585
|
+
this.maxDepth = maxDepth;
|
|
7586
|
+
this.initialBufferSize = initialBufferSize;
|
|
7587
|
+
this.sortKeys = sortKeys;
|
|
7588
|
+
this.forceFloat32 = forceFloat32;
|
|
7589
|
+
this.ignoreUndefined = ignoreUndefined;
|
|
7590
|
+
this.forceIntegerToFloat = forceIntegerToFloat;
|
|
7591
|
+
this.pos = 0;
|
|
7592
|
+
this.view = new DataView(new ArrayBuffer(this.initialBufferSize));
|
|
7593
|
+
this.bytes = new Uint8Array(this.view.buffer);
|
|
7594
|
+
}
|
|
7595
|
+
Encoder.prototype.reinitializeState = function () {
|
|
7596
|
+
this.pos = 0;
|
|
7597
|
+
};
|
|
7598
|
+
/**
|
|
7599
|
+
* This is almost equivalent to {@link Encoder#encode}, but it returns an reference of the encoder's internal buffer and thus much faster than {@link Encoder#encode}.
|
|
7600
|
+
*
|
|
7601
|
+
* @returns Encodes the object and returns a shared reference the encoder's internal buffer.
|
|
7602
|
+
*/
|
|
7603
|
+
Encoder.prototype.encodeSharedRef = function (object) {
|
|
7604
|
+
this.reinitializeState();
|
|
7605
|
+
this.doEncode(object, 1);
|
|
7606
|
+
return this.bytes.subarray(0, this.pos);
|
|
7607
|
+
};
|
|
7608
|
+
/**
|
|
7609
|
+
* @returns Encodes the object and returns a copy of the encoder's internal buffer.
|
|
7610
|
+
*/
|
|
7611
|
+
Encoder.prototype.encode = function (object) {
|
|
7612
|
+
this.reinitializeState();
|
|
7613
|
+
this.doEncode(object, 1);
|
|
7614
|
+
return this.bytes.slice(0, this.pos);
|
|
7615
|
+
};
|
|
7616
|
+
Encoder.prototype.doEncode = function (object, depth) {
|
|
7617
|
+
if (depth > this.maxDepth) {
|
|
7618
|
+
throw new Error("Too deep objects in depth ".concat(depth));
|
|
7619
|
+
}
|
|
7620
|
+
if (object == null) {
|
|
7621
|
+
this.encodeNil();
|
|
7622
|
+
}
|
|
7623
|
+
else if (typeof object === "boolean") {
|
|
7624
|
+
this.encodeBoolean(object);
|
|
7625
|
+
}
|
|
7626
|
+
else if (typeof object === "number") {
|
|
7627
|
+
this.encodeNumber(object);
|
|
7628
|
+
}
|
|
7629
|
+
else if (typeof object === "string") {
|
|
7630
|
+
this.encodeString(object);
|
|
7631
|
+
}
|
|
7632
|
+
else {
|
|
7633
|
+
this.encodeObject(object, depth);
|
|
7634
|
+
}
|
|
7635
|
+
};
|
|
7636
|
+
Encoder.prototype.ensureBufferSizeToWrite = function (sizeToWrite) {
|
|
7637
|
+
var requiredSize = this.pos + sizeToWrite;
|
|
7638
|
+
if (this.view.byteLength < requiredSize) {
|
|
7639
|
+
this.resizeBuffer(requiredSize * 2);
|
|
7640
|
+
}
|
|
7641
|
+
};
|
|
7642
|
+
Encoder.prototype.resizeBuffer = function (newSize) {
|
|
7643
|
+
var newBuffer = new ArrayBuffer(newSize);
|
|
7644
|
+
var newBytes = new Uint8Array(newBuffer);
|
|
7645
|
+
var newView = new DataView(newBuffer);
|
|
7646
|
+
newBytes.set(this.bytes);
|
|
7647
|
+
this.view = newView;
|
|
7648
|
+
this.bytes = newBytes;
|
|
7649
|
+
};
|
|
7650
|
+
Encoder.prototype.encodeNil = function () {
|
|
7651
|
+
this.writeU8(0xc0);
|
|
7652
|
+
};
|
|
7653
|
+
Encoder.prototype.encodeBoolean = function (object) {
|
|
7654
|
+
if (object === false) {
|
|
7655
|
+
this.writeU8(0xc2);
|
|
7656
|
+
}
|
|
7657
|
+
else {
|
|
7658
|
+
this.writeU8(0xc3);
|
|
7659
|
+
}
|
|
7660
|
+
};
|
|
7661
|
+
Encoder.prototype.encodeNumber = function (object) {
|
|
7662
|
+
if (Number.isSafeInteger(object) && !this.forceIntegerToFloat) {
|
|
7663
|
+
if (object >= 0) {
|
|
7664
|
+
if (object < 0x80) {
|
|
7665
|
+
// positive fixint
|
|
7666
|
+
this.writeU8(object);
|
|
7667
|
+
}
|
|
7668
|
+
else if (object < 0x100) {
|
|
7669
|
+
// uint 8
|
|
7670
|
+
this.writeU8(0xcc);
|
|
7671
|
+
this.writeU8(object);
|
|
7672
|
+
}
|
|
7673
|
+
else if (object < 0x10000) {
|
|
7674
|
+
// uint 16
|
|
7675
|
+
this.writeU8(0xcd);
|
|
7676
|
+
this.writeU16(object);
|
|
7677
|
+
}
|
|
7678
|
+
else if (object < 0x100000000) {
|
|
7679
|
+
// uint 32
|
|
7680
|
+
this.writeU8(0xce);
|
|
7681
|
+
this.writeU32(object);
|
|
7682
|
+
}
|
|
7683
|
+
else {
|
|
7684
|
+
// uint 64
|
|
7685
|
+
this.writeU8(0xcf);
|
|
7686
|
+
this.writeU64(object);
|
|
7687
|
+
}
|
|
7688
|
+
}
|
|
7689
|
+
else {
|
|
7690
|
+
if (object >= -0x20) {
|
|
7691
|
+
// negative fixint
|
|
7692
|
+
this.writeU8(0xe0 | (object + 0x20));
|
|
7693
|
+
}
|
|
7694
|
+
else if (object >= -0x80) {
|
|
7695
|
+
// int 8
|
|
7696
|
+
this.writeU8(0xd0);
|
|
7697
|
+
this.writeI8(object);
|
|
7698
|
+
}
|
|
7699
|
+
else if (object >= -0x8000) {
|
|
7700
|
+
// int 16
|
|
7701
|
+
this.writeU8(0xd1);
|
|
7702
|
+
this.writeI16(object);
|
|
7703
|
+
}
|
|
7704
|
+
else if (object >= -0x80000000) {
|
|
7705
|
+
// int 32
|
|
7706
|
+
this.writeU8(0xd2);
|
|
7707
|
+
this.writeI32(object);
|
|
7708
|
+
}
|
|
7709
|
+
else {
|
|
7710
|
+
// int 64
|
|
7711
|
+
this.writeU8(0xd3);
|
|
7712
|
+
this.writeI64(object);
|
|
7713
|
+
}
|
|
7714
|
+
}
|
|
7715
|
+
}
|
|
7716
|
+
else {
|
|
7717
|
+
// non-integer numbers
|
|
7718
|
+
if (this.forceFloat32) {
|
|
7719
|
+
// float 32
|
|
7720
|
+
this.writeU8(0xca);
|
|
7721
|
+
this.writeF32(object);
|
|
7722
|
+
}
|
|
7723
|
+
else {
|
|
7724
|
+
// float 64
|
|
7725
|
+
this.writeU8(0xcb);
|
|
7726
|
+
this.writeF64(object);
|
|
7727
|
+
}
|
|
7728
|
+
}
|
|
7729
|
+
};
|
|
7730
|
+
Encoder.prototype.writeStringHeader = function (byteLength) {
|
|
7731
|
+
if (byteLength < 32) {
|
|
7732
|
+
// fixstr
|
|
7733
|
+
this.writeU8(0xa0 + byteLength);
|
|
7734
|
+
}
|
|
7735
|
+
else if (byteLength < 0x100) {
|
|
7736
|
+
// str 8
|
|
7737
|
+
this.writeU8(0xd9);
|
|
7738
|
+
this.writeU8(byteLength);
|
|
7739
|
+
}
|
|
7740
|
+
else if (byteLength < 0x10000) {
|
|
7741
|
+
// str 16
|
|
7742
|
+
this.writeU8(0xda);
|
|
7743
|
+
this.writeU16(byteLength);
|
|
7744
|
+
}
|
|
7745
|
+
else if (byteLength < 0x100000000) {
|
|
7746
|
+
// str 32
|
|
7747
|
+
this.writeU8(0xdb);
|
|
7748
|
+
this.writeU32(byteLength);
|
|
7749
|
+
}
|
|
7750
|
+
else {
|
|
7751
|
+
throw new Error("Too long string: ".concat(byteLength, " bytes in UTF-8"));
|
|
7752
|
+
}
|
|
7753
|
+
};
|
|
7754
|
+
Encoder.prototype.encodeString = function (object) {
|
|
7755
|
+
var maxHeaderSize = 1 + 4;
|
|
7756
|
+
var strLength = object.length;
|
|
7757
|
+
if (strLength > TEXT_ENCODER_THRESHOLD) {
|
|
7758
|
+
var byteLength = utf8Count(object);
|
|
7759
|
+
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
|
|
7760
|
+
this.writeStringHeader(byteLength);
|
|
7761
|
+
utf8EncodeTE(object, this.bytes, this.pos);
|
|
7762
|
+
this.pos += byteLength;
|
|
7763
|
+
}
|
|
7764
|
+
else {
|
|
7765
|
+
var byteLength = utf8Count(object);
|
|
7766
|
+
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
|
|
7767
|
+
this.writeStringHeader(byteLength);
|
|
7768
|
+
utf8EncodeJs(object, this.bytes, this.pos);
|
|
7769
|
+
this.pos += byteLength;
|
|
7770
|
+
}
|
|
7771
|
+
};
|
|
7772
|
+
Encoder.prototype.encodeObject = function (object, depth) {
|
|
7773
|
+
// try to encode objects with custom codec first of non-primitives
|
|
7774
|
+
var ext = this.extensionCodec.tryToEncode(object, this.context);
|
|
7775
|
+
if (ext != null) {
|
|
7776
|
+
this.encodeExtension(ext);
|
|
7777
|
+
}
|
|
7778
|
+
else if (Array.isArray(object)) {
|
|
7779
|
+
this.encodeArray(object, depth);
|
|
7780
|
+
}
|
|
7781
|
+
else if (ArrayBuffer.isView(object)) {
|
|
7782
|
+
this.encodeBinary(object);
|
|
7783
|
+
}
|
|
7784
|
+
else if (typeof object === "object") {
|
|
7785
|
+
this.encodeMap(object, depth);
|
|
7786
|
+
}
|
|
7787
|
+
else {
|
|
7788
|
+
// symbol, function and other special object come here unless extensionCodec handles them.
|
|
7789
|
+
throw new Error("Unrecognized object: ".concat(Object.prototype.toString.apply(object)));
|
|
7790
|
+
}
|
|
7791
|
+
};
|
|
7792
|
+
Encoder.prototype.encodeBinary = function (object) {
|
|
7793
|
+
var size = object.byteLength;
|
|
7794
|
+
if (size < 0x100) {
|
|
7795
|
+
// bin 8
|
|
7796
|
+
this.writeU8(0xc4);
|
|
7797
|
+
this.writeU8(size);
|
|
7798
|
+
}
|
|
7799
|
+
else if (size < 0x10000) {
|
|
7800
|
+
// bin 16
|
|
7801
|
+
this.writeU8(0xc5);
|
|
7802
|
+
this.writeU16(size);
|
|
7803
|
+
}
|
|
7804
|
+
else if (size < 0x100000000) {
|
|
7805
|
+
// bin 32
|
|
7806
|
+
this.writeU8(0xc6);
|
|
7807
|
+
this.writeU32(size);
|
|
7808
|
+
}
|
|
7809
|
+
else {
|
|
7810
|
+
throw new Error("Too large binary: ".concat(size));
|
|
7811
|
+
}
|
|
7812
|
+
var bytes = ensureUint8Array(object);
|
|
7813
|
+
this.writeU8a(bytes);
|
|
7814
|
+
};
|
|
7815
|
+
Encoder.prototype.encodeArray = function (object, depth) {
|
|
7816
|
+
var size = object.length;
|
|
7817
|
+
if (size < 16) {
|
|
7818
|
+
// fixarray
|
|
7819
|
+
this.writeU8(0x90 + size);
|
|
7820
|
+
}
|
|
7821
|
+
else if (size < 0x10000) {
|
|
7822
|
+
// array 16
|
|
7823
|
+
this.writeU8(0xdc);
|
|
7824
|
+
this.writeU16(size);
|
|
7825
|
+
}
|
|
7826
|
+
else if (size < 0x100000000) {
|
|
7827
|
+
// array 32
|
|
7828
|
+
this.writeU8(0xdd);
|
|
7829
|
+
this.writeU32(size);
|
|
7830
|
+
}
|
|
7831
|
+
else {
|
|
7832
|
+
throw new Error("Too large array: ".concat(size));
|
|
7833
|
+
}
|
|
7834
|
+
for (var _i = 0, object_1 = object; _i < object_1.length; _i++) {
|
|
7835
|
+
var item = object_1[_i];
|
|
7836
|
+
this.doEncode(item, depth + 1);
|
|
7837
|
+
}
|
|
7838
|
+
};
|
|
7839
|
+
Encoder.prototype.countWithoutUndefined = function (object, keys) {
|
|
7840
|
+
var count = 0;
|
|
7841
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
7842
|
+
var key = keys_1[_i];
|
|
7843
|
+
if (object[key] !== undefined) {
|
|
7844
|
+
count++;
|
|
7845
|
+
}
|
|
7846
|
+
}
|
|
7847
|
+
return count;
|
|
7848
|
+
};
|
|
7849
|
+
Encoder.prototype.encodeMap = function (object, depth) {
|
|
7850
|
+
var keys = Object.keys(object);
|
|
7851
|
+
if (this.sortKeys) {
|
|
7852
|
+
keys.sort();
|
|
7853
|
+
}
|
|
7854
|
+
var size = this.ignoreUndefined ? this.countWithoutUndefined(object, keys) : keys.length;
|
|
7855
|
+
if (size < 16) {
|
|
7856
|
+
// fixmap
|
|
7857
|
+
this.writeU8(0x80 + size);
|
|
7858
|
+
}
|
|
7859
|
+
else if (size < 0x10000) {
|
|
7860
|
+
// map 16
|
|
7861
|
+
this.writeU8(0xde);
|
|
7862
|
+
this.writeU16(size);
|
|
7863
|
+
}
|
|
7864
|
+
else if (size < 0x100000000) {
|
|
7865
|
+
// map 32
|
|
7866
|
+
this.writeU8(0xdf);
|
|
7867
|
+
this.writeU32(size);
|
|
7868
|
+
}
|
|
7869
|
+
else {
|
|
7870
|
+
throw new Error("Too large map object: ".concat(size));
|
|
7871
|
+
}
|
|
7872
|
+
for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
|
|
7873
|
+
var key = keys_2[_i];
|
|
7874
|
+
var value = object[key];
|
|
7875
|
+
if (!(this.ignoreUndefined && value === undefined)) {
|
|
7876
|
+
this.encodeString(key);
|
|
7877
|
+
this.doEncode(value, depth + 1);
|
|
7878
|
+
}
|
|
7879
|
+
}
|
|
7880
|
+
};
|
|
7881
|
+
Encoder.prototype.encodeExtension = function (ext) {
|
|
7882
|
+
var size = ext.data.length;
|
|
7883
|
+
if (size === 1) {
|
|
7884
|
+
// fixext 1
|
|
7885
|
+
this.writeU8(0xd4);
|
|
7886
|
+
}
|
|
7887
|
+
else if (size === 2) {
|
|
7888
|
+
// fixext 2
|
|
7889
|
+
this.writeU8(0xd5);
|
|
7890
|
+
}
|
|
7891
|
+
else if (size === 4) {
|
|
7892
|
+
// fixext 4
|
|
7893
|
+
this.writeU8(0xd6);
|
|
7894
|
+
}
|
|
7895
|
+
else if (size === 8) {
|
|
7896
|
+
// fixext 8
|
|
7897
|
+
this.writeU8(0xd7);
|
|
7898
|
+
}
|
|
7899
|
+
else if (size === 16) {
|
|
7900
|
+
// fixext 16
|
|
7901
|
+
this.writeU8(0xd8);
|
|
7902
|
+
}
|
|
7903
|
+
else if (size < 0x100) {
|
|
7904
|
+
// ext 8
|
|
7905
|
+
this.writeU8(0xc7);
|
|
7906
|
+
this.writeU8(size);
|
|
7907
|
+
}
|
|
7908
|
+
else if (size < 0x10000) {
|
|
7909
|
+
// ext 16
|
|
7910
|
+
this.writeU8(0xc8);
|
|
7911
|
+
this.writeU16(size);
|
|
7912
|
+
}
|
|
7913
|
+
else if (size < 0x100000000) {
|
|
7914
|
+
// ext 32
|
|
7915
|
+
this.writeU8(0xc9);
|
|
7916
|
+
this.writeU32(size);
|
|
7917
|
+
}
|
|
7918
|
+
else {
|
|
7919
|
+
throw new Error("Too large extension object: ".concat(size));
|
|
7920
|
+
}
|
|
7921
|
+
this.writeI8(ext.type);
|
|
7922
|
+
this.writeU8a(ext.data);
|
|
7923
|
+
};
|
|
7924
|
+
Encoder.prototype.writeU8 = function (value) {
|
|
7925
|
+
this.ensureBufferSizeToWrite(1);
|
|
7926
|
+
this.view.setUint8(this.pos, value);
|
|
7927
|
+
this.pos++;
|
|
7928
|
+
};
|
|
7929
|
+
Encoder.prototype.writeU8a = function (values) {
|
|
7930
|
+
var size = values.length;
|
|
7931
|
+
this.ensureBufferSizeToWrite(size);
|
|
7932
|
+
this.bytes.set(values, this.pos);
|
|
7933
|
+
this.pos += size;
|
|
7934
|
+
};
|
|
7935
|
+
Encoder.prototype.writeI8 = function (value) {
|
|
7936
|
+
this.ensureBufferSizeToWrite(1);
|
|
7937
|
+
this.view.setInt8(this.pos, value);
|
|
7938
|
+
this.pos++;
|
|
7939
|
+
};
|
|
7940
|
+
Encoder.prototype.writeU16 = function (value) {
|
|
7941
|
+
this.ensureBufferSizeToWrite(2);
|
|
7942
|
+
this.view.setUint16(this.pos, value);
|
|
7943
|
+
this.pos += 2;
|
|
7944
|
+
};
|
|
7945
|
+
Encoder.prototype.writeI16 = function (value) {
|
|
7946
|
+
this.ensureBufferSizeToWrite(2);
|
|
7947
|
+
this.view.setInt16(this.pos, value);
|
|
7948
|
+
this.pos += 2;
|
|
7949
|
+
};
|
|
7950
|
+
Encoder.prototype.writeU32 = function (value) {
|
|
7951
|
+
this.ensureBufferSizeToWrite(4);
|
|
7952
|
+
this.view.setUint32(this.pos, value);
|
|
7953
|
+
this.pos += 4;
|
|
7954
|
+
};
|
|
7955
|
+
Encoder.prototype.writeI32 = function (value) {
|
|
7956
|
+
this.ensureBufferSizeToWrite(4);
|
|
7957
|
+
this.view.setInt32(this.pos, value);
|
|
7958
|
+
this.pos += 4;
|
|
7959
|
+
};
|
|
7960
|
+
Encoder.prototype.writeF32 = function (value) {
|
|
7961
|
+
this.ensureBufferSizeToWrite(4);
|
|
7962
|
+
this.view.setFloat32(this.pos, value);
|
|
7963
|
+
this.pos += 4;
|
|
7964
|
+
};
|
|
7965
|
+
Encoder.prototype.writeF64 = function (value) {
|
|
7966
|
+
this.ensureBufferSizeToWrite(8);
|
|
7967
|
+
this.view.setFloat64(this.pos, value);
|
|
7968
|
+
this.pos += 8;
|
|
7969
|
+
};
|
|
7970
|
+
Encoder.prototype.writeU64 = function (value) {
|
|
7971
|
+
this.ensureBufferSizeToWrite(8);
|
|
7972
|
+
setUint64(this.view, this.pos, value);
|
|
7973
|
+
this.pos += 8;
|
|
7974
|
+
};
|
|
7975
|
+
Encoder.prototype.writeI64 = function (value) {
|
|
7976
|
+
this.ensureBufferSizeToWrite(8);
|
|
7977
|
+
setInt64(this.view, this.pos, value);
|
|
7978
|
+
this.pos += 8;
|
|
7979
|
+
};
|
|
7980
|
+
return Encoder;
|
|
7981
|
+
}());
|
|
7982
|
+
|
|
7983
|
+
var defaultEncodeOptions = {};
|
|
7984
|
+
/**
|
|
7985
|
+
* It encodes `value` in the MessagePack format and
|
|
7986
|
+
* returns a byte buffer.
|
|
7987
|
+
*
|
|
7988
|
+
* The returned buffer is a slice of a larger `ArrayBuffer`, so you have to use its `#byteOffset` and `#byteLength` in order to convert it to another typed arrays including NodeJS `Buffer`.
|
|
7989
|
+
*/
|
|
7990
|
+
function encode(value, options) {
|
|
7991
|
+
if (options === void 0) { options = defaultEncodeOptions; }
|
|
7992
|
+
var encoder = new Encoder(options.extensionCodec, options.context, options.maxDepth, options.initialBufferSize, options.sortKeys, options.forceFloat32, options.ignoreUndefined, options.forceIntegerToFloat);
|
|
7993
|
+
return encoder.encodeSharedRef(value);
|
|
7994
|
+
}
|
|
7995
|
+
|
|
7996
|
+
function prettyByte(byte) {
|
|
7997
|
+
return "".concat(byte < 0 ? "-" : "", "0x").concat(Math.abs(byte).toString(16).padStart(2, "0"));
|
|
7998
|
+
}
|
|
7999
|
+
|
|
8000
|
+
var DEFAULT_MAX_KEY_LENGTH = 16;
|
|
8001
|
+
var DEFAULT_MAX_LENGTH_PER_KEY = 16;
|
|
8002
|
+
var CachedKeyDecoder = /** @class */ (function () {
|
|
8003
|
+
function CachedKeyDecoder(maxKeyLength, maxLengthPerKey) {
|
|
8004
|
+
if (maxKeyLength === void 0) { maxKeyLength = DEFAULT_MAX_KEY_LENGTH; }
|
|
8005
|
+
if (maxLengthPerKey === void 0) { maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY; }
|
|
8006
|
+
this.maxKeyLength = maxKeyLength;
|
|
8007
|
+
this.maxLengthPerKey = maxLengthPerKey;
|
|
8008
|
+
this.hit = 0;
|
|
8009
|
+
this.miss = 0;
|
|
8010
|
+
// avoid `new Array(N)`, which makes a sparse array,
|
|
8011
|
+
// because a sparse array is typically slower than a non-sparse array.
|
|
8012
|
+
this.caches = [];
|
|
8013
|
+
for (var i = 0; i < this.maxKeyLength; i++) {
|
|
8014
|
+
this.caches.push([]);
|
|
8015
|
+
}
|
|
8016
|
+
}
|
|
8017
|
+
CachedKeyDecoder.prototype.canBeCached = function (byteLength) {
|
|
8018
|
+
return byteLength > 0 && byteLength <= this.maxKeyLength;
|
|
8019
|
+
};
|
|
8020
|
+
CachedKeyDecoder.prototype.find = function (bytes, inputOffset, byteLength) {
|
|
8021
|
+
var records = this.caches[byteLength - 1];
|
|
8022
|
+
FIND_CHUNK: for (var _i = 0, records_1 = records; _i < records_1.length; _i++) {
|
|
8023
|
+
var record = records_1[_i];
|
|
8024
|
+
var recordBytes = record.bytes;
|
|
8025
|
+
for (var j = 0; j < byteLength; j++) {
|
|
8026
|
+
if (recordBytes[j] !== bytes[inputOffset + j]) {
|
|
8027
|
+
continue FIND_CHUNK;
|
|
8028
|
+
}
|
|
8029
|
+
}
|
|
8030
|
+
return record.str;
|
|
8031
|
+
}
|
|
8032
|
+
return null;
|
|
8033
|
+
};
|
|
8034
|
+
CachedKeyDecoder.prototype.store = function (bytes, value) {
|
|
8035
|
+
var records = this.caches[bytes.length - 1];
|
|
8036
|
+
var record = { bytes: bytes, str: value };
|
|
8037
|
+
if (records.length >= this.maxLengthPerKey) {
|
|
8038
|
+
// `records` are full!
|
|
8039
|
+
// Set `record` to an arbitrary position.
|
|
8040
|
+
records[(Math.random() * records.length) | 0] = record;
|
|
8041
|
+
}
|
|
8042
|
+
else {
|
|
8043
|
+
records.push(record);
|
|
8044
|
+
}
|
|
8045
|
+
};
|
|
8046
|
+
CachedKeyDecoder.prototype.decode = function (bytes, inputOffset, byteLength) {
|
|
8047
|
+
var cachedValue = this.find(bytes, inputOffset, byteLength);
|
|
8048
|
+
if (cachedValue != null) {
|
|
8049
|
+
this.hit++;
|
|
8050
|
+
return cachedValue;
|
|
8051
|
+
}
|
|
8052
|
+
this.miss++;
|
|
8053
|
+
var str = utf8DecodeJs(bytes, inputOffset, byteLength);
|
|
8054
|
+
// Ensure to copy a slice of bytes because the byte may be NodeJS Buffer and Buffer#slice() returns a reference to its internal ArrayBuffer.
|
|
8055
|
+
var slicedCopyOfBytes = Uint8Array.prototype.slice.call(bytes, inputOffset, inputOffset + byteLength);
|
|
8056
|
+
this.store(slicedCopyOfBytes, str);
|
|
8057
|
+
return str;
|
|
8058
|
+
};
|
|
8059
|
+
return CachedKeyDecoder;
|
|
8060
|
+
}());
|
|
8061
|
+
|
|
8062
|
+
var __awaiter$1 = (null && null.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8063
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8064
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8065
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
8066
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8067
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8068
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8069
|
+
});
|
|
8070
|
+
};
|
|
8071
|
+
var __generator$2 = (null && null.__generator) || function (thisArg, body) {
|
|
8072
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
8073
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
8074
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
8075
|
+
function step(op) {
|
|
8076
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8077
|
+
while (_) try {
|
|
8078
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
8079
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
8080
|
+
switch (op[0]) {
|
|
8081
|
+
case 0: case 1: t = op; break;
|
|
8082
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
8083
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
8084
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
8085
|
+
default:
|
|
8086
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
8087
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
8088
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
8089
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
8090
|
+
if (t[2]) _.ops.pop();
|
|
8091
|
+
_.trys.pop(); continue;
|
|
8092
|
+
}
|
|
8093
|
+
op = body.call(thisArg, _);
|
|
8094
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
8095
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
8096
|
+
}
|
|
8097
|
+
};
|
|
8098
|
+
var __asyncValues = (null && null.__asyncValues) || function (o) {
|
|
8099
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
8100
|
+
var m = o[Symbol.asyncIterator], i;
|
|
8101
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
8102
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
8103
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8104
|
+
};
|
|
8105
|
+
var __await$1 = (null && null.__await) || function (v) { return this instanceof __await$1 ? (this.v = v, this) : new __await$1(v); };
|
|
8106
|
+
var __asyncGenerator$1 = (null && null.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
8107
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
8108
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
8109
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
8110
|
+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
|
8111
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
8112
|
+
function step(r) { r.value instanceof __await$1 ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
8113
|
+
function fulfill(value) { resume("next", value); }
|
|
8114
|
+
function reject(value) { resume("throw", value); }
|
|
8115
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
8116
|
+
};
|
|
8117
|
+
var isValidMapKeyType = function (key) {
|
|
8118
|
+
var keyType = typeof key;
|
|
8119
|
+
return keyType === "string" || keyType === "number";
|
|
8120
|
+
};
|
|
8121
|
+
var HEAD_BYTE_REQUIRED = -1;
|
|
8122
|
+
var EMPTY_VIEW = new DataView(new ArrayBuffer(0));
|
|
8123
|
+
var EMPTY_BYTES = new Uint8Array(EMPTY_VIEW.buffer);
|
|
8124
|
+
// IE11: Hack to support IE11.
|
|
8125
|
+
// IE11: Drop this hack and just use RangeError when IE11 is obsolete.
|
|
8126
|
+
var DataViewIndexOutOfBoundsError = (function () {
|
|
8127
|
+
try {
|
|
8128
|
+
// IE11: The spec says it should throw RangeError,
|
|
8129
|
+
// IE11: but in IE11 it throws TypeError.
|
|
8130
|
+
EMPTY_VIEW.getInt8(0);
|
|
8131
|
+
}
|
|
8132
|
+
catch (e) {
|
|
8133
|
+
return e.constructor;
|
|
8134
|
+
}
|
|
8135
|
+
throw new Error("never reached");
|
|
8136
|
+
})();
|
|
8137
|
+
var MORE_DATA = new DataViewIndexOutOfBoundsError("Insufficient data");
|
|
8138
|
+
var sharedCachedKeyDecoder = new CachedKeyDecoder();
|
|
8139
|
+
var Decoder = /** @class */ (function () {
|
|
8140
|
+
function Decoder(extensionCodec, context, maxStrLength, maxBinLength, maxArrayLength, maxMapLength, maxExtLength, keyDecoder) {
|
|
8141
|
+
if (extensionCodec === void 0) { extensionCodec = ExtensionCodec.defaultCodec; }
|
|
8142
|
+
if (context === void 0) { context = undefined; }
|
|
8143
|
+
if (maxStrLength === void 0) { maxStrLength = UINT32_MAX; }
|
|
8144
|
+
if (maxBinLength === void 0) { maxBinLength = UINT32_MAX; }
|
|
8145
|
+
if (maxArrayLength === void 0) { maxArrayLength = UINT32_MAX; }
|
|
8146
|
+
if (maxMapLength === void 0) { maxMapLength = UINT32_MAX; }
|
|
8147
|
+
if (maxExtLength === void 0) { maxExtLength = UINT32_MAX; }
|
|
8148
|
+
if (keyDecoder === void 0) { keyDecoder = sharedCachedKeyDecoder; }
|
|
8149
|
+
this.extensionCodec = extensionCodec;
|
|
8150
|
+
this.context = context;
|
|
8151
|
+
this.maxStrLength = maxStrLength;
|
|
8152
|
+
this.maxBinLength = maxBinLength;
|
|
8153
|
+
this.maxArrayLength = maxArrayLength;
|
|
8154
|
+
this.maxMapLength = maxMapLength;
|
|
8155
|
+
this.maxExtLength = maxExtLength;
|
|
8156
|
+
this.keyDecoder = keyDecoder;
|
|
8157
|
+
this.totalPos = 0;
|
|
8158
|
+
this.pos = 0;
|
|
8159
|
+
this.view = EMPTY_VIEW;
|
|
8160
|
+
this.bytes = EMPTY_BYTES;
|
|
8161
|
+
this.headByte = HEAD_BYTE_REQUIRED;
|
|
8162
|
+
this.stack = [];
|
|
8163
|
+
}
|
|
8164
|
+
Decoder.prototype.reinitializeState = function () {
|
|
8165
|
+
this.totalPos = 0;
|
|
8166
|
+
this.headByte = HEAD_BYTE_REQUIRED;
|
|
8167
|
+
this.stack.length = 0;
|
|
8168
|
+
// view, bytes, and pos will be re-initialized in setBuffer()
|
|
8169
|
+
};
|
|
8170
|
+
Decoder.prototype.setBuffer = function (buffer) {
|
|
8171
|
+
this.bytes = ensureUint8Array(buffer);
|
|
8172
|
+
this.view = createDataView(this.bytes);
|
|
8173
|
+
this.pos = 0;
|
|
8174
|
+
};
|
|
8175
|
+
Decoder.prototype.appendBuffer = function (buffer) {
|
|
8176
|
+
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining(1)) {
|
|
8177
|
+
this.setBuffer(buffer);
|
|
8178
|
+
}
|
|
8179
|
+
else {
|
|
8180
|
+
var remainingData = this.bytes.subarray(this.pos);
|
|
8181
|
+
var newData = ensureUint8Array(buffer);
|
|
8182
|
+
// concat remainingData + newData
|
|
8183
|
+
var newBuffer = new Uint8Array(remainingData.length + newData.length);
|
|
8184
|
+
newBuffer.set(remainingData);
|
|
8185
|
+
newBuffer.set(newData, remainingData.length);
|
|
8186
|
+
this.setBuffer(newBuffer);
|
|
8187
|
+
}
|
|
8188
|
+
};
|
|
8189
|
+
Decoder.prototype.hasRemaining = function (size) {
|
|
8190
|
+
return this.view.byteLength - this.pos >= size;
|
|
8191
|
+
};
|
|
8192
|
+
Decoder.prototype.createExtraByteError = function (posToShow) {
|
|
8193
|
+
var _a = this, view = _a.view, pos = _a.pos;
|
|
8194
|
+
return new RangeError("Extra ".concat(view.byteLength - pos, " of ").concat(view.byteLength, " byte(s) found at buffer[").concat(posToShow, "]"));
|
|
8195
|
+
};
|
|
8196
|
+
/**
|
|
8197
|
+
* @throws {@link DecodeError}
|
|
8198
|
+
* @throws {@link RangeError}
|
|
8199
|
+
*/
|
|
8200
|
+
Decoder.prototype.decode = function (buffer) {
|
|
8201
|
+
this.reinitializeState();
|
|
8202
|
+
this.setBuffer(buffer);
|
|
8203
|
+
var object = this.doDecodeSync();
|
|
8204
|
+
if (this.hasRemaining(1)) {
|
|
8205
|
+
throw this.createExtraByteError(this.pos);
|
|
8206
|
+
}
|
|
8207
|
+
return object;
|
|
8208
|
+
};
|
|
8209
|
+
Decoder.prototype.decodeMulti = function (buffer) {
|
|
8210
|
+
return __generator$2(this, function (_a) {
|
|
8211
|
+
switch (_a.label) {
|
|
8212
|
+
case 0:
|
|
8213
|
+
this.reinitializeState();
|
|
8214
|
+
this.setBuffer(buffer);
|
|
8215
|
+
_a.label = 1;
|
|
8216
|
+
case 1:
|
|
8217
|
+
if (!this.hasRemaining(1)) return [3 /*break*/, 3];
|
|
8218
|
+
return [4 /*yield*/, this.doDecodeSync()];
|
|
8219
|
+
case 2:
|
|
8220
|
+
_a.sent();
|
|
8221
|
+
return [3 /*break*/, 1];
|
|
8222
|
+
case 3: return [2 /*return*/];
|
|
8223
|
+
}
|
|
8224
|
+
});
|
|
8225
|
+
};
|
|
8226
|
+
Decoder.prototype.decodeAsync = function (stream) {
|
|
8227
|
+
var stream_1, stream_1_1;
|
|
8228
|
+
var e_1, _a;
|
|
8229
|
+
return __awaiter$1(this, void 0, void 0, function () {
|
|
8230
|
+
var decoded, object, buffer, e_1_1, _b, headByte, pos, totalPos;
|
|
8231
|
+
return __generator$2(this, function (_c) {
|
|
8232
|
+
switch (_c.label) {
|
|
8233
|
+
case 0:
|
|
8234
|
+
decoded = false;
|
|
8235
|
+
_c.label = 1;
|
|
8236
|
+
case 1:
|
|
8237
|
+
_c.trys.push([1, 6, 7, 12]);
|
|
8238
|
+
stream_1 = __asyncValues(stream);
|
|
8239
|
+
_c.label = 2;
|
|
8240
|
+
case 2: return [4 /*yield*/, stream_1.next()];
|
|
8241
|
+
case 3:
|
|
8242
|
+
if (!(stream_1_1 = _c.sent(), !stream_1_1.done)) return [3 /*break*/, 5];
|
|
8243
|
+
buffer = stream_1_1.value;
|
|
8244
|
+
if (decoded) {
|
|
8245
|
+
throw this.createExtraByteError(this.totalPos);
|
|
8246
|
+
}
|
|
8247
|
+
this.appendBuffer(buffer);
|
|
8248
|
+
try {
|
|
8249
|
+
object = this.doDecodeSync();
|
|
8250
|
+
decoded = true;
|
|
8251
|
+
}
|
|
8252
|
+
catch (e) {
|
|
8253
|
+
if (!(e instanceof DataViewIndexOutOfBoundsError)) {
|
|
8254
|
+
throw e; // rethrow
|
|
8255
|
+
}
|
|
8256
|
+
// fallthrough
|
|
8257
|
+
}
|
|
8258
|
+
this.totalPos += this.pos;
|
|
8259
|
+
_c.label = 4;
|
|
8260
|
+
case 4: return [3 /*break*/, 2];
|
|
8261
|
+
case 5: return [3 /*break*/, 12];
|
|
8262
|
+
case 6:
|
|
8263
|
+
e_1_1 = _c.sent();
|
|
8264
|
+
e_1 = { error: e_1_1 };
|
|
8265
|
+
return [3 /*break*/, 12];
|
|
8266
|
+
case 7:
|
|
8267
|
+
_c.trys.push([7, , 10, 11]);
|
|
8268
|
+
if (!(stream_1_1 && !stream_1_1.done && (_a = stream_1.return))) return [3 /*break*/, 9];
|
|
8269
|
+
return [4 /*yield*/, _a.call(stream_1)];
|
|
8270
|
+
case 8:
|
|
8271
|
+
_c.sent();
|
|
8272
|
+
_c.label = 9;
|
|
8273
|
+
case 9: return [3 /*break*/, 11];
|
|
8274
|
+
case 10:
|
|
8275
|
+
if (e_1) throw e_1.error;
|
|
8276
|
+
return [7 /*endfinally*/];
|
|
8277
|
+
case 11: return [7 /*endfinally*/];
|
|
8278
|
+
case 12:
|
|
8279
|
+
if (decoded) {
|
|
8280
|
+
if (this.hasRemaining(1)) {
|
|
8281
|
+
throw this.createExtraByteError(this.totalPos);
|
|
8282
|
+
}
|
|
8283
|
+
return [2 /*return*/, object];
|
|
8284
|
+
}
|
|
8285
|
+
_b = this, headByte = _b.headByte, pos = _b.pos, totalPos = _b.totalPos;
|
|
8286
|
+
throw new RangeError("Insufficient data in parsing ".concat(prettyByte(headByte), " at ").concat(totalPos, " (").concat(pos, " in the current buffer)"));
|
|
8287
|
+
}
|
|
8288
|
+
});
|
|
8289
|
+
});
|
|
8290
|
+
};
|
|
8291
|
+
Decoder.prototype.decodeArrayStream = function (stream) {
|
|
8292
|
+
return this.decodeMultiAsync(stream, true);
|
|
8293
|
+
};
|
|
8294
|
+
Decoder.prototype.decodeStream = function (stream) {
|
|
8295
|
+
return this.decodeMultiAsync(stream, false);
|
|
8296
|
+
};
|
|
8297
|
+
Decoder.prototype.decodeMultiAsync = function (stream, isArray) {
|
|
8298
|
+
return __asyncGenerator$1(this, arguments, function decodeMultiAsync_1() {
|
|
8299
|
+
var isArrayHeaderRequired, arrayItemsLeft, stream_2, stream_2_1, buffer, e_2, e_3_1;
|
|
8300
|
+
var e_3, _a;
|
|
8301
|
+
return __generator$2(this, function (_b) {
|
|
8302
|
+
switch (_b.label) {
|
|
8303
|
+
case 0:
|
|
8304
|
+
isArrayHeaderRequired = isArray;
|
|
8305
|
+
arrayItemsLeft = -1;
|
|
8306
|
+
_b.label = 1;
|
|
8307
|
+
case 1:
|
|
8308
|
+
_b.trys.push([1, 13, 14, 19]);
|
|
8309
|
+
stream_2 = __asyncValues(stream);
|
|
8310
|
+
_b.label = 2;
|
|
8311
|
+
case 2: return [4 /*yield*/, __await$1(stream_2.next())];
|
|
8312
|
+
case 3:
|
|
8313
|
+
if (!(stream_2_1 = _b.sent(), !stream_2_1.done)) return [3 /*break*/, 12];
|
|
8314
|
+
buffer = stream_2_1.value;
|
|
8315
|
+
if (isArray && arrayItemsLeft === 0) {
|
|
8316
|
+
throw this.createExtraByteError(this.totalPos);
|
|
8317
|
+
}
|
|
8318
|
+
this.appendBuffer(buffer);
|
|
8319
|
+
if (isArrayHeaderRequired) {
|
|
8320
|
+
arrayItemsLeft = this.readArraySize();
|
|
8321
|
+
isArrayHeaderRequired = false;
|
|
8322
|
+
this.complete();
|
|
8323
|
+
}
|
|
8324
|
+
_b.label = 4;
|
|
8325
|
+
case 4:
|
|
8326
|
+
_b.trys.push([4, 9, , 10]);
|
|
8327
|
+
_b.label = 5;
|
|
8328
|
+
case 5:
|
|
8329
|
+
if (!true) return [3 /*break*/, 8];
|
|
8330
|
+
return [4 /*yield*/, __await$1(this.doDecodeSync())];
|
|
8331
|
+
case 6: return [4 /*yield*/, _b.sent()];
|
|
8332
|
+
case 7:
|
|
8333
|
+
_b.sent();
|
|
8334
|
+
if (--arrayItemsLeft === 0) {
|
|
8335
|
+
return [3 /*break*/, 8];
|
|
8336
|
+
}
|
|
8337
|
+
return [3 /*break*/, 5];
|
|
8338
|
+
case 8: return [3 /*break*/, 10];
|
|
8339
|
+
case 9:
|
|
8340
|
+
e_2 = _b.sent();
|
|
8341
|
+
if (!(e_2 instanceof DataViewIndexOutOfBoundsError)) {
|
|
8342
|
+
throw e_2; // rethrow
|
|
8343
|
+
}
|
|
8344
|
+
return [3 /*break*/, 10];
|
|
8345
|
+
case 10:
|
|
8346
|
+
this.totalPos += this.pos;
|
|
8347
|
+
_b.label = 11;
|
|
8348
|
+
case 11: return [3 /*break*/, 2];
|
|
8349
|
+
case 12: return [3 /*break*/, 19];
|
|
8350
|
+
case 13:
|
|
8351
|
+
e_3_1 = _b.sent();
|
|
8352
|
+
e_3 = { error: e_3_1 };
|
|
8353
|
+
return [3 /*break*/, 19];
|
|
8354
|
+
case 14:
|
|
8355
|
+
_b.trys.push([14, , 17, 18]);
|
|
8356
|
+
if (!(stream_2_1 && !stream_2_1.done && (_a = stream_2.return))) return [3 /*break*/, 16];
|
|
8357
|
+
return [4 /*yield*/, __await$1(_a.call(stream_2))];
|
|
8358
|
+
case 15:
|
|
8359
|
+
_b.sent();
|
|
8360
|
+
_b.label = 16;
|
|
8361
|
+
case 16: return [3 /*break*/, 18];
|
|
8362
|
+
case 17:
|
|
8363
|
+
if (e_3) throw e_3.error;
|
|
8364
|
+
return [7 /*endfinally*/];
|
|
8365
|
+
case 18: return [7 /*endfinally*/];
|
|
8366
|
+
case 19: return [2 /*return*/];
|
|
8367
|
+
}
|
|
8368
|
+
});
|
|
8369
|
+
});
|
|
8370
|
+
};
|
|
8371
|
+
Decoder.prototype.doDecodeSync = function () {
|
|
8372
|
+
DECODE: while (true) {
|
|
8373
|
+
var headByte = this.readHeadByte();
|
|
8374
|
+
var object = void 0;
|
|
8375
|
+
if (headByte >= 0xe0) {
|
|
8376
|
+
// negative fixint (111x xxxx) 0xe0 - 0xff
|
|
8377
|
+
object = headByte - 0x100;
|
|
8378
|
+
}
|
|
8379
|
+
else if (headByte < 0xc0) {
|
|
8380
|
+
if (headByte < 0x80) {
|
|
8381
|
+
// positive fixint (0xxx xxxx) 0x00 - 0x7f
|
|
8382
|
+
object = headByte;
|
|
8383
|
+
}
|
|
8384
|
+
else if (headByte < 0x90) {
|
|
8385
|
+
// fixmap (1000 xxxx) 0x80 - 0x8f
|
|
8386
|
+
var size = headByte - 0x80;
|
|
8387
|
+
if (size !== 0) {
|
|
8388
|
+
this.pushMapState(size);
|
|
8389
|
+
this.complete();
|
|
8390
|
+
continue DECODE;
|
|
8391
|
+
}
|
|
8392
|
+
else {
|
|
8393
|
+
object = {};
|
|
8394
|
+
}
|
|
8395
|
+
}
|
|
8396
|
+
else if (headByte < 0xa0) {
|
|
8397
|
+
// fixarray (1001 xxxx) 0x90 - 0x9f
|
|
8398
|
+
var size = headByte - 0x90;
|
|
8399
|
+
if (size !== 0) {
|
|
8400
|
+
this.pushArrayState(size);
|
|
8401
|
+
this.complete();
|
|
8402
|
+
continue DECODE;
|
|
8403
|
+
}
|
|
8404
|
+
else {
|
|
8405
|
+
object = [];
|
|
8406
|
+
}
|
|
8407
|
+
}
|
|
8408
|
+
else {
|
|
8409
|
+
// fixstr (101x xxxx) 0xa0 - 0xbf
|
|
8410
|
+
var byteLength = headByte - 0xa0;
|
|
8411
|
+
object = this.decodeUtf8String(byteLength, 0);
|
|
8412
|
+
}
|
|
8413
|
+
}
|
|
8414
|
+
else if (headByte === 0xc0) {
|
|
8415
|
+
// nil
|
|
8416
|
+
object = null;
|
|
8417
|
+
}
|
|
8418
|
+
else if (headByte === 0xc2) {
|
|
8419
|
+
// false
|
|
8420
|
+
object = false;
|
|
8421
|
+
}
|
|
8422
|
+
else if (headByte === 0xc3) {
|
|
8423
|
+
// true
|
|
8424
|
+
object = true;
|
|
8425
|
+
}
|
|
8426
|
+
else if (headByte === 0xca) {
|
|
8427
|
+
// float 32
|
|
8428
|
+
object = this.readF32();
|
|
8429
|
+
}
|
|
8430
|
+
else if (headByte === 0xcb) {
|
|
8431
|
+
// float 64
|
|
8432
|
+
object = this.readF64();
|
|
8433
|
+
}
|
|
8434
|
+
else if (headByte === 0xcc) {
|
|
8435
|
+
// uint 8
|
|
8436
|
+
object = this.readU8();
|
|
8437
|
+
}
|
|
8438
|
+
else if (headByte === 0xcd) {
|
|
8439
|
+
// uint 16
|
|
8440
|
+
object = this.readU16();
|
|
8441
|
+
}
|
|
8442
|
+
else if (headByte === 0xce) {
|
|
8443
|
+
// uint 32
|
|
8444
|
+
object = this.readU32();
|
|
8445
|
+
}
|
|
8446
|
+
else if (headByte === 0xcf) {
|
|
8447
|
+
// uint 64
|
|
8448
|
+
object = this.readU64();
|
|
8449
|
+
}
|
|
8450
|
+
else if (headByte === 0xd0) {
|
|
8451
|
+
// int 8
|
|
8452
|
+
object = this.readI8();
|
|
8453
|
+
}
|
|
8454
|
+
else if (headByte === 0xd1) {
|
|
8455
|
+
// int 16
|
|
8456
|
+
object = this.readI16();
|
|
8457
|
+
}
|
|
8458
|
+
else if (headByte === 0xd2) {
|
|
8459
|
+
// int 32
|
|
8460
|
+
object = this.readI32();
|
|
8461
|
+
}
|
|
8462
|
+
else if (headByte === 0xd3) {
|
|
8463
|
+
// int 64
|
|
8464
|
+
object = this.readI64();
|
|
8465
|
+
}
|
|
8466
|
+
else if (headByte === 0xd9) {
|
|
8467
|
+
// str 8
|
|
8468
|
+
var byteLength = this.lookU8();
|
|
8469
|
+
object = this.decodeUtf8String(byteLength, 1);
|
|
8470
|
+
}
|
|
8471
|
+
else if (headByte === 0xda) {
|
|
8472
|
+
// str 16
|
|
8473
|
+
var byteLength = this.lookU16();
|
|
8474
|
+
object = this.decodeUtf8String(byteLength, 2);
|
|
8475
|
+
}
|
|
8476
|
+
else if (headByte === 0xdb) {
|
|
8477
|
+
// str 32
|
|
8478
|
+
var byteLength = this.lookU32();
|
|
8479
|
+
object = this.decodeUtf8String(byteLength, 4);
|
|
8480
|
+
}
|
|
8481
|
+
else if (headByte === 0xdc) {
|
|
8482
|
+
// array 16
|
|
8483
|
+
var size = this.readU16();
|
|
8484
|
+
if (size !== 0) {
|
|
8485
|
+
this.pushArrayState(size);
|
|
8486
|
+
this.complete();
|
|
8487
|
+
continue DECODE;
|
|
8488
|
+
}
|
|
8489
|
+
else {
|
|
8490
|
+
object = [];
|
|
8491
|
+
}
|
|
8492
|
+
}
|
|
8493
|
+
else if (headByte === 0xdd) {
|
|
8494
|
+
// array 32
|
|
8495
|
+
var size = this.readU32();
|
|
8496
|
+
if (size !== 0) {
|
|
8497
|
+
this.pushArrayState(size);
|
|
8498
|
+
this.complete();
|
|
8499
|
+
continue DECODE;
|
|
8500
|
+
}
|
|
8501
|
+
else {
|
|
8502
|
+
object = [];
|
|
8503
|
+
}
|
|
8504
|
+
}
|
|
8505
|
+
else if (headByte === 0xde) {
|
|
8506
|
+
// map 16
|
|
8507
|
+
var size = this.readU16();
|
|
8508
|
+
if (size !== 0) {
|
|
8509
|
+
this.pushMapState(size);
|
|
8510
|
+
this.complete();
|
|
8511
|
+
continue DECODE;
|
|
8512
|
+
}
|
|
8513
|
+
else {
|
|
8514
|
+
object = {};
|
|
8515
|
+
}
|
|
8516
|
+
}
|
|
8517
|
+
else if (headByte === 0xdf) {
|
|
8518
|
+
// map 32
|
|
8519
|
+
var size = this.readU32();
|
|
8520
|
+
if (size !== 0) {
|
|
8521
|
+
this.pushMapState(size);
|
|
8522
|
+
this.complete();
|
|
8523
|
+
continue DECODE;
|
|
8524
|
+
}
|
|
8525
|
+
else {
|
|
8526
|
+
object = {};
|
|
8527
|
+
}
|
|
8528
|
+
}
|
|
8529
|
+
else if (headByte === 0xc4) {
|
|
8530
|
+
// bin 8
|
|
8531
|
+
var size = this.lookU8();
|
|
8532
|
+
object = this.decodeBinary(size, 1);
|
|
8533
|
+
}
|
|
8534
|
+
else if (headByte === 0xc5) {
|
|
8535
|
+
// bin 16
|
|
8536
|
+
var size = this.lookU16();
|
|
8537
|
+
object = this.decodeBinary(size, 2);
|
|
8538
|
+
}
|
|
8539
|
+
else if (headByte === 0xc6) {
|
|
8540
|
+
// bin 32
|
|
8541
|
+
var size = this.lookU32();
|
|
8542
|
+
object = this.decodeBinary(size, 4);
|
|
8543
|
+
}
|
|
8544
|
+
else if (headByte === 0xd4) {
|
|
8545
|
+
// fixext 1
|
|
8546
|
+
object = this.decodeExtension(1, 0);
|
|
8547
|
+
}
|
|
8548
|
+
else if (headByte === 0xd5) {
|
|
8549
|
+
// fixext 2
|
|
8550
|
+
object = this.decodeExtension(2, 0);
|
|
8551
|
+
}
|
|
8552
|
+
else if (headByte === 0xd6) {
|
|
8553
|
+
// fixext 4
|
|
8554
|
+
object = this.decodeExtension(4, 0);
|
|
8555
|
+
}
|
|
8556
|
+
else if (headByte === 0xd7) {
|
|
8557
|
+
// fixext 8
|
|
8558
|
+
object = this.decodeExtension(8, 0);
|
|
8559
|
+
}
|
|
8560
|
+
else if (headByte === 0xd8) {
|
|
8561
|
+
// fixext 16
|
|
8562
|
+
object = this.decodeExtension(16, 0);
|
|
8563
|
+
}
|
|
8564
|
+
else if (headByte === 0xc7) {
|
|
8565
|
+
// ext 8
|
|
8566
|
+
var size = this.lookU8();
|
|
8567
|
+
object = this.decodeExtension(size, 1);
|
|
8568
|
+
}
|
|
8569
|
+
else if (headByte === 0xc8) {
|
|
8570
|
+
// ext 16
|
|
8571
|
+
var size = this.lookU16();
|
|
8572
|
+
object = this.decodeExtension(size, 2);
|
|
8573
|
+
}
|
|
8574
|
+
else if (headByte === 0xc9) {
|
|
8575
|
+
// ext 32
|
|
8576
|
+
var size = this.lookU32();
|
|
8577
|
+
object = this.decodeExtension(size, 4);
|
|
8578
|
+
}
|
|
8579
|
+
else {
|
|
8580
|
+
throw new DecodeError("Unrecognized type byte: ".concat(prettyByte(headByte)));
|
|
8581
|
+
}
|
|
8582
|
+
this.complete();
|
|
8583
|
+
var stack = this.stack;
|
|
8584
|
+
while (stack.length > 0) {
|
|
8585
|
+
// arrays and maps
|
|
8586
|
+
var state = stack[stack.length - 1];
|
|
8587
|
+
if (state.type === 0 /* State.ARRAY */) {
|
|
8588
|
+
state.array[state.position] = object;
|
|
8589
|
+
state.position++;
|
|
8590
|
+
if (state.position === state.size) {
|
|
8591
|
+
stack.pop();
|
|
8592
|
+
object = state.array;
|
|
8593
|
+
}
|
|
8594
|
+
else {
|
|
8595
|
+
continue DECODE;
|
|
8596
|
+
}
|
|
8597
|
+
}
|
|
8598
|
+
else if (state.type === 1 /* State.MAP_KEY */) {
|
|
8599
|
+
if (!isValidMapKeyType(object)) {
|
|
8600
|
+
throw new DecodeError("The type of key must be string or number but " + typeof object);
|
|
8601
|
+
}
|
|
8602
|
+
if (object === "__proto__") {
|
|
8603
|
+
throw new DecodeError("The key __proto__ is not allowed");
|
|
8604
|
+
}
|
|
8605
|
+
state.key = object;
|
|
8606
|
+
state.type = 2 /* State.MAP_VALUE */;
|
|
8607
|
+
continue DECODE;
|
|
8608
|
+
}
|
|
8609
|
+
else {
|
|
8610
|
+
// it must be `state.type === State.MAP_VALUE` here
|
|
8611
|
+
state.map[state.key] = object;
|
|
8612
|
+
state.readCount++;
|
|
8613
|
+
if (state.readCount === state.size) {
|
|
8614
|
+
stack.pop();
|
|
8615
|
+
object = state.map;
|
|
8616
|
+
}
|
|
8617
|
+
else {
|
|
8618
|
+
state.key = null;
|
|
8619
|
+
state.type = 1 /* State.MAP_KEY */;
|
|
8620
|
+
continue DECODE;
|
|
8621
|
+
}
|
|
8622
|
+
}
|
|
8623
|
+
}
|
|
8624
|
+
return object;
|
|
8625
|
+
}
|
|
8626
|
+
};
|
|
8627
|
+
Decoder.prototype.readHeadByte = function () {
|
|
8628
|
+
if (this.headByte === HEAD_BYTE_REQUIRED) {
|
|
8629
|
+
this.headByte = this.readU8();
|
|
8630
|
+
// console.log("headByte", prettyByte(this.headByte));
|
|
8631
|
+
}
|
|
8632
|
+
return this.headByte;
|
|
8633
|
+
};
|
|
8634
|
+
Decoder.prototype.complete = function () {
|
|
8635
|
+
this.headByte = HEAD_BYTE_REQUIRED;
|
|
8636
|
+
};
|
|
8637
|
+
Decoder.prototype.readArraySize = function () {
|
|
8638
|
+
var headByte = this.readHeadByte();
|
|
8639
|
+
switch (headByte) {
|
|
8640
|
+
case 0xdc:
|
|
8641
|
+
return this.readU16();
|
|
8642
|
+
case 0xdd:
|
|
8643
|
+
return this.readU32();
|
|
8644
|
+
default: {
|
|
8645
|
+
if (headByte < 0xa0) {
|
|
8646
|
+
return headByte - 0x90;
|
|
8647
|
+
}
|
|
8648
|
+
else {
|
|
8649
|
+
throw new DecodeError("Unrecognized array type byte: ".concat(prettyByte(headByte)));
|
|
8650
|
+
}
|
|
8651
|
+
}
|
|
8652
|
+
}
|
|
8653
|
+
};
|
|
8654
|
+
Decoder.prototype.pushMapState = function (size) {
|
|
8655
|
+
if (size > this.maxMapLength) {
|
|
8656
|
+
throw new DecodeError("Max length exceeded: map length (".concat(size, ") > maxMapLengthLength (").concat(this.maxMapLength, ")"));
|
|
8657
|
+
}
|
|
8658
|
+
this.stack.push({
|
|
8659
|
+
type: 1 /* State.MAP_KEY */,
|
|
8660
|
+
size: size,
|
|
8661
|
+
key: null,
|
|
8662
|
+
readCount: 0,
|
|
8663
|
+
map: {},
|
|
8664
|
+
});
|
|
8665
|
+
};
|
|
8666
|
+
Decoder.prototype.pushArrayState = function (size) {
|
|
8667
|
+
if (size > this.maxArrayLength) {
|
|
8668
|
+
throw new DecodeError("Max length exceeded: array length (".concat(size, ") > maxArrayLength (").concat(this.maxArrayLength, ")"));
|
|
8669
|
+
}
|
|
8670
|
+
this.stack.push({
|
|
8671
|
+
type: 0 /* State.ARRAY */,
|
|
8672
|
+
size: size,
|
|
8673
|
+
array: new Array(size),
|
|
8674
|
+
position: 0,
|
|
8675
|
+
});
|
|
8676
|
+
};
|
|
8677
|
+
Decoder.prototype.decodeUtf8String = function (byteLength, headerOffset) {
|
|
8678
|
+
var _a;
|
|
8679
|
+
if (byteLength > this.maxStrLength) {
|
|
8680
|
+
throw new DecodeError("Max length exceeded: UTF-8 byte length (".concat(byteLength, ") > maxStrLength (").concat(this.maxStrLength, ")"));
|
|
8681
|
+
}
|
|
8682
|
+
if (this.bytes.byteLength < this.pos + headerOffset + byteLength) {
|
|
8683
|
+
throw MORE_DATA;
|
|
8684
|
+
}
|
|
8685
|
+
var offset = this.pos + headerOffset;
|
|
8686
|
+
var object;
|
|
8687
|
+
if (this.stateIsMapKey() && ((_a = this.keyDecoder) === null || _a === void 0 ? void 0 : _a.canBeCached(byteLength))) {
|
|
8688
|
+
object = this.keyDecoder.decode(this.bytes, offset, byteLength);
|
|
8689
|
+
}
|
|
8690
|
+
else if (byteLength > TEXT_DECODER_THRESHOLD) {
|
|
8691
|
+
object = utf8DecodeTD(this.bytes, offset, byteLength);
|
|
8692
|
+
}
|
|
8693
|
+
else {
|
|
8694
|
+
object = utf8DecodeJs(this.bytes, offset, byteLength);
|
|
8695
|
+
}
|
|
8696
|
+
this.pos += headerOffset + byteLength;
|
|
8697
|
+
return object;
|
|
8698
|
+
};
|
|
8699
|
+
Decoder.prototype.stateIsMapKey = function () {
|
|
8700
|
+
if (this.stack.length > 0) {
|
|
8701
|
+
var state = this.stack[this.stack.length - 1];
|
|
8702
|
+
return state.type === 1 /* State.MAP_KEY */;
|
|
8703
|
+
}
|
|
8704
|
+
return false;
|
|
8705
|
+
};
|
|
8706
|
+
Decoder.prototype.decodeBinary = function (byteLength, headOffset) {
|
|
8707
|
+
if (byteLength > this.maxBinLength) {
|
|
8708
|
+
throw new DecodeError("Max length exceeded: bin length (".concat(byteLength, ") > maxBinLength (").concat(this.maxBinLength, ")"));
|
|
8709
|
+
}
|
|
8710
|
+
if (!this.hasRemaining(byteLength + headOffset)) {
|
|
8711
|
+
throw MORE_DATA;
|
|
8712
|
+
}
|
|
8713
|
+
var offset = this.pos + headOffset;
|
|
8714
|
+
var object = this.bytes.subarray(offset, offset + byteLength);
|
|
8715
|
+
this.pos += headOffset + byteLength;
|
|
8716
|
+
return object;
|
|
8717
|
+
};
|
|
8718
|
+
Decoder.prototype.decodeExtension = function (size, headOffset) {
|
|
8719
|
+
if (size > this.maxExtLength) {
|
|
8720
|
+
throw new DecodeError("Max length exceeded: ext length (".concat(size, ") > maxExtLength (").concat(this.maxExtLength, ")"));
|
|
8721
|
+
}
|
|
8722
|
+
var extType = this.view.getInt8(this.pos + headOffset);
|
|
8723
|
+
var data = this.decodeBinary(size, headOffset + 1 /* extType */);
|
|
8724
|
+
return this.extensionCodec.decode(data, extType, this.context);
|
|
8725
|
+
};
|
|
8726
|
+
Decoder.prototype.lookU8 = function () {
|
|
8727
|
+
return this.view.getUint8(this.pos);
|
|
8728
|
+
};
|
|
8729
|
+
Decoder.prototype.lookU16 = function () {
|
|
8730
|
+
return this.view.getUint16(this.pos);
|
|
8731
|
+
};
|
|
8732
|
+
Decoder.prototype.lookU32 = function () {
|
|
8733
|
+
return this.view.getUint32(this.pos);
|
|
8734
|
+
};
|
|
8735
|
+
Decoder.prototype.readU8 = function () {
|
|
8736
|
+
var value = this.view.getUint8(this.pos);
|
|
8737
|
+
this.pos++;
|
|
8738
|
+
return value;
|
|
8739
|
+
};
|
|
8740
|
+
Decoder.prototype.readI8 = function () {
|
|
8741
|
+
var value = this.view.getInt8(this.pos);
|
|
8742
|
+
this.pos++;
|
|
8743
|
+
return value;
|
|
8744
|
+
};
|
|
8745
|
+
Decoder.prototype.readU16 = function () {
|
|
8746
|
+
var value = this.view.getUint16(this.pos);
|
|
8747
|
+
this.pos += 2;
|
|
8748
|
+
return value;
|
|
8749
|
+
};
|
|
8750
|
+
Decoder.prototype.readI16 = function () {
|
|
8751
|
+
var value = this.view.getInt16(this.pos);
|
|
8752
|
+
this.pos += 2;
|
|
8753
|
+
return value;
|
|
8754
|
+
};
|
|
8755
|
+
Decoder.prototype.readU32 = function () {
|
|
8756
|
+
var value = this.view.getUint32(this.pos);
|
|
8757
|
+
this.pos += 4;
|
|
8758
|
+
return value;
|
|
8759
|
+
};
|
|
8760
|
+
Decoder.prototype.readI32 = function () {
|
|
8761
|
+
var value = this.view.getInt32(this.pos);
|
|
8762
|
+
this.pos += 4;
|
|
8763
|
+
return value;
|
|
8764
|
+
};
|
|
8765
|
+
Decoder.prototype.readU64 = function () {
|
|
8766
|
+
var value = getUint64(this.view, this.pos);
|
|
8767
|
+
this.pos += 8;
|
|
8768
|
+
return value;
|
|
8769
|
+
};
|
|
8770
|
+
Decoder.prototype.readI64 = function () {
|
|
8771
|
+
var value = getInt64(this.view, this.pos);
|
|
8772
|
+
this.pos += 8;
|
|
8773
|
+
return value;
|
|
8774
|
+
};
|
|
8775
|
+
Decoder.prototype.readF32 = function () {
|
|
8776
|
+
var value = this.view.getFloat32(this.pos);
|
|
8777
|
+
this.pos += 4;
|
|
8778
|
+
return value;
|
|
8779
|
+
};
|
|
8780
|
+
Decoder.prototype.readF64 = function () {
|
|
8781
|
+
var value = this.view.getFloat64(this.pos);
|
|
8782
|
+
this.pos += 8;
|
|
8783
|
+
return value;
|
|
8784
|
+
};
|
|
8785
|
+
return Decoder;
|
|
8786
|
+
}());
|
|
8787
|
+
|
|
8788
|
+
var defaultDecodeOptions = {};
|
|
8789
|
+
/**
|
|
8790
|
+
* It decodes a single MessagePack object in a buffer.
|
|
8791
|
+
*
|
|
8792
|
+
* This is a synchronous decoding function.
|
|
8793
|
+
* See other variants for asynchronous decoding: {@link decodeAsync()}, {@link decodeStream()}, or {@link decodeArrayStream()}.
|
|
8794
|
+
*
|
|
8795
|
+
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
|
|
8796
|
+
* @throws {@link DecodeError} if the buffer contains invalid data.
|
|
8797
|
+
*/
|
|
8798
|
+
function decode(buffer, options) {
|
|
8799
|
+
if (options === void 0) { options = defaultDecodeOptions; }
|
|
8800
|
+
var decoder = new Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength);
|
|
8801
|
+
return decoder.decode(buffer);
|
|
8802
|
+
}
|
|
8803
|
+
/**
|
|
8804
|
+
* It decodes multiple MessagePack objects in a buffer.
|
|
8805
|
+
* This is corresponding to {@link decodeMultiStream()}.
|
|
8806
|
+
*
|
|
8807
|
+
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
|
|
8808
|
+
* @throws {@link DecodeError} if the buffer contains invalid data.
|
|
8809
|
+
*/
|
|
8810
|
+
function decodeMulti(buffer, options) {
|
|
8811
|
+
if (options === void 0) { options = defaultDecodeOptions; }
|
|
8812
|
+
var decoder = new Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength);
|
|
8813
|
+
return decoder.decodeMulti(buffer);
|
|
8814
|
+
}
|
|
8815
|
+
|
|
8816
|
+
// utility for whatwg streams
|
|
8817
|
+
var __generator$1 = (null && null.__generator) || function (thisArg, body) {
|
|
8818
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
8819
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
8820
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
8821
|
+
function step(op) {
|
|
8822
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8823
|
+
while (_) try {
|
|
8824
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
8825
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
8826
|
+
switch (op[0]) {
|
|
8827
|
+
case 0: case 1: t = op; break;
|
|
8828
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
8829
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
8830
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
8831
|
+
default:
|
|
8832
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
8833
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
8834
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
8835
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
8836
|
+
if (t[2]) _.ops.pop();
|
|
8837
|
+
_.trys.pop(); continue;
|
|
8838
|
+
}
|
|
8839
|
+
op = body.call(thisArg, _);
|
|
8840
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
8841
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
8842
|
+
}
|
|
8843
|
+
};
|
|
8844
|
+
var __await = (null && null.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); };
|
|
8845
|
+
var __asyncGenerator = (null && null.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
8846
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
8847
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
8848
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
8849
|
+
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
|
8850
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
8851
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
8852
|
+
function fulfill(value) { resume("next", value); }
|
|
8853
|
+
function reject(value) { resume("throw", value); }
|
|
8854
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
8855
|
+
};
|
|
8856
|
+
function isAsyncIterable(object) {
|
|
8857
|
+
return object[Symbol.asyncIterator] != null;
|
|
8858
|
+
}
|
|
8859
|
+
function assertNonNull(value) {
|
|
8860
|
+
if (value == null) {
|
|
8861
|
+
throw new Error("Assertion Failure: value must not be null nor undefined");
|
|
8862
|
+
}
|
|
8863
|
+
}
|
|
8864
|
+
function asyncIterableFromStream(stream) {
|
|
8865
|
+
return __asyncGenerator(this, arguments, function asyncIterableFromStream_1() {
|
|
8866
|
+
var reader, _a, done, value;
|
|
8867
|
+
return __generator$1(this, function (_b) {
|
|
8868
|
+
switch (_b.label) {
|
|
8869
|
+
case 0:
|
|
8870
|
+
reader = stream.getReader();
|
|
8871
|
+
_b.label = 1;
|
|
8872
|
+
case 1:
|
|
8873
|
+
_b.trys.push([1, , 9, 10]);
|
|
8874
|
+
_b.label = 2;
|
|
8875
|
+
case 2:
|
|
8876
|
+
if (!true) return [3 /*break*/, 8];
|
|
8877
|
+
return [4 /*yield*/, __await(reader.read())];
|
|
8878
|
+
case 3:
|
|
8879
|
+
_a = _b.sent(), done = _a.done, value = _a.value;
|
|
8880
|
+
if (!done) return [3 /*break*/, 5];
|
|
8881
|
+
return [4 /*yield*/, __await(void 0)];
|
|
8882
|
+
case 4: return [2 /*return*/, _b.sent()];
|
|
8883
|
+
case 5:
|
|
8884
|
+
assertNonNull(value);
|
|
8885
|
+
return [4 /*yield*/, __await(value)];
|
|
8886
|
+
case 6: return [4 /*yield*/, _b.sent()];
|
|
8887
|
+
case 7:
|
|
8888
|
+
_b.sent();
|
|
8889
|
+
return [3 /*break*/, 2];
|
|
8890
|
+
case 8: return [3 /*break*/, 10];
|
|
8891
|
+
case 9:
|
|
8892
|
+
reader.releaseLock();
|
|
8893
|
+
return [7 /*endfinally*/];
|
|
8894
|
+
case 10: return [2 /*return*/];
|
|
8895
|
+
}
|
|
8896
|
+
});
|
|
8897
|
+
});
|
|
8898
|
+
}
|
|
8899
|
+
function ensureAsyncIterable(streamLike) {
|
|
8900
|
+
if (isAsyncIterable(streamLike)) {
|
|
8901
|
+
return streamLike;
|
|
8902
|
+
}
|
|
8903
|
+
else {
|
|
8904
|
+
return asyncIterableFromStream(streamLike);
|
|
8905
|
+
}
|
|
8906
|
+
}
|
|
8907
|
+
|
|
8908
|
+
var __awaiter = (null && null.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8909
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8910
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8911
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
8912
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8913
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8914
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8915
|
+
});
|
|
8916
|
+
};
|
|
8917
|
+
var __generator = (null && null.__generator) || function (thisArg, body) {
|
|
8918
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
8919
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
8920
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
8921
|
+
function step(op) {
|
|
8922
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8923
|
+
while (_) try {
|
|
8924
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
8925
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
8926
|
+
switch (op[0]) {
|
|
8927
|
+
case 0: case 1: t = op; break;
|
|
8928
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
8929
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
8930
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
8931
|
+
default:
|
|
8932
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
8933
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
8934
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
8935
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
8936
|
+
if (t[2]) _.ops.pop();
|
|
8937
|
+
_.trys.pop(); continue;
|
|
8938
|
+
}
|
|
8939
|
+
op = body.call(thisArg, _);
|
|
8940
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
8941
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
8942
|
+
}
|
|
8943
|
+
};
|
|
8944
|
+
/**
|
|
8945
|
+
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
|
|
8946
|
+
* @throws {@link DecodeError} if the buffer contains invalid data.
|
|
8947
|
+
*/
|
|
8948
|
+
function decodeAsync(streamLike, options) {
|
|
8949
|
+
if (options === void 0) { options = defaultDecodeOptions; }
|
|
8950
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
8951
|
+
var stream, decoder;
|
|
8952
|
+
return __generator(this, function (_a) {
|
|
8953
|
+
stream = ensureAsyncIterable(streamLike);
|
|
8954
|
+
decoder = new Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength);
|
|
8955
|
+
return [2 /*return*/, decoder.decodeAsync(stream)];
|
|
8956
|
+
});
|
|
8957
|
+
});
|
|
8958
|
+
}
|
|
8959
|
+
/**
|
|
8960
|
+
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
|
|
8961
|
+
* @throws {@link DecodeError} if the buffer contains invalid data.
|
|
8962
|
+
*/
|
|
8963
|
+
function decodeArrayStream(streamLike, options) {
|
|
8964
|
+
if (options === void 0) { options = defaultDecodeOptions; }
|
|
8965
|
+
var stream = ensureAsyncIterable(streamLike);
|
|
8966
|
+
var decoder = new Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength);
|
|
8967
|
+
return decoder.decodeArrayStream(stream);
|
|
8968
|
+
}
|
|
8969
|
+
/**
|
|
8970
|
+
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
|
|
8971
|
+
* @throws {@link DecodeError} if the buffer contains invalid data.
|
|
8972
|
+
*/
|
|
8973
|
+
function decodeMultiStream(streamLike, options) {
|
|
8974
|
+
if (options === void 0) { options = defaultDecodeOptions; }
|
|
8975
|
+
var stream = ensureAsyncIterable(streamLike);
|
|
8976
|
+
var decoder = new Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength);
|
|
8977
|
+
return decoder.decodeStream(stream);
|
|
8978
|
+
}
|
|
8979
|
+
/**
|
|
8980
|
+
* @deprecated Use {@link decodeMultiStream()} instead.
|
|
8981
|
+
*/
|
|
8982
|
+
function decodeStream(streamLike, options) {
|
|
8983
|
+
if (options === void 0) { options = defaultDecodeOptions; }
|
|
8984
|
+
return decodeMultiStream(streamLike, options);
|
|
8985
|
+
}
|
|
8986
|
+
|
|
8987
|
+
// Main Functions:
|
|
8988
|
+
|
|
8989
|
+
var PACKAGE_EXT = 'msx';
|
|
8990
|
+
|
|
8991
|
+
function packDatasets(datasets, opts) {
|
|
8992
|
+
var obj = exportDatasets$1(datasets);
|
|
8993
|
+
// encode options: see https://github.com/msgpack/msgpack-javascript
|
|
8994
|
+
// initialBufferSize number 2048
|
|
8995
|
+
// ignoreUndefined boolean false
|
|
8996
|
+
var content = encode(obj, {});
|
|
8997
|
+
return [{
|
|
8998
|
+
content: content,
|
|
8999
|
+
filename: opts.file || 'output.' + PACKAGE_EXT
|
|
9000
|
+
}];
|
|
9001
|
+
}
|
|
9002
|
+
|
|
9003
|
+
// gui: (optional) gui instance
|
|
9004
|
+
//
|
|
9005
|
+
function exportDatasets$1(datasets) {
|
|
9006
|
+
// TODO: add targets
|
|
9007
|
+
// TODO: add gui state
|
|
9008
|
+
return {
|
|
9009
|
+
version: 1,
|
|
9010
|
+
datasets: datasets.map(exportDataset)
|
|
9011
|
+
};
|
|
9012
|
+
}
|
|
9013
|
+
|
|
9014
|
+
// TODO..
|
|
9015
|
+
// export function serializeSession(catalog) {
|
|
9016
|
+
// var obj = exportDatasets(catalog.getDatasets());
|
|
9017
|
+
// return BSON.serialize(obj);
|
|
9018
|
+
// }
|
|
9019
|
+
|
|
9020
|
+
function exportDataset(dataset) {
|
|
9021
|
+
return Object.assign(dataset, {
|
|
9022
|
+
arcs: dataset.arcs ? exportArcs(dataset.arcs) : null,
|
|
9023
|
+
info: dataset.info ? exportInfo(dataset.info) : null,
|
|
9024
|
+
layers: (dataset.layers || []).map(exportLayer)
|
|
9025
|
+
});
|
|
9026
|
+
}
|
|
9027
|
+
|
|
9028
|
+
function typedArrayToBuffer(arr) {
|
|
9029
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
9030
|
+
}
|
|
9031
|
+
|
|
9032
|
+
function exportArcs(arcs) {
|
|
9033
|
+
var data = arcs.getVertexData();
|
|
9034
|
+
var obj = {
|
|
9035
|
+
nn: typedArrayToBuffer(data.nn),
|
|
9036
|
+
xx: typedArrayToBuffer(data.xx),
|
|
9037
|
+
yy: typedArrayToBuffer(data.yy)
|
|
9038
|
+
};
|
|
9039
|
+
return obj;
|
|
9040
|
+
}
|
|
9041
|
+
|
|
9042
|
+
function exportLayer(lyr) {
|
|
9043
|
+
return {
|
|
9044
|
+
name: lyr.name || null,
|
|
9045
|
+
geometry_type: lyr.geometry_type || null,
|
|
9046
|
+
shapes: lyr.shapes || null,
|
|
9047
|
+
data: lyr.data ? lyr.data.getRecords() : null,
|
|
9048
|
+
menu_order: lyr.menu_order || null,
|
|
9049
|
+
pinned: lyr.pinned || false
|
|
9050
|
+
};
|
|
9051
|
+
}
|
|
9052
|
+
|
|
9053
|
+
function exportInfo(info) {
|
|
9054
|
+
// TODO: export CRS
|
|
9055
|
+
return info;
|
|
9056
|
+
}
|
|
9057
|
+
|
|
7157
9058
|
// Guess the type of a data file from file extension, or return null if not sure
|
|
7158
9059
|
function guessInputFileType(file) {
|
|
7159
9060
|
var ext = getFileExtension(file || '').toLowerCase(),
|
|
@@ -7164,8 +9065,8 @@
|
|
|
7164
9065
|
type = 'json';
|
|
7165
9066
|
} else if (ext == 'csv' || ext == 'tsv' || ext == 'txt' || ext == 'tab') {
|
|
7166
9067
|
type = 'text';
|
|
7167
|
-
} else if (ext ==
|
|
7168
|
-
type =
|
|
9068
|
+
} else if (ext == PACKAGE_EXT) {
|
|
9069
|
+
type = PACKAGE_EXT;
|
|
7169
9070
|
}
|
|
7170
9071
|
return type;
|
|
7171
9072
|
}
|
|
@@ -7212,8 +9113,8 @@
|
|
|
7212
9113
|
return !!type && type != 'gz' && type != 'zip';
|
|
7213
9114
|
}
|
|
7214
9115
|
|
|
7215
|
-
function
|
|
7216
|
-
return
|
|
9116
|
+
function isPackageFile(file) {
|
|
9117
|
+
return file.endsWith('.' + PACKAGE_EXT);
|
|
7217
9118
|
}
|
|
7218
9119
|
|
|
7219
9120
|
function isZipFile(file) {
|
|
@@ -7242,7 +9143,7 @@
|
|
|
7242
9143
|
dbf: 'DBF',
|
|
7243
9144
|
kml: 'KML',
|
|
7244
9145
|
kmz: 'KMZ',
|
|
7245
|
-
|
|
9146
|
+
[PACKAGE_EXT]: 'Mapshaper project',
|
|
7246
9147
|
shapefile: 'Shapefile',
|
|
7247
9148
|
svg: 'SVG'
|
|
7248
9149
|
}[fmt] || '';
|
|
@@ -7251,13 +9152,13 @@
|
|
|
7251
9152
|
// Assumes file at @path is one of Mapshaper's supported file types
|
|
7252
9153
|
function isSupportedBinaryInputType(path) {
|
|
7253
9154
|
var ext = getFileExtension(path).toLowerCase();
|
|
7254
|
-
return ext == 'shp' || ext == 'shx' || ext == 'dbf' || ext ==
|
|
9155
|
+
return ext == 'shp' || ext == 'shx' || ext == 'dbf' || ext == PACKAGE_EXT; // GUI also supports zip files
|
|
7255
9156
|
}
|
|
7256
9157
|
|
|
7257
9158
|
function isImportableAsBinary(path) {
|
|
7258
9159
|
var type = guessInputFileType(path);
|
|
7259
9160
|
return isSupportedBinaryInputType(path) || isZipFile(path) ||
|
|
7260
|
-
isGzipFile(path) || isKmzFile(path) ||
|
|
9161
|
+
isGzipFile(path) || isKmzFile(path) || isPackageFile(path) ||
|
|
7261
9162
|
type == 'json' || type == 'text';
|
|
7262
9163
|
}
|
|
7263
9164
|
|
|
@@ -7277,7 +9178,7 @@
|
|
|
7277
9178
|
couldBeDsvFile: couldBeDsvFile,
|
|
7278
9179
|
looksLikeImportableFile: looksLikeImportableFile,
|
|
7279
9180
|
looksLikeContentFile: looksLikeContentFile,
|
|
7280
|
-
|
|
9181
|
+
isPackageFile: isPackageFile,
|
|
7281
9182
|
isZipFile: isZipFile,
|
|
7282
9183
|
isKmzFile: isKmzFile,
|
|
7283
9184
|
isGzipFile: isGzipFile,
|
|
@@ -9836,8 +11737,10 @@
|
|
|
9836
11737
|
if (buf instanceof ArrayBuffer) {
|
|
9837
11738
|
buf = new Uint8Array(buf);
|
|
9838
11739
|
}
|
|
9839
|
-
|
|
9840
|
-
|
|
11740
|
+
unzip(buf, {filter: fflateFilter}, function(err, data) {
|
|
11741
|
+
if (err) cb(err);
|
|
11742
|
+
cb(null, fflatePostprocess(data));
|
|
11743
|
+
});
|
|
9841
11744
|
}
|
|
9842
11745
|
|
|
9843
11746
|
function zipSync(files) {
|
|
@@ -19163,69 +21066,6 @@ ${svg}
|
|
|
19163
21066
|
}];
|
|
19164
21067
|
}
|
|
19165
21068
|
|
|
19166
|
-
function exportBSON(datasets, opts) {
|
|
19167
|
-
var { serialize } = require('bson');
|
|
19168
|
-
var obj = exportDatasets$1(datasets);
|
|
19169
|
-
var content = serialize(obj);
|
|
19170
|
-
return [{
|
|
19171
|
-
content: content,
|
|
19172
|
-
filename: opts.file || 'output.mshp'
|
|
19173
|
-
}];
|
|
19174
|
-
}
|
|
19175
|
-
|
|
19176
|
-
// gui: (optional) gui instance
|
|
19177
|
-
//
|
|
19178
|
-
function exportDatasets$1(datasets) {
|
|
19179
|
-
// TODO: add targets
|
|
19180
|
-
// TODO: add gui state
|
|
19181
|
-
return {
|
|
19182
|
-
version: 1,
|
|
19183
|
-
datasets: datasets.map(exportDataset)
|
|
19184
|
-
};
|
|
19185
|
-
}
|
|
19186
|
-
|
|
19187
|
-
// TODO..
|
|
19188
|
-
// export function serializeSession(catalog) {
|
|
19189
|
-
// var obj = exportDatasets(catalog.getDatasets());
|
|
19190
|
-
// return BSON.serialize(obj);
|
|
19191
|
-
// }
|
|
19192
|
-
|
|
19193
|
-
function exportDataset(dataset) {
|
|
19194
|
-
return Object.assign(dataset, {
|
|
19195
|
-
arcs: dataset.arcs ? exportArcs(dataset.arcs) : null,
|
|
19196
|
-
info: dataset.info ? exportInfo(dataset.info) : null,
|
|
19197
|
-
layers: (dataset.layers || []).map(exportLayer)
|
|
19198
|
-
});
|
|
19199
|
-
}
|
|
19200
|
-
|
|
19201
|
-
function typedArrayToBuffer(arr) {
|
|
19202
|
-
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
19203
|
-
}
|
|
19204
|
-
|
|
19205
|
-
function exportArcs(arcs) {
|
|
19206
|
-
var data = arcs.getVertexData();
|
|
19207
|
-
var obj = {
|
|
19208
|
-
nn: typedArrayToBuffer(data.nn),
|
|
19209
|
-
xx: typedArrayToBuffer(data.xx),
|
|
19210
|
-
yy: typedArrayToBuffer(data.yy)
|
|
19211
|
-
};
|
|
19212
|
-
return obj;
|
|
19213
|
-
}
|
|
19214
|
-
|
|
19215
|
-
function exportLayer(lyr) {
|
|
19216
|
-
return {
|
|
19217
|
-
name: lyr.name || null,
|
|
19218
|
-
geometry_type: lyr.geometry_type || null,
|
|
19219
|
-
shapes: lyr.shapes || null,
|
|
19220
|
-
data: lyr.data ? lyr.data.getRecords() : null
|
|
19221
|
-
};
|
|
19222
|
-
}
|
|
19223
|
-
|
|
19224
|
-
function exportInfo(info) {
|
|
19225
|
-
// TODO: export CRS
|
|
19226
|
-
return info;
|
|
19227
|
-
}
|
|
19228
|
-
|
|
19229
21069
|
function exportRecordsAsFixedWidthString(fields, records, opts) {
|
|
19230
21070
|
var rows = [], col;
|
|
19231
21071
|
for (var i=0; i<fields.length; i++) {
|
|
@@ -20288,8 +22128,8 @@ ${svg}
|
|
|
20288
22128
|
function exportDatasets(datasets, opts) {
|
|
20289
22129
|
var format = getOutputFormat(datasets[0], opts);
|
|
20290
22130
|
var files;
|
|
20291
|
-
if (format ==
|
|
20292
|
-
return
|
|
22131
|
+
if (format == PACKAGE_EXT) {
|
|
22132
|
+
return packDatasets(datasets, opts);
|
|
20293
22133
|
}
|
|
20294
22134
|
if (format == 'kml' || format == 'svg' || format == 'topojson' || format == 'geojson' && opts.combine_layers) {
|
|
20295
22135
|
// multi-layer formats: combine multiple datasets into one
|
|
@@ -20312,7 +22152,7 @@ ${svg}
|
|
|
20312
22152
|
}
|
|
20313
22153
|
files = datasets.reduce(function(memo, dataset) {
|
|
20314
22154
|
if (runningInBrowser()) {
|
|
20315
|
-
utils.sortOn(dataset.layers, '
|
|
22155
|
+
utils.sortOn(dataset.layers, 'menu_order', true);
|
|
20316
22156
|
} else {
|
|
20317
22157
|
// kludge to export layers in order that target= option or previous
|
|
20318
22158
|
// -target command matched them (useful mainly for SVG output)
|
|
@@ -20392,7 +22232,7 @@ ${svg}
|
|
|
20392
22232
|
}
|
|
20393
22233
|
|
|
20394
22234
|
var exporters = {
|
|
20395
|
-
bson:
|
|
22235
|
+
bson: packDatasets,
|
|
20396
22236
|
geojson: exportGeoJSON2,
|
|
20397
22237
|
topojson: exportTopoJSON,
|
|
20398
22238
|
shapefile: exportShapefile,
|
|
@@ -21715,10 +23555,12 @@ ${svg}
|
|
|
21715
23555
|
}
|
|
21716
23556
|
|
|
21717
23557
|
if (!optDef) {
|
|
23558
|
+
// REMOVING quote trimming -- it prevents the use of quoted commands in -run (for example)
|
|
21718
23559
|
// token is not a defined option; add it to _ array for later processing
|
|
21719
23560
|
// Stripping surrounding quotes here, although this may not be necessary since
|
|
21720
23561
|
// (some, most, all?) shells seem to remove quotes.
|
|
21721
|
-
cmd._.push(utils.trimQuotes(token));
|
|
23562
|
+
// cmd._.push(utils.trimQuotes(token));
|
|
23563
|
+
cmd._.push(token);
|
|
21722
23564
|
return;
|
|
21723
23565
|
}
|
|
21724
23566
|
|
|
@@ -25366,9 +27208,9 @@ ${svg}
|
|
|
25366
27208
|
stop("Unsupported Shapefile type:", shpType);
|
|
25367
27209
|
}
|
|
25368
27210
|
if (ShpType.isZType(shpType)) {
|
|
25369
|
-
|
|
27211
|
+
verbose("Warning: Shapefile Z data will be lost.");
|
|
25370
27212
|
} else if (ShpType.isMType(shpType)) {
|
|
25371
|
-
|
|
27213
|
+
verbose("Warning: Shapefile M data will be lost.");
|
|
25372
27214
|
}
|
|
25373
27215
|
|
|
25374
27216
|
// TODO: test cases: null shape; non-null shape with no valid parts
|
|
@@ -26568,6 +28410,9 @@ ${svg}
|
|
|
26568
28410
|
if (obj.cpg) {
|
|
26569
28411
|
// TODO: consider using the input encoding as the default output encoding
|
|
26570
28412
|
dataset.info.cpg = obj.cpg.content;
|
|
28413
|
+
if (typeof dataset.info.cpg != 'string') {
|
|
28414
|
+
error('Invalid encoding argument, expected a string');
|
|
28415
|
+
}
|
|
26571
28416
|
}
|
|
26572
28417
|
return dataset;
|
|
26573
28418
|
}
|
|
@@ -26603,12 +28448,8 @@ ${svg}
|
|
|
26603
28448
|
// Import datasets contained in a BSON blob
|
|
26604
28449
|
// Return command target as a dataset
|
|
26605
28450
|
//
|
|
26606
|
-
function
|
|
26607
|
-
var
|
|
26608
|
-
var obj = deserialize(buf, {
|
|
26609
|
-
promoteBuffers: true,
|
|
26610
|
-
promoteValues: true
|
|
26611
|
-
});
|
|
28451
|
+
function unpackSession(buf) {
|
|
28452
|
+
var obj = decode(buf, {});
|
|
26612
28453
|
if (!isValidSession(obj)) {
|
|
26613
28454
|
stop('Invalid mapshaper session data object');
|
|
26614
28455
|
}
|
|
@@ -26681,10 +28522,10 @@ ${svg}
|
|
|
26681
28522
|
stop('Missing importable files');
|
|
26682
28523
|
}
|
|
26683
28524
|
|
|
26684
|
-
// special case:
|
|
26685
|
-
if (files.some(
|
|
28525
|
+
// special case: package file
|
|
28526
|
+
if (files.some(isPackageFile)) {
|
|
26686
28527
|
if (files.length > 1) {
|
|
26687
|
-
stop('Expected a single
|
|
28528
|
+
stop('Expected a single package file');
|
|
26688
28529
|
}
|
|
26689
28530
|
dataset = importMshpFile(files[0], catalog, opts);
|
|
26690
28531
|
return dataset;
|
|
@@ -26707,7 +28548,7 @@ ${svg}
|
|
|
26707
28548
|
|
|
26708
28549
|
function importMshpFile(file, catalog, opts) {
|
|
26709
28550
|
var buf = cli.readFile(file, null, opts.input);
|
|
26710
|
-
var obj =
|
|
28551
|
+
var obj = unpackSession(buf);
|
|
26711
28552
|
obj.datasets.forEach(function(dataset) {
|
|
26712
28553
|
catalog.addDataset(dataset);
|
|
26713
28554
|
});
|
|
@@ -42437,7 +44278,7 @@ ${svg}
|
|
|
42437
44278
|
var ctx = getBaseContext();
|
|
42438
44279
|
var output, targetData;
|
|
42439
44280
|
// TODO: throw an informative error if target is used when there are multiple targets
|
|
42440
|
-
if (targets.length == 1) {
|
|
44281
|
+
if (targets && targets.length == 1) {
|
|
42441
44282
|
targetData = getRunCommandData(targets[0]);
|
|
42442
44283
|
Object.defineProperty(ctx, 'target', {value: targetData});
|
|
42443
44284
|
}
|
|
@@ -44635,6 +46476,7 @@ ${svg}
|
|
|
44635
46476
|
function commandAcceptsEmptyTarget(name) {
|
|
44636
46477
|
return name == 'graticule' || name == 'i' || name == 'help' ||
|
|
44637
46478
|
name == 'point-grid' || name == 'shape' || name == 'rectangle' ||
|
|
46479
|
+
name == 'require' || name == 'run' || name == 'define' ||
|
|
44638
46480
|
name == 'include' || name == 'print' || name == 'comment' || name == 'if' || name == 'elif' ||
|
|
44639
46481
|
name == 'else' || name == 'endif' || name == 'stop';
|
|
44640
46482
|
}
|