Skip to content

Commit

Permalink
Change CLI deploymentFetch to insert version, authorization, and cont…
Browse files Browse the repository at this point in the history
…ent-type headers by default (#28438)

The one spot that wasn't doing this already was the network test, which uses `bareDeploymentFetch` instead.

GitOrigin-RevId: f20dbbcd599a50226f2d793626db5061fd6e0147
  • Loading branch information
sshader authored and Convex, Inc. committed Jul 30, 2024
1 parent aa623fb commit ac3b44b
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 144 deletions.
9 changes: 1 addition & 8 deletions npm-packages/convex/src/cli/convexExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
deploymentFetch,
logAndHandleFetchError,
} from "./lib/utils.js";
import { version } from "./version.js";
import {
logFailure,
oneoffContext,
Expand Down Expand Up @@ -67,15 +66,10 @@ export const convexExport = new Command("export")
: "";
showSpinner(ctx, `Creating snapshot export${deploymentNotice}`);

const fetch = deploymentFetch(deploymentUrl);
const headers = {
Authorization: `Convex ${adminKey}`,
"Convex-Client": `npm-cli-${version}`,
};
const fetch = deploymentFetch(deploymentUrl, adminKey);
try {
await fetch(`/api/export/request/zip?includeStorage=${includeStorage}`, {
method: "POST",
headers,
});
} catch (e) {
return await logAndHandleFetchError(ctx, e);
Expand Down Expand Up @@ -124,7 +118,6 @@ export const convexExport = new Command("export")
try {
response = await fetch(exportUrl, {
method: "GET",
headers,
});
} catch (e) {
return await logAndHandleFetchError(ctx, e);
Expand Down
17 changes: 4 additions & 13 deletions npm-packages/convex/src/cli/convexImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
deploymentFetch,
logAndHandleFetchError,
} from "./lib/utils.js";
import { version } from "./version.js";
import {
logFailure,
oneoffContext,
Expand Down Expand Up @@ -143,7 +142,7 @@ export const convexImport = new Command("import")
options.yes,
);
}
const fetch = deploymentFetch(deploymentUrl);
const fetch = deploymentFetch(deploymentUrl, adminKey);

const data = ctx.fs.createReadStream(filePath, {
highWaterMark: CHUNK_SIZE,
Expand All @@ -163,10 +162,6 @@ export const convexImport = new Command("import")
mode,
format,
};
const headers = {
Authorization: `Convex ${adminKey}`,
"Convex-Client": `npm-cli-${version}`,
};
const deploymentNotice = options.prod
? ` in your ${chalk.bold("prod")} deployment`
: "";
Expand All @@ -175,7 +170,6 @@ export const convexImport = new Command("import")
try {
const startResp = await fetch("/api/import/start_upload", {
method: "POST",
headers,
});
const { uploadToken } = await startResp.json();

Expand All @@ -187,7 +181,9 @@ export const convexImport = new Command("import")
uploadToken,
)}&partNumber=${partNumber}`;
const partResp = await fetch(partUrl, {
headers,
headers: {
"Content-Type": "application/octet-stream",
},
body: chunk,
method: "POST",
});
Expand All @@ -202,10 +198,6 @@ export const convexImport = new Command("import")
}

const finishResp = await fetch("/api/import/finish_upload", {
headers: {
...headers,
"Content-Type": "application/json",
},
body: JSON.stringify({
import: importArgs,
uploadToken,
Expand Down Expand Up @@ -262,7 +254,6 @@ export const convexImport = new Command("import")
const performUrl = `/api/perform_import`;
try {
await fetch(performUrl, {
headers: { ...headers, "content-type": "application/json" },
method: "POST",
body: JSON.stringify({ importId }),
});
Expand Down
11 changes: 1 addition & 10 deletions npm-packages/convex/src/cli/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ensureHasConvexDependency,
logAndHandleFetchError,
} from "./lib/utils.js";
import { version } from "./version.js";

const envSet = new Command("set")
// Pretend value is required
Expand Down Expand Up @@ -175,17 +174,9 @@ async function callUpdateEnvironmentVariables(
ctx,
deploymentSelection,
);
const fetch = deploymentFetch(url);
const headers = {
Authorization: `Convex ${adminKey}`,
"Convex-Client": `npm-cli-${version}`,
};
const fetch = deploymentFetch(url, adminKey);
try {
await fetch("/api/update_environment_variables", {
headers: {
...headers,
"Content-Type": "application/json",
},
body: JSON.stringify({ changes }),
method: "POST",
});
Expand Down
15 changes: 3 additions & 12 deletions npm-packages/convex/src/cli/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,17 +561,13 @@ export async function pullConfig(
origin: string,
adminKey: string,
): Promise<ConfigWithModuleHashes> {
const fetch = deploymentFetch(origin);
const fetch = deploymentFetch(origin, adminKey);

changeSpinner(ctx, "Downloading current deployment state...");
try {
const res = await fetch("/api/get_config_hashes", {
method: "POST",
body: JSON.stringify({ version, adminKey }),
headers: {
"Content-Type": "application/json",
"Convex-Client": `npm-cli-${version}`,
},
});
deprecationCheckWarning(ctx, res);
const data = await res.json();
Expand Down Expand Up @@ -759,16 +755,12 @@ export async function pushConfig2(
: s;
console.log(JSON.stringify(serializedConfig, custom, 2));
*/
const fetch = deploymentFetch(url);
const fetch = deploymentFetch(url, adminKey);
changeSpinner(ctx, "Analyzing and deploying source code...");
try {
const response = await fetch("/api/deploy2/start_push", {
body: JSON.stringify(serializedConfig),
method: "POST",
headers: {
"Content-Type": "application/json",
"Convex-Client": `npm-cli-${version}`,
},
});
return await response.json();
} catch (error: unknown) {
Expand All @@ -795,7 +787,7 @@ export async function pushConfig(
pushMetrics,
bundledModuleInfos,
);
const fetch = deploymentFetch(url);
const fetch = deploymentFetch(url, adminKey);
try {
if (config.nodeDependencies.length > 0) {
changeSpinner(
Expand All @@ -816,7 +808,6 @@ export async function pushConfig(
headers: {
"Content-Type": "application/json",
"Content-Encoding": "br",
"Convex-Client": `npm-cli-${version}`,
},
});
} catch (error: unknown) {
Expand Down
19 changes: 3 additions & 16 deletions npm-packages/convex/src/cli/lib/deploy2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { changeSpinner, Context, logFailure } from "../../bundler/context.js";
import { version } from "../version.js";
import { deploymentFetch, logAndHandleFetchError } from "./utils.js";
import {
StartPushRequest,
Expand All @@ -23,16 +22,12 @@ export async function startPush(
typeof s === "string" ? s.slice(0, 40) + (s.length > 40 ? "..." : "") : s;
console.log(JSON.stringify(request, custom, 2));
}
const fetch = deploymentFetch(url);
const fetch = deploymentFetch(url, request.adminKey);
changeSpinner(ctx, "Analyzing and deploying source code...");
try {
const response = await fetch("/api/deploy2/start_push", {
body: JSON.stringify(request),
method: "POST",
headers: {
"Content-Type": "application/json",
"Convex-Client": `npm-cli-${version}`,
},
});
return startPushResponse.parse(await response.json());
} catch (error: unknown) {
Expand All @@ -48,7 +43,7 @@ export async function waitForSchema(
url: string,
startPush: StartPushResponse,
) {
const fetch = deploymentFetch(url);
const fetch = deploymentFetch(url, adminKey);
try {
const response = await fetch("/api/deploy2/wait_for_schema", {
body: JSON.stringify({
Expand All @@ -57,10 +52,6 @@ export async function waitForSchema(
dryRun: false,
}),
method: "POST",
headers: {
"Content-Type": "application/json",
"Convex-Client": `npm-cli-${version}`,
},
});
return await response.json();
} catch (error: unknown) {
Expand All @@ -76,7 +67,7 @@ export async function finishPush(
url: string,
startPush: StartPushResponse,
): Promise<void> {
const fetch = deploymentFetch(url);
const fetch = deploymentFetch(url, adminKey);
changeSpinner(ctx, "Finalizing push...");
try {
const response = await fetch("/api/deploy2/finish_push", {
Expand All @@ -86,10 +77,6 @@ export async function finishPush(
dryRun: false,
}),
method: "POST",
headers: {
"Content-Type": "application/json",
"Convex-Client": `npm-cli-${version}`,
},
});
return await response.json();
} catch (error: unknown) {
Expand Down
17 changes: 3 additions & 14 deletions npm-packages/convex/src/cli/lib/indexes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import chalk from "chalk";
import path from "path";
import { bundleSchema } from "../../bundler/index.js";
import { version } from "../version.js";
import {
Context,
changeSpinner,
Expand Down Expand Up @@ -63,14 +62,10 @@ export async function pushSchema(
changeSpinner(ctx, "Checking for index or schema changes...");

let data: PrepareSchemaResponse;
const fetch = deploymentFetch(origin);
const fetch = deploymentFetch(origin, adminKey);
try {
const res = await fetch("/api/prepare_schema", {
method: "POST",
headers: {
"Convex-Client": `npm-cli-${version}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
bundle: bundles[0],
adminKey,
Expand Down Expand Up @@ -99,16 +94,10 @@ async function waitForReadySchema(
schemaId: string,
): Promise<SchemaState> {
const path = `api/schema_state/${schemaId}`;
const depFetch = deploymentFetch(origin);
const depFetch = deploymentFetch(origin, adminKey);
const fetch = async () => {
try {
const resp = await depFetch(path, {
headers: {
Authorization: `Convex ${adminKey}`,
"Convex-Client": `npm-cli-${version}`,
"Content-Type": "application/json",
},
});
const resp = await depFetch(path, { method: "GET" });
const data: SchemaStateResponse = await resp.json();
return data;
} catch (err: unknown) {
Expand Down
21 changes: 4 additions & 17 deletions npm-packages/convex/src/cli/lib/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
logOutput,
logWarning,
} from "../../bundler/context.js";
import { version } from "../version.js";
import { nextBackoff } from "../dev.js";
import chalk from "chalk";
import { deploymentFetch } from "./utils.js";
Expand All @@ -23,18 +22,13 @@ export async function watchLogs(
history?: number | boolean;
},
) {
const authHeader = createAuthHeader(adminKey);
let numFailures = 0;
let isFirst = true;
let cursorMs = 0;

for (;;) {
try {
const { entries, newCursor } = await pollUdfLog(
cursorMs,
url,
authHeader,
);
const { entries, newCursor } = await pollUdfLog(cursorMs, url, adminKey);
cursorMs = newCursor;
numFailures = 0;
// The first execution, we just want to fetch the current head cursor so we don't send stale
Expand Down Expand Up @@ -75,10 +69,6 @@ export async function watchLogs(
}
}

function createAuthHeader(adminKey: string): string {
return `Convex ${adminKey}`;
}

type UdfType = "Query" | "Mutation" | "Action" | "HttpAction";

type StructuredLogLine = {
Expand All @@ -104,14 +94,11 @@ type UdfExecutionResponse = {
async function pollUdfLog(
cursor: number,
url: string,
authHeader: string,
adminKey: string,
): Promise<{ entries: UdfExecutionResponse[]; newCursor: number }> {
const fetch = deploymentFetch(url);
const fetch = deploymentFetch(url, adminKey);
const response = await fetch(`/api/stream_function_logs?cursor=${cursor}`, {
headers: {
Authorization: authHeader,
"Convex-Client": `npm-cli-${version}`,
},
method: "GET",
});
return await response.json();
}
Expand Down
Loading

0 comments on commit ac3b44b

Please sign in to comment.