site/node_modules/sync-fetch/worker.js
2024-10-14 08:09:33 +02:00

36 lines
842 B
JavaScript

#!/usr/bin/env node
const fetch = require('node-fetch')
const shared = require('./shared')
const chunks = []
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (chunk) {
chunks.push(chunk)
})
process.stdin.on('end', function () {
const input = JSON.parse(chunks.join(''))
const request = shared.deserializeRequest(fetch, ...input)
fetch(request)
.then(response => response.buffer()
.then(buffer => respond([
buffer.toString('base64'),
shared.serializeResponse(response)
]))
.catch(error => respond([
'',
shared.serializeResponse(response),
shared.serializeError(error)
]))
)
.catch(error => respond(shared.serializeError(error)))
})
function respond (message) {
process.stdout.write(JSON.stringify(message))
}