UNPKG

tream

Version:

Lightweight lazy streams in TypeScript

42 lines 1.39 kB
import { parse } from 'url'; import { request as node_request } from 'http'; function parseHeaders(h) { var hs = {}; if (h) { for (var f in h) { var d = h[f]; if (d) { var k = f.replace(/(:?^|\-)\w/g, function (c) { return c.toUpperCase(); }); var v = typeof d == 'string' ? d : d.join('; '); hs[k] = v; } } } return hs; } export function request(method, url, headers, body, need_body, res_fn, err_fn) { var _a = parse(url), protocol = _a.protocol, hostname = _a.hostname, port = _a.port, path = _a.path; var req = node_request({ method: method, protocol: protocol, hostname: hostname, port: port, path: path, headers: headers }, function (res) { if (need_body) { var bufs_1 = []; res.on('data', function (buf) { bufs_1.push(buf); }); res.on('end', function () { res_fn(res.statusCode, res.statusMessage, parseHeaders(res.headers), Buffer.concat(bufs_1)); }); } else { res_fn(res.statusCode, res.statusMessage, parseHeaders(res.headers)); } }); req.on('error', err_fn); if (body) { req.write(body); } req.end(); return function () { req.abort(); }; } //# sourceMappingURL=node.js.map