-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (169 loc) · 5.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
const fastifyPlugin = require('fastify-plugin');
const path = require('node:path');
const url = require('node:url');
const {
createReadableStreamFromReadable,
createRequestHandler: createRemixRequestHandler,
} = require('@remix-run/node');
const { Readable } = require('node:stream');
/**
* Constructs a URL object from protocol, hostname, and original URL.
* @param {string} protocol - Protocol (e.g., 'http', 'https').
* @param {string} hostname - Hostname.
* @param {string} originalUrl - Original URL.
* @returns {URL} URL object.
*/
const getUrl = (protocol, hostname, originalUrl) => new URL(`${protocol}://${hostname}${originalUrl}`);
/**
* Converts a Fastify request to a Remix-compatible request.
* @param {import('fastify').FastifyRequest} request - Fastify request.
* @param {import('fastify').FastifyReply} reply - Fastify reply.
* @returns {Request} Remix request.
*/
const createFastifyRemixRequest = (request, reply) => {
const url = getUrl(request.protocol, request.hostname, request.originalUrl);
const abortController = new AbortController();
reply.raw.on('close', () => abortController.abort());
const init = {
method: request.method,
headers: request.headers,
signal: abortController.signal,
};
if (request.method !== 'GET' && request.method !== 'HEAD') {
init.body = createReadableStreamFromReadable(request.raw);
init.duplex = 'half';
}
return new Request(url, init);
};
/**
* Converts a Response object to a Readable stream.
* @param {Response} response - Response object.
* @returns {Readable|null} Readable stream or null if no body.
*/
const responseToReadable = (response) => {
if (!response.body) return null;
const reader = response.body.getReader();
return new Readable({
read() {
reader.read().then(
({ done, value }) => {
if (done) {
this.push(null);
return reader.cancel();
}
this.push(Buffer.from(value));
},
(error) => this.destroy(error)
);
},
cancel(error) {
reader.cancel(error);
},
});
};
/**
* Sends a Remix response via Fastify.
* @param {import('fastify').FastifyReply} reply - Fastify reply.
* @param {Response} result - Remix response.
* @returns {Promise<void>} Promise resolving when response is sent.
*/
const sendFastifyRemixResponse = async (reply, result) => {
reply.headers(Object.fromEntries(result.headers));
return result.body ? reply.send(responseToReadable(result.clone())) : reply.send(await result.text());
};
/**
* Creates a Remix request handler for Fastify.
* @param {Object} options - Request handler options.
* @param {Object} options.build - Build object.
* @param {Function} [options.getLoadContext] - Function to get load context.
* @param {string} options.mode - Mode ('development' or 'production').
* @returns {Function} Request handler function.
*/
const createRequestHandler = ({ build, getLoadContext, mode }) => {
const handler = createRemixRequestHandler(build, mode);
return async (request, reply) => {
try {
const remixRequest = createFastifyRemixRequest(request, reply);
const loadContext = getLoadContext ? await getLoadContext(request, reply) : undefined;
const result = await handler(remixRequest, loadContext);
return sendFastifyRemixResponse(reply, result);
} catch (error) {
return reply.status(500).send('Internal Server Error');
}
};
};
/**
* Creates the Remix Fastify plugin.
* @param {Object} [options] - Plugin options.
* @param {string} [options.buildDirectory='build'] - Build directory.
* @param {string} [options.clientDirectory='client'] - Client directory.
* @param {string} [options.serverDirectory='server'] - Server directory.
* @param {string} [options.serverBuildFile='index.js'] - Server build file.
* @param {string} [options.mode=process.env.NODE_ENV || 'development'] - Mode.
* @param {Object} [options.fastifyStaticOptions={}] - Fastify static options.
* @param {Object} [options.viteOptions={}] - Vite options.
* @returns {Function} Fastify plugin function.
*/
const remixFastify =
(
options = {
buildDirectory: 'build',
clientDirectory: 'client',
serverDirectory: 'server',
serverBuildFile: 'index.js',
mode: process.env.NODE_ENV || 'development',
fastifyStaticOptions: {},
viteOptions: {},
}
) =>
async (fastify) => {
const {
buildDirectory = 'build',
clientDirectory = 'client',
serverDirectory = 'server',
serverBuildFile = 'index.js',
mode = process.env.NODE_ENV || 'development',
fastifyStaticOptions = {},
viteOptions = {},
} = options;
let viteDevServer;
if (mode === 'development') {
const { createServer } = await import('vite');
viteDevServer = await createServer({
...viteOptions,
server: { middlewareMode: true, ...viteOptions?.server },
});
}
const cwd = process.cwd();
const serverBuildUrl = url.pathToFileURL(path.join(cwd, buildDirectory, serverDirectory, serverBuildFile)).href;
const remixHandler = createRequestHandler({
build: viteDevServer
? () => viteDevServer.ssrLoadModule('virtual:remix/server-build')
: () => import(serverBuildUrl),
mode,
});
if (viteDevServer) {
await fastify.register(require('@fastify/middie'));
fastify.use(viteDevServer.middlewares);
} else {
const clientBuildDirectory = path.join(cwd, buildDirectory, clientDirectory);
await fastify.register(require('@fastify/static'), {
root: clientBuildDirectory,
prefix: '/',
wildcard: false,
dotfiles: 'allow',
etag: true,
serveDotFiles: true,
lastModified: true,
...fastifyStaticOptions,
});
}
fastify.all('*', remixHandler);
};
module.exports = fastifyPlugin(remixFastify, {
fastify: '5.x',
name: 'remix-fastify',
});
module.exports.default = remixFastify;
module.exports.remixFastify = remixFastify;