initial commit/backup

This commit is contained in:
2024-11-01 20:55:18 -04:00
commit bc53ce53b1
39 changed files with 10456 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
// read input from stdin (from main process) and process it
process.stdin.on('data', async (data) => {
try {
// parse the request data (assumed to be in JSON format)
const requestData = JSON.parse(data.toString());
// create a new Request object using the parsed request data
const request = new Request(requestData.url, {
method: requestData.method || 'GET',
headers: requestData.headers || {},
body: requestData.body ? JSON.stringify(requestData.body) : undefined,
params: requestData.params,
query: requestData.query
})
// perform the fetch using Node.js native Fetch API
const response = await fetch(request);
// read the response as an array buffer
const buffer = await response.arrayBuffer();
// create response object
const responseObject = {
success: true,
headers: Object.fromEntries(Array.from(response.headers)),
mimeType: response.headers.get('Content-Type') || 'application/octet-stream',
data: Buffer.from(buffer).toString('base64'), // encode as base64 for transmission
}
// write the response to stdout
logger.info(JSON.stringify(responseObject));
} catch (error) {
// handle and report any errors
const errorResponse = { success: false, error: error.message }
process.stdout.write(JSON.stringify(errorResponse) + '\n');
}
});
+75
View File
@@ -0,0 +1,75 @@
import { spawn } from 'child_process';
import { findPath } from '../utils/paths.js';
import loggermod from '../utils/logger.cjs';
const { logger } = loggermod;
export default function spawnworker(request, uid) {
return new Promise(async (resolve, reject) => {
const child = spawn('node', [await findPath('worker.js')]);
const requestObject = {
method: request.method,
headers: Object.fromEntries(Array.from(request.headers)),
url: request.url,
body: await request.text(),
params: request.params,
query: request.query,
};
// send the request data to the child process
child.stdin.write(JSON.stringify(requestObject) + '\n');
// Accumulate data from child process stdout
let accumulatedData = '';
// handle response from the child process
child.stdout.on('data', (data) => {
accumulatedData += data.toString();
// Check if accumulatedData contains a complete JSON object
if (accumulatedData.trim().endsWith('}')) {
let response;
try {
response = JSON.parse(accumulatedData);
} catch (err) {
logger.error('Failed to parse response from child process:', err);
child.kill(1);
resolve({ status: 500 });
return;
}
// If JSON is parsed successfully, resolve the promise
try {
// Decode the base64 data back to a buffer
if (response.data) response.data = Buffer.from(response.data, 'base64');
resolve(new Response(response.data || response, {
headers: response.headers,
status: response.status || 200,
}));
} catch (err) {
logger.error('Error creating response:', err);
resolve(response);
}
child.kill(0);
// Reset accumulatedData for the next potential message
accumulatedData = '';
}
});
// handle errors from the child process
child.stderr.on('data', (error) => {
logger.error(`Child process stderr: ${error}`);
resolve({ status: 500 });
});
// handle if the child process exits unexpectedly
child.on('close', (code) => {
// not 0 or null
if (!!code) {
logger.error(`Child process exited with code ${code}`);
resolve({ status: 500 });
}
});
});
}