if (url.pathname == '/purge_cache') {
fetch('https://api.cloudflare.com/client/v4/zones/7aa7c5aa56d57f34baa93_DOMAINID_33c88e800aceabd96f2/purge_cache', {
method: 'POST',
body: '{"purge_everything":true}',
headers: {
authorization: "Bearer 2nvh5e9mMgN0hkGAd8c0VQ-KnCKPntb"
}
});
fetch('https://api.cloudflare.com/client/v4/zones/7aa7c5aa56d57f34baa9333_DOMAINID_c88e800aceabd96f2/purge_cache', {
method: 'POST',
body: '{"purge_everything":true}',
headers: {
authorization: "Bearer 2nvh5e9mMgN0hkGAd8c0VQ-KnCKPntb"
}
});
var resp = await fetch("https://raw.githubusercontent.com/devUser/projectName/master/myCFWorker.min.js");
if (resp.status == 200) {
resp = await resp.text();
const form = new FormData();
form.append("script", resp);
form.append("metadata", '{"bindings":[],"body_part":"script"}');
const options = {
method: 'PUT',
headers: {
"Authorization": "Bearer KnCKPntb-S7YZM7jQzirCtpwZXWHoDQ"
}
};
options.body = form;
return fetch('https://api.cloudflare.com/client/v4/accounts/7aa7c5aa56d57f34ba_ACCOUNT_ID_a9333c88e800aceabd96f2/workers/scripts/workerNameFromDashboard', options);
} else {
return resp;
}
}
So previously I was manually updating a worker using Quick Edit from my repo whenever I did a change, against the copy in my repo. I keep full-source script in CF when doing dev, and a terser
minified version worker for production. But the synchronization from repo to cloud (CF) was by hand. Since Wrangler DOES NOT RUN on 32 bit windows, and I hate using more and more, vendor specific CLI binaries in the dev/deployment stack, with more and more INI/YAML/JSON/BASH config, this needs to be automated. But not by adding more and more deps/binaries to the stack. So where is the CF script upload API?
But those docs are very poorly written. Selecting “Request Sample: JavaScript / Fetch” and then using that boiler plate code, the lines 'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001',
or 'Content-Type': 'multipart/form-data'
, causes a 400 error.
“second-file.js”
string
Worker script.
Example:
@second-file.js;type=application/javascript+module
So, what is the value of key, “second-file.js”? a JSON? oclet/bytestring/asciistring? Web FormData has no way to send MIME types AFAIK. Is the API file extension aware or not? do I have to include “.js”?
What about metadata key? Docs say its JSON body/val, but only main_module key is documented. For example if I used CF Web Quick Edit, CF Quick Edit when using this API, metadata JSON has an undocumented “inherit_from” key with a SHA1 hex string value. What is a binding? (R2/DO/KV/env vars/be very specific) How is WASM specified?
{“main_module”: “worker.js”, “some_binding”: “stuff”}
Anyways, this code works for me for a addEventListener('fetch', event => { event.respondWith(handleRequest(event.request, event)) })
worker, a github push hook, calls a CF Worker URL, running this script above, and doing the sync. I’m posting it since I found no examples anywhere online of someone actually uploading a worker script through API. No binaries, no package managers, no shell required
Hint, don’t put empty string, or “void 0;” as your script, you will get back, “not 200” with “No event handlers were registered. This script does nothing.” error.
PS All secrets were randomized.