@dxos/edge-client 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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.
Files changed (41) hide show
  1. package/dist/lib/browser/{chunk-LMP5TVOP.mjs → chunk-VESGVCLQ.mjs} +8 -4
  2. package/dist/lib/browser/{chunk-LMP5TVOP.mjs.map → chunk-VESGVCLQ.mjs.map} +3 -3
  3. package/dist/lib/browser/edge-ws-muxer.mjs +1 -1
  4. package/dist/lib/browser/index.mjs +527 -275
  5. package/dist/lib/browser/index.mjs.map +4 -4
  6. package/dist/lib/browser/meta.json +1 -1
  7. package/dist/lib/browser/testing/index.mjs +1 -1
  8. package/dist/lib/browser/testing/index.mjs.map +2 -2
  9. package/dist/lib/node-esm/{chunk-X7J46ISZ.mjs → chunk-JTBFRYNM.mjs} +8 -4
  10. package/dist/lib/node-esm/{chunk-X7J46ISZ.mjs.map → chunk-JTBFRYNM.mjs.map} +3 -3
  11. package/dist/lib/node-esm/edge-ws-muxer.mjs +1 -1
  12. package/dist/lib/node-esm/index.mjs +527 -275
  13. package/dist/lib/node-esm/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/meta.json +1 -1
  15. package/dist/lib/node-esm/testing/index.mjs +1 -1
  16. package/dist/lib/node-esm/testing/index.mjs.map +2 -2
  17. package/dist/types/src/edge-client.d.ts +15 -15
  18. package/dist/types/src/edge-client.d.ts.map +1 -1
  19. package/dist/types/src/edge-http-client.d.ts +22 -1
  20. package/dist/types/src/edge-http-client.d.ts.map +1 -1
  21. package/dist/types/src/edge-ws-connection.d.ts +20 -0
  22. package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
  23. package/dist/types/src/edge-ws-muxer.d.ts.map +1 -1
  24. package/dist/types/src/http-client.d.ts +10 -7
  25. package/dist/types/src/http-client.d.ts.map +1 -1
  26. package/dist/types/src/index.d.ts +4 -3
  27. package/dist/types/src/index.d.ts.map +1 -1
  28. package/dist/types/src/testing/test-utils.d.ts +1 -1
  29. package/dist/types/src/testing/test-utils.d.ts.map +1 -1
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +18 -15
  32. package/src/edge-client.test.ts +4 -4
  33. package/src/edge-client.ts +73 -42
  34. package/src/edge-http-client.ts +172 -31
  35. package/src/edge-ws-connection.ts +129 -8
  36. package/src/edge-ws-muxer.ts +1 -1
  37. package/src/http-client.test.ts +8 -6
  38. package/src/http-client.ts +13 -7
  39. package/src/index.ts +4 -3
  40. package/src/testing/test-utils.ts +4 -4
  41. package/src/websocket.test.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/edge-client",
3
- "version": "0.8.4-main.84f28bd",
3
+ "version": "0.8.4-main.ae835ea",
4
4
  "description": "EDGE Client",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -10,16 +10,19 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
+ "source": "./src/index.ts",
13
14
  "types": "./dist/types/src/index.d.ts",
14
15
  "browser": "./dist/lib/browser/index.mjs",
15
16
  "node": "./dist/lib/node-esm/index.mjs"
16
17
  },
17
18
  "./muxer": {
19
+ "source": "./src/edge-ws-muxer.ts",
18
20
  "types": "./dist/types/src/edge-ws-muxer.d.ts",
19
21
  "browser": "./dist/lib/browser/edge-ws-muxer.mjs",
20
22
  "node": "./dist/lib/node-esm/edge-ws-muxer.mjs"
21
23
  },
