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.
Files changed (38) hide show
  1. package/README.md +64 -2
  2. package/lib/MongoMemoryReplSet.d.ts +57 -0
  3. package/lib/MongoMemoryReplSet.js +307 -0
  4. package/lib/MongoMemoryReplSet.js.flow +252 -0
  5. package/lib/MongoMemoryServer.d.ts +47 -0
  6. package/lib/MongoMemoryServer.js +76 -78
  7. package/lib/MongoMemoryServer.js.flow +22 -26
  8. package/lib/__tests__/multipleDB-test.js +68 -0
  9. package/lib/__tests__/replset-test.js +87 -0
  10. package/lib/__tests__/singleDB-test.js +43 -0
  11. package/lib/index.d.ts +6 -0
  12. package/lib/index.js +32 -14
  13. package/lib/index.js.flow +2 -1
  14. package/lib/types.d.ts +49 -0
  15. package/lib/types.js +1 -0
  16. package/lib/types.js.flow +60 -0
  17. package/lib/util/MongoBinary.d.ts +23 -0
  18. package/lib/util/MongoBinary.js +46 -42
  19. package/lib/util/MongoBinary.js.flow +13 -6
  20. package/lib/util/MongoBinaryDownload.d.ts +33 -0
  21. package/lib/util/MongoBinaryDownload.js +84 -73
  22. package/lib/util/MongoBinaryDownload.js.flow +5 -11
  23. package/lib/util/MongoBinaryDownloadUrl.d.ts +35 -0
  24. package/lib/util/MongoBinaryDownloadUrl.js +68 -31
  25. package/lib/util/MongoBinaryDownloadUrl.js.flow +12 -17
  26. package/lib/util/MongoInstance.d.ts +49 -0
  27. package/lib/util/MongoInstance.js +50 -32
  28. package/lib/util/MongoInstance.js.flow +12 -17
  29. package/lib/util/__tests__/MongoBinary-test.js +52 -0
  30. package/lib/util/__tests__/MongoBinaryDownload-test.js +42 -0
  31. package/lib/util/__tests__/MongoBinaryDownloadUrl-test.js +179 -0
  32. package/lib/util/__tests__/MongoInstance-test.js +133 -0
  33. package/lib/util/db_util.d.ts +20 -0
  34. package/lib/util/db_util.js +56 -0
  35. package/lib/util/db_util.js.flow +29 -0
  36. package/lib/util/mongo_killer.d.ts +0 -0
  37. package/lib/util/mongo_killer.js +5 -5
  38. package/package.json +36 -23
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+
3
+ var _tmp = _interopRequireDefault(require("tmp"));
4
+
5
+ var _MongoInstance = _interopRequireDefault(require("../MongoInstance"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ 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); } }
10
+
11
+ 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); }); }; }
12
+
13
+ jasmine.DEFAULT_TIMEOUT_INTERVAL = 240000;
14
+ let tmpDir;
15
+ beforeEach(() => {
16
+ _tmp.default.setGracefulCleanup();
17
+
18
+ tmpDir = _tmp.default.dirSync({
19
+ prefix: 'mongo-mem-',
20
+ unsafeCleanup: true
21
+ });
22
+ });
23
+ afterEach(() => {
24
+ tmpDir.removeCallback();
25
+ });
26
+ describe('MongoInstance', () => {
27
+ it('should prepare command args', () => {
28
+ const inst = new _MongoInstance.default({
29
+ instance: {
30
+ port: 27333,
31
+ dbPath: '/data',
32
+ storageEngine: 'ephemeralForTest'
33
+ }
34
+ });
35
+ expect(inst.prepareCommandArgs()).toEqual(['--bind_ip', '127.0.0.1', '--port', '27333', '--storageEngine', 'ephemeralForTest', '--dbpath', '/data', '--noauth']);
36
+ });
37
+ it('should allow specifying replSet', () => {
38
+ const inst = new _MongoInstance.default({
39
+ instance: {
40
+ port: 27555,
41
+ dbPath: '/data',
42
+ replSet: 'testset'
43
+ }
44
+ });
45
+ expect(inst.prepareCommandArgs()).toEqual(['--bind_ip', '127.0.0.1', '--port', '27555', '--dbpath', '/data', '--noauth', '--replSet', 'testset']);
46
+ });
47
+ it('should be able to enable auth', () => {
48
+ const inst = new _MongoInstance.default({
49
+ instance: {
50
+ port: 27555,
51
+ dbPath: '/data',
52
+ auth: true
53
+ }
54
+ });
55
+ expect(inst.prepareCommandArgs()).toEqual(['--bind_ip', '127.0.0.1', '--port', '27555', '--dbpath', '/data']);
56
+ });
57
+ it('should be able to pass arbitrary args', () => {
58
+ const args = ['--notablescan', '--nounixsocket'];
59
+ const inst = new _MongoInstance.default({
60
+ instance: {
61
+ port: 27555,
62
+ dbPath: '/data',
63
+ args
64
+ }
65
+ });
66
+ expect(inst.prepareCommandArgs()).toEqual(['--bind_ip', '127.0.0.1', '--port', '27555', '--dbpath', '/data', '--noauth'].concat(args));
67
+ });
68
+ it('should start instance on port 27333',
69
+ /*#__PURE__*/
70
+ _asyncToGenerator(function* () {
71
+ const mongod = yield _MongoInstance.default.run({
72
+ instance: {
73
+ port: 27333,
74
+ dbPath: tmpDir.name
75
+ },
76
+ binary: {
77
+ version: 'latest'
78
+ }
79
+ });
80
+ expect(mongod.getPid()).toBeGreaterThan(0);
81
+ yield mongod.kill();
82
+ }));
83
+ it('should throw error if port is busy',
84
+ /*#__PURE__*/
85
+ _asyncToGenerator(function* () {
86
+ const mongod = yield _MongoInstance.default.run({
87
+ instance: {
88
+ port: 27444,
89
+ dbPath: tmpDir.name
90
+ },
91
+ binary: {
92
+ version: 'latest'
93
+ }
94
+ });
95
+ yield expect(_MongoInstance.default.run({
96
+ instance: {
97
+ port: 27444,
98
+ dbPath: tmpDir.name
99
+ },
100
+ binary: {
101
+ version: 'latest'
102
+ }
103
+ })).rejects.toBeDefined();
104
+ yield mongod.kill();
105
+ }));
106
+ it('should await while mongo is killed',
107
+ /*#__PURE__*/
108
+ _asyncToGenerator(function* () {
109
+ const mongod = yield _MongoInstance.default.run({
110
+ instance: {
111
+ port: 27445,
112
+ dbPath: tmpDir.name
113
+ },
114
+ binary: {
115
+ version: 'latest'
116
+ }
117
+ });
118
+ const pid = mongod.getPid();
119
+ expect(pid).toBeGreaterThan(0);
120
+
121
+ function isPidRunning(p) {
122
+ try {
123
+ return process.kill(p, 0);
124
+ } catch (e) {
125
+ return e.code === 'EPERM';
126
+ }
127
+ }
128
+
129
+ expect(isPidRunning(pid)).toBeTruthy();
130
+ yield mongod.kill();
131
+ expect(isPidRunning(pid)).toBeFalsy();
132
+ }));
133
+ });
@@ -0,0 +1,20 @@
1
+ import { ReplStatusResultT } from '../types';
2
+ /**
3
+ * Returns a database name string.
4
+ * @param {string} dbName
5
+ */
6
+ export function generateDbName(dbName?: string): string;
7
+
8
+ /**
9
+ * Extracts the host and port information from a mongodb URI string.
10
+ * @param {string} uri mongodb URI
11
+ */
12
+ export function getHost(uri: string): string;
13
+
14
+ /**
15
+ * Returns replica set status result.
16
+ * @param {any} db db instance
17
+ */
18
+ export function getReplStatus(db: any): Promise<ReplStatusResultT>;
19
+
20
+ export default generateDbName;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.generateDbName = generateDbName;
7
+ exports.getHost = getHost;
8
+ exports.getReplStatus = getReplStatus;
9
+ exports.default = void 0;
10
+
11
+ var _v = _interopRequireDefault(require("uuid/v4"));
12
+
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+
15
+ 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); } }
16
+
17
+ 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); }); }; }
18
+
19
+ /**
20
+ * Returns a database name string.
21
+ * @param {string} dbName
22
+ */
23
+ function generateDbName(dbName) {
24
+ return dbName || (0, _v.default)();
25
+ }
26
+ /**
27
+ * Extracts the host and port information from a mongodb URI string.
28
+ * @param {string} uri mongodb URI
29
+ */
30
+
31
+
32
+ function getHost(uri) {
33
+ return uri.replace('mongodb://', '').replace(/\/.*/, '');
34
+ }
35
+ /**
36
+ * Returns replica set status result.
37
+ * @param {any} db db instance
38
+ */
39
+
40
+
41
+ function getReplStatus(_x) {
42
+ return _getReplStatus.apply(this, arguments);
43
+ }
44
+
45
+ function _getReplStatus() {
46
+ _getReplStatus = _asyncToGenerator(function* (db) {
47
+ const status = yield db.command({
48
+ replSetGetStatus: 1
49
+ });
50
+ return status;
51
+ });
52
+ return _getReplStatus.apply(this, arguments);
53
+ }
54
+
55
+ var _default = generateDbName;
56
+ exports.default = _default;
@@ -0,0 +1,29 @@
1
+ // @flow
2
+ import uuid from 'uuid/v4';
3
+ import type { ReplStatusResultT } from '../types';
4
+ /**
5
+ * Returns a database name string.
6
+ * @param {string} dbName
7
+ */
8
+ export function generateDbName(dbName?: string): string {
9
+ return dbName || uuid();
10
+ }
11
+
12
+ /**
13
+ * Extracts the host and port information from a mongodb URI string.
14
+ * @param {string} uri mongodb URI
15
+ */
16
+ export function getHost(uri: string): string {
17
+ return uri.replace('mongodb://', '').replace(/\/.*/, '');
18
+ }
19
+
20
+ /**
21
+ * Returns replica set status result.
22
+ * @param {any} db db instance
23
+ */
24
+ export async function getReplStatus(db: any): Promise<ReplStatusResultT> {
25
+ const status = await db.command({ replSetGetStatus: 1 });
26
+ return status;
27
+ }
28
+
29
+ export default generateDbName;
File without changes
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
 
