diff --git a/airplay-worker.js b/airplay-worker.js new file mode 100644 index 0000000..219e4df --- /dev/null +++ b/airplay-worker.js @@ -0,0 +1,16 @@ +const { + Worker, + isMainThread, + parentPort, + workerData, +} = require("node:worker_threads"); +var { WebSocketServer } = require("ws"); +const wss = new WebSocketServer({ port: 8980 }); +wss.on("connection", function connection(ws) { + ws.on("message", function message(data) { + parentPort.postMessage({ message: data }); + }); + parentPort.on("message", (data) => { + ws.send(data); + }); +}); diff --git a/izanami.js b/izanami.js index 21a7c51..03fd294 100644 --- a/izanami.js +++ b/izanami.js @@ -57,19 +57,8 @@ airtunes.on('device', function(key, status, desc) { console.log("deviceStatus", key, status, desc); }) -var func = ` - const {Worker, isMainThread, parentPort, workerData} = require('node:worker_threads'); - var { WebSocketServer } = require('ws'); - const wss = new WebSocketServer({ port: 8980 }); - wss.on('connection', function connection(ws) { - ws.on('message', function message(data) { - parentPort.postMessage({message: data}); - }); - parentPort.on("message", data => { - ws.send(data); - }); - });`; -var worker = new Worker(func, {eval: true}); + +var worker = new Worker('./airplay-worker.js'); worker.on("message", (result) => { parsed_data = JSON.parse(ab2str(result.message)); if (parsed_data.type == "scanDevices") { diff --git a/scripts/esbuild.cjs b/scripts/esbuild.cjs index bb286fc..04515e7 100644 --- a/scripts/esbuild.cjs +++ b/scripts/esbuild.cjs @@ -1,15 +1,31 @@ // @ts-check async function main() { + await require("esbuild").build({ + entryPoints: ["./airplay-worker.js"], + bundle: true, + platform: "node", + outdir: "dist", + external: ["./build"], + loader: { + ".node": "file", + }, + // minify: true, + // minifyIdentifiers: true, + sourcemap: true, + }); + await require("esbuild").build({ entryPoints: ["./izanami.js"], bundle: true, platform: "node", outdir: "dist", - external: ['./build'], + external: ["./build"], loader: { - '.node': 'file', + ".node": "file", }, + sourcemap: true, + // minify: true, // minifyIdentifiers: true, });