@simpleplatform/sdk 2.5.0 → 3.0.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 +382 -44
- package/dist/ai.d.ts +10 -0
- package/dist/ai.js +5 -1
- package/dist/space/actions.d.ts +59 -0
- package/dist/space/actions.js +69 -0
- package/dist/space/browser-transport.d.ts +18 -0
- package/dist/space/browser-transport.js +229 -0
- package/dist/space/core.d.ts +40 -241
- package/dist/space/core.js +69 -499
- package/dist/space/documents.d.ts +25 -0
- package/dist/space/documents.js +72 -0
- package/dist/space/index.d.ts +6 -32
- package/dist/space/index.js +59 -99
- package/dist/space/navigation.d.ts +25 -0
- package/dist/space/navigation.js +32 -0
- package/dist/space/protocol.d.ts +66 -0
- package/dist/space/protocol.js +77 -0
- package/dist/space/record-form.d.ts +94 -0
- package/dist/space/record-form.js +217 -0
- package/dist/space/record.d.ts +137 -0
- package/dist/space/record.js +231 -0
- package/dist/space/status.d.ts +2 -0
- package/dist/space/status.js +7 -0
- package/dist/space/tabs.d.ts +64 -0
- package/dist/space/tabs.js +236 -0
- package/dist/space/tasks.d.ts +84 -0
- package/dist/space/tasks.js +129 -0
- package/dist/space/ui.d.ts +67 -0
- package/dist/space/ui.js +123 -0
- package/dist/tasks.d.ts +18 -0
- package/dist/tasks.js +34 -0
- package/package.json +5 -1
package/dist/space/core.js
CHANGED
|
@@ -1,41 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
1
|
+
import { createActionsClient } from './actions.js';
|
|
2
|
+
import { createDocumentsClient } from './documents.js';
|
|
3
|
+
import { createNavigationClient } from './navigation.js';
|
|
4
|
+
import { deepFreeze, invalidResponse, isObjectRecord, PROTOCOL_VERSION, readResponse, SpaceDataError, SpaceProtocolError } from './protocol.js';
|
|
5
|
+
import { createRecordsClient, isCurrentRecordResult, ProtocolRecordHandle } from './record.js';
|
|
6
|
+
import { createTasksClient } from './tasks.js';
|
|
7
|
+
import { createHeaderClient, createTabsClient, createToastClient, validateSpaceToastOptions } from './ui.js';
|
|
8
|
+
export { PROTOCOL_VERSION, SpaceDataError, SpaceProtocolError } from './protocol.js';
|
|
9
|
+
export { MANAGED_RECORD_FORM_PROTOCOL_VERSION } from './record-form.js';
|
|
10
|
+
export { RECORDS_PROTOCOL_VERSION } from './record.js';
|
|
11
|
+
export { DEFAULT_TABS_TIMEOUT_MS, HEADER_ACTIONS_PROTOCOL_VERSION, HEADER_STATUS_PROTOCOL_VERSION, LUCIDE_ICON_NAME, TAB_ID, TABS_PROTOCOL_VERSION, TOAST_PROTOCOL_VERSION, validateHeaderStatus, validateTabs } from './ui.js';
|
|
12
|
+
/** Composes Space capabilities around the host transports supplied by `connect`. */
|
|
13
|
+
export function createSimpleClient({ actionTransport, capabilities, context = { kind: 'standalone' }, dataTransport, documentTransport, formModelTransport, headerStatusTransport, headerTransport, hostOrigin, navigationTransport, nextRequestId = createRequestId, recordsTransport, runtime, tabsTransport, taskTransport, toastTransport, transport, }) {
|
|
14
|
+
const immutableContext = deepFreeze(structuredClone(context));
|
|
15
|
+
const recordFormCapabilities = capabilities && toastTransport
|
|
16
|
+
? {
|
|
17
|
+
...capabilities,
|
|
18
|
+
showToast: (options) => toastTransport.showToast(validateSpaceToastOptions(options)),
|
|
19
|
+
}
|
|
20
|
+
: capabilities;
|
|
21
|
+
const record = async () => {
|
|
22
|
+
if (immutableContext.kind !== 'record') {
|
|
23
|
+
throw new SpaceProtocolError({
|
|
24
|
+
code: 'unavailable',
|
|
25
|
+
message: 'The current record is available only when this Space is configured as a record view.',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (!transport) {
|
|
29
|
+
throw new SpaceProtocolError({
|
|
30
|
+
code: 'unavailable',
|
|
31
|
+
message: 'The record protocol is unavailable for this record Space.',
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const request = {
|
|
35
|
+
operation: 'record.current',
|
|
36
|
+
payload: {},
|
|
37
|
+
protocol: PROTOCOL_VERSION,
|
|
38
|
+
requestId: nextRequestId(),
|
|
39
|
+
};
|
|
40
|
+
const response = await transport.request(request);
|
|
41
|
+
const result = readResponse(response, request);
|
|
42
|
+
if (!isCurrentRecordResult(result))
|
|
43
|
+
throw invalidResponse('The primary-record response is malformed.');
|
|
44
|
+
return new ProtocolRecordHandle(result, nextRequestId, transport, runtime, (document, variables) => executeData(dataTransport, document, variables), recordFormCapabilities, formModelTransport, immutableContext.applicationId);
|
|
45
|
+
};
|
|
46
|
+
const records = createRecordsClient({
|
|
47
|
+
capabilities: recordFormCapabilities,
|
|
48
|
+
currentRecord: record,
|
|
49
|
+
graphql: (document, variables) => executeData(dataTransport, document, variables),
|
|
50
|
+
nextRequestId,
|
|
51
|
+
recordsTransport,
|
|
52
|
+
runtime,
|
|
53
|
+
});
|
|
39
54
|
return {
|
|
40
55
|
actions: createActionsClient(actionTransport, nextRequestId),
|
|
41
56
|
context: immutableContext,
|
|
@@ -44,52 +59,28 @@ export function createSimpleClient({ actionTransport, context = { kind: 'standal
|
|
|
44
59
|
query: (document, variables) => executeData(dataTransport, document, variables),
|
|
45
60
|
},
|
|
46
61
|
documents: createDocumentsClient(documentTransport, nextRequestId),
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (immutableContext.kind !== 'record') {
|
|
50
|
-
throw new SpaceProtocolError({
|
|
51
|
-
code: 'unavailable',
|
|
52
|
-
message: 'The current record is available only when this Space is configured as a record view.',
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
if (!transport) {
|
|
56
|
-
throw new SpaceProtocolError({
|
|
57
|
-
code: 'unavailable',
|
|
58
|
-
message: 'The record protocol is unavailable for this record Space.',
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
const request = {
|
|
62
|
-
operation: 'record.current',
|
|
63
|
-
payload: {},
|
|
64
|
-
protocol: PROTOCOL_VERSION,
|
|
65
|
-
requestId: nextRequestId(),
|
|
66
|
-
};
|
|
67
|
-
const response = await transport.request(request);
|
|
68
|
-
const result = readResponse(response, request);
|
|
69
|
-
if (!isCurrentRecordResult(result)) {
|
|
70
|
-
throw invalidResponse('The primary-record response is malformed.');
|
|
71
|
-
}
|
|
72
|
-
return new ProtocolRecordHandle(result, nextRequestId, transport);
|
|
73
|
-
},
|
|
74
|
-
},
|
|
62
|
+
navigation: createNavigationClient(hostOrigin, navigationTransport),
|
|
63
|
+
records,
|
|
75
64
|
tasks: createTasksClient(taskTransport, nextRequestId),
|
|
65
|
+
ui: {
|
|
66
|
+
header: createHeaderClient(immutableContext.kind === 'record', headerTransport, headerStatusTransport),
|
|
67
|
+
tabs: createTabsClient(immutableContext.kind === 'record', tabsTransport, nextRequestId),
|
|
68
|
+
toast: createToastClient(toastTransport),
|
|
69
|
+
},
|
|
76
70
|
};
|
|
77
71
|
}
|
|
78
72
|
export function isSpaceContext(value) {
|
|
79
|
-
if (!value
|
|
73
|
+
if (!isObjectRecord(value))
|
|
80
74
|
return false;
|
|
81
|
-
|
|
82
|
-
const context = value;
|
|
83
|
-
if (context.kind === 'standalone') {
|
|
75
|
+
if (value.kind === 'standalone')
|
|
84
76
|
return true;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
&&
|
|
88
|
-
&&
|
|
89
|
-
&&
|
|
90
|
-
&&
|
|
91
|
-
&&
|
|
92
|
-
&& context.recordId.length > 0;
|
|
77
|
+
return value.kind === 'record'
|
|
78
|
+
&& typeof value.applicationId === 'string'
|
|
79
|
+
&& value.applicationId.length > 0
|
|
80
|
+
&& typeof value.tableName === 'string'
|
|
81
|
+
&& value.tableName.length > 0
|
|
82
|
+
&& typeof value.recordId === 'string'
|
|
83
|
+
&& value.recordId.length > 0;
|
|
93
84
|
}
|
|
94
85
|
function executeData(dataTransport, document, variables) {
|
|
95
86
|
if (!dataTransport) {
|
|
@@ -100,429 +91,8 @@ function executeData(dataTransport, document, variables) {
|
|
|
100
91
|
}
|
|
101
92
|
return dataTransport.execute(document, variables);
|
|
102
93
|
}
|
|
103
|
-
/**
|
|
104
|
-
* The host binds the Space's own app to every action it runs, so a Space names
|
|
105
|
-
* only the action and cannot reach another app's. Actions are not tied to a
|
|
106
|
-
* page record, so they are available in any Space whose host negotiated the
|
|
107
|
-
* action protocol, standalone or record.
|
|
108
|
-
*/
|
|
109
|
-
function createActionsClient(transport, nextRequestId) {
|
|
110
|
-
return {
|
|
111
|
-
async run(action, input, options) {
|
|
112
|
-
if (!transport) {
|
|
113
|
-
throw new SpaceProtocolError({
|
|
114
|
-
code: 'unavailable',
|
|
115
|
-
message: 'Actions are unavailable because the Space host did not negotiate the action protocol.',
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
const payload = readActionRunPayload(action, input, options);
|
|
119
|
-
const request = {
|
|
120
|
-
operation: 'action.run',
|
|
121
|
-
payload,
|
|
122
|
-
protocol: PROTOCOL_VERSION,
|
|
123
|
-
requestId: nextRequestId(),
|
|
124
|
-
};
|
|
125
|
-
const response = await requestWithin(transport, request, actionWaitMs(payload.timeoutMs));
|
|
126
|
-
// Any JSON value is a valid result, so it is returned as the host sent it.
|
|
127
|
-
return readResponse(response, request);
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* A staged document is stored without being attached to a record, so staging
|
|
133
|
-
* is available in any Space whose host negotiated the document protocol.
|
|
134
|
-
*/
|
|
135
|
-
function createDocumentsClient(transport, nextRequestId) {
|
|
136
|
-
return {
|
|
137
|
-
async stage(document) {
|
|
138
|
-
if (!transport) {
|
|
139
|
-
throw new SpaceProtocolError({
|
|
140
|
-
code: 'unavailable',
|
|
141
|
-
message: 'Documents are unavailable because the Space host did not negotiate the document protocol.',
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
const { file, mimeType, name } = readDocumentStageInput(document);
|
|
145
|
-
// A Blob cannot itself be transferred. Its bytes are read into a buffer
|
|
146
|
-
// once, and that buffer is handed to the host rather than copied again.
|
|
147
|
-
const bytes = await file.arrayBuffer();
|
|
148
|
-
const request = {
|
|
149
|
-
operation: 'document.stage',
|
|
150
|
-
payload: { bytes, mimeType, name },
|
|
151
|
-
protocol: PROTOCOL_VERSION,
|
|
152
|
-
requestId: nextRequestId(),
|
|
153
|
-
};
|
|
154
|
-
const response = await transport.request(request, [bytes]);
|
|
155
|
-
const result = readResponse(response, request);
|
|
156
|
-
if (!isDocumentStageResult(result))
|
|
157
|
-
throw invalidResponse('The document-stage response is malformed.');
|
|
158
|
-
return { handle: { ...result.handle } };
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Tasks are not tied to a page record, so they are available in any Space
|
|
164
|
-
* whose host negotiated the task protocol, standalone or record.
|
|
165
|
-
*/
|
|
166
|
-
function createTasksClient(transport, nextRequestId) {
|
|
167
|
-
const requireTransport = () => {
|
|
168
|
-
if (!transport) {
|
|
169
|
-
throw new SpaceProtocolError({
|
|
170
|
-
code: 'unavailable',
|
|
171
|
-
message: 'Tasks are unavailable because the Space host did not negotiate the task protocol.',
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
return transport;
|
|
175
|
-
};
|
|
176
|
-
return {
|
|
177
|
-
async create(task) {
|
|
178
|
-
const taskTransport = requireTransport();
|
|
179
|
-
const request = {
|
|
180
|
-
operation: 'task.create',
|
|
181
|
-
payload: readTaskCreatePayload(task),
|
|
182
|
-
protocol: PROTOCOL_VERSION,
|
|
183
|
-
requestId: nextRequestId(),
|
|
184
|
-
};
|
|
185
|
-
const response = await taskTransport.request(request);
|
|
186
|
-
const result = readResponse(response, request);
|
|
187
|
-
if (!isTaskCreateResult(result))
|
|
188
|
-
throw invalidResponse('The task-create response is malformed.');
|
|
189
|
-
const { id, revision, status } = result.task;
|
|
190
|
-
return { task: { id, revision, status } };
|
|
191
|
-
},
|
|
192
|
-
async reply(reply) {
|
|
193
|
-
const taskTransport = requireTransport();
|
|
194
|
-
const request = {
|
|
195
|
-
operation: 'task.reply',
|
|
196
|
-
payload: readTaskReplyPayload(reply),
|
|
197
|
-
protocol: PROTOCOL_VERSION,
|
|
198
|
-
requestId: nextRequestId(),
|
|
199
|
-
};
|
|
200
|
-
const response = await taskTransport.request(request);
|
|
201
|
-
const result = readResponse(response, request);
|
|
202
|
-
if (!isTaskReplyResult(result))
|
|
203
|
-
throw invalidResponse('The task-reply response is malformed.');
|
|
204
|
-
return { messageId: result.messageId, taskRevision: result.taskRevision };
|
|
205
|
-
},
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
class ProtocolRecordHandle {
|
|
209
|
-
constructor({ sessionId, snapshot }, nextRequestId, transport) {
|
|
210
|
-
_ProtocolRecordHandle_nextRequestId.set(this, void 0);
|
|
211
|
-
_ProtocolRecordHandle_transport.set(this, void 0);
|
|
212
|
-
_ProtocolRecordHandle_snapshot.set(this, void 0);
|
|
213
|
-
this.id = sessionId;
|
|
214
|
-
__classPrivateFieldSet(this, _ProtocolRecordHandle_nextRequestId, nextRequestId, "f");
|
|
215
|
-
__classPrivateFieldSet(this, _ProtocolRecordHandle_snapshot, immutableSnapshot(snapshot), "f");
|
|
216
|
-
__classPrivateFieldSet(this, _ProtocolRecordHandle_transport, transport, "f");
|
|
217
|
-
}
|
|
218
|
-
snapshot() {
|
|
219
|
-
return __classPrivateFieldGet(this, _ProtocolRecordHandle_snapshot, "f");
|
|
220
|
-
}
|
|
221
|
-
async update(values) {
|
|
222
|
-
const request = {
|
|
223
|
-
operation: 'record.update',
|
|
224
|
-
payload: {
|
|
225
|
-
sessionId: this.id,
|
|
226
|
-
values,
|
|
227
|
-
},
|
|
228
|
-
protocol: PROTOCOL_VERSION,
|
|
229
|
-
requestId: __classPrivateFieldGet(this, _ProtocolRecordHandle_nextRequestId, "f").call(this),
|
|
230
|
-
};
|
|
231
|
-
const response = await __classPrivateFieldGet(this, _ProtocolRecordHandle_transport, "f").request(request);
|
|
232
|
-
const result = readResponse(response, request);
|
|
233
|
-
if (!isRecordUpdateResult(result))
|
|
234
|
-
throw invalidResponse('The record-update response is malformed.');
|
|
235
|
-
const snapshot = immutableSnapshot(result.snapshot);
|
|
236
|
-
__classPrivateFieldSet(this, _ProtocolRecordHandle_snapshot, snapshot, "f");
|
|
237
|
-
return { ok: result.ok, snapshot };
|
|
238
|
-
}
|
|
239
|
-
async submit() {
|
|
240
|
-
const request = {
|
|
241
|
-
operation: 'record.submit',
|
|
242
|
-
payload: { sessionId: this.id },
|
|
243
|
-
protocol: PROTOCOL_VERSION,
|
|
244
|
-
requestId: __classPrivateFieldGet(this, _ProtocolRecordHandle_nextRequestId, "f").call(this),
|
|
245
|
-
};
|
|
246
|
-
const response = await __classPrivateFieldGet(this, _ProtocolRecordHandle_transport, "f").request(request);
|
|
247
|
-
const result = readResponse(response, request);
|
|
248
|
-
if (!isRecordSubmitResult(result))
|
|
249
|
-
throw invalidResponse('The record-submit response is malformed.');
|
|
250
|
-
const snapshot = immutableSnapshot(result.snapshot);
|
|
251
|
-
__classPrivateFieldSet(this, _ProtocolRecordHandle_snapshot, snapshot, "f");
|
|
252
|
-
return { ok: result.ok, snapshot };
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
_ProtocolRecordHandle_nextRequestId = new WeakMap(), _ProtocolRecordHandle_transport = new WeakMap(), _ProtocolRecordHandle_snapshot = new WeakMap();
|
|
256
94
|
function createRequestId() {
|
|
257
95
|
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function')
|
|
258
96
|
return crypto.randomUUID();
|
|
259
97
|
return `space-request-${Math.random().toString(36).slice(2)}`;
|
|
260
98
|
}
|
|
261
|
-
function immutableSnapshot(snapshot) {
|
|
262
|
-
if (!isRecordSnapshot(snapshot))
|
|
263
|
-
throw invalidResponse('The record snapshot is malformed.');
|
|
264
|
-
return deepFreeze(structuredClone(snapshot));
|
|
265
|
-
}
|
|
266
|
-
function immutableSpaceContext(context) {
|
|
267
|
-
return deepFreeze(structuredClone(context));
|
|
268
|
-
}
|
|
269
|
-
function deepFreeze(value) {
|
|
270
|
-
if (!value || typeof value !== 'object' || Object.isFrozen(value))
|
|
271
|
-
return value;
|
|
272
|
-
for (const child of Object.values(value))
|
|
273
|
-
deepFreeze(child);
|
|
274
|
-
return Object.freeze(value);
|
|
275
|
-
}
|
|
276
|
-
function invalidResponse(message) {
|
|
277
|
-
return new SpaceProtocolError({ code: 'invalid_response', message });
|
|
278
|
-
}
|
|
279
|
-
function invalidRequest(message) {
|
|
280
|
-
return new SpaceProtocolError({ code: 'invalid_request', message });
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Checks a task before it is sent, so a plain-JavaScript caller learns what is
|
|
284
|
-
* wrong without a host round trip. The payload names only the members the
|
|
285
|
-
* operation defines; an optional member left out is not sent at all.
|
|
286
|
-
*/
|
|
287
|
-
function readTaskCreatePayload(task) {
|
|
288
|
-
if (!isObjectRecord(task))
|
|
289
|
-
throw invalidRequest('A task needs a title, a task type, and its input.');
|
|
290
|
-
if (!isNonBlankString(task.title))
|
|
291
|
-
throw invalidRequest('A task needs a title.');
|
|
292
|
-
if (!isNonBlankString(task.taskTypeId))
|
|
293
|
-
throw invalidRequest('A task needs the ID of its task type.');
|
|
294
|
-
if (!isJsonObject(task.input))
|
|
295
|
-
throw invalidRequest('A task input must be a JSON object, and everything in it a JSON value.');
|
|
296
|
-
if (task.assignedToId !== undefined && !isNonBlankString(task.assignedToId))
|
|
297
|
-
throw invalidRequest('A task assignee must be a user ID.');
|
|
298
|
-
return {
|
|
299
|
-
...(task.assignedToId === undefined ? {} : { assignedToId: task.assignedToId }),
|
|
300
|
-
input: task.input,
|
|
301
|
-
taskTypeId: task.taskTypeId,
|
|
302
|
-
title: task.title,
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
function readTaskReplyPayload(reply) {
|
|
306
|
-
if (!isObjectRecord(reply))
|
|
307
|
-
throw invalidRequest('A task reply needs a task ID and its content.');
|
|
308
|
-
if (!isNonBlankString(reply.taskId))
|
|
309
|
-
throw invalidRequest('A task reply needs the ID of its task.');
|
|
310
|
-
if (!isNonBlankString(reply.content))
|
|
311
|
-
throw invalidRequest('A task reply needs content.');
|
|
312
|
-
if (reply.inReplyToMessageId !== undefined && !isNonBlankString(reply.inReplyToMessageId))
|
|
313
|
-
throw invalidRequest('A task reply can answer only a message ID.');
|
|
314
|
-
return {
|
|
315
|
-
content: reply.content,
|
|
316
|
-
...(reply.inReplyToMessageId === undefined ? {} : { inReplyToMessageId: reply.inReplyToMessageId }),
|
|
317
|
-
taskId: reply.taskId,
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
/** An action's name within its app, as the host checks it before adding the app. */
|
|
321
|
-
const ACTION_NAME = /^[a-z0-9][a-z0-9-]*$/;
|
|
322
|
-
/** The host's timeout for an action run that names none. */
|
|
323
|
-
const DEFAULT_ACTION_TIMEOUT_MS = 60000;
|
|
324
|
-
/** How much longer than the host's timeout the client waits for its answer. */
|
|
325
|
-
const ACTION_ANSWER_GRACE_MS = 5000;
|
|
326
|
-
/** The longest delay `setTimeout` keeps; it runs a longer one at once. */
|
|
327
|
-
const MAX_TIMER_MS = 2147483647;
|
|
328
|
-
/**
|
|
329
|
-
* How long a run waits for the host. The host answers `timeout` itself at the
|
|
330
|
-
* timeout, so this wait only ends a run whose answer never comes, as when the
|
|
331
|
-
* host has closed its port. A timeout the host refuses is answered at once, so
|
|
332
|
-
* it only needs to give a delay a timer can hold.
|
|
333
|
-
*/
|
|
334
|
-
function actionWaitMs(timeoutMs) {
|
|
335
|
-
const hostTimeoutMs = Math.max(timeoutMs ?? DEFAULT_ACTION_TIMEOUT_MS, 0);
|
|
336
|
-
return Math.min(hostTimeoutMs + ACTION_ANSWER_GRACE_MS, MAX_TIMER_MS);
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Sends a request and waits at most `waitMs` for its answer. After that it
|
|
340
|
-
* rejects with `timeout` and aborts the transport's wait, so the request is
|
|
341
|
-
* not kept pending on a port the host may no longer read.
|
|
342
|
-
*/
|
|
343
|
-
async function requestWithin(transport, request, waitMs) {
|
|
344
|
-
const controller = new AbortController();
|
|
345
|
-
let timer;
|
|
346
|
-
const expired = new Promise((_resolve, reject) => {
|
|
347
|
-
timer = setTimeout(() => {
|
|
348
|
-
const error = new SpaceProtocolError({
|
|
349
|
-
code: 'timeout',
|
|
350
|
-
message: `The Space host did not answer ${request.operation} within ${waitMs} milliseconds, so its outcome is unknown.`,
|
|
351
|
-
});
|
|
352
|
-
controller.abort(error);
|
|
353
|
-
reject(error);
|
|
354
|
-
}, waitMs);
|
|
355
|
-
});
|
|
356
|
-
try {
|
|
357
|
-
return await Promise.race([transport.request(request, undefined, controller.signal), expired]);
|
|
358
|
-
}
|
|
359
|
-
finally {
|
|
360
|
-
clearTimeout(timer);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Checks an action run before it is sent. The timeout's range is the host's to
|
|
365
|
-
* enforce, so only its type is checked here, and it is sent only when given.
|
|
366
|
-
*/
|
|
367
|
-
function readActionRunPayload(action, input, options) {
|
|
368
|
-
if (typeof action !== 'string' || !ACTION_NAME.test(action))
|
|
369
|
-
throw invalidRequest('An action is named alone, in lowercase letters, digits, and hyphens, such as "document-attach".');
|
|
370
|
-
if (!isJsonValue(input))
|
|
371
|
-
throw invalidRequest('An action input must be a JSON value, and everything in it a JSON value.');
|
|
372
|
-
if (options !== undefined && !isObjectRecord(options))
|
|
373
|
-
throw invalidRequest('Action options must be an object.');
|
|
374
|
-
const timeoutMs = options?.timeoutMs;
|
|
375
|
-
if (timeoutMs !== undefined && (typeof timeoutMs !== 'number' || !Number.isFinite(timeoutMs)))
|
|
376
|
-
throw invalidRequest('An action timeout must be a number of milliseconds.');
|
|
377
|
-
return {
|
|
378
|
-
action,
|
|
379
|
-
input,
|
|
380
|
-
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* The task.create contract takes an object for input, never null, an array, or
|
|
385
|
-
* a single value, so a caller learns that here instead of from the host.
|
|
386
|
-
*/
|
|
387
|
-
function isJsonObject(value) {
|
|
388
|
-
return isObjectRecord(value) && isJsonValue(value);
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Accepts only what JSON carries unchanged. A structured clone would pass a
|
|
392
|
-
* Date, Map, or class instance through a MessagePort and a JSON transport
|
|
393
|
-
* would not, so task and action input are held to JSON on every transport.
|
|
394
|
-
*/
|
|
395
|
-
function isJsonValue(value, ancestors = new Set()) {
|
|
396
|
-
if (value === null || typeof value === 'boolean' || typeof value === 'string')
|
|
397
|
-
return true;
|
|
398
|
-
if (typeof value === 'number')
|
|
399
|
-
return Number.isFinite(value);
|
|
400
|
-
if (!value || typeof value !== 'object' || ancestors.has(value))
|
|
401
|
-
return false;
|
|
402
|
-
const prototype = Object.getPrototypeOf(value);
|
|
403
|
-
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null)
|
|
404
|
-
return false;
|
|
405
|
-
ancestors.add(value);
|
|
406
|
-
const valid = Object.values(value).every(child => isJsonValue(child, ancestors));
|
|
407
|
-
ancestors.delete(value);
|
|
408
|
-
return valid;
|
|
409
|
-
}
|
|
410
|
-
function readDocumentStageInput(document) {
|
|
411
|
-
if (!isObjectRecord(document) || !isBlob(document.file))
|
|
412
|
-
throw invalidRequest('A staged document needs a File or Blob.');
|
|
413
|
-
const name = document.name ?? readFileName(document.file);
|
|
414
|
-
if (!isNonBlankString(name))
|
|
415
|
-
throw invalidRequest('A staged document needs a name, and a Blob has none of its own.');
|
|
416
|
-
if (document.mimeType !== undefined && typeof document.mimeType !== 'string')
|
|
417
|
-
throw invalidRequest('A staged document type must be a MIME type.');
|
|
418
|
-
return {
|
|
419
|
-
file: document.file,
|
|
420
|
-
mimeType: document.mimeType || document.file.type || 'application/octet-stream',
|
|
421
|
-
name,
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
/** Duck-typed so a Blob from another realm, or a test double, is accepted. */
|
|
425
|
-
function isBlob(value) {
|
|
426
|
-
if (!isObjectRecord(value))
|
|
427
|
-
return false;
|
|
428
|
-
return typeof value.arrayBuffer === 'function' && typeof value.size === 'number' && typeof value.type === 'string';
|
|
429
|
-
}
|
|
430
|
-
function readFileName(file) {
|
|
431
|
-
const { name } = file;
|
|
432
|
-
return typeof name === 'string' ? name : undefined;
|
|
433
|
-
}
|
|
434
|
-
function isNonBlankString(value) {
|
|
435
|
-
return typeof value === 'string' && value.trim().length > 0;
|
|
436
|
-
}
|
|
437
|
-
function isNonNegativeInteger(value) {
|
|
438
|
-
return Number.isSafeInteger(value) && value >= 0;
|
|
439
|
-
}
|
|
440
|
-
const DOCUMENT_SCOPES = new Set([
|
|
441
|
-
'ephemeral',
|
|
442
|
-
'record',
|
|
443
|
-
'staged',
|
|
444
|
-
]);
|
|
445
|
-
function isDocumentStageResult(value) {
|
|
446
|
-
if (!isObjectRecord(value) || !isObjectRecord(value.handle))
|
|
447
|
-
return false;
|
|
448
|
-
const handle = value.handle;
|
|
449
|
-
return isNonBlankString(handle.file_hash)
|
|
450
|
-
&& isNonBlankString(handle.filename)
|
|
451
|
-
&& typeof handle.mime_type === 'string'
|
|
452
|
-
&& isNonNegativeInteger(handle.size)
|
|
453
|
-
&& isNonBlankString(handle.storage_path)
|
|
454
|
-
&& (handle.scope === undefined || DOCUMENT_SCOPES.has(handle.scope));
|
|
455
|
-
}
|
|
456
|
-
const TASK_STATUSES = new Set([
|
|
457
|
-
'cancelled',
|
|
458
|
-
'completed',
|
|
459
|
-
'failed',
|
|
460
|
-
'in_progress',
|
|
461
|
-
'queued',
|
|
462
|
-
'waiting',
|
|
463
|
-
]);
|
|
464
|
-
function isTaskCreateResult(value) {
|
|
465
|
-
if (!isObjectRecord(value) || !isObjectRecord(value.task))
|
|
466
|
-
return false;
|
|
467
|
-
const { id, revision, status } = value.task;
|
|
468
|
-
return isNonBlankString(id) && isNonNegativeInteger(revision) && TASK_STATUSES.has(status);
|
|
469
|
-
}
|
|
470
|
-
function isTaskReplyResult(value) {
|
|
471
|
-
return isObjectRecord(value) && isNonBlankString(value.messageId) && isNonNegativeInteger(value.taskRevision);
|
|
472
|
-
}
|
|
473
|
-
function isCurrentRecordResult(value) {
|
|
474
|
-
if (!value || typeof value !== 'object')
|
|
475
|
-
return false;
|
|
476
|
-
const result = value;
|
|
477
|
-
return typeof result.sessionId === 'string' && result.sessionId.length > 0 && isRecordSnapshot(result.snapshot);
|
|
478
|
-
}
|
|
479
|
-
function isRecordSnapshot(value) {
|
|
480
|
-
if (!value || typeof value !== 'object')
|
|
481
|
-
return false;
|
|
482
|
-
const snapshot = value;
|
|
483
|
-
return Number.isSafeInteger(snapshot.revision)
|
|
484
|
-
&& (snapshot.revision ?? -1) >= 0
|
|
485
|
-
&& isRecordErrorSnapshot(snapshot.errors)
|
|
486
|
-
&& isRecordFieldSnapshotMap(snapshot.fields)
|
|
487
|
-
&& isObjectRecord(snapshot.values);
|
|
488
|
-
}
|
|
489
|
-
function isRecordUpdateResult(value) {
|
|
490
|
-
if (!value || typeof value !== 'object')
|
|
491
|
-
return false;
|
|
492
|
-
const result = value;
|
|
493
|
-
return typeof result.ok === 'boolean' && isRecordSnapshot(result.snapshot);
|
|
494
|
-
}
|
|
495
|
-
function isRecordSubmitResult(value) {
|
|
496
|
-
if (!value || typeof value !== 'object')
|
|
497
|
-
return false;
|
|
498
|
-
const result = value;
|
|
499
|
-
return typeof result.ok === 'boolean' && isRecordSnapshot(result.snapshot);
|
|
500
|
-
}
|
|
501
|
-
function isObjectRecord(value) {
|
|
502
|
-
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
503
|
-
}
|
|
504
|
-
function isRecordErrorSnapshot(value) {
|
|
505
|
-
if (!isObjectRecord(value) || !isObjectRecord(value.fields) || !Array.isArray(value.form))
|
|
506
|
-
return false;
|
|
507
|
-
return Object.values(value.fields).every(errors => Array.isArray(errors) && errors.every(error => typeof error === 'string'))
|
|
508
|
-
&& value.form.every(error => isObjectRecord(error) && typeof error.code === 'string' && typeof error.message === 'string');
|
|
509
|
-
}
|
|
510
|
-
function isRecordFieldSnapshotMap(value) {
|
|
511
|
-
return isObjectRecord(value) && Object.values(value).every((field) => {
|
|
512
|
-
if (!isObjectRecord(field))
|
|
513
|
-
return false;
|
|
514
|
-
return (field.error === null || typeof field.error === 'string')
|
|
515
|
-
&& (field.info === null || typeof field.info === 'string')
|
|
516
|
-
&& typeof field.readOnly === 'boolean'
|
|
517
|
-
&& typeof field.required === 'boolean'
|
|
518
|
-
&& typeof field.visible === 'boolean';
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
function readResponse(response, request) {
|
|
522
|
-
if (response.protocol !== PROTOCOL_VERSION || response.requestId !== request.requestId) {
|
|
523
|
-
throw invalidResponse('The response does not match the request envelope.');
|
|
524
|
-
}
|
|
525
|
-
if (!response.ok)
|
|
526
|
-
throw new SpaceProtocolError(response.error);
|
|
527
|
-
return response.result;
|
|
528
|
-
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DocumentHandle } from '../types.js';
|
|
2
|
+
import type { SpaceTransport } from './protocol.js';
|
|
3
|
+
/** A file stored but not yet attached to a record. */
|
|
4
|
+
export interface StagedDocumentHandle extends DocumentHandle {
|
|
5
|
+
scope?: 'ephemeral' | 'record' | 'staged';
|
|
6
|
+
}
|
|
7
|
+
export interface DocumentStageInput {
|
|
8
|
+
/** The file to stage. Its bytes are transferred to the host, not copied. */
|
|
9
|
+
file: Blob | File;
|
|
10
|
+
/** Defaults to the file's own type, then to `application/octet-stream`. */
|
|
11
|
+
mimeType?: string;
|
|
12
|
+
/** Defaults to a File's name. A plain Blob needs an explicit name. */
|
|
13
|
+
name?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface DocumentStageResult {
|
|
16
|
+
handle: StagedDocumentHandle;
|
|
17
|
+
}
|
|
18
|
+
export interface SimpleDocumentsClient {
|
|
19
|
+
stage: (document: DocumentStageInput) => Promise<DocumentStageResult>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Uploads a file to staging without attaching it to a record. The host owns
|
|
23
|
+
* storage and returns only the document handle needed by the Space's workflow.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createDocumentsClient(transport: SpaceTransport | undefined, nextRequestId: () => string): SimpleDocumentsClient;
|