Skip to content

Commit

Permalink
cloudchamber: add internal module insertion (#4810)
Browse files Browse the repository at this point in the history
* cloudchamber: add internal module insertion

* cloudchamber: add https Agent to the fetch function
  • Loading branch information
gabivlj authored Jan 24, 2024
1 parent d031e75 commit 6eb2b9d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changeset/tender-owls-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: Cloudchamber command shows json error message on load account if --json specified

If the user specifies a json option, we should see a more detailed error on why `loadAccount` failed.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ dist
wrangler-dist/
miniflare-dist
packages/wrangler/wrangler.toml
packages/wrangler/src/cloudchamber/internal
packages/prerelease-registry/_worker.bundle
packages/wranglerjs-compat-webpack-plugin/lib
.wrangler-1-cache
Expand Down
3 changes: 3 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
/packages/wrangler/src/api/d1/ @cloudflare/d1 @cloudflare/wrangler
/packages/wrangler/src/d1/ @cloudflare/d1 @cloudflare/wrangler

# Cloudchamber ownership
/packages/wrangler/src/cloudchamber/ @cloudflare/cloudchamber @cloudflare/wrangler

# C3 ownership
/packages/create-cloudflare/ @cloudflare/c3

Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/cloudchamber/client/core/OpenAPI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { Agent } from "undici";
import type { ApiRequestOptions } from "./ApiRequestOptions";

type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
Expand All @@ -16,10 +17,11 @@ export type OpenAPIConfig = {
PASSWORD?: string | Resolver<string>;
HEADERS?: Headers | Resolver<Headers>;
ENCODE_PATH?: (path: string) => string;
AGENT?: Agent;
};

export const OpenAPI: OpenAPIConfig = {
BASE: "https://api.cloudflare.com/client/v4/accounts/<account-tag>/cloudchamber",
BASE: "",
VERSION: "1.0.0",
WITH_CREDENTIALS: false,
CREDENTIALS: "include",
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/cloudchamber/client/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import { fetch, FormData, Headers, RequestInit, Response } from "undici";
import { ApiError } from "./ApiError";
import { CancelablePromise } from "./CancelablePromise";
import { type OpenAPIConfig } from "./OpenAPI";
import type { ApiRequestOptions } from "./ApiRequestOptions";
import type { ApiResult } from "./ApiResult";
import type { OnCancel } from "./CancelablePromise";
import type { OpenAPIConfig } from "./OpenAPI";

const isDefined = <T>(
value: T | null | undefined
Expand Down Expand Up @@ -213,6 +213,7 @@ export const sendRequest = async (
body: body ?? formData,
method: options.method,
signal: controller.signal,
dispatcher: config.AGENT ?? undefined,
};

if (config.WITH_CREDENTIALS) {
Expand Down
25 changes: 18 additions & 7 deletions packages/wrangler/src/cloudchamber/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export async function promiseSpinner<T>(
}

export async function fillOpenAPIConfiguration(config: Config, json: boolean) {
const headers: Record<string, string> = {};
const headers: Record<string, string> =
OpenAPI.HEADERS !== undefined ? { ...OpenAPI.HEADERS } : {};

// if the config cache folder doesn't exist, it means that there is not a node_modules folder in the tree
if (Object.keys(getConfigCache("wrangler-account.json")).length === 0) {
Expand Down Expand Up @@ -195,16 +196,26 @@ export async function fillOpenAPIConfiguration(config: Config, json: boolean) {
// due to our OpenAPI codegenerated client.
headers["User-Agent"] = `wrangler/${wranglerVersion}`;
OpenAPI.CREDENTIALS = "omit";
const [base, errApiURL] = await wrap(getAPIUrl(config));
if (errApiURL) {
crash("getting the API url:" + errApiURL.message);
return;
if (OpenAPI.BASE.length === 0) {
const [base, errApiURL] = await wrap(getAPIUrl(config));
if (errApiURL) {
crash("getting the API url:" + errApiURL.message);
}

OpenAPI.BASE = base;
}

OpenAPI.BASE = base;
OpenAPI.HEADERS = headers;
const [, err] = await wrap(loadAccountSpinner({ json }));
if (err) crash("loading Cloudchamber account failed:" + err.message);

if (err) {
let message = err.message;
if (json && err instanceof ApiError) {
message = JSON.stringify(err);
}

crash("loading Cloudchamber account failed:" + message);
}
}

export function interactWithUser(config: { json?: boolean }): boolean {
Expand Down
12 changes: 12 additions & 0 deletions packages/wrangler/src/cloudchamber/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import { sshCommand } from "./ssh/ssh";
import type { CommonYargsArgvJSON, CommonYargsOptions } from "../yargs-types";
import type { CommandModule } from "yargs";

function internalCommands(args: CommonYargsArgvJSON) {
try {
// Add dynamically an internal module that we can attach internal commands
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cloudchamberInternalRequireEntry = require("./internal/index");
return cloudchamberInternalRequireEntry.internalCommands(args);
} catch {
return args;
}
}

export const cloudchamber = (
yargs: CommonYargsArgvJSON,
subHelp: CommandModule<CommonYargsOptions, CommonYargsOptions>
) => {
yargs = internalCommands(yargs);
return yargs
.command(
"delete [deploymentId]",
Expand Down

0 comments on commit 6eb2b9d

Please sign in to comment.