@base44-preview/sdk 0.8.60-pr.375.db5e899 → 0.8.60-pr.377.428760d
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/dist/actor.d.ts +47 -0
- package/dist/actor.js +8 -0
- package/dist/client.js +21 -21
- package/dist/client.types.d.ts +3 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/modules/actors.d.ts +6 -1
- package/dist/modules/actors.js +31 -1
- package/dist/modules/actors.types.d.ts +52 -6
- package/package.json +1 -8
- package/dist/utils/build-commit-source.d.ts +0 -19
- package/dist/utils/build-commit-source.fallback.d.ts +0 -14
- package/dist/utils/build-commit-source.fallback.js +0 -16
- package/dist/utils/build-commit-source.js +0 -26
- package/dist/utils/build-commit.d.ts +0 -12
- package/dist/utils/build-commit.js +0 -24
package/dist/actor.d.ts
CHANGED
|
@@ -9,6 +9,31 @@
|
|
|
9
9
|
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
10
|
*/
|
|
11
11
|
import type { Base44Client } from "./client";
|
|
12
|
+
/** Who a client is, verified by the platform: a logged-in user or an anonymous visitor. */
|
|
13
|
+
export type ActorConnectionIdentity = Readonly<{
|
|
14
|
+
type: "authenticated";
|
|
15
|
+
userId: string;
|
|
16
|
+
}> | Readonly<{
|
|
17
|
+
type: "anonymous";
|
|
18
|
+
anonymousId: string;
|
|
19
|
+
}>;
|
|
20
|
+
/** Who called one of the actor's callable methods: a client, or the app's server
|
|
21
|
+
* code calling with the service role (`service`). */
|
|
22
|
+
export type ActorCallIdentity = ActorConnectionIdentity | Readonly<{
|
|
23
|
+
type: "service";
|
|
24
|
+
}>;
|
|
25
|
+
/** What a callable method receives besides its args. */
|
|
26
|
+
export interface ActorCallContext {
|
|
27
|
+
identity: ActorCallIdentity;
|
|
28
|
+
/** Carries the caller's own credential, so `createClientFromRequest(request)` acts
|
|
29
|
+
* as the caller, as in a backend function. Don't store it. */
|
|
30
|
+
request: Request;
|
|
31
|
+
}
|
|
32
|
+
/** Throw from a callable method to refuse the call with a 4xx status. */
|
|
33
|
+
export declare class ActorError extends Error {
|
|
34
|
+
readonly status: number;
|
|
35
|
+
constructor(status: number, message: string);
|
|
36
|
+
}
|
|
12
37
|
/**
|
|
13
38
|
* A single client connection. `Send` is the message type this connection accepts
|
|
14
39
|
* via {@link send} — the actor's *outgoing* (server→client) messages.
|
|
@@ -18,6 +43,8 @@ export interface Conn<Send = unknown> {
|
|
|
18
43
|
* receives from `subscribe()`. Identifies a distinct client, so multiple
|
|
19
44
|
* tabs are separate connections. */
|
|
20
45
|
id: string;
|
|
46
|
+
/** The client's identity, verified by the platform before the connection reached the actor. */
|
|
47
|
+
identity?: ActorConnectionIdentity;
|
|
21
48
|
send(data: Send): void;
|
|
22
49
|
reject(code: number, reason: string): void;
|
|
23
50
|
}
|
|
@@ -60,6 +87,26 @@ export declare abstract class Actor<Incoming = unknown, Outgoing = unknown> {
|
|
|
60
87
|
* The schedule is one-shot: it fires once and is cleared before this runs.
|
|
61
88
|
*/
|
|
62
89
|
protected handleWake(_key: string): void | Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* The methods a caller may run on a session, as `base44.actors.<Name>(id).<method>(args)`
|
|
92
|
+
* from a client or `base44.asServiceRole.actors.<Name>(id).<method>(args)` from server code.
|
|
93
|
+
* Nothing else is callable, and a hook-shaped name (`handle*`, `on*`, `_*`) never is.
|
|
94
|
+
*
|
|
95
|
+
* Each runs as `method(args, { identity, request })` (see {@link ActorCallContext}), even
|
|
96
|
+
* when no client is connected, and can {@link broadcast} to the ones that are. Check
|
|
97
|
+
* `identity` before acting: a client can call a method on any session it can name. The
|
|
98
|
+
* return value is the caller's reply; throw an {@link ActorError} to refuse with a 4xx.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* static callable = ["videoReady"];
|
|
103
|
+
* async videoReady(args, { identity }) {
|
|
104
|
+
* if (identity.type !== "service") throw new ActorError(403, "Forbidden");
|
|
105
|
+
* this.broadcast({ type: "video", ...args });
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
static callable?: readonly string[];
|
|
63
110
|
/**
|
|
64
111
|
* Arm a one-shot wake at `at` (epoch ms or a `Date`), identified by `key`.
|
|
65
112
|
* When it comes due the platform calls {@link handleWake} with this `key`.
|
package/dist/actor.js
CHANGED
|
@@ -8,6 +8,14 @@
|
|
|
8
8
|
* At deploy time the bundler replaces this import with the compiled
|
|
9
9
|
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
10
|
*/
|
|
11
|
+
/** Throw from a callable method to refuse the call with a 4xx status. */
|
|
12
|
+
export class ActorError extends Error {
|
|
13
|
+
constructor(status, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.name = "ActorError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
11
19
|
/**
|
|
12
20
|
* Base class for an Actor.
|
|
13
21
|
*
|
package/dist/client.js
CHANGED
|
@@ -8,7 +8,6 @@ import { createVoiceModule } from "./modules/voice.js";
|
|
|
8
8
|
import { createConnectorsModule, createUserConnectorsModule, } from "./modules/connectors.js";
|
|
9
9
|
import { getAccessToken } from "./utils/auth-utils.js";
|
|
10
10
|
import { createFetchWithAuth } from "./utils/fetch-with-auth.js";
|
|
11
|
-
import { BUILD_COMMIT_HEADER, getBuildCommitHeaders, } from "./utils/build-commit.js";
|
|
12
11
|
import { createFunctionsModule } from "./modules/functions.js";
|
|
13
12
|
import { createAgentsModule } from "./modules/agents.js";
|
|
14
13
|
import { createAiGatewayModule } from "./modules/ai-gateway.js";
|
|
@@ -18,7 +17,7 @@ import { createUsersModule } from "./modules/users.js";
|
|
|
18
17
|
import { RoomsSocket } from "./utils/socket-utils.js";
|
|
19
18
|
import { createAnalyticsModule } from "./modules/analytics.js";
|
|
20
19
|
import { createPaymentsModule, createServiceRolePaymentsModule, } from "./modules/payments.js";
|
|
21
|
-
import { createActorsModule, resolveActorsHost, } from "./modules/actors.js";
|
|
20
|
+
import { createActorsModule, createServiceRoleActorsModule, resolveActorsHost, } from "./modules/actors.js";
|
|
22
21
|
/**
|
|
23
22
|
* Creates a Base44 client.
|
|
24
23
|
*
|
|
@@ -57,9 +56,6 @@ import { createActorsModule, resolveActorsHost, } from "./modules/actors.js";
|
|
|
57
56
|
* ```
|
|
58
57
|
*/
|
|
59
58
|
export function createClient(config) {
|
|
60
|
-
return buildClient(config, { sendBuildCommit: true });
|
|
61
|
-
}
|
|
62
|
-
function buildClient(config, { sendBuildCommit }) {
|
|
63
59
|
var _a, _b, _c, _d;
|
|
64
60
|
const { serverUrl = "https://base44.app", appId, analytics, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
|
|
65
61
|
// Normalize appBaseUrl to always be a string (empty if not provided or invalid)
|
|
@@ -80,18 +76,7 @@ function buildClient(config, { sendBuildCommit }) {
|
|
|
80
76
|
}
|
|
81
77
|
return socket;
|
|
82
78
|
};
|
|
83
|
-
// The build commit goes on every API call, so the backend can tell which
|
|
84
|
-
// frontend bundle a request came from. One set by the caller, in any
|
|
85
|
-
// casing, replaces it; resolving that once here keeps every call path,
|
|
86
|
-
// functions.fetch() included, reporting the same build.
|
|
87
|
-
const callerBuildCommit = Object.entries(optionalHeaders !== null && optionalHeaders !== void 0 ? optionalHeaders : {}).find(([name]) => name.toLowerCase() === BUILD_COMMIT_HEADER.toLowerCase());
|
|
88
|
-
const buildCommitHeaders = callerBuildCommit
|
|
89
|
-
? { [BUILD_COMMIT_HEADER]: callerBuildCommit[1] }
|
|
90
|
-
: sendBuildCommit
|
|
91
|
-
? getBuildCommitHeaders()
|
|
92
|
-
: {};
|
|
93
79
|
const headers = {
|
|
94
|
-
...(callerBuildCommit ? {} : buildCommitHeaders),
|
|
95
80
|
...optionalHeaders,
|
|
96
81
|
"X-App-Id": String(appId),
|
|
97
82
|
};
|
|
@@ -136,6 +121,21 @@ function buildClient(config, { sendBuildCommit }) {
|
|
|
136
121
|
token: serviceToken,
|
|
137
122
|
interceptResponses: false,
|
|
138
123
|
});
|
|
124
|
+
// The backend authenticates the caller from the axios client's token (or the
|
|
125
|
+
// anonymous id it adds in a browser) and relays the call to the room.
|
|
126
|
+
const actorMethodCaller = (client) => async (actorName, room, method, args) => {
|
|
127
|
+
var _a;
|
|
128
|
+
const response = await client.post(`/apps/${appId}/actors/${encodeURIComponent(actorName)}/rooms/${encodeURIComponent(room)}/requests`, JSON.stringify({ method, args }), {
|
|
129
|
+
headers: {
|
|
130
|
+
"Content-Type": "application/json",
|
|
131
|
+
// Draft vs published copy, as for a connection.
|
|
132
|
+
...(functionsVersion
|
|
133
|
+
? { "Base44-Functions-Version": functionsVersion }
|
|
134
|
+
: {}),
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
return (_a = response === null || response === void 0 ? void 0 : response.reply) !== null && _a !== void 0 ? _a : undefined;
|
|
138
|
+
};
|
|
139
139
|
// Dedicated client for actor connection-token mints: no onError (a legacy
|
|
140
140
|
// actor answers every mint with an expected 409 before the proxy fallback,
|
|
141
141
|
// which must not reach the app's error handler — the actors module forwards
|
|
@@ -182,6 +182,7 @@ function buildClient(config, { sendBuildCommit }) {
|
|
|
182
182
|
},
|
|
183
183
|
transport: options === null || options === void 0 ? void 0 : options.actorsTransport,
|
|
184
184
|
onMintError: options === null || options === void 0 ? void 0 : options.onError,
|
|
185
|
+
callMethod: actorMethodCaller(axiosClient),
|
|
185
186
|
});
|
|
186
187
|
const userModules = {
|
|
187
188
|
entities: createEntitiesModule({
|
|
@@ -194,7 +195,7 @@ function buildClient(config, { sendBuildCommit }) {
|
|
|
194
195
|
auth: userAuthModule,
|
|
195
196
|
functions: createFunctionsModule(functionsAxiosClient, appId, {
|
|
196
197
|
getAuthHeaders: () => {
|
|
197
|
-
const headers = {
|
|
198
|
+
const headers = {};
|
|
198
199
|
// Get current token from storage or initial config
|
|
199
200
|
const currentToken = token || getAccessToken();
|
|
200
201
|
if (currentToken) {
|
|
@@ -274,6 +275,7 @@ function buildClient(config, { sendBuildCommit }) {
|
|
|
274
275
|
}),
|
|
275
276
|
appLogs: createAppLogsModule(serviceRoleAxiosClient, appId),
|
|
276
277
|
payments: createServiceRolePaymentsModule(serviceRoleAxiosClient, appId),
|
|
278
|
+
actors: createServiceRoleActorsModule(actorMethodCaller(serviceRoleAxiosClient)),
|
|
277
279
|
cleanup: () => {
|
|
278
280
|
if (socket) {
|
|
279
281
|
socket.disconnect();
|
|
@@ -471,14 +473,12 @@ export function createClientFromRequest(request) {
|
|
|
471
473
|
if (dataEnvHeader === "dev" || dataEnvHeader === "prod") {
|
|
472
474
|
additionalHeaders["X-Data-Env"] = dataEnvHeader;
|
|
473
475
|
}
|
|
474
|
-
|
|
475
|
-
// commit, whatever its runtime looks like.
|
|
476
|
-
return buildClient({
|
|
476
|
+
return createClient({
|
|
477
477
|
serverUrl: serverUrlHeader || "https://base44.app",
|
|
478
478
|
appId,
|
|
479
479
|
token: userToken,
|
|
480
480
|
serviceToken: serviceRoleToken,
|
|
481
481
|
functionsVersion: functionsVersion !== null && functionsVersion !== void 0 ? functionsVersion : undefined,
|
|
482
482
|
headers: additionalHeaders,
|
|
483
|
-
}
|
|
483
|
+
});
|
|
484
484
|
}
|
package/dist/client.types.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ import type { AiGatewayModule } from "./modules/ai-gateway.types.js";
|
|
|
12
12
|
import type { AppLogsModule } from "./modules/app-logs.types.js";
|
|
13
13
|
import type { AppModule } from "./modules/app.types.js";
|
|
14
14
|
import type { AnalyticsModule } from "./modules/analytics.types.js";
|
|
15
|
-
import type { ActorsModule } from "./modules/actors.types.js";
|
|
15
|
+
import type { ActorsModule, ServiceRoleActorsModule } from "./modules/actors.types.js";
|
|
16
16
|
import type { FetchWithAuthInit } from "./utils/fetch-with-auth.js";
|
|
17
17
|
/**
|
|
18
18
|
* Options for creating a Base44 client.
|
|
@@ -226,6 +226,8 @@ export interface Base44Client {
|
|
|
226
226
|
* @throws {Error} When accessed without providing a serviceToken during client creation
|
|
227
227
|
*/
|
|
228
228
|
readonly asServiceRole: {
|
|
229
|
+
/** {@link ServiceRoleActorsModule | Actors module} for sending requests to actor sessions as the app's server code. */
|
|
230
|
+
actors: ServiceRoleActorsModule;
|
|
229
231
|
/** {@link AgentsModule | Agents module} with elevated permissions. */
|
|
230
232
|
agents: AgentsModule;
|
|
231
233
|
/** {@link AiGatewayModule | AI Gateway module} with the service-role token. */
|
package/dist/index.d.ts
CHANGED
|
@@ -12,12 +12,12 @@ export type { AgentsModule, AgentName, AgentNameRegistry, AgentConversation, Age
|
|
|
12
12
|
export type { AiGatewayModule, AiGatewayConnection, AiGatewayConnectionOptions, } from "./modules/ai-gateway.types.js";
|
|
13
13
|
export type { AppLogsModule } from "./modules/app-logs.types.js";
|
|
14
14
|
export type { AppModule, AppPublicSettings, AppPublicSettingsResponse, } from "./modules/app.types.js";
|
|
15
|
-
export type { ActorsModule, ActorClient, ActorRef, Connection, ActorSubscription, ActorConnectOptions, ActorNameRegistry, ActorRegistry, } from "./modules/actors.types.js";
|
|
15
|
+
export type { ActorsModule, ActorClient, ActorRef, Connection, ActorSubscription, ActorConnectOptions, ActorNameRegistry, ActorRegistry, ActorConnectable, ActorMethods, ServiceRoleActorRef, ServiceRoleActorsModule, } from "./modules/actors.types.js";
|
|
16
16
|
export type { SsoModule, SsoAccessTokenResponse } from "./modules/sso.types.js";
|
|
17
17
|
export type { PaymentsModule, ServiceRolePaymentsModule, PaymentsErrorCode, PaymentsRecurringFrequency, CheckoutBuyer, CheckoutFreeTrial, CheckoutItem, CheckoutItemRecurring, CheckoutPresentation, CheckoutShipping, CheckoutElement, CheckoutMount, MountableCheckout, CreateCheckoutParams, CreateCheckoutResult, CompleteCheckoutResult, EmbeddedCheckout, PayPalCheckoutOnClick, PayPalEmbeddedCheckout, StripeEmbeddedCheckout, MountCheckoutOptions, ListPurchasesParams, ServiceRoleListPurchasesParams, ListPurchasesResult, Purchase, PurchaseItem, PurchaseStatus, RefundParams, ShippingAddress, RefundResult, CancelSubscriptionOptions, SubscriptionResult, } from "./modules/payments.types.js";
|
|
18
18
|
export type { BrowserModule, BrowserSession, BrowserSessionOptions, } from "./modules/browser.types.js";
|
|
19
19
|
export type { VoiceModule, VoiceSession, VoiceSessionMessage, VoiceSessionOptions, } from "./modules/voice.types.js";
|
|
20
|
-
export { Actor, type Conn } from "./actor.js";
|
|
20
|
+
export { Actor, type Conn, type ActorConnectionIdentity, ActorError, type ActorCallIdentity, type ActorCallContext, } from "./actor.js";
|
|
21
21
|
export type { ConnectorsModule, UserConnectorsModule, ConnectorApiRequest, ConnectorApiResponse, ConnectorApiResponsePhase, } from "./modules/connectors.types.js";
|
|
22
22
|
export type { CustomIntegrationsModule, CustomIntegrationCallParams, CustomIntegrationCallResponse, } from "./modules/custom-integrations.types.js";
|
|
23
23
|
export type { GetAccessTokenOptions, SaveAccessTokenOptions, RemoveAccessTokenOptions, GetLoginUrlOptions, } from "./utils/auth-utils.types.js";
|
package/dist/index.js
CHANGED
|
@@ -3,4 +3,4 @@ import { Base44Error } from "./utils/axios-client.js";
|
|
|
3
3
|
import { getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, } from "./utils/auth-utils.js";
|
|
4
4
|
export { createClient, createClientFromRequest, Base44Error, getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, };
|
|
5
5
|
export * from "./types.js";
|
|
6
|
-
export { Actor } from "./actor.js";
|
|
6
|
+
export { Actor, ActorError, } from "./actor.js";
|
package/dist/modules/actors.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { ActorRef } from "./actors.types.js";
|
|
1
|
+
import type { ActorRef, ServiceRoleActorRef } from "./actors.types.js";
|
|
2
|
+
/** Calls one of a session's callable methods; resolves to its JSON reply. */
|
|
3
|
+
export type CallActorMethod = (actorName: string, room: string, method: string, args: unknown) => Promise<unknown>;
|
|
2
4
|
/** Credentials minted by the platform for one direct actor connection. */
|
|
3
5
|
export interface ActorConnectionCredentials {
|
|
4
6
|
/** Direct actor endpoint, already carrying `?_pk=<connectionId>`. */
|
|
@@ -30,6 +32,7 @@ interface ActorsConfig {
|
|
|
30
32
|
* direct→proxy fallback (which recovers by itself). Wired to the client's
|
|
31
33
|
* `options.onError`. */
|
|
32
34
|
onMintError?: (error: Error) => void;
|
|
35
|
+
callMethod: CallActorMethod;
|
|
33
36
|
}
|
|
34
37
|
/**
|
|
35
38
|
* The legacy platform-proxy URL, byte-for-byte what PartySocket built before
|
|
@@ -48,4 +51,6 @@ export declare function createActorsModule(config: ActorsConfig): {
|
|
|
48
51
|
module: Record<string, (instanceId: string) => ActorRef>;
|
|
49
52
|
closeAll: () => void;
|
|
50
53
|
};
|
|
54
|
+
/** `asServiceRole.actors`: server code can't hold a connection, so it only calls methods. */
|
|
55
|
+
export declare function createServiceRoleActorsModule(callMethod: CallActorMethod): Record<string, (instanceId: string) => ServiceRoleActorRef>;
|
|
51
56
|
export {};
|
package/dist/modules/actors.js
CHANGED
|
@@ -1,4 +1,23 @@
|
|
|
1
1
|
import { WebSocket as ReconnectingWebSocket } from "partysocket";
|
|
2
|
+
// Never actor methods: the ref's own API, what `await`/JSON/frameworks probe for, and
|
|
3
|
+
// Object.prototype (resolved through the `in` check below).
|
|
4
|
+
const NOT_METHODS = new Set(["then", "catch", "finally", "toJSON", "asymmetricMatch", "nodeType", "constructor"]);
|
|
5
|
+
function isMethodName(target, key) {
|
|
6
|
+
return (typeof key === "string" &&
|
|
7
|
+
!(key in target) &&
|
|
8
|
+
!NOT_METHODS.has(key) &&
|
|
9
|
+
!key.startsWith("$$") &&
|
|
10
|
+
!key.startsWith("@@"));
|
|
11
|
+
}
|
|
12
|
+
function withMethods(target, call) {
|
|
13
|
+
return new Proxy(target, {
|
|
14
|
+
get(obj, key) {
|
|
15
|
+
return isMethodName(obj, key)
|
|
16
|
+
? (args) => call(key, args)
|
|
17
|
+
: Reflect.get(obj, key);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
2
21
|
// Heartbeat / half-open detection: the socket only reconnects on a close/error
|
|
3
22
|
// event, so ping periodically and force a reconnect if nothing returns in DEAD_MS.
|
|
4
23
|
const PING_MS = 1000;
|
|
@@ -153,7 +172,7 @@ class Connection {
|
|
|
153
172
|
/** Handle for one actor instance: `connect()` opens the socket (idempotent). */
|
|
154
173
|
function makeActorRef(actorName, instanceId, config, connections) {
|
|
155
174
|
let conn = null;
|
|
156
|
-
|
|
175
|
+
const ref = {
|
|
157
176
|
connect(options) {
|
|
158
177
|
if (conn)
|
|
159
178
|
return conn;
|
|
@@ -167,6 +186,7 @@ function makeActorRef(actorName, instanceId, config, connections) {
|
|
|
167
186
|
return c;
|
|
168
187
|
},
|
|
169
188
|
};
|
|
189
|
+
return withMethods(ref, (method, args) => config.callMethod(actorName, instanceId, method, args));
|
|
170
190
|
}
|
|
171
191
|
/**
|
|
172
192
|
* The legacy platform-proxy URL, byte-for-byte what PartySocket built before
|
|
@@ -226,3 +246,13 @@ export function createActorsModule(config) {
|
|
|
226
246
|
},
|
|
227
247
|
};
|
|
228
248
|
}
|
|
249
|
+
/** `asServiceRole.actors`: server code can't hold a connection, so it only calls methods. */
|
|
250
|
+
export function createServiceRoleActorsModule(callMethod) {
|
|
251
|
+
return new Proxy({}, {
|
|
252
|
+
get(_, key) {
|
|
253
|
+
if (typeof key !== "string" || key === "then")
|
|
254
|
+
return undefined;
|
|
255
|
+
return (instanceId) => withMethods({}, (method, args) => callMethod(key, instanceId, method, args));
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
}
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
* chatRoom: {
|
|
21
21
|
* toClient: { type: "joined" | "left" | "message"; userId?: string; from?: string; text?: string };
|
|
22
22
|
* toServer: { type: "message"; text: string };
|
|
23
|
+
* methods: { kick: (args: { userId: string }) => { kicked: boolean } };
|
|
23
24
|
* };
|
|
24
25
|
* }
|
|
25
26
|
* }
|
|
@@ -43,6 +44,18 @@ type ToClientFor<N extends string> = N extends keyof ActorRegistry ? ActorRegist
|
|
|
43
44
|
type ToServerFor<N extends string> = N extends keyof ActorRegistry ? ActorRegistry[N] extends {
|
|
44
45
|
toServer: infer O;
|
|
45
46
|
} ? O : unknown : unknown;
|
|
47
|
+
type ActorMethodCall<F> = F extends (args: infer A, ...rest: never[]) => infer R ? undefined extends A ? (args?: A) => Promise<Awaited<R>> : (args: A) => Promise<Awaited<R>> : never;
|
|
48
|
+
type UntypedActorMethods = Record<string, (args?: unknown) => Promise<unknown>>;
|
|
49
|
+
/**
|
|
50
|
+
* An actor's callable methods, as seen by a caller: each runs the actor's method of the same
|
|
51
|
+
* name and resolves to its return value. Typed from the actor's `methods` in
|
|
52
|
+
* {@linkcode ActorRegistry}; any name otherwise.
|
|
53
|
+
*/
|
|
54
|
+
export type ActorMethods<N extends string> = N extends keyof ActorRegistry ? ActorRegistry[N] extends {
|
|
55
|
+
methods: infer M;
|
|
56
|
+
} ? {
|
|
57
|
+
[K in keyof M]: ActorMethodCall<M[K]>;
|
|
58
|
+
} : UntypedActorMethods : UntypedActorMethods;
|
|
46
59
|
/**
|
|
47
60
|
* Configures the connection that {@linkcode ActorRef.connect | connect()} opens.
|
|
48
61
|
*/
|
|
@@ -134,9 +147,19 @@ export interface Connection<N extends string = string> {
|
|
|
134
147
|
/**
|
|
135
148
|
* Represents a reference to an actor session, identified by actor name and session ID.
|
|
136
149
|
*
|
|
137
|
-
* Call {@linkcode ActorRef.connect | connect()} to open the WebSocket and get a [connection](/developers/backend/resources/actors/overview#connections)
|
|
150
|
+
* Call {@linkcode ActorRef.connect | connect()} to open the WebSocket and get a [connection](/developers/backend/resources/actors/overview#connections),
|
|
151
|
+
* or call one of the actor's callable methods by name, such as `ref.kick({ userId })`: it runs
|
|
152
|
+
* the actor's method with your identity, even when no client is connected, and resolves to
|
|
153
|
+
* its return value. A refusal rejects with a `Base44Error` carrying the actor's status (404 for
|
|
154
|
+
* a method the actor doesn't list in `static callable`).
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* // Call a method on a session
|
|
159
|
+
* const { kicked } = await base44.actors.chatRoom("session-1").kick({ userId });
|
|
160
|
+
* ```
|
|
138
161
|
*/
|
|
139
|
-
export interface
|
|
162
|
+
export interface ActorConnectable<N extends string = string> {
|
|
140
163
|
/**
|
|
141
164
|
* Creates or returns the connection for this session.
|
|
142
165
|
*
|
|
@@ -157,6 +180,26 @@ export interface ActorRef<N extends string = string> {
|
|
|
157
180
|
*/
|
|
158
181
|
connect(options?: ActorConnectOptions): Connection<N>;
|
|
159
182
|
}
|
|
183
|
+
export type ActorRef<N extends string = string> = ActorConnectable<N> & ActorMethods<N>;
|
|
184
|
+
/**
|
|
185
|
+
* Represents a reference to an actor session for server code running with the
|
|
186
|
+
* [service role](/developers/references/sdk/getting-started/client#service-role).
|
|
187
|
+
* Server code can't hold a connection, so it only calls methods; the actor sees the
|
|
188
|
+
* `service` identity.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* // Tell a user's session that their video is ready
|
|
193
|
+
* await base44.asServiceRole.actors.VideoRoom(userId).videoReady({ id });
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export type ServiceRoleActorRef<N extends string = string> = ActorMethods<N>;
|
|
197
|
+
/**
|
|
198
|
+
* Actors module available with the service role: selects a session whose methods to call.
|
|
199
|
+
*/
|
|
200
|
+
export type ServiceRoleActorsModule = {
|
|
201
|
+
[K in AllActorNames]: (instanceId: string) => ServiceRoleActorRef<string & K>;
|
|
202
|
+
} & Record<string, (instanceId: string) => ServiceRoleActorRef>;
|
|
160
203
|
/**
|
|
161
204
|
* Selects a session for a named actor.
|
|
162
205
|
*
|
|
@@ -193,6 +236,7 @@ export interface ActorClient<N extends string = string> {
|
|
|
193
236
|
* - Manage connections: {@link ActorRef.connect | Open} and {@link Connection.close | close} a WebSocket connection to a session.
|
|
194
237
|
* - Exchange messages: {@link Connection.send | Send} and {@link Connection.subscribe | listen} for messages to and from an actor.
|
|
195
238
|
* - Manage subscriptions: {@link Connection.subscribe | Subscribe} and {@link ActorSubscription.unsubscribe | unsubscribe} from actor messages without closing the connection.
|
|
239
|
+
* - Call methods: run one of an actor's callable methods on a session, without connecting.
|
|
196
240
|
*
|
|
197
241
|
* For a sample flow, see
|
|
198
242
|
* [connect a client to a session](/developers/backend/resources/actors/sample-flows#connect-a-client-to-a-session).
|
|
@@ -200,12 +244,14 @@ export interface ActorClient<N extends string = string> {
|
|
|
200
244
|
* ## Authentication modes
|
|
201
245
|
*
|
|
202
246
|
* This module is available to use with a client in anonymous or user authentication mode. Access it
|
|
203
|
-
* through `base44.actors`.
|
|
204
|
-
* [service role authentication mode](/developers/references/sdk/getting-started/client#service-role)
|
|
247
|
+
* through `base44.actors`. In
|
|
248
|
+
* [service role authentication mode](/developers/references/sdk/getting-started/client#service-role),
|
|
249
|
+
* `base44.asServiceRole.actors` only calls methods.
|
|
205
250
|
*
|
|
206
251
|
* The actor receives each client's [identity](/developers/backend/resources/actors/reference#connections)
|
|
207
|
-
* when the client connects. A client that hasn't logged in
|
|
208
|
-
* has [logged in](/developers/references/sdk/docs/interfaces/auth)
|
|
252
|
+
* when the client connects or calls a method. A client that hasn't logged in is anonymous, a client that
|
|
253
|
+
* has [logged in](/developers/references/sdk/docs/interfaces/auth) is authenticated, and server
|
|
254
|
+
* code calling with the service role is `service`.
|
|
209
255
|
*/
|
|
210
256
|
export type ActorsModule = {
|
|
211
257
|
[K in AllActorNames]: K extends keyof ActorRegistry ? ActorClient<string & K> : ActorClient;
|
package/package.json
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/sdk",
|
|
3
|
-
"version": "0.8.60-pr.
|
|
3
|
+
"version": "0.8.60-pr.377.428760d",
|
|
4
4
|
"description": "JavaScript SDK for Base44 API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
-
"imports": {
|
|
8
|
-
"#build-commit-source": {
|
|
9
|
-
"module": "./dist/utils/build-commit-source.js",
|
|
10
|
-
"default": "./dist/utils/build-commit-source.fallback.js"
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
7
|
"type": "module",
|
|
14
8
|
"files": [
|
|
15
9
|
"dist"
|
|
@@ -22,7 +16,6 @@
|
|
|
22
16
|
"test:e2e": "vitest run tests/e2e",
|
|
23
17
|
"test:watch": "vitest",
|
|
24
18
|
"test:coverage": "vitest run --coverage",
|
|
25
|
-
"test:build-commit": "scripts/verify-build-commit.sh",
|
|
26
19
|
"docs": "typedoc",
|
|
27
20
|
"prepublishOnly": "npm run build",
|
|
28
21
|
"create-docs": "npm run create-docs:generate && npm run create-docs:process",
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reads the commit the app's frontend bundle was built from.
|
|
3
|
-
*
|
|
4
|
-
* Base44's publish pipeline sets `VITE_BASE44_BUILD_COMMIT` for the build, and
|
|
5
|
-
* Vite replaces the expression below with that literal string when it bundles
|
|
6
|
-
* this file out of `node_modules`, in `vite build` and in the dev server's
|
|
7
|
-
* prebundled deps alike. The expression must stay spelled out exactly like
|
|
8
|
-
* this: Vite matches it statically, and any indirection leaves a runtime lookup
|
|
9
|
-
* that is always undefined.
|
|
10
|
-
*
|
|
11
|
-
* A bundler that isn't Vite leaves `import.meta.env` undefined, so the lookup
|
|
12
|
-
* throws, and that means "no build commit".
|
|
13
|
-
*
|
|
14
|
-
* Only reached as `#build-commit-source` under the `module` condition; see
|
|
15
|
-
* `build-commit-source.fallback.ts`.
|
|
16
|
-
*
|
|
17
|
-
* @internal
|
|
18
|
-
*/
|
|
19
|
-
export declare function readBuildCommitSource(): unknown;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What `#build-commit-source` resolves to without the `module` condition: no
|
|
3
|
-
* build commit.
|
|
4
|
-
*
|
|
5
|
-
* The package's `imports` field gives the `import.meta` reader
|
|
6
|
-
* (`build-commit-source.ts`) only to bundlers that match `module`: Vite,
|
|
7
|
-
* webpack and esbuild. Everything else gets this file, so `import.meta` never
|
|
8
|
-
* reaches a runtime or bundler that can't take it. That includes Node, Deno,
|
|
9
|
-
* and Metro on every platform: Hermes rejects `import.meta` outright, and
|
|
10
|
-
* Metro's web build ships a classic script, where it is a SyntaxError.
|
|
11
|
-
*
|
|
12
|
-
* @internal
|
|
13
|
-
*/
|
|
14
|
-
export declare function readBuildCommitSource(): unknown;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What `#build-commit-source` resolves to without the `module` condition: no
|
|
3
|
-
* build commit.
|
|
4
|
-
*
|
|
5
|
-
* The package's `imports` field gives the `import.meta` reader
|
|
6
|
-
* (`build-commit-source.ts`) only to bundlers that match `module`: Vite,
|
|
7
|
-
* webpack and esbuild. Everything else gets this file, so `import.meta` never
|
|
8
|
-
* reaches a runtime or bundler that can't take it. That includes Node, Deno,
|
|
9
|
-
* and Metro on every platform: Hermes rejects `import.meta` outright, and
|
|
10
|
-
* Metro's web build ships a classic script, where it is a SyntaxError.
|
|
11
|
-
*
|
|
12
|
-
* @internal
|
|
13
|
-
*/
|
|
14
|
-
export function readBuildCommitSource() {
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reads the commit the app's frontend bundle was built from.
|
|
3
|
-
*
|
|
4
|
-
* Base44's publish pipeline sets `VITE_BASE44_BUILD_COMMIT` for the build, and
|
|
5
|
-
* Vite replaces the expression below with that literal string when it bundles
|
|
6
|
-
* this file out of `node_modules`, in `vite build` and in the dev server's
|
|
7
|
-
* prebundled deps alike. The expression must stay spelled out exactly like
|
|
8
|
-
* this: Vite matches it statically, and any indirection leaves a runtime lookup
|
|
9
|
-
* that is always undefined.
|
|
10
|
-
*
|
|
11
|
-
* A bundler that isn't Vite leaves `import.meta.env` undefined, so the lookup
|
|
12
|
-
* throws, and that means "no build commit".
|
|
13
|
-
*
|
|
14
|
-
* Only reached as `#build-commit-source` under the `module` condition; see
|
|
15
|
-
* `build-commit-source.fallback.ts`.
|
|
16
|
-
*
|
|
17
|
-
* @internal
|
|
18
|
-
*/
|
|
19
|
-
export function readBuildCommitSource() {
|
|
20
|
-
try {
|
|
21
|
-
return import.meta.env.VITE_BASE44_BUILD_COMMIT;
|
|
22
|
-
}
|
|
23
|
-
catch (_a) {
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/** Header the backend reads to tell which frontend build sent a request. */
|
|
2
|
-
export declare const BUILD_COMMIT_HEADER = "X-Base44-Build";
|
|
3
|
-
/**
|
|
4
|
-
* The build-commit header to send from this client, if any.
|
|
5
|
-
*
|
|
6
|
-
* Only a browser sends it: the value names the browser bundle, so a server
|
|
7
|
-
* (Node, Deno, a backend function) or React Native would report a commit that
|
|
8
|
-
* isn't the one running.
|
|
9
|
-
*
|
|
10
|
-
* @internal
|
|
11
|
-
*/
|
|
12
|
-
export declare function getBuildCommitHeaders(): Record<string, string>;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { isNode, isReactNative } from "./common.js";
|
|
2
|
-
import { readBuildCommitSource } from "#build-commit-source";
|
|
3
|
-
/** Header the backend reads to tell which frontend build sent a request. */
|
|
4
|
-
export const BUILD_COMMIT_HEADER = "X-Base44-Build";
|
|
5
|
-
// The backend accepts exactly this shape and ignores anything else, so a value
|
|
6
|
-
// that doesn't match is never sent.
|
|
7
|
-
const BUILD_COMMIT_PATTERN = /^[a-f0-9]{40}$/;
|
|
8
|
-
/**
|
|
9
|
-
* The build-commit header to send from this client, if any.
|
|
10
|
-
*
|
|
11
|
-
* Only a browser sends it: the value names the browser bundle, so a server
|
|
12
|
-
* (Node, Deno, a backend function) or React Native would report a commit that
|
|
13
|
-
* isn't the one running.
|
|
14
|
-
*
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
export function getBuildCommitHeaders() {
|
|
18
|
-
if (isNode || isReactNative)
|
|
19
|
-
return {};
|
|
20
|
-
const commit = readBuildCommitSource();
|
|
21
|
-
return typeof commit === "string" && BUILD_COMMIT_PATTERN.test(commit)
|
|
22
|
-
? { [BUILD_COMMIT_HEADER]: commit }
|
|
23
|
-
: {};
|
|
24
|
-
}
|