@cloudflare/sandbox 0.0.0-d81d2a5 → 0.0.0-d86b60e
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/CHANGELOG.md +117 -0
- package/Dockerfile +27 -27
- package/README.md +70 -8
- package/container_src/bun.lock +31 -77
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/control-process.ts +784 -0
- package/container_src/handler/exec.ts +99 -254
- package/container_src/handler/file.ts +204 -642
- package/container_src/handler/git.ts +28 -80
- package/container_src/handler/process.ts +443 -515
- package/container_src/handler/session.ts +92 -0
- package/container_src/index.ts +280 -219
- package/container_src/interpreter-service.ts +276 -0
- package/container_src/isolation.ts +1049 -0
- package/container_src/mime-processor.ts +1 -1
- package/container_src/package.json +4 -4
- package/container_src/runtime/executors/javascript/node_executor.ts +123 -0
- package/container_src/runtime/executors/python/ipython_executor.py +338 -0
- package/container_src/runtime/executors/typescript/ts_executor.ts +138 -0
- package/container_src/runtime/process-pool.ts +464 -0
- package/container_src/shell-escape.ts +42 -0
- package/container_src/startup.sh +6 -47
- package/container_src/types.ts +35 -12
- package/package.json +2 -2
- package/src/client.ts +175 -187
- package/src/errors.ts +219 -0
- package/src/index.ts +60 -14
- package/src/interpreter-client.ts +352 -0
- package/src/interpreter-types.ts +102 -95
- package/src/interpreter.ts +8 -8
- package/src/sandbox.ts +306 -337
- package/src/types.ts +125 -24
- package/container_src/jupyter-server.ts +0 -336
- package/src/jupyter-client.ts +0 -266
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,122 @@
|
|
|
1
1
|
# @cloudflare/sandbox
|
|
2
2
|
|
|
3
|
+
## 0.3.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#86](https://github.com/cloudflare/sandbox-sdk/pull/86) [`feafd32`](https://github.com/cloudflare/sandbox-sdk/commit/feafd32a51f50dfaf4994bddcbfb56d46cada622) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix session reuse to reuse existing healthy session
|
|
8
|
+
|
|
9
|
+
## 0.3.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#83](https://github.com/cloudflare/sandbox-sdk/pull/83) [`eec5bb6`](https://github.com/cloudflare/sandbox-sdk/commit/eec5bb6203dd5d775b4b54e91c26de25eeb767ce) Thanks [@mikenomitch](https://github.com/mikenomitch)! - Bump containers package version
|
|
14
|
+
|
|
15
|
+
## 0.3.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#76](https://github.com/cloudflare/sandbox-sdk/pull/76) [`ef9e320`](https://github.com/cloudflare/sandbox-sdk/commit/ef9e320dcef30e57797fef6ebd9a9383fa9720d9) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Replace Jupyter with lightweight interpreters for >90% faster cold starts for `.runCode` calls, while maintaining full code execution capabilities and rich output support.
|
|
20
|
+
|
|
21
|
+
## 0.3.1
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- [#71](https://github.com/cloudflare/sandbox-sdk/pull/71) [`fb3c9c2`](https://github.com/cloudflare/sandbox-sdk/commit/fb3c9c22242d9d4f157c26f547f1e697ef7875f9) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Bump containers package version
|
|
26
|
+
|
|
27
|
+
- [#70](https://github.com/cloudflare/sandbox-sdk/pull/70) [`e1fa354`](https://github.com/cloudflare/sandbox-sdk/commit/e1fa354ab1bc7b0e89db4901b67028ebf1a93d0a) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix escaped quotes in file write operations
|
|
28
|
+
|
|
29
|
+
- [#68](https://github.com/cloudflare/sandbox-sdk/pull/68) [`69b91d1`](https://github.com/cloudflare/sandbox-sdk/commit/69b91d1a8f6afb63262cc381ea93e94a033ed5e8) Thanks [@CyrusNuevoDia](https://github.com/CyrusNuevoDia)! - Configurable timeouts via environment variables in isolation.ts
|
|
30
|
+
|
|
31
|
+
- [#66](https://github.com/cloudflare/sandbox-sdk/pull/66) [`eca93b9`](https://github.com/cloudflare/sandbox-sdk/commit/eca93b97e40fa0d3bd9dc27af2cc214ec355b696) Thanks [@peterp](https://github.com/peterp)! - Determine if the port is specified in the URL.
|
|
32
|
+
|
|
33
|
+
## 0.3.0
|
|
34
|
+
|
|
35
|
+
### Minor Changes
|
|
36
|
+
|
|
37
|
+
- [#59](https://github.com/cloudflare/sandbox-sdk/pull/59) [`b6757f7`](https://github.com/cloudflare/sandbox-sdk/commit/b6757f730c34381d5a70d513944bbf9840f598ab) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Add process isolation for sandbox commands
|
|
38
|
+
|
|
39
|
+
Implements PID namespace isolation to protect control plane processes (Jupyter, Bun) from sandboxed code. Commands executed via `exec()` now run in isolated namespaces that cannot see or interact with system processes.
|
|
40
|
+
|
|
41
|
+
**Key security improvements:**
|
|
42
|
+
|
|
43
|
+
- Control plane processes are hidden from sandboxed commands
|
|
44
|
+
- Platform secrets in `/proc/1/environ` are inaccessible
|
|
45
|
+
- Ports 8888 (Jupyter) and 3000 (Bun) are protected from hijacking
|
|
46
|
+
|
|
47
|
+
**Breaking changes:**
|
|
48
|
+
|
|
49
|
+
1. **Removed `sessionId` parameter**: The `sessionId` parameter has been removed from all methods (`exec()`, `execStream()`, `startProcess()`, etc.). Each sandbox now maintains its own persistent session automatically.
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
// Before: manual session management
|
|
53
|
+
await sandbox.exec("cd /app", { sessionId: "my-session" });
|
|
54
|
+
|
|
55
|
+
// After: automatic session per sandbox
|
|
56
|
+
await sandbox.exec("cd /app");
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
2. **Commands now maintain state**: Commands within the same sandbox now share state (working directory, environment variables, background processes). Previously each command was stateless.
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
// Before: each exec was independent
|
|
63
|
+
await sandbox.exec("cd /app");
|
|
64
|
+
await sandbox.exec("pwd"); // Output: /workspace
|
|
65
|
+
|
|
66
|
+
// After: state persists in session
|
|
67
|
+
await sandbox.exec("cd /app");
|
|
68
|
+
await sandbox.exec("pwd"); // Output: /app
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Migration guide:**
|
|
72
|
+
|
|
73
|
+
- Remove `sessionId` from all method calls - each sandbox maintains its own session
|
|
74
|
+
- If you need isolated execution contexts within the same sandbox, use `sandbox.createSession()`:
|
|
75
|
+
```javascript
|
|
76
|
+
// Create independent sessions with different environments
|
|
77
|
+
const buildSession = await sandbox.createSession({
|
|
78
|
+
name: "build",
|
|
79
|
+
env: { NODE_ENV: "production" },
|
|
80
|
+
cwd: "/build",
|
|
81
|
+
});
|
|
82
|
+
const testSession = await sandbox.createSession({
|
|
83
|
+
name: "test",
|
|
84
|
+
env: { NODE_ENV: "test" },
|
|
85
|
+
cwd: "/test",
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
- Environment variables set in one command persist to the next
|
|
89
|
+
- Background processes remain active until explicitly killed
|
|
90
|
+
- Requires CAP_SYS_ADMIN (available in production, falls back gracefully in dev)
|
|
91
|
+
|
|
92
|
+
### Patch Changes
|
|
93
|
+
|
|
94
|
+
- [#62](https://github.com/cloudflare/sandbox-sdk/pull/62) [`4bedc3a`](https://github.com/cloudflare/sandbox-sdk/commit/4bedc3aba347f3d4090a6efe2c9778bac00ce74a) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix broken build due to bun lockfile not being used
|
|
95
|
+
|
|
96
|
+
## 0.2.4
|
|
97
|
+
|
|
98
|
+
### Patch Changes
|
|
99
|
+
|
|
100
|
+
- [#57](https://github.com/cloudflare/sandbox-sdk/pull/57) [`12bbd12`](https://github.com/cloudflare/sandbox-sdk/commit/12bbd1229c07ef8c1c0bf58a4235a27938155b08) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Add listFiles method
|
|
101
|
+
|
|
102
|
+
## 0.2.3
|
|
103
|
+
|
|
104
|
+
### Patch Changes
|
|
105
|
+
|
|
106
|
+
- [#53](https://github.com/cloudflare/sandbox-sdk/pull/53) [`c87db11`](https://github.com/cloudflare/sandbox-sdk/commit/c87db117693a86cfb667bf09fb7720d6a6e0524d) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Improve jupyterlab config to speed up startup
|
|
107
|
+
|
|
108
|
+
## 0.2.2
|
|
109
|
+
|
|
110
|
+
### Patch Changes
|
|
111
|
+
|
|
112
|
+
- [#51](https://github.com/cloudflare/sandbox-sdk/pull/51) [`4aceb32`](https://github.com/cloudflare/sandbox-sdk/commit/4aceb3215c836f59afcb88b2b325016b3f623f46) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Handle intermittent interpreter failures and decouple jupyter startup
|
|
113
|
+
|
|
114
|
+
## 0.2.1
|
|
115
|
+
|
|
116
|
+
### Patch Changes
|
|
117
|
+
|
|
118
|
+
- [#49](https://github.com/cloudflare/sandbox-sdk/pull/49) [`d81d2a5`](https://github.com/cloudflare/sandbox-sdk/commit/d81d2a563c9af8947d5444019ed4d6156db563e3) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Implement code interpreter API
|
|
119
|
+
|
|
3
120
|
## 0.2.0
|
|
4
121
|
|
|
5
122
|
### Minor Changes
|
package/Dockerfile
CHANGED
|
@@ -33,22 +33,17 @@ RUN apt-get update && apt-get install -y \
|
|
|
33
33
|
python3-pip \
|
|
34
34
|
python3.11-venv \
|
|
35
35
|
# Other useful tools
|
|
36
|
-
sudo \
|
|
37
36
|
ca-certificates \
|
|
38
37
|
gnupg \
|
|
39
38
|
lsb-release \
|
|
39
|
+
strace \
|
|
40
40
|
&& rm -rf /var/lib/apt/lists/*
|
|
41
41
|
|
|
42
42
|
# Set Python 3.11 as default python3
|
|
43
43
|
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
|
|
44
44
|
|
|
45
|
-
# Install Node.js 20 LTS
|
|
46
|
-
|
|
47
|
-
RUN apt-get update && apt-get install -y ca-certificates curl gnupg \
|
|
48
|
-
&& mkdir -p /etc/apt/keyrings \
|
|
49
|
-
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
|
|
50
|
-
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
|
|
51
|
-
&& apt-get update \
|
|
45
|
+
# Install Node.js 20 LTS using official NodeSource setup script
|
|
46
|
+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
|
|
52
47
|
&& apt-get install -y nodejs \
|
|
53
48
|
&& rm -rf /var/lib/apt/lists/*
|
|
54
49
|
|
|
@@ -56,21 +51,12 @@ RUN apt-get update && apt-get install -y ca-certificates curl gnupg \
|
|
|
56
51
|
COPY --from=bun-source /usr/local/bin/bun /usr/local/bin/bun
|
|
57
52
|
COPY --from=bun-source /usr/local/bin/bunx /usr/local/bin/bunx
|
|
58
53
|
|
|
59
|
-
# Install
|
|
54
|
+
# Install essential Python packages for code execution
|
|
60
55
|
RUN pip3 install --no-cache-dir \
|
|
61
|
-
jupyter \
|
|
62
|
-
jupyterlab \
|
|
63
|
-
ipykernel \
|
|
64
|
-
notebook \
|
|
65
56
|
matplotlib \
|
|
66
57
|
numpy \
|
|
67
58
|
pandas \
|
|
68
|
-
|
|
69
|
-
&& python3 -m ipykernel install --user --name python3
|
|
70
|
-
|
|
71
|
-
# Install JavaScript kernel (ijavascript) - using E2B's fork
|
|
72
|
-
RUN npm install -g --unsafe-perm git+https://github.com/e2b-dev/ijavascript.git \
|
|
73
|
-
&& ijsinstall --install=global
|
|
59
|
+
ipython
|
|
74
60
|
|
|
75
61
|
# Set up container server directory
|
|
76
62
|
WORKDIR /container-server
|
|
@@ -79,21 +65,35 @@ WORKDIR /container-server
|
|
|
79
65
|
RUN python3 --version && \
|
|
80
66
|
node --version && \
|
|
81
67
|
npm --version && \
|
|
82
|
-
bun --version
|
|
83
|
-
jupyter --version && \
|
|
84
|
-
jupyter kernelspec list
|
|
68
|
+
bun --version
|
|
85
69
|
|
|
86
70
|
# Copy container source files to server directory
|
|
87
|
-
COPY container_src/package.json ./
|
|
88
|
-
RUN bun install
|
|
71
|
+
COPY container_src/package.json container_src/bun.lock ./
|
|
72
|
+
RUN bun install --frozen-lockfile
|
|
89
73
|
|
|
90
74
|
COPY container_src/ ./
|
|
91
75
|
|
|
92
|
-
#
|
|
76
|
+
# Compile TypeScript files using the locally installed TypeScript
|
|
77
|
+
RUN npx tsc control-process.ts --outDir . --module commonjs --target es2020 --esModuleInterop --skipLibCheck
|
|
78
|
+
RUN cd runtime/executors/javascript && npx tsc node_executor.ts --module commonjs --target es2020 --esModuleInterop --skipLibCheck
|
|
79
|
+
RUN cd runtime/executors/typescript && npx tsc ts_executor.ts --module commonjs --target es2020 --esModuleInterop --skipLibCheck
|
|
80
|
+
|
|
81
|
+
# Configure process pool sizes (can be overridden at runtime)
|
|
82
|
+
ENV PYTHON_POOL_MIN_SIZE=3
|
|
83
|
+
ENV PYTHON_POOL_MAX_SIZE=15
|
|
84
|
+
ENV JAVASCRIPT_POOL_MIN_SIZE=3
|
|
85
|
+
ENV JAVASCRIPT_POOL_MAX_SIZE=10
|
|
86
|
+
ENV TYPESCRIPT_POOL_MIN_SIZE=3
|
|
87
|
+
ENV TYPESCRIPT_POOL_MAX_SIZE=10
|
|
88
|
+
|
|
89
|
+
# Create clean workspace directory for user code
|
|
90
|
+
# Architecture:
|
|
91
|
+
# /container-server/ - SDK infrastructure (server, executors, dependencies)
|
|
92
|
+
# /workspace/ - User's clean workspace for their code
|
|
93
93
|
RUN mkdir -p /workspace
|
|
94
94
|
|
|
95
|
-
# Expose the application port (3000 for control
|
|
96
|
-
EXPOSE 3000
|
|
95
|
+
# Expose the application port (3000 for control)
|
|
96
|
+
EXPOSE 3000
|
|
97
97
|
|
|
98
98
|
# Make startup script executable
|
|
99
99
|
RUN chmod +x startup.sh
|
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ npm install @cloudflare/sandbox
|
|
|
72
72
|
1. **Create a Dockerfile** (temporary requirement, will be removed in future releases):
|
|
73
73
|
|
|
74
74
|
```dockerfile
|
|
75
|
-
FROM docker.io/cloudflare/sandbox:0.
|
|
75
|
+
FROM docker.io/cloudflare/sandbox:0.3.4
|
|
76
76
|
|
|
77
77
|
# Expose the ports you want to expose
|
|
78
78
|
EXPOSE 3000
|
|
@@ -89,7 +89,7 @@ EXPOSE 3000
|
|
|
89
89
|
{
|
|
90
90
|
"class_name": "Sandbox",
|
|
91
91
|
"image": "./Dockerfile",
|
|
92
|
-
"max_instances":
|
|
92
|
+
"max_instances": 20
|
|
93
93
|
}
|
|
94
94
|
],
|
|
95
95
|
"durable_objects": {
|
|
@@ -254,6 +254,14 @@ console.log(result.stdout); // "production"
|
|
|
254
254
|
- `unexposePort(port)` - Remove port exposure
|
|
255
255
|
- `getExposedPorts()` - List all exposed ports with their URLs
|
|
256
256
|
|
|
257
|
+
#### Session Methods
|
|
258
|
+
|
|
259
|
+
- `createSession(options)` - Create an isolated execution session
|
|
260
|
+
- `name`: Session identifier
|
|
261
|
+
- `env`: Environment variables for this session
|
|
262
|
+
- `cwd`: Working directory
|
|
263
|
+
- `isolation`: Enable PID namespace isolation (requires CAP_SYS_ADMIN)
|
|
264
|
+
|
|
257
265
|
<h2 id="code-interpreter">🧪 Code Interpreter</h2>
|
|
258
266
|
|
|
259
267
|
The Sandbox SDK includes powerful code interpreter capabilities, allowing you to execute Python and JavaScript code with rich outputs including charts, tables, and formatted data.
|
|
@@ -703,17 +711,71 @@ for await (const log of parseSSEStream<LogEvent>(logStream)) {
|
|
|
703
711
|
|
|
704
712
|
### Session Management
|
|
705
713
|
|
|
706
|
-
|
|
714
|
+
The SDK provides two approaches for managing execution context:
|
|
715
|
+
|
|
716
|
+
#### Implicit Sessions (Recommended)
|
|
717
|
+
|
|
718
|
+
Each sandbox maintains its own persistent session automatically:
|
|
707
719
|
|
|
708
720
|
```typescript
|
|
709
|
-
const
|
|
721
|
+
const sandbox = getSandbox(env.Sandbox, "my-app");
|
|
710
722
|
|
|
711
|
-
//
|
|
712
|
-
await sandbox.exec("cd /
|
|
713
|
-
await sandbox.exec("
|
|
714
|
-
|
|
723
|
+
// These commands share state (pwd, env vars, etc.)
|
|
724
|
+
await sandbox.exec("cd /app");
|
|
725
|
+
await sandbox.exec("pwd"); // Output: /app
|
|
726
|
+
await sandbox.exec("export MY_VAR=hello");
|
|
727
|
+
await sandbox.exec("echo $MY_VAR"); // Output: hello
|
|
715
728
|
```
|
|
716
729
|
|
|
730
|
+
#### Explicit Sessions for Advanced Use Cases
|
|
731
|
+
|
|
732
|
+
Create isolated execution contexts within the same sandbox:
|
|
733
|
+
|
|
734
|
+
```typescript
|
|
735
|
+
const sandbox = getSandbox(env.Sandbox, "multi-env");
|
|
736
|
+
|
|
737
|
+
// Create independent sessions with different environments
|
|
738
|
+
const buildSession = await sandbox.createSession({
|
|
739
|
+
name: "build",
|
|
740
|
+
env: { NODE_ENV: "production" },
|
|
741
|
+
cwd: "/build"
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
const testSession = await sandbox.createSession({
|
|
745
|
+
name: "test",
|
|
746
|
+
env: { NODE_ENV: "test" },
|
|
747
|
+
cwd: "/test"
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
// Run commands in parallel with different contexts
|
|
751
|
+
await Promise.all([
|
|
752
|
+
buildSession.exec("npm run build"),
|
|
753
|
+
testSession.exec("npm test")
|
|
754
|
+
]);
|
|
755
|
+
```
|
|
756
|
+
|
|
757
|
+
#### Security with AI Agents
|
|
758
|
+
|
|
759
|
+
When using AI coding agents, separate development from execution:
|
|
760
|
+
|
|
761
|
+
```typescript
|
|
762
|
+
// Phase 1: AI agent writes code (with API keys)
|
|
763
|
+
const devSession = await sandbox.createSession({
|
|
764
|
+
name: "ai-development",
|
|
765
|
+
env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY }
|
|
766
|
+
});
|
|
767
|
+
await devSession.exec('opencode "build a web server"');
|
|
768
|
+
|
|
769
|
+
// Phase 2: Run the generated code (without API keys)
|
|
770
|
+
const appSession = await sandbox.createSession({
|
|
771
|
+
name: "app-runtime",
|
|
772
|
+
env: { PORT: "3000" } // Only app-specific vars
|
|
773
|
+
});
|
|
774
|
+
await appSession.exec("node server.js");
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
> **Best Practice**: Keep AI agent credentials separate from your application runtime to prevent accidental exposure of API keys.
|
|
778
|
+
|
|
717
779
|
<h2 id="debugging">🔍 Debugging</h2>
|
|
718
780
|
|
|
719
781
|
Enable verbose logging:
|
package/container_src/bun.lock
CHANGED
|
@@ -4,119 +4,73 @@
|
|
|
4
4
|
"": {
|
|
5
5
|
"name": "sandbox-server",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"
|
|
7
|
+
"esbuild": "^0.21.5",
|
|
8
8
|
"uuid": "^9.0.1",
|
|
9
|
-
"ws": "^8.16.0",
|
|
10
9
|
},
|
|
11
10
|
"devDependencies": {
|
|
11
|
+
"@types/node": "^20.0.0",
|
|
12
12
|
"@types/uuid": "^9.0.7",
|
|
13
|
-
"
|
|
13
|
+
"typescript": "^5.3.0",
|
|
14
14
|
},
|
|
15
15
|
},
|
|
16
16
|
},
|
|
17
17
|
"packages": {
|
|
18
|
-
"@
|
|
18
|
+
"@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.21.5", "", { "os": "aix", "cpu": "ppc64" }, "sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ=="],
|
|
19
19
|
|
|
20
|
-
"@
|
|
20
|
+
"@esbuild/android-arm": ["@esbuild/android-arm@0.21.5", "", { "os": "android", "cpu": "arm" }, "sha512-vCPvzSjpPHEi1siZdlvAlsPxXl7WbOVUBBAowWug4rJHb68Ox8KualB+1ocNvT5fjv6wpkX6o/iEpbDrf68zcg=="],
|
|
21
21
|
|
|
22
|
-
"@
|
|
22
|
+
"@esbuild/android-arm64": ["@esbuild/android-arm64@0.21.5", "", { "os": "android", "cpu": "arm64" }, "sha512-c0uX9VAUBQ7dTDCjq+wdyGLowMdtR/GoC2U5IYk/7D1H1JYC0qseD7+11iMP2mRLN9RcCMRcjC4YMclCzGwS/A=="],
|
|
23
23
|
|
|
24
|
-
"@
|
|
24
|
+
"@esbuild/android-x64": ["@esbuild/android-x64@0.21.5", "", { "os": "android", "cpu": "x64" }, "sha512-D7aPRUUNHRBwHxzxRvp856rjUHRFW1SdQATKXH2hqA0kAZb1hKmi02OpYRacl0TxIGz/ZmXWlbZgjwWYaCakTA=="],
|
|
25
25
|
|
|
26
|
-
"@
|
|
26
|
+
"@esbuild/darwin-arm64": ["@esbuild/darwin-arm64@0.21.5", "", { "os": "darwin", "cpu": "arm64" }, "sha512-DwqXqZyuk5AiWWf3UfLiRDJ5EDd49zg6O9wclZ7kUMv2WRFr4HKjXp/5t8JZ11QbQfUS6/cRCKGwYhtNAY88kQ=="],
|
|
27
27
|
|
|
28
|
-
"@
|
|
28
|
+
"@esbuild/darwin-x64": ["@esbuild/darwin-x64@0.21.5", "", { "os": "darwin", "cpu": "x64" }, "sha512-se/JjF8NlmKVG4kNIuyWMV/22ZaerB+qaSi5MdrXtd6R08kvs2qCN4C09miupktDitvh8jRFflwGFBQcxZRjbw=="],
|
|
29
29
|
|
|
30
|
-
"@
|
|
30
|
+
"@esbuild/freebsd-arm64": ["@esbuild/freebsd-arm64@0.21.5", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-5JcRxxRDUJLX8JXp/wcBCy3pENnCgBR9bN6JsY4OmhfUtIHe3ZW0mawA7+RDAcMLrMIZaf03NlQiX9DGyB8h4g=="],
|
|
31
31
|
|
|
32
|
-
"@
|
|
32
|
+
"@esbuild/freebsd-x64": ["@esbuild/freebsd-x64@0.21.5", "", { "os": "freebsd", "cpu": "x64" }, "sha512-J95kNBj1zkbMXtHVH29bBriQygMXqoVQOQYA+ISs0/2l3T9/kj42ow2mpqerRBxDJnmkUDCaQT/dfNXWX/ZZCQ=="],
|
|
33
33
|
|
|
34
|
-
"@
|
|
34
|
+
"@esbuild/linux-arm": ["@esbuild/linux-arm@0.21.5", "", { "os": "linux", "cpu": "arm" }, "sha512-bPb5AHZtbeNGjCKVZ9UGqGwo8EUu4cLq68E95A53KlxAPRmUyYv2D6F0uUI65XisGOL1hBP5mTronbgo+0bFcA=="],
|
|
35
35
|
|
|
36
|
-
"@
|
|
36
|
+
"@esbuild/linux-arm64": ["@esbuild/linux-arm64@0.21.5", "", { "os": "linux", "cpu": "arm64" }, "sha512-ibKvmyYzKsBeX8d8I7MH/TMfWDXBF3db4qM6sy+7re0YXya+K1cem3on9XgdT2EQGMu4hQyZhan7TeQ8XkGp4Q=="],
|
|
37
37
|
|
|
38
|
-
"@
|
|
38
|
+
"@esbuild/linux-ia32": ["@esbuild/linux-ia32@0.21.5", "", { "os": "linux", "cpu": "ia32" }, "sha512-YvjXDqLRqPDl2dvRODYmmhz4rPeVKYvppfGYKSNGdyZkA01046pLWyRKKI3ax8fbJoK5QbxblURkwK/MWY18Tg=="],
|
|
39
39
|
|
|
40
|
-
"@
|
|
40
|
+
"@esbuild/linux-loong64": ["@esbuild/linux-loong64@0.21.5", "", { "os": "linux", "cpu": "none" }, "sha512-uHf1BmMG8qEvzdrzAqg2SIG/02+4/DHB6a9Kbya0XDvwDEKCoC8ZRWI5JJvNdUjtciBGFQ5PuBlpEOXQj+JQSg=="],
|
|
41
41
|
|
|
42
|
-
"@
|
|
42
|
+
"@esbuild/linux-mips64el": ["@esbuild/linux-mips64el@0.21.5", "", { "os": "linux", "cpu": "none" }, "sha512-IajOmO+KJK23bj52dFSNCMsz1QP1DqM6cwLUv3W1QwyxkyIWecfafnI555fvSGqEKwjMXVLokcV5ygHW5b3Jbg=="],
|
|
43
43
|
|
|
44
|
-
"@
|
|
44
|
+
"@esbuild/linux-ppc64": ["@esbuild/linux-ppc64@0.21.5", "", { "os": "linux", "cpu": "ppc64" }, "sha512-1hHV/Z4OEfMwpLO8rp7CvlhBDnjsC3CttJXIhBi+5Aj5r+MBvy4egg7wCbe//hSsT+RvDAG7s81tAvpL2XAE4w=="],
|
|
45
45
|
|
|
46
|
-
"@
|
|
46
|
+
"@esbuild/linux-riscv64": ["@esbuild/linux-riscv64@0.21.5", "", { "os": "linux", "cpu": "none" }, "sha512-2HdXDMd9GMgTGrPWnJzP2ALSokE/0O5HhTUvWIbD3YdjME8JwvSCnNGBnTThKGEB91OZhzrJ4qIIxk/SBmyDDA=="],
|
|
47
47
|
|
|
48
|
-
"@
|
|
48
|
+
"@esbuild/linux-s390x": ["@esbuild/linux-s390x@0.21.5", "", { "os": "linux", "cpu": "s390x" }, "sha512-zus5sxzqBJD3eXxwvjN1yQkRepANgxE9lgOW2qLnmr8ikMTphkjgXu1HR01K4FJg8h1kEEDAqDcZQtbrRnB41A=="],
|
|
49
49
|
|
|
50
|
-
"@
|
|
50
|
+
"@esbuild/linux-x64": ["@esbuild/linux-x64@0.21.5", "", { "os": "linux", "cpu": "x64" }, "sha512-1rYdTpyv03iycF1+BhzrzQJCdOuAOtaqHTWJZCWvijKD2N5Xu0TtVC8/+1faWqcP9iBCWOmjmhoH94dH82BxPQ=="],
|
|
51
51
|
|
|
52
|
-
"@
|
|
52
|
+
"@esbuild/netbsd-x64": ["@esbuild/netbsd-x64@0.21.5", "", { "os": "none", "cpu": "x64" }, "sha512-Woi2MXzXjMULccIwMnLciyZH4nCIMpWQAs049KEeMvOcNADVxo0UBIQPfSmxB3CWKedngg7sWZdLvLczpe0tLg=="],
|
|
53
53
|
|
|
54
|
-
"@
|
|
55
|
-
|
|
56
|
-
"@types/ws": ["@types/ws@8.18.1", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="],
|
|
57
|
-
|
|
58
|
-
"ajv": ["ajv@8.17.1", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2" } }, "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g=="],
|
|
59
|
-
|
|
60
|
-
"compute-gcd": ["compute-gcd@1.2.1", "", { "dependencies": { "validate.io-array": "^1.0.3", "validate.io-function": "^1.0.2", "validate.io-integer-array": "^1.0.0" } }, "sha512-TwMbxBNz0l71+8Sc4czv13h4kEqnchV9igQZBi6QUaz09dnz13juGnnaWWJTRsP3brxOoxeB4SA2WELLw1hCtg=="],
|
|
61
|
-
|
|
62
|
-
"compute-lcm": ["compute-lcm@1.1.2", "", { "dependencies": { "compute-gcd": "^1.2.1", "validate.io-array": "^1.0.3", "validate.io-function": "^1.0.2", "validate.io-integer-array": "^1.0.0" } }, "sha512-OFNPdQAXnQhDSKioX8/XYT6sdUlXwpeMjfd6ApxMJfyZ4GxmLR1xvMERctlYhlHwIiz6CSpBc2+qYKjHGZw4TQ=="],
|
|
63
|
-
|
|
64
|
-
"fast-deep-equal": ["fast-deep-equal@3.1.3", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="],
|
|
65
|
-
|
|
66
|
-
"fast-uri": ["fast-uri@3.0.6", "", {}, "sha512-Atfo14OibSv5wAp4VWNsFYE1AchQRTv9cBGWET4pZWHzYshFSS9NQI6I57rdKn9croWVMbYFbLhJ+yJvmZIIHw=="],
|
|
67
|
-
|
|
68
|
-
"isomorphic.js": ["isomorphic.js@0.2.5", "", {}, "sha512-PIeMbHqMt4DnUP3MA/Flc0HElYjMXArsw1qwJZcm9sqR8mq3l8NYizFMty0pWwE/tzIGH3EKK5+jes5mAr85yw=="],
|
|
69
|
-
|
|
70
|
-
"json-schema-compare": ["json-schema-compare@0.2.2", "", { "dependencies": { "lodash": "^4.17.4" } }, "sha512-c4WYmDKyJXhs7WWvAWm3uIYnfyWFoIp+JEoX34rctVvEkMYCPGhXtvmFFXiffBbxfZsvQ0RNnV5H7GvDF5HCqQ=="],
|
|
71
|
-
|
|
72
|
-
"json-schema-merge-allof": ["json-schema-merge-allof@0.8.1", "", { "dependencies": { "compute-lcm": "^1.1.2", "json-schema-compare": "^0.2.2", "lodash": "^4.17.20" } }, "sha512-CTUKmIlPJbsWfzRRnOXz+0MjIqvnleIXwFTzz+t9T86HnYX/Rozria6ZVGLktAU9e+NygNljveP+yxqtQp/Q4w=="],
|
|
73
|
-
|
|
74
|
-
"json-schema-traverse": ["json-schema-traverse@1.0.0", "", {}, "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="],
|
|
75
|
-
|
|
76
|
-
"json5": ["json5@2.2.3", "", { "bin": { "json5": "lib/cli.js" } }, "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg=="],
|
|
77
|
-
|
|
78
|
-
"jsonpointer": ["jsonpointer@5.0.1", "", {}, "sha512-p/nXbhSEcu3pZRdkW1OfJhpsVtW1gd4Wa1fnQc9YLiTfAjn0312eMKimbdIQzuZl9aa9xUGaRlP9T/CJE/ditQ=="],
|
|
54
|
+
"@esbuild/openbsd-x64": ["@esbuild/openbsd-x64@0.21.5", "", { "os": "openbsd", "cpu": "x64" }, "sha512-HLNNw99xsvx12lFBUwoT8EVCsSvRNDVxNpjZ7bPn947b8gJPzeHWyNVhFsaerc0n3TsbOINvRP2byTZ5LKezow=="],
|
|
79
55
|
|
|
80
|
-
"
|
|
56
|
+
"@esbuild/sunos-x64": ["@esbuild/sunos-x64@0.21.5", "", { "os": "sunos", "cpu": "x64" }, "sha512-6+gjmFpfy0BHU5Tpptkuh8+uw3mnrvgs+dSPQXQOv3ekbordwnzTVEb4qnIvQcYXq6gzkyTnoZ9dZG+D4garKg=="],
|
|
81
57
|
|
|
82
|
-
"
|
|
58
|
+
"@esbuild/win32-arm64": ["@esbuild/win32-arm64@0.21.5", "", { "os": "win32", "cpu": "arm64" }, "sha512-Z0gOTd75VvXqyq7nsl93zwahcTROgqvuAcYDUr+vOv8uHhNSKROyU961kgtCD1e95IqPKSQKH7tBTslnS3tA8A=="],
|
|
83
59
|
|
|
84
|
-
"
|
|
60
|
+
"@esbuild/win32-ia32": ["@esbuild/win32-ia32@0.21.5", "", { "os": "win32", "cpu": "ia32" }, "sha512-SWXFF1CL2RVNMaVs+BBClwtfZSvDgtL//G/smwAc5oVK/UPu2Gu9tIaRgFmYFFKrmg3SyAjSrElf0TiJ1v8fYA=="],
|
|
85
61
|
|
|
86
|
-
"
|
|
62
|
+
"@esbuild/win32-x64": ["@esbuild/win32-x64@0.21.5", "", { "os": "win32", "cpu": "x64" }, "sha512-tQd/1efJuzPC6rCFwEvLtci/xNFcTZknmXs98FYDfGE4wP9ClFV98nyKrzJKVPMhdDnjzLhdUyMX4PsQAPjwIw=="],
|
|
87
63
|
|
|
88
|
-
"
|
|
64
|
+
"@types/node": ["@types/node@20.19.16", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-VS6TTONVdgwJwtJr7U+ghEjpfmQdqehLLpg/iMYGOd1+ilaFjdBJwFuPggJ4EAYPDCzWfDUHoIxyVnu+tOWVuQ=="],
|
|
89
65
|
|
|
90
|
-
"
|
|
91
|
-
|
|
92
|
-
"react": ["react@19.1.1", "", {}, "sha512-w8nqGImo45dmMIfljjMwOGtbmC/mk4CMYhWIicdSflH91J9TyCyczcPFXJzrZ/ZXcgGRFeP6BU0BEJTw6tZdfQ=="],
|
|
93
|
-
|
|
94
|
-
"react-is": ["react-is@18.3.1", "", {}, "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg=="],
|
|
95
|
-
|
|
96
|
-
"require-from-string": ["require-from-string@2.0.2", "", {}, "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw=="],
|
|
66
|
+
"@types/uuid": ["@types/uuid@9.0.8", "", {}, "sha512-jg+97EGIcY9AGHJJRaaPVgetKDsrTgbRjQ5Msgjh/DQKEFl0DtyRr/VCOyD1T2R1MNeWPK/u7JoGhlDZnKBAfA=="],
|
|
97
67
|
|
|
98
|
-
"
|
|
68
|
+
"esbuild": ["esbuild@0.21.5", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.21.5", "@esbuild/android-arm": "0.21.5", "@esbuild/android-arm64": "0.21.5", "@esbuild/android-x64": "0.21.5", "@esbuild/darwin-arm64": "0.21.5", "@esbuild/darwin-x64": "0.21.5", "@esbuild/freebsd-arm64": "0.21.5", "@esbuild/freebsd-x64": "0.21.5", "@esbuild/linux-arm": "0.21.5", "@esbuild/linux-arm64": "0.21.5", "@esbuild/linux-ia32": "0.21.5", "@esbuild/linux-loong64": "0.21.5", "@esbuild/linux-mips64el": "0.21.5", "@esbuild/linux-ppc64": "0.21.5", "@esbuild/linux-riscv64": "0.21.5", "@esbuild/linux-s390x": "0.21.5", "@esbuild/linux-x64": "0.21.5", "@esbuild/netbsd-x64": "0.21.5", "@esbuild/openbsd-x64": "0.21.5", "@esbuild/sunos-x64": "0.21.5", "@esbuild/win32-arm64": "0.21.5", "@esbuild/win32-ia32": "0.21.5", "@esbuild/win32-x64": "0.21.5" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw=="],
|
|
99
69
|
|
|
100
|
-
"
|
|
70
|
+
"typescript": ["typescript@5.9.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A=="],
|
|
101
71
|
|
|
102
|
-
"
|
|
72
|
+
"undici-types": ["undici-types@6.21.0", "", {}, "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="],
|
|
103
73
|
|
|
104
74
|
"uuid": ["uuid@9.0.1", "", { "bin": { "uuid": "dist/bin/uuid" } }, "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA=="],
|
|
105
|
-
|
|
106
|
-
"validate.io-array": ["validate.io-array@1.0.6", "", {}, "sha512-DeOy7CnPEziggrOO5CZhVKJw6S3Yi7e9e65R1Nl/RTN1vTQKnzjfvks0/8kQ40FP/dsjRAOd4hxmJ7uLa6vxkg=="],
|
|
107
|
-
|
|
108
|
-
"validate.io-function": ["validate.io-function@1.0.2", "", {}, "sha512-LlFybRJEriSuBnUhQyG5bwglhh50EpTL2ul23MPIuR1odjO7XaMLFV8vHGwp7AZciFxtYOeiSCT5st+XSPONiQ=="],
|
|
109
|
-
|
|
110
|
-
"validate.io-integer": ["validate.io-integer@1.0.5", "", { "dependencies": { "validate.io-number": "^1.0.3" } }, "sha512-22izsYSLojN/P6bppBqhgUDjCkr5RY2jd+N2a3DCAUey8ydvrZ/OkGvFPR7qfOpwR2LC5p4Ngzxz36g5Vgr/hQ=="],
|
|
111
|
-
|
|
112
|
-
"validate.io-integer-array": ["validate.io-integer-array@1.0.0", "", { "dependencies": { "validate.io-array": "^1.0.3", "validate.io-integer": "^1.0.4" } }, "sha512-mTrMk/1ytQHtCY0oNO3dztafHYyGU88KL+jRxWuzfOmQb+4qqnWmI+gykvGp8usKZOM0H7keJHEbRaFiYA0VrA=="],
|
|
113
|
-
|
|
114
|
-
"validate.io-number": ["validate.io-number@1.0.3", "", {}, "sha512-kRAyotcbNaSYoDnXvb4MHg/0a1egJdLwS6oJ38TJY7aw9n93Fl/3blIXdyYvPOp55CNxywooG/3BcrwNrBpcSg=="],
|
|
115
|
-
|
|
116
|
-
"ws": ["ws@8.18.3", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg=="],
|
|
117
|
-
|
|
118
|
-
"y-protocols": ["y-protocols@1.0.6", "", { "dependencies": { "lib0": "^0.2.85" }, "peerDependencies": { "yjs": "^13.0.0" } }, "sha512-vHRF2L6iT3rwj1jub/K5tYcTT/mEYDUppgNPXwp8fmLpui9f7Yeq3OEtTLVF012j39QnV+KEQpNqoN7CWU7Y9Q=="],
|
|
119
|
-
|
|
120
|
-
"yjs": ["yjs@13.6.27", "", { "dependencies": { "lib0": "^0.2.99" } }, "sha512-OIDwaflOaq4wC6YlPBy2L6ceKeKuF7DeTxx+jPzv1FHn9tCZ0ZwSRnUBxD05E3yed46fv/FWJbvR+Ud7x0L7zw=="],
|
|
121
75
|
}
|
|
122
76
|
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Circuit Breaker implementation to prevent cascading failures
|
|
3
|
+
*/
|
|
4
|
+
export class CircuitBreaker {
|
|
5
|
+
private failures = 0;
|
|
6
|
+
private lastFailure: number = 0;
|
|
7
|
+
private successCount = 0;
|
|
8
|
+
private state: "closed" | "open" | "half-open" = "closed";
|
|
9
|
+
|
|
10
|
+
// Configuration
|
|
11
|
+
private readonly threshold: number;
|
|
12
|
+
private readonly timeout: number;
|
|
13
|
+
private readonly halfOpenSuccessThreshold: number;
|
|
14
|
+
private readonly name: string;
|
|
15
|
+
|
|
16
|
+
constructor(options: {
|
|
17
|
+
name: string;
|
|
18
|
+
threshold?: number;
|
|
19
|
+
timeout?: number;
|
|
20
|
+
halfOpenSuccessThreshold?: number;
|
|
21
|
+
}) {
|
|
22
|
+
this.name = options.name;
|
|
23
|
+
this.threshold = options.threshold || 5;
|
|
24
|
+
this.timeout = options.timeout || 30000; // 30 seconds
|
|
25
|
+
this.halfOpenSuccessThreshold = options.halfOpenSuccessThreshold || 3;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Execute an operation with circuit breaker protection
|
|
30
|
+
*/
|
|
31
|
+
async execute<T>(operation: () => Promise<T>): Promise<T> {
|
|
32
|
+
// Check circuit state
|
|
33
|
+
if (this.state === "open") {
|
|
34
|
+
if (Date.now() - this.lastFailure > this.timeout) {
|
|
35
|
+
console.log(
|
|
36
|
+
`[CircuitBreaker ${this.name}] Transitioning from open to half-open`
|
|
37
|
+
);
|
|
38
|
+
this.state = "half-open";
|
|
39
|
+
this.successCount = 0;
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Circuit breaker is open for ${this.name}. Retry after ${
|
|
43
|
+
this.timeout - (Date.now() - this.lastFailure)
|
|
44
|
+
}ms`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const result = await operation();
|
|
51
|
+
|
|
52
|
+
// Record success
|
|
53
|
+
if (this.state === "half-open") {
|
|
54
|
+
this.successCount++;
|
|
55
|
+
if (this.successCount >= this.halfOpenSuccessThreshold) {
|
|
56
|
+
console.log(
|
|
57
|
+
`[CircuitBreaker ${this.name}] Transitioning from half-open to closed`
|
|
58
|
+
);
|
|
59
|
+
this.state = "closed";
|
|
60
|
+
this.failures = 0;
|
|
61
|
+
}
|
|
62
|
+
} else if (this.state === "closed") {
|
|
63
|
+
// Reset failure count on success
|
|
64
|
+
this.failures = 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return result;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
this.recordFailure();
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Record a failure and update circuit state
|
|
76
|
+
*/
|
|
77
|
+
private recordFailure() {
|
|
78
|
+
this.failures++;
|
|
79
|
+
this.lastFailure = Date.now();
|
|
80
|
+
|
|
81
|
+
if (this.state === "half-open") {
|
|
82
|
+
console.log(
|
|
83
|
+
`[CircuitBreaker ${this.name}] Failure in half-open state, transitioning to open`
|
|
84
|
+
);
|
|
85
|
+
this.state = "open";
|
|
86
|
+
} else if (this.failures >= this.threshold) {
|
|
87
|
+
console.log(
|
|
88
|
+
`[CircuitBreaker ${this.name}] Threshold reached (${this.failures}/${this.threshold}), transitioning to open`
|
|
89
|
+
);
|
|
90
|
+
this.state = "open";
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get current circuit breaker state
|
|
96
|
+
*/
|
|
97
|
+
getState(): {
|
|
98
|
+
state: string;
|
|
99
|
+
failures: number;
|
|
100
|
+
lastFailure: number;
|
|
101
|
+
isOpen: boolean;
|
|
102
|
+
} {
|
|
103
|
+
return {
|
|
104
|
+
state: this.state,
|
|
105
|
+
failures: this.failures,
|
|
106
|
+
lastFailure: this.lastFailure,
|
|
107
|
+
isOpen: this.state === "open",
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Reset the circuit breaker
|
|
113
|
+
*/
|
|
114
|
+
reset() {
|
|
115
|
+
this.state = "closed";
|
|
116
|
+
this.failures = 0;
|
|
117
|
+
this.successCount = 0;
|
|
118
|
+
this.lastFailure = 0;
|
|
119
|
+
console.log(`[CircuitBreaker ${this.name}] Reset to closed state`);
|
|
120
|
+
}
|
|
121
|
+
}
|