-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.ts
156 lines (132 loc) · 4.13 KB
/
notification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { v5 as uuid } from "jsr:@std/uuid@^1.0.0";
import { connect } from "jsr:@nats-io/transport-deno@3.0.0-18";
import type { NatsConnection } from "jsr:@nats-io/nats-core@3.0.0-46";
const NATS_SERVERS = { servers: "127.0.0.1:4222" };
const HTTP_HOSTNAME = "0.0.0.0";
const HTTP_PORT = 8000;
const PRE = "/intercom";
const UUID_NAMESPACE = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
// -----------------------------------------------------------------------------
export function methodNotAllowed(): Response {
const body = {
error: {
message: "Method Not Allowed",
},
};
return new Response(JSON.stringify(body), {
status: 405,
headers: {
"Access-Control-Allow-Origin": "*",
},
});
}
// -----------------------------------------------------------------------------
export function notFound(): Response {
const body = {
error: {
message: "Not Found",
},
};
return new Response(JSON.stringify(body), {
status: 404,
headers: {
"Access-Control-Allow-Origin": "*",
},
});
}
// -----------------------------------------------------------------------------
export function unauthorized(): Response {
const body = {
error: {
message: "Unauthorized",
},
};
return new Response(JSON.stringify(body), {
status: 401,
headers: {
"Access-Control-Allow-Origin": "*",
},
});
}
// -----------------------------------------------------------------------------
// It returns the hardcoded pub channel for now. Auth checking will be added.
// -----------------------------------------------------------------------------
async function getChannel(req: Request): Promise<string> {
const url = new URL(req.url);
const qs = new URLSearchParams(url.search);
const user = qs.get("user");
if (!user) throw "no user";
const encoder = new TextEncoder();
if (user === "user1") {
const id = encoder.encode("f:d704f61d-fade-4641-b03a-1f211206c5b6:user1");
const userUuid = await uuid.generate(UUID_NAMESPACE, id);
return `notification.${userUuid}`;
} else if (user === "user2") {
const id = encoder.encode("f:d704f61d-fade-4641-b03a-1f211206c5b6:user2");
const userUuid = await uuid.generate(UUID_NAMESPACE, id);
return `notification.${userUuid}`;
} else {
throw "invalid user";
}
}
// -----------------------------------------------------------------------------
function createStream(channel: string): ReadableStream {
console.log(channel);
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
const sub = nc.subscribe(channel);
for await (const m of sub) {
try {
console.log(m);
const data = encoder.encode(`data: coming message\n\n`);
controller.enqueue(data);
} catch (e) {
console.error(e);
}
}
},
cancel() {
console.log("cancelled");
},
});
return stream;
}
// -----------------------------------------------------------------------------
async function notification(req: Request): Promise<Response> {
const channel = await getChannel(req);
if (!channel) return unauthorized();
const stream = createStream(channel);
const headers = {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Origin": "*",
};
return new Response(stream, { headers });
}
// -----------------------------------------------------------------------------
async function handler(req: Request): Promise<Response> {
// check method
if (req.method !== "GET") {
return methodNotAllowed();
}
const url = new URL(req.url);
const path = url.pathname;
if (path === `${PRE}/notification`) {
return await notification(req);
} else {
return notFound();
}
}
// -----------------------------------------------------------------------------
function sseServer() {
Deno.serve({
hostname: HTTP_HOSTNAME,
port: HTTP_PORT,
}, handler);
}
// -----------------------------------------------------------------------------
const nc = await connect(NATS_SERVERS) as NatsConnection;
sseServer();
await nc.closed();