-
-
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.
chore: add cloudflare and deno playgrounds
- Loading branch information
Showing
12 changed files
with
813 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ dist | |
.eslintcache | ||
*.log* | ||
*.env* | ||
.wrangler |
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
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,44 @@ | ||
import { createHandler } from "../src/index.ts"; | ||
import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; | ||
import { DurableObject } from "cloudflare:workers"; | ||
import crossws from "crossws/adapters/cloudflare-durable"; | ||
|
||
// @ts-ignore | ||
import manifestJSON from "__STATIC_CONTENT_MANIFEST"; | ||
const assetManifest = JSON.parse(manifestJSON); | ||
|
||
const ws = crossws(createHandler()); | ||
|
||
export default { | ||
async fetch(request: Request, env: any, ctx: any) { | ||
// Handle websocket | ||
if (request.headers.get("upgrade") === "websocket") { | ||
return ws.handleUpgrade(request as any, env, ctx); | ||
} | ||
// Handle static assets | ||
try { | ||
return await getAssetFromKV( | ||
{ request, waitUntil: ctx.waitUntil.bind(ctx) }, | ||
{ | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: assetManifest, | ||
}, | ||
); | ||
} catch { | ||
const pathname = new URL(request.url).pathname; | ||
return new Response(`"${pathname}" not found`, { status: 404 }); | ||
} | ||
}, | ||
}; | ||
|
||
export class $DurableObject extends DurableObject { | ||
fetch(request) { | ||
return ws.handleDurableUpgrade(this, request); | ||
} | ||
webSocketMessage(client, message) { | ||
Check failure on line 38 in playground/cf-durable.ts GitHub Actions / ci
|
||
return ws.handleDurableMessage(this, client, message); | ||
} | ||
webSocketClose(client, code, reason, wasClean) { | ||
Check failure on line 41 in playground/cf-durable.ts GitHub Actions / ci
Check failure on line 41 in playground/cf-durable.ts GitHub Actions / ci
Check failure on line 41 in playground/cf-durable.ts GitHub Actions / ci
|
||
return ws.handleDurableClose(this, client, code, reason, wasClean); | ||
} | ||
} |
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,31 @@ | ||
import { createHandler } from "../src/index.ts"; | ||
import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; | ||
import crossws from "crossws/adapters/cloudflare"; | ||
|
||
// @ts-ignore | ||
import manifestJSON from "__STATIC_CONTENT_MANIFEST"; | ||
const assetManifest = JSON.parse(manifestJSON); | ||
|
||
const ws = crossws(createHandler()); | ||
|
||
export default { | ||
async fetch(request: Request, env: any, ctx: any) { | ||
// Handle websocket | ||
if (request.headers.get("upgrade") === "websocket") { | ||
return ws.handleUpgrade(request as any, env, ctx); | ||
} | ||
// Handle static assets | ||
try { | ||
return await getAssetFromKV( | ||
{ request, waitUntil: ctx.waitUntil.bind(ctx) }, | ||
{ | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: assetManifest, | ||
}, | ||
); | ||
} catch { | ||
const pathname = new URL(request.url).pathname; | ||
return new Response(`"${pathname}" not found`, { status: 404 }); | ||
} | ||
}, | ||
}; |
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,46 @@ | ||
import crossws from "crossws/adapters/deno"; | ||
import { createHandler } from "../src/index.ts"; | ||
|
||
const ws = crossws(createHandler()); | ||
|
||
const mimes = { | ||
".html": "text/html", | ||
".js": "text/javascript", | ||
".css": "text/css", | ||
}; | ||
|
||
Deno.serve( | ||
{ | ||
port: Number.parseInt(Deno.env.get("PORT") || "3000"), | ||
onListen: ({ port }) => { | ||
console.log(`Server running at http://localhost:${port}`); | ||
}, | ||
}, | ||
async (request, info) => { | ||
// Websocket | ||
if (request.headers.get("upgrade") === "websocket") { | ||
return ws.handleUpgrade(request, info as any); | ||
} | ||
// Static | ||
const url = new URL(request.url); | ||
for (const path of [ | ||
`public${url.pathname}`, | ||
`public${url.pathname}/index.html`, | ||
]) { | ||
const contents = await Deno.readTextFile( | ||
new URL(path, import.meta.url), | ||
).catch(() => undefined); | ||
if (contents) { | ||
const extname = path.match(/\.\w+$/)?.[0] as | ||
| keyof typeof mimes | ||
| undefined; | ||
return new Response(contents, { | ||
headers: { | ||
"content-type": extname ? mimes[extname] : "text/plain", | ||
}, | ||
}); | ||
} | ||
} | ||
return new Response("Not found", { status: 404 }); | ||
}, | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
compatibility_date = "2024-01-01" | ||
|
||
site = { bucket = "./public" } | ||
|
||
[env.worker] | ||
main = "./cf-worker.ts" | ||
|
||
[env.durable] | ||
main = "./cf-durable.ts" | ||
durable_objects.bindings = [ | ||
{ name = "$DurableObject", class_name = "$DurableObject" } | ||
] | ||
migrations = [ | ||
{ tag = "v1", new_classes = ["$DurableObject"] } | ||
] |
Oops, something went wrong.