Skip to content

Commit

Permalink
wip: download of Worker script
Browse files Browse the repository at this point in the history
Workaround for handling multipart/form-data slicing off top and bottom boundaries.
- [x] Need to prevent download in existing projects
- [x] Duplicate the logic for JS files
- [] tests
multipart/formdata not supported currently in Undici nodejs/undici#974
  • Loading branch information
JacobMGEvans committed Aug 10, 2022
1 parent 9e632cd commit 4aebb30
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 37 deletions.
40 changes: 40 additions & 0 deletions packages/wrangler/src/cfetch/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,43 @@ export async function fetchR2Objects(
);
}
}

/**
* This is a wrapper STOPGAP for getting the script which returns a raw text response.
* TODO: remove this once the API is fixed and utilize fetchResult instead. - JACOB
*/
export async function fetchDashboardScript(
resource: string,
bodyInit: RequestInit = {}
): Promise<string> {
await requireLoggedIn();
const auth = requireApiToken();
const headers = cloneHeaders(bodyInit.headers);
addAuthorizationHeaderIfUnspecified(headers, auth);
addUserAgent(headers);

const response = await fetch(`${getCloudflareAPIBaseURL()}${resource}`, {
...bodyInit,
headers,
});

if (!response.ok || !response.body) {
throw new Error(
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
);
}

const usesModules = response.headers
.get("content-type")
?.startsWith("multipart");

if (usesModules) {
const file = await response.text();

// Follow up on issue in Undici about multipart/form-data support & replace the workaround: https://github.com/nodejs/undici/issues/974
// This should be using a builtin formData() parser pattern.
return file.split("\n").slice(4, -4).join("\n");
} else {
return response.text();
}
}
142 changes: 105 additions & 37 deletions packages/wrangler/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import TOML from "@iarna/toml";
import { findUp } from "find-up";
import { version as wranglerVersion } from "../package.json";

import { fetchDashboardScript } from "./cfetch/internal";
import { readConfig } from "./config";
import { confirm, select } from "./dialogs";
import { initializeGit, isGitInstalled, isInsideGitRepo } from "./git-client";
import { logger } from "./logger";
import { getPackageManager } from "./package-manager";
import { parsePackageJSON, parseTOML, readFileSync } from "./parse";
import { requireAuth } from "./user";
import { CommandLineArgsError, printWranglerBanner } from "./index";
import type { ConfigPath } from "./index";

import type { Argv, ArgumentsCamelCase } from "yargs";

Expand All @@ -36,6 +40,11 @@ export async function initOptions(yargs: Argv) {
describe: 'Answer "yes" to any prompts for new projects',
type: "boolean",
alias: "y",
})
.option("from-dash", {
describe: "Use script from the dashboard editor",
type: "string",
hidden: true,
});
}

Expand All @@ -62,6 +71,7 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
const instructions: string[] = [];
let shouldRunPackageManagerInstall = false;
const creationDirectory = path.resolve(process.cwd(), args.name ?? "");
const fromDashboard = args["from-dash"] as string;

