@molecule/api-code-sandbox-flyio 1.0.10 → 1.0.11
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.d.ts +4 -2
- package/dist/exec.d.ts.map +1 -1
- package/dist/exec.js +50 -24
- package/package.json +1 -1
package/dist/exec.d.ts
CHANGED
|
@@ -83,13 +83,15 @@ export declare function buildScript(command: string, opts: ExecOptions | undefin
|
|
|
83
83
|
* Executes a command on a Machine, choosing the direct or detached strategy
|
|
84
84
|
* based on the caller's time budget.
|
|
85
85
|
*
|
|
86
|
+
* A command too large to pass inline (over {@link FLY_EXEC_MAX_ARG_BYTES}) is
|
|
87
|
+
* spilled to a file via chunked writes and run from there, so there is no size
|
|
88
|
+
* ceiling the caller must respect.
|
|
86
89
|
* @param rawExec - Transport callback issuing one Fly exec call.
|
|
87
90
|
* @param command - The shell command to run.
|
|
88
91
|
* @param opts - Core exec options (cwd, env, timeout in ms).
|
|
89
92
|
* @param defaultCwd - Working directory used when `opts.cwd` is unset.
|
|
90
93
|
* @returns The command's stdout, stderr and exit code.
|
|
91
|
-
* @throws {Error} When
|
|
92
|
-
* {@link MAX_SCRIPT_BASE64_BYTES}), or when the transport itself fails.
|
|
94
|
+
* @throws {Error} When staging a large command or the transport itself fails.
|
|
93
95
|
*/
|
|
94
96
|
export declare function execCommand(rawExec: RawExec, command: string, opts: ExecOptions | undefined, defaultCwd: string): Promise<ExecResult>;
|
|
95
97
|
//# sourceMappingURL=exec.d.ts.map
|
package/dist/exec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exec.d.ts","sourceRoot":"","sources":["../src/exec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAGzE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;A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,EAAE,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;AAE7F;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,eAAe,GAAG,UAAU,CAiBlE;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,UAAU,EAAE,MAAM,GACjB,MAAM,CAGR;AAmDD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,UAAU,CAAC,CAqBrB"}
|
package/dist/exec.js
CHANGED
|
@@ -50,10 +50,10 @@ const DEFAULT_TIMEOUT_MS = 600_000;
|
|
|
50
50
|
*/
|
|
51
51
|
export const MAX_CAPTURED_OUTPUT_BYTES = 5 * 1024 * 1024;
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
53
|
+
* Base64 chunk size for spilling a large script to a file, comfortably under
|
|
54
|
+
* {@link FLY_EXEC_MAX_ARG_BYTES} once the `printf … | base64 -d` wrapper is added.
|
|
55
55
|
*/
|
|
56
|
-
const
|
|
56
|
+
const SCRIPT_SPILL_CHUNK_BASE64 = 12_000;
|
|
57
57
|
/**
|
|
58
58
|
* Hard cap on the `sh -c <script>` argument SENT to Fly's exec endpoint. This is
|
|
59
59
|
* NOT the OS `MAX_ARG_STRLEN` (128 KB) — the Fly Machines exec API rejects a
|
|
@@ -128,31 +128,62 @@ function newRunId() {
|
|
|
128
128
|
id += Math.floor(Math.random() * 0xffffffff).toString(16);
|
|
129
129
|
return id.slice(0, 16);
|
|
130
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Writes `script` to `path` on the Machine via chunked base64 execs, each under
|
|
133
|
+
* {@link FLY_EXEC_MAX_ARG_BYTES}.
|
|
134
|
+
*
|
|
135
|
+
* Fly's exec API silently drops a single over-limit `printf %s <base64>`, so a
|
|
136
|
+
* script too large to pass inline (a big `node -e`, a bulk file write) must be
|
|
137
|
+
* delivered in pieces. This is the transport-level equivalent of `docker cp` for
|
|
138
|
+
* a command body — callers never see the limit.
|
|
139
|
+
* @param rawExec - Transport callback issuing one Fly exec call.
|
|
140
|
+
* @param path - Absolute destination path on the Machine.
|
|
141
|
+
* @param script - The script body to stage.
|
|
142
|
+
* @throws {Error} When the work dir or any chunk write fails.
|
|
143
|
+
*/
|
|
144
|
+
async function writeScriptChunked(rawExec, path, script) {
|
|
145
|
+
const base64 = Buffer.from(script, 'utf8').toString('base64');
|
|
146
|
+
const ensure = toExecResult(await rawExec(['sh', '-c', `mkdir -p ${WORK_DIR}`], CONTROL_EXEC_TIMEOUT_SECONDS));
|
|
147
|
+
if (ensure.exitCode !== 0) {
|
|
148
|
+
throw new Error(`Failed to create the Fly exec work dir: ${ensure.stderr || ensure.stdout}`);
|
|
149
|
+
}
|
|
150
|
+
for (let i = 0; i < base64.length; i += SCRIPT_SPILL_CHUNK_BASE64) {
|
|
151
|
+
const op = i === 0 ? '>' : '>>';
|
|
152
|
+
const chunk = base64.slice(i, i + SCRIPT_SPILL_CHUNK_BASE64);
|
|
153
|
+
const written = toExecResult(await rawExec(['sh', '-c', `printf %s ${shellQuote(chunk)} | base64 -d ${op} ${path}`], CONTROL_EXEC_TIMEOUT_SECONDS));
|
|
154
|
+
if (written.exitCode !== 0) {
|
|
155
|
+
throw new Error(`Failed to stage a Fly exec script chunk: ${written.stderr || written.stdout}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
131
159
|
/**
|
|
132
160
|
* Executes a command on a Machine, choosing the direct or detached strategy
|
|
133
161
|
* based on the caller's time budget.
|
|
134
162
|
*
|
|
163
|
+
* A command too large to pass inline (over {@link FLY_EXEC_MAX_ARG_BYTES}) is
|
|
164
|
+
* spilled to a file via chunked writes and run from there, so there is no size
|
|
165
|
+
* ceiling the caller must respect.
|
|
135
166
|
* @param rawExec - Transport callback issuing one Fly exec call.
|
|
136
167
|
* @param command - The shell command to run.
|
|
137
168
|
* @param opts - Core exec options (cwd, env, timeout in ms).
|
|
138
169
|
* @param defaultCwd - Working directory used when `opts.cwd` is unset.
|
|
139
170
|
* @returns The command's stdout, stderr and exit code.
|
|
140
|
-
* @throws {Error} When
|
|
141
|
-
* {@link MAX_SCRIPT_BASE64_BYTES}), or when the transport itself fails.
|
|
171
|
+
* @throws {Error} When staging a large command or the transport itself fails.
|
|
142
172
|
*/
|
|
143
173
|
export async function execCommand(rawExec, command, opts, defaultCwd) {
|
|
144
174
|
const budgetMs = opts?.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
145
175
|
const script = buildScript(command, opts, defaultCwd);
|
|
146
|
-
// A direct command travels as the `sh -c <script>` argument. Fly rejects one
|
|
147
|
-
// over FLY_EXEC_MAX_ARG_BYTES with a silent `exit_code: 0` (see the constant),
|
|
148
|
-
// so refuse it here with a message that names the real limit rather than
|
|
149
|
-
// letting the caller's data vanish. Bulk callers chunk beneath this.
|
|
150
|
-
if (script.length > FLY_EXEC_MAX_ARG_BYTES) {
|
|
151
|
-
throw new Error(`Fly exec command is ${script.length} bytes, over the ${FLY_EXEC_MAX_ARG_BYTES}-byte ` +
|
|
152
|
-
`limit the API silently rejects — chunk the payload smaller.`);
|
|
153
|
-
}
|
|
154
176
|
if (budgetMs <= DIRECT_EXEC_BUDGET_SECONDS * 1000) {
|
|
155
177
|
const seconds = Math.max(1, Math.min(Math.ceil(budgetMs / 1000), FLY_EXEC_MAX_TIMEOUT_SECONDS));
|
|
178
|
+
// Fly's exec API silently drops a command over FLY_EXEC_MAX_ARG_BYTES. A
|
|
179
|
+
// script that big cannot go inline, so spill it to a file (chunked writes,
|
|
180
|
+
// each under the limit) and run that — matching docker exec, which has no
|
|
181
|
+
// such ceiling. The caller stays unaware of the transport's limit.
|
|
182
|
+
if (script.length > FLY_EXEC_MAX_ARG_BYTES) {
|
|
183
|
+
const path = `${WORK_DIR}/direct-${newRunId()}.sh`;
|
|
184
|
+
await writeScriptChunked(rawExec, path, script);
|
|
185
|
+
return toExecResult(await rawExec(['sh', '-c', `sh ${path}; ec=$?; rm -f ${path}; exit $ec`], seconds));
|
|
186
|
+
}
|
|
156
187
|
return toExecResult(await rawExec(['sh', '-c', script], seconds));
|
|
157
188
|
}
|
|
158
189
|
return await execDetached(rawExec, script, budgetMs);
|
|
@@ -172,23 +203,18 @@ export async function execCommand(rawExec, command, opts, defaultCwd) {
|
|
|
172
203
|
*/
|
|
173
204
|
async function execDetached(rawExec, script, budgetMs) {
|
|
174
205
|
const runId = newRunId();
|
|
175
|
-
const base64 = Buffer.from(script, 'utf8').toString('base64');
|
|
176
|
-
if (base64.length > MAX_SCRIPT_BASE64_BYTES) {
|
|
177
|
-
throw new Error(t('codeSandbox.flyio.error.commandTooLarge', { limit: String(MAX_SCRIPT_BASE64_BYTES) }, {
|
|
178
|
-
defaultValue: `Command is too large to launch on a Fly Machine (encoded ${base64.length} bytes, limit ${MAX_SCRIPT_BASE64_BYTES}). ` +
|
|
179
|
-
'Write it to a file with writeFile() and exec that file instead.',
|
|
180
|
-
}));
|
|
181
|
-
}
|
|
182
206
|
const scriptPath = `${WORK_DIR}/${runId}.sh`;
|
|
183
207
|
const outPath = `${WORK_DIR}/${runId}.out`;
|
|
184
208
|
const errPath = `${WORK_DIR}/${runId}.err`;
|
|
185
209
|
const rcPath = `${WORK_DIR}/${runId}.rc`;
|
|
210
|
+
// Stage the script via chunked writes (each under the Fly exec arg limit),
|
|
211
|
+
// then launch it. A single `printf %s <base64>` would exceed the limit for a
|
|
212
|
+
// large script and Fly would drop it silently. writeScriptChunked also creates
|
|
213
|
+
// WORK_DIR, so the launcher below only has to background the run.
|
|
214
|
+
await writeScriptChunked(rawExec, scriptPath, script);
|
|
186
215
|
// The `&` must apply to the nohup line ALONE. In POSIX sh, `a && b &`
|
|
187
|
-
// backgrounds the whole AND-list,
|
|
188
|
-
// the launch, so these are separate statements rather than one chain.
|
|
216
|
+
// backgrounds the whole AND-list, so these are separate statements.
|
|
189
217
|
const launcher = [
|
|
190
|
-
`mkdir -p ${WORK_DIR} || exit 1`,
|
|
191
|
-
`printf %s ${shellQuote(base64)} | base64 -d > ${scriptPath} || exit 1`,
|
|
192
218
|
`nohup sh -c 'sh ${scriptPath} > ${outPath} 2> ${errPath}; printf %s "$?" > ${rcPath}' > /dev/null 2>&1 < /dev/null &`,
|
|
193
219
|
`printf %s ${runId}`,
|
|
194
220
|
].join('\n');
|