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