@cratestack/cbor-web 0.17.1 → 0.18.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.d.ts CHANGED
@@ -18,4 +18,35 @@ import type { CratestackRpcCodec } from "@cratestack/ts-types";
18
18
  * ```
19
19
  */
20
20
  export declare function createCborCodec(): Promise<CratestackRpcCodec>;
21
+ /** Which side of an op a body belongs to. */
22
+ export type CompactDirection = "request" | "response";
23
+ /**
24
+ * The compact-wire-key tables of one client (`docs/design/compact-wire-keys.md`):
25
+ * swaps schema field names for integer keys on the wire and back. Pure
26
+ * functions of bytes; a bad input throws a catchable `Error`.
27
+ */
28
+ export interface CompactKeys {
29
+ /** Named CBOR bytes to compact CBOR bytes. */
30
+ toCompact(named: Uint8Array, op: string, direction: CompactDirection): Uint8Array;
31
+ /** Compact CBOR bytes back to named CBOR bytes. */
32
+ fromCompact(compact: Uint8Array, op: string, direction: CompactDirection): Uint8Array;
33
+ /** Named JSON text to compact JSON text (keys become decimal strings). */
34
+ toCompactJson(named: string, op: string, direction: CompactDirection): string;
35
+ /** Compact JSON text back to named JSON text. */
36
+ fromCompactJson(compact: string, op: string, direction: CompactDirection): string;
37
+ /** The `Cratestack-Contract` header value of the op's current contract. */
38
+ selector(op: string): string | undefined;
39
+ }
40
+ /**
41
+ * Builds the {@link CompactKeys} of a generated client's key-table artifact
42
+ * (its `KEY_TABLES`). Shares the one-time WASM instantiation with
43
+ * {@link createCborCodec}; the returned calls are synchronous.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const keys = await createCompactKeys(tables);
48
+ * const compact = keys.toCompact(codec.encode(body), "model.User.create", "request");
49
+ * ```
50
+ */
51
+ export declare function createCompactKeys(tables: Uint8Array): Promise<CompactKeys>;
21
52
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AA2B/D;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAqBnE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAgC/D;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAqBnE;AAED,6CAA6C;AAC7C,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,UAAU,CAAC;AAEtD;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,8CAA8C;IAC9C,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,GAAG,UAAU,CAAC;IAClF,mDAAmD;IACnD,WAAW,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,GAAG,UAAU,CAAC;IACtF,0EAA0E;IAC1E,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,GAAG,MAAM,CAAC;IAC9E,iDAAiD;IACjD,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,GAAG,MAAM,CAAC;IAClF,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CAC1C;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC,CAUhF"}
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // (see `pnpm run wasm:build` / `../../examples/scripts/wasm-build.mjs`) — not
3
3
  // hand-written, regenerated on every build, excluded from lint/format (see
4
4
  // root `biome.json`) and from git (see `.gitignore`).
5
- import init, { contentType, decode, encode } from "./wasm-pkg/cratestack_cbor_wasm.js";
5
+ import init, { contentType, decode, encode, CompactKeys as WasmCompactKeys, } from "./wasm-pkg/cratestack_cbor_wasm.js";
6
6
  // Memoized across calls so concurrent/repeated `createCborCodec()` callers
7
7
  // share one WASM instantiation instead of re-fetching/re-compiling the
8
8
  // module. Only a *successful* init is cached — a failed attempt (e.g. the
@@ -60,4 +60,26 @@ export async function createCborCodec() {
60
60
  },
61
61
  };
62
62
  }
63
+ /**
64
+ * Builds the {@link CompactKeys} of a generated client's key-table artifact
65
+ * (its `KEY_TABLES`). Shares the one-time WASM instantiation with
66
+ * {@link createCborCodec}; the returned calls are synchronous.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * const keys = await createCompactKeys(tables);
71
+ * const compact = keys.toCompact(codec.encode(body), "model.User.create", "request");
72
+ * ```
73
+ */
74
+ export async function createCompactKeys(tables) {
75
+ await ensureInitialized();
76
+ const keys = WasmCompactKeys.fromTables(tables);
77
+ return {
78
+ toCompact: (named, op, direction) => keys.toCompact(named, op, direction),
79
+ fromCompact: (compact, op, direction) => keys.fromCompact(compact, op, direction),
80
+ toCompactJson: (named, op, direction) => keys.toCompactJson(named, op, direction),
81
+ fromCompactJson: (compact, op, direction) => keys.fromCompactJson(compact, op, direction),
82
+ selector: (op) => keys.selector(op),
83
+ };
84
+ }
63
85
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,iFAAiF;AACjF,8EAA8E;AAC9E,2EAA2E;AAC3E,sDAAsD;AACtD,OAAO,IAAI,EAAE,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,oCAAoC,CAAC;AAEvF,2EAA2E;AAC3E,uEAAuE;AACvE,0EAA0E;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,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,EAAE,EAAE,SAAS,CAAC;QACjF,eAAe,EAAE,CAAC,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,EAAE,EAAE,SAAS,CAAC;QACzF,QAAQ,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;KACpC,CAAC;AACJ,CAAC"}
@@ -1,5 +1,167 @@
1
1
  /* @ts-self-types="./cratestack_cbor_wasm.d.ts" */
