Skip to content

Commit 1d42a50

Browse files
authored
Add ready promise and close reason (#12)
* Add ready promise and close reason Adds a `socket.ready` promise that is resolved when the socket connection has been established. Adds an optional `reason` argument to `socket.close()` that is to be forwarded on to the underlying `ReadableStream` and `WritableStream` when they are canceled/aborted. * Rename `ready` to `opened`
1 parent 1a95306 commit 1d42a50

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

index.bs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const tls_socket = new TLSSocket({ key: '...', cert: '...' });
6363
tls_socket.connect();
6464
</pre>
6565

66-
Additionally, the binding object does not necessarily have to be an instance of a class, nor does it even have to be JavaScript. It can be any mechanism that exposes the {{connect()}} method. Cloudflare achieves this through [environment bindings](https://developers.cloudflare.com/workers/configuration/bindings/).
66+
Additionally, the binding object does not necessarily have to be an instance of a class, nor does it even have to be JavaScript. It can be any mechanism that exposes the {{connect()}} method. Cloudflare achieves this through [environment bindings](https://developers.cloudflare.com/workers/configuration/bindings/).
6767

6868
<h2 id="socket-section">Socket</h2>
6969

@@ -94,8 +94,10 @@ interface Socket {
9494
readonly attribute ReadableStream readable;
9595
readonly attribute WritableStream writable;
9696

97+
readonly attribute Promise&lt;undefined> opened;
98+
9799
readonly attribute Promise&lt;undefined> closed;
98-
Promise&lt;undefined> close();
100+
Promise&lt;undefined> close(optional any reason);
99101

100102
[NewObject] Socket startTls();
101103
};
@@ -154,6 +156,12 @@ The {{writable}} attribute is a {{WritableStream}} which sends data to the serve
154156
</pre>
155157
</div>
156158

159+
<h4 id="opened-attribute">opened</h4>
160+
161+
The {{opened}} attribute is a promise that is resolved when the socket connection has been
162+
successfully established, or is rejected if the connection fails. For sockets use secure-transport,
163+
the resolution of the {{opened}} promise indicates the completion of the secure handshake.
164+
157165
<h4 id="closed-attribute">closed</h4>
158166

159167
The {{closed}} attribute is a promise which can be used to keep track of the socket state. It gets resolved under the
@@ -165,7 +173,7 @@ following circumstances:
165173
</ul>
166174

167175
<div class="note">
168-
The current Cloudflare Workers implementation behaves as described above, specifically the
176+
The current Cloudflare Workers implementation behaves as described above, specifically the
169177
ReadableStream needs to be read until completion for the `closed` promise to resolve, if the
170178
ReadableStream is not read then even if the server closes the connection the `closed` promise
171179
will not resolve.
@@ -184,10 +192,14 @@ Cancelling the socket's ReadableStream and closing the socket's WritableStream d
184192

185193
<h3 id="methods">Methods</h3>
186194

187-
<h4 id="close-method">close()</h4>
195+
<h4 id="close-method">close(optional any reason)</h4>
188196

189197
The {{close()}} method closes the socket and its underlying connection. It returns the same promise as the {{closed}} attribute.
190198

199+
When called, the {{ReadableStream}} and {{WritableStream}} associated with the {{Socket}} will
200+
be canceled and aborted, respectively. If the {{reason}} argument is specified, the {{reason}}
201+
will be passed on to both the {{ReadableStream}} and {{WritableStream}}.
202+
191203
<h4 id="starttls-method">startTls()</h4>
192204

193205
The {{startTls()}} method enables opportunistic TLS (otherwise known as [StartTLS](https://en.wikipedia.org/wiki/Opportunistic_TLS)) which is a requirement for some protocols (primarily postgres/mysql and other DB protocols).
@@ -284,7 +296,7 @@ At any point during the creation of the {{Socket}} instance, `connect` may throw
284296
{{secureTransport}} member
285297
</dt>
286298
<dd>
287-
The secure transport mode to use.
299+
The secure transport mode to use.
288300
<dl>
289301
<dt>{{off}}</dt>
290302
<dd>A connection is established in plain text.</dd>

0 commit comments

Comments
 (0)