@loaders.gl/json 4.4.0-alpha.1 → 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 +10 -7
  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/index.cjs CHANGED
@@ -597,27 +597,13 @@ function checkBufferLength(parser) {
597
597
  var JSONPath = class {
598
598
  path;
599
599
  constructor(path = null) {
600
- this.path = ["$"];
601
- if (path instanceof JSONPath) {
602
- this.path = [...path.path];
603
- return;
604
- }
605
- if (Array.isArray(path)) {
606
- this.path.push(...path);
607
- return;
608
- }
609
- if (typeof path === "string") {
610
- this.path = path.split(".");
611
- if (this.path[0] !== "$") {
612
- throw new Error("JSONPaths must start with $");
613
- }
614
- }
600
+ this.path = parseJsonPath(path);
615
601
  }
616
602
  clone() {
617
603
  return new JSONPath(this);
618
604
  }
619
605
  toString() {
620
- return this.path.join(".");
606
+ return formatJsonPath(this.path);
621
607
  }
622
608
  push(name) {
623
609
  this.path.push(name);
@@ -669,6 +655,210 @@ var JSONPath = class {
669
655
  return object[field];
670
656
  }
671
657
  };
658
+ function parseJsonPath(path) {
659
+ if (path instanceof JSONPath) {
660
+ return [...path.path];
661
+ }
662
+ if (Array.isArray(path)) {
663
+ return ["$"].concat(path);
664
+ }
665
+ if (typeof path === "string") {
666
+ return parseJsonPathString(path);
667
+ }
668
+ return ["$"];
669
+ }
670
+ function parseJsonPathString(pathString) {
671
+ const trimmedPath = pathString.trim();
672
+ if (!trimmedPath.startsWith("$")) {
673
+ throw new Error("JSONPath must start with $");
674
+ }
675
+ const segments = ["$"];
676
+ let index = 1;
677
+ let arrayElementSelectorEncountered = false;
678
+ while (index < trimmedPath.length) {
679
+ const character = trimmedPath[index];
680
+ if (character === ".") {
681
+ if (arrayElementSelectorEncountered) {
682
+ throw new Error("JSONPath cannot select fields after array element selectors");
683
+ }
684
+ index += 1;
685
+ if (trimmedPath[index] === ".") {
686
+ throw new Error("JSONPath descendant selectors (..) are not supported");
687
+ }
688
+ const { value, nextIndex, isWildcard } = parseDotSegment(trimmedPath, index);
689
+ if (isWildcard) {
690
+ if (nextIndex < trimmedPath.length) {
691
+ throw new Error("JSONPath wildcard selectors must terminate the path");
692
+ }
693
+ arrayElementSelectorEncountered = true;
694
+ index = nextIndex;
695
+ continue;
696
+ }
697
+ segments.push(value);
698
+ index = nextIndex;
699
+ continue;
700
+ }
701
+ if (character === "[") {
702
+ const parsedSegment = parseBracketSegment(trimmedPath, index);
703
+ if (parsedSegment.type === "property") {
704
+ if (arrayElementSelectorEncountered) {
705
+ throw new Error("JSONPath cannot select fields after array element selectors");
706
+ }
707
+ segments.push(parsedSegment.value);
708
+ } else {
709
+ arrayElementSelectorEncountered = true;
710
+ }
711
+ index = parsedSegment.nextIndex;
712
+ continue;
713
+ }
714
+ if (character === "@") {
715
+ throw new Error("JSONPath current node selector (@) is not supported");
716
+ }
717
+ if (character.trim() === "") {
718
+ index += 1;
719
+ continue;
720
+ }
721
+ throw new Error(`Unexpected character "${character}" in JSONPath`);
722
+ }
723
+ return segments;
724
+ }
725
+ function parseDotSegment(pathString, startIndex) {
726
+ if (startIndex >= pathString.length) {
727
+ throw new Error("JSONPath cannot end with a period");
728
+ }
729
+ if (pathString[startIndex] === "*") {
730
+ return { value: "*", nextIndex: startIndex + 1, isWildcard: true };
731
+ }
732
+ const firstCharacter = pathString[startIndex];
733
+ if (firstCharacter === "@") {
734
+ throw new Error("JSONPath current node selector (@) is not supported");
735
+ }
736
+ if (!isIdentifierStartCharacter(firstCharacter)) {
737
+ throw new Error("JSONPath property names after period must start with a letter, $ or _");
738
+ }
739
+ let endIndex = startIndex + 1;
740
+ while (endIndex < pathString.length && isIdentifierCharacter(pathString[endIndex])) {
741
+ endIndex++;
742
+ }
743
+ if (endIndex === startIndex) {
744
+ throw new Error("JSONPath is missing a property name after period");
745
+ }
746
+ return {
747
+ value: pathString.slice(startIndex, endIndex),
748
+ nextIndex: endIndex,
749
+ isWildcard: false
750
+ };
751
+ }
752
+ function parseBracketSegment(pathString, startIndex) {
753
+ const contentStartIndex = startIndex + 1;
754
+ if (contentStartIndex >= pathString.length) {
755
+ throw new Error("JSONPath has unterminated bracket");
756
+ }
757
+ const firstCharacter = pathString[contentStartIndex];
758
+ if (firstCharacter === "'" || firstCharacter === '"') {
759
+ const { value, nextIndex } = parseBracketProperty(pathString, contentStartIndex);
760
+ return { type: "property", value, nextIndex };
761
+ }
762
+ const closingBracketIndex = pathString.indexOf("]", contentStartIndex);
763
+ if (closingBracketIndex === -1) {
764
+ throw new Error("JSONPath has unterminated bracket");
765
+ }
766
+ const content = pathString.slice(contentStartIndex, closingBracketIndex).trim();
767
+ const unsupportedSelectorMessage = getUnsupportedBracketSelectorMessage(content);
768
+ if (unsupportedSelectorMessage) {
769
+ throw new Error(unsupportedSelectorMessage);
770
+ }
771
+ if (content === "*") {
772
+ return { type: "array-selector", nextIndex: closingBracketIndex + 1 };
773
+ }
774
+ if (/^\d+$/.test(content)) {
775
+ throw new Error("JSONPath array index selectors are not supported");
776
+ }
777
+ if (/^\d*\s*:\s*\d*(\s*:\s*\d*)?$/.test(content)) {
778
+ return { type: "array-selector", nextIndex: closingBracketIndex + 1 };
779
+ }
780
+ throw new Error(`Unsupported bracket selector "[${content}]" in JSONPath`);
781
+ }
782
+ function getUnsupportedBracketSelectorMessage(content) {
783
+ if (!content.length) {
784
+ return "JSONPath bracket selectors cannot be empty";
785
+ }
786
+ if (content.startsWith("(")) {
787
+ return "JSONPath script selectors are not supported";
788
+ }
789
+ if (content.startsWith("?")) {
790
+ return "JSONPath filter selectors are not supported";
791
+ }
792
+ if (content.includes(",")) {
793
+ return "JSONPath union selectors are not supported";
794
+ }
795
+ if (content.startsWith("@") || content.includes("@.")) {
796
+ return "JSONPath current node selector (@) is not supported";
797
+ }
798
+ return null;
799
+ }
800
+ function parseBracketProperty(pathString, startIndex) {
801
+ const quoteCharacter = pathString[startIndex];
802
+ let index = startIndex + 1;
803
+ let value = "";
804
+ let terminated = false;
805
+ while (index < pathString.length) {
806
+ const character = pathString[index];
807
+ if (character === "\\") {
808
+ index += 1;
809
+ if (index >= pathString.length) {
810
+ break;
811
+ }
812
+ value += pathString[index];
813
+ index += 1;
814
+ continue;
815
+ }
816
+ if (character === quoteCharacter) {
817
+ terminated = true;
818
+ index += 1;
819
+ break;
820
+ }
821
+ value += character;
822
+ index += 1;
823
+ }
824
+ if (!terminated) {
825
+ throw new Error("JSONPath string in bracket property selector is unterminated");
826
+ }
827
+ while (index < pathString.length && pathString[index].trim() === "") {
828
+ index += 1;
829
+ }
830
+ if (pathString[index] !== "]") {
831
+ throw new Error("JSONPath property selectors must end with ]");
832
+ }
833
+ if (!value.length) {
834
+ throw new Error("JSONPath property selectors cannot be empty");
835
+ }
836
+ return { value, nextIndex: index + 1 };
837
+ }
838
+ function isIdentifierCharacter(character) {
839
+ return /[a-zA-Z0-9$_]/.test(character);
840
+ }
841
+ function isIdentifierStartCharacter(character) {
842
+ return /[a-zA-Z_$]/.test(character);
843
+ }
844
+ function isIdentifierSegment(segment) {
845
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(segment);
846
+ }
847
+ function formatJsonPath(path) {
848
+ return path.map((segment, index) => {
849
+ if (index === 0) {
850
+ return segment;
851
+ }
852
+ if (segment === "*") {
853
+ return ".*";
854
+ }
855
+ if (isIdentifierSegment(segment)) {
856
+ return `.${segment}`;
857
+ }
858
+ const escapedSegment = segment.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
859
+ return `['${escapedSegment}']`;
860
+ }).join("");
861
+ }
672
862
 
673
863
  // dist/lib/json-parser/json-parser.js
674
864
  var JSONParser = class {
@@ -848,13 +1038,13 @@ var StreamingJSONParser = class extends JSONParser {
848
1038
 
849
1039
  // dist/lib/parsers/parse-json-in-batches.js
850
1040
  async function* parseJSONInBatches(binaryAsyncIterator, options) {
851
- var _a;
852
- const asyncIterator = (0, import_loader_utils.makeTextDecoderIterator)(binaryAsyncIterator);
853
- const { metadata } = options;
1041
+ var _a, _b;
1042
+ const asyncIterator = (0, import_loader_utils.makeTextDecoderIterator)((0, import_loader_utils.toArrayBufferIterator)(binaryAsyncIterator));
1043
+ const metadata = Boolean(((_a = options == null ? void 0 : options.core) == null ? void 0 : _a.metadata) || (options == null ? void 0 : options.metadata));
854
1044
  const { jsonpaths } = options.json || {};
855
1045
  let isFirstChunk = true;
856
1046
  const schema = null;
857
- const tableBatchBuilder = new import_schema_utils2.TableBatchBuilder(schema, options);
1047
+ const tableBatchBuilder = new import_schema_utils2.TableBatchBuilder(schema, options == null ? void 0 : options.core);
858
1048
  const parser = new StreamingJSONParser({ jsonpaths });
859
1049
  for await (const chunk of asyncIterator) {
860
1050
  const rows = parser.write(chunk);
@@ -863,7 +1053,7 @@ async function* parseJSONInBatches(binaryAsyncIterator, options) {
863
1053
  if (metadata) {
864
1054
  const initialBatch = {
865
1055
  // Common fields
866
- shape: ((_a = options == null ? void 0 : options.json) == null ? void 0 : _a.shape) || "array-row-table",
1056
+ shape: ((_b = options == null ? void 0 : options.json) == null ? void 0 : _b.shape) || "array-row-table",
867
1057
  batchType: "partial-result",
868
1058
  data: [],
869
1059
  length: 0,
@@ -923,7 +1113,7 @@ function rebuildJsonObject(batch, data) {
923
1113
  }
924
1114
 
925
1115
  // dist/json-loader.js
926
- var VERSION = true ? "4.4.0-alpha.0" : "latest";
1116
+ var VERSION = true ? "4.4.0-alpha.9" : "latest";
927
1117
  var JSONLoader = {
928
1118
  dataType: null,
929
1119
  batchType: null,
@@ -977,13 +1167,13 @@ function parseNDJSONSync(ndjsonText) {
977
1167
  var import_schema_utils4 = require("@loaders.gl/schema-utils");
978
1168
  var import_loader_utils2 = require("@loaders.gl/loader-utils");
979
1169
  async function* parseNDJSONInBatches(binaryAsyncIterator, options) {
980
- const textIterator = (0, import_loader_utils2.makeTextDecoderIterator)(binaryAsyncIterator);
1170
+ const textIterator = (0, import_loader_utils2.makeTextDecoderIterator)((0, import_loader_utils2.toArrayBufferIterator)(binaryAsyncIterator));
981
1171
  const lineIterator = (0, import_loader_utils2.makeLineIterator)(textIterator);
982
1172
  const numberedLineIterator = (0, import_loader_utils2.makeNumberedLineIterator)(lineIterator);
983
1173
  const schema = null;
984
1174
  const shape = "row-table";
985
1175
  const tableBatchBuilder = new import_schema_utils4.TableBatchBuilder(schema, {
986
- ...options,
1176
+ ...(options == null ? void 0 : options.core) || options,
987
1177
  shape
988
1178
  });
989
1179
  for await (const { counter, line } of numberedLineIterator) {
@@ -1006,7 +1196,7 @@ async function* parseNDJSONInBatches(binaryAsyncIterator, options) {
1006
1196
  }
1007
1197
 
1008
1198
  // dist/ndjson-loader.js
1009
- var VERSION2 = true ? "4.4.0-alpha.0" : "latest";
1199
+ var VERSION2 = true ? "4.4.0-alpha.9" : "latest";
1010
1200
  var NDJSONLoader = {
1011
1201
  dataType: null,
1012
1202
  batchType: null,
@@ -1058,7 +1248,7 @@ var JSONWriter = {
1058
1248
 
1059
1249
  // dist/geojson-loader.js
1060
1250
  var import_gis = require("@loaders.gl/gis");
1061
- var VERSION3 = true ? "4.4.0-alpha.0" : "latest";
1251
+ var VERSION3 = true ? "4.4.0-alpha.9" : "latest";
1062
1252
  var GeoJSONWorkerLoader = {
1063
1253
  dataType: null,
1064
1254
  batchType: null,
@@ -1121,7 +1311,8 @@ function parseTextSync2(text, options) {
1121
1311
  }
1122
1312
  function parseInBatches2(asyncIterator, options) {
1123
1313
  options = { ...GeoJSONLoader.options, ...options };
1124
- options.json = { ...GeoJSONLoader.options.geojson, ...options.geojson };
1314
+ options.json = { ...GeoJSONLoader.options.json, ...options.json };
1315
+ options.geojson = { ...GeoJSONLoader.options.geojson, ...options.geojson };
1125
1316
  const geojsonIterator = parseJSONInBatches(asyncIterator, options);
1126
1317
  switch (options.gis.format) {
1127
1318
  case "binary":
@@ -1138,7 +1329,7 @@ async function* makeBinaryGeometryIterator(geojsonIterator) {
1138
1329
  }
1139
1330
 
1140
1331
  // dist/geojson-writer.js
1141
- var import_loader_utils3 = require("@loaders.gl/loader-utils");
1332
+ var import_loader_utils4 = require("@loaders.gl/loader-utils");
1142
1333
 
1143
1334
  // dist/lib/encoders/geojson-encoder.js
1144
1335
  var import_schema_utils8 = require("@loaders.gl/schema-utils");
@@ -1209,6 +1400,7 @@ function getFeatureFromRow(table, row, geometryColumnIndex) {
1209
1400
  }
1210
1401
 
1211
1402
  // dist/lib/encoder-utils/utf8-encoder.js
1403
+ var import_loader_utils3 = require("@loaders.gl/loader-utils");
1212
1404
  var Utf8ArrayBufferEncoder = class {
1213
1405
  chunkSize;
1214
1406
  strings = [];
@@ -1227,7 +1419,7 @@ var Utf8ArrayBufferEncoder = class {
1227
1419
  return this.totalLength >= this.chunkSize;
1228
1420
  }
1229
1421
  getArrayBufferBatch() {
1230
- return this.textEncoder.encode(this.getStringBatch()).buffer;
1422
+ return (0, import_loader_utils3.ensureArrayBuffer)(this.textEncoder.encode(this.getStringBatch()).buffer);
1231
1423
  }
1232
1424
  getStringBatch() {
1233
1425
  const stringChunk = this.strings.join("");
@@ -1296,7 +1488,7 @@ var GeoJSONWriter = {
1296
1488
  async encode(table, options) {
1297
1489
  const tableIterator = [table];
1298
1490
  const batches = encodeTableAsGeojsonInBatches(tableIterator, options);
1299
- return await (0, import_loader_utils3.concatenateArrayBuffersAsync)(batches);
1491
+ return await (0, import_loader_utils4.concatenateArrayBuffersAsync)(batches);
1300
1492
  },
1301
1493
  encodeInBatches: (tableIterator, options) => encodeTableAsGeojsonInBatches(tableIterator, options)
1302
1494
  };