@valerypopoff/rivet2-core 2.33.0 → 2.34.1

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.
@@ -11557,6 +11557,13 @@ var FNV_OFFSETS = {
11557
11557
  };
11558
11558
  var cachedEncoder = new globalThis.TextEncoder();
11559
11559
  function fnv1aUint8Array(uint8Array, size) {
11560
+ if (size === 32) {
11561
+ let hash2 = 2166136261;
11562
+ for (let index = 0; index < uint8Array.length; index++) {
11563
+ hash2 = Math.imul(hash2 ^ uint8Array[index], 16777619);
11564
+ }
11565
+ return BigInt(hash2 >>> 0);
11566
+ }
11560
11567
  const fnvPrime = FNV_PRIMES[size];
11561
11568
  let hash = FNV_OFFSETS[size];
11562
11569
  for (let index = 0; index < uint8Array.length; index++) {
@@ -11630,7 +11637,16 @@ var toRecordedEventMap = {
11630
11637
  },
11631
11638
  execution
11632
11639
  ),
11633
- nodeFinish: ({ node, outputs, processId, resultOrigin, durationMs, splitRunDurationMs, streamingWatchTerminal, execution }) => withExecution(
11640
+ nodeFinish: ({
11641
+ node,
11642
+ outputs,
11643
+ processId,
11644
+ resultOrigin,
11645
+ durationMs,
11646
+ splitRunDurationMs,
11647
+ streamingWatchTerminal,
11648
+ execution
11649
+ }) => withExecution(
11634
11650
  withDuration(
11635
11651
  {
11636
11652
  nodeId: node.id,
@@ -11644,7 +11660,17 @@ var toRecordedEventMap = {
11644
11660
  ),
11645
11661
  execution
11646
11662
  ),
11647
- nodeError: ({ node, error, processId, outputs, splitOutputs, resultOrigin, durationMs, splitRunDurationMs, execution }) => withExecution(
11663
+ nodeError: ({
11664
+ node,
11665
+ error,
11666
+ processId,
11667
+ outputs,
11668
+ splitOutputs,
11669
+ resultOrigin,
11670
+ durationMs,
11671
+ splitRunDurationMs,
11672
+ execution
11673
+ }) => withExecution(
11648
11674
  withDuration(
11649
11675
  {
11650
11676
  nodeId: node.id,
@@ -11802,18 +11828,17 @@ function mapValuesDeep(obj, fn) {
11802
11828
  });
11803
11829
  }
11804
11830
  if ((0, import_lodash_es5.isPlainObject)(obj)) {
11805
- return Object.fromEntries(
11806
- Object.entries(obj).map(([key, value]) => {
11807
- if ((0, import_lodash_es5.isPlainObject)(value) || Array.isArray(value)) {
11808
- return [key, mapValuesDeep(value, fn)];
11809
- }
11810
- return [key, fn(value)];
11811
- })
11812
- );
11831
+ const entries2 = Object.entries(obj);
11832
+ for (const entry of entries2) {
11833
+ const value = entry[1];
11834
+ entry[1] = (0, import_lodash_es5.isPlainObject)(value) || Array.isArray(value) ? mapValuesDeep(value, fn) : fn(value);
11835
+ }
11836
+ return Object.fromEntries(entries2);
11813
11837
  }
11814
11838
  return fn(obj);
11815
11839
  }
11816
11840
  function serializeToObject(recording) {
11841
+ const stringHashes = /* @__PURE__ */ new Map();
11817
11842
  const serialized = {
11818
11843
  version: 1,
11819
11844
  recording,
@@ -11833,9 +11858,13 @@ function serializeToObject(recording) {
11833
11858
  return `$ASSET:${id}`;
11834
11859
  }
11835
11860
  }
11836
- if (typeof val === "string" && !val.startsWith("$ASSET:") && val.length > 30) {
11837
- const hash = fnv1a(val, { size: 32 });
11838
- serialized.strings[`${hash}`] = val;
11861
+ if (typeof val === "string" && val.length > 30 && !val.startsWith("$ASSET:")) {
11862
+ let hash = stringHashes.get(val);
11863
+ if (hash === void 0) {
11864
+ hash = String(fnv1a(val, { size: 32 }));
11865
+ stringHashes.set(val, hash);
11866
+ }
11867
+ serialized.strings[hash] = val;
11839
11868
  return `$STRING:${hash}`;
11840
11869
  }
11841
11870
  return val;
@@ -11873,27 +11902,64 @@ var ExecutionRecorder = class _ExecutionRecorder {
11873
11902
  #emitter;
11874
11903
  #includePartialOutputs;
11875
11904
  #includeTrace;
11905
+ #capturePolicy;
11906
+ #captureDiscarded = false;
11876
11907
  constructor(options2 = {}) {
11908
+ var _a;
11877
11909
  this.#emitter = new import_emittery.default();
11878
11910
  this.#emitter.bindMethods(this, ["on", "off", "once"]);
11879
11911
  this.#includePartialOutputs = options2.includePartialOutputs ?? false;
11880
11912
  this.#includeTrace = options2.includeTrace ?? false;
11913
+ try {
11914
+ this.#capturePolicy = (_a = options2.createCapturePolicy) == null ? void 0 : _a.call(options2, this);
11915
+ } catch {
11916
+ this.#captureDiscarded = true;
11917
+ }
11918
+ }
11919
+ get captureDiscarded() {
11920
+ return this.#captureDiscarded;
11921
+ }
11922
+ /** Release replay data, without cancelling the graph or its terminal listener. */
11923
+ discard() {
11924
+ this.#captureDiscarded = true;
11925
+ this.#events.length = 0;
11926
+ const policy = this.#capturePolicy;
11927
+ this.#capturePolicy = void 0;
11928
+ try {
11929
+ policy == null ? void 0 : policy.release();
11930
+ } catch {
11931
+ }
11932
+ }
11933
+ #append(event) {
11934
+ if (this.#captureDiscarded) return;
11935
+ try {
11936
+ if (this.#capturePolicy && !this.#capturePolicy.retain(event)) {
11937
+ this.discard();
11938
+ return;
11939
+ }
11940
+ } catch {
11941
+ this.discard();
11942
+ return;
11943
+ }
11944
+ this.#events.push(event);
11881
11945
  }
11882
11946
  on = void 0;
11883
11947
  off = void 0;
11884
11948
  once = void 0;
11885
11949
  recordSocket(channel, options2 = {}) {
11886
- return new Promise((resolve) => {
11950
+ return new Promise((resolve, reject) => {
11887
11951
  var _a, _b;
11888
- this.recordingId = (0, import_non_secure7.nanoid)();
11889
11952
  let settled = false;
11890
- const finish = (emitFinishedRecording) => {
11953
+ const detach = () => {
11891
11954
  var _a2;
11892
- if (settled) return;
11893
- settled = true;
11894
11955
  channel.removeEventListener("message", listener);
11895
11956
  channel.removeEventListener("close", onClose);
11896
11957
  (_a2 = options2.signal) == null ? void 0 : _a2.removeEventListener("abort", onOwnerDisposed);
11958
+ };
11959
+ const finish = (emitFinishedRecording) => {
11960
+ if (settled) return;
11961
+ settled = true;
11962
+ detach();
11897
11963
  if (emitFinishedRecording) {
11898
11964
  emitDetached(this.#emitter, "finish", {
11899
11965
  recording: this.getRecording()
@@ -11926,57 +11992,80 @@ var ExecutionRecorder = class _ExecutionRecorder {
11926
11992
  if (getEventProjectScope(data) !== void 0) {
11927
11993
  return;
11928
11994
  }
11929
- this.#events.push(toRecordedEvent(message2, data));
11995
+ if (!this.#captureDiscarded) this.#append(toRecordedEvent(message2, data));
11930
11996
  if (isSocketRecordingTerminalEvent(message2)) {
11931
11997
  finish(true);
11932
11998
  }
11933
11999
  };
11934
- if ((_a = options2.signal) == null ? void 0 : _a.aborted) {
11935
- onOwnerDisposed();
11936
- return;
11937
- }
11938
- channel.addEventListener("message", listener);
11939
- channel.addEventListener("close", onClose, { once: true });
11940
- (_b = options2.signal) == null ? void 0 : _b.addEventListener("abort", onOwnerDisposed, { once: true });
11941
- if (channel.readyState === CLOSED_WEBSOCKET_READY_STATE) {
11942
- onClose();
12000
+ try {
12001
+ this.recordingId = (0, import_non_secure7.nanoid)();
12002
+ if ((_a = options2.signal) == null ? void 0 : _a.aborted) {
12003
+ onOwnerDisposed();
12004
+ return;
12005
+ }
12006
+ channel.addEventListener("message", listener);
12007
+ channel.addEventListener("close", onClose, { once: true });
12008
+ (_b = options2.signal) == null ? void 0 : _b.addEventListener("abort", onOwnerDisposed, { once: true });
12009
+ if (channel.readyState === CLOSED_WEBSOCKET_READY_STATE) onClose();
12010
+ } catch (error) {
12011
+ settled = true;
12012
+ try {
12013
+ detach();
12014
+ } catch {
12015
+ }
12016
+ this.discard();
12017
+ reject(error);
11943
12018
  }
11944
12019
  });
11945
12020
  }
11946
12021
  record(processor) {
11947
- this.recordingId = (0, import_non_secure7.nanoid)();
11948
- const recordingProjectScope = processor.recordingProjectScope;
12022
+ let unsubscribeAny;
12023
+ let unsubscribeFinish;
11949
12024
  let settled = false;
11950
12025
  const finish = () => {
11951
12026
  if (settled) return;
11952
12027
  settled = true;
11953
- unsubscribeAny();
11954
- unsubscribeFinish();
12028
+ unsubscribeAny == null ? void 0 : unsubscribeAny();
12029
+ unsubscribeFinish == null ? void 0 : unsubscribeFinish();
11955
12030
  emitDetached(this.#emitter, "finish", {
11956
12031
  recording: this.getRecording()
11957
12032
  });
11958
12033
  };
11959
- const unsubscribeAny = processor.onAny((event, data) => {
11960
- if (settled || event === "finish") {
11961
- return;
11962
- }
11963
- if (this.#includePartialOutputs === false && event === "partialOutput") {
11964
- return;
11965
- }
11966
- if (this.#includeTrace === false && event === "trace") {
11967
- return;
11968
- }
11969
- const eventScope = getEventProjectScope(data);
11970
- if (eventScope !== void 0 && eventScope !== recordingProjectScope) {
11971
- return;
12034
+ try {
12035
+ this.recordingId = (0, import_non_secure7.nanoid)();
12036
+ const recordingProjectScope = processor.recordingProjectScope;
12037
+ unsubscribeAny = processor.onAny((event, data) => {
12038
+ if (settled || event === "finish") {
12039
+ return;
12040
+ }
12041
+ if (this.#includePartialOutputs === false && event === "partialOutput") {
12042
+ return;
12043
+ }
12044
+ if (this.#includeTrace === false && event === "trace") {
12045
+ return;
12046
+ }
12047
+ const eventScope = getEventProjectScope(data);
12048
+ if (eventScope !== void 0 && eventScope !== recordingProjectScope) {
12049
+ return;
12050
+ }
12051
+ if (!this.#captureDiscarded) this.#append(toRecordedEvent(event, data));
12052
+ });
12053
+ unsubscribeFinish = processor.on("finish", finish);
12054
+ } catch (error) {
12055
+ settled = true;
12056
+ try {
12057
+ unsubscribeAny == null ? void 0 : unsubscribeAny();
12058
+ } catch {
11972
12059
  }
11973
- this.#events.push(toRecordedEvent(event, data));
11974
- });
11975
- const unsubscribeFinish = processor.on("finish", finish);
12060
+ this.discard();
12061
+ throw error;
12062
+ }
11976
12063
  return (terminal) => {
11977
12064
  if (settled) return;
11978
- if ((terminal == null ? void 0 : terminal.type) === "done") this.#events.push(toRecordedEvent("done", { results: terminal.results }));
11979
- if ((terminal == null ? void 0 : terminal.type) === "error") this.#events.push(toRecordedEvent("error", { error: terminal.error }));
12065
+ if (!this.#captureDiscarded && (terminal == null ? void 0 : terminal.type) === "done")
12066
+ this.#append(toRecordedEvent("done", { results: terminal.results }));
12067
+ if (!this.#captureDiscarded && (terminal == null ? void 0 : terminal.type) === "error")
12068
+ this.#append(toRecordedEvent("error", { error: terminal.error }));
11980
12069
  finish();
11981
12070
  };
11982
12071
  }
@@ -12015,9 +12104,11 @@ var ExecutionRecorder = class _ExecutionRecorder {
12015
12104
  return recorder;
12016
12105
  }
12017
12106
  serialize() {
12107
+ if (this.#captureDiscarded) throw new Error("Recording capture is unavailable.");
12018
12108
  return JSON.stringify(serializeToObject(this.getRecording()));
12019
12109
  }
12020
12110
  serializeStream() {
12111
+ if (this.#captureDiscarded) throw new Error("Recording capture is unavailable.");
12021
12112
  return (0, import_json_stream_es.stringifyJsonStream)(serializeToObject(this.getRecording()));
12022
12113
  }
12023
12114
  };
@@ -40170,6 +40261,18 @@ var HttpCallNodeImpl = class extends NodeImpl {
40170
40261
  async process(inputs, context) {
40171
40262
  var _a, _b;
40172
40263
  const requestAttempts = this.data.retryOnNon200 ? createHttpCallRequestAttempts() : void 0;
40264
+ let ownedResponse;
40265
+ const discardResponse = () => {
40266
+ const response = ownedResponse;
40267
+ ownedResponse = void 0;
40268
+ try {
40269
+ const body = response == null ? void 0 : response.body;
40270
+ if (body && !body.locked) {
40271
+ void body.cancel().catch(() => void 0);
40272
+ }
40273
+ } catch {
40274
+ }
40275
+ };
40173
40276
  try {
40174
40277
  const method = getInputOrData(this.data, inputs, "method", "string");
40175
40278
  const url = getInputOrData(this.data, inputs, "url", "string");
@@ -40206,13 +40309,15 @@ var HttpCallNodeImpl = class extends NodeImpl {
40206
40309
  }
40207
40310
  const performRequest = async () => {
40208
40311
  try {
40209
- return await fetch(url, {
40312
+ const response2 = await fetch(url, {
40210
40313
  method,
40211
40314
  headers,
40212
40315
  body,
40213
40316
  signal: context.signal,
40214
40317
  mode: "cors"
40215
40318
  });
40319
+ ownedResponse = response2;
40320
+ return response2;
40216
40321
  } catch (err) {
40217
40322
  if (isAbortError(err, context.signal)) {
40218
40323
  throw err;
@@ -40242,6 +40347,7 @@ var HttpCallNodeImpl = class extends NodeImpl {
40242
40347
  const repeatTimes = normalizeHttpRetryCount(this.data.retryOnNon200RepeatTimes);
40243
40348
  const cooldownMs = normalizeHttpRetryCooldownMs(this.data.retryOnNon200CooldownMs);
40244
40349
  for (let attempt = 0; response.status !== 200 && attempt < repeatTimes; attempt++) {
40350
+ discardResponse();
40245
40351
  await waitForRetryCooldown(cooldownMs, context.signal);
40246
40352
  response = await performTrackedRequest();
40247
40353
  }
@@ -40260,12 +40366,14 @@ var HttpCallNodeImpl = class extends NodeImpl {
40260
40366
  const output = responseMetadataOutputs;
40261
40367
  if (this.data.isBinaryOutput) {
40262
40368
  const responseBlob = await response.blob();
40369
+ ownedResponse = void 0;
40263
40370
  output["binary"] = {
40264
40371
  type: "binary",
40265
40372
  value: new Uint8Array(await responseBlob.arrayBuffer())
40266
40373
  };
40267
40374
  } else {
40268
40375
  const responseText = await response.text();
40376
+ ownedResponse = void 0;
40269
40377
  output["res_body"] = {
40270
40378
  type: "string",
40271
40379
  value: responseText
@@ -40304,6 +40412,8 @@ var HttpCallNodeImpl = class extends NodeImpl {
40304
40412
  });
40305
40413
  }
40306
40414
  throw error;
40415
+ } finally {
40416
+ discardResponse();
40307
40417
  }
40308
40418
  }
40309
40419
  };