@atproto/common 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4014,31 +4014,45 @@ var require_pino = __commonJS({
4014
4014
  // src/index.ts
4015
4015
  var src_exports2 = {};
4016
4016
  __export(src_exports2, {
4017
+ AsyncBuffer: () => AsyncBuffer,
4018
+ AsyncBufferFullError: () => AsyncBufferFullError,
4019
+ DAY: () => DAY,
4020
+ HOUR: () => HOUR,
4021
+ MINUTE: () => MINUTE,
4017
4022
  MaxSizeChecker: () => MaxSizeChecker,
4023
+ SECOND: () => SECOND,
4018
4024
  TID: () => TID,
4025
+ allComplete: () => allComplete,
4019
4026
  asyncFilter: () => asyncFilter,
4027
+ bailableWait: () => bailableWait,
4020
4028
  bytesToStream: () => bytesToStream,
4029
+ cborBytesToRecord: () => cborBytesToRecord,
4030
+ cborDecode: () => cborDecode,
4031
+ cborEncode: () => cborEncode,
4021
4032
  check: () => check_exports,
4022
- cidForData: () => cidForData,
4033
+ chunkArray: () => chunkArray,
4034
+ cidForCbor: () => cidForCbor,
4023
4035
  cloneStream: () => cloneStream,
4036
+ createDeferrable: () => createDeferrable,
4037
+ createDeferrables: () => createDeferrables,
4038
+ dataToCborBlock: () => dataToCborBlock,
4024
4039
  def: () => def,
4025
4040
  errHasMsg: () => errHasMsg,
4026
4041
  flattenUint8Arrays: () => flattenUint8Arrays,
4027
4042
  forwardStreamErrors: () => forwardStreamErrors,
4028
- ipldBytesToRecord: () => ipldBytesToRecord,
4029
- ipldBytesToValue: () => ipldBytesToValue,
4030
- isCid: () => isCid,
4031
4043
  isErrnoException: () => isErrnoException,
4032
4044
  noUndefinedVals: () => noUndefinedVals,
4045
+ range: () => range,
4046
+ readFromGenerator: () => readFromGenerator,
4033
4047
  s32decode: () => s32decode,
4034
4048
  s32encode: () => s32encode,
4049
+ schema: () => schema,
4035
4050
  sha256RawToCid: () => sha256RawToCid,
4036
4051
  streamSize: () => streamSize,
4037
4052
  streamToArray: () => streamToArray,
4038
4053
  subsystemLogger: () => subsystemLogger,
4039
4054
  util: () => util_exports,
4040
- valueToIpldBlock: () => valueToIpldBlock,
4041
- valueToIpldBytes: () => valueToIpldBytes,
4055
+ verifyCidForBytes: () => verifyCidForBytes,
4042
4056
  wait: () => wait
4043
4057
  });
4044
4058
  module.exports = __toCommonJS(src_exports2);
@@ -4064,10 +4078,13 @@ var isObject = (obj) => {
4064
4078
  var util_exports = {};
4065
4079
  __export(util_exports, {
4066
4080
  asyncFilter: () => asyncFilter,
4081
+ bailableWait: () => bailableWait,
4082
+ chunkArray: () => chunkArray,
4067
4083
  errHasMsg: () => errHasMsg,
4068
4084
  flattenUint8Arrays: () => flattenUint8Arrays,
4069
4085
  isErrnoException: () => isErrnoException,
4070
4086
  noUndefinedVals: () => noUndefinedVals,
4087
+ range: () => range,
4071
4088
  s32decode: () => s32decode,
4072
4089
  s32encode: () => s32encode,
4073
4090
  streamToArray: () => streamToArray,
@@ -4084,6 +4101,17 @@ var noUndefinedVals = (obj) => {
4084
4101
  var wait = (ms) => {
4085
4102
  return new Promise((res) => setTimeout(res, ms));
4086
4103
  };
4104
+ var bailableWait = (ms) => {
4105
+ let bail;
4106
+ const waitPromise = new Promise((res) => {
4107
+ const timeout = setTimeout(res, ms);
4108
+ bail = () => {
4109
+ clearTimeout(timeout);
4110
+ res();
4111
+ };
4112
+ });
4113
+ return { bail, wait: () => waitPromise };
4114
+ };
4087
4115
  var flattenUint8Arrays = (arrs) => {
4088
4116
  const length2 = arrs.reduce((acc, cur) => {
4089
4117
  return acc + cur.length;
@@ -4125,11 +4153,106 @@ var asyncFilter = async (arr, fn) => {
4125
4153
  return arr.filter((_, i) => results[i]);
4126
4154
  };
4127
4155
  var isErrnoException = (err) => {
4128
- return !!err && "code" in err;
4156
+ return !!err && err["code"];
4129
4157
  };
4130
4158
  var errHasMsg = (err, msg) => {
4131
4159
  return !!err && typeof err === "object" && err["message"] === msg;
4132
4160
  };
4161
+ var chunkArray = (arr, chunkSize) => {
4162
+ return arr.reduce((acc, cur, i) => {
4163
+ const chunkI = Math.floor(i / chunkSize);
4164
+ if (!acc[chunkI]) {
4165
+ acc[chunkI] = [];
4166
+ }
4167
+ acc[chunkI].push(cur);
4168
+ return acc;
4169
+ }, []);
4170
+ };
4171
+ var range = (num) => {
4172
+ const nums = [];
4173
+ for (let i = 0; i < num; i++) {
4174
+ nums.push(i);
4175
+ }
4176
+ return nums;
4177
+ };
4178
+
4179
+ // src/async.ts
4180
+ var readFromGenerator = async (gen, maxLength = Number.MAX_SAFE_INTEGER, timeout = 2e3) => {
4181
+ const evts = [];
4182
+ while (evts.length < maxLength) {
4183
+ const { bail, wait: wait2 } = bailableWait(timeout);
4184
+ const maybeEvt = await Promise.race([gen.next(), wait2()]);
4185
+ bail();
4186
+ if (!maybeEvt)
4187
+ break;
4188
+ const evt = maybeEvt;
4189
+ if (evt.done)
4190
+ break;
4191
+ evts.push(evt.value);
4192
+ }
4193
+ return evts;
4194
+ };
4195
+ var createDeferrable = () => {
4196
+ let resolve;
4197
+ const promise = new Promise((res) => {
4198
+ resolve = () => res();
4199
+ });
4200
+ return { resolve, complete: promise };
4201
+ };
4202
+ var createDeferrables = (count) => {
4203
+ const list = [];
4204
+ for (let i = 0; i < count; i++) {
4205
+ list.push(createDeferrable());
4206
+ }
4207
+ return list;
4208
+ };
4209
+ var allComplete = async (deferrables) => {
4210
+ await Promise.all(deferrables.map((d) => d.complete));
4211
+ };
4212
+ var AsyncBuffer = class {
4213
+ constructor(maxSize) {
4214
+ this.maxSize = maxSize;
4215
+ this.buffer = [];
4216
+ this.resetPromise();
4217
+ }
4218
+ get curr() {
4219
+ return this.buffer;
4220
+ }
4221
+ get size() {
4222
+ return this.buffer.length;
4223
+ }
4224
+ resetPromise() {
4225
+ this.promise = new Promise((r) => this.resolve = r);
4226
+ }
4227
+ push(item) {
4228
+ this.buffer.push(item);
4229
+ this.resolve();
4230
+ }
4231
+ pushMany(items) {
4232
+ items.forEach((i) => this.buffer.push(i));
4233
+ this.resolve();
4234
+ }
4235
+ async *events() {
4236
+ while (true) {
4237
+ await this.promise;
4238
+ if (this.maxSize && this.size > this.maxSize) {
4239
+ throw new AsyncBufferFullError(this.maxSize);
4240
+ }
4241
+ const [first, ...rest] = this.buffer;
4242
+ if (first) {
4243
+ this.buffer = rest;
4244
+ yield first;
4245
+ } else {
4246
+ this.resetPromise();
4247
+ }
4248
+ }
4249
+ }
4250
+ };
4251
+ var AsyncBufferFullError = class extends Error {
4252
+ constructor(maxSize) {
4253
+ super(`ReachedMaxBufferSize: ${maxSize}`);
4254
+ }
4255
+ };
4133
4256
 
4134
4257
  // src/tid.ts
4135
4258
  var lastTimestamp = 0;
@@ -4330,21 +4453,21 @@ var create = (code3, digest) => {
4330
4453
  const size = digest.byteLength;
4331
4454
  const sizeOffset = encodingLength(code3);
4332
4455
  const digestOffset = sizeOffset + encodingLength(size);
4333
- const bytes2 = new Uint8Array(digestOffset + size);
4334
- encodeTo(code3, bytes2, 0);
4335
- encodeTo(size, bytes2, sizeOffset);
4336
- bytes2.set(digest, digestOffset);
4337
- return new Digest(code3, size, digest, bytes2);
4456
+ const bytes = new Uint8Array(digestOffset + size);
4457
+ encodeTo(code3, bytes, 0);
4458
+ encodeTo(size, bytes, sizeOffset);
4459
+ bytes.set(digest, digestOffset);
4460
+ return new Digest(code3, size, digest, bytes);
4338
4461
  };
4339
4462
  var decode3 = (multihash) => {
4340
- const bytes2 = coerce(multihash);
4341
- const [code3, sizeOffset] = decode2(bytes2);
4342
- const [size, digestOffset] = decode2(bytes2.subarray(sizeOffset));
4343
- const digest = bytes2.subarray(sizeOffset + digestOffset);
4463
+ const bytes = coerce(multihash);
4464
+ const [code3, sizeOffset] = decode2(bytes);
4465
+ const [size, digestOffset] = decode2(bytes.subarray(sizeOffset));
4466
+ const digest = bytes.subarray(sizeOffset + digestOffset);
4344
4467
  if (digest.byteLength !== size) {
4345
4468
  throw new Error("Incorrect length");
4346
4469
  }
4347
- return new Digest(code3, size, digest, bytes2);
4470
+ return new Digest(code3, size, digest, bytes);
4348
4471
  };
4349
4472
  var equals2 = (a, b) => {
4350
4473
  if (a === b) {
@@ -4354,11 +4477,11 @@ var equals2 = (a, b) => {
4354
4477
  }
4355
4478
  };
4356
4479
  var Digest = class {
4357
- constructor(code3, size, digest, bytes2) {
4480
+ constructor(code3, size, digest, bytes) {
4358
4481
  this.code = code3;
4359
4482
  this.size = size;
4360
4483
  this.digest = digest;
4361
- this.bytes = bytes2;
4484
+ this.bytes = bytes;
4362
4485
  }
4363
4486
  };
4364
4487
 
@@ -4505,9 +4628,9 @@ var Encoder = class {
4505
4628
  this.prefix = prefix;
4506
4629
  this.baseEncode = baseEncode;
4507
4630
  }
4508
- encode(bytes2) {
4509
- if (bytes2 instanceof Uint8Array) {
4510
- return `${this.prefix}${this.baseEncode(bytes2)}`;
4631
+ encode(bytes) {
4632
+ if (bytes instanceof Uint8Array) {
4633
+ return `${this.prefix}${this.baseEncode(bytes)}`;
4511
4634
  } else {
4512
4635
  throw Error("Unknown type, must be binary type");
4513
4636
  }
@@ -4721,13 +4844,13 @@ var base32z = rfc4648({
4721
4844
 
4722
4845
  // ../../node_modules/multiformats/esm/src/cid.js
4723
4846
  var CID = class {
4724
- constructor(version2, code3, multihash, bytes2) {
4847
+ constructor(version2, code3, multihash, bytes) {
4725
4848
  this.code = code3;
4726
4849
  this.version = version2;
4727
4850
  this.multihash = multihash;
4728
- this.bytes = bytes2;
4729
- this.byteOffset = bytes2.byteOffset;
4730
- this.byteLength = bytes2.byteLength;
4851
+ this.bytes = bytes;
4852
+ this.byteOffset = bytes.byteOffset;
4853
+ this.byteLength = bytes.byteLength;
4731
4854
  this.asCID = this;
4732
4855
  this._baseCache = /* @__PURE__ */ new Map();
4733
4856
  Object.defineProperties(this, {
@@ -4777,12 +4900,12 @@ var CID = class {
4777
4900
  return other && this.code === other.code && this.version === other.version && equals2(this.multihash, other.multihash);
4778
4901
  }
4779
4902
  toString(base2) {
4780
- const { bytes: bytes2, version: version2, _baseCache } = this;
4903
+ const { bytes, version: version2, _baseCache } = this;
4781
4904
  switch (version2) {
4782
4905
  case 0:
4783
- return toStringV0(bytes2, _baseCache, base2 || base58btc.encoder);
4906
+ return toStringV0(bytes, _baseCache, base2 || base58btc.encoder);
4784
4907
  default:
4785
- return toStringV1(bytes2, _baseCache, base2 || base32.encoder);
4908
+ return toStringV1(bytes, _baseCache, base2 || base32.encoder);
4786
4909
  }
4787
4910
  }
4788
4911
  toJSON() {
@@ -4821,8 +4944,8 @@ var CID = class {
4821
4944
  if (value instanceof CID) {
4822
4945
  return value;
4823
4946
  } else if (value != null && value.asCID === value) {
4824
- const { version: version2, code: code3, multihash, bytes: bytes2 } = value;
4825
- return new CID(version2, code3, multihash, bytes2 || encodeCID(version2, code3, multihash.bytes));
4947
+ const { version: version2, code: code3, multihash, bytes } = value;
4948
+ return new CID(version2, code3, multihash, bytes || encodeCID(version2, code3, multihash.bytes));
4826
4949
  } else if (value != null && value[cidSymbol] === true) {
4827
4950
  const { version: version2, multihash, code: code3 } = value;
4828
4951
  const digest = decode3(multihash);
@@ -4844,8 +4967,8 @@ var CID = class {
4844
4967
  }
4845
4968
  }
4846
4969
  case 1: {
4847
- const bytes2 = encodeCID(version2, code3, digest.bytes);
4848
- return new CID(version2, code3, digest, bytes2);
4970
+ const bytes = encodeCID(version2, code3, digest.bytes);
4971
+ return new CID(version2, code3, digest, bytes);
4849
4972
  }
4850
4973
  default: {
4851
4974
  throw new Error("Invalid version");
@@ -4858,26 +4981,26 @@ var CID = class {
4858
4981
  static createV1(code3, digest) {
4859
4982
  return CID.create(1, code3, digest);
4860
4983
  }
4861
- static decode(bytes2) {
4862
- const [cid2, remainder] = CID.decodeFirst(bytes2);
4984
+ static decode(bytes) {
4985
+ const [cid, remainder] = CID.decodeFirst(bytes);
4863
4986
  if (remainder.length) {
4864
4987
  throw new Error("Incorrect length");
4865
4988
  }
4866
- return cid2;
4989
+ return cid;
4867
4990
  }
4868
- static decodeFirst(bytes2) {
4869
- const specs = CID.inspectBytes(bytes2);
4991
+ static decodeFirst(bytes) {
4992
+ const specs = CID.inspectBytes(bytes);
4870
4993
  const prefixSize = specs.size - specs.multihashSize;
4871
- const multihashBytes = coerce(bytes2.subarray(prefixSize, prefixSize + specs.multihashSize));
4994
+ const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize));
4872
4995
  if (multihashBytes.byteLength !== specs.multihashSize) {
4873
4996
  throw new Error("Incorrect length");
4874
4997
  }
4875
4998
  const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize);
4876
4999
  const digest = new Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes);
4877
- const cid2 = specs.version === 0 ? CID.createV0(digest) : CID.createV1(specs.codec, digest);
5000
+ const cid = specs.version === 0 ? CID.createV0(digest) : CID.createV1(specs.codec, digest);
4878
5001
  return [
4879
- cid2,
4880
- bytes2.subarray(specs.size)
5002
+ cid,
5003
+ bytes.subarray(specs.size)
4881
5004
  ];
4882
5005
  }
4883
5006
  static inspectBytes(initialBytes) {
@@ -4913,10 +5036,10 @@ var CID = class {
4913
5036
  };
4914
5037
  }
4915
5038
  static parse(source, base2) {
4916
- const [prefix, bytes2] = parseCIDtoBytes(source, base2);
4917
- const cid2 = CID.decode(bytes2);
4918
- cid2._baseCache.set(prefix, source);
4919
- return cid2;
5039
+ const [prefix, bytes] = parseCIDtoBytes(source, base2);
5040
+ const cid = CID.decode(bytes);
5041
+ cid._baseCache.set(prefix, source);
5042
+ return cid;
4920
5043
  }
4921
5044
  };
4922
5045
  var parseCIDtoBytes = (source, base2) => {
@@ -4953,29 +5076,29 @@ var parseCIDtoBytes = (source, base2) => {
4953
5076
  }
4954
5077
  }
4955
5078
  };
4956
- var toStringV0 = (bytes2, cache, base2) => {
5079
+ var toStringV0 = (bytes, cache, base2) => {
4957
5080
  const { prefix } = base2;
4958
5081
  if (prefix !== base58btc.prefix) {
4959
5082
  throw Error(`Cannot string encode V0 in ${base2.name} encoding`);
4960
5083
  }
4961
- const cid2 = cache.get(prefix);
4962
- if (cid2 == null) {
4963
- const cid3 = base2.encode(bytes2).slice(1);
4964
- cache.set(prefix, cid3);
4965
- return cid3;
4966
- } else {
5084
+ const cid = cache.get(prefix);
5085
+ if (cid == null) {
5086
+ const cid2 = base2.encode(bytes).slice(1);
5087
+ cache.set(prefix, cid2);
4967
5088
  return cid2;
5089
+ } else {
5090
+ return cid;
4968
5091
  }
4969
5092
  };
4970
- var toStringV1 = (bytes2, cache, base2) => {
5093
+ var toStringV1 = (bytes, cache, base2) => {
4971
5094
  const { prefix } = base2;
4972
- const cid2 = cache.get(prefix);
4973
- if (cid2 == null) {
4974
- const cid3 = base2.encode(bytes2);
4975
- cache.set(prefix, cid3);
4976
- return cid3;
4977
- } else {
5095
+ const cid = cache.get(prefix);
5096
+ if (cid == null) {
5097
+ const cid2 = base2.encode(bytes);
5098
+ cache.set(prefix, cid2);
4978
5099
  return cid2;
5100
+ } else {
5101
+ return cid;
4979
5102
  }
4980
5103
  };
4981
5104
  var DAG_PB_CODE = 112;
@@ -4983,11 +5106,11 @@ var SHA_256_CODE = 18;
4983
5106
  var encodeCID = (version2, code3, multihash) => {
4984
5107
  const codeOffset = encodingLength(version2);
4985
5108
  const hashOffset = codeOffset + encodingLength(code3);
4986
- const bytes2 = new Uint8Array(hashOffset + multihash.byteLength);
4987
- encodeTo(version2, bytes2, 0);
4988
- encodeTo(code3, bytes2, codeOffset);
4989
- bytes2.set(multihash, hashOffset);
4990
- return bytes2;
5109
+ const bytes = new Uint8Array(hashOffset + multihash.byteLength);
5110
+ encodeTo(version2, bytes, 0);
5111
+ encodeTo(code3, bytes, codeOffset);
5112
+ bytes.set(multihash, hashOffset);
5113
+ return bytes;
4991
5114
  };
4992
5115
  var cidSymbol = Symbol.for("@ipld/js-cid/CID");
4993
5116
  var readonly = {
@@ -5001,8 +5124,8 @@ var hidden = {
5001
5124
  configurable: false
5002
5125
  };
5003
5126
  var version = "0.0.0-dev";
5004
- var deprecate = (range, message) => {
5005
- if (range.test(version)) {
5127
+ var deprecate = (range2, message) => {
5128
+ if (range2.test(version)) {
5006
5129
  console.warn(message);
5007
5130
  } else {
5008
5131
  throw new Error(message);
@@ -5065,22 +5188,22 @@ var links = function* (source, base2) {
5065
5188
  ...path,
5066
5189
  index
5067
5190
  ];
5068
- const cid2 = CID.asCID(element);
5069
- if (cid2) {
5191
+ const cid = CID.asCID(element);
5192
+ if (cid) {
5070
5193
  yield [
5071
5194
  elementPath.join("/"),
5072
- cid2
5195
+ cid
5073
5196
  ];
5074
5197
  } else if (typeof element === "object") {
5075
5198
  yield* links(element, elementPath);
5076
5199
  }
5077
5200
  }
5078
5201
  } else {
5079
- const cid2 = CID.asCID(value);
5080
- if (cid2) {
5202
+ const cid = CID.asCID(value);
5203
+ if (cid) {
5081
5204
  yield [
5082
5205
  path.join("/"),
5083
- cid2
5206
+ cid
5084
5207
  ];
5085
5208
  } else {
5086
5209
  yield* links(value, path);
@@ -5123,10 +5246,10 @@ var get = (source, path) => {
5123
5246
  if (node == null) {
5124
5247
  throw new Error(`Object has no property at ${path.slice(0, index + 1).map((part) => `[${JSON.stringify(part)}]`).join("")}`);
5125
5248
  }
5126
- const cid2 = CID.asCID(node);
5127
- if (cid2) {
5249
+ const cid = CID.asCID(node);
5250
+ if (cid) {
5128
5251
  return {
5129
- value: cid2,
5252
+ value: cid,
5130
5253
  remaining: path.slice(index + 1).join("/")
5131
5254
  };
5132
5255
  }
@@ -5134,11 +5257,11 @@ var get = (source, path) => {
5134
5257
  return { value: node };
5135
5258
  };
5136
5259
  var Block = class {
5137
- constructor({ cid: cid2, bytes: bytes2, value }) {
5138
- if (!cid2 || !bytes2 || typeof value === "undefined")
5260
+ constructor({ cid, bytes, value }) {
5261
+ if (!cid || !bytes || typeof value === "undefined")
5139
5262
  throw new Error("Missing required argument");
5140
- this.cid = cid2;
5141
- this.bytes = bytes2;
5263
+ this.cid = cid;
5264
+ this.bytes = bytes;
5142
5265
  this.value = value;
5143
5266
  this.asBlock = this;
5144
5267
  Object.defineProperties(this, {
@@ -5163,13 +5286,13 @@ var encode3 = async ({ value, codec, hasher }) => {
5163
5286
  throw new Error('Missing required argument "value"');
5164
5287
  if (!codec || !hasher)
5165
5288
  throw new Error("Missing required argument: codec or hasher");
5166
- const bytes2 = codec.encode(value);
5167
- const hash = await hasher.digest(bytes2);
5168
- const cid2 = CID.create(1, codec.code, hash);
5289
+ const bytes = codec.encode(value);
5290
+ const hash = await hasher.digest(bytes);
5291
+ const cid = CID.create(1, codec.code, hash);
5169
5292
  return new Block({
5170
5293
  value,
5171
- bytes: bytes2,
5172
- cid: cid2
5294
+ bytes,
5295
+ cid
5173
5296
  });
5174
5297
  };
5175
5298
 
@@ -5335,10 +5458,10 @@ function asU8A(buf2) {
5335
5458
  }
5336
5459
  return isBuffer2(buf2) ? new Uint8Array(buf2.buffer, buf2.byteOffset, buf2.byteLength) : buf2;
5337
5460
  }
5338
- var toString = useBuffer ? (bytes2, start, end) => {
5339
- return end - start > 64 ? globalThis.Buffer.from(bytes2.subarray(start, end)).toString("utf8") : utf8Slice(bytes2, start, end);
5340
- } : (bytes2, start, end) => {
5341
- return end - start > 64 ? textDecoder.decode(bytes2.subarray(start, end)) : utf8Slice(bytes2, start, end);
5461
+ var toString = useBuffer ? (bytes, start, end) => {
5462
+ return end - start > 64 ? globalThis.Buffer.from(bytes.subarray(start, end)).toString("utf8") : utf8Slice(bytes, start, end);
5463
+ } : (bytes, start, end) => {
5464
+ return end - start > 64 ? textDecoder.decode(bytes.subarray(start, end)) : utf8Slice(bytes, start, end);
5342
5465
  };
5343
5466
  var fromString = useBuffer ? (string) => {
5344
5467
  return string.length > 64 ? globalThis.Buffer.from(string) : utf8ToBytes(string);
@@ -5348,13 +5471,13 @@ var fromString = useBuffer ? (string) => {
5348
5471
  var fromArray = (arr) => {
5349
5472
  return Uint8Array.from(arr);
5350
5473
  };
5351
- var slice = useBuffer ? (bytes2, start, end) => {
5352
- if (isBuffer2(bytes2)) {
5353
- return new Uint8Array(bytes2.subarray(start, end));
5474
+ var slice = useBuffer ? (bytes, start, end) => {
5475
+ if (isBuffer2(bytes)) {
5476
+ return new Uint8Array(bytes.subarray(start, end));
5354
5477
  }
5355
- return bytes2.slice(start, end);
5356
- } : (bytes2, start, end) => {
5357
- return bytes2.slice(start, end);
5478
+ return bytes.slice(start, end);
5479
+ } : (bytes, start, end) => {
5480
+ return bytes.slice(start, end);
5358
5481
  };
5359
5482
  var concat = useBuffer ? (chunks, length2) => {
5360
5483
  chunks = chunks.map((c) => c instanceof Uint8Array ? c : globalThis.Buffer.from(c));
@@ -5392,18 +5515,18 @@ function utf8ToBytes(string, units = Infinity) {
5392
5515
  let codePoint;
5393
5516
  const length2 = string.length;
5394
5517
  let leadSurrogate = null;
5395
- const bytes2 = [];
5518
+ const bytes = [];
5396
5519
  for (let i = 0; i < length2; ++i) {
5397
5520
  codePoint = string.charCodeAt(i);
5398
5521
  if (codePoint > 55295 && codePoint < 57344) {
5399
5522
  if (!leadSurrogate) {
5400
5523
  if (codePoint > 56319) {
5401
5524
  if ((units -= 3) > -1)
5402
- bytes2.push(239, 191, 189);
5525
+ bytes.push(239, 191, 189);
5403
5526
  continue;
5404
5527
  } else if (i + 1 === length2) {
5405
5528
  if ((units -= 3) > -1)
5406
- bytes2.push(239, 191, 189);
5529
+ bytes.push(239, 191, 189);
5407
5530
  continue;
5408
5531
  }
5409
5532
  leadSurrogate = codePoint;
@@ -5411,37 +5534,37 @@ function utf8ToBytes(string, units = Infinity) {
5411
5534
  }
5412
5535
  if (codePoint < 56320) {
5413
5536
  if ((units -= 3) > -1)
5414
- bytes2.push(239, 191, 189);
5537
+ bytes.push(239, 191, 189);
5415
5538
  leadSurrogate = codePoint;
5416
5539
  continue;
5417
5540
  }
5418
5541
  codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
5419
5542
  } else if (leadSurrogate) {
5420
5543
  if ((units -= 3) > -1)
5421
- bytes2.push(239, 191, 189);
5544
+ bytes.push(239, 191, 189);
5422
5545
  }
5423
5546
  leadSurrogate = null;
5424
5547
  if (codePoint < 128) {
5425
5548
  if ((units -= 1) < 0)
5426
5549
  break;
5427
- bytes2.push(codePoint);
5550
+ bytes.push(codePoint);
5428
5551
  } else if (codePoint < 2048) {
5429
5552
  if ((units -= 2) < 0)
5430
5553
  break;
5431
- bytes2.push(codePoint >> 6 | 192, codePoint & 63 | 128);
5554
+ bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128);
5432
5555
  } else if (codePoint < 65536) {
5433
5556
  if ((units -= 3) < 0)
5434
5557
  break;
5435
- bytes2.push(codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
5558
+ bytes.push(codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
5436
5559
  } else if (codePoint < 1114112) {
5437
5560
  if ((units -= 4) < 0)
5438
5561
  break;
5439
- bytes2.push(codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
5562
+ bytes.push(codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
5440
5563
  } else {
5441
5564
  throw new Error("Invalid code point");
5442
5565
  }
5443
5566
  }
5444
- return bytes2;
5567
+ return bytes;
5445
5568
  }
5446
5569
  function utf8Slice(buf2, offset, end) {
5447
5570
  const res = [];
@@ -5536,12 +5659,12 @@ var Bl = class {
5536
5659
  this.maxCursor = this._initReuseChunk.length - 1;
5537
5660
  }
5538
5661
  }
5539
- push(bytes2) {
5662
+ push(bytes) {
5540
5663
  let topChunk = this.chunks[this.chunks.length - 1];
5541
- const newMax = this.cursor + bytes2.length;
5664
+ const newMax = this.cursor + bytes.length;
5542
5665
  if (newMax <= this.maxCursor + 1) {
5543
5666
  const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
5544
- topChunk.set(bytes2, chunkPos);
5667
+ topChunk.set(bytes, chunkPos);
5545
5668
  } else {
5546
5669
  if (topChunk) {
5547
5670
  const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
@@ -5550,20 +5673,20 @@ var Bl = class {
5550
5673
  this.maxCursor = this.cursor - 1;
5551
5674
  }
5552
5675
  }
5553
- if (bytes2.length < 64 && bytes2.length < this.chunkSize) {
5676
+ if (bytes.length < 64 && bytes.length < this.chunkSize) {
5554
5677
  topChunk = alloc(this.chunkSize);
5555
5678
  this.chunks.push(topChunk);
5556
5679
  this.maxCursor += topChunk.length;
5557
5680
  if (this._initReuseChunk === null) {
5558
5681
  this._initReuseChunk = topChunk;
5559
5682
  }
5560
- topChunk.set(bytes2, 0);
5683
+ topChunk.set(bytes, 0);
5561
5684
  } else {
5562
- this.chunks.push(bytes2);
5563
- this.maxCursor += bytes2.length;
5685
+ this.chunks.push(bytes);
5686
+ this.maxCursor += bytes.length;
5564
5687
  }
5565
5688
  }
5566
- this.cursor += bytes2.length;
5689
+ this.cursor += bytes.length;
5567
5690
  }
5568
5691
  toBytes(reset = false) {
5569
5692
  let byts;
@@ -5830,13 +5953,13 @@ function tokenBytes(token) {
5830
5953
  return token.encodedBytes;
5831
5954
  }
5832
5955
  function encodeBytes(buf2, token) {
5833
- const bytes2 = tokenBytes(token);
5834
- encodeUintValue(buf2, token.type.majorEncoded, bytes2.length);
5835
- buf2.push(bytes2);
5956
+ const bytes = tokenBytes(token);
5957
+ encodeUintValue(buf2, token.type.majorEncoded, bytes.length);
5958
+ buf2.push(bytes);
5836
5959
  }
5837
5960
  encodeBytes.encodedSize = function encodedSize4(token) {
5838
- const bytes2 = tokenBytes(token);
5839
- return encodeUintValue.encodedSize(bytes2.length) + bytes2.length;
5961
+ const bytes = tokenBytes(token);
5962
+ return encodeUintValue.encodedSize(bytes.length) + bytes.length;
5840
5963
  };
5841
5964
  encodeBytes.compareTokens = function compareTokens3(tok1, tok2) {
5842
5965
  return compareBytes(tokenBytes(tok1), tokenBytes(tok2));
@@ -5993,7 +6116,7 @@ function decodeBreak(_data, _pos, _minor, options) {
5993
6116
  }
5994
6117
  return new Token(Type.break, void 0, 1);
5995
6118
  }
5996
- function createToken(value, bytes2, options) {
6119
+ function createToken(value, bytes, options) {
5997
6120
  if (options) {
5998
6121
  if (options.allowNaN === false && Number.isNaN(value)) {
5999
6122
  throw new Error(`${decodeErrPrefix} NaN values are not supported`);
@@ -6002,7 +6125,7 @@ function createToken(value, bytes2, options) {
6002
6125
  throw new Error(`${decodeErrPrefix} Infinity values are not supported`);
6003
6126
  }
6004
6127
  }
6005
- return new Token(Type.float, value, bytes2);
6128
+ return new Token(Type.float, value, bytes);
6006
6129
  }
6007
6130
  function decodeFloat16(data, pos, _minor, options) {
6008
6131
  return createToken(readFloat16(data, pos + 1), 3, options);
@@ -6661,15 +6784,15 @@ function cidEncoder(obj) {
6661
6784
  if (obj.asCID !== obj) {
6662
6785
  return null;
6663
6786
  }
6664
- const cid2 = CID.asCID(obj);
6665
- if (!cid2) {
6787
+ const cid = CID.asCID(obj);
6788
+ if (!cid) {
6666
6789
  return null;
6667
6790
  }
6668
- const bytes2 = new Uint8Array(cid2.bytes.byteLength + 1);
6669
- bytes2.set(cid2.bytes, 1);
6791
+ const bytes = new Uint8Array(cid.bytes.byteLength + 1);
6792
+ bytes.set(cid.bytes, 1);
6670
6793
  return [
6671
6794
  new Token(Type.tag, CID_CBOR_TAG),
6672
- new Token(Type.bytes, bytes2)
6795
+ new Token(Type.bytes, bytes)
6673
6796
  ];
6674
6797
  }
6675
6798
  function undefinedEncoder() {
@@ -6692,11 +6815,11 @@ var encodeOptions = {
6692
6815
  number: numberEncoder
6693
6816
  }
6694
6817
  };
6695
- function cidDecoder(bytes2) {
6696
- if (bytes2[0] !== 0) {
6818
+ function cidDecoder(bytes) {
6819
+ if (bytes[0] !== 0) {
6697
6820
  throw new Error("Invalid CID for CBOR tag 42; expected leading 0x00");
6698
6821
  }
6699
- return CID.decode(bytes2.subarray(1));
6822
+ return CID.decode(bytes.subarray(1));
6700
6823
  }
6701
6824
  var decodeOptions = {
6702
6825
  allowIndefinite: false,
@@ -6714,31 +6837,36 @@ var code2 = 113;
6714
6837
  var encode5 = (node) => encode4(node, encodeOptions);
6715
6838
  var decode6 = (data) => decode5(data, decodeOptions);
6716
6839
 
6717
- // src/blocks.ts
6718
- var valueToIpldBlock = async (data) => {
6840
+ // src/ipld.ts
6841
+ var dataToCborBlock = async (data) => {
6719
6842
  return encode3({
6720
6843
  value: data,
6721
6844
  codec: esm_exports,
6722
6845
  hasher: sha256
6723
6846
  });
6724
6847
  };
6848
+ var verifyCidForBytes = async (cid, bytes) => {
6849
+ const digest = await sha256.digest(bytes);
6850
+ const expected = CID.createV1(cid.code, digest);
6851
+ if (!cid.equals(expected)) {
6852
+ throw new Error(
6853
+ `Not a valid CID for bytes. Expected: ${expected.toString()} Got: ${cid.toString()}`
6854
+ );
6855
+ }
6856
+ };
6725
6857
  var sha256RawToCid = (hash) => {
6726
6858
  const digest = digest_exports.create(sha256.code, hash);
6727
6859
  return CID.createV1(code, digest);
6728
6860
  };
6729
- var cidForData = async (data) => {
6730
- const block = await valueToIpldBlock(data);
6861
+ var cidForCbor = async (data) => {
6862
+ const block = await dataToCborBlock(data);
6731
6863
  return block.cid;
6732
6864
  };
6733
- var valueToIpldBytes = (value) => {
6734
- return encode5(value);
6735
- };
6736
- var ipldBytesToValue = (bytes2) => {
6737
- return decode6(bytes2);
6738
- };
6739
- var ipldBytesToRecord = (bytes2) => {
6740
- const val = ipldBytesToValue(bytes2);
6741
- if (typeof val !== "object" || val === null) {
6865
+ var cborEncode = encode5;
6866
+ var cborDecode = decode6;
6867
+ var cborBytesToRecord = (bytes) => {
6868
+ const val = cborDecode(bytes);
6869
+ if (!check_exports.is(val, schema.record)) {
6742
6870
  throw new Error(`Expected object, got: ${val}`);
6743
6871
  }
6744
6872
  return val;
@@ -8203,9 +8331,9 @@ var ZodArray = class extends ZodType {
8203
8331
  return this.min(1, message);
8204
8332
  }
8205
8333
  };
8206
- ZodArray.create = (schema, params) => {
8334
+ ZodArray.create = (schema2, params) => {
8207
8335
  return new ZodArray({
8208
- type: schema,
8336
+ type: schema2,
8209
8337
  minLength: null,
8210
8338
  maxLength: null,
8211
8339
  typeName: ZodFirstPartyTypeKind.ZodArray,
@@ -8230,27 +8358,27 @@ var AugmentFactory = (def2) => (augmentation) => {
8230
8358
  })
8231
8359
  });
8232
8360
  };
8233
- function deepPartialify(schema) {
8234
- if (schema instanceof ZodObject) {
8361
+ function deepPartialify(schema2) {
8362
+ if (schema2 instanceof ZodObject) {
8235
8363
  const newShape = {};
8236
- for (const key in schema.shape) {
8237
- const fieldSchema = schema.shape[key];
8364
+ for (const key in schema2.shape) {
8365
+ const fieldSchema = schema2.shape[key];
8238
8366
  newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
8239
8367
  }
8240
8368
  return new ZodObject({
8241
- ...schema._def,
8369
+ ...schema2._def,
8242
8370
  shape: () => newShape
8243
8371
  });
8244
- } else if (schema instanceof ZodArray) {
8245
- return ZodArray.create(deepPartialify(schema.element));
8246
- } else if (schema instanceof ZodOptional) {
8247
- return ZodOptional.create(deepPartialify(schema.unwrap()));
8248
- } else if (schema instanceof ZodNullable) {
8249
- return ZodNullable.create(deepPartialify(schema.unwrap()));
8250
- } else if (schema instanceof ZodTuple) {
8251
- return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
8372
+ } else if (schema2 instanceof ZodArray) {
8373
+ return ZodArray.create(deepPartialify(schema2.element));
8374
+ } else if (schema2 instanceof ZodOptional) {
8375
+ return ZodOptional.create(deepPartialify(schema2.unwrap()));
8376
+ } else if (schema2 instanceof ZodNullable) {
8377
+ return ZodNullable.create(deepPartialify(schema2.unwrap()));
8378
+ } else if (schema2 instanceof ZodTuple) {
8379
+ return ZodTuple.create(schema2.items.map((item) => deepPartialify(item)));
8252
8380
  } else {
8253
- return schema;
8381
+ return schema2;
8254
8382
  }
8255
8383
  }
8256
8384
  var ZodObject = class extends ZodType {
@@ -8388,8 +8516,8 @@ var ZodObject = class extends ZodType {
8388
8516
  unknownKeys: "passthrough"
8389
8517
  });
8390
8518
  }
8391
- setKey(key, schema) {
8392
- return this.augment({ [key]: schema });
8519
+ setKey(key, schema2) {
8520
+ return this.augment({ [key]: schema2 });
8393
8521
  }
8394
8522
  merge(merging) {
8395
8523
  const merged = new ZodObject({
@@ -8782,10 +8910,10 @@ var ZodTuple = class extends ZodType {
8782
8910
  status.dirty();
8783
8911
  }
8784
8912
  const items = ctx.data.map((item, itemIndex) => {
8785
- const schema = this._def.items[itemIndex] || this._def.rest;
8786
- if (!schema)
8913
+ const schema2 = this._def.items[itemIndex] || this._def.rest;
8914
+ if (!schema2)
8787
8915
  return null;
8788
- return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
8916
+ return schema2._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
8789
8917
  }).filter((x) => !!x);
8790
8918
  if (ctx.common.async) {
8791
8919
  return Promise.all(items).then((results) => {
@@ -9276,9 +9404,9 @@ var ZodPromise = class extends ZodType {
9276
9404
  }));
9277
9405
  }
9278
9406
  };
9279
- ZodPromise.create = (schema, params) => {
9407
+ ZodPromise.create = (schema2, params) => {
9280
9408
  return new ZodPromise({
9281
- type: schema,
9409
+ type: schema2,
9282
9410
  typeName: ZodFirstPartyTypeKind.ZodPromise,
9283
9411
  ...processCreateParams(params)
9284
9412
  });
@@ -9382,17 +9510,17 @@ var ZodEffects = class extends ZodType {
9382
9510
  util.assertNever(effect);
9383
9511
  }
9384
9512
  };
9385
- ZodEffects.create = (schema, effect, params) => {
9513
+ ZodEffects.create = (schema2, effect, params) => {
9386
9514
  return new ZodEffects({
9387
- schema,
9515
+ schema: schema2,
9388
9516
  typeName: ZodFirstPartyTypeKind.ZodEffects,
9389
9517
  effect,
9390
9518
  ...processCreateParams(params)
9391
9519
  });
9392
9520
  };
9393
- ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
9521
+ ZodEffects.createWithPreprocess = (preprocess, schema2, params) => {
9394
9522
  return new ZodEffects({
9395
- schema,
9523
+ schema: schema2,
9396
9524
  effect: { type: "preprocess", transform: preprocess },
9397
9525
  typeName: ZodFirstPartyTypeKind.ZodEffects,
9398
9526
  ...processCreateParams(params)
@@ -9691,30 +9819,37 @@ var mod = /* @__PURE__ */ Object.freeze({
9691
9819
  });
9692
9820
 
9693
9821
  // src/types.ts
9694
- var cid = mod.any().refine((obj) => CID.asCID(obj) !== null, {
9822
+ var cidSchema = mod.any().refine((obj) => CID.asCID(obj) !== null, {
9695
9823
  message: "Not a CID"
9696
9824
  }).transform((obj) => CID.asCID(obj));
9697
- var isCid = (str) => {
9698
- try {
9699
- CID.parse(str);
9700
- return true;
9701
- } catch (err) {
9702
- return false;
9703
- }
9825
+ var schema = {
9826
+ cid: cidSchema,
9827
+ bytes: mod.instanceof(Uint8Array),
9828
+ string: mod.string(),
9829
+ record: mod.record(mod.string(), mod.unknown()),
9830
+ unknown: mod.unknown()
9704
9831
  };
9705
- var strToCid = mod.string().refine(isCid, { message: "Not a valid CID" }).transform((str) => CID.parse(str));
9706
- var bytes = mod.instanceof(Uint8Array);
9707
- var strToInt = mod.string().refine((str) => !isNaN(parseInt(str)), {
9708
- message: "Cannot parse string to integer"
9709
- }).transform((str) => parseInt(str));
9710
- var strToBool = mod.string().transform((str) => str === "true" || str === "t");
9711
9832
  var def = {
9712
- string: mod.string(),
9713
- cid,
9714
- strToCid,
9715
- bytes,
9716
- strToInt,
9717
- strToBool
9833
+ cid: {
9834
+ name: "cid",
9835
+ schema: schema.cid
9836
+ },
9837
+ bytes: {
9838
+ name: "bytes",
9839
+ schema: schema.bytes
9840
+ },
9841
+ string: {
9842
+ name: "string",
9843
+ schema: schema.string
9844
+ },
9845
+ record: {
9846
+ name: "record",
9847
+ schema: schema.record
9848
+ },
9849
+ unknown: {
9850
+ name: "unknown",
9851
+ schema: schema.unknown
9852
+ }
9718
9853
  };
9719
9854
 
9720
9855
  // src/streams.ts
@@ -9740,9 +9875,9 @@ var streamSize = async (stream) => {
9740
9875
  }
9741
9876
  return size;
9742
9877
  };
9743
- var bytesToStream = (bytes2) => {
9878
+ var bytesToStream = (bytes) => {
9744
9879
  const stream = new import_stream.Readable();
9745
- stream.push(bytes2);
9880
+ stream.push(bytes);
9746
9881
  stream.push(null);
9747
9882
  return stream;
9748
9883
  };
@@ -9761,33 +9896,53 @@ var MaxSizeChecker = class extends import_stream.Transform {
9761
9896
  return cb(null, chunk);
9762
9897
  }
9763
9898
  };
9899
+
9900
+ // src/times.ts
9901
+ var SECOND = 1e3;
9902
+ var MINUTE = SECOND * 60;
9903
+ var HOUR = MINUTE * 60;
9904
+ var DAY = HOUR * 24;
9764
9905
  // Annotate the CommonJS export names for ESM import in node:
9765
9906
  0 && (module.exports = {
9907
+ AsyncBuffer,
9908
+ AsyncBufferFullError,
9909
+ DAY,
9910
+ HOUR,
9911
+ MINUTE,
9766
9912
  MaxSizeChecker,
9913
+ SECOND,
9767
9914
  TID,
9915
+ allComplete,
9768
9916
  asyncFilter,
9917
+ bailableWait,
9769
9918
  bytesToStream,
9919
+ cborBytesToRecord,
9920
+ cborDecode,
9921
+ cborEncode,
9770
9922
  check,
9771
- cidForData,
9923
+ chunkArray,
9924
+ cidForCbor,
9772
9925
  cloneStream,
9926
+ createDeferrable,
9927
+ createDeferrables,
9928
+ dataToCborBlock,
9773
9929
  def,
9774
9930
  errHasMsg,
9775
9931
  flattenUint8Arrays,
9776
9932
  forwardStreamErrors,
9777
- ipldBytesToRecord,
9778
- ipldBytesToValue,
9779
- isCid,
9780
9933
  isErrnoException,
9781
9934
  noUndefinedVals,
9935
+ range,
9936
+ readFromGenerator,
9782
9937
  s32decode,
9783
9938
  s32encode,
9939
+ schema,
9784
9940
  sha256RawToCid,
9785
9941
  streamSize,
9786
9942
  streamToArray,
9787
9943
  subsystemLogger,
9788
9944
  util,
9789
- valueToIpldBlock,
9790
- valueToIpldBytes,
9945
+ verifyCidForBytes,
9791
9946
  wait
9792
9947
  });
9793
9948
  //# sourceMappingURL=index.js.map