iii-browser-sdk 0.25.0-rc.4 → 0.25.0-rc.6

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/index.cjs CHANGED
@@ -805,7 +805,11 @@ const MATCH_ALL = compileFilter({});
805
805
  */
806
806
  const FEATURE_OBSERVE_V1 = "observe.v1";
807
807
  const FEATURE_LOCAL_DISPATCH_V1 = "local-dispatch.v1";
808
- /** What this SDK declares in `engine::workers::register`. */
808
+ /**
809
+ * What this SDK declares in `engine::workers::register`. Not
810
+ * `large-messages.v1`, unlike the other SDKs: a browser receives one 16 MiB
811
+ * WebSocket frame at most.
812
+ */
809
813
  const SDK_FEATURES = [FEATURE_OBSERVE_V1, FEATURE_LOCAL_DISPATCH_V1];
810
814
 
811
815
  //#endregion
@@ -2231,6 +2235,22 @@ function buildLocalInvocationStats(windowMs, stats, drops) {
2231
2235
 
2232
2236
  //#endregion
2233
2237
  //#region src/iii.ts
2238
+ const MAX_JSON_FRAME_BYTES = 16 * 1024 * 1024;
2239
+ const JSON_ENCODER = new TextEncoder();
2240
+ const PAYLOAD_SIZE_DOCS = "https://iii.dev/docs/creating-workers/channels#payload-size";
2241
+ function payloadTooLargeMessage(size) {
2242
+ return `Serialized JSON envelope is ${size} bytes; limit is ${MAX_JSON_FRAME_BYTES} bytes. Use channels for larger payloads: ${PAYLOAD_SIZE_DOCS}`;
2243
+ }
2244
+ function createPayloadTooLargeError(size, functionId) {
2245
+ return new InvocationError({
2246
+ code: "payload_too_large",
2247
+ message: payloadTooLargeMessage(size),
2248
+ function_id: functionId
2249
+ });
2250
+ }
2251
+ function isPayloadTooLargeError(error) {
2252
+ return error instanceof Error && "code" in error && error.code === "payload_too_large";
2253
+ }
2234
2254
  /** Short random suffix; `crypto.randomUUID` is unavailable on insecure origins. */
2235
2255
  function randomId() {
2236
2256
  const c = globalThis.crypto;
@@ -2404,19 +2424,20 @@ var Sdk = class {
2404
2424
  this.observeQueue = [];
2405
2425
  this.registerTriggerType = (triggerType, handler) => {
2406
2426
  this.warnDeprecated("registerTriggerType");
2407
- this.sendMessage("registertriggertype", triggerType, true);
2408
- this.triggerTypes.set(triggerType.id, {
2409
- message: {
2410
- ...triggerType,
2411
- message_type: "registertriggertype"
2412
- },
2427
+ const message = this.snapshotRegistration({
2428
+ ...triggerType,
2429
+ message_type: "registertriggertype"
2430
+ });
2431
+ this.sendMessage("registertriggertype", message, true);
2432
+ this.triggerTypes.set(message.id, {
2433
+ message,
2413
2434
  handler
2414
2435
  });
2415
2436
  return {
2416
- id: triggerType.id,
2437
+ id: message.id,
2417
2438
  registerTrigger: (functionId, config) => {
2418
2439
  return this.registerTrigger({
2419
- type: triggerType.id,
2440
+ type: message.id,
2420
2441
  function_id: functionId,
2421
2442
  config,
2422
2443
  namespace: this.namespace
@@ -2425,7 +2446,7 @@ var Sdk = class {
2425
2446
  registerFunction: (functionId, handler, config) => {
2426
2447
  const ref = this.registerFunction(functionId, handler);
2427
2448
  this.registerTrigger({
2428
- type: triggerType.id,
2449
+ type: message.id,
2429
2450
  function_id: functionId,
2430
2451
  config,
2431
2452
  namespace: this.namespace
@@ -2433,7 +2454,7 @@ var Sdk = class {
2433
2454
  return ref;
2434
2455
  },
2435
2456
  unregister: () => {
2436
- this.unregisterTriggerType(triggerType);
2457
+ this.unregisterTriggerType(message);
2437
2458
  }
2438
2459
  };
2439
2460
  };
@@ -2448,7 +2469,7 @@ var Sdk = class {
2448
2469
  this.registerTrigger = (trigger) => {
2449
2470
  this.warnDeprecated("registerTrigger");
2450
2471
  const id = require_utils.randomUUID();
2451
- const fullTrigger = {
2472
+ const fullTrigger = this.snapshotRegistration({
2452
2473
  ...trigger,
2453
2474
  id,
2454
2475
  message_type: "registertrigger",
@@ -2456,7 +2477,7 @@ var Sdk = class {
2456
2477
  const namespace = callNamespace(trigger.namespace, this.namespace, "RegisterTriggerInput.namespace");
2457
2478
  return namespace !== void 0 ? { namespace } : {};
2458
2479
  })()
2459
- };
2480
+ });
2460
2481
  this.sendMessage("registertrigger", fullTrigger, true);
2461
2482
  this.triggers.set(id, fullTrigger);
2462
2483
  return { unregister: () => {
@@ -2474,12 +2495,12 @@ var Sdk = class {
2474
2495
  const { observe: hooks, localDispatch, ...wireOptions } = options ?? {};
2475
2496
  validateObserveHooks(functionId, hooks);
2476
2497
  const descriptor = observeDescriptor(hooks);
2477
- const fullMessage = {
2498
+ const fullMessage = this.snapshotRegistration({
2478
2499
  ...wireOptions,
2479
2500
  ...descriptor ? { observe: descriptor } : {},
2480
2501
  id: functionId,
2481
2502
  message_type: "registerfunction"
2482
- };
2503
+ });
2483
2504
  this.sendMessage("registerfunction", fullMessage, true);
2484
2505
  const observe = new FunctionObservers(functionId, hooks, localDispatch !== false, true, this.observeHost);
2485
2506
  if (observe.needsObserve && this.registeredOnConnection && !this.engineFeatures.has("observe.v1")) this.warnHooksInactive(functionId);
@@ -2953,32 +2974,78 @@ var Sdk = class {
2953
2974
  this.connectionEpoch++;
2954
2975
  this.registeredOnConnection = false;
2955
2976
  if (this.ws) this.ws.onmessage = this.onMessage.bind(this);
2956
- if (this.workerId) this.sendMessageRaw(JSON.stringify({
2957
- type: "reattach",
2958
- previous_worker_id: this.workerId,
2959
- reattach_token: this.reattachToken
2960
- }));
2977
+ if (this.workerId) try {
2978
+ this.sendMessageRaw(JSON.stringify({
2979
+ type: "reattach",
2980
+ previous_worker_id: this.workerId,
2981
+ reattach_token: this.reattachToken
2982
+ }));
2983
+ } catch (error) {
2984
+ if (!isPayloadTooLargeError(error)) throw error;
2985
+ console.error("[iii] Reattach JSON envelope rejected", error);
2986
+ }
2961
2987
  this.registerWorkerMetadata();
2962
2988
  this.triggerTypes.forEach(({ message }) => {
2963
- this.sendMessage("registertriggertype", message, true);
2989
+ this.replayRegistration("registertriggertype", message);
2964
2990
  });
2965
2991
  this.functions.forEach(({ message }) => {
2966
- this.sendMessage("registerfunction", message, true);
2992
+ this.replayRegistration("registerfunction", message);
2967
2993
  });
2968
2994
  this.triggers.forEach((trigger) => {
2969
- this.sendMessage("registertrigger", trigger, true);
2995
+ this.replayRegistration("registertrigger", trigger);
2970
2996
  });
2971
2997
  const pending = this.messagesToSend;
2972
2998
  this.messagesToSend = [];
2973
2999
  for (const message of pending) {
2974
3000
  if (message.type === "invokefunction" && typeof message.invocation_id === "string" && !this.invocations.has(message.invocation_id)) continue;
2975
- this.sendMessageRaw(JSON.stringify(message));
3001
+ try {
3002
+ this.sendMessageRaw(JSON.stringify(message));
3003
+ } catch (error) {
3004
+ if (!isPayloadTooLargeError(error)) throw error;
3005
+ console.error("[iii] Queued JSON envelope rejected", error);
3006
+ }
2976
3007
  }
2977
3008
  }
2978
3009
  isOpen() {
2979
3010
  return this.ws?.readyState === WebSocket.OPEN;
2980
3011
  }
3012
+ snapshotRegistration(message) {
3013
+ return JSON.parse(JSON.stringify(message));
3014
+ }
3015
+ replayRegistration(type, message) {
3016
+ try {
3017
+ this.sendMessage(type, message, true);
3018
+ } catch (error) {
3019
+ if (!isPayloadTooLargeError(error)) throw error;
3020
+ console.error("[iii] Registration JSON envelope rejected", error);
3021
+ }
3022
+ }
3023
+ prepareJson(message) {
3024
+ const data = JSON.stringify(message);
3025
+ const size = JSON_ENCODER.encode(data).byteLength;
3026
+ if (size <= MAX_JSON_FRAME_BYTES) return data;
3027
+ const error = createPayloadTooLargeError(size, typeof message.function_id === "string" ? message.function_id : void 0);
3028
+ if (message.type === "invocationresult") {
3029
+ const fallback = JSON.stringify({
3030
+ type: "invocationresult",
3031
+ invocation_id: message.invocation_id,
3032
+ function_id: message.function_id,
3033
+ error: {
3034
+ code: error.code,
3035
+ message: payloadTooLargeMessage(size)
3036
+ }
3037
+ });
3038
+ if (JSON_ENCODER.encode(fallback).byteLength <= MAX_JSON_FRAME_BYTES) return fallback;
3039
+ }
3040
+ if (typeof message.invocation_id === "string") {
3041
+ const pending = this.invocations.get(message.invocation_id);
3042
+ this.invocations.delete(message.invocation_id);
3043
+ pending?.reject(error);
3044
+ }
3045
+ throw error;
3046
+ }
2981
3047
  sendMessageRaw(data) {
3048
+ if (JSON_ENCODER.encode(data).byteLength > MAX_JSON_FRAME_BYTES) data = this.prepareJson(JSON.parse(data));
2982
3049
  if (this.ws && this.isOpen()) try {
2983
3050
  this.ws.send(data);
2984
3051
  } catch (error) {
@@ -3036,8 +3103,9 @@ var Sdk = class {
3036
3103
  }
3037
3104
  sendMessage(messageType, message, skipIfClosed = false) {
3038
3105
  const wireMessage = this.toWireFormat(messageType, message);
3039
- if (this.isOpen()) this.sendMessageRaw(JSON.stringify(wireMessage));
3040
- else if (!skipIfClosed) this.messagesToSend.push(wireMessage);
3106
+ const data = this.prepareJson(wireMessage);
3107
+ if (this.isOpen()) this.sendMessageRaw(data);
3108
+ else if (!skipIfClosed) this.messagesToSend.push(JSON.parse(data));
3041
3109
  }
3042
3110
  onInvocationResult(invocation_id, result, error) {
3043
3111
  const invocation = this.invocations.get(invocation_id);