@opensubtitles/guessit-wasm 0.0.0-stage → 5.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 CHANGED
@@ -1,3 +1,44 @@
1
- # Temporary Holding Version
1
+ # @opensubtitles/guessit-wasm
2
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.
3
+ [guessit-js](https://github.com/opensubtitles/guessit-js) compiled to one standalone
4
+ WebAssembly module (QuickJS via [Javy](https://github.com/bytecodealliance/javy)).
5
+ It has no JS runtime dependency and runs in any WASI runtime.
6
+
7
+ > Using JavaScript/TypeScript? Prefer the [`guessit-js`](https://www.npmjs.com/package/guessit-js)
8
+ > package — it's smaller and faster. This package exists for WASI hosts
9
+ > (wasmtime, wasmer, Go/Rust/Python embedders, edge runtimes).
10
+
11
+ The version always matches the `guessit-js` release it was built from. The same
12
+ binaries are attached to each [GitHub release](https://github.com/opensubtitles/guessit-js/releases).
13
+
14
+ ## Contract
15
+
16
+ `guessit.wasm` is a WASI command. Write JSON to stdin and read one JSON object from stdout:
17
+
18
+ ```sh
19
+ echo '{"filename":"The.Matrix.1999.1080p.BluRay.x264.mkv"}' | wasmtime node_modules/@opensubtitles/guessit-wasm/guessit.wasm
20
+ # {"title":"The Matrix","year":1999,"screen_size":"1080p","source":"Blu-ray",...}
21
+ ```
22
+
23
+ The input can include `"options"` (the same options as `guessit-js`, e.g. `{"type":"episode"}`).
24
+ If it fails, the output is `{"error":"..."}`.
25
+
26
+ ## Node helper
27
+
28
+ ```js
29
+ import { guessit, wasmPath } from '@opensubtitles/guessit-wasm';
30
+
31
+ const result = await guessit('Show.S01E02.720p.mkv');
32
+ // { title: 'Show', season: 1, episode: 2, screen_size: '720p', ... }
33
+ ```
34
+
35
+ `guessit()` instantiates the module once per call through `node:wasi`. Compilation is cached.
36
+
37
+ ## Files
38
+
39
+ | File | |
40
+ |---|---|
41
+ | `guessit.wasm` | the module (~1.8 MB) |
42
+ | `guessit.wasm.gz` | gzip -9 of the same (~750 KB) |
43
+
44
+ License: LGPL-3.0, same as guessit-js.
package/guessit.wasm ADDED
Binary file
Binary file
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /** Absolute path to guessit.wasm (a WASI command: JSON on stdin, JSON on stdout). */
2
+ export declare const wasmPath: string;
3
+
4
+ /** Parse a media filename with the WASM build of guessit-js (Node >= 18, node:wasi). */
5
+ export declare function guessit(
6
+ filename: string,
7
+ options?: Record<string, unknown>,
8
+ ): Promise<Record<string, unknown>>;
9
+
10
+ export default guessit;
package/index.js ADDED
@@ -0,0 +1,58 @@
1
+ // Node helper for the guessit WASI module. The .wasm is a WASI command built
2
+ // with Javy: it reads {"filename", "options"} JSON on stdin and writes the
3
+ // result JSON on stdout. Any other WASI runtime (wasmtime, wasmer, ...) can run
4
+ // `wasmPath` directly; this file only wraps that contract for Node.
5
+ import { fileURLToPath } from 'node:url';
6
+
7
+ export const wasmPath = fileURLToPath(new URL('./guessit.wasm', import.meta.url));
8
+
9
+ let modulePromise;
10
+
11
+ function loadModule() {
12
+ if (!modulePromise) {
13
+ modulePromise = (async () => {
14
+ const { readFile } = await import('node:fs/promises');
15
+ return WebAssembly.compile(await readFile(wasmPath));
16
+ })();
17
+ }
18
+ return modulePromise;
19
+ }
20
+
21
+ /**
22
+ * Parse a media filename with the WASM build of guessit-js.
23
+ * @param {string} filename
24
+ * @param {Record<string, unknown>} [options]
25
+ * @returns {Promise<Record<string, unknown>>}
26
+ */
27
+ export async function guessit(filename, options = {}) {
28
+ if (typeof filename !== 'string' || !filename) throw new TypeError('filename must be a non-empty string');
29
+ const [{ WASI }, fs, os, path] = await Promise.all([
30
+ import('node:wasi'), import('node:fs'), import('node:os'), import('node:path'),
31
+ ]);
32
+ const module = await loadModule();
33
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'guessit-wasm-'));
34
+ const inPath = path.join(dir, 'in.json');
35
+ const outPath = path.join(dir, 'out.json');
36
+ fs.writeFileSync(inPath, JSON.stringify({ filename, options }));
37
+ const stdin = fs.openSync(inPath, 'r');
38
+ const stdout = fs.openSync(outPath, 'w');
39
+ try {
40
+ const wasi = new WASI({ version: 'preview1', args: [], env: {}, stdin, stdout, stderr: 2, returnOnExit: true });
41
+ const instance = await WebAssembly.instantiate(module, wasi.getImportObject());
42
+ wasi.start(instance);
43
+ } finally {
44
+ fs.closeSync(stdin);
45
+ fs.closeSync(stdout);
46
+ }
47
+ try {
48
+ const result = JSON.parse(fs.readFileSync(outPath, 'utf8'));
49
+ if (result && typeof result === 'object' && 'error' in result && Object.keys(result).length === 1) {
50
+ throw new Error(`guessit-wasm: ${result.error}`);
51
+ }
52
+ return result;
53
+ } finally {
54
+ fs.rmSync(dir, { recursive: true, force: true });
55
+ }
56
+ }
57
+
58
+ export default guessit;
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@opensubtitles/guessit-wasm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "5.1.0",
4
+ "description": "guessit-js compiled to a standalone WASI WebAssembly module (Javy/QuickJS) — parse media filenames from any WASI runtime",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "types": "./index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ },
13
+ "./guessit.wasm": "./guessit.wasm",
14
+ "./guessit.wasm.gz": "./guessit.wasm.gz",
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "index.js",
19
+ "index.d.ts",
20
+ "guessit.wasm",
21
+ "guessit.wasm.gz",
22
+ "README.md"
23
+ ],
24
+ "engines": {
25
+ "node": ">=18"
26
+ },
27
+ "keywords": [
28
+ "guessit",
29
+ "wasm",
30
+ "wasi",
31
+ "webassembly",
32
+ "filename",
33
+ "parser",
34
+ "media",
35
+ "metadata"
36
+ ],
37
+ "license": "LGPL-3.0",
38
+ "homepage": "https://github.com/opensubtitles/guessit-js/tree/main/packages/guessit-wasm#readme",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/opensubtitles/guessit-js.git",
42
+ "directory": "packages/guessit-wasm"
43
+ },
44
+ "bugs": {
45
+ "url": "https://github.com/opensubtitles/guessit-js/issues"
46
+ }
47
+ }