@bitfab/sdk 0.38.4 → 0.38.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
@@ -42,7 +42,7 @@ var __version__, __packageName__;
42
42
  var init_version_generated = __esm({
43
43
  "src/version.generated.ts"() {
44
44
  "use strict";
45
- __version__ = "0.38.4";
45
+ __version__ = "0.38.5";
46
46
  __packageName__ = "@bitfab/sdk";
47
47
  }
48
48
  });
@@ -2187,11 +2187,16 @@ function normalizeMockOverrides(mockOverride) {
2187
2187
  if (mockOverride === void 0) {
2188
2188
  return [];
2189
2189
  }
2190
- return Array.isArray(mockOverride) ? mockOverride : [mockOverride];
2190
+ const overrides = Array.isArray(mockOverride) ? mockOverride : [mockOverride];
2191
+ return overrides.map(
2192
+ (override) => typeof override === "function" ? { match: () => true, value: override } : override
2193
+ );
2191
2194
  }
2195
+ var NO_MOCK_OVERRIDE;
2192
2196
  var init_mockOverride = __esm({
2193
2197
  "src/mockOverride.ts"() {
2194
2198
  "use strict";
2199
+ NO_MOCK_OVERRIDE = /* @__PURE__ */ Symbol("bitfab.noMockOverride");
2195
2200
  }
2196
2201
  });
2197
2202
 
