@loaders.gl/json 4.4.0-alpha.2 → 4.4.0-alpha.9

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.
Files changed (62) hide show
  1. package/dist/dist.dev.js +260 -24
  2. package/dist/dist.min.js +11 -11
  3. package/dist/geojson-loader.d.ts.map +1 -1
  4. package/dist/geojson-loader.js +4 -2
  5. package/dist/geojson-loader.js.map +1 -0
  6. package/dist/geojson-worker.js +239 -22
  7. package/dist/geojson-writer.js +1 -0
  8. package/dist/geojson-writer.js.map +1 -0
  9. package/dist/index.cjs +222 -30
  10. package/dist/index.cjs.map +4 -4
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/json-loader.js +2 -1
  14. package/dist/json-loader.js.map +1 -0
  15. package/dist/json-writer.js +1 -0
  16. package/dist/json-writer.js.map +1 -0
  17. package/dist/lib/clarinet/clarinet.js +1 -0
  18. package/dist/lib/clarinet/clarinet.js.map +1 -0
  19. package/dist/lib/encoder-utils/encode-table-row.js +1 -0
  20. package/dist/lib/encoder-utils/encode-table-row.js.map +1 -0
  21. package/dist/lib/encoder-utils/encode-utils.js +1 -0
  22. package/dist/lib/encoder-utils/encode-utils.js.map +1 -0
  23. package/dist/lib/encoder-utils/utf8-encoder.d.ts +1 -1
  24. package/dist/lib/encoder-utils/utf8-encoder.d.ts.map +1 -1
  25. package/dist/lib/encoder-utils/utf8-encoder.js +3 -1
  26. package/dist/lib/encoder-utils/utf8-encoder.js.map +1 -0
  27. package/dist/lib/encoders/geojson-encoder.js +1 -0
  28. package/dist/lib/encoders/geojson-encoder.js.map +1 -0
  29. package/dist/lib/encoders/json-encoder.js +1 -0
  30. package/dist/lib/encoders/json-encoder.js.map +1 -0
  31. package/dist/lib/json-parser/json-parser.js +1 -0
  32. package/dist/lib/json-parser/json-parser.js.map +1 -0
  33. package/dist/lib/json-parser/streaming-json-parser.js +1 -0
  34. package/dist/lib/json-parser/streaming-json-parser.js.map +1 -0
  35. package/dist/lib/jsonpath/jsonpath.d.ts.map +1 -1
  36. package/dist/lib/jsonpath/jsonpath.js +213 -18
  37. package/dist/lib/jsonpath/jsonpath.js.map +1 -0
  38. package/dist/lib/parsers/parse-json-in-batches.d.ts +1 -1
  39. package/dist/lib/parsers/parse-json-in-batches.d.ts.map +1 -1
  40. package/dist/lib/parsers/parse-json-in-batches.js +5 -4
  41. package/dist/lib/parsers/parse-json-in-batches.js.map +1 -0
  42. package/dist/lib/parsers/parse-json.js +1 -0
  43. package/dist/lib/parsers/parse-json.js.map +1 -0
  44. package/dist/lib/parsers/parse-ndjson-in-batches.d.ts +1 -1
  45. package/dist/lib/parsers/parse-ndjson-in-batches.d.ts.map +1 -1
  46. package/dist/lib/parsers/parse-ndjson-in-batches.js +4 -3
  47. package/dist/lib/parsers/parse-ndjson-in-batches.js.map +1 -0
  48. package/dist/lib/parsers/parse-ndjson.js +1 -0
  49. package/dist/lib/parsers/parse-ndjson.js.map +1 -0
  50. package/dist/ndgeoson-loader.js +2 -1
  51. package/dist/ndgeoson-loader.js.map +1 -0
  52. package/dist/ndjson-loader.js +2 -1
  53. package/dist/ndjson-loader.js.map +1 -0
  54. package/dist/workers/geojson-worker.js +1 -0
  55. package/dist/workers/geojson-worker.js.map +1 -0
  56. package/package.json +9 -6
  57. package/src/geojson-loader.ts +2 -1
  58. package/src/json-loader.ts +3 -1
  59. package/src/lib/encoder-utils/utf8-encoder.ts +4 -2
  60. package/src/lib/jsonpath/jsonpath.ts +268 -21
  61. package/src/lib/parsers/parse-json-in-batches.ts +7 -5
  62. package/src/lib/parsers/parse-ndjson-in-batches.ts +7 -4
