@temporal-cortex/toon 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/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # @temporal-cortex/toon
2
+
3
+ WASM-powered TOON (Token-Oriented Object Notation) encoder/decoder for Node.js.
4
+
5
+ This package wraps the Rust `temporal-cortex-toon` library via WebAssembly, providing near-native performance for TOON encoding and decoding in JavaScript/TypeScript environments.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @temporal-cortex/toon
11
+ # or
12
+ pnpm add @temporal-cortex/toon
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { encode, decode } from "@temporal-cortex/toon";
19
+
20
+ // JSON string → TOON string
21
+ const toon = encode('{"name":"Alice","scores":[95,87,92]}');
22
+ console.log(toon);
23
+ // name: Alice
24
+ // scores[3]: 95,87,92
25
+
26
+ // TOON string → JSON string (perfect roundtrip)
27
+ const json = decode(toon);
28
+ console.log(json);
29
+ // {"name":"Alice","scores":[95,87,92]}
30
+ ```
31
+
32
+ ## API
33
+
34
+ ### `encode(json: string): string`
35
+
36
+ Converts a valid JSON string into TOON format. Throws if the input is not valid JSON.
37
+
38
+ ### `decode(toon: string): string`
39
+
40
+ Converts a TOON string back into compact JSON. Throws if the input is not valid TOON.
41
+
42
+ ## Build from Source
43
+
44
+ This package requires the WASM artifacts to be built from the Rust crate first:
45
+
46
+ ```bash
47
+ # From the monorepo root:
48
+
49
+ # 1. Build the WASM binary
50
+ cargo build -p temporal-cortex-toon-wasm --target wasm32-unknown-unknown --release
51
+
52
+ # 2. Generate Node.js bindings
53
+ wasm-bindgen --target nodejs \
54
+ --out-dir packages/temporal-cortex-toon-js/wasm/ \
55
+ target/wasm32-unknown-unknown/release/toon_wasm.wasm
56
+
57
+ # 3. Rename for ESM/CJS compatibility
58
+ mv packages/temporal-cortex-toon-js/wasm/toon_wasm.js packages/temporal-cortex-toon-js/wasm/toon_wasm.cjs
59
+
60
+ # 4. Build TypeScript
61
+ pnpm --filter @temporal-cortex/toon build
62
+
63
+ # 5. Run tests
64
+ pnpm --filter @temporal-cortex/toon test
65
+ ```
66
+
67
+ ## Architecture
68
+
69
+ ```
70
+ src/index.ts ← Public API (encode/decode), loads WASM via createRequire
71
+ wasm/toon_wasm.cjs ← wasm-bindgen generated CommonJS bindings
72
+ wasm/toon_wasm.wasm ← Compiled WASM binary from temporal-cortex-toon (Rust)
73
+ wasm/toon_wasm.d.ts ← TypeScript type declarations for WASM exports
74
+ ```
75
+
76
+ The package uses `createRequire(import.meta.url)` to load the CommonJS WASM bindings from an ESM context. This bridges the module system mismatch since `wasm-bindgen --target nodejs` generates CommonJS but the package uses `"type": "module"`.
77
+
78
+ ## Testing
79
+
80
+ 26 tests covering encode, decode, and roundtrip operations:
81
+
82
+ ```bash
83
+ pnpm --filter @temporal-cortex/toon test
84
+ ```
85
+
86
+ ## License
87
+
88
+ MIT OR Apache-2.0
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Encode a JSON string into TOON format.
3
+ * @param json - A valid JSON string
4
+ * @returns The TOON-encoded string
5
+ */
6
+ export declare function encode(json: string): string;
7
+ /**
8
+ * Decode a TOON string back into JSON format.
9
+ * @param toon - A valid TOON string
10
+ * @returns The JSON string
11
+ */
12
+ export declare function decode(toon: string): string;
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ // @temporal-cortex/toon — WASM-powered TOON encoder/decoder for Node.js
2
+ //
3
+ // This module loads the WASM binary compiled from the temporal-cortex-toon-wasm Rust crate.
4
+ // The WASM bindings are generated by wasm-bindgen as CommonJS (.cjs), but this
5
+ // package uses ESM ("type": "module" in package.json). We bridge the two module
6
+ // systems using Node's createRequire().
7
+ //
8
+ // Build chain: temporal-cortex-toon (Rust) → temporal-cortex-toon-wasm (wasm-bindgen) → temporal-cortex-toon-js (this wrapper)
9
+ import { createRequire } from "module";
10
+ const require = createRequire(import.meta.url);
11
+ // Load the WASM-generated Node.js bindings (.cjs for CommonJS/ESM compat)
12
+ const wasm = require("../wasm/toon_wasm.cjs");
13
+ /**
14
+ * Encode a JSON string into TOON format.
15
+ * @param json - A valid JSON string
16
+ * @returns The TOON-encoded string
17
+ */
18
+ export function encode(json) {
19
+ return wasm.encode(json);
20
+ }
21
+ /**
22
+ * Decode a TOON string back into JSON format.
23
+ * @param toon - A valid TOON string
24
+ * @returns The JSON string
25
+ */
26
+ export function decode(toon) {
27
+ return wasm.decode(toon);
28
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@temporal-cortex/toon",
3
+ "version": "0.1.0",
4
+ "description": "TOON (Token-Oriented Object Notation) encoder/decoder — compact JSON for LLMs",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "wasm"
11
+ ],
12
+ "keywords": [
13
+ "toon",
14
+ "json",
15
+ "llm",
16
+ "compression",
17
+ "wasm"
18
+ ],
19
+ "license": "MIT OR Apache-2.0",
20
+ "author": "Billy Lui",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/billylui/temporal-cortex-core.git",
24
+ "directory": "packages/temporal-cortex-toon-js"
25
+ },
26
+ "homepage": "https://github.com/billylui/temporal-cortex-core",
27
+ "bugs": {
28
+ "url": "https://github.com/billylui/temporal-cortex-core/issues"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^20",
32
+ "typescript": "^5",
33
+ "vitest": "^3"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "test": "vitest run",
38
+ "test:watch": "vitest"
39
+ }
40
+ }
@@ -0,0 +1,198 @@
1
+ /* @ts-self-types="./toon_wasm.d.ts" */
2
+
3
+ /**
4
+ * Decode a TOON string back into compact JSON format.
5
+ *
6
+ * Returns the JSON string, or throws a JS error if the input is not valid TOON.
7
+ * @param {string} toon
8
+ * @returns {string}
9
+ */
10
+ function decode(toon) {
11
+ let deferred3_0;
12
+ let deferred3_1;
13
+ try {
14
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
15
+ const ptr0 = passStringToWasm0(toon, wasm.__wbindgen_export, wasm.__wbindgen_export2);
16
+ const len0 = WASM_VECTOR_LEN;
17
+ wasm.decode(retptr, ptr0, len0);
18
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
19
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
20
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
21
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
22
+ var ptr2 = r0;
23
+ var len2 = r1;
24
+ if (r3) {
25
+ ptr2 = 0; len2 = 0;
26
+ throw takeObject(r2);
27
+ }
28
+ deferred3_0 = ptr2;
29
+ deferred3_1 = len2;
30
+ return getStringFromWasm0(ptr2, len2);
31
+ } finally {
32
+ wasm.__wbindgen_add_to_stack_pointer(16);
33
+ wasm.__wbindgen_export3(deferred3_0, deferred3_1, 1);
34
+ }
35
+ }
36
+ exports.decode = decode;
37
+
38
+ /**
39
+ * Encode a JSON string into TOON v3.0 format.
40
+ *
41
+ * Returns the TOON string, or throws a JS error if the input is not valid JSON.
42
+ * @param {string} json
43
+ * @returns {string}
44
+ */
45
+ function encode(json) {
46
+ let deferred3_0;
47
+ let deferred3_1;
48
+ try {
49
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
50
+ const ptr0 = passStringToWasm0(json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
51
+ const len0 = WASM_VECTOR_LEN;
52
+ wasm.encode(retptr, ptr0, len0);
53
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
54
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
55
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
56
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
57
+ var ptr2 = r0;
58
+ var len2 = r1;
59
+ if (r3) {
60
+ ptr2 = 0; len2 = 0;
61
+ throw takeObject(r2);
62
+ }
63
+ deferred3_0 = ptr2;
64
+ deferred3_1 = len2;
65
+ return getStringFromWasm0(ptr2, len2);
66
+ } finally {
67
+ wasm.__wbindgen_add_to_stack_pointer(16);
68
+ wasm.__wbindgen_export3(deferred3_0, deferred3_1, 1);
69
+ }
70
+ }
71
+ exports.encode = encode;
72
+
73
+ function __wbg_get_imports() {
74
+ const import0 = {
75
+ __proto__: null,
76
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
77
+ // Cast intrinsic for `Ref(String) -> Externref`.
78
+ const ret = getStringFromWasm0(arg0, arg1);
79
+ return addHeapObject(ret);
80
+ },
81
+ };
82
+ return {
83
+ __proto__: null,
84
+ "./toon_wasm_bg.js": import0,
85
+ };
86
+ }
87
+
88
+ function addHeapObject(obj) {
89
+ if (heap_next === heap.length) heap.push(heap.length + 1);
90
+ const idx = heap_next;
91
+ heap_next = heap[idx];
92
+
93
+ heap[idx] = obj;
94
+ return idx;
95
+ }
96
+
97
+ function dropObject(idx) {
98
+ if (idx < 132) return;
99
+ heap[idx] = heap_next;
100
+ heap_next = idx;
101
+ }
102
+
103
+ let cachedDataViewMemory0 = null;
104
+ function getDataViewMemory0() {
105
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
106
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
107
+ }
108
+ return cachedDataViewMemory0;
109
+ }
110
+
111
+ function getStringFromWasm0(ptr, len) {
112
+ ptr = ptr >>> 0;
113
+ return decodeText(ptr, len);
114
+ }
115
+
116
+ let cachedUint8ArrayMemory0 = null;
117
+ function getUint8ArrayMemory0() {
118
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
119
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
120
+ }
121
+ return cachedUint8ArrayMemory0;
122
+ }
123
+
124
+ function getObject(idx) { return heap[idx]; }
125
+
126
+ let heap = new Array(128).fill(undefined);
127
+ heap.push(undefined, null, true, false);
128
+
129
+ let heap_next = heap.length;
130
+
131
+ function passStringToWasm0(arg, malloc, realloc) {
132
+ if (realloc === undefined) {
133
+ const buf = cachedTextEncoder.encode(arg);
134
+ const ptr = malloc(buf.length, 1) >>> 0;
135
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
136
+ WASM_VECTOR_LEN = buf.length;
137
+ return ptr;
138
+ }
139
+
140
+ let len = arg.length;
141
+ let ptr = malloc(len, 1) >>> 0;
142
+
143
+ const mem = getUint8ArrayMemory0();
144
+
145
+ let offset = 0;
146
+
147
+ for (; offset < len; offset++) {
148
+ const code = arg.charCodeAt(offset);
149
+ if (code > 0x7F) break;
150
+ mem[ptr + offset] = code;
151
+ }
152
+ if (offset !== len) {
153
+ if (offset !== 0) {
154
+ arg = arg.slice(offset);
155
+ }
156
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
157
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
158
+ const ret = cachedTextEncoder.encodeInto(arg, view);
159
+
160
+ offset += ret.written;
161
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
162
+ }
163
+
164
+ WASM_VECTOR_LEN = offset;
165
+ return ptr;
166
+ }
167
+
168
+ function takeObject(idx) {
169
+ const ret = getObject(idx);
170
+ dropObject(idx);
171
+ return ret;
172
+ }
173
+
174
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
175
+ cachedTextDecoder.decode();
176
+ function decodeText(ptr, len) {
177
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
178
+ }
179
+
180
+ const cachedTextEncoder = new TextEncoder();
181
+
182
+ if (!('encodeInto' in cachedTextEncoder)) {
183
+ cachedTextEncoder.encodeInto = function (arg, view) {
184
+ const buf = cachedTextEncoder.encode(arg);
185
+ view.set(buf);
186
+ return {
187
+ read: arg.length,
188
+ written: buf.length
189
+ };
190
+ };
191
+ }
192
+
193
+ let WASM_VECTOR_LEN = 0;
194
+
195
+ const wasmPath = `${__dirname}/toon_wasm_bg.wasm`;
196
+ const wasmBytes = require('fs').readFileSync(wasmPath);
197
+ const wasmModule = new WebAssembly.Module(wasmBytes);
198
+ const wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
@@ -0,0 +1,16 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Decode a TOON string back into compact JSON format.
6
+ *
7
+ * Returns the JSON string, or throws a JS error if the input is not valid TOON.
8
+ */
9
+ export function decode(toon: string): string;
10
+
11
+ /**
12
+ * Encode a JSON string into TOON v3.0 format.
13
+ *
14
+ * Returns the TOON string, or throws a JS error if the input is not valid JSON.
15
+ */
16
+ export function encode(json: string): string;
Binary file
@@ -0,0 +1,9 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const decode: (a: number, b: number, c: number) => void;
5
+ export const encode: (a: number, b: number, c: number) => void;
6
+ export const __wbindgen_add_to_stack_pointer: (a: number) => number;
7
+ export const __wbindgen_export: (a: number, b: number) => number;
8
+ export const __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
9
+ export const __wbindgen_export3: (a: number, b: number, c: number) => void;