@spader/node-whisper-cpp 0.0.7 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ContextOptions, Segment, TranscribeOptions } from "./types.js";
2
2
  export type { ContextOptions, Segment, TranscribeOptions };
3
+ export * as platform from "./platform.js";
3
4
  export declare function version(): string;
4
5
  export declare function systemInfo(): string;
5
6
  export declare function createContext(options: ContextOptions): Context;
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { loadAddon } from "./loader.js";
2
+ export * as platform from "./platform.js";
2
3
  export function version() {
3
4
  return loadAddon().version();
4
5
  }
package/dist/loader.js CHANGED
@@ -1,50 +1,29 @@
1
1
  import { createRequire } from "node:module";
2
- import { platform, arch } from "node:process";
3
2
  import { dirname, join } from "node:path";
4
3
  import { fileURLToPath } from "node:url";
4
+ import { detect } from "./platform.js";
5
5
  const require = createRequire(import.meta.url);
6
6
  const currentDir = dirname(fileURLToPath(import.meta.url));
7
- const platformPackages = {
8
- "darwin-arm64": "@spader/node-whisper-cpp-mac-arm64-metal",
9
- "linux-x64": "@spader/node-whisper-cpp-linux-x64-cpu",
10
- };
11
- const localPlatformDirs = {
12
- "@spader/node-whisper-cpp-mac-arm64-metal": "mac-arm64-metal",
13
- "@spader/node-whisper-cpp-linux-x64-cpu": "linux-x64-cpu",
14
- };
15
- function getPlatformKey() {
16
- return `${platform}-${arch}`;
17
- }
18
- function resolvePlatformPackageName() {
19
- const platformKey = getPlatformKey();
20
- const packageName = platformPackages[platformKey];
21
- if (packageName) {
22
- return packageName;
23
- }
24
- throw new Error(`Unsupported platform ${platformKey}. Supported platforms: ${Object.keys(platformPackages).join(", ")}`);
25
- }
26
- function resolvePlatformPackageDir(packageName) {
7
+ function resolvePlatformPackageDir(name) {
27
8
  try {
28
- const entryPath = require.resolve(packageName);
29
- return join(dirname(entryPath), "..");
9
+ const entry = require.resolve(name);
10
+ return join(dirname(entry), "..");
30
11
  }
31
12
  catch {
32
- const packageDirName = localPlatformDirs[packageName];
33
- if (packageDirName == null) {
34
- throw new Error(`No local package directory mapping for ${packageName}`);
35
- }
36
- return join(currentDir, "..", "packages", "platform", packageDirName);
13
+ const prefix = "@spader/node-whisper-cpp-";
14
+ return join(currentDir, "..", "packages", "platform", name.slice(prefix.length));
37
15
  }
38
16
  }
39
- let addonCache = null;
17
+ let store = {
18
+ addon: null
19
+ };
40
20
  export function loadAddon() {
41
- if (addonCache != null) {
42
- return addonCache;
43
- }
44
- const packageName = resolvePlatformPackageName();
45
- const packageDir = resolvePlatformPackageDir(packageName);
46
- const binsDir = join(packageDir, "bins");
47
- const addonPath = join(binsDir, "whisper-addon.node");
48
- addonCache = require(addonPath);
49
- return addonCache;
21
+ if (store.addon != null)
22
+ return store.addon;
23
+ const triple = detect();
24
+ const name = `@spader/node-whisper-cpp-${triple}`;
25
+ const dir = resolvePlatformPackageDir(name);
26
+ const addonPath = join(dir, "bins", "whisper-addon.node");
27
+ store.addon = require(addonPath);
28
+ return store.addon;
50
29
  }
@@ -0,0 +1,17 @@
1
+ export type Os = "darwin" | "linux";
2
+ export type Architecture = "arm64" | "x64";
3
+ export type Backend = "metal" | "cpu" | "cuda" | "vulkan";
4
+ export type Libc = "gnu" | "musl" | "apple";
5
+ export type Target = {
6
+ os: Os;
7
+ arch: Architecture;
8
+ backend: Backend;
9
+ libc: Libc;
10
+ };
11
+ declare const getPlatformId: (target: Target) => string;
12
+ declare const getPackageName: (target: Target) => string;
13
+ declare function detect(): string;
14
+ declare function detectTarget(): Target;
15
+ declare function resolve(backend?: Backend): string;
16
+ declare function resolveTarget(backend?: Backend): Target;
17
+ export { detect, detectTarget, resolve, resolveTarget, getPlatformId, getPackageName };
@@ -0,0 +1,79 @@
1
+ import { execSync } from "node:child_process";
2
+ import { platform as os, arch } from "node:process";
3
+ function detectLibc() {
4
+ if (os == "darwin") {
5
+ return "apple";
6
+ }
7
+ else if (os == "linux") {
8
+ try {
9
+ const result = execSync("ldd --version 2>&1", { encoding: "utf8" });
10
+ return result.includes("musl") ? "musl" : "gnu";
11
+ }
12
+ catch (e) {
13
+ return e.stdout?.includes("musl") ? "musl" : "gnu";
14
+ }
15
+ }
16
+ throw new Error(`Unsupported OS: ${os}`);
17
+ }
18
+ const VALID_BACKENDS = ["metal", "cpu", "cuda", "vulkan"];
19
+ function detectBackend() {
20
+ const env = process.env.NODE_WHISPER_CPP_BACKEND;
21
+ if (env) {
22
+ if (!VALID_BACKENDS.includes(env)) {
23
+ throw new Error(`Invalid NODE_WHISPER_CPP_BACKEND: ${env} (expected one of ${VALID_BACKENDS.join(", ")})`);
24
+ }
25
+ return env;
26
+ }
27
+ if (os == "darwin") {
28
+ return "metal";
29
+ }
30
+ else if (os == "linux") {
31
+ try {
32
+ execSync("nvidia-smi", { stdio: "ignore" });
33
+ return "cuda";
34
+ }
35
+ catch { }
36
+ try {
37
+ execSync("vulkaninfo", { stdio: "ignore" });
38
+ return "vulkan";
39
+ }
40
+ catch { }
41
+ return "cpu";
42
+ }
43
+ throw new Error(`Unsupported OS: ${os}`);
44
+ }
45
+ const getPlatformId = (target) => {
46
+ const parts = [target.arch, target.os, target.backend];
47
+ if (os === "linux") {
48
+ parts.push(target.libc);
49
+ }
50
+ return parts.join("-");
51
+ };
52
+ const getPackageName = (target) => {
53
+ return `node-whisper-cpp-${getPlatformId(target)}`;
54
+ };
55
+ function detect() {
56
+ const target = detectTarget();
57
+ return getPlatformId(target);
58
+ }
59
+ function detectTarget() {
60
+ if (os !== "linux" && os !== "darwin") {
61
+ throw new Error(`Unsupported OS: ${os}`);
62
+ }
63
+ if (arch !== "x64" && arch !== "arm64") {
64
+ throw new Error(`Unsupported architecture: ${arch}`);
65
+ }
66
+ return { os, arch, backend: detectBackend(), libc: detectLibc() };
67
+ }
68
+ function resolve(backend) {
69
+ const target = resolveTarget(backend);
70
+ return getPlatformId(target);
71
+ }
72
+ function resolveTarget(backend) {
73
+ const detected = detectTarget();
74
+ return {
75
+ ...detected,
76
+ backend: backend ?? detected.backend,
77
+ };
78
+ }
79
+ export { detect, detectTarget, resolve, resolveTarget, getPlatformId, getPackageName };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@spader/node-whisper-cpp",
3
- "version": "0.0.7",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
+ "packageManager": "bun@1.3.9",
5
6
  "main": "./dist/index.js",
6
7
  "types": "./dist/index.d.ts",
7
8
  "exports": {
@@ -16,21 +17,24 @@
16
17
  },
17
18
  "files": [
18
19
  "dist/",
19
- "packages/platform/mac-arm64-metal/",
20
- "packages/platform/linux-x64-cpu/",
20
+ "packages/platform/arm64-darwin-metal/",
21
+ "packages/platform/arm64-darwin-cpu/",
22
+ "packages/platform/x64-linux-cpu-gnu/",
23
+ "packages/platform/x64-linux-cuda-gnu/",
21
24
  "package.json"
22
25
  ],
23
26
  "scripts": {
24
- "build": "bun run ./tools/build.ts system",
25
- "build:mac-arm64": "bun run ./tools/build.ts all --os mac --arch arm64 --backend metal",
26
- "build:linux-x64": "bun run ./tools/build.ts all --os linux --arch x64 --backend cpu",
27
- "ci": "bun run ./tools/ci.ts install",
27
+ "build": "bun run ./tools/build.ts all",
28
+ "smoke": "NODE_WHISPER_CPP_BACKEND=cpu bun run ./tools/build.ts all && bun run test",
29
+ "smoke:cpu": "NODE_WHISPER_CPP_BACKEND=cpu bun run ./tools/build.ts all --ci && bun run test",
28
30
  "test": "bun test ./test/smoke.test.ts",
29
31
  "clean": "bun run ./tools/clean.ts"
30
32
  },
31
33
  "optionalDependencies": {
32
- "@spader/node-whisper-cpp-mac-arm64-metal": "0.0.7",
33
- "@spader/node-whisper-cpp-linux-x64-cpu": "0.0.7"
34
+ "@spader/node-whisper-cpp-arm64-darwin-metal": "0.2.0",
35
+ "@spader/node-whisper-cpp-arm64-darwin-cpu": "0.2.0",
36
+ "@spader/node-whisper-cpp-x64-linux-cpu-gnu": "0.2.0",
37
+ "@spader/node-whisper-cpp-x64-linux-cuda-gnu": "0.2.0"
34
38
  },
35
39
  "repository": {
36
40
  "type": "git",