package/dist/dist.dev.js CHANGED
@@ -12653,6 +12653,9 @@ return true;`);
12653
12653
  }
12654
12654
  }
12655
12655
 
12656
+ // ../loader-utils/src/lib/javascript-utils/is-type.ts
12657
+ var isSharedArrayBuffer = (value) => typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer;
12658
+
12656
12659
  // ../loader-utils/src/lib/binary-utils/array-buffer-utils.ts
12657
12660
  function concatenateArrayBuffers(...sources) {
12658
12661
  return concatenateArrayBuffersFromArray(sources);
@@ -12705,10 +12708,52 @@ return true;`);
12705
12708
  async function concatenateArrayBuffersAsync(asyncIterator) {
12706
12709
  const arrayBuffers = [];
12707
12710
  for await (const chunk of asyncIterator) {
12708
- arrayBuffers.push(chunk);
12711
+ arrayBuffers.push(copyToArrayBuffer(chunk));
12709
12712
  }
12710
12713
  return concatenateArrayBuffers(...arrayBuffers);
12711
12714
  }
12715
+ async function* toArrayBufferIterator(asyncIterator) {
12716
+ for await (const chunk of asyncIterator) {
12717
+ yield copyToArrayBuffer(chunk);
12718
+ }
12719
+ }
12720
+ function copyToArrayBuffer(chunk) {
12721
+ if (chunk instanceof ArrayBuffer) {
12722
+ return chunk;
12723
+ }
12724
+ if (ArrayBuffer.isView(chunk)) {
12725
+ const { buffer, byteOffset, byteLength } = chunk;
12726
+ return copyFromBuffer(buffer, byteOffset, byteLength);
12727
+ }
12728
+ return copyFromBuffer(chunk);
12729
+ }
12730
+ function copyFromBuffer(buffer, byteOffset = 0, byteLength = buffer.byteLength - byteOffset) {
12731
+ const view = new Uint8Array(buffer, byteOffset, byteLength);
12732
+ const copy2 = new Uint8Array(view.length);
12733
+ copy2.set(view);
12734
+ return copy2.buffer;
12735
+ }
12736
+
12737
+ // ../loader-utils/src/lib/binary-utils/memory-conversion-utils.ts
12738
+ function ensureArrayBuffer(bufferSource) {
12739
+ if (bufferSource instanceof ArrayBuffer) {
12740
+ return bufferSource;
12741
+ }
12742
+ if (isSharedArrayBuffer(bufferSource)) {
12743
+ return copyToArrayBuffer2(bufferSource);
12744
+ }
12745
+ const { buffer, byteOffset, byteLength } = bufferSource;
12746
+ if (buffer instanceof ArrayBuffer && byteOffset === 0 && byteLength === buffer.byteLength) {
12747
+ return buffer;
12748
+ }
12749
+ return copyToArrayBuffer2(buffer, byteOffset, byteLength);
12750
+ }
12751
+ function copyToArrayBuffer2(buffer, byteOffset = 0, byteLength = buffer.byteLength - byteOffset) {
12752
+ const view = new Uint8Array(buffer, byteOffset, byteLength);
12753
+ const copy2 = new Uint8Array(view.length);
12754
+ copy2.set(view);
12755
+ return copy2.buffer;
12756
+ }
12712
12757
 
12713
12758
  // src/lib/clarinet/clarinet.ts
12714
12759
  var MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;
@@ -13213,27 +13258,13 @@ Char: ${this.c}`;
13213
13258
  var JSONPath = class {
13214
13259
  path;
13215
13260
  constructor(path = null) {
13216
- this.path = ["$"];
13217
- if (path instanceof JSONPath) {
13218
- this.path = [...path.path];
13219
- return;
13220
- }
13221
- if (Array.isArray(path)) {
13222
- this.path.push(...path);
13223
- return;
13224
- }
13225
- if (typeof path === "string") {
13226
- this.path = path.split(".");
13227
- if (this.path[0] !== "$") {
13228
- throw new Error("JSONPaths must start with $");
13229
- }
13230
- }
13261
+ this.path = parseJsonPath(path);
13231
13262
  }
13232
13263
  clone() {
13233
13264
  return new JSONPath(this);
13234
13265
  }
13235
13266
  toString() {
13236
- return this.path.join(".");
13267
+ return formatJsonPath(this.path);
13237
13268
  }
13238
13269
  push(name) {
13239
13270
  this.path.push(name);
@@ -13285,6 +13316,210 @@ Char: ${this.c}`;
13285
13316
  return object[field];
