@meritt/shapeof-wasm 0.1.0 → 0.2.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.
Files changed (3) hide show
  1. package/package.json +4 -4
  2. package/shapeof.js +79 -64
  3. package/shapeof.wasm +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meritt/shapeof-wasm",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Infers JSON Schema 2020-12 from examples of JSON, loose JSON or JSON Lines for structured outputs or validation, in WebAssembly",
5
5
  "keywords": [
6
6
  "json",
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "type": "module",
25
25
  "engines": {
26
- "node": ">=26.13.0"
26
+ "node": ">=26.13.1"
27
27
  },
28
28
  "sideEffects": false,
29
29
  "exports": {
@@ -40,12 +40,12 @@
40
40
  "devEngines": {
41
41
  "packageManager": {
42
42
  "name": "pnpm",
43
- "version": "^12.15.0",
43
+ "version": "12.25.1",
44
44
  "onFail": "download"
45
45
  },
46
46
  "runtime": {
47
47
  "name": "node",
48
- "version": "^26.13.0",
48
+ "version": "26.14.0",
49
49
  "onFail": "error"
50
50
  }
51
51
  }
package/shapeof.js CHANGED
@@ -1,93 +1,108 @@
1
- const VERSION = '0.1.0';
2
- const STATUS = ['ok', 'invalid_utf8', 'invalid_argument', 'internal'];
1
+ const VERSION = '0.2.0';
3
2
  const TARGETS = ['openai', 'anthropic', 'plain'];
4
- const OK = 0;
5
- // shapeof_report on wasm32: status, then the report as (pointer, length).
3
+ const STATUS = ['ok', 'invalid_utf8', 'invalid_argument', 'internal'];
4
+ const MAX_INPUT_UNITS = 1_000_000;
6
5
  const REPORT_SIZE = 12;
6
+ const encoder = new TextEncoder();
7
+ const decoder = new TextDecoder('utf-8', { fatal: true });
7
8
 
8
- export class ShapeOfError extends Error {
9
- constructor(code) {
9
+ class ShapeOfError extends Error {
10
+ constructor(code, at) {
10
11
  super(code);
11
12
  this.name = 'ShapeOfError';
12
13
  this.code = code;
14
+ if (at !== undefined) this.at = at;
13
15
  }
14
16
  }
15
17
 
16
- export async function load(source = new URL('./shapeof.wasm', import.meta.url)) {
17
- const module = await compile(source);
18
- const { exports } = await WebAssembly.instantiate(module, {});
19
- return new ShapeOf(exports);
20
- }
21
-
22
- async function compile(source) {
23
- if (source instanceof WebAssembly.Module) return source;
24
- if (typeof source === 'string') source = new URL(source, import.meta.url);
25
- if (source instanceof URL) {
26
- if (source.protocol === 'file:') {
27
- const { readFile } = await import('node:fs/promises');
28
- return WebAssembly.compile(await readFile(source));
18
+ function instantiate(module) {
19
+ try {
20
+ const { exports } = new WebAssembly.Instance(module, {});
21
+ const memory = new Uint8Array(exports.memory.buffer);
22
+ const start = exports.shapeof_version() >>> 0;
23
+ const end = start + VERSION.length;
24
+ if (
25
+ start === 0 || end >= memory.length || memory[end] !== 0 ||
26
+ decoder.decode(memory.subarray(start, end)) !== VERSION ||
27
+ ['shapeof_alloc', 'shapeof_dealloc', 'shapeof_infer', 'shapeof_report_free']
28
+ .some(name => typeof exports[name] !== 'function')
29
+ ) {
30
+ throw new ShapeOfError('version');
29
31
  }
30
- source = fetch(source);
31
- }
32
- if (source instanceof Promise || source instanceof Response) {
33
- return WebAssembly.compileStreaming(source);
32
+ return exports;
33
+ } catch (error) {
34
+ if (error instanceof RangeError || error instanceof WebAssembly.RuntimeError) throw new ShapeOfError('internal');
35
+ throw new ShapeOfError('version');
34
36
  }
35
- return WebAssembly.compile(source);
36
37
  }
37
38
 
38
- const encoder = new TextEncoder();
39
- const decoder = new TextDecoder('utf-8', { fatal: true });
39
+ export async function load(module) {
40
+ if (!(module instanceof WebAssembly.Module)) throw new ShapeOfError('invalid_argument');
41
+ return new ShapeOf(module);
42
+ }
40
43
 
41
44
  class ShapeOf {
42
- #x;
45
+ #module;
46
+ #exports;
43
47
 
44
- constructor(exports) {
45
- this.#x = exports;
46
- const version = typeof exports.shapeof_version === 'function' ? this.#cString(exports.shapeof_version()) : '';
47
- if (version !== VERSION) throw new ShapeOfError('version');
48
+ constructor(module) {
49
+ this.#module = module;
50
+ this.#exports = instantiate(module);
48
51
  this.version = VERSION;
49
52
  }
50
53
 
51
- schema(text, target) {
54
+ #call(name, ...args) {
55
+ try {
56
+ return this.#exports[name](...args);
57
+ } catch {
58
+ this.#exports = null;
59
+ throw new ShapeOfError('internal');
60
+ }
61
+ }
62
+
63
+ #alloc(length) {
64
+ if (length === 0) return 0;
65
+ const pointer = this.#call('shapeof_alloc', length) >>> 0;
66
+ if (pointer === 0) throw new ShapeOfError('internal');
67
+ return pointer;
68
+ }
69
+
70
+ shape(text, target) {
52
71
  const index = TARGETS.indexOf(target);
53
72
  if (typeof text !== 'string' || index < 0) throw new ShapeOfError('invalid_argument');
54
- // A lone surrogate has no UTF-8 form; it reads as U+FFFD, like TextEncoder writes it.
55
- const bytes = encoder.encode(text);
56
- const x = this.#x;
57
- const out = x.shapeof_alloc(REPORT_SIZE);
58
- if (out === 0) throw new ShapeOfError('internal');
73
+ if (text.length > MAX_INPUT_UNITS) throw new ShapeOfError('too_large');
74
+
75
+ this.#exports ??= instantiate(this.#module);
76
+ let bytes;
77
+ let input = 0;
78
+ let out = 0;
79
+ let filled = false;
59
80
  try {
60
- this.#with(bytes, (p, n) => x.shapeof_infer(p, n, index, out));
61
- // Read only after the call: memory may have grown and detached old views.
62
- const view = new DataView(x.memory.buffer, out, REPORT_SIZE);
81
+ bytes = encoder.encode(text);
82
+ input = this.#alloc(bytes.length);
83
+ out = this.#alloc(REPORT_SIZE);
84
+ new Uint8Array(this.#exports.memory.buffer).set(bytes, input);
85
+ this.#call('shapeof_infer', input, bytes.length, index, out);
86
+ filled = true;
87
+
88
+ const memory = this.#exports.memory.buffer;
89
+ const view = new DataView(memory, out, REPORT_SIZE);
63
90
  const status = view.getUint32(0, true);
64
- if (status !== OK) throw new ShapeOfError(STATUS[status] ?? 'internal');
65
- const report = new Uint8Array(x.memory.buffer, view.getUint32(4, true), view.getUint32(8, true));
91
+ if (status !== 0) throw new ShapeOfError(STATUS[status] ?? 'internal');
92
+ const report = new Uint8Array(memory, view.getUint32(4, true), view.getUint32(8, true));
66
93
  const result = JSON.parse(decoder.decode(report));
67
- x.shapeof_report_free(out);
94
+ if (typeof result.error === 'string') throw new ShapeOfError(result.error);
95
+ if (result.error) throw new ShapeOfError('syntax', result.error);
68
96
  return result;
97
+ } catch (error) {
98
+ if (error instanceof ShapeOfError) throw error;
99
+ throw new ShapeOfError('internal');
69
100
  } finally {
70
- x.shapeof_dealloc(out, REPORT_SIZE);
71
- }
72
- }
73
-
74
- #with(bytes, use) {
75
- const x = this.#x;
76
- const n = bytes.length;
77
- const p = x.shapeof_alloc(n);
78
- if (n > 0 && p === 0) throw new ShapeOfError('invalid_argument');
79
- try {
80
- new Uint8Array(x.memory.buffer, p, n).set(bytes);
81
- return use(p, n);
82
- } finally {
83
- x.shapeof_dealloc(p, n);
101
+ if (this.#exports) {
102
+ if (filled) this.#call('shapeof_report_free', out);
103
+ if (out) this.#call('shapeof_dealloc', out, REPORT_SIZE);
104
+ if (input) this.#call('shapeof_dealloc', input, bytes.length);
105
+ }
84
106
  }
85
107
  }
86
-
87
- #cString(p) {
88
- const memory = new Uint8Array(this.#x.memory.buffer);
89
- let end = p;
90
- while (memory[end] !== 0) end++;
91
- return decoder.decode(memory.subarray(p, end));
92
- }
93
108
  }
package/shapeof.wasm CHANGED
Binary file