@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.
- package/package.json +4 -4
- package/shapeof.js +79 -64
- package/shapeof.wasm +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meritt/shapeof-wasm",
|
|
3
|
-
"version": "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.
|
|
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": "
|
|
43
|
+
"version": "12.25.1",
|
|
44
44
|
"onFail": "download"
|
|
45
45
|
},
|
|
46
46
|
"runtime": {
|
|
47
47
|
"name": "node",
|
|
48
|
-
"version": "
|
|
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.
|
|
2
|
-
const STATUS = ['ok', 'invalid_utf8', 'invalid_argument', 'internal'];
|
|
1
|
+
const VERSION = '0.2.0';
|
|
3
2
|
const TARGETS = ['openai', 'anthropic', 'plain'];
|
|
4
|
-
const
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
#
|
|
45
|
+
#module;
|
|
46
|
+
#exports;
|
|
43
47
|
|
|
44
|
-
constructor(
|
|
45
|
-
this.#
|
|
46
|
-
|
|
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
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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 !==
|
|
65
|
-
const report = new Uint8Array(
|
|
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
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|