-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61adebd
commit a7dcb3e
Showing
24 changed files
with
422 additions
and
152 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
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 @@ | ||
import "./server"; |
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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>HTTP/2 - Better SSE</title> | ||
</head> | ||
<body> | ||
<script src="/index.js"></script> | ||
</body> | ||
</html> |
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,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); | ||
}); |
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,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.` | ||
); | ||
}); | ||
})(); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -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", | ||
|
@@ -17,6 +15,10 @@ | |
"tcp", | ||
"events" | ||
], | ||
"engines": { | ||
"node": ">=12", | ||
"pnpm": ">=9" | ||
}, | ||
"scripts": { | ||
"build": "webpack --env production", | ||
"test": "vitest", | ||
|
@@ -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", | ||
|
Oops, something went wrong.