UNPKG

tream

Version:

Lightweight lazy streams in TypeScript

45 lines 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var url_1 = require("url"); var http_1 = require("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; } function request(method, url, headers, body, need_body, res_fn, err_fn) { var _a = url_1.parse(url), protocol = _a.protocol, hostname = _a.hostname, port = _a.port, path = _a.path; var req = http_1.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(); }; } exports.request = request; //# sourceMappingURL=node.js.map