UNPKG

@factorial-finance/blueprint-node

Version:

blueprint-node-plugin

99 lines (98 loc) 3.77 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.compileInWorker = compileInWorker; exports.compileContractsInWorkers = compileContractsInWorkers; const worker_threads_1 = require("worker_threads"); const path = __importStar(require("path")); async function compileInWorker(contractName) { return new Promise((resolve) => { const workerScriptPath = path.join(__dirname, 'compile-worker-script.js'); const worker = new worker_threads_1.Worker(workerScriptPath, { workerData: { contractName, cwd: process.cwd() } }); let resolved = false; worker.on('message', (result) => { if (resolved) return; resolved = true; if (result.success) { resolve({ contractName: result.contractName, hash: result.hash }); } else { if (result.error && !result.error.includes('Missing initializer')) { console.error(`Failed to compile ${result.contractName}: ${result.error}`); } resolve(null); } worker.terminate(); }); worker.on('error', (error) => { if (resolved) return; resolved = true; console.error(`Worker error for ${contractName}:`, error); resolve(null); worker.terminate(); }); // Increase timeout to 60 seconds for compilation setTimeout(() => { if (resolved) return; resolved = true; console.error(`Compilation timeout for ${contractName} (60s)`); worker.terminate(); resolve(null); }, 60000); }); } async function compileContractsInWorkers(contractNames, maxWorkers = 4) { const results = {}; // Process in batches to limit concurrent workers for (let i = 0; i < contractNames.length; i += maxWorkers) { const batch = contractNames.slice(i, i + maxWorkers); const batchPromises = batch.map(name => compileInWorker(name)); const batchResults = await Promise.all(batchPromises); for (const result of batchResults) { if (result) { results[result.contractName] = result.hash; } } } return results; }