3
3
  /* eslint-disable */
4
+
4
5
  /*
5
6
  make sure every few seconds that parent is still alive
6
7
  and if it is dead, we will kill child too.
7
8
  this is to ensure that exits done via kill
8
9
  wont leave mongod around
9
10
  */
10
-
11
11
  var parentPid = parseInt(process.argv[2], 10);
12
12
  var childPid = parseInt(process.argv[3], 10);
13
13
 
@@ -19,13 +19,13 @@ if (parentPid && childPid) {
19
19
  } catch (e) {
20
20
  try {
21
21
  process.kill(childPid);
22
- } catch (ee) {
23
- // doesnt matter if it is dead
22
+ } catch (ee) {// doesnt matter if it is dead
24
23
  }
24
+
25
25
  process.exit();
26
- }
26
+ } // if child dead
27
+
27
28
 
28
- // if child dead
29
29
  try {
30
30
  process.kill(childPid, 0);
31
31
  } catch (e) {
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "mongodb-memory-server",
3
- "version": "2.1.0",
3
+ "version": "2.4.1",
4
4
  "description": "In-memory MongoDB Server. Designed with testing in mind, the server will allow you to connect your favourite ODM or client library to the MongoDB Server and run integration tests isolated from each other.",
5
5
  "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
6
7
  "repository": "https://github.com/nodkz/mongodb-memory-server",
7
8
  "author": "Nodkz (pavel.chertorogov@gmail.com)",
8
9
  "files": [
@@ -24,32 +25,39 @@
24
25
  },
25
26
  "homepage": "https://github.com/nodkz/mongodb-memory-server",
26
27
  "devDependencies": {
27
- "babel-cli": "^6.26.0",
28
- "babel-eslint": "^8.2.6",
29
- "babel-jest": "^23.4.2",
30
- "babel-plugin-transform-class-properties": "^6.24.1",
31
- "babel-plugin-transform-flow-strip-types": "^6.22.0",
32
- "babel-plugin-transform-object-rest-spread": "^6.26.0",
33
- "babel-plugin-transform-runtime": "^6.23.0",
34
- "babel-preset-env": "^1.7.0",
28
+ "@babel/cli": "^7.0.0",
29
+ "@babel/core": "^7.0.0",
30
+ "@babel/plugin-proposal-class-properties": "^7.0.0",
31
+ "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
32
+ "@babel/plugin-proposal-optional-chaining": "^7.0.0",
33
+ "@babel/plugin-transform-flow-strip-types": "^7.0.0",
34
+ "@babel/preset-env": "^7.0.0",
35
+ "@babel/preset-flow": "^7.0.0",
36
+ "@types/getos": "^3.0.0",
37
+ "@types/node": "^10.11.1",
38
+ "babel-core": "^7.0.0-bridge.0",
39
+ "babel-eslint": "^9.0.0",
40
+ "babel-jest": "^23.6.0",
35
41
  "cz-conventional-changelog": "^2.1.0",
36
- "eslint": "^5.2.0",
37
- "eslint-config-airbnb-base": "^13.0.0",
38
- "eslint-config-prettier": "^2.9.0",
42
+ "eslint": "^5.5.0",
43
+ "eslint-config-airbnb-base": "^13.1.0",
44
+ "eslint-config-prettier": "^3.0.1",
39
45
  "eslint-plugin-flowtype": "^2.50.0",
40
- "eslint-plugin-import": "^2.12.0",
46
+ "eslint-plugin-import": "^2.14.0",
41
47
  "eslint-plugin-prettier": "^2.6.2",
42
- "flow-bin": "^0.77.0",
43
- "jest": "^23.4.2",
44
- "mongodb": "3.1.1",
48
+ "flow-bin": "^0.80.0",
49
+ "jest": "^23.6.0",
50
+ "mongodb": "^3.1.6",
45
51
  "npm-run-all": "^4.1.3",
46
- "prettier": "^1.14.0",
52
+ "prettier": "^1.14.2",
47
53
  "rimraf": "^2.6.2",
48
- "semantic-release": "^15.9.3"
54
+ "semantic-release": "^15.9.15",
55
+ "tslint": "^5.11.0",
56
+ "typescript": "^3.1.1"
49
57
  },
50
58
  "dependencies": {
51
- "babel-runtime": "^6.26.0",
52
- "debug": "^3.1.0",
59
+ "@babel/runtime": "^7.0.0",
60
+ "debug": "^4.0.1",
53
61
  "decompress": "^4.2.0",
54
62
  "get-port": "^4.0.0",
55
63
  "getos": "^3.1.0",
@@ -60,16 +68,21 @@
60
68
  "tmp": "^0.0.33",
61
69
  "uuid": "^3.2.1"
62
70
  },
63
- "peerDependencies": {},
71
+ "peerDependencies": {
72
+ "mongodb": ">= 3.0"
73
+ },
64
74
  "scripts": {
65
75
  "build": "npm-run-all build:*",
66
76
  "build:cjs": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
67
77
  "build:flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
78
+ "build:ts": "find ./src -name '*.d.ts' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`; done",
68
79
  "watch": "jest --watch",
69
80
  "coverage": "jest --coverage",
70
- "lint": "eslint --ext .js ./src",
81
+ "lint": "eslint --ext .js ./src && npm run tslint",
71
82
  "flow": "./node_modules/.bin/flow",
72
- "test": "npm run coverage && npm run lint && npm run flow",
83
+ "test": "npm run coverage && npm run lint && npm run flow && npm run tscheck",
84
+ "tscheck": "tsc",
85
+ "tslint": "tslint -p . \"src/**/*.d.ts\"",
73
86
  "semantic-release": "semantic-release",
74
87
  "postinstall": "node ./postinstall.js"
75
88
  },