fimo-sveltekit 2.8.0 → 2.9.0-staging.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/dist/config.js +42 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +57 -1
- package/package.json +4 -4
package/dist/config.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { copyFileSync, mkdirSync, readFileSync, readdirSync, realpathSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join, relative } from 'node:path';
|
|
1
3
|
import adapter from '@sveltejs/adapter-vercel';
|
|
4
|
+
import { loadEnv } from 'vite';
|
|
2
5
|
/**
|
|
3
6
|
* Configure a SvelteKit project for Fimo hosting without exposing the hosting
|
|
4
7
|
* provider in the generated application: `kit: { adapter: fimo() }`.
|
|
@@ -7,5 +10,43 @@ import adapter from '@sveltejs/adapter-vercel';
|
|
|
7
10
|
* only installed by SvelteKit projects (FIMO-1635).
|
|
8
11
|
*/
|
|
9
12
|
export function fimo() {
|
|
10
|
-
|
|
13
|
+
const provider = adapter({ runtime: 'nodejs24.x' });
|
|
14
|
+
return {
|
|
15
|
+
...provider,
|
|
16
|
+
async adapt(builder) {
|
|
17
|
+
await provider.adapt(builder);
|
|
18
|
+
// Fimo's hosted build passes deployment env through .env. Read it at
|
|
19
|
+
// build time; an explicit empty process value keeps publication unchanged.
|
|
20
|
+
const studioOrigin = process.env.FIMO_PREVIEW_PARENT_ORIGIN ??
|
|
21
|
+
loadEnv('production', builder.config.kit.env.dir, 'FIMO_PREVIEW_PARENT_ORIGIN').FIMO_PREVIEW_PARENT_ORIGIN;
|
|
22
|
+
if (!studioOrigin)
|
|
23
|
+
return;
|
|
24
|
+
// FIMO-2910: wrap the adapter's final Web Response, after hooks and native
|
|
25
|
+
// CSP. Production builds retain the unmodified provider output.
|
|
26
|
+
const functions = '.vercel/output/functions';
|
|
27
|
+
const wrapped = new Set();
|
|
28
|
+
for (const entry of readdirSync(functions, { recursive: true, withFileTypes: true })) {
|
|
29
|
+
if (!entry.isFile() || entry.name !== '.vc-config.json')
|
|
30
|
+
continue;
|
|
31
|
+
const directory = realpathSync(entry.parentPath);
|
|
32
|
+
// Several route aliases can point at the same provider function.
|
|
33
|
+
if (wrapped.has(directory))
|
|
34
|
+
continue;
|
|
35
|
+
wrapped.add(directory);
|
|
36
|
+
const configFile = join(directory, entry.name);
|
|
37
|
+
const config = JSON.parse(readFileSync(configFile, 'utf8'));
|
|
38
|
+
const entrypoint = config.runtime === 'edge' ? 'entrypoint' : 'handler';
|
|
39
|
+
const wrapper = join(directory, '.fimo-preview');
|
|
40
|
+
mkdirSync(wrapper, { recursive: true });
|
|
41
|
+
copyFileSync(new URL('./frame-headers.js', import.meta.resolve('fimo/preview')), join(wrapper, 'frame-headers.mjs'));
|
|
42
|
+
writeFileSync(join(wrapper, 'handler.mjs'), `import handler from ${JSON.stringify('../' + config[entrypoint])};
|
|
43
|
+
import { adaptPreviewFrameResponse } from './frame-headers.mjs';
|
|
44
|
+
const fetch = async (...args) => adaptPreviewFrameResponse(
|
|
45
|
+
await ${config.runtime === 'edge' ? 'handler' : 'handler.fetch'}(...args), ${JSON.stringify(studioOrigin)});
|
|
46
|
+
export default ${config.runtime === 'edge' ? 'fetch' : '{ ...handler, fetch }'};\n`);
|
|
47
|
+
config[entrypoint] = relative(directory, join(wrapper, 'handler.mjs'));
|
|
48
|
+
writeFileSync(configFile, JSON.stringify(config));
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
};
|
|
11
52
|
}
|
package/dist/vite.d.ts
CHANGED
package/dist/vite.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { adaptPreviewFrameHeaders } from 'fimo/preview';
|
|
1
2
|
import runtimeEnvPlugin from 'fimo/vite/runtime-env';
|
|
3
|
+
import { loadEnv } from 'vite';
|
|
2
4
|
import svelteDataIdPlugin from './data-id.js';
|
|
3
5
|
/**
|
|
4
6
|
* Narrow SvelteKit build connector. Points dev and production builds at the
|
|
@@ -10,5 +12,59 @@ import svelteDataIdPlugin from './data-id.js';
|
|
|
10
12
|
* Usage in the app's vite.config.ts: `plugins: [fimo(), sveltekit()]`.
|
|
11
13
|
*/
|
|
12
14
|
export function fimo() {
|
|
13
|
-
return [
|
|
15
|
+
return [
|
|
16
|
+
runtimeEnvPlugin(),
|
|
17
|
+
svelteDataIdPlugin(),
|
|
18
|
+
{
|
|
19
|
+
name: 'fimo:sveltekit-preview-frame-headers',
|
|
20
|
+
apply: 'serve',
|
|
21
|
+
configureServer(server) {
|
|
22
|
+
const studioOrigin = process.env.FIMO_PREVIEW_PARENT_ORIGIN ??
|
|
23
|
+
loadEnv(server.config.mode, server.config.envDir, 'FIMO_PREVIEW_PARENT_ORIGIN').FIMO_PREVIEW_PARENT_ORIGIN;
|
|
24
|
+
if (!studioOrigin)
|
|
25
|
+
return;
|
|
26
|
+
server.middlewares.use((_req, res, next) => {
|
|
27
|
+
// FIMO-2910: SvelteKit writes native CSP after rendering. Adapt at the
|
|
28
|
+
// response boundary, after application hooks and before bytes are sent.
|
|
29
|
+
const writeHead = res.writeHead;
|
|
30
|
+
res.writeHead = function (statusCode, reasonOrHeaders, headers) {
|
|
31
|
+
const supplied = typeof reasonOrHeaders === 'string' ? headers : reasonOrHeaders;
|
|
32
|
+
if (supplied) {
|
|
33
|
+
if (Array.isArray(supplied)) {
|
|
34
|
+
const replaced = new Set();
|
|
35
|
+
for (let i = 0; i < supplied.length; i += 2) {
|
|
36
|
+
const key = String(supplied[i]).toLowerCase();
|
|
37
|
+
if (!replaced.has(key))
|
|
38
|
+
this.removeHeader(key);
|
|
39
|
+
replaced.add(key);
|
|
40
|
+
const value = supplied[i + 1];
|
|
41
|
+
this.appendHeader(key, typeof value === 'number' ? String(value) : value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
for (const [key, value] of Object.entries(supplied))
|
|
46
|
+
if (value !== undefined)
|
|
47
|
+
this.setHeader(key, value);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const original = Object.entries(this.getHeaders()).flatMap(([key, value]) => (Array.isArray(value) ? value : value === undefined ? [] : [String(value)]).map((value) => ({
|
|
51
|
+
key,
|
|
52
|
+
value,
|
|
53
|
+
})));
|
|
54
|
+
const adapted = adaptPreviewFrameHeaders(original, studioOrigin);
|
|
55
|
+
if (adapted !== original) {
|
|
56
|
+
this.removeHeader('x-frame-options');
|
|
57
|
+
const policies = adapted.filter((header) => header.key.toLowerCase() === 'content-security-policy');
|
|
58
|
+
if (policies.length)
|
|
59
|
+
this.setHeader('content-security-policy', policies.map((header) => header.value));
|
|
60
|
+
}
|
|
61
|
+
if (typeof reasonOrHeaders === 'string')
|
|
62
|
+
this.statusMessage = reasonOrHeaders;
|
|
63
|
+
return writeHead.call(this, statusCode);
|
|
64
|
+
};
|
|
65
|
+
next();
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
];
|
|
14
70
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fimo-sveltekit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0-staging.2",
|
|
4
4
|
"description": "Fimo runtime and hosting adapter for SvelteKit projects. Pairs with the fimo package at the same version.",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/",
|
|
@@ -48,16 +48,16 @@
|
|
|
48
48
|
"@sveltejs/adapter-vercel": "6.3.4"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@fimo/tsconfig": "2.
|
|
51
|
+
"@fimo/tsconfig": "2.9.0-staging.2",
|
|
52
52
|
"@types/node": "22.18.6",
|
|
53
|
-
"fimo": "2.
|
|
53
|
+
"fimo": "2.9.0-staging.2",
|
|
54
54
|
"typescript": "7.0.2",
|
|
55
55
|
"vite": "8.3.4",
|
|
56
56
|
"vitest": "5.0.4"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"@sveltejs/kit": "^2.0.0",
|
|
60
|
-
"fimo": "
|
|
60
|
+
"fimo": "2.9.0-staging.2",
|
|
61
61
|
"svelte": "^5.0.0",
|
|
62
62
|
"vite": "^7.0.0 || ^8.0.0"
|
|
63
63
|
},
|