@alignable/bifrost-fastify 0.0.2

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/index.ts ADDED
@@ -0,0 +1,200 @@
1
+ // Note that this file isn't processed by Vite, see https://github.com/brillout/vite-plugin-ssr/issues/562
2
+ import { FastifyReply, RawServerBase, FastifyPluginAsync } from "fastify";
3
+ import { FastifyRequest, RequestGenericInterface } from "fastify/types/request";
4
+ import proxy from "@fastify/http-proxy";
5
+ import accepts from "@fastify/accepts";
6
+ import { type PageContextProxy, type Proxy } from "../bifrost/types/internal";
7
+ import forwarded from "@fastify/forwarded";
8
+ import { Writable } from "stream";
9
+ import jsdom from "jsdom";
10
+ import { IncomingHttpHeaders, IncomingMessage } from "http";
11
+ import {
12
+ Http2ServerRequest,
13
+ IncomingHttpHeaders as Http2IncomingHttpHeaders,
14
+ } from "http2";
15
+ import { renderPage } from "vite-plugin-ssr/server";
16
+
17
+ type RequestExtendedWithProxy = FastifyRequest<
18
+ RequestGenericInterface,
19
+ RawServerBase
20
+ > & { _proxy?: { isPageContext: boolean; originalUrl?: string } };
21
+
22
+ function streamToString(stream: Writable): Promise<string> {
23
+ const chunks: Buffer[] = [];
24
+ return new Promise((resolve, reject) => {
25
+ stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
26
+ stream.on("error", (err) => reject(err));
27
+ stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
28
+ });
29
+ }
30
+
31
+ async function replyWithPage(
32
+ reply: FastifyReply<RawServerBase>,
33
+ pageContext: Awaited<ReturnType<typeof renderPage>>
34
+ ): Promise<FastifyReply> {
35
+ const { httpResponse } = pageContext;
36
+
37
+ if (!httpResponse) {
38
+ return reply.code(404).type("text/html").send("Not Found");
39
+ }
40
+
41
+ const { body, statusCode, contentType } = httpResponse;
42
+
43
+ return reply.status(statusCode).type(contentType).send(body);
44
+ }
45
+
46
+ const proxyPageId = "/proxy/pages";
47
+
48
+ interface ViteProxyPluginOptions {
49
+ upstream: URL;
50
+ host: URL;
51
+ getLayout: (reply: FastifyReply<RawServerBase>) => {
52
+ layout: string;
53
+ layoutProps: any;
54
+ };
55
+ /// Use to signal to legacy backend to return special results (eg. remove navbar etc)
56
+ rewriteRequestHeaders?: (
57
+ req: Http2ServerRequest | IncomingMessage,
58
+ headers: Http2IncomingHttpHeaders | IncomingHttpHeaders
59
+ ) => Http2IncomingHttpHeaders | IncomingHttpHeaders;
60
+ }
61
+ /**
62
+ * Fastify plugin that wraps @fasitfy/http-proxy to proxy Rails/Turbolinks server into a Vite-Plugin-SSR site.
63
+ */
64
+ export const viteProxyPlugin: FastifyPluginAsync<
65
+ ViteProxyPluginOptions
66
+ > = async (fastify, { upstream, host, rewriteRequestHeaders, getLayout }) => {
67
+ await fastify.register(accepts);
68
+ await fastify.register(proxy, {
69
+ upstream: upstream.href,
70
+ async preHandler(req, reply) {
71
+ if (req.method === "GET" && req.accepts().type(["html"]) === "html") {
72
+ const pageContextInit = {
73
+ urlOriginal: req.url,
74
+ };
75
+
76
+ const pageContext = await renderPage<
77
+ { _pageId: string },
78
+ typeof pageContextInit
79
+ >(pageContextInit);
80
+
81
+ const proxy = pageContext._pageId === proxyPageId;
82
+
83
+ if (!proxy) {
84
+ return replyWithPage(reply, pageContext);
85
+ } else {
86
+ // pageContext.json is added on client navigations to indicate we are returning just json for the client router
87
+ // we have to remove it before proxying though.
88
+ (req as RequestExtendedWithProxy)._proxy = {
89
+ isPageContext: req.raw.url!.includes("/index.pageContext.json"),
90
+ originalUrl: req.raw.url,
91
+ };
92
+ req.raw.url = req.raw.url!.replace("/index.pageContext.json", "");
93
+ }
94
+ }
95
+ },
96
+ replyOptions: {
97
+ rewriteRequestHeaders(request, headers) {
98
+ // Delete cache headers
99
+ delete headers["if-modified-since"];
100
+ delete headers["if-none-match"];
101
+ delete headers["if-unmodified-since"];
102
+ delete headers["if-none-match"];
103
+ delete headers["if-range"];
104
+
105
+ const fwd = forwarded(request as IncomingMessage).reverse();
106
+ // fwd.push(request.ip); TODO: not sure if this is needed
107
+ headers["X-Forwarded-For"] = fwd.join(", ");
108
+ headers["X-Forwarded-Host"] = host.host;
109
+ headers["X-Forwarded-Protocol"] = host.protocol;
110
+ if (rewriteRequestHeaders) {
111
+ return rewriteRequestHeaders(request, headers);
112
+ }
113
+ return headers;
114
+ },
115
+ async onResponse(req, reply: FastifyReply<RawServerBase>, res) {
116
+ const { isPageContext = false, originalUrl = undefined } =
117
+ (req as RequestExtendedWithProxy)._proxy || {};
118
+ if (isPageContext && originalUrl) {
119
+ // restore url rewrite
120
+ req.raw.url = originalUrl;
121
+ }
122
+
123
+ if ([301, 302, 303, 307, 308].includes(reply.statusCode)) {
124
+ const location = reply.getHeader("location") as string;
125
+ if (location) {
126
+ const url = new URL(location);
127
+ if (url.host === upstream.host) {
128
+ // rewrite redirect on upstream's host to the proxy host
129
+ url.host = host.host;
130
+ }
131
+ reply.header("location", url);
132
+ if (isPageContext) {
133
+ reply
134
+ .status(200)
135
+ .type("application/json")
136
+ .send(
137
+ JSON.stringify({
138
+ pageContext: {
139
+ // A bit hacky, but we manually construct the VPS pageContext here
140
+ _pageId: proxyPageId,
141
+ redirectTo: url,
142
+ },
143
+ })
144
+ );
145
+ } else {
146
+ return reply.send(res);
147
+ }
148
+ }
149
+ }
150
+
151
+ const { layout, layoutProps } = getLayout(reply);
152
+ if (!layout) {
153
+ return reply.send(res);
154
+ }
155
+
156
+ const html = await streamToString(res);
157
+ const dom = new jsdom.JSDOM(html);
158
+ const doc = dom.window.document;
159
+ const bodyEl = doc.querySelector("body");
160
+ const head = doc.querySelector("head");
161
+
162
+ if (!bodyEl || !head) {
163
+ return reply.code(404).type("text/html").send("proxy failed");
164
+ }
165
+
166
+ // disable vite-plugin-ssr link interceptor. May not be neccessary in future:
167
+ // https://github.com/brillout/vite-plugin-ssr/discussions/728#discussioncomment-5634111
168
+ bodyEl
169
+ .querySelectorAll("a[rel='external']")
170
+ .forEach((e) => e.setAttribute("data-turbolinks", "false"));
171
+ bodyEl.querySelectorAll("a").forEach((e) => (e.rel = "external"));
172
+
173
+ const bodyAttrs: Record<string, string> = {};
174
+ bodyEl.getAttributeNames().forEach((name) => {
175
+ bodyAttrs[name] = bodyEl.getAttribute(name)!;
176
+ });
177
+
178
+ const proxy: Proxy = {
179
+ body: bodyEl.innerHTML,
180
+ head: head.innerHTML,
181
+ bodyAttrs,
182
+ };
183
+
184
+ const pageContextInit: Partial<PageContextProxy> = {
185
+ urlOriginal: req.url,
186
+ layout,
187
+ layoutProps,
188
+ };
189
+ // proxySendClient is serialized and sent to client on subsequent navigation. proxy is ONLY included server-side to avoid doubling page size
190
+ if (isPageContext) { //TODO: send whole string instead of parsing and let browser do the parsing (turbolinks bridge lib)
191
+ Object.assign(pageContextInit, { proxySendClient: proxy });
192
+ } else {
193
+ Object.assign(pageContextInit, { proxy });
194
+ }
195
+ const pageContext = await renderPage(pageContextInit);
196
+ return replyWithPage(reply, pageContext);
197
+ },
198
+ },
199
+ });
200
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@alignable/bifrost-fastify",
3
+ "repository": "https://github.com/Alignable/bifrost.git",
4
+ "version": "0.0.2",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "dependencies": {
9
+ "@fastify/accepts": "^4.1.0",
10
+ "@fastify/forwarded": "^2.2.0",
11
+ "@fastify/http-proxy": "^8.4.0",
12
+ "@fastify/reply-from": "^9.0.1",
13
+ "compression": "^1.7.4",
14
+ "cross-env": "^7.0.3",
15
+ "fastify": "^4.9.2",
16
+ "jsdom": "^21.1.1",
17
+ "sirv": "^2.0.2",
18
+ "ts-node": "^10.9.1",
19
+ "vite": "^4.0.3"
20
+ },
21
+ "peerDependencies": {
22
+ "fastify": "^4.9.2",
23
+ "vite-plugin-ssr": "^0.4.120"
24
+ },
25
+ "devDependencies": {
26
+ "@types/connect": "^3.4.35",
27
+ "@types/jsdom": "^21.1.1",
28
+ "@types/node": "^18.11.9",
29
+ "@types/react": "^18.0.8",
30
+ "@types/react-dom": "^18.0.3",
31
+ "nodemon": "^2.0.21",
32
+ "react": "^18.2.0",
33
+ "react-dom": "^18.2.0",
34
+ "tsup": "^6.7.0",
35
+ "typescript": "^5.0.4"
36
+ },
37
+ "scripts": {
38
+ "build": "yarn tsup"
39
+ }
40
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "target": "ES2020",
5
+ "declaration": true,
6
+ "moduleResolution": "node",
7
+ "skipLibCheck": true,
8
+ "esModuleInterop": true,
9
+ "noEmit": true
10
+ },
11
+ "files": ["./index.ts"],
12
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup'
2
+
3
+ export default defineConfig({
4
+ entry: [
5
+ './index.ts',
6
+ ],
7
+ format: 'esm',
8
+ clean: true,
9
+ sourcemap: true,
10
+ dts: true
11
+ })