@gukhanmun/stdict-cdb 0.1.0-dev.16 → 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/README.md CHANGED
@@ -1,10 +1,21 @@
1
1
  @gukhanmun/stdict-cdb
2
2
  =====================
3
3
 
4
+ [![JSR][JSR badge]][JSR]
5
+ [![npm][npm badge]][npm]
6
+ [![License: GPL-3.0-only][GPL badge]][GPL]
7
+
4
8
  Standard Korean Language Dictionary (표준국어대사전, 標準國語大辭典) compiled
5
9
  into a Gukhanmun CDB binary and distributed as an npm package. Use this with
6
10
  `@gukhanmun/wasm` or `@gukhanmun/napi` when `ko-KR` readings are needed.
7
11
 
12
+ [JSR badge]: https://jsr.io/badges/@gukhanmun/stdict-cdb
13
+ [JSR]: https://jsr.io/@gukhanmun/stdict-cdb
14
+ [npm badge]: https://img.shields.io/npm/v/@gukhanmun/stdict-cdb?logo=npm
15
+ [npm]: https://www.npmjs.com/package/@gukhanmun/stdict-cdb
16
+ [GPL badge]: https://img.shields.io/npm/l/%40gukhanmun%2Fstdict-cdb
17
+ [GPL]: https://www.gnu.org/licenses/gpl-3.0.html
18
+
8
19
 
9
20
  Installation
10
21
  ------------
@@ -31,11 +42,14 @@ console.log(g.convert("漢字를 한글로")); // "한자를 한글로"
31
42
 
32
43
  `stdictCdb()` returns a `DictionarySource` record (a
33
44
  `{ format: "cdb", bytes: Uint8Array }` object). `stdictCdbBytes()` returns only
34
- the `Uint8Array` when you need the raw bytes. `stdictCdbUrl` is the URL of the
45
+ the `Uint8Array` when you need the raw bytes; pass an explicit `URL` to load a
46
+ relocated copy instead of the bundled one. `stdictCdbUrl` is the URL of the
35
47
  binary inside the package, useful for preloading or caching.
36
48
 
37
- On Node.js the bytes are read with `node:fs/promises`. On all other runtimes
38
- they are fetched with `fetch`.
49
+ `stdictCdbBytes()` picks how to load from the URL scheme, not the runtime: a
50
+ `file:` URL (the usual case for an npm install) is read from disk with
51
+ `node:fs/promises`, while any other scheme (such as the `https:` URL of a Deno
52
+ install from JSR) is fetched with `fetch`.
39
53
 
40
54
 
41
55
  Relation to `@gukhanmun/stdict-fst`
package/dist/index.d.ts CHANGED
@@ -58,11 +58,22 @@ declare const stdictCdbUrl: URL;
58
58
  /**
59
59
  * Loads the bundled Standard Korean Language Dictionary as raw bytes.
60
60
  *
61
- * On Node.js uses `node:fs/promises`; on all other runtimes uses `fetch`.
61
+ * The access strategy is chosen from the URL scheme, not from the host
62
+ * runtime. A `file:` URL is read from disk with `node:fs/promises` (Node.js,
63
+ * Deno, and Bun all provide it, and it is the only option since Node.js's
64
+ * `fetch` rejects `file:` URLs); any other scheme is retrieved with `fetch`.
62
65
  *
66
+ * Branching on the scheme is what makes a JSR install work on Deno: there
67
+ * `import.meta.url` (and therefore {@link stdictCdbUrl}) is an `https:` URL,
68
+ * yet Deno also exposes `process.versions.node`, so a runtime sniff would
69
+ * wrongly take the `node:fs` path and `readFile` would reject the `https:`
70
+ * URL with "The URL must be of scheme file".
71
+ *
72
+ * @param url Location of the CDB binary to read. Defaults to the bundled
73
+ * {@link stdictCdbUrl}; pass another URL to load a relocated copy.
63
74
  * @returns The CDB binary as a `Uint8Array`.
64
75
  */
65
- declare function stdictCdbBytes(): Promise<Uint8Array<ArrayBuffer>>;
76
+ declare function stdictCdbBytes(url?: URL): Promise<Uint8Array<ArrayBuffer>>;
66
77
  /**
67
78
  * Loads the bundled Standard Korean Language Dictionary as a
68
79
  * {@link FileDictionarySource} ready to pass to `load({ dictionaries: [...] })`.
package/dist/index.js CHANGED
@@ -4,16 +4,30 @@ const stdictCdbUrl = new URL("./stdict.cdb", import.meta.url);
4
4
  /**
5
5
  * Loads the bundled Standard Korean Language Dictionary as raw bytes.
6
6
  *
7
- * On Node.js uses `node:fs/promises`; on all other runtimes uses `fetch`.
7
+ * The access strategy is chosen from the URL scheme, not from the host
8
+ * runtime. A `file:` URL is read from disk with `node:fs/promises` (Node.js,
9
+ * Deno, and Bun all provide it, and it is the only option since Node.js's
10
+ * `fetch` rejects `file:` URLs); any other scheme is retrieved with `fetch`.
8
11
  *
12
+ * Branching on the scheme is what makes a JSR install work on Deno: there
13
+ * `import.meta.url` (and therefore {@link stdictCdbUrl}) is an `https:` URL,
14
+ * yet Deno also exposes `process.versions.node`, so a runtime sniff would
15
+ * wrongly take the `node:fs` path and `readFile` would reject the `https:`
16
+ * URL with "The URL must be of scheme file".
17
+ *
18
+ * @param url Location of the CDB binary to read. Defaults to the bundled
19
+ * {@link stdictCdbUrl}; pass another URL to load a relocated copy.
9
20
  * @returns The CDB binary as a `Uint8Array`.
10
21
  */
11
- async function stdictCdbBytes() {
12
- if (typeof globalThis.process?.versions?.node === "string") {
13
- const buf = await (await import("node:fs/promises")).readFile(stdictCdbUrl);
22
+ async function stdictCdbBytes(url = stdictCdbUrl) {
23
+ if (url.protocol === "file:") {
24
+ const buf = await (await import(
25
+ /* webpackIgnore: true */
26
+ "node:fs/promises"
27
+ )).readFile(url);
14
28
  return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
15
29
  }
16
- const response = await fetch(stdictCdbUrl);
30
+ const response = await fetch(url);
17
31
  if (!response.ok) throw new Error(`Failed to fetch stdict CDB: HTTP ${response.status}`);
18
32
  return new Uint8Array(await response.arrayBuffer());
19
33
  }
package/dist/stdict.cdb CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gukhanmun/stdict-cdb",
3
- "version": "0.1.0-dev.16",
3
+ "version": "0.1.0",
4
4
  "description": "Standard Korean Language Dictionary bundled as a CDB binary for @gukhanmun/wasm and @gukhanmun/napi.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -46,7 +46,7 @@
46
46
  "@gukhanmun/types": "*"
47
47
  },
48
48
  "devDependencies": {
49
- "@gukhanmun/types": "0.1.0-dev.16+395401fa8b3429703127c66d1db02c435606d87b"
49
+ "@gukhanmun/types": "0.1.0"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsdown && node -e \"require('node:fs').copyFileSync('stdict.cdb','dist/stdict.cdb')\""