Skip to content

Commit

Permalink
durable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad committed Mar 21, 2024
1 parent ae603c0 commit 90cb524
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 109 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
}
},
"dependencies": {
"stackblitz-cr": "workspace:^"
"stackblitz-cr": "workspace:^",
"wrangler": "^3.36.0"
}
}
5 changes: 4 additions & 1 deletion packages/backend/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { R2Bucket } from '@cloudflare/workers-types';
import {fileURLToPath} from 'url'
import { DurableObject, R2Bucket } from '@cloudflare/workers-types';
import ncb from 'nitro-cloudflare-dev'

declare module "nitro-cloudflare-dev" {
interface Env {
PKGS: R2Bucket
WORKFLOWS: DurableObject
}
}

Expand All @@ -20,6 +22,7 @@ export default defineNitroConfig({
preset: 'cloudflare-pages',
modules: [ncb],
srcDir: "server",
entry: fileURLToPath(new URL('./preset/entry.ts', import.meta.url)),

runtimeConfig: {
appId: "",
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DurableObjectState } from '@cloudflare/workers-types';
import type { Env } from 'nitro-cloudflare-dev';

export class Workflows {
constructor(state: DurableObjectState, env: Env) {
}

async fetch(request: Request) {
return new Response("Hello World");
}
}
4 changes: 4 additions & 0 deletions packages/backend/preset/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import entry from '#internal/nitro/entries/cloudflare-pages'

export * from '../objects'
export default entry
7 changes: 7 additions & 0 deletions packages/backend/preset/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type NitroPreset } from "nitropack";
import { fileURLToPath } from "url";

export default <NitroPreset>{
extends: "cloudflare-pages",
entry: fileURLToPath(new URL('./entry.ts', import.meta.url)),
};
10 changes: 9 additions & 1 deletion packages/backend/server/routes/webhook.post.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {objectHash} from 'ohash'
import { App } from "octokit";

export default eventHandler(async (event) => {
Expand All @@ -10,7 +11,14 @@ export default eventHandler(async (event) => {
},
});
app.webhooks.on("workflow_job.queued", ({ octokit, payload }) => {
console.log(payload.workflow_job.node_id)
const token = {
url: payload.workflow_job.url,
attempt: payload.workflow_job.run_attempt,
actor: payload.sender.id
}
const hashedToken = objectHash(token)
console.log(token)
console.log(hashedToken)
});

type EmitterWebhookEvent = Parameters<typeof app.webhooks.receive>[0]
Expand Down
14 changes: 12 additions & 2 deletions packages/backend/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# compatibility_date = "2023-12-01"
# main = "./.output/server/index.mjs"
# workers_dev = true
# compatibility_date = "2023-12-01"
compatibility_date = "2024-03-14"

# rules = [
# { type = "ESModule", globs = ["**/*.js", "**/*.mjs"]},
Expand All @@ -13,4 +13,14 @@

[[r2_buckets]]
binding = 'PKGS' # <~ valid JavaScript variable name
bucket_name = 'PACKAGES'
bucket_name = 'Packages'

[durable_objects]
bindings = [
{ name = "WORKFLOWS", class_name = "Workflows" }
]

[[migrations]]
tag = "v1" # Should be unique for each entry
new_classes = ["Workflows"]

16 changes: 8 additions & 8 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineCommand, runMain } from "citty";
// import { createRequire } from "module";
import { version } from "./package.json";
import {hash, objectHash} from 'ohash'
import {objectHash} from 'ohash'
import { Octokit } from "@octokit/action";
import "./environments";

Expand All @@ -11,11 +11,10 @@ if (process.env.GITHUB_ACTIONS !== 'true') {
}

const {GITHUB_SERVER_URL, GITHUB_REPOSITORY, GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, GITHUB_ACTOR_ID} = process.env
const octokit = new Octokit();
const eventPayload = await import(process.env.GITHUB_EVENT_PATH, {
with: { type: "json" },
});

// const octokit = new Octokit();
// const eventPayload = await import(process.env.GITHUB_EVENT_PATH, {
// with: { type: "json" },
// });

// Note: If you need to use a workflow run's URL from within a job, you can combine these variables: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
const url = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}`
Expand All @@ -26,8 +25,9 @@ const token = {
actor: GITHUB_ACTOR_ID
}

console.log(token)
console.log(objectHash(token))
const hashedToken = objectHash(token)

console.log(hashedToken)

// console.log(octokit)
// console.log(eventPayload)
Expand Down
Loading

0 comments on commit 90cb524

Please sign in to comment.