13286
13317
  }
13287
13318
  };
13319
+ function parseJsonPath(path) {
13320
+ if (path instanceof JSONPath) {
13321
+ return [...path.path];
13322
+ }
13323
+ if (Array.isArray(path)) {
13324
+ return ["$"].concat(path);
13325
+ }
13326
+ if (typeof path === "string") {
13327
+ return parseJsonPathString(path);
13328
+ }
13329
+ return ["$"];
13330
+ }
13331
+ function parseJsonPathString(pathString) {
13332
+ const trimmedPath = pathString.trim();
13333
+ if (!trimmedPath.startsWith("$")) {
13334
+ throw new Error("JSONPath must start with $");
13335
+ }
13336
+ const segments = ["$"];
13337
+ let index = 1;
13338
+ let arrayElementSelectorEncountered = false;
13339
+ while (index < trimmedPath.length) {
13340
+ const character = trimmedPath[index];
13341
+ if (character === ".") {
13342
+ if (arrayElementSelectorEncountered) {
13343
+ throw new Error("JSONPath cannot select fields after array element selectors");
13344
+ }
13345
+ index += 1;
13346
+ if (trimmedPath[index] === ".") {
13347
+ throw new Error("JSONPath descendant selectors (..) are not supported");
13348
+ }
13349
+ const { value, nextIndex, isWildcard } = parseDotSegment(trimmedPath, index);
13350
+ if (isWildcard) {
13351
+ if (nextIndex < trimmedPath.length) {
13352
+ throw new Error("JSONPath wildcard selectors must terminate the path");
13353
+ }
13354
+ arrayElementSelectorEncountered = true;
13355
+ index = nextIndex;
13356
+ continue;
13357
+ }
13358
+ segments.push(value);
13359
+ index = nextIndex;
13360
+ continue;
13361
+ }
13362
+ if (character === "[") {
13363
+ const parsedSegment = parseBracketSegment(trimmedPath, index);
13364
+ if (parsedSegment.type === "property") {
13365
+ if (arrayElementSelectorEncountered) {
13366
+ throw new Error("JSONPath cannot select fields after array element selectors");
13367
+ }
13368
+ segments.push(parsedSegment.value);
13369
+ } else {
13370
+ arrayElementSelectorEncountered = true;
13371
+ }
13372
+ index = parsedSegment.nextIndex;
13373
+ continue;
13374
+ }
13375
+ if (character === "@") {
13376
+ throw new Error("JSONPath current node selector (@) is not supported");
13377
+ }
13378
+ if (character.trim() === "") {
13379
+ index += 1;
13380
+ continue;
13381
+ }
13382
+ throw new Error(`Unexpected character "${character}" in JSONPath`);
13383
+ }
13384
+ return segments;
13385
+ }
13386
+ function parseDotSegment(pathString, startIndex) {
13387
+ if (startIndex >= pathString.length) {
13388
+ throw new Error("JSONPath cannot end with a period");
13389
+ }
13390
+ if (pathString[startIndex] === "*") {
13391
+ return { value: "*", nextIndex: startIndex + 1, isWildcard: true };
13392
+ }
13393
+ const firstCharacter = pathString[startIndex];
13394
+ if (firstCharacter === "@") {
13395
+ throw new Error("JSONPath current node selector (@) is not supported");
13396
+ }
13397
+ if (!isIdentifierStartCharacter(firstCharacter)) {
13398
+ throw new Error("JSONPath property names after period must start with a letter, $ or _");
13399
+ }
13400
+ let endIndex = startIndex + 1;
13401
+ while (endIndex < pathString.length && isIdentifierCharacter(pathString[endIndex])) {
13402
+ endIndex++;
13403
+ }
13404
+ if (endIndex === startIndex) {
13405
+ throw new Error("JSONPath is missing a property name after period");
13406
+ }
13407
+ return {
13408
+ value: pathString.slice(startIndex, endIndex),
13409
+ nextIndex: endIndex,
13410
+ isWildcard: false
13411
+ };
13412
+ }
13413
+ function parseBracketSegment(pathString, startIndex) {
13414
+ const contentStartIndex = startIndex + 1;
13415
+ if (contentStartIndex >= pathString.length) {
13416
+ throw new Error("JSONPath has unterminated bracket");
13417
+ }
13418
+ const firstCharacter = pathString[contentStartIndex];
13419
+ if (firstCharacter === "'" || firstCharacter === '"') {
13420
+ const { value, nextIndex } = parseBracketProperty(pathString, contentStartIndex);
13421
+ return { type: "property", value, nextIndex };
13422
+ }
13423
+ const closingBracketIndex = pathString.indexOf("]", contentStartIndex);
13424
+ if (closingBracketIndex === -1) {
13425
+ throw new Error("JSONPath has unterminated bracket");
13426
+ }
13427
+ const content = pathString.slice(contentStartIndex, closingBracketIndex).trim();
13428
+ const unsupportedSelectorMessage = getUnsupportedBracketSelectorMessage(content);
13429
+ if (unsupportedSelectorMessage) {
13430
+ throw new Error(unsupportedSelectorMessage);
13431
+ }
13432
+ if (content === "*") {
13433
+ return { type: "array-selector", nextIndex: closingBracketIndex + 1 };
13434
+ }
13435
+ if (/^\d+$/.test(content)) {
13436
+ throw new Error("JSONPath array index selectors are not supported");
13437
+ }
13438
+ if (/^\d*\s*:\s*\d*(\s*:\s*\d*)?$/.test(content)) {
13439
+ return { type: "array-selector", nextIndex: closingBracketIndex + 1 };
13440
+ }
13441
+ throw new Error(`Unsupported bracket selector "[${content}]" in JSONPath`);
13442
+ }
13443
+ function getUnsupportedBracketSelectorMessage(content) {
13444
+ if (!content.length) {
13445
+ return "JSONPath bracket selectors cannot be empty";
13446
+ }
13447
+ if (content.startsWith("(")) {
13448
+ return "JSONPath script selectors are not supported";
13449
+ }
13450
+ if (content.startsWith("?")) {
13451
+ return "JSONPath filter selectors are not supported";
13452
+ }
13453
+ if (content.includes(",")) {
13454
+ return "JSONPath union selectors are not supported";
13455
+ }
13456
+ if (content.startsWith("@") || content.includes("@.")) {
13457
+ return "JSONPath current node selector (@) is not supported";
13458
+ }
13459
+ return null;
13460
+ }
13461
+ function parseBracketProperty(pathString, startIndex) {
13462
+ const quoteCharacter = pathString[startIndex];
13463
+ let index = startIndex + 1;
13464
+ let value = "";
13465
+ let terminated = false;
13466
+ while (index < pathString.length) {
13467
+ const character = pathString[index];
13468
+ if (character === "\\") {
13469
+ index += 1;
13470
+ if (index >= pathString.length) {
13471
+ break;
13472
+ }
13473
+ value += pathString[index];
13474
+ index += 1;
13475
+ continue;
13476
+ }
13477
+ if (character === quoteCharacter) {
13478
+ terminated = true;
13479
+ index += 1;
13480
+ break;
13481
+ }
13482
+ value += character;
13483
+ index += 1;
13484
+ }
13485
+ if (!terminated) {
13486
+ throw new Error("JSONPath string in bracket property selector is unterminated");
13487
+ }
13488
+ while (index < pathString.length && pathString[index].trim() === "") {
13489
+ index += 1;
13490
+ }
13491
+ if (pathString[index] !== "]") {
13492
+ throw new Error("JSONPath property selectors must end with ]");
13493
+ }
13494
+ if (!value.length) {
13495
+ throw new Error("JSONPath property selectors cannot be empty");
13496
+ }
13497
+ return { value, nextIndex: index + 1 };
13498
+ }
13499
+ function isIdentifierCharacter(character) {
13500
+ return /[a-zA-Z0-9$_]/.test(character);
13501
+ }
13502
+ function isIdentifierStartCharacter(character) {
13503
+ return /[a-zA-Z_$]/.test(character);
13504
+ }
13505
+ function isIdentifierSegment(segment) {
13506
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(segment);
13507
+ }
13508
+ function formatJsonPath(path) {
13509
+ return path.map((segment, index) => {
13510
+ if (index === 0) {
13511
+ return segment;
13512
+ }
13513
+ if (segment === "*") {
13514
+ return ".*";
13515
+ }
13516
+ if (isIdentifierSegment(segment)) {
13517
+ return `.${segment}`;
13518
+ }
13519
+ const escapedSegment = segment.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
13520
+ return `['${escapedSegment}']`;
13521
+ }).join("");
13522
+ }
13288
13523
 
