garu-ko 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. package/dist/index.js +20 -4
  2. package/package.json +16 -3
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
- const DEFAULT_MODEL_URL = new URL('../models/base.gmdl', import.meta.url).href;
1
+ const isNode = typeof process !== 'undefined' &&
2
+ process.versions != null &&
3
+ process.versions.node != null;
2
4
  const EMPTY_RESULT = Object.freeze({
3
5
  tokens: [],
4
6
  score: 0,
@@ -27,14 +29,28 @@ export class Garu {
27
29
  if (options?.modelData) {
28
30
  modelBytes = new Uint8Array(options.modelData);
29
31
  }
32
+ else if (options?.modelUrl) {
33
+ const response = await fetch(options.modelUrl);
34
+ if (!response.ok) {
35
+ throw new Error(`Failed to fetch model from ${options.modelUrl}: ${response.status} ${response.statusText}`);
36
+ }
37
+ modelBytes = new Uint8Array(await response.arrayBuffer());
38
+ }
39
+ else if (isNode) {
40
+ const { readFile } = await import('fs/promises');
41
+ const { fileURLToPath } = await import('url');
42
+ const { join, dirname } = await import('path');
43
+ const dir = dirname(fileURLToPath(import.meta.url));
44
+ const buf = await readFile(join(dir, '..', 'models', 'base.gmdl'));
45
+ modelBytes = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
46
+ }
30
47
  else {
31
- const url = options?.modelUrl ?? DEFAULT_MODEL_URL;
48
+ const url = new URL('../models/base.gmdl', import.meta.url).href;
32
49
  const response = await fetch(url);
33
50
  if (!response.ok) {
34
51
  throw new Error(`Failed to fetch model from ${url}: ${response.status} ${response.statusText}`);
35
52
  }
36
- const buffer = await response.arrayBuffer();
37
- modelBytes = new Uint8Array(buffer);
53
+ modelBytes = new Uint8Array(await response.arrayBuffer());
38
54
  }
39
55
  // Construct the WASM analyzer
40
56
  const wasmInstance = new wasmModule.GaruWasm(modelBytes);
package/package.json CHANGED
@@ -1,18 +1,30 @@
1
1
  {
2
2
  "name": "garu-ko",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Ultra-lightweight Korean morphological analyzer for the web (2.2MB model, WASM 93KB, F1 95.3%)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
- "files": ["dist", "pkg", "models", "README.md"],
8
+ "files": [
9
+ "dist",
10
+ "pkg",
11
+ "models",
12
+ "README.md"
13
+ ],
9
14
  "scripts": {
10
15
  "build:wasm": "wasm-pack build ../crates/garu-wasm --target web --out-dir ../../js/pkg",
11
16
  "build:js": "tsc",
12
17
  "build": "npm run build:wasm && npm run build:js",
13
18
  "test": "vitest run"
14
19
  },
15
- "keywords": ["korean", "morphological-analysis", "nlp", "wasm", "lightweight", "browser"],
20
+ "keywords": [
21
+ "korean",
22
+ "morphological-analysis",
23
+ "nlp",
24
+ "wasm",
25
+ "lightweight",
26
+ "browser"
27
+ ],
16
28
  "author": "cyj <dydwls140@naver.com>",
17
29
  "repository": {
18
30
  "type": "git",
@@ -20,6 +32,7 @@
20
32
  },
21
33
  "license": "MIT",
22
34
  "devDependencies": {
35
+ "@types/node": "^25.5.0",
23
36
  "typescript": "^5.4",
24
37
  "vitest": "^2.0"
25
38
  }