helpers.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.req = exports.json = exports.toBuffer = void 0;
  27. const http = __importStar(require("http"));
  28. const https = __importStar(require("https"));
  29. async function toBuffer(stream) {
  30. let length = 0;
  31. const chunks = [];
  32. for await (const chunk of stream) {
  33. length += chunk.length;
  34. chunks.push(chunk);
  35. }
  36. return Buffer.concat(chunks, length);
  37. }
  38. exports.toBuffer = toBuffer;
  39. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  40. async function json(stream) {
  41. const buf = await toBuffer(stream);
  42. const str = buf.toString('utf8');
  43. try {
  44. return JSON.parse(str);
  45. }
  46. catch (_err) {
  47. const err = _err;
  48. err.message += ` (input: ${str})`;
  49. throw err;
  50. }
  51. }
  52. exports.json = json;
  53. function req(url, opts = {}) {
  54. const href = typeof url === 'string' ? url : url.href;
  55. const req = (href.startsWith('https:') ? https : http).request(url, opts);
  56. const promise = new Promise((resolve, reject) => {
  57. req
  58. .once('response', resolve)
  59. .once('error', reject)
  60. .end();
  61. });
  62. req.then = promise.then.bind(promise);
  63. return req;
  64. }
  65. exports.req = req;
  66. //# sourceMappingURL=helpers.js.map