@siddharatha/adapter-node-rolldown 1.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 +21 -0
- package/README.md +581 -0
- package/files/env.js +50 -0
- package/files/handler.js +16 -0
- package/files/index.js +217 -0
- package/files/middlewares.js +162 -0
- package/files/shims.js +6 -0
- package/files/telemetry.js +126 -0
- package/index.d.ts +125 -0
- package/index.js +232 -0
- package/package.json +55 -0
package/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { writeFileSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { rolldown } from 'rolldown';
|
|
4
|
+
|
|
5
|
+
const files = fileURLToPath(new URL('./files', import.meta.url).href);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} AdapterOptions
|
|
9
|
+
* @property {string} [out='build'] - Output directory
|
|
10
|
+
* @property {boolean} [precompress=true] - Pre-compress static assets
|
|
11
|
+
* @property {string} [envPrefix=''] - Prefix for environment variables
|
|
12
|
+
* @property {boolean} [compression=true] - Enable runtime compression
|
|
13
|
+
* @property {number} [compressionLevel=6] - Compression level (1-9)
|
|
14
|
+
* @property {string} [bodyLimit='10mb'] - Body parser size limit
|
|
15
|
+
* @property {boolean} [websocket=true] - Enable WebSocket support
|
|
16
|
+
* @property {string} [websocketPath='/ws'] - WebSocket endpoint path
|
|
17
|
+
* @property {boolean} [telemetry=true] - Enable OpenTelemetry
|
|
18
|
+
* @property {object} [telemetryConfig={}] - Additional telemetry configuration
|
|
19
|
+
* @property {number} [telemetrySampleRate=1.0] - Sampling rate (0.0-1.0)
|
|
20
|
+
* @property {boolean} [healthCheck=true] - Enable health check endpoints
|
|
21
|
+
* @property {number} [gracefulShutdownTimeout=30000] - Graceful shutdown timeout (ms)
|
|
22
|
+
* @property {boolean} [polyfill=true] - Inject global polyfills
|
|
23
|
+
* @property {string[]|((pkg: any) => string[])} [external] - External packages to exclude from bundle. Can be array of package names or function that receives package.json and returns array
|
|
24
|
+
* @property {boolean} [bundleAll=false] - Bundle all dependencies (ignore package.json dependencies)
|
|
25
|
+
* @property {object} [rolldownOptions={}] - Additional rolldown configuration options
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* High-performance SvelteKit adapter for Node.js
|
|
30
|
+
* @param {AdapterOptions} options
|
|
31
|
+
*/
|
|
32
|
+
export default function (options = {}) {
|
|
33
|
+
const {
|
|
34
|
+
out = 'build',
|
|
35
|
+
precompress = true,
|
|
36
|
+
envPrefix = '',
|
|
37
|
+
compression = true,
|
|
38
|
+
compressionLevel = 6,
|
|
39
|
+
bodyLimit = '1000mb',
|
|
40
|
+
websocket = true,
|
|
41
|
+
websocketPath = '/ws',
|
|
42
|
+
telemetry = true,
|
|
43
|
+
telemetryConfig = {},
|
|
44
|
+
telemetrySampleRate = 1.0,
|
|
45
|
+
healthCheck = true,
|
|
46
|
+
gracefulShutdownTimeout = 30000,
|
|
47
|
+
polyfill = true,
|
|
48
|
+
external,
|
|
49
|
+
bundleAll = false,
|
|
50
|
+
rolldownOptions = {}
|
|
51
|
+
} = options;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
name: '@sveltejs/adapter-node-rolldown',
|
|
55
|
+
|
|
56
|
+
async adapt(builder) {
|
|
57
|
+
const tmp = builder.getBuildDirectory('adapter-node-rolldown');
|
|
58
|
+
|
|
59
|
+
builder.rimraf(out);
|
|
60
|
+
builder.rimraf(tmp);
|
|
61
|
+
builder.mkdirp(tmp);
|
|
62
|
+
|
|
63
|
+
builder.log.minor('Copying assets');
|
|
64
|
+
builder.writeClient(`${out}/client${builder.config.kit.paths.base}`);
|
|
65
|
+
builder.writePrerendered(`${out}/prerendered${builder.config.kit.paths.base}`);
|
|
66
|
+
|
|
67
|
+
if (precompress) {
|
|
68
|
+
builder.log.minor('Compressing assets');
|
|
69
|
+
await Promise.all([
|
|
70
|
+
builder.compress(`${out}/client`),
|
|
71
|
+
builder.compress(`${out}/prerendered`)
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
builder.log.minor('Building server');
|
|
76
|
+
|
|
77
|
+
builder.writeServer(tmp);
|
|
78
|
+
|
|
79
|
+
writeFileSync(
|
|
80
|
+
`${tmp}/manifest.js`,
|
|
81
|
+
[
|
|
82
|
+
`export const manifest = ${builder.generateManifest({ relativePath: './' })};`,
|
|
83
|
+
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});`,
|
|
84
|
+
`export const base = ${JSON.stringify(builder.config.kit.paths.base)};`
|
|
85
|
+
].join('\n\n')
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
|
|
89
|
+
|
|
90
|
+
/** @type {Record<string, string>} */
|
|
91
|
+
const input = {
|
|
92
|
+
index: `${tmp}/index.js`,
|
|
93
|
+
manifest: `${tmp}/manifest.js`
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// Support for instrumentation files
|
|
97
|
+
if (builder.hasServerInstrumentationFile?.()) {
|
|
98
|
+
input['instrumentation.server'] = `${tmp}/instrumentation.server.js`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Node.js built-in modules (always external)
|
|
102
|
+
const builtins = [
|
|
103
|
+
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'console',
|
|
104
|
+
'constants', 'crypto', 'dgram', 'diagnostics_channel', 'dns', 'domain',
|
|
105
|
+
'events', 'fs', 'http', 'http2', 'https', 'inspector', 'module', 'net',
|
|
106
|
+
'os', 'path', 'perf_hooks', 'process', 'punycode', 'querystring', 'readline',
|
|
107
|
+
'repl', 'stream', 'string_decoder', 'sys', 'timers', 'tls', 'trace_events',
|
|
108
|
+
'tty', 'url', 'util', 'v8', 'vm', 'wasi', 'worker_threads', 'zlib'
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
// Determine external packages
|
|
112
|
+
let externalPackages = [];
|
|
113
|
+
if (!bundleAll) {
|
|
114
|
+
if (typeof external === 'function') {
|
|
115
|
+
externalPackages = external(pkg);
|
|
116
|
+
} else if (Array.isArray(external)) {
|
|
117
|
+
externalPackages = external;
|
|
118
|
+
} else {
|
|
119
|
+
// Default: use package.json dependencies
|
|
120
|
+
externalPackages = Object.keys(pkg.dependencies || {});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Combine builtins with external packages
|
|
125
|
+
const allExternal = [...new Set([...builtins, ...externalPackages])];
|
|
126
|
+
|
|
127
|
+
// Convert to regex patterns for deep exports support
|
|
128
|
+
// Also handle node: prefix for built-ins
|
|
129
|
+
const externalPatterns = allExternal.map((d) =>
|
|
130
|
+
new RegExp(`^(node:)?${d.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(\\/.*)?$`)
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// Bundle the Vite output so deployments only need production dependencies
|
|
134
|
+
// Anything in devDependencies will get included in the bundled code
|
|
135
|
+
// Rolldown has native support for node resolution, CommonJS, and JSON
|
|
136
|
+
const bundle = await rolldown({
|
|
137
|
+
input,
|
|
138
|
+
external: externalPatterns,
|
|
139
|
+
resolve: {
|
|
140
|
+
conditionNames: ['node', 'import'],
|
|
141
|
+
...rolldownOptions.resolve
|
|
142
|
+
},
|
|
143
|
+
cwd: process.cwd(),
|
|
144
|
+
...rolldownOptions
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await bundle.write({
|
|
148
|
+
dir: `${out}/server`,
|
|
149
|
+
format: 'esm',
|
|
150
|
+
sourcemap: true,
|
|
151
|
+
chunkFileNames: 'chunks/[name]-[hash].js'
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
builder.copy(files, out, {
|
|
155
|
+
replace: {
|
|
156
|
+
ENV: './env.js',
|
|
157
|
+
HANDLER: './handler.js',
|
|
158
|
+
MANIFEST: './server/manifest.js',
|
|
159
|
+
SERVER: './server/index.js',
|
|
160
|
+
SHIMS: './shims.js',
|
|
161
|
+
MIDDLEWARES: './middlewares.js',
|
|
162
|
+
TELEMETRY: './telemetry.js',
|
|
163
|
+
ENV_PREFIX: JSON.stringify(envPrefix),
|
|
164
|
+
COMPRESSION_ENABLED: JSON.stringify(compression),
|
|
165
|
+
COMPRESSION_LEVEL: JSON.stringify(compressionLevel),
|
|
166
|
+
BODY_LIMIT: JSON.stringify(bodyLimit),
|
|
167
|
+
WEBSOCKET_ENABLED: JSON.stringify(websocket),
|
|
168
|
+
WEBSOCKET_PATH: JSON.stringify(websocketPath),
|
|
169
|
+
TELEMETRY_ENABLED: JSON.stringify(telemetry),
|
|
170
|
+
TELEMETRY_CONFIG: JSON.stringify(telemetryConfig),
|
|
171
|
+
TELEMETRY_SAMPLE_RATE: JSON.stringify(telemetrySampleRate),
|
|
172
|
+
HEALTH_CHECK_ENABLED: JSON.stringify(healthCheck),
|
|
173
|
+
GRACEFUL_SHUTDOWN_TIMEOUT: JSON.stringify(gracefulShutdownTimeout),
|
|
174
|
+
POLYFILL: JSON.stringify(polyfill)
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Support for instrumentation
|
|
179
|
+
if (builder.hasServerInstrumentationFile?.()) {
|
|
180
|
+
builder.instrument?.({
|
|
181
|
+
entrypoint: `${out}/index.js`,
|
|
182
|
+
instrumentation: `${out}/server/instrumentation.server.js`,
|
|
183
|
+
module: {
|
|
184
|
+
exports: ['path', 'host', 'port', 'server']
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
builder.log.minor('Generating package.json');
|
|
190
|
+
writeFileSync(
|
|
191
|
+
`${out}/package.json`,
|
|
192
|
+
JSON.stringify(
|
|
193
|
+
{
|
|
194
|
+
name: 'sveltekit-app',
|
|
195
|
+
version: '1.0.0',
|
|
196
|
+
type: 'module',
|
|
197
|
+
main: './index.js',
|
|
198
|
+
engines: {
|
|
199
|
+
node: '>=24.12.0'
|
|
200
|
+
},
|
|
201
|
+
dependencies: Object.fromEntries(
|
|
202
|
+
Object.entries({
|
|
203
|
+
'@polka/url': '^1.0.0-next.28',
|
|
204
|
+
polka: '^0.5.2',
|
|
205
|
+
sirv: '^3.0.2',
|
|
206
|
+
compression: '^1.7.4',
|
|
207
|
+
ws: websocket ? '^8.16.0' : undefined,
|
|
208
|
+
'@opentelemetry/sdk-node': telemetry ? '0.48.0' : undefined,
|
|
209
|
+
'@opentelemetry/auto-instrumentations-node': telemetry ? '0.41.0' : undefined,
|
|
210
|
+
'@opentelemetry/exporter-trace-otlp-http': telemetry ? '0.48.0' : undefined,
|
|
211
|
+
'@opentelemetry/exporter-trace-otlp-grpc': telemetry ? '0.48.0' : undefined,
|
|
212
|
+
'@opentelemetry/resources': telemetry ? '1.21.0' : undefined,
|
|
213
|
+
'@opentelemetry/semantic-conventions': telemetry ? '1.21.0' : undefined,
|
|
214
|
+
'@opentelemetry/api': telemetry ? '1.7.0' : undefined
|
|
215
|
+
}).filter(([, value]) => value !== undefined)
|
|
216
|
+
)
|
|
217
|
+
},
|
|
218
|
+
null,
|
|
219
|
+
2
|
|
220
|
+
)
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
builder.log.success(`Adapter complete! Output: ${out}`);
|
|
224
|
+
builder.log.info(`\nTo run the server:\n cd ${out}\n npm install\n node index.js\n`);
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
supports: {
|
|
228
|
+
read: () => true,
|
|
229
|
+
instrumentation: () => true
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@siddharatha/adapter-node-rolldown",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "High-performance SvelteKit adapter for Node.js with Polka, WebSockets, and OpenTelemetry",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"files",
|
|
11
|
+
"index.js",
|
|
12
|
+
"index.d.ts"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=24.7.0"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"svelte",
|
|
22
|
+
"sveltekit",
|
|
23
|
+
"adapter",
|
|
24
|
+
"polka",
|
|
25
|
+
"performance",
|
|
26
|
+
"opentelemetry",
|
|
27
|
+
"websocket",
|
|
28
|
+
"kubernetes",
|
|
29
|
+
"ecs",
|
|
30
|
+
"rolldown"
|
|
31
|
+
],
|
|
32
|
+
"author": "",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"rolldown": "^0.15.0",
|
|
36
|
+
"@polka/url": "^1.0.0-next.28",
|
|
37
|
+
"polka": "^0.5.2",
|
|
38
|
+
"sirv": "^3.0.2",
|
|
39
|
+
"compression": "^1.7.4",
|
|
40
|
+
"ws": "^8.16.0",
|
|
41
|
+
"@opentelemetry/sdk-node": "0.48.0",
|
|
42
|
+
"@opentelemetry/auto-instrumentations-node": "0.41.0",
|
|
43
|
+
"@opentelemetry/exporter-trace-otlp-http": "0.48.0",
|
|
44
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "0.48.0",
|
|
45
|
+
"@opentelemetry/resources": "1.21.0",
|
|
46
|
+
"@opentelemetry/semantic-conventions": "1.21.0",
|
|
47
|
+
"@opentelemetry/api": "1.7.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@sveltejs/kit": "^2.4.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@sveltejs/kit": "^2.4.0"
|
|
54
|
+
}
|
|
55
|
+
}
|