@@ -3133,6 +3138,7 @@ __export(index_exports, {
3133
3138
  DEFAULT_SERVICE_URL: () => DEFAULT_SERVICE_URL,
3134
3139
  DbBranchReplayError: () => DbBranchReplayError,
3135
3140
  HttpClient: () => HttpClient,
3141
+ NO_MOCK_OVERRIDE: () => NO_MOCK_OVERRIDE,
3136
3142
  ReplayError: () => ReplayError,
3137
3143
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
3138
3144
  __version__: () => __version__,
@@ -6555,6 +6561,49 @@ var Bitfab = class {
6555
6561
  } catch {
6556
6562
  }
6557
6563
  };
6564
+ const recordSpan = (result) => {
6565
+ if (options.finalize) {
6566
+ void self.httpClient.trackDeferred(
6567
+ Promise.resolve().then(() => options.finalize(result)).then((output) => sendSpan({ result: output })).catch(
6568
+ (error) => sendSpan({
6569
+ result: void 0,
6570
+ error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
6571
+ })
6572
+ )
6573
+ );
6574
+ } else {
6575
+ void sendSpan({ result });
6576
+ }
6577
+ };
6578
+ executeWithContext = () => {
6579
+ let result;
6580
+ try {
6581
+ result = fn.apply(this, args);
6582
+ } catch (error) {
6583
+ void sendSpan({
6584
+ result: void 0,
6585
+ error: error instanceof Error ? error.message : String(error)
6586
+ });
6587
+ throw error;
6588
+ }
6589
+ if (result instanceof Promise) {
6590
+ return result.then((resolvedResult) => {
6591
+ recordSpan(resolvedResult);
6592
+ return resolvedResult;
6593
+ }).catch((error) => {
6594
+ void sendSpan({
6595
+ result: void 0,
6596
+ error: error instanceof Error ? error.message : String(error)
6597
+ });
6598
+ throw error;
6599
+ });
6600
+ }
6601
+ if (isAsyncGenerator(result)) {
6602
+ return wrapAsyncGenerator(result, newStack, sendSpan);
6603
+ }
6604
+ recordSpan(result);
6605
+ return result;
6606
+ };
6558
6607
  const replayCtxForMock = getReplayContext();
6559
6608
  if (replayCtxForMock?.mockTree && !isRootSpan) {
6560
6609
  const counters = replayCtxForMock.callCounters;
@@ -6613,6 +6662,7 @@ var Bitfab = class {
6613
6662
  }
6614
6663
  return output;
6615
6664
  };
6665
+ const shouldMockWithBaseStrategy = replayCtxForMock.mockStrategy === "all" || replayCtxForMock.mockStrategy === "marked" && options.mockOnReplay === true;
6616
6666
  if (replayCtxForMock.mockOverrides?.length) {
6617
6667
  const nodeMeta = {
6618
6668
  traceFunctionKey,
@@ -6620,28 +6670,75 @@ var Bitfab = class {
6620
6670
  type: options.type ?? "custom",
6621
6671
  originalSpanId: mockSpan?.sourceSpanId
6622
6672
  };
6623
- const override = replayCtxForMock.mockOverrides.find(
6624
- (o) => o.match(nodeMeta)
6625
- );
6626
- if (override) {
6627
- const injected = resolveMockValue(override.value, {
6628
- node: nodeMeta,
6629
- inputs: args,
6630
- getOriginalOutput: () => Promise.resolve(resolveRecordedOutput())
6631
- });
6632
- if (injected instanceof Promise) {
6633
- return emitMockAsync(injected, "override");
6673
+ const overrideCtx = {
6674
+ node: nodeMeta,
6675
+ inputs: args,
6676
+ getOriginalOutput: () => Promise.resolve(resolveRecordedOutput())
6677
+ };
6678
+ const resolveOverrideFrom = (startIndex) => {
6679
+ for (let index = startIndex; index < replayCtxForMock.mockOverrides.length; index += 1) {
6680
+ const override = replayCtxForMock.mockOverrides[index];
6681
+ if (!override?.match(nodeMeta)) {
6682
+ continue;
6683
+ }
6684
+ const injected = resolveMockValue(override.value, overrideCtx);
6685
+ if (injected instanceof Promise) {
6686
+ return injected.then(
6687
+ (output) => output === NO_MOCK_OVERRIDE ? resolveOverrideFrom(index + 1) : { matched: true, output }
6688
+ );
6689
+ }
6690
+ if (injected !== NO_MOCK_OVERRIDE) {
6691
+ return { matched: true, output: injected };
6692
+ }
6634
6693
  }
6635
- return emitMock(injected, "override");
6694
+ return { matched: false };
6695
+ };
6696
+ const resolution = resolveOverrideFrom(0);
6697
+ if (resolution instanceof Promise) {
6698
+ if (!fnReturnsPromise) {
6699
+ throw new BitfabError(
6700
+ `Cannot resolve an asynchronous mock override for synchronous span "${traceFunctionKey}". Make the wrapped function async or return NO_MOCK_OVERRIDE synchronously.`
6701
+ );
6702
+ }
6703
+ return runWithSpanStack(newStack, async () => {
6704
+ const resolved = await resolution;
6705
+ if (resolved.matched) {
6706
+ void sendSpan({
6707
+ result: resolved.output,
6708
+ mocked: true,
6709
+ mockTarget: "output",
6710
+ mockSource: "override"
6711
+ });
6712
+ return resolved.output;
6713
+ }
6714
+ if (shouldMockWithBaseStrategy && !mockSpan) {
6715
+ throw new BitfabError(
6716
+ `Replay selected span "${traceFunctionKey}:${baseSpanParams.spanName}" for mocking, but recorded occurrence ${callIndex + 1} is unavailable. The real span was not executed.`
6717
+ );
6718
+ }
6719
+ if (shouldMockWithBaseStrategy) {
6720
+ const output = await resolveRecordedOutput();
6721
+ void sendSpan({
6722
+ result: output,
6723
+ mocked: true,
6724
+ mockTarget: "output",
6725
+ mockSource: "recorded"
6726
+ });
6727
+ return output;
6728
+ }
6729
+ return executeWithContext();
6730
+ });
6731
+ }
6732
+ if (resolution.matched) {
6733
+ return emitMock(resolution.output, "override");
6636
6734
  }
6637
6735
  }
6638
- const shouldMock = replayCtxForMock.mockStrategy === "all" || replayCtxForMock.mockStrategy === "marked" && options.mockOnReplay === true;
6639
- if (shouldMock && !mockSpan) {
6736
+ if (shouldMockWithBaseStrategy && !mockSpan) {
6640
6737
  throw new BitfabError(
6641
6738
  `Replay selected span "${traceFunctionKey}:${baseSpanParams.spanName}" for mocking, but recorded occurrence ${callIndex + 1} is unavailable. The real span was not executed.`
6642
6739
  );
6643
6740
  }
6644
- if (shouldMock) {
6741
+ if (shouldMockWithBaseStrategy) {
6645
6742
  const recorded = resolveRecordedOutput();
6646
6743
  if (recorded instanceof Promise) {
6647
6744
  return emitMockAsync(recorded, "recorded");
@@ -6649,49 +6746,6 @@ var Bitfab = class {
6649
6746
  return emitMock(recorded, "recorded");
6650
6747
  }
6651
6748
  }
6652
- const recordSpan = (result) => {
6653
- if (options.finalize) {
6654
- void self.httpClient.trackDeferred(
6655
- Promise.resolve().then(() => options.finalize(result)).then((output) => sendSpan({ result: output })).catch(
6656
- (error) => sendSpan({
6657
- result: void 0,
6658
- error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
6659
- })
6660
- )
6661
- );
6662
- } else {
6663
- void sendSpan({ result });
6664
- }
6665
- };
6666
- executeWithContext = () => {
6667
- let result;
6668
- try {
6669
- result = fn.apply(this, args);
6670
- } catch (error) {
6671
- void sendSpan({
6672
- result: void 0,
6673
- error: error instanceof Error ? error.message : String(error)
6674
- });
6675
- throw error;
6676
- }
6677
- if (result instanceof Promise) {
6678
- return result.then((resolvedResult) => {
6679
- recordSpan(resolvedResult);
6680
- return resolvedResult;
6681
- }).catch((error) => {
6682
- void sendSpan({
6683
- result: void 0,
6684
- error: error instanceof Error ? error.message : String(error)
6685
- });
6686
- throw error;
6687
- });
6688
- }
6689
- if (isAsyncGenerator(result)) {
6690
- return wrapAsyncGenerator(result, newStack, sendSpan);
6691
- }
6692
- recordSpan(result);
6693
- return result;
6694
- };
6695
6749
  } catch (setupError) {
6696
6750
  if (registeredTraceId) {
6697
6751
  activeTraceStates.delete(registeredTraceId);
@@ -6978,8 +7032,40 @@ var Bitfab = class {
6978
7032
  ...params.mockSource && { mockSource: params.mockSource }
6979
7033
  });
6980
7034
  }
6981
- registerMockOverride(overrideOrMatch, value) {
6982
- const override = typeof overrideOrMatch === "function" ? { match: overrideOrMatch, value } : overrideOrMatch;
7035
+ registerMockOverride(overrideOrResolverOrMatch, ...values) {
7036
+ let override;
7037
+ if (typeof overrideOrResolverOrMatch === "string") {
7038
+ const keyedOverride = values[0];
7039
+ if (values.length !== 1 || keyedOverride === void 0) {
7040
+ throw new BitfabError(
7041
+ "registerMockOverride(traceFunctionKey, override) requires a resolver function or { match, value } override."
7042
+ );
7043
+ }
7044
+ if (typeof keyedOverride === "function") {
7045
+ override = {
7046
+ match: (node) => node.traceFunctionKey === overrideOrResolverOrMatch,
7047
+ value: keyedOverride
7048
+ };
7049
+ } else if (typeof keyedOverride === "object" && keyedOverride !== null && "match" in keyedOverride && "value" in keyedOverride) {
7050
+ override = {
7051
+ match: (node) => node.traceFunctionKey === overrideOrResolverOrMatch && keyedOverride.match(node),
7052
+ value: keyedOverride.value
7053
+ };
7054
+ } else {
7055
+ throw new BitfabError(
7056
+ "registerMockOverride(traceFunctionKey, override) requires a resolver function or { match, value } override."
7057
+ );
7058
+ }
7059
+ } else if (typeof overrideOrResolverOrMatch !== "function") {
7060
+ override = overrideOrResolverOrMatch;
7061
+ } else if (values.length === 0) {
7062
+ override = { match: () => true, value: overrideOrResolverOrMatch };
7063
+ } else {
7064
+ override = {
7065
+ match: overrideOrResolverOrMatch,
7066
+ value: values[0]
7067
+ };
7068
+ }
6983
7069
  this.mockOverrides.push(override);
6984
7070
  }
6985
7071
  /** Remove all overrides registered via {@link registerMockOverride}. */
@@ -7229,6 +7315,7 @@ var finalizers = {
7229
7315
 
7230
7316
  // src/index.ts
7231
7317
  init_http();
7318
+ init_mockOverride();
7232
7319
  init_replay();
7233
7320
 
7234
7321
  // src/replayRegistry.ts
@@ -7252,6 +7339,7 @@ function defineReplayRegistry(registry) {
7252
7339
  DEFAULT_SERVICE_URL,
7253
7340
  DbBranchReplayError,
7254
7341
  HttpClient,
7342
+ NO_MOCK_OVERRIDE,
7255
7343
  ReplayError,
7256
7344
  SUPPORTED_PROVIDERS,
7257
7345
  __version__,