@jongodb/memory-server 0.1.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/LICENSE ADDED
@@ -0,0 +1,179 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+
package/README.md ADDED
@@ -0,0 +1,270 @@
1
+ # @jongodb/memory-server
2
+
3
+ Node test runtime adapter for `jongodb`.
4
+
5
+ It starts a local `jongodb` launcher process, exposes a MongoDB URI, and manages process lifecycle for test runners.
6
+
7
+ ## Scope
8
+
9
+ - integration-test runtime only
10
+ - launcher lifecycle management (start/stop/detach)
11
+ - framework-agnostic base helper
12
+ - Jest/Vitest/Nest Jest convenience helpers
13
+
14
+ This package is not a production MongoDB server.
15
+
16
+ ## Why This vs Embedded MongoDB (`mongodb-memory-server`)
17
+
18
+ `@jongodb/memory-server` can run in native binary mode instead of booting a full `mongod` test binary.
19
+
20
+ Practical effects:
21
+
22
+ - no `mongod` binary download/extract step in cold environments
23
+ - lower launcher startup overhead in many integration-test loops
24
+ - fewer environment issues tied to `mongod` binary provisioning
25
+
26
+ Rough performance expectation (not a guarantee):
27
+
28
+ - cold CI/dev machine: often much faster because there is no `mongod` artifact fetch/unpack
29
+ - warm cache environment: startup-phase wins are usually smaller
30
+
31
+ Observed benchmarks (NestJS integration suite subset, same machine):
32
+
33
+ - Benchmark A: suite-scoped lifecycle (historical)
34
+ - workload: `9 suites / 102 tests` (`--runInBand`)
35
+ - server boots: `9` times per run (once per suite)
36
+ - `mongodb-memory-server` real:
37
+ - run1: `28.17s`
38
+ - run2: `27.19s`
39
+ - average: `27.68s`
40
+ - `jongodb` native real:
41
+ - run1: `12.35s`
42
+ - run2: `12.04s`
43
+ - average: `12.20s`
44
+ - delta: about `55.9%` faster (`15.48s` less wall clock)
45
+ - Benchmark B: single-boot lifecycle (current)
46
+ - workload: `45 suites / 510 tests` (`--runInBand`)
47
+ - server boots: `1` time per run (shared for full Jest process)
48
+ - `mongodb-memory-server` real:
49
+ - run1: `92.40s`
50
+ - run2: `91.84s`
51
+ - average: `92.12s`
52
+ - `jongodb` native real:
53
+ - run1: `33.03s`
54
+ - run2: `32.90s`
55
+ - average: `32.97s`
56
+ - delta: about `64.2%` faster (`59.15s` less wall clock)
57
+
58
+ Your actual delta depends on test shape, I/O, and query workload.
59
+
60
+ ## Requirements
61
+
62
+ - Node.js 20+
63
+ - one launcher runtime:
64
+ - native binary, or
65
+ - Java 17+ with `jongodb` classpath
66
+
67
+ ## Install
68
+
69
+ ```bash
70
+ npm i -D @jongodb/memory-server
71
+ ```
72
+
73
+ ## Quick Start (Recommended)
74
+
75
+ Use `createJongodbEnvRuntime` and keep one runtime per test process.
76
+
77
+ ```ts
78
+ import { beforeAll, afterAll } from "@jest/globals";
79
+ import { createJongodbEnvRuntime } from "@jongodb/memory-server/runtime";
80
+
81
+ const runtime = createJongodbEnvRuntime({
82
+ databaseName: "test"
83
+ });
84
+
85
+ beforeAll(async () => {
86
+ await runtime.setup();
87
+ });
88
+
89
+ afterAll(async () => {
90
+ await runtime.teardown();
91
+ });
92
+ ```
93
+
94
+ Behavior:
95
+
96
+ - `setup()` starts launcher and writes `process.env.MONGODB_URI` (default)
97
+ - `teardown()` stops launcher and restores previous env value
98
+
99
+ ## Launcher Modes
100
+
101
+ `launchMode` values:
102
+
103
+ - `auto` (default): binary first, Java fallback
104
+ - `binary`: binary only, fail fast if binary not resolvable
105
+ - `java`: Java only, fail fast if classpath not configured
106
+
107
+ In `auto` mode, launch failures are attempted in order and reported with source tags.
108
+
109
+ ## Binary Runtime
110
+
111
+ Binary resolution order:
112
+
113
+ 1. `options.binaryPath`
114
+ 2. `JONGODB_BINARY_PATH`
115
+ 3. bundled platform package (if installed)
116
+
117
+ Current bundled package targets:
118
+
119
+ - `@jongodb/memory-server-bin-darwin-arm64`
120
+ - `@jongodb/memory-server-bin-linux-x64-gnu`
121
+ - `@jongodb/memory-server-bin-win32-x64`
122
+
123
+ Version policy:
124
+
125
+ - binary package versions are pinned to the core package version (`optionalDependencies`)
126
+ - if a bundled binary is not installed/available for your version, `auto` mode can still run with Java fallback
127
+
128
+ Binary-only example:
129
+
130
+ ```ts
131
+ import { startJongodbMemoryServer } from "@jongodb/memory-server";
132
+
133
+ const server = await startJongodbMemoryServer({
134
+ launchMode: "binary",
135
+ binaryPath: process.env.JONGODB_BINARY_PATH
136
+ });
137
+ ```
138
+
139
+ ## Java Runtime
140
+
141
+ Provide classpath by:
142
+
143
+ 1. `startJongodbMemoryServer({ classpath: "..." })`, or
144
+ 2. `JONGODB_CLASSPATH`
145
+
146
+ Repository helper command (when using this repo launcher build):
147
+
148
+ ```bash
149
+ ./.tooling/gradle-8.10.2/bin/gradle -q printLauncherClasspath
150
+ ```
151
+
152
+ ## Standalone Launcher Contract
153
+
154
+ Binary and Java launchers follow one contract:
155
+
156
+ - readiness line on stdout: `JONGODB_URI=mongodb://...`
157
+ - optional failure line on stderr: `JONGODB_START_FAILURE=...`
158
+ - process remains alive until terminated by signal (`SIGTERM`/`SIGINT`)
159
+
160
+ The adapter relies on this contract for deterministic startup/teardown handling.
161
+
162
+ ## Runner Helpers
163
+
164
+ ### Jest (per-file hooks)
165
+
166
+ ```ts
167
+ import { beforeAll, afterAll } from "@jest/globals";
168
+ import { registerJongodbForJest } from "@jongodb/memory-server/jest";
169
+
170
+ registerJongodbForJest({ beforeAll, afterAll });
171
+ ```
172
+
173
+ ### Jest (global setup/teardown)
174
+
175
+ `jest.global-setup.ts`:
176
+
177
+ ```ts
178
+ import { createJestGlobalSetup } from "@jongodb/memory-server/jest";
179
+
180
+ export default createJestGlobalSetup();
181
+ ```
182
+
183
+ `jest.global-teardown.ts`:
184
+
185
+ ```ts
186
+ import { createJestGlobalTeardown } from "@jongodb/memory-server/jest";
187
+
188
+ export default createJestGlobalTeardown();
189
+ ```
190
+
191
+ ### Vitest
192
+
193
+ ```ts
194
+ import { beforeAll, afterAll } from "vitest";
195
+ import { registerJongodbForVitest } from "@jongodb/memory-server/vitest";
196
+
197
+ registerJongodbForVitest({ beforeAll, afterAll });
198
+ ```
199
+
200
+ ### NestJS (Jest E2E)
201
+
202
+ ```ts
203
+ import { beforeAll, afterAll } from "@jest/globals";
204
+ import { registerJongodbForNestJest } from "@jongodb/memory-server/nestjs";
205
+
206
+ registerJongodbForNestJest({ beforeAll, afterAll });
207
+ ```
208
+
209
+ ## Common Integration Patterns
210
+
211
+ - NestJS + Mongoose: `MongooseModule.forRootAsync({ useFactory: () => ({ uri: process.env.MONGODB_URI }) })`
212
+ - Express/Fastify: inject `process.env.MONGODB_URI` into DB bootstrap before app init
213
+ - Prisma (Mongo): set `envVarName: "DATABASE_URL"` for runtime helper
214
+ - TypeORM (Mongo): pass runtime URI as `url`
215
+
216
+ ## API Surface
217
+
218
+ Main export (`@jongodb/memory-server`):
219
+
220
+ - `startJongodbMemoryServer(options?)`
221
+
222
+ Runtime export (`@jongodb/memory-server/runtime`):
223
+
224
+ - `createJongodbEnvRuntime(options?)`
225
+
226
+ Jest export (`@jongodb/memory-server/jest`):
227
+
228
+ - `registerJongodbForJest(hooks, options?)`
229
+ - `createJestGlobalSetup(options?)`
230
+ - `createJestGlobalTeardown(options?)`
231
+ - `readJestGlobalState(options?)`
232
+ - `readJestGlobalUri(options?)`
233
+
234
+ Nest export (`@jongodb/memory-server/nestjs`):
235
+
236
+ - `registerJongodbForNestJest(hooks, options?)`
237
+
238
+ Vitest export (`@jongodb/memory-server/vitest`):
239
+
240
+ - `registerJongodbForVitest(hooks, options?)`
241
+
242
+ ## Options Reference
243
+
244
+ Core options:
245
+
246
+ - `launchMode`: `auto` | `binary` | `java` (default: `auto`)
247
+ - `binaryPath`: binary executable path
248
+ - `classpath`: Java classpath string or string array
249
+ - `javaPath`: Java executable path (default: `java`)
250
+ - `launcherClass`: Java launcher class (default: `org.jongodb.server.TcpMongoServerLauncher`)
251
+ - `databaseName`: default DB in generated URI (default: `test`)
252
+ - `host`: bind host (default: `127.0.0.1`)
253
+ - `port`: bind port (`0` for ephemeral, default: `0`)
254
+ - `startupTimeoutMs`: startup timeout (default: `15000`)
255
+ - `stopTimeoutMs`: stop timeout before forced kill (default: `5000`)
256
+ - `env`: additional child process env vars
257
+ - `logLevel`: `silent` | `info` | `debug` (default: `silent`)
258
+ - `envVarName` (runtime/jest/vitest helpers): target env key (default: `MONGODB_URI`)
259
+
260
+ ## Troubleshooting
261
+
262
+ - `No launcher runtime configured`: set binary path/env or classpath/env
263
+ - `Binary launch mode requested but no binary was found`: provide `binaryPath` or `JONGODB_BINARY_PATH`
264
+ - `Java launch mode requested but Java classpath is not configured`: provide `classpath` or `JONGODB_CLASSPATH`
265
+ - `spawn ... ENOENT`: missing binary/java executable path
266
+ - startup timeout: launcher did not emit `JONGODB_URI=...`
267
+
268
+ Parallel test tip:
269
+
270
+ - use unique `databaseName` per worker/process to avoid data collisions
@@ -0,0 +1,25 @@
1
+ type LogLevel = "silent" | "info" | "debug";
2
+ type LaunchMode = "auto" | "binary" | "java";
3
+ export interface JongodbMemoryServerOptions {
4
+ host?: string;
5
+ port?: number;
6
+ databaseName?: string;
7
+ startupTimeoutMs?: number;
8
+ stopTimeoutMs?: number;
9
+ javaPath?: string;
10
+ launcherClass?: string;
11
+ classpath?: string | string[];
12
+ binaryPath?: string;
13
+ launchMode?: LaunchMode;
14
+ env?: Record<string, string>;
15
+ logLevel?: LogLevel;
16
+ }
17
+ export interface JongodbMemoryServer {
18
+ readonly uri: string;
19
+ readonly pid: number;
20
+ detach(): void;
21
+ stop(): Promise<void>;
22
+ }
23
+ export declare function startJongodbMemoryServer(options?: JongodbMemoryServerOptions): Promise<JongodbMemoryServer>;
24
+ export {};
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA,KAAK,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,CAAC;AAC5C,KAAK,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE7C,MAAM,WAAW,0BAA0B;IACzC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,MAAM,IAAI,IAAI,CAAC;IACf,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAcD,wBAAsB,wBAAwB,CAC5C,OAAO,GAAE,0BAA+B,GACvC,OAAO,CAAC,mBAAmB,CAAC,CA6C9B"}