Files
AzerothAI/scripts/start-local-runtime.mjs
T

29 lines
1.4 KiB
JavaScript

// Run the built worker without Wrangler's development proxy or file watcher.
import './sites-env.mjs';
import {createRequire} from 'node:module';
import path from 'node:path';
import {readdirSync} from 'node:fs';
import {unstable_getMiniflareWorkerOptions} from 'wrangler';
const require = createRequire(import.meta.url);
const wranglerRequire = createRequire(require.resolve('wrangler'));
const {Miniflare} = wranglerRequire('miniflare');
const config = path.resolve('dist/server/wrangler.json');
const {workerOptions, externalWorkers, main} = unstable_getMiniflareWorkerOptions(config);
if (!main) throw new Error('Build the application before starting the local runtime');
const root = path.dirname(main);
const modules = readdirSync(root, {recursive: true})
.filter(file => /\.(m?js)$/.test(file))
.map(file => ({type: 'ESModule', path: path.join(root, file)}));
modules.sort((a, b) => a.path === main ? -1 : b.path === main ? 1 : 0);
const runtime = new Miniflare({
host: process.env.RUNTIME_HOST || (process.argv.includes('--lan') ? '0.0.0.0' : '127.0.0.1'),
port: Number(process.env.RUNTIME_PORT || 8787),
d1Persist: path.resolve('.wrangler/state/v3/d1'),
workers: [{...workerOptions, modules, modulesRoot: root}, ...externalWorkers],
});
console.log(`AzerothAI local runtime ready: ${await runtime.ready}`);
for (const signal of ['SIGINT', 'SIGTERM']) {
process.once(signal, async () => { await runtime.dispose(); process.exit(0); });
}