@dxos/protocols 0.8.4-main.406dc2a → 0.8.4-main.548089c
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/src/edge-error.d.ts +11 -7
- package/dist/src/edge-error.d.ts.map +1 -1
- package/dist/src/edge-error.js +22 -54
- package/dist/src/edge-error.js.map +1 -1
- package/dist/src/edge.d.ts +92 -5
- package/dist/src/edge.d.ts.map +1 -1
- package/dist/src/edge.js +108 -2
- package/dist/src/edge.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -9
- package/src/edge-error.ts +23 -71
- package/src/edge.ts +199 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/protocols",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.548089c",
|
|
4
4
|
"description": "Protobuf definitions for DXOS protocols.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -60,19 +60,19 @@
|
|
|
60
60
|
],
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"@bufbuild/protobuf": "2.9.0",
|
|
63
|
-
"@dxos/codec-protobuf": "0.8.4-main.
|
|
64
|
-
"@dxos/effect": "0.8.4-main.
|
|
65
|
-
"@dxos/errors": "0.8.4-main.
|
|
66
|
-
"@dxos/
|
|
67
|
-
"@dxos/
|
|
68
|
-
"@dxos/timeframe": "0.8.4-main.
|
|
69
|
-
"@dxos/util": "0.8.4-main.
|
|
63
|
+
"@dxos/codec-protobuf": "0.8.4-main.548089c",
|
|
64
|
+
"@dxos/effect": "0.8.4-main.548089c",
|
|
65
|
+
"@dxos/errors": "0.8.4-main.548089c",
|
|
66
|
+
"@dxos/keys": "0.8.4-main.548089c",
|
|
67
|
+
"@dxos/invariant": "0.8.4-main.548089c",
|
|
68
|
+
"@dxos/timeframe": "0.8.4-main.548089c",
|
|
69
|
+
"@dxos/util": "0.8.4-main.548089c"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@bufbuild/buf": "1.43.0",
|
|
73
73
|
"@bufbuild/protoc-gen-es": "2.9.0",
|
|
74
74
|
"effect": "3.18.3",
|
|
75
|
-
"@dxos/protobuf-compiler": "0.8.4-main.
|
|
75
|
+
"@dxos/protobuf-compiler": "0.8.4-main.548089c"
|
|
76
76
|
},
|
|
77
77
|
"peerDependencies": {
|
|
78
78
|
"effect": "^3.13.3"
|
package/src/edge-error.ts
CHANGED
|
@@ -2,18 +2,28 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import { type EdgeErrorData, type EdgeHttpFailure } from './edge';
|
|
5
|
+
import { type EdgeErrorData, type EdgeFailure, EdgeHttpErrorCodec, ErrorCodec } from './edge.js';
|
|
8
6
|
|
|
9
7
|
// TODO(burdon): Reconcile with @dxos/errors.
|
|
8
|
+
/**
|
|
9
|
+
* Error thrown when a call to the Edge service fails.
|
|
10
|
+
* There 3 possible sources of failure:
|
|
11
|
+
* 1. EdgeFailure (JSON body with { success: false }) -> Processing the request failed and was gracefully handled by EDGE service.
|
|
12
|
+
* 2. Http failure (non-ok code) -> The HTTP request failed (e.g. timeout, network error).
|
|
13
|
+
* -> Unhandled exception on EDGE side, EDGE would provide serialized error in the response body.
|
|
14
|
+
* 3. Processing failure -> Unhandled exception on client side while processing the response.
|
|
15
|
+
*/
|
|
10
16
|
export class EdgeCallFailedError extends Error {
|
|
11
|
-
public static
|
|
12
|
-
|
|
13
|
-
reason:
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
public static fromUnsuccessfulResponse(response: Response, body: EdgeFailure): EdgeCallFailedError {
|
|
18
|
+
const error = new EdgeCallFailedError({
|
|
19
|
+
reason: body.reason,
|
|
20
|
+
errorData: body.errorData,
|
|
21
|
+
isRetryable: body.errorData == null && response.headers.has('Retry-After'),
|
|
22
|
+
retryAfterMs: getRetryAfterMillis(response),
|
|
23
|
+
cause: body.cause ? ErrorCodec.decode(body.cause) : undefined,
|
|
16
24
|
});
|
|
25
|
+
|
|
26
|
+
return error;
|
|
17
27
|
}
|
|
18
28
|
|
|
19
29
|
public static async fromHttpFailure(response: Response): Promise<EdgeCallFailedError> {
|
|
@@ -21,16 +31,15 @@ export class EdgeCallFailedError extends Error {
|
|
|
21
31
|
reason: `HTTP code ${response.status}: ${response.statusText}.`,
|
|
22
32
|
isRetryable: isRetryableCode(response.status),
|
|
23
33
|
retryAfterMs: getRetryAfterMillis(response),
|
|
24
|
-
cause: await
|
|
34
|
+
cause: await EdgeHttpErrorCodec.decode(response),
|
|
25
35
|
});
|
|
26
36
|
}
|
|
27
37
|
|
|
28
|
-
public static
|
|
38
|
+
public static fromProcessingFailureCause(cause: Error): EdgeCallFailedError {
|
|
29
39
|
return new EdgeCallFailedError({
|
|
30
|
-
reason:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
retryAfterMs: getRetryAfterMillis(response),
|
|
40
|
+
reason: 'Error processing request.',
|
|
41
|
+
isRetryable: true,
|
|
42
|
+
cause,
|
|
34
43
|
});
|
|
35
44
|
}
|
|
36
45
|
|
|
@@ -68,12 +77,6 @@ const getRetryAfterMillis = (response: Response) => {
|
|
|
68
77
|
return Number.isNaN(retryAfter) || retryAfter === 0 ? undefined : retryAfter * 1000;
|
|
69
78
|
};
|
|
70
79
|
|
|
71
|
-
export const createRetryableHttpFailure = (args: { reason: any; retryAfterSeconds: number }) => {
|
|
72
|
-
return new Response(JSON.stringify({ success: false, reason: args.reason }), {
|
|
73
|
-
headers: { 'Retry-After': String(args.retryAfterSeconds) },
|
|
74
|
-
});
|
|
75
|
-
};
|
|
76
|
-
|
|
77
80
|
const isRetryableCode = (status: number) => {
|
|
78
81
|
if (status === 501) {
|
|
79
82
|
// Not Implemented
|
|
@@ -81,54 +84,3 @@ const isRetryableCode = (status: number) => {
|
|
|
81
84
|
}
|
|
82
85
|
return !(status >= 400 && status < 500);
|
|
83
86
|
};
|
|
84
|
-
|
|
85
|
-
const parseErrorBody = async (response: Response): Promise<Error | undefined> => {
|
|
86
|
-
if (response.headers.get('Content-Type') !== 'application/json') {
|
|
87
|
-
const body = await response.text();
|
|
88
|
-
return new Error(body.slice(0, 256));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const body = await response.json();
|
|
92
|
-
if (!('error' in body)) {
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return parseSerializedError(body.error);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
type SerializedError = {
|
|
100
|
-
code?: string;
|
|
101
|
-
message?: string;
|
|
102
|
-
context?: Record<string, unknown>;
|
|
103
|
-
stack?: string;
|
|
104
|
-
cause?: SerializedError;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const parseSerializedError = (serializedError: SerializedError): Error => {
|
|
108
|
-
let err: Error;
|
|
109
|
-
if (typeof serializedError.code === 'string') {
|
|
110
|
-
err = new BaseError(serializedError.code, {
|
|
111
|
-
message: serializedError.message ?? 'Unknown error',
|
|
112
|
-
cause: serializedError.cause ? parseSerializedError(serializedError.cause) : undefined,
|
|
113
|
-
context: serializedError.context,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
if (serializedError.stack) {
|
|
117
|
-
Object.defineProperty(err, 'stack', {
|
|
118
|
-
value: serializedError.stack,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
err = new Error(serializedError.message ?? 'Unknown error', {
|
|
123
|
-
cause: serializedError.cause ? parseSerializedError(serializedError.cause) : undefined,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
if (serializedError.stack) {
|
|
127
|
-
Object.defineProperty(err, 'stack', {
|
|
128
|
-
value: serializedError.stack,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return err;
|
|
134
|
-
};
|
package/src/edge.ts
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
+
import { BaseError } from '@dxos/errors';
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
7
9
|
import { SpaceId } from '@dxos/keys';
|
|
8
10
|
|
|
9
11
|
// TODO(burdon): Rename EdgerRouterEndpoint.
|
|
@@ -16,20 +18,38 @@ export enum EdgeService {
|
|
|
16
18
|
STATUS = 'status',
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
export type
|
|
21
|
+
export type EdgeSuccess<T> = {
|
|
20
22
|
success: true;
|
|
21
23
|
data: T;
|
|
22
24
|
};
|
|
23
25
|
|
|
26
|
+
export type SerializedError = {
|
|
27
|
+
code?: string;
|
|
28
|
+
message?: string;
|
|
29
|
+
context?: Record<string, unknown>;
|
|
30
|
+
stack?: string;
|
|
31
|
+
cause?: SerializedError;
|
|
32
|
+
};
|
|
33
|
+
|
|
24
34
|
export type EdgeErrorData = { type: string } & Record<string, any>;
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
|
|
36
|
+
/**
|
|
37
|
+
* This is the shape of the error response from the Edge service,
|
|
38
|
+
* when the error is gracefully handled, the Response will be an object with this shape and have status code 200.
|
|
39
|
+
*/
|
|
40
|
+
export type EdgeFailure = {
|
|
41
|
+
/**
|
|
42
|
+
* Branded Type.
|
|
43
|
+
*/
|
|
28
44
|
success: false;
|
|
29
45
|
/**
|
|
30
46
|
* An explanation of why the call failed. Used mostly for logging and monitoring.
|
|
31
47
|
*/
|
|
32
48
|
reason: string;
|
|
49
|
+
/**
|
|
50
|
+
* Cause Error captured on the EDGE service to aid debugging on the client.
|
|
51
|
+
*/
|
|
52
|
+
cause?: SerializedError;
|
|
33
53
|
/**
|
|
34
54
|
* Information that can be used to retry the request such that it will succeed, for example:
|
|
35
55
|
* 1. { type: 'auth_required', challenge: string }
|
|
@@ -43,7 +63,87 @@ export type EdgeHttpFailure = {
|
|
|
43
63
|
errorData?: EdgeErrorData;
|
|
44
64
|
};
|
|
45
65
|
|
|
46
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Represents a body response from the Edge service.
|
|
68
|
+
*/
|
|
69
|
+
export type EdgeBody<T> = EdgeSuccess<T> | EdgeFailure;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Use this to create a response from the Edge service.
|
|
73
|
+
*/
|
|
74
|
+
export const EdgeResponse = Object.freeze({
|
|
75
|
+
success: <T>(data: T, status: number = 200): Response => {
|
|
76
|
+
invariant(status >= 200 && status < 300, 'Status code must be in the 2xx range');
|
|
77
|
+
const headers = new Headers({ 'Content-Type': 'application/json' });
|
|
78
|
+
return new Response(
|
|
79
|
+
JSON.stringify({
|
|
80
|
+
success: true,
|
|
81
|
+
data,
|
|
82
|
+
} satisfies EdgeSuccess<T>),
|
|
83
|
+
{
|
|
84
|
+
status,
|
|
85
|
+
headers,
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
failure: ({
|
|
90
|
+
reason,
|
|
91
|
+
cause,
|
|
92
|
+
errorData,
|
|
93
|
+
shouldRetryAfter,
|
|
94
|
+
status = 500,
|
|
95
|
+
}: {
|
|
96
|
+
/**
|
|
97
|
+
* An explanation of why the call failed. Used mostly for logging and monitoring.
|
|
98
|
+
*/
|
|
99
|
+
reason: string;
|
|
100
|
+
/**
|
|
101
|
+
* Error that caused the failure.
|
|
102
|
+
* Useful for debugging.
|
|
103
|
+
*/
|
|
104
|
+
cause?: Error;
|
|
105
|
+
/**
|
|
106
|
+
* Information that can be used to retry the request such that it will succeed, for example:
|
|
107
|
+
* 1. { type: 'auth_required', challenge: string }
|
|
108
|
+
* Requires retrying the request with challenge signature included.
|
|
109
|
+
* 2. { type: 'user_confirmation_required', dialog: { title: string, message: string, confirmation_payload: string } }
|
|
110
|
+
* Requires showing a confirmation dialog to a user and retrying the request with confirmation_payload included
|
|
111
|
+
* if the user confirms.
|
|
112
|
+
* When errorData is returned simply retrying the request won't have any effect.
|
|
113
|
+
* EdgeHttpClient should parse well-known errorData into Error types and throw.
|
|
114
|
+
*/
|
|
115
|
+
errorData?: EdgeErrorData;
|
|
116
|
+
/**
|
|
117
|
+
* If provided, this request will be marked as retryable and the client will wait for the specified number of milliseconds before retrying.
|
|
118
|
+
* If not provided, the client will not retry the request.
|
|
119
|
+
*/
|
|
120
|
+
shouldRetryAfter?: number;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Status code of the response.
|
|
124
|
+
* @default 500
|
|
125
|
+
*/
|
|
126
|
+
status?: number;
|
|
127
|
+
}): Response => {
|
|
128
|
+
const headers = new Headers({ 'Content-Type': 'application/json' });
|
|
129
|
+
if (shouldRetryAfter) {
|
|
130
|
+
headers.set('Retry-After', String(shouldRetryAfter));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return new Response(
|
|
134
|
+
JSON.stringify({
|
|
135
|
+
success: false,
|
|
136
|
+
reason,
|
|
137
|
+
errorData,
|
|
138
|
+
cause: cause ? ErrorCodec.encode(cause) : undefined,
|
|
139
|
+
} satisfies EdgeFailure),
|
|
140
|
+
{
|
|
141
|
+
status,
|
|
142
|
+
headers,
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
},
|
|
146
|
+
});
|
|
47
147
|
|
|
48
148
|
export type GetNotarizationResponseBody = {
|
|
49
149
|
awaitingNotarization: { credentials: string[] };
|
|
@@ -122,8 +222,26 @@ export type UploadFunctionRequest = {
|
|
|
122
222
|
ownerPublicKey: string;
|
|
123
223
|
entryPoint: string;
|
|
124
224
|
assets: Record<string, Uint8Array>;
|
|
225
|
+
/**
|
|
226
|
+
* Runtime in Edge that will be used to run the function.
|
|
227
|
+
* Runtime cannot be changed once the function was deployed.
|
|
228
|
+
* @default Runtime.WORKERS_FOR_PLATFORMS
|
|
229
|
+
*/
|
|
230
|
+
runtime?: Runtime;
|
|
125
231
|
};
|
|
126
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Note: Do not change the values of these enums, this values are stored in the FunctionVersions database.
|
|
235
|
+
*/
|
|
236
|
+
export enum Runtime {
|
|
237
|
+
// https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/
|
|
238
|
+
WORKERS_FOR_PLATFORMS = 'WORKERS_FOR_PLATFORMS',
|
|
239
|
+
// https://developers.cloudflare.com/workers/runtime-apis/bindings/worker-loader/
|
|
240
|
+
WORKER_LOADER = 'WORKER_LOADER',
|
|
241
|
+
// Local worker dispatcher for testing.
|
|
242
|
+
TEST = 'TEST',
|
|
243
|
+
}
|
|
244
|
+
|
|
127
245
|
export type UploadFunctionResponseBody = {
|
|
128
246
|
functionId: string;
|
|
129
247
|
version: string;
|
|
@@ -273,7 +391,82 @@ export type ExportBundleResponse = {
|
|
|
273
391
|
}[];
|
|
274
392
|
};
|
|
275
393
|
|
|
276
|
-
export const DocumentCodec = {
|
|
394
|
+
export const DocumentCodec = Object.freeze({
|
|
277
395
|
encode: (doc: Uint8Array) => Buffer.from(doc).toString('base64'),
|
|
278
396
|
decode: (doc: string) => new Uint8Array(Buffer.from(doc, 'base64')),
|
|
279
|
-
};
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
const MAX_ERROR_DEPTH = 3;
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Codec for serializing and deserializing Edge Errors.
|
|
403
|
+
*/
|
|
404
|
+
export const ErrorCodec = Object.freeze({
|
|
405
|
+
encode: (err: Error, depth: number = 0): SerializedError => ({
|
|
406
|
+
code: 'code' in err ? (err as any).code : undefined,
|
|
407
|
+
message: err.message,
|
|
408
|
+
stack: err.stack,
|
|
409
|
+
cause: err.cause instanceof Error && depth < MAX_ERROR_DEPTH ? ErrorCodec.encode(err.cause, depth + 1) : undefined,
|
|
410
|
+
}),
|
|
411
|
+
decode: (serializedError: SerializedError, depth: number = 0): Error => {
|
|
412
|
+
let err: Error;
|
|
413
|
+
if (typeof serializedError.code === 'string') {
|
|
414
|
+
err = new BaseError(serializedError.code, {
|
|
415
|
+
message: serializedError.message ?? 'Unknown error',
|
|
416
|
+
cause:
|
|
417
|
+
serializedError.cause && depth < MAX_ERROR_DEPTH
|
|
418
|
+
? ErrorCodec.decode(serializedError.cause, depth + 1)
|
|
419
|
+
: undefined,
|
|
420
|
+
context: serializedError.context,
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
if (serializedError.stack) {
|
|
424
|
+
Object.defineProperty(err, 'stack', {
|
|
425
|
+
value: serializedError.stack,
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
} else {
|
|
429
|
+
err = new Error(serializedError.message ?? 'Unknown error', {
|
|
430
|
+
cause:
|
|
431
|
+
serializedError.cause && depth < MAX_ERROR_DEPTH
|
|
432
|
+
? ErrorCodec.decode(serializedError.cause, depth + 1)
|
|
433
|
+
: undefined,
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
if (serializedError.stack) {
|
|
437
|
+
Object.defineProperty(err, 'stack', {
|
|
438
|
+
value: serializedError.stack,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return err;
|
|
444
|
+
},
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Codec for serializing and deserializing Edge unhandled HTTP errors.
|
|
449
|
+
*/
|
|
450
|
+
export const EdgeHttpErrorCodec = Object.freeze({
|
|
451
|
+
encode: (err: Error): Response =>
|
|
452
|
+
new Response(
|
|
453
|
+
JSON.stringify({
|
|
454
|
+
error: ErrorCodec.encode(err),
|
|
455
|
+
}),
|
|
456
|
+
{ status: 500 },
|
|
457
|
+
),
|
|
458
|
+
|
|
459
|
+
decode: async (response: Response): Promise<Error | undefined> => {
|
|
460
|
+
if (response.headers.get('Content-Type') !== 'application/json') {
|
|
461
|
+
const body = await response.clone().text();
|
|
462
|
+
return new Error(body.slice(0, 256));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const body = await response.clone().json();
|
|
466
|
+
if (!('error' in body)) {
|
|
467
|
+
return undefined;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return ErrorCodec.decode(body.error);
|
|
471
|
+
},
|
|
472
|
+
});
|