mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-25 12:54:09 +02:00
Retry Prism on occupied ports
This commit is contained in:
@@ -55,22 +55,41 @@ function listIds(output) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function startPrism(spec, port) {
|
async function startPrism(spec, port) {
|
||||||
const prism = spawn(
|
for (let attempt = 0; attempt < 20; attempt++, port++) {
|
||||||
"npx",
|
const prism = spawn(
|
||||||
["-y", "@stoplight/prism-cli", "mock", "--errors", "-p", String(port), "-h", "127.0.0.1", spec],
|
"npx",
|
||||||
{ stdio: ["ignore", "pipe", "pipe"] },
|
[
|
||||||
);
|
"-y",
|
||||||
let log = "";
|
"@stoplight/prism-cli",
|
||||||
prism.stdout.on("data", (d) => (log += d));
|
"mock",
|
||||||
prism.stderr.on("data", (d) => (log += d));
|
"--errors",
|
||||||
const deadline = Date.now() + 30_000;
|
"-p",
|
||||||
while (Date.now() < deadline) {
|
String(port),
|
||||||
if (log.includes("Prism is listening")) return { prism, getLog: () => log };
|
"-h",
|
||||||
if (prism.exitCode != null) throw new Error(`Prism exited:\n${log}`);
|
"127.0.0.1",
|
||||||
await new Promise((r) => setTimeout(r, 200));
|
spec,
|
||||||
|
],
|
||||||
|
{ stdio: ["ignore", "pipe", "pipe"] },
|
||||||
|
);
|
||||||
|
let log = "";
|
||||||
|
prism.stdout.on("data", (d) => (log += d));
|
||||||
|
prism.stderr.on("data", (d) => (log += d));
|
||||||
|
const deadline = Date.now() + 30_000;
|
||||||
|
let failed = false;
|
||||||
|
while (Date.now() < deadline) {
|
||||||
|
if (log.includes("Prism is listening")) return { prism, port, getLog: () => log };
|
||||||
|
if (log.includes("EADDRINUSE") || prism.exitCode != null) {
|
||||||
|
failed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
await new Promise((r) => setTimeout(r, 200));
|
||||||
|
}
|
||||||
|
prism.kill();
|
||||||
|
if (failed && log.includes("EADDRINUSE")) continue;
|
||||||
|
if (!failed) throw new Error(`Prism did not start in time:\n${log}`);
|
||||||
|
throw new Error(`Prism exited:\n${log}`);
|
||||||
}
|
}
|
||||||
prism.kill();
|
throw new Error("No free port found for Prism");
|
||||||
throw new Error(`Prism did not start in time:\n${log}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pointVariablesAtPrism(variables, prismUrl) {
|
function pointVariablesAtPrism(variables, prismUrl) {
|
||||||
@@ -166,49 +185,49 @@ for (const spec of specs) {
|
|||||||
JSON.stringify({ id: workspaceId, settingFollowRedirects: false }),
|
JSON.stringify({ id: workspaceId, settingFollowRedirects: false }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const prismUrl = `http://127.0.0.1:${port}`;
|
const { prism, port: boundPort, getLog } = await startPrism(spec, port);
|
||||||
const environmentIds = listIds(yaak(dataDir, ["environment", "list", workspaceId]));
|
port = boundPort + 1;
|
||||||
let activeEnvironment = null;
|
try {
|
||||||
for (const id of environmentIds) {
|
const prismUrl = `http://127.0.0.1:${boundPort}`;
|
||||||
const environment = yaakJson(dataDir, ["environment", "show", id]);
|
const environmentIds = listIds(yaak(dataDir, ["environment", "list", workspaceId]));
|
||||||
yaak(dataDir, [
|
let activeEnvironment = null;
|
||||||
"environment",
|
for (const id of environmentIds) {
|
||||||
"update",
|
const environment = yaakJson(dataDir, ["environment", "show", id]);
|
||||||
"--json",
|
|
||||||
JSON.stringify({
|
|
||||||
id,
|
|
||||||
variables: pointVariablesAtPrism(environment.variables ?? [], prismUrl),
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
if (environment.parentModel === "environment" && activeEnvironment == null) {
|
|
||||||
activeEnvironment = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestIds = listIds(yaak(dataDir, ["request", "list", workspaceId]));
|
|
||||||
const requests = new Map();
|
|
||||||
for (const id of requestIds) {
|
|
||||||
const request = yaakJson(dataDir, ["request", "show", id]);
|
|
||||||
requests.set(id, request);
|
|
||||||
// OAuth2 would try to fetch a real token; Prism only checks that the
|
|
||||||
// Authorization header is present, so a dummy bearer keeps it satisfied.
|
|
||||||
if (request.authenticationType === "oauth2") {
|
|
||||||
yaak(dataDir, [
|
yaak(dataDir, [
|
||||||
"request",
|
"environment",
|
||||||
"update",
|
"update",
|
||||||
"--json",
|
"--json",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
id,
|
id,
|
||||||
authenticationType: "bearer",
|
variables: pointVariablesAtPrism(environment.variables ?? [], prismUrl),
|
||||||
authentication: { token: "test-token", prefix: "Bearer" },
|
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
if (environment.parentModel === "environment" && activeEnvironment == null) {
|
||||||
|
activeEnvironment = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestIds = listIds(yaak(dataDir, ["request", "list", workspaceId]));
|
||||||
|
const requests = new Map();
|
||||||
|
for (const id of requestIds) {
|
||||||
|
const request = yaakJson(dataDir, ["request", "show", id]);
|
||||||
|
requests.set(id, request);
|
||||||
|
// OAuth2 would try to fetch a real token; Prism only checks that the
|
||||||
|
// Authorization header is present, so a dummy bearer keeps it satisfied.
|
||||||
|
if (request.authenticationType === "oauth2") {
|
||||||
|
yaak(dataDir, [
|
||||||
|
"request",
|
||||||
|
"update",
|
||||||
|
"--json",
|
||||||
|
JSON.stringify({
|
||||||
|
id,
|
||||||
|
authenticationType: "bearer",
|
||||||
|
authentication: { token: "test-token", prefix: "Bearer" },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const { prism, getLog } = await startPrism(spec, port);
|
|
||||||
port++;
|
|
||||||
try {
|
|
||||||
const sendArgs = ["send", workspaceId];
|
const sendArgs = ["send", workspaceId];
|
||||||
if (activeEnvironment != null) sendArgs.push("-e", activeEnvironment);
|
if (activeEnvironment != null) sendArgs.push("-e", activeEnvironment);
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user