22
24
  "./testing": {
25
+ "source": "./src/testing/index.ts",
23
26
  "types": "./dist/types/src/testing/index.d.ts",
24
27
  "browser": "./dist/lib/browser/testing/index.mjs",
25
28
  "node": "./dist/lib/node-esm/testing/index.mjs"
@@ -39,24 +42,24 @@
39
42
  "README.md"
40
43
  ],
41
44
  "dependencies": {
42
- "@effect/platform": "^0.87.12",
45
+ "@effect/platform": "^0.92.1",
43
46
  "isomorphic-ws": "^5.0.0",
44
47
  "ws": "^8.14.2",
45
- "@dxos/async": "0.8.4-main.84f28bd",
46
- "@dxos/context": "0.8.4-main.84f28bd",
47
- "@dxos/credentials": "0.8.4-main.84f28bd",
48
- "@dxos/debug": "0.8.4-main.84f28bd",
49
- "@dxos/crypto": "0.8.4-main.84f28bd",
50
- "@dxos/invariant": "0.8.4-main.84f28bd",
51
- "@dxos/keyring": "0.8.4-main.84f28bd",
52
- "@dxos/keys": "0.8.4-main.84f28bd",
53
- "@dxos/log": "0.8.4-main.84f28bd",
54
- "@dxos/node-std": "0.8.4-main.84f28bd",
55
- "@dxos/protocols": "0.8.4-main.84f28bd",
56
- "@dxos/util": "0.8.4-main.84f28bd"
48
+ "@dxos/credentials": "0.8.4-main.ae835ea",
49
+ "@dxos/context": "0.8.4-main.ae835ea",
50
+ "@dxos/debug": "0.8.4-main.ae835ea",
51
+ "@dxos/invariant": "0.8.4-main.ae835ea",
52
+ "@dxos/keyring": "0.8.4-main.ae835ea",
53
+ "@dxos/keys": "0.8.4-main.ae835ea",
54
+ "@dxos/log": "0.8.4-main.ae835ea",
55
+ "@dxos/async": "0.8.4-main.ae835ea",
56
+ "@dxos/node-std": "0.8.4-main.ae835ea",
57
+ "@dxos/crypto": "0.8.4-main.ae835ea",
58
+ "@dxos/protocols": "0.8.4-main.ae835ea",
59
+ "@dxos/util": "0.8.4-main.ae835ea"
57
60
  },
58
61
  "devDependencies": {
59
- "@dxos/test-utils": "0.8.4-main.84f28bd"
62
+ "@dxos/test-utils": "0.8.4-main.ae835ea"
60
63
  },
