@impz/client 5.0.0 → 6.0.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/README.md +10 -172
- package/dist/index.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -71,179 +71,17 @@ const result = await imp.run('dev', ['sh', '-c', 'uname -a; cat'], { stdin: 'hi\
|
|
|
71
71
|
console.log(result.code, new TextDecoder().decode(result.stdout));
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
`imp.openExec` streams
|
|
75
|
-
session
|
|
76
|
-
a loop keeps to the network's pace. It sends a large write in frames of at most 1 MiB: impd closes a
|
|
77
|
-
socket that sends one frame over 16 MiB.
|
|
74
|
+
`imp.openExec` streams the output instead, `imp.openConsole` opens a login shell with a tty, and a
|
|
75
|
+
named session outlives the socket, so a client can attach to it again and resume where it stopped.
|
|
78
76
|
|
|
79
|
-
|
|
80
|
-
(`maxUnreadBytes`) the session ends with `OUTPUT_OVERFLOW`. Cancelling both streams ends the session
|
|
81
|
-
and stops the command; so do `close()` and the `signal` option's abort.
|
|
77
|
+
## Errors, features and versions
|
|
82
78
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
try {
|
|
89
|
-
for await (const chunk of tail.stdout) {
|
|
90
|
-
if (process.stdout.write(chunk) === false) {
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
} finally {
|
|
95
|
-
tail.close();
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
`imp.openConsole` opens a login shell with a tty, the shell of the image's user, as `impz console`
|
|
100
|
-
does. With [xterm.js](https://xtermjs.org):
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
const shell = await imp.openConsole('dev', { cols: term.cols, rows: term.rows });
|
|
104
|
-
|
|
105
|
-
term.onData((data) => {
|
|
106
|
-
shell.write(data).catch(() => {});
|
|
107
|
-
});
|
|
108
|
-
term.onResize(({ cols, rows }) => shell.resize(cols, rows));
|
|
109
|
-
|
|
110
|
-
const reader = shell.stdout.getReader();
|
|
111
|
-
|
|
112
|
-
for (let read = await reader.read(); !read.done; read = await reader.read()) {
|
|
113
|
-
term.write(read.value);
|
|
114
|
-
}
|
|
115
|
-
```
|
|
79
|
+
A failed call throws an `ORPCError` with impd's `code` and typed `data`; `safe` and `isDefinedError`
|
|
80
|
+
narrow it. A call to a procedure that an older impd lacks throws an `UnknownProcedureError` instead.
|
|
81
|
+
An older impd drops an input field it does not know, so `requireFeatures` checks the features a call
|
|
82
|
+
needs first. `imp.checkServer()` tells whether impd speaks this client's API.
|
|
116
83
|
|
|
117
|
-
|
|
118
|
-
and the shell outlives the handle: `close()` detaches.
|
|
119
|
-
`imp.openAttach('dev', 'main', { cols, rows })` attaches to a running session; its `stdout` starts
|
|
120
|
-
with a replay of the recent output. `sessions.list` names them without waking the imp. `started`
|
|
121
|
-
resolves with `{ pid, session, created, groupKill, output }`. When impd ends the socket while the
|
|
122
|
-
session runs on, `exit` rejects with `DETACHED`, and its `data.reason` says why: `taken_over`
|
|
123
|
-
(another client attached; one is attached at a time), `slow` (the client fell too far behind) or
|
|
124
|
-
`lost` (impd lost the guest, as when the imp slept; attach again).
|
|
125
|
-
|
|
126
|
-
### Resuming a session
|
|
127
|
-
|
|
128
|
-
`started.output` places the data in the session's output. With `continuity: 'offsets'` it has the
|
|
129
|
-
generation (`executionGeneration`, one run of the process), the `bootId`, `bufferStart` and `end`,
|
|
130
|
-
and `offset`, the offset of the first data byte after `prelude` mode bytes; `{ continuity: 'none' }`
|
|
131
|
-
means an imp whose agent predates offsets, or an older impd. `exit` resolves with `offset`, and
|
|
132
|
-
`DETACHED` has `data.offset`: the offset after the last byte received. Keep the generation and that
|
|
133
|
-
offset, and resume from them:
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
const tail = await imp.openAttach('dev', 'main', {
|
|
137
|
-
resumeFrom: { executionGeneration, offset },
|
|
138
|
-
wake: false,
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
const { output } = await tail.started;
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
`output.resume` says how the resume was met: `exact`; `gap`, whose bytes `[from, to)` are gone (a
|
|
145
|
-
terminal should attach again without `resumeFrom`, since the data can start inside an escape
|
|
146
|
-
sequence); or `generation_changed`, a new process, whose data starts at `firstOffset`. A resume can
|
|
147
|
-
repeat bytes you have: drop those below your high-water mark. `output.coldBoots` lists the imp's
|
|
148
|
-
last cold boots, newest first, with a `cause` (`start`, `wake_fallback`, `watchdog`, `restore`,
|
|
149
|
-
`recovery`, `unknown`); the first one after your `bootId` ended your generation. `wake: false`
|
|
150
|
-
rejects with `InvalidStateError` instead of booting or waking the imp.
|
|
151
|
-
|
|
152
|
-
A disconnected client cannot recover bytes below `bufferStart`, any byte of a generation that a cold
|
|
153
|
-
boot ended, or the output of an exec without `session`. Keep what you received if you need them.
|
|
154
|
-
`system.info()` has `features.sessionOffsets` on an impd that carries offsets.
|
|
155
|
-
|
|
156
|
-
With a tty, `sendSignal('SIGINT')` and `sendSignal('SIGQUIT')` send ^C and ^\ as keys, so they reach
|
|
157
|
-
the foreground job; other signals, and every signal without a tty, go to the process. A write after
|
|
158
|
-
the session ended rejects with the error that ended it, such as `CONNECTION_CLOSED` or `RESTARTING`,
|
|
159
|
-
or with `CLOSED` after an exit or a `close`. `closeStdin` after the session ended does nothing.
|
|
160
|
-
|
|
161
|
-
The socket authenticates with a single-use exec ticket from `exec.ticket`, so the token never goes
|
|
162
|
-
in a URL; a browser behind a proxy that adds the token works the same way.
|
|
163
|
-
|
|
164
|
-
`exit` resolves with `{ code, signal }`, where `code` is null when a signal ended the command. When
|
|
165
|
-
the command did not run to its exit, `exit` rejects with an `ExecError` whose `code` is impd's (as
|
|
166
|
-
in the table below, plus `EXEC_FAILED` when the command cannot start and `INNER_DOWN` when the imp's
|
|
167
|
-
container is down) or one of `UNAUTHORIZED` (also for an exec ticket that expired or was used),
|
|
168
|
-
`UNREACHABLE`, `RESTARTING`, `CONNECTION_CLOSED`, `DETACHED`, `BAD_MESSAGE`, `CLOSED`,
|
|
169
|
-
`OUTPUT_OVERFLOW` and `LOCAL_ERROR`. Its `data` is impd's error data. Three codes reject as their
|
|
170
|
-
own subclasses, with typed `data`: `NoSessionError` (`NO_SESSION`:
|
|
171
|
-
`{ bootId, coldBoots, previous? }`, or no data from an agent without offsets), `InvalidStateError`
|
|
172
|
-
(`INVALID_STATE`: `{ state, allowed, coldBoots? }`) and `InvalidResumeError` (`INVALID_RESUME`:
|
|
173
|
-
`{ end, bufferStart }`, a resume past the end). An abort before the command starts, during the
|
|
174
|
-
ticket call or the connect, rejects with the abort's reason instead, an `AbortError` by default;
|
|
175
|
-
after the start it ends the session as `CLOSED`.
|
|
176
|
-
|
|
177
|
-
## Errors
|
|
178
|
-
|
|
179
|
-
A failed call throws an `ORPCError`, or an `UnknownProcedureError` for a procedure that an older
|
|
180
|
-
impd lacks. impd's errors carry a `code` and typed `data`:
|
|
181
|
-
|
|
182
|
-
| Code | When | `data` |
|
|
183
|
-
| --------------------- | ---------------------------------------------------- | -------------------------------------- |
|
|
184
|
-
| `NOT_FOUND` | No imp, image, checkpoint or session has that name. | `{ kind, name }` |
|
|
185
|
-
| `CONFLICT` | The name is taken. | `{ kind, name }` |
|
|
186
|
-
| `INVALID_STATE` | The imp's state does not allow the call. | `{ state, allowed, coldBoots? }` |
|
|
187
|
-
| `RAM_BUDGET_EXCEEDED` | The host has no room, even after sleeping idle imps. | `{ budgetMib, usedMib, requestedMib }` |
|
|
188
|
-
| `SERVICE_UNAVAILABLE` | impd is stopping. | |
|
|
189
|
-
| `FORBIDDEN` | An exec ticket was used for another imp. | |
|
|
190
|
-
| `AGENT_OUTDATED` | The imp's agent has no sessions; stop and start it. | |
|
|
191
|
-
|
|
192
|
-
```ts
|
|
193
|
-
import { isDefinedError, safe } from '@impz/client';
|
|
84
|
+
## Documentation
|
|
194
85
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (isDefinedError(error) && error.code === 'RAM_BUDGET_EXCEEDED') {
|
|
198
|
-
console.log(`needs ${error.data.requestedMib} MiB`);
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
A 401 means the token is wrong.
|
|
203
|
-
|
|
204
|
-
## Features
|
|
205
|
-
|
|
206
|
-
An older impd drops an input field it does not know, and the call succeeds without it. Check its
|
|
207
|
-
features before you send a field that a later impd added:
|
|
208
|
-
|
|
209
|
-
| Field | Feature | Since |
|
|
210
|
-
| ------------------------------------------------- | ----------------- | ------ |
|
|
211
|
-
| `grantable` on `tokens.create` | `grantableTokens` | 0.27.0 |
|
|
212
|
-
| `rebind` on `secrets.add` (`replace`) | `secretRebind` | 0.27.0 |
|
|
213
|
-
| `require` on `openExec` | `execRequire` | 0.30.0 |
|
|
214
|
-
| `secretFiles`, `removeSecretFiles` on `system.gc` | `secretFilesGc` | 0.31.0 |
|
|
215
|
-
|
|
216
|
-
`requireFeatures` reads the flags once, from `system.features`, or from `system.info` on an impd
|
|
217
|
-
older than it. It throws a `MissingFeatureError` for the first feature missing, with what that impd
|
|
218
|
-
would do instead:
|
|
219
|
-
|
|
220
|
-
```ts
|
|
221
|
-
import { requireFeatures } from '@impz/client';
|
|
222
|
-
|
|
223
|
-
await requireFeatures(imp, { grantableTokens: 'make the token without a grantable list' });
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
Without the `secretRebind` feature, a `replace` that changes the hosts keeps every grant. With it,
|
|
227
|
-
it fails with `CONFLICT` and `data.reason: 'binding_changed'` unless `rebind` is set.
|
|
228
|
-
|
|
229
|
-
`openExec` and `openExecSession` check `execRequire` themselves when `require` is set. With
|
|
230
|
-
`require: ['broker']`, the command starts only once impd set the credential broker's variables and
|
|
231
|
-
CA bundle for this boot; otherwise the exec fails with `PRECONDITION_FAILED` and
|
|
232
|
-
`data: { reason: 'broker_not_ready', detail }`, and nothing runs. Without the feature it fails the
|
|
233
|
-
same way with `reason: 'impd_outdated'` before it sends the start. See
|
|
234
|
-
[requiring the broker](https://github.com/zgeoff/impz/blob/main/docs/guides/connectors.md#requiring-the-broker).
|
|
235
|
-
|
|
236
|
-
## Versions
|
|
237
|
-
|
|
238
|
-
The client and impd are released together with the same version. `imp.checkServer()` tells whether
|
|
239
|
-
impd speaks this client's API: the major version must match, and before 1.0 the minor version too.
|
|
240
|
-
Its optional `signal` ends the call, and the check rejects with the signal's reason. `check.info`
|
|
241
|
-
holds the `system.info` answer it read, features included.
|
|
242
|
-
|
|
243
|
-
```ts
|
|
244
|
-
const check = await imp.checkServer({ signal: AbortSignal.timeout(5000) });
|
|
245
|
-
|
|
246
|
-
if (!check.compatible) {
|
|
247
|
-
throw new Error(`impd ${check.serverVersion} does not match client ${check.clientVersion}`);
|
|
248
|
-
}
|
|
249
|
-
```
|
|
86
|
+
The [client guide](https://github.com/zgeoff/impz/blob/main/docs/guides/client.md) covers streaming,
|
|
87
|
+
consoles, sessions and resume, every error code, the features table and the version check.
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { P, match } from "ts-pattern";
|
|
|
5
5
|
import pRetry, { AbortError } from "p-retry";
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/check-server.ts
|
|
8
|
-
const CLIENT_VERSION = "
|
|
8
|
+
const CLIENT_VERSION = "6.0.1";
|
|
9
9
|
async function checkServer(rpc, options = {}) {
|
|
10
10
|
const signal = options.signal;
|
|
11
11
|
const callOptions = signal === void 0 ? {} : { signal };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@impz/client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Typed client for impd, the imp host daemon: imps, checkpoints, images and exec",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@orpc/client": "^1.15.
|
|
20
|
-
"@orpc/contract": "^1.15.
|
|
19
|
+
"@orpc/client": "^1.15.7",
|
|
20
|
+
"@orpc/contract": "^1.15.7",
|
|
21
21
|
"p-retry": "^8.0.1",
|
|
22
|
-
"ts-pattern": "^5.9.
|
|
22
|
+
"ts-pattern": "^5.9.2",
|
|
23
23
|
"zod": "^4.6.7"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|