if (args.site) {
const gitDirectory =
Expand Down Expand Up @@ -98,12 +108,15 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
let justCreatedWranglerToml = false;

if (fs.existsSync(wranglerTomlDestination)) {
let shouldContinue = false;
logger.warn(
`${path.relative(process.cwd(), wranglerTomlDestination)} already exists!`
);
const shouldContinue = await confirm(
"Do you want to continue initializing this project?"
);
if (!fromDashboard) {
shouldContinue = await confirm(
"Do you want to continue initializing this project?"
);
}
if (!shouldContinue) {
return;
}
Expand Down Expand Up @@ -438,36 +451,63 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
process.cwd(),
path.join(creationDirectory, "./src/index.ts")
);

const newWorkerType = yesFlag
? "fetch"
: await getNewWorkerType(newWorkerFilename);

if (newWorkerType !== "none") {
const template = getNewWorkerTemplate("ts", newWorkerType);

if (fromDashboard) {
const config = readConfig(args.config as ConfigPath, args);
const accountId = await requireAuth(config);
await mkdir(path.join(creationDirectory, "./src"), {
recursive: true,
});
await writeFile(
path.join(creationDirectory, "./src/index.ts"),
readFileSync(path.join(__dirname, `../templates/${template}`))

const dashScript = await fetchDashboardScript(
`/accounts/${accountId}/workers/scripts/${fromDashboard}`,
{
method: "GET",
}
);

logger.log(
`✨ Created ${path.relative(
process.cwd(),
path.join(creationDirectory, "./src/index.ts")
)}`
await writeFile(
path.join(creationDirectory, "./src/index.ts"),
dashScript
);

await writePackageJsonScriptsAndUpdateWranglerToml(
shouldWritePackageJsonScripts,
justCreatedWranglerToml,
pathToPackageJson,
"src/index.ts",
getNewWorkerToml(newWorkerType)
{}
);
} else {
const newWorkerType = yesFlag
? "fetch"
: await getNewWorkerType(newWorkerFilename);

if (newWorkerType !== "none") {
const template = getNewWorkerTemplate("ts", newWorkerType);

await mkdir(path.join(creationDirectory, "./src"), {
recursive: true,
});
await writeFile(
path.join(creationDirectory, "./src/index.ts"),
readFileSync(path.join(__dirname, `../templates/${template}`))
);

logger.log(
`✨ Created ${path.relative(
process.cwd(),
path.join(creationDirectory, "./src/index.ts")
)}`
);

await writePackageJsonScriptsAndUpdateWranglerToml(
shouldWritePackageJsonScripts,
justCreatedWranglerToml,
pathToPackageJson,
"src/index.ts",
getNewWorkerToml(newWorkerType)
);
}
}
}
} else {
Expand All @@ -477,35 +517,63 @@ export async function initHandler(args: ArgumentsCamelCase<InitArgs>) {
path.join(creationDirectory, "./src/index.js")
);

const newWorkerType = yesFlag
? "fetch"
: await getNewWorkerType(newWorkerFilename);

if (newWorkerType !== "none") {
const template = getNewWorkerTemplate("js", newWorkerType);

if (fromDashboard) {
const config = readConfig(args.config as ConfigPath, args);
const accountId = await requireAuth(config);
await mkdir(path.join(creationDirectory, "./src"), {
recursive: true,
});
await writeFile(
path.join(creationDirectory, "./src/index.js"),
readFileSync(path.join(__dirname, `../templates/${template}`))

const dashScript = await fetchDashboardScript(
`/accounts/${accountId}/workers/scripts/${fromDashboard}`,
{
method: "GET",
}
);

logger.log(
`✨ Created ${path.relative(
process.cwd(),
path.join(creationDirectory, "./src/index.js")
)}`
await writeFile(
path.join(creationDirectory, "./src/index.js"),
dashScript
);

await writePackageJsonScriptsAndUpdateWranglerToml(
shouldWritePackageJsonScripts,
justCreatedWranglerToml,
pathToPackageJson,
"src/index.js",
getNewWorkerToml(newWorkerType)
"src/index.ts",
{}
);
} else {
const newWorkerType = yesFlag
? "fetch"
: await getNewWorkerType(newWorkerFilename);

if (newWorkerType !== "none") {
const template = getNewWorkerTemplate("js", newWorkerType);

await mkdir(path.join(creationDirectory, "./src"), {
recursive: true,
});
await writeFile(
path.join(creationDirectory, "./src/index.js"),
readFileSync(path.join(__dirname, `../templates/${template}`))
);

logger.log(
`✨ Created ${path.relative(
process.cwd(),
path.join(creationDirectory, "./src/index.js")
)}`
);

await writePackageJsonScriptsAndUpdateWranglerToml(
shouldWritePackageJsonScripts,
justCreatedWranglerToml,
pathToPackageJson,
"src/index.js",
getNewWorkerToml(newWorkerType)
);
}
}
}
}
Expand Down

0 comments on commit 4aebb30

Please sign in to comment.