@vitessce/spatial-utils 4.0.4 → 4.0.6

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.
@@ -6079,8 +6079,8 @@ DimensionError.prototype = new RangeError();
6079
6079
  DimensionError.prototype.constructor = RangeError;
6080
6080
  DimensionError.prototype.name = "DimensionError";
6081
6081
  DimensionError.prototype.isDimensionError = true;
6082
- function IndexError$1(index2, min2, max2) {
6083
- if (!(this instanceof IndexError$1)) {
6082
+ function IndexError(index2, min2, max2) {
6083
+ if (!(this instanceof IndexError)) {
6084
6084
  throw new SyntaxError("Constructor must be called with the new operator");
6085
6085
  }
6086
6086
  this.index = index2;
@@ -6100,10 +6100,10 @@ function IndexError$1(index2, min2, max2) {
6100
6100
  }
6101
6101
  this.stack = new Error().stack;
6102
6102
  }
6103
- IndexError$1.prototype = new RangeError();
6104
- IndexError$1.prototype.constructor = RangeError;
6105
- IndexError$1.prototype.name = "IndexError";
6106
- IndexError$1.prototype.isIndexError = true;
6103
+ IndexError.prototype = new RangeError();
6104
+ IndexError.prototype.constructor = RangeError;
6105
+ IndexError.prototype.name = "IndexError";
6106
+ IndexError.prototype.isIndexError = true;
6107
6107
  function arraySize(x) {
6108
6108
  var s2 = [];
6109
6109
  while (Array.isArray(x)) {
@@ -6150,7 +6150,7 @@ function validateIndex(index2, length2) {
6150
6150
  throw new TypeError("Index must be an integer (value: " + index2 + ")");
6151
6151
  }
6152
6152
  if (index2 < 0 || typeof length2 === "number" && index2 >= length2) {
6153
- throw new IndexError$1(index2, length2);
6153
+ throw new IndexError(index2, length2);
6154
6154
  }
6155
6155
  }
6156
6156
  function resize(array, size2, defaultValue2) {
@@ -43491,9 +43491,9 @@ function getAccessorFromBuffer(typedArray, options) {
43491
43491
  startIndices,
43492
43492
  nested
43493
43493
  } = options;
43494
- const bytesPerElement = typedArray.BYTES_PER_ELEMENT;
43495
- const elementStride = stride ? stride / bytesPerElement : size2;
43496
- const elementOffset = offset ? offset / bytesPerElement : 0;
43494
+ const bytesPerElement2 = typedArray.BYTES_PER_ELEMENT;
43495
+ const elementStride = stride ? stride / bytesPerElement2 : size2;
43496
+ const elementOffset = offset ? offset / bytesPerElement2 : 0;
43497
43497
  const vertexCount = Math.floor((typedArray.length - elementOffset) / elementStride);
43498
43498
  return (_, {
43499
43499
  index: index2,
@@ -52284,9 +52284,9 @@ function getTextFromBuffer({
52284
52284
  startIndices,
52285
52285
  characterSet
52286
52286
  }) {
52287
- const bytesPerElement = value.BYTES_PER_ELEMENT;
52288
- const elementStride = stride ? stride / bytesPerElement : 1;
52289
- const elementOffset = offset ? offset / bytesPerElement : 0;
52287
+ const bytesPerElement2 = value.BYTES_PER_ELEMENT;
52288
+ const elementStride = stride ? stride / bytesPerElement2 : 1;
52289
+ const elementOffset = offset ? offset / bytesPerElement2 : 0;
52290
52290
  const characterCount = startIndices[length2] || Math.ceil((value.length - elementOffset) / elementStride);
52291
52291
  const autoCharacterSet = characterSet && /* @__PURE__ */ new Set();
52292
52292
  const texts = new Array(length2);
@@ -56148,7 +56148,7 @@ function requireLong() {
56148
56148
  var MIN_VALUE = fromBits(0, 2147483648 | 0, false);
56149
56149
  Long2.MIN_VALUE = MIN_VALUE;
56150
56150
  var LongPrototype = Long2.prototype;
56151
- LongPrototype.toInt = function toInt() {
56151
+ LongPrototype.toInt = function toInt2() {
56152
56152
  return this.unsigned ? this.low >>> 0 : this.low;
56153
56153
  };
56154
56154
  LongPrototype.toNumber = function toNumber2() {
@@ -74230,11 +74230,11 @@ class GLTFPostProcessor {
74230
74230
  }
74231
74231
  return accessor;
74232
74232
  }
74233
- _getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count2) {
74234
- const result = new Uint8Array(count2 * bytesPerElement);
74233
+ _getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement2, count2) {
74234
+ const result = new Uint8Array(count2 * bytesPerElement2);
74235
74235
  for (let i2 = 0; i2 < count2; i2++) {
74236
74236
  const elementOffset = byteOffset + i2 * byteStride;
74237
- result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i2 * bytesPerElement);
74237
+ result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement2)), i2 * bytesPerElement2);
74238
74238
  }
74239
74239
  return result.buffer;
74240
74240
  }
@@ -119129,6 +119129,20 @@ function requireDistEs6() {
119129
119129
  var distEs6Exports$2 = requireDistEs6();
119130
119130
  var distEs6Exports$1 = requireDistEs6$1();
119131
119131
  var distEs6Exports = requireDistEs6$2();
119132
+ function runSelectionWithBusySignal(onBusy, work) {
119133
+ if (typeof onBusy !== "function" || typeof requestAnimationFrame !== "function") {
119134
+ work();
119135
+ return;
119136
+ }
119137
+ onBusy(true);
119138
+ requestAnimationFrame(() => setTimeout(() => {
119139
+ try {
119140
+ work();
119141
+ } finally {
119142
+ onBusy(false);
119143
+ }
119144
+ }, 0));
119145
+ }
119132
119146
  const EDIT_TYPE_ADD = "addFeature";
119133
119147
  const EDIT_TYPE_CLEAR = "clearFeatures";
119134
119148
  class ClickableDrawPolygonByDraggingMode extends distEs6Exports.DrawPolygonByDraggingMode {
@@ -119143,7 +119157,8 @@ const MODE_MAP = {
119143
119157
  const defaultProps$f = {
119144
119158
  layerIds: [],
119145
119159
  onSelect: () => {
119146
- }
119160
+ },
119161
+ onSelectionBusy: null
119147
119162
  };
119148
119163
  const EMPTY_DATA = {
119149
119164
  type: "FeatureCollection",
@@ -119235,11 +119250,12 @@ class SelectionLayer extends CompositeLayer {
119235
119250
  selectedFeatureIndexes: [],
119236
119251
  data: EMPTY_DATA,
119237
119252
  onEdit: ({ updatedData, editType }) => {
119253
+ const { onSelectionBusy } = this.props;
119238
119254
  if (editType === EDIT_TYPE_ADD) {
119239
119255
  const { coordinates: coordinates2 } = updatedData.features[0].geometry;
119240
- this._selectPolygonObjects(coordinates2);
119256
+ runSelectionWithBusySignal(onSelectionBusy, () => this._selectPolygonObjects(coordinates2));
119241
119257
  } else if (editType === EDIT_TYPE_CLEAR) {
119242
- this._selectEmpty();
119258
+ runSelectionWithBusySignal(onSelectionBusy, () => this._selectEmpty());
119243
119259
  }
119244
119260
  },
119245
119261
  _subLayerProps: {
@@ -120380,22 +120396,22 @@ function addDecoder(cases, importFn) {
120380
120396
  }
120381
120397
  cases.forEach((c) => registry$2.set(c, importFn));
120382
120398
  }
120383
- addDecoder([void 0, 1], () => import("./raw-ChHiLbWA.js").then((m) => m.default));
120384
- addDecoder(5, () => import("./lzw-BqJ_dFlf.js").then((m) => m.default));
120399
+ addDecoder([void 0, 1], () => import("./raw-BFsjOa9D.js").then((m) => m.default));
120400
+ addDecoder(5, () => import("./lzw-C0hm4LU9.js").then((m) => m.default));
120385
120401
  addDecoder(6, () => {
120386
120402
  throw new Error("old style JPEG compression is not supported.");
120387
120403
  });
120388
- addDecoder(7, () => import("./jpeg-D0B2DXrb.js").then((m) => m.default));
120389
- addDecoder([8, 32946], () => import("./deflate-BCZYB9gc.js").then((m) => m.default));
120390
- addDecoder(32773, () => import("./packbits-C3Wqc-kA.js").then((m) => m.default));
120404
+ addDecoder(7, () => import("./jpeg-CEsy1aGu.js").then((m) => m.default));
120405
+ addDecoder([8, 32946], () => import("./deflate-BpOWypHr.js").then((m) => m.default));
120406
+ addDecoder(32773, () => import("./packbits-CkQ8qELh.js").then((m) => m.default));
120391
120407
  addDecoder(
120392
120408
  34887,
120393
- () => import("./lerc-D1N0v-gZ.js").then(async (m) => {
120409
+ () => import("./lerc-CoY4QzZT.js").then(async (m) => {
120394
120410
  await m.zstd.init();
120395
120411
  return m;
120396
120412
  }).then((m) => m.default)
120397
120413
  );
120398
- addDecoder(50001, () => import("./webimage-Del4KeFH.js").then((m) => m.default));
120414
+ addDecoder(50001, () => import("./webimage-C8ngyR6p.js").then((m) => m.default));
120399
120415
  function decodeRowAcc(row, stride) {
120400
120416
  let length2 = row.length - stride;
120401
120417
  let offset = 0;
@@ -129372,7 +129388,7 @@ const COMPRESSORS = /* @__PURE__ */ new Set(["blosclz", "lz4", "lz4hc", "snappy"
129372
129388
  let emscriptenModule;
129373
129389
  const init = () => blosc_codec({ noInitialRun: true, wasmBinary });
129374
129390
  const Blosc = (_a$3 = class {
129375
- constructor(clevel = 5, cname = "lz4", shuffle = 1, blocksize = 0) {
129391
+ constructor(clevel = 5, cname = "lz4", shuffle2 = 1, blocksize = 0) {
129376
129392
  if (clevel < 0 || clevel > 9) {
129377
129393
  throw new Error(`Invalid compression level: '${clevel}'. It should be between 0 and 9`);
129378
129394
  }
@@ -129380,17 +129396,17 @@ const Blosc = (_a$3 = class {
129380
129396
  throw new Error(`Invalid compressor '${cname}'. Valid compressors include
129381
129397
  'blosclz', 'lz4', 'lz4hc','snappy', 'zlib', 'zstd'.`);
129382
129398
  }
129383
- if (shuffle < -1 || shuffle > 2) {
129384
- throw new Error(`Invalid shuffle ${shuffle}. Must be one of 0 (NOSHUFFLE),
129399
+ if (shuffle2 < -1 || shuffle2 > 2) {
129400
+ throw new Error(`Invalid shuffle ${shuffle2}. Must be one of 0 (NOSHUFFLE),
129385
129401
  1 (SHUFFLE), 2 (BITSHUFFLE), -1 (AUTOSHUFFLE).`);
129386
129402
  }
129387
129403
  this.blocksize = blocksize;
129388
129404
  this.clevel = clevel;
129389
129405
  this.cname = cname;
129390
- this.shuffle = shuffle;
129406
+ this.shuffle = shuffle2;
129391
129407
  }
129392
- static fromConfig({ blocksize, clevel, cname, shuffle }) {
129393
- return new _a$3(clevel, cname, shuffle, blocksize);
129408
+ static fromConfig({ blocksize, clevel, cname, shuffle: shuffle2 }) {
129409
+ return new _a$3(clevel, cname, shuffle2, blocksize);
129394
129410
  }
129395
129411
  async encode(data) {
129396
129412
  if (!emscriptenModule) {
@@ -138255,12 +138271,97 @@ function coordinateTransformationsToMatrixForSpatialData(ngffMetadata, targetCoo
138255
138271
  );
138256
138272
  return coordinateTransformationsToMatrix(normalizedTransformations, normAxes);
138257
138273
  }
138274
+ class ZarritaError extends Error {
138275
+ }
138276
+ class NotFoundError extends ZarritaError {
138277
+ constructor(context, options = {}) {
138278
+ super(`Not found: ${context}`, { cause: options.cause });
138279
+ this._tag = "NotFoundError";
138280
+ this.name = "NotFoundError";
138281
+ this.path = options.path;
138282
+ this.found = options.found;
138283
+ }
138284
+ }
138285
+ class InvalidMetadataError extends ZarritaError {
138286
+ constructor(message, options = {}) {
138287
+ super(message, { cause: options.cause });
138288
+ this._tag = "InvalidMetadataError";
138289
+ this.name = "InvalidMetadataError";
138290
+ this.path = options.path;
138291
+ }
138292
+ }
138293
+ class UnknownCodecError extends ZarritaError {
138294
+ constructor(codec) {
138295
+ super(`Unknown codec: ${codec}`);
138296
+ this._tag = "UnknownCodecError";
138297
+ this.name = "UnknownCodecError";
138298
+ this.codec = codec;
138299
+ }
138300
+ }
138301
+ class CodecPipelineError extends ZarritaError {
138302
+ constructor(options) {
138303
+ const parts = [
138304
+ `Failed to ${options.direction} chunk`,
138305
+ options.codec && `via codec "${options.codec}"`,
138306
+ options.chunkPath && `at ${options.chunkPath}`
138307
+ ].filter(Boolean);
138308
+ super(parts.join(" "), { cause: options.cause });
138309
+ this._tag = "CodecPipelineError";
138310
+ this.name = "CodecPipelineError";
138311
+ this.direction = options.direction;
138312
+ this.codec = options.codec;
138313
+ this.chunkPath = options.chunkPath;
138314
+ }
138315
+ }
138316
+ class InvalidSelectionError extends ZarritaError {
138317
+ constructor() {
138318
+ super(...arguments);
138319
+ this._tag = "InvalidSelectionError";
138320
+ this.name = "InvalidSelectionError";
138321
+ }
138322
+ }
138323
+ class UnsupportedError extends ZarritaError {
138324
+ constructor(feature2) {
138325
+ super(`Unsupported: ${feature2}`);
138326
+ this._tag = "UnsupportedError";
138327
+ this.name = "UnsupportedError";
138328
+ this.feature = feature2;
138329
+ }
138330
+ }
138331
+ function unimplementedEncode(codecName) {
138332
+ return () => {
138333
+ throw new UnsupportedError(`${codecName} encode`);
138334
+ };
138335
+ }
138336
+ class BitroundCodec {
138337
+ constructor(configuration, _meta) {
138338
+ this.kind = "array_to_array";
138339
+ this.encode = unimplementedEncode("bitround");
138340
+ if (configuration.keepbits < 0) {
138341
+ throw new InvalidMetadataError("keepbits must be zero or positive");
138342
+ }
138343
+ }
138344
+ static fromConfig(configuration, meta) {
138345
+ return new BitroundCodec(configuration, meta);
138346
+ }
138347
+ /**
138348
+ * Decode a chunk of data (no-op).
138349
+ * @param arr - The chunk to decode
138350
+ * @returns The decoded chunk
138351
+ */
138352
+ decode(arr) {
138353
+ return arr;
138354
+ }
138355
+ }
138356
+ function isArrayBufferLike(x) {
138357
+ return x instanceof ArrayBuffer || x instanceof SharedArrayBuffer;
138358
+ }
138258
138359
  class BoolArray {
138259
138360
  #bytes;
138260
138361
  constructor(x, byteOffset, length2) {
138261
138362
  if (typeof x === "number") {
138262
138363
  this.#bytes = new Uint8Array(x);
138263
- } else if (x instanceof ArrayBuffer) {
138364
+ } else if (isArrayBufferLike(x)) {
138264
138365
  this.#bytes = new Uint8Array(x, byteOffset, length2);
138265
138366
  } else {
138266
138367
  this.#bytes = new Uint8Array(Array.from(x, (v) => v ? 1 : 0));
@@ -138304,7 +138405,7 @@ class ByteStringArray {
138304
138405
  this.#encoder = new TextEncoder();
138305
138406
  if (typeof x === "number") {
138306
138407
  this._data = new Uint8Array(x * chars);
138307
- } else if (x instanceof ArrayBuffer) {
138408
+ } else if (isArrayBufferLike(x)) {
138308
138409
  if (length2)
138309
138410
  length2 = length2 * chars;
138310
138411
  this._data = new Uint8Array(x, byteOffset, length2);
@@ -138358,7 +138459,7 @@ class UnicodeStringArray {
138358
138459
  this.chars = chars;
138359
138460
  if (typeof x === "number") {
138360
138461
  this.#data = new Int32Array(x * chars);
138361
- } else if (x instanceof ArrayBuffer) {
138462
+ } else if (isArrayBufferLike(x)) {
138362
138463
  if (length2)
138363
138464
  length2 *= chars;
138364
138465
  this.#data = new Int32Array(x, byteOffset, length2);
@@ -138417,15 +138518,29 @@ class UnicodeStringArray {
138417
138518
  }
138418
138519
  }
138419
138520
  }
138420
- function json_decode_object(bytes) {
138521
+ function assertSharedArrayBufferAvailable() {
138522
+ if (typeof SharedArrayBuffer === "undefined") {
138523
+ throw new Error("SharedArrayBuffer is not available. In browsers, this requires Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers to be set.");
138524
+ }
138525
+ }
138526
+ function createBuffer(byteLength, useShared) {
138527
+ {
138528
+ return new SharedArrayBuffer(byteLength);
138529
+ }
138530
+ }
138531
+ function jsonDecodeObject(bytes) {
138421
138532
  const str = new TextDecoder().decode(bytes);
138422
- return JSON.parse(str);
138533
+ try {
138534
+ return JSON.parse(str);
138535
+ } catch (cause) {
138536
+ throw new InvalidMetadataError("Failed to decode JSON", { cause });
138537
+ }
138423
138538
  }
138424
- function byteswap_inplace(view, bytes_per_element2) {
138425
- const numFlips = bytes_per_element2 / 2;
138426
- const endByteIndex = bytes_per_element2 - 1;
138539
+ function byteswapInplace(view, bytesPerElement2) {
138540
+ const numFlips = bytesPerElement2 / 2;
138541
+ const endByteIndex = bytesPerElement2 - 1;
138427
138542
  let t = 0;
138428
- for (let i2 = 0; i2 < view.length; i2 += bytes_per_element2) {
138543
+ for (let i2 = 0; i2 < view.length; i2 += bytesPerElement2) {
138429
138544
  for (let j = 0; j < numFlips; j += 1) {
138430
138545
  t = view[i2 + j];
138431
138546
  view[i2 + j] = view[i2 + endByteIndex - j];
@@ -138433,16 +138548,16 @@ function byteswap_inplace(view, bytes_per_element2) {
138433
138548
  }
138434
138549
  }
138435
138550
  }
138436
- function get_ctr(data_type) {
138437
- if (data_type === "v2:object") {
138551
+ function getCtr(dataType) {
138552
+ if (dataType === "v2:object") {
138438
138553
  return globalThis.Array;
138439
138554
  }
138440
- let match = data_type.match(/v2:([US])(\d+)/);
138555
+ let match = dataType.match(/v2:([US])(\d+)/);
138441
138556
  if (match) {
138442
138557
  let [, kind, chars] = match;
138443
138558
  return (kind === "U" ? UnicodeStringArray : ByteStringArray).bind(null, Number(chars));
138444
138559
  }
138445
- if (data_type === "string") {
138560
+ if (dataType === "string") {
138446
138561
  return globalThis.Array;
138447
138562
  }
138448
138563
  let ctr = {
@@ -138458,11 +138573,13 @@ function get_ctr(data_type) {
138458
138573
  float32: Float32Array,
138459
138574
  float64: Float64Array,
138460
138575
  bool: BoolArray
138461
- }[data_type];
138462
- assert$1(ctr, `Unknown or unsupported data_type: ${data_type}`);
138576
+ }[dataType];
138577
+ if (!ctr) {
138578
+ throw new InvalidMetadataError(`Unknown or unsupported dataType: ${dataType}`);
138579
+ }
138463
138580
  return ctr;
138464
138581
  }
138465
- function get_strides(shape, order) {
138582
+ function getStrides(shape, order) {
138466
138583
  const rank2 = shape.length;
138467
138584
  if (typeof order === "string") {
138468
138585
  order = order === "C" ? Array.from({ length: rank2 }, (_, i2) => i2) : Array.from({ length: rank2 }, (_, i2) => rank2 - 1 - i2);
@@ -138476,25 +138593,27 @@ function get_strides(shape, order) {
138476
138593
  }
138477
138594
  return stride;
138478
138595
  }
138479
- function create_chunk_key_encoder({ name: name2, configuration }) {
138596
+ function createChunkKeyEncoder({ name: name2, configuration }) {
138480
138597
  if (name2 === "default") {
138481
138598
  const separator = configuration?.separator ?? "/";
138482
- return (chunk_coords) => ["c", ...chunk_coords].join(separator);
138599
+ return (chunkCoords) => ["c", ...chunkCoords].join(separator);
138483
138600
  }
138484
138601
  if (name2 === "v2") {
138485
138602
  const separator = configuration?.separator ?? ".";
138486
- return (chunk_coords) => chunk_coords.join(separator) || "0";
138603
+ return (chunkCoords) => chunkCoords.join(separator) || "0";
138487
138604
  }
138488
- throw new Error(`Unknown chunk key encoding: ${name2}`);
138605
+ throw new InvalidMetadataError(`Unknown chunk key encoding: ${name2}`);
138489
138606
  }
138490
- function coerce_dtype(dtype) {
138607
+ function coerceDtype(dtype) {
138491
138608
  if (dtype === "|O") {
138492
- return { data_type: "v2:object" };
138609
+ return { dataType: "v2:object" };
138493
138610
  }
138494
138611
  let match = dtype.match(/^([<|>])(.*)$/);
138495
- assert$1(match, `Invalid dtype: ${dtype}`);
138612
+ if (!match) {
138613
+ throw new InvalidMetadataError(`Invalid dtype: ${dtype}`);
138614
+ }
138496
138615
  let [, endian, rest] = match;
138497
- let data_type = {
138616
+ let dataType = {
138498
138617
  b1: "bool",
138499
138618
  i1: "int8",
138500
138619
  u1: "uint8",
@@ -138508,33 +138627,73 @@ function coerce_dtype(dtype) {
138508
138627
  f4: "float32",
138509
138628
  f8: "float64"
138510
138629
  }[rest] ?? (rest.startsWith("S") || rest.startsWith("U") ? `v2:${rest}` : void 0);
138511
- assert$1(data_type, `Unsupported or unknown dtype: ${dtype}`);
138630
+ if (!dataType) {
138631
+ throw new InvalidMetadataError(`Unsupported or unknown dtype: ${dtype}`);
138632
+ }
138512
138633
  if (endian === "|") {
138513
- return { data_type };
138634
+ return { dataType };
138514
138635
  }
138515
- return { data_type, endian: endian === "<" ? "little" : "big" };
138636
+ return { dataType, endian: endian === "<" ? "little" : "big" };
138637
+ }
138638
+ function isFixedScaleOffsetConfig(filter) {
138639
+ return (filter.id === "fixedscaleoffset" || filter.id === "numcodecs.fixedscaleoffset") && typeof filter.scale === "number" && typeof filter.offset === "number" && (filter.astype === void 0 || typeof filter.astype === "string") && (filter.dtype === void 0 || typeof filter.dtype === "string");
138516
138640
  }
138517
- function v2_to_v3_array_metadata(meta, attributes = {}) {
138641
+ function v2ToV3ArrayMetadata(meta, attributes = {}) {
138518
138642
  let codecs = [];
138519
- let dtype = coerce_dtype(meta.dtype);
138643
+ let dtype = coerceDtype(meta.dtype);
138520
138644
  if (meta.order === "F") {
138521
138645
  codecs.push({ name: "transpose", configuration: { order: "F" } });
138522
138646
  }
138647
+ for (let filter of meta.filters ?? []) {
138648
+ if (filter.id === "fixedscaleoffset" || filter.id === "numcodecs.fixedscaleoffset") {
138649
+ if (!isFixedScaleOffsetConfig(filter)) {
138650
+ throw new InvalidMetadataError(`Invalid fixedscaleoffset filter: ${JSON.stringify(filter)}`);
138651
+ }
138652
+ codecs.push({
138653
+ name: "scale_offset",
138654
+ configuration: {
138655
+ scale: filter.scale,
138656
+ offset: filter.offset
138657
+ }
138658
+ });
138659
+ let astype = filter.astype ?? filter.dtype;
138660
+ if (astype !== void 0 && astype !== meta.dtype) {
138661
+ let castTarget = coerceDtype(astype).dataType;
138662
+ if (!isDataType(castTarget, "number") && !isDataType(castTarget, "bigint")) {
138663
+ throw new InvalidMetadataError(`fixedscaleoffset astype must be a numeric data type, got ${astype}`);
138664
+ }
138665
+ codecs.push({
138666
+ name: "cast_value",
138667
+ configuration: {
138668
+ data_type: castTarget,
138669
+ // `np.around` uses banker's rounding (round-half-to-even).
138670
+ rounding: "nearest-even",
138671
+ // Matches de-facto numpy integer-overflow behavior.
138672
+ out_of_range: "wrap"
138673
+ }
138674
+ });
138675
+ }
138676
+ continue;
138677
+ }
138678
+ let { id, ...configuration } = filter;
138679
+ codecs.push({ name: `numcodecs.${id}`, configuration });
138680
+ }
138523
138681
  if ("endian" in dtype && dtype.endian === "big") {
138524
138682
  codecs.push({ name: "bytes", configuration: { endian: "big" } });
138525
138683
  }
138526
- for (let { id, ...configuration } of meta.filters ?? []) {
138527
- codecs.push({ name: id, configuration });
138528
- }
138529
138684
  if (meta.compressor) {
138530
138685
  let { id, ...configuration } = meta.compressor;
138531
- codecs.push({ name: id, configuration });
138686
+ codecs.push({ name: `numcodecs.${id}`, configuration });
138687
+ }
138688
+ let dimensionNames;
138689
+ if (globalThis.Array.isArray(attributes._ARRAY_DIMENSIONS)) {
138690
+ dimensionNames = attributes._ARRAY_DIMENSIONS;
138532
138691
  }
138533
138692
  return {
138534
138693
  zarr_format: 3,
138535
138694
  node_type: "array",
138536
138695
  shape: meta.shape,
138537
- data_type: dtype.data_type,
138696
+ data_type: dtype.dataType,
138538
138697
  chunk_grid: {
138539
138698
  name: "regular",
138540
138699
  configuration: {
@@ -138549,44 +138708,63 @@ function v2_to_v3_array_metadata(meta, attributes = {}) {
138549
138708
  },
138550
138709
  codecs,
138551
138710
  fill_value: meta.fill_value,
138711
+ dimension_names: dimensionNames,
138552
138712
  attributes
138553
138713
  };
138554
138714
  }
138555
- function v2_to_v3_group_metadata(_meta, attributes = {}) {
138715
+ function v2ToV3GroupMetadata(_meta, attributes = {}) {
138556
138716
  return {
138557
138717
  zarr_format: 3,
138558
138718
  node_type: "group",
138559
138719
  attributes
138560
138720
  };
138561
138721
  }
138562
- function is_dtype(dtype, query) {
138722
+ function isDataType(dtype, query) {
138563
138723
  if (query !== "number" && query !== "bigint" && query !== "boolean" && query !== "object" && query !== "string") {
138564
138724
  return dtype === query;
138565
138725
  }
138566
- let is_boolean = dtype === "bool";
138726
+ let isBoolean2 = dtype === "bool";
138567
138727
  if (query === "boolean")
138568
- return is_boolean;
138569
- let is_string = dtype.startsWith("v2:U") || dtype.startsWith("v2:S") || dtype === "string";
138728
+ return isBoolean2;
138729
+ let isString2 = dtype.startsWith("v2:U") || dtype.startsWith("v2:S") || dtype === "string";
138570
138730
  if (query === "string")
138571
- return is_string;
138572
- let is_bigint = dtype === "int64" || dtype === "uint64";
138731
+ return isString2;
138732
+ let isBigint = dtype === "int64" || dtype === "uint64";
138573
138733
  if (query === "bigint")
138574
- return is_bigint;
138575
- let is_object = dtype === "v2:object";
138734
+ return isBigint;
138735
+ let isObject2 = dtype === "v2:object";
138576
138736
  if (query === "object")
138577
- return is_object;
138578
- return !is_string && !is_bigint && !is_boolean && !is_object;
138737
+ return isObject2;
138738
+ return !isString2 && !isBigint && !isBoolean2 && !isObject2;
138579
138739
  }
138580
- function is_sharding_codec(codec) {
138740
+ function isShardingCodec(codec) {
138581
138741
  return codec?.name === "sharding_indexed";
138582
138742
  }
138583
- function ensure_correct_scalar(metadata) {
138743
+ function ensureCorrectScalar(metadata) {
138584
138744
  if ((metadata.data_type === "uint64" || metadata.data_type === "int64") && metadata.fill_value != null) {
138585
138745
  return BigInt(metadata.fill_value);
138586
138746
  }
138747
+ let isFloat = metadata.data_type === "float16" || metadata.data_type === "float32" || metadata.data_type === "float64";
138748
+ if (typeof metadata.fill_value === "string" && isFloat) {
138749
+ let mapping = {
138750
+ NaN: NaN,
138751
+ Infinity: Infinity,
138752
+ "-Infinity": -Infinity
138753
+ };
138754
+ if (metadata.fill_value in mapping) {
138755
+ return mapping[metadata.fill_value];
138756
+ }
138757
+ }
138587
138758
  return metadata.fill_value;
138588
138759
  }
138589
- function rethrow_unless(error2, ...errors) {
138760
+ function resolveSignal(opts) {
138761
+ let a2 = opts.signal;
138762
+ let b = opts.opts?.signal;
138763
+ if (a2 && b)
138764
+ return AbortSignal.any([a2, b]);
138765
+ return a2 ?? b;
138766
+ }
138767
+ function rethrowUnless(error2, ...errors) {
138590
138768
  if (!errors.some((ErrorClass) => error2 instanceof ErrorClass)) {
138591
138769
  throw error2;
138592
138770
  }
@@ -138597,7 +138775,13 @@ function assert$1(expression, msg = "") {
138597
138775
  }
138598
138776
  }
138599
138777
  async function decompress(data, { format: format2, signal }) {
138600
- const response = data instanceof Response ? data : new Response(data);
138778
+ let response;
138779
+ if (data instanceof ArrayBuffer) {
138780
+ response = new Response(data);
138781
+ } else {
138782
+ let bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
138783
+ response = new Response(bytes.slice().buffer);
138784
+ }
138601
138785
  assert$1(response.body, "Response does not contain body.");
138602
138786
  try {
138603
138787
  const decompressedResponse = new Response(response.body.pipeThrough(new DecompressionStream(format2), { signal }));
@@ -138608,37 +138792,13 @@ async function decompress(data, { format: format2, signal }) {
138608
138792
  throw new Error(`Failed to decode ${format2}`);
138609
138793
  }
138610
138794
  }
138611
- class BitroundCodec {
138612
- constructor(configuration, _meta) {
138613
- this.kind = "array_to_array";
138614
- assert$1(configuration.keepbits >= 0, "keepbits must be zero or positive");
138615
- }
138616
- static fromConfig(configuration, meta) {
138617
- return new BitroundCodec(configuration, meta);
138618
- }
138619
- /**
138620
- * Encode a chunk of data with bit-rounding.
138621
- * @param _arr - The chunk to encode
138622
- */
138623
- encode(_arr) {
138624
- throw new Error("`BitroundCodec.encode` is not implemented. Please open an issue at https://github.com/manzt/zarrita.js/issues.");
138625
- }
138626
- /**
138627
- * Decode a chunk of data (no-op).
138628
- * @param arr - The chunk to decode
138629
- * @returns The decoded chunk
138630
- */
138631
- decode(arr) {
138632
- return arr;
138633
- }
138634
- }
138635
- const LITTLE_ENDIAN_OS = system_is_little_endian();
138636
- function system_is_little_endian() {
138795
+ const LITTLE_ENDIAN_OS = systemIsLittleEndian();
138796
+ function systemIsLittleEndian() {
138637
138797
  const a2 = new Uint32Array([305419896]);
138638
138798
  const b = new Uint8Array(a2.buffer, a2.byteOffset, a2.byteLength);
138639
138799
  return !(b[0] === 18);
138640
138800
  }
138641
- function bytes_per_element(TypedArray) {
138801
+ function bytesPerElement(TypedArray) {
138642
138802
  if ("BYTES_PER_ELEMENT" in TypedArray) {
138643
138803
  return TypedArray.BYTES_PER_ELEMENT;
138644
138804
  }
@@ -138648,9 +138808,9 @@ class BytesCodec {
138648
138808
  constructor(configuration, meta) {
138649
138809
  this.kind = "array_to_bytes";
138650
138810
  this.#endian = configuration?.endian;
138651
- this.#TypedArray = get_ctr(meta.data_type);
138811
+ this.#TypedArray = getCtr(meta.dataType);
138652
138812
  this.#shape = meta.shape;
138653
- this.#stride = get_strides(meta.shape, "C");
138813
+ this.#stride = getStrides(meta.shape, "C");
138654
138814
  const sample = new this.#TypedArray(0);
138655
138815
  this.#BYTES_PER_ELEMENT = sample.BYTES_PER_ELEMENT;
138656
138816
  }
@@ -138665,13 +138825,21 @@ class BytesCodec {
138665
138825
  encode(arr) {
138666
138826
  let bytes = new Uint8Array(arr.data.buffer);
138667
138827
  if (LITTLE_ENDIAN_OS && this.#endian === "big") {
138668
- byteswap_inplace(bytes, bytes_per_element(this.#TypedArray));
138828
+ bytes = bytes.slice();
138829
+ byteswapInplace(bytes, bytesPerElement(this.#TypedArray));
138669
138830
  }
138670
138831
  return bytes;
138671
138832
  }
138833
+ computeEncodedSize(decodedSize) {
138834
+ return decodedSize;
138835
+ }
138672
138836
  decode(bytes) {
138673
138837
  if (LITTLE_ENDIAN_OS && this.#endian === "big") {
138674
- byteswap_inplace(bytes, bytes_per_element(this.#TypedArray));
138838
+ bytes = bytes.slice();
138839
+ byteswapInplace(bytes, bytesPerElement(this.#TypedArray));
138840
+ }
138841
+ if (bytes.byteOffset % this.#BYTES_PER_ELEMENT !== 0) {
138842
+ bytes = bytes.slice();
138675
138843
  }
138676
138844
  return {
138677
138845
  data: new this.#TypedArray(bytes.buffer, bytes.byteOffset, bytes.byteLength / this.#BYTES_PER_ELEMENT),
@@ -138680,42 +138848,359 @@ class BytesCodec {
138680
138848
  };
138681
138849
  }
138682
138850
  }
138851
+ const SPECIAL_FLOATS = {
138852
+ NaN: NaN,
138853
+ Infinity: Infinity,
138854
+ "-Infinity": -Infinity
138855
+ };
138856
+ const FLOAT_BYTES = {
138857
+ float16: 2,
138858
+ float32: 4,
138859
+ float64: 8
138860
+ };
138861
+ function isFloatType(dataType) {
138862
+ return dataType in FLOAT_BYTES;
138863
+ }
138864
+ function isBigintType(dataType) {
138865
+ return dataType === "int64" || dataType === "uint64";
138866
+ }
138867
+ function hexToFloat(hex, byteWidth) {
138868
+ const int = BigInt(hex);
138869
+ const buf = new ArrayBuffer(byteWidth);
138870
+ const view = new DataView(buf);
138871
+ if (byteWidth === 2) {
138872
+ if (typeof view.getFloat16 !== "function") {
138873
+ throw new UnsupportedError("float16 hex-encoded scalar decoding (requires DataView.prototype.getFloat16)");
138874
+ }
138875
+ view.setUint16(0, Number(int));
138876
+ return view.getFloat16(0);
138877
+ }
138878
+ if (byteWidth === 4) {
138879
+ view.setUint32(0, Number(int));
138880
+ return view.getFloat32(0);
138881
+ }
138882
+ view.setBigUint64(0, int);
138883
+ return view.getFloat64(0);
138884
+ }
138885
+ function parseJsonScalar(dataType, value) {
138886
+ if (isBigintType(dataType)) {
138887
+ if (typeof value !== "number" || !Number.isInteger(value)) {
138888
+ throw new InvalidMetadataError(`Expected an integer value for data type "${dataType}", got ${JSON.stringify(value)}`);
138889
+ }
138890
+ return BigInt(value);
138891
+ }
138892
+ if (typeof value === "number") {
138893
+ if (!isFloatType(dataType) && !Number.isInteger(value)) {
138894
+ throw new InvalidMetadataError(`Expected an integer value for data type "${dataType}", got ${value}`);
138895
+ }
138896
+ return value;
138897
+ }
138898
+ if (!isFloatType(dataType)) {
138899
+ throw new InvalidMetadataError(`String-encoded scalar "${value}" is not valid for non-float data type "${dataType}"`);
138900
+ }
138901
+ if (value in SPECIAL_FLOATS) {
138902
+ return SPECIAL_FLOATS[value];
138903
+ }
138904
+ return hexToFloat(value, FLOAT_BYTES[dataType]);
138905
+ }
138906
+ const SUPPORTED$2 = /* @__PURE__ */ new Set([
138907
+ "int8",
138908
+ "uint8",
138909
+ "int16",
138910
+ "uint16",
138911
+ "int32",
138912
+ "uint32",
138913
+ "int64",
138914
+ "uint64",
138915
+ "float16",
138916
+ "float32",
138917
+ "float64"
138918
+ ]);
138919
+ const INT_BOUNDS = {
138920
+ int8: [-128, 2 ** 7 - 1],
138921
+ uint8: [0, 2 ** 8 - 1],
138922
+ int16: [-32768, 2 ** 15 - 1],
138923
+ uint16: [0, 2 ** 16 - 1],
138924
+ int32: [-2147483648, 2 ** 31 - 1],
138925
+ uint32: [0, 2 ** 32 - 1]
138926
+ };
138927
+ const BIGINT_BOUNDS = {
138928
+ int64: [-(2n ** 63n), 2n ** 63n - 1n],
138929
+ uint64: [0n, 2n ** 64n - 1n]
138930
+ };
138931
+ function parseScalarMapEntries(entries, srcType, tgtType) {
138932
+ return entries.map(([src, tgt]) => ({
138933
+ src: parseJsonScalar(srcType, src),
138934
+ tgt: parseJsonScalar(tgtType, tgt)
138935
+ }));
138936
+ }
138937
+ function scalarMapLookup(value, entries) {
138938
+ for (const entry of entries) {
138939
+ if (typeof entry.src === "number" && Number.isNaN(entry.src)) {
138940
+ if (typeof value === "number" && Number.isNaN(value)) {
138941
+ return entry.tgt;
138942
+ }
138943
+ } else if (value === entry.src) {
138944
+ return entry.tgt;
138945
+ }
138946
+ }
138947
+ return void 0;
138948
+ }
138949
+ function roundNearestEven(value) {
138950
+ if (!Number.isFinite(value))
138951
+ return value;
138952
+ if (Math.abs(value - Math.trunc(value)) === 0.5) {
138953
+ const floor2 = Math.floor(value);
138954
+ const ceil2 = Math.ceil(value);
138955
+ return floor2 % 2 === 0 ? floor2 : ceil2;
138956
+ }
138957
+ return Math.round(value);
138958
+ }
138959
+ function nearestAway(value) {
138960
+ return Math.sign(value) * Math.floor(Math.abs(value) + 0.5);
138961
+ }
138962
+ function getRoundingFn(mode) {
138963
+ switch (mode) {
138964
+ case "nearest-even":
138965
+ return roundNearestEven;
138966
+ case "towards-zero":
138967
+ return Math.trunc;
138968
+ case "towards-positive":
138969
+ return Math.ceil;
138970
+ case "towards-negative":
138971
+ return Math.floor;
138972
+ case "nearest-away":
138973
+ return nearestAway;
138974
+ }
138975
+ }
138976
+ function makeIntRangeCheck(lo, hi, outOfRange) {
138977
+ const range2 = hi - lo + 1;
138978
+ switch (outOfRange) {
138979
+ case "clamp":
138980
+ return (v) => v < lo ? lo : v > hi ? hi : v;
138981
+ case "wrap":
138982
+ return (v) => v >= lo && v <= hi ? v : ((v - lo) % range2 + range2) % range2 + lo;
138983
+ default:
138984
+ return (v) => {
138985
+ if (v >= lo && v <= hi)
138986
+ return v;
138987
+ throw new Error(`Value ${v} out of range [${lo}, ${hi}]. Set out_of_range='clamp' or out_of_range='wrap' to handle this.`);
138988
+ };
138989
+ }
138990
+ }
138991
+ function makeBigintRangeCheck(lo, hi, outOfRange) {
138992
+ const range2 = hi - lo + 1n;
138993
+ switch (outOfRange) {
138994
+ case "clamp":
138995
+ return (v) => v < lo ? lo : v > hi ? hi : v;
138996
+ case "wrap":
138997
+ return (v) => v >= lo && v <= hi ? v : ((v - lo) % range2 + range2) % range2 + lo;
138998
+ default:
138999
+ return (v) => {
139000
+ if (v >= lo && v <= hi)
139001
+ return v;
139002
+ throw new Error(`Value ${v} out of range [${lo}, ${hi}]. Set out_of_range='clamp' or out_of_range='wrap' to handle this.`);
139003
+ };
139004
+ }
139005
+ }
139006
+ class CastValueCodec {
139007
+ constructor(arrayType, encodedType, rounding, outOfRange, decodeMapEntries, encodeMapEntries) {
139008
+ this.kind = "array_to_array";
139009
+ this.encode = unimplementedEncode("cast_value");
139010
+ this.#encodedType = encodedType;
139011
+ this.#arrayTypeCtr = getCtr(arrayType);
139012
+ this.#decodeValue = buildConverter(encodedType, arrayType, rounding, outOfRange, decodeMapEntries);
139013
+ this.#encodeFillValue = buildConverter(arrayType, encodedType, rounding, outOfRange, encodeMapEntries);
139014
+ }
139015
+ #encodedType;
139016
+ #arrayTypeCtr;
139017
+ #decodeValue;
139018
+ #encodeFillValue;
139019
+ /** Return updated metadata reflecting the type and fill value after encoding. */
139020
+ getEncodedMeta(meta) {
139021
+ let fillValue = meta.fillValue;
139022
+ if (fillValue != null) {
139023
+ fillValue = this.#encodeFillValue(fillValue);
139024
+ }
139025
+ return { ...meta, dataType: this.#encodedType, fillValue };
139026
+ }
139027
+ static fromConfig(config3, meta) {
139028
+ const arrayType = meta.dataType;
139029
+ const encodedType = config3.data_type;
139030
+ if (!SUPPORTED$2.has(arrayType)) {
139031
+ throw new InvalidMetadataError(`cast_value codec does not support array data type: ${arrayType}`);
139032
+ }
139033
+ if (!SUPPORTED$2.has(encodedType)) {
139034
+ throw new InvalidMetadataError(`cast_value codec does not support encoded data type: ${encodedType}`);
139035
+ }
139036
+ const rounding = config3.rounding ?? "nearest-even";
139037
+ const decodeMapEntries = config3.scalar_map?.decode ? parseScalarMapEntries(config3.scalar_map.decode, encodedType, arrayType) : [];
139038
+ const encodeMapEntries = config3.scalar_map?.encode ? parseScalarMapEntries(config3.scalar_map.encode, arrayType, encodedType) : [];
139039
+ return new CastValueCodec(arrayType, encodedType, rounding, config3.out_of_range, decodeMapEntries, encodeMapEntries);
139040
+ }
139041
+ decode(chunk) {
139042
+ const input = chunk.data;
139043
+ const out = new this.#arrayTypeCtr(input.length);
139044
+ for (let i2 = 0; i2 < input.length; i2++) {
139045
+ out[i2] = this.#decodeValue(input[i2]);
139046
+ }
139047
+ return { data: out, shape: chunk.shape, stride: chunk.stride };
139048
+ }
139049
+ }
139050
+ function buildConverter(sourceType, targetType, rounding, outOfRange, mapEntries) {
139051
+ const srcIsFloat = isFloatType(sourceType);
139052
+ const srcIsBigint = isBigintType(sourceType);
139053
+ const dstIsFloat = isFloatType(targetType);
139054
+ const dstIsBigint = isBigintType(targetType);
139055
+ let baseFn;
139056
+ if (srcIsFloat && dstIsFloat) {
139057
+ if (rounding !== "nearest-even") {
139058
+ throw new InvalidMetadataError(`cast_value float -> float only supports "nearest-even" rounding, got "${rounding}"`);
139059
+ }
139060
+ baseFn = (v) => v;
139061
+ } else if (srcIsFloat && !dstIsFloat && !dstIsBigint) {
139062
+ const round2 = getRoundingFn(rounding);
139063
+ const check = makeIntRangeCheck(...INT_BOUNDS[targetType], outOfRange);
139064
+ baseFn = (v) => {
139065
+ if (!Number.isFinite(v)) {
139066
+ throw new Error(`Cannot cast ${v} to integer type without scalar_map`);
139067
+ }
139068
+ return check(round2(v));
139069
+ };
139070
+ } else if (srcIsFloat && dstIsBigint) {
139071
+ const round2 = getRoundingFn(rounding);
139072
+ const check = makeBigintRangeCheck(...BIGINT_BOUNDS[targetType], outOfRange);
139073
+ baseFn = (v) => {
139074
+ if (!Number.isFinite(v)) {
139075
+ throw new Error(`Cannot cast ${v} to integer type without scalar_map`);
139076
+ }
139077
+ return check(BigInt(round2(v)));
139078
+ };
139079
+ } else if (!srcIsFloat && !srcIsBigint && dstIsFloat) {
139080
+ baseFn = (v) => v;
139081
+ } else if (srcIsBigint && dstIsFloat) {
139082
+ baseFn = (v) => Number(v);
139083
+ } else if (!srcIsFloat && !srcIsBigint && !dstIsFloat && !dstIsBigint) {
139084
+ baseFn = makeIntRangeCheck(...INT_BOUNDS[targetType], outOfRange);
139085
+ } else if (!srcIsFloat && !srcIsBigint && dstIsBigint) {
139086
+ const check = makeBigintRangeCheck(...BIGINT_BOUNDS[targetType], outOfRange);
139087
+ baseFn = (v) => check(BigInt(v));
139088
+ } else if (srcIsBigint && !dstIsFloat && !dstIsBigint) {
139089
+ const check = makeIntRangeCheck(...INT_BOUNDS[targetType], outOfRange);
139090
+ baseFn = (v) => check(Number(v));
139091
+ } else if (srcIsBigint && dstIsBigint) {
139092
+ baseFn = makeBigintRangeCheck(...BIGINT_BOUNDS[targetType], outOfRange);
139093
+ } else {
139094
+ throw new Error(`Unhandled type combination: ${sourceType} -> ${targetType}`);
139095
+ }
139096
+ if (mapEntries.length === 0) {
139097
+ return baseFn;
139098
+ }
139099
+ const fn = (v) => {
139100
+ const mapped = scalarMapLookup(v, mapEntries);
139101
+ if (mapped !== void 0)
139102
+ return mapped;
139103
+ return baseFn(v);
139104
+ };
139105
+ return fn;
139106
+ }
138683
139107
  class Crc32cCodec {
138684
139108
  constructor() {
138685
139109
  this.kind = "bytes_to_bytes";
139110
+ this.encode = unimplementedEncode("crc32c");
138686
139111
  }
138687
139112
  static fromConfig() {
138688
139113
  return new Crc32cCodec();
138689
139114
  }
138690
- encode(_) {
138691
- throw new Error("Not implemented");
138692
- }
138693
139115
  decode(arr) {
138694
139116
  return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength - 4);
138695
139117
  }
139118
+ computeEncodedSize(decodedSize) {
139119
+ return decodedSize + 4;
139120
+ }
139121
+ }
139122
+ const SUPPORTED$1 = /* @__PURE__ */ new Set([
139123
+ "int8",
139124
+ "uint8",
139125
+ "int16",
139126
+ "uint16",
139127
+ "int32",
139128
+ "uint32",
139129
+ "int64",
139130
+ "uint64",
139131
+ "float16",
139132
+ "float32",
139133
+ "float64"
139134
+ ]);
139135
+ function assertCOrFContiguous(shape, stride) {
139136
+ const n3 = shape.length;
139137
+ let c = n3 === 0 || stride[n3 - 1] === 1;
139138
+ for (let i2 = n3 - 2; i2 >= 0 && c; i2--) {
139139
+ c = stride[i2] === stride[i2 + 1] * shape[i2 + 1];
139140
+ }
139141
+ if (c)
139142
+ return;
139143
+ let f2 = n3 === 0 || stride[0] === 1;
139144
+ for (let i2 = 1; i2 < n3 && f2; i2++) {
139145
+ f2 = stride[i2] === stride[i2 - 1] * shape[i2 - 1];
139146
+ }
139147
+ if (f2)
139148
+ return;
139149
+ throw new Error(`DeltaCodec requires C- or Fortran-contiguous strides, got shape=${JSON.stringify(shape)} stride=${JSON.stringify(stride)}`);
139150
+ }
139151
+ class DeltaCodec {
139152
+ constructor(ctr) {
139153
+ this.kind = "array_to_array";
139154
+ this.#ctr = ctr;
139155
+ }
139156
+ #ctr;
139157
+ static fromConfig(_config, meta) {
139158
+ if (!SUPPORTED$1.has(meta.dataType)) {
139159
+ throw new InvalidMetadataError(`Delta codec does not support data type: ${meta.dataType}`);
139160
+ }
139161
+ return new DeltaCodec(getCtr(meta.dataType));
139162
+ }
139163
+ encode(chunk) {
139164
+ assertCOrFContiguous(chunk.shape, chunk.stride);
139165
+ const src = chunk.data;
139166
+ const out = new this.#ctr(src.length);
139167
+ out[0] = src[0];
139168
+ for (let i2 = 1; i2 < src.length; i2++) {
139169
+ out[i2] = src[i2] - src[i2 - 1];
139170
+ }
139171
+ return { data: out, shape: chunk.shape, stride: chunk.stride };
139172
+ }
139173
+ decode(chunk) {
139174
+ assertCOrFContiguous(chunk.shape, chunk.stride);
139175
+ const src = chunk.data;
139176
+ const out = new this.#ctr(src.length);
139177
+ out[0] = src[0];
139178
+ for (let i2 = 1; i2 < src.length; i2++) {
139179
+ out[i2] = out[i2 - 1] + src[i2];
139180
+ }
139181
+ return { data: out, shape: chunk.shape, stride: chunk.stride };
139182
+ }
138696
139183
  }
138697
139184
  class GzipCodec {
138698
139185
  constructor() {
138699
139186
  this.kind = "bytes_to_bytes";
139187
+ this.encode = unimplementedEncode("gzip");
138700
139188
  }
138701
139189
  static fromConfig(_) {
138702
139190
  return new GzipCodec();
138703
139191
  }
138704
- encode(_bytes) {
138705
- throw new Error("Gzip encoding is not enabled by default. Please register a custom codec with `numcodecs/gzip`.");
138706
- }
138707
139192
  async decode(bytes) {
138708
139193
  const buffer = await decompress(bytes, { format: "gzip" });
138709
139194
  return new Uint8Array(buffer);
138710
139195
  }
138711
139196
  }
138712
- function throw_on_nan_replacer(_key, value) {
139197
+ function throwOnNanReplacer(_key, value) {
138713
139198
  assert$1(!Number.isNaN(value), "JsonCodec allow_nan is false but NaN was encountered during encoding.");
138714
139199
  assert$1(value !== Number.POSITIVE_INFINITY, "JsonCodec allow_nan is false but Infinity was encountered during encoding.");
138715
139200
  assert$1(value !== Number.NEGATIVE_INFINITY, "JsonCodec allow_nan is false but -Infinity was encountered during encoding.");
138716
139201
  return value;
138717
139202
  }
138718
- function sort_keys_replacer(_key, value) {
139203
+ function sortKeysReplacer(_key, value) {
138719
139204
  return value instanceof Object && !Array.isArray(value) ? Object.keys(value).sort().reduce((sorted, key) => {
138720
139205
  sorted[key] = value[key];
138721
139206
  return sorted;
@@ -138734,7 +139219,7 @@ class JsonCodec {
138734
139219
  separators = [", ", ": "];
138735
139220
  }
138736
139221
  }
138737
- this.#encoder_config = {
139222
+ this.#encoderConfig = {
138738
139223
  encoding,
138739
139224
  skipkeys,
138740
139225
  ensure_ascii,
@@ -138744,59 +139229,142 @@ class JsonCodec {
138744
139229
  separators,
138745
139230
  sort_keys
138746
139231
  };
138747
- this.#decoder_config = { strict };
139232
+ this.#decoderConfig = { strict };
138748
139233
  }
138749
- #encoder_config;
138750
- #decoder_config;
139234
+ #encoderConfig;
139235
+ #decoderConfig;
138751
139236
  static fromConfig(configuration) {
138752
139237
  return new JsonCodec(configuration);
138753
139238
  }
138754
139239
  encode(buf) {
138755
- const { indent, encoding, ensure_ascii, check_circular, allow_nan, sort_keys } = this.#encoder_config;
139240
+ const { indent, encoding, ensure_ascii, check_circular, allow_nan, sort_keys } = this.#encoderConfig;
138756
139241
  assert$1(encoding === "utf-8", "JsonCodec does not yet support non-utf-8 encoding.");
138757
- const replacer_functions = [];
139242
+ const replacerFunctions = [];
138758
139243
  assert$1(check_circular, "JsonCodec does not yet support skipping the check for circular references during encoding.");
138759
139244
  if (!allow_nan) {
138760
- replacer_functions.push(throw_on_nan_replacer);
139245
+ replacerFunctions.push(throwOnNanReplacer);
138761
139246
  }
138762
139247
  if (sort_keys) {
138763
- replacer_functions.push(sort_keys_replacer);
139248
+ replacerFunctions.push(sortKeysReplacer);
138764
139249
  }
138765
139250
  const items = Array.from(buf.data);
138766
139251
  items.push("|O");
138767
139252
  items.push(buf.shape);
138768
139253
  let replacer;
138769
- if (replacer_functions.length) {
139254
+ if (replacerFunctions.length) {
138770
139255
  replacer = (key, value) => {
138771
- let new_value = value;
138772
- for (let sub_replacer of replacer_functions) {
138773
- new_value = sub_replacer(key, new_value);
139256
+ let newValue = value;
139257
+ for (let subReplacer of replacerFunctions) {
139258
+ newValue = subReplacer(key, newValue);
138774
139259
  }
138775
- return new_value;
139260
+ return newValue;
138776
139261
  };
138777
139262
  }
138778
- let json_str = JSON.stringify(items, replacer, indent);
139263
+ let jsonStr = JSON.stringify(items, replacer, indent);
138779
139264
  if (ensure_ascii) {
138780
- json_str = json_str.replace(/[\u007F-\uFFFF]/g, (chr) => {
138781
- const full_str = `0000${chr.charCodeAt(0).toString(16)}`;
138782
- const sub_str = full_str.substring(full_str.length - 4);
138783
- return `\\u${sub_str}`;
139265
+ jsonStr = jsonStr.replace(/[\u007F-\uFFFF]/g, (chr) => {
139266
+ const fullStr = `0000${chr.charCodeAt(0).toString(16)}`;
139267
+ const subStr = fullStr.substring(fullStr.length - 4);
139268
+ return `\\u${subStr}`;
138784
139269
  });
138785
139270
  }
138786
- return new TextEncoder().encode(json_str);
139271
+ return new TextEncoder().encode(jsonStr);
138787
139272
  }
138788
139273
  decode(bytes) {
138789
- const { strict } = this.#decoder_config;
139274
+ const { strict } = this.#decoderConfig;
138790
139275
  assert$1(strict, "JsonCodec does not yet support non-strict decoding.");
138791
- const items = json_decode_object(bytes);
139276
+ const items = jsonDecodeObject(bytes);
138792
139277
  const shape = items.pop();
138793
139278
  items.pop();
138794
139279
  assert$1(shape, "0D not implemented for JsonCodec.");
138795
- const stride = get_strides(shape, "C");
139280
+ const stride = getStrides(shape, "C");
138796
139281
  const data = items;
138797
139282
  return { data, shape, stride };
138798
139283
  }
138799
139284
  }
139285
+ const SUPPORTED = /* @__PURE__ */ new Set([
139286
+ "int8",
139287
+ "uint8",
139288
+ "int16",
139289
+ "uint16",
139290
+ "int32",
139291
+ "uint32",
139292
+ "int64",
139293
+ "uint64",
139294
+ "float16",
139295
+ "float32",
139296
+ "float64"
139297
+ ]);
139298
+ class ScaleOffsetCodec {
139299
+ constructor(scale2, offset, ctr) {
139300
+ this.kind = "array_to_array";
139301
+ this.encode = unimplementedEncode("scale_offset");
139302
+ this.#scale = scale2;
139303
+ this.#offset = offset;
139304
+ this.#ctr = ctr;
139305
+ }
139306
+ #ctr;
139307
+ #scale;
139308
+ #offset;
139309
+ static fromConfig(config3, meta) {
139310
+ if (!SUPPORTED.has(meta.dataType)) {
139311
+ throw new InvalidMetadataError(`scale_offset codec does not support data type: ${meta.dataType}`);
139312
+ }
139313
+ return new ScaleOffsetCodec(parseJsonScalar(meta.dataType, config3.scale ?? 1), parseJsonScalar(meta.dataType, config3.offset ?? 0), getCtr(meta.dataType));
139314
+ }
139315
+ decode(chunk) {
139316
+ const src = chunk.data;
139317
+ const out = new this.#ctr(src.length);
139318
+ for (let i2 = 0; i2 < src.length; i2++) {
139319
+ out[i2] = src[i2] / this.#scale + this.#offset;
139320
+ }
139321
+ return { data: out, shape: chunk.shape, stride: chunk.stride };
139322
+ }
139323
+ }
139324
+ class ShuffleCodec {
139325
+ constructor(configuration, meta) {
139326
+ this.kind = "bytes_to_bytes";
139327
+ if (meta) {
139328
+ let sample = new (getCtr(meta.dataType))(0);
139329
+ assert$1("BYTES_PER_ELEMENT" in sample, `Shuffle codec requires a fixed-size dtype, got "${meta.dataType}"`);
139330
+ this.#BYTES_PER_ELEMENT = sample.BYTES_PER_ELEMENT;
139331
+ } else {
139332
+ this.#BYTES_PER_ELEMENT = configuration.elementsize ?? 4;
139333
+ }
139334
+ }
139335
+ #BYTES_PER_ELEMENT;
139336
+ static fromConfig(configuration, meta) {
139337
+ return new ShuffleCodec(configuration, meta);
139338
+ }
139339
+ encode(data) {
139340
+ return shuffle(data, this.#BYTES_PER_ELEMENT);
139341
+ }
139342
+ decode(data) {
139343
+ return unshuffle(data, this.#BYTES_PER_ELEMENT);
139344
+ }
139345
+ }
139346
+ function shuffle(data, elementSize) {
139347
+ let length2 = data.length;
139348
+ let nElements = Math.floor(length2 / elementSize);
139349
+ let result = new Uint8Array(length2);
139350
+ for (let byte = 0; byte < elementSize; byte++) {
139351
+ for (let i2 = 0; i2 < nElements; i2++) {
139352
+ result[byte * nElements + i2] = data[i2 * elementSize + byte];
139353
+ }
139354
+ }
139355
+ return result;
139356
+ }
139357
+ function unshuffle(data, elementSize) {
139358
+ let length2 = data.length;
139359
+ let nElements = Math.floor(length2 / elementSize);
139360
+ let result = new Uint8Array(length2);
139361
+ for (let byte = 0; byte < elementSize; byte++) {
139362
+ for (let i2 = 0; i2 < nElements; i2++) {
139363
+ result[i2 * elementSize + byte] = data[byte * nElements + i2];
139364
+ }
139365
+ }
139366
+ return result;
139367
+ }
138800
139368
  function proxy(arr) {
138801
139369
  if (arr instanceof BoolArray || arr instanceof ByteStringArray || arr instanceof UnicodeStringArray) {
138802
139370
  const arrp = new Proxy(arr, {
@@ -138812,40 +139380,42 @@ function proxy(arr) {
138812
139380
  }
138813
139381
  return arr;
138814
139382
  }
138815
- function empty_like(chunk, order) {
139383
+ function emptyLike(chunk, order) {
138816
139384
  let data;
138817
139385
  if (chunk.data instanceof ByteStringArray || chunk.data instanceof UnicodeStringArray) {
138818
- data = new chunk.constructor(
138819
- // @ts-expect-error
138820
- chunk.data.length,
138821
- chunk.data.chars
139386
+ data = new chunk.data.constructor(
139387
+ // @ts-expect-error - the two argument form is not on the shared type
139388
+ chunk.data.chars,
139389
+ chunk.data.length
138822
139390
  );
138823
139391
  } else {
138824
- data = new chunk.constructor(chunk.data.length);
139392
+ data = new chunk.data.constructor(chunk.data.length);
138825
139393
  }
138826
139394
  return {
138827
139395
  data,
138828
139396
  shape: chunk.shape,
138829
- stride: get_strides(chunk.shape, order)
138830
- };
138831
- }
138832
- function convert_array_order(src, target) {
138833
- let out = empty_like(src, target);
138834
- let n_dims = src.shape.length;
138835
- let size2 = src.data.length;
138836
- let index2 = Array(n_dims).fill(0);
138837
- let src_data = proxy(src.data);
138838
- let out_data = proxy(out.data);
138839
- for (let src_idx = 0; src_idx < size2; src_idx++) {
138840
- let out_idx = 0;
138841
- for (let dim = 0; dim < n_dims; dim++) {
138842
- out_idx += index2[dim] * out.stride[dim];
138843
- }
138844
- out_data[out_idx] = src_data[src_idx];
139397
+ stride: getStrides(chunk.shape, order)
139398
+ };
139399
+ }
139400
+ function convertArrayOrder(src, target) {
139401
+ let out = emptyLike(src, target);
139402
+ let nDims = src.shape.length;
139403
+ let size2 = src.shape.reduce((a2, b) => a2 * b, 1);
139404
+ let index2 = Array(nDims).fill(0);
139405
+ let srcData = proxy(src.data);
139406
+ let outData = proxy(out.data);
139407
+ for (let n3 = 0; n3 < size2; n3++) {
139408
+ let srcIdx = 0;
139409
+ let outIdx = 0;
139410
+ for (let dim = 0; dim < nDims; dim++) {
139411
+ srcIdx += index2[dim] * src.stride[dim];
139412
+ outIdx += index2[dim] * out.stride[dim];
139413
+ }
139414
+ outData[outIdx] = srcData[srcIdx];
138845
139415
  index2[0] += 1;
138846
- for (let dim = 0; dim < n_dims; dim++) {
139416
+ for (let dim = 0; dim < nDims; dim++) {
138847
139417
  if (index2[dim] === src.shape[dim]) {
138848
- if (dim + 1 === n_dims) {
139418
+ if (dim + 1 === nDims) {
138849
139419
  break;
138850
139420
  }
138851
139421
  index2[dim] = 0;
@@ -138855,13 +139425,13 @@ function convert_array_order(src, target) {
138855
139425
  }
138856
139426
  return out;
138857
139427
  }
138858
- function get_order(chunk) {
139428
+ function getOrder(chunk) {
138859
139429
  let rank2 = chunk.shape.length;
138860
139430
  assert$1(rank2 === chunk.stride.length, "Shape and stride must have the same length.");
138861
139431
  return chunk.stride.map((s2, i2) => ({ stride: s2, index: i2 })).sort((a2, b) => b.stride - a2.stride).map((entry) => entry.index);
138862
139432
  }
138863
- function matches_order(chunk, target) {
138864
- let source = get_order(chunk);
139433
+ function matchesOrder(chunk, target) {
139434
+ let source = getOrder(chunk);
138865
139435
  assert$1(source.length === target.length, "Orders must match");
138866
139436
  return source.every((dim, i2) => dim === target[i2]);
138867
139437
  }
@@ -138871,70 +139441,64 @@ class TransposeCodec {
138871
139441
  let value = configuration.order ?? "C";
138872
139442
  let rank2 = meta.shape.length;
138873
139443
  let order = new Array(rank2);
138874
- let inverseOrder = new Array(rank2);
138875
139444
  if (value === "C") {
138876
139445
  for (let i2 = 0; i2 < rank2; ++i2) {
138877
139446
  order[i2] = i2;
138878
- inverseOrder[i2] = i2;
138879
139447
  }
138880
139448
  } else if (value === "F") {
138881
139449
  for (let i2 = 0; i2 < rank2; ++i2) {
138882
139450
  order[i2] = rank2 - i2 - 1;
138883
- inverseOrder[i2] = rank2 - i2 - 1;
138884
139451
  }
138885
139452
  } else {
138886
139453
  order = value;
138887
- order.forEach((x, i2) => {
138888
- assert$1(inverseOrder[x] === void 0, `Invalid permutation: ${JSON.stringify(value)}`);
138889
- inverseOrder[x] = i2;
139454
+ let seen = new Array(rank2);
139455
+ order.forEach((x) => {
139456
+ assert$1(!seen[x], `Invalid permutation: ${JSON.stringify(value)}`);
139457
+ seen[x] = true;
138890
139458
  });
138891
139459
  }
138892
139460
  this.#order = order;
138893
- this.#inverseOrder = inverseOrder;
138894
139461
  }
138895
139462
  #order;
138896
- #inverseOrder;
138897
139463
  static fromConfig(configuration, meta) {
138898
139464
  return new TransposeCodec(configuration, meta);
138899
139465
  }
138900
139466
  encode(arr) {
138901
- if (matches_order(arr, this.#inverseOrder)) {
139467
+ if (matchesOrder(arr, this.#order)) {
138902
139468
  return arr;
138903
139469
  }
138904
- return convert_array_order(arr, this.#inverseOrder);
139470
+ return convertArrayOrder(arr, this.#order);
138905
139471
  }
138906
139472
  decode(arr) {
138907
139473
  return {
138908
139474
  data: arr.data,
138909
139475
  shape: arr.shape,
138910
- stride: get_strides(arr.shape, this.#order)
139476
+ stride: getStrides(arr.shape, this.#order)
138911
139477
  };
138912
139478
  }
138913
139479
  }
138914
139480
  class VLenUTF8 {
138915
139481
  constructor(shape) {
138916
139482
  this.kind = "array_to_bytes";
139483
+ this.encode = unimplementedEncode("vlen-utf8");
138917
139484
  this.#shape = shape;
138918
- this.#strides = get_strides(shape, "C");
139485
+ this.#strides = getStrides(shape, "C");
138919
139486
  }
138920
139487
  #shape;
138921
139488
  #strides;
138922
139489
  static fromConfig(_, meta) {
138923
139490
  return new VLenUTF8(meta.shape);
138924
139491
  }
138925
- encode(_chunk) {
138926
- throw new Error("Method not implemented.");
138927
- }
138928
139492
  decode(bytes) {
138929
139493
  let decoder = new TextDecoder();
138930
139494
  let view = new DataView(bytes.buffer);
138931
139495
  let data = Array(view.getUint32(0, true));
138932
139496
  let pos = 4;
138933
139497
  for (let i2 = 0; i2 < data.length; i2++) {
138934
- let item_length = view.getUint32(pos, true);
139498
+ let itemLength = view.getUint32(pos, true);
138935
139499
  pos += 4;
138936
- data[i2] = decoder.decode(bytes.buffer.slice(pos, pos + item_length));
138937
- pos += item_length;
139500
+ data[i2] = decoder.decode(bytes.buffer.slice(pos, pos + itemLength));
139501
+ pos += itemLength;
138938
139502
  }
138939
139503
  return { data, shape: this.#shape, stride: this.#strides };
138940
139504
  }
@@ -138942,130 +139506,175 @@ class VLenUTF8 {
138942
139506
  class ZlibCodec {
138943
139507
  constructor() {
138944
139508
  this.kind = "bytes_to_bytes";
139509
+ this.encode = unimplementedEncode("zlib");
138945
139510
  }
138946
139511
  static fromConfig(_) {
138947
139512
  return new ZlibCodec();
138948
139513
  }
138949
- encode(_bytes) {
138950
- throw new Error("Zlib encoding is not enabled by default. Please register a codec with `numcodecs/zlib`.");
138951
- }
138952
139514
  async decode(bytes) {
138953
139515
  const buffer = await decompress(bytes, { format: "deflate" });
138954
139516
  return new Uint8Array(buffer);
138955
139517
  }
138956
139518
  }
138957
- function create_default_registry() {
138958
- return (/* @__PURE__ */ new Map()).set("blosc", () => import("./blosc-DvQQ1ST0.js").then((m) => m.default)).set("lz4", () => import("./lz4-BIbM36RN.js").then((m) => m.default)).set("zstd", () => import("./zstd-CO575QiM.js").then((m) => m.default)).set("gzip", () => GzipCodec).set("zlib", () => ZlibCodec).set("transpose", () => TransposeCodec).set("bytes", () => BytesCodec).set("crc32c", () => Crc32cCodec).set("vlen-utf8", () => VLenUTF8).set("json2", () => JsonCodec).set("bitround", () => BitroundCodec);
139519
+ function createDefaultRegistry() {
139520
+ let blosc = () => import("./blosc-DvQQ1ST0.js").then((m) => m.default);
139521
+ let lz4 = () => import("./lz4-BIbM36RN.js").then((m) => m.default);
139522
+ let zstd = () => import("./zstd-CO575QiM.js").then((m) => m.default);
139523
+ let gzip2 = () => GzipCodec;
139524
+ let zlib = () => ZlibCodec;
139525
+ return (/* @__PURE__ */ new Map()).set("blosc", blosc).set("lz4", lz4).set("zstd", zstd).set("gzip", gzip2).set("zlib", zlib).set("transpose", () => TransposeCodec).set("bytes", () => BytesCodec).set("crc32c", () => Crc32cCodec).set("vlen-utf8", () => VLenUTF8).set("json2", () => JsonCodec).set("bitround", () => BitroundCodec).set("cast_value", () => CastValueCodec).set("scale_offset", () => ScaleOffsetCodec).set("numcodecs.blosc", blosc).set("numcodecs.lz4", lz4).set("numcodecs.zstd", zstd).set("numcodecs.gzip", gzip2).set("numcodecs.zlib", zlib).set("numcodecs.vlen-utf8", () => VLenUTF8).set("numcodecs.shuffle", () => ShuffleCodec).set("numcodecs.delta", () => DeltaCodec).set("numcodecs.bitround", () => BitroundCodec).set("numcodecs.json2", () => JsonCodec);
138959
139526
  }
138960
- const registry = create_default_registry();
138961
- function create_codec_pipeline(chunk_metadata) {
138962
- let codecs;
139527
+ const registry = createDefaultRegistry();
139528
+ function createCodecPipeline(chunkMetadata) {
139529
+ let codecsPromise;
139530
+ function getCodecs() {
139531
+ if (!codecsPromise)
139532
+ codecsPromise = loadCodecs(chunkMetadata);
139533
+ return codecsPromise;
139534
+ }
139535
+ async function runStep(direction, codec, fn) {
139536
+ try {
139537
+ return await fn();
139538
+ } catch (cause) {
139539
+ throw new CodecPipelineError({ direction, codec, cause });
139540
+ }
139541
+ }
138963
139542
  return {
138964
139543
  async encode(chunk) {
138965
- if (!codecs)
138966
- codecs = await load_codecs(chunk_metadata);
138967
- for (const codec of codecs.array_to_array) {
138968
- chunk = await codec.encode(chunk);
139544
+ let codecs = await getCodecs();
139545
+ for (const { name: name2, codec } of codecs.arrayToArray) {
139546
+ chunk = await runStep("encode", name2, () => codec.encode(chunk));
138969
139547
  }
138970
- let bytes = await codecs.array_to_bytes.encode(chunk);
138971
- for (const codec of codecs.bytes_to_bytes) {
138972
- bytes = await codec.encode(bytes);
139548
+ let bytes = await runStep("encode", codecs.arrayToBytes.name, () => codecs.arrayToBytes.codec.encode(chunk));
139549
+ for (const { name: name2, codec } of codecs.bytesToBytes) {
139550
+ bytes = await runStep("encode", name2, () => codec.encode(bytes));
138973
139551
  }
138974
139552
  return bytes;
138975
139553
  },
138976
139554
  async decode(bytes) {
138977
- if (!codecs)
138978
- codecs = await load_codecs(chunk_metadata);
138979
- for (let i2 = codecs.bytes_to_bytes.length - 1; i2 >= 0; i2--) {
138980
- bytes = await codecs.bytes_to_bytes[i2].decode(bytes);
139555
+ let codecs = await getCodecs();
139556
+ for (let i2 = codecs.bytesToBytes.length - 1; i2 >= 0; i2--) {
139557
+ const { name: name2, codec } = codecs.bytesToBytes[i2];
139558
+ bytes = await runStep("decode", name2, () => codec.decode(bytes));
138981
139559
  }
138982
- let chunk = await codecs.array_to_bytes.decode(bytes);
138983
- for (let i2 = codecs.array_to_array.length - 1; i2 >= 0; i2--) {
138984
- chunk = await codecs.array_to_array[i2].decode(chunk);
139560
+ let chunk = await runStep("decode", codecs.arrayToBytes.name, () => codecs.arrayToBytes.codec.decode(bytes));
139561
+ for (let i2 = codecs.arrayToArray.length - 1; i2 >= 0; i2--) {
139562
+ const { name: name2, codec } = codecs.arrayToArray[i2];
139563
+ chunk = await runStep("decode", name2, () => codec.decode(chunk));
138985
139564
  }
138986
139565
  return chunk;
139566
+ },
139567
+ async computeEncodedSize(decodedSize) {
139568
+ let codecs = await getCodecs();
139569
+ let size2 = applyEncodedSize(codecs.arrayToBytes.name, codecs.arrayToBytes.codec, decodedSize);
139570
+ for (const { name: name2, codec } of codecs.bytesToBytes) {
139571
+ size2 = applyEncodedSize(name2, codec, size2);
139572
+ }
139573
+ return size2;
138987
139574
  }
138988
139575
  };
138989
139576
  }
138990
- async function load_codecs(chunk_meta) {
138991
- let promises = chunk_meta.codecs.map(async (meta) => {
139577
+ function applyEncodedSize(name2, codec, size2) {
139578
+ if (!codec.computeEncodedSize) {
139579
+ throw new InvalidMetadataError(`Codec "${name2}" cannot compute its encoded size; it is not a fixed-size codec and cannot be used in a sharding index pipeline`);
139580
+ }
139581
+ return codec.computeEncodedSize(size2);
139582
+ }
139583
+ async function loadCodecs(chunkMeta) {
139584
+ let promises = chunkMeta.codecs.map(async (meta) => {
138992
139585
  let Codec = await registry.get(meta.name)?.();
138993
- assert$1(Codec, `Unknown codec: ${meta.name}`);
139586
+ if (!Codec) {
139587
+ throw new UnknownCodecError(meta.name);
139588
+ }
138994
139589
  return { Codec, meta };
138995
139590
  });
138996
- let array_to_array = [];
138997
- let array_to_bytes;
138998
- let bytes_to_bytes = [];
139591
+ let arrayToArray = [];
139592
+ let arrayToBytes;
139593
+ let bytesToBytes = [];
139594
+ let currentMeta = { ...chunkMeta };
138999
139595
  for await (let { Codec, meta } of promises) {
139000
- let codec = Codec.fromConfig(meta.configuration, chunk_meta);
139596
+ let codec = Codec.fromConfig(meta.configuration, currentMeta);
139001
139597
  switch (codec.kind) {
139002
139598
  case "array_to_array":
139003
- array_to_array.push(codec);
139599
+ arrayToArray.push({
139600
+ name: meta.name,
139601
+ codec
139602
+ });
139603
+ if (codec.getEncodedMeta) {
139604
+ currentMeta = codec.getEncodedMeta(currentMeta);
139605
+ }
139004
139606
  break;
139005
139607
  case "array_to_bytes":
139006
- array_to_bytes = codec;
139608
+ arrayToBytes = {
139609
+ name: meta.name,
139610
+ codec
139611
+ };
139007
139612
  break;
139008
139613
  default:
139009
- bytes_to_bytes.push(codec);
139614
+ bytesToBytes.push({
139615
+ name: meta.name,
139616
+ codec
139617
+ });
139010
139618
  }
139011
139619
  }
139012
- if (!array_to_bytes) {
139013
- assert$1(is_typed_array_like_meta(chunk_meta), `Cannot encode ${chunk_meta.data_type} to bytes without a codec`);
139014
- array_to_bytes = BytesCodec.fromConfig({ endian: "little" }, chunk_meta);
139015
- }
139016
- return { array_to_array, array_to_bytes, bytes_to_bytes };
139017
- }
139018
- function is_typed_array_like_meta(meta) {
139019
- return meta.data_type !== "v2:object" && meta.data_type !== "string";
139020
- }
139021
- class NodeNotFoundError extends Error {
139022
- constructor(context, options = {}) {
139023
- super(`Node not found: ${context}`, options);
139024
- this.name = "NodeNotFoundError";
139620
+ if (!arrayToBytes) {
139621
+ if (!isTypedArrayLikeMeta(currentMeta)) {
139622
+ throw new InvalidMetadataError(`Cannot encode ${currentMeta.dataType} to bytes without a codec`);
139623
+ }
139624
+ arrayToBytes = {
139625
+ name: "bytes",
139626
+ codec: BytesCodec.fromConfig({ endian: "little" }, currentMeta)
139627
+ };
139025
139628
  }
139629
+ return {
139630
+ arrayToArray,
139631
+ arrayToBytes,
139632
+ bytesToBytes
139633
+ };
139026
139634
  }
139027
- class KeyError extends Error {
139028
- constructor(path) {
139029
- super(`Missing key: ${path}`);
139030
- this.name = "KeyError";
139031
- }
139635
+ function isTypedArrayLikeMeta(meta) {
139636
+ return meta.dataType !== "v2:object" && meta.dataType !== "string";
139032
139637
  }
139033
139638
  const MAX_BIG_UINT = 18446744073709551615n;
139034
- function create_sharded_chunk_getter(location, shard_shape, encode_shard_key, sharding_config) {
139035
- assert$1(location.store.getRange, "Store does not support range requests");
139036
- let get_range = location.store.getRange.bind(location.store);
139037
- let index_shape = shard_shape.map((d, i2) => d / sharding_config.chunk_shape[i2]);
139038
- let index_codec = create_codec_pipeline({
139039
- data_type: "uint64",
139040
- shape: [...index_shape, 2],
139041
- codecs: sharding_config.index_codecs
139639
+ function createShardedChunkGetter(location, shardShape, encodeShardKey, shardingConfig) {
139640
+ if (!location.store.getRange) {
139641
+ throw new UnsupportedError("sharding requires a store with getRange");
139642
+ }
139643
+ let getRange = location.store.getRange.bind(location.store);
139644
+ let indexShape = shardShape.map((d, i2) => d / shardingConfig.chunk_shape[i2]);
139645
+ let indexCodec = createCodecPipeline({
139646
+ dataType: "uint64",
139647
+ shape: [...indexShape, 2],
139648
+ codecs: shardingConfig.index_codecs,
139649
+ fillValue: null
139042
139650
  });
139651
+ let rawIndexSize = 16 * indexShape.reduce((a2, b) => a2 * b, 1);
139043
139652
  let cache2 = {};
139044
- return async (chunk_coord, options) => {
139045
- let shard_coord = chunk_coord.map((d, i2) => Math.floor(d / index_shape[i2]));
139046
- let shard_path = location.resolve(encode_shard_key(shard_coord)).path;
139047
- let index2;
139048
- if (shard_path in cache2) {
139049
- index2 = cache2[shard_path];
139050
- } else {
139051
- let checksum_size = 4;
139052
- let index_size = 16 * index_shape.reduce((a2, b) => a2 * b, 1);
139053
- let bytes = await get_range(shard_path, {
139054
- suffixLength: index_size + checksum_size
139055
- }, options);
139056
- index2 = cache2[shard_path] = bytes ? await index_codec.decode(bytes) : null;
139653
+ return async (chunkCoord, options) => {
139654
+ let shardCoord = chunkCoord.map((d, i2) => Math.floor(d / indexShape[i2]));
139655
+ let shardPath = location.resolve(encodeShardKey(shardCoord)).path;
139656
+ if (!(shardPath in cache2)) {
139657
+ cache2[shardPath] = (async () => {
139658
+ let suffixLength = await indexCodec.computeEncodedSize(rawIndexSize);
139659
+ let bytes = await getRange(shardPath, { suffixLength }, options);
139660
+ return bytes ? await indexCodec.decode(bytes) : null;
139661
+ })().catch((err2) => {
139662
+ delete cache2[shardPath];
139663
+ throw err2;
139664
+ });
139057
139665
  }
139666
+ let index2 = await cache2[shardPath];
139058
139667
  if (index2 === null) {
139059
139668
  return void 0;
139060
139669
  }
139061
139670
  let { data, shape, stride } = index2;
139062
- let linear_offset = chunk_coord.map((d, i2) => d % shape[i2]).reduce((acc, sel, idx) => acc + sel * stride[idx], 0);
139063
- let offset = data[linear_offset];
139064
- let length2 = data[linear_offset + 1];
139671
+ let linearOffset = chunkCoord.map((d, i2) => d % shape[i2]).reduce((acc, sel, idx) => acc + sel * stride[idx], 0);
139672
+ let offset = data[linearOffset];
139673
+ let length2 = data[linearOffset + 1];
139065
139674
  if (offset === MAX_BIG_UINT && length2 === MAX_BIG_UINT) {
139066
139675
  return void 0;
139067
139676
  }
139068
- return get_range(shard_path, {
139677
+ return getRange(shardPath, {
139069
139678
  offset: Number(offset),
139070
139679
  length: Number(length2)
139071
139680
  }, options);
@@ -139093,55 +139702,63 @@ class Group extends Location {
139093
139702
  return this.#metadata.attributes;
139094
139703
  }
139095
139704
  }
139096
- function get_array_order(codecs) {
139097
- const maybe_transpose_codec = codecs.find((c) => c.name === "transpose");
139098
- return maybe_transpose_codec?.configuration?.order ?? "C";
139705
+ function getArrayOrder(codecs) {
139706
+ const maybeTransposeCodec = codecs.find((c) => c.name === "transpose");
139707
+ return maybeTransposeCodec?.configuration?.order ?? "C";
139708
+ }
139709
+ function projectOrder(order, axes) {
139710
+ let rank2 = new Map(axes.map((axis, i2) => [axis, i2]));
139711
+ return order.filter((axis) => rank2.has(axis)).map((axis) => rank2.get(axis));
139712
+ }
139713
+ function makeStrideGetter(nativeOrder) {
139714
+ return (shape, axes) => {
139715
+ let order = globalThis.Array.isArray(nativeOrder) && axes ? projectOrder(nativeOrder, axes) : nativeOrder;
139716
+ return getStrides(shape, order);
139717
+ };
139099
139718
  }
139100
139719
  const CONTEXT_MARKER = Symbol("zarrita.context");
139101
- function get_context(obj) {
139720
+ function getContext(obj) {
139102
139721
  return obj[CONTEXT_MARKER];
139103
139722
  }
139104
- function create_context(location, metadata) {
139105
- let { configuration } = metadata.codecs.find(is_sharding_codec) ?? {};
139106
- let shared_context = {
139107
- encode_chunk_key: create_chunk_key_encoder(metadata.chunk_key_encoding),
139108
- TypedArray: get_ctr(metadata.data_type),
139109
- fill_value: metadata.fill_value
139723
+ function createContext(location, metadata) {
139724
+ let { configuration } = metadata.codecs.find(isShardingCodec) ?? {};
139725
+ let sharedContext = {
139726
+ encodeChunkKey: createChunkKeyEncoder(metadata.chunk_key_encoding),
139727
+ TypedArray: getCtr(metadata.data_type),
139728
+ fillValue: metadata.fill_value
139110
139729
  };
139111
139730
  if (configuration) {
139112
- let native_order2 = get_array_order(configuration.codecs);
139731
+ let nativeOrder2 = getArrayOrder(configuration.codecs);
139113
139732
  return {
139114
- ...shared_context,
139733
+ ...sharedContext,
139115
139734
  kind: "sharded",
139116
- chunk_shape: configuration.chunk_shape,
139117
- codec: create_codec_pipeline({
139118
- data_type: metadata.data_type,
139735
+ chunkShape: configuration.chunk_shape,
139736
+ codec: createCodecPipeline({
139737
+ dataType: metadata.data_type,
139119
139738
  shape: configuration.chunk_shape,
139120
- codecs: configuration.codecs
139739
+ codecs: configuration.codecs,
139740
+ fillValue: metadata.fill_value
139121
139741
  }),
139122
- get_strides(shape) {
139123
- return get_strides(shape, native_order2);
139124
- },
139125
- get_chunk_bytes: create_sharded_chunk_getter(location, metadata.chunk_grid.configuration.chunk_shape, shared_context.encode_chunk_key, configuration)
139742
+ getStrides: makeStrideGetter(nativeOrder2),
139743
+ getChunkBytes: createShardedChunkGetter(location, metadata.chunk_grid.configuration.chunk_shape, sharedContext.encodeChunkKey, configuration)
139126
139744
  };
139127
139745
  }
139128
- let native_order = get_array_order(metadata.codecs);
139746
+ let nativeOrder = getArrayOrder(metadata.codecs);
139129
139747
  return {
139130
- ...shared_context,
139748
+ ...sharedContext,
139131
139749
  kind: "regular",
139132
- chunk_shape: metadata.chunk_grid.configuration.chunk_shape,
139133
- codec: create_codec_pipeline({
139134
- data_type: metadata.data_type,
139750
+ chunkShape: metadata.chunk_grid.configuration.chunk_shape,
139751
+ codec: createCodecPipeline({
139752
+ dataType: metadata.data_type,
139135
139753
  shape: metadata.chunk_grid.configuration.chunk_shape,
139136
- codecs: metadata.codecs
139754
+ codecs: metadata.codecs,
139755
+ fillValue: metadata.fill_value
139137
139756
  }),
139138
- get_strides(shape) {
139139
- return get_strides(shape, native_order);
139140
- },
139141
- async get_chunk_bytes(chunk_coords, options) {
139142
- let chunk_key = shared_context.encode_chunk_key(chunk_coords);
139143
- let chunk_path = location.resolve(chunk_key).path;
139144
- return location.store.get(chunk_path, options);
139757
+ getStrides: makeStrideGetter(nativeOrder),
139758
+ async getChunkBytes(chunkCoords, options) {
139759
+ let chunkKey = sharedContext.encodeChunkKey(chunkCoords);
139760
+ let chunkPath = location.resolve(chunkKey).path;
139761
+ return location.store.get(chunkPath, options);
139145
139762
  }
139146
139763
  };
139147
139764
  }
@@ -139151,37 +139768,58 @@ let Array$1 = class Array2 extends (_a = Location, _a) {
139151
139768
  this.kind = "array";
139152
139769
  this.#metadata = {
139153
139770
  ...metadata,
139154
- fill_value: ensure_correct_scalar(metadata)
139771
+ fill_value: ensureCorrectScalar(metadata)
139155
139772
  };
139156
- this[CONTEXT_MARKER] = create_context(this, metadata);
139773
+ this[CONTEXT_MARKER] = createContext(this, this.#metadata);
139157
139774
  }
139158
139775
  #metadata;
139159
139776
  get attrs() {
139160
139777
  return this.#metadata.attributes;
139161
139778
  }
139779
+ get dimensionNames() {
139780
+ return this.#metadata.dimension_names;
139781
+ }
139782
+ get fillValue() {
139783
+ return this.#metadata.fill_value;
139784
+ }
139162
139785
  get shape() {
139163
139786
  return this.#metadata.shape;
139164
139787
  }
139165
139788
  get chunks() {
139166
- return this[CONTEXT_MARKER].chunk_shape;
139789
+ return this[CONTEXT_MARKER].chunkShape;
139167
139790
  }
139168
139791
  get dtype() {
139169
139792
  return this.#metadata.data_type;
139170
139793
  }
139171
- async getChunk(chunk_coords, options) {
139794
+ async getChunk(chunkCoords, options, opts) {
139795
+ if (opts?.useSharedArrayBuffer) {
139796
+ assertSharedArrayBufferAvailable();
139797
+ }
139172
139798
  let context = this[CONTEXT_MARKER];
139173
- let maybe_bytes = await context.get_chunk_bytes(chunk_coords, options);
139174
- if (!maybe_bytes) {
139175
- let size2 = context.chunk_shape.reduce((a2, b) => a2 * b, 1);
139176
- let data = new context.TypedArray(size2);
139177
- data.fill(context.fill_value);
139799
+ let maybeBytes = await context.getChunkBytes(chunkCoords, options);
139800
+ if (!maybeBytes) {
139801
+ let size2 = context.chunkShape.reduce((a2, b) => a2 * b, 1);
139802
+ let data;
139803
+ if (opts?.useSharedArrayBuffer) {
139804
+ let sample = new context.TypedArray(0);
139805
+ if (!("BYTES_PER_ELEMENT" in sample)) {
139806
+ console.warn("zarrita: useSharedArrayBuffer is not supported for non-buffer-backed data types.");
139807
+ data = new context.TypedArray(size2);
139808
+ } else {
139809
+ let buffer = createBuffer(size2 * sample.BYTES_PER_ELEMENT);
139810
+ data = new context.TypedArray(buffer, 0, size2);
139811
+ }
139812
+ } else {
139813
+ data = new context.TypedArray(size2);
139814
+ }
139815
+ data.fill(context.fillValue);
139178
139816
  return {
139179
139817
  data,
139180
- shape: context.chunk_shape,
139181
- stride: context.get_strides(context.chunk_shape)
139818
+ shape: context.chunkShape,
139819
+ stride: context.getStrides(context.chunkShape)
139182
139820
  };
139183
139821
  }
139184
- return context.codec.decode(maybe_bytes);
139822
+ return context.codec.decode(maybeBytes);
139185
139823
  }
139186
139824
  /**
139187
139825
  * A helper method to narrow `zarr.Array` Dtype.
@@ -139201,9 +139839,130 @@ let Array$1 = class Array2 extends (_a = Location, _a) {
139201
139839
  * ```
139202
139840
  */
139203
139841
  is(query) {
139204
- return is_dtype(this.dtype, query);
139842
+ return isDataType(this.dtype, query);
139205
139843
  }
139206
139844
  };
139845
+ function applyExtensions(value, extensions) {
139846
+ let result = value;
139847
+ for (let ext of extensions) {
139848
+ if (result instanceof Promise) {
139849
+ result = result.then((v) => ext(v));
139850
+ } else {
139851
+ result = ext(result);
139852
+ }
139853
+ }
139854
+ return result;
139855
+ }
139856
+ function extendArray(array, ...extensions) {
139857
+ return applyExtensions(array, extensions);
139858
+ }
139859
+ async function maybeExtend(array) {
139860
+ let exts = array.store.arrayExtensions;
139861
+ if (!exts?.length)
139862
+ return array;
139863
+ let variadic = extendArray;
139864
+ return await variadic(array, ...exts);
139865
+ }
139866
+ let VERSION_COUNTER = createVersionCounter();
139867
+ function createVersionCounter() {
139868
+ let versionCounts = /* @__PURE__ */ new WeakMap();
139869
+ function getCounts(store) {
139870
+ let counts = versionCounts.get(store) ?? { v2: 0, v3: 0 };
139871
+ versionCounts.set(store, counts);
139872
+ return counts;
139873
+ }
139874
+ return {
139875
+ increment(store, version2) {
139876
+ getCounts(store)[version2] += 1;
139877
+ },
139878
+ versionMax(store) {
139879
+ let counts = getCounts(store);
139880
+ return counts.v3 > counts.v2 ? "v3" : "v2";
139881
+ }
139882
+ };
139883
+ }
139884
+ async function loadAttrs(location, signal) {
139885
+ let metaBytes = await location.store.get(location.resolve(".zattrs").path, {
139886
+ signal
139887
+ });
139888
+ if (!metaBytes)
139889
+ return {};
139890
+ return jsonDecodeObject(metaBytes);
139891
+ }
139892
+ async function openV2(location, options = {}) {
139893
+ let loc = "store" in location ? location : new Location(location);
139894
+ let { signal } = options;
139895
+ let attrs = {};
139896
+ if (options.attrs ?? true)
139897
+ attrs = await loadAttrs(loc, signal);
139898
+ signal?.throwIfAborted();
139899
+ if (options.kind === "array")
139900
+ return openArrayV2(loc, attrs, signal);
139901
+ if (options.kind === "group")
139902
+ return openGroupV2(loc, attrs, signal);
139903
+ return openArrayV2(loc, attrs, signal).catch((err2) => {
139904
+ rethrowUnless(err2, NotFoundError, InvalidMetadataError);
139905
+ return openGroupV2(loc, attrs, signal);
139906
+ });
139907
+ }
139908
+ async function openArrayV2(location, attrs, signal) {
139909
+ let { path } = location.resolve(".zarray");
139910
+ let meta = await location.store.get(path, { signal });
139911
+ if (!meta) {
139912
+ throw new NotFoundError("v2 array", { path });
139913
+ }
139914
+ VERSION_COUNTER.increment(location.store, "v2");
139915
+ return maybeExtend(new Array$1(location.store, location.path, v2ToV3ArrayMetadata(jsonDecodeObject(meta), attrs)));
139916
+ }
139917
+ async function openGroupV2(location, attrs, signal) {
139918
+ let { path } = location.resolve(".zgroup");
139919
+ let meta = await location.store.get(path, { signal });
139920
+ if (!meta) {
139921
+ throw new NotFoundError("v2 group", { path });
139922
+ }
139923
+ VERSION_COUNTER.increment(location.store, "v2");
139924
+ return new Group(location.store, location.path, v2ToV3GroupMetadata(jsonDecodeObject(meta), attrs));
139925
+ }
139926
+ async function _openV3(location, signal) {
139927
+ let { store, path } = location.resolve("zarr.json");
139928
+ let meta = await location.store.get(path, { signal });
139929
+ if (!meta) {
139930
+ throw new NotFoundError("v3 array or group", { path });
139931
+ }
139932
+ let metaDoc = jsonDecodeObject(meta);
139933
+ if (metaDoc.node_type === "array") {
139934
+ metaDoc.fill_value = ensureCorrectScalar(metaDoc);
139935
+ }
139936
+ return metaDoc.node_type === "array" ? maybeExtend(new Array$1(store, location.path, metaDoc)) : new Group(store, location.path, metaDoc);
139937
+ }
139938
+ async function openV3(location, options = {}) {
139939
+ let loc = "store" in location ? location : new Location(location);
139940
+ let node2 = await _openV3(loc, options.signal);
139941
+ VERSION_COUNTER.increment(loc.store, "v3");
139942
+ if (options.kind === void 0)
139943
+ return node2;
139944
+ if (options.kind === "array" && node2 instanceof Array$1)
139945
+ return node2;
139946
+ if (options.kind === "group" && node2 instanceof Group)
139947
+ return node2;
139948
+ let kind = node2 instanceof Array$1 ? "array" : "group";
139949
+ throw new NotFoundError(`${options.kind} at ${loc.path}`, {
139950
+ path: loc.path,
139951
+ found: kind
139952
+ });
139953
+ }
139954
+ async function open(location, options = {}) {
139955
+ let store = "store" in location ? location.store : location;
139956
+ let versionMax = VERSION_COUNTER.versionMax(store);
139957
+ let openPrimary = versionMax === "v2" ? open.v2 : open.v3;
139958
+ let openSecondary = versionMax === "v2" ? open.v3 : open.v2;
139959
+ return openPrimary(location, options).catch((err2) => {
139960
+ rethrowUnless(err2, NotFoundError, InvalidMetadataError);
139961
+ return openSecondary(location, options);
139962
+ });
139963
+ }
139964
+ open.v2 = openV2;
139965
+ open.v3 = openV3;
139207
139966
  function* range(start, stop, step = 1) {
139208
139967
  if (stop === void 0) {
139209
139968
  stop = start;
@@ -139236,15 +139995,15 @@ function* product(...iterables) {
139236
139995
  results[i2] = iterators[i2].next();
139237
139996
  }
139238
139997
  }
139239
- function slice_indices({ start, stop, step }, length2) {
139998
+ function sliceIndices({ start, stop, step }, length2) {
139240
139999
  if (step === 0) {
139241
- throw new Error("slice step cannot be zero");
140000
+ throw new InvalidSelectionError("slice step cannot be zero");
139242
140001
  }
139243
140002
  step = step ?? 1;
139244
- const step_is_negative = step < 0;
139245
- const [lower, upper] = step_is_negative ? [-1, length2 - 1] : [0, length2];
140003
+ const stepIsNegative = step < 0;
140004
+ const [lower, upper] = stepIsNegative ? [-1, length2 - 1] : [0, length2];
139246
140005
  if (start === null) {
139247
- start = step_is_negative ? upper : lower;
140006
+ start = stepIsNegative ? upper : lower;
139248
140007
  } else {
139249
140008
  if (start < 0) {
139250
140009
  start += length2;
@@ -139256,7 +140015,7 @@ function slice_indices({ start, stop, step }, length2) {
139256
140015
  }
139257
140016
  }
139258
140017
  if (stop === null) {
139259
- stop = step_is_negative ? lower : upper;
140018
+ stop = stepIsNegative ? lower : upper;
139260
140019
  } else {
139261
140020
  if (stop < 0) {
139262
140021
  stop += length2;
@@ -139269,147 +140028,153 @@ function slice_indices({ start, stop, step }, length2) {
139269
140028
  }
139270
140029
  return [start, stop, step];
139271
140030
  }
140031
+ function toInt(value) {
140032
+ if (value == null)
140033
+ return null;
140034
+ if (typeof value === "bigint") {
140035
+ if (value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
140036
+ throw new InvalidSelectionError(`Cannot safely convert ${value} to a number. Value exceeds Number.MAX_SAFE_INTEGER.`);
140037
+ }
140038
+ return Number(value);
140039
+ }
140040
+ return value;
140041
+ }
139272
140042
  function slice(start, stop, step = null) {
139273
140043
  if (stop === void 0) {
139274
140044
  stop = start;
139275
140045
  start = null;
139276
140046
  }
139277
140047
  return {
139278
- start,
139279
- stop,
139280
- step
140048
+ start: toInt(start),
140049
+ stop: toInt(stop),
140050
+ step: toInt(step)
139281
140051
  };
139282
140052
  }
139283
- function create_queue() {
140053
+ function createQueue() {
139284
140054
  const promises = [];
139285
140055
  return {
139286
140056
  add: (fn) => promises.push(fn()),
139287
140057
  onIdle: () => Promise.all(promises)
139288
140058
  };
139289
140059
  }
139290
- class IndexError extends Error {
139291
- constructor(msg) {
139292
- super(msg);
139293
- this.name = "IndexError";
139294
- }
140060
+ function errTooManyIndices(selection, shape) {
140061
+ throw new InvalidSelectionError(`too many indicies for array; expected ${shape.length}, got ${selection.length}`);
139295
140062
  }
139296
- function err_too_many_indices(selection, shape) {
139297
- throw new IndexError(`too many indicies for array; expected ${shape.length}, got ${selection.length}`);
140063
+ function errBoundscheck(dimLen) {
140064
+ throw new InvalidSelectionError(`index out of bounds for dimension with length ${dimLen}`);
139298
140065
  }
139299
- function err_boundscheck(dim_len) {
139300
- throw new IndexError(`index out of bounds for dimension with length ${dim_len}`);
140066
+ function errNegativeStep() {
140067
+ throw new InvalidSelectionError("only slices with step >= 1 are supported");
139301
140068
  }
139302
- function err_negative_step() {
139303
- throw new IndexError("only slices with step >= 1 are supported");
139304
- }
139305
- function check_selection_length(selection, shape) {
140069
+ function checkSelectionLength(selection, shape) {
139306
140070
  if (selection.length > shape.length) {
139307
- err_too_many_indices(selection, shape);
140071
+ errTooManyIndices(selection, shape);
139308
140072
  }
139309
140073
  }
139310
- function normalize_integer_selection(dim_sel, dim_len) {
139311
- dim_sel = Math.trunc(dim_sel);
139312
- if (dim_sel < 0) {
139313
- dim_sel = dim_len + dim_sel;
140074
+ function normalizeIntegerSelection(dimSel, dimLen) {
140075
+ dimSel = Math.trunc(dimSel);
140076
+ if (dimSel < 0) {
140077
+ dimSel = dimLen + dimSel;
139314
140078
  }
139315
- if (dim_sel >= dim_len || dim_sel < 0) {
139316
- err_boundscheck(dim_len);
140079
+ if (dimSel >= dimLen || dimSel < 0) {
140080
+ errBoundscheck(dimLen);
139317
140081
  }
139318
- return dim_sel;
140082
+ return dimSel;
139319
140083
  }
139320
140084
  class IntDimIndexer {
139321
- constructor({ dim_sel, dim_len, dim_chunk_len }) {
139322
- dim_sel = normalize_integer_selection(dim_sel, dim_len);
139323
- this.dim_sel = dim_sel;
139324
- this.dim_len = dim_len;
139325
- this.dim_chunk_len = dim_chunk_len;
140085
+ constructor({ dimSel, dimLen, dimChunkLen }) {
140086
+ dimSel = normalizeIntegerSelection(dimSel, dimLen);
140087
+ this.dimSel = dimSel;
140088
+ this.dimLen = dimLen;
140089
+ this.dimChunkLen = dimChunkLen;
139326
140090
  this.nitems = 1;
139327
140091
  }
139328
140092
  *[Symbol.iterator]() {
139329
- const dim_chunk_ix = Math.floor(this.dim_sel / this.dim_chunk_len);
139330
- const dim_offset = dim_chunk_ix * this.dim_chunk_len;
139331
- const dim_chunk_sel = this.dim_sel - dim_offset;
139332
- yield { dim_chunk_ix, dim_chunk_sel };
140093
+ const dimChunkIx = Math.floor(this.dimSel / this.dimChunkLen);
140094
+ const dimOffset = dimChunkIx * this.dimChunkLen;
140095
+ const dimChunkSel = this.dimSel - dimOffset;
140096
+ yield { dimChunkIx, dimChunkSel };
139333
140097
  }
139334
140098
  }
139335
140099
  class SliceDimIndexer {
139336
- constructor({ dim_sel, dim_len, dim_chunk_len }) {
139337
- const [start, stop, step] = slice_indices(dim_sel, dim_len);
140100
+ constructor({ dimSel, dimLen, dimChunkLen }) {
140101
+ const [start, stop, step] = sliceIndices(dimSel, dimLen);
139338
140102
  this.start = start;
139339
140103
  this.stop = stop;
139340
140104
  this.step = step;
139341
140105
  if (this.step < 1)
139342
- err_negative_step();
139343
- this.dim_len = dim_len;
139344
- this.dim_chunk_len = dim_chunk_len;
140106
+ errNegativeStep();
140107
+ this.dimLen = dimLen;
140108
+ this.dimChunkLen = dimChunkLen;
139345
140109
  this.nitems = Math.max(0, Math.ceil((this.stop - this.start) / this.step));
139346
- this.nchunks = Math.ceil(this.dim_len / this.dim_chunk_len);
140110
+ this.nchunks = Math.ceil(this.dimLen / this.dimChunkLen);
139347
140111
  }
139348
140112
  *[Symbol.iterator]() {
139349
- const dim_chunk_ix_from = Math.floor(this.start / this.dim_chunk_len);
139350
- const dim_chunk_ix_to = Math.ceil(this.stop / this.dim_chunk_len);
139351
- for (const dim_chunk_ix of range(dim_chunk_ix_from, dim_chunk_ix_to)) {
139352
- const dim_offset = dim_chunk_ix * this.dim_chunk_len;
139353
- const dim_limit = Math.min(this.dim_len, (dim_chunk_ix + 1) * this.dim_chunk_len);
139354
- const dim_chunk_len = dim_limit - dim_offset;
139355
- let dim_out_offset = 0;
139356
- let dim_chunk_sel_start = 0;
139357
- if (this.start < dim_offset) {
139358
- const remainder = (dim_offset - this.start) % this.step;
140113
+ const dimChunkIx_from = Math.floor(this.start / this.dimChunkLen);
140114
+ const dimChunkIx_to = Math.ceil(this.stop / this.dimChunkLen);
140115
+ for (const dimChunkIx of range(dimChunkIx_from, dimChunkIx_to)) {
140116
+ const dimOffset = dimChunkIx * this.dimChunkLen;
140117
+ const dimLimit = Math.min(this.dimLen, (dimChunkIx + 1) * this.dimChunkLen);
140118
+ const dimChunkLen = dimLimit - dimOffset;
140119
+ let dimOutOffset = 0;
140120
+ let dimChunkSelStart = 0;
140121
+ if (this.start < dimOffset) {
140122
+ const remainder = (dimOffset - this.start) % this.step;
139359
140123
  if (remainder)
139360
- dim_chunk_sel_start += this.step - remainder;
139361
- dim_out_offset = Math.ceil((dim_offset - this.start) / this.step);
140124
+ dimChunkSelStart += this.step - remainder;
140125
+ dimOutOffset = Math.ceil((dimOffset - this.start) / this.step);
139362
140126
  } else {
139363
- dim_chunk_sel_start = this.start - dim_offset;
140127
+ dimChunkSelStart = this.start - dimOffset;
139364
140128
  }
139365
- const dim_chunk_sel_stop = this.stop > dim_limit ? dim_chunk_len : this.stop - dim_offset;
139366
- const dim_chunk_sel = [
139367
- dim_chunk_sel_start,
139368
- dim_chunk_sel_stop,
140129
+ const dimChunkSelStop = this.stop > dimLimit ? dimChunkLen : this.stop - dimOffset;
140130
+ const dimChunkSel = [
140131
+ dimChunkSelStart,
140132
+ dimChunkSelStop,
139369
140133
  this.step
139370
140134
  ];
139371
- const dim_chunk_nitems = Math.ceil((dim_chunk_sel_stop - dim_chunk_sel_start) / this.step);
139372
- const dim_out_sel = [
139373
- dim_out_offset,
139374
- dim_out_offset + dim_chunk_nitems,
140135
+ const dimChunkNitems = Math.ceil((dimChunkSelStop - dimChunkSelStart) / this.step);
140136
+ const dimOutSel = [
140137
+ dimOutOffset,
140138
+ dimOutOffset + dimChunkNitems,
139375
140139
  1
139376
140140
  ];
139377
- yield { dim_chunk_ix, dim_chunk_sel, dim_out_sel };
140141
+ yield { dimChunkIx, dimChunkSel, dimOutSel };
139378
140142
  }
139379
140143
  }
139380
140144
  }
139381
- function normalize_selection(selection, shape) {
140145
+ function normalizeSelection(selection, shape) {
139382
140146
  let normalized = [];
139383
140147
  if (selection === null) {
139384
140148
  normalized = shape.map((_) => slice(null));
139385
140149
  } else if (Array.isArray(selection)) {
139386
140150
  normalized = selection.map((s2) => s2 ?? slice(null));
139387
140151
  }
139388
- check_selection_length(normalized, shape);
140152
+ checkSelectionLength(normalized, shape);
139389
140153
  return normalized;
139390
140154
  }
139391
140155
  class BasicIndexer {
139392
- constructor({ selection, shape, chunk_shape }) {
139393
- this.dim_indexers = normalize_selection(selection, shape).map((dim_sel, i2) => {
139394
- return new (typeof dim_sel === "number" ? IntDimIndexer : SliceDimIndexer)({
140156
+ constructor({ selection, shape, chunkShape }) {
140157
+ this.dimIndexers = normalizeSelection(selection, shape).map((dimSel, i2) => {
140158
+ return new (typeof dimSel === "number" ? IntDimIndexer : SliceDimIndexer)({
139395
140159
  // @ts-expect-error ts inference not strong enough to know correct chunk
139396
- dim_sel,
139397
- dim_len: shape[i2],
139398
- dim_chunk_len: chunk_shape[i2]
140160
+ dimSel,
140161
+ dimLen: shape[i2],
140162
+ dimChunkLen: chunkShape[i2]
139399
140163
  });
139400
140164
  });
139401
- this.shape = this.dim_indexers.filter((ixr) => ixr instanceof SliceDimIndexer).map((sixr) => sixr.nitems);
140165
+ this.shape = this.dimIndexers.filter((ixr) => ixr instanceof SliceDimIndexer).map((sixr) => sixr.nitems);
140166
+ this.outputAxes = this.dimIndexers.map((ixr, axis) => ({ ixr, axis })).filter(({ ixr }) => ixr instanceof SliceDimIndexer).map(({ axis }) => axis);
139402
140167
  }
139403
140168
  *[Symbol.iterator]() {
139404
- for (const dim_projections of product(...this.dim_indexers)) {
139405
- const chunk_coords = dim_projections.map((p) => p.dim_chunk_ix);
139406
- const mapping = dim_projections.map((p) => {
139407
- if ("dim_out_sel" in p) {
139408
- return { from: p.dim_chunk_sel, to: p.dim_out_sel };
140169
+ for (const dimProjections of product(...this.dimIndexers)) {
140170
+ const chunkCoords = dimProjections.map((p) => p.dimChunkIx);
140171
+ const mapping = dimProjections.map((p) => {
140172
+ if ("dimOutSel" in p) {
140173
+ return { from: p.dimChunkSel, to: p.dimOutSel };
139409
140174
  }
139410
- return { from: p.dim_chunk_sel, to: null };
140175
+ return { from: p.dimChunkSel, to: null };
139411
140176
  });
139412
- yield { chunk_coords, mapping };
140177
+ yield { chunkCoords, mapping };
139413
140178
  }
139414
140179
  }
139415
140180
  }
@@ -139417,30 +140182,53 @@ function unwrap(arr, idx) {
139417
140182
  return "get" in arr ? arr.get(idx) : arr[idx];
139418
140183
  }
139419
140184
  async function get$1(arr, selection, opts, setter2) {
139420
- let context = get_context(arr);
140185
+ if (opts.useSharedArrayBuffer) {
140186
+ assertSharedArrayBufferAvailable();
140187
+ }
140188
+ let signal = resolveSignal(opts);
140189
+ let context = getContext(arr);
139421
140190
  let indexer = new BasicIndexer({
139422
140191
  selection,
139423
140192
  shape: arr.shape,
139424
- chunk_shape: arr.chunks
140193
+ chunkShape: arr.chunks
139425
140194
  });
139426
- let out = setter2.prepare(new context.TypedArray(indexer.shape.reduce((a2, b) => a2 * b, 1)), indexer.shape, context.get_strides(indexer.shape));
139427
- let queue = opts.create_queue?.() ?? create_queue();
139428
- for (const { chunk_coords, mapping } of indexer) {
140195
+ if (arr.shape.length === 0) {
140196
+ let { data: data2 } = await arr.getChunk([], { signal }, { useSharedArrayBuffer: opts.useSharedArrayBuffer });
140197
+ return unwrap(data2, 0);
140198
+ }
140199
+ let size2 = indexer.shape.reduce((a2, b) => a2 * b, 1);
140200
+ let data;
140201
+ if (opts.useSharedArrayBuffer) {
140202
+ let sample = new context.TypedArray(0);
140203
+ if (!("BYTES_PER_ELEMENT" in sample)) {
140204
+ console.warn("zarrita: useSharedArrayBuffer is not supported for non-buffer-backed data types.");
140205
+ data = new context.TypedArray(size2);
140206
+ } else {
140207
+ let buffer = createBuffer(size2 * sample.BYTES_PER_ELEMENT);
140208
+ data = new context.TypedArray(buffer, 0, size2);
140209
+ }
140210
+ } else {
140211
+ data = new context.TypedArray(size2);
140212
+ }
140213
+ let out = setter2.prepare(data, indexer.shape, context.getStrides(indexer.shape, indexer.outputAxes));
140214
+ let queue = opts.createQueue?.() ?? createQueue();
140215
+ for (const { chunkCoords, mapping } of indexer) {
139429
140216
  queue.add(async () => {
139430
- let { data, shape, stride } = await arr.getChunk(chunk_coords, opts.opts);
139431
- let chunk = setter2.prepare(data, shape, stride);
139432
- setter2.set_from_chunk(out, chunk, mapping);
140217
+ signal?.throwIfAborted();
140218
+ let { data: data2, shape, stride } = await arr.getChunk(chunkCoords, { signal }, { useSharedArrayBuffer: opts.useSharedArrayBuffer });
140219
+ let chunk = setter2.prepare(data2, shape, stride);
140220
+ setter2.setFromChunk(out, chunk, mapping);
139433
140221
  });
139434
140222
  }
139435
140223
  await queue.onIdle();
139436
140224
  return indexer.shape.length === 0 ? unwrap(out.data, 0) : out;
139437
140225
  }
139438
- function object_array_view(arr, offset = 0, size2) {
140226
+ function objectArrayView(arr, offset = 0, size2) {
139439
140227
  let length2 = size2 ?? arr.length - offset;
139440
140228
  return {
139441
140229
  length: length2,
139442
140230
  subarray(from, to = length2) {
139443
- return object_array_view(arr, offset + from, to - from);
140231
+ return objectArrayView(arr, offset + from, to - from);
139444
140232
  },
139445
140233
  set(data, start = 0) {
139446
140234
  for (let i2 = 0; i2 < data.length; i2++) {
@@ -139452,32 +140240,32 @@ function object_array_view(arr, offset = 0, size2) {
139452
140240
  }
139453
140241
  };
139454
140242
  }
139455
- function compat_chunk(arr) {
140243
+ function compatChunk(arr) {
139456
140244
  if (globalThis.Array.isArray(arr.data)) {
139457
140245
  return {
139458
140246
  // @ts-expect-error
139459
- data: object_array_view(arr.data),
140247
+ data: objectArrayView(arr.data),
139460
140248
  stride: arr.stride,
139461
- bytes_per_element: 1
140249
+ bytesPerElement: 1
139462
140250
  };
139463
140251
  }
139464
140252
  return {
139465
140253
  data: new Uint8Array(arr.data.buffer, arr.data.byteOffset, arr.data.byteLength),
139466
140254
  stride: arr.stride,
139467
- bytes_per_element: arr.data.BYTES_PER_ELEMENT
140255
+ bytesPerElement: arr.data.BYTES_PER_ELEMENT
139468
140256
  };
139469
140257
  }
139470
- function get_typed_array_constructor(arr) {
140258
+ function getTypedArrayConstructor(arr) {
139471
140259
  if ("chars" in arr) {
139472
140260
  return arr.constructor.bind(null, arr.chars);
139473
140261
  }
139474
140262
  return arr.constructor;
139475
140263
  }
139476
- function compat_scalar(arr, value) {
140264
+ function compatScalar(arr, value) {
139477
140265
  if (globalThis.Array.isArray(arr.data)) {
139478
- return object_array_view([value]);
140266
+ return objectArrayView([value]);
139479
140267
  }
139480
- let TypedArray = get_typed_array_constructor(arr.data);
140268
+ let TypedArray = getTypedArrayConstructor(arr.data);
139481
140269
  let data = new TypedArray([value]);
139482
140270
  return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
139483
140271
  }
@@ -139485,19 +140273,19 @@ const setter = {
139485
140273
  prepare(data, shape, stride) {
139486
140274
  return { data, shape, stride };
139487
140275
  },
139488
- set_scalar(dest, sel, value) {
139489
- let view = compat_chunk(dest);
139490
- set_scalar_binary(view, sel, compat_scalar(dest, value), view.bytes_per_element);
140276
+ setScalar(dest, sel, value) {
140277
+ let view = compatChunk(dest);
140278
+ setScalarBinary(view, sel, compatScalar(dest, value), view.bytesPerElement);
139491
140279
  },
139492
- set_from_chunk(dest, src, projections) {
139493
- let view = compat_chunk(dest);
139494
- set_from_chunk_binary(view, compat_chunk(src), view.bytes_per_element, projections);
140280
+ setFromChunk(dest, src, projections) {
140281
+ let view = compatChunk(dest);
140282
+ setFromChunkBinary(view, compatChunk(src), view.bytesPerElement, projections);
139495
140283
  }
139496
140284
  };
139497
140285
  async function get(arr, selection = null, opts = {}) {
139498
140286
  return get$1(arr, selection, opts, setter);
139499
140287
  }
139500
- function indices_len(start, stop, step) {
140288
+ function indicesLen(start, stop, step) {
139501
140289
  if (step < 0 && stop < start) {
139502
140290
  return Math.floor((start - stop - 1) / -step) + 1;
139503
140291
  }
@@ -139505,183 +140293,107 @@ function indices_len(start, stop, step) {
139505
140293
  return Math.floor((stop - start - 1) / step) + 1;
139506
140294
  return 0;
139507
140295
  }
139508
- function set_scalar_binary(out, out_selection, value, bytes_per_element2) {
139509
- if (out_selection.length === 0) {
140296
+ function setScalarBinary(out, outSelection, value, bytesPerElement2) {
140297
+ if (outSelection.length === 0) {
139510
140298
  out.data.set(value, 0);
139511
140299
  return;
139512
140300
  }
139513
- const [slice2, ...slices] = out_selection;
139514
- const [curr_stride, ...stride] = out.stride;
140301
+ const [slice2, ...slices] = outSelection;
140302
+ const [currStride, ...stride] = out.stride;
139515
140303
  if (typeof slice2 === "number") {
139516
- const data = out.data.subarray(curr_stride * slice2 * bytes_per_element2);
139517
- set_scalar_binary({ data, stride }, slices, value, bytes_per_element2);
140304
+ const data = out.data.subarray(currStride * slice2 * bytesPerElement2);
140305
+ setScalarBinary({ data, stride }, slices, value, bytesPerElement2);
139518
140306
  return;
139519
140307
  }
139520
140308
  const [from, to, step] = slice2;
139521
- const len2 = indices_len(from, to, step);
140309
+ const len2 = indicesLen(from, to, step);
139522
140310
  if (slices.length === 0) {
139523
140311
  for (let i2 = 0; i2 < len2; i2++) {
139524
- out.data.set(value, curr_stride * (from + step * i2) * bytes_per_element2);
140312
+ out.data.set(value, currStride * (from + step * i2) * bytesPerElement2);
139525
140313
  }
139526
140314
  return;
139527
140315
  }
139528
140316
  for (let i2 = 0; i2 < len2; i2++) {
139529
- const data = out.data.subarray(curr_stride * (from + step * i2) * bytes_per_element2);
139530
- set_scalar_binary({ data, stride }, slices, value, bytes_per_element2);
140317
+ const data = out.data.subarray(currStride * (from + step * i2) * bytesPerElement2);
140318
+ setScalarBinary({ data, stride }, slices, value, bytesPerElement2);
139531
140319
  }
139532
140320
  }
139533
- function set_from_chunk_binary(dest, src, bytes_per_element2, projections) {
140321
+ function contiguousSpan(projections, destStride, srcStride) {
140322
+ if (projections.length !== destStride.length || projections.length !== srcStride.length) {
140323
+ return null;
140324
+ }
140325
+ let size2 = 1;
140326
+ let destOffset = 0;
140327
+ let srcOffset = 0;
140328
+ for (let i2 = projections.length - 1; i2 >= 0; i2--) {
140329
+ const proj = projections[i2];
140330
+ if (proj.from === null || proj.to === null)
140331
+ return null;
140332
+ if (destStride[i2] !== size2 || srcStride[i2] !== size2)
140333
+ return null;
140334
+ const [from, to, step] = proj.to;
140335
+ const [sfrom, , sstep] = proj.from;
140336
+ if (step !== 1 || sstep !== 1)
140337
+ return null;
140338
+ destOffset += size2 * from;
140339
+ srcOffset += size2 * sfrom;
140340
+ size2 *= indicesLen(from, to, step);
140341
+ }
140342
+ return { size: size2, destOffset, srcOffset };
140343
+ }
140344
+ function setFromChunkBinary(dest, src, bytesPerElement2, projections) {
140345
+ const span = contiguousSpan(projections, dest.stride, src.stride);
140346
+ if (span !== null) {
140347
+ const offset = span.srcOffset * bytesPerElement2;
140348
+ dest.data.set(src.data.subarray(offset, offset + span.size * bytesPerElement2), span.destOffset * bytesPerElement2);
140349
+ return;
140350
+ }
139534
140351
  const [proj, ...projs] = projections;
139535
140352
  const [dstride, ...dstrides] = dest.stride;
139536
140353
  const [sstride, ...sstrides] = src.stride;
139537
140354
  if (proj.from === null) {
139538
140355
  if (projs.length === 0) {
139539
- dest.data.set(src.data.subarray(0, bytes_per_element2), proj.to * bytes_per_element2);
140356
+ dest.data.set(src.data.subarray(0, bytesPerElement2), dstride * proj.to * bytesPerElement2);
139540
140357
  return;
139541
140358
  }
139542
- set_from_chunk_binary({
139543
- data: dest.data.subarray(dstride * proj.to * bytes_per_element2),
140359
+ setFromChunkBinary({
140360
+ data: dest.data.subarray(dstride * proj.to * bytesPerElement2),
139544
140361
  stride: dstrides
139545
- }, src, bytes_per_element2, projs);
140362
+ }, src, bytesPerElement2, projs);
139546
140363
  return;
139547
140364
  }
139548
140365
  if (proj.to === null) {
139549
140366
  if (projs.length === 0) {
139550
- let offset = proj.from * bytes_per_element2;
139551
- dest.data.set(src.data.subarray(offset, offset + bytes_per_element2), 0);
140367
+ let offset = sstride * proj.from * bytesPerElement2;
140368
+ dest.data.set(src.data.subarray(offset, offset + bytesPerElement2), 0);
139552
140369
  return;
139553
140370
  }
139554
- set_from_chunk_binary(dest, {
139555
- data: src.data.subarray(sstride * proj.from * bytes_per_element2),
140371
+ setFromChunkBinary(dest, {
140372
+ data: src.data.subarray(sstride * proj.from * bytesPerElement2),
139556
140373
  stride: sstrides
139557
- }, bytes_per_element2, projs);
140374
+ }, bytesPerElement2, projs);
139558
140375
  return;
139559
140376
  }
139560
140377
  const [from, to, step] = proj.to;
139561
140378
  const [sfrom, _, sstep] = proj.from;
139562
- const len2 = indices_len(from, to, step);
140379
+ const len2 = indicesLen(from, to, step);
139563
140380
  if (projs.length === 0) {
139564
- if (step === 1 && sstep === 1 && dstride === 1 && sstride === 1) {
139565
- let offset = sfrom * bytes_per_element2;
139566
- let size2 = len2 * bytes_per_element2;
139567
- dest.data.set(src.data.subarray(offset, offset + size2), from * bytes_per_element2);
139568
- return;
139569
- }
139570
140381
  for (let i2 = 0; i2 < len2; i2++) {
139571
- let offset = sstride * (sfrom + sstep * i2) * bytes_per_element2;
139572
- dest.data.set(src.data.subarray(offset, offset + bytes_per_element2), dstride * (from + step * i2) * bytes_per_element2);
140382
+ let offset = sstride * (sfrom + sstep * i2) * bytesPerElement2;
140383
+ dest.data.set(src.data.subarray(offset, offset + bytesPerElement2), dstride * (from + step * i2) * bytesPerElement2);
139573
140384
  }
139574
140385
  return;
139575
140386
  }
139576
140387
  for (let i2 = 0; i2 < len2; i2++) {
139577
- set_from_chunk_binary({
139578
- data: dest.data.subarray(dstride * (from + i2 * step) * bytes_per_element2),
140388
+ setFromChunkBinary({
140389
+ data: dest.data.subarray(dstride * (from + i2 * step) * bytesPerElement2),
139579
140390
  stride: dstrides
139580
140391
  }, {
139581
- data: src.data.subarray(sstride * (sfrom + i2 * sstep) * bytes_per_element2),
140392
+ data: src.data.subarray(sstride * (sfrom + i2 * sstep) * bytesPerElement2),
139582
140393
  stride: sstrides
139583
- }, bytes_per_element2, projs);
139584
- }
139585
- }
139586
- let VERSION_COUNTER = create_version_counter();
139587
- function create_version_counter() {
139588
- let version_counts = /* @__PURE__ */ new WeakMap();
139589
- function get_counts(store) {
139590
- let counts = version_counts.get(store) ?? { v2: 0, v3: 0 };
139591
- version_counts.set(store, counts);
139592
- return counts;
140394
+ }, bytesPerElement2, projs);
139593
140395
  }
139594
- return {
139595
- increment(store, version2) {
139596
- get_counts(store)[version2] += 1;
139597
- },
139598
- version_max(store) {
139599
- let counts = get_counts(store);
139600
- return counts.v3 > counts.v2 ? "v3" : "v2";
139601
- }
139602
- };
139603
- }
139604
- async function load_attrs(location) {
139605
- let meta_bytes = await location.store.get(location.resolve(".zattrs").path);
139606
- if (!meta_bytes)
139607
- return {};
139608
- return json_decode_object(meta_bytes);
139609
- }
139610
- async function open_v2(location, options = {}) {
139611
- let loc = "store" in location ? location : new Location(location);
139612
- let attrs = {};
139613
- if (options.attrs ?? true)
139614
- attrs = await load_attrs(loc);
139615
- if (options.kind === "array")
139616
- return open_array_v2(loc, attrs);
139617
- if (options.kind === "group")
139618
- return open_group_v2(loc, attrs);
139619
- return open_array_v2(loc, attrs).catch((err2) => {
139620
- rethrow_unless(err2, NodeNotFoundError);
139621
- return open_group_v2(loc, attrs);
139622
- });
139623
- }
139624
- async function open_array_v2(location, attrs) {
139625
- let { path } = location.resolve(".zarray");
139626
- let meta = await location.store.get(path);
139627
- if (!meta) {
139628
- throw new NodeNotFoundError("v2 array", {
139629
- cause: new KeyError(path)
139630
- });
139631
- }
139632
- VERSION_COUNTER.increment(location.store, "v2");
139633
- return new Array$1(location.store, location.path, v2_to_v3_array_metadata(json_decode_object(meta), attrs));
139634
- }
139635
- async function open_group_v2(location, attrs) {
139636
- let { path } = location.resolve(".zgroup");
139637
- let meta = await location.store.get(path);
139638
- if (!meta) {
139639
- throw new NodeNotFoundError("v2 group", {
139640
- cause: new KeyError(path)
139641
- });
139642
- }
139643
- VERSION_COUNTER.increment(location.store, "v2");
139644
- return new Group(location.store, location.path, v2_to_v3_group_metadata(json_decode_object(meta), attrs));
139645
- }
139646
- async function _open_v3(location) {
139647
- let { store, path } = location.resolve("zarr.json");
139648
- let meta = await location.store.get(path);
139649
- if (!meta) {
139650
- throw new NodeNotFoundError("v3 array or group", {
139651
- cause: new KeyError(path)
139652
- });
139653
- }
139654
- let meta_doc = json_decode_object(meta);
139655
- if (meta_doc.node_type === "array") {
139656
- meta_doc.fill_value = ensure_correct_scalar(meta_doc);
139657
- }
139658
- return meta_doc.node_type === "array" ? new Array$1(store, location.path, meta_doc) : new Group(store, location.path, meta_doc);
139659
- }
139660
- async function open_v3(location, options = {}) {
139661
- let loc = "store" in location ? location : new Location(location);
139662
- let node2 = await _open_v3(loc);
139663
- VERSION_COUNTER.increment(loc.store, "v3");
139664
- if (options.kind === void 0)
139665
- return node2;
139666
- if (options.kind === "array" && node2 instanceof Array$1)
139667
- return node2;
139668
- if (options.kind === "group" && node2 instanceof Group)
139669
- return node2;
139670
- let kind = node2 instanceof Array$1 ? "array" : "group";
139671
- throw new Error(`Expected node of kind ${options.kind}, found ${kind}.`);
139672
- }
139673
- async function open(location, options = {}) {
139674
- let store = "store" in location ? location.store : location;
139675
- let version_max = VERSION_COUNTER.version_max(store);
139676
- let open_primary = version_max === "v2" ? open.v2 : open.v3;
139677
- let open_secondary = version_max === "v2" ? open.v3 : open.v2;
139678
- return open_primary(location, options).catch((err2) => {
139679
- rethrow_unless(err2, NodeNotFoundError);
139680
- return open_secondary(location, options);
139681
- });
139682
140396
  }
139683
- open.v2 = open_v2;
139684
- open.v3 = open_v3;
139685
140397
  function getV2DataType(dtype) {
139686
140398
  const mapping = {
139687
140399
  int8: "|i1",