@meritt/shapeof-wasm 0.0.0-stage → 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/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.1.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.0"
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.15.0",
44
+ "onFail": "download"
45
+ },
46
+ "runtime": {
47
+ "name": "node",
48
+ "version": "^26.13.0",
49
+ "onFail": "error"
50
+ }
51
+ }
6
52
  }
package/shapeof.js ADDED
@@ -0,0 +1,93 @@
1
+ const VERSION = '0.1.0';
2
+ const STATUS = ['ok', 'invalid_utf8', 'invalid_argument', 'internal'];
3
+ const TARGETS = ['openai', 'anthropic', 'plain'];
4
+ const OK = 0;
5
+ // shapeof_report on wasm32: status, then the report as (pointer, length).
6
+ const REPORT_SIZE = 12;
7
+
8
+ export class ShapeOfError extends Error {
9
+ constructor(code) {
10
+ super(code);
11
+ this.name = 'ShapeOfError';
12
+ this.code = code;
13
+ }
14
+ }
15
+
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));
29
+ }
30
+ source = fetch(source);
31
+ }
32
+ if (source instanceof Promise || source instanceof Response) {
33
+ return WebAssembly.compileStreaming(source);
34
+ }
35
+ return WebAssembly.compile(source);
36
+ }
37
+
38
+ const encoder = new TextEncoder();
39
+ const decoder = new TextDecoder('utf-8', { fatal: true });
40
+
41
+ class ShapeOf {
42
+ #x;
43
+
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
+ this.version = VERSION;
49
+ }
50
+
51
+ schema(text, target) {
52
+ const index = TARGETS.indexOf(target);
53
+ 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');
59
+ 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);
63
+ 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));
66
+ const result = JSON.parse(decoder.decode(report));
67
+ x.shapeof_report_free(out);
68
+ return result;
69
+ } 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);
84
+ }
85
+ }
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
+ }
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.