forked from cloudflare/cloudflare-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-upload.ts
87 lines (78 loc) · 2.68 KB
/
script-upload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env -S npm run tsn -T
/*
* Generate an API token: https://developers.cloudflare.com/fundamentals/api/get-started/create-token/
* (Not Global API Key!)
*
* Find your account id: https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/
*
* Set these environment variables:
* - CLOUDFLARE_API_TOKEN
* - CLOUDFLARE_ACCOUNT_ID
*
* ### Workers for Platforms ###
*
* For uploading a User Worker to a dispatch namespace:
* https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/
*
* Define a "dispatchNamespaceName" variable and change the entire "const script = " line to the following:
* "const script = await client.workersForPlatforms.dispatch.namespaces.scripts.update(dispatchNamespaceName, scriptName, {"
*/
import Cloudflare from 'cloudflare';
import { toFile } from 'cloudflare/index';
const apiToken = process.env['CLOUDFLARE_API_TOKEN'] ?? '';
if (!apiToken) {
throw new Error('Please set envar CLOUDFLARE_ACCOUNT_ID');
}
const accountID = process.env['CLOUDFLARE_ACCOUNT_ID'] ?? '';
if (!accountID) {
throw new Error('Please set envar CLOUDFLARE_API_TOKEN');
}
const client = new Cloudflare({
apiToken: apiToken,
});
async function main() {
const scriptName = 'my-hello-world-script';
const scriptFileName = `${scriptName}.mjs`;
// Workers Scripts prefer Module Syntax
// https://blog.cloudflare.com/workers-javascript-modules/
const scriptContent = `
export default {
async fetch(request, env, ctx) {
return new Response(env.MESSAGE, { status: 200 });
}
};
`;
try {
// https://developers.cloudflare.com/api/resources/workers/subresources/scripts/methods/update/
const script = await client.workers.scripts.update(scriptName, {
account_id: accountID,
// https://developers.cloudflare.com/workers/configuration/multipart-upload-metadata/
metadata: {
main_module: scriptFileName,
bindings: [
{
type: 'plain_text',
name: 'MESSAGE',
text: 'Hello World!',
},
],
},
files: {
// Add main_module file
[scriptFileName]: await toFile(Buffer.from(scriptContent), scriptFileName, {
type: 'application/javascript+module',
}),
// Can add other files, such as more modules or source maps
// [sourceMapFileName]: await toFile(Buffer.from(sourceMapContent), sourceMapFileName, {
// type: 'application/source-map',
// }),
},
});
console.log('Script Upload success!');
console.log(JSON.stringify(script, null, 2));
} catch (error) {
console.error('Script Upload failure!');
console.error(error);
}
}
main();