Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewWid committed Oct 1, 2024
1 parent 61adebd commit a7dcb3e
Show file tree
Hide file tree
Showing 24 changed files with 422 additions and 152 deletions.
12 changes: 7 additions & 5 deletions examples/getting-started/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from "path";
import express from "express";
import {createSession, Session} from "better-sse";
import * as SSE from "better-sse/http2";

console.log(SSE);

const app = express();

Expand All @@ -11,7 +13,7 @@ app.use(express.static(path.resolve(__dirname, "./public")));
*/
declare module "express-serve-static-core" {
interface Response {
sse: Session;
// sse: Session;
}
}

Expand All @@ -21,17 +23,17 @@ app.get(
* Attach the session instance to the response.
*/
async (req, res, next) => {
const session = await createSession(req, res);
// const session = await createSession(req, res);

res.sse = session;
// res.sse = session;

next();
},
/**
* Push a message with the event name "ping".
*/
(_, res) => {
res.sse.push("Hello world!", "ping");
// res.sse.push("Hello world!", "ping");
}
);

Expand Down
1 change: 1 addition & 0 deletions examples/http2-compat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./server";
10 changes: 10 additions & 0 deletions examples/http2-compat/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>HTTP/2 - Better SSE</title>
</head>
<body>
<script src="/index.js"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/http2-compat/public/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const eventSource = new EventSource("/sse");

eventSource.addEventListener("ping", (event) => {
const {type, data} = event;

const element = document.createElement("pre");

element.innerText = `Got '${type}' event: ${data}.`;

document.body.appendChild(element);
});
69 changes: 69 additions & 0 deletions examples/http2-compat/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {resolve} from "path";
import {promisify} from "util";
import {createSecureServer} from "http2";
import {
CertificateCreationOptions,
CertificateCreationResult,
createCertificate as createCertificateCallback,
} from "pem";
import {Http2CompatSession} from "better-sse";

(async () => {
const createCertificate = promisify<
CertificateCreationOptions,
CertificateCreationResult
>(createCertificateCallback);

const indexHtmlPath = resolve(__dirname, "./public/index.html");
const indexJsPath = resolve(__dirname, "./public/index.js");

const {serviceKey: key, certificate: cert} = await createCertificate({
selfSigned: true,
days: 1,
});

const server = createSecureServer({key, cert}, async (req, res) => {
const {":path": path, ":method": method} = req.headers;
const {stream} = res;

if (method !== "GET") {
stream.respond({":status": 405});
stream.end();
return;
}

switch (path) {
case "/": {
stream.respondWithFile(indexHtmlPath);
break;
}
case "/index.js": {
stream.respondWithFile(indexJsPath);
break;
}
case "/sse": {
const session = new Http2CompatSession(req, res);

session.once("connected", () => {
session.push("Hello world", "ping");
});

break;
}
default: {
stream.respond({":status": 404});
stream.end();
}
}
});

server.on("error", console.error);

const PORT = process.env.PORT ?? 8443;

server.listen(PORT, () => {
console.log(
`Server listening. Open https://localhost:${PORT} in your browser.`
);
});
})();
16 changes: 10 additions & 6 deletions examples/http2/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CertificateCreationResult,
createCertificate as createCertificateCallback,
} from "pem";
import {createSession} from "better-sse";
import {Http2Session} from "better-sse";

(async () => {
const createCertificate = promisify<
Expand All @@ -22,9 +22,10 @@ import {createSession} from "better-sse";
days: 1,
});

const server = createSecureServer({key, cert}, async (req, res) => {
const {":path": path, ":method": method} = req.headers;
const {stream} = res;
const server = createSecureServer({key, cert});

server.on("stream", (stream, headers) => {
const {":path": path, ":method": method} = headers;

if (method !== "GET") {
stream.respond({":status": 405});
Expand All @@ -42,14 +43,17 @@ import {createSession} from "better-sse";
break;
}
case "/sse": {
const session = await createSession(req, res);
const session = new Http2Session(stream, headers);

session.push("Hello world", "ping");
session.once("connected", () => {
session.push("Hello world", "ping");
});

break;
}
default: {
stream.respond({":status": 404});
stream.end();
}
}
});
Expand Down
39 changes: 30 additions & 9 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"benchmark": "^2.1.4",
"better-sse": "^0.12.1",
"better-sse": "file:..",
"easy-server-sent-events": "^1.0.14",
"eventsource": "^2.0.2",
"express": "^4.19.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "ES2019",
"strict": true,
"outDir": "./build",
"moduleResolution": "node",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"name": "better-sse",
"description": "Dead simple, dependency-less, spec-compliant server-sent events implementation for Node, written in TypeScript.",
"version": "0.13.0",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"license": "MIT",
"author": "Matthew W. <[email protected]>",
"repository": "github:MatthewWid/better-sse",
Expand All @@ -17,6 +15,10 @@
"tcp",
"events"
],
"engines": {
"node": ">=12",
"pnpm": ">=9"
},
"scripts": {
"build": "webpack --env production",
"test": "vitest",
Expand All @@ -25,14 +27,16 @@
"lint": "eslint \"./src/**/*.ts\"",
"prepublishOnly": "npm-run-all clean format test build"
},
"main": "./build/http1.js",
"types": "./build/adapters/http1/index.d.ts",
"exports": {
".": "./build/http1.js",
"./http2": "./build/http2.js"
},
"files": [
"build",
"!build/**/*.map"
],
"engines": {
"node": ">=12",
"pnpm": ">=9"
},
"devDependencies": {
"@types/eventsource": "^1.1.11",
"@types/express": "^4.17.16",
Expand Down
Loading

0 comments on commit a7dcb3e

Please sign in to comment.