@use-tusk/drift-node-sdk 0.1.0 → 0.1.4

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/README.md CHANGED
@@ -3,8 +3,9 @@
3
3
  </p>
4
4
 
5
5
  <p align="center">
6
+ <a href="https://www.npmjs.com/package/@use-tusk/drift-node-sdk"><img src="https://img.shields.io/npm/v/@use-tusk/drift-node-sdk" alt="npm version"></a>
6
7
  <a href="https://opensource.org/licenses/Apache-2.0"><img src="https://img.shields.io/badge/License-Apache_2.0-blue.svg" alt="License: Apache 2.0"></a>
7
- <a href="https://github.com/Use-Tusk/tusk-drift-sdk/commits/main"><img src="https://img.shields.io/github/last-commit/Use-Tusk/tusk-drift-sdk" alt="GitHub last commit"></a>
8
+ <a href="https://github.com/Use-Tusk/drift-node-sdk/commits/main/"><img src="https://img.shields.io/github/last-commit/Use-Tusk/drift-node-sdk" alt="GitHub last commit"></a>
8
9
  </p>
9
10
 
10
11
  The Node.js Tusk Drift SDK enables fast and deterministic API testing by capturing and replaying API calls made to/from your service. Automatically record real-world API calls, then replay them as tests using the [Tusk CLI](https://github.com/Use-Tusk/tusk-drift-cli) to find regressions. During replay, all outbound requests are intercepted with recorded data to ensure consistent behavior without side-effects.
@@ -49,28 +50,57 @@ npm install @use-tusk/drift-node-sdk
49
50
  Before setting up the SDK, ensure you have:
50
51
 
51
52
  - Completed the [CLI wizard](https://github.com/Use-Tusk/tusk-drift-cli?tab=readme-ov-file#quick-start)
52
- - Obtained an API key from the [Tusk Drift dashboard](https://usetusk.ai/app/settings/api-keysgoogle.com)
53
+ - Obtained an API key from the [Tusk Drift dashboard](https://usetusk.ai/app/settings/api-keys) (only required if using Tusk Cloud)
53
54
 
54
55
  Follow these steps in order to properly initialize the Tusk Drift SDK:
55
56
 
56
57
  ### 1. Create SDK Initialization File
57
58
 
58
- Create a separate file (e.g. `tdInit.ts`) to initialize the Tusk Drift SDK. This ensures the SDK is initialized as early as possible before any other modules are loaded.
59
+ Create a separate file (e.g. `tuskDriftInit.ts`) to initialize the Tusk Drift SDK. This ensures the SDK is initialized as early as possible before any other modules are loaded.
60
+
61
+ **IMPORTANT**: Ensure that `TuskDrift` is initialized before any other telemetry providers (e.g. OpenTelemetry, Sentry, etc.). If not, your existing telemetry may not work properly.
62
+
63
+ #### For CommonJS Applications
59
64
 
60
65
  ```typescript
61
- // tdInit.ts
66
+ // tuskDriftInit.ts
62
67
  import { TuskDrift } from "@use-tusk/drift-node-sdk";
63
68
 
64
69
  // Initialize SDK immediately
65
70
  TuskDrift.initialize({
66
71
  apiKey: process.env.TUSK_DRIFT_API_KEY,
67
- env: process.env.ENV,
68
- logLevel: process.env.TUSK_DRIFT_LOG_LEVEL,
72
+ env: process.env.NODE_ENV,
69
73
  });
70
74
 
71
75
  export { TuskDrift };
72
76
  ```
73
77
 
78
+ #### For ESM Applications
79
+
80
+ ESM applications require additional setup to properly intercept module imports:
81
+
82
+ ```typescript
83
+ // tuskDriftInit.ts
84
+ import { register } from 'node:module';
85
+ import { pathToFileURL } from 'node:url';
86
+
87
+ // Register the ESM loader
88
+ // This enables interception of ESM module imports
89
+ register('@use-tusk/drift-node-sdk/hook.mjs', pathToFileURL('./'));
90
+
91
+ import { TuskDrift } from "@use-tusk/drift-node-sdk";
92
+
93
+ // Initialize SDK immediately
94
+ TuskDrift.initialize({
95
+ apiKey: process.env.TUSK_DRIFT_API_KEY,
96
+ env: process.env.NODE_ENV,
97
+ });
98
+
99
+ export { TuskDrift };
100
+ ```
101
+
102
+ **Why the ESM loader is needed**: ESM imports are statically analyzed and hoisted, meaning all imports are resolved before any code runs. The `register()` call sets up Node.js loader hooks that intercept module imports, allowing the SDK to instrument packages like `postgres`, `http`, etc. Without this, the SDK cannot patch ESM modules.
103
+
74
104
  #### Configuration Options
75
105
 
76
106
  <table>
@@ -86,13 +116,13 @@ export { TuskDrift };
86
116
  <tr>
87
117
  <td><code>apiKey</code></td>
88
118
  <td><code>string</code></td>
89
- <td><b>Required.</b></td>
119
+ <td><b>Required if using Tusk Cloud</b></td>
90
120
  <td>Your Tusk Drift API key.</td>
91
121
  </tr>
92
122
  <tr>
93
123
  <td><code>env</code></td>
94
124
  <td><code>string</code></td>
95
- <td><b>Required.</b></td>
125
+ <td><code>process.env.NODE_ENV</code></td>
96
126
  <td>The environment name.</td>
97
127
  </tr>
98
128
  <tr>
@@ -106,18 +136,37 @@ export { TuskDrift };
106
136
 
107
137
  ### 2. Import SDK at Application Entry Point
108
138
 
109
- In your main server file (e.g., `server.ts`, `index.ts`, `app.ts`), import the initialized SDK **at the very top**, before any other imports:
139
+ #### For CommonJS Applications
140
+
141
+ In your main server file (e.g., `server.ts`, `index.ts`, `app.ts`), require the initialized SDK **at the very top**, before any other requires:
110
142
 
111
143
  ```typescript
112
144
  // server.ts
113
- import { TuskDrift } from "./tdInit"; // MUST be the first import
145
+ import { TuskDrift } from "./tuskDriftInit"; // MUST be the first import
114
146
 
115
147
  // ... other imports ...
116
148
 
117
149
  // Your application setup...
118
150
  ```
119
151
 
120
- > **IMPORTANT**: Ensure NO require/import calls are made before importing the SDK initialization file. This guarantees proper instrumentation of all dependencies.
152
+ > **IMPORTANT**: Ensure NO require calls are made before requiring the SDK initialization file. This guarantees proper instrumentation of all dependencies.
153
+
154
+ #### For ESM Applications
155
+
156
+ For ESM applications, you **cannot** control import order within your application code because all imports are hoisted. Instead, use the `--import` flag:
157
+
158
+ **Update your package.json scripts**:
159
+
160
+ ```json
161
+ {
162
+ "scripts": {
163
+ "dev": "node --import ./dist/tuskDriftInit.js dist/server.js",
164
+ "dev:record": "TUSK_DRIFT_MODE=RECORD node --import ./dist/tuskDriftInit.js dist/server.js"
165
+ }
166
+ }
167
+ ```
168
+
169
+ **Why `--import` is required for ESM**: In ESM, all `import` statements are hoisted and evaluated before any code runs, making it impossible to control import order within a file. The `--import` flag ensures the SDK initialization (including loader registration) happens in a separate phase before your application code loads, guaranteeing proper module interception.
121
170
 
122
171
  ### 3. Update Configuration File
123
172
 
@@ -165,13 +214,13 @@ recording:
165
214
  </tbody>
166
215
  </table>
167
216
 
168
- ### 3. Mark App as Ready
217
+ ### 4. Mark App as Ready
169
218
 
170
219
  Once your application has completed initialization (database connections, middleware setup, etc.), mark it as ready:
171
220
 
172
221
  ```typescript
173
222
  // server.ts
174
- import { TuskDrift } from "./tdInit";
223
+ import { TuskDrift } from "./tuskDriftInit";
175
224
 
176
225
  // ... other imports ...
177
226
 
@@ -259,7 +308,11 @@ You should see output similar to:
259
308
 
260
309
  1. **Check sampling rate**: Ensure `sampling_rate` in `.tusk/config.yaml` is 1.0
261
310
  2. **Verify app readiness**: Make sure you're calling `TuskDrift.markAppAsReady()`
262
- 3. **Check endpoint paths**: Ensure your endpoint isn't in `exclude_paths`
311
+ 3. **Use debug mode in SDK**: Add `logLevel: 'debug'` to the initialization parameters
312
+
313
+ #### Existing telemetry not working
314
+
315
+ Ensure that `TuskDrift.initialize()` is called before any other telemetry providers (e.g. OpenTelemetry, Sentry, etc.).
263
316
 
264
317
  #### Replay failures
265
318