@base44-preview/sdk 0.8.60-pr.377.f503122 → 0.8.62-pr.379.50a3fa2
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 +26 -16
- package/dist/actor.js +8 -0
- package/dist/client.js +27 -9
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/modules/actors.d.ts +5 -5
- package/dist/modules/actors.js +24 -7
- package/dist/modules/actors.types.d.ts +45 -50
- package/dist/utils/build-commit-source.d.ts +19 -0
- package/dist/utils/build-commit-source.fallback.d.ts +14 -0
- package/dist/utils/build-commit-source.fallback.js +16 -0
- package/dist/utils/build-commit-source.js +26 -0
- package/dist/utils/build-commit.d.ts +12 -0
- package/dist/utils/build-commit.js +24 -0
- package/package.json +8 -1
package/dist/actor.d.ts
CHANGED
|
@@ -17,13 +17,22 @@ export type ActorConnectionIdentity = Readonly<{
|
|
|
17
17
|
type: "anonymous";
|
|
18
18
|
anonymousId: string;
|
|
19
19
|
}>;
|
|
20
|
-
/** Who
|
|
21
|
-
*
|
|
22
|
-
export type
|
|
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
23
|
type: "service";
|
|
24
24
|
}>;
|
|
25
|
-
|
|
26
|
-
|
|
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);
|
|
27
36
|
}
|
|
28
37
|
/**
|
|
29
38
|
* A single client connection. `Send` is the message type this connection accepts
|
|
@@ -79,24 +88,25 @@ export declare abstract class Actor<Incoming = unknown, Outgoing = unknown> {
|
|
|
79
88
|
*/
|
|
80
89
|
protected handleWake(_key: string): void | Promise<void>;
|
|
81
90
|
/**
|
|
82
|
-
*
|
|
83
|
-
* client or
|
|
84
|
-
*
|
|
85
|
-
* acting: a client can post to any session it can name.
|
|
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.
|
|
86
94
|
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
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.
|
|
89
99
|
*
|
|
90
100
|
* @example
|
|
91
101
|
* ```ts
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* this.broadcast({ type: "
|
|
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 });
|
|
96
106
|
* }
|
|
97
107
|
* ```
|
|
98
108
|
*/
|
|
99
|
-
|
|
109
|
+
static callable?: readonly string[];
|
|
100
110
|
/**
|
|
101
111
|
* Arm a one-shot wake at `at` (epoch ms or a `Date`), identified by `key`.
|
|
102
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,6 +8,7 @@ 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";
|
|
11
12
|
import { createFunctionsModule } from "./modules/functions.js";
|
|
12
13
|
import { createAgentsModule } from "./modules/agents.js";
|
|
13
14
|
import { createAiGatewayModule } from "./modules/ai-gateway.js";
|
|
@@ -56,6 +57,9 @@ import { createActorsModule, createServiceRoleActorsModule, resolveActorsHost, }
|
|
|
56
57
|
* ```
|
|
57
58
|
*/
|
|
58
59
|
export function createClient(config) {
|
|
60
|
+
return buildClient(config, { sendBuildCommit: true });
|
|
61
|
+
}
|
|
62
|
+
function buildClient(config, { sendBuildCommit }) {
|
|
59
63
|
var _a, _b, _c, _d;
|
|
60
64
|
const { serverUrl = "https://base44.app", appId, analytics, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
|
|
61
65
|
// Normalize appBaseUrl to always be a string (empty if not provided or invalid)
|
|
@@ -76,7 +80,18 @@ export function createClient(config) {
|
|
|
76
80
|
}
|
|
77
81
|
return socket;
|
|
78
82
|
};
|
|
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
|
+
: {};
|
|
79
93
|
const headers = {
|
|
94
|
+
...(callerBuildCommit ? {} : buildCommitHeaders),
|
|
80
95
|
...optionalHeaders,
|
|
81
96
|
"X-App-Id": String(appId),
|
|
82
97
|
};
|
|
@@ -122,9 +137,10 @@ export function createClient(config) {
|
|
|
122
137
|
interceptResponses: false,
|
|
123
138
|
});
|
|
124
139
|
// The backend authenticates the caller from the axios client's token (or the
|
|
125
|
-
// anonymous id it adds in a browser) and relays the
|
|
126
|
-
const
|
|
127
|
-
|
|
140
|
+
// anonymous id it adds in a browser) and relays the call to the room.
|
|
141
|
+
const actorMethodCaller = (client) => async (actorName, room, method, args) => {
|
|
142
|
+
var _a;
|
|
143
|
+
const response = await client.post(`/apps/${appId}/actors/${encodeURIComponent(actorName)}/rooms/${encodeURIComponent(room)}/requests`, JSON.stringify({ method, args }), {
|
|
128
144
|
headers: {
|
|
129
145
|
"Content-Type": "application/json",
|
|
130
146
|
// Draft vs published copy, as for a connection.
|
|
@@ -133,7 +149,7 @@ export function createClient(config) {
|
|
|
133
149
|
: {}),
|
|
134
150
|
},
|
|
135
151
|
});
|
|
136
|
-
return
|
|
152
|
+
return (_a = response === null || response === void 0 ? void 0 : response.reply) !== null && _a !== void 0 ? _a : undefined;
|
|
137
153
|
};
|
|
138
154
|
// Dedicated client for actor connection-token mints: no onError (a legacy
|
|
139
155
|
// actor answers every mint with an expected 409 before the proxy fallback,
|
|
@@ -181,7 +197,7 @@ export function createClient(config) {
|
|
|
181
197
|
},
|
|
182
198
|
transport: options === null || options === void 0 ? void 0 : options.actorsTransport,
|
|
183
199
|
onMintError: options === null || options === void 0 ? void 0 : options.onError,
|
|
184
|
-
|
|
200
|
+
callMethod: actorMethodCaller(axiosClient),
|
|
185
201
|
});
|
|
186
202
|
const userModules = {
|
|
187
203
|
entities: createEntitiesModule({
|
|
@@ -194,7 +210,7 @@ export function createClient(config) {
|
|
|
194
210
|
auth: userAuthModule,
|
|
195
211
|
functions: createFunctionsModule(functionsAxiosClient, appId, {
|
|
196
212
|
getAuthHeaders: () => {
|
|
197
|
-
const headers = {};
|
|
213
|
+
const headers = { ...buildCommitHeaders };
|
|
198
214
|
// Get current token from storage or initial config
|
|
199
215
|
const currentToken = token || getAccessToken();
|
|
200
216
|
if (currentToken) {
|
|
@@ -274,7 +290,7 @@ export function createClient(config) {
|
|
|
274
290
|
}),
|
|
275
291
|
appLogs: createAppLogsModule(serviceRoleAxiosClient, appId),
|
|
276
292
|
payments: createServiceRolePaymentsModule(serviceRoleAxiosClient, appId),
|
|
277
|
-
actors: createServiceRoleActorsModule(
|
|
293
|
+
actors: createServiceRoleActorsModule(actorMethodCaller(serviceRoleAxiosClient)),
|
|
278
294
|
cleanup: () => {
|
|
279
295
|
if (socket) {
|
|
280
296
|
socket.disconnect();
|
|
@@ -472,12 +488,14 @@ export function createClientFromRequest(request) {
|
|
|
472
488
|
if (dataEnvHeader === "dev" || dataEnvHeader === "prod") {
|
|
473
489
|
additionalHeaders["X-Data-Env"] = dataEnvHeader;
|
|
474
490
|
}
|
|
475
|
-
|
|
491
|
+
// A backend function isn't the browser bundle, so it never reports a build
|
|
492
|
+
// commit, whatever its runtime looks like.
|
|
493
|
+
return buildClient({
|
|
476
494
|
serverUrl: serverUrlHeader || "https://base44.app",
|
|
477
495
|
appId,
|
|
478
496
|
token: userToken,
|
|
479
497
|
serviceToken: serviceRoleToken,
|
|
480
498
|
functionsVersion: functionsVersion !== null && functionsVersion !== void 0 ? functionsVersion : undefined,
|
|
481
499
|
headers: additionalHeaders,
|
|
482
|
-
});
|
|
500
|
+
}, { sendBuildCommit: false });
|
|
483
501
|
}
|
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, ServiceRoleActorRef, ServiceRoleActorsModule, } 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, type ActorConnectionIdentity, type
|
|
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,6 +1,6 @@
|
|
|
1
1
|
import type { ActorRef, ServiceRoleActorRef } from "./actors.types.js";
|
|
2
|
-
/**
|
|
3
|
-
export type
|
|
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>;
|
|
4
4
|
/** Credentials minted by the platform for one direct actor connection. */
|
|
5
5
|
export interface ActorConnectionCredentials {
|
|
6
6
|
/** Direct actor endpoint, already carrying `?_pk=<connectionId>`. */
|
|
@@ -32,7 +32,7 @@ interface ActorsConfig {
|
|
|
32
32
|
* direct→proxy fallback (which recovers by itself). Wired to the client's
|
|
33
33
|
* `options.onError`. */
|
|
34
34
|
onMintError?: (error: Error) => void;
|
|
35
|
-
|
|
35
|
+
callMethod: CallActorMethod;
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* The legacy platform-proxy URL, byte-for-byte what PartySocket built before
|
|
@@ -51,6 +51,6 @@ export declare function createActorsModule(config: ActorsConfig): {
|
|
|
51
51
|
module: Record<string, (instanceId: string) => ActorRef>;
|
|
52
52
|
closeAll: () => void;
|
|
53
53
|
};
|
|
54
|
-
/** `asServiceRole.actors`: server code can't hold a connection, so it only
|
|
55
|
-
export declare function createServiceRoleActorsModule(
|
|
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>;
|
|
56
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;
|
|
@@ -166,8 +185,8 @@ function makeActorRef(actorName, instanceId, config, connections) {
|
|
|
166
185
|
connections.add(c);
|
|
167
186
|
return c;
|
|
168
187
|
},
|
|
169
|
-
post: (body) => config.postRequest(actorName, instanceId, body),
|
|
170
188
|
};
|
|
189
|
+
return withMethods(ref, (method, args) => config.callMethod(actorName, instanceId, method, args));
|
|
171
190
|
}
|
|
172
191
|
/**
|
|
173
192
|
* The legacy platform-proxy URL, byte-for-byte what PartySocket built before
|
|
@@ -227,15 +246,13 @@ export function createActorsModule(config) {
|
|
|
227
246
|
},
|
|
228
247
|
};
|
|
229
248
|
}
|
|
230
|
-
/** `asServiceRole.actors`: server code can't hold a connection, so it only
|
|
231
|
-
export function createServiceRoleActorsModule(
|
|
249
|
+
/** `asServiceRole.actors`: server code can't hold a connection, so it only calls methods. */
|
|
250
|
+
export function createServiceRoleActorsModule(callMethod) {
|
|
232
251
|
return new Proxy({}, {
|
|
233
252
|
get(_, key) {
|
|
234
253
|
if (typeof key !== "string" || key === "then")
|
|
235
254
|
return undefined;
|
|
236
|
-
return (instanceId) => ({
|
|
237
|
-
post: (body) => postRequest(key, instanceId, body),
|
|
238
|
-
});
|
|
255
|
+
return (instanceId) => withMethods({}, (method, args) => callMethod(key, instanceId, method, args));
|
|
239
256
|
},
|
|
240
257
|
});
|
|
241
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,8 +44,20 @@ 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>>;
|
|
46
49
|
/**
|
|
47
|
-
*
|
|
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;
|
|
59
|
+
/**
|
|
60
|
+
* Configures the connection that {@linkcode ActorConnectable.connect | connect()} opens.
|
|
48
61
|
*/
|
|
49
62
|
export interface ActorConnectOptions {
|
|
50
63
|
/**
|
|
@@ -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
|
|
150
|
+
* Call {@linkcode ActorConnectable.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
|
*
|
|
@@ -156,55 +179,27 @@ export interface ActorRef<N extends string = string> {
|
|
|
156
179
|
* ```
|
|
157
180
|
*/
|
|
158
181
|
connect(options?: ActorConnectOptions): Connection<N>;
|
|
159
|
-
/**
|
|
160
|
-
* Sends a request to this session's `handleRequest` and returns its reply.
|
|
161
|
-
*
|
|
162
|
-
* The actor receives the request with the caller's identity: the logged-in
|
|
163
|
-
* user, or an anonymous visitor. Unlike {@linkcode ActorRef.connect | connect()},
|
|
164
|
-
* no connection is opened, so the request reaches the session even when no
|
|
165
|
-
* client is connected; the actor can still `broadcast()` from it.
|
|
166
|
-
*
|
|
167
|
-
* @param body - JSON-serializable request body, up to 64 KB.
|
|
168
|
-
* @returns The JSON body of the actor's response, or `undefined` when it has none.
|
|
169
|
-
* @throws {Base44Error} When the actor refuses the request or doesn't implement
|
|
170
|
-
* `handleRequest` (404), with the actor's status code.
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* ```typescript
|
|
174
|
-
* // Send a request to a session
|
|
175
|
-
* const reply = await base44.actors.chatRoom("session-1").post({ type: "ping" });
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
post<T = unknown>(body?: unknown): Promise<T | undefined>;
|
|
179
182
|
}
|
|
183
|
+
export type ActorRef<N extends string = string> = ActorConnectable<N> & ActorMethods<N>;
|
|
180
184
|
/**
|
|
181
185
|
* Represents a reference to an actor session for server code running with the
|
|
182
186
|
* [service role](/developers/references/sdk/getting-started/client#service-role).
|
|
183
|
-
* Server code can't hold a connection, so it only
|
|
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
|
+
* ```
|
|
184
195
|
*/
|
|
185
|
-
export
|
|
186
|
-
/**
|
|
187
|
-
* Sends a request to this session's `handleRequest` as the app's server code,
|
|
188
|
-
* which the actor sees as the `service` identity, and returns its reply.
|
|
189
|
-
*
|
|
190
|
-
* @param body - JSON-serializable request body, up to 64 KB.
|
|
191
|
-
* @returns The JSON body of the actor's response, or `undefined` when it has none.
|
|
192
|
-
* @throws {Base44Error} When the actor refuses the request or doesn't implement
|
|
193
|
-
* `handleRequest` (404), with the actor's status code.
|
|
194
|
-
*
|
|
195
|
-
* @example
|
|
196
|
-
* ```typescript
|
|
197
|
-
* // Tell a user's session that their video is ready
|
|
198
|
-
* await base44.asServiceRole.actors.VideoRoom(userId).post({ type: "video_ready", id });
|
|
199
|
-
* ```
|
|
200
|
-
*/
|
|
201
|
-
post<T = unknown>(body?: unknown): Promise<T | undefined>;
|
|
202
|
-
}
|
|
196
|
+
export type ServiceRoleActorRef<N extends string = string> = ActorMethods<N>;
|
|
203
197
|
/**
|
|
204
|
-
* Actors module available with the service role: selects a session to
|
|
205
|
-
* requests to with {@linkcode ServiceRoleActorRef.post | post()}.
|
|
198
|
+
* Actors module available with the service role: selects a session whose methods to call.
|
|
206
199
|
*/
|
|
207
|
-
export type ServiceRoleActorsModule =
|
|
200
|
+
export type ServiceRoleActorsModule = {
|
|
201
|
+
[K in AllActorNames]: (instanceId: string) => ServiceRoleActorRef<string & K>;
|
|
202
|
+
} & Record<string, (instanceId: string) => ServiceRoleActorRef>;
|
|
208
203
|
/**
|
|
209
204
|
* Selects a session for a named actor.
|
|
210
205
|
*
|
|
@@ -238,10 +233,10 @@ export interface ActorClient<N extends string = string> {
|
|
|
238
233
|
*
|
|
239
234
|
* The actors module provides the functionality to:
|
|
240
235
|
*
|
|
241
|
-
* - Manage connections: {@link
|
|
236
|
+
* - Manage connections: {@link ActorConnectable.connect | Open} and {@link Connection.close | close} a WebSocket connection to a session.
|
|
242
237
|
* - Exchange messages: {@link Connection.send | Send} and {@link Connection.subscribe | listen} for messages to and from an actor.
|
|
243
238
|
* - Manage subscriptions: {@link Connection.subscribe | Subscribe} and {@link ActorSubscription.unsubscribe | unsubscribe} from actor messages without closing the connection.
|
|
244
|
-
* -
|
|
239
|
+
* - Call methods: run one of an actor's callable methods on a session, without connecting.
|
|
245
240
|
*
|
|
246
241
|
* For a sample flow, see
|
|
247
242
|
* [connect a client to a session](/developers/backend/resources/actors/sample-flows#connect-a-client-to-a-session).
|
|
@@ -251,12 +246,12 @@ export interface ActorClient<N extends string = string> {
|
|
|
251
246
|
* This module is available to use with a client in anonymous or user authentication mode. Access it
|
|
252
247
|
* through `base44.actors`. In
|
|
253
248
|
* [service role authentication mode](/developers/references/sdk/getting-started/client#service-role),
|
|
254
|
-
* `base44.asServiceRole.actors` only
|
|
249
|
+
* `base44.asServiceRole.actors` only calls methods.
|
|
255
250
|
*
|
|
256
251
|
* The actor receives each client's [identity](/developers/backend/resources/actors/reference#connections)
|
|
257
|
-
* when the client connects or
|
|
252
|
+
* when the client connects or calls a method. A client that hasn't logged in is anonymous, a client that
|
|
258
253
|
* has [logged in](/developers/references/sdk/docs/interfaces/auth) is authenticated, and server
|
|
259
|
-
* code
|
|
254
|
+
* code calling with the service role is `service`.
|
|
260
255
|
*/
|
|
261
256
|
export type ActorsModule = {
|
|
262
257
|
[K in AllActorNames]: K extends keyof ActorRegistry ? ActorClient<string & K> : ActorClient;
|
|
@@ -0,0 +1,19 @@
|
|
|
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;
|
|
@@ -0,0 +1,14 @@
|
|
|
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;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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>;
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/sdk",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.62-pr.379.50a3fa2",
|
|
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
|
+
},
|
|
7
13
|
"type": "module",
|
|
8
14
|
"files": [
|
|
9
15
|
"dist"
|
|
@@ -16,6 +22,7 @@
|
|
|
16
22
|
"test:e2e": "vitest run tests/e2e",
|
|
17
23
|
"test:watch": "vitest",
|
|
18
24
|
"test:coverage": "vitest run --coverage",
|
|
25
|
+
"test:build-commit": "scripts/verify-build-commit.sh",
|
|
19
26
|
"docs": "typedoc",
|
|
20
27
|
"prepublishOnly": "npm run build",
|
|
21
28
|
"create-docs": "npm run create-docs:generate && npm run create-docs:process",
|