@noverax/courier-sveltekit 0.0.0-stage → 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 +21 -0
- package/README.md +226 -2
- package/lib/handle.d.ts +25 -0
- package/lib/handle.js +24 -0
- package/lib/index.d.ts +49 -0
- package/lib/index.js +48 -0
- package/lib/request-context-CXZTuX53.d.ts +59 -0
- package/lib/request-context-ZdTULX9b.js +99 -0
- package/package.json +71 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NoveraX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,227 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<a href="https://courier.noverax.cloud">
|
|
4
|
+
<picture>
|
|
5
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/NoveraX/courier/master/docs/public/logos/courier-mark-light.svg" />
|
|
6
|
+
<img alt="Courier" src="https://raw.githubusercontent.com/NoveraX/courier/master/docs/public/logos/courier-mark-dark.svg" height="44" />
|
|
7
|
+
</picture>
|
|
8
|
+
</a>
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
<h1>Courier for SvelteKit</h1>
|
|
11
|
+
|
|
12
|
+
<p><strong>Typed calls to any backend, from one SvelteKit endpoint.</strong></p>
|
|
13
|
+
|
|
14
|
+
<p>One endpoint proxies every call your pages may make, and every hook, <code>load</code> function and form action calls your backend with the session of the request it serves.</p>
|
|
15
|
+
|
|
16
|
+
<p>
|
|
17
|
+
<a href="https://www.npmjs.com/package/@noverax/courier-sveltekit"><img alt="npm" src="https://img.shields.io/npm/v/@noverax/courier-sveltekit?style=flat-square&label=npm&labelColor=08090a&color=1c63d6" /></a>
|
|
18
|
+
<a href="https://www.typescriptlang.org/"><img alt="TypeScript strict" src="https://img.shields.io/badge/typescript-strict-1c63d6?style=flat-square&labelColor=08090a" /></a>
|
|
19
|
+
<a href="https://github.com/NoveraX/courier/blob/master/LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-1c63d6?style=flat-square&labelColor=08090a" /></a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
<p>
|
|
23
|
+
<a href="https://courier.noverax.cloud/docs/frameworks/sveltekit"><strong>SvelteKit guide</strong></a>
|
|
24
|
+
·
|
|
25
|
+
<a href="https://courier.noverax.cloud/docs">Documentation</a>
|
|
26
|
+
·
|
|
27
|
+
<a href="https://github.com/NoveraX/courier#readme">Courier</a>
|
|
28
|
+
·
|
|
29
|
+
<a href="https://github.com/NoveraX/courier/tree/master/packages/sveltekit">Source</a>
|
|
30
|
+
</p>
|
|
31
|
+
<br />
|
|
32
|
+
</div>
|
|
33
|
+
|
|
34
|
+
<br />
|
|
35
|
+
|
|
36
|
+
## Overview
|
|
37
|
+
|
|
38
|
+
The SvelteKit adapter serves Courier's proxy from a `+server.ts` endpoint on Web Standard `Request` and `Response`, and finds the request SvelteKit is serving so every call carries its session.
|
|
39
|
+
|
|
40
|
+
<table>
|
|
41
|
+
<tr>
|
|
42
|
+
<td width="33%" valign="top">
|
|
43
|
+
<h4>One endpoint</h4>
|
|
44
|
+
<code>courierHandler()</code> in <code>src/routes/api/request/+server.ts</code> answers every endpoint the boundary exposes.
|
|
45
|
+
</td>
|
|
46
|
+
<td width="33%" valign="top">
|
|
47
|
+
<h4>The user's session</h4>
|
|
48
|
+
Every hook, <code>load</code> function, form action and endpoint carries the session of the request being served.
|
|
49
|
+
</td>
|
|
50
|
+
<td width="33%" valign="top">
|
|
51
|
+
<h4>Cookies as written</h4>
|
|
52
|
+
The session cookies the API sets go to <code>cookies.set()</code> exactly as the API wrote them.
|
|
53
|
+
</td>
|
|
54
|
+
</tr>
|
|
55
|
+
<tr>
|
|
56
|
+
<td valign="top">
|
|
57
|
+
<h4>Production notes</h4>
|
|
58
|
+
What <code>ORIGIN</code>, <code>BODY_SIZE_LIMIT</code> and SvelteKit's CSRF check need under <code>adapter-node</code>.
|
|
59
|
+
</td>
|
|
60
|
+
<td valign="top">
|
|
61
|
+
<h4>Cancelled calls</h4>
|
|
62
|
+
When the browser cancels, the proxy stops its upstream request, watching the client's socket under <code>adapter-node</code>.
|
|
63
|
+
</td>
|
|
64
|
+
<td valign="top">
|
|
65
|
+
<h4>Secure by default</h4>
|
|
66
|
+
Another site's page is refused, an unknown host gets 421, and only the cookies the API's session names cross the proxy.
|
|
67
|
+
</td>
|
|
68
|
+
</tr>
|
|
69
|
+
</table>
|
|
70
|
+
|
|
71
|
+
<br />
|
|
72
|
+
|
|
73
|
+
## Quick start
|
|
74
|
+
|
|
75
|
+
In your SvelteKit project:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npx @noverax/create-courier
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
It adds the endpoint, registered from `hooks.server.ts`, a typed example API, Studio and a mock — showing each change to your files before it makes it — installs the packages and type-checks the result. For a new project: `npx @noverax/create-courier my-app --framework sveltekit`. The steps below do the same by hand.
|
|
82
|
+
|
|
83
|
+
<br />
|
|
84
|
+
|
|
85
|
+
## Install
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm install @noverax/courier-sveltekit @noverax/courier @sveltejs/kit
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
<br />
|
|
92
|
+
|
|
93
|
+
## Usage
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
// src/hooks.server.ts
|
|
97
|
+
import { registerSources } from "@noverax/courier"
|
|
98
|
+
import { sources } from "$lib/api"
|
|
99
|
+
|
|
100
|
+
registerSources(sources)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// src/routes/api/request/+server.ts
|
|
105
|
+
import { courierHandler } from "@noverax/courier-sveltekit"
|
|
106
|
+
|
|
107
|
+
const handler = courierHandler()
|
|
108
|
+
export const GET = handler
|
|
109
|
+
export const POST = handler
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`courierHandler()` is zero-arg — sources are read at request time from the core global registry populated by `registerSources()` in `hooks.server.ts`. The handler uses Web Standard `Request`/`Response` natively. GET requests are routed to query-type endpoints, POST requests handle mutations.
|
|
113
|
+
|
|
114
|
+
<br />
|
|
115
|
+
|
|
116
|
+
## In production
|
|
117
|
+
|
|
118
|
+
SvelteKit answers some calls before the proxy sees them. With `@sveltejs/adapter-node`:
|
|
119
|
+
|
|
120
|
+
- **Set `ORIGIN`** to the URL the app is served at, such as `ORIGIN=https://app.example.com node build`. Without it SvelteKit cannot tell the protocol it is served over and reports an `https:` request URL, which a browser's `http:` origin never matches, so the proxy refuses every `POST` with 403. Behind a proxy that sets forwarding headers, `PROTOCOL_HEADER` and `HOST_HEADER` do the same job.
|
|
121
|
+
- **Raise `BODY_SIZE_LIMIT`**, which is 512 KB by default and answers any larger body with its own 413 `Payload Too Large` — a 900 KB JSON call or any upload over it — whatever the proxy allows. The proxy reads a JSON call up to its `maxBodyBytes`, 1 MB, and an upload up to its `maxUploadBytes`, 10 MB; `BODY_SIZE_LIMIT=11M` covers both, and a larger `maxUploadBytes` needs a larger limit.
|
|
122
|
+
- **Callers without an `Origin`**: SvelteKit's CSRF check refuses a `multipart/form-data` or `text/plain` `POST` whose `Origin` does not match, and one with no `Origin` at all, such as a server-side script, with a plain-text 403. Browser pages on the app's own origin pass it.
|
|
123
|
+
|
|
124
|
+
In `vite dev`, a chunked body larger than the limit resets the connection instead of answering 413, and a client that declares an oversized upload can see the connection close before it has sent it all.
|
|
125
|
+
|
|
126
|
+
<br />
|
|
127
|
+
|
|
128
|
+
## With Frontend
|
|
129
|
+
|
|
130
|
+
Pair with `createProxyCaller` on the client:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { createProxyCaller } from "@noverax/courier"
|
|
134
|
+
import type { api } from "./api"
|
|
135
|
+
|
|
136
|
+
// The first argument is the key this source has in your `sources` map —
|
|
137
|
+
// it is what the proxy addresses, and it travels as `?_s=<name>`.
|
|
138
|
+
const caller = createProxyCaller<typeof api>("api", {
|
|
139
|
+
proxyPath: { http: "/api/request" },
|
|
140
|
+
})
|
|
141
|
+
const users = await caller.users.getAll()
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
<br />
|
|
145
|
+
|
|
146
|
+
## Cancelled calls
|
|
147
|
+
|
|
148
|
+
When the browser cancels a call — an aborted upload, a query it no longer needs — the proxy stops its own upstream request. SvelteKit's request signal does not report a client that leaves after its body has arrived, so under `@sveltejs/adapter-node` the handler watches the client's socket, from `platform.req`, instead. The Vite dev server exposes no connection: during `vite dev` a cancelled call still runs to completion upstream.
|
|
149
|
+
|
|
150
|
+
<br />
|
|
151
|
+
|
|
152
|
+
## The user's session
|
|
153
|
+
|
|
154
|
+
An API that keeps the user's session describes it in `defineApi`, and every call a hook, a `load` function, a form action or an endpoint makes carries the session of the request being served — the cookies the session names and nothing else of your application's — once the request can be found:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
// src/hooks.server.ts — SvelteKit 2.20 and later
|
|
158
|
+
import { getRequestEvent } from "$app/server"
|
|
159
|
+
import { registerRequestContext } from "@noverax/courier"
|
|
160
|
+
import { svelteKitRequestContext } from "@noverax/courier-sveltekit"
|
|
161
|
+
|
|
162
|
+
registerRequestContext(svelteKitRequestContext(getRequestEvent))
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
// src/hooks.server.ts — any SvelteKit 2, where the platform has AsyncLocalStorage
|
|
167
|
+
import { requestContextHandle } from "@noverax/courier-sveltekit/handle"
|
|
168
|
+
|
|
169
|
+
export const handle = requestContextHandle()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The session cookies the API sets go to `cookies.set()` exactly as the API wrote them, a `load` function's included.
|
|
173
|
+
|
|
174
|
+
<br />
|
|
175
|
+
|
|
176
|
+
## API
|
|
177
|
+
|
|
178
|
+
### `courierHandler()`
|
|
179
|
+
|
|
180
|
+
Creates a SvelteKit request handler. Returns a function compatible with SvelteKit's `GET`/`POST` server exports.
|
|
181
|
+
|
|
182
|
+
### `svelteKitRequestContext(getRequestEvent)`
|
|
183
|
+
|
|
184
|
+
The request SvelteKit 2.20 and later is serving, for `registerRequestContext`.
|
|
185
|
+
|
|
186
|
+
### `requestContextHandle()`
|
|
187
|
+
|
|
188
|
+
From `@noverax/courier-sveltekit/handle`: a `handle` hook that serves every request with its session at hand.
|
|
189
|
+
|
|
190
|
+
<br />
|
|
191
|
+
|
|
192
|
+
## Part of Courier
|
|
193
|
+
|
|
194
|
+
<table>
|
|
195
|
+
<tr>
|
|
196
|
+
<td width="50%" valign="top">
|
|
197
|
+
<a href="https://github.com/NoveraX/courier/tree/master/packages/core"><strong>@noverax/courier</strong></a><br />
|
|
198
|
+
Define your API once; call it typed from server code and the browser.
|
|
199
|
+
</td>
|
|
200
|
+
<td width="50%" valign="top">
|
|
201
|
+
<a href="https://github.com/NoveraX/courier/tree/master/packages/studio"><strong>@noverax/courier-studio</strong></a><br />
|
|
202
|
+
See every request your app makes, live.
|
|
203
|
+
</td>
|
|
204
|
+
</tr>
|
|
205
|
+
<tr>
|
|
206
|
+
<td width="50%" valign="top">
|
|
207
|
+
<a href="https://github.com/NoveraX/courier/tree/master/packages/mcp"><strong>@noverax/courier-mcp</strong></a><br />
|
|
208
|
+
Your API, as tools for AI agents.
|
|
209
|
+
</td>
|
|
210
|
+
<td width="50%" valign="top">
|
|
211
|
+
<a href="https://github.com/NoveraX/courier/tree/master/packages/create-courier"><strong>@noverax/create-courier</strong></a><br />
|
|
212
|
+
All of it in your project, in one command.
|
|
213
|
+
</td>
|
|
214
|
+
</tr>
|
|
215
|
+
</table>
|
|
216
|
+
|
|
217
|
+
<br />
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
|
|
221
|
+
[MIT](https://github.com/NoveraX/courier/blob/master/LICENSE)
|
|
222
|
+
|
|
223
|
+
<br />
|
|
224
|
+
|
|
225
|
+
<div align="center">
|
|
226
|
+
<sub>Part of <a href="https://github.com/NoveraX/courier">Courier</a> · built by <a href="https://github.com/NoveraX">NoveraX</a></sub>
|
|
227
|
+
</div>
|
package/lib/handle.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { t as SvelteKitServerEvent } from "./request-context-CXZTuX53.js";
|
|
2
|
+
//#region src/handle.d.ts
|
|
3
|
+
/** A SvelteKit `handle` hook, as far as it reads its input. */
|
|
4
|
+
export type RequestContextHandle = <TEvent extends SvelteKitServerEvent, TResponse>(input: {
|
|
5
|
+
event: TEvent;
|
|
6
|
+
resolve: (event: TEvent) => TResponse;
|
|
7
|
+
}) => TResponse;
|
|
8
|
+
/**
|
|
9
|
+
* A `handle` hook that serves every request with its session and headers at
|
|
10
|
+
* hand: each call the request's hooks, `load` functions, form actions and
|
|
11
|
+
* endpoints make — through every `await` — takes them from it. It works
|
|
12
|
+
* with any SvelteKit 2, on a platform with `AsyncLocalStorage`: Node, Bun,
|
|
13
|
+
* Deno, Cloudflare with `nodejs_als`.
|
|
14
|
+
*
|
|
15
|
+
* @returns The hook, alone or in a `sequence`
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // src/hooks.server.ts
|
|
19
|
+
* import { requestContextHandle } from "@noverax/courier-sveltekit/handle";
|
|
20
|
+
*
|
|
21
|
+
* export const handle = requestContextHandle();
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function requestContextHandle(): RequestContextHandle;
|
|
25
|
+
//#endregion
|
package/lib/handle.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { t as svelteKitIncomingRequest } from "./request-context-ZdTULX9b.js";
|
|
2
|
+
import { withIncomingRequest } from "@noverax/courier/node";
|
|
3
|
+
//#region src/handle.ts
|
|
4
|
+
/**
|
|
5
|
+
* A `handle` hook that serves every request with its session and headers at
|
|
6
|
+
* hand: each call the request's hooks, `load` functions, form actions and
|
|
7
|
+
* endpoints make — through every `await` — takes them from it. It works
|
|
8
|
+
* with any SvelteKit 2, on a platform with `AsyncLocalStorage`: Node, Bun,
|
|
9
|
+
* Deno, Cloudflare with `nodejs_als`.
|
|
10
|
+
*
|
|
11
|
+
* @returns The hook, alone or in a `sequence`
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // src/hooks.server.ts
|
|
15
|
+
* import { requestContextHandle } from "@noverax/courier-sveltekit/handle";
|
|
16
|
+
*
|
|
17
|
+
* export const handle = requestContextHandle();
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function requestContextHandle() {
|
|
21
|
+
return ({ event, resolve }) => withIncomingRequest(svelteKitIncomingRequest(event), () => resolve(event));
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { requestContextHandle };
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { n as svelteKitIncomingRequest, r as svelteKitRequestContext, t as SvelteKitServerEvent } from "./request-context-CXZTuX53.js";
|
|
2
|
+
import { ClientConnection, ConnectResult, HandlerConfig } from "@noverax/courier";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The parts of SvelteKit's `RequestEvent` the handler reads. `platform.req`
|
|
6
|
+
* is the Node request `@sveltejs/adapter-node` passes along.
|
|
7
|
+
*/
|
|
8
|
+
export interface SvelteKitRequestEvent {
|
|
9
|
+
request: Request;
|
|
10
|
+
platform?: {
|
|
11
|
+
req?: {
|
|
12
|
+
socket?: ClientConnection;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a SvelteKit request handler for the courier proxy.
|
|
18
|
+
*
|
|
19
|
+
* Sources are read from the core global registry populated by
|
|
20
|
+
* `attachRuntime()` — call `await attachRuntime()` in your server
|
|
21
|
+
* bootstrap before the first request.
|
|
22
|
+
*
|
|
23
|
+
* SvelteKit's request signal fires only while the body is still uploading,
|
|
24
|
+
* so a call the browser cancels later would run to completion upstream.
|
|
25
|
+
* Under `@sveltejs/adapter-node` the handler watches the client's socket
|
|
26
|
+
* instead and stops the upstream call when it closes. The Vite dev server
|
|
27
|
+
* exposes no connection, so there a cancelled call still runs to the end.
|
|
28
|
+
*
|
|
29
|
+
* On its first request it tells an attached Studio which sources it serves,
|
|
30
|
+
* so the Playground can run them in this process.
|
|
31
|
+
*
|
|
32
|
+
* @param config - Handler options, including the `allowedOrigins` whose pages
|
|
33
|
+
* may call the proxy: every call and every stream is checked against them.
|
|
34
|
+
* Omit it to accept the defaults.
|
|
35
|
+
* @returns A handler for the route's `GET` and `POST` exports
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // src/routes/api/request/+server.ts
|
|
40
|
+
* import { courierHandler } from "@noverax/courier-sveltekit";
|
|
41
|
+
*
|
|
42
|
+
* const handler = courierHandler();
|
|
43
|
+
* export const GET = handler;
|
|
44
|
+
* export const POST = handler;
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function courierHandler<TConnect extends ConnectResult = ConnectResult>(config?: HandlerConfig<TConnect>): (event: SvelteKitRequestEvent) => Promise<Response>;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { type SvelteKitServerEvent, svelteKitIncomingRequest, svelteKitRequestContext };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { n as svelteKitRequestContext, t as svelteKitIncomingRequest } from "./request-context-ZdTULX9b.js";
|
|
2
|
+
import { createHandler, handleWhileConnected } from "@noverax/courier";
|
|
3
|
+
import { announceWhenRegistered } from "@noverax/courier/announce";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a SvelteKit request handler for the courier proxy.
|
|
7
|
+
*
|
|
8
|
+
* Sources are read from the core global registry populated by
|
|
9
|
+
* `attachRuntime()` — call `await attachRuntime()` in your server
|
|
10
|
+
* bootstrap before the first request.
|
|
11
|
+
*
|
|
12
|
+
* SvelteKit's request signal fires only while the body is still uploading,
|
|
13
|
+
* so a call the browser cancels later would run to completion upstream.
|
|
14
|
+
* Under `@sveltejs/adapter-node` the handler watches the client's socket
|
|
15
|
+
* instead and stops the upstream call when it closes. The Vite dev server
|
|
16
|
+
* exposes no connection, so there a cancelled call still runs to the end.
|
|
17
|
+
*
|
|
18
|
+
* On its first request it tells an attached Studio which sources it serves,
|
|
19
|
+
* so the Playground can run them in this process.
|
|
20
|
+
*
|
|
21
|
+
* @param config - Handler options, including the `allowedOrigins` whose pages
|
|
22
|
+
* may call the proxy: every call and every stream is checked against them.
|
|
23
|
+
* Omit it to accept the defaults.
|
|
24
|
+
* @returns A handler for the route's `GET` and `POST` exports
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* // src/routes/api/request/+server.ts
|
|
29
|
+
* import { courierHandler } from "@noverax/courier-sveltekit";
|
|
30
|
+
*
|
|
31
|
+
* const handler = courierHandler();
|
|
32
|
+
* export const GET = handler;
|
|
33
|
+
* export const POST = handler;
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
function courierHandler(config) {
|
|
37
|
+
const { handle } = createHandler(config);
|
|
38
|
+
const announce = announceWhenRegistered({
|
|
39
|
+
transports: ["http", "sse"],
|
|
40
|
+
framework: "sveltekit"
|
|
41
|
+
});
|
|
42
|
+
return ({ request, platform }) => {
|
|
43
|
+
announce();
|
|
44
|
+
return handleWhileConnected(handle, request, platform?.req?.socket);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { courierHandler, svelteKitIncomingRequest, svelteKitRequestContext };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { IncomingRequest, RequestContextProvider } from "@noverax/courier";
|
|
2
|
+
//#region src/request-context.d.ts
|
|
3
|
+
/** A cookie's options, as SvelteKit's `cookies.set()` takes them. */
|
|
4
|
+
interface SvelteKitCookieOptions {
|
|
5
|
+
path: string;
|
|
6
|
+
domain?: string;
|
|
7
|
+
expires?: Date;
|
|
8
|
+
maxAge?: number;
|
|
9
|
+
secure: boolean;
|
|
10
|
+
httpOnly: boolean;
|
|
11
|
+
sameSite?: "strict" | "lax" | "none";
|
|
12
|
+
partitioned?: boolean;
|
|
13
|
+
priority?: "low" | "medium" | "high";
|
|
14
|
+
encode: (value: string) => string;
|
|
15
|
+
}
|
|
16
|
+
/** The parts of SvelteKit's `RequestEvent` the session reads. */
|
|
17
|
+
interface SvelteKitServerEvent {
|
|
18
|
+
request: Request;
|
|
19
|
+
url: URL;
|
|
20
|
+
cookies: {
|
|
21
|
+
set(name: string, value: string, options: SvelteKitCookieOptions): void;
|
|
22
|
+
};
|
|
23
|
+
getClientAddress(): string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The request SvelteKit 2.20 and later is serving, found with its own
|
|
27
|
+
* `getRequestEvent` — in a hook, a server `load`, a form action or an
|
|
28
|
+
* endpoint. Where the platform lacks `AsyncLocalStorage`, SvelteKit knows it
|
|
29
|
+
* only until the code first waits. Registering it twice registers it once.
|
|
30
|
+
*
|
|
31
|
+
* @param getRequestEvent - `getRequestEvent` from `$app/server`
|
|
32
|
+
* @returns The provider, for `registerRequestContext`
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* // src/hooks.server.ts
|
|
36
|
+
* import { getRequestEvent } from "$app/server";
|
|
37
|
+
* import { registerRequestContext } from "@noverax/courier";
|
|
38
|
+
* import { svelteKitRequestContext } from "@noverax/courier-sveltekit";
|
|
39
|
+
*
|
|
40
|
+
* registerRequestContext(svelteKitRequestContext(getRequestEvent));
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function svelteKitRequestContext(getRequestEvent: () => SvelteKitServerEvent): RequestContextProvider;
|
|
44
|
+
/**
|
|
45
|
+
* A SvelteKit request event, as the session reads it: its origin is the one
|
|
46
|
+
* SvelteKit addresses the app by, its user's address the one the adapter
|
|
47
|
+
* vouches for, and the cookies the API sets go to `cookies.set()` exactly
|
|
48
|
+
* as the API wrote them.
|
|
49
|
+
*
|
|
50
|
+
* @param event - The request event
|
|
51
|
+
* @returns The request, as the session reads it
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* withIncomingRequest(svelteKitIncomingRequest(event), () => resolve(event));
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function svelteKitIncomingRequest(event: SvelteKitServerEvent): IncomingRequest;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { svelteKitIncomingRequest as n, svelteKitRequestContext as r, SvelteKitServerEvent as t };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createLogger } from "@noverax/courier";
|
|
2
|
+
//#region src/request-context.ts
|
|
3
|
+
const log = createLogger("SvelteKit");
|
|
4
|
+
const providers = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* The request SvelteKit 2.20 and later is serving, found with its own
|
|
7
|
+
* `getRequestEvent` — in a hook, a server `load`, a form action or an
|
|
8
|
+
* endpoint. Where the platform lacks `AsyncLocalStorage`, SvelteKit knows it
|
|
9
|
+
* only until the code first waits. Registering it twice registers it once.
|
|
10
|
+
*
|
|
11
|
+
* @param getRequestEvent - `getRequestEvent` from `$app/server`
|
|
12
|
+
* @returns The provider, for `registerRequestContext`
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // src/hooks.server.ts
|
|
16
|
+
* import { getRequestEvent } from "$app/server";
|
|
17
|
+
* import { registerRequestContext } from "@noverax/courier";
|
|
18
|
+
* import { svelteKitRequestContext } from "@noverax/courier-sveltekit";
|
|
19
|
+
*
|
|
20
|
+
* registerRequestContext(svelteKitRequestContext(getRequestEvent));
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function svelteKitRequestContext(getRequestEvent) {
|
|
24
|
+
let provider = providers.get(getRequestEvent);
|
|
25
|
+
if (provider === void 0) {
|
|
26
|
+
provider = () => eventNow(getRequestEvent);
|
|
27
|
+
providers.set(getRequestEvent, provider);
|
|
28
|
+
}
|
|
29
|
+
return provider;
|
|
30
|
+
}
|
|
31
|
+
function eventNow(getRequestEvent) {
|
|
32
|
+
try {
|
|
33
|
+
return svelteKitIncomingRequest(getRequestEvent());
|
|
34
|
+
} catch (error) {
|
|
35
|
+
log.debug("No request event to take a session from", error);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A SvelteKit request event, as the session reads it: its origin is the one
|
|
41
|
+
* SvelteKit addresses the app by, its user's address the one the adapter
|
|
42
|
+
* vouches for, and the cookies the API sets go to `cookies.set()` exactly
|
|
43
|
+
* as the API wrote them.
|
|
44
|
+
*
|
|
45
|
+
* @param event - The request event
|
|
46
|
+
* @returns The request, as the session reads it
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* withIncomingRequest(svelteKitIncomingRequest(event), () => resolve(event));
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function svelteKitIncomingRequest(event) {
|
|
53
|
+
return {
|
|
54
|
+
key: event,
|
|
55
|
+
method: event.request.method,
|
|
56
|
+
origin: event.url.origin,
|
|
57
|
+
headers: event.request.headers,
|
|
58
|
+
get clientAddress() {
|
|
59
|
+
return clientAddressOf(event);
|
|
60
|
+
},
|
|
61
|
+
setCookie: (relayed) => setCookieIn(event, relayed)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function clientAddressOf(event) {
|
|
65
|
+
try {
|
|
66
|
+
return event.getClientAddress();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
log.debug("The adapter gave no client address", error);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function setCookieIn(event, { cookie }) {
|
|
73
|
+
try {
|
|
74
|
+
event.cookies.set(cookie.name, cookie.value, optionsOf(cookie));
|
|
75
|
+
return true;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (error instanceof Error && error.message.includes("after the response has been generated")) return "the response had already been generated";
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function optionsOf(cookie) {
|
|
82
|
+
return {
|
|
83
|
+
path: cookie.path ?? "/",
|
|
84
|
+
...cookie.domain !== void 0 && { domain: cookie.domain },
|
|
85
|
+
...cookie.expires !== void 0 && { expires: new Date(cookie.expires) },
|
|
86
|
+
...cookie.maxAge !== void 0 && { maxAge: cookie.maxAge },
|
|
87
|
+
secure: cookie.secure === true,
|
|
88
|
+
httpOnly: cookie.httpOnly === true,
|
|
89
|
+
...cookie.sameSite !== void 0 && { sameSite: lower(cookie.sameSite) },
|
|
90
|
+
...cookie.partitioned && { partitioned: true },
|
|
91
|
+
...cookie.priority !== void 0 && { priority: lower(cookie.priority) },
|
|
92
|
+
encode: (value) => value
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function lower(value) {
|
|
96
|
+
return value.toLowerCase();
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
export { svelteKitRequestContext as n, svelteKitIncomingRequest as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noverax/courier-sveltekit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SvelteKit adapter for Courier: an endpoint that proxies typed calls to any backend, with the user's session in load functions and form actions",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"courier",
|
|
7
|
+
"sveltekit",
|
|
8
|
+
"svelte",
|
|
9
|
+
"load",
|
|
10
|
+
"form-actions",
|
|
11
|
+
"ssr",
|
|
12
|
+
"proxy",
|
|
13
|
+
"bff",
|
|
14
|
+
"typescript",
|
|
15
|
+
"type-safe",
|
|
16
|
+
"api-client",
|
|
17
|
+
"session"
|
|
18
|
+
],
|
|
19
|
+
"author": {
|
|
20
|
+
"name": "NoveraX",
|
|
21
|
+
"url": "https://github.com/NoveraX"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/NoveraX/courier.git",
|
|
27
|
+
"directory": "packages/sveltekit"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://courier.noverax.cloud/docs/frameworks/sveltekit",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/NoveraX/courier/issues"
|
|
32
|
+
},
|
|
33
|
+
"type": "module",
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22"
|
|
36
|
+
},
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"main": "./lib/index.js",
|
|
39
|
+
"module": "./lib/index.js",
|
|
40
|
+
"types": "./lib/index.d.ts",
|
|
41
|
+
"exports": {
|
|
42
|
+
"./package.json": "./package.json",
|
|
43
|
+
".": {
|
|
44
|
+
"types": "./lib/index.d.ts",
|
|
45
|
+
"import": "./lib/index.js",
|
|
46
|
+
"default": "./lib/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./handle": {
|
|
49
|
+
"types": "./lib/handle.d.ts",
|
|
50
|
+
"import": "./lib/handle.js",
|
|
51
|
+
"default": "./lib/handle.js"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"lib",
|
|
56
|
+
"README.md",
|
|
57
|
+
"LICENSE"
|
|
58
|
+
],
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsdown",
|
|
61
|
+
"typecheck": "tsc --noEmit",
|
|
62
|
+
"dev": "tsdown --watch"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"@noverax/courier": "^0.1.0"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@noverax/courier": "*"
|
|
72
|
+
}
|
|
73
|
+
}
|