-
Notifications
You must be signed in to change notification settings - Fork 1
/
readable_stream_reader.ts
146 lines (130 loc) · 4.17 KB
/
readable_stream_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
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
import {
IsReadableStream,
IsReadableStreamLocked,
ReadableStream,
ReadableStreamCancel,
ReadableStreamCreateReadResult,
ReadableStreamReadResult
} from "./readable_stream.ts";
import {defer, Defer} from "./defer.ts";
import {Assert} from "./util.ts";
import {ReadableStreamBYOBReader} from "./readable_stream_byob_reader.ts";
export interface ReadableStreamReader<T = any> {
readonly closed: Promise<any>;
cancel(reason?): Promise<void>;
read(view?: ArrayBufferView): Promise<ReadableStreamReadResult<T>>;
releaseLock(): Promise<any>;
}
export class ReadableStreamDefaultReader<T = any>
implements ReadableStreamReader<T> {
readRequests: { promise: Defer<any>; forAuthorCode }[];
constructor(stream: ReadableStream) {
if (!IsReadableStream(stream)) {
throw new TypeError();
}
if (IsReadableStreamLocked(stream)) {
throw new TypeError();
}
ReadableStreamReaderGenericInitialize(this, stream);
this.readRequests = [];
}
get closed(): Promise<undefined> {
if (!IsReadableStreamDefaultReader(this)) {
return Promise.reject(new TypeError());
}
return this.closedPromise;
}
closedPromise: Defer<undefined>;
ownerReadableStream: ReadableStream;
cancel(reason?): Promise<void> {
if (!IsReadableStreamDefaultReader(this)) {
return Promise.reject(new TypeError());
}
if (this.ownerReadableStream === void 0) {
return Promise.reject(new TypeError());
}
return ReadableStreamReaderGenericCancel(this, reason);
}
read(): Promise<ReadableStreamReadResult<T>> {
if (!IsReadableStreamDefaultReader(this)) {
return Promise.reject(new TypeError());
}
if (this.ownerReadableStream === void 0) {
return Promise.reject(new TypeError());
}
return ReadableStreamDefaultReaderRead(this, true);
}
releaseLock() {
if (!IsReadableStreamDefaultReader(this)) {
return Promise.reject(new TypeError());
}
if (this.ownerReadableStream === void 0) {
return Promise.reject(new TypeError());
}
if (this.readRequests.length > 0) {
throw new TypeError();
}
ReadableStreamReaderGenericRelease(this);
}
}
export function IsReadableStreamDefaultReader<T>(
a
): a is ReadableStreamDefaultReader<T> {
return typeof a === "object" && a.hasOwnProperty("readRequests");
}
export function ReadableStreamReaderGenericCancel<T>(
reader: ReadableStreamBYOBReader | ReadableStreamDefaultReader<T>,
reason
) {
const stream = reader.ownerReadableStream;
Assert(stream !== void 0);
return ReadableStreamCancel(stream, reason);
}
export function ReadableStreamReaderGenericInitialize<T>(
reader: ReadableStreamBYOBReader | ReadableStreamDefaultReader<T>,
stream: ReadableStream
) {
reader.ownerReadableStream = stream;
stream.reader = reader;
if (stream.state === "readable") {
reader.closedPromise = defer();
} else if (stream.state === "closed") {
reader.closedPromise = defer();
reader.closedPromise.resolve(void 0);
} else {
Assert(stream.state === "errored");
reader.closedPromise = defer();
reader.closedPromise.reject(stream.storedError);
}
}
export function ReadableStreamReaderGenericRelease<T>(
reader: ReadableStreamBYOBReader | ReadableStreamDefaultReader<T>
) {
Assert(reader.ownerReadableStream !== void 0);
Assert(reader.ownerReadableStream.reader === reader);
if (reader.ownerReadableStream.state === "readable") {
reader.closedPromise.reject(new TypeError());
} else {
reader.closedPromise.reject(new TypeError());
}
reader.ownerReadableStream.reader = void 0;
reader.ownerReadableStream = void 0;
}
export function ReadableStreamDefaultReaderRead<T>(
reader: ReadableStreamDefaultReader<T>,
forAuthorCode: boolean = false
): Promise<{ value; done: boolean }> {
const stream = reader.ownerReadableStream;
Assert(stream !== void 0);
stream.disturbed = true;
if (stream.state === "closed") {
return Promise.resolve(
ReadableStreamCreateReadResult(void 0, true, forAuthorCode)
);
}
if (stream.state === "errored") {
return Promise.reject(stream.storedError);
}
Assert(stream.state === "readable");
return stream.readableStreamController.PullSteps(forAuthorCode);
}