13289
13524
  // src/lib/json-parser/json-parser.ts
13290
13525
  var JSONParser = class {
@@ -13464,12 +13699,12 @@ Char: ${this.c}`;
13464
13699
 
13465
13700
  // src/lib/parsers/parse-json-in-batches.ts
13466
13701
  async function* parseJSONInBatches(binaryAsyncIterator, options) {
13467
- const asyncIterator = makeTextDecoderIterator(binaryAsyncIterator);
13468
- const { metadata } = options;
13702
+ const asyncIterator = makeTextDecoderIterator(toArrayBufferIterator(binaryAsyncIterator));
13703
+ const metadata = Boolean(options?.core?.metadata || options?.metadata);
13469
13704
  const { jsonpaths } = options.json || {};
13470
13705
  let isFirstChunk = true;
13471
13706
  const schema = null;
13472
- const tableBatchBuilder = new TableBatchBuilder(schema, options);
13707
+ const tableBatchBuilder = new TableBatchBuilder(schema, options?.core);
13473
13708
  const parser = new StreamingJSONParser({ jsonpaths });
13474
13709
  for await (const chunk of asyncIterator) {
13475
13710
  const rows = parser.write(chunk);
@@ -13589,13 +13824,13 @@ Char: ${this.c}`;
13589
13824
 
13590
13825
  // src/lib/parsers/parse-ndjson-in-batches.ts
13591
13826
  async function* parseNDJSONInBatches(binaryAsyncIterator, options) {
13592
- const textIterator = makeTextDecoderIterator(binaryAsyncIterator);
13827
+ const textIterator = makeTextDecoderIterator(toArrayBufferIterator(binaryAsyncIterator));
13593
13828
  const lineIterator = makeLineIterator(textIterator);
13594
13829
  const numberedLineIterator = makeNumberedLineIterator(lineIterator);
13595
13830
  const schema = null;
13596
13831
  const shape = "row-table";
13597
13832
  const tableBatchBuilder = new TableBatchBuilder(schema, {
13598
- ...options,
13833
+ ...options?.core || options,
13599
13834
  shape
13600
13835
  });
13601
13836
  for await (const { counter, line } of numberedLineIterator) {
@@ -14703,7 +14938,8 @@ Char: ${this.c}`;
14703
14938
  }
14704
14939
  function parseInBatches2(asyncIterator, options) {
14705
14940
  options = { ...GeoJSONLoader.options, ...options };
14706
- options.json = { ...GeoJSONLoader.options.geojson, ...options.geojson };
14941
+ options.json = { ...GeoJSONLoader.options.json, ...options.json };
14942
+ options.geojson = { ...GeoJSONLoader.options.geojson, ...options.geojson };
14707
14943
  const geojsonIterator = parseJSONInBatches(asyncIterator, options);
14708
14944
  switch (options.gis.format) {
14709
14945
  case "binary":
@@ -14798,7 +15034,7 @@ Char: ${this.c}`;
14798
15034
  return this.totalLength >= this.chunkSize;
14799
15035
  }
14800
15036
  getArrayBufferBatch() {
14801
- return this.textEncoder.encode(this.getStringBatch()).buffer;
15037
+ return ensureArrayBuffer(this.textEncoder.encode(this.getStringBatch()).buffer);
14802
15038
  }
14803
15039
  getStringBatch() {
14804
15040
  const stringChunk = this.strings.join("");