@bcts/dcbor 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 +40 -178
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +268 -317
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +268 -317
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +40 -178
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +40 -174
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -3
- package/src/cbor.ts +3 -3
- package/src/diag.ts +17 -2
- package/src/index.ts +2 -6
- package/src/tags-store.ts +35 -54
- package/src/tags.ts +7 -6
- package/src/walk.ts +8 -179
package/dist/index.cjs
CHANGED
|
@@ -5108,7 +5108,13 @@ var TagsStore = class {
|
|
|
5108
5108
|
/**
|
|
5109
5109
|
* Insert a tag into the registry.
|
|
5110
5110
|
*
|
|
5111
|
-
*
|
|
5111
|
+
* Matches Rust's TagsStore::insert() behavior:
|
|
5112
|
+
* - Throws if the tag name is undefined or empty
|
|
5113
|
+
* - Throws if a tag with the same value exists with a different name
|
|
5114
|
+
* - Allows re-registering the same tag value with the same name
|
|
5115
|
+
*
|
|
5116
|
+
* @param tag - The tag to register (must have a non-empty name)
|
|
5117
|
+
* @throws Error if tag has no name, empty name, or conflicts with existing registration
|
|
5112
5118
|
*
|
|
5113
5119
|
* @example
|
|
5114
5120
|
* ```typescript
|
|
@@ -5117,9 +5123,13 @@ var TagsStore = class {
|
|
|
5117
5123
|
* ```
|
|
5118
5124
|
*/
|
|
5119
5125
|
insert(tag) {
|
|
5126
|
+
const name = tag.name;
|
|
5127
|
+
if (name === void 0 || name === "") throw new Error(`Tag ${tag.value} must have a non-empty name`);
|
|
5120
5128
|
const key = this.#valueKey(tag.value);
|
|
5129
|
+
const existing = this.#tagsByValue.get(key);
|
|
5130
|
+
if (existing?.name !== void 0 && existing.name !== name) throw new Error(`Attempt to register tag: ${tag.value} '${existing.name}' with different name: '${name}'`);
|
|
5121
5131
|
this.#tagsByValue.set(key, tag);
|
|
5122
|
-
|
|
5132
|
+
this.#tagsByName.set(name, tag);
|
|
5123
5133
|
}
|
|
5124
5134
|
/**
|
|
5125
5135
|
* Insert multiple tags into the registry.
|
|
@@ -5157,21 +5167,6 @@ var TagsStore = class {
|
|
|
5157
5167
|
const key = this.#valueKey(tagValue$1);
|
|
5158
5168
|
this.#summarizers.set(key, summarizer);
|
|
5159
5169
|
}
|
|
5160
|
-
/**
|
|
5161
|
-
* Remove a tag from the registry.
|
|
5162
|
-
*
|
|
5163
|
-
* @param tagValue - The numeric tag value to remove
|
|
5164
|
-
* @returns true if a tag was removed, false otherwise
|
|
5165
|
-
*/
|
|
5166
|
-
remove(tagValue$1) {
|
|
5167
|
-
const key = this.#valueKey(tagValue$1);
|
|
5168
|
-
const tag = this.#tagsByValue.get(key);
|
|
5169
|
-
if (tag === void 0) return false;
|
|
5170
|
-
this.#tagsByValue.delete(key);
|
|
5171
|
-
if (tag.name !== void 0) this.#tagsByName.delete(tag.name);
|
|
5172
|
-
this.#summarizers.delete(key);
|
|
5173
|
-
return true;
|
|
5174
|
-
}
|
|
5175
5170
|
assignedNameForTag(tag) {
|
|
5176
5171
|
const key = this.#valueKey(tag.value);
|
|
5177
5172
|
return this.#tagsByValue.get(key)?.name;
|
|
@@ -5195,30 +5190,6 @@ var TagsStore = class {
|
|
|
5195
5190
|
return this.#summarizers.get(key);
|
|
5196
5191
|
}
|
|
5197
5192
|
/**
|
|
5198
|
-
* Get all registered tags.
|
|
5199
|
-
*
|
|
5200
|
-
* @returns Array of all registered tags
|
|
5201
|
-
*/
|
|
5202
|
-
getAllTags() {
|
|
5203
|
-
return Array.from(this.#tagsByValue.values());
|
|
5204
|
-
}
|
|
5205
|
-
/**
|
|
5206
|
-
* Clear all registered tags and summarizers.
|
|
5207
|
-
*/
|
|
5208
|
-
clear() {
|
|
5209
|
-
this.#tagsByValue.clear();
|
|
5210
|
-
this.#tagsByName.clear();
|
|
5211
|
-
this.#summarizers.clear();
|
|
5212
|
-
}
|
|
5213
|
-
/**
|
|
5214
|
-
* Get the number of registered tags.
|
|
5215
|
-
*
|
|
5216
|
-
* @returns Number of tags in the registry
|
|
5217
|
-
*/
|
|
5218
|
-
get size() {
|
|
5219
|
-
return this.#tagsByValue.size;
|
|
5220
|
-
}
|
|
5221
|
-
/**
|
|
5222
5193
|
* Create a string key for a numeric tag value.
|
|
5223
5194
|
* Handles both number and bigint types.
|
|
5224
5195
|
*
|
|
@@ -5610,11 +5581,17 @@ function formatMap(map, opts) {
|
|
|
5610
5581
|
}
|
|
5611
5582
|
/**
|
|
5612
5583
|
* Format tagged value.
|
|
5584
|
+
*
|
|
5585
|
+
* Matches Rust's diag_item() for Tagged case.
|
|
5613
5586
|
*/
|
|
5614
5587
|
function formatTagged(tag, content, opts) {
|
|
5615
5588
|
if (opts.summarize === true) {
|
|
5616
5589
|
const summarizer = resolveTagsStore(opts.tags)?.summarizer(tag);
|
|
5617
|
-
if (summarizer !== void 0)
|
|
5590
|
+
if (summarizer !== void 0) {
|
|
5591
|
+
const result$1 = summarizer(content, opts.flat ?? false);
|
|
5592
|
+
if (result$1.ok) return result$1.value;
|
|
5593
|
+
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}>`;
|
|
5594
|
+
}
|
|
5618
5595
|
}
|
|
5619
5596
|
let comment;
|
|
5620
5597
|
if (opts.annotate === true) {
|
|
@@ -6899,21 +6876,20 @@ const asKeyValue = (element) => {
|
|
|
6899
6876
|
* @param cbor - The CBOR value to traverse
|
|
6900
6877
|
* @param initialState - Initial state value
|
|
6901
6878
|
* @param visitor - Function to call for each element
|
|
6902
|
-
* @returns Final state after traversal
|
|
6903
6879
|
*
|
|
6904
6880
|
* @example
|
|
6905
6881
|
* ```typescript
|
|
6906
|
-
* // Count all text strings in a structure
|
|
6907
|
-
*
|
|
6882
|
+
* // Count all text strings in a structure using RefCell-like pattern
|
|
6883
|
+
* const count = { value: 0 };
|
|
6908
6884
|
*
|
|
6909
6885
|
* const structure = cbor({ name: 'Alice', tags: ['urgent', 'draft'] });
|
|
6910
|
-
*
|
|
6886
|
+
* walk(structure, null, (element, level, edge, state) => {
|
|
6911
6887
|
* if (element.type === 'single' && element.cbor.type === MajorType.Text) {
|
|
6912
|
-
*
|
|
6888
|
+
* count.value++;
|
|
6913
6889
|
* }
|
|
6914
6890
|
* return [state, false];
|
|
6915
6891
|
* });
|
|
6916
|
-
* console.log(
|
|
6892
|
+
* console.log(count.value); // 3 (name, urgent, draft)
|
|
6917
6893
|
* ```
|
|
6918
6894
|
*
|
|
6919
6895
|
* @example
|
|
@@ -6922,7 +6898,7 @@ const asKeyValue = (element) => {
|
|
|
6922
6898
|
* const structure = cbor([1, 2, 3, 'found', 5, 6]);
|
|
6923
6899
|
* let found = false;
|
|
6924
6900
|
*
|
|
6925
|
-
* walk(structure, null, (element, level, edge) => {
|
|
6901
|
+
* walk(structure, null, (element, level, edge, state) => {
|
|
6926
6902
|
* if (element.type === 'single' &&
|
|
6927
6903
|
* element.cbor.type === MajorType.Text &&
|
|
6928
6904
|
* element.cbor.value === 'found') {
|
|
@@ -6934,7 +6910,7 @@ const asKeyValue = (element) => {
|
|
|
6934
6910
|
* ```
|
|
6935
6911
|
*/
|
|
6936
6912
|
const walk = (cbor$1, initialState, visitor) => {
|
|
6937
|
-
|
|
6913
|
+
walkInternal(cbor$1, 0, { type: EdgeType.None }, initialState, visitor);
|
|
6938
6914
|
};
|
|
6939
6915
|
/**
|
|
6940
6916
|
* Internal recursive walk implementation.
|
|
@@ -7022,125 +6998,6 @@ function walkMap(cbor$1, level, state, visitor) {
|
|
|
7022
6998
|
function walkTagged(cbor$1, level, state, visitor) {
|
|
7023
6999
|
return walkInternal(cbor$1.value, level + 1, { type: EdgeType.TaggedContent }, state, visitor);
|
|
7024
7000
|
}
|
|
7025
|
-
/**
|
|
7026
|
-
* Helper: Count all elements in a CBOR tree.
|
|
7027
|
-
*
|
|
7028
|
-
* @param cbor - The CBOR value to count
|
|
7029
|
-
* @returns Total number of elements visited
|
|
7030
|
-
*
|
|
7031
|
-
* @example
|
|
7032
|
-
* ```typescript
|
|
7033
|
-
* const structure = cbor([1, 2, [3, 4]]);
|
|
7034
|
-
* const count = countElements(structure);
|
|
7035
|
-
* console.log(count); // 6 (array, 1, 2, inner array, 3, 4)
|
|
7036
|
-
* ```
|
|
7037
|
-
*/
|
|
7038
|
-
const countElements = (cbor$1) => {
|
|
7039
|
-
return walk(cbor$1, { count: 0 }, (_element, _level, _edge, state) => {
|
|
7040
|
-
return [{ count: state.count + 1 }, false];
|
|
7041
|
-
}).count;
|
|
7042
|
-
};
|
|
7043
|
-
/**
|
|
7044
|
-
* Helper: Collect all elements at a specific depth level.
|
|
7045
|
-
*
|
|
7046
|
-
* @param cbor - The CBOR value to traverse
|
|
7047
|
-
* @param targetLevel - The depth level to collect (0 = root)
|
|
7048
|
-
* @returns Array of CBOR values at the target level
|
|
7049
|
-
*
|
|
7050
|
-
* @example
|
|
7051
|
-
* ```typescript
|
|
7052
|
-
* const structure = cbor([[1, 2], [3, 4]]);
|
|
7053
|
-
* const level1 = collectAtLevel(structure, 1);
|
|
7054
|
-
* // Returns: [[1, 2], [3, 4]]
|
|
7055
|
-
* const level2 = collectAtLevel(structure, 2);
|
|
7056
|
-
* // Returns: [1, 2, 3, 4]
|
|
7057
|
-
* ```
|
|
7058
|
-
*/
|
|
7059
|
-
const collectAtLevel = (cbor$1, targetLevel) => {
|
|
7060
|
-
return walk(cbor$1, { items: [] }, (element, level, _edge, state) => {
|
|
7061
|
-
if (level === targetLevel && element.type === "single") return [{ items: [...state.items, element.cbor] }, false];
|
|
7062
|
-
return [state, false];
|
|
7063
|
-
}).items;
|
|
7064
|
-
};
|
|
7065
|
-
/**
|
|
7066
|
-
* Helper: Find first element matching a predicate.
|
|
7067
|
-
*
|
|
7068
|
-
* @template T - Type of extracted value
|
|
7069
|
-
* @param cbor - The CBOR value to search
|
|
7070
|
-
* @param predicate - Function to test each element
|
|
7071
|
-
* @returns First matching element, or undefined if not found
|
|
7072
|
-
*
|
|
7073
|
-
* @example
|
|
7074
|
-
* ```typescript
|
|
7075
|
-
* const structure = cbor({ users: [
|
|
7076
|
-
* { name: 'Alice', age: 30 },
|
|
7077
|
-
* { name: 'Bob', age: 25 }
|
|
7078
|
-
* ]});
|
|
7079
|
-
*
|
|
7080
|
-
* const bob = findFirst(structure, (element) => {
|
|
7081
|
-
* if (element.type === 'single' &&
|
|
7082
|
-
* element.cbor.type === MajorType.Text &&
|
|
7083
|
-
* element.cbor.value === 'Bob') {
|
|
7084
|
-
* return true;
|
|
7085
|
-
* }
|
|
7086
|
-
* return false;
|
|
7087
|
-
* });
|
|
7088
|
-
* ```
|
|
7089
|
-
*/
|
|
7090
|
-
const findFirst = (cbor$1, predicate) => {
|
|
7091
|
-
return walk(cbor$1, {}, (element, _level, _edge, state) => {
|
|
7092
|
-
if (state.found !== void 0) return [state, true];
|
|
7093
|
-
if (predicate(element)) {
|
|
7094
|
-
if (element.type === "single") return [{ found: element.cbor }, true];
|
|
7095
|
-
return [state, true];
|
|
7096
|
-
}
|
|
7097
|
-
return [state, false];
|
|
7098
|
-
}).found;
|
|
7099
|
-
};
|
|
7100
|
-
/**
|
|
7101
|
-
* Helper: Collect all text strings in a CBOR tree.
|
|
7102
|
-
*
|
|
7103
|
-
* @param cbor - The CBOR value to traverse
|
|
7104
|
-
* @returns Array of all text string values found
|
|
7105
|
-
*
|
|
7106
|
-
* @example
|
|
7107
|
-
* ```typescript
|
|
7108
|
-
* const doc = cbor({
|
|
7109
|
-
* title: 'Document',
|
|
7110
|
-
* tags: ['urgent', 'draft'],
|
|
7111
|
-
* author: { name: 'Alice' }
|
|
7112
|
-
* });
|
|
7113
|
-
*
|
|
7114
|
-
* const texts = collectAllText(doc);
|
|
7115
|
-
* // Returns: ['Document', 'urgent', 'draft', 'Alice']
|
|
7116
|
-
* ```
|
|
7117
|
-
*/
|
|
7118
|
-
const collectAllText = (cbor$1) => {
|
|
7119
|
-
return walk(cbor$1, { texts: [] }, (element, _level, _edge, state) => {
|
|
7120
|
-
if (element.type === "single" && element.cbor.type === MajorType.Text) return [{ texts: [...state.texts, element.cbor.value] }, false];
|
|
7121
|
-
return [state, false];
|
|
7122
|
-
}).texts;
|
|
7123
|
-
};
|
|
7124
|
-
/**
|
|
7125
|
-
* Helper: Get the maximum depth of a CBOR tree.
|
|
7126
|
-
*
|
|
7127
|
-
* @param cbor - The CBOR value to measure
|
|
7128
|
-
* @returns Maximum depth (0 for leaf values, 1+ for containers)
|
|
7129
|
-
*
|
|
7130
|
-
* @example
|
|
7131
|
-
* ```typescript
|
|
7132
|
-
* const flat = cbor([1, 2, 3]);
|
|
7133
|
-
* console.log(maxDepth(flat)); // 1
|
|
7134
|
-
*
|
|
7135
|
-
* const nested = cbor([[[1]]]);
|
|
7136
|
-
* console.log(maxDepth(nested)); // 3
|
|
7137
|
-
* ```
|
|
7138
|
-
*/
|
|
7139
|
-
const maxDepth = (cbor$1) => {
|
|
7140
|
-
return walk(cbor$1, { maxDepth: 0 }, (_element, level, _edge, state) => {
|
|
7141
|
-
return [{ maxDepth: Math.max(state.maxDepth, level) }, false];
|
|
7142
|
-
}).maxDepth;
|
|
7143
|
-
};
|
|
7144
7001
|
|
|
7145
7002
|
//#endregion
|
|
7146
7003
|
//#region src/cbor.ts
|
|
@@ -7555,7 +7412,7 @@ const attachMethods = (obj) => {
|
|
|
7555
7412
|
return this.value;
|
|
7556
7413
|
},
|
|
7557
7414
|
walk(initialState, visitor) {
|
|
7558
|
-
|
|
7415
|
+
walk(this, initialState, visitor);
|
|
7559
7416
|
},
|
|
7560
7417
|
validateTag(expectedTags) {
|
|
7561
7418
|
if (this.type !== MajorType.Tagged) throw new CborError({ type: "WrongType" });
|
|
@@ -8293,9 +8150,18 @@ const registerTagsIn = (tagsStore) => {
|
|
|
8293
8150
|
tagsStore.insertAll(tags);
|
|
8294
8151
|
tagsStore.setSummarizer(TAG_DATE, (untaggedCbor, _flat) => {
|
|
8295
8152
|
try {
|
|
8296
|
-
return
|
|
8297
|
-
|
|
8298
|
-
|
|
8153
|
+
return {
|
|
8154
|
+
ok: true,
|
|
8155
|
+
value: CborDate.fromUntaggedCbor(untaggedCbor).toString()
|
|
8156
|
+
};
|
|
8157
|
+
} catch (e) {
|
|
8158
|
+
return {
|
|
8159
|
+
ok: false,
|
|
8160
|
+
error: {
|
|
8161
|
+
type: "Custom",
|
|
8162
|
+
message: e instanceof Error ? e.message : String(e)
|
|
8163
|
+
}
|
|
8164
|
+
};
|
|
8299
8165
|
}
|
|
8300
8166
|
});
|
|
8301
8167
|
};
|
|
@@ -9079,9 +8945,6 @@ exports.asUnsigned = asUnsigned;
|
|
|
9079
8945
|
exports.bytesToHex = bytesToHex;
|
|
9080
8946
|
exports.cbor = cbor;
|
|
9081
8947
|
exports.cborData = cborData;
|
|
9082
|
-
exports.collectAllText = collectAllText;
|
|
9083
|
-
exports.collectAtLevel = collectAtLevel;
|
|
9084
|
-
exports.countElements = countElements;
|
|
9085
8948
|
exports.createTag = createTag;
|
|
9086
8949
|
exports.createTaggedCbor = createTaggedCbor;
|
|
9087
8950
|
exports.decodeCbor = decodeCbor;
|
|
@@ -9105,7 +8968,6 @@ exports.expectText = expectText;
|
|
|
9105
8968
|
exports.expectUnsigned = expectUnsigned;
|
|
9106
8969
|
exports.extractCbor = extractCbor;
|
|
9107
8970
|
exports.extractTaggedContent = extractTaggedContent;
|
|
9108
|
-
exports.findFirst = findFirst;
|
|
9109
8971
|
exports.getGlobalTagsStore = getGlobalTagsStore;
|
|
9110
8972
|
exports.getTaggedContent = getTaggedContent;
|
|
9111
8973
|
exports.hasFractionalPart = hasFractionalPart;
|
|
@@ -9132,7 +8994,6 @@ exports.mapKeys = mapKeys;
|
|
|
9132
8994
|
exports.mapSize = mapSize;
|
|
9133
8995
|
exports.mapValue = mapValue;
|
|
9134
8996
|
exports.mapValues = mapValues;
|
|
9135
|
-
exports.maxDepth = maxDepth;
|
|
9136
8997
|
exports.registerTags = registerTags;
|
|
9137
8998
|
exports.registerTagsIn = registerTagsIn;
|
|
9138
8999
|
exports.simpleName = simpleName;
|
|
@@ -9148,4 +9009,5 @@ exports.tryIntoBool = expectBoolean;
|
|
|
9148
9009
|
exports.tryIntoByteString = expectBytes;
|
|
9149
9010
|
exports.tryIntoText = expectText;
|
|
9150
9011
|
exports.validateTag = validateTag;
|
|
9012
|
+
exports.walk = walk;
|
|
9151
9013
|
//# sourceMappingURL=index.cjs.map
|