Skip to content

Commit 3690a00

Browse files
committed
feat(MongoMemoryReplSet): remove usage of "tmp" package
1 parent 62d7876 commit 3690a00

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

docs/api/classes/mongo-memory-replset.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Used by [`start`](#start) and to restart without fully running everything again
7171

7272
<span class="badge badge--warning">Internal</span>
7373

74-
Typings: `protected async ensureKeyFile(): Promise<tmp.DirResult>`
74+
Typings: `protected async ensureKeyFile(): Promise<string>`
7575

7676
Ensures and returns that [`_keyfiletmp`](#_keyfiletmp) is defined an exists and also that the keyfile is created
7777

@@ -212,9 +212,9 @@ Stores the options used for the ReplSet Initiation, uses [`ReplSetOpts`](../inte
212212

213213
<span class="badge badge--warning">Internal</span>
214214

215-
Typings: `protected _keyfiletmp?: tmp.DirResult`
215+
Typings: `protected _keyfiletmp?: string`
216216

217-
Stores the `tmpDir` for the keyfile location
217+
Stores the path of the created temporary directory for the keyfile location
218218

219219
### state
220220

packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import {
44
assertion,
55
authDefault,
66
Cleanup,
7+
createTmpDir,
78
ensureAsync,
89
generateDbName,
910
getHost,
1011
isNullOrUndefined,
1112
ManagerAdvanced,
13+
removeDir,
1214
statPath,
1315
uriTemplate,
1416
} from './util/utils';
@@ -29,14 +31,11 @@ import {
2931
StateError,
3032
WaitForPrimaryTimeoutError,
3133
} from './util/errors';
32-
import * as tmp from 'tmp';
3334
import { promises as fs } from 'fs';
3435
import { resolve } from 'path';
3536

3637
const log = debug('MongoMS:MongoMemoryReplSet');
3738

38-
tmp.setGracefulCleanup();
39-
4039
// "setImmediate" is used to ensure the functions are async, otherwise the process might evaluate the one function before other async functions (like "start")
4140
// and so skip to next state check or return before actually ready
4241

@@ -162,7 +161,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
162161
/** Options for the Replset itself and defaults for instances */
163162
protected _replSetOpts!: Required<ReplSetOpts>;
164163
/** TMPDIR for the keyfile, when auth is used */
165-
protected _keyfiletmp?: tmp.DirResult;
164+
protected _keyfiletmp?: string;
166165

167166
protected _state: MongoMemoryReplSetStates = MongoMemoryReplSetStates.stopped;
168167
protected _ranCreateAuth: boolean = false;
@@ -416,7 +415,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
416415

417416
if (this._ranCreateAuth) {
418417
log('initAllServers: "_ranCreateAuth" is true, re-using auth');
419-
const keyfilepath = resolve((await this.ensureKeyFile()).name, 'keyfile');
418+
const keyfilepath = resolve(await this.ensureKeyFile(), 'keyfile');
420419
for (const server of this.servers) {
421420
assertion(
422421
!isNullOrUndefined(server.instanceInfo),
@@ -445,7 +444,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
445444
let keyfilePath: string | undefined = undefined;
446445

447446
if (this.enableAuth()) {
448-
keyfilePath = resolve((await this.ensureKeyFile()).name, 'keyfile');
447+
keyfilePath = resolve(await this.ensureKeyFile(), 'keyfile');
449448
}
450449

451450
// Any servers defined within `_instanceOpts` should be started first as
@@ -479,18 +478,14 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
479478
* Ensure "_keyfiletmp" is defined
480479
* @returns the ensured "_keyfiletmp" value
481480
*/
482-
protected async ensureKeyFile(): Promise<tmp.DirResult> {
481+
protected async ensureKeyFile(): Promise<string> {
483482
log('ensureKeyFile');
484483

485484
if (isNullOrUndefined(this._keyfiletmp)) {
486-
this._keyfiletmp = tmp.dirSync({
487-
mode: 0o766,
488-
prefix: 'mongo-mem-keyfile-',
489-
unsafeCleanup: true,
490-
});
485+
this._keyfiletmp = await createTmpDir('mongo-mem-keyfile-');
491486
}
492487

493-
const keyfilepath = resolve(this._keyfiletmp.name, 'keyfile');
488+
const keyfilepath = resolve(this._keyfiletmp, 'keyfile');
494489

495490
// if path does not exist or have no access, create it (or fail)
496491
if (!(await statPath(keyfilepath))) {
@@ -499,7 +494,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
499494
assertion(typeof this._replSetOpts.auth === 'object', new AuthNotObjectError());
500495

501496
await fs.writeFile(
502-
resolve(this._keyfiletmp.name, 'keyfile'),
497+
resolve(this._keyfiletmp, 'keyfile'),
503498
this._replSetOpts.auth.keyfileContent ?? '0123456789',
504499
{ mode: 0o700 } // this is because otherwise mongodb errors with "permissions are too open" on unix systems
505500
);
@@ -617,7 +612,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
617612

618613
// cleanup the keyfile tmpdir
619614
if (!isNullOrUndefined(this._keyfiletmp)) {
620-
this._keyfiletmp.removeCallback();
615+
await removeDir(this._keyfiletmp);
621616
this._keyfiletmp = undefined;
622617
}
623618

0 commit comments

Comments
 (0)