guessit-js 4.7.1 → 4.9.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
@@ -5,17 +5,18 @@ Extract metadata (title, year, season, episode, codec, language, etc.) from medi
5
5
  [![npm version](https://img.shields.io/npm/v/guessit-js.svg)](https://www.npmjs.com/package/guessit-js)
6
6
  [![Tests](https://github.com/opensubtitles/guessit-js/actions/workflows/ci.yml/badge.svg)](https://github.com/opensubtitles/guessit-js/actions/workflows/ci.yml)
7
7
  [![License: LGPL-3.0](https://img.shields.io/badge/License-LGPL--3.0-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)
8
- [![Python guessit compat](https://img.shields.io/badge/guessit_compat-100%25_(1342%2F1342)-brightgreen)](https://github.com/guessit-io/guessit)
8
+ [![Python corpus](https://img.shields.io/badge/python_corpus-2191%2F2229-brightgreen)](https://github.com/guessit-io/guessit)
9
9
  [![Coverage](https://img.shields.io/badge/coverage-97%25-brightgreen)](https://github.com/opensubtitles/guessit-js/actions/workflows/ci.yml)
10
10
 
11
11
  **[Live Demo](https://opensubtitles.github.io/guessit-js)** · **[API Docs](https://opensubtitles.github.io/guessit-js/docs)** · **[npm](https://www.npmjs.com/package/guessit-js)**
12
12
 
13
13
  ## Features
14
14
 
15
- - **100% compatibility** with Python guessit 4.4.0 (1342/1342 fixtures passing, including Python's own grown 4.x corpus) — and **more correct**: ships fixes for 32 historical upstream bugs, Python's still-open #875 and #877, and 112 of the 276 cross-parser cases Python admits failing (see [Differences from Python](#differences-from-python-guessit))
16
- - **3.5x faster** than Python (6.87ms vs 23.86ms per parse)
15
+ - **Runs 2191 of the 2229 cases in Python guessit's own test suite** (upstream 4.5.1, its corpus *and* its per-rule files) — and is **more correct**: fixes for 32 historical upstream bugs, Python's still-open #877, and 114 of the 276 cross-parser cases Python admits failing (see [Differences from Python](#differences-from-python-guessit))
16
+ - **~2x faster** than Python per parse ([measured](#performance))
17
17
  - **50 properties** detected: title, year, season, episode, resolution, codec, language, and more — with a [machine-readable schema](#output-schema)
18
- - **Single dependency** ([rebulk-js](https://www.npmjs.com/package/rebulk-js))
18
+ - **Batch parsing**: `guessitMany()` in-process, or one POST for up to 100 names
19
+ - **Zero runtime dependencies** ([rebulk-js](https://www.npmjs.com/package/rebulk-js) is bundled at build time)
19
20
  - **Dual format**: ESM and CommonJS
20
21
  - **TypeScript**: full, precise type definitions (typed `GuessItResult`, enum'd value fields)
21
22
  - **WASM**: runs in any WASI-compatible runtime, **bit-identical to the JS build**
@@ -85,6 +86,28 @@ guessit('X2.2003.720p.DSNP.WEB-DL.DDP5.1.H.264-EVO.mkv');
85
86
  // streaming_service: 'Disney+', source: 'Web', ... }
86
87
  ```
87
88
 
89
+ ### Batch
90
+
91
+ `guessitMany()` parses a list in one pass. `guessit()` re-parses the options and
92
+ re-merges them with the whole config on every call — more work than matching a
93
+ short name — so hoisting that out of the loop is the point. Each entry carries
94
+ the `input_string` plus either a `result` or the `error` that name produced, so
95
+ one unparseable name cannot abort the rest, and entries can be correlated without
96
+ relying on array position.
97
+
98
+ ```typescript
99
+ import { guessitMany } from 'guessit-js';
100
+
101
+ guessitMany(['A.2020.1080p.mkv', 'B.S01E02.mkv'], { type: 'episode' });
102
+ // [
103
+ // { input_string: 'A.2020.1080p.mkv', result: { title: 'A', year: 2020, ... } },
104
+ // { input_string: 'B.S01E02.mkv', result: { title: 'B', season: 1, ... } }
105
+ // ]
106
+ ```
107
+
108
+ In-process there is no cap — pass the whole library. The 100-name cap applies
109
+ only to the HTTP surfaces, where it bounds one request's shape.
110
+
88
111
  ### CommonJS
89
112
 
90
113
  ```javascript
@@ -144,7 +167,25 @@ Regenerate the schema (after parsing changes) with `npm run schema`. A test (`te
144
167
 
145
168
  ## REST API
146
169
 
147
- No install needed beyond the package itself:
170
+ **Hosted demo (testing only):**
171
+
172
+ ```bash
173
+ curl "https://guessit.opensubtitles.com/api/guessit?filename=Movie.2024.1080p.mkv"
174
+ curl -X POST https://guessit.opensubtitles.com/api/guessit \
175
+ -H 'Content-Type: application/json' \
176
+ -d '{"filenames": ["A.2020.mkv", "B.S01E02.mkv"]}'
177
+ ```
178
+
179
+ > ⚠️ **For testing and evaluation only — never use in production.** It runs on
180
+ > a free Cloudflare Workers tier with a hard daily request cap and no uptime
181
+ > guarantee; it can be throttled, rate-limited, or removed at any time. For
182
+ > production, self-host (`npx guessit-js --serve`, one command) or better,
183
+ > call the library in-process (~1.5 ms per parse, no HTTP at all).
184
+
185
+ Runs on Cloudflare Workers at the edge, CORS enabled, batch up to 100
186
+ filenames per POST, redeployed automatically with every release.
187
+
188
+ **Self-hosted** — no install needed beyond the package itself:
148
189
 
149
190
  ```bash
150
191
  npx guessit-js --serve # port 3847 (or --serve 8080 / PORT env)
@@ -158,6 +199,20 @@ CLI parsing flags become server defaults: `guessit-js --serve -t episode -L en`.
158
199
  The repo also ships a fuller dev server (`npm start`) with the demo page,
159
200
  Swagger UI at `/docs`, and static WASM serving.
160
201
 
202
+ **Batch contract**, identical on all three HTTP surfaces (hosted worker,
203
+ `--serve`, `npm start`):
204
+
205
+ | | |
206
+ |---|---|
207
+ | Request | `POST /api/guessit` with `{"filenames": [...], "options": {...}}` |
208
+ | Response | one entry per name, in request order, each `{input_string, result}` or `{input_string, error}` |
209
+ | Limits | 100 names per request, 1024 characters per name, 1 MiB body |
210
+ | Errors | `400` for a malformed body, `413` for any limit exceeded |
211
+
212
+ A name that fails to parse becomes an `error` entry — the request still answers
213
+ `200`. The caps bound the shape of one request, not throughput: for 10k names,
214
+ send 100 requests, or skip HTTP and call `guessitMany()` in-process.
215
+
161
216
  ## WASM
162
217
 
163
218
  For non-JS environments (Rust, Go, C++, edge compute). Uses [Javy](https://github.com/bytecodealliance/javy) (QuickJS → WASM).
@@ -172,22 +227,37 @@ The WASM build is **bit-identical to the JS build** across the entire test corpu
172
227
 
173
228
  ## Differences from Python guessit
174
229
 
175
- guessit-js is a faithful port (1342/1342 fixtures, tracking Python 4.4.0), but it is
176
- **not bug-for-bug identical** — where Python has a genuine parsing bug, guessit-js
177
- is corrected. Highlights:
178
-
179
- - **32 upstream guessit bugs fixed** that Python still gets wrong — e.g. `Us.2019`
180
- (title vs country `US`), `The.Collector` (title vs edition), `X2.2003…` (short
181
- title), `grown-ish…[eztv]` (hyphenated title split), `cd`-matching mid-hash, and
182
- source/codec/extension tokens leaking into `release_group`/`title`. Full ledger:
230
+ guessit-js is a faithful port (tracking Python 4.5.1), but it is **not
231
+ bug-for-bug identical** — where Python has a genuine parsing bug, guessit-js is
232
+ corrected. Run against a live Python guessit 4.5.1 over all 2490 corpus inputs,
233
+ **2247 (90.2%) come back identical**; every remaining difference is listed, with a
234
+ verdict, in [`docs/python-parity.md`](docs/python-parity.md). Highlights:
235
+
236
+ - **Cases the released Python 4.5.1 still gets wrong** (each verified against the
237
+ real Python CLI; the [demo](https://opensubtitles.github.io/guessit-js) shows
238
+ live side-by-sides): `X2.2003…` (Python: `bonus: 2`, no title), Python's
239
+ [#875](https://github.com/guessit-io/guessit/issues/875) (fixed upstream in
240
+ 4.5.1, years after it was fixed here) and still-open
241
+ [#877](https://github.com/guessit-io/guessit/issues/877) (`Season 3 - 11`,
242
+ anchored weak episodes, `0x539` ids), Python's dead
243
+ `RemoveLessSpecificSeasonEpisode("episode")` pass
244
+ ([#961](https://github.com/guessit-io/guessit/issues/961) — diagnosed from this
245
+ port in [#962](https://github.com/guessit-io/guessit/pull/962), fixed upstream
246
+ by rebulk 6.0.2 plus [#982](https://github.com/guessit-io/guessit/pull/982),
247
+ which carries this port's specificity guard), and **114 of the 276
248
+ cross-parser cases Python's own test data admits failing** — fullwidth CJK
249
+ bracket groups, UFC event numbers, anime compound dash titles, fully-bracketed
250
+ titles, and more.
251
+ - **32 historical upstream bugs** were fixed here ahead of upstream during the
252
+ 4.x catch-up (many since adopted by Python 4.4 — e.g. `Us.2019`,
253
+ `The.Collector`, `grown-ish`, `imdb_id`/`tmdb_id`, `volume`, artwork and
254
+ month-date detection are now correct in both). Full ledger with dispositions:
183
255
  [`docs/upstream-issues.md`](docs/upstream-issues.md).
184
- - **More properties / better detection:** `imdb_id`/`tmdb_id`/`tvdb_id`, `volume`,
185
- archive & image **containers** (`.rar`/`.7z`/`.jpg`…), artwork classification
186
- (`poster`/`fanart` → `other`), VR / Opening-Ending credits, month-name dates,
187
- CJK season/episode markers, and detection of Telugu/Spanish that Python misses.
188
- - **Typed, schema-described output:** a precise `GuessItResult` interface, a
189
- complete `properties()` (Python's is partial), and a JSON Schema.
190
- - **No Python runtime:** zero runtime dependencies, ESM + CJS + WASM, ~3.5x faster.
256
+ - **Typed, schema-described output:** a precise `GuessItResult` interface with
257
+ enum'd value fields, `properties()` mirroring Python's API, and a JSON Schema
258
+ (draft-07) of the output — none of which Python provides as machine-readable
259
+ artifacts.
260
+ - **No Python runtime:** zero runtime dependencies, ESM + CJS + WASM, ~2x faster.
191
261
  - **Intentional divergences** (cases where guessit-js is *more* correct than
192
262
  Python) are catalogued per-example in [`docs/python-parity.md`](docs/python-parity.md).
193
263
 
@@ -203,19 +273,24 @@ is corrected. Highlights:
203
273
  ([#690](https://github.com/guessit-io/guessit/issues/690)/[#696](https://github.com/guessit-io/guessit/issues/696)/[#747](https://github.com/guessit-io/guessit/issues/747)):
204
274
  e.g. `Re ZERO …- Season 2 - 15`, romaji + `(English title)` — no unambiguous
205
275
  correct parse.
206
- - **12 debatable cases** vs Python (neither clearly right) are listed under
207
- "② NEUTRAL" in [`docs/python-parity.md`](docs/python-parity.md).
276
+ - **Differences that are nobody's bug** — a bare `2012` read as a title here and a
277
+ year there, Python's 23 allowed languages against this port's 60 — are listed
278
+ with a verdict each in [`docs/python-parity.md`](docs/python-parity.md), and the
279
+ 38 upstream cases this port does not satisfy yet are parked in
280
+ `test/fixtures/upstream-rules-pending.yml`.
208
281
 
209
282
  ## Performance
210
283
 
211
- Warm per-parse, measured on one machine (absolute numbers are hardware-dependent —
212
- the [live demo](https://opensubtitles.github.io/guessit-js) times it in your browser):
284
+ Warm per-parse. The Node and Python numbers below come from one run of the same
285
+ 400 corpus names through each parser on the same machine (Apple silicon, Node 20,
286
+ Python 3.11, guessit 4.5.1), best of three passes after a warm-up — absolute
287
+ numbers are hardware-dependent, and the [live
288
+ demo](https://opensubtitles.github.io/guessit-js) times it in your browser:
213
289
 
214
290
  | Runtime | ms/parse | Notes |
215
291
  |---------|----------|-------|
216
- | **Browser (V8)** | ~2–3 ms | fastest; JIT-compiled |
217
- | **Node.js 22 (V8)** | ~4.4 ms | **~3.5× faster than Python** (same machine) — recommended for servers/CLI |
218
- | **Python 3.8** | ~15.5 ms | reference |
292
+ | **Node.js 20 (V8)** | ~1.7 ms | **~2× faster than Python guessit** on identical input — recommended for servers/CLI |
293
+ | **Python guessit 4.5.1** | ~3.5 ms | reference, same 400 names |
219
294
  | **WASM (QuickJS/Javy)** | ~35 ms warm · ~150 ms cold | for portability, **not** speed (see below) |
220
295
 
221
296
  **About the WASM build.** It exists so you can run guessit in environments without
package/bin/cli.mjs CHANGED
@@ -8,6 +8,18 @@ import { homedir } from 'os';
8
8
  import { join } from 'path';
9
9
  import { guessit, properties as apiProperties, version } from '../dist/guessit-js.js';
10
10
 
11
+ // Parsing cost grows with the square of the number of matches a name yields, so
12
+ // a long synthetic string costs far more than its length suggests: 400
13
+ // repetitions of "S01E01" take seconds where a real name takes under 3 ms. The
14
+ // whole fixture corpus tops out at 196 characters, so these bounds only ever
15
+ // reject something that was never a filename. --serve is the only path that
16
+ // takes untrusted input; parsing local arguments stays unbounded.
17
+ const MAX_FILENAME_LENGTH = 1024;
18
+ // 100 names is the documented batch contract across every HTTP surface. It is a
19
+ // request-shape bound, not a throughput one: a caller with 10k names sends 100
20
+ // requests, or skips HTTP and calls guessitMany() in-process, which is unbounded.
21
+ const MAX_BATCH = 100;
22
+
11
23
  // Python's GuessitEncoder emits babelfish `.name` in JSON ("English",
12
24
  // "UNITED STATES") while YAML uses str() ("en", "pt-BR", "US"). Mirror both.
13
25
  const ALPHA3_TO_2 = {"eng":"en","fra":"fr","deu":"de","spa":"es","ita":"it","por":"pt","rus":"ru","jpn":"ja","zho":"zh","kor":"ko","ara":"ar","hin":"hi","tur":"tr","pol":"pl","nld":"nl","swe":"sv","dan":"da","nor":"no","fin":"fi","hun":"hu","ces":"cs","ron":"ro","ukr":"uk","heb":"he","cat":"ca","vie":"vi","tha":"th","ind":"id","mal":"ml","tel":"te","tam":"ta","bul":"bg","hrv":"hr","srp":"sr","slk":"sk","slv":"sl","ell":"el","lit":"lt","lav":"lv","est":"et","glg":"gl","eus":"eu","ben":"bn","isl":"is","mkd":"mk","bos":"bs","alb":"sq","per":"fa","msa":"ms","mon":"mn","urd":"ur","pan":"pa","guj":"gu","kan":"kn","mar":"mr","asm":"as","mya":"my","khm":"km","lao":"lo"};
@@ -383,6 +395,10 @@ if (serve) {
383
395
  });
384
396
  return;
385
397
  }
398
+ if (filename.length > MAX_FILENAME_LENGTH) {
399
+ sendJson(res, 413, { error: `filename exceeds ${MAX_FILENAME_LENGTH} characters` });
400
+ return;
401
+ }
386
402
  const opts = { ...baseOptions };
387
403
  const type = url.searchParams.get('type');
388
404
  if (type === 'movie' || type === 'episode') opts.type = type;
@@ -399,8 +415,21 @@ if (serve) {
399
415
  const body = JSON.parse(Buffer.concat(chunks).toString('utf8'));
400
416
  const opts = { ...baseOptions, ...(body.options || {}) };
401
417
  if (Array.isArray(body.filenames)) {
402
- sendJson(res, 200, body.filenames.map((f) => display(guessit(String(f), opts), 'json')));
418
+ if (body.filenames.length > MAX_BATCH) {
419
+ sendJson(res, 413, { error: `Max ${MAX_BATCH} filenames per request` });
420
+ return;
421
+ }
422
+ const names = body.filenames.map(String);
423
+ if (names.some((f) => f.length > MAX_FILENAME_LENGTH)) {
424
+ sendJson(res, 413, { error: `filename exceeds ${MAX_FILENAME_LENGTH} characters` });
425
+ return;
426
+ }
427
+ sendJson(res, 200, names.map((f) => display(guessit(f, opts), 'json')));
403
428
  } else if (typeof body.filename === 'string') {
429
+ if (body.filename.length > MAX_FILENAME_LENGTH) {
430
+ sendJson(res, 413, { error: `filename exceeds ${MAX_FILENAME_LENGTH} characters` });
431
+ return;
432
+ }
404
433
  sendJson(res, 200, display(guessit(body.filename, opts), 'json'));
405
434
  } else {
406
435
  sendJson(res, 400, { error: 'Body must contain "filename" (string) or "filenames" (array)' });
package/dist/api.d.ts CHANGED
@@ -10,6 +10,19 @@ export declare class GuessItException extends Error {
10
10
  options: Record<string, unknown>;
11
11
  constructor(string: string, options: Record<string, unknown>, cause?: unknown);
12
12
  }
13
+ /**
14
+ * One entry of a `guessitMany` batch. A string that parses yields `result`; a
15
+ * string that throws yields `error` instead, so a single bad name cannot abort
16
+ * the rest of the batch. `input_string` is always the string as it was passed in,
17
+ * which lets callers correlate entries without relying on array position.
18
+ */
19
+ export type GuessItBatchEntry = {
20
+ input_string: string;
21
+ result: GuessItResult;
22
+ } | {
23
+ input_string: string;
24
+ error: string;
25
+ };
13
26
  export declare class GuessItApi {
14
27
  private _rebulk;
15
28
  private _config;
@@ -18,6 +31,18 @@ export declare class GuessItApi {
18
31
  reset(): void;
19
32
  configure(options?: RawOptions, rulesBuilder?: (config: AdvancedConfig) => Rebulk, force?: boolean, sanitizeOptions?: boolean): Record<string, unknown>;
20
33
  guessit(string: string, options?: RawOptions): GuessItResult;
34
+ /**
35
+ * Parse many strings in one pass.
36
+ *
37
+ * `guessit()` re-parses the options and re-merges them with the whole config on
38
+ * every call — work that costs more than matching a short name does. Hoisting it
39
+ * out of the loop is the reason this method exists; the rules never write to the
40
+ * merged options, so one object is safe to share across the batch.
41
+ */
42
+ guessitMany(strings: string[], options?: RawOptions): GuessItBatchEntry[];
43
+ /** Options → config-merged options, ready to hand to rebulk as its context. */
44
+ private _prepare;
45
+ private _guess;
21
46
  properties(options?: RawOptions): Record<string, unknown[]>;
22
47
  suggestedExpected(titles: string[], options?: RawOptions): string[];
23
48
  }
@@ -27,6 +52,11 @@ export declare const defaultApi: GuessItApi;
27
52
  * Guess metadata from a filename or release name.
28
53
  */
29
54
  export declare function guessit(string: string, options?: RawOptions): GuessItResult;
55
+ /**
56
+ * Guess metadata for many filenames or release names with one options pass.
57
+ * Each entry carries either a `result` or the `error` that string produced.
58
+ */
59
+ export declare function guessitMany(strings: string[], options?: RawOptions): GuessItBatchEntry[];
30
60
  /**
31
61
  * Get all properties that can be detected.
32
62
  */