-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5bc619
commit fe6341a
Showing
14 changed files
with
222 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
.kdjs_isolate | ||
.secrets.js | ||
.vscode | ||
*.out.js | ||
build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @externs */ | ||
|
||
import cloudflare from "./cloudflare.d"; | ||
|
||
/** | ||
* @typedef {{ | ||
* accountId: string, | ||
* token: string | ||
* }} | ||
*/ | ||
cloudflare.Auth; | ||
|
||
/** | ||
* @typedef {{ | ||
* success: boolean | ||
* }} | ||
*/ | ||
cloudflare.Response; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import "./api.d"; | ||
|
||
/** | ||
* The keys here are deliberately different from `cloudflare.Auth` | ||
* to prevent kdjs from defensively treating Auth like an extern. | ||
* | ||
* @struct | ||
* @typedef {{ | ||
* account: string, | ||
* apiToken: string | ||
* }} | ||
*/ | ||
const Auth = {}; | ||
|
||
/** @const {string} */ | ||
const ApiV4 = "https://api.cloudflare.com/client/v4"; | ||
|
||
export { Auth, ApiV4 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @externs */ | ||
|
||
/** @const */ | ||
const cloudflare = {} | ||
|
||
export default cloudflare; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { expect, test } from "bun:test"; | ||
import process from "node:process"; | ||
import { bekle as wait } from "../../../util/promises"; | ||
import { Auth } from "../api"; | ||
import workers from "../workers"; | ||
|
||
/** | ||
* @return {!Promise<Auth>} | ||
*/ | ||
const getAuth = () => { | ||
/** @const {Auth} */ | ||
const auth = { | ||
account: process.env["CF_TESTING_ACCOUNT_ID"], | ||
apiToken: process.env["CF_TESTING_API_TOKEN"], | ||
}; | ||
|
||
const secrets = process.cwd() + "/.secrets.js"; | ||
return auth.account | ||
? Promise.resolve(auth) | ||
: import(secrets) | ||
.then((mod) => /** @type {Auth} */({ | ||
account: mod["CloudflareAuth"].accountId, | ||
apiToken: mod["CloudflareAuth"].token, | ||
})); | ||
} | ||
|
||
test("upload, fetch and delete worker", async () => { | ||
/** @const {Auth} */ | ||
const auth = await getAuth(); | ||
/** @const {string} */ | ||
const name = `test-worker-${Math.floor(1000 + Math.random() * 9000)}`; | ||
/** @const {string} */ | ||
const code = `export default {fetch(){return new Response("${name}",{headers:{"content-type":"text/plain"}})}}`; | ||
|
||
const uploadResult = await workers.upload(auth, name, code); | ||
expect(uploadResult.success).toBeTrue(); | ||
|
||
const workersDevResult = await workers.enableWorkersDev(auth, name); | ||
expect(workersDevResult.success).toBeTrue(); | ||
|
||
const maxAttempts = 5; | ||
let attempt = 0; | ||
for (; attempt < maxAttempts; ++attempt) { | ||
await wait(5000); | ||
try { | ||
const fetchResult = await fetch(`https://${name}.kimlikdao-testing.workers.dev`); | ||
if (fetchResult.status == 200) { | ||
const text = await fetchResult.text(); | ||
expect(text).toBe(name); | ||
break; | ||
} | ||
} catch (_) { } | ||
} | ||
await workers.delete(auth, name); | ||
expect(attempt).toBeLessThan(maxAttempts); | ||
}, { | ||
timeout: 15_000 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { ApiV4, Auth } from "./api"; | ||
|
||
/** | ||
* @param {Auth} auth | ||
* @param {string} name | ||
* @param {string} code | ||
* @param {!Array<{ | ||
* name: string, | ||
* namespace_id: string | ||
* }>=} kvBindings | ||
* @return {!Promise<cloudflare.Response>} | ||
*/ | ||
const upload = (auth, name, code, kvBindings) => { | ||
/** @const {string} */ | ||
const yesterday = new Date(Date.now() - 86400000).toISOString().split('T')[0]; | ||
/** @dict */ | ||
const metadata = { | ||
"main_module": "a.js", | ||
"compatibility_date": yesterday | ||
}; | ||
if (kvBindings && kvBindings.length) { | ||
for (const kv of kvBindings) | ||
kv["type"] = "kv_namespace"; | ||
metadata["bindings"] = kvBindings; | ||
} | ||
/** @const {!FormData} */ | ||
const form = new FormData(); | ||
form.append("metadata", new Blob([JSON.stringify(metadata)], { type: "application/json" })); | ||
form.append("a.js", new File([code], "a.js", { type: "application/javascript+module;charset=utf-8" })); | ||
return fetch(`${ApiV4}/accounts/${auth.account}/workers/scripts/${name}`, { | ||
method: "PUT", | ||
headers: { "authorization": `Bearer ${auth.apiToken}` }, | ||
body: form | ||
}).then((res) => res.json()); | ||
}; | ||
|
||
/** | ||
* @param {Auth} auth | ||
* @param {string} name | ||
* @param {string} url | ||
* @return {!Promise<cloudflare.Response>} | ||
*/ | ||
const bind = (auth, name, url) => fetch(`${ApiV4}/accounts/${auth.account}/workers/domains`, { | ||
method: "PUT", | ||
headers: { | ||
"authorization": `Bearer ${auth.apiToken}`, | ||
"content-type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
"environment": "production", | ||
"hostname": url, | ||
"service": name | ||
}) | ||
}).then((res) => res.json()); | ||
|
||
/** | ||
* @param {Auth} auth | ||
* @param {string} name | ||
* @return {!Promise<cloudflare.Response>} | ||
*/ | ||
const enableWorkersDev = (auth, name) => fetch(`${ApiV4}/accounts/${auth.account}/workers/services/${name}/environments/production/subdomain`, { | ||
method: "POST", | ||
headers: { | ||
"authorization": `Bearer ${auth.apiToken}`, | ||
"content-type": "application/json" | ||
}, | ||
body: JSON.stringify({ enabled: true }) | ||
}).then(res => res.json()); | ||
|
||
const workers = { | ||
upload, | ||
bind, | ||
/** | ||
* Deletes a worker by name. | ||
* | ||
* @param {Auth} auth | ||
* @param {string} name | ||
* @return {!Promise<cloudflare.Response>} | ||
*/ | ||
delete(auth, name) { | ||
return fetch(`${ApiV4}/accounts/${auth.account}/workers/scripts/${name}`, { | ||
method: "DELETE", | ||
headers: { "authorization": `Bearer ${auth.apiToken}` } | ||
}).then((res) => res.json()); | ||
}, | ||
enableWorkersDev, | ||
}; | ||
|
||
export default workers; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.