Skip to content

Commit dd7f7d2

Browse files
sshaderConvex, Inc.
authored andcommitted
Fully switch off axios in the CLI (#27604)
Tom did most of this a while ago, but we had a couple commands still using Axios. This switches them + cleans up all the axios code in our CLI. I tested all of the commands I touched manually. I think this should be fairly safe given that the previous switch from Axios to fetch went fine. GitOrigin-RevId: 2cf632be09869a5d799710e1ca37ac1122b7b9a2
1 parent 28f7074 commit dd7f7d2

File tree

12 files changed

+88
-230
lines changed

12 files changed

+88
-230
lines changed

npm-packages/common/config/rush/pnpm-lock.yaml

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm-packages/convex/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@
215215
"@types/ws": "^8.5.3",
216216
"@typescript-eslint/eslint-plugin": "^6.7.4",
217217
"@typescript-eslint/parser": "^6.7.4",
218-
"axios": "1.6.2",
219-
"axios-retry": "^3.2.4",
220218
"bufferutil": "^4.0.7",
221219
"chalk": "4",
222220
"chokidar": "3.5.3",

npm-packages/convex/src/bundler/fs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as fsPromises from "fs/promises";
77
import os from "os";
88
import path from "path";
99
import crypto from "crypto";
10+
import { Readable } from "stream";
1011

1112
export type NormalizedPath = string;
1213

@@ -139,7 +140,7 @@ class NodeFs implements Filesystem {
139140
// To avoid issues with filesystem events triggering for our own streamed file
140141
// writes, writeFileStream is intentionally not on the Filesystem interface
141142
// and not implemented by RecordingFs.
142-
async writeFileStream(path: string, stream: ReadStream): Promise<void> {
143+
async writeFileStream(path: string, stream: Readable): Promise<void> {
143144
// 'wx' means O_CREAT | O_EXCL | O_WRONLY
144145
// 0o644 means owner has readwrite access, everyone else has read access.
145146
const fileHandle = await fsPromises.open(path, "wx", 0o644);

npm-packages/convex/src/cli/convexExport.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Command, Option } from "@commander-js/extra-typings";
22
import chalk from "chalk";
33
import {
44
ensureHasConvexDependency,
5-
logAndHandleAxiosError,
6-
deploymentClient,
75
waitUntilCalled,
6+
deploymentFetch,
7+
logAndHandleFetchError,
88
} from "./lib/utils.js";
99
import { version } from "./version.js";
1010
import {
@@ -22,11 +22,11 @@ import {
2222
deploymentSelectionFromOptions,
2323
} from "./lib/api.js";
2424
import { subscribe } from "./lib/run.js";
25-
import { AxiosResponse } from "axios";
2625
import { nodeFs } from "../bundler/fs.js";
2726
import path from "path";
2827
import { deploymentDashboardUrlPage } from "./dashboard.js";
2928
import { actionDescription } from "./lib/command.js";
29+
import { Readable } from "stream";
3030

3131
export const convexExport = new Command("export")
3232
.summary("Export data from your deployment to a ZIP file")
@@ -67,19 +67,18 @@ export const convexExport = new Command("export")
6767
: "";
6868
showSpinner(ctx, `Creating snapshot export${deploymentNotice}`);
6969

70-
const client = deploymentClient(deploymentUrl);
70+
const fetch = deploymentFetch(deploymentUrl);
7171
const headers = {
7272
Authorization: `Convex ${adminKey}`,
7373
"Convex-Client": `npm-cli-${version}`,
7474
};
7575
try {
76-
await client.post(
77-
`/api/export/request/zip?includeStorage=${includeStorage}`,
78-
null,
79-
{ headers },
80-
);
76+
await fetch(`/api/export/request/zip?includeStorage=${includeStorage}`, {
77+
method: "POST",
78+
headers,
79+
});
8180
} catch (e) {
82-
return await logAndHandleAxiosError(ctx, e);
81+
return await logAndHandleFetchError(ctx, e);
8382
}
8483

8584
const snapshotExportState = await waitForStableExportState(
@@ -121,22 +120,22 @@ export const convexExport = new Command("export")
121120
const exportUrl = `/api/export/zip/${snapshotExportState.start_ts.toString()}?adminKey=${encodeURIComponent(
122121
adminKey,
123122
)}`;
124-
let response: AxiosResponse;
123+
let response: Response;
125124
try {
126-
response = await client.get(exportUrl, {
125+
response = await fetch(exportUrl, {
126+
method: "GET",
127127
headers,
128-
responseType: "stream",
129128
});
130129
} catch (e) {
131-
return await logAndHandleAxiosError(ctx, e);
130+
return await logAndHandleFetchError(ctx, e);
132131
}
133132

134133
let filePath;
135134
if (ctx.fs.exists(inputPath)) {
136135
const st = ctx.fs.stat(inputPath);
137136
if (st.isDirectory()) {
138137
const contentDisposition =
139-
response.headers["content-disposition"] ?? "";
138+
response.headers.get("content-disposition") ?? "";
140139
let filename = `snapshot_${snapshotExportState.start_ts.toString()}.zip`;
141140
if (contentDisposition.startsWith("attachment; filename=")) {
142141
filename = contentDisposition.slice("attachment; filename=".length);
@@ -155,7 +154,10 @@ export const convexExport = new Command("export")
155154
);
156155

157156
try {
158-
await nodeFs.writeFileStream(filePath, response.data);
157+
await nodeFs.writeFileStream(
158+
filePath,
159+
Readable.fromWeb(response.body! as any),
160+
);
159161
} catch (e) {
160162
logFailure(ctx, `Exporting data failed`);
161163
logError(ctx, chalk.red(e));

npm-packages/convex/src/cli/convexImport.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import chalk from "chalk";
22
import inquirer from "inquirer";
33
import {
44
ensureHasConvexDependency,
5-
logAndHandleAxiosError,
65
formatSize,
7-
deploymentClient,
86
waitUntilCalled,
7+
deploymentFetch,
8+
logAndHandleFetchError,
99
} from "./lib/utils.js";
1010
import { version } from "./version.js";
1111
import {
@@ -143,7 +143,7 @@ export const convexImport = new Command("import")
143143
options.yes,
144144
);
145145
}
146-
const client = deploymentClient(deploymentUrl);
146+
const fetch = deploymentFetch(deploymentUrl);
147147

148148
const data = ctx.fs.createReadStream(filePath, {
149149
highWaterMark: CHUNK_SIZE,
@@ -173,10 +173,11 @@ export const convexImport = new Command("import")
173173
const tableNotice = tableName ? ` to table "${chalk.bold(tableName)}"` : "";
174174
let importId: string;
175175
try {
176-
const startResp = await client.post("/api/import/start_upload", null, {
176+
const startResp = await fetch("/api/import/start_upload", {
177+
method: "POST",
177178
headers,
178179
});
179-
const { uploadToken } = startResp.data;
180+
const { uploadToken } = await startResp.json();
180181

181182
const partTokens = [];
182183
let partNumber = 1;
@@ -185,8 +186,12 @@ export const convexImport = new Command("import")
185186
const partUrl = `/api/import/upload_part?uploadToken=${encodeURIComponent(
186187
uploadToken,
187188
)}&partNumber=${partNumber}`;
188-
const partResp = await client.post(partUrl, chunk, { headers });
189-
partTokens.push(partResp.data);
189+
const partResp = await fetch(partUrl, {
190+
headers,
191+
body: chunk,
192+
method: "POST",
193+
});
194+
partTokens.push(await partResp.text());
190195
partNumber += 1;
191196
changeSpinner(
192197
ctx,
@@ -196,24 +201,28 @@ export const convexImport = new Command("import")
196201
);
197202
}
198203

199-
const finishResp = await client.post(
200-
"/api/import/finish_upload",
201-
{
204+
const finishResp = await fetch("/api/import/finish_upload", {
205+
headers: {
206+
...headers,
207+
"Content-Type": "application/json",
208+
},
209+
body: JSON.stringify({
202210
import: importArgs,
203211
uploadToken,
204212
partTokens,
205-
},
206-
{ headers },
207-
);
208-
importId = finishResp.data.importId;
213+
}),
214+
method: "POST",
215+
});
216+
const body = await finishResp.json();
217+
importId = body.importId;
209218
} catch (e) {
210219
logFailure(
211220
ctx,
212221
`Importing data from "${chalk.bold(
213222
filePath,
214223
)}"${tableNotice}${deploymentNotice} failed`,
215224
);
216-
return await logAndHandleAxiosError(ctx, e);
225+
return await logAndHandleFetchError(ctx, e);
217226
}
218227
changeSpinner(ctx, "Parsing uploaded data");
219228
// eslint-disable-next-line no-constant-condition
@@ -252,15 +261,19 @@ export const convexImport = new Command("import")
252261
showSpinner(ctx, `Importing`);
253262
const performUrl = `/api/perform_import`;
254263
try {
255-
await client.post(performUrl, { importId }, { headers });
264+
await fetch(performUrl, {
265+
headers: { ...headers, "content-type": "application/json" },
266+
method: "POST",
267+
body: JSON.stringify({ importId }),
268+
});
256269
} catch (e) {
257270
logFailure(
258271
ctx,
259272
`Importing data from "${chalk.bold(
260273
filePath,
261274
)}"${tableNotice}${deploymentNotice} failed`,
262275
);
263-
return await logAndHandleAxiosError(ctx, e);
276+
return await logAndHandleFetchError(ctx, e);
264277
}
265278
// Now we have kicked off the rest of the import, go around the loop again.
266279
break;

npm-packages/convex/src/cli/docs.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from "chalk";
33
import open from "open";
44
import { oneoffContext } from "../bundler/context.js";
55
import { getTargetDeploymentName } from "./lib/deployment.js";
6-
import { bigBrainClient, deprecationCheckWarning } from "./lib/utils.js";
6+
import { bigBrainFetch, deprecationCheckWarning } from "./lib/utils.js";
77

88
export const docs = new Command("docs")
99
.description("Open the docs in the browser")
@@ -14,11 +14,12 @@ export const docs = new Command("docs")
1414
// command we don't care at all if the user is in the right directory
1515
const configuredDeployment = getTargetDeploymentName();
1616
const getCookieUrl = `get_cookie/${configuredDeployment}`;
17-
const client = await bigBrainClient(ctx);
17+
const fetch = await bigBrainFetch(ctx);
1818
try {
19-
const res = await client.get(getCookieUrl);
19+
const res = await fetch(getCookieUrl);
2020
deprecationCheckWarning(ctx, res);
21-
await openDocs(options.open, res.data.cookie);
21+
const { cookie } = await res.json();
22+
await openDocs(options.open, cookie);
2223
} catch {
2324
await openDocs(options.open);
2425
}

npm-packages/convex/src/cli/env.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import {
1616
import { actionDescription } from "./lib/command.js";
1717
import { runQuery } from "./lib/run.js";
1818
import {
19-
deploymentClient,
19+
deploymentFetch,
2020
ensureHasConvexDependency,
21-
logAndHandleAxiosError,
21+
logAndHandleFetchError,
2222
} from "./lib/utils.js";
2323
import { version } from "./version.js";
2424

@@ -175,19 +175,20 @@ async function callUpdateEnvironmentVariables(
175175
ctx,
176176
deploymentSelection,
177177
);
178-
const client = deploymentClient(url);
178+
const fetch = deploymentFetch(url);
179179
const headers = {
180180
Authorization: `Convex ${adminKey}`,
181181
"Convex-Client": `npm-cli-${version}`,
182182
};
183183
try {
184-
await client.post(
185-
"/api/update_environment_variables",
186-
{ changes },
187-
{
188-
headers,
184+
await fetch("/api/update_environment_variables", {
185+
headers: {
186+
...headers,
187+
"Content-Type": "application/json",
189188
},
190-
);
189+
body: JSON.stringify({ changes }),
190+
method: "POST",
191+
});
191192
return deploymentType !== undefined || deploymentName !== undefined
192193
? ` (on${
193194
deploymentType !== undefined ? " " + chalk.bold(deploymentType) : ""
@@ -196,7 +197,7 @@ async function callUpdateEnvironmentVariables(
196197
})`
197198
: "";
198199
} catch (e) {
199-
return await logAndHandleAxiosError(ctx, e);
200+
return await logAndHandleFetchError(ctx, e);
200201
}
201202
}
202203

npm-packages/convex/src/cli/lib/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
ErrorData,
2727
loadPackageJson,
2828
deploymentFetch,
29-
fetchDeprecationCheckWarning,
29+
deprecationCheckWarning,
3030
logAndHandleFetchError,
3131
ThrowingFetchError,
3232
} from "./utils.js";
@@ -579,7 +579,7 @@ export async function pullConfig(
579579
"Convex-Client": `npm-cli-${version}`,
580580
},
581581
});
582-
fetchDeprecationCheckWarning(ctx, res);
582+
deprecationCheckWarning(ctx, res);
583583
const data = await res.json();
584584
const backendConfig = parseBackendConfig(data.config);
585585
const projectConfig = {

npm-packages/convex/src/cli/lib/indexes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
poll,
1414
logAndHandleFetchError,
1515
deploymentFetch,
16-
fetchDeprecationCheckWarning,
16+
deprecationCheckWarning,
1717
} from "./utils.js";
1818

1919
type IndexMetadata = {
@@ -77,7 +77,7 @@ export async function pushSchema(
7777
dryRun,
7878
}),
7979
});
80-
fetchDeprecationCheckWarning(ctx, res);
80+
deprecationCheckWarning(ctx, res);
8181
data = await res.json();
8282
} catch (err: unknown) {
8383
logFailure(ctx, `Error: Unable to run schema validation on ${origin}`);

npm-packages/convex/src/cli/lib/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { writeDeploymentEnvVar } from "./deployment.js";
2222
import { writeConvexUrlToEnvFile } from "./envvars.js";
2323
import {
2424
functionsDir,
25-
logAndHandleAxiosError,
25+
logAndHandleFetchError,
2626
validateOrSelectTeam,
2727
} from "./utils.js";
2828

@@ -73,7 +73,7 @@ export async function init(
7373
));
7474
} catch (err) {
7575
logFailure(ctx, "Unable to create project.");
76-
return await logAndHandleAxiosError(ctx, err);
76+
return await logAndHandleFetchError(ctx, err);
7777
}
7878

7979
const teamMessage = didChooseBetweenTeams

0 commit comments

Comments
 (0)