@alteran/astro 0.1.6 → 0.1.7
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/src/worker/runtime.ts +43 -6
- package/types/astro-manifest.d.ts +6 -0
- package/types/worker.d.ts +5 -1
package/package.json
CHANGED
package/src/worker/runtime.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { handle } from 'astro/internal/handler';
|
|
2
|
-
import { onRequest } from '../middleware';
|
|
3
1
|
import { seed } from '../db/seed';
|
|
4
2
|
import { validateConfigOrThrow } from '../lib/config';
|
|
5
3
|
import type { Env } from '../env';
|
|
4
|
+
import type { SSRManifest } from 'astro';
|
|
6
5
|
import type {
|
|
7
6
|
ExecutionContext,
|
|
8
7
|
Request as WorkersRequest,
|
|
@@ -15,11 +14,19 @@ export type PdsFetchHandler = (
|
|
|
15
14
|
ctx: ExecutionContext
|
|
16
15
|
) => Promise<WorkersResponse>;
|
|
17
16
|
|
|
17
|
+
export interface CreatePdsFetchHandlerOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Optionally pass the host project's manifest when composing the worker manually.
|
|
20
|
+
* When omitted, the integration will load the manifest lazily from the build output.
|
|
21
|
+
*/
|
|
22
|
+
manifest?: SSRManifest;
|
|
23
|
+
}
|
|
24
|
+
|
|
18
25
|
/**
|
|
19
26
|
* Returns the Alteran PDS Worker fetch handler so downstream apps can
|
|
20
27
|
* compose it inside their own Cloudflare Worker entrypoint.
|
|
21
28
|
*/
|
|
22
|
-
export function createPdsFetchHandler(): PdsFetchHandler {
|
|
29
|
+
export function createPdsFetchHandler(options?: CreatePdsFetchHandlerOptions): PdsFetchHandler {
|
|
23
30
|
return async function fetch(request: WorkersRequest, env: Env, ctx: ExecutionContext) {
|
|
24
31
|
try {
|
|
25
32
|
validateConfigOrThrow(env);
|
|
@@ -53,11 +60,41 @@ export function createPdsFetchHandler(): PdsFetchHandler {
|
|
|
53
60
|
return (await stub.fetch(request as any)) as unknown as WorkersResponse;
|
|
54
61
|
}
|
|
55
62
|
|
|
56
|
-
const
|
|
57
|
-
|
|
63
|
+
const astroFetch = await getAstroFetch(options);
|
|
64
|
+
const response = await astroFetch(request, env as any, ctx);
|
|
65
|
+
return response as unknown as WorkersResponse;
|
|
58
66
|
};
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
|
|
69
|
+
type AstroFetchHandler = (
|
|
70
|
+
request: WorkersRequest,
|
|
71
|
+
env: Env,
|
|
72
|
+
ctx: ExecutionContext
|
|
73
|
+
) => Promise<WorkersResponse>;
|
|
74
|
+
|
|
75
|
+
let cachedFetchPromise: Promise<AstroFetchHandler> | undefined;
|
|
76
|
+
|
|
77
|
+
async function loadAstroFetchFromManifest(manifest: SSRManifest): Promise<AstroFetchHandler> {
|
|
78
|
+
const { createExports } = await import('@astrojs/cloudflare/entrypoints/server.js');
|
|
79
|
+
const exports = createExports(manifest);
|
|
80
|
+
return exports.default.fetch as unknown as AstroFetchHandler;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function getAstroFetch(options?: CreatePdsFetchHandlerOptions): Promise<AstroFetchHandler> {
|
|
84
|
+
if (options?.manifest) {
|
|
85
|
+
return loadAstroFetchFromManifest(options.manifest);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!cachedFetchPromise) {
|
|
89
|
+
cachedFetchPromise = (async () => {
|
|
90
|
+
const { manifest } = await import('@astrojs-manifest');
|
|
91
|
+
return loadAstroFetchFromManifest(manifest as SSRManifest);
|
|
92
|
+
})();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return cachedFetchPromise;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { onRequest } from '../middleware';
|
|
62
99
|
export { seed };
|
|
63
100
|
export { validateConfigOrThrow };
|
package/types/worker.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
createPdsFetchHandler,
|
|
3
|
+
type PdsFetchHandler,
|
|
4
|
+
type CreatePdsFetchHandlerOptions,
|
|
5
|
+
} from '../src/worker/runtime';
|
|
2
6
|
export { Sequencer } from '../src/worker/sequencer';
|
|
3
7
|
export { onRequest } from '../src/middleware';
|
|
4
8
|
export { seed } from '../src/db/seed';
|