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

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
@@ -2231,6 +2231,19 @@ function buildLocalInvocationStats(windowMs, stats, drops) {
2231
2231
 
2232
2232
  //#endregion
2233
2233
  //#region src/iii.ts
2234
+ const MAX_JSON_FRAME_BYTES = 16 * 1024 * 1024;
2235
+ const JSON_ENCODER = new TextEncoder();
2236
+ const PAYLOAD_SIZE_DOCS = "https://iii.dev/docs/creating-workers/channels#payload-size";
2237
+ function createPayloadTooLargeError(size, functionId) {
2238
+ return new InvocationError({
2239
+ code: "payload_too_large",
2240
+ message: `Serialized JSON envelope is ${size} bytes; limit is ${MAX_JSON_FRAME_BYTES} bytes. Use channels for larger payloads: ${PAYLOAD_SIZE_DOCS}`,
2241
+ function_id: functionId
2242
+ });
2243
+ }
2244
+ function isPayloadTooLargeError(error) {
2245
+ return error instanceof Error && "code" in error && error.code === "payload_too_large";
2246
+ }
2234
2247
  /** Short random suffix; `crypto.randomUUID` is unavailable on insecure origins. */
2235
2248
  function randomId() {
2236
2249
  const c = globalThis.crypto;
@@ -2404,19 +2417,20 @@ var Sdk = class {
2404
2417
  this.observeQueue = [];
2405
2418
  this.registerTriggerType = (triggerType, handler) => {
2406
2419
  this.warnDeprecated("registerTriggerType");
2407
- this.sendMessage("registertriggertype", triggerType, true);
2408
- this.triggerTypes.set(triggerType.id, {
2409
- message: {
2410
- ...triggerType,
2411
- message_type: "registertriggertype"
2412
- },
2420
+ const message = this.snapshotRegistration({
2421
+ ...triggerType,
2422
+ message_type: "registertriggertype"
2423
+ });
2424
+ this.sendMessage("registertriggertype", message, true);
2425
+ this.triggerTypes.set(message.id, {
2426
+ message,
2413
2427
  handler
2414
2428
  });
2415
2429
  return {
2416
- id: triggerType.id,
2430
+ id: message.id,
2417
2431
  registerTrigger: (functionId, config) => {
2418
2432
  return this.registerTrigger({
2419
- type: triggerType.id,
2433
+ type: message.id,
2420
2434
  function_id: functionId,
2421
2435
  config,
2422
2436
  namespace: this.namespace
@@ -2425,7 +2439,7 @@ var Sdk = class {
2425
2439
  registerFunction: (functionId, handler, config) => {
2426
2440
  const ref = this.registerFunction(functionId, handler);
2427
2441
  this.registerTrigger({
2428
- type: triggerType.id,
2442
+ type: message.id,
2429
2443
  function_id: functionId,
2430
2444
  config,
2431
2445
  namespace: this.namespace
@@ -2433,7 +2447,7 @@ var Sdk = class {
2433
2447
  return ref;
2434
2448
  },
2435
2449
  unregister: () => {
2436
- this.unregisterTriggerType(triggerType);
2450
+ this.unregisterTriggerType(message);
2437
2451
  }
2438
2452
  };
2439
2453
  };
@@ -2448,7 +2462,7 @@ var Sdk = class {
2448
2462
  this.registerTrigger = (trigger) => {
2449
2463
  this.warnDeprecated("registerTrigger");
2450
2464
  const id = require_utils.randomUUID();
2451
- const fullTrigger = {
2465
+ const fullTrigger = this.snapshotRegistration({
2452
2466
  ...trigger,
2453
2467
  id,
2454
2468
  message_type: "registertrigger",
@@ -2456,7 +2470,7 @@ var Sdk = class {
2456
2470
  const namespace = callNamespace(trigger.namespace, this.namespace, "RegisterTriggerInput.namespace");
2457
2471
  return namespace !== void 0 ? { namespace } : {};
2458
2472
  })()
2459
- };
2473
+ });
2460
2474
  this.sendMessage("registertrigger", fullTrigger, true);
2461
2475
  this.triggers.set(id, fullTrigger);
2462
2476
  return { unregister: () => {
@@ -2474,12 +2488,12 @@ var Sdk = class {
2474
2488
  const { observe: hooks, localDispatch, ...wireOptions } = options ?? {};
2475
2489
  validateObserveHooks(functionId, hooks);
2476
2490
  const descriptor = observeDescriptor(hooks);
2477
- const fullMessage = {
2491
+ const fullMessage = this.snapshotRegistration({
2478
2492
  ...wireOptions,
2479
2493
  ...descriptor ? { observe: descriptor } : {},
2480
2494
  id: functionId,
2481
2495
  message_type: "registerfunction"
2482
- };
2496
+ });
2483
2497
  this.sendMessage("registerfunction", fullMessage, true);
2484
2498
  const observe = new FunctionObservers(functionId, hooks, localDispatch !== false, true, this.observeHost);
2485
2499
  if (observe.needsObserve && this.registeredOnConnection && !this.engineFeatures.has("observe.v1")) this.warnHooksInactive(functionId);
@@ -2953,32 +2967,78 @@ var Sdk = class {
2953
2967
  this.connectionEpoch++;
2954
2968
  this.registeredOnConnection = false;
2955
2969
  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
- }));
2970
+ if (this.workerId) try {
2971
+ this.sendMessageRaw(JSON.stringify({
2972
+ type: "reattach",
2973
+ previous_worker_id: this.workerId,
2974
+ reattach_token: this.reattachToken
2975
+ }));
2976
+ } catch (error) {
2977
+ if (!isPayloadTooLargeError(error)) throw error;
2978
+ console.error("[iii] Reattach JSON envelope rejected", error);
2979
+ }
2961
2980
  this.registerWorkerMetadata();
2962
2981
  this.triggerTypes.forEach(({ message }) => {
2963
- this.sendMessage("registertriggertype", message, true);
2982
+ this.replayRegistration("registertriggertype", message);
2964
2983
  });
2965
2984
  this.functions.forEach(({ message }) => {
2966
- this.sendMessage("registerfunction", message, true);
2985
+ this.replayRegistration("registerfunction", message);
2967
2986
  });
2968
2987
  this.triggers.forEach((trigger) => {
2969
- this.sendMessage("registertrigger", trigger, true);
2988
+ this.replayRegistration("registertrigger", trigger);
2970
2989
  });
2971
2990
  const pending = this.messagesToSend;
2972
2991
  this.messagesToSend = [];
2973
2992
  for (const message of pending) {
2974
2993
  if (message.type === "invokefunction" && typeof message.invocation_id === "string" && !this.invocations.has(message.invocation_id)) continue;
2975
- this.sendMessageRaw(JSON.stringify(message));
2994
+ try {
2995
+ this.sendMessageRaw(JSON.stringify(message));
2996
+ } catch (error) {
2997
+ if (!isPayloadTooLargeError(error)) throw error;
2998
+ console.error("[iii] Queued JSON envelope rejected", error);
2999
+ }
2976
3000
  }
2977
3001
  }
2978
3002
  isOpen() {
2979
3003
  return this.ws?.readyState === WebSocket.OPEN;
2980
3004
  }
3005
+ snapshotRegistration(message) {
3006
+ return JSON.parse(JSON.stringify(message));
3007
+ }
3008
+ replayRegistration(type, message) {
3009
+ try {
3010
+ this.sendMessage(type, message, true);
3011
+ } catch (error) {
3012
+ if (!isPayloadTooLargeError(error)) throw error;
3013
+ console.error("[iii] Registration JSON envelope rejected", error);
3014
+ }
3015
+ }
3016
+ prepareJson(message) {
3017
+ const data = JSON.stringify(message);
3018
+ const size = JSON_ENCODER.encode(data).byteLength;
3019
+ if (size <= MAX_JSON_FRAME_BYTES) return data;
3020
+ const error = createPayloadTooLargeError(size, typeof message.function_id === "string" ? message.function_id : void 0);
3021
+ if (message.type === "invocationresult") {
3022
+ const fallback = JSON.stringify({
3023
+ type: "invocationresult",
3024
+ invocation_id: message.invocation_id,
3025
+ function_id: message.function_id,
3026
+ error: {
3027
+ code: error.code,
3028
+ message: error.message
3029
+ }
3030
+ });
3031
+ if (JSON_ENCODER.encode(fallback).byteLength <= MAX_JSON_FRAME_BYTES) return fallback;
3032
+ }
3033
+ if (typeof message.invocation_id === "string") {
3034
+ const pending = this.invocations.get(message.invocation_id);
3035
+ this.invocations.delete(message.invocation_id);
3036
+ pending?.reject(error);
3037
+ }
3038
+ throw error;
3039
+ }
2981
3040
  sendMessageRaw(data) {
3041
+ if (JSON_ENCODER.encode(data).byteLength > MAX_JSON_FRAME_BYTES) data = this.prepareJson(JSON.parse(data));
2982
3042
  if (this.ws && this.isOpen()) try {
2983
3043
  this.ws.send(data);
2984
3044
  } catch (error) {
@@ -3036,8 +3096,9 @@ var Sdk = class {
3036
3096
  }
3037
3097
  sendMessage(messageType, message, skipIfClosed = false) {
3038
3098
  const wireMessage = this.toWireFormat(messageType, message);
3039
- if (this.isOpen()) this.sendMessageRaw(JSON.stringify(wireMessage));
3040
- else if (!skipIfClosed) this.messagesToSend.push(wireMessage);
3099
+ const data = this.prepareJson(wireMessage);
3100
+ if (this.isOpen()) this.sendMessageRaw(data);
3101
+ else if (!skipIfClosed) this.messagesToSend.push(JSON.parse(data));
3041
3102
  }
3042
3103
  onInvocationResult(invocation_id, result, error) {
3043
3104
  const invocation = this.invocations.get(invocation_id);