-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
accept-sse.ts
69 lines (67 loc) · 1.81 KB
/
accept-sse.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
import { STATUS_TEXT } from "https://deno.land/std@0.104.0/http/http_status.ts";
import { HttpContext } from "../models/http-context.ts";
import { assert } from "../deps_test.ts";
export async function acceptSSE(
context: HttpContext,
): Promise<unknown> {
return;
}
// TODO(native)
// export async function acceptSSE(
// context: HttpContext,
// ): Promise<(data: string, retry?: number) => Promise<number>> {
// const { w: bufWriter } = context.request.serverRequest;
//
// const headers = context.response.headers;
//
// headers.append("Content-Type", "text/event-stream");
// headers.append("Connection", "keep-alive");
// headers.append("Cache-Control", "no-cache");
//
// const protoMajor = 1;
// const protoMinor = 1;
// const statusCode = context.response.status || 200;
// const statusText = STATUS_TEXT.get(statusCode);
//
// let out = `HTTP/${protoMajor}.${protoMinor} ${statusCode} ${statusText}\r\n`;
//
// for (const [key, value] of headers) {
// out += `${key}: ${value}\r\n`;
// }
// out += "\r\n";
//
// const header = new TextEncoder().encode(out);
// let n = await bufWriter.write(header);
// assert(header.byteLength == n);
//
// const encoder = new TextEncoder();
// let id = 0;
//
// async function send(data: string, retry: number = 1000): Promise<number> {
// const _data = data.split("\n");
//
// let msg: string = "";
//
// msg += `retry: ${retry}\n`;
//
// _data.forEach((str) => {
// msg += `data: ${str}\n`;
// });
//
// msg += `id: ${id}\n\n`;
//
// const body = encoder.encode(msg);
//
// try {
// const n = await bufWriter.write(body);
// await bufWriter.flush();
// assert(n == body.byteLength);
// id++;
// return n;
// } catch (err) {
// return -1;
// }
// }
//
// return send;
// }