UNPKG

resedit-cli

Version:

Command-line tool for editing Windows Resource data in executable binaries

44 lines (43 loc) 1.32 kB
import * as log from '../log.js'; const fetchFunction = await (async () => { if (typeof fetch === 'function') { log.debug('[sign] Built-in fetch is found'); return fetch; } try { return (await import('node-fetch')).default; } catch (_a) { return null; } })(); export function isAvailable() { return fetchFunction !== null; } export default function requestSimpleUsingFetch(url, opt, cb) { log.debug('[sign] Use fetch function'); async function inner() { try { const response = await fetchFunction(url, { method: opt.method, headers: opt.headers, body: opt.body, }); const buffer = Buffer.from(await response.arrayBuffer()); const cbHeaders = {}; for (const pair of response.headers.entries()) { cbHeaders[pair[0]] = pair[1]; } if (response.ok) { cb(null, cbHeaders, buffer); } else { cb(new Error(`Server error ${response.status} ${response.statusText}`), cbHeaders, buffer); } } catch (error) { cb(error, {}, Buffer.from('')); } } inner(); }