@exisjs/mongo 0.0.0-stage → 0.11.0

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Prasanth
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,65 @@
1
- # Temporary Holding Version
1
+ # @exisjs/mongo
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm @exisjs/mongo package](https://img.shields.io/npm/v/@exisjs/mongo.svg)](https://npmjs.org/package/@exisjs/mongo)
4
+ [![npm license](https://img.shields.io/npm/l/@exisjs/mongo.svg)](https://github.com/v25group/exisjs/blob/main/LICENSE)
5
+
6
+ MongoDB stores for [ExisJS](https://github.com/v25group/exisjs), so every instance of an app shares one set of rate limits, idempotency keys, cron locks and `@Cacheable` results. Built on the native `mongodb` driver (6.x or 7.x); works with Mongoose.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @exisjs/mongo mongodb
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```typescript
17
+ import { MongoClient } from 'mongodb'
18
+ import { mongoStores } from '@exisjs/mongo'
19
+ import { rateLimit, idempotent } from 'exisjs/middleware'
20
+
21
+ const client = new MongoClient(process.env.MONGODB_URL!)
22
+ const stores = mongoStores(client.db('app')) // collections named exis_*
23
+
24
+ rateLimit({ max: 100, windowMs: 60_000, store: stores.rateLimit })
25
+ idempotent({ store: stores.idempotency })
26
+ app.cron.lock = stores.cronLock
27
+ // exis.config.ts: defineConfig({ cache: { store: stores.cache } })
28
+ ```
29
+
30
+ | Export | Implements | Default collection(s) |
31
+ | --------------------------- | ----------------------------------------------------- | --------------------------------------------- |
32
+ | `MongoRateLimitStore` | `RateLimitStore` (`increment`, `get`, `reset`) | `exis_rate_limits` |
33
+ | `MongoIdempotencyStore` | `IdempotencyStore` (`get`, `set`, `claim`, `release`) | `exis_idempotency`, `exis_idempotency_claims` |
34
+ | `MongoCronLock` | `CronLock` (`acquire`, `release`) | `exis_cron_locks` |
35
+ | `MongoCacheStore` | `CacheStore` (`get`, `set`, `delete`) | `exis_cache` |
36
+ | `mongoStores(db, options?)` | all four on one `Db`, plus `ensureIndexes()` | prefix `exis_` |
37
+
38
+ Every constructor takes `(db, options?)` with `prefix`, `collection` and `autoIndex`. `MongoIdempotencyStore` also takes `claimsCollection` and `ttlMs` (default 24 h), `MongoCacheStore` takes `ttlMs` (default 5 min), and `MongoCronLock` takes `instance` (a label saved with the lock).
39
+
40
+ ### Mongoose
41
+
42
+ Pass the native database after connecting:
43
+
44
+ ```typescript
45
+ await mongoose.connect(process.env.MONGODB_URL!)
46
+ const stores = mongoStores(mongoose.connection.db!)
47
+ ```
48
+
49
+ ### Indexes
50
+
51
+ Each collection gets a TTL index on `expiresAt` (`exis_ttl`), created on first use. Call `await stores.ensureIndexes()` to create them up front, or pass `autoIndex: false` and run it from a migration. Expired documents are ignored at once, without waiting for MongoDB's TTL monitor.
52
+
53
+ ### Atomicity
54
+
55
+ Rate limit hits, idempotency claims and cron lock acquires are each one atomic `findOneAndUpdate` with upsert, timed by the database server's clock. Each class passes the `runStoreConformance` suites from `exisjs/testing`.
56
+
57
+ For money, a domain-level unique key (a unique index on your payments) is still the strongest guard; use idempotency keys on top of it.
58
+
59
+ ## Documentation
60
+
61
+ See [Shared stores with MongoDB](https://github.com/v25group/exisjs/blob/main/docs/content/mongo.mdx).
62
+
63
+ ## License
64
+
65
+ MIT
@@ -0,0 +1,40 @@
1
+ import type { Collection, Db } from 'mongodb';
2
+ import type { CacheStore } from 'exisjs/middleware';
3
+ import { type MongoStoreBaseOptions } from './shared';
4
+ export interface MongoCacheStoreOptions extends MongoStoreBaseOptions {
5
+ /** Collection name. Default `<prefix>cache` */
6
+ collection?: string;
7
+ /** TTL when `set` gets none, in milliseconds. Default 5 minutes */
8
+ ttlMs?: number;
9
+ }
10
+ interface CacheDoc {
11
+ _id: string;
12
+ value: string;
13
+ expiresAt: Date;
14
+ }
15
+ /**
16
+ * The `@Cacheable` cache, shared by every instance, so a `@CacheEvict` on
17
+ * one instance reaches them all. Values are saved as JSON text: cache only
18
+ * JSON-safe data (a `Date` comes back as a string). A document holds at
19
+ * most 16 MB.
20
+ *
21
+ * @example exis.config.ts
22
+ * ```ts
23
+ * export default defineConfig({ cache: { store: new MongoCacheStore(db) } })
24
+ * ```
25
+ */
26
+ export declare class MongoCacheStore implements CacheStore {
27
+ readonly collection: Collection<CacheDoc>;
28
+ private readonly defaultTtlMs;
29
+ private readonly autoIndex;
30
+ private readonly indexes;
31
+ constructor(db: Db, options?: MongoCacheStoreOptions);
32
+ /** Creates the TTL index that deletes expired entries. Safe to call again. */
33
+ ensureIndexes(): Promise<void>;
34
+ private ready;
35
+ get(key: string): Promise<unknown>;
36
+ set(key: string, value: unknown, ttlMs?: number): Promise<void>;
37
+ delete(key: string): Promise<void>;
38
+ }
39
+ export {};
40
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAUL,KAAK,qBAAqB,EAC3B,MAAM,UAAU,CAAA;AAEjB,MAAM,WAAW,sBAAuB,SAAQ,qBAAqB;IACnE,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,mEAAmE;IACnE,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,UAAU,QAAQ;IAChB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,IAAI,CAAA;CAChB;AAED;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,YAAW,UAAU;IAChD,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAA;IACzC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAQ;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;gBAEjC,EAAE,EAAE,EAAE,EAAE,OAAO,GAAE,sBAA2B;IASxD,8EAA8E;IAC9E,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;YAIhB,KAAK;IAIb,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAMlC,GAAG,CACP,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,OAAO,EACd,KAAK,SAAoB,GACxB,OAAO,CAAC,IAAI,CAAC;IAqBV,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIzC"}
package/dist/cache.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoCacheStore = void 0;
4
+ const shared_1 = require("./shared");
5
+ /**
6
+ * The `@Cacheable` cache, shared by every instance, so a `@CacheEvict` on
7
+ * one instance reaches them all. Values are saved as JSON text: cache only
8
+ * JSON-safe data (a `Date` comes back as a string). A document holds at
9
+ * most 16 MB.
10
+ *
11
+ * @example exis.config.ts
12
+ * ```ts
13
+ * export default defineConfig({ cache: { store: new MongoCacheStore(db) } })
14
+ * ```
15
+ */
16
+ class MongoCacheStore {
17
+ constructor(db, options = {}) {
18
+ this.collection = (0, shared_1.collectionFor)(db, options, 'cache');
19
+ this.defaultTtlMs = options.ttlMs ?? 300000;
20
+ this.autoIndex = options.autoIndex ?? true;
21
+ this.indexes = (0, shared_1.once)(async () => {
22
+ await (0, shared_1.ttlIndex)(this.collection);
23
+ });
24
+ }
25
+ /** Creates the TTL index that deletes expired entries. Safe to call again. */
26
+ ensureIndexes() {
27
+ return this.indexes();
28
+ }
29
+ async ready() {
30
+ if (this.autoIndex)
31
+ await this.indexes();
32
+ }
33
+ async get(key) {
34
+ await this.ready();
35
+ const doc = await this.collection.findOne({ _id: key, ...shared_1.notExpired });
36
+ return doc ? (0, shared_1.decode)(doc.value) : undefined;
37
+ }
38
+ async set(key, value, ttlMs = this.defaultTtlMs) {
39
+ // undefined (or anything JSON cannot hold) means "not cached"
40
+ const text = (0, shared_1.encode)(value);
41
+ if (text === undefined)
42
+ return this.delete(key);
43
+ await this.ready();
44
+ await (0, shared_1.retryOnDuplicate)(() => this.collection.updateOne({ _id: key }, [
45
+ {
46
+ $set: {
47
+ value: { $literal: text },
48
+ [shared_1.EXPIRES_FIELD]: (0, shared_1.nowPlus)(ttlMs),
49
+ },
50
+ },
51
+ ], { upsert: true }));
52
+ }
53
+ async delete(key) {
54
+ await this.ready();
55
+ await this.collection.deleteOne({ _id: key });
56
+ }
57
+ }
58
+ exports.MongoCache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M,IAAI,GAAG,IAAA,eAAM,EAAC,KAAK,CAAC,CAAA;QAC1B,IAAI,IAAI,KAAK,SAAS;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC/C,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;QAClB,MAAM,IAAA,yBAAgB,EAAC,GAAG,EAAE,CAC1B,IAAI,CAAC,UAAU,CAAC,SAAS,CACvB,EAAE,GAAG,EAAE,GAAG,EAAE,EACZ;YACE;gBACE,IAAI,EAAE;oBACJ,KAAK,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE;oBACzB,CAAC,sBAAa,CAAC,EAAE,IAAA,gBAAO,EAAC,KAAK,CAAC;iBAChC;aACF;SACF,EACD,EAAE,MAAM,EAAE,IAAI,EAAE,CACjB,CACF,CAAA;IACH,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW;QACtB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;QAClB,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAA;IAC/C,CAAC;CACF;AA3DD,0CA2DC"}
@@ -0,0 +1,40 @@
1
+ import type { Collection, Db } from 'mongodb';
2
+ import type { CronLock } from 'exisjs/cron';
3
+ import { type MongoStoreBaseOptions } from './shared';
4
+ export interface MongoCronLockOptions extends MongoStoreBaseOptions {
5
+ /** Collection name. Default `<prefix>cron_locks` */
6
+ collection?: string;
7
+ /** Label saved with each lock to show which instance holds it. Default `<hostname>:<pid>` */
8
+ instance?: string;
9
+ }
10
+ interface LockDoc {
11
+ _id: string;
12
+ owner: string;
13
+ instance: string;
14
+ expiresAt: Date;
15
+ acquiredAt: Date;
16
+ }
17
+ /**
18
+ * Lets one instance run each cron tick. `acquire` is one atomic
19
+ * `findOneAndUpdate` (upsert) that takes the job's lock when it is free or
20
+ * expired; `release` drops it only if this instance still holds it.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * app.cron.lock = new MongoCronLock(db)
25
+ * ```
26
+ */
27
+ export declare class MongoCronLock implements CronLock {
28
+ readonly collection: Collection<LockDoc>;
29
+ readonly instance: string;
30
+ private readonly autoIndex;
31
+ private readonly indexes;
32
+ private readonly held;
33
+ constructor(db: Db, options?: MongoCronLockOptions);
34
+ /** Creates the TTL index that deletes expired locks. Safe to call again. */
35
+ ensureIndexes(): Promise<void>;
36
+ acquire(jobName: string, ttlMs: number): Promise<boolean>;
37
+ release(jobName: string): Promise<void>;
38
+ }
39
+ export {};
40
+ //# sourceMappingURL=cron-lock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cron-lock.d.ts","sourceRoot":"","sources":["../src/cron-lock.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAQL,KAAK,qBAAqB,EAC3B,MAAM,UAAU,CAAA;AAEjB,MAAM,WAAW,oBAAqB,SAAQ,qBAAqB;IACjE,oDAAoD;IACpD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,6FAA6F;IAC7F,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,UAAU,OAAO;IACf,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,IAAI,CAAA;IACf,UAAU,EAAE,IAAI,CAAA;CACjB;AAED;;;;;;;;;GASG;AACH,qBAAa,aAAc,YAAW,QAAQ;IAC5C,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;IACxC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAE7C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA4B;gBAErC,EAAE,EAAE,EAAE,EAAE,OAAO,GAAE,oBAAyB;IAStD,4EAA4E;IAC5E,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA4BzD,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAM9C"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoCronLock = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const node_os_1 = require("node:os");
6
+ const shared_1 = require("./shared");
7
+ /**
8
+ * Lets one instance run each cron tick. `acquire` is one atomic
9
+ * `findOneAndUpdate` (upsert) that takes the job's lock when it is free or
10
+ * expired; `release` drops it only if this instance still holds it.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * app.cron.lock = new MongoCronLock(db)
15
+ * ```
16
+ */
17
+ class MongoCronLock {
18
+ constructor(db, options = {}) {
19
+ // Lock tokens this process holds, by job
20
+ this.held = new Map();
21
+ this.collection = (0, shared_1.collectionFor)(db, options, 'cron_locks');
22
+ this.instance = options.instance ?? `${(0, node_os_1.hostname)()}:${process.pid}`;
23
+ this.autoIndex = options.autoIndex ?? true;
24
+ this.indexes = (0, shared_1.once)(async () => {
25
+ await (0, shared_1.ttlIndex)(this.collection);
26
+ });
27
+ }
28
+ /** Creates the TTL index that deletes expired locks. Safe to call again. */
29
+ ensureIndexes() {
30
+ return this.indexes();
31
+ }
32
+ async acquire(jobName, ttlMs) {
33
+ if (this.autoIndex)
34
+ await this.indexes();
35
+ const token = (0, node_crypto_1.randomUUID)();
36
+ // Every field changes only when the lock is free or expired
37
+ const take = (mine, theirs) => ({
38
+ $cond: [shared_1.expired, mine, theirs],
39
+ });
40
+ const doc = await (0, shared_1.retryOnDuplicate)(() => this.collection.findOneAndUpdate({ _id: jobName }, [
41
+ {
42
+ $set: {
43
+ owner: take({ $literal: token }, '$owner'),
44
+ instance: take({ $literal: this.instance }, '$instance'),
45
+ acquiredAt: take('$$NOW', '$acquiredAt'),
46
+ [shared_1.EXPIRES_FIELD]: take((0, shared_1.nowPlus)(ttlMs), `$${shared_1.EXPIRES_FIELD}`),
47
+ },
48
+ },
49
+ ], { upsert: true, returnDocument: 'after' }));
50
+ if (doc?.owner !== token)
51
+ return false;
52
+ this.held.set(jobName, token);
53
+ return true;
54
+ }
55
+ async release(jobName) {
56
+ const token = this.held.get(jobName);
57
+ if (token === undefined)
58
+ return;
59
+ this.held.delete(jobName);
60
+ await this.collection.deleteOne({ _id: jobName, owner: token });
61
+ }
62
+ }
63
+ exports.MongoCronLock = MongoCronLock;
64
+ //# sourceMappingURL=cron-lock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cron-lock.js","sourceRoot":"","sources":["../src/cron-lock.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AACxC,qCAAkC;AAGlC,qCASiB;AAiBjB;;;;;;;;;GASG;AACH,MAAa,aAAa;IAQxB,YAAY,EAAM,EAAE,UAAgC,EAAE;QAHtD,yCAAyC;QACxB,SAAI,GAAG,IAAI,GAAG,EAAkB,CAAA;QAG/C,IAAI,CAAC,UAAU,GAAG,IAAA,sBAAa,EAAU,EAAE,EAAE,OAAO,EAAE,YAAY,CAAC,CAAA;QACnE,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,GAAG,IAAA,kBAAQ,GAAE,IAAI,OAAO,CAAC,GAAG,EAAE,CAAA;QAClE,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAA;QAC1C,IAAI,CAAC,OAAO,GAAG,IAAA,aAAI,EAAC,KAAK,IAAI,EAAE;YAC7B,MAAM,IAAA,iBAAQ,EAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACjC,CAAC,CAAC,CAAA;IACJ,CAAC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AC3B,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACpC,IAAI,KAAK,KAAK,SAAS;YAAE,OAAM;QAC/B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QACzB,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;IACjE,CAAC;CACF;AAxDD,sCAwDC"}
@@ -0,0 +1,54 @@
1
+ import type { Collection, Db } from 'mongodb';
2
+ import type { IdempotencyRecord, IdempotencyStore } from 'exisjs/middleware';
3
+ import { type MongoStoreBaseOptions } from './shared';
4
+ export interface MongoIdempotencyStoreOptions extends MongoStoreBaseOptions {
5
+ /** Collection for stored responses. Default `<prefix>idempotency` */
6
+ collection?: string;
7
+ /** Collection for in-progress claims. Default `<prefix>idempotency_claims` */
8
+ claimsCollection?: string;
9
+ /** TTL when `set` gets none, in milliseconds. Default 24 hours */
10
+ ttlMs?: number;
11
+ }
12
+ interface RecordDoc {
13
+ _id: string;
14
+ value: string;
15
+ expiresAt: Date;
16
+ }
17
+ interface ClaimDoc {
18
+ _id: string;
19
+ owner: string;
20
+ expiresAt: Date;
21
+ }
22
+ /**
23
+ * Stores idempotent responses and the claims that stop two instances from
24
+ * running one request at once.
25
+ *
26
+ * `claim` is one atomic `findOneAndUpdate` (upsert): it takes the key when
27
+ * no claim exists or the old one has expired, and otherwise leaves it with
28
+ * its holder. An expired claim can be taken at once, before MongoDB's TTL
29
+ * monitor (about once a minute) deletes it.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const store = new MongoIdempotencyStore(db)
34
+ * app.post('/payments', idempotent({ store }), createPayment)
35
+ * ```
36
+ */
37
+ export declare class MongoIdempotencyStore implements IdempotencyStore {
38
+ readonly collection: Collection<RecordDoc>;
39
+ readonly claims: Collection<ClaimDoc>;
40
+ private readonly defaultTtlMs;
41
+ private readonly autoIndex;
42
+ private readonly indexes;
43
+ private readonly held;
44
+ constructor(db: Db, options?: MongoIdempotencyStoreOptions);
45
+ /** Creates the TTL indexes for records and claims. Safe to call again. */
46
+ ensureIndexes(): Promise<void>;
47
+ private ready;
48
+ get(key: string): Promise<IdempotencyRecord | null>;
49
+ set(key: string, data: IdempotencyRecord, ttlMs?: number): Promise<void>;
50
+ claim(key: string, ttlMs: number): Promise<boolean>;
51
+ release(key: string): Promise<void>;
52
+ }
53
+ export {};
54
+ //# sourceMappingURL=idempotency.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idempotency.d.ts","sourceRoot":"","sources":["../src/idempotency.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAC5E,OAAO,EAWL,KAAK,qBAAqB,EAC3B,MAAM,UAAU,CAAA;AAEjB,MAAM,WAAW,4BAA6B,SAAQ,qBAAqB;IACzE,qEAAqE;IACrE,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,8EAA8E;IAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,kEAAkE;IAClE,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,UAAU,SAAS;IACjB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,IAAI,CAAA;CAChB;AAED,UAAU,QAAQ;IAChB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,IAAI,CAAA;CAChB;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,qBAAsB,YAAW,gBAAgB;IAC5D,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,SAAS,CAAC,CAAA;IAC1C,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAA;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAQ;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAE7C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA4B;gBAErC,EAAE,EAAE,EAAE,EAAE,OAAO,GAAE,4BAAiC;IAc9D,0EAA0E;IAC1E,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;YAIhB,KAAK;IAIb,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IAMnD,GAAG,CACP,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,iBAAiB,EACvB,KAAK,SAAoB,GACxB,OAAO,CAAC,IAAI,CAAC;IAkBV,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAwBnD,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAM1C"}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoIdempotencyStore = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const shared_1 = require("./shared");
6
+ /**
7
+ * Stores idempotent responses and the claims that stop two instances from
8
+ * running one request at once.
9
+ *
10
+ * `claim` is one atomic `findOneAndUpdate` (upsert): it takes the key when
11
+ * no claim exists or the old one has expired, and otherwise leaves it with
12
+ * its holder. An expired claim can be taken at once, before MongoDB's TTL
13
+ * monitor (about once a minute) deletes it.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const store = new MongoIdempotencyStore(db)
18
+ * app.post('/payments', idempotent({ store }), createPayment)
19
+ * ```
20
+ */
21
+ class MongoIdempotencyStore {
22
+ constructor(db, options = {}) {
23
+ // Claims this process holds, so release() only drops its own
24
+ this.held = new Map();
25
+ this.collection = (0, shared_1.collectionFor)(db, options, 'idempotency');
26
+ this.claims = (0, shared_1.collectionFor)(db, { ...options, collection: options.claimsCollection }, 'idempotency_claims');
27
+ this.defaultTtlMs = options.ttlMs ?? 86400000;
28
+ this.autoIndex = options.autoIndex ?? true;
29
+ this.indexes = (0, shared_1.once)(async () => {
30
+ await Promise.all([(0, shared_1.ttlIndex)(this.collection), (0, shared_1.ttlIndex)(this.claims)]);
31
+ });
32
+ }
33
+ /** Creates the TTL indexes for records and claims. Safe to call again. */
34
+ ensureIndexes() {
35
+ return this.indexes();
36
+ }
37
+ async ready() {
38
+ if (this.autoIndex)
39
+ await this.indexes();
40
+ }
41
+ async get(key) {
42
+ await this.ready();
43
+ const doc = await this.collection.findOne({ _id: key, ...shared_1.notExpired });
44
+ return doc ? (0, shared_1.decode)(doc.value) : null;
45
+ }
46
+ async set(key, data, ttlMs = this.defaultTtlMs) {
47
+ await this.ready();
48
+ await (0, shared_1.retryOnDuplicate)(() => this.collection.updateOne({ _id: key }, [
49
+ {
50
+ $set: {
51
+ value: { $literal: (0, shared_1.encode)(data) },
52
+ [shared_1.EXPIRES_FIELD]: (0, shared_1.nowPlus)(ttlMs),
53
+ },
54
+ },
55
+ ], { upsert: true }));
56
+ }
57
+ async claim(key, ttlMs) {
58
+ await this.ready();
59
+ const token = (0, node_crypto_1.randomUUID)();
60
+ const doc = await (0, shared_1.retryOnDuplicate)(() => this.claims.findOneAndUpdate({ _id: key }, [
61
+ {
62
+ $set: {
63
+ owner: { $cond: [shared_1.expired, { $literal: token }, '$owner'] },
64
+ [shared_1.EXPIRES_FIELD]: {
65
+ $cond: [shared_1.expired, (0, shared_1.nowPlus)(ttlMs), `$${shared_1.EXPIRES_FIELD}`],
66
+ },
67
+ },
68
+ },
69
+ ], { upsert: true, returnDocument: 'after' }));
70
+ if (doc?.owner !== token)
71
+ return false;
72
+ this.held.set(key, token);
73
+ return true;
74
+ }
75
+ async release(key) {
76
+ const token = this.held.get(key);
77
+ if (token === undefined)
78
+ return;
79
+ this.held.delete(key);
80
+ await this.claims.deleteOne({ _id: key, owner: token });
81
+ }
82
+ }
83
+ exports.MongoIdempotencyStore = MongoIdempotencyStore;
84
+ //# sourceMappingURL=idempotency.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idempotency.js","sourceRoot":"","sources":["../src/idempotency.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AAGxC,qCAYiB;AAuBjB;;;;;;;;;;;;;;GAcG;AACH,MAAa,qBAAqB;IAShC,YAAY,EAAM,EAAE,UAAwC,EAAE;QAH9D,6DAA6D;QAC5C,SAAI,GAAG,IAAI,GAAG,EAAkB,CAAA;QAG/C,IAAI,CAAC,UAAU,GAAG,IAAA,sBAAa,EAAY,EAAE,EAAE,OAAO,EAAE,aAAa,CAAC,CAAA;QACtE,IAAI,CAAC,MAAM,GAAG,IAAA,sBAAa,EACzB,EAAE,EACF,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,gBAAgB,EAAE,EACpD,oBAAoB,CACrB,CAAA;QACD,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,KAAK,IAAI,QAAU,CAAA;QAC/C,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAA;QAC1C,IAAI,CAAC,OAAO,GAAG,IAAA,aAAI,EAAC,KAAK,IAAI,EAAE;YAC7B,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAA,iBAAQ,EAAC,IAAI,CAAC,UAAU,CAAC,EAAE,IAAA,iBAAQ,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;QACvE,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,0EAA0E;IAC1E,aAAa;QACX,OAAO,IAAI,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;IAEO,KAAK,CAAC,KAAK;QACjB,IAAI,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,CAAC,OAAO,EAAE,CAAA;IAC1C,CAAC;IAED,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oBAC1D,CAAC,sBAAa,CAAC,EAAE;wBACf,KAAK,EAAE,CAAC,gBAAO,EAAE,IAAA,gBAAO,EAAC,KAAK,CAAC,EAAE,IAAI,sBAAa,EAAE,CAAC;qBACtD;iBACF;aACF;SACF,EACD,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,CAC1C,CACF,CAAA;QACD,IAAI,GAAG,EAAE,KAAK,KAAK,KAAK;YAAE,OAAO,KAAK,CAAA;QACtC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QACzB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,GAAW;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAChC,IAAI,KAAK,KAAK,SAAS;YAAE,OAAM;QAC/B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACrB,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;IACzD,CAAC;CACF;AA1FD,sDA0FC"}
@@ -0,0 +1,38 @@
1
+ import type { Db } from 'mongodb';
2
+ import { MongoRateLimitStore, type MongoRateLimitStoreOptions } from './rate-limit';
3
+ import { MongoIdempotencyStore, type MongoIdempotencyStoreOptions } from './idempotency';
4
+ import { MongoCronLock, type MongoCronLockOptions } from './cron-lock';
5
+ import { MongoCacheStore, type MongoCacheStoreOptions } from './cache';
6
+ import { DEFAULT_PREFIX, type MongoStoreBaseOptions } from './shared';
7
+ export { MongoRateLimitStore, MongoIdempotencyStore, MongoCronLock, MongoCacheStore, DEFAULT_PREFIX, };
8
+ export type { MongoRateLimitStoreOptions, MongoIdempotencyStoreOptions, MongoCronLockOptions, MongoCacheStoreOptions, MongoStoreBaseOptions, };
9
+ export interface MongoStoresOptions extends MongoStoreBaseOptions {
10
+ rateLimit?: Omit<MongoRateLimitStoreOptions, keyof MongoStoreBaseOptions>;
11
+ idempotency?: Omit<MongoIdempotencyStoreOptions, keyof MongoStoreBaseOptions>;
12
+ cronLock?: Omit<MongoCronLockOptions, keyof MongoStoreBaseOptions>;
13
+ cache?: Omit<MongoCacheStoreOptions, keyof MongoStoreBaseOptions>;
14
+ }
15
+ export interface MongoStores {
16
+ rateLimit: MongoRateLimitStore;
17
+ idempotency: MongoIdempotencyStore;
18
+ cronLock: MongoCronLock;
19
+ cache: MongoCacheStore;
20
+ /** Creates every store's indexes now (idempotent). Useful at start or in a migration. */
21
+ ensureIndexes(): Promise<void>;
22
+ }
23
+ /**
24
+ * All four stores on one database, with collections named `<prefix>…`
25
+ * (default `exis_`). With Mongoose, pass `mongoose.connection.db` after
26
+ * `mongoose.connect()`.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const stores = mongoStores(client.db('app'))
31
+ * app.use(rateLimit({ max: 100, store: stores.rateLimit }))
32
+ * app.post('/payments', idempotent({ store: stores.idempotency }), pay)
33
+ * app.cron.lock = stores.cronLock
34
+ * // exis.config.ts: cache: { store: stores.cache }
35
+ * ```
36
+ */
37
+ export declare function mongoStores(db: Db, options?: MongoStoresOptions): MongoStores;
38
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EACL,mBAAmB,EACnB,KAAK,0BAA0B,EAChC,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,qBAAqB,EACrB,KAAK,4BAA4B,EAClC,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,aAAa,EAAE,KAAK,oBAAoB,EAAE,MAAM,aAAa,CAAA;AACtE,OAAO,EAAE,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,SAAS,CAAA;AACtE,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAErE,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,aAAa,EACb,eAAe,EACf,cAAc,GACf,CAAA;AACD,YAAY,EACV,0BAA0B,EAC1B,4BAA4B,EAC5B,oBAAoB,EACpB,sBAAsB,EACtB,qBAAqB,GACtB,CAAA;AAED,MAAM,WAAW,kBAAmB,SAAQ,qBAAqB;IAC/D,SAAS,CAAC,EAAE,IAAI,CAAC,0BAA0B,EAAE,MAAM,qBAAqB,CAAC,CAAA;IACzE,WAAW,CAAC,EAAE,IAAI,CAAC,4BAA4B,EAAE,MAAM,qBAAqB,CAAC,CAAA;IAC7E,QAAQ,CAAC,EAAE,IAAI,CAAC,oBAAoB,EAAE,MAAM,qBAAqB,CAAC,CAAA;IAClE,KAAK,CAAC,EAAE,IAAI,CAAC,sBAAsB,EAAE,MAAM,qBAAqB,CAAC,CAAA;CAClE;AAED,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,mBAAmB,CAAA;IAC9B,WAAW,EAAE,qBAAqB,CAAA;IAClC,QAAQ,EAAE,aAAa,CAAA;IACvB,KAAK,EAAE,eAAe,CAAA;IACtB,yFAAyF;IACzF,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CAC/B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CACzB,EAAE,EAAE,EAAE,EACN,OAAO,GAAE,kBAAuB,GAC/B,WAAW,CA6Bb"}
package/dist/index.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_PREFIX = exports.MongoCacheStore = exports.MongoCronLock = exports.MongoIdempotencyStore = exports.MongoRateLimitStore = void 0;
4
+ exports.mongoStores = mongoStores;
5
+ const rate_limit_1 = require("./rate-limit");
6
+ Object.defineProperty(exports, "MongoRateLimitStore", { enumerable: true, get: function () { return rate_limit_1.MongoRateLimitStore; } });
7
+ const idempotency_1 = require("./idempotency");
8
+ Object.defineProperty(exports, "MongoIdempotencyStore", { enumerable: true, get: function () { return idempotency_1.MongoIdempotencyStore; } });
9
+ const cron_lock_1 = require("./cron-lock");
10
+ Object.defineProperty(exports, "MongoCronLock", { enumerable: true, get: function () { return cron_lock_1.MongoCronLock; } });
11
+ const cache_1 = require("./cache");
12
+ Object.defineProperty(exports, "MongoCacheStore", { enumerable: true, get: function () { return cache_1.MongoCacheStore; } });
13
+ const shared_1 = require("./shared");
14
+ Object.defineProperty(exports, "DEFAULT_PREFIX", { enumerable: true, get: function () { return shared_1.DEFAULT_PREFIX; } });
15
+ /**
16
+ * All four stores on one database, with collections named `<prefix>…`
17
+ * (default `exis_`). With Mongoose, pass `mongoose.connection.db` after
18
+ * `mongoose.connect()`.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const stores = mongoStores(client.db('app'))
23
+ * app.use(rateLimit({ max: 100, store: stores.rateLimit }))
24
+ * app.post('/payments', idempotent({ store: stores.idempotency }), pay)
25
+ * app.cron.lock = stores.cronLock
26
+ * // exis.config.ts: cache: { store: stores.cache }
27
+ * ```
28
+ */
29
+ function mongoStores(db, options = {}) {
30
+ const base = {
31
+ prefix: options.prefix ?? shared_1.DEFAULT_PREFIX,
32
+ autoIndex: options.autoIndex,
33
+ };
34
+ const rateLimit = new rate_limit_1.MongoRateLimitStore(db, {
35
+ ...options.rateLimit,
36
+ ...base,
37
+ });
38
+ const idempotency = new idempotency_1.MongoIdempotencyStore(db, {
39
+ ...options.idempotency,
40
+ ...base,
41
+ });
42
+ const cronLock = new cron_lock_1.MongoCronLock(db, { ...options.cronLock, ...base });
43
+ const cache = new cache_1.MongoCacheStore(db, { ...options.cache, ...base });
44
+ return {
45
+ rateLimit,
46
+ idempotency,
47
+ cronLock,
48
+ cache,
49
+ async ensureIndexes() {
50
+ await Promise.all([
51
+ rateLimit.ensureIndexes(),
52
+ idempotency.ensureIndexes(),
53
+ cronLock.ensureIndexes(),
54
+ cache.ensureIndexes(),
55
+ ]);
56
+ },
57
+ };
58
+ }
59
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AA0DA,kCAgCC;AAzFD,6CAGqB;AAUnB,oGAZA,gCAAmB,OAYA;AATrB,+CAGsB;AAOpB,sGATA,mCAAqB,OASA;AANvB,2CAAsE;AAOpE,8FAPO,yBAAa,OAOP;AANf,mCAAsE;AAOpE,gGAPO,uBAAe,OAOP;AANjB,qCAAqE;AAOnE,+FAPO,uBAAc,OAOP;AA0BhB;;;;;;;;;;;;;GAaG;AACH,SAAgB,WAAW,CACzB,EAAM,EACN,UAA8B,EAAE;IAEhC,MAAM,IAAI,GAA0B;QAClC,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,uBAAc;QACxC,SAAS,EAAE,OAAO,CAAC,SAAS;KAC7B,CAAA;IACD,MAAM,SAAS,GAAG,IAAI,gCAAmB,CAAC,EAAE,EAAE;QAC5C,GAAG,OAAO,CAAC,SAAS;QACpB,GAAG,IAAI;KACR,CAAC,CAAA;IACF,MAAM,WAAW,GAAG,IAAI,mCAAqB,CAAC,EAAE,EAAE;QAChD,GAAG,OAAO,CAAC,WAAW;QACtB,GAAG,IAAI;KACR,CAAC,CAAA;IACF,MAAM,QAAQ,GAAG,IAAI,yBAAa,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,QAAQ,EAAE,GAAG,IAAI,EAAE,CAAC,CAAA;IACxE,MAAM,KAAK,GAAG,IAAI,uBAAe,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,EAAE,CAAC,CAAA;IACpE,OAAO;QACL,SAAS;QACT,WAAW;QACX,QAAQ;QACR,KAAK;QACL,KAAK,CAAC,aAAa;YACjB,MAAM,OAAO,CAAC,GAAG,CAAC;gBAChB,SAAS,CAAC,aAAa,EAAE;gBACzB,WAAW,CAAC,aAAa,EAAE;gBAC3B,QAAQ,CAAC,aAAa,EAAE;gBACxB,KAAK,CAAC,aAAa,EAAE;aACtB,CAAC,CAAA;QACJ,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { Collection, Db } from 'mongodb';
2
+ import type { RateLimitHit, RateLimitStore } from 'exisjs/middleware';
3
+ import { type MongoStoreBaseOptions } from './shared';
4
+ export interface MongoRateLimitStoreOptions extends MongoStoreBaseOptions {
5
+ /** Collection name. Default `<prefix>rate_limits` */
6
+ collection?: string;
7
+ }
8
+ interface RateLimitDoc {
9
+ _id: string;
10
+ count: number;
11
+ expiresAt: Date;
12
+ }
13
+ /**
14
+ * Fixed-window hit counter shared by every instance. Each hit is one atomic
15
+ * `findOneAndUpdate` (upsert) that adds one, or starts a new window at 1
16
+ * once the old one has ended. Window ends use the database server's clock.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * app.use(rateLimit({ max: 100, windowMs: 60_000, store: new MongoRateLimitStore(db) }))
21
+ * ```
22
+ */
23
+ export declare class MongoRateLimitStore implements RateLimitStore {
24
+ readonly collection: Collection<RateLimitDoc>;
25
+ private readonly autoIndex;
26
+ private readonly indexes;
27
+ constructor(db: Db, options?: MongoRateLimitStoreOptions);
28
+ /** Creates the TTL index that deletes finished windows. Safe to call again. */
29
+ ensureIndexes(): Promise<void>;
30
+ private ready;
31
+ increment(key: string, windowMs: number): Promise<RateLimitHit>;
32
+ get(key: string): Promise<RateLimitHit | undefined>;
33
+ reset(key: string): Promise<void>;
34
+ }
35
+ export {};
36
+ //# sourceMappingURL=rate-limit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,EASL,KAAK,qBAAqB,EAC3B,MAAM,UAAU,CAAA;AAEjB,MAAM,WAAW,0BAA2B,SAAQ,qBAAqB;IACvE,qDAAqD;IACrD,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,UAAU,YAAY;IACpB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,IAAI,CAAA;CAChB;AAED;;;;;;;;;GASG;AACH,qBAAa,mBAAoB,YAAW,cAAc;IACxD,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,YAAY,CAAC,CAAA;IAC7C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;gBAEjC,EAAE,EAAE,EAAE,EAAE,OAAO,GAAE,0BAA+B;IAQ5D,+EAA+E;IAC/E,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;YAIhB,KAAK;IAIb,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IA0B/D,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC;IAQnD,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIxC"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoRateLimitStore = void 0;
4
+ const shared_1 = require("./shared");
5
+ /**
6
+ * Fixed-window hit counter shared by every instance. Each hit is one atomic
7
+ * `findOneAndUpdate` (upsert) that adds one, or starts a new window at 1
8
+ * once the old one has ended. Window ends use the database server's clock.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * app.use(rateLimit({ max: 100, windowMs: 60_000, store: new MongoRateLimitStore(db) }))
13
+ * ```
14
+ */
15
+ class MongoRateLimitStore {
16
+ constructor(db, options = {}) {
17
+ this.collection = (0, shared_1.collectionFor)(db, options, 'rate_limits');
18
+ this.autoIndex = options.autoIndex ?? true;
19
+ this.indexes = (0, shared_1.once)(async () => {
20
+ await (0, shared_1.ttlIndex)(this.collection);
21
+ });
22
+ }
23
+ /** Creates the TTL index that deletes finished windows. Safe to call again. */
24
+ ensureIndexes() {
25
+ return this.indexes();
26
+ }
27
+ async ready() {
28
+ if (this.autoIndex)
29
+ await this.indexes();
30
+ }
31
+ async increment(key, windowMs) {
32
+ await this.ready();
33
+ const doc = await (0, shared_1.retryOnDuplicate)(() => this.collection.findOneAndUpdate({ _id: key }, [
34
+ {
35
+ // Both fields read the old document, so a finished (or new)
36
+ // window restarts at 1 with a fresh end
37
+ $set: {
38
+ count: {
39
+ $cond: [shared_1.expired, 1, { $add: [{ $ifNull: ['$count', 0] }, 1] }],
40
+ },
41
+ [shared_1.EXPIRES_FIELD]: {
42
+ $cond: [shared_1.expired, (0, shared_1.nowPlus)(windowMs), `$${shared_1.EXPIRES_FIELD}`],
43
+ },
44
+ },
45
+ },
46
+ ], { upsert: true, returnDocument: 'after' }));
47
+ if (!doc)
48
+ throw new Error('[exis:mongo] Rate limit upsert returned nothing');
49
+ return { count: doc.count, resetTime: doc.expiresAt.getTime() };
50
+ }
51
+ async get(key) {
52
+ await this.ready();
53
+ const doc = await this.collection.findOne({ _id: key, ...shared_1.notExpired });
54
+ return doc
55
+ ? { count: doc.count, resetTime: doc.expiresAt.getTime() }
56
+ : undefined;
57
+ }
58
+ async reset(key) {
59
+ await this.ready();
60
+ await this.collection.deleteOne({ _id: key });
61
+ }
62
+ }
63
+ exports.MongoRateLimitStore = MongoRateLimitStore;
64
+ //# sourceMappingURL=rate-limit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":";;;AAEA,qCAUiB;AAajB;;;;;;;;;GASG;AACH,MAAa,mBAAmB;IAK9B,YAAY,EAA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1C,CACF,CAAA;QACD,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAA;QAC5E,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,CAAA;IACjE,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW;QACnB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;QAClB,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,mBAAU,EAAE,CAAC,CAAA;QACtE,OAAO,GAAG;YACR,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE;YAC1D,CAAC,CAAC,SAAS,CAAA;IACf,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,GAAW;QACrB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;QAClB,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAA;IAC/C,CAAC;CACF;AA5DD,kDA4DC"}
@@ -0,0 +1,41 @@
1
+ import type { Collection, Db, Document } from 'mongodb';
2
+ /** Collection name prefix used when none is given */
3
+ export declare const DEFAULT_PREFIX = "exis_";
4
+ /** Options every store accepts */
5
+ export interface MongoStoreBaseOptions {
6
+ /** Prefix for the default collection names. Default `exis_` */
7
+ prefix?: string;
8
+ /**
9
+ * Create the store's indexes on first use (idempotent `createIndex`).
10
+ * Set false when indexes are managed by migrations; then call
11
+ * `ensureIndexes()` yourself once. Default true
12
+ */
13
+ autoIndex?: boolean;
14
+ }
15
+ export declare const EXPIRES_FIELD = "expiresAt";
16
+ export declare function ttlIndex(collection: Collection<any>): Promise<string>;
17
+ /** Runs `create` once; a failure is not cached, so the next call retries */
18
+ export declare function once(create: () => Promise<void>): () => Promise<void>;
19
+ export declare function collectionFor<T extends Document>(db: Db, options: MongoStoreBaseOptions & {
20
+ collection?: string;
21
+ }, suffix: string): Collection<T>;
22
+ export declare function isDuplicateKey(err: unknown): boolean;
23
+ /**
24
+ * Two concurrent upserts on one new `_id` can both try to insert; one gets
25
+ * a duplicate key error. The document exists by then, so a retry updates it.
26
+ */
27
+ export declare function retryOnDuplicate<T>(fn: () => Promise<T>): Promise<T>;
28
+ /** `$$NOW + ms`, evaluated on the server so instances agree on the time */
29
+ export declare function nowPlus(ms: number): Document;
30
+ /** Matches a document whose expiry is still in the future (server time) */
31
+ export declare const notExpired: Document;
32
+ /** Matches a missing document or one whose expiry has passed (server time) */
33
+ export declare const expired: Document;
34
+ /**
35
+ * Values are saved as JSON text: the same round trip the store contracts
36
+ * describe (a Date comes back as a string), and no BSON limits on field
37
+ * names such as `$` or `.` inside user data.
38
+ */
39
+ export declare function encode(value: unknown): string | undefined;
40
+ export declare function decode(text: unknown): unknown;
41
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEvD,qDAAqD;AACrD,eAAO,MAAM,cAAc,UAAU,CAAA;AAErC,kCAAkC;AAClC,MAAM,WAAW,qBAAqB;IACpC,+DAA+D;IAC/D,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAKD,eAAO,MAAM,aAAa,cAAc,CAAA;AAExC,wBAAgB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAKrE;AAED,4EAA4E;AAC5E,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAOrE;AAED,wBAAgB,aAAa,CAAC,CAAC,SAAS,QAAQ,EAC9C,EAAE,EAAE,EAAE,EACN,OAAO,EAAE,qBAAqB,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,EACxD,MAAM,EAAE,MAAM,GACb,UAAU,CAAC,CAAC,CAAC,CAIf;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAOpD;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAQ1E;AAED,2EAA2E;AAC3E,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,QAAQ,CAE5C;AAED,2EAA2E;AAC3E,eAAO,MAAM,UAAU,EAAE,QAExB,CAAA;AAED,8EAA8E;AAC9E,eAAO,MAAM,OAAO,EAAE,QAErB,CAAA;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAEzD;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAE7C"}
package/dist/shared.js ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expired = exports.notExpired = exports.EXPIRES_FIELD = exports.DEFAULT_PREFIX = void 0;
4
+ exports.ttlIndex = ttlIndex;
5
+ exports.once = once;
6
+ exports.collectionFor = collectionFor;
7
+ exports.isDuplicateKey = isDuplicateKey;
8
+ exports.retryOnDuplicate = retryOnDuplicate;
9
+ exports.nowPlus = nowPlus;
10
+ exports.encode = encode;
11
+ exports.decode = decode;
12
+ /** Collection name prefix used when none is given */
13
+ exports.DEFAULT_PREFIX = 'exis_';
14
+ // Expiry documents carry a Date that the TTL index (expireAfterSeconds: 0)
15
+ // deletes once passed. The TTL monitor runs about once a minute, so every
16
+ // read also checks the date itself.
17
+ exports.EXPIRES_FIELD = 'expiresAt';
18
+ function ttlIndex(collection) {
19
+ return collection.createIndex({ [exports.EXPIRES_FIELD]: 1 }, { expireAfterSeconds: 0, name: 'exis_ttl' });
20
+ }
21
+ /** Runs `create` once; a failure is not cached, so the next call retries */
22
+ function once(create) {
23
+ let pending;
24
+ return () => (pending ?? (pending = create().catch((err) => {
25
+ pending = undefined;
26
+ throw err;
27
+ })));
28
+ }
29
+ function collectionFor(db, options, suffix) {
30
+ return db.collection(options.collection ?? `${options.prefix ?? exports.DEFAULT_PREFIX}${suffix}`);
31
+ }
32
+ function isDuplicateKey(err) {
33
+ return (typeof err === 'object' &&
34
+ err !== null &&
35
+ (err.code === 11000 ||
36
+ err.code === 11001));
37
+ }
38
+ /**
39
+ * Two concurrent upserts on one new `_id` can both try to insert; one gets
40
+ * a duplicate key error. The document exists by then, so a retry updates it.
41
+ */
42
+ async function retryOnDuplicate(fn) {
43
+ for (let attempt = 0;; attempt++) {
44
+ try {
45
+ return await fn();
46
+ }
47
+ catch (err) {
48
+ if (!isDuplicateKey(err) || attempt >= 4)
49
+ throw err;
50
+ }
51
+ }
52
+ }
53
+ /** `$$NOW + ms`, evaluated on the server so instances agree on the time */
54
+ function nowPlus(ms) {
55
+ return { $add: ['$$NOW', Math.max(0, Math.floor(ms))] };
56
+ }
57
+ /** Matches a document whose expiry is still in the future (server time) */
58
+ exports.notExpired = {
59
+ $expr: { $gt: [`$${exports.EXPIRES_FIELD}`, '$$NOW'] },
60
+ };
61
+ /** Matches a missing document or one whose expiry has passed (server time) */
62
+ exports.expired = {
63
+ $not: [{ $gt: [`$${exports.EXPIRES_FIELD}`, '$$NOW'] }],
64
+ };
65
+ /**
66
+ * Values are saved as JSON text: the same round trip the store contracts
67
+ * describe (a Date comes back as a string), and no BSON limits on field
68
+ * names such as `$` or `.` inside user data.
69
+ */
70
+ function encode(value) {
71
+ return JSON.stringify(value);
72
+ }
73
+ function decode(text) {
74
+ return typeof text === 'string' ? JSON.parse(text) : undefined;
75
+ }
76
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":";;;AAsBA,4BAKC;AAGD,oBAOC;AAED,sCAQC;AAED,wCAOC;AAMD,4CAQC;AAGD,0BAEC;AAiBD,wBAEC;AAED,wBAEC;AAhGD,qDAAqD;AACxC,QAAA,cAAc,GAAG,OAAO,CAAA;AAcrC,2EAA2E;AAC3E,0EAA0E;AAC1E,oCAAoC;AACvB,QAAA,aAAa,GAAG,WAAW,CAAA;AAExC,SAAgB,QAAQ,CAAC,UAA2B;IAClD,OAAO,UAAU,CAAC,WAAW,CAC3B,EAAE,CAAC,qBAAa,CAAC,EAAE,CAAC,EAAE,EACtB,EAAE,kBAAkB,EAAE,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAC5C,CAAA;AACH,CAAC;AAED,4EAA4E;AAC5E,SAAgB,IAAI,CAAC,MAA2B;IAC9C,IAAI,OAAkC,CAAA;IACtC,OAAO,GAAG,EAAE,CACV,CAAC,OAAO,KAAP,OAAO,GAAK,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QAClC,OAAO,GAAG,SAAS,CAAA;QACnB,MAAM,GAAG,CAAA;IACX,CAAC,CAAC,EAAC,CAAA;AACP,CAAC;AAED,SAAgB,aAAa,CAC3B,EAAM,EACN,OAAwD,EACxD,MAAc;IAEd,OAAO,EAAE,CAAC,UAAU,CAClB,OAAO,CAAC,UAAU,IAAI,GAAG,OAAO,CAAC,MAAM,IAAI,sBAAc,GAAG,MAAM,EAAE,CACrE,CAAA;AACH,CAAC;AAED,SAAgB,cAAc,CAAC,GAAY;IACzC,OAAO,CACL,OAAO,GAAG,KAAK,QAAQ;QACvB,GAAG,KAAK,IAAI;QACZ,CAAE,GAA0B,CAAC,IA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  "name": "@exisjs/mongo",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.11.0",
4
+ "description": "MongoDB stores for ExisJS: rate limits, idempotency keys, cron locks and the @Cacheable cache, shared by every instance",
5
+ "keywords": [
6
+ "exisjs",
7
+ "exis",
8
+ "mongodb",
9
+ "mongoose",
10
+ "rate-limit",
11
+ "idempotency",
12
+ "cron",
13
+ "cache",
14
+ "distributed-lock"
15
+ ],
16
+ "homepage": "https://github.com/v25group/exisjs/tree/main/packages/mongo#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/v25group/exisjs/issues"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/v25group/exisjs.git",
23
+ "directory": "packages/mongo"
24
+ },
25
+ "license": "MIT",
26
+ "author": "V25 Group <support@v25group.com>",
27
+ "main": "dist/index.js",
28
+ "types": "dist/index.d.ts",
29
+ "type": "commonjs",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "engines": {
41
+ "node": ">=24.0.0"
42
+ },
43
+ "scripts": {
44
+ "prebuild": "node -e \"require('fs').rmSync('dist', {recursive:true, force:true}); require('fs').rmSync('tsconfig.tsbuildinfo', {force:true})\"",
45
+ "build": "tsc",
46
+ "dev": "tsc --watch",
47
+ "prepublishOnly": "npm run build"
48
+ },
49
+ "peerDependencies": {
50
+ "exisjs": "~0.11.0",
51
+ "mongodb": ">=6 <8"
52
+ },
53
+ "devDependencies": {
54
+ "@types/node": "^24.19.2",
55
+ "mongodb": "^7.8.0"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }