@diffenderfer-games/hub 2.0.0-next.0 → 2.0.0-next.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -21
- package/README.md +191 -139
- package/dist/doctor/cli.mjs +49 -19
- package/dist/doctor/cli.mjs.map +1 -1
- package/dist/shared/client-version.gen.js +1 -1
- package/dist/shared/daily.js +1 -1
- package/dist/shared/globals.js +8 -3
- package/dist/shared/globals.js.map +1 -1
- package/dist/shared/index.d.ts +1 -1
- package/package.json +89 -89
- package/src/auto.ts +39 -39
- package/src/core/connection.ts +299 -299
- package/src/core/errors.ts +147 -147
- package/src/core/game-data.ts +38 -38
- package/src/core/hub-economy.ts +179 -179
- package/src/core/hub-progress.ts +205 -205
- package/src/core/hub.ts +131 -131
- package/src/core/menu-control.ts +61 -61
- package/src/core/offline/events.ts +50 -50
- package/src/core/offline/favorites.ts +21 -21
- package/src/core/offline/local-store.ts +261 -261
- package/src/core/offline/queue.ts +171 -171
- package/src/core/page.ts +74 -74
- package/src/core/rest/endpoint-types.ts +22 -22
- package/src/core/rest/rest-client.ts +152 -152
- package/src/core/storage/json-guards.ts +74 -74
- package/src/core/storage/key-value.ts +66 -66
- package/src/core/ticket.ts +43 -43
- package/src/core/types.ts +134 -134
- package/src/core/version/client-version.gen.ts +1 -1
- package/src/core/version/hub-version.ts +62 -62
- package/src/core/version/semver.ts +97 -97
- package/src/daily/daily-system.ts +161 -161
- package/src/daily/page-daily.ts +36 -36
- package/src/daily/types.ts +67 -67
- package/src/daily.ts +10 -10
- package/src/doctor/checks.ts +78 -76
- package/src/doctor/cli.ts +98 -106
- package/src/doctor/hub-version-fetch.ts +43 -0
- package/src/doctor/tsconfig.json +12 -12
- package/src/globals.ts +115 -110
- package/src/index.ts +50 -50
- package/src/input/device-state.ts +146 -146
- package/src/input/edges.ts +55 -55
- package/src/input/gamepad.ts +61 -61
- package/src/input/input-events.ts +32 -32
- package/src/input/input-registry.ts +164 -164
- package/src/input/input-system.ts +393 -393
- package/src/input/legacy-controls.ts +64 -64
- package/src/input/nav-state.ts +191 -191
- package/src/input/navigator.ts +171 -171
- package/src/input/on-screen-controls.ts +101 -101
- package/src/input/page-control.ts +43 -43
- package/src/input/page-input.ts +193 -193
- package/src/input/player-remaps.ts +77 -77
- package/src/input/source-values.ts +116 -116
- package/src/input/spatial.ts +47 -47
- package/src/input/types.ts +331 -331
- package/src/input/zero-config.ts +177 -177
- package/src/input.ts +22 -22
- package/src/mp.ts +10 -10
- package/src/overlay/controls-panel.ts +163 -163
- package/src/overlay/dom.ts +46 -46
- package/src/overlay/layout-clusters.ts +162 -162
- package/src/overlay/layout-geometry.ts +136 -136
- package/src/overlay/layout.ts +203 -203
- package/src/overlay/listener-scope.ts +23 -23
- package/src/overlay/on-screen-keyboard.ts +137 -137
- package/src/overlay/overlay.ts +208 -208
- package/src/overlay/styles.ts +9 -9
- package/src/overlay/virtual-controls.ts +262 -262
- package/src/race.ts +10 -10
- package/src/realtime/bridge.ts +121 -121
- package/src/realtime/cross-bundle.ts +29 -29
- package/src/realtime/fallback-notice.ts +48 -48
- package/src/realtime/hub-link.ts +65 -65
- package/src/realtime/latest-state.ts +33 -33
- package/src/realtime/multiplayer-forwarder.ts +28 -28
- package/src/realtime/multiplayer-types.ts +52 -52
- package/src/realtime/page-runtime.ts +60 -60
- package/src/realtime/race-forwarder.ts +59 -59
- package/src/realtime/race-types.ts +132 -132
- package/src/realtime/room-types.ts +125 -125
- package/src/realtime/rooms-forwarder.ts +79 -79
- package/src/realtime/runtime-api.ts +49 -49
- package/src/realtime/runtime-opener.ts +61 -61
- package/src/realtime/social-forwarder.ts +72 -72
- package/src/realtime/social-types.ts +78 -78
- package/src/realtime/types.ts +53 -53
- package/src/realtime/wire-types.ts +72 -72
- package/src/rooms.ts +10 -10
- package/src/social.ts +18 -18
- package/src/types.ts +13 -13
- package/src/uistack/page-stack.ts +45 -45
- package/src/uistack/play-suspension.ts +82 -82
- package/src/uistack/types.ts +76 -76
- package/src/uistack/ui-stack.ts +144 -144
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The REST transport: a small fetch router over the contract's route table
|
|
3
|
-
* (plan §12 A10). It splits one merged fields object into the path, query
|
|
4
|
-
* and JSON body the way ayepi's client does, stamps `x-hub-client`, and turns
|
|
5
|
-
* failures into {@link HubError}s. It carries no schemas: replies are typed
|
|
6
|
-
* from the contract and trusted, as ayepi's zod-free client trusts them.
|
|
7
|
-
*/
|
|
8
|
-
import { errorFor, HubError, readErrorFields, UNSUPPORTED_BY_HUB } from '../errors';
|
|
9
|
-
import { isRecord } from '../storage/json-guards';
|
|
10
|
-
import { HUB_CLIENT_VERSION } from '../version/client-version.gen';
|
|
11
|
-
import type { EndpointFields, EndpointName, EndpointReply } from './endpoint-types';
|
|
12
|
-
import { ROUTES } from './routes.gen';
|
|
13
|
-
|
|
14
|
-
/** The header that tells the hub which client version is calling. */
|
|
15
|
-
export const CLIENT_VERSION_HEADER = 'x-hub-client';
|
|
16
|
-
|
|
17
|
-
/** What both hubs answer for a route they do not have. */
|
|
18
|
-
const NO_SUCH_ENDPOINT = 'No such API endpoint.';
|
|
19
|
-
|
|
20
|
-
/** How a {@link RestClient} reaches the hub. */
|
|
21
|
-
export interface RestClientOptions {
|
|
22
|
-
/** The API's absolute base URL without a trailing slash, e.g. `https://diffenderfer.games/_api`. */
|
|
23
|
-
readonly apiBase: string;
|
|
24
|
-
/** The fetch to use. */
|
|
25
|
-
readonly fetch: typeof fetch;
|
|
26
|
-
/** Called whenever the hub answered at all (any status): the network is up. */
|
|
27
|
-
readonly onReachable?: () => void;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** The arguments of a call: the fields, optional when the endpoint takes none that are required. */
|
|
31
|
-
export type CallArguments<K extends EndpointName> = Partial<EndpointFields<K>> extends EndpointFields<K>
|
|
32
|
-
? [fields?: EndpointFields<K>]
|
|
33
|
-
: [fields: EndpointFields<K>];
|
|
34
|
-
|
|
35
|
-
/** Typed calls to the hub's public REST API. */
|
|
36
|
-
export interface RestClient {
|
|
37
|
-
/** The API's absolute base URL. */
|
|
38
|
-
readonly apiBase: string;
|
|
39
|
-
/** Call an endpoint; resolves with its reply body, rejects with a {@link HubError}. */
|
|
40
|
-
call<K extends EndpointName>(name: K, ...fieldsArgument: CallArguments<K>): Promise<EndpointReply<K>>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** One field value as URL text. */
|
|
44
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a request field value of any contract type
|
|
45
|
-
function urlText(fieldValue: unknown): string {
|
|
46
|
-
if (typeof fieldValue === 'string') {
|
|
47
|
-
return fieldValue;
|
|
48
|
-
}
|
|
49
|
-
if (typeof fieldValue === 'number' || typeof fieldValue === 'boolean') {
|
|
50
|
-
return String(fieldValue);
|
|
51
|
-
}
|
|
52
|
-
return JSON.stringify(fieldValue);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** The request one call makes: its URL (under the API base), method and JSON body. */
|
|
56
|
-
export interface PlannedRequest {
|
|
57
|
-
/** The path and query, e.g. `/games/pool/leaderboards/best?limit=50`. */
|
|
58
|
-
readonly pathAndQuery: string;
|
|
59
|
-
/** The HTTP method. */
|
|
60
|
-
readonly method: string;
|
|
61
|
-
/** The JSON body text, or `undefined` for none. */
|
|
62
|
-
readonly bodyText: string | undefined;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Split merged fields into path, query and body for one endpoint: `:name`
|
|
67
|
-
* segments take their field, query keys go to the query string, and the
|
|
68
|
-
* rest is the JSON body. POST and PUT always send a body (`{}` when empty),
|
|
69
|
-
* as the v1 client did; DELETE sends one only when it has fields.
|
|
70
|
-
*/
|
|
71
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the caller's typed fields, walked generically
|
|
72
|
-
export function planRequest(name: EndpointName, fields: unknown): PlannedRequest {
|
|
73
|
-
const route = ROUTES[name];
|
|
74
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the caller's typed fields, walked generically
|
|
75
|
-
const remaining = new Map<string, unknown>(Object.entries(isRecord(fields) ? fields : {}).filter(([, fieldValue]) => fieldValue !== undefined));
|
|
76
|
-
const path = route.path.replace(/:([A-Za-z]+)/g, (_segment, paramName: string) => {
|
|
77
|
-
const paramValue = remaining.get(paramName);
|
|
78
|
-
remaining.delete(paramName);
|
|
79
|
-
return encodeURIComponent(urlText(paramValue));
|
|
80
|
-
});
|
|
81
|
-
const queryKeys: readonly string[] = route.method === 'GET' ? [...remaining.keys()] : route.query;
|
|
82
|
-
const query = new URLSearchParams();
|
|
83
|
-
for (const key of queryKeys) {
|
|
84
|
-
const queryValue = remaining.get(key);
|
|
85
|
-
remaining.delete(key);
|
|
86
|
-
if (queryValue !== undefined && queryValue !== null) {
|
|
87
|
-
query.set(key, urlText(queryValue));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
const queryText = query.toString();
|
|
91
|
-
const hasBody = route.method === 'POST' || route.method === 'PUT' || (route.method === 'DELETE' && remaining.size > 0);
|
|
92
|
-
return {
|
|
93
|
-
pathAndQuery: queryText === '' ? path : `${path}?${queryText}`,
|
|
94
|
-
method: route.method,
|
|
95
|
-
bodyText: hasBody ? JSON.stringify(Object.fromEntries(remaining)) : undefined,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* The reply body, typed by the contract. This is the client's one trust
|
|
101
|
-
* boundary on replies: there is no zod in the browser bundle (plan D16).
|
|
102
|
-
*/
|
|
103
|
-
async function readReply<Reply>(response: Response): Promise<Reply> {
|
|
104
|
-
try {
|
|
105
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- trust boundary: the hub's JSON reply, typed by the contract (the zod-free client trusts the server, plan D16)
|
|
106
|
-
return await response.json();
|
|
107
|
-
} catch {
|
|
108
|
-
throw new HubError('The hub sent a reply that is not JSON.', response.status);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** The error for a failed response. */
|
|
113
|
-
async function failureOf(name: EndpointName, response: Response): Promise<HubError> {
|
|
114
|
-
let fields = readErrorFields(undefined);
|
|
115
|
-
try {
|
|
116
|
-
fields = readErrorFields(await response.json());
|
|
117
|
-
} catch {
|
|
118
|
-
// A non-JSON error body keeps the generic message.
|
|
119
|
-
}
|
|
120
|
-
if (response.status === 404 && fields.error === NO_SUCH_ENDPOINT) {
|
|
121
|
-
return new HubError(`This hub does not have "${name}" yet: deploy the hub first, or use an older client.`, 404, { code: UNSUPPORTED_BY_HUB });
|
|
122
|
-
}
|
|
123
|
-
return errorFor(fields.error ?? `Request failed (${response.status})`, response.status, fields);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** A REST client for one API base. */
|
|
127
|
-
export function createRestClient(options: RestClientOptions): RestClient {
|
|
128
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the caller's typed fields, walked generically
|
|
129
|
-
async function send<Reply>(name: EndpointName, fields: unknown): Promise<Reply> {
|
|
130
|
-
const planned = planRequest(name, fields);
|
|
131
|
-
let response: Response;
|
|
132
|
-
try {
|
|
133
|
-
response = await options.fetch(options.apiBase + planned.pathAndQuery, {
|
|
134
|
-
method: planned.method,
|
|
135
|
-
credentials: 'same-origin',
|
|
136
|
-
headers: { 'content-type': 'application/json', [CLIENT_VERSION_HEADER]: HUB_CLIENT_VERSION },
|
|
137
|
-
...(planned.bodyText === undefined ? {} : { body: planned.bodyText }),
|
|
138
|
-
});
|
|
139
|
-
} catch (failure) {
|
|
140
|
-
throw new HubError(`Hub unreachable: ${failure instanceof Error ? failure.message : String(failure)}`);
|
|
141
|
-
}
|
|
142
|
-
options.onReachable?.();
|
|
143
|
-
if (!response.ok) {
|
|
144
|
-
throw await failureOf(name, response);
|
|
145
|
-
}
|
|
146
|
-
return readReply<Reply>(response);
|
|
147
|
-
}
|
|
148
|
-
return {
|
|
149
|
-
apiBase: options.apiBase,
|
|
150
|
-
call: (name, ...fieldsArgument) => send(name, fieldsArgument[0]),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The REST transport: a small fetch router over the contract's route table
|
|
3
|
+
* (plan §12 A10). It splits one merged fields object into the path, query
|
|
4
|
+
* and JSON body the way ayepi's client does, stamps `x-hub-client`, and turns
|
|
5
|
+
* failures into {@link HubError}s. It carries no schemas: replies are typed
|
|
6
|
+
* from the contract and trusted, as ayepi's zod-free client trusts them.
|
|
7
|
+
*/
|
|
8
|
+
import { errorFor, HubError, readErrorFields, UNSUPPORTED_BY_HUB } from '../errors';
|
|
9
|
+
import { isRecord } from '../storage/json-guards';
|
|
10
|
+
import { HUB_CLIENT_VERSION } from '../version/client-version.gen';
|
|
11
|
+
import type { EndpointFields, EndpointName, EndpointReply } from './endpoint-types';
|
|
12
|
+
import { ROUTES } from './routes.gen';
|
|
13
|
+
|
|
14
|
+
/** The header that tells the hub which client version is calling. */
|
|
15
|
+
export const CLIENT_VERSION_HEADER = 'x-hub-client';
|
|
16
|
+
|
|
17
|
+
/** What both hubs answer for a route they do not have. */
|
|
18
|
+
const NO_SUCH_ENDPOINT = 'No such API endpoint.';
|
|
19
|
+
|
|
20
|
+
/** How a {@link RestClient} reaches the hub. */
|
|
21
|
+
export interface RestClientOptions {
|
|
22
|
+
/** The API's absolute base URL without a trailing slash, e.g. `https://diffenderfer.games/_api`. */
|
|
23
|
+
readonly apiBase: string;
|
|
24
|
+
/** The fetch to use. */
|
|
25
|
+
readonly fetch: typeof fetch;
|
|
26
|
+
/** Called whenever the hub answered at all (any status): the network is up. */
|
|
27
|
+
readonly onReachable?: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The arguments of a call: the fields, optional when the endpoint takes none that are required. */
|
|
31
|
+
export type CallArguments<K extends EndpointName> = Partial<EndpointFields<K>> extends EndpointFields<K>
|
|
32
|
+
? [fields?: EndpointFields<K>]
|
|
33
|
+
: [fields: EndpointFields<K>];
|
|
34
|
+
|
|
35
|
+
/** Typed calls to the hub's public REST API. */
|
|
36
|
+
export interface RestClient {
|
|
37
|
+
/** The API's absolute base URL. */
|
|
38
|
+
readonly apiBase: string;
|
|
39
|
+
/** Call an endpoint; resolves with its reply body, rejects with a {@link HubError}. */
|
|
40
|
+
call<K extends EndpointName>(name: K, ...fieldsArgument: CallArguments<K>): Promise<EndpointReply<K>>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** One field value as URL text. */
|
|
44
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a request field value of any contract type
|
|
45
|
+
function urlText(fieldValue: unknown): string {
|
|
46
|
+
if (typeof fieldValue === 'string') {
|
|
47
|
+
return fieldValue;
|
|
48
|
+
}
|
|
49
|
+
if (typeof fieldValue === 'number' || typeof fieldValue === 'boolean') {
|
|
50
|
+
return String(fieldValue);
|
|
51
|
+
}
|
|
52
|
+
return JSON.stringify(fieldValue);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The request one call makes: its URL (under the API base), method and JSON body. */
|
|
56
|
+
export interface PlannedRequest {
|
|
57
|
+
/** The path and query, e.g. `/games/pool/leaderboards/best?limit=50`. */
|
|
58
|
+
readonly pathAndQuery: string;
|
|
59
|
+
/** The HTTP method. */
|
|
60
|
+
readonly method: string;
|
|
61
|
+
/** The JSON body text, or `undefined` for none. */
|
|
62
|
+
readonly bodyText: string | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Split merged fields into path, query and body for one endpoint: `:name`
|
|
67
|
+
* segments take their field, query keys go to the query string, and the
|
|
68
|
+
* rest is the JSON body. POST and PUT always send a body (`{}` when empty),
|
|
69
|
+
* as the v1 client did; DELETE sends one only when it has fields.
|
|
70
|
+
*/
|
|
71
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the caller's typed fields, walked generically
|
|
72
|
+
export function planRequest(name: EndpointName, fields: unknown): PlannedRequest {
|
|
73
|
+
const route = ROUTES[name];
|
|
74
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the caller's typed fields, walked generically
|
|
75
|
+
const remaining = new Map<string, unknown>(Object.entries(isRecord(fields) ? fields : {}).filter(([, fieldValue]) => fieldValue !== undefined));
|
|
76
|
+
const path = route.path.replace(/:([A-Za-z]+)/g, (_segment, paramName: string) => {
|
|
77
|
+
const paramValue = remaining.get(paramName);
|
|
78
|
+
remaining.delete(paramName);
|
|
79
|
+
return encodeURIComponent(urlText(paramValue));
|
|
80
|
+
});
|
|
81
|
+
const queryKeys: readonly string[] = route.method === 'GET' ? [...remaining.keys()] : route.query;
|
|
82
|
+
const query = new URLSearchParams();
|
|
83
|
+
for (const key of queryKeys) {
|
|
84
|
+
const queryValue = remaining.get(key);
|
|
85
|
+
remaining.delete(key);
|
|
86
|
+
if (queryValue !== undefined && queryValue !== null) {
|
|
87
|
+
query.set(key, urlText(queryValue));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const queryText = query.toString();
|
|
91
|
+
const hasBody = route.method === 'POST' || route.method === 'PUT' || (route.method === 'DELETE' && remaining.size > 0);
|
|
92
|
+
return {
|
|
93
|
+
pathAndQuery: queryText === '' ? path : `${path}?${queryText}`,
|
|
94
|
+
method: route.method,
|
|
95
|
+
bodyText: hasBody ? JSON.stringify(Object.fromEntries(remaining)) : undefined,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The reply body, typed by the contract. This is the client's one trust
|
|
101
|
+
* boundary on replies: there is no zod in the browser bundle (plan D16).
|
|
102
|
+
*/
|
|
103
|
+
async function readReply<Reply>(response: Response): Promise<Reply> {
|
|
104
|
+
try {
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- trust boundary: the hub's JSON reply, typed by the contract (the zod-free client trusts the server, plan D16)
|
|
106
|
+
return await response.json();
|
|
107
|
+
} catch {
|
|
108
|
+
throw new HubError('The hub sent a reply that is not JSON.', response.status);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The error for a failed response. */
|
|
113
|
+
async function failureOf(name: EndpointName, response: Response): Promise<HubError> {
|
|
114
|
+
let fields = readErrorFields(undefined);
|
|
115
|
+
try {
|
|
116
|
+
fields = readErrorFields(await response.json());
|
|
117
|
+
} catch {
|
|
118
|
+
// A non-JSON error body keeps the generic message.
|
|
119
|
+
}
|
|
120
|
+
if (response.status === 404 && fields.error === NO_SUCH_ENDPOINT) {
|
|
121
|
+
return new HubError(`This hub does not have "${name}" yet: deploy the hub first, or use an older client.`, 404, { code: UNSUPPORTED_BY_HUB });
|
|
122
|
+
}
|
|
123
|
+
return errorFor(fields.error ?? `Request failed (${response.status})`, response.status, fields);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** A REST client for one API base. */
|
|
127
|
+
export function createRestClient(options: RestClientOptions): RestClient {
|
|
128
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the caller's typed fields, walked generically
|
|
129
|
+
async function send<Reply>(name: EndpointName, fields: unknown): Promise<Reply> {
|
|
130
|
+
const planned = planRequest(name, fields);
|
|
131
|
+
let response: Response;
|
|
132
|
+
try {
|
|
133
|
+
response = await options.fetch(options.apiBase + planned.pathAndQuery, {
|
|
134
|
+
method: planned.method,
|
|
135
|
+
credentials: 'same-origin',
|
|
136
|
+
headers: { 'content-type': 'application/json', [CLIENT_VERSION_HEADER]: HUB_CLIENT_VERSION },
|
|
137
|
+
...(planned.bodyText === undefined ? {} : { body: planned.bodyText }),
|
|
138
|
+
});
|
|
139
|
+
} catch (failure) {
|
|
140
|
+
throw new HubError(`Hub unreachable: ${failure instanceof Error ? failure.message : String(failure)}`);
|
|
141
|
+
}
|
|
142
|
+
options.onReachable?.();
|
|
143
|
+
if (!response.ok) {
|
|
144
|
+
throw await failureOf(name, response);
|
|
145
|
+
}
|
|
146
|
+
return readReply<Reply>(response);
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
apiBase: options.apiBase,
|
|
150
|
+
call: (name, ...fieldsArgument) => send(name, fieldsArgument[0]),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Small type guards for values this client did not create: JSON read back
|
|
3
|
-
* from `localStorage`, error bodies, objects from other bundles. The browser
|
|
4
|
-
* client carries no zod (plan D16), so these replace schema parsing on the
|
|
5
|
-
* few shapes it reads.
|
|
6
|
-
*/
|
|
7
|
-
import type { Json } from '@diffenderfer-games/hub-contract';
|
|
8
|
-
|
|
9
|
-
/** A plain object's fields, not yet checked. */
|
|
10
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the fields of a value read from storage or another bundle
|
|
11
|
-
export type UncheckedRecord = Readonly<Record<string, unknown>>;
|
|
12
|
-
|
|
13
|
-
/** Whether `candidate` is a non-null, non-array object. */
|
|
14
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: checks a value read from storage or another bundle
|
|
15
|
-
export function isRecord(candidate: unknown): candidate is UncheckedRecord {
|
|
16
|
-
return typeof candidate === 'object' && candidate !== null && !Array.isArray(candidate);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** `candidate` when it is a string, else `undefined`. */
|
|
20
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: one field of an unchecked record
|
|
21
|
-
export function optionalString(candidate: unknown): string | undefined {
|
|
22
|
-
return typeof candidate === 'string' ? candidate : undefined;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** `candidate` when it is a finite number, else `undefined`. */
|
|
26
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: one field of an unchecked record
|
|
27
|
-
export function optionalNumber(candidate: unknown): number | undefined {
|
|
28
|
-
return typeof candidate === 'number' && Number.isFinite(candidate) ? candidate : undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Whether `candidate` is a string or `null`. */
|
|
32
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: one field of an unchecked record
|
|
33
|
-
export function isNullableString(candidate: unknown): candidate is string | null {
|
|
34
|
-
return candidate === null || typeof candidate === 'string';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Whether `candidate` is a JSON value (what `JSON.parse` can return). */
|
|
38
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: checks a value read from storage
|
|
39
|
-
export function isJson(candidate: unknown): candidate is Json {
|
|
40
|
-
if (candidate === null || typeof candidate === 'string' || typeof candidate === 'boolean') {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
if (typeof candidate === 'number') {
|
|
44
|
-
return Number.isFinite(candidate);
|
|
45
|
-
}
|
|
46
|
-
if (Array.isArray(candidate)) {
|
|
47
|
-
return candidate.every(isJson);
|
|
48
|
-
}
|
|
49
|
-
return isRecord(candidate) && Object.values(candidate).every(isJson);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Whether `candidate` is an object whose every value is a finite number. */
|
|
53
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: checks a value read from storage
|
|
54
|
-
export function isNumberRecord(candidate: unknown): candidate is Readonly<Record<string, number>> {
|
|
55
|
-
return isRecord(candidate) && Object.values(candidate).every((entry) => optionalNumber(entry) !== undefined);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** The strings in `candidate`, or an empty list when it is not an array. */
|
|
59
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a list read from storage
|
|
60
|
-
export function stringsIn(candidate: unknown): string[] {
|
|
61
|
-
return Array.isArray(candidate) ? candidate.filter((entry): entry is string => typeof entry === 'string') : [];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Parse JSON text without throwing: `undefined` when it is not JSON. */
|
|
65
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: text read from storage, checked by the caller's guard next
|
|
66
|
-
export function parseJsonText(text: string): unknown {
|
|
67
|
-
try {
|
|
68
|
-
// eslint-disable-next-line no-restricted-syntax -- trust boundary: JSON.parse's result, checked by the caller's guard next
|
|
69
|
-
const parsed: unknown = JSON.parse(text);
|
|
70
|
-
return parsed;
|
|
71
|
-
} catch {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Small type guards for values this client did not create: JSON read back
|
|
3
|
+
* from `localStorage`, error bodies, objects from other bundles. The browser
|
|
4
|
+
* client carries no zod (plan D16), so these replace schema parsing on the
|
|
5
|
+
* few shapes it reads.
|
|
6
|
+
*/
|
|
7
|
+
import type { Json } from '@diffenderfer-games/hub-contract';
|
|
8
|
+
|
|
9
|
+
/** A plain object's fields, not yet checked. */
|
|
10
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: the fields of a value read from storage or another bundle
|
|
11
|
+
export type UncheckedRecord = Readonly<Record<string, unknown>>;
|
|
12
|
+
|
|
13
|
+
/** Whether `candidate` is a non-null, non-array object. */
|
|
14
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: checks a value read from storage or another bundle
|
|
15
|
+
export function isRecord(candidate: unknown): candidate is UncheckedRecord {
|
|
16
|
+
return typeof candidate === 'object' && candidate !== null && !Array.isArray(candidate);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** `candidate` when it is a string, else `undefined`. */
|
|
20
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: one field of an unchecked record
|
|
21
|
+
export function optionalString(candidate: unknown): string | undefined {
|
|
22
|
+
return typeof candidate === 'string' ? candidate : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** `candidate` when it is a finite number, else `undefined`. */
|
|
26
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: one field of an unchecked record
|
|
27
|
+
export function optionalNumber(candidate: unknown): number | undefined {
|
|
28
|
+
return typeof candidate === 'number' && Number.isFinite(candidate) ? candidate : undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Whether `candidate` is a string or `null`. */
|
|
32
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: one field of an unchecked record
|
|
33
|
+
export function isNullableString(candidate: unknown): candidate is string | null {
|
|
34
|
+
return candidate === null || typeof candidate === 'string';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Whether `candidate` is a JSON value (what `JSON.parse` can return). */
|
|
38
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: checks a value read from storage
|
|
39
|
+
export function isJson(candidate: unknown): candidate is Json {
|
|
40
|
+
if (candidate === null || typeof candidate === 'string' || typeof candidate === 'boolean') {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (typeof candidate === 'number') {
|
|
44
|
+
return Number.isFinite(candidate);
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(candidate)) {
|
|
47
|
+
return candidate.every(isJson);
|
|
48
|
+
}
|
|
49
|
+
return isRecord(candidate) && Object.values(candidate).every(isJson);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Whether `candidate` is an object whose every value is a finite number. */
|
|
53
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: checks a value read from storage
|
|
54
|
+
export function isNumberRecord(candidate: unknown): candidate is Readonly<Record<string, number>> {
|
|
55
|
+
return isRecord(candidate) && Object.values(candidate).every((entry) => optionalNumber(entry) !== undefined);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The strings in `candidate`, or an empty list when it is not an array. */
|
|
59
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: a list read from storage
|
|
60
|
+
export function stringsIn(candidate: unknown): string[] {
|
|
61
|
+
return Array.isArray(candidate) ? candidate.filter((entry): entry is string => typeof entry === 'string') : [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Parse JSON text without throwing: `undefined` when it is not JSON. */
|
|
65
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: text read from storage, checked by the caller's guard next
|
|
66
|
+
export function parseJsonText(text: string): unknown {
|
|
67
|
+
try {
|
|
68
|
+
// eslint-disable-next-line no-restricted-syntax -- trust boundary: JSON.parse's result, checked by the caller's guard next
|
|
69
|
+
const parsed: unknown = JSON.parse(text);
|
|
70
|
+
return parsed;
|
|
71
|
+
} catch {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The string store behind the offline cache and queues: `localStorage` when
|
|
3
|
-
* the page has it, else an in-memory map (privacy mode, Node, tests). Every
|
|
4
|
-
* key keeps the v1 client's name, so a page upgraded from a vendored copy
|
|
5
|
-
* still reads the queue and cache that copy wrote.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** A string key-value store with `localStorage`'s semantics. */
|
|
9
|
-
export interface KeyValueStore {
|
|
10
|
-
/** The stored text, or `null`. */
|
|
11
|
-
read(key: string): string | null;
|
|
12
|
-
/** Store text; a full or blocked storage is ignored. */
|
|
13
|
-
write(key: string, text: string): void;
|
|
14
|
-
/** Forget a key. */
|
|
15
|
-
remove(key: string): void;
|
|
16
|
-
/** Every stored key. */
|
|
17
|
-
keys(): string[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** A store backed by a `Storage` (`localStorage`). */
|
|
21
|
-
function storageBacked(storage: Storage): KeyValueStore {
|
|
22
|
-
return {
|
|
23
|
-
read: (key) => storage.getItem(key),
|
|
24
|
-
write: (key, text) => {
|
|
25
|
-
try {
|
|
26
|
-
storage.setItem(key, text);
|
|
27
|
-
} catch {
|
|
28
|
-
// A full or blocked storage loses the write, as in v1.
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
remove: (key) => {
|
|
32
|
-
storage.removeItem(key);
|
|
33
|
-
},
|
|
34
|
-
keys: () => Object.keys(storage),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** A store held in memory for this page only. */
|
|
39
|
-
export function createMemoryStore(): KeyValueStore {
|
|
40
|
-
const entries = new Map<string, string>();
|
|
41
|
-
return {
|
|
42
|
-
read: (key) => entries.get(key) ?? null,
|
|
43
|
-
write: (key, text) => {
|
|
44
|
-
entries.set(key, text);
|
|
45
|
-
},
|
|
46
|
-
remove: (key) => {
|
|
47
|
-
entries.delete(key);
|
|
48
|
-
},
|
|
49
|
-
keys: () => [...entries.keys()],
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** The page's `localStorage`, or `undefined` when it is missing or throws (privacy mode). */
|
|
54
|
-
function pageStorage(): Storage | undefined {
|
|
55
|
-
try {
|
|
56
|
-
return typeof localStorage === 'undefined' ? undefined : localStorage;
|
|
57
|
-
} catch {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** `localStorage` when available, else a fresh in-memory store. */
|
|
63
|
-
export function createPageStore(): KeyValueStore {
|
|
64
|
-
const storage = pageStorage();
|
|
65
|
-
return storage === undefined ? createMemoryStore() : storageBacked(storage);
|
|
66
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The string store behind the offline cache and queues: `localStorage` when
|
|
3
|
+
* the page has it, else an in-memory map (privacy mode, Node, tests). Every
|
|
4
|
+
* key keeps the v1 client's name, so a page upgraded from a vendored copy
|
|
5
|
+
* still reads the queue and cache that copy wrote.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A string key-value store with `localStorage`'s semantics. */
|
|
9
|
+
export interface KeyValueStore {
|
|
10
|
+
/** The stored text, or `null`. */
|
|
11
|
+
read(key: string): string | null;
|
|
12
|
+
/** Store text; a full or blocked storage is ignored. */
|
|
13
|
+
write(key: string, text: string): void;
|
|
14
|
+
/** Forget a key. */
|
|
15
|
+
remove(key: string): void;
|
|
16
|
+
/** Every stored key. */
|
|
17
|
+
keys(): string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A store backed by a `Storage` (`localStorage`). */
|
|
21
|
+
function storageBacked(storage: Storage): KeyValueStore {
|
|
22
|
+
return {
|
|
23
|
+
read: (key) => storage.getItem(key),
|
|
24
|
+
write: (key, text) => {
|
|
25
|
+
try {
|
|
26
|
+
storage.setItem(key, text);
|
|
27
|
+
} catch {
|
|
28
|
+
// A full or blocked storage loses the write, as in v1.
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
remove: (key) => {
|
|
32
|
+
storage.removeItem(key);
|
|
33
|
+
},
|
|
34
|
+
keys: () => Object.keys(storage),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A store held in memory for this page only. */
|
|
39
|
+
export function createMemoryStore(): KeyValueStore {
|
|
40
|
+
const entries = new Map<string, string>();
|
|
41
|
+
return {
|
|
42
|
+
read: (key) => entries.get(key) ?? null,
|
|
43
|
+
write: (key, text) => {
|
|
44
|
+
entries.set(key, text);
|
|
45
|
+
},
|
|
46
|
+
remove: (key) => {
|
|
47
|
+
entries.delete(key);
|
|
48
|
+
},
|
|
49
|
+
keys: () => [...entries.keys()],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The page's `localStorage`, or `undefined` when it is missing or throws (privacy mode). */
|
|
54
|
+
function pageStorage(): Storage | undefined {
|
|
55
|
+
try {
|
|
56
|
+
return typeof localStorage === 'undefined' ? undefined : localStorage;
|
|
57
|
+
} catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `localStorage` when available, else a fresh in-memory store. */
|
|
63
|
+
export function createPageStore(): KeyValueStore {
|
|
64
|
+
const storage = pageStorage();
|
|
65
|
+
return storage === undefined ? createMemoryStore() : storageBacked(storage);
|
|
66
|
+
}
|