-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
2274 lines (2025 loc) · 86.6 KB
/
index.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
Direct Sockets API
</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class="remove" defer></script>
<script class='remove'>
var respecConfig = {
specStatus: "CG-DRAFT",
github: {
repoURL: "WICG/direct-sockets",
branch: "main"
},
group: "wicg",
editors: [
{
name: "Andrew Rayskiy",
company: "Google Inc.",
companyURL: "https://google.com",
w3cid: 135463
}
],
formerEditors: [
{
name: "Eric Willigers",
company: "Google Inc.",
companyURL: "https://google.com",
w3cid: 67534
},
],
latestVersion: null,
xref: ["web-platform", "streams", "permissions-policy", "HTML", "infra", "DOM", "webidl", "isolated-contexts", "private-network-access"],
// eventually add mdn: true, and caniuse: "direct-sockets"
};
</script>
</head>
<body data-cite="Direct Sockets">
<section id="abstract">
<p>
This specification defines an API that allows web applications to talk
to servers and devices that have their own protocols incompatible with
those available on the web.
</p>
</section>
<section id="sotd">
This is a work in progress. All <a
href="https://github.com/WICG/direct-sockets">contributions</a> welcome.
</section>
<section data-dfn-for="TCPSocket">
<h2>
{{TCPSocket}} interface
</h2>
<pre class="idl">
[
Exposed=(Window,DedicatedWorker),
SecureContext,
IsolatedContext
]
interface TCPSocket {
constructor(DOMString remoteAddress,
unsigned short remotePort,
optional TCPSocketOptions options = {});
readonly attribute Promise<TCPSocketOpenInfo> opened;
readonly attribute Promise<undefined> closed;
Promise<undefined> close();
};
</pre>
<p>
Methods on this interface typically complete asynchronously, queuing work on the
<dfn>TCPSocket task source</dfn>.
<p>
Instances of {{TCPSocket}} are created with the internal slots described in the following
table:
<table class="simple" data-dfn-for="TCPSocket">
<tr>
<th>Internal slot
<th>Initial value
<th>Description (non-normative)
<tr>
<td><dfn>[[\readable]]</dfn>
<td>`null`
<td>A {{ReadableStream}} that receives data from the socket
<tr>
<td><dfn>[[\writable]]</dfn>
<td>`null`
<td>A {{WritableStream}} that transmits data to the socket
<tr>
<td><dfn>[[\openedPromise]]</dfn>
<td>`new Promise`
<td>A {{Promise}} used to wait for the socket to be opened. Corresponds to the
{{TCPSocket/opened}} member.
<tr>
<td><dfn>[[\closedPromise]]
<td>`new Promise`
<td>A {{Promise}} used to wait for the socket to close or error. Corresponds to the
{{TCPSocket/closed}} member.
</table>
<section>
<h3><dfn>constructor()</dfn> method</h3>
<aside class="example">
In order to communicate via TCP a socket connection must be requested first. The socket
object constructor
allows the site to specify the necessary parameters which control how data is transmitted
and received.
<pre class="js">
const socket = new TCPSocket(/*remoteAddress=*/, /*remotePort=*/, /*options=*/);
</pre>
The developer should wait for the {{TCPSocket/opened}} promise to be resolved to gain access
to {{TCPSocketOpenInfo/readable}} and {{TCPSocketOpenInfo/writable}} streams.
<pre class="js">
const { readable, writable } = await socket.opened;
</pre>
Once {{TCPSocket/opened}} has resolved, the {{TCPSocketOpenInfo/readable}} and
{{TCPSocketOpenInfo/writable}}
can be accessed to get the {{ReadableStream}} and {{WritableStream}} instances for receiving
data from and sending data to the socket.
</aside>
The {{TCPSocket/constructor()}} steps are:
<ol>
<li>
If [=this=]'s [=relevant global object=]'s [=associated Document=] is
not [=allowed to use=] the [=policy-controlled feature=] named "[=policy-controlled
feature/direct-sockets=]", throw a
"{{NotAllowedError}}" {{DOMException}}.
<li>
If |options|["{{TCPSocketOptions/keepAliveDelay}}"] is less than 1,000, throw a
{{TypeError}}.
<li>
If |options|["{{TCPSocketOptions/sendBufferSize}}"] is equal to 0, throw a {{TypeError}}.
<li>
If |options|["{{TCPSocketOptions/receiveBufferSize}}"] is equal to 0, throw a
{{TypeError}}.
<li>
Perform the following steps [=in parallel=].
<ol>
<li>
If |remoteAddress| resolves to an IP address belonging to the
[=IP address space/private=] network address space and [=this=]'s [=relevant global object=]'s [=associated Document=] is
not [=allowed to use=] the [=policy-controlled feature=] named "[=policy-controlled feature/direct-sockets-private=]", [=queue a global task=] on the [=relevant global
object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
[=Reject=] the {{TCPSocket/[[openedPromise]]}} with a "{{NotAllowedError}}"
{{DOMException}}.
<li>
[=Reject=] the {{TCPSocket/[[closedPromise]]}} with a "{{NotAllowedError}}"
{{DOMException}}.
</ol>
<li>
Invoke the operating system to open a TCP socket using the given |remoteAddress| and
|remotePort| and the connection parameters (or their defaults) specified in |options|.
<li>
If this fails for any other reason, [=queue a global task=] on the [=relevant global
object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
[=Reject=] the {{TCPSocket/[[openedPromise]]}} with a "{{NetworkError}}"
{{DOMException}}.
<li>
[=Reject=] the {{TCPSocket/[[closedPromise]]}} with a "{{NetworkError}}"
{{DOMException}}.
</ol>
<li>
On success, [=queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
[=initialize TCPSocket readable stream|Initialize=] {{TCPSocket/[[readable]]}}.
<li>
[=initialize TCPSocket writable stream|Initialize=] {{TCPSocket/[[writable]]}}.
<li>
Let |openInfo:TCPSocketOpenInfo| be a new {{TCPSocketOpenInfo}}.
<li>
Set |openInfo|["{{TCPSocketOpenInfo/readable}}"] to
[=this=].{{TCPSocket/[[readable]]}}.
<li>
Set |openInfo|["{{TCPSocketOpenInfo/writable}}"] to
[=this=].{{TCPSocket/[[writable]]}}.
<li>
Populate the remaining fields of |openInfo| using the information provided by the
operating system:
|openInfo|["{{TCPSocketOpenInfo/remoteAddress}}"],
|openInfo|["{{TCPSocketOpenInfo/remotePort}}"],
|openInfo|["{{TCPSocketOpenInfo/localAddress}}"] and
|openInfo|["{{TCPSocketOpenInfo/localPort}}"].
<li>
[=Resolve=] [=this=].{{TCPSocket/[[openedPromise]]}} with |openInfo|.
</ol>
</ol>
</ol>
<section data-dfn-for="TCPSocketOptions">
<h4><dfn>TCPSocketOptions</dfn> dictionary</h4>
<pre class="idl">
enum SocketDnsQueryType {
"ipv4",
"ipv6"
};
dictionary TCPSocketOptions {
[EnforceRange] unsigned long sendBufferSize;
[EnforceRange] unsigned long receiveBufferSize;
boolean noDelay = false;
[EnforceRange] unsigned long keepAliveDelay;
SocketDnsQueryType dnsQueryType;
};
</pre>
<dl>
<dt>
<dfn>sendBufferSize</dfn> member
</dt>
<dd>
The requested send buffer size, in bytes.
If not specified, then platform-specific default value will be used.
</dd>
<dt>
<dfn>receiveBufferSize</dfn> member
</dt>
<dd>
The requested receive buffer size, in bytes.
If not specified, then platform-specific default value will be used.
</dd>
<dt>
<dfn>noDelay</dfn> member
</dt>
<dd>
Enables the `TCP_NODELAY` option,
disabling <a href="https://en.wikipedia.org/wiki/Nagle%27s_algorithm">Nagle's
algorithm</a>.
<div class="note">
No-Delay is disabled by default.
</div>
</dd>
<dt>
<dfn>keepAliveDelay</dfn> member
</dt>
<dd>
If specified, enables TCP Keep-Alive by setting `SO_KEEPALIVE` option on the socket
to `true`. The way the actual delay is set is platform-specific:
<ol>
<li>
On Linux & ChromeOS `keepAliveDelay` is applied to `TCP_KEEPIDLE` and
`TCP_KEEPINTVL`;
<li>
On MacOS `keepAliveDelay` affects `TCP_KEEPALIVE`;
<li>
On Windows `keepAliveDelay` is replicated to `keepalivetime` and `keepaliveinterval`
of <a href="https://learn.microsoft.com/en-us/windows/win32/winsock/sio-keepalive-vals">`SIO_KEEPALIVE_VALS`</a>.
</ol>
<div class="note">
Keep-Alive is disabled by default.
</div>
</dd>
<dt>
<dfn>dnsQueryType</dfn> member
</dt>
<dd>
Indicates whether IPv4 or IPv6 record should be returned during DNS lookup.
If omitted, the OS will select the record type(s) to be queried automatically
depending on IPv4/IPv6 settings and reachability.
</dd>
</dl>
</section>
</section>
<section>
<h3>{{TCPSocket/[[readable]]}} attribute (internal)</h3>
<aside class="example">
An application receiving data from a TCP socket will typically use the {{ReadableStream}}
like this:
<pre class="js">
const { readable } = await socket.opened;
const reader = readable.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) {
// |reader| has been canceled.
break;
}
// Do something with |value|...
}
reader.releaseLock();
</pre>
</aside>
The steps to <dfn data-lt="initialize TCPSocket readable stream">initialize the TCPSocket
readable stream</dfn> are:
<ol>
<li>
Let |stream:ReadableStream| be a [=new=] {{ReadableStream}}.
<li>
Let |pullAlgorithm| be the following steps:
<ol>
<li>
Let |desiredSize| be the
[=ReadableStream/desired size to fill up to the high water mark=] for
[=this=].{{TCPSocket/[[readable]]}}.
<li>
If [=this=].{{TCPSocket/[[readable]]}}'s [=ReadableStream/current BYOB request view=]
is non-null, then set |desiredSize| to [=this=].{{TCPSocket/[[readable]]}}'s
[=ReadableStream/current BYOB request view=]'s [=BufferSource/byte length=].
<li>
Run the following steps in parallel:
<ol>
<li>
Invoke the operating system to read up to |desiredSize| bytes from the socket,
placing the result in the [=byte sequence=] |bytes|.
<li>
[=Queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
If the connection was closed gracefully, run the following steps:
<ol>
<li>
Invoke [=ReadableStream/close=] on [=this=].{{TCPSocket/[[readable]]}}.
<li>
Invoke the steps to [=handle closing the TCPSocket readable stream=].
</ol>
<div class="note">
This is triggered by the peer sending a packet with the FIN flag set
and is typically indicated by the operating system returning 0 bytes
when asked for more data from the socket.
</div>
<li>
If no errors were encountered, then:
<ol>
<li>
If [=this=].{{TCPSocket/[[readable]]}}'s
[=ReadableStream/current BYOB request view=] is non-null,
then [=ArrayBufferView/write=] |bytes| into
[=this=].{{TCPSocket/[[readable]]}}'s
[=ReadableStream/current BYOB request view=], and set
|view| to [=this=].{{TCPSocket/[[readable]]}}'s
[=ReadableStream/current BYOB request view=].
<li>
Otherwise, set |view| to the result of
[=ArrayBufferView/create|creating=] a {{Uint8Array}} from
|bytes| in [=this=]'s [=relevant Realm=].
<li>
[=ReadableStream/Enqueue=] |view| into
[=this=].{{TCPSocket/[[readable]]}}.
</ol>
<li>
If a network or operating system error was encountered, invoke
[=ReadableStream/error=] on [=this=].{{TCPSocket/[[readable]]}} with
a "{{NetworkError}}" {{DOMException}} and invoke the steps
to [=handle closing the TCPSocket readable stream=].
</ol>
</ol>
<li>
Return [=a promise resolved with=] `undefined`.
</ol>
<li>
Let |cancelAlgorithm| be the following steps:
<ol>
<li>
Invoke the steps to [=handle closing the TCPSocket readable stream=].
<li>
Return [=a promise resolved with=] `undefined`.
</ol>
<li>
[=ReadableStream/Set up with byte reading support=] |stream| with
<i>[=ReadableStream/set up with byte reading support/pullAlgorithm=]</i>
set to |pullAlgorithm|,
<i>[=ReadableStream/set up with byte reading support/cancelAlgorithm=]</i>
set to |cancelAlgorithm|, and
<i>[=ReadableStream/set up with byte reading support/highWaterMark=]</i>
set to an implementation-defined value.
<li>Set [=this=].{{TCPSocket/[[readable]]}} to |stream|.
</ol>
To <dfn>handle closing the TCPSocket readable stream</dfn> perform the following steps:
<ol>
<li>
If [=this=].{{TCPSocket/[[writable]]}} is active, abort these steps.
<li>
Run the following steps [=in parallel=].
<ol>
<li>
Invoke the operating system to close the socket.
<li>
[=Queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ul>
<li>
If [=this=].{{TCPSocket/[[writable]]}} is errored,
[=reject=] [=this=].{{TCPSocket/[[closedPromise]]}}
with [=this=].{{TCPSocket/[[writable]]}}.`[[storedPromise]]`.
<li>
Otherwise, if [=this=].{{TCPSocket/[[readable]]}} is errored,
[=reject=] [=this=].{{TCPSocket/[[closedPromise]]}}
with [=this=].{{TCPSocket/[[readable]]}}.`[[storedPromise]]`.
<li>
Otherwise, [=resolve=] [=this=].{{TCPSocket/[[closedPromise]]}} with `undefined`.
</ul>
</ol>
</ol>
</section>
<section>
<h3>{{TCPSocket/[[writable]]}} attribute (internal)</h3>
<aside class="example">
To write individual chunks of data to the socket a
{{WritableStreamDefaultWriter}} can be created and released as necessary.
This example uses a `TextEncoder` to encode a {{DOMString}} as the
necessary {{Uint8Array}} for transmission.
<pre class="js">
const encoder = new TextEncoder();
const { writable } = await socket.opened;
const writer = writable.getWriter();
await writer.write(encoder.encode("PING"));
writer.releaseLock();
</pre>
The {{WritableStreamDefaultWriter/write()}} method returns a {{Promise}} which
resolves when data has been written. While having some data available in
the transmit buffer is important to maintain good throughput awaiting this
{{Promise}} before generating too many chunks of data is a good practice
to avoid excessive buffering.
</aside>
The steps to <dfn data-lt="initialize TCPSocket writable stream">initialize the TCPSocket
writable stream</dfn> are:
<ol>
<li>
Let |stream:WritableStream| be a [=new=] {{WritableStream}}.
<li>
Let |signal:AbortSignal| be |stream|'s [=WritableStream/signal=].
<li>
Let |writeAlgorithm| be the following steps, given |chunk|:
<ol>
<li>
Let |promise:Promise| be [=a new promise=].
<li>
Assert: |signal| is not [=AbortSignal/aborted=].
<li>
If |chunk| cannot be [=converted to an IDL value=] of type
{{BufferSource}}, reject |promise| with a {{TypeError}} and return
|promise|. Otherwise, save the result of the conversion in
|source:BufferSource|.
<li>
[=Get a copy of the buffer source=] |source| and save the result in
|bytes|.
<li>
[=In parallel=], run the following steps:
<ol>
<li>
Invoke the operating system to write |bytes| to the socket.
<div class="note">
The operating system may return from this operation once
|bytes| has been queued for transmission rather than after it
has been transmitted.
</div>
<li>
[=Queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
If the chunk was successfully written, [=resolve=] |promise| with `undefined`.
<div class="note">
[[STREAMS]] specifies that |writeAlgorithm| will only be
invoked after the {{Promise}} returned by a previous
invocation of this algorithm has resolved. For efficiency
an implementation is allowed to resolve this {{Promise}}
early in order to coalesce multiple chunks waiting in the
{{WritableStream}}'s internal queue into a single request
to the operating system.
</div>
<li>
If a network or operating system error was encountered:
<ol>
<li>
[=Reject=] |promise| with a "{{NetworkError}}"
{{DOMException}}.
<li>
Invoke the steps to [=handle closing the TCPSocket writable
stream=].
</ol>
<li>
If |signal| is [=AbortSignal/aborted=], [=reject=] |promise|
with |signal|'s [=AbortSignal/abort reason=].
</ol>
</ol>
<li>
Return |promise|.
</ol>
<li>
Let |abortAlgorithm| be the following steps:
<ol>
<li>
Let |promise| be [=a new promise=].
<li>
Run the following steps [=in parallel=]:
<ol>
<li>
Invoke the operating system to shutdown the socket for writing.
<li>
[=Queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
Invoke the steps to [=handle closing the TCPSocket writable stream=].
<li>
[=Resolve=] |promise| with `undefined`.
</ol>
</ol>
<li>
Return |promise|.
</ol>
<li>
Let |closeAlgorithm| be the following steps:
<ol>
<li>
Let |promise| be [=a new promise=].
<li>
Run the following steps [=in parallel=].
<ol>
<li>
Invoke the operating system to shutdown the socket for writing.
<li>
[=Queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ol>
<li>
Invoke the steps to [=handle closing the TCPSocket writable stream=].
<li>
If |signal| is [=AbortSignal/aborted=], [=reject=] |promise|
with |signal|'s [=AbortSignal/abort reason=].
<li>
[=Resolve=] |promise| with `undefined`.
</ol>
</ol>
<li>
Return |promise|.
</ol>
<li>
[=WritableStream/Set up=] |stream| with
<i>[=WritableStream/set up/writeAlgorithm=]</i>
set to |writeAlgorithm|,
<i>[=WritableStream/set up/abortAlgorithm=]</i>
set to |abortAlgorithm|,
<i>[=WritableStream/set up/closeAlgorithm=]</i>
set to |closeAlgorithm|,
<i>[=WritableStream/set up/highWaterMark=]</i>
set to an implementation-defined value.
<li>
[=AbortSignal/Add=] the following abort steps to |signal|:
<ol>
<li>
Cause any invocation of the operating system to write to the
socket to return as soon as possible no matter how much data has
been written.
</ol>
<li>Set [=this=].{{TCPSocket/[[writable]]}} to |stream|.
</ol>
To <dfn>handle closing the TCPSocket writable stream</dfn> perform the following steps:
<ol>
<li>
If [=this=].{{TCPSocket/[[readable]]}} is active, abort these steps.
<li>
Run the following steps [=in parallel=].
<ol>
<li>
Invoke the operating system to close the socket.
<li>
[=Queue a global task=] on the [=relevant global object=] of
[=this=] using the [=TCPSocket task source=] to run the
following steps:
<ul>
<li>
If [=this=].{{TCPSocket/[[readable]]}} is errored,
[=reject=] [=this=].{{TCPSocket/[[closedPromise]]}}
with [=this=].{{TCPSocket/[[readable]]}}.`[[storedPromise]]`.
<li>
Otherwise, if [=this=].{{TCPSocket/[[writable]]}} is errored,
[=reject=] [=this=].{{TCPSocket/[[closedPromise]]}}
with [=this=].{{TCPSocket/[[writable]]}}.`[[storedPromise]]`.
<li>
Otherwise, [=resolve=] [=this=].{{TCPSocket/[[closedPromise]]}} with `undefined`.
</ul>
</ol>
</ol>
</section>
<section>
<h3><dfn>opened</dfn> attribute</h3>
<aside class="example">
The {{TCPSocket/opened}} is an attribute containing all information about the socket.
Once it is resolved, the developer can use the {{TCPSocketOpenInfo/readable}} and
{{TCPSocketOpenInfo/writable}}
to read from and write to the socket.
<pre class="js">
const { readable, writable } = await socket.opened;
const reader = readable.getReader();
// Read from the socket using |reader|...
const writer = writable.getWriter();
// Write to the socket using |writer|...
</pre>
</aside>
When called, returns the [=this=].{{TCPSocket/[[openedPromise]]}}.
<section data-dfn-for="TCPSocketOpenInfo">
<h4><dfn>TCPSocketOpenInfo</dfn> dictionary</h4>
<pre class="idl">
dictionary TCPSocketOpenInfo {
ReadableStream readable;
WritableStream writable;
DOMString remoteAddress;
unsigned short remotePort;
DOMString localAddress;
unsigned short localPort;
};
</pre>
<dl>
<dt>
<dfn>readable</dfn> member
</dt>
<dd>
The readable side of the socket. Set to {{TCPSocket/[[readable]]}}.
</dd>
<dt>
<dfn>writable</dfn> member
</dt>
<dd>
The writable side of the socket. Set to {{TCPSocket/[[writable]]}}.
</dd>
<dt>
<dfn>remoteAddress</dfn> member
</dt>
<dd>
Resolved remote IP address that the socket is connected to.
</dd>
<dt>
<dfn>remotePort</dfn> member
</dt>
<dd>
Remote port that the socket is connected to.
</dd>
<dt>
<dfn>localAddress</dfn> member
</dt>
<dd>
Local IP address that the socket is bound to.
</dd>
<dt>
<dfn>localPort</dfn> member
</dt>
<dd>
Local port that the socket is bound to.
</dd>
</dl>
</section>
</section>
<section>
<h3><dfn>closed</dfn> attribute</h3>
<aside class="example">
The {{TCPSocket/closed}} is an attribute that keeps track of the socket state.
It gets resolved if the socket is closed gracefully (i.e. by the user) or rejected in case
of an error.
<pre class="js">
socket.closed.then(() => console.log("Closed"), () => console.log("Errored"));
</pre>
Note that {{TCPSocket/closed}} will be automatically rejected or resolved once both
{{TCPSocket/[[readable]]}} and {{TCPSocket/[[writable]]}} reach a closed or errored state.
</aside>
When called, returns the [=this=].{{TCPSocket/[[closedPromise]]}}.
</section>
<section>
<h3><dfn>close()</dfn> method</h3>
<aside class="example">
When communication with the port is no longer required it can be closed
and the associated resources released by the system.
<p>
Calling `socket.`{{TCPSocket/close()}} implicitly invokes
`opened.`{{TCPSocketOpenInfo/readable}}`.`{{ReadableStream/cancel()}} and
`opened.`{{TCPSocketOpenInfo/writable}}`.`{{WritableStream/abort()}} in order to
clear any buffered data. If the application has called
`opened.`{{TCPSocketOpenInfo/readable}}`.`{{ReadableStream/getReader()}} or
`opened.`{{TCPSocketOpenInfo/writable}}`.`{{WritableStream/getWriter()}} the stream
is locked and the socket cannot be closed. This forces the developer to
decide how to handle any read or write operations that are in progress.
For example, to ensure that all buffered data has been transmitted before
the socket is closed the application must await the {{Promise}} returned by
`writer.`{{WritableStreamDefaultWriter/close()}}.
<pre class="js">
const encoder = new TextEncoder();
const { writable } = await socket.opened;
const writer = writable.getWriter();
writer.write(encoder.encode("A long message that will take..."));
await writer.close();
await socket.close();
</pre>
To discard any unsent data the application could instead call
`writer.`{{WritableStreamDefaultWriter/abort()}}.
If a loop is being used to read data from the socket, then it must be exited before calling
`socket`.{{TCPSocket/close()}}.
<pre class="js">
const { readable } = await socket.opened;
const reader = readable.getReader();
async function readUntilClosed() {
while (true) {
const { value, done } = await reader.read();
if (done) {
// |reader| has been canceled.
break;
}
// Do something with |value|...
}
await socket.close();
}
const read_complete = readUntilClosed();
// Sometime later...
reader.releaseLock();
await read_complete;
</pre>
</aside>
The {{TCPSocket/close()}} method steps are:
<ol>
<li>
If [=this=].{{TCPSocket/[[openedPromise]]}} is rejected or not yet resolved, [=reject=]
with
"{{InvalidStateError}}" {{DOMException}}.
<li>
If [=this=].{{TCPSocket/[[closedPromise]]}} is settled, return
[=this=].{{TCPSocket/[[closedPromise]]}}.
<li>
If [=this=].{{TCPSocket/[[readable]]}} or [=this=].{{TCPSocket/[[writable]]}} are <a
href="https://streams.spec.whatwg.org/#lock">locked</a>,
[=reject=] with "{{InvalidStateError}}" {{DOMException}}.
<li>
Let |cancelPromise:Promise| be the result of invoking
[=ReadableStream/cancel=] on [=this=].{{TCPSocket/[[readable]]}}.
<li>
Set |cancelPromise|.[[\PromiseIsHandled]] to true.
<li>
Let |abortPromise:Promise| be the result of invoking
[=WritableStream/abort=] on [=this=].{{TCPSocket/[[writable]]}}.
<li>
Set |abortPromise|.[[\PromiseIsHandled]] to true.
<li>
Return [=this=].{{TCPSocket/[[closedPromise]]}}.
</ol>
</section>
</section>
<section data-dfn-for="UDPSocket">
<h2>
{{UDPSocket}} interface
</h2>
<pre class="idl">
[
Exposed=(Window,DedicatedWorker),
SecureContext,
IsolatedContext
]
interface UDPSocket {
constructor(optional UDPSocketOptions options = {});
readonly attribute Promise<UDPSocketOpenInfo> opened;
readonly attribute Promise<undefined> closed;
Promise<undefined> close();
};
</pre>
Methods on this interface typically complete asynchronously, queuing work on the
<dfn>UDPSocket task source</dfn>.
<p>
Instances of {{UDPSocket}} are created with the internal slots described in the following
table:
<table class="simple" data-dfn-for="UDPSocket">
<tr>
<th>Internal slot
<th>Initial value
<th>Description (non-normative)
<tr>
<td><dfn>[[\readable]]</dfn>
<td>`null`
<td>A {{ReadableStream}} that receives data from the socket
<tr>
<td><dfn>[[\writable]]</dfn>
<td>`null`
<td>A {{WritableStream}} that transmits data to the socket
<tr>
<td><dfn>[[\openedPromise]]</dfn>
<td>`new Promise`
<td>A {{Promise}} used to wait for the socket to be opened. Corresponds to the
{{UDPSocket/opened}} member.
<tr>
<td><dfn>[[\closedPromise]]
<td>`new Promise`
<td>A {{Promise}} used to wait for the socket to close or error. Corresponds to the
{{UDPSocket/closed}} member.
</table>
<section>
<h3><dfn>constructor()</dfn> method</h3>
<aside class="example">
In order to communicate via UDP a socket must be opened first. The socket object constructor
allows the site to specify the necessary parameters which control how data is transmitted
and received.
<pre class="js">
const socket = new UDPSocket(/*options=*/);
</pre>
The developer should wait for the {{UDPSocket/opened}} promise to be resolved to gain access
to {{UDPSocketOpenInfo/readable}} and {{UDPSocketOpenInfo/writable}} streams.
<pre class="js">
const { readable, writable } = await socket.opened;
</pre>
Once {{UDPSocket/opened}} has resolved, the {{UDPSocketOpenInfo/readable}} and
{{UDPSocketOpenInfo/writable}}
can be accessed to get the {{ReadableStream}} and {{WritableStream}} instances for receiving
data from and sending data to the socket.
</aside>
{{UDPSocket}} can operate in either {{UDPSocket/connected}} or {{UDPSocket/bound}} <dfn>mode</dfn> which
is decided based on the provided set of constructor options.
<ul>
<li>
In <dfn>connected</dfn> {{UDPSocket/mode}}, the UDP socket is associated with a specific destination
IP address and port number. This means that any data sent using the socket is automatically
sent to the specified destination without the need to specify the address and port every
time. This {{UDPSocket/mode}} is assumed when {{UDPSocketOptions/remoteAddress}} and
{{UDPSocketOptions/remotePort}} are specified in {{UDPSocketOptions}}.
<div class="note">
This {{UDPSocket/mode}} is useful for applications that require a constant communication channel
between two endpoints, such as real-time streaming applications.
</div>
<li>
In <dfn>bound</dfn> {{UDPSocket/mode}}, the UDP socket is bound to a specific local IP address and port
number. This means that any data received on that IP address and port is delivered to the
socket. Similarly, any data sent using the socket is sent from the bound IP address and
port. This {{UDPSocket/mode}} is assumed when {{UDPSocketOptions/localAddress}} is specified in
{{UDPSocketOptions}}.
<div class="note">
This {{UDPSocket/mode}} is useful for applications that need to listen for incoming data on a
specific port or interface, such as server applications that receive incoming messages
from multiple clients.
</div>
</ul>
The {{UDPSocket/constructor()}} steps are:
<ol>
<li>
If [=this=]'s [=relevant global object=]'s [=associated Document=] is
not [=allowed to use=] the [=policy-controlled feature=] named "[=policy-controlled
feature/direct-sockets=]", throw a
"{{NotAllowedError}}" {{DOMException}}.
<li>
If only one of |options|["{{UDPSocketOptions/remoteAddress}}"] and
|options|["{{UDPSocketOptions/remotePort}}"] is specified, throw a {{TypeError}}.
<li>
Alternatively, if both |options|["{{UDPSocketOptions/remoteAddress}}"] and
|options|["{{UDPSocketOptions/remotePort}}"] are specified, assume {{UDPSocket/connected}}
{{UDPSocket/mode}}.
<li>
If |options|["{{UDPSocketOptions/localPort}}"] is equal to 0 or specified without
|options|["{{UDPSocketOptions/localAddress}}"], throw a {{TypeError}}.
<li>
If |options|["{{UDPSocketOptions/localAddress}}"] is specified:
<ol>
<li>
If {{UDPSocket/connected}} {{UDPSocket/mode}} was previously inferred, throw a {{TypeError}}.
<li>
If |options|["{{UDPSocketOptions/localAddress}}"] is not a valid IP address,
throw a {{TypeError}}.
<li>
Assume {{UDPSocket/bound}} {{UDPSocket/mode}}.
</ol>
<li>
If no {{UDPSocket/mode}} has been inferred at this point, throw a {{TypeError}}.
<li>
If |options|["{{UDPSocketOptions/dnsQueryType}}"] is specified in {{UDPSocket/bound}}
{{UDPSocket/mode}}, throw a {{TypeError}}.
<li>
If |options|["{{UDPSocketOptions/ipv6Only}}"] is specified:
<ol>
<li>
If inferred {{UDPSocket/mode}} is {{UDPSocket/connected}}, throw a {{TypeError}}.
<li>
If |options|["{{UDPSocketOptions/localAddress}}"] is not equal to the IPv6
unspecified address (`::`), throw a {{TypeError}}.
</ol>
<li>
If |options|["{{UDPSocketOptions/sendBufferSize}}"] is equal to 0, throw a {{TypeError}}.
<li>
If |options|["{{UDPSocketOptions/receiveBufferSize}}"] is equal to 0, throw a
{{TypeError}}.
<li>
Perform the following steps [=in parallel=].
<ol>
<li>
If the inferred {{UDPSocket/mode}} is {{UDPSocket/connected}} and |remoteAddress| resolves to an IP address belonging to the
[=IP address space/private=] network address space and [=this=]'s [=relevant global object=]'s [=associated Document=] is
not [=allowed to use=] the [=policy-controlled feature=] named "[=policy-controlled feature/direct-sockets-private=]", [=queue a global task=] on the [=relevant global
object=] of
[=this=] using the [=UDPSocket task source=] to run the
following steps:
<ol>
<li>