Skip to content

Commit

Permalink
Merge branch 'feature/10.0' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Jun 24, 2024
2 parents 1734dc9 + 139cfde commit 6e01bef
Show file tree
Hide file tree
Showing 38 changed files with 723 additions and 596 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x, 22.x]
node-version: [16.x, 18.x, 20.x, 22.x]
steps:
- uses: actions/checkout@v4
if: github.event.inputs.git-ref == ''
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
env:
CI: true
- name: Send codecov.io stats
if: matrix.node-version == '14.x'
if: matrix.node-version == '16.x'
uses: codecov/codecov-action@v3

publish:
Expand All @@ -61,10 +61,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 18
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x
- name: Install node_modules
run: yarn install
- name: Build
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ Choose any package, because they are the same. They differ only in the default c

### Requirements

- NodeJS: 14.20.1+
- Typescript: 5.0+ (if used)
- NodeJS: 16.20.1+
- Typescript: 5.3+ (if used)

And one of those (on Linux):

Expand All @@ -86,7 +86,7 @@ On Linux, you will also need `libcurl4` (or `libcurl3` on some older distro vers

### Configuring which mongod binary to use

The default behavior is that version `6.0.14` for your OS will be downloaded. By setting [Environment variables](https://nodkz.github.io/mongodb-memory-server/docs/api/config-options) you are able to specify which version and binary will be downloaded:
The default behavior is that version `7.0.9` for your OS will be downloaded. By setting [Environment variables](https://nodkz.github.io/mongodb-memory-server/docs/api/config-options) you are able to specify which version and binary will be downloaded:

```sh
export MONGOMS_DOWNLOAD_URL=https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz
Expand Down Expand Up @@ -128,7 +128,7 @@ const mongod = new MongoMemoryServer({
auth?: boolean, // add "--auth" argument, dont use this directly use top-level "auth"
},
binary?: {
version?: string, // by default '6.0.14'
version?: string, // by default '7.0.9'
downloadDir?: string, // see the documentation on what is chosen by default https://nodkz.github.io/mongodb-memory-server/docs/api/config-options#download_dir
platform?: string, // by default os.platform()
arch?: string, // by default os.arch()
Expand Down
16 changes: 1 addition & 15 deletions docs/api/config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Example: `ubuntu-18.04`

Option `VERSION` is used to set what mongodb version should be downloaded

Default: `6.0.14` (see [Mongodb Server Versions](../guides/mongodb-server-versions.md) for a complete list and policy)
Default: `7.0.9` (see [Mongodb Server Versions](../guides/mongodb-server-versions.md) for a complete list and policy)

This Option does not have a effect when [`ARCHIVE_NAME`](#archive_name) or [`DOWNLOAD_URL`](#download_url) is defined.

Expand Down Expand Up @@ -269,20 +269,6 @@ Also see [ARCHIVE_NAME](#archive_name).
Keep in mind that downloaded binaries will never be automatically deleted.
:::

### EXP_NET0LISTEN

| Environment Variable | PackageJson |
| :------------------: | :---------: |
| `MONGOMS_EXP_NET0LISTEN` | `expNet0Listen` |

Option `EXP_NET0LISTEN` is used to use the experimental (non-predictable) port generation of `net.listen`.

This option will use a random open port, which will lessen the "port is already in use" errors, but will not eliminate them.

This is a experimental option, it maybe removed, renamed or have changed behavior in the future.

Default: `false`

## How to use them in the package.json

To use the config options in the `package.json`, they need to be camelCased (and without `_`), and need to be in the property `config.mongodbMemoryServer`
Expand Down
20 changes: 20 additions & 0 deletions docs/api/interfaces/mongo-dispose-opts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: mongo-memory-dispose-opts
title: 'DisposeOptions'
---

API Documentation of `DisposeOptions`-Interface

## Values for `DisposeOptions`

### enabled

Typings: `enabled?: boolean`

Set whether to stop the manager on `[Symbol.asyncDispose]` calls.

### cleanup

Typings: `cleanup?: Cleanup`

Set custom cleanup options to be used for disposal, see [`cleanup` function](../classes/mongo-memory-server.md#cleanup) (the same options apply for the replset).
6 changes: 6 additions & 0 deletions docs/api/interfaces/mongo-memory-server-opts.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ Set custom spawn options for spawning processes, uses [`SpawnOptions`](https://n
Typings: `auth?: AutomaticAuth`

Set custom Authentication options for the instance, uses [`AutomaticAuth`](./mongo-memory-server-automaticauth.md).

### dispose

Typings: `dispose?: DisposeOptions`

Set custom behavior for when `[Symbol.asyncDispose]` is called, uses [`DisposeOptions`](./mongo-dispose-opts.md).
6 changes: 6 additions & 0 deletions docs/api/interfaces/replset-opts.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ Set how many ReplSet members to spawn, this number will be deducted from length
:::tip
It is recommended to set this number to a **odd** number, and try to never have it be **even**, see [MongoDB Deploy an Odd Number of Members](https://www.mongodb.com/docs/v5.2/core/replica-set-architectures/#deploy-an-odd-number-of-members).
:::

### dispose

Typings: `dispose?: DisposeOptions`

Set custom behavior for when `[Symbol.asyncDispose]` is called, uses [`DisposeOptions`](./mongo-dispose-opts.md).
11 changes: 11 additions & 0 deletions docs/guides/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ Since `8.4.0` objects can also be used instead of just booleans for parameter in

For Example `.stop({ doCleanup: false })` can be used instead of `.stop(false)`.
:::

### Does this package support Explicit Resource Management?

Yes, `[Symbol.asyncDispose]` is implemented for all manager classes, behavior can be configured via `dispose` options:

- [`MongoMemoryServerOpts.dispose`](../api/interfaces/mongo-memory-server-opts.md#dispose)
- [`ReplSetOpts.dispose`](../api/interfaces/replset-opts.md#dispose)

:::note
Note that when using `await using server =` that `[Symbol.asyncDispose]` is called at the end of the scope even if the value is reassigned to something out of the current scope.
:::
42 changes: 42 additions & 0 deletions docs/guides/migration/migrate10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
id: migrate10
title: 'Migrate to version 10.0.0'
---

Here are the Important changes made for 10.0.0

:::caution Important, Read this first
This Guide is written for migration from version `9.4.0` to `10.0.0`, for versions `>10.0.0 <11.0.0`, please consult the [CHANGELOG](https://github.com/nodkz/mongodb-memory-server/blob/master/CHANGELOG.md)
:::

## Breaking Changes

### Minimal NodeJS version is now `16`

With 10.0.0 the minimal nodejs required is `16.20.1`.

### Mongodb Driver Version upgraded to 6.x

The used MongoDB Driver version is now `6.7.0`.

### Default binary version is now 7.x

The default binary version has been upgraded from `6.0.x` to `7.0.x`. For more specifics see [mongodb-server-versions](../mongodb-server-versions.md).

:::note
In mongodb `7.0.0` storage engine `ephemeralForTest` has been removed, mongodb-memory-server will automatically translate any occurrence to `wiredTiger` with a warning.

It is recommended to run the tests against a tmpfs or equivalent (default `/tmp` on linux / macos).
:::

## Non-Breaking changes / Additions

### Compiler target is now `es2021`

The tsconfig `target` option has been updated to `es2021`, which will result in less polyfills.
This should be a non-breaking change.

### Getport now uses `net0listen` by default

This means a port is now generated by the engine (like nodejs) itself, previously known as `EXP_NET0LISTEN`.
This should reduce amount of `Max port tries exceeded` errors.
1 change: 1 addition & 0 deletions docs/guides/mongodb-server-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Starting with MongoDB version 5.0, the default versions for `mongodb-memory-serv

| `mongodb-memory-server-core` Version | Default MongoDB Version |
| :----------------------------------: | :---------------------: |
| 10.0.x - 10.0.x | 7.0.9 |
| 9.2.x - 9.2.x | 6.0.14 |
| 9.0.x - 9.1.x | 6.0.9 |
| 8.14.x - 8.16.x | 5.0.19 |
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/quick-start-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ If Top-Level async-await is not available for your case, the code can in most ca

## Requirements

- NodeJS: 14.20.1+
- Typescript: 5.0+ (if used)
- NodeJS: 16.20.1+
- Typescript: 5.3+ (if used)

When on Linux, one of the following are required:

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/supported-systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Default version is `1`
(uses mongodb's `ubuntu` release)<br/>
Lowest supported Distribution version is `3` (or `0.3`)<br/>
Highest version is `7`<br/>
Default version is `6`
Default version is `7`

### Linux Mint

Expand All @@ -127,7 +127,7 @@ Default version is `6`
(uses mongodb's `ubuntu` release)<br/>
Lowest supported Distribution version is `17`<br/>
Highest version is `21`<br/>
Default version is `20`
Default version is `21`

### Suse

Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"@commitlint/cli": "17.8.1",
"@commitlint/config-conventional": "17.8.1",
"@types/jest": "^29.5.12",
"@types/node": "~14.18.63",
"@typescript-eslint/eslint-plugin": "5.59.9",
"@typescript-eslint/parser": "5.59.9",
"@types/node": "~16.11.7",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0",
"commitlint": "17.8.1",
"cross-env": "7.0.3",
"doctoc": "2.2.1",
Expand All @@ -22,11 +22,11 @@
"eslint-plugin-prettier": "5.1.3",
"husky": "8.0.3",
"jest": "29.7.0",
"lint-staged": "13.2.3",
"prettier": "3.2.5",
"lint-staged": "14.0.1",
"prettier": "3.3.0",
"ts-jest": "29.1.4",
"typedoc": "^0.24.8",
"typescript": "5.0.4"
"typedoc": "^0.25.13",
"typescript": "~5.3.3"
},
"workspaces": {
"packages": [
Expand Down
6 changes: 3 additions & 3 deletions packages/mongodb-memory-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"directory": "packages/mongodb-memory-server-core"
},
"engines": {
"node": ">=14.20.1"
"node": ">=16.20.1"
},
"files": [
"lib",
Expand Down Expand Up @@ -43,13 +43,13 @@
"yazl": "^2.5.1"
},
"dependencies": {
"async-mutex": "^0.4.1",
"async-mutex": "^0.5.0",
"camelcase": "^6.3.0",
"debug": "^4.3.5",
"find-cache-dir": "^3.3.2",
"follow-redirects": "^1.15.6",
"https-proxy-agent": "^7.0.4",
"mongodb": "^5.9.2",
"mongodb": "^6.7.0",
"new-find-package-json": "^2.0.0",
"semver": "^7.6.2",
"tar-stream": "^3.1.7",
Expand Down
31 changes: 20 additions & 11 deletions packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { EventEmitter } from 'events';
import { MongoMemoryServer, AutomaticAuth, MongoMemoryServerOpts } from './MongoMemoryServer';
import {
MongoMemoryServer,
AutomaticAuth,
MongoMemoryServerOpts,
DisposeOptions,
} from './MongoMemoryServer';
import {
assertion,
authDefault,
Expand Down Expand Up @@ -92,6 +97,10 @@ export interface ReplSetOpts {
* @default {}
*/
configSettings?: MongoMemoryReplSetConfigSettings;
/**
* Options for automatic dispose for "Explicit Resource Management"
*/
dispose?: DisposeOptions;
}

/**
Expand Down Expand Up @@ -140,6 +149,7 @@ export enum MongoMemoryReplSetEvents {
stateChange = 'stateChange',
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface MongoMemoryReplSet extends EventEmitter {
// Overwrite EventEmitter's definitions (to provide at least the event names)
emit(event: MongoMemoryReplSetEvents, ...args: any[]): boolean;
Expand All @@ -150,6 +160,7 @@ export interface MongoMemoryReplSet extends EventEmitter {
/**
* Class for managing an replSet
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced {
/**
* All servers this ReplSet instance manages
Expand Down Expand Up @@ -262,6 +273,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
spawn: {},
storageEngine,
configSettings: {},
dispose: {},
};
// force overwrite "storageEngine" because it is transformed already
this._replSetOpts = { ...defaults, ...val, storageEngine };
Expand Down Expand Up @@ -522,11 +534,6 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
/** Default to cleanup temporary, but not custom dbpaths */
let cleanup: Cleanup = { doCleanup: true, force: false };

// TODO: for next major release (10.0), this should be removed
if (typeof cleanupOptions === 'boolean') {
throw new Error('Unsupported argument type: boolean');
}

// handle the new way of setting what and how to cleanup
if (typeof cleanupOptions === 'object') {
cleanup = cleanupOptions;
Expand Down Expand Up @@ -577,11 +584,6 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
/** Default to doing cleanup, but not forcing it */
let cleanup: Cleanup = { doCleanup: true, force: false };

// TODO: for next major release (10.0), this should be removed
if (typeof options === 'boolean') {
throw new Error('Unsupported argument type: boolean');
}

// handle the new way of setting what and how to cleanup
if (typeof options === 'object') {
cleanup = options;
Expand Down Expand Up @@ -797,6 +799,13 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced

log('_waitForPrimary: detected one primary instance ');
}

// Symbol for "Explicit Resource Management"
async [Symbol.asyncDispose]() {
if (this.replSetOpts.dispose?.enabled ?? true) {
await this.stop(this.replSetOpts.dispose?.cleanup);
}
}
}

export default MongoMemoryReplSet;
Expand Down
Loading

0 comments on commit 6e01bef

Please sign in to comment.