@bcts/uniform-resources 1.0.0-alpha.11 → 1.0.0-alpha.13

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.cjs CHANGED
@@ -4898,7 +4898,13 @@ var TagsStore = class {
4898
4898
  /**
4899
4899
  * Insert a tag into the registry.
4900
4900
  *
4901
- * @param tag - The tag to register
4901
+ * Matches Rust's TagsStore::insert() behavior:
4902
+ * - Throws if the tag name is undefined or empty
4903
+ * - Throws if a tag with the same value exists with a different name
4904
+ * - Allows re-registering the same tag value with the same name
4905
+ *
4906
+ * @param tag - The tag to register (must have a non-empty name)
4907
+ * @throws Error if tag has no name, empty name, or conflicts with existing registration
4902
4908
  *
4903
4909
  * @example
4904
4910
  * ```typescript
@@ -4907,9 +4913,13 @@ var TagsStore = class {
4907
4913
  * ```
4908
4914
  */
4909
4915
  insert(tag) {
4916
+ const name = tag.name;
4917
+ if (name === void 0 || name === "") throw new Error(`Tag ${tag.value} must have a non-empty name`);
4910
4918
  const key = this.#valueKey(tag.value);
4919
+ const existing = this.#tagsByValue.get(key);
4920
+ if (existing?.name !== void 0 && existing.name !== name) throw new Error(`Attempt to register tag: ${tag.value} '${existing.name}' with different name: '${name}'`);
4911
4921
  this.#tagsByValue.set(key, tag);
4912
- if (tag.name !== void 0) this.#tagsByName.set(tag.name, tag);
4922
+ this.#tagsByName.set(name, tag);
4913
4923
  }
4914
4924
  /**
4915
4925
  * Insert multiple tags into the registry.
@@ -4947,21 +4957,6 @@ var TagsStore = class {
4947
4957
  const key = this.#valueKey(tagValue$1);
4948
4958
  this.#summarizers.set(key, summarizer);
4949
4959
  }
4950
- /**
4951
- * Remove a tag from the registry.
4952
- *
4953
- * @param tagValue - The numeric tag value to remove
4954
- * @returns true if a tag was removed, false otherwise
4955
- */
4956
- remove(tagValue$1) {
4957
- const key = this.#valueKey(tagValue$1);
4958
- const tag = this.#tagsByValue.get(key);
4959
- if (tag === void 0) return false;
4960
- this.#tagsByValue.delete(key);
4961
- if (tag.name !== void 0) this.#tagsByName.delete(tag.name);
4962
- this.#summarizers.delete(key);
4963
- return true;
4964
- }
4965
4960
  assignedNameForTag(tag) {
4966
4961
  const key = this.#valueKey(tag.value);
4967
4962
  return this.#tagsByValue.get(key)?.name;
@@ -4985,30 +4980,6 @@ var TagsStore = class {
4985
4980
  return this.#summarizers.get(key);
4986
4981
  }
4987
4982
  /**
4988
- * Get all registered tags.
4989
- *
4990
- * @returns Array of all registered tags
4991
- */
4992
- getAllTags() {
4993
- return Array.from(this.#tagsByValue.values());
4994
- }
4995
- /**
4996
- * Clear all registered tags and summarizers.
4997
- */
4998
- clear() {
4999
- this.#tagsByValue.clear();
5000
- this.#tagsByName.clear();
5001
- this.#summarizers.clear();
5002
- }
5003
- /**
5004
- * Get the number of registered tags.
5005
- *
5006
- * @returns Number of tags in the registry
5007
- */
5008
- get size() {
5009
- return this.#tagsByValue.size;
5010
- }
5011
- /**
5012
4983
  * Create a string key for a numeric tag value.
5013
4984
  * Handles both number and bigint types.
5014
4985
  *
@@ -5371,11 +5342,17 @@ function formatMap(map, opts) {
5371
5342
  }
5372
5343
  /**
5373
5344
  * Format tagged value.
5345
+ *
5346
+ * Matches Rust's diag_item() for Tagged case.
5374
5347
  */
5375
5348
  function formatTagged(tag, content, opts) {
5376
5349
  if (opts.summarize === true) {
5377
5350
  const summarizer = resolveTagsStore(opts.tags)?.summarizer(tag);
5378
- if (summarizer !== void 0) return summarizer(content, opts.flat ?? false);
5351
+ if (summarizer !== void 0) {
5352
+ const result$1 = summarizer(content, opts.flat ?? false);
5353
+ if (result$1.ok) return result$1.value;
5354
+ else return `<error: ${result$1.error.type === "Custom" ? result$1.error.message : result$1.error.type === "WrongTag" ? `expected CBOR tag ${result$1.error.expected.value}, but got ${result$1.error.actual.value}` : result$1.error.type}>`;
5355
+ }
5379
5356
  }
5380
5357
  let comment;
5381
5358
  if (opts.annotate === true) {
@@ -6006,21 +5983,20 @@ let EdgeType = /* @__PURE__ */ function(EdgeType$1) {
6006
5983
  * @param cbor - The CBOR value to traverse
6007
5984
  * @param initialState - Initial state value
6008
5985
  * @param visitor - Function to call for each element
6009
- * @returns Final state after traversal
6010
5986
  *
6011
5987
  * @example
6012
5988
  * ```typescript
6013
- * // Count all text strings in a structure
6014
- * interface CountState { count: number }
5989
+ * // Count all text strings in a structure using RefCell-like pattern
5990
+ * const count = { value: 0 };
6015
5991
  *
6016
5992
  * const structure = cbor({ name: 'Alice', tags: ['urgent', 'draft'] });
6017
- * const result = walk(structure, { count: 0 }, (element, level, edge, state) => {
5993
+ * walk(structure, null, (element, level, edge, state) => {
6018
5994
  * if (element.type === 'single' && element.cbor.type === MajorType.Text) {
6019
- * return [{ count: state.count + 1 }, false];
5995
+ * count.value++;
6020
5996
  * }
6021
5997
  * return [state, false];
6022
5998
  * });
6023
- * console.log(result.count); // 3 (name, urgent, draft)
5999
+ * console.log(count.value); // 3 (name, urgent, draft)
6024
6000
  * ```
6025
6001
  *
6026
6002
  * @example
@@ -6029,7 +6005,7 @@ let EdgeType = /* @__PURE__ */ function(EdgeType$1) {
6029
6005
  * const structure = cbor([1, 2, 3, 'found', 5, 6]);
6030
6006
  * let found = false;
6031
6007
  *
6032
- * walk(structure, null, (element, level, edge) => {
6008
+ * walk(structure, null, (element, level, edge, state) => {
6033
6009
  * if (element.type === 'single' &&
6034
6010
  * element.cbor.type === MajorType.Text &&
6035
6011
  * element.cbor.value === 'found') {
@@ -6041,7 +6017,7 @@ let EdgeType = /* @__PURE__ */ function(EdgeType$1) {
6041
6017
  * ```
6042
6018
  */
6043
6019
  const walk = (cbor$1, initialState, visitor) => {
6044
- return walkInternal(cbor$1, 0, { type: EdgeType.None }, initialState, visitor);
6020
+ walkInternal(cbor$1, 0, { type: EdgeType.None }, initialState, visitor);
6045
6021
  };
6046
6022
  /**
6047
6023
  * Internal recursive walk implementation.
@@ -6525,7 +6501,7 @@ const attachMethods = (obj) => {
6525
6501
  return this.value;
6526
6502
  },
6527
6503
  walk(initialState, visitor) {
6528
- return walk(this, initialState, visitor);
6504
+ walk(this, initialState, visitor);
6529
6505
  },
6530
6506
  validateTag(expectedTags) {
6531
6507
  if (this.type !== MajorType.Tagged) throw new CborError({ type: "WrongType" });
@@ -6661,6 +6637,16 @@ var CBORError = class extends URError {
6661
6637
  }
6662
6638
  };
6663
6639
  /**
6640
+ * Error type for UR decoder errors.
6641
+ * Matches Rust's Error::UR(String) variant.
6642
+ */
6643
+ var URDecodeError = class extends URError {
6644
+ constructor(message) {
6645
+ super(`UR decoder error (${message})`);
6646
+ this.name = "URDecodeError";
6647
+ }
6648
+ };
6649
+ /**
6664
6650
  * Helper function to check if a result is an error.
6665
6651
  */
6666
6652
  function isError(result) {
@@ -6689,13 +6675,6 @@ function isValidURType(urType) {
6689
6675
  return Array.from(urType).every((char) => isURTypeChar(char));
6690
6676
  }
6691
6677
  /**
6692
- * Validates and returns a UR type, or throws an error if invalid.
6693
- */
6694
- function validateURType(urType) {
6695
- if (!isValidURType(urType)) throw new InvalidTypeError();
6696
- return urType;
6697
- }
6698
- /**
6699
6678
  * Bytewords for encoding/decoding bytes as words.
6700
6679
  * See: https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-004-bytewords.md
6701
6680
  */
@@ -8123,14 +8102,6 @@ var MultipartEncoder = class {
8123
8102
  this._fountainEncoder = new FountainEncoder(ur.cbor().toData(), maxFragmentLen);
8124
8103
  }
8125
8104
  /**
8126
- * Returns whether the message fits in a single part.
8127
- *
8128
- * For single-part messages, consider using UR.string() directly.
8129
- */
8130
- isSinglePart() {
8131
- return this._fountainEncoder.isSinglePart();
8132
- }
8133
- /**
8134
8105
  * Gets the next part of the encoding.
8135
8106
  *
8136
8107
  * Parts 1 through seqLen are "pure" fragments containing one piece each.
@@ -8188,22 +8159,6 @@ var MultipartEncoder = class {
8188
8159
  partsCount() {
8189
8160
  return this._fountainEncoder.seqLen;
8190
8161
  }
8191
- /**
8192
- * Checks if all pure parts have been emitted.
8193
- *
8194
- * Even after this returns true, you can continue calling nextPart()
8195
- * to generate additional rateless parts for redundancy.
8196
- */
8197
- isComplete() {
8198
- return this._fountainEncoder.isComplete();
8199
- }
8200
- /**
8201
- * Resets the encoder to start from the beginning.
8202
- */
8203
- reset() {
8204
- this._currentIndex = 0;
8205
- this._fountainEncoder.reset();
8206
- }
8207
8162
  };
8208
8163
 
8209
8164
  //#endregion
@@ -8314,48 +8269,25 @@ var MultipartDecoder = class {
8314
8269
  message() {
8315
8270
  return this._decodedMessage;
8316
8271
  }
8317
- /**
8318
- * Returns the decoding progress as a fraction (0 to 1).
8319
- */
8320
- progress() {
8321
- if (this._decodedMessage !== null) return 1;
8322
- if (this._fountainDecoder === null) return 0;
8323
- return this._fountainDecoder.progress();
8324
- }
8325
- /**
8326
- * Resets the decoder to receive a new message.
8327
- */
8328
- reset() {
8329
- this._urType = null;
8330
- this._fountainDecoder = null;
8331
- this._decodedMessage = null;
8332
- }
8333
8272
  };
8334
8273
 
8335
8274
  //#endregion
8336
8275
  exports.BYTEMOJIS = BYTEMOJIS;
8337
8276
  exports.BYTEWORDS = BYTEWORDS;
8338
- exports.BYTEWORDS_MAP = BYTEWORDS_MAP;
8339
8277
  exports.BytewordsError = BytewordsError;
8340
8278
  exports.BytewordsStyle = BytewordsStyle;
8341
8279
  exports.CBORError = CBORError;
8342
- exports.FountainDecoder = FountainDecoder;
8343
- exports.FountainEncoder = FountainEncoder;
8344
8280
  exports.InvalidSchemeError = InvalidSchemeError;
8345
8281
  exports.InvalidTypeError = InvalidTypeError;
8346
- exports.MINIMAL_BYTEWORDS_MAP = MINIMAL_BYTEWORDS_MAP;
8347
8282
  exports.MultipartDecoder = MultipartDecoder;
8348
8283
  exports.MultipartEncoder = MultipartEncoder;
8349
8284
  exports.NotSinglePartError = NotSinglePartError;
8350
8285
  exports.TypeUnspecifiedError = TypeUnspecifiedError;
8351
8286
  exports.UR = UR;
8287
+ exports.URDecodeError = URDecodeError;
8352
8288
  exports.URError = URError;
8353
8289
  exports.URType = URType;
8354
8290
  exports.UnexpectedTypeError = UnexpectedTypeError;
8355
- exports.Xoshiro256 = Xoshiro256;
8356
- exports.chooseFragments = chooseFragments;
8357
- exports.crc32 = crc32;
8358
- exports.createSeed = createSeed;
8359
8291
  exports.decodeBytewords = decodeBytewords;
8360
8292
  exports.encodeBytemojisIdentifier = encodeBytemojisIdentifier;
8361
8293
  exports.encodeBytewords = encodeBytewords;
@@ -8364,10 +8296,4 @@ exports.isError = isError;
8364
8296
  exports.isURCodable = isURCodable;
8365
8297
  exports.isURDecodable = isURDecodable;
8366
8298
  exports.isUREncodable = isUREncodable;
8367
- exports.isURTypeChar = isURTypeChar;
8368
- exports.isValidURType = isValidURType;
8369
- exports.mixFragments = mixFragments;
8370
- exports.splitMessage = splitMessage;
8371
- exports.validateURType = validateURType;
8372
- exports.xorBytes = xorBytes;
8373
8299
  //# sourceMappingURL=index.cjs.map