-
Notifications
You must be signed in to change notification settings - Fork 1
/
readable_stream_byob_reader.ts
115 lines (106 loc) · 3.22 KB
/
readable_stream_byob_reader.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
import {Defer} from "./defer.ts";
import {
IsReadableStream,
IsReadableStreamLocked,
ReadableStream,
ReadableStreamReadResult
} from "./readable_stream.ts";
import {Assert, isArrayBufferView} from "./util.ts";
import {
ReadableStreamReader,
ReadableStreamReaderGenericCancel,
ReadableStreamReaderGenericInitialize,
ReadableStreamReaderGenericRelease
} from "./readable_stream_reader.ts";
import {
IsReadableByteStreamController,
ReadableByteStreamControllerPullInto,
ReadableByteStreamController
} from "./readable_byte_stream_controller.ts";
export class ReadableStreamBYOBReader
implements ReadableStreamReader<ArrayBufferView> {
readIntoRequests: { promise: Defer<any>; forAuthorCode: boolean }[];
constructor(stream: ReadableStream) {
if (!IsReadableStream(stream)) {
throw new TypeError();
}
if (!IsReadableByteStreamController(stream.readableStreamController)) {
throw new TypeError();
}
if (IsReadableStreamLocked(stream)) {
throw new TypeError();
}
ReadableStreamReaderGenericInitialize(this, stream);
this.readIntoRequests = [];
}
get closed(): Promise<undefined> {
if (!IsReadableStreamBYOBReader(this)) {
return Promise.reject(new TypeError());
}
return this.closedPromise;
}
closedPromise: Defer<undefined>;
ownerReadableStream: ReadableStream;
cancel(reason): Promise<undefined> {
if (!IsReadableStreamBYOBReader(this)) {
return Promise.reject(new TypeError());
}
if (this.ownerReadableStream === void 0) {
return Promise.reject(new TypeError());
}
return ReadableStreamReaderGenericCancel(this, reason);
}
read<T extends ArrayBufferView>(
view: T
): Promise<ReadableStreamReadResult<T>> {
if (!IsReadableStreamBYOBReader(this)) {
return Promise.reject(new TypeError());
}
if (this.ownerReadableStream === void 0) {
return Promise.reject(new TypeError());
}
if (typeof view !== "object") {
return Promise.reject(new TypeError());
}
if (!isArrayBufferView(view)) {
throw new TypeError("view is not ArrayBufferView: " + view);
}
return ReadableStreamBYOBReaderRead(this, view, true);
}
releaseLock() {
if (!IsReadableStreamBYOBReader(this)) {
return Promise.reject(new TypeError());
}
if (this.ownerReadableStream === void 0) {
return Promise.reject(new TypeError());
}
if (this.readIntoRequests.length > 0) {
throw new TypeError();
}
ReadableStreamReaderGenericRelease(this);
}
}
export function IsReadableStreamBYOBReader(a): a is ReadableStreamBYOBReader {
return typeof a === "object" && a.hasOwnProperty("readIntoRequests");
}
export function ReadableStreamBYOBReaderRead(
reader: ReadableStreamBYOBReader,
view: ArrayBufferView,
forAuthorCode?: boolean
) {
if (forAuthorCode === void 0) {
forAuthorCode = false;
}
const stream = reader.ownerReadableStream;
Assert(stream !== void 0);
stream.disturbed = true;
if (stream.state === "errored") {
return Promise.reject(stream.storedError);
}
Assert(stream.state === "readable");
return ReadableByteStreamControllerPullInto(
stream.readableStreamController as ReadableByteStreamController,
view,
forAuthorCode
);
}