UNPKG

@graphql-mesh/serve-runtime

Version:
31 lines (30 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useStaticFiles = void 0; const cross_helpers_1 = require("@graphql-mesh/cross-helpers"); const utils_1 = require("@graphql-mesh/utils"); const useStaticFiles = ({ baseDir, staticFiles = 'public' }) => { return { onRequest({ request, url, endResponse, fetchAPI }) { if (request.method === 'GET') { let relativePath = url.pathname; if (relativePath === '/' || !relativePath) { relativePath = 'index.html'; } const absoluteStaticFilesPath = cross_helpers_1.path.join(baseDir, staticFiles); const absolutePath = cross_helpers_1.path.join(absoluteStaticFilesPath, relativePath); if (absolutePath.startsWith(absoluteStaticFilesPath)) { return (0, utils_1.pathExists)(absolutePath).then(exists => { if (exists) { const readStream = cross_helpers_1.fs.createReadStream(absolutePath); endResponse(new fetchAPI.Response(readStream, { status: 200, })); } }); } } }, }; }; exports.useStaticFiles = useStaticFiles;