wickra-shazam-wasm 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,62 @@
1
+ # Wickra Shazam — WASM
2
+
3
+ WASM bindings for the `wickra-shazam` data-driven core, compiled to WebAssembly
4
+ with wasm-bindgen. Build a `Shazam` from a spec JSON, drive it with command JSON,
5
+ read back match reports — the same protocol as every other binding, running in
6
+ the browser.
7
+
8
+ The core is built with `--no-default-features`, so indexing and matching run
9
+ **sequentially** (no rayon thread pool in the browser sandbox) and are
10
+ byte-identical to the native parallel path.
11
+
12
+ ## Build
13
+
14
+ ```bash
15
+ wasm-pack build --target web
16
+ ```
17
+
18
+ This emits `pkg/` with the `.wasm` module and JS glue.
19
+
20
+ ## Usage
21
+
22
+ ```js
23
+ import init, { Shazam, version } from "./pkg/wickra_shazam_wasm.js";
24
+
25
+ await init();
26
+
27
+ const spec = JSON.stringify({
28
+ features: [{ kind: "price", field: "close" }],
29
+ window: 1,
30
+ metric: "euclid",
31
+ });
32
+
33
+ const shazam = new Shazam(spec);
34
+
35
+ const candle = (time, close) => ({
36
+ time, open: close, high: close, low: close, close, volume: 1.0,
37
+ });
38
+
39
+ const history = Array.from({ length: 10 }, (_, i) => candle(i + 1, 100 + i + 1));
40
+ shazam.command(JSON.stringify({ cmd: "index", history }));
41
+
42
+ const report = JSON.parse(shazam.command(JSON.stringify({
43
+ cmd: "match",
44
+ current: [candle(11, 110.0)],
45
+ k: 3,
46
+ })));
47
+
48
+ console.log(report.matches.map((m) => m.ts));
49
+ console.log(version());
50
+ ```
51
+
52
+ ## API
53
+
54
+ | Member | Description |
55
+ |--------|-------------|
56
+ | `new Shazam(specJson)` | Build a shazam from a spec JSON (throws on an invalid spec). |
57
+ | `shazam.command(cmdJson)` | Apply a command JSON, return the response JSON. |
58
+ | `shazam.version()` / `version()` | The library version. |
59
+
60
+ ## License
61
+
62
+ `MIT OR Apache-2.0`.
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "wickra-shazam-wasm",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "wickra-lib"
6
+ ],
7
+ "description": "WebAssembly bindings for wickra-shazam — the data-driven history-fingerprint match core in the browser.",
8
+ "version": "0.1.0",
9
+ "license": "MIT OR Apache-2.0",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/wickra-lib/wickra-shazam"
13
+ },
14
+ "files": [
15
+ "wickra_shazam_wasm_bg.wasm",
16
+ "wickra_shazam_wasm.js",
17
+ "wickra_shazam_wasm_bg.js",
18
+ "wickra_shazam_wasm.d.ts"
19
+ ],
20
+ "main": "wickra_shazam_wasm.js",
21
+ "homepage": "https://github.com/wickra-lib/wickra-shazam",
22
+ "types": "wickra_shazam_wasm.d.ts",
23
+ "sideEffects": [
24
+ "./wickra_shazam_wasm.js",
25
+ "./snippets/*"
26
+ ],
27
+ "author": "kingchenc <support@wickra.org>",
28
+ "bugs": {
29
+ "url": "https://github.com/wickra-lib/wickra-shazam/issues"
30
+ }
31
+ }
@@ -0,0 +1,27 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A shazam instance driven by JSON commands.
6
+ */
7
+ export class Shazam {
8
+ free(): void;
9
+ [Symbol.dispose](): void;
10
+ /**
11
+ * Apply a command JSON and return the resulting response JSON.
12
+ */
13
+ command(cmd_json: string): string;
14
+ /**
15
+ * Build a shazam from a spec JSON string.
16
+ */
17
+ constructor(spec_json: string);
18
+ /**
19
+ * The library version.
20
+ */
21
+ version(): string;
22
+ }
23
+
24
+ /**
25
+ * The library version.
26
+ */
27
+ export function version(): string;
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./wickra_shazam_wasm.d.ts" */
2
+ import * as wasm from "./wickra_shazam_wasm_bg.wasm";
3
+ import { __wbg_set_wasm } from "./wickra_shazam_wasm_bg.js";
4
+
5
+ __wbg_set_wasm(wasm);
6
+
7
+ export {
8
+ Shazam, version
9
+ } from "./wickra_shazam_wasm_bg.js";
@@ -0,0 +1,241 @@
1
+ /**
2
+ * A shazam instance driven by JSON commands.
3
+ */
4
+ export class Shazam {
5
+ __destroy_into_raw() {
6
+ const ptr = this.__wbg_ptr;
7
+ this.__wbg_ptr = 0;
8
+ ShazamFinalization.unregister(this);
9
+ return ptr;
10
+ }
11
+ free() {
12
+ const ptr = this.__destroy_into_raw();
13
+ wasm.__wbg_shazam_free(ptr, 0);
14
+ }
15
+ /**
16
+ * Apply a command JSON and return the resulting response JSON.
17
+ * @param {string} cmd_json
18
+ * @returns {string}
19
+ */
20
+ command(cmd_json) {
21
+ let deferred3_0;
22
+ let deferred3_1;
23
+ try {
24
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
25
+ const ptr0 = passStringToWasm0(cmd_json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
26
+ const len0 = WASM_VECTOR_LEN;
27
+ wasm.shazam_command(retptr, this.__wbg_ptr, ptr0, len0);
28
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
29
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
30
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
31
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
32
+ var ptr2 = r0;
33
+ var len2 = r1;
34
+ if (r3) {
35
+ ptr2 = 0; len2 = 0;
36
+ throw takeObject(r2);
37
+ }
38
+ deferred3_0 = ptr2;
39
+ deferred3_1 = len2;
40
+ return getStringFromWasm0(ptr2, len2);
41
+ } finally {
42
+ wasm.__wbindgen_add_to_stack_pointer(16);
43
+ wasm.__wbindgen_export3(deferred3_0, deferred3_1, 1);
44
+ }
45
+ }
46
+ /**
47
+ * Build a shazam from a spec JSON string.
48
+ * @param {string} spec_json
49
+ */
50
+ constructor(spec_json) {
51
+ try {
52
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
53
+ const ptr0 = passStringToWasm0(spec_json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
54
+ const len0 = WASM_VECTOR_LEN;
55
+ wasm.shazam_new(retptr, ptr0, len0);
56
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
57
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
58
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
59
+ if (r2) {
60
+ throw takeObject(r1);
61
+ }
62
+ this.__wbg_ptr = r0;
63
+ ShazamFinalization.register(this, this.__wbg_ptr, this);
64
+ return this;
65
+ } finally {
66
+ wasm.__wbindgen_add_to_stack_pointer(16);
67
+ }
68
+ }
69
+ /**
70
+ * The library version.
71
+ * @returns {string}
72
+ */
73
+ version() {
74
+ let deferred1_0;
75
+ let deferred1_1;
76
+ try {
77
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
78
+ wasm.shazam_version(retptr, this.__wbg_ptr);
79
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
80
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
81
+ deferred1_0 = r0;
82
+ deferred1_1 = r1;
83
+ return getStringFromWasm0(r0, r1);
84
+ } finally {
85
+ wasm.__wbindgen_add_to_stack_pointer(16);
86
+ wasm.__wbindgen_export3(deferred1_0, deferred1_1, 1);
87
+ }
88
+ }
89
+ }
90
+ if (Symbol.dispose) Shazam.prototype[Symbol.dispose] = Shazam.prototype.free;
91
+
92
+ /**
93
+ * The library version.
94
+ * @returns {string}
95
+ */
96
+ export function version() {
97
+ let deferred1_0;
98
+ let deferred1_1;
99
+ try {
100
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
101
+ wasm.version(retptr);
102
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
103
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
104
+ deferred1_0 = r0;
105
+ deferred1_1 = r1;
106
+ return getStringFromWasm0(r0, r1);
107
+ } finally {
108
+ wasm.__wbindgen_add_to_stack_pointer(16);
109
+ wasm.__wbindgen_export3(deferred1_0, deferred1_1, 1);
110
+ }
111
+ }
112
+ export function __wbg_Error_67e7344beaa85059(arg0, arg1) {
113
+ const ret = Error(getStringFromWasm0(arg0, arg1));
114
+ return addHeapObject(ret);
115
+ }
116
+ export function __wbg___wbindgen_throw_5d9e815e6fdf150f(arg0, arg1) {
117
+ throw new Error(getStringFromWasm0(arg0, arg1));
118
+ }
119
+ const ShazamFinalization = (typeof FinalizationRegistry === 'undefined')
120
+ ? { register: () => {}, unregister: () => {} }
121
+ : new FinalizationRegistry(ptr => wasm.__wbg_shazam_free(ptr, 1));
122
+
123
+ function addHeapObject(obj) {
124
+ if (heap_next === heap.length) heap.push(heap.length + 1);
125
+ const idx = heap_next;
126
+ heap_next = heap[idx];
127
+
128
+ heap[idx] = obj;
129
+ return idx;
130
+ }
131
+
132
+ function dropObject(idx) {
133
+ if (idx < 1028) return;
134
+ heap[idx] = heap_next;
135
+ heap_next = idx;
136
+ }
137
+
138
+ let cachedDataViewMemory0 = null;
139
+ function getDataViewMemory0() {
140
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
141
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
142
+ }
143
+ return cachedDataViewMemory0;
144
+ }
145
+
146
+ function getStringFromWasm0(ptr, len) {
147
+ return decodeText(ptr >>> 0, len);
148
+ }
149
+
150
+ let cachedUint8ArrayMemory0 = null;
151
+ function getUint8ArrayMemory0() {
152
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
153
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
154
+ }
155
+ return cachedUint8ArrayMemory0;
156
+ }
157
+
158
+ function getObject(idx) { return heap[idx]; }
159
+
160
+ let heap = new Array(1024).fill(undefined);
161
+ heap.push(undefined, null, true, false);
162
+
163
+ let heap_next = heap.length;
164
+
165
+ function passStringToWasm0(arg, malloc, realloc) {
166
+ if (realloc === undefined) {
167
+ const buf = cachedTextEncoder.encode(arg);
168
+ const ptr = malloc(buf.length, 1) >>> 0;
169
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
170
+ WASM_VECTOR_LEN = buf.length;
171
+ return ptr;
172
+ }
173
+
174
+ let len = arg.length;
175
+ let ptr = malloc(len, 1) >>> 0;
176
+
177
+ const mem = getUint8ArrayMemory0();
178
+
179
+ let offset = 0;
180
+
181
+ for (; offset < len; offset++) {
182
+ const code = arg.charCodeAt(offset);
183
+ if (code > 0x7F) break;
184
+ mem[ptr + offset] = code;
185
+ }
186
+ if (offset !== len) {
187
+ if (offset !== 0) {
188
+ arg = arg.slice(offset);
189
+ }
190
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
191
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
192
+ const ret = cachedTextEncoder.encodeInto(arg, view);
193
+
194
+ offset += ret.written;
195
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
196
+ }
197
+
198
+ WASM_VECTOR_LEN = offset;
199
+ return ptr;
200
+ }
201
+
202
+ function takeObject(idx) {
203
+ const ret = getObject(idx);
204
+ dropObject(idx);
205
+ return ret;
206
+ }
207
+
208
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
209
+ cachedTextDecoder.decode();
210
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
211
+ let numBytesDecoded = 0;
212
+ function decodeText(ptr, len) {
213
+ numBytesDecoded += len;
214
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
215
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
216
+ cachedTextDecoder.decode();
217
+ numBytesDecoded = len;
218
+ }
219
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
220
+ }
221
+
222
+ const cachedTextEncoder = new TextEncoder();
223
+
224
+ if (!('encodeInto' in cachedTextEncoder)) {
225
+ cachedTextEncoder.encodeInto = function (arg, view) {
226
+ const buf = cachedTextEncoder.encode(arg);
227
+ view.set(buf);
228
+ return {
229
+ read: arg.length,
230
+ written: buf.length
231
+ };
232
+ };
233
+ }
234
+
235
+ let WASM_VECTOR_LEN = 0;
236
+
237
+
238
+ let wasm;
239
+ export function __wbg_set_wasm(val) {
240
+ wasm = val;
241
+ }
Binary file