forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_sent_event.ts
110 lines (105 loc) · 4.25 KB
/
server_sent_event.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*
* Provides {@linkcode ServerSentEvent} and
* {@linkcode ServerSentEventStreamTarget} which provides an interface to send
* server sent events to a browser using the DOM event model.
*
* The {@linkcode ServerSentEventStreamTarget} provides the `.asResponse()` or
* `.asResponseInit()` to provide a body and headers to the client to establish
* the event connection. This is accomplished by keeping a connection open to
* the client by not closing the body, which allows events to be sent down the
* connection and processed by the client browser.
*
* See more about Server-sent events on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
*
* ## Example
*
* ```ts
* import {
* ServerSentEvent,
* ServerSentEventStreamTarget,
* } from "https://deno.land/std@$STD_VERSION/http/unstable_server_sent_event.ts";
*
* Deno.serve({ port: 8000 }, (request) => {
* const target = new ServerSentEventStreamTarget();
* let counter = 0;
*
* // Sends an event every 2 seconds, incrementing the ID
* const id = setInterval(() => {
* const evt = new ServerSentEvent(
* "message",
* { data: { hello: "world" }, id: counter++ },
* );
* target.dispatchEvent(evt);
* }, 2000);
*
* target.addEventListener("close", () => clearInterval(id));
* return target.asResponse();
* });
* ```
*
* @module
*/
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*/
import {
ServerSentEvent as ServerSentEvent_,
type ServerSentEventInit as ServerSentEventInit_,
ServerSentEventStreamTarget as ServerSentEventStreamTarget_,
type ServerSentEventTarget as ServerSentEventTarget_,
type ServerSentEventTargetOptions as ServerSentEventTargetOptions_,
} from "./unstable_server_sent_event.ts";
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*/
export type ServerSentEventInit = ServerSentEventInit_;
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*/
export type ServerSentEventTarget = ServerSentEventTarget_;
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*/
export type ServerSentEventTargetOptions = ServerSentEventTargetOptions_;
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*
* An event which contains information which will be sent to the remote
* connection and be made available in an `EventSource` as an event. A server
* creates new events and dispatches them on the target which will then be
* sent to a client.
*
* See more about Server-sent events on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
*
* ### Example
*
* ```ts
* import {
* ServerSentEvent,
* ServerSentEventStreamTarget,
* } from "https://deno.land/std@$STD_VERSION/http/server_sent_event.ts";
*
* Deno.serve({ port: 8000 }, (request) => {
* const target = new ServerSentEventStreamTarget();
* const evt = new ServerSentEvent("message", {
* data: { hello: "world" },
* id: 1
* });
* target.dispatchEvent(evt);
* return target.asResponse();
* });
* ```
*/
export const ServerSentEvent = ServerSentEvent_;
/**
* @deprecated (will be removed in 0.209.0) Use {@linkcode ServerSentEventStream} from {@link https://deno.land/std/http/server_sent_event_stream.ts} instead.
*
* An implementation of {@linkcode ServerSentEventTarget} that provides a
* readable stream as a body of a response to establish a connection to a
* client.
*/
export const ServerSentEventStreamTarget = ServerSentEventStreamTarget_;