Skip to content

Commit

Permalink
⛅️ Add improved CF instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Jan 7, 2025
1 parent e5bc619 commit fe6341a
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 99 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
bun_unit_tests:
name: bun unit tests
runs-on: ubuntu-latest
env:
CF_TESTING_ACCOUNT_ID: ${{ secrets.CF_TESTING_ACCOUNT_ID }}
CF_TESTING_API_TOKEN: ${{ secrets.CF_TESTING_API_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v2
Expand Down Expand Up @@ -37,6 +40,19 @@ jobs:
bun i
bun run test did
kdjs_unit_tests_kastro:
name: kdjs unit tests, kastro
runs-on: ubuntu-latest
env:
CF_TESTING_ACCOUNT_ID: ${{ secrets.CF_TESTING_ACCOUNT_ID }}
CF_TESTING_API_TOKEN: ${{ secrets.CF_TESTING_API_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v2
- run: |
bun i
bun run test kastro
kdjs_unit_tests_others:
name: kdjs unit tests, others
runs-on: ubuntu-latest
Expand All @@ -45,7 +61,7 @@ jobs:
- uses: oven-sh/setup-bun@v2
- run: |
bun i
bun run test --filter did crypto
bun run test --filter did crypto kastro
compile_kdjs_with_kdjs:
name: Compile `kdjs` with `kdjs`
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.kdjs_isolate
.secrets.js
.vscode
*.out.js
build
Expand Down
18 changes: 18 additions & 0 deletions kastro/cloudflare/api.d.js
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;
18 changes: 18 additions & 0 deletions kastro/cloudflare/api.js
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 };
6 changes: 6 additions & 0 deletions kastro/cloudflare/cloudflare.d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @externs */

/** @const */
const cloudflare = {}

export default cloudflare;
65 changes: 0 additions & 65 deletions kastro/cloudflare/crate.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { CompressedMimes } from "../workers/mimes";

/** @const {string} */
const CloudflareV4 = "https://api.cloudflare.com/client/v4";

/** @const {!Array<string>} */
const Extensions = ['', '.br', '.gz'];

/**
* @typedef {{
* accountId: string,
* token: string
* }}
*/
const Auth = {};

/**
* @param {!Array<string>} assets
* @return {!Set<string>}
Expand Down Expand Up @@ -57,58 +46,6 @@ const uploadAssets = async (auth, namespaceId, namedAssets) => {
console.log("🌀 Uploading static files:", staticFiles);
}

/**
* @param {Auth} auth
* @param {string} name
* @param {string} code
* @param {!Array<{
* name: string,
* namespace_id: string
* }>=} kvBindings
* @return {!Promise<*>}
*/
const uploadWorker = (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(`${CloudflareV4}/accounts/${auth.accountId}/workers/scripts/${name}`, {
method: "PUT",
headers: { "authorization": `Bearer ${auth.token}` },
body: form
}).then((res) => res.json());
};

/**
* @param {Auth} auth
* @param {string} name
* @param {string} url
*/
const bindWorker = (auth, name, url) => fetch(`${CloudflareV4}/accounts/${auth.accountId}/workers/domains`, {
method: "PUT",
headers: {
"authorization": `Bearer ${auth.token}`,
"content-type": "application/json"
},
body: JSON.stringify({
"environment": "production",
"hostname": url,
"service": name
})
}).then((res) => res.json());

const deployCrate = (crateName) => import(crateName)
.then((crate) => {
Expand All @@ -117,6 +54,4 @@ const deployCrate = (crateName) => import(crateName)

export {
Auth,
bindWorker,
uploadWorker
};
Empty file added kastro/cloudflare/kvs.js
Empty file.
58 changes: 58 additions & 0 deletions kastro/cloudflare/test/workers.test.js
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
});
89 changes: 89 additions & 0 deletions kastro/cloudflare/workers.js
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;
30 changes: 0 additions & 30 deletions kastro/compiler/worker.js

This file was deleted.

1 change: 1 addition & 0 deletions kdjs/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const compile = async (params, checkFreshFn) => {
"module_resolution": "NODE",
"dependency_mode": "PRUNE",
"entry_point": /** @type {string} */(params["entry"]),
"chunk_output_type": "ES_MODULES"
};
if (params["define"])
options["define"] = /** @type {(!Array<string>|boolean|string)} */(params["define"]);
Expand Down
8 changes: 8 additions & 0 deletions kdjs/externs/node/process.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const process = {};
*/
process.exit = (exitCode) => { }

/**
* @return {string}
*/
process.cwd = () => { };

/**
* @param {string} event
* @param {function(*)} handler
Expand All @@ -16,3 +21,6 @@ process.on = (event, handler) => { }

/** @const {!Array<string>} */
process.argv;

/** @const {!Object<string, string>} */
process.env;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"devDependencies": {
"@ethereumjs/evm": "^3.1.1",
"@noble/secp256k1": "^2.1.0",
"ethers": "^6.13.4",
"@noble/secp256k1": "^2.2.2",
"ethers": "^6.13.5",
"mina-signer": "^3.0.7"
},
"dependencies": {
Expand Down
Loading

0 comments on commit fe6341a

Please sign in to comment.