@earendil-works/gondolin 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/exec.js DELETED
@@ -1,209 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const net_1 = __importDefault(require("net"));
7
- const virtio_protocol_1 = require("./virtio-protocol");
8
- function parseArgs(argv) {
9
- const args = { commands: [] };
10
- let current = null;
11
- let nextId = 1;
12
- const fail = (message) => {
13
- console.error(message);
14
- usage();
15
- process.exit(1);
16
- };
17
- const parseId = (value) => {
18
- const id = Number(value);
19
- if (!Number.isFinite(id))
20
- fail("--id must be a number");
21
- if (id >= nextId)
22
- nextId = id + 1;
23
- return id;
24
- };
25
- const separatorIndex = argv.indexOf("--");
26
- if (separatorIndex !== -1) {
27
- const optionArgs = argv.slice(0, separatorIndex);
28
- const commandArgs = argv.slice(separatorIndex + 1);
29
- if (commandArgs.length === 0)
30
- fail("missing command after --");
31
- current = {
32
- cmd: commandArgs[0],
33
- argv: commandArgs.slice(1),
34
- env: [],
35
- id: nextId++,
36
- };
37
- args.commands.push(current);
38
- for (let i = 0; i < optionArgs.length; i += 1) {
39
- const arg = optionArgs[i];
40
- switch (arg) {
41
- case "--sock":
42
- args.sock = optionArgs[++i];
43
- break;
44
- case "--env":
45
- current.env.push(optionArgs[++i]);
46
- break;
47
- case "--cwd":
48
- current.cwd = optionArgs[++i];
49
- break;
50
- case "--id":
51
- current.id = parseId(optionArgs[++i]);
52
- break;
53
- case "--help":
54
- case "-h":
55
- usage();
56
- process.exit(0);
57
- default:
58
- fail(`Unknown argument: ${arg}`);
59
- }
60
- }
61
- return args;
62
- }
63
- const requireCurrent = (flag) => {
64
- if (!current)
65
- fail(`${flag} requires --cmd`);
66
- return current;
67
- };
68
- for (let i = 0; i < argv.length; i += 1) {
69
- const arg = argv[i];
70
- switch (arg) {
71
- case "--sock":
72
- args.sock = argv[++i];
73
- break;
74
- case "--cmd":
75
- current = { cmd: argv[++i], argv: [], env: [], id: nextId++ };
76
- args.commands.push(current);
77
- break;
78
- case "--arg": {
79
- const command = requireCurrent("--arg");
80
- command.argv.push(argv[++i]);
81
- break;
82
- }
83
- case "--env": {
84
- const command = requireCurrent("--env");
85
- command.env.push(argv[++i]);
86
- break;
87
- }
88
- case "--cwd": {
89
- const command = requireCurrent("--cwd");
90
- command.cwd = argv[++i];
91
- break;
92
- }
93
- case "--id": {
94
- const command = requireCurrent("--id");
95
- command.id = parseId(argv[++i]);
96
- break;
97
- }
98
- case "--help":
99
- case "-h":
100
- usage();
101
- process.exit(0);
102
- default:
103
- fail(`Unknown argument: ${arg}`);
104
- }
105
- }
106
- return args;
107
- }
108
- function usage() {
109
- console.log("Usage:");
110
- console.log(" node dist/exec.js --sock PATH -- CMD [ARGS...]");
111
- console.log(" node dist/exec.js --sock PATH --cmd CMD [--arg ARG] [--env KEY=VALUE] [--cwd PATH] [--cmd CMD ...]");
112
- console.log("Use -- to pass a command and its arguments directly.");
113
- console.log("Arguments apply to the most recent --cmd.");
114
- }
115
- function buildCommandPayload(command) {
116
- const payload = {
117
- cmd: command.cmd,
118
- };
119
- if (command.argv.length > 0)
120
- payload.argv = command.argv;
121
- if (command.env.length > 0)
122
- payload.env = command.env;
123
- if (command.cwd)
124
- payload.cwd = command.cwd;
125
- return payload;
126
- }
127
- function main() {
128
- const args = parseArgs(process.argv.slice(2));
129
- if (!args.sock || args.commands.length === 0) {
130
- usage();
131
- process.exit(1);
132
- }
133
- const socket = net_1.default.createConnection({ path: args.sock });
134
- const reader = new virtio_protocol_1.FrameReader();
135
- let currentIndex = 0;
136
- let inflightId = null;
137
- let exitCode = 0;
138
- let closing = false;
139
- const sendNext = () => {
140
- const command = args.commands[currentIndex];
141
- inflightId = command.id;
142
- const payload = buildCommandPayload(command);
143
- const message = (0, virtio_protocol_1.buildExecRequest)(command.id, payload);
144
- socket.write((0, virtio_protocol_1.encodeFrame)(message));
145
- };
146
- const finish = (code) => {
147
- if (code !== undefined && exitCode === 0)
148
- exitCode = code;
149
- if (closing)
150
- return;
151
- closing = true;
152
- socket.end();
153
- };
154
- socket.on("connect", () => {
155
- console.log(`connected to ${args.sock}`);
156
- sendNext();
157
- });
158
- socket.on("data", (chunk) => {
159
- reader.push(chunk, (frame) => {
160
- const message = (0, virtio_protocol_1.decodeMessage)(frame);
161
- if (message.t === "exec_output") {
162
- const data = message.p.data;
163
- if (message.p.stream === "stdout") {
164
- process.stdout.write(data);
165
- }
166
- else {
167
- process.stderr.write(data);
168
- }
169
- }
170
- else if (message.t === "exec_response") {
171
- if (inflightId !== null && message.id !== inflightId) {
172
- console.error(`unexpected response id ${message.id} (expected ${inflightId})`);
173
- finish(1);
174
- return;
175
- }
176
- const code = message.p.exit_code ?? 1;
177
- const signal = message.p.signal;
178
- if (signal !== undefined) {
179
- console.error(`process exited due to signal ${signal}`);
180
- }
181
- if (code !== 0 && exitCode === 0)
182
- exitCode = code;
183
- currentIndex += 1;
184
- if (currentIndex < args.commands.length) {
185
- sendNext();
186
- }
187
- else {
188
- finish();
189
- }
190
- }
191
- else if (message.t === "error") {
192
- console.error(`error ${message.p.code}: ${message.p.message}`);
193
- finish(1);
194
- }
195
- });
196
- });
197
- socket.on("error", (err) => {
198
- console.error(`socket error: ${err.message}`);
199
- finish(1);
200
- });
201
- socket.on("end", () => {
202
- if (!closing && exitCode === 0)
203
- exitCode = 1;
204
- });
205
- socket.on("close", () => {
206
- process.exit(exitCode);
207
- });
208
- }
209
- main();