@supatype/ssr 0.9.0 → 0.9.2
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +9 -7
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +28 -0
- package/README.md +58 -8
- package/dist/cookie-parser.d.ts.map +1 -1
- package/dist/cookie-parser.js +8 -29
- package/dist/cookie-parser.js.map +1 -1
- package/dist/jwt-claims.d.ts +13 -0
- package/dist/jwt-claims.d.ts.map +1 -0
- package/dist/jwt-claims.js +30 -0
- package/dist/jwt-claims.js.map +1 -0
- package/dist/next.d.ts +45 -0
- package/dist/next.d.ts.map +1 -0
- package/dist/next.js +146 -0
- package/dist/next.js.map +1 -0
- package/dist/server-client.d.ts +8 -1
- package/dist/server-client.d.ts.map +1 -1
- package/dist/server-client.js +16 -15
- package/dist/server-client.js.map +1 -1
- package/dist/server-env.d.ts +20 -0
- package/dist/server-env.d.ts.map +1 -0
- package/dist/server-env.js +25 -0
- package/dist/server-env.js.map +1 -0
- package/package.json +6 -2
- package/src/cookie-parser.ts +7 -28
- package/src/jwt-claims.ts +30 -0
- package/src/next.ts +160 -0
- package/src/server-client.ts +28 -4
- package/src/server-env.ts +26 -0
- package/tests/next.test.ts +205 -0
- package/tests/server-client-env.test.ts +108 -0
- package/tsconfig.tsbuildinfo +1 -1
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @supatype/ssr@0.9.
|
|
2
|
+
> @supatype/ssr@0.9.2 test /home/runner/work/supatype/supatype/packages/ssr
|
|
3
3
|
> vitest run --maxWorkers=50%
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
[1m[46m RUN [49m[22m [36mv4.0.18 [39m[90m/home/runner/work/supatype/supatype/packages/ssr[39m
|
|
7
7
|
|
|
8
|
-
[32m✓[39m tests/
|
|
9
|
-
[32m✓[39m tests/server-client.test.ts [2m([22m[
|
|
8
|
+
[32m✓[39m tests/next.test.ts [2m([22m[2m25 tests[22m[2m)[22m[33m 454[2mms[22m[39m
|
|
9
|
+
[32m✓[39m tests/server-client-env.test.ts [2m([22m[2m6 tests[22m[2m)[22m[32m 266[2mms[22m[39m
|
|
10
|
+
[32m✓[39m tests/server-client.test.ts [2m([22m[2m9 tests[22m[2m)[22m[33m 400[2mms[22m[39m
|
|
11
|
+
[32m✓[39m tests/cookie-parser.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 81[2mms[22m[39m
|
|
10
12
|
|
|
11
|
-
[2m Test Files [22m [1m[
|
|
12
|
-
[2m Tests [22m [1m[
|
|
13
|
-
[2m Start at [22m
|
|
14
|
-
[2m Duration [22m 5.
|
|
13
|
+
[2m Test Files [22m [1m[32m4 passed[39m[22m[90m (4)[39m
|
|
14
|
+
[2m Tests [22m [1m[32m43 passed[39m[22m[90m (43)[39m
|
|
15
|
+
[2m Start at [22m 12:01:48
|
|
16
|
+
[2m Duration [22m 5.40s[2m (transform 3.04s, setup 0ms, import 4.31s, tests 1.20s, environment 1ms)[22m
|
|
15
17
|
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
* **next:** `@supatype/ssr/next` exports `GET` for `app/supatype-env.js/route.ts`. It answers
|
|
8
|
+
`window.__SUPATYPE_ENV__ = {"url":"...","anonKey":"..."};` from `SUPATYPE_URL` and
|
|
9
|
+
`SUPATYPE_ANON_KEY` as the running server has them, with the Supatype server's own body and headers
|
|
10
|
+
(`application/javascript; charset=utf-8`, `Cache-Control: no-cache`, `X-Content-Type-Options:
|
|
11
|
+
nosniff`). Switched off with `SUPATYPE_PUBLIC_ENV=false` (or `0`, `no`, `off`) it is 404
|
|
12
|
+
`no-store`. Misconfigured it is still 200, `no-store`, with
|
|
13
|
+
`window.__SUPATYPE_ENV__ = {"error":"..."};` naming every setting to fix and never echoing a key,
|
|
14
|
+
because a browser runs no script answered with an error status and tells the page nothing of
|
|
15
|
+
why; the client throws that reason on its first call. The faults: `SUPATYPE_ANON_KEY` or
|
|
16
|
+
`SUPATYPE_URL` unset (a page given only the key would fail for want of an address; with both
|
|
17
|
+
unset, or a service role key and no address, the reason names each fault on its own line, the
|
|
18
|
+
key's first, so one visit fixes them all), `SUPATYPE_PUBLIC_ENV` holding a word it cannot read
|
|
19
|
+
(the reason names the words it takes), or the anon key variable holding a service role token. No
|
|
20
|
+
service role value is ever read.
|
|
21
|
+
With `<Script src="/supatype-env.js" strategy="beforeInteractive" />`
|
|
22
|
+
in the root layout, one `next build` runs against any deployment. The route file writes
|
|
23
|
+
`export const dynamic = "force-dynamic"` itself, since Next's build does not recognize a
|
|
24
|
+
re-exported segment config.
|
|
25
|
+
* **server-client:** `createServerClient(options)`, and `undefined` for either of `url` and
|
|
26
|
+
`anonKey`, take them from `SUPATYPE_URL` and `SUPATYPE_ANON_KEY` in the server's environment,
|
|
27
|
+
read when the client is created, and where one is unset from `NEXT_PUBLIC_SUPATYPE_URL` or
|
|
28
|
+
`NEXT_PUBLIC_SUPATYPE_ANON_KEY`. `createServerClient(url, anonKey, options)` works as before.
|
package/README.md
CHANGED
|
@@ -16,18 +16,25 @@ Create server client factories that read cookies on each request: one for Server
|
|
|
16
16
|
cannot write cookies, and one for Route Handlers and Server Actions, which can:
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
|
-
// lib/supatype-server.ts
|
|
19
|
+
// lib/supatype-server.ts, in the folder that holds app/ (the project root, or src/ when the app is
|
|
20
|
+
// in src/app), where create-next-app's `@/*` alias points.
|
|
20
21
|
import { createServerClient } from "@supatype/ssr"
|
|
21
22
|
import { cookies } from "next/headers"
|
|
23
|
+
import { connection } from "next/server"
|
|
22
24
|
import type { Database } from "@/types/database"
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
// The address and anon key come from SUPATYPE_URL and SUPATYPE_ANON_KEY in the server's
|
|
27
|
+
// environment, read on each call, so the same build runs against whichever deployment it is
|
|
28
|
+
// started for. Pass them first, as createServerClient(url, anonKey, options), to use others.
|
|
26
29
|
|
|
27
|
-
// Server Components: no `setAll`, so the client never refreshes the session.
|
|
30
|
+
// Server Components: no `setAll`, so the client never refreshes the session. `connection()` renders
|
|
31
|
+
// the component at request time: the client reads the clock to check the session's expiry, which
|
|
32
|
+
// Next 16 with `cacheComponents` logs as an error while prerendering. It is in Next 15 and later;
|
|
33
|
+
// on Next 14 leave it out.
|
|
28
34
|
export async function createClient() {
|
|
29
35
|
const cookieStore = await cookies()
|
|
30
|
-
|
|
36
|
+
await connection()
|
|
37
|
+
return createServerClient<Database>({
|
|
31
38
|
cookies: {
|
|
32
39
|
getAll() { return cookieStore.getAll() },
|
|
33
40
|
},
|
|
@@ -37,7 +44,7 @@ export async function createClient() {
|
|
|
37
44
|
// Route Handlers and Server Actions: a refreshed session goes back to the browser.
|
|
38
45
|
export async function createWritableClient() {
|
|
39
46
|
const cookieStore = await cookies()
|
|
40
|
-
return createServerClient<Database>(
|
|
47
|
+
return createServerClient<Database>({
|
|
41
48
|
cookies: {
|
|
42
49
|
getAll() { return cookieStore.getAll() },
|
|
43
50
|
setAll(cookiesToSet) {
|
|
@@ -72,6 +79,47 @@ const { data: { session } } = await supatype.auth.getSession()
|
|
|
72
79
|
// is: its access token may have expired, since that client never refreshes it.
|
|
73
80
|
```
|
|
74
81
|
|
|
82
|
+
### The browser client's values at run time (`@supatype/ssr/next`)
|
|
83
|
+
|
|
84
|
+
`next build` prerenders static pages without the deployment's values. Serve them to the page when
|
|
85
|
+
it loads instead, from the same two server variables, with a route and one line in the root layout:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
// app/supatype-env.js/route.ts
|
|
89
|
+
export { GET } from "@supatype/ssr/next"
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
// app/layout.tsx
|
|
94
|
+
import Script from "next/script"
|
|
95
|
+
|
|
96
|
+
// inside <body>, before anything else:
|
|
97
|
+
<Script src="/supatype-env.js" strategy="beforeInteractive" />
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The route needs no segment config, and must not export `dynamic`: Next 16 with `cacheComponents`
|
|
101
|
+
on, as a new create-next-app has it, refuses that with a 500. `GET` reads the request, which makes
|
|
102
|
+
Next 14 and Next 16 with `cacheComponents` render it per request rather than prerender it with the
|
|
103
|
+
build machine's values; Next 15, and Next 16 without `cacheComponents`, render every `GET` per
|
|
104
|
+
request anyway. A route written by `supatype init` 0.9.1 has `export const dynamic = "force-dynamic"`;
|
|
105
|
+
`supatype generate` (run by the project's `prepare` on install) replaces it while it is still as
|
|
106
|
+
init wrote it, and warns about one changed since. Otherwise delete that line. `GET` answers
|
|
107
|
+
`window.__SUPATYPE_ENV__ = {"url":"...","anonKey":"..."};` from `SUPATYPE_URL` and
|
|
108
|
+
`SUPATYPE_ANON_KEY` as the server has them on each request, with `Cache-Control: no-cache`, the same
|
|
109
|
+
body and headers the Supatype server gives its own `/supatype-env.js`. Switched off with
|
|
110
|
+
`SUPATYPE_PUBLIC_ENV=false` it answers 404 `no-store`. Misconfigured it still answers 200, with
|
|
111
|
+
`Cache-Control: no-store` and `window.__SUPATYPE_ENV__ = {"error":"..."};`, since a browser runs no
|
|
112
|
+
script answered with an error status and tells the page nothing of why. The browser client throws
|
|
113
|
+
that reason on its first call. It names every setting to fix, one line each and the key's first:
|
|
114
|
+
`SUPATYPE_ANON_KEY` or `SUPATYPE_URL` unset (both named when both are), `SUPATYPE_PUBLIC_ENV` set to
|
|
115
|
+
a word other than true/1/yes/on or false/0/no/off (blank counts as on), or the anon key variable
|
|
116
|
+
holding a service role token. No key is echoed, and no service role value is ever read.
|
|
117
|
+
|
|
118
|
+
The browser client, created with no `url` or `anonKey` (or with `NEXT_PUBLIC_` names left unset at
|
|
119
|
+
build time), reads `window.__SUPATYPE_ENV__` at its first request. A client created at module scope
|
|
120
|
+
does not throw while the build prerenders a page; its first request throws, naming what to set,
|
|
121
|
+
when neither the build nor the page supplied the values.
|
|
122
|
+
|
|
75
123
|
### Cookie adapter interface
|
|
76
124
|
|
|
77
125
|
`createServerClient` accepts any framework's cookie store via the `CookieAdapter` interface:
|
|
@@ -120,9 +168,11 @@ createServerClient(url, anonKey, {
|
|
|
120
168
|
|
|
121
169
|
## API
|
|
122
170
|
|
|
123
|
-
### `createServerClient<TDatabase>(url, anonKey, options)`
|
|
171
|
+
### `createServerClient<TDatabase>(url, anonKey, options)`, or `createServerClient<TDatabase>(options)`
|
|
124
172
|
|
|
125
|
-
|
|
173
|
+
`url` and `anonKey` default to `SUPATYPE_URL` and `SUPATYPE_ANON_KEY` from the server's environment
|
|
174
|
+
when left out, `undefined` or empty, and where one of those is unset to `NEXT_PUBLIC_SUPATYPE_URL` or
|
|
175
|
+
`NEXT_PUBLIC_SUPATYPE_ANON_KEY`. Returns a fully-typed `SupatypeClient` pre-loaded with the user's session from cookies. The returned client has the same API as the browser client, `.from()`, `.auth`, `.storage`, `.rpc()`, etc.
|
|
126
176
|
|
|
127
177
|
| Option | Type | Default | Description |
|
|
128
178
|
|--------|------|---------|-------------|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie-parser.d.ts","sourceRoot":"","sources":["../src/cookie-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAQ,MAAM,kBAAkB,CAAA;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"cookie-parser.d.ts","sourceRoot":"","sources":["../src/cookie-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAQ,MAAM,kBAAkB,CAAA;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAG7C,oFAAoF;AACpF,eAAO,MAAM,qBAAqB,OAAO,CAAA;AAEzC,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,EAC/C,MAAM,GAAE,MAA8B,GACrC,OAAO,GAAG,IAAI,CAoBhB;AAsDD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,EAAE,MAAM,GAAE,MAA8B,GAAG,WAAW,CAc1G"}
|
package/dist/cookie-parser.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { decodeBase64, isJsonObject, jwtClaims } from "./jwt-claims.js";
|
|
1
2
|
/** The prefix `@supatype/client` names its session cookie with: `st-auth-token`. */
|
|
2
3
|
export const DEFAULT_COOKIE_PREFIX = "st";
|
|
3
4
|
export function parseSessionFromCookies(cookies, prefix = DEFAULT_COOKIE_PREFIX) {
|
|
@@ -18,10 +19,6 @@ export function parseSessionFromCookies(cookies, prefix = DEFAULT_COOKIE_PREFIX)
|
|
|
18
19
|
}
|
|
19
20
|
return null;
|
|
20
21
|
}
|
|
21
|
-
/** True for a JSON object: not null, not an array. */
|
|
22
|
-
function isJsonObject(value) {
|
|
23
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
|
-
}
|
|
25
22
|
function parseSessionValue(value) {
|
|
26
23
|
try {
|
|
27
24
|
return normalizeSession(JSON.parse(decodeURIComponent(value)));
|
|
@@ -33,16 +30,6 @@ function parseSessionValue(value) {
|
|
|
33
30
|
catch { /* not base64 JSON */ }
|
|
34
31
|
return null;
|
|
35
32
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Base64 or base64url text decoded as UTF-8, with `atob` and `TextDecoder` rather than Node's
|
|
38
|
-
* `Buffer`: the server client also runs on edge runtimes, which have no `Buffer`. Throws on input
|
|
39
|
-
* that is not base64.
|
|
40
|
-
*/
|
|
41
|
-
function decodeBase64(text) {
|
|
42
|
-
const standard = text.replace(/-/g, "+").replace(/_/g, "/");
|
|
43
|
-
const binary = atob(standard + "=".repeat((4 - (standard.length % 4)) % 4));
|
|
44
|
-
return new TextDecoder().decode(Uint8Array.from(binary, (c) => c.charCodeAt(0)));
|
|
45
|
-
}
|
|
46
33
|
/**
|
|
47
34
|
* The session a cookie holds. One whose access token has expired is kept when it carries a refresh
|
|
48
35
|
* token: the first request the client makes refreshes it, and the server client writes the new
|
|
@@ -66,22 +53,14 @@ function normalizeSession(r) {
|
|
|
66
53
|
};
|
|
67
54
|
}
|
|
68
55
|
function isJwtExpired(token) {
|
|
69
|
-
|
|
70
|
-
|
|
56
|
+
// A token that is not a JWT, or whose payload is not an object, reads as expired.
|
|
57
|
+
const payload = jwtClaims(token);
|
|
58
|
+
if (payload === null)
|
|
71
59
|
return true;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return true;
|
|
77
|
-
const exp = typeof payload["exp"] === "number" ? payload["exp"] : null;
|
|
78
|
-
if (exp === null)
|
|
79
|
-
return false;
|
|
80
|
-
return Date.now() / 1000 > exp;
|
|
81
|
-
}
|
|
82
|
-
catch {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
60
|
+
const exp = typeof payload["exp"] === "number" ? payload["exp"] : null;
|
|
61
|
+
if (exp === null)
|
|
62
|
+
return false;
|
|
63
|
+
return Date.now() / 1000 > exp;
|
|
85
64
|
}
|
|
86
65
|
function normalizeUser(raw) {
|
|
87
66
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie-parser.js","sourceRoot":"","sources":["../src/cookie-parser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cookie-parser.js","sourceRoot":"","sources":["../src/cookie-parser.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAEvE,oFAAoF;AACpF,MAA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KAAK,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,CAAA;AAC1G,CAAC;AAED,SAAS,aAAa,CAAC,IAAU;IAC/B,OAAO;QACL,EAAE,EAAE,IAAI,CAAC,EAAE;QACX,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;QACtD,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;QACtD,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;QACnD,YAAY,EAAE,IAAI,CAAC,WAAW;QAC9B,aAAa,EAAE,IAAI,CAAC,YAAY;QAChC,UAAU,EAAE,IAAI,CAAC,SAAS;QAC1B,UAAU,EAAE,IAAI,CAAC,SAAS;KAC3B,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a JWT's claims without verifying it, in a way every runtime the server client reaches
|
|
3
|
+
* supports: `atob` and `TextDecoder` rather than Node's `Buffer`, which edge runtimes do not have.
|
|
4
|
+
* Shared by the cookie parser (a session's expiry) and the Next.js route (a service role token
|
|
5
|
+
* where the anon key belongs).
|
|
6
|
+
*/
|
|
7
|
+
/** True for a JSON object: not null, not an array. */
|
|
8
|
+
export declare function isJsonObject(value: unknown): value is Record<string, unknown>;
|
|
9
|
+
/** Base64 or base64url text decoded as UTF-8. Throws on input that is not base64. */
|
|
10
|
+
export declare function decodeBase64(text: string): string;
|
|
11
|
+
/** The payload of a three-part JWT, or null for anything else. Not verified. */
|
|
12
|
+
export declare function jwtClaims(token: string): Record<string, unknown> | null;
|
|
13
|
+
//# sourceMappingURL=jwt-claims.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-claims.d.ts","sourceRoot":"","sources":["../src/jwt-claims.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,sDAAsD;AACtD,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE7E;AAED,qFAAqF;AACrF,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAIjD;AAED,gFAAgF;AAChF,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CASvE"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a JWT's claims without verifying it, in a way every runtime the server client reaches
|
|
3
|
+
* supports: `atob` and `TextDecoder` rather than Node's `Buffer`, which edge runtimes do not have.
|
|
4
|
+
* Shared by the cookie parser (a session's expiry) and the Next.js route (a service role token
|
|
5
|
+
* where the anon key belongs).
|
|
6
|
+
*/
|
|
7
|
+
/** True for a JSON object: not null, not an array. */
|
|
8
|
+
export function isJsonObject(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
/** Base64 or base64url text decoded as UTF-8. Throws on input that is not base64. */
|
|
12
|
+
export function decodeBase64(text) {
|
|
13
|
+
const standard = text.replace(/-/g, "+").replace(/_/g, "/");
|
|
14
|
+
const binary = atob(standard + "=".repeat((4 - (standard.length % 4)) % 4));
|
|
15
|
+
return new TextDecoder().decode(Uint8Array.from(binary, (c) => c.charCodeAt(0)));
|
|
16
|
+
}
|
|
17
|
+
/** The payload of a three-part JWT, or null for anything else. Not verified. */
|
|
18
|
+
export function jwtClaims(token) {
|
|
19
|
+
const payload = token.split(".");
|
|
20
|
+
if (payload.length !== 3 || payload[1] === undefined)
|
|
21
|
+
return null;
|
|
22
|
+
try {
|
|
23
|
+
const claims = JSON.parse(decodeBase64(payload[1]));
|
|
24
|
+
return isJsonObject(claims) ? claims : null;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=jwt-claims.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-claims.js","sourceRoot":"","sources":["../src/jwt-claims.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,sDAAsD;AACtD,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC7E,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IAC3D,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;IAC3E,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAClF,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAChC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,SAAS;QAAE,OAAO,IAAI,CAAA;IACjE,IAAI,CAAC;QACH,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC5D,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/supatype-env.js` for a Next.js app: the route that gives a page the deployment's API address
|
|
3
|
+
* and anon key when it loads, so one `next build` runs against any deployment.
|
|
4
|
+
*
|
|
5
|
+
* `next build` prerenders the static pages without the deployment's values, and the client then
|
|
6
|
+
* reads them from `window.__SUPATYPE_ENV__` at its first request. This route sets that global from
|
|
7
|
+
* `SUPATYPE_URL` and `SUPATYPE_ANON_KEY` as the running server has them, with the same body,
|
|
8
|
+
* headers and misconfigured answer as the Supatype server's own `/supatype-env.js`. Put it at
|
|
9
|
+
* `app/supatype-env.js/route.ts`:
|
|
10
|
+
*
|
|
11
|
+
* export { GET } from "@supatype/ssr/next"
|
|
12
|
+
*
|
|
13
|
+
* It must not be prerendered, or the build machine's values (usually none) would be served as a
|
|
14
|
+
* static file. `GET` reads the request, which is what keeps it per request on every Next version:
|
|
15
|
+
* Next 14 and Next 16 with `cacheComponents` stop prerendering a `GET` handler that reads its
|
|
16
|
+
* request, and Next 15 and 16 without it render every `GET` per request anyway. The route file
|
|
17
|
+
* needs no segment config, and must not have one: Next 16 with `cacheComponents` answers 500 for a
|
|
18
|
+
* route that exports `dynamic` (0.9.1 release test, RB6). Then load it before the app in the root
|
|
19
|
+
* layout:
|
|
20
|
+
*
|
|
21
|
+
* <Script src="/supatype-env.js" strategy="beforeInteractive" />
|
|
22
|
+
*
|
|
23
|
+
* The anon key is not a secret: every browser that loads the app holds it, and row-level security
|
|
24
|
+
* protects the data. The service role key is never read here, and an anon key variable holding a
|
|
25
|
+
* service role token is refused rather than published.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated Do not export it from the route: Next reads no re-exported segment config, and Next 16
|
|
29
|
+
* with `cacheComponents` refuses a `dynamic` one. `GET` keeps itself per request (see above). Kept
|
|
30
|
+
* so an import of it still builds.
|
|
31
|
+
*/
|
|
32
|
+
export declare const dynamic = "force-dynamic";
|
|
33
|
+
/**
|
|
34
|
+
* `GET /supatype-env.js`: the script setting `window.__SUPATYPE_ENV__`. Misconfigured, it is still
|
|
35
|
+
* 200 and sets only `error`, the reason, because a browser runs no script answered with an error
|
|
36
|
+
* status and tells the page nothing of why: this is the one way the client can name the fault on
|
|
37
|
+
* its first call. Switched off it is 404, as the Supatype server answers.
|
|
38
|
+
*
|
|
39
|
+
* Next passes the request; reading it keeps the route out of the build's prerender (see the top of
|
|
40
|
+
* this file). Nothing in it changes the answer. It is required, not optional: Next type-checks a
|
|
41
|
+
* route handler's first parameter as `Request | NextRequest`, and `Request | undefined` failed
|
|
42
|
+
* `next build` in a Next 15 app.
|
|
43
|
+
*/
|
|
44
|
+
export declare function GET(request: Request): Response;
|
|
45
|
+
//# sourceMappingURL=next.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next.d.ts","sourceRoot":"","sources":["../src/next.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAKH;;;;GAIG;AACH,eAAO,MAAM,OAAO,kBAAkB,CAAA;AActC;;;;;;;;;;GAUG;AACH,wBAAgB,GAAG,CAAC,OAAO,EAAE,OAAO,GAAG,QAAQ,CAO9C"}
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/supatype-env.js` for a Next.js app: the route that gives a page the deployment's API address
|
|
3
|
+
* and anon key when it loads, so one `next build` runs against any deployment.
|
|
4
|
+
*
|
|
5
|
+
* `next build` prerenders the static pages without the deployment's values, and the client then
|
|
6
|
+
* reads them from `window.__SUPATYPE_ENV__` at its first request. This route sets that global from
|
|
7
|
+
* `SUPATYPE_URL` and `SUPATYPE_ANON_KEY` as the running server has them, with the same body,
|
|
8
|
+
* headers and misconfigured answer as the Supatype server's own `/supatype-env.js`. Put it at
|
|
9
|
+
* `app/supatype-env.js/route.ts`:
|
|
10
|
+
*
|
|
11
|
+
* export { GET } from "@supatype/ssr/next"
|
|
12
|
+
*
|
|
13
|
+
* It must not be prerendered, or the build machine's values (usually none) would be served as a
|
|
14
|
+
* static file. `GET` reads the request, which is what keeps it per request on every Next version:
|
|
15
|
+
* Next 14 and Next 16 with `cacheComponents` stop prerendering a `GET` handler that reads its
|
|
16
|
+
* request, and Next 15 and 16 without it render every `GET` per request anyway. The route file
|
|
17
|
+
* needs no segment config, and must not have one: Next 16 with `cacheComponents` answers 500 for a
|
|
18
|
+
* route that exports `dynamic` (0.9.1 release test, RB6). Then load it before the app in the root
|
|
19
|
+
* layout:
|
|
20
|
+
*
|
|
21
|
+
* <Script src="/supatype-env.js" strategy="beforeInteractive" />
|
|
22
|
+
*
|
|
23
|
+
* The anon key is not a secret: every browser that loads the app holds it, and row-level security
|
|
24
|
+
* protects the data. The service role key is never read here, and an anon key variable holding a
|
|
25
|
+
* service role token is refused rather than published.
|
|
26
|
+
*/
|
|
27
|
+
import { jwtClaims } from "./jwt-claims.js";
|
|
28
|
+
import { SERVER_ANON_KEY_ENV, SERVER_URL_ENV, serverEnv } from "./server-env.js";
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Do not export it from the route: Next reads no re-exported segment config, and Next 16
|
|
31
|
+
* with `cacheComponents` refuses a `dynamic` one. `GET` keeps itself per request (see above). Kept
|
|
32
|
+
* so an import of it still builds.
|
|
33
|
+
*/
|
|
34
|
+
export const dynamic = "force-dynamic";
|
|
35
|
+
const PUBLIC_ENV_SWITCH = "SUPATYPE_PUBLIC_ENV";
|
|
36
|
+
const SCRIPT_GLOBAL = "window.__SUPATYPE_ENV__";
|
|
37
|
+
/** `<`, `>`, `&` and the two JavaScript line separators, escaped in the body as Go's JSON does. */
|
|
38
|
+
const SCRIPT_UNSAFE = new Set([0x3c, 0x3e, 0x26, 0x2028, 0x2029]);
|
|
39
|
+
/**
|
|
40
|
+
* `GET /supatype-env.js`: the script setting `window.__SUPATYPE_ENV__`. Misconfigured, it is still
|
|
41
|
+
* 200 and sets only `error`, the reason, because a browser runs no script answered with an error
|
|
42
|
+
* status and tells the page nothing of why: this is the one way the client can name the fault on
|
|
43
|
+
* its first call. Switched off it is 404, as the Supatype server answers.
|
|
44
|
+
*
|
|
45
|
+
* Next passes the request; reading it keeps the route out of the build's prerender (see the top of
|
|
46
|
+
* this file). Nothing in it changes the answer. It is required, not optional: Next type-checks a
|
|
47
|
+
* route handler's first parameter as `Request | NextRequest`, and `Request | undefined` failed
|
|
48
|
+
* `next build` in a Next 15 app.
|
|
49
|
+
*/
|
|
50
|
+
export function GET(request) {
|
|
51
|
+
void request.headers;
|
|
52
|
+
const env = readPublicEnv();
|
|
53
|
+
if (env === "off")
|
|
54
|
+
return notFound();
|
|
55
|
+
// A reason is kept by nothing, so the fixed configuration is read on the next load. Values are
|
|
56
|
+
// revalidated on every load: they change when the deployment's configuration does.
|
|
57
|
+
return script(env, "error" in env ? "no-store" : "no-cache");
|
|
58
|
+
}
|
|
59
|
+
/** The values to serve, read from the environment now, or why there are none. */
|
|
60
|
+
function readPublicEnv() {
|
|
61
|
+
const enabled = publicEnvSwitch(serverEnv(PUBLIC_ENV_SWITCH));
|
|
62
|
+
if (enabled === false)
|
|
63
|
+
return "off";
|
|
64
|
+
// A word it cannot read is a fault to fix, not a choice to turn the route off.
|
|
65
|
+
if (enabled !== true)
|
|
66
|
+
return { error: enabled.invalid };
|
|
67
|
+
// Both, or neither: a page given only the key fails at its first request for want of an address,
|
|
68
|
+
// and the reason naming the variables says why, where the deployment is configured. Every fault
|
|
69
|
+
// is named in one reason, one per line and the key's first, so a single visit fixes them all.
|
|
70
|
+
const url = publicUrl(serverEnv(SERVER_URL_ENV));
|
|
71
|
+
const anonKey = serverEnv(SERVER_ANON_KEY_ENV);
|
|
72
|
+
const keyFault = anonKeyFault(anonKey);
|
|
73
|
+
const urlFault = url === undefined ? notSet(SERVER_URL_ENV) : undefined;
|
|
74
|
+
if (url === undefined || anonKey === undefined || keyFault !== undefined) {
|
|
75
|
+
const faults = [keyFault, urlFault].filter((fault) => fault !== undefined);
|
|
76
|
+
return { error: faults.join("\n") };
|
|
77
|
+
}
|
|
78
|
+
return { url, anonKey };
|
|
79
|
+
}
|
|
80
|
+
/** Why the anon key variable cannot be served, or undefined when it can. */
|
|
81
|
+
function anonKeyFault(anonKey) {
|
|
82
|
+
if (anonKey === undefined)
|
|
83
|
+
return notSet(SERVER_ANON_KEY_ENV);
|
|
84
|
+
if (jwtClaims(anonKey)?.["role"] !== "service_role")
|
|
85
|
+
return undefined;
|
|
86
|
+
return (`public env unavailable: ${SERVER_ANON_KEY_ENV} holds a service_role token, which must never ` +
|
|
87
|
+
"reach a browser. Set it to the project's anon key.");
|
|
88
|
+
}
|
|
89
|
+
/** The reason line for a variable the server does not have, as the Supatype server words it. */
|
|
90
|
+
function notSet(name) {
|
|
91
|
+
return `public env unavailable: ${name} is not set on this server`;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* `SUPATYPE_PUBLIC_ENV` as the server reads it: on when blank or 1, true, yes or on; off for 0, false,
|
|
95
|
+
* no or off. Any other word stops the server from starting; here, with no start to stop, the route
|
|
96
|
+
* is a reason naming the variable and the words it takes.
|
|
97
|
+
*/
|
|
98
|
+
function publicEnvSwitch(raw) {
|
|
99
|
+
const value = (raw ?? "").toLowerCase();
|
|
100
|
+
if (["", "1", "true", "yes", "on"].includes(value))
|
|
101
|
+
return true;
|
|
102
|
+
if (["0", "false", "no", "off"].includes(value))
|
|
103
|
+
return false;
|
|
104
|
+
return {
|
|
105
|
+
invalid: `public env unavailable: ${PUBLIC_ENV_SWITCH}=${JSON.stringify(raw)} is not a switch value. Use ` +
|
|
106
|
+
"true, 1, yes or on (or leave it blank) to serve it, and false, 0, no or off to turn it off.",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** The API root, as the server gives it: no trailing slash, no auth service mount. */
|
|
110
|
+
function publicUrl(raw) {
|
|
111
|
+
const trimmed = raw?.replace(/\/+$/, "").replace(/\/auth\/v1$/, "");
|
|
112
|
+
return trimmed === undefined || trimmed === "" ? undefined : trimmed;
|
|
113
|
+
}
|
|
114
|
+
/** JSON that cannot close the script element or end the statement early. */
|
|
115
|
+
function escapeForScript(json) {
|
|
116
|
+
let out = "";
|
|
117
|
+
for (const ch of json) {
|
|
118
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
119
|
+
out += SCRIPT_UNSAFE.has(code) ? `\\u${code.toString(16).padStart(4, "0")}` : ch;
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
/** The statement setting `window.__SUPATYPE_ENV__` to `doc`, with the server's headers. */
|
|
124
|
+
function script(doc, cacheControl) {
|
|
125
|
+
const body = `${SCRIPT_GLOBAL} = ${escapeForScript(JSON.stringify(doc))};\n`;
|
|
126
|
+
return new Response(body, {
|
|
127
|
+
status: 200,
|
|
128
|
+
headers: {
|
|
129
|
+
"Content-Type": "application/javascript; charset=utf-8",
|
|
130
|
+
"Cache-Control": cacheControl,
|
|
131
|
+
"X-Content-Type-Options": "nosniff",
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/** A 404 nothing keeps, so switching the route back on is seen at once, as the server answers. */
|
|
136
|
+
function notFound() {
|
|
137
|
+
return new Response("404 page not found\n", {
|
|
138
|
+
status: 404,
|
|
139
|
+
headers: {
|
|
140
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
141
|
+
"Cache-Control": "no-store",
|
|
142
|
+
"X-Content-Type-Options": "nosniff",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=next.js.map
|
package/dist/next.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next.js","sourceRoot":"","sources":["../src/next.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAEhF;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,eAAe,CAAA;AAEtC,MAAM,iBAAiB,GAAG,qBAAqB,CAAA;AAC/C,MAAM,aAAa,GAAG,yBAAyB,CAAA;AAE/C,mGAAmG;AACnG,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;AAQjE;;;;;;;;;;GAUG;AACH,MAAM,UAAU,GAAG,CAAC,OAAgB;IAClC,KAAK,OAAO,CAAC,OAAO,CAAA;IACpB,MAAM,GAAG,GAAG,aAAa,EAAE,CAAA;IAC3B,IAAI,GAAG,KAAK,KAAK;QAAE,OAAO,QAAQ,EAAE,CAAA;IACpC,+FAA+F;IAC/F,mFAAmF;IACnF,OAAO,MAAM,CAAC,GAAG,EAAE,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;AAC9D,CAAC;AAED,iFAAiF;AACjF,SAAS,aAAa;IACpB,MAAM,OAAO,GAAG,eAAe,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAAA;IAC7D,IAAI,OAAO,KAAK,KAAK;QAAE,OAAO,KAAK,CAAA;IACnC,+EAA+E;IAC/E,IAAI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3F,SAAS,MAAM,CAAC,GAAW,EAAE,YAAqC;IAChE,MAAM,IAAI,GAAG,GAAG,aAAa,MAAM,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,KAAK,CAAA;IAC5E,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE;QACxB,MAAM,EAAE,GAAG;QACX,OAAO,EAAE;YACP,cAAc,EAAE,uCAAuC;YACvD,eAAe,EAAE,YAAY;YAC7B,wBAAwB,EAAE,SAAS;SACpC;KACF,CAAC,CAAA;AACJ,CAAC;AAED,kGAAkG;AAClG,SAAS,QAAQ;IACf,OAAO,IAAI,QAAQ,CAAC,sBAAsB,EAAE;QAC1C,MAAM,EAAE,GAAG;QACX,OAAO,EAAE;YACP,cAAc,EAAE,2BAA2B;YAC3C,eAAe,EAAE,UAAU;YAC3B,wBAAwB,EAAE,SAAS;SACpC;KACF,CAAC,CAAA;AACJ,CAAC"}
|
package/dist/server-client.d.ts
CHANGED
|
@@ -11,6 +11,13 @@ import type { ServerClientOptions } from "./types.js";
|
|
|
11
11
|
* Without `setAll` (a Server Component) it never refreshes: an expired access token is sent as it
|
|
12
12
|
* is and refused. A refresh there would spend the browser's refresh token and return one the
|
|
13
13
|
* browser never receives, so the browser's own next refresh would be refused and sign it out.
|
|
14
|
+
*
|
|
15
|
+
* The address and anon key default to `SUPATYPE_URL` and `SUPATYPE_ANON_KEY` from the server's
|
|
16
|
+
* environment, read now: pass only the options, or `undefined` for either. These are the same two
|
|
17
|
+
* variables `@supatype/ssr/next` serves to the page. Where one is unset, `NEXT_PUBLIC_SUPATYPE_URL`
|
|
18
|
+
* or `NEXT_PUBLIC_SUPATYPE_ANON_KEY` is used instead. With none given or set, the client's first
|
|
19
|
+
* request throws the error naming them.
|
|
14
20
|
*/
|
|
15
|
-
export declare function createServerClient<TDatabase extends AnyDatabase = AnyDatabase>(
|
|
21
|
+
export declare function createServerClient<TDatabase extends AnyDatabase = AnyDatabase>(options: ServerClientOptions): SupatypeClient<TDatabase>;
|
|
22
|
+
export declare function createServerClient<TDatabase extends AnyDatabase = AnyDatabase>(url: string | undefined, anonKey: string | undefined, options: ServerClientOptions): SupatypeClient<TDatabase>;
|
|
16
23
|
//# sourceMappingURL=server-client.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-client.d.ts","sourceRoot":"","sources":["../src/server-client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"server-client.d.ts","sourceRoot":"","sources":["../src/server-client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAGnE,OAAO,KAAK,EAAiB,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAEpE;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,SAAS,WAAW,GAAG,WAAW,EAC5E,OAAO,EAAE,mBAAmB,GAC3B,cAAc,CAAC,SAAS,CAAC,CAAA;AAC5B,wBAAgB,kBAAkB,CAAC,SAAS,SAAS,WAAW,GAAG,WAAW,EAC5E,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,mBAAmB,GAC3B,cAAc,CAAC,SAAS,CAAC,CAAA"}
|
package/dist/server-client.js
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
1
|
import { createClient } from "@supatype/client";
|
|
2
2
|
import { DEFAULT_COOKIE_PREFIX, parseSessionFromCookies, sessionCookie } from "./cookie-parser.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* It keeps no session of its own and runs no refresh timer: the cookie is the only store, and the
|
|
7
|
-
* client lives as long as the request. With `setAll`, a refresh happens only when a request it
|
|
8
|
-
* makes finds the access token expired, and the rotated session goes back to the browser through
|
|
9
|
-
* `setAll`, so the browser holds the new refresh token rather than the one the refresh just spent.
|
|
10
|
-
*
|
|
11
|
-
* Without `setAll` (a Server Component) it never refreshes: an expired access token is sent as it
|
|
12
|
-
* is and refused. A refresh there would spend the browser's refresh token and return one the
|
|
13
|
-
* browser never receives, so the browser's own next refresh would be refused and sign it out.
|
|
14
|
-
*/
|
|
15
|
-
export function createServerClient(url, anonKey, options) {
|
|
3
|
+
import { SERVER_ANON_KEY_NAMES, SERVER_URL_NAMES, serverEnv } from "./server-env.js";
|
|
4
|
+
export function createServerClient(...args) {
|
|
5
|
+
const [url, anonKey, options] = args.length === 1 ? [undefined, undefined, args[0]] : args;
|
|
16
6
|
const prefix = options.cookiePrefix ?? DEFAULT_COOKIE_PREFIX;
|
|
17
7
|
const session = parseSessionFromCookies(options.cookies.getAll(), prefix);
|
|
18
8
|
const client = createClient({
|
|
19
|
-
url,
|
|
20
|
-
anonKey,
|
|
9
|
+
url: givenOrServerEnv(url, SERVER_URL_NAMES),
|
|
10
|
+
anonKey: givenOrServerEnv(anonKey, SERVER_ANON_KEY_NAMES),
|
|
21
11
|
auth: {
|
|
22
12
|
persistSession: false,
|
|
23
13
|
autoRefreshToken: false,
|
|
@@ -29,6 +19,17 @@ export function createServerClient(url, anonKey, options) {
|
|
|
29
19
|
writeSessionChanges(client, options.cookies, prefix);
|
|
30
20
|
return client;
|
|
31
21
|
}
|
|
22
|
+
/** The value given, unless it is missing or blank, else the first of the server's `names` that is set. */
|
|
23
|
+
function givenOrServerEnv(given, names) {
|
|
24
|
+
if (given !== undefined && given.trim() !== "")
|
|
25
|
+
return given;
|
|
26
|
+
for (const name of names) {
|
|
27
|
+
const value = serverEnv(name);
|
|
28
|
+
if (value !== undefined)
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
32
33
|
/**
|
|
33
34
|
* Send every change to the client's session (a refresh, a sign-in, a sign-out) back to the browser
|
|
34
35
|
* through the adapter's `setAll`, so the next request reads the new tokens rather than the old ones.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-client.js","sourceRoot":"","sources":["../src/server-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAE/C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"server-client.js","sourceRoot":"","sources":["../src/server-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAE/C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClG,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AA6BpF,MAAM,UAAU,kBAAkB,CAChC,GAAG,IAA2F;IAE9F,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAC1F,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,IAAI,qBAAqB,CAAA;IAC5D,MAAM,OAAO,GAAG,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,CAAA;IACzE,MAAM,MAAM,GAAG,YAAY,CAAY;QACrC,GAAG,EAAE,gBAAgB,CAAC,GAAG,EAAE,gBAAgB,CAAC;QAC5C,OAAO,EAAE,gBAAgB,CAAC,OAAO,EAAE,qBAAqB,CAAC;QACzD,IAAI,EAAE;YACJ,cAAc,EAAE,KAAK;YACrB,gBAAgB,EAAE,KAAK;YACvB,mBAAmB,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS;YACzD,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
|
3
|
+
* anon key from: `createServerClient`'s defaults, and the values `@supatype/ssr/next` serves to the
|
|
4
|
+
* page. They are read where the app runs, so one build serves whichever deployment it runs as. A
|
|
5
|
+
* `NEXT_PUBLIC_` name, fixed into the build, is only the server client's fallback.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SERVER_URL_ENV = "SUPATYPE_URL";
|
|
8
|
+
export declare const SERVER_ANON_KEY_ENV = "SUPATYPE_ANON_KEY";
|
|
9
|
+
/**
|
|
10
|
+
* Where `createServerClient` looks for each value: the server's own name first, read where the app
|
|
11
|
+
* runs, then the Next.js build-time name an app may already set. The route serves only the first.
|
|
12
|
+
*/
|
|
13
|
+
export declare const SERVER_URL_NAMES: readonly ["SUPATYPE_URL", "NEXT_PUBLIC_SUPATYPE_URL"];
|
|
14
|
+
export declare const SERVER_ANON_KEY_NAMES: readonly ["SUPATYPE_ANON_KEY", "NEXT_PUBLIC_SUPATYPE_ANON_KEY"];
|
|
15
|
+
/**
|
|
16
|
+
* A variable from the server's environment, trimmed; undefined when unset or blank, and where there
|
|
17
|
+
* is no `process` at all. Read by name at call time, so no bundler can fix its build-time value in.
|
|
18
|
+
*/
|
|
19
|
+
export declare function serverEnv(name: string): string | undefined;
|
|
20
|
+
//# sourceMappingURL=server-env.d.ts.map
|