@proton/ccxt
Version:
A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading library with support for 130+ exchanges
43 lines (40 loc) • 1.32 kB
JavaScript
// ----------------------------------------------------------------------------
// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
// EDIT THE CORRESPONDENT .ts FILE INSTEAD
import * as http from 'http';
import * as https from 'https';
export async function toBuffer(stream) {
let length = 0;
const chunks = [];
for await (const chunk of stream) {
length += chunk.length;
chunks.push(chunk);
}
return Buffer.concat(chunks, length);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function json(stream) {
const buf = await toBuffer(stream);
const str = buf.toString('utf8');
try {
return JSON.parse(str);
}
catch (_err) {
const err = _err;
err.message += ` (input: ${str})`;
throw err;
}
}
export function req(url, opts = {}) {
const href = typeof url === 'string' ? url : url.href;
const req = (href.startsWith('https:') ? https : http).request(url, opts);
const promise = new Promise((resolve, reject) => {
req
.once('response', resolve)
.once('error', reject)
.end();
});
req.then = promise.then.bind(promise);
return req;
}