UNPKG

grafserv

Version:

A highly optimized server for GraphQL, powered by Grafast

104 lines 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeGraphiQLHandler = makeGraphiQLHandler; exports.makeGraphiQLStaticHandler = makeGraphiQLStaticHandler; const node_util_1 = require("node:util"); const node_zlib_1 = require("node:zlib"); const server_1 = require("ruru/server"); const static_1 = require("ruru/static"); const brotliCompress = (0, node_util_1.promisify)(node_zlib_1.brotliCompress); const BROTLI_OPTIONS = { params: { // No compression is ~3.2KB and 100,000 compresses takes 195ms // Level 1 compression is ~1.2KB and 100,000 compresses takes 1.46s // Level 5 compression is ~0.96KB and 100,000 compresses takes 3.12s // Level 11 compression is ~0.85KB and 100,000 compresses takes 211s [node_zlib_1.constants.BROTLI_PARAM_QUALITY]: 5, }, }; const BROTLI_HEADERS = { "content-encoding": "br", "content-type": "text/html; charset=utf-8", }; function makeGraphiQLHandler(resolvedPreset, middleware, dynamicOptions) { return async (request) => { const config = { ...resolvedPreset.ruru, // Override the ruru staticPath; this isn't for Ruru CLI staticPath: dynamicOptions.graphiqlStaticPath, endpoint: dynamicOptions.graphqlPath, // TODO: websocket endpoint clientConfig: { ...resolvedPreset.ruru?.clientConfig, debugTools: dynamicOptions.explain === true ? ["explain", "plan"] : dynamicOptions.explain === false ? [] : dynamicOptions.explain, }, }; const htmlParts = (0, server_1.makeHTMLParts)(config); let html; if (middleware != null && middleware.middleware.ruruHTML != null) { html = await middleware.run("ruruHTML", { resolvedPreset, config, htmlParts: { ...htmlParts }, request, }, ruruHTMLFromEvent); } else { html = (0, server_1.ruruHTML)(config, htmlParts); } let payload = Buffer.from(html, "utf8"); const accept = request.getHeader("accept-encoding"); if (typeof accept === "string" && /\bbr\b/i.test(accept)) { payload = await brotliCompress(payload, BROTLI_OPTIONS); const headers = BROTLI_HEADERS; return { type: "raw", request, dynamicOptions, headers, payload }; } else { return { type: "html", request, dynamicOptions, payload }; } }; } function makeGraphiQLStaticHandler(resolvedPreset, _middleware, dynamicOptions) { return async (request) => { const file = await (0, static_1.getStaticFile)({ ...resolvedPreset.ruru, // Override the ruru staticPath; this isn't for Ruru CLI staticPath: dynamicOptions.graphiqlStaticPath, urlPath: request.path, acceptEncoding: request.getHeader("accept-encoding"), }); if (!file) { return { request, dynamicOptions, type: "notFound", }; } const etag = request.getHeader("if-none-match"); if (etag === file.headers.etag) { return { statusCode: 304, request, dynamicOptions, type: "noContent", headers: { etag }, }; } return { statusCode: 200, request, dynamicOptions, type: "raw", headers: file.headers, payload: file.content, }; }; } function ruruHTMLFromEvent(event) { return (0, server_1.ruruHTML)(event.config, event.htmlParts); } //# sourceMappingURL=graphiql.js.map