async-bulkhead-ts 0.1.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.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jan Balangue
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 ADDED
@@ -0,0 +1,128 @@
1
+ # async-bulkhead-ts
2
+
3
+ Fail-fast **admission control** and **bulkheads** for async workloads in Node.js.
4
+
5
+ Designed for services that prefer **rejecting work early** over queueing and degrading under load.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - ✅ **Fail-fast by default** (no hidden queues)
12
+ - ✅ Simple **bulkhead / concurrency limits**
13
+ - ✅ Explicit admission + release lifecycle
14
+ - ✅ Zero dependencies
15
+ - ✅ ESM + CJS support
16
+ - ✅ Node.js **20+**
17
+
18
+ Non-goals (by design):
19
+ - ❌ No background workers
20
+ - ❌ No retry logic
21
+ - ❌ No distributed coordination
22
+ - ❌ No metrics backend (hooks later)
23
+
24
+ ---
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ npm install async-bulkhead-ts
30
+ ```
31
+
32
+ ## Basic Usage
33
+
34
+ ```ts
35
+ import { createBulkhead } from 'async-bulkhead-ts';
36
+
37
+ const bulkhead = createBulkhead({ maxConcurrent: 10 });
38
+ const admission = bulkhead.admit();
39
+
40
+ if (!admission.ok) {
41
+ // Fail fast — shed load, return 503, etc.
42
+ throw new Error('Service overloaded');
43
+ }
44
+
45
+ try {
46
+ // Do async work
47
+ await doWork();
48
+ } finally {
49
+ admission.release();
50
+ }
51
+ ```
52
+
53
+ ## With a Queue (Optional)
54
+
55
+ Queues are opt-in and bounded.
56
+ ```ts
57
+ const bulkhead = createBulkhead({
58
+ maxConcurrent: 10,
59
+ maxQueue: 20,
60
+ });
61
+ ```
62
+
63
+ If both concurrency and queue limits are exceeded, admission fails immediately.
64
+
65
+ ## API
66
+
67
+ `createBulkhead(options)`
68
+
69
+ ```ts
70
+ type BulkheadOptions = {
71
+ maxConcurrent: number;
72
+ maxQueue?: number;
73
+ };
74
+ ```
75
+
76
+ Returns:
77
+
78
+ ```json
79
+ {
80
+ admit(): AdmitResult;
81
+ stats(): {
82
+ inFlight: number;
83
+ queued: number;
84
+ maxConcurrent: number;
85
+ maxQueue: number;
86
+ };
87
+ }
88
+ ```
89
+
90
+ `admit()`
91
+
92
+ ```ts
93
+ type AdmitResult =
94
+ | { ok: true; release: () => void }
95
+ | { ok: false; reason: 'concurrency_limit' | 'queue_limit' };
96
+ ```
97
+
98
+ You must call `release()` exactly once if admission succeeds.
99
+
100
+ Failing to release will permanently reduce capacity.
101
+
102
+ ## Design Philosophy
103
+
104
+ This library is intentionally small.
105
+
106
+ It exists to enforce **backpressure at the boundary** of your system:
107
+
108
+ * before request fan-out
109
+ * before hitting downstream dependencies
110
+ * before saturation cascades
111
+
112
+ If you need retries, buffering, scheduling, or persistence—compose those *around* this, not inside it.
113
+
114
+ ## Compatibility
115
+
116
+ * Node.js: 20+ (24 LTS recommended)
117
+ * Module formats: ESM and CommonJS
118
+
119
+ ## Roadmap (Short)
120
+
121
+ * `bulkhead.run(fn)` helper
122
+ * Structured metrics hooks
123
+ * `AbortSignal` / cancellation support
124
+ * Optional time-based admission windows
125
+
126
+ ## License
127
+
128
+ MIT © 2026
package/dist/index.cjs ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createBulkhead: () => createBulkhead
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ function createBulkhead(opts) {
27
+ if (!Number.isInteger(opts.maxConcurrent) || opts.maxConcurrent <= 0) {
28
+ throw new Error("maxConcurrent must be a positive integer");
29
+ }
30
+ const maxQueue = opts.maxQueue ?? 0;
31
+ let inFlight = 0;
32
+ const queue = [];
33
+ const tryStartNext = () => {
34
+ while (inFlight < opts.maxConcurrent && queue.length > 0) {
35
+ const start = queue.shift();
36
+ inFlight++;
37
+ start();
38
+ }
39
+ };
40
+ const release = () => {
41
+ inFlight = Math.max(0, inFlight - 1);
42
+ tryStartNext();
43
+ };
44
+ const admit = () => {
45
+ if (inFlight < opts.maxConcurrent) {
46
+ inFlight++;
47
+ return { ok: true, release };
48
+ }
49
+ if (maxQueue > 0 && queue.length < maxQueue) {
50
+ let started = false;
51
+ const gate = () => {
52
+ started = true;
53
+ };
54
+ queue.push(gate);
55
+ return {
56
+ ok: true,
57
+ release: () => {
58
+ if (!started) {
59
+ const idx = queue.indexOf(gate);
60
+ if (idx >= 0) queue.splice(idx, 1);
61
+ } else {
62
+ release();
63
+ }
64
+ }
65
+ };
66
+ }
67
+ return { ok: false, reason: maxQueue > 0 ? "queue_limit" : "concurrency_limit" };
68
+ };
69
+ return {
70
+ admit,
71
+ stats: () => ({ inFlight, queued: queue.length, maxConcurrent: opts.maxConcurrent, maxQueue })
72
+ };
73
+ }
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ createBulkhead
77
+ });
78
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type BulkheadOptions = {\n maxConcurrent: number;\n maxQueue?: number; // if undefined => no queue (fail fast)\n};\n\nexport type AdmitResult =\n | { ok: true; release: () => void }\n | { ok: false; reason: \"concurrency_limit\" | \"queue_limit\" };\n\nexport function createBulkhead(opts: BulkheadOptions) {\n if (!Number.isInteger(opts.maxConcurrent) || opts.maxConcurrent <= 0) {\n throw new Error(\"maxConcurrent must be a positive integer\");\n }\n const maxQueue = opts.maxQueue ?? 0;\n\n let inFlight = 0;\n const queue: Array<() => void> = [];\n\n const tryStartNext = () => {\n while (inFlight < opts.maxConcurrent && queue.length > 0) {\n const start = queue.shift()!;\n inFlight++;\n start();\n }\n };\n\n const release = () => {\n inFlight = Math.max(0, inFlight - 1);\n tryStartNext();\n };\n\n const admit = (): AdmitResult => {\n if (inFlight < opts.maxConcurrent) {\n inFlight++;\n return { ok: true, release };\n }\n if (maxQueue > 0 && queue.length < maxQueue) {\n let started = false;\n const gate = () => {\n started = true;\n };\n queue.push(gate);\n\n return {\n ok: true,\n release: () => {\n // If not started yet, remove from queue and free slot reserved by queue entry.\n if (!started) {\n const idx = queue.indexOf(gate);\n if (idx >= 0) queue.splice(idx, 1);\n } else {\n release();\n }\n }\n };\n }\n return { ok: false, reason: maxQueue > 0 ? \"queue_limit\" : \"concurrency_limit\" };\n };\n\n return {\n admit,\n stats: () => ({ inFlight, queued: queue.length, maxConcurrent: opts.maxConcurrent, maxQueue })\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASO,SAAS,eAAe,MAAuB;AACpD,MAAI,CAAC,OAAO,UAAU,KAAK,aAAa,KAAK,KAAK,iBAAiB,GAAG;AACpE,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AACA,QAAM,WAAW,KAAK,YAAY;AAElC,MAAI,WAAW;AACf,QAAM,QAA2B,CAAC;AAElC,QAAM,eAAe,MAAM;AACzB,WAAO,WAAW,KAAK,iBAAiB,MAAM,SAAS,GAAG;AACxD,YAAM,QAAQ,MAAM,MAAM;AAC1B;AACA,YAAM;AAAA,IACR;AAAA,EACF;AAEA,QAAM,UAAU,MAAM;AACpB,eAAW,KAAK,IAAI,GAAG,WAAW,CAAC;AACnC,iBAAa;AAAA,EACf;AAEA,QAAM,QAAQ,MAAmB;AAC/B,QAAI,WAAW,KAAK,eAAe;AACjC;AACA,aAAO,EAAE,IAAI,MAAM,QAAQ;AAAA,IAC7B;AACA,QAAI,WAAW,KAAK,MAAM,SAAS,UAAU;AAC3C,UAAI,UAAU;AACd,YAAM,OAAO,MAAM;AACjB,kBAAU;AAAA,MACZ;AACA,YAAM,KAAK,IAAI;AAEf,aAAO;AAAA,QACL,IAAI;AAAA,QACJ,SAAS,MAAM;AAEb,cAAI,CAAC,SAAS;AACZ,kBAAM,MAAM,MAAM,QAAQ,IAAI;AAC9B,gBAAI,OAAO,EAAG,OAAM,OAAO,KAAK,CAAC;AAAA,UACnC,OAAO;AACL,oBAAQ;AAAA,UACV;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO,EAAE,IAAI,OAAO,QAAQ,WAAW,IAAI,gBAAgB,oBAAoB;AAAA,EACjF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,OAAO,OAAO,EAAE,UAAU,QAAQ,MAAM,QAAQ,eAAe,KAAK,eAAe,SAAS;AAAA,EAC9F;AACF;","names":[]}
@@ -0,0 +1,22 @@
1
+ type BulkheadOptions = {
2
+ maxConcurrent: number;
3
+ maxQueue?: number;
4
+ };
5
+ type AdmitResult = {
6
+ ok: true;
7
+ release: () => void;
8
+ } | {
9
+ ok: false;
10
+ reason: "concurrency_limit" | "queue_limit";
11
+ };
12
+ declare function createBulkhead(opts: BulkheadOptions): {
13
+ admit: () => AdmitResult;
14
+ stats: () => {
15
+ inFlight: number;
16
+ queued: number;
17
+ maxConcurrent: number;
18
+ maxQueue: number;
19
+ };
20
+ };
21
+
22
+ export { type AdmitResult, type BulkheadOptions, createBulkhead };
@@ -0,0 +1,22 @@
1
+ type BulkheadOptions = {
2
+ maxConcurrent: number;
3
+ maxQueue?: number;
4
+ };
5
+ type AdmitResult = {
6
+ ok: true;
7
+ release: () => void;
8
+ } | {
9
+ ok: false;
10
+ reason: "concurrency_limit" | "queue_limit";
11
+ };
12
+ declare function createBulkhead(opts: BulkheadOptions): {
13
+ admit: () => AdmitResult;
14
+ stats: () => {
15
+ inFlight: number;
16
+ queued: number;
17
+ maxConcurrent: number;
18
+ maxQueue: number;
19
+ };
20
+ };
21
+
22
+ export { type AdmitResult, type BulkheadOptions, createBulkhead };
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ // src/index.ts
2
+ function createBulkhead(opts) {
3
+ if (!Number.isInteger(opts.maxConcurrent) || opts.maxConcurrent <= 0) {
4
+ throw new Error("maxConcurrent must be a positive integer");
5
+ }
6
+ const maxQueue = opts.maxQueue ?? 0;
7
+ let inFlight = 0;
8
+ const queue = [];
9
+ const tryStartNext = () => {
10
+ while (inFlight < opts.maxConcurrent && queue.length > 0) {
11
+ const start = queue.shift();
12
+ inFlight++;
13
+ start();
14
+ }
15
+ };
16
+ const release = () => {
17
+ inFlight = Math.max(0, inFlight - 1);
18
+ tryStartNext();
19
+ };
20
+ const admit = () => {
21
+ if (inFlight < opts.maxConcurrent) {
22
+ inFlight++;
23
+ return { ok: true, release };
24
+ }
25
+ if (maxQueue > 0 && queue.length < maxQueue) {
26
+ let started = false;
27
+ const gate = () => {
28
+ started = true;
29
+ };
30
+ queue.push(gate);
31
+ return {
32
+ ok: true,
33
+ release: () => {
34
+ if (!started) {
35
+ const idx = queue.indexOf(gate);
36
+ if (idx >= 0) queue.splice(idx, 1);
37
+ } else {
38
+ release();
39
+ }
40
+ }
41
+ };
42
+ }
43
+ return { ok: false, reason: maxQueue > 0 ? "queue_limit" : "concurrency_limit" };
44
+ };
45
+ return {
46
+ admit,
47
+ stats: () => ({ inFlight, queued: queue.length, maxConcurrent: opts.maxConcurrent, maxQueue })
48
+ };
49
+ }
50
+ export {
51
+ createBulkhead
52
+ };
53
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type BulkheadOptions = {\n maxConcurrent: number;\n maxQueue?: number; // if undefined => no queue (fail fast)\n};\n\nexport type AdmitResult =\n | { ok: true; release: () => void }\n | { ok: false; reason: \"concurrency_limit\" | \"queue_limit\" };\n\nexport function createBulkhead(opts: BulkheadOptions) {\n if (!Number.isInteger(opts.maxConcurrent) || opts.maxConcurrent <= 0) {\n throw new Error(\"maxConcurrent must be a positive integer\");\n }\n const maxQueue = opts.maxQueue ?? 0;\n\n let inFlight = 0;\n const queue: Array<() => void> = [];\n\n const tryStartNext = () => {\n while (inFlight < opts.maxConcurrent && queue.length > 0) {\n const start = queue.shift()!;\n inFlight++;\n start();\n }\n };\n\n const release = () => {\n inFlight = Math.max(0, inFlight - 1);\n tryStartNext();\n };\n\n const admit = (): AdmitResult => {\n if (inFlight < opts.maxConcurrent) {\n inFlight++;\n return { ok: true, release };\n }\n if (maxQueue > 0 && queue.length < maxQueue) {\n let started = false;\n const gate = () => {\n started = true;\n };\n queue.push(gate);\n\n return {\n ok: true,\n release: () => {\n // If not started yet, remove from queue and free slot reserved by queue entry.\n if (!started) {\n const idx = queue.indexOf(gate);\n if (idx >= 0) queue.splice(idx, 1);\n } else {\n release();\n }\n }\n };\n }\n return { ok: false, reason: maxQueue > 0 ? \"queue_limit\" : \"concurrency_limit\" };\n };\n\n return {\n admit,\n stats: () => ({ inFlight, queued: queue.length, maxConcurrent: opts.maxConcurrent, maxQueue })\n };\n}\n"],"mappings":";AASO,SAAS,eAAe,MAAuB;AACpD,MAAI,CAAC,OAAO,UAAU,KAAK,aAAa,KAAK,KAAK,iBAAiB,GAAG;AACpE,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AACA,QAAM,WAAW,KAAK,YAAY;AAElC,MAAI,WAAW;AACf,QAAM,QAA2B,CAAC;AAElC,QAAM,eAAe,MAAM;AACzB,WAAO,WAAW,KAAK,iBAAiB,MAAM,SAAS,GAAG;AACxD,YAAM,QAAQ,MAAM,MAAM;AAC1B;AACA,YAAM;AAAA,IACR;AAAA,EACF;AAEA,QAAM,UAAU,MAAM;AACpB,eAAW,KAAK,IAAI,GAAG,WAAW,CAAC;AACnC,iBAAa;AAAA,EACf;AAEA,QAAM,QAAQ,MAAmB;AAC/B,QAAI,WAAW,KAAK,eAAe;AACjC;AACA,aAAO,EAAE,IAAI,MAAM,QAAQ;AAAA,IAC7B;AACA,QAAI,WAAW,KAAK,MAAM,SAAS,UAAU;AAC3C,UAAI,UAAU;AACd,YAAM,OAAO,MAAM;AACjB,kBAAU;AAAA,MACZ;AACA,YAAM,KAAK,IAAI;AAEf,aAAO;AAAA,QACL,IAAI;AAAA,QACJ,SAAS,MAAM;AAEb,cAAI,CAAC,SAAS;AACZ,kBAAM,MAAM,MAAM,QAAQ,IAAI;AAC9B,gBAAI,OAAO,EAAG,OAAM,OAAO,KAAK,CAAC;AAAA,UACnC,OAAO;AACL,oBAAQ;AAAA,UACV;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO,EAAE,IAAI,OAAO,QAAQ,WAAW,IAAI,gBAAgB,oBAAoB;AAAA,EACjF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,OAAO,OAAO,EAAE,UAAU,QAAQ,MAAM,QAAQ,eAAe,KAAK,eAAe,SAAS;AAAA,EAC9F;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "async-bulkhead-ts",
3
+ "version": "0.1.0",
4
+ "description": "Fail-fast admission control + bulkheads for async workloads",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "require": {
21
+ "types": "./dist/index.d.cts",
22
+ "default": "./dist/index.cjs"
23
+ }
24
+ }
25
+ },
26
+ "engines": {
27
+ "node": ">=20"
28
+ },
29
+ "scripts": {
30
+ "build": "tsup",
31
+ "test": "vitest",
32
+ "lint": "eslint .",
33
+ "prepublishOnly": "npm run test && npm run build"
34
+ },
35
+ "devDependencies": {
36
+ "@eslint/js": "^9.39.2",
37
+ "@types/node": "^25.0.9",
38
+ "eslint": "^9.39.2",
39
+ "eslint-config-prettier": "^10.1.8",
40
+ "prettier": "^3.8.0",
41
+ "tsup": "^8.5.1",
42
+ "typescript": "^5.9.3",
43
+ "typescript-eslint": "^8.53.1",
44
+ "vitest": "^4.0.17"
45
+ }
46
+ }