UNPKG

gitlab-acebase

Version:

AceBase realtime database server (webserver endpoint to allow remote connections)

78 lines 3.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetch = void 0; const https_1 = require("https"); const http_1 = require("http"); /** * Very lightweight custom fetch implementation to avoid additional dependencies * @param url * @param options */ function fetch(url, options) { return new Promise((resolve, reject) => { const method = (options === null || options === void 0 ? void 0 : options.method) || 'GET'; const headers = options === null || options === void 0 ? void 0 : options.headers; const isHttps = url.startsWith('https'); const request = isHttps ? https_1.request : http_1.request; const req = request(url, { method, headers }, res => { const ready = new Promise((resolve, reject) => { const chunks = []; res.on('data', data => { chunks.push(data); }); res.on('end', () => { const buffer = Buffer.concat(chunks); resolve(buffer); }); res.on('error', (err) => { reject(err); }); }); const response = { get status() { return res.statusCode; }, get headers() { return { get(name) { const val = res.headers[name.toLowerCase()]; if (val instanceof Array) { return val.join(', '); } return val; }, }; }, text() { return __awaiter(this, void 0, void 0, function* () { const buffer = yield ready; return buffer.toString('utf8'); }); }, json() { return this.text().then(json => JSON.parse(json)); }, arrayBuffer() { return __awaiter(this, void 0, void 0, function* () { const data = yield ready; const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength); return arrayBuffer; }); }, }; resolve(response); }); req.on('error', reject); (options === null || options === void 0 ? void 0 : options.body) && req.write(options.body, 'utf8'); req.end(); }); } exports.fetch = fetch; //# sourceMappingURL=simple-fetch.js.map