UNPKG

realm

Version:

Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores

56 lines 2.37 kB
"use strict"; //////////////////////////////////////////////////////////////////////////// // // Copyright 2022 Realm Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // //////////////////////////////////////////////////////////////////////////// Object.defineProperty(exports, "__esModule", { value: true }); exports.createNetworkTransport = void 0; const internal_1 = require("../internal"); const debug = (0, internal_1.extendDebug)("network"); function flattenHeaders(headers) { const result = {}; headers.forEach((value, key) => { result[key] = value; }); return result; } /** @internal */ function createNetworkTransport(fetch = internal_1.network.fetch) { return internal_1.binding.Helpers.makeNetworkTransport((request, callback) => { const [url, init] = internal_1.binding.toFetchArgs(request); debug("requesting %s %O", url, init); fetch(url, init).then(async (response) => { debug("responded %O", response); const headers = flattenHeaders(response.headers); const contentType = headers["content-type"]; const body = contentType ? await response.text() : ""; callback({ customStatusCode: 0, httpStatusCode: response.status, headers, body, }); }, (err) => { // Core will propagate any non-zero "custom status code" through to the caller // The error message is passed through the body const reason = err.message || "Unknown"; const body = `request to ${request.url} failed, reason: ${reason}`; callback({ httpStatusCode: 0, headers: {}, customStatusCode: -1, body }); }); }); } exports.createNetworkTransport = createNetworkTransport; //# sourceMappingURL=NetworkTransport.js.map