Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"scripts": {
"dev": "rolldown -cw",
"build": "rolldown -c",
"test": "vitest run",
"test": "pnpm test:unit && pnpm test:apps",
"check": "tsc",
"lint": "prettier --check .",
"format": "pnpm lint --write",
"prepublishOnly": "pnpm build"
"prepublishOnly": "pnpm build",
"test:unit": "vitest run",
"test:apps": "pnpm build && pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test"
},
"devDependencies": {
"@polka/url": "catalog:",
Expand Down
4 changes: 4 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules
/.svelte-kit
/build
21 changes: 21 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "test-adapter-node-instrumentation",
"version": "0.0.1",
"private": true,
"type": "module",
"imports": {
"#lib/*": "./src/lib/*"
},
"scripts": {
"dev": "vite dev",
"build": "vite build",
"prepare": "svelte-kit sync || echo ''",
"test": "MY_BASE_URL=https://api.example pnpm build && node test/boot.js"
},
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"svelte": "catalog:",
"vite": "catalog:"
}
}
12 changes: 12 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>

<body>
<div>%sveltekit.body%</div>
</body>
</html>
5 changes: 5 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/src/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineEnvVars } from '@sveltejs/kit/env';

export const variables = defineEnvVars({
MY_BASE_URL: { public: false, static: false }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// The mere presence of this file makes adapter-node call `builder.instrument()`
// (with `env: 'process.env'` on the eager-env branch), generating the env-init
// facade — the code path under test.
console.log('[instrumentation] evaluated');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MY_BASE_URL } from '$app/env/private';

// Module-scope read — mirrors configuring an API client with `baseUrl` from dynamic
// env. Correct only if env is populated before this module evaluates.
export const CAPTURED_AT_MODULE_SCOPE = MY_BASE_URL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>adapter-node instrumentation test app</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import process from 'node:process';
import { json } from '@sveltejs/kit';
import { CAPTURED_AT_MODULE_SCOPE } from '#lib/server/api-client.js';

export function GET() {
return json({
captured: CAPTURED_AT_MODULE_SCOPE ?? null,
live: process.env.MY_BASE_URL ?? null
});
}
66 changes: 66 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/test/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Boots the adapter-node build output with an instrumentation file present and
// asserts that (1) the server starts at all — regression for the env-init facade
// resolving `set_env` from the adapter's own `env.js` (sveltejs/kit#16303),
// and (2) a module-scope `$app/env/private` read observes the runtime value —
// regression for sveltejs/kit#16288.
import { spawn } from 'node:child_process';
import process from 'node:process';

const PORT = 3399;
const EXPECTED = 'https://api.example';

const server = spawn(process.execPath, ['build/index.js'], {
env: {
...process.env,
MY_BASE_URL: EXPECTED,
PORT: String(PORT),
HOST: '127.0.0.1'
},
stdio: ['ignore', 'pipe', 'pipe']
});

let output = '';
server.stdout.on('data', (chunk) => (output += chunk));
server.stderr.on('data', (chunk) => (output += chunk));

let exited = false;
server.on('exit', () => (exited = true));

function fail(message) {
console.error(`FAIL: ${message}`);
console.error('--- server output ---');
console.error(output || '(none)');
server.kill();
process.exit(1);
}

try {
let response;
for (let attempt = 0; attempt < 40; attempt++) {
if (exited) fail('server exited before responding (startup crash)');
await new Promise((resolve) => setTimeout(resolve, 250));
try {
response = await fetch(`http://127.0.0.1:${PORT}/env`);
break;
} catch {
// not up yet
}
}
if (!response) fail('server never came up');

const { captured, live } = await response.json();
if (live !== EXPECTED)
fail(`live env read is ${JSON.stringify(live)}, expected ${JSON.stringify(EXPECTED)}`);
if (captured !== EXPECTED) {
fail(
`module-scope $app/env/private read captured ${JSON.stringify(captured)} — evaluated before env was set`
);
}
console.log(
'PASS: server booted with instrumentation and module-scope env read observed the runtime value'
);
server.kill();
process.exit(0);
} catch (error) {
fail(error.message);
}
13 changes: 13 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"moduleResolution": "bundler"
},
"extends": "./.svelte-kit/tsconfig.json"
}
14 changes: 14 additions & 0 deletions packages/adapter-node/test/apps/instrumentation/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { sveltekit } from '@sveltejs/kit/vite';
import adapter from '../../../index.js';

/** @type {import('vite').UserConfig} */
export default {
build: {
minify: false
},
plugins: [
sveltekit({
adapter: adapter()
})
]
};
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading