mongodb-memory-server 2.1.0 → 2.4.1
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 +64 -2
- package/lib/MongoMemoryReplSet.d.ts +57 -0
- package/lib/MongoMemoryReplSet.js +307 -0
- package/lib/MongoMemoryReplSet.js.flow +252 -0
- package/lib/MongoMemoryServer.d.ts +47 -0
- package/lib/MongoMemoryServer.js +76 -78
- package/lib/MongoMemoryServer.js.flow +22 -26
- package/lib/__tests__/multipleDB-test.js +68 -0
- package/lib/__tests__/replset-test.js +87 -0
- package/lib/__tests__/singleDB-test.js +43 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +32 -14
- package/lib/index.js.flow +2 -1
- package/lib/types.d.ts +49 -0
- package/lib/types.js +1 -0
- package/lib/types.js.flow +60 -0
- package/lib/util/MongoBinary.d.ts +23 -0
- package/lib/util/MongoBinary.js +46 -42
- package/lib/util/MongoBinary.js.flow +13 -6
- package/lib/util/MongoBinaryDownload.d.ts +33 -0
- package/lib/util/MongoBinaryDownload.js +84 -73
- package/lib/util/MongoBinaryDownload.js.flow +5 -11
- package/lib/util/MongoBinaryDownloadUrl.d.ts +35 -0
- package/lib/util/MongoBinaryDownloadUrl.js +68 -31
- package/lib/util/MongoBinaryDownloadUrl.js.flow +12 -17
- package/lib/util/MongoInstance.d.ts +49 -0
- package/lib/util/MongoInstance.js +50 -32
- package/lib/util/MongoInstance.js.flow +12 -17
- package/lib/util/__tests__/MongoBinary-test.js +52 -0
- package/lib/util/__tests__/MongoBinaryDownload-test.js +42 -0
- package/lib/util/__tests__/MongoBinaryDownloadUrl-test.js +179 -0
- package/lib/util/__tests__/MongoInstance-test.js +133 -0
- package/lib/util/db_util.d.ts +20 -0
- package/lib/util/db_util.js +56 -0
- package/lib/util/db_util.js.flow +29 -0
- package/lib/util/mongo_killer.d.ts +0 -0
- package/lib/util/mongo_killer.js +5 -5
- package/package.json +36 -23
package/lib/index.js
CHANGED
|
@@ -1,25 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
Object.defineProperty(exports, "MongoMemoryServer", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _MongoMemoryServer.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "MongoMemoryReplSet", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _MongoMemoryReplSet.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "MongoInstance", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return _MongoInstance.default;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "MongoBinary", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _MongoBinary.default;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
exports.default = void 0;
|
|
11
31
|
|
|
12
|
-
var
|
|
32
|
+
var _MongoMemoryServer = _interopRequireDefault(require("./MongoMemoryServer"));
|
|
13
33
|
|
|
14
|
-
var
|
|
34
|
+
var _MongoMemoryReplSet = _interopRequireDefault(require("./MongoMemoryReplSet"));
|
|
15
35
|
|
|
16
|
-
var
|
|
36
|
+
var _MongoInstance = _interopRequireDefault(require("./util/MongoInstance"));
|
|
17
37
|
|
|
18
|
-
var
|
|
38
|
+
var _MongoBinary = _interopRequireDefault(require("./util/MongoBinary"));
|
|
19
39
|
|
|
20
40
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
41
|
|
|
22
|
-
|
|
23
|
-
exports.
|
|
24
|
-
exports.MongoInstance = _MongoInstance2.default;
|
|
25
|
-
exports.MongoBinary = _MongoBinary2.default;
|
|
42
|
+
var _default = _MongoMemoryServer.default;
|
|
43
|
+
exports.default = _default;
|
package/lib/index.js.flow
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
|
|
3
3
|
import MongoMemoryServer from './MongoMemoryServer';
|
|
4
|
+
import MongoMemoryReplSet from './MongoMemoryReplSet';
|
|
4
5
|
import MongoInstance from './util/MongoInstance';
|
|
5
6
|
import MongoBinary from './util/MongoBinary';
|
|
6
7
|
|
|
7
8
|
export default MongoMemoryServer;
|
|
8
|
-
export { MongoMemoryServer, MongoInstance, MongoBinary };
|
|
9
|
+
export { MongoMemoryServer, MongoMemoryReplSet, MongoInstance, MongoBinary };
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/// <reference types='node' />
|
|
2
|
+
|
|
3
|
+
// These types are used internally to the module.
|
|
4
|
+
|
|
5
|
+
export type DebugFn = (...args: any[]) => any;
|
|
6
|
+
export type DebugPropT = boolean | DebugFn;
|
|
7
|
+
|
|
8
|
+
export interface DownloadProgressT {
|
|
9
|
+
current: number;
|
|
10
|
+
length: number;
|
|
11
|
+
totalMb: number;
|
|
12
|
+
lastPrintedAt: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type CallbackFn = (...args: any[]) => any;
|
|
16
|
+
|
|
17
|
+
export { SpawnOptions } from 'child_process';
|
|
18
|
+
|
|
19
|
+
export type StorageEngineT = 'devnull' | 'ephemeralForTest' | 'mmapv1' | 'wiredTiger';
|
|
20
|
+
|
|
21
|
+
export interface MongoMemoryInstancePropBaseT {
|
|
22
|
+
args?: string[];
|
|
23
|
+
port?: number | null;
|
|
24
|
+
dbPath?: string;
|
|
25
|
+
storageEngine?: StorageEngineT;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface MongoMemoryInstancePropT extends MongoMemoryInstancePropBaseT {
|
|
29
|
+
auth?: boolean;
|
|
30
|
+
dbName?: string;
|
|
31
|
+
debug?: DebugPropT;
|
|
32
|
+
ip?: string; // for binding to all IP addresses set it to `::,0.0.0.0`, by default '127.0.0.1'
|
|
33
|
+
replSet?: string;
|
|
34
|
+
storageEngine?: StorageEngineT;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ReplStatusMemberT {
|
|
38
|
+
_id: number;
|
|
39
|
+
name: string;
|
|
40
|
+
health: number;
|
|
41
|
+
state: number;
|
|
42
|
+
stateStr: string;
|
|
43
|
+
uptime: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ReplStatusResultT {
|
|
47
|
+
set: string;
|
|
48
|
+
members: ReplStatusMemberT[];
|
|
49
|
+
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
export type DebugFn = (...args: any[]) => any;
|
|
3
|
+
export type DebugPropT = boolean | DebugFn;
|
|
4
|
+
|
|
5
|
+
export type DownloadProgressT = {
|
|
6
|
+
current: number,
|
|
7
|
+
length: number,
|
|
8
|
+
totalMb: number,
|
|
9
|
+
lastPrintedAt: number,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type CallbackFn = (...args: any[]) => any;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Adapted from the TypeScript definition for node since flow doens't seem to know about these types.
|
|
16
|
+
*/
|
|
17
|
+
export interface SpawnOptions {
|
|
18
|
+
cwd?: string;
|
|
19
|
+
env?: {};
|
|
20
|
+
argv0?: string;
|
|
21
|
+
stdio?: string | any[];
|
|
22
|
+
detached?: boolean;
|
|
23
|
+
uid?: number;
|
|
24
|
+
gid?: number;
|
|
25
|
+
shell?: boolean | string;
|
|
26
|
+
windowsVerbatimArguments?: boolean;
|
|
27
|
+
windowsHide?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type StorageEngineT = 'devnull' | 'ephemeralForTest' | 'mmapv1' | 'wiredTiger';
|
|
31
|
+
|
|
32
|
+
export interface MongoMemoryInstancePropBaseT {
|
|
33
|
+
args?: string[];
|
|
34
|
+
port?: ?number;
|
|
35
|
+
dbPath?: string;
|
|
36
|
+
storageEngine?: StorageEngineT;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface MongoMemoryInstancePropT extends MongoMemoryInstancePropBaseT {
|
|
40
|
+
auth?: boolean;
|
|
41
|
+
dbName?: string;
|
|
42
|
+
debug?: DebugPropT;
|
|
43
|
+
ip?: string; // for binding to all IP addresses set it to `::,0.0.0.0`, by default '127.0.0.1'
|
|
44
|
+
replSet?: string;
|
|
45
|
+
storageEngine?: StorageEngineT;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ReplStatusMemberT {
|
|
49
|
+
_id: number;
|
|
50
|
+
name: string;
|
|
51
|
+
health: number;
|
|
52
|
+
state: number;
|
|
53
|
+
stateStr: string;
|
|
54
|
+
uptime: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ReplStatusResultT {
|
|
58
|
+
set: string;
|
|
59
|
+
members: ReplStatusMemberT[];
|
|
60
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DebugPropT } from '../types';
|
|
2
|
+
|
|
3
|
+
export interface MongoBinaryCache {
|
|
4
|
+
[version: string]: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface MongoBinaryOpts {
|
|
8
|
+
version?: string;
|
|
9
|
+
downloadDir?: string;
|
|
10
|
+
platform?: string;
|
|
11
|
+
arch?: string;
|
|
12
|
+
debug?: DebugPropT;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// disable error for a class with all static functions,
|
|
16
|
+
// so the TypeScript declaration would map the implementation with flow types for easier support.
|
|
17
|
+
// tslint:disable-next-line:no-unnecessary-class
|
|
18
|
+
export default class MongoBinary {
|
|
19
|
+
static cache: MongoBinaryCache;
|
|
20
|
+
|
|
21
|
+
static getPath(opts?: MongoBinaryOpts): Promise<string>;
|
|
22
|
+
static hasValidBinPath(files: string[]): boolean;
|
|
23
|
+
}
|
package/lib/util/MongoBinary.js
CHANGED
|
@@ -1,50 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.default = void 0;
|
|
6
7
|
|
|
7
|
-
var _os = require(
|
|
8
|
+
var _os = _interopRequireDefault(require("os"));
|
|
8
9
|
|
|
9
|
-
var
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
11
|
|
|
11
|
-
var
|
|
12
|
+
var _lockfile = _interopRequireDefault(require("lockfile"));
|
|
12
13
|
|
|
13
|
-
var
|
|
14
|
+
var _mkdirp = _interopRequireDefault(require("mkdirp"));
|
|
14
15
|
|
|
15
|
-
var
|
|
16
|
+
var _MongoBinaryDownload = _interopRequireDefault(require("./MongoBinaryDownload"));
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var _mkdirp = require('mkdirp');
|
|
20
|
-
|
|
21
|
-
var _mkdirp2 = _interopRequireDefault(_mkdirp);
|
|
22
|
-
|
|
23
|
-
var _MongoBinaryDownload = require('./MongoBinaryDownload');
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
19
|
|
|
25
|
-
var
|
|
20
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
26
21
|
|
|
27
|
-
function
|
|
22
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
28
23
|
|
|
29
|
-
function
|
|
24
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
30
25
|
|
|
31
26
|
class MongoBinary {
|
|
32
|
-
|
|
33
27
|
static getPath(opts = {}) {
|
|
34
28
|
var _this = this;
|
|
35
29
|
|
|
36
30
|
return _asyncToGenerator(function* () {
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
var _process$env, _process$env2, _process$env3, _process$env4;
|
|
32
|
+
|
|
33
|
+
const defaultOptions = {
|
|
34
|
+
downloadDir: ((_process$env = process.env) === null || _process$env === void 0 ? void 0 : _process$env.MONGOMS_DOWNLOAD_DIR) || _path.default.resolve(_os.default.homedir(), '.mongodb-binaries'),
|
|
35
|
+
platform: ((_process$env2 = process.env) === null || _process$env2 === void 0 ? void 0 : _process$env2.MONGOMS_PLATFORM) || _os.default.platform(),
|
|
36
|
+
arch: ((_process$env3 = process.env) === null || _process$env3 === void 0 ? void 0 : _process$env3.MONGOMS_ARCH) || _os.default.arch(),
|
|
37
|
+
version: ((_process$env4 = process.env) === null || _process$env4 === void 0 ? void 0 : _process$env4.MONGOMS_VERSION) || 'latest',
|
|
38
|
+
debug: typeof process.env.MONGOMS_DEBUG === 'string' ? ['1', 'on', 'yes', 'true'].indexOf(process.env.MONGOMS_DEBUG.toLowerCase()) !== -1 : false
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const _Object$assign = Object.assign({}, defaultOptions, opts),
|
|
42
|
+
downloadDir = _Object$assign.downloadDir,
|
|
43
|
+
platform = _Object$assign.platform,
|
|
44
|
+
arch = _Object$assign.arch,
|
|
45
|
+
version = _Object$assign.version;
|
|
46
46
|
|
|
47
47
|
let debug;
|
|
48
|
+
|
|
48
49
|
if (opts.debug) {
|
|
49
50
|
if (opts.debug.call && typeof opts.debug === 'function' && opts.debug.apply) {
|
|
50
51
|
debug = opts.debug;
|
|
@@ -52,49 +53,48 @@ class MongoBinary {
|
|
|
52
53
|
debug = console.log.bind(null);
|
|
53
54
|
}
|
|
54
55
|
} else {
|
|
55
|
-
debug =
|
|
56
|
+
debug = msg => {}; // eslint-disable-line
|
|
57
|
+
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
if (_this.cache[version]) {
|
|
59
61
|
debug(`MongoBinary: found cached binary path for ${version}`);
|
|
60
62
|
} else {
|
|
61
63
|
// create downloadDir if not exists
|
|
62
|
-
yield new Promise(
|
|
63
|
-
(0,
|
|
64
|
+
yield new Promise((resolve, reject) => {
|
|
65
|
+
(0, _mkdirp.default)(downloadDir, err => {
|
|
64
66
|
if (err) reject(err);else resolve();
|
|
65
67
|
});
|
|
66
68
|
});
|
|
67
69
|
|
|
68
|
-
const lockfile =
|
|
70
|
+
const lockfile = _path.default.resolve(downloadDir, `${version}.lock`); // wait lock
|
|
69
71
|
|
|
70
|
-
|
|
71
|
-
yield new Promise(
|
|
72
|
-
|
|
72
|
+
|
|
73
|
+
yield new Promise((resolve, reject) => {
|
|
74
|
+
_lockfile.default.lock(lockfile, {
|
|
73
75
|
wait: 120000,
|
|
74
76
|
pollPeriod: 100,
|
|
75
77
|
stale: 110000,
|
|
76
78
|
retries: 3,
|
|
77
79
|
retryWait: 100
|
|
78
|
-
},
|
|
80
|
+
}, err => {
|
|
79
81
|
if (err) reject(err);else resolve();
|
|
80
82
|
});
|
|
81
|
-
});
|
|
83
|
+
}); // again check cache, maybe other instance resolve it
|
|
82
84
|
|
|
83
|
-
// again check cache, maybe other instance resolve it
|
|
84
85
|
if (!_this.cache[version]) {
|
|
85
|
-
const downloader = new
|
|
86
|
+
const downloader = new _MongoBinaryDownload.default({
|
|
86
87
|
downloadDir,
|
|
87
88
|
platform,
|
|
88
89
|
arch,
|
|
89
90
|
version
|
|
90
91
|
});
|
|
91
|
-
|
|
92
92
|
downloader.debug = debug;
|
|
93
93
|
_this.cache[version] = yield downloader.getMongodPath();
|
|
94
|
-
}
|
|
94
|
+
} // remove lock
|
|
95
|
+
|
|
95
96
|
|
|
96
|
-
|
|
97
|
-
_lockfile2.default.unlock(lockfile, function (err) {
|
|
97
|
+
_lockfile.default.unlock(lockfile, err => {
|
|
98
98
|
debug(err ? `MongoBinary: Error when removing download lock ${err}` : `MongoBinary: Download lock removed`);
|
|
99
99
|
});
|
|
100
100
|
}
|
|
@@ -110,8 +110,12 @@ class MongoBinary {
|
|
|
110
110
|
} else if (files.length > 1) {
|
|
111
111
|
return false;
|
|
112
112
|
}
|
|
113
|
+
|
|
113
114
|
return false;
|
|
114
115
|
}
|
|
116
|
+
|
|
115
117
|
}
|
|
118
|
+
|
|
116
119
|
exports.default = MongoBinary;
|
|
117
|
-
|
|
120
|
+
|
|
121
|
+
_defineProperty(MongoBinary, "cache", {});
|
|
@@ -22,12 +22,19 @@ export default class MongoBinary {
|
|
|
22
22
|
static cache: MongoBinaryCache = {};
|
|
23
23
|
|
|
24
24
|
static async getPath(opts?: MongoBinaryOpts = {}): Promise<string> {
|
|
25
|
-
const {
|
|
26
|
-
downloadDir
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
const defaultOptions = {
|
|
26
|
+
downloadDir:
|
|
27
|
+
process.env?.MONGOMS_DOWNLOAD_DIR || path.resolve(os.homedir(), '.mongodb-binaries'),
|
|
28
|
+
platform: process.env?.MONGOMS_PLATFORM || os.platform(),
|
|
29
|
+
arch: process.env?.MONGOMS_ARCH || os.arch(),
|
|
30
|
+
version: process.env?.MONGOMS_VERSION || 'latest',
|
|
31
|
+
debug:
|
|
32
|
+
typeof process.env.MONGOMS_DEBUG === 'string'
|
|
33
|
+
? ['1', 'on', 'yes', 'true'].indexOf(process.env.MONGOMS_DEBUG.toLowerCase()) !== -1
|
|
34
|
+
: false,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const { downloadDir, platform, arch, version } = Object.assign({}, defaultOptions, opts);
|
|
31
38
|
|
|
32
39
|
let debug;
|
|
33
40
|
if (opts.debug) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { DebugPropT, DebugFn, DownloadProgressT } from '../types';
|
|
2
|
+
|
|
3
|
+
export interface MongoBinaryDownloadOpts {
|
|
4
|
+
version: string;
|
|
5
|
+
downloadDir: string;
|
|
6
|
+
platform: string;
|
|
7
|
+
arch: string;
|
|
8
|
+
debug?: DebugPropT;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default class MongoBinaryDownload {
|
|
12
|
+
debug: DebugFn;
|
|
13
|
+
dlProgress: DownloadProgressT;
|
|
14
|
+
|
|
15
|
+
downloadDir: string;
|
|
16
|
+
arch: string;
|
|
17
|
+
version: string;
|
|
18
|
+
platform: string;
|
|
19
|
+
|
|
20
|
+
constructor(opts: MongoBinaryDownloadOpts);
|
|
21
|
+
getMongodPath(): Promise<string>;
|
|
22
|
+
startDownload(): Promise<string>;
|
|
23
|
+
checkMd5(mongoDBArchiveMd5: string, mongoDBArchive: string): Promise<void>;
|
|
24
|
+
download(downloadUrl: string): Promise<string>;
|
|
25
|
+
extract(mongoDBArchive: string): Promise<string>;
|
|
26
|
+
httpDownload(
|
|
27
|
+
httpOptions: any, // tslint:disable-line:no-any
|
|
28
|
+
downloadLocation: string,
|
|
29
|
+
tempDownloadLocation: string
|
|
30
|
+
): Promise<string>;
|
|
31
|
+
printDownloadProgress(chunk: any): void; // tslint:disable-line:no-any
|
|
32
|
+
locationExists(location: string): boolean;
|
|
33
|
+
}
|