ras-stack 4.0.0 → 4.2.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/README.md +1 -0
- package/dist/spacetime/index.d.ts +36 -0
- package/dist/spacetime/index.js +102 -0
- package/dist/spacetime/index.js.map +1 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -37,6 +37,7 @@ An application can use one surface without adopting the others. The npm package
|
|
|
37
37
|
| Authentication | Better Auth | Secure option builders, origins, secrets, redirects, failure classification, and React action state |
|
|
38
38
|
| Data | Drizzle, `better-sqlite3`, libsql, Postgres.js | Connection lifecycle, safety defaults, migrations, target selection, and conformance checks |
|
|
39
39
|
| Realtime | Centrifuge, Centrifugo, Caddy | Publishing, tokens, browser/React lifecycle, proxy configuration, binaries, and supervision |
|
|
40
|
+
| Realtime database | SpacetimeDB | Server calls, migration-planned module publishing, environment headers, and binding checks |
|
|
40
41
|
| Email and uploads | Nodemailer 9/10, `tus-js-client` | SMTP configuration/delivery, auth callbacks, and promise-based resumable uploads |
|
|
41
42
|
| Observability | PostHog JS, React, Node, and OpenTelemetry | Analytics, replay, errors, logs, metrics, tracing, correlation, proxying, and shutdown |
|
|
42
43
|
| Delivery | GitHub Actions, Changesets, Dokploy, Docker | Checks, releases, preview lifecycle/status, production assets, and runtime binaries |
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type SpacetimeConnection = {
|
|
2
|
+
/** Origin of the SpacetimeDB host. Plain HTTP is accepted only for loopback or `internalHost`. */
|
|
3
|
+
url: string;
|
|
4
|
+
database: string;
|
|
5
|
+
token: string;
|
|
6
|
+
/** Sent with every request, for example Cloudflare Access service-token headers. */
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
internalHost?: string;
|
|
9
|
+
fetch?: typeof fetch;
|
|
10
|
+
};
|
|
11
|
+
export declare class SpacetimeRequestError extends Error {
|
|
12
|
+
readonly status: number;
|
|
13
|
+
constructor(status: number, message: string);
|
|
14
|
+
}
|
|
15
|
+
export declare function spacetimeDatabaseUrl(connection: Pick<SpacetimeConnection, 'url' | 'database' | 'internalHost'>, path?: string): URL;
|
|
16
|
+
export declare function callSpacetime(connection: SpacetimeConnection, name: string, arguments_: unknown[], options?: {
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}): Promise<Response>;
|
|
19
|
+
/** `spacetime-environment` publish headers, which SpacetimeDB stores with the module and keeps across later publishes. */
|
|
20
|
+
export declare function moduleEnvironmentHeaders(environment: Record<string, string>): [string, string][];
|
|
21
|
+
export type PublishSpacetimeModuleOptions = {
|
|
22
|
+
environment?: Record<string, string>;
|
|
23
|
+
/** Accept an automatic migration that changes the schema in ways existing clients cannot read. */
|
|
24
|
+
breakClients?: boolean;
|
|
25
|
+
timeoutMs?: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Publishes a JavaScript module through SpacetimeDB's migration planner: a missing database is created, an automatic
|
|
29
|
+
* migration is applied, and a migration that needs manual steps or would break clients unasked is refused before
|
|
30
|
+
* anything changes.
|
|
31
|
+
*/
|
|
32
|
+
export declare function publishSpacetimeModule(connection: SpacetimeConnection, module: Uint8Array<ArrayBuffer>, options?: PublishSpacetimeModuleOptions): Promise<void>;
|
|
33
|
+
/** Deletes the database; one that does not exist counts as already deleted, so cleanup can run repeatedly. */
|
|
34
|
+
export declare function deleteSpacetimeDatabase(connection: SpacetimeConnection, options?: {
|
|
35
|
+
timeoutMs?: number;
|
|
36
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export class SpacetimeRequestError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
constructor(status, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.name = 'SpacetimeRequestError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function spacetimeDatabaseUrl(connection, path = '') {
|
|
10
|
+
const base = new URL(connection.url);
|
|
11
|
+
const internal = /^[a-z][a-z0-9-]{1,63}$/.test(connection.internalHost ?? '') && base.hostname === connection.internalHost;
|
|
12
|
+
const local = base.protocol === 'http:' && (['localhost', '127.0.0.1'].includes(base.hostname) || internal);
|
|
13
|
+
if ((!local && base.protocol !== 'https:') || base.username || base.password || base.search || base.hash || base.pathname !== '/') {
|
|
14
|
+
throw new Error('Invalid SpacetimeDB URL');
|
|
15
|
+
}
|
|
16
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(connection.database))
|
|
17
|
+
throw new Error('Invalid SpacetimeDB database name');
|
|
18
|
+
return new URL(`/v1/database/${connection.database}${path}`, base);
|
|
19
|
+
}
|
|
20
|
+
export async function callSpacetime(connection, name, arguments_, options = {}) {
|
|
21
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name))
|
|
22
|
+
throw new Error('Invalid SpacetimeDB procedure name');
|
|
23
|
+
return request(connection, spacetimeDatabaseUrl(connection, `/call/${name}`), name, {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: { 'content-type': 'application/json' },
|
|
26
|
+
body: JSON.stringify(arguments_),
|
|
27
|
+
timeoutMs: options.timeoutMs ?? 10_000,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** `spacetime-environment` publish headers, which SpacetimeDB stores with the module and keeps across later publishes. */
|
|
31
|
+
export function moduleEnvironmentHeaders(environment) {
|
|
32
|
+
return Object.entries(environment).map(([key, value]) => {
|
|
33
|
+
// A bare header value is visible ASCII without quotes, commas, or backslashes.
|
|
34
|
+
if (!/^[!#-&(-+\--[\]-~]+$/.test(value))
|
|
35
|
+
throw new Error(`Invalid ${key} for the module environment`);
|
|
36
|
+
return ['spacetime-environment', `${key}=${value}`];
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Publishes a JavaScript module through SpacetimeDB's migration planner: a missing database is created, an automatic
|
|
41
|
+
* migration is applied, and a migration that needs manual steps or would break clients unasked is refused before
|
|
42
|
+
* anything changes.
|
|
43
|
+
*/
|
|
44
|
+
export async function publishSpacetimeModule(connection, module, options = {}) {
|
|
45
|
+
const timeoutMs = options.timeoutMs ?? 90_000;
|
|
46
|
+
const binary = { 'content-type': 'application/octet-stream' };
|
|
47
|
+
const plan = await request(connection, spacetimeDatabaseUrl(connection, '/pre_publish?host_type=Js'), 'pre_publish', {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
headers: binary,
|
|
50
|
+
body: module,
|
|
51
|
+
timeoutMs,
|
|
52
|
+
allowNotFound: true,
|
|
53
|
+
});
|
|
54
|
+
const query = new URLSearchParams({ host_type: 'Js' });
|
|
55
|
+
if (plan.status !== 404) {
|
|
56
|
+
const result = (await plan.json());
|
|
57
|
+
if (result.ManualMigrate)
|
|
58
|
+
throw new Error(`SpacetimeDB database needs a manual migration: ${result.ManualMigrate.reason}`);
|
|
59
|
+
if (!result.AutoMigrate)
|
|
60
|
+
throw new Error('SpacetimeDB returned an unknown migration plan');
|
|
61
|
+
if (result.AutoMigrate.break_clients) {
|
|
62
|
+
if (!options.breakClients)
|
|
63
|
+
throw new Error('SpacetimeDB migration would break existing clients; pass breakClients to accept it');
|
|
64
|
+
query.set('policy', 'BreakClients');
|
|
65
|
+
query.set('token', result.AutoMigrate.token);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const environment = moduleEnvironmentHeaders(options.environment ?? {});
|
|
69
|
+
await request(connection, spacetimeDatabaseUrl(connection, `?${query}`), 'publish', {
|
|
70
|
+
method: 'PUT',
|
|
71
|
+
headers: [...Object.entries(binary), ...environment],
|
|
72
|
+
body: module,
|
|
73
|
+
timeoutMs,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Deletes the database; one that does not exist counts as already deleted, so cleanup can run repeatedly. */
|
|
77
|
+
export async function deleteSpacetimeDatabase(connection, options = {}) {
|
|
78
|
+
await request(connection, spacetimeDatabaseUrl(connection), 'delete', {
|
|
79
|
+
method: 'DELETE',
|
|
80
|
+
headers: {},
|
|
81
|
+
timeoutMs: options.timeoutMs ?? 15_000,
|
|
82
|
+
allowNotFound: true,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
async function request(connection, url, operation, init) {
|
|
86
|
+
const headers = new Headers(init.headers);
|
|
87
|
+
for (const [name, value] of Object.entries(connection.headers ?? {}))
|
|
88
|
+
headers.set(name, value);
|
|
89
|
+
headers.set('authorization', `Bearer ${connection.token}`);
|
|
90
|
+
const response = await (connection.fetch ?? fetch)(url, {
|
|
91
|
+
method: init.method,
|
|
92
|
+
headers,
|
|
93
|
+
...(init.body === undefined ? {} : { body: init.body }),
|
|
94
|
+
// A redirect would resend the bearer token to wherever it points.
|
|
95
|
+
redirect: 'manual',
|
|
96
|
+
signal: AbortSignal.timeout(init.timeoutMs),
|
|
97
|
+
});
|
|
98
|
+
if (response.ok || (init.allowNotFound && response.status === 404))
|
|
99
|
+
return response;
|
|
100
|
+
throw new SpacetimeRequestError(response.status, `SpacetimeDB ${operation} failed with HTTP ${response.status}: ${(await response.text()).slice(0, 300)}`);
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/spacetime/index.ts"],"names":[],"mappings":"AAWA,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAEnC,MAAM;IADjB,YACW,MAAc,EACvB,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAA;sBAHL,MAAM;QAIf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAA;IACrC,CAAC;CACF;AAED,MAAM,UAAU,oBAAoB,CAAC,UAA0E,EAAE,IAAI,GAAG,EAAE;IACxH,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;IACpC,MAAM,QAAQ,GAAG,wBAAwB,CAAC,IAAI,CAAC,UAAU,CAAC,YAAY,IAAI,EAAE,CAAC,IAAI,IAAI,CAAC,QAAQ,KAAK,U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AC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,IAAI,EAAE,CAAC;QAAE,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;IAC9F,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,UAAU,CAAC,KAAK,EAAE,CAAC,CAAA;IAC1D,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,GAAG,EAAE;QACtD,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,OAAO;QACP,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;QACvD,kEAAkE;QAClE,QAAQ,EAAE,QAAQ;QAClB,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC;KAC5C,CAAC,CAAA;IACF,IAAI,QAAQ,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,CAAC;QAAE,OAAO,QAAQ,CAAA;IACnF,MAAM,IAAI,qBAAqB,CAC7B,QAAQ,CAAC,MAAM,EACf,eAAe,SAAS,qBAAqB,QAAQ,CAAC,MAAM,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CACzG,CAAA;AACH,CAAC","sourcesContent":["export type SpacetimeConnection = {\n /** Origin of the SpacetimeDB host. Plain HTTP is accepted only for loopback or `internalHost`. */\n url: string\n database: string\n token: string\n /** Sent with every request, for example Cloudflare Access service-token headers. */\n headers?: Record<string, string>\n internalHost?: string\n fetch?: typeof fetch\n}\n\nexport class SpacetimeRequestError extends Error {\n constructor(\n readonly status: number,\n message: string,\n ) {\n super(message)\n this.name = 'SpacetimeRequestError'\n }\n}\n\nexport function spacetimeDatabaseUrl(connection: Pick<SpacetimeConnection, 'url' | 'database' | 'internalHost'>, path = '') {\n const base = new URL(connection.url)\n const internal = /^[a-z][a-z0-9-]{1,63}$/.test(connection.internalHost ?? '') && base.hostname === connection.internalHost\n const local = base.protocol === 'http:' && (['localhost', '127.0.0.1'].includes(base.hostname) || internal)\n if ((!local && base.protocol !== 'https:') || base.username || base.password || base.search || base.hash || base.pathname !== '/') {\n throw new Error('Invalid SpacetimeDB URL')\n }\n if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(connection.database)) throw new Error('Invalid SpacetimeDB database name')\n return new URL(`/v1/database/${connection.database}${path}`, base)\n}\n\nexport async function callSpacetime(\n connection: SpacetimeConnection,\n name: string,\n arguments_: unknown[],\n options: { timeoutMs?: number } = {},\n) {\n if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) throw new Error('Invalid SpacetimeDB procedure name')\n return request(connection, spacetimeDatabaseUrl(connection, `/call/${name}`), name, {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify(arguments_),\n timeoutMs: options.timeoutMs ?? 10_000,\n })\n}\n\n/** `spacetime-environment` publish headers, which SpacetimeDB stores with the module and keeps across later publishes. */\nexport function moduleEnvironmentHeaders(environment: Record<string, string>): [string, string][] {\n return Object.entries(environment).map(([key, value]) => {\n // A bare header value is visible ASCII without quotes, commas, or backslashes.\n if (!/^[!#-&(-+\\--[\\]-~]+$/.test(value)) throw new Error(`Invalid ${key} for the module environment`)\n return ['spacetime-environment', `${key}=${value}`]\n })\n}\n\nexport type PublishSpacetimeModuleOptions = {\n environment?: Record<string, string>\n /** Accept an automatic migration that changes the schema in ways existing clients cannot read. */\n breakClients?: boolean\n timeoutMs?: number\n}\n\n/**\n * Publishes a JavaScript module through SpacetimeDB's migration planner: a missing database is created, an automatic\n * migration is applied, and a migration that needs manual steps or would break clients unasked is refused before\n * anything changes.\n */\nexport async function publishSpacetimeModule(\n connection: SpacetimeConnection,\n module: Uint8Array<ArrayBuffer>,\n options: PublishSpacetimeModuleOptions = {},\n) {\n const timeoutMs = options.timeoutMs ?? 90_000\n const binary = { 'content-type': 'application/octet-stream' }\n const plan = await request(connection, spacetimeDatabaseUrl(connection, '/pre_publish?host_type=Js'), 'pre_publish', {\n method: 'POST',\n headers: binary,\n body: module,\n timeoutMs,\n allowNotFound: true,\n })\n const query = new URLSearchParams({ host_type: 'Js' })\n if (plan.status !== 404) {\n const result = (await plan.json()) as {\n AutoMigrate?: { token: string; break_clients: boolean }\n ManualMigrate?: { reason: string }\n }\n if (result.ManualMigrate) throw new Error(`SpacetimeDB database needs a manual migration: ${result.ManualMigrate.reason}`)\n if (!result.AutoMigrate) throw new Error('SpacetimeDB returned an unknown migration plan')\n if (result.AutoMigrate.break_clients) {\n if (!options.breakClients) throw new Error('SpacetimeDB migration would break existing clients; pass breakClients to accept it')\n query.set('policy', 'BreakClients')\n query.set('token', result.AutoMigrate.token)\n }\n }\n const environment = moduleEnvironmentHeaders(options.environment ?? {})\n await request(connection, spacetimeDatabaseUrl(connection, `?${query}`), 'publish', {\n method: 'PUT',\n headers: [...Object.entries(binary), ...environment],\n body: module,\n timeoutMs,\n })\n}\n\n/** Deletes the database; one that does not exist counts as already deleted, so cleanup can run repeatedly. */\nexport async function deleteSpacetimeDatabase(connection: SpacetimeConnection, options: { timeoutMs?: number } = {}) {\n await request(connection, spacetimeDatabaseUrl(connection), 'delete', {\n method: 'DELETE',\n headers: {},\n timeoutMs: options.timeoutMs ?? 15_000,\n allowNotFound: true,\n })\n}\n\nasync function request(\n connection: SpacetimeConnection,\n url: URL,\n operation: string,\n init: { method: string; headers: HeadersInit; body?: BodyInit; timeoutMs: number; allowNotFound?: boolean },\n) {\n const headers = new Headers(init.headers)\n for (const [name, value] of Object.entries(connection.headers ?? {})) headers.set(name, value)\n headers.set('authorization', `Bearer ${connection.token}`)\n const response = await (connection.fetch ?? fetch)(url, {\n method: init.method,\n headers,\n ...(init.body === undefined ? {} : { body: init.body }),\n // A redirect would resend the bearer token to wherever it points.\n redirect: 'manual',\n signal: AbortSignal.timeout(init.timeoutMs),\n })\n if (response.ok || (init.allowNotFound && response.status === 404)) return response\n throw new SpacetimeRequestError(\n response.status,\n `SpacetimeDB ${operation} failed with HTTP ${response.status}: ${(await response.text()).slice(0, 300)}`,\n )\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ras-stack",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Composable full-stack primitives shared across Richard Solomou's applications.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authentication",
|
|
@@ -102,6 +102,10 @@
|
|
|
102
102
|
"types": "./dist/runtime/index.d.ts",
|
|
103
103
|
"default": "./dist/runtime/index.js"
|
|
104
104
|
},
|
|
105
|
+
"./spacetime": {
|
|
106
|
+
"types": "./dist/spacetime/index.d.ts",
|
|
107
|
+
"default": "./dist/spacetime/index.js"
|
|
108
|
+
},
|
|
105
109
|
"./tanstack/client": {
|
|
106
110
|
"types": "./dist/tanstack/client.d.ts",
|
|
107
111
|
"default": "./dist/tanstack/client.js"
|