@meritt/shapeof-wasm 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Alexey Simonenko, alexey@simonenko.dev, https://simonenko.xyz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@meritt/shapeof-wasm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Infers JSON Schema 2020-12 from examples of JSON, loose JSON or JSON Lines for structured outputs or validation, in WebAssembly",
5
+ "keywords": [
6
+ "json",
7
+ "json-schema",
8
+ "jsonl",
9
+ "structured-outputs",
10
+ "wasm",
11
+ "webassembly"
12
+ ],
13
+ "license": "MIT",
14
+ "author": {
15
+ "name": "Alexey Simonenko",
16
+ "email": "alexey@simonenko.dev",
17
+ "url": "https://simonenko.xyz"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/meritt/shapeof.rs.git",
22
+ "directory": "packages/npm"
23
+ },
24
+ "type": "module",
25
+ "engines": {
26
+ "node": ">=26.13.1"
27
+ },
28
+ "sideEffects": false,
29
+ "exports": {
30
+ ".": "./shapeof.js",
31
+ "./shapeof.wasm": "./shapeof.wasm"
32
+ },
33
+ "files": [
34
+ "shapeof.js",
35
+ "shapeof.wasm"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "devEngines": {
41
+ "packageManager": {
42
+ "name": "pnpm",
43
+ "version": "12.25.1",
44
+ "onFail": "download"
45
+ },
46
+ "runtime": {
47
+ "name": "node",
48
+ "version": "26.14.0",
49
+ "onFail": "error"
50
+ }
51
+ }
6
52
  }
package/shapeof.js ADDED
@@ -0,0 +1,108 @@
1
+ const VERSION = '0.2.0';
2
+ const TARGETS = ['openai', 'anthropic', 'plain'];
3
+ const STATUS = ['ok', 'invalid_utf8', 'invalid_argument', 'internal'];
4
+ const MAX_INPUT_UNITS = 1_000_000;
5
+ const REPORT_SIZE = 12;
6
+ const encoder = new TextEncoder();
7
+ const decoder = new TextDecoder('utf-8', { fatal: true });
8
+
9
+ class ShapeOfError extends Error {
10
+ constructor(code, at) {
11
+ super(code);
12
+ this.name = 'ShapeOfError';
13
+ this.code = code;
14
+ if (at !== undefined) this.at = at;
15
+ }
16
+ }
17
+
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');
31
+ }
32
+ return exports;
33
+ } catch (error) {
34
+ if (error instanceof RangeError || error instanceof WebAssembly.RuntimeError) throw new ShapeOfError('internal');
35
+ throw new ShapeOfError('version');
36
+ }
37
+ }
38
+
39
+ export async function load(module) {
40
+ if (!(module instanceof WebAssembly.Module)) throw new ShapeOfError('invalid_argument');
41
+ return new ShapeOf(module);
42
+ }
43
+
44
+ class ShapeOf {
45
+ #module;
46
+ #exports;
47
+
48
+ constructor(module) {
49
+ this.#module = module;
50
+ this.#exports = instantiate(module);
51
+ this.version = VERSION;
52
+ }
53
+
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) {
71
+ const index = TARGETS.indexOf(target);
72
+ if (typeof text !== 'string' || index < 0) throw new ShapeOfError('invalid_argument');
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;
80
+ try {
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);
90
+ const status = view.getUint32(0, 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));
93
+ const result = JSON.parse(decoder.decode(report));
94
+ if (typeof result.error === 'string') throw new ShapeOfError(result.error);
95
+ if (result.error) throw new ShapeOfError('syntax', result.error);
96
+ return result;
97
+ } catch (error) {
98
+ if (error instanceof ShapeOfError) throw error;
99
+ throw new ShapeOfError('internal');
100
+ } finally {
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
+ }
106
+ }
107
+ }
108
+ }
package/shapeof.wasm ADDED
Binary file
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.