@alignable/bifrost-fastify 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ function extractDomElements(html) {
|
|
|
37
37
|
// index.ts
|
|
38
38
|
import { Http2ServerRequest } from "http2";
|
|
39
39
|
import { text } from "stream/consumers";
|
|
40
|
+
import { parse as parseContentType } from "fast-content-type-parse";
|
|
40
41
|
var viteProxyPlugin = async (fastify, opts) => {
|
|
41
42
|
const { upstream, host, onError, buildPageContextInit } = opts;
|
|
42
43
|
async function replyWithPage(reply, pageContext) {
|
|
@@ -147,10 +148,14 @@ var viteProxyPlugin = async (fastify, opts) => {
|
|
|
147
148
|
if (!proxyLayoutInfo) {
|
|
148
149
|
return reply.send("stream" in res ? res.stream : res);
|
|
149
150
|
}
|
|
151
|
+
const contentType = reply.getHeader("content-type");
|
|
152
|
+
if (!contentType || parseContentType(contentType).type !== "text/html") {
|
|
153
|
+
return reply.send("stream" in res ? res.stream : res);
|
|
154
|
+
}
|
|
150
155
|
const html = await text(res.stream);
|
|
151
156
|
const { bodyAttributes, bodyInnerHtml, headInnerHtml } = extractDomElements(html);
|
|
152
157
|
if (!bodyInnerHtml || !headInnerHtml) {
|
|
153
|
-
|
|
158
|
+
return reply.send(html);
|
|
154
159
|
}
|
|
155
160
|
const pageContextInit = {
|
|
156
161
|
urlOriginal: req.url,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts","../lib/extractDomElements.ts"],"sourcesContent":["// Note that this file isn't processed by Vite, see https://github.com/brillout/vike/issues/562\nimport {\n FastifyReply,\n RawServerBase,\n FastifyPluginAsync,\n RouteGenericInterface,\n} from \"fastify\";\nimport { FastifyRequest, RequestGenericInterface } from \"fastify/types/request\";\nimport proxy, { type FastifyHttpProxyOptions } from \"@fastify/http-proxy\";\nimport accepts from \"@fastify/accepts\";\nimport forwarded from \"@fastify/forwarded\";\nimport type { GetLayout, WrappedServerOnly } from \"@alignable/bifrost/config\";\nimport { PassThrough, Writable } from \"stream\";\nimport { renderPage } from \"vike/server\";\nimport { PageContextServer } from \"vike/types\";\nimport { extractDomElements } from \"./lib/extractDomElements\";\nimport { Http2ServerRequest } from \"http2\";\nimport { IncomingMessage } from \"http\";\nimport { text } from \"node:stream/consumers\";\n\ntype RenderedPageContext = Awaited<\n ReturnType<\n typeof renderPage<\n {\n isClientSideNavigation?: boolean;\n },\n { urlOriginal: string }\n >\n >\n>;\n\ndeclare module \"fastify\" {\n interface FastifyRequest {\n bifrostPageId?: string | null;\n vikePageContext?: Partial<PageContextServer> | null;\n getLayout: GetLayout | null;\n customPageContextInit: Partial<Omit<PageContextServer, \"headers\">> | null;\n }\n}\n\ntype RawRequestExtendedWithProxy = FastifyRequest<\n RequestGenericInterface,\n RawServerBase\n>[\"raw\"] & {\n _bfproxy?: boolean;\n};\n\ninterface ViteProxyPluginOptions\n extends Omit<\n FastifyHttpProxyOptions,\n \"upstream\" | \"preHandler\" | \"replyOptions\"\n > {\n upstream: URL;\n host: URL;\n onError?: (error: any, pageContext: RenderedPageContext) => void;\n buildPageContextInit?: (\n req: FastifyRequest\n ) => Promise<Partial<Omit<PageContextServer, \"headers\">>>;\n}\n/**\n * Fastify plugin that wraps @fasitfy/http-proxy to proxy Rails/Turbolinks server into a vike site.\n */\nexport const viteProxyPlugin: FastifyPluginAsync<\n ViteProxyPluginOptions\n> = async (fastify, opts) => {\n const { upstream, host, onError, buildPageContextInit } = opts;\n async function replyWithPage(\n reply: FastifyReply<RouteGenericInterface, RawServerBase>,\n pageContext: RenderedPageContext\n ): Promise<FastifyReply> {\n const { httpResponse } = pageContext;\n\n if (\n onError &&\n httpResponse?.statusCode === 500 &&\n pageContext.errorWhileRendering\n ) {\n onError(pageContext.errorWhileRendering, pageContext);\n }\n\n if (!httpResponse) {\n return reply.code(404).type(\"text/html\").send(\"Not Found\");\n }\n\n const { pipe, statusCode, headers } = httpResponse;\n const stream = new PassThrough();\n pipe(stream);\n return reply\n .status(statusCode)\n .headers(Object.fromEntries(headers))\n .send(stream);\n }\n await fastify.register(accepts);\n fastify.decorateRequest(\"bifrostPageId\", null);\n fastify.decorateRequest(\"vikePageContext\", null);\n fastify.decorateRequest(\"getLayout\", null);\n fastify.decorateRequest(\"customPageContextInit\", null);\n await fastify.register(proxy, {\n ...opts,\n upstream: upstream.href,\n websocket: true,\n async preHandler(req, reply) {\n if (\n (req.method === \"GET\" || req.method === \"HEAD\") &&\n req.accepts().type([\"html\"]) === \"html\"\n ) {\n req.customPageContextInit = buildPageContextInit\n ? await buildPageContextInit(req)\n : {};\n\n const pageContextInit = {\n urlOriginal: req.url,\n headersOriginal: req.headers,\n ...req.customPageContextInit,\n };\n\n const pageContext = await renderPage(pageContextInit);\n\n // this does not handle getting the original pageId when errors are thrown: https://github.com/vikejs/vike/issues/1112\n req.bifrostPageId = pageContext.pageId;\n req.vikePageContext = pageContext;\n\n const proxyMode = pageContext.config?.proxyMode;\n\n switch (proxyMode) {\n case \"passthru\": {\n req.log.info(`bifrost: passthru proxy to backend`);\n return;\n }\n case \"wrapped\": {\n req.log.info(`bifrost: proxy route matched, proxying to backend`);\n if (!!pageContext.isClientSideNavigation) {\n // This should never happen because wrapped proxy routes have no onBeforeRender. onRenderClient should make a request to the legacy backend.\n req.log.error(\n \"Wrapped proxy route is requesting index.pageContext.json. Something is wrong with the client.\"\n );\n return reply.redirect(\n req.url.replace(\"/index.pageContext.json\", \"\")\n );\n }\n if (!pageContext.config?.getLayout) {\n req.log.error(\n \"Config missing getLayout on wrapped route! Falling back to passthru proxy\"\n );\n return;\n }\n\n let proxyHeadersAlreadySet = true;\n for (const [key, val] of Object.entries(\n pageContext.config?.proxyHeaders || {}\n )) {\n proxyHeadersAlreadySet &&= req.headers[key.toLowerCase()] == val;\n req.headers[key.toLowerCase()] = val;\n }\n // If proxy headers set, this is a client navigation meant to go direct to legacy backend.\n // Use passthru proxy in this case.\n // ALB CANNOT be used for this. see `onBeforeRenderClient` for details\n if (proxyHeadersAlreadySet) return;\n\n (req.raw as RawRequestExtendedWithProxy)._bfproxy = true;\n req.getLayout = pageContext.config.getLayout;\n return;\n }\n default:\n req.log.info(`bifrost: rendering page ${pageContext.pageId}`);\n return replyWithPage(reply, pageContext);\n }\n }\n },\n replyOptions: {\n rewriteRequestHeaders(request, headers) {\n if (!(request.raw instanceof Http2ServerRequest)) {\n const fwd = forwarded(request.raw).reverse();\n headers[\"X-Forwarded-For\"] = fwd.join(\", \");\n headers[\"X-Forwarded-Host\"] = host.host;\n headers[\"X-Forwarded-Proto\"] = host.protocol;\n }\n\n if ((request.raw as RawRequestExtendedWithProxy)._bfproxy) {\n // Proxying and wrapping\n\n // Delete cache headers\n delete headers[\"if-modified-since\"];\n delete headers[\"if-none-match\"];\n delete headers[\"if-unmodified-since\"];\n delete headers[\"if-none-match\"];\n delete headers[\"if-range\"];\n }\n return headers;\n },\n async onResponse(req, reply, res) {\n if ([301, 302, 303, 307, 308].includes(reply.statusCode)) {\n const location = reply.getHeader(\"location\") as string;\n if (location) {\n const url = new URL(location, host.href);\n if (url.host === upstream.host || url.host === host.host) {\n // rewrite redirect on upstream's host to the proxy host\n url.host = host.host;\n url.protocol = host.protocol;\n }\n reply.header(\"location\", url);\n return reply.send(\"stream\" in res ? res.stream : res);\n }\n }\n\n const proxyLayoutInfo = req.getLayout?.(reply.getHeaders());\n if (!proxyLayoutInfo) {\n return reply.send(\"stream\" in res ? res.stream : res);\n }\n\n const html = await text(res.stream);\n\n const { bodyAttributes, bodyInnerHtml, headInnerHtml } =\n extractDomElements(html);\n\n if (!bodyInnerHtml || !headInnerHtml) {\n throw new Error(\"Proxy failed\");\n }\n\n const pageContextInit = {\n urlOriginal: req.url,\n headersOriginal: req.headers,\n // Critical that we don't set any passToClient values in pageContextInit\n // If we do, Vike re-requests pageContext on client navigation. This breaks wrapped proxy.\n _wrappedServerOnly: {\n bodyAttributes,\n bodyInnerHtml,\n headInnerHtml,\n proxyLayoutInfo,\n } satisfies WrappedServerOnly,\n ...req.customPageContextInit,\n };\n const pageContext = await renderPage(pageContextInit);\n return replyWithPage(reply, pageContext);\n },\n },\n });\n};\n","import { Parser } from \"htmlparser2\";\nimport { DomHandler, type Element } from \"domhandler\";\nimport { findOne } from \"domutils\";\nimport render from \"dom-serializer\";\n\nfunction getInnerHtml(element: Element): string {\n return element.children.map((c) => render(c)).join(\"\");\n}\n\nexport function extractDomElements(html: string): {\n bodyInnerHtml: string | null;\n headInnerHtml: string | null;\n bodyAttributes: Record<string, string>;\n} {\n let bodyInnerHtml: string | null = null;\n let headInnerHtml: string | null = null;\n let bodyAttributes: Record<string, string> = {};\n const handler = new DomHandler((error, dom) => {\n if (!error) {\n const body = findOne((elem) => elem.name === \"body\", dom);\n const head = findOne((elem) => elem.name === \"head\", dom);\n if (body && head) {\n bodyAttributes = body.attribs;\n bodyInnerHtml = getInnerHtml(body);\n headInnerHtml = getInnerHtml(head);\n }\n }\n });\n const parser = new Parser(handler);\n parser.write(html);\n parser.end();\n return { bodyInnerHtml, headInnerHtml, bodyAttributes };\n}\n"],"mappings":";AAQA,OAAO,WAA6C;AACpD,OAAO,aAAa;AACpB,OAAO,eAAe;AAEtB,SAAS,mBAA6B;AACtC,SAAS,kBAAkB;;;ACb3B,SAAS,cAAc;AACvB,SAAS,kBAAgC;AACzC,SAAS,eAAe;AACxB,OAAO,YAAY;AAEnB,SAAS,aAAa,SAA0B;AAC9C,SAAO,QAAQ,SAAS,IAAI,CAAC,MAAM,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE;AACvD;AAEO,SAAS,mBAAmB,MAIjC;AACA,MAAI,gBAA+B;AACnC,MAAI,gBAA+B;AACnC,MAAI,iBAAyC,CAAC;AAC9C,QAAM,UAAU,IAAI,WAAW,CAAC,OAAO,QAAQ;AAC7C,QAAI,CA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,IACnD,mBAAmB,IAAI;AAEzB,YAAI,CAAC,iBAAiB,CAAC,eAAe;AACpC,gBAAM,IAAI,MAAM,cAAc;AAAA,QAChC;AAEA,cAAM,kBAAkB;AAAA,UACtB,aAAa,IAAI;AAAA,UACjB,iBAAiB,IAAI;AAAA;AAAA;AAAA,UAGrB,oBAAoB;AAAA,YAClB;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,UACA,GAAG,IAAI;AAAA,QACT;AACA,cAAM,cAAc,MAAM,WAAW,eAAe;AACpD,eAAO,cAAc,OAAO,WAAW;AAAA,MACzC;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../index.ts","../lib/extractDomElements.ts"],"sourcesContent":["// Note that this file isn't processed by Vite, see https://github.com/brillout/vike/issues/562\nimport {\n FastifyReply,\n RawServerBase,\n FastifyPluginAsync,\n RouteGenericInterface,\n} from \"fastify\";\nimport { FastifyRequest, RequestGenericInterface } from \"fastify/types/request\";\nimport proxy, { type FastifyHttpProxyOptions } from \"@fastify/http-proxy\";\nimport accepts from \"@fastify/accepts\";\nimport forwarded from \"@fastify/forwarded\";\nimport type { GetLayout, WrappedServerOnly } from \"@alignable/bifrost/config\";\nimport { PassThrough, Writable } from \"stream\";\nimport { renderPage } from \"vike/server\";\nimport { PageContextServer } from \"vike/types\";\nimport { extractDomElements } from \"./lib/extractDomElements\";\nimport { Http2ServerRequest } from \"http2\";\nimport { IncomingMessage } from \"http\";\nimport { text } from \"node:stream/consumers\";\nimport { parse as parseContentType } from 'fast-content-type-parse';\n\ntype RenderedPageContext = Awaited<\n ReturnType<\n typeof renderPage<\n {\n isClientSideNavigation?: boolean;\n },\n { urlOriginal: string }\n >\n >\n>;\n\ndeclare module \"fastify\" {\n interface FastifyRequest {\n bifrostPageId?: string | null;\n vikePageContext?: Partial<PageContextServer> | null;\n getLayout: GetLayout | null;\n customPageContextInit: Partial<Omit<PageContextServer, \"headers\">> | null;\n }\n}\n\ntype RawRequestExtendedWithProxy = FastifyRequest<\n RequestGenericInterface,\n RawServerBase\n>[\"raw\"] & {\n _bfproxy?: boolean;\n};\n\ninterface ViteProxyPluginOptions\n extends Omit<\n FastifyHttpProxyOptions,\n \"upstream\" | \"preHandler\" | \"replyOptions\"\n > {\n upstream: URL;\n host: URL;\n onError?: (error: any, pageContext: RenderedPageContext) => void;\n buildPageContextInit?: (\n req: FastifyRequest\n ) => Promise<Partial<Omit<PageContextServer, \"headers\">>>;\n}\n/**\n * Fastify plugin that wraps @fasitfy/http-proxy to proxy Rails/Turbolinks server into a vike site.\n */\nexport const viteProxyPlugin: FastifyPluginAsync<\n ViteProxyPluginOptions\n> = async (fastify, opts) => {\n const { upstream, host, onError, buildPageContextInit } = opts;\n async function replyWithPage(\n reply: FastifyReply<RouteGenericInterface, RawServerBase>,\n pageContext: RenderedPageContext\n ): Promise<FastifyReply> {\n const { httpResponse } = pageContext;\n\n if (\n onError &&\n httpResponse?.statusCode === 500 &&\n pageContext.errorWhileRendering\n ) {\n onError(pageContext.errorWhileRendering, pageContext);\n }\n\n if (!httpResponse) {\n return reply.code(404).type(\"text/html\").send(\"Not Found\");\n }\n\n const { pipe, statusCode, headers } = httpResponse;\n const stream = new PassThrough();\n pipe(stream);\n return reply\n .status(statusCode)\n .headers(Object.fromEntries(headers))\n .send(stream);\n }\n await fastify.register(accepts);\n fastify.decorateRequest(\"bifrostPageId\", null);\n fastify.decorateRequest(\"vikePageContext\", null);\n fastify.decorateRequest(\"getLayout\", null);\n fastify.decorateRequest(\"customPageContextInit\", null);\n await fastify.register(proxy, {\n ...opts,\n upstream: upstream.href,\n websocket: true,\n async preHandler(req, reply) {\n if (\n (req.method === \"GET\" || req.method === \"HEAD\") &&\n req.accepts().type([\"html\"]) === \"html\"\n ) {\n req.customPageContextInit = buildPageContextInit\n ? await buildPageContextInit(req)\n : {};\n\n const pageContextInit = {\n urlOriginal: req.url,\n headersOriginal: req.headers,\n ...req.customPageContextInit,\n };\n\n const pageContext = await renderPage(pageContextInit);\n\n // this does not handle getting the original pageId when errors are thrown: https://github.com/vikejs/vike/issues/1112\n req.bifrostPageId = pageContext.pageId;\n req.vikePageContext = pageContext;\n\n const proxyMode = pageContext.config?.proxyMode;\n\n switch (proxyMode) {\n case \"passthru\": {\n req.log.info(`bifrost: passthru proxy to backend`);\n return;\n }\n case \"wrapped\": {\n req.log.info(`bifrost: proxy route matched, proxying to backend`);\n if (!!pageContext.isClientSideNavigation) {\n // This should never happen because wrapped proxy routes have no onBeforeRender. onRenderClient should make a request to the legacy backend.\n req.log.error(\n \"Wrapped proxy route is requesting index.pageContext.json. Something is wrong with the client.\"\n );\n return reply.redirect(\n req.url.replace(\"/index.pageContext.json\", \"\")\n );\n }\n if (!pageContext.config?.getLayout) {\n req.log.error(\n \"Config missing getLayout on wrapped route! Falling back to passthru proxy\"\n );\n return;\n }\n\n let proxyHeadersAlreadySet = true;\n for (const [key, val] of Object.entries(\n pageContext.config?.proxyHeaders || {}\n )) {\n proxyHeadersAlreadySet &&= req.headers[key.toLowerCase()] == val;\n req.headers[key.toLowerCase()] = val;\n }\n // If proxy headers set, this is a client navigation meant to go direct to legacy backend.\n // Use passthru proxy in this case.\n // ALB CANNOT be used for this. see `onBeforeRenderClient` for details\n if (proxyHeadersAlreadySet) return;\n\n (req.raw as RawRequestExtendedWithProxy)._bfproxy = true;\n req.getLayout = pageContext.config.getLayout;\n return;\n }\n default:\n req.log.info(`bifrost: rendering page ${pageContext.pageId}`);\n return replyWithPage(reply, pageContext);\n }\n }\n },\n replyOptions: {\n rewriteRequestHeaders(request, headers) {\n if (!(request.raw instanceof Http2ServerRequest)) {\n const fwd = forwarded(request.raw).reverse();\n headers[\"X-Forwarded-For\"] = fwd.join(\", \");\n headers[\"X-Forwarded-Host\"] = host.host;\n headers[\"X-Forwarded-Proto\"] = host.protocol;\n }\n\n if ((request.raw as RawRequestExtendedWithProxy)._bfproxy) {\n // Proxying and wrapping\n\n // Delete cache headers\n delete headers[\"if-modified-since\"];\n delete headers[\"if-none-match\"];\n delete headers[\"if-unmodified-since\"];\n delete headers[\"if-none-match\"];\n delete headers[\"if-range\"];\n }\n return headers;\n },\n async onResponse(req, reply, res) {\n if ([301, 302, 303, 307, 308].includes(reply.statusCode)) {\n const location = reply.getHeader(\"location\") as string;\n if (location) {\n const url = new URL(location, host.href);\n if (url.host === upstream.host || url.host === host.host) {\n // rewrite redirect on upstream's host to the proxy host\n url.host = host.host;\n url.protocol = host.protocol;\n }\n reply.header(\"location\", url);\n return reply.send(\"stream\" in res ? res.stream : res);\n }\n }\n\n const proxyLayoutInfo = req.getLayout?.(reply.getHeaders());\n if (!proxyLayoutInfo) {\n return reply.send(\"stream\" in res ? res.stream : res);\n }\n\n const contentType = reply.getHeader(\"content-type\") as string | undefined;\n \n if (!contentType || parseContentType(contentType).type !== 'text/html') {\n return reply.send(\"stream\" in res ? res.stream : res);\n }\n\n const html = await text(res.stream);\n\n const { bodyAttributes, bodyInnerHtml, headInnerHtml } =\n extractDomElements(html);\n\n if (!bodyInnerHtml || !headInnerHtml) {\n return reply.send(html);\n }\n\n const pageContextInit = {\n urlOriginal: req.url,\n headersOriginal: req.headers,\n // Critical that we don't set any passToClient values in pageContextInit\n // If we do, Vike re-requests pageContext on client navigation. This breaks wrapped proxy.\n _wrappedServerOnly: {\n bodyAttributes,\n bodyInnerHtml,\n headInnerHtml,\n proxyLayoutInfo,\n } satisfies WrappedServerOnly,\n ...req.customPageContextInit,\n };\n const pageContext = await renderPage(pageContextInit);\n return replyWithPage(reply, pageContext);\n },\n },\n });\n};\n","import { Parser } from \"htmlparser2\";\nimport { DomHandler, type Element } from \"domhandler\";\nimport { findOne } from \"domutils\";\nimport render from \"dom-serializer\";\n\nfunction getInnerHtml(element: Element): string {\n return element.children.map((c) => render(c)).join(\"\");\n}\n\nexport function extractDomElements(html: string): {\n bodyInnerHtml: string | null;\n headInnerHtml: string | null;\n bodyAttributes: Record<string, string>;\n} {\n let bodyInnerHtml: string | null = null;\n let headInnerHtml: string | null = null;\n let bodyAttributes: Record<string, string> = {};\n const handler = new DomHandler((error, dom) => {\n if (!error) {\n const body = findOne((elem) => elem.name === \"body\", dom);\n const head = findOne((elem) => elem.name === \"head\", dom);\n if (body && head) {\n bodyAttributes = body.attribs;\n bodyInnerHtml = getInnerHtml(body);\n headInnerHtml = getInnerHtml(head);\n }\n }\n });\n const parser = new Parser(handler);\n parser.write(html);\n parser.end();\n return { bodyInnerHtml, headInnerHtml, bodyAttributes };\n}\n"],"mappings":";AAQA,OAAO,WAA6C;AACpD,OAAO,aAAa;AACpB,OAAO,eAAe;AAEtB,SAAS,mBAA6B;AACtC,SAAS,kBAAkB;;;ACb3B,SAAS,cAAc;AACvB,SAAS,kBAAgC;AACzC,SAAS,eAAe;AACxB,OAAO,YAAY;AAEnB,SAAS,aAAa,SAA0B;AAC9C,SAAO,QAAQ,SAAS,IAAI,CAAC,MAAM,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE;AACvD;AAEO,SAAS,mBAAmB,MAIjC;AACA,MAAI,gBAA+B;AACnC,MAAI,gBAA+B;AACnC,MAAI,iBAAyC,CAAC;AAC9C,QAAM,UAAU,IAAI,WAAW,CAAC,OAAO,QAAQ;AAC7C,QAAI,CAAC,OAAO;AACV,YAAM,OAAO,QAAQ,CAAC,SAAS,KAAK,SAAS,QAAQ,GAAG;AACxD,YAAM,OAAO,QAAQ,CAAC,SAAS,KAAK,SAAS,QAAQ,GAAG;AACxD,UAAI,QAAQ,MAAM;AAChB,yBAAiB,KAAK;AACtB,wBAAgB,aAAa,IAAI;AACjC,wBAAgB,aAAa,IAAI;AAAA,MACnC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,SAAS,IAAI,OAAO,OAAO;AACjC,SAAO,MAAM,IAAI;AACjB,SAAO,IAAI;AACX,SAAO,EAAE,eAAe,eAAe,eAAe;AACxD;;;ADhBA,SAAS,0BAA0B;AAEnC,SAAS,YAAY;AACrB,SAAS,SAAS,wB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AA,UACA,GAAG,IAAI;AAAA,QACT;AACA,cAAM,cAAc,MAAM,WAAW,eAAe;AACpD,eAAO,cAAc,OAAO,WAAW;AAAA,MACzC;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alignable/bifrost-fastify",
|
|
3
3
|
"repository": "https://github.com/Alignable/bifrost.git",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.11",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -13,13 +13,14 @@
|
|
|
13
13
|
"dom-serializer": "^2.0.0",
|
|
14
14
|
"domhandler": "^5.0.3",
|
|
15
15
|
"domutils": "^3.2.2",
|
|
16
|
+
"fast-content-type-parse": "^3.0.0",
|
|
16
17
|
"htmlparser2": "^10.0.0"
|
|
17
18
|
},
|
|
18
19
|
"peerDependencies": {
|
|
19
20
|
"@alignable/bifrost": "1.0.10",
|
|
20
21
|
"fastify": "^5.0.0",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
22
|
+
"vike": "0.4.247",
|
|
23
|
+
"vite": ">=6"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@types/connect": "^3.4.35",
|