Skip to content

Commit 03555d5

Browse files
committed
init demo-cdn
1 parent ccf3e0d commit 03555d5

File tree

6 files changed

+181
-6
lines changed

6 files changed

+181
-6
lines changed

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@jsr:registry=https://npm.jsr.io

demo-cdn/index.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>PulseBeam Demo - CDN</title>
7+
</head>
8+
<body>
9+
<h1>Hello World</h1>
10+
<script type="importmap">
11+
{
12+
"imports": {
13+
"@pulsebeam/peer": "https://esm.sh/jsr/@pulsebeam/[email protected]"
14+
}
15+
}
16+
</script>
17+
<script type="module">
18+
import { createPeer } from "@pulsebeam/peer";
19+
20+
21+
</script>
22+
</body>
23+
</html>
24+

demo-cdn/main.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import handler from "serve-handler";
2+
import http from "http";
3+
import { App, FirewallClaims, PeerClaims } from "@pulsebeam/server/node";
4+
5+
// default values are only used for testing only!!
6+
const appId = process.env["PULSEBEAM_APP_ID"] || "app_e66Jb4zkt66nvlUKMRTSZ";
7+
const appSecret = process.env["PULSEBEAM_APP_SECRET"] ||
8+
"sk_7317736f8a8d075a03cdea6b6b76094ae424cbf619a8e9273e633daed3f55c38";
9+
const app = new App(appId, appSecret);
10+
11+
const server = http.createServer((request, response) => {
12+
const url = new URL(request.url, `http://${request.headers.host}`);
13+
if (url.pathname === "/auth") {
14+
const groupId = url.searchParams.get("groupId");
15+
const peerId = url.searchParams.get("peerId");
16+
17+
if (!groupId || !peerId) {
18+
response.writeHead(400, { "Content-Type": "text/plain" });
19+
response.end("Bad Request - groupId and peerId are required");
20+
return;
21+
}
22+
23+
const claims = new PeerClaims(groupId, peerId);
24+
const rule = new FirewallClaims("*", "*");
25+
claims.setAllowIncoming0(rule);
26+
claims.setAllowOutgoing0(rule);
27+
28+
const token = app.createToken(claims, 3600);
29+
response.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
30+
response.end(token);
31+
return;
32+
}
33+
34+
// You pass two more arguments for config and middleware
35+
// More details here: https://github.com/vercel/serve-handler#options
36+
return handler(request, response);
37+
});
38+
39+
server.listen(3000, () => {
40+
console.log("Running at http://localhost:3000");
41+
});

demo-cdn/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@pulsebeam/demo-cdn",
3+
"version": "1.0.0",
4+
"main": "main.js",
5+
"type": "module",
6+
"scripts": {
7+
"start": "node --experimental-vm-modules --experimental-wasm-modules main.js"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"description": "",
13+
"dependencies": {
14+
"@pulsebeam/server": "npm:@jsr/pulsebeam__server@^0.0.8",
15+
"serve-handler": "^6.1.6"
16+
}
17+
}

package-lock.json

+97-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"workspaces": [
33
"peer",
44
"demo-react",
5+
"demo-cdn",
56
"e2e"
67
]
78
}

0 commit comments

Comments
 (0)