UNPKG

@sebastianwessel/quickjs

Version:

A typescript package to execute JavaScript and TypeScript code in a WebAssembly QuickJS sandbox

47 lines (46 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.provideHttp = void 0; const fetch_js_1 = require("../../adapter/fetch.js"); const expose_js_1 = require("../expose/expose.js"); /** * Provide http related functions */ const provideHttp = (ctx, scope, options, input) => { const injectUnsupported = (name) => () => { throw new Error(`Not supported: ${name} has been disabled for security reasons or is not supported by the runtime`); }; let fetchFunction = Object.assign(injectUnsupported('fetch'), { preconnect: async (_url, _options) => { return; }, }); if (options.allowFetch) { const adapter = options.fetchAdapter ?? (0, fetch_js_1.getDefaultFetchAdapter)({ fs: input?.fs }); fetchFunction = Object.assign(adapter, { preconnect: async (_url, _options) => { return; }, }); } (0, expose_js_1.expose)(ctx, scope, { __parseURL: (input, base) => { const url = new URL(input, base); return { href: url.href, origin: url.origin, protocol: url.protocol, username: url.username, password: url.password, host: url.host, hostname: url.hostname, port: url.port, pathname: url.pathname, hash: url.hash, search: url.search, }; }, fetch: fetchFunction, }); }; exports.provideHttp = provideHttp;