@everyonesoftware/common 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +54 -36
- package/package.json +6 -2
- package/sources/asyncIterator.ts +436 -436
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +47 -47
- package/sources/asyncResult.ts +95 -95
- package/sources/byteList.ts +201 -201
- package/sources/byteListStream.ts +120 -120
- package/sources/byteReadStream.ts +23 -23
- package/sources/byteWriteStream.ts +15 -15
- package/sources/characterList.ts +194 -194
- package/sources/characterListStream.ts +150 -150
- package/sources/characterReadStream.ts +80 -80
- package/sources/characterReadStreamIterator.ts +127 -127
- package/sources/concatenateIterable.ts +118 -118
- package/sources/concatenateIterator.ts +164 -164
- package/sources/currentProcess.ts +157 -157
- package/sources/dateTime.ts +129 -129
- package/sources/depthFirstSearch.ts +229 -229
- package/sources/flatMapIterable.ts +103 -103
- package/sources/flatMapIterator.ts +151 -151
- package/sources/generator.ts +250 -250
- package/sources/index.ts +1 -1
- package/sources/iterator.ts +480 -480
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +123 -123
- package/sources/javascriptSetSet.ts +133 -133
- package/sources/listQueue.ts +61 -61
- package/sources/listStack.ts +61 -61
- package/sources/luxonDateTime.ts +108 -108
- package/sources/mapAsyncIterator.ts +140 -140
- package/sources/mutableMap.ts +291 -291
- package/sources/node.ts +36 -36
- package/sources/promiseAsyncResult.ts +173 -173
- package/sources/queue.ts +48 -48
- package/sources/recreationDotGovClient.ts +258 -258
- package/sources/searchControl.ts +41 -41
- package/sources/set.ts +243 -243
- package/sources/skipAsyncIterator.ts +144 -144
- package/sources/stack.ts +47 -47
- package/sources/syncResult.ts +299 -299
- package/sources/takeAsyncIterator.ts +140 -140
- package/sources/whereAsyncIterator.ts +142 -142
- package/sources/wonderlandTrailClient.ts +1502 -1502
- package/tests/assertTestTests.ts +74 -74
- package/tests/byteListStreamTests.ts +389 -389
- package/tests/byteListTests.ts +26 -26
- package/tests/characterListStreamTests.ts +390 -390
- package/tests/characterListTests.ts +249 -249
- package/tests/dateTimeTests.ts +29 -29
- package/tests/depthFirstSearchTests.ts +105 -105
- package/tests/generatorTests.ts +85 -85
- package/tests/mutableMapTests.ts +153 -153
- package/tests/promiseAsyncResultTests.ts +687 -687
- package/tests/queueTests.ts +28 -28
- package/tests/recreationDotGovClientTests.ts +190 -190
- package/tests/setTests.ts +139 -139
- package/tests/stackTests.ts +65 -65
- package/tests/syncResultTests.ts +1250 -1250
- package/tests/wonderlandTrailClientTests.ts +451 -451
- package/tsup.config.ts +12 -12
package/sources/byteList.ts
CHANGED
|
@@ -1,202 +1,202 @@
|
|
|
1
|
-
import { EqualFunctions } from "./equalFunctions";
|
|
2
|
-
import { Iterable } from "./iterable";
|
|
3
|
-
import { Iterator } from "./iterator";
|
|
4
|
-
import { JavascriptIterable, JavascriptIterator } from "./javascript";
|
|
5
|
-
import { List } from "./list";
|
|
6
|
-
import { PreCondition } from "./preCondition";
|
|
7
|
-
import { SyncResult } from "./syncResult";
|
|
8
|
-
import { ToStringFunctions } from "./toStringFunctions";
|
|
9
|
-
import { Type } from "./types";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A {@link List} of bytes.
|
|
13
|
-
*/
|
|
14
|
-
export class ByteList implements List<number>
|
|
15
|
-
{
|
|
16
|
-
private bytes: Uint8Array;
|
|
17
|
-
private count: number;
|
|
18
|
-
|
|
19
|
-
private constructor()
|
|
20
|
-
{
|
|
21
|
-
this.bytes = new Uint8Array(0);
|
|
22
|
-
this.count = 0;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public static create(initialValues?: JavascriptIterable<number>): ByteList
|
|
26
|
-
{
|
|
27
|
-
const result: ByteList = new ByteList();
|
|
28
|
-
if (initialValues)
|
|
29
|
-
{
|
|
30
|
-
result.addAll(initialValues);
|
|
31
|
-
}
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public add(value: number): this
|
|
36
|
-
{
|
|
37
|
-
return List.add(this, value);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public addAll(values: JavascriptIterable<number>): this
|
|
41
|
-
{
|
|
42
|
-
return List.addAll(this, values);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public insert(index: number, value: number): this
|
|
46
|
-
{
|
|
47
|
-
PreCondition.assertInsertIndex(index, this.getCount().await(), "index");
|
|
48
|
-
PreCondition.assertByte(value, "value");
|
|
49
|
-
|
|
50
|
-
if (this.count < this.bytes.length)
|
|
51
|
-
{
|
|
52
|
-
if (index < this.count)
|
|
53
|
-
{
|
|
54
|
-
this.bytes.copyWithin(index + 1, index, this.count);
|
|
55
|
-
}
|
|
56
|
-
this.bytes[index] = value;
|
|
57
|
-
}
|
|
58
|
-
else
|
|
59
|
-
{
|
|
60
|
-
const newCapacity: number = (this.bytes.length * 2) + 1;
|
|
61
|
-
const newBytes: Uint8Array = new Uint8Array(newCapacity);
|
|
62
|
-
if (index > 0)
|
|
63
|
-
{
|
|
64
|
-
newBytes.set(this.bytes.subarray(0, index));
|
|
65
|
-
}
|
|
66
|
-
newBytes[index] = value;
|
|
67
|
-
if (index < this.count)
|
|
68
|
-
{
|
|
69
|
-
newBytes.set(this.bytes.subarray(index), index + 1);
|
|
70
|
-
}
|
|
71
|
-
this.bytes = newBytes;
|
|
72
|
-
}
|
|
73
|
-
this.count++;
|
|
74
|
-
|
|
75
|
-
return this;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public insertAll(index: number, values: JavascriptIterable<number>): this
|
|
79
|
-
{
|
|
80
|
-
return List.insertAll(this, index, values);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
public remove(value: number, equalFunctions?: EqualFunctions): SyncResult<number>
|
|
84
|
-
{
|
|
85
|
-
PreCondition.assertByte(value, "value");
|
|
86
|
-
|
|
87
|
-
return List.remove(this, value, equalFunctions);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public removeAt(index: number): SyncResult<number>
|
|
91
|
-
{
|
|
92
|
-
PreCondition.assertAccessIndex(index, this.count, "index");
|
|
93
|
-
|
|
94
|
-
const result: number = this.bytes[index];
|
|
95
|
-
this.bytes.copyWithin(index, index + 1, this.count);
|
|
96
|
-
this.count--;
|
|
97
|
-
|
|
98
|
-
return SyncResult.value(result);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public removeFirst(): SyncResult<number>
|
|
102
|
-
{
|
|
103
|
-
return List.removeFirst(this);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public removeLast(): SyncResult<number>
|
|
107
|
-
{
|
|
108
|
-
return List.removeLast(this);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
public set(index: number, value: number): this
|
|
112
|
-
{
|
|
113
|
-
PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
|
|
114
|
-
PreCondition.assertByte(value, "value");
|
|
115
|
-
|
|
116
|
-
this.bytes[index] = value;
|
|
117
|
-
|
|
118
|
-
return this;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public iterate(): Iterator<number>
|
|
122
|
-
{
|
|
123
|
-
return Iterator.create(this.bytes[Symbol.iterator]().take(this.count));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public get(index: number): SyncResult<number>
|
|
127
|
-
{
|
|
128
|
-
PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
|
|
129
|
-
|
|
130
|
-
return SyncResult.value(this.bytes.at(index)!);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
public toArray(): SyncResult<number[]>
|
|
134
|
-
{
|
|
135
|
-
return List.toArray(this);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
public any(): SyncResult<boolean>
|
|
139
|
-
{
|
|
140
|
-
return this.getCount().then(count => count > 0);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public getCount(): SyncResult<number>
|
|
144
|
-
{
|
|
145
|
-
return SyncResult.value(this.count);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public equals(right: JavascriptIterable<number>, equalFunctions?: EqualFunctions): SyncResult<boolean>
|
|
149
|
-
{
|
|
150
|
-
return List.equals(this, right, equalFunctions);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public toString(toStringFunctions?: ToStringFunctions): string
|
|
154
|
-
{
|
|
155
|
-
return List.toString(this, toStringFunctions);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public concatenate(...toConcatenate: JavascriptIterable<number>[]): Iterable<number>
|
|
159
|
-
{
|
|
160
|
-
return List.concatenate(this, ...toConcatenate);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
public map<TOutput>(mapping: (value: number) => TOutput | SyncResult<TOutput>): Iterable<TOutput>
|
|
164
|
-
{
|
|
165
|
-
return List.map(this, mapping);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
public flatMap<TOutput>(mapping: (value: number) => JavascriptIterable<TOutput>): Iterable<TOutput>
|
|
169
|
-
{
|
|
170
|
-
return List.flatMap(this, mapping);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
public where(condition: (value: number) => (boolean | SyncResult<boolean>)): Iterable<number>
|
|
174
|
-
{
|
|
175
|
-
return List.where(this, condition);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
public instanceOf<TOutput extends number>(typeOrTypeCheck: Type<TOutput> | ((value: number) => value is TOutput)): Iterable<TOutput>
|
|
179
|
-
{
|
|
180
|
-
return List.instanceOf(this, typeOrTypeCheck);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
public first(condition?: ((value: number) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<number>
|
|
184
|
-
{
|
|
185
|
-
return List.first(this, condition);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
public last(condition?: ((value: number) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<number>
|
|
189
|
-
{
|
|
190
|
-
return List.last(this, condition);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
public contains(value: number, equalFunctions?: EqualFunctions): SyncResult<boolean>
|
|
194
|
-
{
|
|
195
|
-
return List.contains(this, value, equalFunctions);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
public [Symbol.iterator](): JavascriptIterator<number>
|
|
199
|
-
{
|
|
200
|
-
return List[Symbol.iterator](this);
|
|
201
|
-
}
|
|
1
|
+
import { EqualFunctions } from "./equalFunctions";
|
|
2
|
+
import { Iterable } from "./iterable";
|
|
3
|
+
import { Iterator } from "./iterator";
|
|
4
|
+
import { JavascriptIterable, JavascriptIterator } from "./javascript";
|
|
5
|
+
import { List } from "./list";
|
|
6
|
+
import { PreCondition } from "./preCondition";
|
|
7
|
+
import { SyncResult } from "./syncResult";
|
|
8
|
+
import { ToStringFunctions } from "./toStringFunctions";
|
|
9
|
+
import { Type } from "./types";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A {@link List} of bytes.
|
|
13
|
+
*/
|
|
14
|
+
export class ByteList implements List<number>
|
|
15
|
+
{
|
|
16
|
+
private bytes: Uint8Array;
|
|
17
|
+
private count: number;
|
|
18
|
+
|
|
19
|
+
private constructor()
|
|
20
|
+
{
|
|
21
|
+
this.bytes = new Uint8Array(0);
|
|
22
|
+
this.count = 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static create(initialValues?: JavascriptIterable<number>): ByteList
|
|
26
|
+
{
|
|
27
|
+
const result: ByteList = new ByteList();
|
|
28
|
+
if (initialValues)
|
|
29
|
+
{
|
|
30
|
+
result.addAll(initialValues);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public add(value: number): this
|
|
36
|
+
{
|
|
37
|
+
return List.add(this, value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public addAll(values: JavascriptIterable<number>): this
|
|
41
|
+
{
|
|
42
|
+
return List.addAll(this, values);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public insert(index: number, value: number): this
|
|
46
|
+
{
|
|
47
|
+
PreCondition.assertInsertIndex(index, this.getCount().await(), "index");
|
|
48
|
+
PreCondition.assertByte(value, "value");
|
|
49
|
+
|
|
50
|
+
if (this.count < this.bytes.length)
|
|
51
|
+
{
|
|
52
|
+
if (index < this.count)
|
|
53
|
+
{
|
|
54
|
+
this.bytes.copyWithin(index + 1, index, this.count);
|
|
55
|
+
}
|
|
56
|
+
this.bytes[index] = value;
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
{
|
|
60
|
+
const newCapacity: number = (this.bytes.length * 2) + 1;
|
|
61
|
+
const newBytes: Uint8Array = new Uint8Array(newCapacity);
|
|
62
|
+
if (index > 0)
|
|
63
|
+
{
|
|
64
|
+
newBytes.set(this.bytes.subarray(0, index));
|
|
65
|
+
}
|
|
66
|
+
newBytes[index] = value;
|
|
67
|
+
if (index < this.count)
|
|
68
|
+
{
|
|
69
|
+
newBytes.set(this.bytes.subarray(index), index + 1);
|
|
70
|
+
}
|
|
71
|
+
this.bytes = newBytes;
|
|
72
|
+
}
|
|
73
|
+
this.count++;
|
|
74
|
+
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public insertAll(index: number, values: JavascriptIterable<number>): this
|
|
79
|
+
{
|
|
80
|
+
return List.insertAll(this, index, values);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public remove(value: number, equalFunctions?: EqualFunctions): SyncResult<number>
|
|
84
|
+
{
|
|
85
|
+
PreCondition.assertByte(value, "value");
|
|
86
|
+
|
|
87
|
+
return List.remove(this, value, equalFunctions);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public removeAt(index: number): SyncResult<number>
|
|
91
|
+
{
|
|
92
|
+
PreCondition.assertAccessIndex(index, this.count, "index");
|
|
93
|
+
|
|
94
|
+
const result: number = this.bytes[index];
|
|
95
|
+
this.bytes.copyWithin(index, index + 1, this.count);
|
|
96
|
+
this.count--;
|
|
97
|
+
|
|
98
|
+
return SyncResult.value(result);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public removeFirst(): SyncResult<number>
|
|
102
|
+
{
|
|
103
|
+
return List.removeFirst(this);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public removeLast(): SyncResult<number>
|
|
107
|
+
{
|
|
108
|
+
return List.removeLast(this);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public set(index: number, value: number): this
|
|
112
|
+
{
|
|
113
|
+
PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
|
|
114
|
+
PreCondition.assertByte(value, "value");
|
|
115
|
+
|
|
116
|
+
this.bytes[index] = value;
|
|
117
|
+
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public iterate(): Iterator<number>
|
|
122
|
+
{
|
|
123
|
+
return Iterator.create(this.bytes[Symbol.iterator]().take(this.count));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public get(index: number): SyncResult<number>
|
|
127
|
+
{
|
|
128
|
+
PreCondition.assertAccessIndex(index, this.getCount().await(), "index");
|
|
129
|
+
|
|
130
|
+
return SyncResult.value(this.bytes.at(index)!);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public toArray(): SyncResult<number[]>
|
|
134
|
+
{
|
|
135
|
+
return List.toArray(this);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public any(): SyncResult<boolean>
|
|
139
|
+
{
|
|
140
|
+
return this.getCount().then(count => count > 0);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public getCount(): SyncResult<number>
|
|
144
|
+
{
|
|
145
|
+
return SyncResult.value(this.count);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public equals(right: JavascriptIterable<number>, equalFunctions?: EqualFunctions): SyncResult<boolean>
|
|
149
|
+
{
|
|
150
|
+
return List.equals(this, right, equalFunctions);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public toString(toStringFunctions?: ToStringFunctions): string
|
|
154
|
+
{
|
|
155
|
+
return List.toString(this, toStringFunctions);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public concatenate(...toConcatenate: JavascriptIterable<number>[]): Iterable<number>
|
|
159
|
+
{
|
|
160
|
+
return List.concatenate(this, ...toConcatenate);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public map<TOutput>(mapping: (value: number) => TOutput | SyncResult<TOutput>): Iterable<TOutput>
|
|
164
|
+
{
|
|
165
|
+
return List.map(this, mapping);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public flatMap<TOutput>(mapping: (value: number) => JavascriptIterable<TOutput>): Iterable<TOutput>
|
|
169
|
+
{
|
|
170
|
+
return List.flatMap(this, mapping);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public where(condition: (value: number) => (boolean | SyncResult<boolean>)): Iterable<number>
|
|
174
|
+
{
|
|
175
|
+
return List.where(this, condition);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public instanceOf<TOutput extends number>(typeOrTypeCheck: Type<TOutput> | ((value: number) => value is TOutput)): Iterable<TOutput>
|
|
179
|
+
{
|
|
180
|
+
return List.instanceOf(this, typeOrTypeCheck);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public first(condition?: ((value: number) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<number>
|
|
184
|
+
{
|
|
185
|
+
return List.first(this, condition);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public last(condition?: ((value: number) => (boolean | SyncResult<boolean>)) | undefined): SyncResult<number>
|
|
189
|
+
{
|
|
190
|
+
return List.last(this, condition);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public contains(value: number, equalFunctions?: EqualFunctions): SyncResult<boolean>
|
|
194
|
+
{
|
|
195
|
+
return List.contains(this, value, equalFunctions);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public [Symbol.iterator](): JavascriptIterator<number>
|
|
199
|
+
{
|
|
200
|
+
return List[Symbol.iterator](this);
|
|
201
|
+
}
|
|
202
202
|
}
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import { ByteList } from "./byteList";
|
|
2
|
-
import { ByteReadStream } from "./byteReadStream";
|
|
3
|
-
import { ByteWriteStream } from "./byteWriteStream";
|
|
4
|
-
import { EmptyError } from "./emptyError";
|
|
5
|
-
import { PreCondition } from "./preCondition";
|
|
6
|
-
import { SyncResult } from "./syncResult";
|
|
7
|
-
import { isArray, isNumber, isUndefinedOrNull } from "./types";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* A {@link ByteReadStream} and {@link ByteWriteStream} implementation that is implemented using a
|
|
11
|
-
* {@link ByteList}.
|
|
12
|
-
*/
|
|
13
|
-
export class ByteListStream implements ByteReadStream, ByteWriteStream
|
|
14
|
-
{
|
|
15
|
-
private readonly list: ByteList;
|
|
16
|
-
|
|
17
|
-
private constructor()
|
|
18
|
-
{
|
|
19
|
-
this.list = ByteList.create();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public static create(initialValues?: Uint8Array | number[]): ByteListStream
|
|
23
|
-
{
|
|
24
|
-
const result: ByteListStream = new ByteListStream();
|
|
25
|
-
if (initialValues)
|
|
26
|
-
{
|
|
27
|
-
result.writeBytes(initialValues).await();
|
|
28
|
-
}
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Get the number of bytes that are available to be read.
|
|
34
|
-
*/
|
|
35
|
-
public getAvailableByteCount(): number
|
|
36
|
-
{
|
|
37
|
-
return this.list.getCount().await();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public writeBytes(bytes: Uint8Array | number[], startIndex?: number, length?: number): SyncResult<number>
|
|
41
|
-
{
|
|
42
|
-
PreCondition.assertNotUndefinedAndNotNull(bytes, "bytes");
|
|
43
|
-
|
|
44
|
-
if (isArray(bytes))
|
|
45
|
-
{
|
|
46
|
-
bytes = new Uint8Array(bytes);
|
|
47
|
-
}
|
|
48
|
-
if (isUndefinedOrNull(startIndex))
|
|
49
|
-
{
|
|
50
|
-
startIndex = 0;
|
|
51
|
-
}
|
|
52
|
-
if (isUndefinedOrNull(length))
|
|
53
|
-
{
|
|
54
|
-
length = bytes.length - startIndex;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
PreCondition.assertInsertIndex(startIndex, bytes.length, "startIndex");
|
|
58
|
-
PreCondition.assertBetween(0, length, bytes.length - startIndex, "length");
|
|
59
|
-
|
|
60
|
-
this.list.addAll(bytes.subarray(startIndex, length + startIndex));
|
|
61
|
-
|
|
62
|
-
return SyncResult.value(length);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public readBytes(count: number): SyncResult<Uint8Array>;
|
|
66
|
-
public readBytes(output: Uint8Array, startIndex?: number, count?: number): SyncResult<number>;
|
|
67
|
-
readBytes(countOrOutput: number | Uint8Array, startIndex?: number, count?: number): SyncResult<number> | SyncResult<Uint8Array>
|
|
68
|
-
{
|
|
69
|
-
let result: SyncResult<number> | SyncResult<Uint8Array>;
|
|
70
|
-
if (isNumber(countOrOutput))
|
|
71
|
-
{
|
|
72
|
-
PreCondition.assertGreaterThanOrEqualTo(countOrOutput, 0, "count");
|
|
73
|
-
|
|
74
|
-
if (!this.list.any().await())
|
|
75
|
-
{
|
|
76
|
-
result = SyncResult.error<Uint8Array>(new EmptyError());
|
|
77
|
-
}
|
|
78
|
-
else
|
|
79
|
-
{
|
|
80
|
-
const bytesReadCount: number = Math.min(countOrOutput, this.list.getCount().await());
|
|
81
|
-
const output: Uint8Array = new Uint8Array(bytesReadCount);
|
|
82
|
-
for (let i = 0; i < bytesReadCount; i++)
|
|
83
|
-
{
|
|
84
|
-
output[i] = this.list.removeFirst().await();
|
|
85
|
-
}
|
|
86
|
-
result = SyncResult.value(output);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else
|
|
90
|
-
{
|
|
91
|
-
PreCondition.assertNotUndefinedAndNotNull(countOrOutput, "output");
|
|
92
|
-
|
|
93
|
-
if (isUndefinedOrNull(startIndex))
|
|
94
|
-
{
|
|
95
|
-
startIndex = 0;
|
|
96
|
-
}
|
|
97
|
-
if (isUndefinedOrNull(count))
|
|
98
|
-
{
|
|
99
|
-
count = countOrOutput.length - startIndex;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
PreCondition.assertInsertIndex(startIndex, countOrOutput.length, "startIndex");
|
|
103
|
-
PreCondition.assertBetween(0, count, countOrOutput.length - startIndex, "count");
|
|
104
|
-
|
|
105
|
-
if (!this.list.any().await())
|
|
106
|
-
{
|
|
107
|
-
result = SyncResult.error<number>(new EmptyError());
|
|
108
|
-
}
|
|
109
|
-
else
|
|
110
|
-
{
|
|
111
|
-
const bytesReadCount: number = Math.min(count, this.list.getCount().await());
|
|
112
|
-
for (let i = 0; i < bytesReadCount; i++)
|
|
113
|
-
{
|
|
114
|
-
countOrOutput[startIndex + i] = this.list.removeFirst().await();
|
|
115
|
-
}
|
|
116
|
-
result = SyncResult.value(bytesReadCount);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return result;
|
|
120
|
-
}
|
|
1
|
+
import { ByteList } from "./byteList";
|
|
2
|
+
import { ByteReadStream } from "./byteReadStream";
|
|
3
|
+
import { ByteWriteStream } from "./byteWriteStream";
|
|
4
|
+
import { EmptyError } from "./emptyError";
|
|
5
|
+
import { PreCondition } from "./preCondition";
|
|
6
|
+
import { SyncResult } from "./syncResult";
|
|
7
|
+
import { isArray, isNumber, isUndefinedOrNull } from "./types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A {@link ByteReadStream} and {@link ByteWriteStream} implementation that is implemented using a
|
|
11
|
+
* {@link ByteList}.
|
|
12
|
+
*/
|
|
13
|
+
export class ByteListStream implements ByteReadStream, ByteWriteStream
|
|
14
|
+
{
|
|
15
|
+
private readonly list: ByteList;
|
|
16
|
+
|
|
17
|
+
private constructor()
|
|
18
|
+
{
|
|
19
|
+
this.list = ByteList.create();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static create(initialValues?: Uint8Array | number[]): ByteListStream
|
|
23
|
+
{
|
|
24
|
+
const result: ByteListStream = new ByteListStream();
|
|
25
|
+
if (initialValues)
|
|
26
|
+
{
|
|
27
|
+
result.writeBytes(initialValues).await();
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get the number of bytes that are available to be read.
|
|
34
|
+
*/
|
|
35
|
+
public getAvailableByteCount(): number
|
|
36
|
+
{
|
|
37
|
+
return this.list.getCount().await();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public writeBytes(bytes: Uint8Array | number[], startIndex?: number, length?: number): SyncResult<number>
|
|
41
|
+
{
|
|
42
|
+
PreCondition.assertNotUndefinedAndNotNull(bytes, "bytes");
|
|
43
|
+
|
|
44
|
+
if (isArray(bytes))
|
|
45
|
+
{
|
|
46
|
+
bytes = new Uint8Array(bytes);
|
|
47
|
+
}
|
|
48
|
+
if (isUndefinedOrNull(startIndex))
|
|
49
|
+
{
|
|
50
|
+
startIndex = 0;
|
|
51
|
+
}
|
|
52
|
+
if (isUndefinedOrNull(length))
|
|
53
|
+
{
|
|
54
|
+
length = bytes.length - startIndex;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
PreCondition.assertInsertIndex(startIndex, bytes.length, "startIndex");
|
|
58
|
+
PreCondition.assertBetween(0, length, bytes.length - startIndex, "length");
|
|
59
|
+
|
|
60
|
+
this.list.addAll(bytes.subarray(startIndex, length + startIndex));
|
|
61
|
+
|
|
62
|
+
return SyncResult.value(length);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public readBytes(count: number): SyncResult<Uint8Array>;
|
|
66
|
+
public readBytes(output: Uint8Array, startIndex?: number, count?: number): SyncResult<number>;
|
|
67
|
+
readBytes(countOrOutput: number | Uint8Array, startIndex?: number, count?: number): SyncResult<number> | SyncResult<Uint8Array>
|
|
68
|
+
{
|
|
69
|
+
let result: SyncResult<number> | SyncResult<Uint8Array>;
|
|
70
|
+
if (isNumber(countOrOutput))
|
|
71
|
+
{
|
|
72
|
+
PreCondition.assertGreaterThanOrEqualTo(countOrOutput, 0, "count");
|
|
73
|
+
|
|
74
|
+
if (!this.list.any().await())
|
|
75
|
+
{
|
|
76
|
+
result = SyncResult.error<Uint8Array>(new EmptyError());
|
|
77
|
+
}
|
|
78
|
+
else
|
|
79
|
+
{
|
|
80
|
+
const bytesReadCount: number = Math.min(countOrOutput, this.list.getCount().await());
|
|
81
|
+
const output: Uint8Array = new Uint8Array(bytesReadCount);
|
|
82
|
+
for (let i = 0; i < bytesReadCount; i++)
|
|
83
|
+
{
|
|
84
|
+
output[i] = this.list.removeFirst().await();
|
|
85
|
+
}
|
|
86
|
+
result = SyncResult.value(output);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else
|
|
90
|
+
{
|
|
91
|
+
PreCondition.assertNotUndefinedAndNotNull(countOrOutput, "output");
|
|
92
|
+
|
|
93
|
+
if (isUndefinedOrNull(startIndex))
|
|
94
|
+
{
|
|
95
|
+
startIndex = 0;
|
|
96
|
+
}
|
|
97
|
+
if (isUndefinedOrNull(count))
|
|
98
|
+
{
|
|
99
|
+
count = countOrOutput.length - startIndex;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
PreCondition.assertInsertIndex(startIndex, countOrOutput.length, "startIndex");
|
|
103
|
+
PreCondition.assertBetween(0, count, countOrOutput.length - startIndex, "count");
|
|
104
|
+
|
|
105
|
+
if (!this.list.any().await())
|
|
106
|
+
{
|
|
107
|
+
result = SyncResult.error<number>(new EmptyError());
|
|
108
|
+
}
|
|
109
|
+
else
|
|
110
|
+
{
|
|
111
|
+
const bytesReadCount: number = Math.min(count, this.list.getCount().await());
|
|
112
|
+
for (let i = 0; i < bytesReadCount; i++)
|
|
113
|
+
{
|
|
114
|
+
countOrOutput[startIndex + i] = this.list.removeFirst().await();
|
|
115
|
+
}
|
|
116
|
+
result = SyncResult.value(bytesReadCount);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
121
|
}
|