UNPKG

@nori-zk/mina-token-bridge

Version:

A Mina zk-program contract allowing users to mint tokens on Nori Bridge.

71 lines 2.32 kB
import { fork } from 'child_process'; import { DeferredPromise } from '../index.js'; import path from 'path'; export function getWorkerUrl(url) { let filePath = url.pathname; // Windows fix for leading slash in drive letter paths if (process.platform === 'win32' && filePath.startsWith('/')) { filePath = filePath.slice(1); } // No decodeURIComponent here if you want platform independence const parts = filePath.split(path.sep); // On Unix, splitting absolute path starts with '', so remove it but keep root const isAbsolute = path.isAbsolute(filePath); if (isAbsolute && parts[0] === '') { parts.shift(); } const srcIndex = parts.indexOf('src'); const targetIndex = parts.indexOf('target'); // Insert 'target' before 'src' only if target is missing if (srcIndex !== -1 && targetIndex === -1) { parts.splice(srcIndex, 0, 'target'); } // Fix extension const filename = parts.pop() ?? ''; const baseName = filename.endsWith('.ts') ? filename.slice(0, -3) + '.js' : filename; parts.push(baseName); const root = isAbsolute ? path.parse(filePath).root : ''; return path.join(root, ...parts); } export class WorkerParent { constructor(scriptPath) { this.deferedReady = new DeferredPromise(); const resolvedPath = getWorkerUrl(scriptPath); this.child = fork(resolvedPath, [], { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], }); } async ready() { return this.deferedReady.promise; } async call(data) { await this.ready(); this.child.send?.(data); } onMessageHandler(callback) { this.child.on('message', (msg) => { if (msg === 'ready') { this.deferedReady.resolve(); } else if (typeof msg === 'string') { callback(msg); } else { callback(JSON.stringify(msg)); } }); } onErrorHandler(callback) { this.child.on('error', (error) => { console.log('error', error); callback(error); }); } terminate() { this.child.removeAllListeners(); this.child.kill(); } } //# sourceMappingURL=index.node.js.map