@voyant-travel/worker-runtime 0.1.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 +201 -0
- package/README.md +51 -0
- package/dist/api-dispatch.d.ts +40 -0
- package/dist/api-dispatch.d.ts.map +1 -0
- package/dist/api-dispatch.js +63 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/ssr-manifest.d.ts +51 -0
- package/dist/ssr-manifest.d.ts.map +1 -0
- package/dist/ssr-manifest.js +49 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/worker-fetch.d.ts +16 -0
- package/dist/worker-fetch.d.ts.map +1 -0
- package/dist/worker-fetch.js +18 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may accept and charge a
|
|
167
|
+
fee for, the acceptance of support, warranty, indemnity, or
|
|
168
|
+
other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 PixelMakers Studio SRL
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @voyant-travel/worker-runtime
|
|
2
|
+
|
|
3
|
+
Framework-owned Cloudflare Worker entry logic for Voyant apps: API/auth/SSR
|
|
4
|
+
dispatch and SSR-manifest restriction, delivered as a versioned package
|
|
5
|
+
instead of copied template files.
|
|
6
|
+
|
|
7
|
+
Part of the Packaged Admin direction (`docs/architecture/packaged-admin-rfc.md`,
|
|
8
|
+
Phase 0): a project's `entry.ts` shrinks to bindings + factory calls, and
|
|
9
|
+
fixes to this load-bearing code path arrive via a dependency bump.
|
|
10
|
+
|
|
11
|
+
## What it does
|
|
12
|
+
|
|
13
|
+
- **`createApiDispatch({ loadApiApp, loadAuthApp? })`** — routes hosting-URL
|
|
14
|
+
requests (`/api/*`) onto the app surface (`/v1/*`, `/auth/*`, `/health`),
|
|
15
|
+
stripping the prefix and preserving method/headers/body/search. When
|
|
16
|
+
`loadAuthApp` is set, `/api/auth/*` dispatches to the lean auth app without
|
|
17
|
+
instantiating the full API graph (the cold-start outage fix), and non-OPTIONS
|
|
18
|
+
auth traffic warms the full app in the background via `ctx.waitUntil`.
|
|
19
|
+
- **`createWorkerFetch({ api, ssr })`** — the Worker `fetch` entrypoint:
|
|
20
|
+
API-prefixed requests go to the dispatch, everything else to the SSR handler.
|
|
21
|
+
- **`withActiveRouteSsrManifest(streamHandler)`** — restricts the TanStack
|
|
22
|
+
Start SSR manifest to the active route matches so the first paint isn't
|
|
23
|
+
flooded with speculative preloads. Structurally typed; no router dependency.
|
|
24
|
+
- **`lazyApp(load)`** — memoizes an app loader per isolate.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// src/entry.ts
|
|
30
|
+
import { createStartHandler, defaultStreamHandler } from "@tanstack/react-start/server"
|
|
31
|
+
import { createWorkerFetch, lazyApp, withActiveRouteSsrManifest } from "@voyant-travel/worker-runtime"
|
|
32
|
+
|
|
33
|
+
const startHandler = createStartHandler(withActiveRouteSsrManifest(defaultStreamHandler))
|
|
34
|
+
|
|
35
|
+
const fetch = createWorkerFetch<CloudflareBindings>({
|
|
36
|
+
api: {
|
|
37
|
+
loadApiApp: lazyApp(() => import("./api/app").then((m) => ({ fetch: m.app.fetch }))),
|
|
38
|
+
loadAuthApp: lazyApp(() => import("./api/auth-app").then((m) => ({ fetch: m.authApp.fetch }))),
|
|
39
|
+
},
|
|
40
|
+
ssr: (request, env) => startHandler(request, { context: { env } } as never),
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
export default { fetch }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Scheduled (cron) handlers, Durable Objects, and workflow wiring remain
|
|
47
|
+
app-owned composition — see RFC §4.4.
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
Apache-2.0
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AppLoader, FetchApp, WaitUntilContext } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Memoize an app loader so the underlying dynamic `import()` runs once per
|
|
4
|
+
* isolate, no matter how many requests race on it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function lazyApp<Env, Ctx extends WaitUntilContext = WaitUntilContext>(load: () => Promise<FetchApp<Env, Ctx>>): AppLoader<Env, Ctx>;
|
|
7
|
+
export interface CreateApiDispatchOptions<Env, Ctx extends WaitUntilContext = WaitUntilContext> {
|
|
8
|
+
/** Loads the full API app (the heavy module graph). Wrap with {@link lazyApp}. */
|
|
9
|
+
loadApiApp: AppLoader<Env, Ctx>;
|
|
10
|
+
/**
|
|
11
|
+
* Optional lean auth app. When set, requests under `authPrefix` dispatch to
|
|
12
|
+
* it WITHOUT loading the full API graph — the fix for the cold-start outage
|
|
13
|
+
* where the first `/api/auth/*` call instantiated the whole API and hung.
|
|
14
|
+
* Non-OPTIONS auth requests warm the full app in the background via
|
|
15
|
+
* `ctx.waitUntil` so the next API call is hot.
|
|
16
|
+
*/
|
|
17
|
+
loadAuthApp?: AppLoader<Env, Ctx>;
|
|
18
|
+
/** Hosting prefix stripped before dispatch. Default `/api`. */
|
|
19
|
+
apiPrefix?: string;
|
|
20
|
+
/** Auth sub-prefix served by the lean app. Default `${apiPrefix}/auth`. */
|
|
21
|
+
authPrefix?: string;
|
|
22
|
+
/** Background-warm the full app on auth traffic. Default true. */
|
|
23
|
+
warmApiOnAuth?: boolean;
|
|
24
|
+
/** Called when the background warm-up fails. Defaults to `console.error`. */
|
|
25
|
+
onWarmError?: (error: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
export interface ApiDispatch<Env, Ctx extends WaitUntilContext = WaitUntilContext> {
|
|
28
|
+
isApiRequest(pathname: string): boolean;
|
|
29
|
+
isAuthRequest(pathname: string): boolean;
|
|
30
|
+
/** Strip the hosting prefix (`/api/v1/x` → `/v1/x`), preserving search/body. */
|
|
31
|
+
toAppRequest(request: Request): Request;
|
|
32
|
+
dispatch(request: Request, env: Env, ctx: Ctx): Promise<Response>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Prefix-routed dispatch from a hosting Worker URL space (`/api/*`) onto a
|
|
36
|
+
* Hono-style app surface (`/v1/*`, `/auth/*`, `/health`). Framework-owned:
|
|
37
|
+
* apps supply only the loaders for their own modules.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createApiDispatch<Env, Ctx extends WaitUntilContext = WaitUntilContext>(options: CreateApiDispatchOptions<Env, Ctx>): ApiDispatch<Env, Ctx>;
|
|
40
|
+
//# sourceMappingURL=api-dispatch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-dispatch.d.ts","sourceRoot":"","sources":["../src/api-dispatch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAIvE;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB,EAC1E,IAAI,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GACtC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAMrB;AAED,MAAM,WAAW,wBAAwB,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB;IAC5F,kFAAkF;IAClF,UAAU,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAC/B;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IACjC,+DAA+D;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,2EAA2E;IAC3E,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,kEAAkE;IAClE,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,6EAA6E;IAC7E,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;CACvC;AAED,MAAM,WAAW,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB;IAC/E,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAA;IACvC,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAA;IACxC,gFAAgF;IAChF,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAA;IACvC,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;CAClE;AAMD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB,EACpF,OAAO,EAAE,wBAAwB,CAAC,GAAG,EAAE,GAAG,CAAC,GAC1C,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAgDvB"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const DEFAULT_API_PREFIX = "/api";
|
|
2
|
+
/**
|
|
3
|
+
* Memoize an app loader so the underlying dynamic `import()` runs once per
|
|
4
|
+
* isolate, no matter how many requests race on it.
|
|
5
|
+
*/
|
|
6
|
+
export function lazyApp(load) {
|
|
7
|
+
let promise;
|
|
8
|
+
return () => {
|
|
9
|
+
promise ??= load();
|
|
10
|
+
return promise;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function matchesPrefix(pathname, prefix) {
|
|
14
|
+
return pathname === prefix || pathname.startsWith(`${prefix}/`);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Prefix-routed dispatch from a hosting Worker URL space (`/api/*`) onto a
|
|
18
|
+
* Hono-style app surface (`/v1/*`, `/auth/*`, `/health`). Framework-owned:
|
|
19
|
+
* apps supply only the loaders for their own modules.
|
|
20
|
+
*/
|
|
21
|
+
export function createApiDispatch(options) {
|
|
22
|
+
const apiPrefix = options.apiPrefix ?? DEFAULT_API_PREFIX;
|
|
23
|
+
const authPrefix = options.authPrefix ?? `${apiPrefix}/auth`;
|
|
24
|
+
const warmApiOnAuth = options.warmApiOnAuth ?? true;
|
|
25
|
+
const onWarmError = options.onWarmError ??
|
|
26
|
+
((error) => {
|
|
27
|
+
console.error("[worker-runtime] background API warm failed:", error);
|
|
28
|
+
});
|
|
29
|
+
function toAppRequest(request) {
|
|
30
|
+
const url = new URL(request.url);
|
|
31
|
+
const stripped = url.pathname.slice(apiPrefix.length) || "/";
|
|
32
|
+
const appUrl = new URL(stripped, url.origin);
|
|
33
|
+
appUrl.search = url.search;
|
|
34
|
+
const bodyless = request.method === "GET" || request.method === "HEAD";
|
|
35
|
+
return new Request(appUrl.toString(), {
|
|
36
|
+
method: request.method,
|
|
37
|
+
headers: request.headers,
|
|
38
|
+
body: bodyless ? null : request.body,
|
|
39
|
+
redirect: request.redirect,
|
|
40
|
+
signal: request.signal,
|
|
41
|
+
...(bodyless ? {} : { duplex: "half" }),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async function dispatch(request, env, ctx) {
|
|
45
|
+
const { loadApiApp, loadAuthApp } = options;
|
|
46
|
+
if (loadAuthApp && matchesPrefix(new URL(request.url).pathname, authPrefix)) {
|
|
47
|
+
const authApp = await loadAuthApp();
|
|
48
|
+
const response = await authApp.fetch(toAppRequest(request), env, ctx);
|
|
49
|
+
if (warmApiOnAuth && request.method !== "OPTIONS") {
|
|
50
|
+
ctx.waitUntil(loadApiApp().then(() => undefined, onWarmError));
|
|
51
|
+
}
|
|
52
|
+
return response;
|
|
53
|
+
}
|
|
54
|
+
const apiApp = await loadApiApp();
|
|
55
|
+
return apiApp.fetch(toAppRequest(request), env, ctx);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
isApiRequest: (pathname) => matchesPrefix(pathname, apiPrefix),
|
|
59
|
+
isAuthRequest: (pathname) => matchesPrefix(pathname, authPrefix),
|
|
60
|
+
toAppRequest,
|
|
61
|
+
dispatch,
|
|
62
|
+
};
|
|
63
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { ApiDispatch, CreateApiDispatchOptions } from "./api-dispatch.js";
|
|
2
|
+
export { createApiDispatch, lazyApp } from "./api-dispatch.js";
|
|
3
|
+
export type { SsrManifest, SsrManifestRouter } from "./ssr-manifest.js";
|
|
4
|
+
export { restrictSsrManifestToActiveRoutes, withActiveRouteSsrManifest } from "./ssr-manifest.js";
|
|
5
|
+
export type { AppLoader, FetchApp, WaitUntilContext } from "./types.js";
|
|
6
|
+
export type { CreateWorkerFetchOptions, SsrHandler } from "./worker-fetch.js";
|
|
7
|
+
export { createWorkerFetch } from "./worker-fetch.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC9D,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACvE,OAAO,EAAE,iCAAiC,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAA;AACjG,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AACvE,YAAY,EAAE,wBAAwB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSR-manifest restriction for TanStack Start workers.
|
|
3
|
+
*
|
|
4
|
+
* By default the SSR stream emits preload/asset hints for every route in the
|
|
5
|
+
* manifest; on large admins that floods the first paint with hundreds of
|
|
6
|
+
* speculative preloads. Restricting the manifest to the matched routes keeps
|
|
7
|
+
* the initial response lean. Typed structurally so this package needs no
|
|
8
|
+
* dependency on the router — any router exposing `stores.matches` and an
|
|
9
|
+
* `ssr.manifest` fits.
|
|
10
|
+
*/
|
|
11
|
+
type ManifestRoute = {
|
|
12
|
+
assets?: Array<unknown>;
|
|
13
|
+
preloads?: Array<unknown>;
|
|
14
|
+
};
|
|
15
|
+
export type SsrManifest = {
|
|
16
|
+
inlineCss?: unknown;
|
|
17
|
+
routes: Record<string, ManifestRoute | undefined>;
|
|
18
|
+
};
|
|
19
|
+
export interface SsrManifestRouter {
|
|
20
|
+
stores: {
|
|
21
|
+
matches: {
|
|
22
|
+
get(): ReadonlyArray<{
|
|
23
|
+
routeId: string;
|
|
24
|
+
}>;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
ssr?: {
|
|
28
|
+
readonly manifest?: SsrManifest;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Replace `router.ssr.manifest` with a view filtered to the active route
|
|
33
|
+
* matches. The filter runs lazily (getter) because matches settle after the
|
|
34
|
+
* handler installs the restriction.
|
|
35
|
+
*/
|
|
36
|
+
export declare function restrictSsrManifestToActiveRoutes(router: SsrManifestRouter): void;
|
|
37
|
+
/**
|
|
38
|
+
* Wrap a TanStack Start stream-handler callback so every SSR render gets the
|
|
39
|
+
* active-route manifest restriction:
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* const startHandler = createStartHandler(
|
|
43
|
+
* withActiveRouteSsrManifest(defaultStreamHandler),
|
|
44
|
+
* )
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function withActiveRouteSsrManifest<TCtx extends {
|
|
48
|
+
router: unknown;
|
|
49
|
+
}, TResult>(handler: (ctx: TCtx) => TResult): (ctx: TCtx) => TResult;
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=ssr-manifest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssr-manifest.d.ts","sourceRoot":"","sources":["../src/ssr-manifest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,KAAK,aAAa,GAAG;IACnB,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IACvB,QAAQ,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;CAC1B,CAAA;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,SAAS,CAAC,CAAA;CAClD,CAAA;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE;QACN,OAAO,EAAE;YACP,GAAG,IAAI,aAAa,CAAC;gBAAE,OAAO,EAAE,MAAM,CAAA;aAAE,CAAC,CAAA;SAC1C,CAAA;KACF,CAAA;IACD,GAAG,CAAC,EAAE;QACJ,QAAQ,CAAC,QAAQ,CAAC,EAAE,WAAW,CAAA;KAChC,CAAA;CACF;AAED;;;;GAIG;AACH,wBAAgB,iCAAiC,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAoBjF;AAED;;;;;;;;;GASG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,SAAS;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,EAAE,OAAO,EAClF,OAAO,EAAE,CAAC,GAAG,EAAE,IAAI,KAAK,OAAO,GAC9B,CAAC,GAAG,EAAE,IAAI,KAAK,OAAO,CAKxB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSR-manifest restriction for TanStack Start workers.
|
|
3
|
+
*
|
|
4
|
+
* By default the SSR stream emits preload/asset hints for every route in the
|
|
5
|
+
* manifest; on large admins that floods the first paint with hundreds of
|
|
6
|
+
* speculative preloads. Restricting the manifest to the matched routes keeps
|
|
7
|
+
* the initial response lean. Typed structurally so this package needs no
|
|
8
|
+
* dependency on the router — any router exposing `stores.matches` and an
|
|
9
|
+
* `ssr.manifest` fits.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Replace `router.ssr.manifest` with a view filtered to the active route
|
|
13
|
+
* matches. The filter runs lazily (getter) because matches settle after the
|
|
14
|
+
* handler installs the restriction.
|
|
15
|
+
*/
|
|
16
|
+
export function restrictSsrManifestToActiveRoutes(router) {
|
|
17
|
+
const ssr = router.ssr;
|
|
18
|
+
if (!ssr?.manifest)
|
|
19
|
+
return;
|
|
20
|
+
router.ssr = {
|
|
21
|
+
get manifest() {
|
|
22
|
+
const manifest = ssr.manifest;
|
|
23
|
+
if (!manifest)
|
|
24
|
+
return manifest;
|
|
25
|
+
const activeRouteIds = new Set(router.stores.matches.get().map((match) => match.routeId));
|
|
26
|
+
const routes = Object.fromEntries(Object.entries(manifest.routes).filter(([routeId]) => activeRouteIds.has(routeId)));
|
|
27
|
+
return {
|
|
28
|
+
...manifest,
|
|
29
|
+
routes,
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Wrap a TanStack Start stream-handler callback so every SSR render gets the
|
|
36
|
+
* active-route manifest restriction:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* const startHandler = createStartHandler(
|
|
40
|
+
* withActiveRouteSsrManifest(defaultStreamHandler),
|
|
41
|
+
* )
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function withActiveRouteSsrManifest(handler) {
|
|
45
|
+
return (ctx) => {
|
|
46
|
+
restrictSsrManifestToActiveRoutes(ctx.router);
|
|
47
|
+
return handler(ctx);
|
|
48
|
+
};
|
|
49
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal structural slice of the Cloudflare `ExecutionContext`. Only
|
|
3
|
+
* `waitUntil` is required by this package; the full context object is passed
|
|
4
|
+
* through to apps untouched, so any richer context type remains compatible.
|
|
5
|
+
*/
|
|
6
|
+
export interface WaitUntilContext {
|
|
7
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Anything with a Worker-style `fetch` — a Hono app, a Better Auth handler,
|
|
11
|
+
* or a plain `{ fetch }` module default export.
|
|
12
|
+
*/
|
|
13
|
+
export interface FetchApp<Env = unknown, Ctx extends WaitUntilContext = WaitUntilContext> {
|
|
14
|
+
fetch(request: Request, env?: Env, ctx?: Ctx): Response | Promise<Response>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Lazily resolves a {@link FetchApp}. Loaders own the dynamic `import()` of
|
|
18
|
+
* app modules so the Worker entry stays out of the API graph until a request
|
|
19
|
+
* actually needs it.
|
|
20
|
+
*/
|
|
21
|
+
export type AppLoader<Env = unknown, Ctx extends WaitUntilContext = WaitUntilContext> = () => Promise<FetchApp<Env, Ctx>>;
|
|
22
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;CAC3C;AAED;;;GAGG;AACH,MAAM,WAAW,QAAQ,CAAC,GAAG,GAAG,OAAO,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB;IACtF,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,GAAG,GAAG,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC5E;AAED;;;;GAIG;AACH,MAAM,MAAM,SAAS,CACnB,GAAG,GAAG,OAAO,EACb,GAAG,SAAS,gBAAgB,GAAG,gBAAgB,IAC7C,MAAM,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ApiDispatch, type CreateApiDispatchOptions } from "./api-dispatch.js";
|
|
2
|
+
import type { WaitUntilContext } from "./types.js";
|
|
3
|
+
export type SsrHandler<Env, Ctx extends WaitUntilContext = WaitUntilContext> = (request: Request, env: Env, ctx: Ctx) => Response | Promise<Response>;
|
|
4
|
+
export interface CreateWorkerFetchOptions<Env, Ctx extends WaitUntilContext = WaitUntilContext> {
|
|
5
|
+
/** An {@link ApiDispatch} instance, or options to build one. */
|
|
6
|
+
api: ApiDispatch<Env, Ctx> | CreateApiDispatchOptions<Env, Ctx>;
|
|
7
|
+
/** Handles every non-API request (typically the SSR start handler). */
|
|
8
|
+
ssr: SsrHandler<Env, Ctx>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The Worker `fetch` entrypoint: API-prefixed requests go through the
|
|
12
|
+
* prefix-stripping dispatch, everything else goes to SSR. The app's
|
|
13
|
+
* `entry.ts` shrinks to bindings plus this factory call.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createWorkerFetch<Env, Ctx extends WaitUntilContext = WaitUntilContext>(options: CreateWorkerFetchOptions<Env, Ctx>): (request: Request, env: Env, ctx: Ctx) => Promise<Response>;
|
|
16
|
+
//# sourceMappingURL=worker-fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-fetch.d.ts","sourceRoot":"","sources":["../src/worker-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,wBAAwB,EAE9B,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD,MAAM,MAAM,UAAU,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB,IAAI,CAC7E,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,GAAG,KACL,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEjC,MAAM,WAAW,wBAAwB,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB;IAC5F,gEAAgE;IAChE,GAAG,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,wBAAwB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IAC/D,uEAAuE;IACvE,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;CAC1B;AAQD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,GAAG,SAAS,gBAAgB,GAAG,gBAAgB,EACpF,OAAO,EAAE,wBAAwB,CAAC,GAAG,EAAE,GAAG,CAAC,GAC1C,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,QAAQ,CAAC,CAS7D"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createApiDispatch, } from "./api-dispatch.js";
|
|
2
|
+
function isApiDispatch(api) {
|
|
3
|
+
return "dispatch" in api;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The Worker `fetch` entrypoint: API-prefixed requests go through the
|
|
7
|
+
* prefix-stripping dispatch, everything else goes to SSR. The app's
|
|
8
|
+
* `entry.ts` shrinks to bindings plus this factory call.
|
|
9
|
+
*/
|
|
10
|
+
export function createWorkerFetch(options) {
|
|
11
|
+
const api = isApiDispatch(options.api) ? options.api : createApiDispatch(options.api);
|
|
12
|
+
return async (request, env, ctx) => {
|
|
13
|
+
if (api.isApiRequest(new URL(request.url).pathname)) {
|
|
14
|
+
return api.dispatch(request, env, ctx);
|
|
15
|
+
}
|
|
16
|
+
return options.ssr(request, env, ctx);
|
|
17
|
+
};
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@voyant-travel/worker-runtime",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./api-dispatch": {
|
|
14
|
+
"types": "./dist/api-dispatch.d.ts",
|
|
15
|
+
"import": "./dist/api-dispatch.js",
|
|
16
|
+
"default": "./dist/api-dispatch.js"
|
|
17
|
+
},
|
|
18
|
+
"./ssr-manifest": {
|
|
19
|
+
"types": "./dist/ssr-manifest.d.ts",
|
|
20
|
+
"import": "./dist/ssr-manifest.js",
|
|
21
|
+
"default": "./dist/ssr-manifest.js"
|
|
22
|
+
},
|
|
23
|
+
"./worker-fetch": {
|
|
24
|
+
"types": "./dist/worker-fetch.d.ts",
|
|
25
|
+
"import": "./dist/worker-fetch.js",
|
|
26
|
+
"default": "./dist/worker-fetch.js"
|
|
27
|
+
},
|
|
28
|
+
"./types": {
|
|
29
|
+
"types": "./dist/types.d.ts",
|
|
30
|
+
"import": "./dist/types.js",
|
|
31
|
+
"default": "./dist/types.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "^6.0.2",
|
|
42
|
+
"vitest": "^4.1.2",
|
|
43
|
+
"@voyant-travel/voyant-typescript-config": "^0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/voyant-travel/voyant.git",
|
|
48
|
+
"directory": "packages/worker-runtime"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"lint": "biome check src/ tests/",
|
|
53
|
+
"test": "vitest run",
|
|
54
|
+
"build": "tsc -p tsconfig.json",
|
|
55
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo"
|
|
56
|
+
},
|
|
57
|
+
"main": "./dist/index.js",
|
|
58
|
+
"types": "./dist/index.d.ts"
|
|
59
|
+
}
|