61
64
  "peerDependencies": {
62
65
  "effect": "^3.13.3"
@@ -41,17 +41,17 @@ describe('EdgeClient', () => {
41
41
 
42
42
  const { client } = await openNewClient(endpoint);
43
43
 
44
- expect(client.status).toBe(EdgeStatus.NOT_CONNECTED);
44
+ expect(client.status.state).toBe(EdgeStatus.ConnectionState.NOT_CONNECTED);
45
45
  admitConnection.wake();
46
- await expect.poll(() => client.status).toBe(EdgeStatus.CONNECTED);
46
+ await expect.poll(() => client.status.state).toBe(EdgeStatus.ConnectionState.CONNECTED);
47
47
 
48
48
  admitConnection.reset();
49
49
  await closeConnection();
50
50
  expect(client.isOpen).is.true;
51
- await expect.poll(() => client.status).toBe(EdgeStatus.NOT_CONNECTED);
51
+ await expect.poll(() => client.status.state).toBe(EdgeStatus.ConnectionState.NOT_CONNECTED);
52
52
 
53
53
  admitConnection.wake();
54
- await expect.poll(() => client.status).toBe(EdgeStatus.CONNECTED);
54
+ await expect.poll(() => client.status.state).toBe(EdgeStatus.ConnectionState.CONNECTED);
55
55
  });
56
56
 
57
57
  test('set identity reconnects', async () => {
@@ -2,8 +2,15 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { Trigger, scheduleMicroTask, TriggerState, PersistentLifecycle, Event } from '@dxos/async';
6
- import { Resource, type Lifecycle } from '@dxos/context';
5
+ import {
6
+ Event,
7
+ PersistentLifecycle,
8
+ Trigger,
9
+ TriggerState,
10
+ scheduleMicroTask,
11
+ scheduleTaskInterval,
12
+ } from '@dxos/async';
13
+ import { type Lifecycle, Resource } from '@dxos/context';
7
14
  import { log, logInfo } from '@dxos/log';
8
15
  import { type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
9
16
  import { EdgeStatus } from '@dxos/protocols/proto/dxos/client/services';
@@ -17,9 +24,19 @@ import { getEdgeUrlWithProtocol } from './utils';
17
24
 
18
25
  const DEFAULT_TIMEOUT = 10_000;
19
26
 
27
+ // Refresh status every second: rtt, rate counters.
28
+ const STATUS_REFRESH_INTERVAL = 1000;
29
+
20
30
  export type MessageListener = (message: Message) => void;
21
31
  export type ReconnectListener = () => void;
22
32
 
33
+ export type MessengerConfig = {
34
+ socketEndpoint: string;
35
+ timeout?: number;
36
+ protocol?: Protocol;
37
+ disableAuth?: boolean;
38
+ };
39
+
23
40
  export interface EdgeConnection extends Required<Lifecycle> {
24
41
  statusChanged: Event<EdgeStatus>;
25
42
  get info(): any;
@@ -28,18 +45,11 @@ export interface EdgeConnection extends Required<Lifecycle> {
28
45
  get isOpen(): boolean;
29
46
  get status(): EdgeStatus;
30
47
  setIdentity(identity: EdgeIdentity): void;
48
+ send(message: Message): Promise<void>;
31
49
  onMessage(listener: MessageListener): () => void;
32
50
  onReconnected(listener: ReconnectListener): () => void;
33
- send(message: Message): Promise<void>;
34
51
  }
35
52
 
36
- export type MessengerConfig = {
37
- socketEndpoint: string;
38
- timeout?: number;
39
- protocol?: Protocol;
40
- disableAuth?: boolean;
41
- };
42
-
43
53
  /**
44
54
  * Messenger client for EDGE:
45
55
  * - While open, uses PersistentLifecycle to keep an open EdgeWsConnection, reconnecting on failures.
@@ -81,9 +91,18 @@ export class EdgeClient extends Resource implements EdgeConnection {
81
91
  }
82
92
 
83
93
  get status(): EdgeStatus {
84
- return Boolean(this._currentConnection) && this._ready.state === TriggerState.RESOLVED
85
- ? EdgeStatus.CONNECTED
86
- : EdgeStatus.NOT_CONNECTED;
94
+ return {
95
+ state:
96
+ Boolean(this._currentConnection) && this._ready.state === TriggerState.RESOLVED
97
+ ? EdgeStatus.ConnectionState.CONNECTED
98
+ : EdgeStatus.ConnectionState.NOT_CONNECTED,
99
+ uptime: this._currentConnection?.uptime ?? 0,
100
+ rtt: this._currentConnection?.rtt ?? 0,
101
+ rateBytesUp: this._currentConnection?.uploadRate ?? 0,
102
+ rateBytesDown: this._currentConnection?.downloadRate ?? 0,
103
+ messagesSent: this._currentConnection?.messagesSent ?? 0,
104
+ messagesReceived: this._currentConnection?.messagesReceived ?? 0,
105
+ };
87
106
  }
88
107
 
89
108
  get identityKey() {
@@ -94,7 +113,7 @@ export class EdgeClient extends Resource implements EdgeConnection {
94
113
  return this._identity.peerKey;
95
114
  }
96
115
 
97
- setIdentity(identity: EdgeIdentity): void {
116
+ setIdentity(identity: EdgeIdentity) {
98
117
  if (identity.identityKey !== this._identity.identityKey || identity.peerKey !== this._identity.peerKey) {
99
118
  log('Edge identity changed', { identity, oldIdentity: this._identity });
100
119
  this._identity = identity;
@@ -103,12 +122,36 @@ export class EdgeClient extends Resource implements EdgeConnection {
103
122
  }
104
123
  }
105
124
 
106
- public onMessage(listener: MessageListener): () => void {
125
+ /**
126
+ * Send message.
127
+ * NOTE: The message is guaranteed to be delivered but the service must respond with a message to confirm processing.
128
+ */
129
+ public async send(message: Message) {
130
+ if (this._ready.state !== TriggerState.RESOLVED) {
131
+ log('waiting for websocket');
132
+ await this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT });
133
+ }
134
+
135
+ if (!this._currentConnection) {
136
+ throw new EdgeConnectionClosedError();
137
+ }
138
+
139
+ if (
140
+ message.source &&
141
+ (message.source.peerKey !== this._identity.peerKey || message.source.identityKey !== this.identityKey)
142
+ ) {
143
+ throw new EdgeIdentityChangedError();
144
+ }
145
+
146
+ this._currentConnection.send(message);
147
+ }
148
+
149
+ public onMessage(listener: MessageListener) {
107
150
  this._messageListeners.add(listener);
108
151
  return () => this._messageListeners.delete(listener);
109
152
  }
110
153
 
111
- public onReconnected(listener: () => void): () => void {
154
+ public onReconnected(listener: () => void) {
112
155
  this._reconnectListeners.add(listener);
113
156
  if (this._ready.state === TriggerState.RESOLVED) {
114
157
  // Microtask so that listener is always called asynchronously, no matter the state of the ready trigger
@@ -123,6 +166,7 @@ export class EdgeClient extends Resource implements EdgeConnection {
123
166
  }
124
167
  });
125
168
  }
169
+
126
170
  return () => this._reconnectListeners.delete(listener);
127
171
  }
128
172
 
@@ -134,6 +178,18 @@ export class EdgeClient extends Resource implements EdgeConnection {
134
178
  this._persistentLifecycle.open().catch((err) => {
135
179
  log.warn('Error while opening connection', { err });
136
180
  });
181
+
182
+ // Notify about status changes (rtt, rate counters).
183
+ scheduleTaskInterval(
184
+ this._ctx,
185
+ async () => {
186
+ if (!this._currentConnection) {
187
+ return;
188
+ }
189
+ this.statusChanged.emit(this.status);
190
+ },
191
+ STATUS_REFRESH_INTERVAL,
192
+ );
137
193
  }
138
194
 
139
195
  /**
@@ -200,7 +256,6 @@ export class EdgeClient extends Resource implements EdgeConnection {
200
256
  // Race with restartRequired so that restart is not blocked by _connect execution.
201
257
  // Wait on ready to attempt a reconnect if it times out.
202
258
  await Promise.race([this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT }), restartRequired]);
203
-
204
259
  return connection;
205
260
  }
206
261
 
@@ -237,30 +292,6 @@ export class EdgeClient extends Resource implements EdgeConnection {
237
292
  }
238
293
  }
239
294
 
240
- /**
241
- * Send message.
242
- * NOTE: The message is guaranteed to be delivered but the service must respond with a message to confirm processing.
243
- */
244
- public async send(message: Message): Promise<void> {
245
- if (this._ready.state !== TriggerState.RESOLVED) {
246
- log('waiting for websocket to become ready');
247
- await this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT });
248
- }
249
-
250
- if (!this._currentConnection) {
251
- throw new EdgeConnectionClosedError();
252
- }
253
-
254
- if (
255
- message.source &&
256
- (message.source.peerKey !== this._identity.peerKey || message.source.identityKey !== this.identityKey)
257
- ) {
258
- throw new EdgeIdentityChangedError();
259
- }
260
-
261
- this._currentConnection.send(message);
262
- }
263
-
264
295
  private async _createAuthHeader(path: string): Promise<string | undefined> {
265
296
  const httpUrl = new URL(path, this._baseHttpUrl);
266
297
  httpUrl.protocol = getEdgeUrlWithProtocol(this._baseWsUrl.toString(), 'http');
@@ -277,7 +308,7 @@ export class EdgeClient extends Resource implements EdgeConnection {
277
308
  }
278
309
 
279
310
  const encodePresentationWsAuthHeader = (encodedPresentation: Uint8Array): string => {
280
- // = and / characters are not allowed in the WebSocket subprotocol header.
311
+ // '=' and '/' characters are not allowed in the WebSocket subprotocol header.
281
312
  const encodedToken = Buffer.from(encodedPresentation).toString('base64').replace(/=*$/, '').replaceAll('/', '|');
282
313
  return `base64url.bearer.authorization.dxos.org.${encodedToken}`;
283
314
  };
@@ -2,47 +2,54 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { FetchHttpClient, HttpClient } from '@effect/platform';
6
- import { Effect, pipe } from 'effect';
5
+ import * as FetchHttpClient from '@effect/platform/FetchHttpClient';
6
+ import * as HttpClient from '@effect/platform/HttpClient';
7
+ import * as Effect from 'effect/Effect';
8
+ import * as Function from 'effect/Function';
7
9
 
8
10
  import { sleep } from '@dxos/async';
9
11
  import { Context } from '@dxos/context';
12
+ import { invariant } from '@dxos/invariant';
10
13
  import { type PublicKey, type SpaceId } from '@dxos/keys';
11
14
  import { log } from '@dxos/log';
12
15
  import {
13
- type CreateAgentResponseBody,
14
16
  type CreateAgentRequestBody,
17
+ type CreateAgentResponseBody,
15
18
  type CreateSpaceRequest,
16
19
  type CreateSpaceResponseBody,
17
20
  EdgeAuthChallengeError,
21
+ type EdgeBody,
18
22
  EdgeCallFailedError,
19
- type EdgeHttpResponse,
23
+ type EdgeStatus,
20
24
  type ExecuteWorkflowResponseBody,
25
+ type ExportBundleRequest,
26
+ type ExportBundleResponse,
21
27
  type GetAgentStatusResponseBody,
22
28
  type GetNotarizationResponseBody,
29
+ type ImportBundleRequest,
23
30
  type InitiateOAuthFlowRequest,
24
31
  type InitiateOAuthFlowResponse,
25
32
  type JoinSpaceRequest,
26
33
  type JoinSpaceResponseBody,
27
- type RecoverIdentityRequest,
28
- type RecoverIdentityResponseBody,
29
34
  type ObjectId,
30
35
  type PostNotarizationRequestBody,
31
- type QueueQuery,
32
36
  type QueryResult,
37
+ type QueueQuery,
38
+ type RecoverIdentityRequest,
39
+ type RecoverIdentityResponseBody,
33
40
  type UploadFunctionRequest,
34
41
  type UploadFunctionResponseBody,
35
- type EdgeStatus,
36
42
  } from '@dxos/protocols';
37
43
  import { createUrl } from '@dxos/util';
38
44
 
39
45
  import { type EdgeIdentity, handleAuthChallenge } from './edge-identity';
40
- import { encodeAuthHeader, HttpConfig, withLogging, withRetryConfig } from './http-client';
46
+ import { HttpConfig, encodeAuthHeader, withLogging, withRetryConfig } from './http-client';
41
47
  import { getEdgeUrlWithProtocol } from './utils';
42
48
 
43
49
  const DEFAULT_RETRY_TIMEOUT = 1500;
44
50
  const DEFAULT_RETRY_JITTER = 500;
45
51
  const DEFAULT_MAX_RETRIES_COUNT = 3;
52
+ const WARNING_BODY_SIZE = 10 * 1024 * 1024; // 10MB
46
53
 
47
54
  export type RetryConfig = {
48
55
  /**
@@ -64,6 +71,16 @@ type EdgeHttpRequestArgs = {
64
71
  context?: Context;
65
72
  retry?: RetryConfig;
66
73
  body?: any;
74
+ /**
75
+ * @default true
76
+ */
77
+ json?: boolean;
78
+
79
+ /**
80
+ * Do not expect a standard EDGE JSON response with a `success` field.
81
+ * @deprecated Use only for debugging.
82
+ */
83
+ rawResponse?: boolean;
67
84
  };
68
85
 
69
86
  export type EdgeHttpGetArgs = Pick<EdgeHttpRequestArgs, 'context' | 'retry'>;
@@ -246,8 +263,63 @@ export class EdgeHttpClient {
246
263
  body: UploadFunctionRequest,
247
264
  args?: EdgeHttpGetArgs,
248
265
  ): Promise<UploadFunctionResponseBody> {
266
+ const formData = new FormData();
267
+ formData.append('name', body.name ?? '');
268
+ formData.append('version', body.version);
269
+ formData.append('ownerPublicKey', body.ownerPublicKey);
270
+ formData.append('entryPoint', body.entryPoint);
271
+ for (const [filename, content] of Object.entries(body.assets)) {
272
+ formData.append(
273
+ 'assets',
274
+ new Blob([content as Uint8Array<ArrayBuffer>], { type: getFileMimeType(filename) }),
275
+ filename,
276
+ );
277
+ }
278
+
249
279
  const path = ['functions', ...(pathParts.functionId ? [pathParts.functionId] : [])].join('/');
250
- return this._call(new URL(path, this.baseUrl), { ...args, body, method: 'PUT' });
280
+ return this._call(new URL(path, this.baseUrl), {
281
+ ...args,
282
+ body: formData,
283
+ method: 'PUT',
284
+ json: false,
285
+ });
286
+ }
287
+
288
+ public async listFunctions(args?: EdgeHttpGetArgs): Promise<any> {
289
+ return this._call(new URL('/functions', this.baseUrl), { ...args, method: 'GET' });
290
+ }
291
+
292
+ public async invokeFunction(
293
+ params: {
294
+ functionId: string;
295
+ version?: string;
296
+ spaceId?: SpaceId;
297
+ cpuTimeLimit?: number;
298
+ subrequestsLimit?: number;
299
+ },
300
+ input: unknown,
301
+ args?: EdgeHttpGetArgs,
302
+ ): Promise<any> {
303
+ const url = new URL(`/functions/${params.functionId}`, this.baseUrl);
304
+ if (params.version) {
305
+ url.searchParams.set('version', params.version);
306
+ }
307
+ if (params.spaceId) {
308
+ url.searchParams.set('spaceId', params.spaceId.toString());
309
+ }
310
+ if (params.cpuTimeLimit) {
311
+ url.searchParams.set('cpuTimeLimit', params.cpuTimeLimit.toString());
312
+ }
313
+ if (params.subrequestsLimit) {
314
+ url.searchParams.set('subrequestsLimit', params.subrequestsLimit.toString());
315
+ }
316
+
317
+ return this._call(url, {
318
+ ...args,
319
+ body: input,
320
+ method: 'POST',
321
+ rawResponse: true,
322
+ });
251
323
  }
252
324
 
253
325
  //
@@ -267,12 +339,44 @@ export class EdgeHttpClient {
267
339
  });
