Skip to content

Commit

Permalink
feat: download of Worker script
Browse files Browse the repository at this point in the history
Added the --from-dash <worker-name>, getting the source code into an initialized project replacing the template script.

Resolves #1624
Discussion: #1623
notes: Workaround for handling multipart/form-data slicing off top and bottom boundaries.
multipart/formdata parsing not supported currently in Undici nodejs/undici#974
  • Loading branch information
JacobMGEvans committed Aug 11, 2022
1 parent 9e632cd commit 19be735
Show file tree
Hide file tree
Showing 6 changed files with 753 additions and 397 deletions.
11 changes: 11 additions & 0 deletions .changeset/six-needles-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": patch
---

feat: download of Worker script
Added the --from-dash <worker-name>, getting the source code into an initialized project replacing the template script.

Resolves #1624
Discussion: #1623
notes: Workaround for handling multipart/form-data slicing off top and bottom boundaries.
multipart/formdata parsing not supported currently in Undici https://github.com/nodejs/undici/issues/974
49 changes: 49 additions & 0 deletions packages/wrangler/src/__tests__/helpers/mock-cfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export function unsetAllMocks() {

const kvGetMocks = new Map<string, string | Buffer>();
const r2GetMocks = new Map<string, string | undefined>();
const dashScriptMocks = new Map<string, string | undefined>();

/**
* @mocked typeof fetchKVGetValue
Expand Down Expand Up @@ -260,4 +261,52 @@ export function setMockFetchR2Objects({
export function unsetSpecialMockFns() {
kvGetMocks.clear();
r2GetMocks.clear();
dashScriptMocks.clear();
}

/**
* @mocked typeof fetchDashScript
* multipart/form-data is the response for modules and raw text for the Script endpoint.
*/
export async function mockFetchDashScript(
resource: string,
bodyInit: {
body: BodyInit | Readable;
headers: HeadersInit | undefined;
method: "GET";
}
): Promise<string> {
/**
* Here we destroy & removeListeners to "drain" the stream, for testing purposes
* mimicking the fetch request taking in the stream and draining it.
*/
if (bodyInit.body instanceof Readable) {
bodyInit.body.destroy();
bodyInit.body.removeAllListeners();
}

if (dashScriptMocks.has(resource)) {
const value = dashScriptMocks.get(resource) ?? "";

return value;
}
throw new Error(`no mock found for \`init from-dash\` - ${resource}`);
}

/**
* Mock setter for usage within test blocks, companion helper to `mockFetchDashScript`
*/
export function setMockFetchDashScript({
accountId,
fromDashScriptName,
mockResponse,
}: {
accountId: string;
fromDashScriptName: string;
mockResponse?: string;
}) {
dashScriptMocks.set(
`/accounts/${accountId}/workers/scripts/${fromDashScriptName}`,
mockResponse
);
}

0 comments on commit 19be735

Please sign in to comment.