@capsulos/sdk 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/dist/capsule.d.ts +55 -0
- package/dist/capsule.js +126 -0
- package/dist/capsule.js.map +1 -0
- package/dist/client.d.ts +31 -0
- package/dist/client.js +146 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/ports.d.ts +11 -0
- package/dist/ports.js +26 -0
- package/dist/ports.js.map +1 -0
- package/dist/pty.d.ts +23 -0
- package/dist/pty.js +122 -0
- package/dist/pty.js.map +1 -0
- package/dist/types.d.ts +168 -0
- package/dist/types.js +17 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -3
- package/src/capsule.ts +139 -0
- package/src/client.test.ts +79 -0
- package/src/client.ts +170 -0
- package/src/e2e.test.ts +189 -0
- package/src/index.ts +7 -0
- package/src/ports.ts +31 -0
- package/src/pty.ts +126 -0
- package/src/types.ts +185 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DaemonClient, type ClientOptions } from './client.js';
|
|
2
|
+
import { Ports } from './ports.js';
|
|
3
|
+
import { PtySession } from './pty.js';
|
|
4
|
+
import { type CapsuleSpec, type ExecOptions, type ExecResult, type FileEntry, type Health, type Provider, type PtyOptions } from './types.js';
|
|
5
|
+
export interface CapsuleOptions extends ClientOptions {
|
|
6
|
+
/** How long to wait for the daemon to answer /health after creation. */
|
|
7
|
+
readyTimeoutMs?: number;
|
|
8
|
+
/** How long the provider may take to run the capsule (default 120 s); on timeout it is terminated. */
|
|
9
|
+
createTimeoutMs?: number;
|
|
10
|
+
/** Abort creation (the provider terminates what it started) or the wait for the daemon. */
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
}
|
|
13
|
+
export declare class Capsule {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
private readonly provider;
|
|
16
|
+
private readonly client;
|
|
17
|
+
readonly commands: Commands;
|
|
18
|
+
readonly files: Files;
|
|
19
|
+
readonly pty: Pty;
|
|
20
|
+
/** Servers listening inside the capsule, reachable through the daemon (preview URLs). */
|
|
21
|
+
readonly ports: Ports;
|
|
22
|
+
private constructor();
|
|
23
|
+
/** Throw a capsule: create it with the provider and wait until it is ready. */
|
|
24
|
+
static create(provider: Provider, spec: CapsuleSpec, opts?: CapsuleOptions): Promise<Capsule>;
|
|
25
|
+
/** Reconnect to a capsule that already exists (it may be suspended: the first request resumes it). */
|
|
26
|
+
static connect(provider: Provider, id: string, opts?: CapsuleOptions): Promise<Capsule>;
|
|
27
|
+
health(): Promise<Health>;
|
|
28
|
+
/** Put the capsule back in your pocket. */
|
|
29
|
+
kill(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export declare class Commands {
|
|
32
|
+
private readonly client;
|
|
33
|
+
constructor(client: DaemonClient);
|
|
34
|
+
/** Run a command (through `sh -c` unless `args` is given). Pass onStdout/onStderr for live output. */
|
|
35
|
+
run(cmd: string, opts?: ExecOptions): Promise<ExecResult>;
|
|
36
|
+
}
|
|
37
|
+
export declare class Pty {
|
|
38
|
+
private readonly client;
|
|
39
|
+
constructor(client: DaemonClient);
|
|
40
|
+
/** Open an interactive terminal (a shell by default). */
|
|
41
|
+
open(opts?: PtyOptions): Promise<PtySession>;
|
|
42
|
+
}
|
|
43
|
+
export declare class Files {
|
|
44
|
+
private readonly client;
|
|
45
|
+
constructor(client: DaemonClient);
|
|
46
|
+
read(path: string): Promise<string>;
|
|
47
|
+
readBytes(path: string): Promise<Uint8Array>;
|
|
48
|
+
write(path: string, data: string | Uint8Array, opts?: {
|
|
49
|
+
mode?: string;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
list(path: string): Promise<FileEntry[]>;
|
|
52
|
+
stat(path: string): Promise<FileEntry>;
|
|
53
|
+
mkdir(path: string): Promise<void>;
|
|
54
|
+
remove(path: string): Promise<void>;
|
|
55
|
+
}
|
package/dist/capsule.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// The public face of a capsule. `Capsule.create()` asks the provider for a
|
|
2
|
+
// machine and waits until capsulod answers; everything after that goes
|
|
3
|
+
// straight to the daemon.
|
|
4
|
+
import { DaemonClient } from './client.js';
|
|
5
|
+
import { Ports } from './ports.js';
|
|
6
|
+
import { PtySession } from './pty.js';
|
|
7
|
+
import { CapsuleError, } from './types.js';
|
|
8
|
+
export class Capsule {
|
|
9
|
+
id;
|
|
10
|
+
provider;
|
|
11
|
+
client;
|
|
12
|
+
commands;
|
|
13
|
+
files;
|
|
14
|
+
pty;
|
|
15
|
+
/** Servers listening inside the capsule, reachable through the daemon (preview URLs). */
|
|
16
|
+
ports;
|
|
17
|
+
constructor(id, provider, client) {
|
|
18
|
+
this.id = id;
|
|
19
|
+
this.provider = provider;
|
|
20
|
+
this.client = client;
|
|
21
|
+
this.commands = new Commands(client);
|
|
22
|
+
this.files = new Files(client);
|
|
23
|
+
this.pty = new Pty(client);
|
|
24
|
+
this.ports = new Ports(client.connection);
|
|
25
|
+
}
|
|
26
|
+
/** Throw a capsule: create it with the provider and wait until it is ready. */
|
|
27
|
+
static async create(provider, spec, opts = {}) {
|
|
28
|
+
const handle = await provider.create(spec, { timeoutMs: opts.createTimeoutMs, signal: opts.signal });
|
|
29
|
+
const client = new DaemonClient(handle.connection, opts);
|
|
30
|
+
const health = await waitReady(client, opts.readyTimeoutMs ?? 120_000, opts.signal);
|
|
31
|
+
// Providers deliver the mount table through their boot hook. When that did
|
|
32
|
+
// not happen (no hook, or the hook came without its payload), hand it over
|
|
33
|
+
// directly. Either way, a mount that failed is an error here, not a
|
|
34
|
+
// surprise later when a write lands in a plain directory.
|
|
35
|
+
let results = health.mounts ?? [];
|
|
36
|
+
const wanted = Object.keys(spec.mounts ?? {});
|
|
37
|
+
const applied = new Set(results.map((r) => r.path));
|
|
38
|
+
const hookMissed = !health.configured || wanted.some((p) => !applied.has(p));
|
|
39
|
+
if (hookMissed && (spec.mounts || spec.metadata)) {
|
|
40
|
+
results = await client.configure({ mounts: spec.mounts, metadata: spec.metadata });
|
|
41
|
+
}
|
|
42
|
+
const failed = results.filter((r) => !r.ok);
|
|
43
|
+
if (failed.length) {
|
|
44
|
+
throw new CapsuleError(`mounts failed: ${failed.map((r) => `${r.path} (${r.driver}): ${r.error}`).join('; ')}`);
|
|
45
|
+
}
|
|
46
|
+
return new Capsule(handle.id, provider, client);
|
|
47
|
+
}
|
|
48
|
+
/** Reconnect to a capsule that already exists (it may be suspended: the first request resumes it). */
|
|
49
|
+
static async connect(provider, id, opts = {}) {
|
|
50
|
+
const conn = await provider.connect(id);
|
|
51
|
+
return new Capsule(id, provider, new DaemonClient(conn, opts));
|
|
52
|
+
}
|
|
53
|
+
health() {
|
|
54
|
+
return this.client.health();
|
|
55
|
+
}
|
|
56
|
+
/** Put the capsule back in your pocket. */
|
|
57
|
+
async kill() {
|
|
58
|
+
await this.provider.terminate(this.id);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class Commands {
|
|
62
|
+
client;
|
|
63
|
+
constructor(client) {
|
|
64
|
+
this.client = client;
|
|
65
|
+
}
|
|
66
|
+
/** Run a command (through `sh -c` unless `args` is given). Pass onStdout/onStderr for live output. */
|
|
67
|
+
run(cmd, opts) {
|
|
68
|
+
return this.client.exec(cmd, opts);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export class Pty {
|
|
72
|
+
client;
|
|
73
|
+
constructor(client) {
|
|
74
|
+
this.client = client;
|
|
75
|
+
}
|
|
76
|
+
/** Open an interactive terminal (a shell by default). */
|
|
77
|
+
open(opts) {
|
|
78
|
+
return PtySession.open(this.client.connection, opts);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export class Files {
|
|
82
|
+
client;
|
|
83
|
+
constructor(client) {
|
|
84
|
+
this.client = client;
|
|
85
|
+
}
|
|
86
|
+
read(path) {
|
|
87
|
+
return this.client.readText(path);
|
|
88
|
+
}
|
|
89
|
+
readBytes(path) {
|
|
90
|
+
return this.client.readFile(path);
|
|
91
|
+
}
|
|
92
|
+
write(path, data, opts) {
|
|
93
|
+
return this.client.writeFile(path, data, opts);
|
|
94
|
+
}
|
|
95
|
+
list(path) {
|
|
96
|
+
return this.client.list(path);
|
|
97
|
+
}
|
|
98
|
+
stat(path) {
|
|
99
|
+
return this.client.stat(path);
|
|
100
|
+
}
|
|
101
|
+
mkdir(path) {
|
|
102
|
+
return this.client.mkdir(path);
|
|
103
|
+
}
|
|
104
|
+
remove(path) {
|
|
105
|
+
return this.client.remove(path);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function waitReady(client, timeoutMs, signal) {
|
|
109
|
+
const t0 = Date.now();
|
|
110
|
+
let lastErr;
|
|
111
|
+
while (Date.now() - t0 < timeoutMs) {
|
|
112
|
+
if (signal?.aborted)
|
|
113
|
+
throw new CapsuleError('wait for the capsule daemon aborted');
|
|
114
|
+
try {
|
|
115
|
+
const h = await client.health();
|
|
116
|
+
if (h.ok)
|
|
117
|
+
return h;
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
lastErr = err;
|
|
121
|
+
}
|
|
122
|
+
await new Promise((r) => setTimeout(r, 400));
|
|
123
|
+
}
|
|
124
|
+
throw new CapsuleError(`capsule daemon did not become ready in ${timeoutMs}ms (${String(lastErr)})`);
|
|
125
|
+
}
|
|
126
|
+
//# sourceM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,GAAG,GAAG,CAAC;QAChB,CAAC;QACD,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/C,CAAC;IACD,MAAM,IAAI,YAAY,CAAC,0CAA0C,SAAS,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AACvG,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Connection, type ExecEvent, type ExecOptions, type ExecResult, type FileEntry, type Health, type MountResult, type MountSpec } from './types.js';
|
|
2
|
+
export interface ClientOptions {
|
|
3
|
+
fetch?: typeof fetch;
|
|
4
|
+
requestTimeoutMs?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class DaemonClient {
|
|
7
|
+
readonly connection: Connection;
|
|
8
|
+
private readonly fetchImpl;
|
|
9
|
+
private readonly requestTimeoutMs;
|
|
10
|
+
constructor(connection: Connection, opts?: ClientOptions);
|
|
11
|
+
private request;
|
|
12
|
+
health(): Promise<Health>;
|
|
13
|
+
/** Apply the mount table when the provider's boot hook did not deliver it. */
|
|
14
|
+
configure(body: {
|
|
15
|
+
mounts?: Record<string, MountSpec>;
|
|
16
|
+
metadata?: Record<string, string>;
|
|
17
|
+
}): Promise<MountResult[]>;
|
|
18
|
+
/** Buffered or streamed depending on whether callbacks are given. */
|
|
19
|
+
exec(cmd: string, opts?: ExecOptions): Promise<ExecResult>;
|
|
20
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
21
|
+
readText(path: string): Promise<string>;
|
|
22
|
+
writeFile(path: string, data: string | Uint8Array, opts?: {
|
|
23
|
+
mode?: string;
|
|
24
|
+
}): Promise<void>;
|
|
25
|
+
list(path: string): Promise<FileEntry[]>;
|
|
26
|
+
stat(path: string): Promise<FileEntry>;
|
|
27
|
+
mkdir(path: string): Promise<void>;
|
|
28
|
+
remove(path: string): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/** Minimal SSE parser over a fetch Response body. */
|
|
31
|
+
export declare function parseSse(res: Response): AsyncGenerator<ExecEvent>;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Thin HTTP client for capsulod. Every request has a timeout: on a capsule
|
|
2
|
+
// with egress blocked, a dead connection shows up as a hang, never as an error.
|
|
3
|
+
import { CapsuleError, } from './types.js';
|
|
4
|
+
export class DaemonClient {
|
|
5
|
+
connection;
|
|
6
|
+
fetchImpl;
|
|
7
|
+
requestTimeoutMs;
|
|
8
|
+
constructor(connection, opts = {}) {
|
|
9
|
+
this.connection = connection;
|
|
10
|
+
this.fetchImpl = opts.fetch ?? ((...args) => fetch(...args));
|
|
11
|
+
this.requestTimeoutMs = opts.requestTimeoutMs ?? 30_000;
|
|
12
|
+
}
|
|
13
|
+
async request(path, init = {}, fresh = false) {
|
|
14
|
+
const headers = { ...(await this.connection.headers({ fresh })), ...init.headers };
|
|
15
|
+
const res = await this.fetchImpl(`${this.connection.baseUrl}${path}`, {
|
|
16
|
+
...init,
|
|
17
|
+
headers,
|
|
18
|
+
signal: AbortSignal.timeout(init.timeoutMs ?? this.requestTimeoutMs),
|
|
19
|
+
});
|
|
20
|
+
// A rejected credential (expired or revoked endpoint token) gets one retry with a fresh one.
|
|
21
|
+
if ((res.status === 401 || res.status === 403) && !fresh) {
|
|
22
|
+
return this.request(path, init, true);
|
|
23
|
+
}
|
|
24
|
+
if (!res.ok) {
|
|
25
|
+
let detail = '';
|
|
26
|
+
try {
|
|
27
|
+
detail = (await res.json()).error ?? '';
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
/* non-JSON error body */
|
|
31
|
+
}
|
|
32
|
+
throw new CapsuleError(`${init.method ?? 'GET'} ${path} → HTTP ${res.status}${detail ? `: ${detail}` : ''}`, res.status);
|
|
33
|
+
}
|
|
34
|
+
return res;
|
|
35
|
+
}
|
|
36
|
+
async health() {
|
|
37
|
+
const res = await this.request('/health', { timeoutMs: 10_000 });
|
|
38
|
+
return (await res.json());
|
|
39
|
+
}
|
|
40
|
+
/** Apply the mount table when the provider's boot hook did not deliver it. */
|
|
41
|
+
async configure(body) {
|
|
42
|
+
const res = await this.request('/configure', {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
body: JSON.stringify(body),
|
|
45
|
+
headers: { 'Content-Type': 'application/json' },
|
|
46
|
+
timeoutMs: 60_000,
|
|
47
|
+
});
|
|
48
|
+
return (await res.json()).mounts ?? [];
|
|
49
|
+
}
|
|
50
|
+
/** Buffered or streamed depending on whether callbacks are given. */
|
|
51
|
+
async exec(cmd, opts = {}) {
|
|
52
|
+
const body = JSON.stringify({
|
|
53
|
+
cmd,
|
|
54
|
+
args: opts.args,
|
|
55
|
+
cwd: opts.cwd,
|
|
56
|
+
env: opts.env,
|
|
57
|
+
stdin: opts.stdin,
|
|
58
|
+
timeoutMs: opts.timeoutMs,
|
|
59
|
+
});
|
|
60
|
+
// Request timeout = command timeout + slack; the daemon enforces the real one.
|
|
61
|
+
const timeoutMs = (opts.timeoutMs ?? 60_000) + 15_000;
|
|
62
|
+
const streaming = Boolean(opts.onStdout || opts.onStderr);
|
|
63
|
+
if (!streaming) {
|
|
64
|
+
const res = await this.request('/exec', { method: 'POST', body, headers: { 'Content-Type': 'application/json' }, timeoutMs });
|
|
65
|
+
return (await res.json());
|
|
66
|
+
}
|
|
67
|
+
const startedAt = Date.now();
|
|
68
|
+
const res = await this.request('/exec', {
|
|
69
|
+
method: 'POST',
|
|
70
|
+
body,
|
|
71
|
+
headers: { 'Content-Type': 'application/json', Accept: 'text/event-stream' },
|
|
72
|
+
timeoutMs,
|
|
73
|
+
});
|
|
74
|
+
const result = { exitCode: -1, stdout: '', stderr: '', timeout: false, durationMs: 0 };
|
|
75
|
+
for await (const ev of parseSse(res)) {
|
|
76
|
+
if (ev.type === 'stdout') {
|
|
77
|
+
result.stdout += ev.data;
|
|
78
|
+
opts.onStdout?.(ev.data);
|
|
79
|
+
}
|
|
80
|
+
else if (ev.type === 'stderr') {
|
|
81
|
+
result.stderr += ev.data;
|
|
82
|
+
opts.onStderr?.(ev.data);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
result.exitCode = ev.exitCode ?? 0;
|
|
86
|
+
result.timeout = ev.timeout ?? false;
|
|
87
|
+
if (ev.error)
|
|
88
|
+
result.error = ev.error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
result.durationMs = Date.now() - startedAt;
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
async readFile(path) {
|
|
95
|
+
const res = await this.request(`/files/read?path=${encodeURIComponent(path)}`);
|
|
96
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
97
|
+
}
|
|
98
|
+
async readText(path) {
|
|
99
|
+
return new TextDecoder().decode(await this.readFile(path));
|
|
100
|
+
}
|
|
101
|
+
async writeFile(path, data, opts = {}) {
|
|
102
|
+
const q = new URLSearchParams({ path, ...(opts.mode ? { mode: opts.mode } : {}) });
|
|
103
|
+
await this.request(`/files/write?${q}`, {
|
|
104
|
+
method: 'PUT',
|
|
105
|
+
body: typeof data === 'string' ? data : data,
|
|
106
|
+
headers: { 'Content-Type': 'application/octet-stream' },
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
async list(path) {
|
|
110
|
+
const res = await this.request(`/files/list?path=${encodeURIComponent(path)}`);
|
|
111
|
+
return (await res.json());
|
|
112
|
+
}
|
|
113
|
+
async stat(path) {
|
|
114
|
+
const res = await this.request(`/files/stat?path=${encodeURIComponent(path)}`);
|
|
115
|
+
return (await res.json());
|
|
116
|
+
}
|
|
117
|
+
async mkdir(path) {
|
|
118
|
+
await this.request(`/files/mkdir?path=${encodeURIComponent(path)}`, { method: 'POST' });
|
|
119
|
+
}
|
|
120
|
+
async remove(path) {
|
|
121
|
+
await this.request(`/files?path=${encodeURIComponent(path)}`, { method: 'DELETE' });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Minimal SSE parser over a fetch Response body. */
|
|
125
|
+
export async function* parseSse(res) {
|
|
126
|
+
const reader = res.body?.getReader();
|
|
127
|
+
if (!reader)
|
|
128
|
+
throw new CapsuleError('exec stream has no body');
|
|
129
|
+
const decoder = new TextDecoder();
|
|
130
|
+
let buffer = '';
|
|
131
|
+
for (;;) {
|
|
132
|
+
const { value, done } = await reader.read();
|
|
133
|
+
if (done)
|
|
134
|
+
break;
|
|
135
|
+
buffer += decoder.decode(value, { stream: true });
|
|
136
|
+
let idx;
|
|
137
|
+
while ((idx = buffer.indexOf('\n\n')) !== -1) {
|
|
138
|
+
const frame = buffer.slice(0, idx);
|
|
139
|
+
buffer = buffer.slice(idx + 2);
|
|
140
|
+
const dataLine = frame.split('\n').find((l) => l.startsWith('data: '));
|
|
141
|
+
if (dataLine)
|
|
142
|
+
yield JSON.parse(dataLine.slice(6));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,gFAAgF;AAChF,OAAO,EACL,YAAY,GASb,MAAM,YAAY,CAAC;AAOpB,MAAM,OAAO,YAAY;IAKZ;IAJM,SAAS,CAAe;IACxB,gBAAgB,CAAS;IAE1C,YACW,UAAsB,EAC/B,OAAsB,EAAE;QADf,eAAU,GAAV,UAAU,CAAY;QAG/B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;QAC7D,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,IAAI,MAAM,CAAC;IAC1D,CAAC;IAEO,KAAK,CAAC,OAAO,CAAC,IAAY,EAAE,OAA6C,EAAE,EAAE,KAAK,GAAG,KAAK;QAChG,MAAM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K,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;YACnC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;YAC/B,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC;YACvE,IAAI,QAAQ;gBAAE,MAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAc,CAAC;QACjE,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Capsule, Commands, Files, Pty } from './capsule.js';
|
|
2
|
+
export { Ports } from './ports.js';
|
|
3
|
+
export { PtySession } from './pty.js';
|
|
4
|
+
export type { CapsuleOptions } from './capsule.js';
|
|
5
|
+
export { DaemonClient, parseSse } from './client.js';
|
|
6
|
+
export type { ClientOptions } from './client.js';
|
|
7
|
+
export * from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAErD,cAAc,YAAY,CAAC"}
|
package/dist/ports.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Connection } from './types.js';
|
|
2
|
+
export declare class Ports {
|
|
3
|
+
private readonly conn;
|
|
4
|
+
constructor(conn: Connection);
|
|
5
|
+
/** URL of `path` on the given port, as seen through the daemon (headers still apply, see `connection`). */
|
|
6
|
+
url(port: number, path?: string): string;
|
|
7
|
+
/** A Connection whose baseUrl is the proxied port: build clients, WebSockets, fetches on it. */
|
|
8
|
+
connection(port: number): Connection;
|
|
9
|
+
/** `fetch()` against the server on `port` inside the capsule, with the capsule's auth headers attached. */
|
|
10
|
+
fetch(port: number, path?: string, init?: RequestInit): Promise<Response>;
|
|
11
|
+
}
|
package/dist/ports.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class Ports {
|
|
2
|
+
conn;
|
|
3
|
+
constructor(conn) {
|
|
4
|
+
this.conn = conn;
|
|
5
|
+
}
|
|
6
|
+
/** URL of `path` on the given port, as seen through the daemon (headers still apply, see `connection`). */
|
|
7
|
+
url(port, path = '/') {
|
|
8
|
+
checkPort(port);
|
|
9
|
+
return `${this.conn.baseUrl}/proxy/${port}${path.startsWith('/') ? path : `/${path}`}`;
|
|
10
|
+
}
|
|
11
|
+
/** A Connection whose baseUrl is the proxied port: build clients, WebSockets, fetches on it. */
|
|
12
|
+
connection(port) {
|
|
13
|
+
checkPort(port);
|
|
14
|
+
return { baseUrl: `${this.conn.baseUrl}/proxy/${port}`, headers: (opts) => this.conn.headers(opts) };
|
|
15
|
+
}
|
|
16
|
+
/** `fetch()` against the server on `port` inside the capsule, with the capsule's auth headers attached. */
|
|
17
|
+
async fetch(port, path = '/', init = {}) {
|
|
18
|
+
const headers = { ...(await this.conn.headers()), ...init.headers };
|
|
19
|
+
return fetch(this.url(port, path), { signal: AbortSignal.timeout(60_000), ...init, headers });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function checkPort(port) {
|
|
23
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
24
|
+
throw new RangeError(`bad port: ${port}`);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=ports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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