vite-plugin-fastly 0.0.0
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/LICENSE +8 -0
- package/dist/plugin.d.ts +49 -0
- package/dist/plugin.js +435 -0
- package/dist/plugin.js.map +1 -0
- package/dist/runner.js +95 -0
- package/dist/runner.js.map +1 -0
- package/package.json +57 -0
- package/readme.md +29 -0
- package/types.d.ts +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2021 Fatih Aygün
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { ServerOptions } from 'http-proxy-3';
|
|
3
|
+
|
|
4
|
+
interface FastlyPluginOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A unique key to identify the plugin instance. This is required if you
|
|
7
|
+
* have multiple Fastly plugin instances in the same Vite configuration.
|
|
8
|
+
*
|
|
9
|
+
* @default "vite-plugin-fastly"
|
|
10
|
+
*/
|
|
11
|
+
uniqueName?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Vite environment name that the Fastly plugin should configure.
|
|
14
|
+
*
|
|
15
|
+
* @default "ssr"
|
|
16
|
+
*/
|
|
17
|
+
viteEnvironmentName?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Function that returns the command to build the Fastly Module Runner.
|
|
20
|
+
* The callback receives the input and output file paths.
|
|
21
|
+
*
|
|
22
|
+
* @default (input, output) => `js-compute-runtime ${input} ${output}`
|
|
23
|
+
*/
|
|
24
|
+
getRunnerBuildCommand?: (input: string, output: string) => string;
|
|
25
|
+
/**
|
|
26
|
+
* IPv4 address of the Fastly Dev Server to proxy requests to during development.
|
|
27
|
+
* If you have multiple Fastly plugin instances, make sure each instance uses
|
|
28
|
+
* a unique address.
|
|
29
|
+
*
|
|
30
|
+
* @default "127.0.0.1:7676"
|
|
31
|
+
*/
|
|
32
|
+
fastlyDevServerAddress?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Function that returns the command to launch the Fastly Dev Server.
|
|
35
|
+
* The callback receives the compiled wasm file path and the address to bind to.
|
|
36
|
+
*
|
|
37
|
+
* @default (wasmFile, address) => `fastly compute serve --file=${wasmFile} --addr=${address}`
|
|
38
|
+
*/
|
|
39
|
+
getLaunchDevServerCommand?: (wasmFile: string, address: string) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Options to pass to the HTTP proxy server.
|
|
42
|
+
*
|
|
43
|
+
* @default {}
|
|
44
|
+
*/
|
|
45
|
+
proxyOptions?: ServerOptions;
|
|
46
|
+
}
|
|
47
|
+
declare function fastly(options?: FastlyPluginOptions): Plugin[];
|
|
48
|
+
|
|
49
|
+
export { type FastlyPluginOptions, fastly };
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
// src/plugin/plugin.ts
|
|
2
|
+
import { createProxy } from "http-proxy-3";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import { execSync, spawn } from "child_process";
|
|
5
|
+
import treeKill from "tree-kill-promise";
|
|
6
|
+
|
|
7
|
+
// src/plugin/expose-environment.ts
|
|
8
|
+
function exposeEnvironment() {
|
|
9
|
+
let devServerUrl;
|
|
10
|
+
let command;
|
|
11
|
+
function getModuleContents(environmentName) {
|
|
12
|
+
const url = devServerUrl ? JSON.stringify(devServerUrl) : "undefined";
|
|
13
|
+
return `export const name = ${JSON.stringify(environmentName)}
|
|
14
|
+
export const command = ${JSON.stringify(command)}
|
|
15
|
+
export const devServerUrl = ${url}`;
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
name: "vite-plugin-fastly/expose-environment",
|
|
19
|
+
enforce: "pre",
|
|
20
|
+
resolveId: {
|
|
21
|
+
filter: {
|
|
22
|
+
id: /^vite-plugin-fastly:environment$/
|
|
23
|
+
},
|
|
24
|
+
handler(source) {
|
|
25
|
+
if (source === "vite-plugin-fastly:environment") {
|
|
26
|
+
return "\0virtual:vite-plugin-fastly:environment";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
load: {
|
|
31
|
+
filter: {
|
|
32
|
+
id: /^\0virtual:vite-plugin-fastly:environment$/
|
|
33
|
+
},
|
|
34
|
+
handler(id) {
|
|
35
|
+
if (id !== "\0virtual:vite-plugin-fastly:environment") return;
|
|
36
|
+
return getModuleContents(this.environment.name);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
configResolved(config) {
|
|
40
|
+
command = config.command;
|
|
41
|
+
},
|
|
42
|
+
configureServer(server) {
|
|
43
|
+
server.httpServer?.once("listening", () => {
|
|
44
|
+
devServerUrl = server.resolvedUrls?.local[0];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/plugin/plugin.ts
|
|
51
|
+
var fastlyProcesses = /* @__PURE__ */ new Map();
|
|
52
|
+
function fastly(options = {}) {
|
|
53
|
+
const {
|
|
54
|
+
uniqueName = "vite-plugin-fastly",
|
|
55
|
+
viteEnvironmentName = "ssr",
|
|
56
|
+
fastlyDevServerAddress = "127.0.0.1:7676",
|
|
57
|
+
getRunnerBuildCommand = (input, output) => `js-compute-runtime ${input} ${output}`,
|
|
58
|
+
getLaunchDevServerCommand = (wasmFile, address) => `fastly compute serve --file=${wasmFile} --addr=${address}`,
|
|
59
|
+
proxyOptions = {}
|
|
60
|
+
} = options;
|
|
61
|
+
let command;
|
|
62
|
+
let handlerEntry;
|
|
63
|
+
let fastlyProcessKilled = false;
|
|
64
|
+
let clientConfigured = false;
|
|
65
|
+
let buildCommand;
|
|
66
|
+
return [
|
|
67
|
+
exposeEnvironment(),
|
|
68
|
+
{
|
|
69
|
+
name: uniqueName,
|
|
70
|
+
config(_, env) {
|
|
71
|
+
return {
|
|
72
|
+
environments: {
|
|
73
|
+
[viteEnvironmentName]: {
|
|
74
|
+
optimizeDeps: {
|
|
75
|
+
noDiscovery: false,
|
|
76
|
+
exclude: [
|
|
77
|
+
"fastly:acl",
|
|
78
|
+
"fastly:backend",
|
|
79
|
+
"fastly:cache",
|
|
80
|
+
"fastly:cache-override",
|
|
81
|
+
"fastly:compute",
|
|
82
|
+
"fastly:config-store",
|
|
83
|
+
"fastly:device",
|
|
84
|
+
"fastly:dictionary",
|
|
85
|
+
"fastly:edge-rate-limiter",
|
|
86
|
+
"fastly:env",
|
|
87
|
+
"fastly:experimental",
|
|
88
|
+
"fastly:fanout",
|
|
89
|
+
"fastly:geolocation",
|
|
90
|
+
"fastly:html-rewriter",
|
|
91
|
+
"fastly:image-optimizer",
|
|
92
|
+
"fastly:kv-store",
|
|
93
|
+
"fastly:logger",
|
|
94
|
+
"fastly:secret-store",
|
|
95
|
+
"fastly:websocket"
|
|
96
|
+
],
|
|
97
|
+
esbuildOptions: {
|
|
98
|
+
platform: "neutral",
|
|
99
|
+
minify: true,
|
|
100
|
+
define: {
|
|
101
|
+
"process.env.NODE_ENV": JSON.stringify(env.mode)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
resolve: {
|
|
106
|
+
builtins: [/^fastly:/],
|
|
107
|
+
noExternal: true,
|
|
108
|
+
conditions: ["fastly", "workerd"]
|
|
109
|
+
},
|
|
110
|
+
build: {
|
|
111
|
+
rollupOptions: {
|
|
112
|
+
output: {
|
|
113
|
+
inlineDynamicImports: true
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
dev: {}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
api: {
|
|
123
|
+
fastly: {
|
|
124
|
+
address: fastlyDevServerAddress
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
configResolved(config) {
|
|
128
|
+
command = config.command;
|
|
129
|
+
if (command !== "serve") return;
|
|
130
|
+
let sameNameInstanceCount = 0;
|
|
131
|
+
let sameAddressInstanceCount = 0;
|
|
132
|
+
for (const plugin of config.plugins) {
|
|
133
|
+
if (plugin.name === uniqueName) {
|
|
134
|
+
sameNameInstanceCount++;
|
|
135
|
+
}
|
|
136
|
+
if (plugin.api?.fastly?.address === fastlyDevServerAddress) {
|
|
137
|
+
sameAddressInstanceCount++;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (sameNameInstanceCount > 1) {
|
|
141
|
+
console.error(
|
|
142
|
+
`[vite-plugin-fastly] Multiple Fastly plugin instances with the same uniqueName "${uniqueName}" detected.`
|
|
143
|
+
);
|
|
144
|
+
console.error(
|
|
145
|
+
`[vite-plugin-fastly] If you really need multiple instances, give each instance a unique "uniqueName".`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
if (sameAddressInstanceCount > 1) {
|
|
149
|
+
console.error(
|
|
150
|
+
`[vite-plugin-fastly] Multiple Fastly plugin instances configured to use the same Fastly Dev Server address "${fastlyDevServerAddress}".`
|
|
151
|
+
);
|
|
152
|
+
console.error(
|
|
153
|
+
`[vite-plugin-fastly] If you really need multiple instances, give each instance a unique "fastlyDevServerAddress"`
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (sameNameInstanceCount > 1 || sameAddressInstanceCount > 1) {
|
|
157
|
+
process.exit(1);
|
|
158
|
+
}
|
|
159
|
+
const clientInput = config.environments.client?.build.rollupOptions.input;
|
|
160
|
+
if (typeof clientInput === "string") {
|
|
161
|
+
clientConfigured = true;
|
|
162
|
+
} else if (Array.isArray(clientInput) && clientInput.length > 0) {
|
|
163
|
+
clientConfigured = true;
|
|
164
|
+
} else if (typeof clientInput === "object" && clientInput !== null && Object.keys(clientInput).length > 0) {
|
|
165
|
+
clientConfigured = true;
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
async configEnvironment(name, config, env) {
|
|
169
|
+
if (name !== viteEnvironmentName || env.command !== "serve") {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const input = config.build?.rollupOptions?.input;
|
|
173
|
+
if (typeof input === "string") {
|
|
174
|
+
handlerEntry = input;
|
|
175
|
+
} else if (Array.isArray(input)) {
|
|
176
|
+
handlerEntry = input[0];
|
|
177
|
+
} else if (typeof input === "object" && input !== null) {
|
|
178
|
+
const values = Object.values(input);
|
|
179
|
+
handlerEntry = values[0];
|
|
180
|
+
}
|
|
181
|
+
if (!handlerEntry) {
|
|
182
|
+
return this.error(
|
|
183
|
+
`[${uniqueName}] No entry point found in Rollup options. Please specify an input in environments.${viteEnvironmentName}.build.rollupOptions.input.`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
fastlyProcessKilled = true;
|
|
187
|
+
await killProcessAndChildren(this, uniqueName);
|
|
188
|
+
const wasmFile = buildDevRunnerIfNecessary(
|
|
189
|
+
this,
|
|
190
|
+
uniqueName,
|
|
191
|
+
getRunnerBuildCommand
|
|
192
|
+
);
|
|
193
|
+
const launchCommand = getLaunchDevServerCommand(
|
|
194
|
+
wasmFile,
|
|
195
|
+
fastlyDevServerAddress
|
|
196
|
+
);
|
|
197
|
+
this.info(`[${uniqueName}] Launching Fastly Dev Server with command:`);
|
|
198
|
+
this.info(`[${uniqueName}] ${launchCommand}`);
|
|
199
|
+
const fastlyDevServerProcess = spawn(launchCommand, {
|
|
200
|
+
shell: true,
|
|
201
|
+
stdio: "inherit"
|
|
202
|
+
});
|
|
203
|
+
fastlyProcesses.set(uniqueName, fastlyDevServerProcess);
|
|
204
|
+
await new Promise((resolve, reject) => {
|
|
205
|
+
const checkInterval = 500;
|
|
206
|
+
const maxAttempts = 60;
|
|
207
|
+
let attempts = 0;
|
|
208
|
+
const interval = setInterval(async () => {
|
|
209
|
+
attempts++;
|
|
210
|
+
if (attempts > maxAttempts) {
|
|
211
|
+
clearInterval(interval);
|
|
212
|
+
reject(
|
|
213
|
+
new Error(
|
|
214
|
+
`[${uniqueName}] Fastly Dev Server did not become ready in time.`
|
|
215
|
+
)
|
|
216
|
+
);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetch(
|
|
221
|
+
`http://${fastlyDevServerAddress}/@vite-plugin-fastly/ready`
|
|
222
|
+
);
|
|
223
|
+
if (response.ok) {
|
|
224
|
+
clearInterval(interval);
|
|
225
|
+
resolve();
|
|
226
|
+
}
|
|
227
|
+
} catch {
|
|
228
|
+
}
|
|
229
|
+
}, checkInterval);
|
|
230
|
+
});
|
|
231
|
+
this.info(
|
|
232
|
+
`[${uniqueName}] Fastly Dev Server is running at http://${fastlyDevServerAddress}`
|
|
233
|
+
);
|
|
234
|
+
},
|
|
235
|
+
async writeBundle(outputOptions, bundle) {
|
|
236
|
+
if (command !== "build" || this.environment.name !== viteEnvironmentName) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const jsFile = Object.values(bundle).find(
|
|
240
|
+
(file) => file.type === "chunk" && file.isEntry
|
|
241
|
+
);
|
|
242
|
+
if (!jsFile) {
|
|
243
|
+
throw new Error(`[${uniqueName}] No JS entry chunk found in bundle.`);
|
|
244
|
+
}
|
|
245
|
+
const input = outputOptions.dir + "/" + jsFile.fileName;
|
|
246
|
+
const output = outputOptions.dir + "/app.wasm";
|
|
247
|
+
buildCommand = `js-compute-runtime ${input} ${output}`;
|
|
248
|
+
},
|
|
249
|
+
async closeBundle(error) {
|
|
250
|
+
if (error || command !== "build" || this.environment.name !== viteEnvironmentName) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
this.info(`Building Fastly WASM module with command:`);
|
|
254
|
+
this.info(buildCommand);
|
|
255
|
+
execSync(buildCommand, { stdio: "inherit" });
|
|
256
|
+
},
|
|
257
|
+
async buildEnd() {
|
|
258
|
+
if (command === "build" || fastlyProcessKilled) return;
|
|
259
|
+
fastlyProcessKilled = true;
|
|
260
|
+
await killProcessAndChildren(this, uniqueName);
|
|
261
|
+
},
|
|
262
|
+
configureServer(server) {
|
|
263
|
+
const environment = server.environments[viteEnvironmentName];
|
|
264
|
+
let address = null;
|
|
265
|
+
server.httpServer?.on("listening", () => {
|
|
266
|
+
address = getServerAddress(server.httpServer);
|
|
267
|
+
});
|
|
268
|
+
server.httpServer?.on("close", () => {
|
|
269
|
+
address = null;
|
|
270
|
+
});
|
|
271
|
+
server.middlewares.use((req, res, next) => {
|
|
272
|
+
if (req.method !== "POST" || req.url !== "/@vite-plugin-fastly/transport") {
|
|
273
|
+
return next();
|
|
274
|
+
}
|
|
275
|
+
void (async () => {
|
|
276
|
+
try {
|
|
277
|
+
const body = await readyBody(req);
|
|
278
|
+
const data = JSON.parse(body);
|
|
279
|
+
const result = await environment.hot.handleInvoke(data);
|
|
280
|
+
res.setHeader("Content-Type", "application/json");
|
|
281
|
+
res.end(JSON.stringify(result));
|
|
282
|
+
} catch (error) {
|
|
283
|
+
return next(error);
|
|
284
|
+
}
|
|
285
|
+
})();
|
|
286
|
+
});
|
|
287
|
+
const proxy = createProxy(proxyOptions);
|
|
288
|
+
const proxyMiddleware = (req, res, next) => {
|
|
289
|
+
if (!address) {
|
|
290
|
+
return next(
|
|
291
|
+
new Error(
|
|
292
|
+
`[${uniqueName}] Vite server address is not available for proxying`
|
|
293
|
+
)
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
try {
|
|
297
|
+
proxy.web(
|
|
298
|
+
req,
|
|
299
|
+
res,
|
|
300
|
+
{
|
|
301
|
+
target: `http://${fastlyDevServerAddress}`,
|
|
302
|
+
headers: {
|
|
303
|
+
"Vite-Plugin-Fastly-Vite-Server-Address": address,
|
|
304
|
+
"Vite-Plugin-Fastly-Handler-Entry": handlerEntry
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
(error) => {
|
|
308
|
+
next(error);
|
|
309
|
+
}
|
|
310
|
+
);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
next(error);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
if (clientConfigured) {
|
|
316
|
+
return () => {
|
|
317
|
+
server.middlewares.use(proxyMiddleware);
|
|
318
|
+
};
|
|
319
|
+
} else {
|
|
320
|
+
server.middlewares.use(proxyMiddleware);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
async function readyBody(req) {
|
|
327
|
+
return new Promise((resolve, reject) => {
|
|
328
|
+
let body = "";
|
|
329
|
+
req.on("data", (chunk) => {
|
|
330
|
+
body += chunk;
|
|
331
|
+
});
|
|
332
|
+
req.on("end", () => {
|
|
333
|
+
resolve(body);
|
|
334
|
+
});
|
|
335
|
+
req.on("error", (err) => {
|
|
336
|
+
reject(err);
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
function getServerAddress(server) {
|
|
341
|
+
const address = server?.address();
|
|
342
|
+
if (!address) {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
let host;
|
|
346
|
+
if (typeof address === "string") {
|
|
347
|
+
host = address;
|
|
348
|
+
} else {
|
|
349
|
+
switch (address.address) {
|
|
350
|
+
case "127.0.0.1":
|
|
351
|
+
case "::":
|
|
352
|
+
case "::1":
|
|
353
|
+
case "0000:0000:0000:0000:0000:0000:0000:0001":
|
|
354
|
+
host = "localhost";
|
|
355
|
+
break;
|
|
356
|
+
default:
|
|
357
|
+
host = address.address;
|
|
358
|
+
}
|
|
359
|
+
host = `http://${host}:${address.port}`;
|
|
360
|
+
}
|
|
361
|
+
return host;
|
|
362
|
+
}
|
|
363
|
+
function buildDevRunnerIfNecessary(ctx, uniqueName, getCommand) {
|
|
364
|
+
const jsComputeRuntimeVersion = getJsComputeRuntimeVersion(uniqueName);
|
|
365
|
+
const input = "node_modules/vite-plugin-fastly/dist/runner.js";
|
|
366
|
+
const output = `node_modules/.vite-plugin-fastly/runner.${jsComputeRuntimeVersion}.wasm`;
|
|
367
|
+
const inputStat = fs.statSync(input);
|
|
368
|
+
const outputStat = statOrNull(output);
|
|
369
|
+
if (outputStat && outputStat.isFile() && outputStat.mtimeMs >= inputStat.mtimeMs) {
|
|
370
|
+
return output;
|
|
371
|
+
}
|
|
372
|
+
ctx.info(`[${uniqueName}] Building Fastly Module Runner for dev server...`);
|
|
373
|
+
execSync(getCommand(input, output), { stdio: "inherit" });
|
|
374
|
+
return output;
|
|
375
|
+
}
|
|
376
|
+
function getJsComputeRuntimeVersion(uniqueName) {
|
|
377
|
+
const output = execSync("js-compute-runtime --version").toString();
|
|
378
|
+
const lastSpaceIndex = output.lastIndexOf(" ");
|
|
379
|
+
if (lastSpaceIndex === -1) {
|
|
380
|
+
throw new Error(
|
|
381
|
+
`[${uniqueName}] Unexpected js-compute-runtime version output: ${output}`
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
return output.slice(lastSpaceIndex + 1).trim();
|
|
385
|
+
}
|
|
386
|
+
function statOrNull(path) {
|
|
387
|
+
try {
|
|
388
|
+
return fs.statSync(path);
|
|
389
|
+
} catch {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async function killProcessAndChildren(ctx, name) {
|
|
394
|
+
const proc = fastlyProcesses.get(name);
|
|
395
|
+
if (!proc) return;
|
|
396
|
+
fastlyProcesses.delete(name);
|
|
397
|
+
const pid = proc.pid;
|
|
398
|
+
if (!pid || proc.exitCode) return;
|
|
399
|
+
ctx.info(`
|
|
400
|
+
[${name}] Shutting down Fastly dev server`);
|
|
401
|
+
await treeKill(pid, "SIGINT");
|
|
402
|
+
await new Promise((resolve) => {
|
|
403
|
+
proc.on("exit", () => resolve());
|
|
404
|
+
proc.on("error", () => resolve());
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
var killAllInProgress = false;
|
|
408
|
+
async function killAll() {
|
|
409
|
+
killAllInProgress = true;
|
|
410
|
+
const promises = [];
|
|
411
|
+
for (const name of fastlyProcesses.keys()) {
|
|
412
|
+
promises.push(
|
|
413
|
+
killProcessAndChildren(
|
|
414
|
+
{
|
|
415
|
+
info: (msg) => {
|
|
416
|
+
process.stdout.write(msg + "\n");
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
name
|
|
420
|
+
)
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
await Promise.all(promises);
|
|
424
|
+
}
|
|
425
|
+
process.on("SIGINT", async () => {
|
|
426
|
+
if (!killAllInProgress) {
|
|
427
|
+
void killAll().finally(() => {
|
|
428
|
+
process.exit(0);
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
export {
|
|
433
|
+
fastly
|
|
434
|
+
};
|
|
435
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin/plugin.ts","../src/plugin/expose-environment.ts"],"sourcesContent":["import type { ConfigPluginContext, Connect, HttpServer, Plugin } from \"vite\";\nimport { createProxy, type ServerOptions } from \"http-proxy-3\";\nimport type { IncomingMessage } from \"node:http\";\nimport fs from \"node:fs\";\nimport { ChildProcess, execSync, spawn } from \"node:child_process\";\nimport treeKill from \"tree-kill-promise\";\nimport exposeEnvironment from \"./expose-environment\";\n\nexport interface FastlyPluginOptions {\n\t/**\n\t * A unique key to identify the plugin instance. This is required if you\n\t * have multiple Fastly plugin instances in the same Vite configuration.\n\t *\n\t * @default \"vite-plugin-fastly\"\n\t */\n\tuniqueName?: string;\n\n\t/**\n\t * Vite environment name that the Fastly plugin should configure.\n\t *\n\t * @default \"ssr\"\n\t */\n\tviteEnvironmentName?: string;\n\n\t/**\n\t * Function that returns the command to build the Fastly Module Runner.\n\t * The callback receives the input and output file paths.\n\t *\n\t * @default (input, output) => `js-compute-runtime ${input} ${output}`\n\t */\n\tgetRunnerBuildCommand?: (input: string, output: string) => string;\n\n\t/**\n\t * IPv4 address of the Fastly Dev Server to proxy requests to during development.\n\t * If you have multiple Fastly plugin instances, make sure each instance uses\n\t * a unique address.\n\t *\n\t * @default \"127.0.0.1:7676\"\n\t */\n\tfastlyDevServerAddress?: string;\n\n\t/**\n\t * Function that returns the command to launch the Fastly Dev Server.\n\t * The callback receives the compiled wasm file path and the address to bind to.\n\t *\n\t * @default (wasmFile, address) => `fastly compute serve --file=${wasmFile} --addr=${address}`\n\t */\n\tgetLaunchDevServerCommand?: (wasmFile: string, address: string) => string;\n\n\t/**\n\t * Options to pass to the HTTP proxy server.\n\t *\n\t * @default {}\n\t */\n\tproxyOptions?: ServerOptions;\n}\n\n// Map of uniqueName to Fastly Dev Server child processes\nconst fastlyProcesses = new Map<string, ChildProcess>();\n\nexport function fastly(options: FastlyPluginOptions = {}): Plugin[] {\n\tconst {\n\t\tuniqueName = \"vite-plugin-fastly\",\n\t\tviteEnvironmentName = \"ssr\",\n\t\tfastlyDevServerAddress = \"127.0.0.1:7676\",\n\t\tgetRunnerBuildCommand = (input: string, output: string) =>\n\t\t\t`js-compute-runtime ${input} ${output}`,\n\t\tgetLaunchDevServerCommand = (wasmFile, address) =>\n\t\t\t`fastly compute serve --file=${wasmFile} --addr=${address}`,\n\t\tproxyOptions = {},\n\t} = options;\n\n\tlet command: \"serve\" | \"build\";\n\tlet handlerEntry: string | undefined;\n\tlet fastlyProcessKilled = false;\n\tlet clientConfigured = false;\n\tlet buildCommand: string | undefined;\n\n\treturn [\n\t\texposeEnvironment(),\n\t\t{\n\t\t\tname: uniqueName,\n\n\t\t\tconfig(_, env) {\n\t\t\t\treturn {\n\t\t\t\t\tenvironments: {\n\t\t\t\t\t\t[viteEnvironmentName]: {\n\t\t\t\t\t\t\toptimizeDeps: {\n\t\t\t\t\t\t\t\tnoDiscovery: false,\n\t\t\t\t\t\t\t\texclude: [\n\t\t\t\t\t\t\t\t\t\"fastly:acl\",\n\t\t\t\t\t\t\t\t\t\"fastly:backend\",\n\t\t\t\t\t\t\t\t\t\"fastly:cache\",\n\t\t\t\t\t\t\t\t\t\"fastly:cache-override\",\n\t\t\t\t\t\t\t\t\t\"fastly:compute\",\n\t\t\t\t\t\t\t\t\t\"fastly:config-store\",\n\t\t\t\t\t\t\t\t\t\"fastly:device\",\n\t\t\t\t\t\t\t\t\t\"fastly:dictionary\",\n\t\t\t\t\t\t\t\t\t\"fastly:edge-rate-limiter\",\n\t\t\t\t\t\t\t\t\t\"fastly:env\",\n\t\t\t\t\t\t\t\t\t\"fastly:experimental\",\n\t\t\t\t\t\t\t\t\t\"fastly:fanout\",\n\t\t\t\t\t\t\t\t\t\"fastly:geolocation\",\n\t\t\t\t\t\t\t\t\t\"fastly:html-rewriter\",\n\t\t\t\t\t\t\t\t\t\"fastly:image-optimizer\",\n\t\t\t\t\t\t\t\t\t\"fastly:kv-store\",\n\t\t\t\t\t\t\t\t\t\"fastly:logger\",\n\t\t\t\t\t\t\t\t\t\"fastly:secret-store\",\n\t\t\t\t\t\t\t\t\t\"fastly:websocket\",\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tesbuildOptions: {\n\t\t\t\t\t\t\t\t\tplatform: \"neutral\",\n\t\t\t\t\t\t\t\t\tminify: true,\n\t\t\t\t\t\t\t\t\tdefine: {\n\t\t\t\t\t\t\t\t\t\t\"process.env.NODE_ENV\": JSON.stringify(env.mode),\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tresolve: {\n\t\t\t\t\t\t\t\tbuiltins: [/^fastly:/],\n\t\t\t\t\t\t\t\tnoExternal: true,\n\t\t\t\t\t\t\t\tconditions: [\"fastly\", \"workerd\"],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tbuild: {\n\t\t\t\t\t\t\t\trollupOptions: {\n\t\t\t\t\t\t\t\t\toutput: {\n\t\t\t\t\t\t\t\t\t\tinlineDynamicImports: true,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tdev: {},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\n\t\t\tapi: {\n\t\t\t\tfastly: {\n\t\t\t\t\taddress: fastlyDevServerAddress,\n\t\t\t\t},\n\t\t\t},\n\n\t\t\tconfigResolved(config) {\n\t\t\t\tcommand = config.command;\n\t\t\t\tif (command !== \"serve\") return;\n\n\t\t\t\t// Scan plugins for multiple instances with the same uniqueName\n\t\t\t\tlet sameNameInstanceCount = 0;\n\t\t\t\tlet sameAddressInstanceCount = 0;\n\t\t\t\tfor (const plugin of config.plugins) {\n\t\t\t\t\tif (plugin.name === uniqueName) {\n\t\t\t\t\t\tsameNameInstanceCount++;\n\t\t\t\t\t}\n\t\t\t\t\tif (plugin.api?.fastly?.address === fastlyDevServerAddress) {\n\t\t\t\t\t\tsameAddressInstanceCount++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (sameNameInstanceCount > 1) {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`[vite-plugin-fastly] Multiple Fastly plugin instances with the same uniqueName \"${uniqueName}\" detected.`,\n\t\t\t\t\t);\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`[vite-plugin-fastly] If you really need multiple instances, give each instance a unique \"uniqueName\".`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tif (sameAddressInstanceCount > 1) {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`[vite-plugin-fastly] Multiple Fastly plugin instances configured to use the same Fastly Dev Server address \"${fastlyDevServerAddress}\".`,\n\t\t\t\t\t);\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`[vite-plugin-fastly] If you really need multiple instances, give each instance a unique \"fastlyDevServerAddress\"`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tif (sameNameInstanceCount > 1 || sameAddressInstanceCount > 1) {\n\t\t\t\t\t// This is a fatal error, we cannot continue\n\t\t\t\t\tprocess.exit(1);\n\t\t\t\t}\n\n\t\t\t\tconst clientInput =\n\t\t\t\t\tconfig.environments.client?.build.rollupOptions.input;\n\t\t\t\tif (typeof clientInput === \"string\") {\n\t\t\t\t\tclientConfigured = true;\n\t\t\t\t} else if (Array.isArray(clientInput) && clientInput.length > 0) {\n\t\t\t\t\tclientConfigured = true;\n\t\t\t\t} else if (\n\t\t\t\t\ttypeof clientInput === \"object\" &&\n\t\t\t\t\tclientInput !== null &&\n\t\t\t\t\tObject.keys(clientInput).length > 0\n\t\t\t\t) {\n\t\t\t\t\tclientConfigured = true;\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync configEnvironment(name, config, env) {\n\t\t\t\tif (name !== viteEnvironmentName || env.command !== \"serve\") {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst input = config.build?.rollupOptions?.input;\n\t\t\t\tif (typeof input === \"string\") {\n\t\t\t\t\thandlerEntry = input;\n\t\t\t\t} else if (Array.isArray(input)) {\n\t\t\t\t\thandlerEntry = input[0];\n\t\t\t\t} else if (typeof input === \"object\" && input !== null) {\n\t\t\t\t\tconst values = Object.values(input);\n\t\t\t\t\thandlerEntry = values[0];\n\t\t\t\t}\n\n\t\t\t\tif (!handlerEntry) {\n\t\t\t\t\treturn this.error(\n\t\t\t\t\t\t`[${uniqueName}] No entry point found in Rollup options. Please specify an input in environments.${viteEnvironmentName}.build.rollupOptions.input.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// Kill previous Fastly Dev Server process if any\n\t\t\t\tfastlyProcessKilled = true;\n\t\t\t\tawait killProcessAndChildren(this, uniqueName);\n\n\t\t\t\tconst wasmFile = buildDevRunnerIfNecessary(\n\t\t\t\t\tthis,\n\t\t\t\t\tuniqueName,\n\t\t\t\t\tgetRunnerBuildCommand,\n\t\t\t\t);\n\n\t\t\t\tconst launchCommand = getLaunchDevServerCommand(\n\t\t\t\t\twasmFile,\n\t\t\t\t\tfastlyDevServerAddress,\n\t\t\t\t);\n\n\t\t\t\tthis.info(`[${uniqueName}] Launching Fastly Dev Server with command:`);\n\t\t\t\tthis.info(`[${uniqueName}] ${launchCommand}`);\n\t\t\t\tconst fastlyDevServerProcess = spawn(launchCommand, {\n\t\t\t\t\tshell: true,\n\t\t\t\t\tstdio: \"inherit\",\n\t\t\t\t});\n\n\t\t\t\tfastlyProcesses.set(uniqueName, fastlyDevServerProcess);\n\n\t\t\t\t// Wait until server is ready\n\t\t\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\t\t\tconst checkInterval = 500;\n\t\t\t\t\tconst maxAttempts = 60; // 30 seconds\n\t\t\t\t\tlet attempts = 0;\n\n\t\t\t\t\tconst interval = setInterval(async () => {\n\t\t\t\t\t\tattempts++;\n\t\t\t\t\t\tif (attempts > maxAttempts) {\n\t\t\t\t\t\t\tclearInterval(interval);\n\t\t\t\t\t\t\treject(\n\t\t\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t\t\t`[${uniqueName}] Fastly Dev Server did not become ready in time.`,\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst response = await fetch(\n\t\t\t\t\t\t\t\t`http://${fastlyDevServerAddress}/@vite-plugin-fastly/ready`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tif (response.ok) {\n\t\t\t\t\t\t\t\tclearInterval(interval);\n\t\t\t\t\t\t\t\tresolve();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// Ignore errors, server is not ready yet\n\t\t\t\t\t\t}\n\t\t\t\t\t}, checkInterval);\n\t\t\t\t});\n\n\t\t\t\tthis.info(\n\t\t\t\t\t`[${uniqueName}] Fastly Dev Server is running at http://${fastlyDevServerAddress}`,\n\t\t\t\t);\n\t\t\t},\n\n\t\t\tasync writeBundle(outputOptions, bundle) {\n\t\t\t\tif (\n\t\t\t\t\tcommand !== \"build\" ||\n\t\t\t\t\tthis.environment.name !== viteEnvironmentName\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst jsFile = Object.values(bundle).find(\n\t\t\t\t\t(file) => file.type === \"chunk\" && file.isEntry,\n\t\t\t\t);\n\t\t\t\tif (!jsFile) {\n\t\t\t\t\tthrow new Error(`[${uniqueName}] No JS entry chunk found in bundle.`);\n\t\t\t\t}\n\n\t\t\t\tconst input = outputOptions.dir + \"/\" + jsFile.fileName;\n\t\t\t\tconst output = outputOptions.dir + \"/app.wasm\";\n\n\t\t\t\tbuildCommand = `js-compute-runtime ${input} ${output}`;\n\t\t\t},\n\n\t\t\tasync closeBundle(error) {\n\t\t\t\tif (\n\t\t\t\t\terror ||\n\t\t\t\t\tcommand !== \"build\" ||\n\t\t\t\t\tthis.environment.name !== viteEnvironmentName\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tthis.info(`Building Fastly WASM module with command:`);\n\t\t\t\tthis.info(buildCommand!);\n\t\t\t\texecSync(buildCommand!, { stdio: \"inherit\" });\n\t\t\t},\n\n\t\t\tasync buildEnd() {\n\t\t\t\tif (command === \"build\" || fastlyProcessKilled) return;\n\t\t\t\tfastlyProcessKilled = true;\n\t\t\t\tawait killProcessAndChildren(this, uniqueName);\n\t\t\t},\n\n\t\t\tconfigureServer(server) {\n\t\t\t\tconst environment = server.environments[viteEnvironmentName]!;\n\t\t\t\tlet address: string | null = null;\n\n\t\t\t\tserver.httpServer?.on(\"listening\", () => {\n\t\t\t\t\taddress = getServerAddress(server.httpServer);\n\t\t\t\t});\n\n\t\t\t\tserver.httpServer?.on(\"close\", () => {\n\t\t\t\t\taddress = null;\n\t\t\t\t});\n\n\t\t\t\t// Transport endpoint for the runner\n\t\t\t\tserver.middlewares.use((req, res, next) => {\n\t\t\t\t\tif (\n\t\t\t\t\t\treq.method !== \"POST\" ||\n\t\t\t\t\t\treq.url !== \"/@vite-plugin-fastly/transport\"\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn next();\n\t\t\t\t\t}\n\n\t\t\t\t\tvoid (async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// Read the body\n\t\t\t\t\t\t\tconst body = await readyBody(req);\n\t\t\t\t\t\t\tconst data = JSON.parse(body);\n\n\t\t\t\t\t\t\tconst result = await environment.hot.handleInvoke(data);\n\n\t\t\t\t\t\t\tres.setHeader(\"Content-Type\", \"application/json\");\n\t\t\t\t\t\t\tres.end(JSON.stringify(result));\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\treturn next(error);\n\t\t\t\t\t\t}\n\t\t\t\t\t})();\n\t\t\t\t});\n\n\t\t\t\tconst proxy = createProxy(proxyOptions);\n\n\t\t\t\tconst proxyMiddleware: Connect.NextHandleFunction = (\n\t\t\t\t\treq,\n\t\t\t\t\tres,\n\t\t\t\t\tnext,\n\t\t\t\t) => {\n\t\t\t\t\tif (!address) {\n\t\t\t\t\t\treturn next(\n\t\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t\t`[${uniqueName}] Vite server address is not available for proxying`,\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tproxy.web(\n\t\t\t\t\t\t\treq,\n\t\t\t\t\t\t\tres,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttarget: `http://${fastlyDevServerAddress}`,\n\t\t\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\t\t\"Vite-Plugin-Fastly-Vite-Server-Address\": address,\n\t\t\t\t\t\t\t\t\t\"Vite-Plugin-Fastly-Handler-Entry\": handlerEntry,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t(error) => {\n\t\t\t\t\t\t\t\tnext(error);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tnext(error);\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\t// If a client build is configured, add the proxy middleware after Vite's own middlewares.\n\t\t\t\t// Otherwise, assume a pure server-side setup and add the proxy as the first middleware.\n\t\t\t\tif (clientConfigured) {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tserver.middlewares.use(proxyMiddleware);\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tserver.middlewares.use(proxyMiddleware);\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t];\n}\n\nasync function readyBody(req: IncomingMessage): Promise<string> {\n\treturn new Promise<string>((resolve, reject) => {\n\t\tlet body = \"\";\n\t\treq.on(\"data\", (chunk) => {\n\t\t\tbody += chunk;\n\t\t});\n\n\t\treq.on(\"end\", () => {\n\t\t\tresolve(body);\n\t\t});\n\n\t\treq.on(\"error\", (err) => {\n\t\t\treject(err);\n\t\t});\n\t});\n}\n\nfunction getServerAddress(server: HttpServer | null): string | null {\n\tconst address = server?.address();\n\tif (!address) {\n\t\treturn null;\n\t}\n\n\tlet host: string;\n\n\tif (typeof address === \"string\") {\n\t\thost = address;\n\t} else {\n\t\tswitch (address.address) {\n\t\t\tcase \"127.0.0.1\":\n\t\t\tcase \"::\":\n\t\t\tcase \"::1\":\n\t\t\tcase \"0000:0000:0000:0000:0000:0000:0000:0001\":\n\t\t\t\thost = \"localhost\";\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\thost = address.address;\n\t\t}\n\n\t\thost = `http://${host}:${address.port}`;\n\t}\n\n\treturn host;\n}\n\nfunction buildDevRunnerIfNecessary(\n\tctx: ConfigPluginContext,\n\tuniqueName: string,\n\tgetCommand: (input: string, output: string) => string,\n): string {\n\tconst jsComputeRuntimeVersion = getJsComputeRuntimeVersion(uniqueName);\n\tconst input = \"node_modules/vite-plugin-fastly/dist/runner.js\";\n\tconst output = `node_modules/.vite-plugin-fastly/runner.${jsComputeRuntimeVersion}.wasm`;\n\n\tconst inputStat = fs.statSync(input);\n\tconst outputStat = statOrNull(output);\n\n\tif (\n\t\toutputStat &&\n\t\toutputStat.isFile() &&\n\t\toutputStat.mtimeMs >= inputStat.mtimeMs\n\t) {\n\t\t// Up to date\n\t\treturn output;\n\t}\n\n\tctx.info(`[${uniqueName}] Building Fastly Module Runner for dev server...`);\n\n\texecSync(getCommand(input, output), { stdio: \"inherit\" });\n\n\treturn output;\n}\n\nfunction getJsComputeRuntimeVersion(uniqueName: string): string {\n\tconst output = execSync(\"js-compute-runtime --version\").toString();\n\n\t// Parse version from something like \"js-compute-runtime-cli.js 3.38.2\\n\"\n\tconst lastSpaceIndex = output.lastIndexOf(\" \");\n\tif (lastSpaceIndex === -1) {\n\t\tthrow new Error(\n\t\t\t`[${uniqueName}] Unexpected js-compute-runtime version output: ${output}`,\n\t\t);\n\t}\n\n\treturn output.slice(lastSpaceIndex + 1).trim();\n}\n\nfunction statOrNull(path: string): fs.Stats | null {\n\ttry {\n\t\treturn fs.statSync(path);\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nasync function killProcessAndChildren(\n\tctx: {\n\t\tinfo: (msg: string) => void;\n\t},\n\tname: string,\n) {\n\tconst proc = fastlyProcesses.get(name);\n\tif (!proc) return;\n\n\tfastlyProcesses.delete(name);\n\n\tconst pid = proc.pid;\n\tif (!pid || proc.exitCode) return;\n\n\tctx.info(`\\n[${name}] Shutting down Fastly dev server`);\n\n\tawait treeKill(pid, \"SIGINT\");\n\n\tawait new Promise<void>((resolve) => {\n\t\tproc.on(\"exit\", () => resolve());\n\t\tproc.on(\"error\", () => resolve());\n\t});\n}\n\nlet killAllInProgress = false;\n\nasync function killAll() {\n\tkillAllInProgress = true;\n\tconst promises: Promise<void>[] = [];\n\tfor (const name of fastlyProcesses.keys()) {\n\t\tpromises.push(\n\t\t\tkillProcessAndChildren(\n\t\t\t\t{\n\t\t\t\t\tinfo: (msg: string) => {\n\t\t\t\t\t\tprocess.stdout.write(msg + \"\\n\");\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tname,\n\t\t\t),\n\t\t);\n\t}\n\tawait Promise.all(promises);\n}\n\nprocess.on(\"SIGINT\", async () => {\n\tif (!killAllInProgress) {\n\t\tvoid killAll().finally(() => {\n\t\t\tprocess.exit(0);\n\t\t});\n\t}\n});\n","import type { Plugin } from \"vite\";\n\nexport default function exposeEnvironment(): Plugin {\n\tlet devServerUrl: string | undefined;\n\tlet command: \"serve\" | \"build\";\n\n\tfunction getModuleContents(environmentName: string) {\n\t\tconst url = devServerUrl ? JSON.stringify(devServerUrl) : \"undefined\";\n\n\t\treturn (\n\t\t\t`export const name = ${JSON.stringify(environmentName)}\\n` +\n\t\t\t`export const command = ${JSON.stringify(command)}\\n` +\n\t\t\t`export const devServerUrl = ${url}`\n\t\t);\n\t}\n\n\treturn {\n\t\tname: \"vite-plugin-fastly/expose-environment\",\n\n\t\tenforce: \"pre\",\n\n\t\tresolveId: {\n\t\t\tfilter: {\n\t\t\t\tid: /^vite-plugin-fastly:environment$/,\n\t\t\t},\n\t\t\thandler(source) {\n\t\t\t\tif (source === \"vite-plugin-fastly:environment\") {\n\t\t\t\t\treturn \"\\0virtual:vite-plugin-fastly:environment\";\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\n\t\tload: {\n\t\t\tfilter: {\n\t\t\t\tid: /^\\0virtual:vite-plugin-fastly:environment$/,\n\t\t\t},\n\t\t\thandler(id) {\n\t\t\t\tif (id !== \"\\0virtual:vite-plugin-fastly:environment\") return;\n\t\t\t\treturn getModuleContents(this.environment.name);\n\t\t\t},\n\t\t},\n\n\t\tconfigResolved(config) {\n\t\t\tcommand = config.command;\n\t\t},\n\n\t\tconfigureServer(server) {\n\t\t\tserver.httpServer?.once(\"listening\", () => {\n\t\t\t\tdevServerUrl = server.resolvedUrls?.local[0];\n\t\t\t});\n\t\t},\n\t} satisfies Plugin;\n}\n"],"mappings":";AACA,SAAS,mBAAuC;AAEhD,OAAO,QAAQ;AACf,SAAuB,UAAU,aAAa;AAC9C,OAAO,cAAc;;;ACHN,SAAR,oBAA6C;AACnD,MAAI;AACJ,MAAI;AAEJ,WAAS,kBAAkB,iBAAyB;AACnD,UAAM,MAAM,eAAe,KAAK,UAAU,YAAY,IAAI;AAE1D,WACC,uBAAuB,KAAK,UAAU,eAAe,CAAC;AAAA,yBAC5B,KAAK,UAAU,OAAO,CAAC;AAAA,8BAClB,GAAG;AAAA,EAEpC;AAEA,SAAO;AAAA,IACN,MAAM;AAAA,IAEN,SAAS;AAAA,IAET,WAAW;AAAA,MACV,QAAQ;AAAA,QACP,IAAI;AAAA,MACL;AAAA,MACA,QAAQ,QAAQ;AACf,YAAI,WAAW,kCAAkC;AAChD,iBAAO;AAAA,QACR;AAAA,MACD;AAAA,IACD;AAAA,IAEA,MAAM;AAAA,MACL,QAAQ;AAAA,QACP,IAAI;AAAA,MACL;AAAA,MACA,QAAQ,IAAI;AACX,YAAI,OAAO,2CAA4C;AACvD,eAAO,kBAAkB,KAAK,YAAY,IAAI;AAAA,MAC/C;AAAA,IACD;AAAA,IAEA,eAAe,QAAQ;AACtB,gBAAU,OAAO;AAAA,IAClB;AAAA,IAEA,gBAAgB,QAAQ;AACvB,aAAO,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Ae,uBACd,KAGA,MACC;AACD,QAAM,OAAO,gBAAgB,IAAI,IAAI;AACrC,MAAI,CAAC,KAAM;AAEX,kBAAgB,OAAO,IAAI;AAE3B,QAAM,MAAM,KAAK;AACjB,MAAI,CAAC,OAAO,KAAK,SAAU;AAE3B,MAAI,KAAK;AAAA,GAAM,IAAI,mCAAmC;AAEtD,QAAM,SAAS,KAAK,QAAQ;AAE5B,QAAM,IAAI,QAAc,CAAC,YAAY;AACpC,SAAK,GAAG,QAAQ,MAAM,QAAQ,CAAC;AAC/B,SAAK,GAAG,SAAS,MAAM,QAAQ,CAAC;AAAA,EACjC,CAAC;AACF;AAEA,IAAI,oBAAoB;AAExB,eAAe,UAAU;AACxB,sBAAoB;AACpB,QAAM,WAA4B,CAAC;AACnC,aAAW,QAAQ,gBAAgB,KAAK,GAAG;AAC1C,aAAS;AAAA,MACR;AAAA,QACC;AAAA,UACC,MAAM,CAAC,QAAgB;AACtB,oBAAQ,OAAO,MAAM,MAAM,IAAI;AAAA,UAChC;AAAA,QACD;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACA,QAAM,QAAQ,IAAI,QAAQ;AAC3B;AAEA,QAAQ,GAAG,UAAU,YAAY;AAChC,MAAI,CAAC,mBAAmB;AACvB,SAAK,QAAQ,EAAE,QAAQ,MAAM;AAC5B,cAAQ,KAAK,CAAC;AAAA,IACf,CAAC;AAAA,EACF;AACD,CAAC;","names":[]}
|
package/dist/runner.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/runner/runner.ts
|
|
2
|
+
import { ESModulesEvaluator, ModuleRunner } from "vite/module-runner";
|
|
3
|
+
var evaluator = new ESModulesEvaluator();
|
|
4
|
+
evaluator.runExternalModule = async (filepath) => {
|
|
5
|
+
switch (filepath) {
|
|
6
|
+
case "fastly:acl":
|
|
7
|
+
return await import("fastly:acl");
|
|
8
|
+
case "fastly:backend":
|
|
9
|
+
return await import("fastly:backend");
|
|
10
|
+
case "fastly:cache":
|
|
11
|
+
return await import("fastly:cache");
|
|
12
|
+
case "fastly:cache-override":
|
|
13
|
+
return await import("fastly:cache-override");
|
|
14
|
+
case "fastly:compute":
|
|
15
|
+
return await import("fastly:compute");
|
|
16
|
+
case "fastly:config-store":
|
|
17
|
+
return await import("fastly:config-store");
|
|
18
|
+
case "fastly:device":
|
|
19
|
+
return await import("fastly:device");
|
|
20
|
+
case "fastly:dictionary":
|
|
21
|
+
return await import("fastly:dictionary");
|
|
22
|
+
case "fastly:edge-rate-limiter":
|
|
23
|
+
return await import("fastly:edge-rate-limiter");
|
|
24
|
+
case "fastly:env":
|
|
25
|
+
return await import("fastly:env");
|
|
26
|
+
case "fastly:experimental":
|
|
27
|
+
return await import("fastly:experimental");
|
|
28
|
+
case "fastly:fanout":
|
|
29
|
+
return await import("fastly:fanout");
|
|
30
|
+
case "fastly:geolocation":
|
|
31
|
+
return await import("fastly:geolocation");
|
|
32
|
+
case "fastly:html-rewriter":
|
|
33
|
+
return await import("fastly:html-rewriter");
|
|
34
|
+
case "fastly:image-optimizer":
|
|
35
|
+
return await import("fastly:image-optimizer");
|
|
36
|
+
case "fastly:kv-store":
|
|
37
|
+
return await import("fastly:kv-store");
|
|
38
|
+
case "fastly:logger":
|
|
39
|
+
return await import("fastly:logger");
|
|
40
|
+
case "fastly:secret-store":
|
|
41
|
+
return await import("fastly:secret-store");
|
|
42
|
+
case "fastly:websocket":
|
|
43
|
+
return await import("fastly:websocket");
|
|
44
|
+
default:
|
|
45
|
+
throw new Error("Unknown external module: " + filepath);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var runner = new ModuleRunner(
|
|
49
|
+
{
|
|
50
|
+
hmr: false,
|
|
51
|
+
transport: {
|
|
52
|
+
async invoke(data) {
|
|
53
|
+
return fetch(`${viteServerAddress}/@vite-plugin-fastly/transport`, {
|
|
54
|
+
method: "POST",
|
|
55
|
+
body: JSON.stringify(data)
|
|
56
|
+
}).then((r) => {
|
|
57
|
+
if (!r.ok) {
|
|
58
|
+
return { error: new Error(`Transport error ${r.status}`) };
|
|
59
|
+
}
|
|
60
|
+
return r.json();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
evaluator
|
|
66
|
+
);
|
|
67
|
+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
|
|
68
|
+
var viteServerAddress;
|
|
69
|
+
async function handleRequest(event) {
|
|
70
|
+
const request = event.request;
|
|
71
|
+
if (request.method === "GET") {
|
|
72
|
+
const url = new URL(request.url);
|
|
73
|
+
const path = url.pathname;
|
|
74
|
+
if (path === "/@vite-plugin-fastly/ready") {
|
|
75
|
+
return new Response("OK", { status: 200 });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
viteServerAddress ||= request.headers.get(
|
|
79
|
+
"Vite-Plugin-Fastly-Vite-Server-Address"
|
|
80
|
+
);
|
|
81
|
+
const handlerEntry = request.headers.get("Vite-Plugin-Fastly-Handler-Entry");
|
|
82
|
+
if (!viteServerAddress) {
|
|
83
|
+
return new Response("Vite server address header is missing", {
|
|
84
|
+
status: 500
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (!handlerEntry) {
|
|
88
|
+
return new Response("Handler entry header is missing", {
|
|
89
|
+
status: 500
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const module = await runner.import(handlerEntry);
|
|
93
|
+
return await module.default(event);
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/runner/runner.ts"],"sourcesContent":["import { ESModulesEvaluator, ModuleRunner } from \"vite/module-runner\";\n\nconst evaluator = new ESModulesEvaluator();\n\nevaluator.runExternalModule = async (filepath) => {\n\tswitch (filepath) {\n\t\tcase \"fastly:acl\":\n\t\t\treturn await import(\"fastly:acl\");\n\t\tcase \"fastly:backend\":\n\t\t\treturn await import(\"fastly:backend\");\n\t\tcase \"fastly:cache\":\n\t\t\treturn await import(\"fastly:cache\");\n\t\tcase \"fastly:cache-override\":\n\t\t\treturn await import(\"fastly:cache-override\");\n\t\tcase \"fastly:compute\":\n\t\t\treturn await import(\"fastly:compute\");\n\t\tcase \"fastly:config-store\":\n\t\t\treturn await import(\"fastly:config-store\");\n\t\tcase \"fastly:device\":\n\t\t\treturn await import(\"fastly:device\");\n\t\tcase \"fastly:dictionary\":\n\t\t\treturn await import(\"fastly:dictionary\");\n\t\tcase \"fastly:edge-rate-limiter\":\n\t\t\treturn await import(\"fastly:edge-rate-limiter\");\n\t\tcase \"fastly:env\":\n\t\t\treturn await import(\"fastly:env\");\n\t\tcase \"fastly:experimental\":\n\t\t\treturn await import(\"fastly:experimental\");\n\t\tcase \"fastly:fanout\":\n\t\t\treturn await import(\"fastly:fanout\");\n\t\tcase \"fastly:geolocation\":\n\t\t\treturn await import(\"fastly:geolocation\");\n\t\tcase \"fastly:html-rewriter\":\n\t\t\treturn await import(\"fastly:html-rewriter\");\n\t\tcase \"fastly:image-optimizer\":\n\t\t\treturn await import(\"fastly:image-optimizer\");\n\t\tcase \"fastly:kv-store\":\n\t\t\treturn await import(\"fastly:kv-store\");\n\t\tcase \"fastly:logger\":\n\t\t\treturn await import(\"fastly:logger\");\n\t\tcase \"fastly:secret-store\":\n\t\t\treturn await import(\"fastly:secret-store\");\n\t\tcase \"fastly:websocket\":\n\t\t\treturn await import(\"fastly:websocket\");\n\n\t\tdefault:\n\t\t\tthrow new Error(\"Unknown external module: \" + filepath);\n\t}\n};\n\nconst runner = new ModuleRunner(\n\t{\n\t\thmr: false,\n\n\t\ttransport: {\n\t\t\tasync invoke(data: any) {\n\t\t\t\treturn fetch(`${viteServerAddress}/@vite-plugin-fastly/transport`, {\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: JSON.stringify(data),\n\t\t\t\t}).then((r) => {\n\t\t\t\t\tif (!r.ok) {\n\t\t\t\t\t\treturn { error: new Error(`Transport error ${r.status}`) };\n\t\t\t\t\t}\n\n\t\t\t\t\treturn r.json() as Promise<any>;\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t},\n\tevaluator,\n);\n\naddEventListener(\"fetch\", (event) => event.respondWith(handleRequest(event)));\n\nlet viteServerAddress: string;\n\nasync function handleRequest(event: FetchEvent) {\n\tconst request = event.request;\n\n\tif (request.method === \"GET\") {\n\t\tconst url = new URL(request.url);\n\t\tconst path = url.pathname;\n\t\tif (path === \"/@vite-plugin-fastly/ready\") {\n\t\t\treturn new Response(\"OK\", { status: 200 });\n\t\t}\n\t}\n\n\tviteServerAddress ||= request.headers.get(\n\t\t\"Vite-Plugin-Fastly-Vite-Server-Address\",\n\t)!;\n\n\tconst handlerEntry = request.headers.get(\"Vite-Plugin-Fastly-Handler-Entry\")!;\n\n\tif (!viteServerAddress) {\n\t\treturn new Response(\"Vite server address header is missing\", {\n\t\t\tstatus: 500,\n\t\t});\n\t}\n\n\tif (!handlerEntry) {\n\t\treturn new Response(\"Handler entry header is missing\", {\n\t\t\tstatus: 500,\n\t\t});\n\t}\n\n\tconst module = await runner.import(handlerEntry);\n\treturn await module.default(event);\n}\n"],"mappings":";AAAA,SAAS,oBAAoB,oBAAoB;AAEjD,IAAM,YAAY,IAAI,mBAAmB;AAEzC,UAAU,oBAAoB,OAAO,aAAa;AACjD,UAAQ,UAAU;AAAA,IACjB,KAAK;AACJ,aAAO,MAAM,OAAO,YAAY;AAAA,IACjC,KAAK;AACJ,aAAO,MAAM,OAAO,gBAAgB;AAAA,IACrC,KAAK;AACJ,aAAO,MAAM,OAAO,cAAc;AAAA,IACnC,KAAK;AACJ,aAAO,MAAM,OAAO,uBAAuB;AAAA,IAC5C,KAAK;AACJ,aAAO,MAAM,OAAO,gBAAgB;AAAA,IACrC,KAAK;AACJ,aAAO,MAAM,OAAO,qBAAqB;AAAA,IAC1C,KAAK;AACJ,aAAO,MAAM,OAAO,eAAe;AAAA,IACpC,KAAK;AACJ,aAAO,MAAM,OAAO,mBAAmB;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7B,UAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,UAAM,OAAO,IAAI;AACjB,QAAI,SAAS,8BAA8B;AAC1C,aAAO,IAAI,SAAS,MAAM,EAAE,QAAQ,IAAI,CAAC;AAAA,IAC1C;AAAA,EACD;AAEA,wBAAsB,QAAQ,QAAQ;AAAA,IACrC;AAAA,EACD;AAEA,QAAM,eAAe,QAAQ,QAAQ,IAAI,kCAAkC;AAE3E,MAAI,CAAC,mBAAmB;AACvB,WAAO,IAAI,SAAS,yCAAyC;AAAA,MAC5D,QAAQ;AAAA,IACT,CAAC;AAAA,EACF;AAEA,MAAI,CAAC,cAAc;AAClB,WAAO,IAAI,SAAS,mCAAmC;AAAA,MACtD,QAAQ;AAAA,IACT,CAAC;AAAA,EACF;AAEA,QAAM,SAAS,MAAM,OAAO,OAAO,YAAY;AAC/C,SAAO,MAAM,OAAO,QAAQ,KAAK;AAClC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-fastly",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"*.d.ts"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/plugin.js",
|
|
11
|
+
"./runner": "./dist/runner.js",
|
|
12
|
+
"./types": "./types.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"typesVersions": {
|
|
15
|
+
"*": {
|
|
16
|
+
"*": [
|
|
17
|
+
"dist/*.d.ts"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"description": "vite-plugin-fastly",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": "github:cyco130/vite-plugin-fastly",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"react",
|
|
26
|
+
"vite",
|
|
27
|
+
"nextjs",
|
|
28
|
+
"ssr"
|
|
29
|
+
],
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@fastly/js-compute": "3",
|
|
32
|
+
"vite": "7"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@cyco130/eslint-config": "^6.1.1",
|
|
36
|
+
"@fastly/cli": "^13.3.0",
|
|
37
|
+
"@fastly/js-compute": "^3.38.2",
|
|
38
|
+
"@types/node": "^25.0.7",
|
|
39
|
+
"eslint": "^9.39.2",
|
|
40
|
+
"publint": "^0.3.16",
|
|
41
|
+
"tsup": "^8.5.1",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"vite": "^7.3.1"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"http-proxy-3": "^1.23.2",
|
|
47
|
+
"tree-kill-promise": "^5.0.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup --clean",
|
|
51
|
+
"dev": "tsup --watch",
|
|
52
|
+
"test": "pnpm run /^test:/",
|
|
53
|
+
"test:typecheck": "tsc -p tsconfig.json --noEmit",
|
|
54
|
+
"test:lint": "eslint . --max-warnings 0 --ignore-pattern dist --no-warn-ignored",
|
|
55
|
+
"test:package": "publint"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# `vite-plugin-fastly`
|
|
2
|
+
|
|
3
|
+
`vite-plugin-fastly` is a [Vite](https://vite.dev/) plugin for developing and building [Fastly Compute JavaScript](https://www.fastly.com/documentation/guides/compute/developer-guides/javascript/) applications with Vite.
|
|
4
|
+
|
|
5
|
+
Fastly's official local development solution is the `fastly compute serve` command (available via the [Fastly CLI](https://www.fastly.com/documentation/reference/cli/)). It supports a `--watch` flag that rebuilds and reloads your application when source files change. However, this rebuild process can be slow since it involves bundling your application and compiling it to WebAssembly.
|
|
6
|
+
|
|
7
|
+
`vite-plugin-fastly` relies instead on Vite's [Environment API](https://vite.dev/guide/api-environment) to transform your source files on-the-fly during local development, resulting in a much faster feedback loop. It also enables access to Vite features like glob imports and the vast ecosystem of Vite plugins.
|
|
8
|
+
|
|
9
|
+
## Usage scenarios
|
|
10
|
+
|
|
11
|
+
You can use `vite-plugin-fastly` to build pure server-side Fastly Compute JavaScript applications, or full-stack applications that also include client-side code, for example a React application with SSR.
|
|
12
|
+
|
|
13
|
+
Check the starter examples:
|
|
14
|
+
|
|
15
|
+
- [Server-only](/examples/server-only/readme.md)
|
|
16
|
+
- [Client and server](/examples/client-server/readme.md)
|
|
17
|
+
- [React SSR](/examples/react-ssr/readme.md)
|
|
18
|
+
|
|
19
|
+
## How it works
|
|
20
|
+
|
|
21
|
+
During development, `vite-plugin-fastly` works by launching a remote module runner via `fastly compute serve` that communicates with the Vite development server to transform source files on-the-fly and run them via a Vite [`ModuleRunner`](https://vite.dev/guide/api-environment-runtimes#modulerunner). This runner, in turn, loads your entry module and passes incoming requests to it.
|
|
22
|
+
|
|
23
|
+
The plugin also adds a middleware to the Vite development server that proxies requests to the remote module runner. This proxy is placed _before_ Vite's own middlewares if you don't configure a client entry point via `environments.client.build.rollupOptions.input`, or _after_ if you do. When placed before, the setup essentially disables Vite's client-side features.
|
|
24
|
+
|
|
25
|
+
## Limitations
|
|
26
|
+
|
|
27
|
+
Fastly Compute JavaScript runtime (and its local emulator) has a heap size limit of 128MB. Since the plugin uses on-the-fly compilation, this means that large files or dependencies may cause out-of-memory errors during development.
|
|
28
|
+
|
|
29
|
+
`vite-plugin-fastly` tries to mitigate this by enabling minification during Vite's dependency pre-bundling. Using dynamic imports to lazy-load source files and dependencies can also help reduce per-request memory usage.
|