@http-forge/core 0.4.6 → 0.4.7

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.
@@ -66,6 +66,7 @@ export declare class ConfigService implements IConfigService {
66
66
  getSuitesPath(): string;
67
67
  getModulePaths(): string[];
68
68
  getScriptScope(): 'shared' | 'isolated';
69
+ getScriptTimeout(): number;
69
70
  getWorkspacePath(): string;
70
71
  reload(): void;
71
72
  configExists(): boolean;
@@ -44,6 +44,12 @@ export interface ScriptsConfig {
44
44
  * must pass through pm.variables / pm.environment / pm.globals.
45
45
  */
46
46
  scope?: 'shared' | 'isolated';
47
+ /**
48
+ * Per-script timeout in milliseconds (default 5000). Bounds both the synchronous
49
+ * script CPU guard and the async event-loop drain budget (pending setTimeout /
50
+ * un-awaited Promises) that the runner waits on before advancing to the next request.
51
+ */
52
+ timeout?: number;
47
53
  }
48
54
  /**
49
55
  * Runner configuration
@@ -212,6 +218,8 @@ export interface IConfigService {
212
218
  getModulePaths(): string[];
213
219
  /** Get the script scope mode ('shared' default, or 'isolated' for Postman parity) */
214
220
  getScriptScope(): 'shared' | 'isolated';
221
+ /** Get the per-script timeout in milliseconds (default 5000) */
222
+ getScriptTimeout(): number;
215
223
  /** Get the workspace root path */
216
224
  getWorkspacePath(): string;
217
225
  /** Reload configuration from disk */
@@ -37,10 +37,19 @@ export interface VariableResolverConfig {
37
37
  */
38
38
  export declare class VariableResolver {
39
39
  private readonly allVariables;
40
+ /**
41
+ * Maximum nesting depth for recursive {{var}} resolution.
42
+ * Postman-compatible cap that guards against reference cycles
43
+ * (e.g. a -> {{b}}, b -> {{a}}).
44
+ */
45
+ private static readonly MAX_RESOLUTION_DEPTH;
40
46
  constructor(config: VariableResolverConfig);
41
47
  /**
42
48
  * Resolve variables in a string ({{varName}} syntax)
43
- * Uses the full 5-step pipeline with optional escaping for quoted strings
49
+ * Uses the full 5-step pipeline with optional escaping for quoted strings.
50
+ * Nested references — a variable whose value itself contains {{...}} — are
51
+ * resolved recursively (Postman-style) by repeating the substitution until
52
+ * the string stabilizes or the depth cap is reached.
44
53
  */
45
54
  resolveString(str: string, escape?: boolean): string;
46
55
  /**
@@ -48,6 +57,20 @@ export declare class VariableResolver {
48
57
  * Extra variables take highest precedence
49
58
  */
50
59
  resolveStringWithExtra(str: string, extraVariables: Record<string, string>, escape?: boolean): string;
60
+ /**
61
+ * Repeatedly apply a single substitution pass until the result stops changing
62
+ * (fixed point) or the nesting depth cap is hit. This resolves variable values
63
+ * that themselves contain {{...}} references. Undefined tokens are left as-is,
64
+ * so a pass that resolves nothing produces no change and terminates the loop;
65
+ * the depth cap guards against reference cycles (a -> {{b}}, b -> {{a}}).
66
+ */
67
+ private resolveUntilStable;
68
+ /**
69
+ * Single substitution pass over a string using the 5-step pipeline.
70
+ * With escaping enabled, resolved values are escaped for their surrounding
71
+ * quote context.
72
+ */
73
+ private resolveSinglePass;
51
74
  /**
52
75
  * Resolve variables in an object recursively
53
76
  */
@@ -62,6 +62,7 @@ export declare class RequestExecutor {
62
62
  forgeRoot?: string;
63
63
  scriptExecutor?: IScriptExecutor;
64
64
  scriptScope?: 'shared' | 'isolated';
65
+ scriptTimeout?: number;
65
66
  });
66
67
  /**
67
68
  * Execute a request with full pipeline
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Async Drain — Postman-compatible event-loop draining for script sandboxes
3
+ *
4
+ * Postman/Newman keep a script's sandbox alive after its synchronous body returns,
5
+ * pumping the event loop until all pending timers (`setTimeout`/`setInterval`) and
6
+ * microtasks (un-awaited Promises) have settled — bounded by a script timeout. Only
7
+ * then does the runner advance, committing any variable mutations made by those late
8
+ * callbacks so the next request can see them.
9
+ *
10
+ * This module provides an instrumented timer set plus a `drain()` loop that replicates
11
+ * that behavior. The timers wrap the real Node timers, track outstanding handles, and
12
+ * swallow errors thrown inside deferred callbacks (reporting them as non-fatal) so a
13
+ * late failure never rejects the surrounding request — matching Postman, where such
14
+ * callbacks run on a later tick outside the script's try/catch.
15
+ */
16
+ /** Sandbox-facing timer functions, drop-in replacements for the Node globals. */
17
+ export interface SandboxTimers {
18
+ setTimeout: (handler: (...args: any[]) => void, timeout?: number, ...args: any[]) => any;
19
+ clearTimeout: (handle: any) => void;
20
+ setInterval: (handler: (...args: any[]) => void, timeout?: number, ...args: any[]) => any;
21
+ clearInterval: (handle: any) => void;
22
+ }
23
+ /** Result of a drain operation. */
24
+ export interface DrainResult {
25
+ /** True if the timeout cap was hit while timers were still pending. */
26
+ timedOut: boolean;
27
+ /** Approximate wall-clock time spent draining, in milliseconds. */
28
+ elapsedMs: number;
29
+ }
30
+ /**
31
+ * A registry of instrumented timers bound to a single script session.
32
+ */
33
+ export interface TimerRegistry {
34
+ /** Timer functions to expose inside the VM sandbox. */
35
+ readonly timers: SandboxTimers;
36
+ /** Number of timers still pending (not yet fired or cleared, or active intervals). */
37
+ pendingCount(): number;
38
+ /** Cancel every outstanding timer. Used at the drain cap and on dispose. */
39
+ clearAll(): void;
40
+ /**
41
+ * Pump the event loop until no timers are pending (and a final microtask flush
42
+ * yields nothing new) or until `timeoutMs` of wall-clock time elapses, whichever
43
+ * comes first. On timeout, all remaining timers are force-cleared.
44
+ */
45
+ drain(timeoutMs: number): Promise<DrainResult>;
46
+ }
47
+ /**
48
+ * Create a timer registry whose deferred-callback errors are routed to `onTimerError`.
49
+ *
50
+ * @param onTimerError Invoked (never throws) when a timer callback throws. The error is
51
+ * swallowed relative to the request so the run continues.
52
+ */
53
+ export declare function createTimerRegistry(onTimerError: (error: unknown) => void): TimerRegistry;
@@ -8,16 +8,22 @@
8
8
  * This matches Postman's behavior for per-request script execution.
9
9
  */
10
10
  import * as vm from 'vm';
11
+ import { SandboxTimers } from './async-drain';
11
12
  import { CommonScriptContext, IRequestScriptSession, PostResponseScriptResult, PreRequestScriptContext, PreRequestScriptResult, ResponseContext } from './interfaces';
12
13
  /**
13
14
  * Dependencies injected from ScriptExecutor
14
15
  * Interface Segregation: Only the methods needed by the session
15
16
  */
16
17
  export interface SessionDependencies {
17
- createVM: (ctx: any, scriptConsole: any) => vm.Context;
18
+ createVM: (ctx: any, scriptConsole: any, timers?: SandboxTimers) => vm.Context;
18
19
  createCommonContext: (context: CommonScriptContext, eventName: 'prerequest' | 'test') => any;
19
20
  /** When true, each script level runs in its own scope (Postman-compatible). */
20
21
  isolateScripts?: boolean;
22
+ /**
23
+ * Per-script timeout in milliseconds. Bounds BOTH the synchronous VM CPU guard and
24
+ * the async event-loop drain budget (pending timers / un-awaited Promises).
25
+ */
26
+ scriptTimeoutMs?: number;
21
27
  }
22
28
  /**
23
29
  * Request Script Session Implementation
@@ -43,6 +49,7 @@ export declare class RequestScriptSession implements IRequestScriptSession {
43
49
  private _nextRequest;
44
50
  private _skipRequest;
45
51
  private _visualizerData;
52
+ private readonly timerRegistry;
46
53
  constructor(deps: SessionDependencies, initialContext: PreRequestScriptContext);
47
54
  /**
48
55
  * Initialize the shared VM context
@@ -23,8 +23,9 @@ export declare class ScriptExecutor implements IScriptExecutor {
23
23
  private readonly httpService?;
24
24
  private readonly secretRegistry?;
25
25
  private readonly scopeMode;
26
+ private readonly scriptTimeoutMs;
26
27
  private readonly moduleLoader;
27
- constructor(httpService?: IHttpRequestService | undefined, modulePaths?: string[], secretRegistry?: SecretResolverRegistry | undefined, scopeMode?: 'shared' | 'isolated');
28
+ constructor(httpService?: IHttpRequestService | undefined, modulePaths?: string[], secretRegistry?: SecretResolverRegistry | undefined, scopeMode?: 'shared' | 'isolated', scriptTimeoutMs?: number);
28
29
  /**
29
30
  * Create a request execution session
30
31
  * Factory method implementing IScriptExecutor
@@ -33,6 +34,10 @@ export declare class ScriptExecutor implements IScriptExecutor {
33
34
  /**
34
35
  * Create VM context with sandbox configuration
35
36
  * Returns a context object that can be used with vm.runInContext()
37
+ *
38
+ * @param timers Optional instrumented timer set. When provided, the sandbox uses
39
+ * these instead of the Node globals so the session can track and
40
+ * drain pending timers (Postman-compatible async behavior).
36
41
  */
37
42
  private createVM;
38
43
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@http-forge/core",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "description": "Headless HTTP testing engine with Postman collection support, dynamic variables, and script-based automation.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",