forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
571 lines (498 loc) · 15.6 KB
/
utils.ts
File metadata and controls
571 lines (498 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import * as child_process from 'node:child_process';
import { on, once } from 'node:events';
import * as fs from 'node:fs/promises';
import { tmpdir } from 'node:os';
import * as path from 'node:path';
import { EJSON } from 'bson';
import * as BSON from 'bson';
import { expect } from 'chai';
import { Readable } from 'stream';
import { setTimeout } from 'timers';
import { inspect, promisify } from 'util';
import {
type AnyClientBulkWriteModel,
type Document,
type HostAddress,
MongoClient,
type MongoClientOptions,
type TopologyOptions
} from '../../src';
import { OP_MSG } from '../../src/cmap/wire_protocol/constants';
import { Topology } from '../../src/sdam/topology';
import { processTimeMS } from '../../src/utils';
import { type TestConfiguration } from './runner/config';
export function ensureCalledWith(stub: any, args: any[]) {
args.forEach((m: any) => expect(stub).to.have.been.calledWith(m));
}
export class EventCollector {
private _events: Record<string, any[]>;
private _timeout: number;
constructor(
obj: { on: (arg0: any, arg1: (event: any) => number) => void },
events: any[],
options?: { timeout: number }
) {
this._events = Object.create(null);
this._timeout = options ? options.timeout : 5000;
events.forEach((eventName: string | number) => {
this._events[eventName] = [];
obj.on(eventName, (event: any) => this._events[eventName].push(event));
});
}
waitForEvent(eventName: string, callback: (error?: Error, events?: any[]) => void): void;
waitForEvent(
eventName: string,
count: number,
callback: (error?: Error, events?: any[]) => void
): void;
waitForEvent(
eventName: string,
count: number | ((error?: Error, events?: any[]) => void),
callback?: (error?: Error, events?: any[]) => void
): void {
if (typeof count === 'function') {
callback = count;
count = 1;
}
this.waitForEventImpl(this, Date.now(), eventName, count, callback);
}
reset(eventName: string) {
if (eventName == null) {
Object.keys(this._events).forEach(eventName => {
this._events[eventName] = [];
});
return;
}
if (this._events[eventName] == null) {
throw new TypeError(`invalid event name "${eventName}" specified for reset`);
}
this._events[eventName] = [];
}
waitForEventImpl(
collector: this,
start: number,
eventName: string | number,
count: number,
callback: (error?: Error, events?: any[]) => void
) {
const events = collector._events[eventName];
if (events.length >= count) {
return callback(undefined, events);
}
if (Date.now() - start >= collector._timeout) {
return callback(new Error(`timed out waiting for event "${eventName}"`));
}
setTimeout(() => this.waitForEventImpl(collector, start, eventName, count, callback), 10);
}
}
export function getEncryptExtraOptions(): MongoClientOptions['autoEncryption']['extraOptions'] {
if (
typeof process.env.CRYPT_SHARED_LIB_PATH === 'string' &&
process.env.CRYPT_SHARED_LIB_PATH.length > 0
) {
return { cryptSharedLibPath: process.env.CRYPT_SHARED_LIB_PATH };
}
return {};
}
export function getEnvironmentalOptions() {
const options = {};
if (process.env.MONGODB_API_VERSION) {
Object.assign(options, {
serverApi: { version: process.env.MONGODB_API_VERSION }
});
}
return options;
}
/**
* Use as a template string tag to stringify objects in the template string
* Attempts to use EJSON (to make type information obvious)
* falls back to util.inspect if there's an error (circular reference)
*/
export function ejson(strings: TemplateStringsArray, ...values: any[]) {
const stringParts = [strings[0]];
for (const [idx, value] of values.entries()) {
if (typeof value === 'object') {
let stringifiedObject: string;
try {
stringifiedObject = EJSON.stringify(value, { relaxed: false });
} catch {
stringifiedObject = inspect(value, {
depth: Infinity,
showHidden: true,
compact: true
});
}
stringParts.push(stringifiedObject);
} else {
stringParts.push(String(value));
}
stringParts.push(strings[idx + 1]);
}
return stringParts.join('');
}
/**
* Run an async function after some set timeout
* @param fn - function to run
* @param ms - timeout in MS
*/
export const runLater = (fn: () => Promise<void>, ms: number) => {
return new Promise<void>((resolve, reject) => {
setTimeout(() => fn().then(resolve).catch(reject), ms);
});
};
export const sleep = promisify(setTimeout);
/**
* If you are using sinon fake timers, it can end up blocking queued IO from running
* awaiting a setTimeout call will allow the event loop to process Networking/FS callbacks
*/
export const processTick = () => new Promise(resolve => setTimeout(resolve, 0));
export function getIndicesOfAuthInUrl(connectionString: string | string[]) {
const doubleSlashIndex = connectionString.indexOf('//');
const atIndex = connectionString.indexOf('@');
if (doubleSlashIndex === -1 || atIndex === -1) {
return null;
}
return {
start: doubleSlashIndex + 2,
end: atIndex
};
}
export function removeAuthFromConnectionString(connectionString: string) {
const indices = getIndicesOfAuthInUrl(connectionString);
if (!indices) {
return connectionString;
}
const { start, end } = indices;
if (start === -1 || end === -1) {
return connectionString;
}
return connectionString.slice(0, start) + connectionString.slice(end + 1);
}
export function extractAuthFromConnectionString(connectionString: string | any[]) {
const indices = getIndicesOfAuthInUrl(connectionString);
if (!indices) {
return null;
}
return connectionString.slice(indices.start, indices.end);
}
export interface FailPoint {
configureFailPoint: 'failCommand' | 'failGetMoreAfterCursorCheckout' | 'maxTimeNeverTimeOut';
mode: { activationProbability: number } | { times: number } | 'alwaysOn' | 'off';
}
export interface FailCommandFailPoint extends FailPoint {
configureFailPoint: 'failCommand';
data: {
failCommands: string[];
errorCode?: number;
closeConnection?: boolean;
blockConnection?: boolean;
blockTimeMS?: number;
writeConcernError?: { code: number; errmsg?: string; errorLabels?: string[] };
threadName?: string;
failInternalCommands?: boolean;
errorLabels?: string[];
appName?: string;
namespace?: string;
};
}
export function bufferToStream(buffer) {
const stream = new Readable();
if (Array.isArray(buffer)) {
buffer.forEach(b => stream.push(b));
} else {
stream.push(buffer);
}
stream.push(null);
return stream;
}
export function generateOpMsgBuffer(document: Document): Buffer {
const header = Buffer.alloc(4 * 4 + 4);
const typeBuffer = Buffer.alloc(1);
typeBuffer[0] = 0;
const docBuffer = BSON.serialize(document);
const totalLength = header.length + typeBuffer.length + docBuffer.length;
header.writeInt32LE(totalLength, 0);
header.writeInt32LE(0, 4);
header.writeInt32LE(0, 8);
header.writeInt32LE(OP_MSG, 12);
header.writeUInt32LE(0, 16);
return Buffer.concat([header, typeBuffer, docBuffer]);
}
export const alphabetically = (a: any, b: any) => {
const res = `${a}`.localeCompare(`${b}`, 'en-US', {
usage: 'sort',
numeric: true,
ignorePunctuation: false
});
return res < 0 ? -1 : res > 0 ? 1 : 0;
};
export const sorted = <T>(iterable: Iterable<T>, how: (a: T, b: T) => 0 | 1 | -1) => {
if (typeof how !== 'function') {
throw new TypeError('must provide a "how" function to sorted');
}
const items = Array.from(iterable);
items.sort(how);
return items;
};
/**
* This helper method is necessary as the changes in NODE-4720 required the Topology constructor to
* accept a client with access to a logger. This helper serves to keep old tests working with minimal
* changes*/
export function topologyWithPlaceholderClient(
seeds: string | string[] | HostAddress | HostAddress[],
options: Partial<TopologyOptions>
): Topology {
return new Topology(
new MongoClient('mongodb://iLoveJavaScript'),
seeds,
options as TopologyOptions
);
}
export async function itInNodeProcess(
title: string,
fn: (d: { expect: typeof import('chai').expect }) => void
) {
it(title, async () => {
const script = `
import { expect } from 'chai';
const run = ${fn};
run({ expect }).then(
() => {
process.exitCode = 0;
},
error => {
console.error(error)
process.exitCode = 1;
}
);\n`;
const scriptName = `./testing_${title.split(/\s/).join('_')}_script.cts`;
const cwd = path.resolve(__dirname, '..', '..');
const tsNode = path.resolve(__dirname, '..', '..', 'node_modules', '.bin', 'ts-node');
try {
await fs.writeFile(scriptName, script, { encoding: 'utf8' });
const scriptInstance = child_process.fork(scriptName, {
signal: AbortSignal.timeout(50_000),
cwd,
stdio: 'pipe',
execArgv: [tsNode]
});
scriptInstance.stdout?.setEncoding('utf8');
scriptInstance.stderr?.setEncoding('utf8');
let stdout = '';
scriptInstance.stdout?.addListener('data', data => {
stdout += data;
});
let stderr = '';
scriptInstance.stderr?.addListener('data', (data: string) => {
stderr += data;
});
// do not fail the test if the debugger is running
stderr = stderr
.split('\n')
.filter(line => !line.startsWith('Debugger') && !line.startsWith('For help'))
.join('\n');
const [exitCode] = await once(scriptInstance, 'close');
if (stderr.length) console.log(stderr);
expect({ exitCode, stdout, stderr }).to.deep.equal({ exitCode: 0, stdout: '', stderr: '' });
} finally {
await fs.unlink(scriptName);
}
});
}
/**
* Connects the client and waits until `client` has emitted `count` connectionCreated events.
*
* **This will hang if the client does not have a maxPoolSizeSet!**
*
* This is useful when you want to ensure that the client has pools that are full of connections.
*
* This does not guarantee that all pools that the client has are completely full unless
* count = number of servers to which the client is connected * maxPoolSize. But it can
* serve as a way to ensure that some connections have been established and are in the pools.
*/
export async function waitUntilPoolsFilled(
client: MongoClient,
signal: AbortSignal,
count: number = client.s.options.maxPoolSize
): Promise<void> {
let connectionCount = 0;
async function wait$() {
for await (const _event of on(client, 'connectionCreated', { signal })) {
connectionCount++;
if (connectionCount >= count) {
break;
}
}
}
await Promise.all([wait$(), client.connect()]);
}
export async function configureFailPoint(
configuration: TestConfiguration,
failPoint: FailPoint,
uri?: string | undefined
);
export async function configureFailPoint(
configuration: TestConfiguration,
failPoint: FailCommandFailPoint,
uri?: string | undefined
);
export async function configureFailPoint<T extends FailPoint>(
configuration: TestConfiguration,
failPoint: T,
uri = configuration.url()
) {
const utilClient = configuration.newClient(uri);
await utilClient.connect();
try {
await utilClient.db('admin').command(failPoint);
} finally {
await utilClient.close();
}
}
export async function clearFailPoint(
configuration: TestConfiguration,
failPoint: FailPoint['configureFailPoint'] = 'failCommand',
url = configuration.url()
) {
const utilClient = configuration.newClient(url);
await utilClient.connect();
try {
await utilClient.db('admin').command(<FailPoint>{
configureFailPoint: failPoint,
mode: 'off'
});
} finally {
await utilClient.close();
}
}
export async function makeMultiBatchWrite(
configuration: TestConfiguration
): Promise<AnyClientBulkWriteModel<any>[]> {
const { maxBsonObjectSize, maxMessageSizeBytes } = await configuration.hello();
const length = maxMessageSizeBytes / maxBsonObjectSize + 1;
const models = Array.from({ length }, () => ({
namespace: 'db.coll',
name: 'insertOne' as const,
document: { a: 'b'.repeat(maxBsonObjectSize - 500) }
}));
return models;
}
export async function makeMultiResponseBatchModelArray(
configuration: TestConfiguration
): Promise<AnyClientBulkWriteModel<any>[]> {
const { maxBsonObjectSize } = await configuration.hello();
const namespace = `foo.${new BSON.ObjectId().toHexString()}`;
const models: AnyClientBulkWriteModel<any>[] = [
{
name: 'updateOne',
namespace,
update: { $set: { age: 1 } },
upsert: true,
filter: { _id: 'a'.repeat(maxBsonObjectSize / 2) }
},
{
name: 'updateOne',
namespace,
update: { $set: { age: 1 } },
upsert: true,
filter: { _id: 'b'.repeat(maxBsonObjectSize / 2) }
}
];
return models;
}
export function fakeServer() {
return {
s: { state: 'connected' },
removeListener: () => true,
pool: {
checkOut: async () => ({}),
checkIn: () => undefined
}
};
}
/**
* A utility to measure the duration of an async function. This is intended to be used for CSOT
* testing, where we expect to timeout within a certain threshold and want to measure the duration
* of that operation.
*/
export async function measureDuration<T>(f: () => Promise<T>): Promise<{
duration: number;
result: T | Error;
}> {
const start = processTimeMS();
const result = await f().catch(e => e);
const end = processTimeMS();
return {
duration: end - start,
result
};
}
export function mergeTestMetadata(
metadata: MongoDBMetadataUI,
newMetadata: MongoDBMetadataUI
): MongoDBMetadataUI {
return {
requires: {
...metadata.requires,
...newMetadata.requires
},
sessions: {
...metadata.sessions,
...newMetadata.sessions
}
};
}
export function findLast<T, S extends T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => value is S,
thisArg?: any
): S | undefined;
export function findLast<T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
thisArg?: any
): T | undefined;
export function findLast(
array: unknown[],
predicate: (value: unknown, index: number, array: unknown[]) => boolean,
thisArg?: any
): unknown | undefined {
if (typeof array.findLast === 'function') return array.findLast(predicate, thisArg);
return array.slice().reverse().find(predicate, thisArg);
}
// Node.js 16 doesn't make this global, but it can still be obtained.
export const DOMException: {
new: (
message?: string,
nameOrOptions?: string | { name?: string; cause?: unknown }
) => DOMException;
} = (() => {
if (globalThis.DOMException != null) return globalThis.DOMException;
const ac = new AbortController();
ac.abort();
return ac.signal.reason.constructor;
})();
export function configureMongocryptdSpawnHooks(
options: { port?: string; pidfilepath?: string } = {}
): { port: string } {
const port = options.port ?? '27022';
const pidfilepath = options.pidfilepath ?? path.join(tmpdir(), new BSON.ObjectId().toHexString());
let childProcess: child_process.ChildProcess;
beforeEach(async function () {
childProcess = child_process.spawn(
'mongocryptd',
['--port', port, '--ipv6', '--pidfilepath', pidfilepath],
{
stdio: 'ignore',
detached: false
}
);
childProcess.on('error', error => console.warn(this.currentTest?.fullTitle(), error));
});
afterEach(function () {
childProcess.kill('SIGKILL');
});
return {
port
};
}