2
2
 
3
+ /**
4
+ * The key tables of one client: swaps field names for integer keys on
5
+ * the wire and back. Build it with `createCompactKeys` in the JS wrapper.
6
+ */
7
+ export class CompactKeys {
8
+ static __wrap(ptr) {
9
+ const obj = Object.create(CompactKeys.prototype);
10
+ obj.__wbg_ptr = ptr;
11
+ CompactKeysFinalization.register(obj, obj.__wbg_ptr, obj);
12
+ return obj;
13
+ }
14
+ __destroy_into_raw() {
15
+ const ptr = this.__wbg_ptr;
16
+ this.__wbg_ptr = 0;
17
+ CompactKeysFinalization.unregister(this);
18
+ return ptr;
19
+ }
20
+ free() {
21
+ const ptr = this.__destroy_into_raw();
22
+ wasm.__wbg_compactkeys_free(ptr, 0);
23
+ }
24
+ /**
25
+ * Compact JSON text back to named JSON text.
26
+ * @param {string} compact
27
+ * @param {string} op
28
+ * @param {string} direction
29
+ * @returns {string}
30
+ */
31
+ fromCompactJson(compact, op, direction) {
32
+ let deferred5_0;
33
+ let deferred5_1;
34
+ try {
35
+ const ptr0 = passStringToWasm0(compact, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
36
+ const len0 = WASM_VECTOR_LEN;
37
+ const ptr1 = passStringToWasm0(op, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
38
+ const len1 = WASM_VECTOR_LEN;
39
+ const ptr2 = passStringToWasm0(direction, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
40
+ const len2 = WASM_VECTOR_LEN;
41
+ const ret = wasm.compactkeys_fromCompactJson(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
42
+ var ptr4 = ret[0];
43
+ var len4 = ret[1];
44
+ if (ret[3]) {
45
+ ptr4 = 0; len4 = 0;
46
+ throw takeFromExternrefTable0(ret[2]);
47
+ }
48
+ deferred5_0 = ptr4;
49
+ deferred5_1 = len4;
50
+ return getStringFromWasm0(ptr4, len4);
51
+ } finally {
52
+ wasm.__wbindgen_free(deferred5_0, deferred5_1, 1);
53
+ }
54
+ }
55
+ /**
56
+ * Compact CBOR bytes back to named CBOR bytes.
57
+ * @param {Uint8Array} compact
58
+ * @param {string} op
59
+ * @param {string} direction
60
+ * @returns {Uint8Array}
61
+ */
62
+ fromCompact(compact, op, direction) {
63
+ const ptr0 = passArray8ToWasm0(compact, wasm.__wbindgen_malloc);
64
+ const len0 = WASM_VECTOR_LEN;
65
+ const ptr1 = passStringToWasm0(op, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
66
+ const len1 = WASM_VECTOR_LEN;
67
+ const ptr2 = passStringToWasm0(direction, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
68
+ const len2 = WASM_VECTOR_LEN;
69
+ const ret = wasm.compactkeys_fromCompact(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
70
+ if (ret[3]) {
71
+ throw takeFromExternrefTable0(ret[2]);
72
+ }
73
+ var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
74
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
75
+ return v4;
76
+ }
77
+ /**
78
+ * Loads the generated key-table artifact.
79
+ * @param {Uint8Array} tables
80
+ * @returns {CompactKeys}
81
+ */
82
+ static fromTables(tables) {
83
+ const ptr0 = passArray8ToWasm0(tables, wasm.__wbindgen_malloc);
84
+ const len0 = WASM_VECTOR_LEN;
85
+ const ret = wasm.compactkeys_fromTables(ptr0, len0);
86
+ if (ret[2]) {
87
+ throw takeFromExternrefTable0(ret[1]);
88
+ }
89
+ return CompactKeys.__wrap(ret[0]);
90
+ }
91
+ /**
92
+ * The `Cratestack-Contract` header value of `op`'s current contract,
93
+ * or `undefined` for an op the tables lack.
94
+ * @param {string} op
95
+ * @returns {string | undefined}
96
+ */
97
+ selector(op) {
98
+ const ptr0 = passStringToWasm0(op, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
99
+ const len0 = WASM_VECTOR_LEN;
100
+ const ret = wasm.compactkeys_selector(this.__wbg_ptr, ptr0, len0);
101
+ let v2;
102
+ if (ret[0] !== 0) {
103
+ v2 = getStringFromWasm0(ret[0], ret[1]);
104
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
105
+ }
106
+ return v2;
107
+ }
108
+ /**
109
+ * Named JSON text to compact JSON text (decimal-string keys).
110
+ * @param {string} named
111
+ * @param {string} op
112
+ * @param {string} direction
113
+ * @returns {string}
114
+ */
115
+ toCompactJson(named, op, direction) {
116
+ let deferred5_0;
117
+ let deferred5_1;
118
+ try {
119
+ const ptr0 = passStringToWasm0(named, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
120
+ const len0 = WASM_VECTOR_LEN;
121
+ const ptr1 = passStringToWasm0(op, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
122
+ const len1 = WASM_VECTOR_LEN;
123
+ const ptr2 = passStringToWasm0(direction, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
124
+ const len2 = WASM_VECTOR_LEN;
125
+ const ret = wasm.compactkeys_toCompactJson(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
126
+ var ptr4 = ret[0];
127
+ var len4 = ret[1];
128
+ if (ret[3]) {
129
+ ptr4 = 0; len4 = 0;
130
+ throw takeFromExternrefTable0(ret[2]);
131
+ }
132
+ deferred5_0 = ptr4;
133
+ deferred5_1 = len4;
134
+ return getStringFromWasm0(ptr4, len4);
135
+ } finally {
136
+ wasm.__wbindgen_free(deferred5_0, deferred5_1, 1);
137
+ }
138
+ }
139
+ /**
140
+ * Named CBOR bytes to compact CBOR bytes. `direction` is
141
+ * `"request"` or `"response"`.
142
+ * @param {Uint8Array} named
143
+ * @param {string} op
144
+ * @param {string} direction
145
+ * @returns {Uint8Array}
146
+ */
147
+ toCompact(named, op, direction) {
148
+ const ptr0 = passArray8ToWasm0(named, wasm.__wbindgen_malloc);
149
+ const len0 = WASM_VECTOR_LEN;
150
+ const ptr1 = passStringToWasm0(op, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
151
+ const len1 = WASM_VECTOR_LEN;
152
+ const ptr2 = passStringToWasm0(direction, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
153
+ const len2 = WASM_VECTOR_LEN;
154
+ const ret = wasm.compactkeys_toCompact(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
155
+ if (ret[3]) {
156
+ throw takeFromExternrefTable0(ret[2]);
157
+ }
158
+ var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
159
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
160
+ return v4;
161
+ }
162
+ }
163
+ if (Symbol.dispose) CompactKeys.prototype[Symbol.dispose] = CompactKeys.prototype.free;
164
+
3
165
  /**
4
166
  * Mirrors `CborCodec::CONTENT_TYPE` ("application/cbor") — kept as a
5
167
  * function rather than a duplicated JS-side string literal so there is
@@ -353,6 +515,10 @@ function __wbg_get_imports() {
353
515
  };
354
516
  }
355
517
 
518
+ const CompactKeysFinalization = (typeof FinalizationRegistry === 'undefined')
519
+ ? { register: () => {}, unregister: () => {} }
520
+ : new FinalizationRegistry(ptr => wasm.__wbg_compactkeys_free(ptr, 1));
521
+
356
522
  function addToExternrefTable0(obj) {
357
523
  const idx = wasm.__externref_table_alloc();
358
524
  wasm.__wbindgen_externrefs.set(idx, obj);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratestack/cbor-web",
3
- "version": "0.17.1",
3
+ "version": "0.18.0",
4
4
  "description": "wasm-bindgen CBOR codec for browser CrateStack RPC clients. Async one-time WASM init, synchronous encode/decode after — backed by the same Rust CborCodec the server and Rust client already use.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -43,7 +43,7 @@
43
43
  "lint": "biome check ."
44
44
  },
45
45
  "dependencies": {
46
- "@cratestack/ts-types": "0.17.1"
46
+ "@cratestack/ts-types": "0.18.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "typescript": "^7.0.2",