@ldtr/nestjs-webtransport 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -14
- package/package.json +20 -9
package/README.md
CHANGED
|
@@ -26,15 +26,10 @@ import { readFile } from "node:fs/promises"
|
|
|
26
26
|
|
|
27
27
|
@WebTransportServer({ name: "main" })
|
|
28
28
|
export class MainWebTransportServer implements WebTransportServerOptionsFactory {
|
|
29
|
-
constructor(
|
|
30
|
-
private readonly configService: ConfigService
|
|
31
|
-
) {
|
|
32
|
-
}
|
|
33
|
-
|
|
34
29
|
async options(): Promise<HttpServerInit> {
|
|
35
30
|
const cert = await readFile("./certs/cert.pem", { encoding: "utf-8" })
|
|
36
31
|
const privKey = await readFile("./certs/key.pem", { encoding: "utf-8" })
|
|
37
|
-
const secret =
|
|
32
|
+
const secret = "change-me"
|
|
38
33
|
return {
|
|
39
34
|
port: 3001,
|
|
40
35
|
host: "0.0.0.0",
|
|
@@ -48,21 +43,58 @@ export class MainWebTransportServer implements WebTransportServerOptionsFactory
|
|
|
48
43
|
|
|
49
44
|
@WebTransportGateway({ server: "main", path: "/events" })
|
|
50
45
|
export class EventsGateway implements WebTransportGatewayLifecycle {
|
|
51
|
-
onSession(session: WtSession): void {
|
|
52
|
-
|
|
46
|
+
async onSession(session: WtSession): Promise<void> {
|
|
47
|
+
console.log("new WebTransport session")
|
|
48
|
+
try {
|
|
49
|
+
// Create a unidirectional (Write/Only) stream from the server to the client.
|
|
50
|
+
const stream = await session.createStreamWO()
|
|
51
|
+
|
|
52
|
+
// This stream can be used to send data, but it cannot read data back.
|
|
53
|
+
await stream.write(new TextEncoder().encode("Hello from the server."))
|
|
54
|
+
|
|
55
|
+
await stream.closeWritable()
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error(error)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async onStreamRW(stream: WtStreamRW): Promise<void> {
|
|
62
|
+
console.log("New bidirectional stream opened by client (Read/Write).")
|
|
63
|
+
await this.readStream(stream,
|
|
64
|
+
chunk => console.log(`Received chunk: ${chunk.length} byte(s).`),
|
|
65
|
+
async () => {
|
|
66
|
+
await stream.write(new TextEncoder().encode("Server received all chunks."))
|
|
67
|
+
await stream.closeWritable()
|
|
68
|
+
}
|
|
69
|
+
)
|
|
53
70
|
}
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
72
|
+
async onStreamRO(stream: WtStreamRO): Promise<void> {
|
|
73
|
+
console.log("New unidirectional stream opened by client (Read/Only).")
|
|
74
|
+
await this.readStream(stream)
|
|
57
75
|
}
|
|
76
|
+
|
|
77
|
+
private async readStream(
|
|
78
|
+
stream: WtStreamRW | WtStreamRO,
|
|
79
|
+
onChunk?: (chunk: Uint8Array) => void | Promise<void>,
|
|
80
|
+
onClosed?: () => void | Promise<void>
|
|
81
|
+
): Promise<void> {
|
|
82
|
+
try {
|
|
83
|
+
// The stream must be read immediately to consume incoming client data.
|
|
84
|
+
for await (const chunk of stream.read())
|
|
85
|
+
void onChunk?.(chunk)
|
|
58
86
|
|
|
59
|
-
|
|
60
|
-
|
|
87
|
+
// Client closed stream
|
|
88
|
+
await onClosed?.()
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error("Failed to read stream:", error)
|
|
91
|
+
throw error
|
|
92
|
+
}
|
|
61
93
|
}
|
|
62
94
|
}
|
|
63
95
|
```
|
|
64
96
|
|
|
65
|
-
Then import them in your application with the
|
|
97
|
+
Then import them in your application with the `WebTransportModule`:
|
|
66
98
|
|
|
67
99
|
```ts
|
|
68
100
|
import { WebTransportModule } from "@ldtr/nestjs-webtransport"
|
|
@@ -70,7 +102,7 @@ import { Module } from "@nestjs/common"
|
|
|
70
102
|
|
|
71
103
|
@Module({
|
|
72
104
|
imports: [
|
|
73
|
-
|
|
105
|
+
WebTransportModule],
|
|
74
106
|
providers: [
|
|
75
107
|
EventsGateway,
|
|
76
108
|
MainWebTransportServer]
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
{
|
|
2
2
|
"name": "@ldtr/nestjs-webtransport",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "NestJS module for creating WebTransport servers and gateways.",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"files": ["dist"],
|
|
6
7
|
"main": "./dist/index.cjs",
|
|
7
|
-
"
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
8
|
+
"types": "./dist/index.d.cts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"types": "./dist/index.d.
|
|
12
|
-
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.cts",
|
|
13
12
|
"require": "./dist/index.cjs"
|
|
14
13
|
}
|
|
15
14
|
},
|
|
@@ -21,12 +20,24 @@
|
|
|
21
20
|
"@fails-components/webtransport": "^1.6.3",
|
|
22
21
|
"@fails-components/webtransport-transport-http3-quiche": "^1.6.3",
|
|
23
22
|
"reflect-metadata": "^0.1.13",
|
|
24
|
-
"tslib": "^2.3.0"
|
|
23
|
+
"tslib": "^2.3.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
25
26
|
"tsdown": "^0.22.3",
|
|
26
27
|
"typescript": "^6.0.3"
|
|
27
28
|
},
|
|
28
29
|
"peerDependencies": {
|
|
29
30
|
"@nestjs/common": "^11.0.0",
|
|
30
31
|
"@nestjs/core": "^11.0.0"
|
|
31
|
-
}
|
|
32
|
-
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"nestjs",
|
|
35
|
+
"webtransport",
|
|
36
|
+
"webtransport-server",
|
|
37
|
+
"http3",
|
|
38
|
+
"quic",
|
|
39
|
+
"gateway",
|
|
40
|
+
"module",
|
|
41
|
+
"typescript"
|
|
42
|
+
]
|
|
43
|
+
}
|