268
340
  }
269
341
 
342
+ //
343
+ // Triggers
344
+ //
345
+
346
+ public async getCronTriggers(spaceId: SpaceId) {
347
+ return this._call(new URL(`/test/functions/${spaceId}/triggers/crons`, this.baseUrl), { method: 'GET' });
348
+ }
349
+
350
+ //
351
+ // Import/Export space.
352
+ //
353
+
354
+ public async importBundle(
355
+ spaceId: SpaceId, //
356
+ body: ImportBundleRequest,
357
+ args?: EdgeHttpGetArgs,
358
+ ): Promise<void> {
359
+ return this._call(new URL(`/spaces/${spaceId}/import`, this.baseUrl), { ...args, body, method: 'PUT' });
360
+ }
361
+
362
+ public async exportBundle(
363
+ spaceId: SpaceId,
364
+ body: ExportBundleRequest,
365
+ args?: EdgeHttpGetArgs,
366
+ ): Promise<ExportBundleResponse> {
367
+ return this._call(new URL(`/spaces/${spaceId}/export`, this.baseUrl), {
368
+ ...args,
369
+ body,
370
+ method: 'POST',
371
+ });
372
+ }
373
+
270
374
  //
271
375
  // Internal
272
376
  //
273
377
 
274
378
  private async _fetch<T>(url: URL, args: EdgeHttpRequestArgs): Promise<T> {
275
- return pipe(
379
+ return Function.pipe(
276
380
  HttpClient.get(url),
277
381
  withLogging,
278
382
  withRetryConfig,
@@ -286,44 +390,53 @@ export class EdgeHttpClient {
286
390
  // TODO(burdon): Refactor with effect (see edge-http-client.test.ts).
287
391
  private async _call<T>(url: URL, args: EdgeHttpRequestArgs): Promise<T> {
288
392
  const shouldRetry = createRetryHandler(args);
289
- const requestContext = args.context ?? new Context();
393
+ const requestContext = args.context ?? Context.default();
290
394
  log('fetch', { url, request: args.body });
291
395
 
292
396
  let handledAuth = false;
293
397
  while (true) {
294
- let processingError: EdgeCallFailedError;
295
- let retryAfterHeaderValue: number = Number.NaN;
398
+ let processingError: EdgeCallFailedError | undefined = undefined;
296
399
  try {
297
400
  const request = createRequest(args, this._authHeader);
298
401
  const response = await fetch(url, request);
299
- retryAfterHeaderValue = Number(response.headers.get('Retry-After'));
402
+ const body: EdgeBody<T> | undefined =
403
+ response.headers.get('Content-Type') === 'application/json' ? await response.clone().json() : undefined;
404
+
300
405
  if (response.ok) {
301
- const body = (await response.json()) as EdgeHttpResponse<T>;
406
+ if (args.rawResponse) {
407
+ return body as any;
408
+ }
409
+ invariant(body, 'Expected body to be present');
410
+ if (!('success' in body)) {
411
+ return body;
412
+ }
302
413
  if (body.success) {
303
414
  return body.data;
304
415
  }
305
-
306
- log.warn('unsuccessful edge response', { url, body });
307
- if (body.errorData?.type === 'auth_challenge' && typeof body.errorData?.challenge === 'string') {
308
- processingError = new EdgeAuthChallengeError(body.errorData.challenge, body.errorData);
309
- } else {
310
- processingError = EdgeCallFailedError.fromUnsuccessfulResponse(response, body);
311
- }
312
416
  } else if (response.status === 401 && !handledAuth) {
313
417
  this._authHeader = await this._handleUnauthorized(response);
314
418
  handledAuth = true;
315
419
  continue;
420
+ }
421
+
422
+ invariant(!body?.success, 'Expected body to not be a failure response or undefined.');
423
+
424
+ if (body?.errorData?.type === 'auth_challenge' && typeof body?.errorData?.challenge === 'string') {
425
+ processingError = new EdgeAuthChallengeError(body.errorData.challenge, body.errorData);
426
+ } else if (body?.success === false) {
427
+ processingError = EdgeCallFailedError.fromUnsuccessfulResponse(response, body);
316
428
  } else {
317
- processingError = EdgeCallFailedError.fromHttpFailure(response);
429
+ invariant(!response.ok, 'Expected response to not be ok.');
430
+ processingError = await EdgeCallFailedError.fromHttpFailure(response);
318
431
  }
319
432
  } catch (error: any) {
320
433
  processingError = EdgeCallFailedError.fromProcessingFailureCause(error);
321
434
  }
322
435
 
323
- if (processingError.isRetryable && (await shouldRetry(requestContext, retryAfterHeaderValue))) {
436
+ if (processingError?.isRetryable && (await shouldRetry(requestContext, processingError.retryAfterMs))) {
324
437
  log('retrying edge request', { url, processingError });
325
438
  } else {
326
- throw processingError;
439
+ throw processingError!;
327
440
  }
328
441
  }
329
442
  }
@@ -331,7 +444,7 @@ export class EdgeHttpClient {
331
444
  private async _handleUnauthorized(response: Response): Promise<string> {
332
445
  if (!this._edgeIdentity) {
333
446
  log.warn('unauthorized response received before identity was set');
334
- throw EdgeCallFailedError.fromHttpFailure(response);
447
+ throw await EdgeCallFailedError.fromHttpFailure(response);
335
448
  }
336
449
 
337
450
  const challenge = await handleAuthChallenge(response, this._edgeIdentity);
@@ -339,11 +452,32 @@ export class EdgeHttpClient {
339
452
  }
340
453
  }
341
454
 
342
- const createRequest = ({ method, body }: EdgeHttpRequestArgs, authHeader: string | undefined): RequestInit => {
455
+ const createRequest = (
456
+ { method, body, json = true }: EdgeHttpRequestArgs,
457
+ authHeader: string | undefined,
458
+ ): RequestInit => {
459
+ let requestBody: BodyInit | undefined;
460
+ const headers: HeadersInit = {};
461
+
462
+ if (json) {
463
+ requestBody = body && JSON.stringify(body);
464
+ headers['Content-Type'] = 'application/json';
465
+ } else {
466
+ requestBody = body;
467
+ }
468
+
469
+ if (typeof requestBody === 'string' && requestBody.length > WARNING_BODY_SIZE) {
470
+ log.warn('Request with large body', { bodySize: requestBody.length });
471
+ }
472
+
473
+ if (authHeader) {
474
+ headers['Authorization'] = authHeader;
475
+ }
476
+
343
477
  return {
344
478
  method,
345
- body: body && JSON.stringify(body),
346
- headers: authHeader ? { Authorization: authHeader } : undefined,
479
+ body: requestBody,
480
+ headers,
347
481
  };
348
482
  };
349
483
 
@@ -359,7 +493,7 @@ const createRetryHandler = ({ retry }: EdgeHttpRequestArgs) => {
359
493
  const maxRetries = retry.count ?? DEFAULT_MAX_RETRIES_COUNT;
360
494
  const baseTimeout = retry.timeout ?? DEFAULT_RETRY_TIMEOUT;
361
495
  const jitter = retry.jitter ?? DEFAULT_RETRY_JITTER;
362
- return async (ctx: Context, retryAfter: number) => {
496
+ return async (ctx: Context, retryAfter?: number) => {
363
497
  if (++retries > maxRetries || ctx.disposed) {
364
498
  return false;
365
499
  }
@@ -374,3 +508,10 @@ const createRetryHandler = ({ retry }: EdgeHttpRequestArgs) => {
374
508
  return true;
375
509
  };
376
510
  };
511
+
512
+ const getFileMimeType = (filename: string) =>
513
+ ['.js', '.mjs'].some((codeExtension) => filename.endsWith(codeExtension))
514
+ ? 'application/javascript+module'
515
+ : filename.endsWith('.wasm')
516
+ ? 'application/wasm'
517
+ : 'application/octet-stream';