@reproapp/node-sdk 0.0.27 → 0.0.28
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.js +1 -4
- package/package.json +1 -1
- package/src/index.ts +1 -4
- package/test/mongodb-integration.test.js +57 -0
package/dist/index.js
CHANGED
|
@@ -3997,6 +3997,7 @@ async function maybeCaptureDbValueAsync(params, sink) {
|
|
|
3997
3997
|
}
|
|
3998
3998
|
function reproMiddleware(cfg) {
|
|
3999
3999
|
const masking = normalizeMaskingConfig(cfg.masking);
|
|
4000
|
+
patchMongoDb(cfg);
|
|
4000
4001
|
return async function (req, res, next) {
|
|
4001
4002
|
const querySid = readRequestQueryParam(req, REPRO_QUERY_SESSION_PARAM);
|
|
4002
4003
|
const queryAid = readRequestQueryParam(req, REPRO_QUERY_ACTION_PARAM);
|
|
@@ -7979,10 +7980,6 @@ function patchMongoDb(cfg) {
|
|
|
7979
7980
|
mongoDbPatchConfigs.push(cfg);
|
|
7980
7981
|
installMongoDbRequireHook();
|
|
7981
7982
|
const mongoModules = new Set();
|
|
7982
|
-
const resolvedMongoModule = requireOptionalHostModule('mongodb');
|
|
7983
|
-
if (resolvedMongoModule) {
|
|
7984
|
-
mongoModules.add(resolvedMongoModule);
|
|
7985
|
-
}
|
|
7986
7983
|
for (const loadedMongoModule of getLoadedModuleExportsByPackage('mongodb')) {
|
|
7987
7984
|
mongoModules.add(loadedMongoModule);
|
|
7988
7985
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4892,6 +4892,7 @@ export type ReproInitConfig = ReproMiddlewareConfig & {
|
|
|
4892
4892
|
|
|
4893
4893
|
export function reproMiddleware(cfg: ReproMiddlewareConfig) {
|
|
4894
4894
|
const masking = normalizeMaskingConfig(cfg.masking);
|
|
4895
|
+
patchMongoDb(cfg);
|
|
4895
4896
|
return async function (req: Request, res: Response, next: NextFunction) {
|
|
4896
4897
|
const querySid = readRequestQueryParam(req, REPRO_QUERY_SESSION_PARAM);
|
|
4897
4898
|
const queryAid = readRequestQueryParam(req, REPRO_QUERY_ACTION_PARAM);
|
|
@@ -9225,10 +9226,6 @@ export function patchMongoDb(cfg: MongoDbPatchConfig): void {
|
|
|
9225
9226
|
installMongoDbRequireHook();
|
|
9226
9227
|
|
|
9227
9228
|
const mongoModules = new Set<any>();
|
|
9228
|
-
const resolvedMongoModule = requireOptionalHostModule('mongodb');
|
|
9229
|
-
if (resolvedMongoModule) {
|
|
9230
|
-
mongoModules.add(resolvedMongoModule);
|
|
9231
|
-
}
|
|
9232
9229
|
for (const loadedMongoModule of getLoadedModuleExportsByPackage('mongodb')) {
|
|
9233
9230
|
mongoModules.add(loadedMongoModule);
|
|
9234
9231
|
}
|
|
@@ -7,6 +7,16 @@ process.env.TRACE_LINGER_AFTER_FINISH_MS = '0';
|
|
|
7
7
|
|
|
8
8
|
const assert = require('assert');
|
|
9
9
|
const { EventEmitter } = require('events');
|
|
10
|
+
const mongodb = require('mongodb');
|
|
11
|
+
|
|
12
|
+
const originalNativeMongoInsertOne = mongodb.Collection.prototype.insertOne;
|
|
13
|
+
|
|
14
|
+
mongodb.Collection.prototype.insertOne = async function reproFakeNativeMongoInsertOne(doc, options) {
|
|
15
|
+
assert.strictEqual(options.session.id, 'native-real-session-1');
|
|
16
|
+
doc._id = doc._id ?? 'subject-real-1';
|
|
17
|
+
return { acknowledged: true, insertedId: doc._id };
|
|
18
|
+
};
|
|
19
|
+
|
|
10
20
|
const { flushIngestQueue } = require('../dist/ingest/client');
|
|
11
21
|
const { reproMiddleware, __reproTestHooks } = require('../dist');
|
|
12
22
|
|
|
@@ -122,6 +132,50 @@ function dbChanges(events) {
|
|
|
122
132
|
.flatMap((event) => Array.isArray(event.payload?.db) ? event.payload.db : []);
|
|
123
133
|
}
|
|
124
134
|
|
|
135
|
+
async function testReproMiddlewarePatchesPreloadedNativeMongoClientInsertOne() {
|
|
136
|
+
await withCapturedFetch(async (capturedBodies) => {
|
|
137
|
+
__reproTestHooks.resetSdkBackgroundQueuesForTest();
|
|
138
|
+
const sessionId = 'S_mongo_real_client_insert';
|
|
139
|
+
const actionId = 'A_mongo_real_client_insert';
|
|
140
|
+
const cfg = makeConfig(sessionId, actionId);
|
|
141
|
+
|
|
142
|
+
const collection = new mongodb.MongoClient('mongodb://127.0.0.1:27017/clinical')
|
|
143
|
+
.db('clinical')
|
|
144
|
+
.collection('subjects');
|
|
145
|
+
|
|
146
|
+
await runCapturedRequest(cfg, sessionId, actionId, async () => {
|
|
147
|
+
const result = await collection.insertOne(
|
|
148
|
+
{ subjectNumber: '10013038', status: 'screened' },
|
|
149
|
+
{
|
|
150
|
+
session: {
|
|
151
|
+
id: 'native-real-session-1',
|
|
152
|
+
hasEnded: false,
|
|
153
|
+
inTransaction: () => true,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
assert.deepStrictEqual(result, { acknowledged: true, insertedId: 'subject-real-1' });
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
await waitFor(() => dbChanges(flattenEvents(capturedBodies)).length === 1);
|
|
161
|
+
await drainSdk();
|
|
162
|
+
|
|
163
|
+
const [change] = dbChanges(flattenEvents(capturedBodies));
|
|
164
|
+
assert.strictEqual(change.type, 'mongo');
|
|
165
|
+
assert.strictEqual(change.collection, 'subjects');
|
|
166
|
+
assert.strictEqual(change.op, 'insertOne');
|
|
167
|
+
assert.strictEqual(change.pk._id, 'subject-real-1');
|
|
168
|
+
assert.strictEqual(change.query.driver, 'mongodb');
|
|
169
|
+
assert.strictEqual(change.query.db, 'clinical');
|
|
170
|
+
assert.strictEqual(change.query.doc.subjectNumber, '10013038');
|
|
171
|
+
assert.strictEqual(change.query.options.session.__kind, 'mongodb_client_session');
|
|
172
|
+
assert.strictEqual(change.query.options.session.id, 'native-real-session-1');
|
|
173
|
+
assert.strictEqual(change.after.subjectNumber, '10013038');
|
|
174
|
+
assert.strictEqual(change.resultMeta.insertedId, 'subject-real-1');
|
|
175
|
+
assert.strictEqual(change.resultMeta.acknowledged, true);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
125
179
|
async function testNativeInsertOneIsCaptured() {
|
|
126
180
|
await withCapturedFetch(async (capturedBodies) => {
|
|
127
181
|
__reproTestHooks.resetSdkBackgroundQueuesForTest();
|
|
@@ -321,6 +375,7 @@ async function testNativeOperationFailureIsCapturedAndRethrown() {
|
|
|
321
375
|
}
|
|
322
376
|
|
|
323
377
|
(async () => {
|
|
378
|
+
await testReproMiddlewarePatchesPreloadedNativeMongoClientInsertOne();
|
|
324
379
|
await testNativeInsertOneIsCaptured();
|
|
325
380
|
await testNativeUpdateOneUpsertIsCaptured();
|
|
326
381
|
await testNativeFindCursorToArrayIsCaptured();
|
|
@@ -329,4 +384,6 @@ async function testNativeOperationFailureIsCapturedAndRethrown() {
|
|
|
329
384
|
})().catch((err) => {
|
|
330
385
|
console.error(err);
|
|
331
386
|
process.exit(1);
|
|
387
|
+
}).finally(() => {
|
|
388
|
+
mongodb.Collection.prototype.insertOne = originalNativeMongoInsertOne;
|
|
332
389
|
});
|