@molecule/api-code-sandbox-docker 1.0.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 +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/provider.d.ts +54 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +943 -0
- package/dist/types.d.ts +69 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-code-sandbox-docker` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-code-sandbox-docker is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Docker code-sandbox provider for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* **Tenant network isolation (secure default).** Each sandbox is placed on a dedicated
|
|
6
|
+
* user-defined Docker network created with inter-container communication DISABLED
|
|
7
|
+
* (`com.docker.network.bridge.enable_icc=false`), so one tenant's sandbox cannot reach another
|
|
8
|
+
* tenant's Vite/API dev-server ports by IP. Do NOT set `SANDBOX_DOCKER_NETWORK="bridge"` — the
|
|
9
|
+
* shared docker bridge has ICC enabled (no cross-tenant isolation) and is REFUSED in production.
|
|
10
|
+
* Override `SANDBOX_DOCKER_NETWORK` only to point at another dedicated ICC-off network. This is
|
|
11
|
+
* an L2 isolation control; pair it with host-layer default-deny egress filtering (operator-
|
|
12
|
+
* provisioned) for full isolation. [C1-1]
|
|
13
|
+
*
|
|
14
|
+
* **Prerequisites.** A reachable Docker daemon and the base image already pulled
|
|
15
|
+
* on the host. The daemon is reached over a unix socket by default
|
|
16
|
+
* (`config.socketPath` ?? `DOCKER_SOCKET_PATH` ?? `/var/run/docker.sock`); a
|
|
17
|
+
* remote or rootless daemon can be selected with `config.host`/`config.port` or a
|
|
18
|
+
* `DOCKER_HOST` (`tcp://host:port` or `unix:///path`). Only PLAIN (unencrypted)
|
|
19
|
+
* TCP is supported — front a TLS-protected daemon (2376) with a local socket
|
|
20
|
+
* proxy. The provider never pulls images, so `create()` fails with a no-such-image
|
|
21
|
+
* error if the base image (default `node:22-slim`, or `config.baseImage`) is
|
|
22
|
+
* absent. The isolated sandbox network is auto-created on first use; it is NOT a
|
|
23
|
+
* prerequisite.
|
|
24
|
+
*
|
|
25
|
+
* **No per-sandbox disk quota.** The Docker API cannot portably cap a
|
|
26
|
+
* container/volume size (it needs specific storage drivers — e.g. overlay2 on xfs
|
|
27
|
+
* with `pquota` — and errors on the common overlay2/ext4 host), so this provider
|
|
28
|
+
* enforces none. The core `resources.diskMB` is accepted but not applied here; cap
|
|
29
|
+
* disk at the host / volume level instead.
|
|
30
|
+
*
|
|
31
|
+
* @module
|
|
32
|
+
*/
|
|
33
|
+
export * from './browser-guard.js';
|
|
34
|
+
export * from './provider.js';
|
|
35
|
+
export * from './types.js';
|
|
36
|
+
import type { SandboxProvider } from '@molecule/api-code-sandbox';
|
|
37
|
+
/**
|
|
38
|
+
* The provider implementation.
|
|
39
|
+
*/
|
|
40
|
+
export declare const provider: SandboxProvider;
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAE1B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAMjE;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,eAUrB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Docker code-sandbox provider for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* **Tenant network isolation (secure default).** Each sandbox is placed on a dedicated
|
|
6
|
+
* user-defined Docker network created with inter-container communication DISABLED
|
|
7
|
+
* (`com.docker.network.bridge.enable_icc=false`), so one tenant's sandbox cannot reach another
|
|
8
|
+
* tenant's Vite/API dev-server ports by IP. Do NOT set `SANDBOX_DOCKER_NETWORK="bridge"` — the
|
|
9
|
+
* shared docker bridge has ICC enabled (no cross-tenant isolation) and is REFUSED in production.
|
|
10
|
+
* Override `SANDBOX_DOCKER_NETWORK` only to point at another dedicated ICC-off network. This is
|
|
11
|
+
* an L2 isolation control; pair it with host-layer default-deny egress filtering (operator-
|
|
12
|
+
* provisioned) for full isolation. [C1-1]
|
|
13
|
+
*
|
|
14
|
+
* **Prerequisites.** A reachable Docker daemon and the base image already pulled
|
|
15
|
+
* on the host. The daemon is reached over a unix socket by default
|
|
16
|
+
* (`config.socketPath` ?? `DOCKER_SOCKET_PATH` ?? `/var/run/docker.sock`); a
|
|
17
|
+
* remote or rootless daemon can be selected with `config.host`/`config.port` or a
|
|
18
|
+
* `DOCKER_HOST` (`tcp://host:port` or `unix:///path`). Only PLAIN (unencrypted)
|
|
19
|
+
* TCP is supported — front a TLS-protected daemon (2376) with a local socket
|
|
20
|
+
* proxy. The provider never pulls images, so `create()` fails with a no-such-image
|
|
21
|
+
* error if the base image (default `node:22-slim`, or `config.baseImage`) is
|
|
22
|
+
* absent. The isolated sandbox network is auto-created on first use; it is NOT a
|
|
23
|
+
* prerequisite.
|
|
24
|
+
*
|
|
25
|
+
* **No per-sandbox disk quota.** The Docker API cannot portably cap a
|
|
26
|
+
* container/volume size (it needs specific storage drivers — e.g. overlay2 on xfs
|
|
27
|
+
* with `pquota` — and errors on the common overlay2/ext4 host), so this provider
|
|
28
|
+
* enforces none. The core `resources.diskMB` is accepted but not applied here; cap
|
|
29
|
+
* disk at the host / volume level instead.
|
|
30
|
+
*
|
|
31
|
+
* @module
|
|
32
|
+
*/
|
|
33
|
+
export * from './browser-guard.js';
|
|
34
|
+
export * from './provider.js';
|
|
35
|
+
export * from './types.js';
|
|
36
|
+
import { createProvider } from './provider.js';
|
|
37
|
+
/** Lazily-initialized provider singleton. Defers creation until first use so that env vars / secrets are resolved. */
|
|
38
|
+
let _provider = null;
|
|
39
|
+
/**
|
|
40
|
+
* The provider implementation.
|
|
41
|
+
*/
|
|
42
|
+
export const provider = new Proxy({}, {
|
|
43
|
+
get(_, prop, receiver) {
|
|
44
|
+
if (!_provider)
|
|
45
|
+
_provider = createProvider();
|
|
46
|
+
return Reflect.get(_provider, prop, receiver);
|
|
47
|
+
},
|
|
48
|
+
// set trap: methods run with `this` bound to the proxy — without it, instance-state writes land on the dummy target and are lost (see api-push-notifications-web-push)
|
|
49
|
+
set(_, prop, value) {
|
|
50
|
+
if (!_provider)
|
|
51
|
+
_provider = createProvider();
|
|
52
|
+
return Reflect.set(_provider, prop, value);
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Docker implementation of SandboxProvider.
|
|
3
|
+
*
|
|
4
|
+
* Uses the Docker Engine API via HTTP to manage containers.
|
|
5
|
+
* Each sandbox runs as an isolated Docker container with resource limits.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { SandboxProvider } from '@molecule/api-code-sandbox';
|
|
10
|
+
import type { DockerConfig } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Retry a Docker API operation that failed with a TRANSIENT fault — a request
|
|
13
|
+
* timeout or a connection-level reset, i.e. the daemon was momentarily overwhelmed,
|
|
14
|
+
* not the request malformed. This is the fix for the observed "first cold boot of a
|
|
15
|
+
* concurrent batch dies on `Docker API timeout: POST /containers/create`" failure:
|
|
16
|
+
* a single 30 s create timeout under daemon load currently kills the whole boot.
|
|
17
|
+
*
|
|
18
|
+
* HTTP error responses (4xx/5xx) are deliberately NOT retried — those are real
|
|
19
|
+
* answers (no-such-image, name conflict), not transient network faults. Bounded
|
|
20
|
+
* attempts with linear backoff.
|
|
21
|
+
*
|
|
22
|
+
* `onRetry` is the idempotency guard: a create that TIMED OUT client-side may have
|
|
23
|
+
* still succeeded server-side, so before re-issuing it the caller can adopt the
|
|
24
|
+
* already-created resource (looked up by a unique label) instead of leaking a
|
|
25
|
+
* duplicate container. If `onRetry` returns non-null, that value is used and the
|
|
26
|
+
* operation is not re-issued.
|
|
27
|
+
*
|
|
28
|
+
* @param op - the operation to attempt.
|
|
29
|
+
* @param opts - tuning + the optional adopt-on-retry guard.
|
|
30
|
+
* @param opts.label - short operation name for log lines.
|
|
31
|
+
* @param opts.attempts - max attempts (default 3).
|
|
32
|
+
* @param opts.onRetry - adopt-an-existing-resource guard, run before each retry.
|
|
33
|
+
* @param opts.delayMs - backoff for attempt N (default `400 * N` ms).
|
|
34
|
+
* @param opts.log - optional logger for retry warnings.
|
|
35
|
+
* @returns the operation's result, or an adopted result from `onRetry`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function withTransientRetry<T>(op: () => Promise<T>, opts: {
|
|
38
|
+
label: string;
|
|
39
|
+
attempts?: number;
|
|
40
|
+
onRetry?: () => Promise<T | null>;
|
|
41
|
+
delayMs?: (attempt: number) => number;
|
|
42
|
+
log?: {
|
|
43
|
+
warn: (message: string, meta?: unknown) => void;
|
|
44
|
+
};
|
|
45
|
+
}): Promise<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new `DockerSandboxProvider` instance with the given configuration.
|
|
48
|
+
* @param config - Optional Docker-specific configuration: daemon endpoint
|
|
49
|
+
* (`socketPath`, or `host`/`port` for a TCP daemon; `DOCKER_HOST` is also
|
|
50
|
+
* honored), `baseImage`, CPU/memory defaults, and the sandbox `network`.
|
|
51
|
+
* @returns A `SandboxProvider` that manages Docker containers as sandboxes.
|
|
52
|
+
*/
|
|
53
|
+
export declare function createProvider(config?: DockerConfig): SandboxProvider;
|
|
54
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAOV,eAAe,EAEhB,MAAM,4BAA4B,CAAA;AAOnC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AA8I9C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,IAAI,EAAE;IACJ,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IACjC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAA;IACrC,GAAG,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAA;KAAE,CAAA;CAC1D,GACA,OAAO,CAAC,CAAC,CAAC,CA2BZ;AAo3BD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,eAAe,CAErE"}
|