@supatype/client 0.9.1 → 0.10.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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +112 -72
- package/.turbo/turbo-typecheck.log +2 -2
- package/CHANGELOG.md +123 -20
- package/README.md +18 -0
- package/dist/auth.d.ts +13 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +53 -13
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +4 -1
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +2 -1
- package/dist/bindings.js.map +1 -1
- package/dist/channel-watch.d.ts +42 -0
- package/dist/channel-watch.d.ts.map +1 -0
- package/dist/channel-watch.js +59 -0
- package/dist/channel-watch.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +31 -0
- package/dist/errors.js.map +1 -1
- package/dist/exact-values.d.ts +18 -5
- package/dist/exact-values.d.ts.map +1 -1
- package/dist/exact-values.js +76 -12
- package/dist/exact-values.js.map +1 -1
- package/dist/field-kinds-registry.d.ts +1 -1
- package/dist/field-kinds-registry.d.ts.map +1 -1
- package/dist/field-kinds-registry.js +20 -2
- package/dist/field-kinds-registry.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/latest-rows.d.ts +15 -0
- package/dist/latest-rows.d.ts.map +1 -1
- package/dist/latest-rows.js +14 -7
- package/dist/latest-rows.js.map +1 -1
- package/dist/public-env.d.ts +13 -0
- package/dist/public-env.d.ts.map +1 -0
- package/dist/public-env.js +23 -0
- package/dist/public-env.js.map +1 -0
- package/dist/query-cache.d.ts +2 -1
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +7 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +12 -4
- package/dist/query.js.map +1 -1
- package/dist/realtime-refusals.d.ts +35 -0
- package/dist/realtime-refusals.d.ts.map +1 -0
- package/dist/realtime-refusals.js +73 -0
- package/dist/realtime-refusals.js.map +1 -0
- package/dist/realtime.d.ts +87 -8
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +304 -51
- package/dist/realtime.js.map +1 -1
- package/dist/runtime-env.d.ts +2 -0
- package/dist/runtime-env.d.ts.map +1 -1
- package/dist/runtime-env.js +45 -5
- package/dist/runtime-env.js.map +1 -1
- package/dist/table-subscription.d.ts +27 -11
- package/dist/table-subscription.d.ts.map +1 -1
- package/dist/table-subscription.js +159 -25
- package/dist/table-subscription.js.map +1 -1
- package/dist/types.d.ts +34 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/auth.ts +55 -12
- package/src/bindings.ts +4 -1
- package/src/channel-watch.ts +86 -0
- package/src/errors.ts +54 -0
- package/src/exact-values.ts +101 -13
- package/src/field-kinds-registry.ts +2 -3
- package/src/index.ts +14 -2
- package/src/latest-rows.ts +24 -7
- package/src/public-env.ts +22 -0
- package/src/query-cache.ts +2 -1
- package/src/query.ts +12 -4
- package/src/realtime-refusals.ts +100 -0
- package/src/realtime.ts +385 -56
- package/src/runtime-env.ts +53 -5
- package/src/table-subscription.ts +190 -32
- package/src/types.ts +40 -1
- package/tests/auth-restore-user.test.ts +84 -0
- package/tests/auth-sso.test.ts +32 -1
- package/tests/auth.test.ts +11 -0
- package/tests/channel-watch-private.test.ts +105 -0
- package/tests/channel-watch.test.ts +97 -0
- package/tests/exact-response.test.ts +101 -0
- package/tests/exact-values.test.ts +57 -0
- package/tests/examples-runtime-env.test.ts +135 -0
- package/tests/field-kinds-registry.test.ts +12 -0
- package/tests/helpers/close-event.ts +12 -0
- package/tests/helpers/fake-client.ts +38 -0
- package/tests/helpers/mock-websocket.ts +22 -0
- package/tests/helpers/reactive-options-binding.ts +99 -0
- package/tests/public-env.test.ts +43 -0
- package/tests/query-cache.test.ts +1 -1
- package/tests/query.test.ts +39 -2
- package/tests/realtime-connection.test.ts +228 -0
- package/tests/realtime-exact-values.test.ts +42 -0
- package/tests/realtime-multi-tenant-integration.test.ts +0 -1
- package/tests/realtime-private-channels.test.ts +377 -0
- package/tests/realtime-refusal-error.test.ts +80 -0
- package/tests/realtime-rows-after-broadcast.test.ts +205 -0
- package/tests/realtime-session-auth.test.ts +0 -1
- package/tests/realtime-shared-channel.test.ts +9 -27
- package/tests/realtime-typed-channels.test.ts +49 -0
- package/tests/runtime-env-expo.test.ts +58 -0
- package/tests/runtime-env.test.ts +49 -0
- package/tests/subscribe-frame.test.ts +48 -0
- package/tests/table-subscription-rows.test.ts +136 -0
- package/tests/table-subscription.test.ts +4 -3
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/type-tests/declared-channels.ts +30 -0
- package/type-tests/tsconfig.json +4 -0
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TableRelationship } from "./select-types.js";
|
|
2
|
+
import type { CacheStatus } from "./query-cache.js";
|
|
2
3
|
import type { SupatypeError } from "./errors.js";
|
|
3
4
|
export interface UserIdentity {
|
|
4
5
|
id: string;
|
|
@@ -65,6 +66,11 @@ export interface SignInWithSSOOptions {
|
|
|
65
66
|
* accepts it either way.
|
|
66
67
|
*/
|
|
67
68
|
flowType?: AuthFlowType | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Studio's own sign-in. The auth server then returns the browser to its Studio address and
|
|
71
|
+
* ignores `redirectTo`, as `studio=1` does for `signInWithOAuth`. Apps leave this unset.
|
|
72
|
+
*/
|
|
73
|
+
studio?: boolean | undefined;
|
|
68
74
|
}
|
|
69
75
|
/** Sign in through the SAML identity provider registered for the user's email domain. */
|
|
70
76
|
export interface SignInWithSSODomain {
|
|
@@ -192,7 +198,7 @@ export interface QueryResult<TData> {
|
|
|
192
198
|
error: SupatypeError | null;
|
|
193
199
|
count: number | null;
|
|
194
200
|
meta?: {
|
|
195
|
-
cacheStatus?:
|
|
201
|
+
cacheStatus?: CacheStatus | undefined;
|
|
196
202
|
/** Present only when the response named restricted columns. Absent means "not stated". */
|
|
197
203
|
maskedFields?: MaskedField[] | undefined;
|
|
198
204
|
} | undefined;
|
|
@@ -275,6 +281,33 @@ export interface SupatypeModels {
|
|
|
275
281
|
}
|
|
276
282
|
export interface SupatypeBuckets {
|
|
277
283
|
}
|
|
284
|
+
export interface SupatypeChannels {
|
|
285
|
+
}
|
|
286
|
+
/** What one `{param}` of a channel pattern holds: a number for an Integer parameter, else text. */
|
|
287
|
+
type TopicParam<TParams, TName extends string> = TParams extends {
|
|
288
|
+
[K in TName]: number;
|
|
289
|
+
} ? `${number}` : string;
|
|
290
|
+
/** A pattern with every `{param}` and a trailing `*` replaced by the text that can stand there. */
|
|
291
|
+
type TopicOfPattern<TPattern extends string, TParams> = TPattern extends `${infer Head}{${infer Name}}${infer Rest}` ? `${Head}${TopicParam<TParams, Name>}${TopicOfPattern<Rest, TParams>}` : TPattern extends `${infer Head}*` ? `${Head}${string}` : TPattern;
|
|
292
|
+
/**
|
|
293
|
+
* The topics a set of declared channels can name, as template literal types. `{ "lot:{id}": ... }`
|
|
294
|
+
* gives `` `lot:${string}` ``, `"lobby:*"` gives `` `lobby:${string}` ``, and an Integer parameter
|
|
295
|
+
* gives `` `${number}` `` in its place. Generated code fills {@link SupatypeChannels}; this takes the
|
|
296
|
+
* declarations as an argument so it can be checked without augmenting that interface.
|
|
297
|
+
*/
|
|
298
|
+
export type DeclaredTopics<TChannels> = {
|
|
299
|
+
[P in keyof TChannels & string]: TopicOfPattern<P, TChannels[P] extends {
|
|
300
|
+
params: infer R;
|
|
301
|
+
} ? R : never>;
|
|
302
|
+
}[keyof TChannels & string];
|
|
303
|
+
/** The topics the project's schema declares. `never` until `supatype generate` has run. */
|
|
304
|
+
export type DeclaredTopic = DeclaredTopics<SupatypeChannels>;
|
|
305
|
+
/**
|
|
306
|
+
* A channel name: a declared topic, which autocompletes, or any other string, which an `open`
|
|
307
|
+
* realtime server still admits. The `string & {}` keeps the literal suggestions from collapsing
|
|
308
|
+
* into plain `string`.
|
|
309
|
+
*/
|
|
310
|
+
export type ChannelName = DeclaredTopic | (string & {});
|
|
278
311
|
export interface SupatypeFunctions {
|
|
279
312
|
}
|
|
280
313
|
/**
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAIhD,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAClC;AAED,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,UAAU,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;IACzC,MAAM,EAAE,UAAU,GAAG,YAAY,CAAA;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC3B;AAED,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,KAAK,CAAC,EAAE,MAA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AAS,CAAA;IAC9D;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IACpC;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAChC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAA;CAC9E"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAkdA;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAA;AAEnC;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,UAAU,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supatype/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Supatype client SDK: typed REST, auth, storage, realtime",
|
|
6
6
|
"type": "module",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc --project tsconfig.json",
|
|
33
33
|
"dev": "tsc --project tsconfig.json --watch",
|
|
34
|
-
"typecheck": "tsc --project tests/tsconfig.json --noEmit",
|
|
34
|
+
"typecheck": "tsc --project tests/tsconfig.json --noEmit && tsc --project type-tests/tsconfig.json --noEmit",
|
|
35
35
|
"test": "vitest run --maxWorkers=50%",
|
|
36
36
|
"clean": "rm -rf dist *.tsbuildinfo"
|
|
37
37
|
}
|
package/src/auth.ts
CHANGED
|
@@ -72,6 +72,20 @@ const PROVIDER_SUFFIX = "-oauth-provider"
|
|
|
72
72
|
* sent back in the URL for a sign-in `signInWithSSO` started.
|
|
73
73
|
*/
|
|
74
74
|
export const SSO_PROVIDER = "sso"
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The domain `signInWithSSO` looks a provider up by, from what a person typed: the part after the
|
|
78
|
+
* last `@` of a work email, or the whole of a bare domain, trimmed and lower-cased. Null when there
|
|
79
|
+
* is nothing to look up, so a form can refuse it before asking the server.
|
|
80
|
+
*
|
|
81
|
+
* One implementation for `<SSOForm>` and Studio's sign-in, which each had their own and disagreed:
|
|
82
|
+
* Studio found no domain in a bare one, and the form sent a domain typed after `@ ` with its space.
|
|
83
|
+
*/
|
|
84
|
+
export function ssoDomain(input: string): string | null {
|
|
85
|
+
const value = input.trim().toLowerCase()
|
|
86
|
+
const domain = value.slice(value.lastIndexOf("@") + 1).trim()
|
|
87
|
+
return domain === "" ? null : domain
|
|
88
|
+
}
|
|
75
89
|
const DEBUG_AUTH = typeof process !== "undefined" && process.env["NEXT_PUBLIC_SUPATYPE_DEBUG_AUTH"] === "1"
|
|
76
90
|
|
|
77
91
|
/**
|
|
@@ -128,17 +142,41 @@ function eitherKey<T>(
|
|
|
128
142
|
return guard(b) ? b : undefined
|
|
129
143
|
}
|
|
130
144
|
|
|
131
|
-
/**
|
|
132
|
-
function
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
if (
|
|
145
|
+
/** `appMetadata` as `app_metadata`: the key `_parseUser` and its helpers read. */
|
|
146
|
+
function snakeCaseKey(key: string): string {
|
|
147
|
+
return key.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** A stored object with each camelCase key also under its snake_case name, unless that is set. Shallow. */
|
|
151
|
+
function withSnakeKeys(stored: Record<string, unknown>): Record<string, unknown> {
|
|
152
|
+
const normalized = { ...stored }
|
|
153
|
+
for (const [key, value] of Object.entries(stored)) {
|
|
154
|
+
const snake = snakeCaseKey(key)
|
|
155
|
+
if (snake !== key && stored[snake] === undefined) normalized[snake] = value
|
|
156
|
+
}
|
|
157
|
+
return normalized
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The `is_anonymous` claim of an access token, or undefined when it has none. */
|
|
161
|
+
function anonymousClaim(accessToken: string): boolean | undefined {
|
|
162
|
+
const claim = decodeJwtPayload(accessToken.split(".")[1] ?? "")?.["is_anonymous"]
|
|
163
|
+
return typeof claim === "boolean" ? claim : undefined
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A stored user with its camelCase keys copied to the snake_case ones `_parseUser` reads, so a user
|
|
168
|
+
* this client wrote (`isAnonymous`, `identities[].userId`, ...) survives a restore. A user stored
|
|
169
|
+
* without the anonymous flag takes it from the token's claim.
|
|
170
|
+
*/
|
|
171
|
+
function snakeCaseUser(user: Record<string, unknown>, accessToken: string): Record<string, unknown> {
|
|
172
|
+
const normalized = withSnakeKeys(user)
|
|
173
|
+
for (const list of ["identities", "factors"] as const) {
|
|
174
|
+
const items = user[list]
|
|
175
|
+
if (Array.isArray(items)) normalized[list] = items.map((i) => (isJsonObject(i) ? withSnakeKeys(i) : i))
|
|
176
|
+
}
|
|
177
|
+
if (normalized["is_anonymous"] === undefined) {
|
|
178
|
+
const claim = anonymousClaim(accessToken)
|
|
179
|
+
if (claim !== undefined) normalized["is_anonymous"] = claim
|
|
142
180
|
}
|
|
143
181
|
return normalized
|
|
144
182
|
}
|
|
@@ -426,16 +464,20 @@ export class AuthClient {
|
|
|
426
464
|
async signInWithPassword(credentials: {
|
|
427
465
|
email: string
|
|
428
466
|
password: string
|
|
467
|
+
/** `captchaToken` is the provider widget's answer, which a project with captcha on requires. */
|
|
468
|
+
options?: { captchaToken?: string | undefined } | undefined
|
|
429
469
|
}): Promise<{
|
|
430
470
|
data: { session: Session | null; user: User | null }
|
|
431
471
|
error: SupatypeError | null
|
|
432
472
|
}> {
|
|
473
|
+
const captchaToken = credentials.options?.captchaToken
|
|
433
474
|
const res = await this.send(`${this.url}/token?grant_type=password`, {
|
|
434
475
|
method: "POST",
|
|
435
476
|
headers: this.baseHeaders,
|
|
436
477
|
body: JSON.stringify({
|
|
437
478
|
email: credentials.email,
|
|
438
479
|
password: credentials.password,
|
|
480
|
+
...(captchaToken !== undefined && { supatype_meta_security: { captcha_token: captchaToken } }),
|
|
439
481
|
}),
|
|
440
482
|
})
|
|
441
483
|
return this._parseAuthResponse(res)
|
|
@@ -553,6 +595,7 @@ export class AuthClient {
|
|
|
553
595
|
body["skip_http_redirect"] = true
|
|
554
596
|
if (opts.redirectTo !== undefined) body["redirect_to"] = opts.redirectTo
|
|
555
597
|
if (opts.captchaToken !== undefined) body["supatype_meta_security"] = { captcha_token: opts.captchaToken }
|
|
598
|
+
if (opts.studio === true) body["studio"] = true
|
|
556
599
|
const flowType = opts.flowType ?? "pkce"
|
|
557
600
|
return flowType === "pkce" ? { ...body, ...(await this.beginPkce()) } : body
|
|
558
601
|
}
|
|
@@ -1398,7 +1441,7 @@ export class AuthClient {
|
|
|
1398
1441
|
refreshToken: eitherKey(raw, "refreshToken", "refresh_token", isString) ?? "",
|
|
1399
1442
|
tokenType: eitherKey(raw, "tokenType", "token_type", isString) ?? "bearer",
|
|
1400
1443
|
expiresIn: eitherKey(raw, "expiresIn", "expires_in", isNumber) ?? 3600,
|
|
1401
|
-
user: this._parseUser(snakeCaseUser(user)),
|
|
1444
|
+
user: this._parseUser(snakeCaseUser(user, accessToken)),
|
|
1402
1445
|
...(expiresAt !== undefined && { expiresAt }),
|
|
1403
1446
|
}
|
|
1404
1447
|
}
|
package/src/bindings.ts
CHANGED
|
@@ -14,11 +14,14 @@ export type {
|
|
|
14
14
|
TableSubscribeOptions,
|
|
15
15
|
TableSubscription,
|
|
16
16
|
} from "./table-subscription.js"
|
|
17
|
+
export { channelSlot, watchChannel, watchConnection } from "./channel-watch.js"
|
|
18
|
+
export type { ChannelSlot } from "./channel-watch.js"
|
|
19
|
+
export type { ChannelOptions, ChannelSubscription, RealtimeConnectionState } from "./realtime.js"
|
|
17
20
|
export { startOAuthRedirect } from "./auth-redirect.js"
|
|
18
21
|
export { isClientFor, isRowsOf, isCallerValue } from "./caller-types.js"
|
|
19
22
|
export { runMutation } from "./mutation-request.js"
|
|
20
23
|
export { reportThrown } from "./thrown-error.js"
|
|
21
|
-
export { latestRowsReader } from "./latest-rows.js"
|
|
24
|
+
export { latestRowsReader, withPage } from "./latest-rows.js"
|
|
22
25
|
export type { RowsReadOptions, RowsView } from "./latest-rows.js"
|
|
23
26
|
export type { MutationOperation, MutationOutcome, MutationRequest, MutationSelect } from "./mutation-request.js"
|
|
24
27
|
export { livePreviewMessage, previewMessageOf, originOf } from "./live-preview.js"
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ChannelOptions, ChannelSubscription, RealtimeClient, RealtimeConnectionState } from "./realtime.js"
|
|
2
|
+
import type { ChannelName } from "./types.js"
|
|
3
|
+
import type { ChannelActionRefusal } from "./realtime-refusals.js"
|
|
4
|
+
import { subscriptionViewOf, type SubscriptionView } from "./table-subscription.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One channel with any number of listeners, and the socket's own state, for the Vue, Svelte and
|
|
8
|
+
* Solid bindings. Each binding is only the adapter between these and its framework's state, as it
|
|
9
|
+
* is for `watchTable`; React's `useChannel` reads the same two client signals directly.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Join the channel `name`, let `setup` add its listeners first, and report each status as the
|
|
14
|
+
* binding's view of it. Returns the unsubscribe.
|
|
15
|
+
*
|
|
16
|
+
* `opts.private` asks for a private channel: see {@link ChannelOptions}. A refusal then shows as a
|
|
17
|
+
* `RealtimeChannelError`, so a binding can read its `reason`.
|
|
18
|
+
*
|
|
19
|
+
* `onRefused` hears a send, track or payload the server refused on the joined channel. The status
|
|
20
|
+
* does not move for it, so it has its own channel to the binding.
|
|
21
|
+
*/
|
|
22
|
+
export function watchChannel<TRow = Record<string, unknown>>(
|
|
23
|
+
realtime: RealtimeClient,
|
|
24
|
+
name: ChannelName,
|
|
25
|
+
setup: (channel: ChannelSubscription<TRow>) => void,
|
|
26
|
+
onStatus: (view: SubscriptionView) => void,
|
|
27
|
+
opts?: ChannelOptions | undefined,
|
|
28
|
+
onRefused?: ((refusal: ChannelActionRefusal) => void) | undefined,
|
|
29
|
+
): () => void {
|
|
30
|
+
const channel = realtime.channel<TRow>(name, opts)
|
|
31
|
+
setup(channel)
|
|
32
|
+
if (onRefused !== undefined) channel.onRefused(onRefused)
|
|
33
|
+
channel.subscribe((status, err) => onStatus(subscriptionViewOf(status, err)))
|
|
34
|
+
return () => channel.unsubscribe()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Report the socket's state now and at each change. Returns the unsubscribe.
|
|
39
|
+
*
|
|
40
|
+
* The state is reported straight away because a binding mounting after the socket opened would
|
|
41
|
+
* otherwise show "disconnected" until the next change.
|
|
42
|
+
*/
|
|
43
|
+
export function watchConnection(
|
|
44
|
+
realtime: RealtimeClient,
|
|
45
|
+
onChange: (state: RealtimeConnectionState) => void,
|
|
46
|
+
): () => void {
|
|
47
|
+
onChange(realtime.connectionState)
|
|
48
|
+
return realtime.onConnectionChange(onChange)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One channel at a time, which can be moved to another name. */
|
|
52
|
+
export interface ChannelSlot {
|
|
53
|
+
/** Join `name`, leaving the channel held first. Joining the name already held does nothing. */
|
|
54
|
+
join(name: ChannelName): void
|
|
55
|
+
/** Leave the channel held, if any. */
|
|
56
|
+
leave(): void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A binding's channel when its name can change: the old channel is left, its listeners go with it,
|
|
61
|
+
* `setup` runs on the new one, and the status reads "connecting" until it joins. A binding calls
|
|
62
|
+
* `join` for the first name and again for each change, and `leave` when it is torn down. `opts` is
|
|
63
|
+
* given to every channel it joins, and `onRefused` hears every refused send on them.
|
|
64
|
+
*/
|
|
65
|
+
export function channelSlot<TRow = Record<string, unknown>>(
|
|
66
|
+
realtime: RealtimeClient,
|
|
67
|
+
setup: (channel: ChannelSubscription<TRow>) => void,
|
|
68
|
+
onStatus: (view: SubscriptionView) => void,
|
|
69
|
+
opts?: ChannelOptions | undefined,
|
|
70
|
+
onRefused?: ((refusal: ChannelActionRefusal) => void) | undefined,
|
|
71
|
+
): ChannelSlot {
|
|
72
|
+
let held: { name: ChannelName; stop: () => void } | null = null
|
|
73
|
+
const leave = (): void => {
|
|
74
|
+
held?.stop()
|
|
75
|
+
held = null
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
join(name) {
|
|
79
|
+
if (held?.name === name) return
|
|
80
|
+
if (held !== null) onStatus({ status: "connecting", error: null })
|
|
81
|
+
leave()
|
|
82
|
+
held = { name, stop: watchChannel<TRow>(realtime, name, setup, onStatus, opts, onRefused) }
|
|
83
|
+
},
|
|
84
|
+
leave,
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/errors.ts
CHANGED
|
@@ -195,6 +195,60 @@ export class RealtimeError extends SupatypeCodedError {
|
|
|
195
195
|
}
|
|
196
196
|
nameErrors(RealtimeError, "RealtimeError")
|
|
197
197
|
|
|
198
|
+
/** Why a channel's join was refused, or its access taken away. */
|
|
199
|
+
export type RealtimeChannelErrorReason =
|
|
200
|
+
/** The server does not announce private channel support, so a `private: true` join was never sent. */
|
|
201
|
+
| "unsupported_server"
|
|
202
|
+
/** A declaration matched the channel and its `join` rule denied this user. */
|
|
203
|
+
| "join_denied"
|
|
204
|
+
/** The join said `private: true` and no `Channel` declaration matches the name. */
|
|
205
|
+
| "not_declared"
|
|
206
|
+
/** A declaration matches, but the name's parameters do not fit it (`lot:abc` for `lot:{id}`). */
|
|
207
|
+
| "does_not_fit"
|
|
208
|
+
/** The server could not reach the database to check. Retrying may succeed. */
|
|
209
|
+
| "unavailable"
|
|
210
|
+
/** The server is a shared instance, where private channels are not available. */
|
|
211
|
+
| "shared_instance"
|
|
212
|
+
/** A channel the socket held was taken away: the token changed, a re-check ran, or a declaration went. */
|
|
213
|
+
| "withdrawn"
|
|
214
|
+
/** The server refused for a reason this client has no name for; `message` has it. */
|
|
215
|
+
| "refused"
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* A channel the realtime server refused, or took back. Handed to a channel's status callback as the
|
|
219
|
+
* second argument with `CHANNEL_ERROR`: `subscribe((status, err) => ...)`.
|
|
220
|
+
*
|
|
221
|
+
* `reason` says which, so an app can tell a user who may not join from a typo in a channel name,
|
|
222
|
+
* and a withdrawal from either. `retryable` is true only for `unavailable`: every other reason
|
|
223
|
+
* is the server's answer and asking again gets the same one.
|
|
224
|
+
*/
|
|
225
|
+
export class RealtimeChannelError extends RealtimeError {
|
|
226
|
+
readonly channel: string
|
|
227
|
+
readonly reason: RealtimeChannelErrorReason
|
|
228
|
+
|
|
229
|
+
constructor(message: string, channel: string, reason: RealtimeChannelErrorReason) {
|
|
230
|
+
super(message, RealtimeErrorCodes.CHANNEL_ERROR)
|
|
231
|
+
this.channel = channel
|
|
232
|
+
this.reason = reason
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Whether asking again can succeed: only when the server could not check. */
|
|
236
|
+
get retryable(): boolean {
|
|
237
|
+
return this.reason === "unavailable"
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
nameErrors(RealtimeChannelError, "RealtimeChannelError")
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Whether `value` is a `RealtimeChannelError`.
|
|
244
|
+
*
|
|
245
|
+
* By name rather than `instanceof`, so it holds across two copies of this package in one bundle,
|
|
246
|
+
* which a framework binding and an app can easily have.
|
|
247
|
+
*/
|
|
248
|
+
export function isRealtimeChannelError(value: unknown): value is RealtimeChannelError {
|
|
249
|
+
return value instanceof Error && value.name === "RealtimeChannelError" && "reason" in value && "channel" in value
|
|
250
|
+
}
|
|
251
|
+
|
|
198
252
|
/**
|
|
199
253
|
* Network errors (connection failures, timeouts, DNS).
|
|
200
254
|
*/
|
package/src/exact-values.ts
CHANGED
|
@@ -15,25 +15,74 @@
|
|
|
15
15
|
* module exists to remove.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import { isJsonObject } from "./json.js"
|
|
18
|
+
import { isJsonObject, isOneOf } from "./json.js"
|
|
19
19
|
|
|
20
|
-
/**
|
|
21
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The shapes a column can need. `numeric` covers both `decimal` and `money`. The `[]` forms are
|
|
22
|
+
* Postgres arrays of the same, which protect and convert each element the way the single value is.
|
|
23
|
+
*/
|
|
24
|
+
export type ExactKind = "bigint" | "numeric" | "bigint[]" | "numeric[]"
|
|
22
25
|
|
|
23
26
|
/** Column name to kind, for the table being read. */
|
|
24
27
|
export type ExactFields = Readonly<Record<string, ExactKind>>
|
|
25
28
|
|
|
29
|
+
/** Whether this client knows the kind. A newer generator or service may name ones it does not. */
|
|
30
|
+
export const isExactKind = isOneOf(["bigint", "numeric", "bigint[]", "numeric[]"])
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The columns whose kind this client knows. A kind it does not know is ignored, not refused: that
|
|
34
|
+
* column's value passes through as sent, so a future kind never costs a whole row or change.
|
|
35
|
+
*/
|
|
36
|
+
export function knownExactKinds(columns: Readonly<Record<string, string>>): ExactFields {
|
|
37
|
+
const known: Record<string, ExactKind> = {}
|
|
38
|
+
for (const [column, kind] of Object.entries(columns)) {
|
|
39
|
+
if (isExactKind(kind)) known[column] = kind
|
|
40
|
+
}
|
|
41
|
+
return known
|
|
42
|
+
}
|
|
43
|
+
|
|
26
44
|
const WHITESPACE = new Set([" ", "\t", "\n", "\r"])
|
|
27
45
|
const QUOTE = '"'
|
|
28
46
|
|
|
29
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* A numeric literal found as the value of a key, or as an element of an array that is, with where
|
|
49
|
+
* it sits in the text. For an element, `key` and `depth` are those of the column holding the
|
|
50
|
+
* array, however deeply the element is nested in it.
|
|
51
|
+
*/
|
|
30
52
|
type KeyedNumber = {
|
|
31
53
|
readonly key: string
|
|
32
54
|
readonly depth: number
|
|
55
|
+
readonly element: boolean
|
|
33
56
|
readonly start: number
|
|
34
57
|
readonly end: number
|
|
35
58
|
}
|
|
36
59
|
|
|
60
|
+
/** An open `{` or `[`, and the column an array belongs to when it is one's value. */
|
|
61
|
+
type Frame = {
|
|
62
|
+
readonly container: "{" | "["
|
|
63
|
+
readonly column: string | null
|
|
64
|
+
readonly columnDepth: number
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The frame for a container just opened. An array that is a key's value belongs to that column, and
|
|
69
|
+
* so does an array nested directly inside it, which is how a multi-dimensional column arrives.
|
|
70
|
+
*/
|
|
71
|
+
function openFrame(
|
|
72
|
+
container: "{" | "[",
|
|
73
|
+
parent: Frame | undefined,
|
|
74
|
+
valueKey: string | null,
|
|
75
|
+
depth: number,
|
|
76
|
+
): Frame {
|
|
77
|
+
if (container === "[" && parent?.container === "[" && parent.column !== null) {
|
|
78
|
+
return { container, column: parent.column, columnDepth: parent.columnDepth }
|
|
79
|
+
}
|
|
80
|
+
if (container === "[" && valueKey !== null) {
|
|
81
|
+
return { container, column: valueKey, columnDepth: depth }
|
|
82
|
+
}
|
|
83
|
+
return { container, column: null, columnDepth: depth }
|
|
84
|
+
}
|
|
85
|
+
|
|
37
86
|
/**
|
|
38
87
|
* Walk the JSON text and report every numeric literal that is the value of a key.
|
|
39
88
|
*
|
|
@@ -44,6 +93,9 @@ function forEachKeyedNumber(body: string, visit: (found: KeyedNumber) => void):
|
|
|
44
93
|
let i = 0
|
|
45
94
|
let depth = 0
|
|
46
95
|
let pendingKey: string | null = null
|
|
96
|
+
// The key whose value is being read, so a `[` can tell which column it opens.
|
|
97
|
+
let valueKey: string | null = null
|
|
98
|
+
const frames: Frame[] = []
|
|
47
99
|
|
|
48
100
|
while (i < body.length) {
|
|
49
101
|
const ch = body.charAt(i)
|
|
@@ -52,31 +104,46 @@ function forEachKeyedNumber(body: string, visit: (found: KeyedNumber) => void):
|
|
|
52
104
|
const end = endOfString(body, i)
|
|
53
105
|
const raw = body.slice(i, end)
|
|
54
106
|
i = end
|
|
107
|
+
valueKey = null
|
|
55
108
|
pendingKey = nextMeaningfulChar(body, i) === ":" ? readKey(raw) : null
|
|
56
109
|
continue
|
|
57
110
|
}
|
|
58
111
|
|
|
59
112
|
if (ch === "{" || ch === "[") {
|
|
113
|
+
frames.push(openFrame(ch, frames[frames.length - 1], valueKey, depth))
|
|
114
|
+
valueKey = null
|
|
60
115
|
depth += 1
|
|
61
116
|
i += 1
|
|
62
117
|
continue
|
|
63
118
|
}
|
|
64
119
|
|
|
65
120
|
if (ch === "}" || ch === "]") {
|
|
121
|
+
frames.pop()
|
|
66
122
|
depth -= 1
|
|
67
123
|
i += 1
|
|
68
124
|
continue
|
|
69
125
|
}
|
|
70
126
|
|
|
127
|
+
const owner = frames[frames.length - 1]
|
|
128
|
+
if (owner?.container === "[" && owner.column !== null && (ch === "-" || isDigit(ch))) {
|
|
129
|
+
const end = endOfNumber(body, i)
|
|
130
|
+
if (end > i) {
|
|
131
|
+
visit({ key: owner.column, depth: owner.columnDepth, element: true, start: i, end })
|
|
132
|
+
i = end
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
71
137
|
if (ch === ":") {
|
|
72
138
|
i += 1
|
|
73
139
|
while (i < body.length && WHITESPACE.has(body.charAt(i))) i += 1
|
|
74
140
|
const key = pendingKey
|
|
75
141
|
pendingKey = null
|
|
142
|
+
valueKey = key
|
|
76
143
|
if (key !== null) {
|
|
77
144
|
const end = endOfNumber(body, i)
|
|
78
145
|
if (end > i) {
|
|
79
|
-
visit({ key, depth, start: i, end })
|
|
146
|
+
visit({ key, depth, element: false, start: i, end })
|
|
80
147
|
i = end
|
|
81
148
|
}
|
|
82
149
|
}
|
|
@@ -103,7 +170,10 @@ export function quoteExactNumbers(body: string, fields: ExactFields): string {
|
|
|
103
170
|
|
|
104
171
|
const spans: KeyedNumber[] = []
|
|
105
172
|
forEachKeyedNumber(body, (found) => {
|
|
106
|
-
if (found.depth
|
|
173
|
+
if (found.depth !== rowDepth) return
|
|
174
|
+
const kind = fields[found.key]
|
|
175
|
+
// An element only counts inside an array column, and a bare value only outside one.
|
|
176
|
+
if (kind !== undefined && isArrayKind(kind) === found.element) spans.push(found)
|
|
107
177
|
})
|
|
108
178
|
if (spans.length === 0) return body
|
|
109
179
|
|
|
@@ -122,9 +192,12 @@ export function quoteExactNumbers(body: string, fields: ExactFields): string {
|
|
|
122
192
|
/**
|
|
123
193
|
* The columns whose literals will not survive `JSON.parse`.
|
|
124
194
|
*
|
|
125
|
-
* The safety net for when column metadata could not be loaded
|
|
126
|
-
* would quietly restore the corruption this module exists to
|
|
127
|
-
* over-reporting here costs a clear error and under-reporting
|
|
195
|
+
* The safety net for when column metadata could not be loaded, or does not describe a column.
|
|
196
|
+
* Without it a failed metadata fetch would quietly restore the corruption this module exists to
|
|
197
|
+
* remove. Reports at any depth, since over-reporting here costs a clear error and under-reporting
|
|
198
|
+
* costs a wrong number. A literal inside an array counts, at any nesting depth, and is reported
|
|
199
|
+
* under the column holding the array. Columns registered as exact kinds are quoted before this
|
|
200
|
+
* runs, so they are protected rather than reported.
|
|
128
201
|
*/
|
|
129
202
|
export function findLossyNumbers(body: string): string[] {
|
|
130
203
|
const names: string[] = []
|
|
@@ -174,16 +247,31 @@ export function coerceRecordKinds(
|
|
|
174
247
|
let copy: Record<string, unknown> | null = null
|
|
175
248
|
|
|
176
249
|
for (const [name, kind] of Object.entries(fields)) {
|
|
177
|
-
if (kind !== "bigint") continue
|
|
178
250
|
const current = source[name]
|
|
179
|
-
if (typeof current
|
|
180
|
-
|
|
181
|
-
|
|
251
|
+
if (kind === "bigint" && typeof current === "string") {
|
|
252
|
+
copy ??= { ...source }
|
|
253
|
+
copy[name] = BigInt(current)
|
|
254
|
+
} else if (kind === "bigint[]" && Array.isArray(current)) {
|
|
255
|
+
copy ??= { ...source }
|
|
256
|
+
copy[name] = bigintsIn(current)
|
|
257
|
+
}
|
|
182
258
|
}
|
|
183
259
|
|
|
184
260
|
return copy ?? source
|
|
185
261
|
}
|
|
186
262
|
|
|
263
|
+
function isArrayKind(kind: ExactKind): boolean {
|
|
264
|
+
return kind === "bigint[]" || kind === "numeric[]"
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** An array with every quoted element a native bigint, at any depth. Null stays null. */
|
|
268
|
+
function bigintsIn(items: readonly unknown[]): unknown[] {
|
|
269
|
+
return items.map((item) => {
|
|
270
|
+
if (typeof item === "string") return BigInt(item)
|
|
271
|
+
return Array.isArray(item) ? bigintsIn(item) : item
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
|
|
187
275
|
/** Whether parsing this literal as a double changes the value it names. */
|
|
188
276
|
function isLossy(literal: string): boolean {
|
|
189
277
|
const asNumber = Number(literal)
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
* had. The gap could not be reported because nothing could detect it. Here, absence is a fact:
|
|
17
17
|
* {@link fieldKindsAreRegistered} is false, and the caller can say so.
|
|
18
18
|
*/
|
|
19
|
-
import type
|
|
20
|
-
import { isJsonObject
|
|
19
|
+
import { isExactKind, type ExactFields, type ExactKind } from "./exact-values.js"
|
|
20
|
+
import { isJsonObject } from "./json.js"
|
|
21
21
|
|
|
22
22
|
/** Table name to its exact-value columns. */
|
|
23
23
|
export type ExactFieldsByTable = Readonly<Record<string, ExactFields>>
|
|
@@ -38,7 +38,6 @@ export type UnrecognisedFieldKindsDocument = {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
const SUPPORTED_VERSION = 1
|
|
41
|
-
const isExactKind = isOneOf(["bigint", "numeric"])
|
|
42
41
|
const NO_FIELDS: ExactFields = {}
|
|
43
42
|
|
|
44
43
|
// Module level, and deliberately not per client. Server-side rendering builds a client per
|