@sellable/install
Version:
One-command installer for Sellable MCP in Claude Code, Codex, and Hermes
123 lines (113 loc) • 4.08 kB
JavaScript
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import {
FlyExecStagingError,
isFlyExecStagingArguments,
runFlyExecStagingArguments,
} from "./fly-exec-staging.mjs";
import {
FlySkillsBridgeError,
SKILLS_REQUEST_MAX_BYTES,
executeExternalSkillsBridgeOperation,
} from "./fly-skills-bridge.mjs";
export const FLY_SKILLS_EXEC_ARGUMENT_MAX_BYTES = 96 * 1024;
export const FLY_SKILLS_EXEC_ARGUMENT_MAX_COUNT = 8;
const BASE64URL_CHUNK = /^[A-Za-z0-9_-]+$/;
const FLY_SKILLS_EXEC_ENCODED_MAX_BYTES =
Math.ceil(SKILLS_REQUEST_MAX_BYTES / 3) * 4;
export function decodeFlySkillsExecArguments(args) {
if (!Array.isArray(args) || args.length === 0) {
throw new FlySkillsBridgeError("skills_request_rejected");
}
if (args.length > FLY_SKILLS_EXEC_ARGUMENT_MAX_COUNT) {
throw new FlySkillsBridgeError("skills_request_too_large");
}
let encoded = "";
for (const chunk of args) {
if (typeof chunk !== "string" || !BASE64URL_CHUNK.test(chunk)) {
throw new FlySkillsBridgeError("skills_request_rejected");
}
if (Buffer.byteLength(chunk, "utf8") > FLY_SKILLS_EXEC_ARGUMENT_MAX_BYTES) {
throw new FlySkillsBridgeError("skills_request_too_large");
}
encoded += chunk;
if (encoded.length > FLY_SKILLS_EXEC_ENCODED_MAX_BYTES) {
throw new FlySkillsBridgeError("skills_request_too_large");
}
}
return decodeFlySkillsExecEncoded(encoded);
}
export function decodeFlySkillsExecEncoded(encoded) {
if (
typeof encoded !== "string" ||
encoded.length === 0 ||
!BASE64URL_CHUNK.test(encoded)
) {
throw new FlySkillsBridgeError("skills_request_rejected");
}
if (encoded.length > FLY_SKILLS_EXEC_ENCODED_MAX_BYTES) {
throw new FlySkillsBridgeError("skills_request_too_large");
}
let bytes;
try {
bytes = Buffer.from(encoded, "base64url");
} catch {
throw new FlySkillsBridgeError("skills_request_rejected");
}
if (bytes.toString("base64url") !== encoded) {
throw new FlySkillsBridgeError("skills_request_rejected");
}
if (bytes.byteLength > SKILLS_REQUEST_MAX_BYTES) {
throw new FlySkillsBridgeError("skills_request_too_large");
}
try {
const json = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
return JSON.parse(json);
} catch {
throw new FlySkillsBridgeError("skills_request_rejected");
}
}
/**
* Resolves the request a CLI call carries: inline as base64url arguments, or
* staged in pieces by earlier calls and named by a commit call. A stage call
* returns its receipt instead, and runs no Skills operation.
*
* @param {string[]} args
* @param {Omit<Parameters<typeof runFlyExecStagingArguments>[1], "channel" | "maxTotalBytes">} [stagingOptions]
* @returns {{ receipt: { ok: true; operation: "stage"; index: number } } | { request: any }}
*/
export function resolveFlySkillsExecArguments(args, stagingOptions) {
if (!isFlyExecStagingArguments(args)) {
return { request: decodeFlySkillsExecArguments(args) };
}
const staged = runFlyExecStagingArguments(args, {
...stagingOptions,
channel: "skills",
maxTotalBytes: FLY_SKILLS_EXEC_ENCODED_MAX_BYTES,
});
return staged.kind === "staged"
? { receipt: staged.receipt }
: { request: decodeFlySkillsExecEncoded(staged.encoded) };
}
export async function runFlySkillsBridgeExecCli() {
try {
const resolved = resolveFlySkillsExecArguments(process.argv.slice(2));
const result =
"receipt" in resolved
? resolved.receipt
: executeExternalSkillsBridgeOperation(resolved.request);
process.stdout.write(`${JSON.stringify(result)}\n`);
process.exitCode = result.ok ? 0 : 2;
} catch (error) {
process.stdout.write(
`${JSON.stringify({ ok: false, code: error instanceof FlySkillsBridgeError || error instanceof FlyExecStagingError ? error.code : "skills_bridge_failed" })}\n`
);
process.exitCode = 2;
}
}
if (
resolve(process.argv[1] ?? "") === resolve(fileURLToPath(import.meta.url))
) {
await runFlySkillsBridgeExecCli();
}