11import { execFile , type ExecFileException } from "node:child_process" ;
22import { createHash , randomUUID } from "node:crypto" ;
3- import { createReadStream , mkdirSync , renameSync , rmSync , statSync } from "node:fs" ;
3+ import { createReadStream , existsSync , mkdirSync , renameSync , rmSync , statSync } from "node:fs" ;
44import { homedir } from "node:os" ;
55import { join } from "node:path" ;
66import { fileURLToPath } from "node:url" ;
@@ -12,7 +12,6 @@ import {
1212 runNpm ,
1313 sweepStaleStaging ,
1414} from "../utils/optionalPackages.js" ;
15- import { createRenderCancellationScope } from "../utils/renderCancellation.js" ;
1615import {
1716 mergeWindowsToWords ,
1817 SHERPA_ERROR_PREFIX ,
@@ -24,7 +23,11 @@ import { getPreparedWavDurationSeconds, prepareWav, type TranscribeResult } from
2423
2524const RUNTIME = "sherpa-onnx-node" ;
2625const RUNTIME_VERSION = "1.13.8" ;
27- export const SHERPA_RUNTIME_DIR = join ( CACHE_DIR , `${ RUNTIME } @${ RUNTIME_VERSION } ` ) ;
26+ /** Per platform and arch, like the native package inside, so Rosetta never shadows arm64. */
27+ export const SHERPA_RUNTIME_DIR = join (
28+ CACHE_DIR ,
29+ `${ RUNTIME } @${ RUNTIME_VERSION } -${ process . platform } -${ process . arch } ` ,
30+ ) ;
2831
2932const MODEL_REPO = "csukuangfj/sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8" ;
3033const MODEL_REVISION = "2bda32ec70b097a55adaa07d9a7173915b43cc78" ;
@@ -107,23 +110,29 @@ export function sherpaUnsupportedReason(host: Host = currentHost()): string | nu
107110 return `Parakeet needs glibc 2.32 or newer; this system has glibc ${ host . glibc } .` ;
108111}
109112
110- export function sherpaRuntimeInstalled ( ) : boolean {
111- return isInstalled ( SHERPA_RUNTIME_DIR , RUNTIME ) ;
113+ const nativePackageName = ( platform : string , arch : string ) =>
114+ `sherpa-onnx-${ platform === "win32" ? "win" : platform } -${ arch } ` ;
115+
116+ /** The runtime and the native binary its loader requires; either can be missing on its own. */
117+ export function sherpaRuntimeInstalled ( dir = SHERPA_RUNTIME_DIR ) : boolean {
118+ const binary = join ( dir , "node_modules" , nativePackageName ( process . platform , process . arch ) ) ;
119+ return isInstalled ( dir , RUNTIME ) && existsSync ( join ( binary , "sherpa-onnx.node" ) ) ;
112120}
113121
114122/** The native package, pinned too: the runtime's own optionalDependencies accept any 1.13.x. */
115123export function sherpaPlatformPackage ( platform = process . platform , arch = process . arch ) : string {
116- return `sherpa-onnx- ${ platform === "win32" ? "win" : platform } - ${ arch } @${ RUNTIME_VERSION } ` ;
124+ return `${ nativePackageName ( platform , arch ) } @${ RUNTIME_VERSION } ` ;
117125}
118126
119127export function installSherpaRuntime ( {
120128 run = runNpm ,
121129 signal,
122- } : { run ?: typeof runNpm ; signal ?: AbortSignal } = { } ) : Promise < void > {
130+ dir = SHERPA_RUNTIME_DIR ,
131+ } : { run ?: typeof runNpm ; signal ?: AbortSignal ; dir ?: string } = { } ) : Promise < void > {
132+ // install() keeps any dir holding the runtime manifest, so a half-installed one goes first.
133+ if ( ! sherpaRuntimeInstalled ( dir ) ) rmSync ( dir , { recursive : true , force : true } ) ;
123134 const native = sherpaPlatformPackage ( ) ;
124- return install ( SHERPA_RUNTIME_DIR , RUNTIME , RUNTIME_VERSION , ( args ) =>
125- run ( [ ...args , native ] , signal ) ,
126- ) ;
135+ return install ( dir , RUNTIME , RUNTIME_VERSION , ( args ) => run ( [ ...args , native ] , signal ) ) ;
127136}
128137
129138/** Sizes only: hashing 650 MB on every transcribe is too slow, and install already verified them. */
@@ -217,6 +226,8 @@ function recognizerConfig(): object {
217226/** Ctrl-C or a SIGTERM stopped the decode: the user's cancel, never a reason to fall back. */
218227export class DecodeCancelled extends Error { }
219228
229+ const MAX_REASON_CHARS = 600 ;
230+
220231function failureReason ( err : ExecFileException | null , stderr : string ) : string {
221232 const how = ! err
222233 ? "exited without a result"
@@ -232,7 +243,8 @@ function failureReason(err: ExecFileException | null, stderr: string): string {
232243 const why =
233244 lines . find ( ( line ) => line . startsWith ( SHERPA_ERROR_PREFIX ) ) ?. slice ( SHERPA_ERROR_PREFIX . length ) ??
234245 lines . at ( - 1 ) ;
235- return why ? `${ how } : ${ why } ` : how ;
246+ if ( ! why ) return how ;
247+ return `${ how } : ${ why . length > MAX_REASON_CHARS ? `${ why . slice ( 0 , MAX_REASON_CHARS ) } …` : why } ` ;
236248}
237249
238250/** Decodes in a child process: onnxruntime aborts the whole process on some inputs, uncatchably. */
@@ -281,20 +293,13 @@ export function prepareSherpaWav(
281293 }
282294}
283295
296+ /** The caller owns the cancellation scope: it must already cover audio preparation. */
284297export async function transcribeWithSherpa (
285298 wavPath : string ,
286299 dir : string ,
287- options ? : { onProgress ?: ( message : string ) => void } ,
300+ options : { signal : AbortSignal ; onProgress ?: ( message : string ) => void } ,
288301) : Promise < TranscribeResult > {
289- const unsupported = sherpaUnsupportedReason ( ) ;
290- if ( unsupported ) throw new Error ( unsupported ) ;
291- // Aborts on SIGINT/SIGTERM/SIGHUP, which also kills the child if only this process was signalled.
292- const cancellation = createRenderCancellationScope ( ) ;
293- try {
294- options ?. onProgress ?.( "Transcribing with Parakeet..." ) ;
295- const windows = await decode ( wavPath , cancellation . signal ) ;
296- return writeParakeetTranscript ( dir , mergeWindowsToWords ( windows ) ) ;
297- } finally {
298- cancellation . dispose ( ) ;
299- }
302+ options . onProgress ?.( "Transcribing with Parakeet..." ) ;
303+ const windows = await decode ( wavPath , options . signal ) ;
304+ return writeParakeetTranscript ( dir , mergeWindowsToWords ( windows ) ) ;
300305}
0 commit comments