From c639e44ca7a65aeaa77599d37747d097fd5c82c3 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 20 Feb 2025 23:07:45 +0100 Subject: [PATCH 1/2] fix: dont bail when ip cannot be determined (happens when no wifi) --- packages/sampler/sample-server.mjs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/sampler/sample-server.mjs b/packages/sampler/sample-server.mjs index 12520992d..7039a8ce1 100644 --- a/packages/sampler/sample-server.mjs +++ b/packages/sampler/sample-server.mjs @@ -99,12 +99,6 @@ Object.keys(networkInterfaces).forEach((key) => { }); }); -if (!IP) { - console.error("Unable to determine server's IP address."); - // eslint-disable-next-line - process.exit(1); -} - server.listen(PORT, IP_ADDRESS, () => { console.log(`@strudel/sampler is now serving audio files from: ${directory} @@ -113,6 +107,6 @@ To use them in the Strudel REPL, run: samples('http://localhost:${PORT}') Or on a machine in the same network: - samples('http://${IP}:${PORT}') + ${IP ? `samples('http://${IP}:${PORT}')` : `Unable to determine server's IP address.`} `); }); From b016720696db3f62f30da35107d346c07f7a3e56 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 20 Feb 2025 23:37:59 +0100 Subject: [PATCH 2/2] sampler: support serving any file (like strudel.json) --- packages/sampler/sample-server.mjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/sampler/sample-server.mjs b/packages/sampler/sample-server.mjs index 7039a8ce1..d1e56108f 100644 --- a/packages/sampler/sample-server.mjs +++ b/packages/sampler/sample-server.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import cowsay from 'cowsay'; -import { createReadStream } from 'fs'; +import { createReadStream, existsSync } from 'fs'; import { readdir } from 'fs/promises'; import http from 'http'; import { join, sep } from 'path'; @@ -70,12 +70,15 @@ const server = http.createServer(async (req, res) => { return res.end(JSON.stringify(banks)); } let subpath = decodeURIComponent(req.url); - if (!files.includes(subpath)) { + const filePath = join(directory, subpath.split('/').join(sep)); + + //console.log('GET:', filePath); + const isFound = existsSync(filePath); + if (!isFound) { res.statusCode = 404; res.end('File not found'); return; } - const filePath = join(directory, subpath.split('/').join(sep)); const readStream = createReadStream(filePath); readStream.on('error', (err) => { res.statusCode = 500;