UNPKG

signalk-server

Version:

An implementation of a [Signal K](http://signalk.org) server for boats.

88 lines 3.39 kB
"use strict"; /** * Fetch Wrapper for WASM Network Capability * * Provides a Node.js fetch wrapper that handles various header formats */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNodeFetch = getNodeFetch; const debug_1 = __importDefault(require("debug")); const debug = (0, debug_1.default)('signalk:wasm:fetch'); let cachedFetch = null; /** * Get a properly wrapped fetch function for use with as-fetch */ function getNodeFetch() { if (cachedFetch) { return cachedFetch; } try { // Try to use native Node.js fetch (Node 18+) const nativeFetch = globalThis.fetch; if (!nativeFetch) { throw new Error('Native fetch not available'); } // Wrap native fetch to handle headers properly for as-fetch cachedFetch = async (input, init) => { const sanitizedInit = init ? { ...init } : {}; // Ensure headers are in a format Node.js fetch accepts if (sanitizedInit.headers) { const headers = sanitizedInit.headers; if (typeof headers === 'object' && !Array.isArray(headers) && !(headers instanceof Headers)) { if (Object.getPrototypeOf(headers) === Object.prototype || Object.getPrototypeOf(headers) === null) { sanitizedInit.headers = headers; } else { const headersObj = {}; try { for (const [key, value] of Object.entries(headers)) { headersObj[key] = String(value); } sanitizedInit.headers = headersObj; } catch (err) { debug('Error converting headers:', err); sanitizedInit.headers = {}; } } } else if (Array.isArray(headers)) { const headersObj = {}; for (const [key, value] of headers) { headersObj[key] = value; } sanitizedInit.headers = headersObj; } else if (headers instanceof Headers) { const headersObj = {}; headers.forEach((value, key) => { headersObj[key] = value; }); sanitizedInit.headers = headersObj; } else { sanitizedInit.headers = {}; } } else { sanitizedInit.headers = {}; } return nativeFetch(input, sanitizedInit); }; return cachedFetch; } catch { debug('Warning: Native fetch not available, network capability will be limited'); cachedFetch = async () => { throw new Error('Fetch not available - Node.js 18+ required for network capability'); }; return cachedFetch; } } //# sourceMappingURL=fetch-wrapper.js.map