@skgo/sveltekit-adapter 0.16.2 → 0.16.4
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/package.json +1 -1
- package/skgo-adapter/app-server.js +12 -4
- package/skgo-adapter/entry.js +6 -0
- package/skgo-adapter.js +37 -1
package/package.json
CHANGED
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
// below shadows the star, which is how the four substitutions take effect.
|
|
24
24
|
import * as real from 'skgo:kit/remote';
|
|
25
25
|
import { stringify_remote_arg } from 'skgo:kit/shared';
|
|
26
|
-
import { HttpError, Redirect } from '@sveltejs/kit/internal
|
|
26
|
+
import { HandledHttpError, HttpError, Redirect } from '@sveltejs/kit/internal';
|
|
27
|
+
import { DEV } from 'esm-env';
|
|
27
28
|
import { parse } from 'skgo:kit/transport';
|
|
28
29
|
|
|
29
30
|
export * from 'skgo:kit/app-server';
|
|
@@ -47,6 +48,9 @@ export * from 'skgo:kit/app-server';
|
|
|
47
48
|
async function host(id, payload) {
|
|
48
49
|
const raw = await globalThis.__skgo_remote(id, payload);
|
|
49
50
|
const res = JSON.parse(raw);
|
|
51
|
+
if (res.h) {
|
|
52
|
+
throw new HandledHttpError(res.h);
|
|
53
|
+
}
|
|
50
54
|
if (res.r) {
|
|
51
55
|
throw new Redirect(res.r.status, res.r.location);
|
|
52
56
|
}
|
|
@@ -64,9 +68,13 @@ export function query(validate_or_fn, maybe_fn) {
|
|
|
64
68
|
|
|
65
69
|
export function prerender(validate_or_fn, maybe_fn, options) {
|
|
66
70
|
const fn = (arg) => host(wrapper.__.id, stringify_remote_arg(arg));
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
const hasValidator = typeof maybe_fn === 'function';
|
|
72
|
+
const normalizedOptions = options ?? (hasValidator ? undefined : maybe_fn);
|
|
73
|
+
const wrapper = !DEV && !normalizedOptions?.dynamic
|
|
74
|
+
? real.prerender('unchecked', fn, normalizedOptions)
|
|
75
|
+
: hasValidator
|
|
76
|
+
? real.prerender(validate_or_fn, fn, options)
|
|
77
|
+
: real.prerender(fn, maybe_fn);
|
|
70
78
|
return wrapper;
|
|
71
79
|
}
|
|
72
80
|
|
package/skgo-adapter/entry.js
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
SvelteKitError,
|
|
21
21
|
ValidationError
|
|
22
22
|
} from '@sveltejs/kit/internal/server';
|
|
23
|
+
import { HandledHttpError } from '@sveltejs/kit/internal';
|
|
23
24
|
import * as devalue from 'skgo:devalue';
|
|
24
25
|
import { decoders, init_transport, parse } from 'skgo:kit/transport';
|
|
25
26
|
import { components, universalLoads } from 'skgo:nodes';
|
|
@@ -47,6 +48,7 @@ init_transport(transport ?? {});
|
|
|
47
48
|
* skgo has no handleError hook, so there is nothing here to await or merge.
|
|
48
49
|
*/
|
|
49
50
|
function handle_error(error) {
|
|
51
|
+
if (error instanceof HandledHttpError) return error.body;
|
|
50
52
|
if (error instanceof HttpError) return error.body;
|
|
51
53
|
if (error instanceof SvelteKitError) return { status: error.status, message: error.text };
|
|
52
54
|
if (error instanceof ValidationError) return { status: 400, message: 'Bad Request' };
|
|
@@ -418,6 +420,8 @@ globalThis.__skgo_render = function (req_json) {
|
|
|
418
420
|
(err) => {
|
|
419
421
|
if (err instanceof Redirect) {
|
|
420
422
|
result.redirect = { status: err.status, location: err.location };
|
|
423
|
+
} else if (err instanceof HandledHttpError) {
|
|
424
|
+
result.handled_error = err.body;
|
|
421
425
|
} else {
|
|
422
426
|
result.failure = (err && (err.stack || err.message)) || String(err);
|
|
423
427
|
}
|
|
@@ -427,6 +431,8 @@ globalThis.__skgo_render = function (req_json) {
|
|
|
427
431
|
} catch (err) {
|
|
428
432
|
if (err instanceof Redirect) {
|
|
429
433
|
result.redirect = { status: err.status, location: err.location };
|
|
434
|
+
} else if (err instanceof HandledHttpError) {
|
|
435
|
+
result.handled_error = err.body;
|
|
430
436
|
} else {
|
|
431
437
|
result.failure = (err && (err.stack || err.message)) || String(err);
|
|
432
438
|
}
|
package/skgo-adapter.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
linkSync,
|
|
2
3
|
existsSync,
|
|
3
4
|
mkdtempSync,
|
|
4
5
|
mkdirSync,
|
|
@@ -7,12 +8,14 @@ import {
|
|
|
7
8
|
realpathSync,
|
|
8
9
|
rmSync,
|
|
9
10
|
statSync,
|
|
11
|
+
unlinkSync,
|
|
10
12
|
writeFileSync
|
|
11
13
|
} from 'node:fs';
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
12
15
|
import { spawn } from 'node:child_process';
|
|
13
16
|
import { createRequire } from 'node:module';
|
|
14
17
|
import { tmpdir } from 'node:os';
|
|
15
|
-
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
18
|
+
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
|
16
19
|
import { pathToFileURL } from 'node:url';
|
|
17
20
|
import { gojaDevEnvironment, gojaDevUnchangedFiles, gojaEnvironment, goEnvironmentValues, nodeTable, SSR_TARGET } from './skgo-adapter/env.js';
|
|
18
21
|
import { identity } from './skgo-adapter/identity.js';
|
|
@@ -250,6 +253,7 @@ export default function skgo({ out = 'build', precompress = true } = {}) {
|
|
|
250
253
|
await builder.compress(`${out}/client`);
|
|
251
254
|
if (existsSync(`${out}/prerendered`)) await builder.compress(`${out}/prerendered`);
|
|
252
255
|
}
|
|
256
|
+
const prerenderedFiles = relocateQueryPrerenderedFiles(`${out}/prerendered`, out);
|
|
253
257
|
|
|
254
258
|
// `builder` has no writeJson in kit 3.0.0-next.27 (it existed on the
|
|
255
259
|
// kit 2 line); write the manifest ourselves.
|
|
@@ -314,6 +318,7 @@ export default function skgo({ out = 'build', precompress = true } = {}) {
|
|
|
314
318
|
})),
|
|
315
319
|
remotes: generated.remotes,
|
|
316
320
|
prerendered,
|
|
321
|
+
...(Object.keys(prerenderedFiles).length > 0 ? { prerenderedFiles } : {}),
|
|
317
322
|
precompressed: precompress
|
|
318
323
|
},
|
|
319
324
|
null,
|
|
@@ -331,6 +336,37 @@ export default function skgo({ out = 'build', precompress = true } = {}) {
|
|
|
331
336
|
};
|
|
332
337
|
}
|
|
333
338
|
|
|
339
|
+
/**
|
|
340
|
+
* Kit's native prerender filenames include query strings. Move only the
|
|
341
|
+
* adapter-owned copy of those files to safe build paths so Go's embed patterns
|
|
342
|
+
* can include them, while retaining the exact Kit filename as the manifest key.
|
|
343
|
+
* Hard-link-then-unlink publishes without replacing an existing destination.
|
|
344
|
+
*
|
|
345
|
+
* @param {string} root
|
|
346
|
+
* @param {string} out
|
|
347
|
+
* @returns {Record<string, string>}
|
|
348
|
+
*/
|
|
349
|
+
function relocateQueryPrerenderedFiles(root, out) {
|
|
350
|
+
if (!existsSync(root)) return {};
|
|
351
|
+
const mapping = {};
|
|
352
|
+
for (const file of walk(root)) {
|
|
353
|
+
const logical = relative(root, file).split(sep).join('/');
|
|
354
|
+
if (!logical.includes('?')) continue;
|
|
355
|
+
const digest = createHash('sha256').update(logical, 'utf8').digest('hex');
|
|
356
|
+
const physical = `prerendered-files/${digest}`;
|
|
357
|
+
const destination = join(out, physical);
|
|
358
|
+
mkdirSync(dirname(destination), { recursive: true });
|
|
359
|
+
try {
|
|
360
|
+
linkSync(file, destination);
|
|
361
|
+
} catch (error) {
|
|
362
|
+
throw new Error(`skgo: cannot publish query prerendered file ${logical} at ${physical}: ${error}`);
|
|
363
|
+
}
|
|
364
|
+
unlinkSync(file);
|
|
365
|
+
mapping[logical] = physical;
|
|
366
|
+
}
|
|
367
|
+
return mapping;
|
|
368
|
+
}
|
|
369
|
+
|
|
334
370
|
/**
|
|
335
371
|
* Reads what `skgo generate` emitted.
|
|
336
372
|
*
|