@pkmn/sim 0.4.19 → 0.4.23

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.
Files changed (88) hide show
  1. package/build/config/formats.js +752 -743
  2. package/build/config/formats.js.map +1 -1
  3. package/build/data/aliases.js +4 -4
  4. package/build/data/aliases.js.map +1 -1
  5. package/build/data/conditions.js +4 -1
  6. package/build/data/conditions.js.map +1 -1
  7. package/build/data/formats-data.js +27 -27
  8. package/build/data/formats-data.js.map +1 -1
  9. package/build/data/learnsets.js +10 -10
  10. package/build/data/learnsets.js.map +1 -1
  11. package/build/data/mods/gen1/moves.js +1 -1
  12. package/build/data/mods/gen1/moves.js.map +1 -1
  13. package/build/data/mods/gen1/scripts.js +18 -22
  14. package/build/data/mods/gen1/scripts.js.map +1 -1
  15. package/build/data/mods/gen2/scripts.js +16 -23
  16. package/build/data/mods/gen2/scripts.js.map +1 -1
  17. package/build/data/mods/gen3/moves.js +2 -1
  18. package/build/data/mods/gen3/moves.js.map +1 -1
  19. package/build/data/mods/gen4/conditions.js +6 -0
  20. package/build/data/mods/gen4/conditions.js.map +1 -1
  21. package/build/data/mods/gen4/moves.js +2 -1
  22. package/build/data/mods/gen4/moves.js.map +1 -1
  23. package/build/data/mods/gen4/scripts.js +2 -1
  24. package/build/data/mods/gen4/scripts.js.map +1 -1
  25. package/build/data/mods/gen6/conditions.js +4 -1
  26. package/build/data/mods/gen6/conditions.js.map +1 -1
  27. package/build/data/mods/gen7/formats-data.js +2 -2
  28. package/build/data/mods/gen7/formats-data.js.map +1 -1
  29. package/build/data/mods/gen7/moves.js +8 -0
  30. package/build/data/mods/gen7/moves.js.map +1 -1
  31. package/build/data/moves.js +26 -14
  32. package/build/data/moves.js.map +1 -1
  33. package/build/data/rulesets.js +258 -16
  34. package/build/data/rulesets.js.map +1 -1
  35. package/build/data/tags.js +3 -3
  36. package/build/data/tags.js.map +1 -1
  37. package/build/lib/streams.d.ts +1 -199
  38. package/build/lib/streams.js +11 -772
  39. package/build/lib/streams.js.map +1 -1
  40. package/build/sim/battle-actions.d.ts +1 -1
  41. package/build/sim/battle-actions.js +18 -42
  42. package/build/sim/battle-actions.js.map +1 -1
  43. package/build/sim/battle.d.ts +1 -0
  44. package/build/sim/battle.js +5 -0
  45. package/build/sim/battle.js.map +1 -1
  46. package/build/sim/dex-moves.d.ts +31 -11
  47. package/build/sim/dex-moves.js +4 -3
  48. package/build/sim/dex-moves.js.map +1 -1
  49. package/build/sim/dex-species.js +11 -1
  50. package/build/sim/dex-species.js.map +1 -1
  51. package/build/sim/exported-global-types.d.ts +4 -0
  52. package/build/sim/global-types.d.ts +4 -0
  53. package/build/sim/pokemon.js +1 -1
  54. package/build/sim/pokemon.js.map +1 -1
  55. package/build/sim/side.js +2 -2
  56. package/build/sim/side.js.map +1 -1
  57. package/build/sim/team-validator.d.ts +4 -0
  58. package/build/sim/team-validator.js +37 -11
  59. package/build/sim/team-validator.js.map +1 -1
  60. package/config/formats.ts +649 -630
  61. package/data/aliases.ts +4 -4
  62. package/data/conditions.ts +4 -1
  63. package/data/formats-data.ts +27 -27
  64. package/data/learnsets.ts +10 -10
  65. package/data/mods/gen1/moves.ts +1 -1
  66. package/data/mods/gen1/scripts.ts +23 -19
  67. package/data/mods/gen2/scripts.ts +20 -19
  68. package/data/mods/gen3/moves.ts +2 -1
  69. package/data/mods/gen4/conditions.ts +6 -0
  70. package/data/mods/gen4/moves.ts +2 -1
  71. package/data/mods/gen4/scripts.ts +1 -1
  72. package/data/mods/gen6/conditions.ts +4 -1
  73. package/data/mods/gen7/formats-data.ts +2 -2
  74. package/data/mods/gen7/moves.ts +8 -0
  75. package/data/moves.ts +23 -14
  76. package/data/rulesets.ts +235 -16
  77. package/data/tags.ts +3 -3
  78. package/lib/streams.ts +1 -874
  79. package/package.json +4 -3
  80. package/sim/battle-actions.ts +19 -40
  81. package/sim/battle.ts +6 -0
  82. package/sim/dex-moves.ts +35 -13
  83. package/sim/dex-species.ts +10 -1
  84. package/sim/exported-global-types.ts +4 -0
  85. package/sim/global-types.ts +4 -0
  86. package/sim/pokemon.ts +1 -1
  87. package/sim/side.ts +2 -2
  88. package/sim/team-validator.ts +41 -10
@@ -1,775 +1,14 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
2
12
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stdpipe = exports.stdout = exports.stdin = exports.readAll = exports.ObjectReadWriteStream = exports.ObjectWriteStream = exports.ObjectReadStream = exports.ReadWriteStream = exports.WriteStream = exports.ReadStream = void 0;
4
- /**
5
- * Streams
6
- * Pokemon Showdown - http://pokemonshowdown.com/
7
- *
8
- * The Node.js standard library's Streams are really hard to use. This
9
- * offers a better stream API.
10
- *
11
- * Documented in STREAMS.md.
12
- *
13
- * @license MIT
14
- */
15
- const BUF_SIZE = 65536 * 4;
16
- class ReadStream {
17
- constructor(optionsOrStreamLike = {}) {
18
- this.buf = Buffer.allocUnsafe(BUF_SIZE);
19
- this.bufStart = 0;
20
- this.bufEnd = 0;
21
- this.bufCapacity = BUF_SIZE;
22
- this.readSize = 0;
23
- this.atEOF = false;
24
- this.errorBuf = null;
25
- this.encoding = 'utf8';
26
- this.isReadable = true;
27
- this.isWritable = false;
28
- this.nodeReadableStream = null;
29
- this.nextPushResolver = null;
30
- this.nextPush = new Promise(resolve => {
31
- this.nextPushResolver = resolve;
32
- });
33
- this.awaitingPush = false;
34
- let options;
35
- if (typeof optionsOrStreamLike === 'string') {
36
- options = { buffer: optionsOrStreamLike };
37
- }
38
- else if (optionsOrStreamLike instanceof Buffer) {
39
- options = { buffer: optionsOrStreamLike };
40
- }
41
- else if (typeof optionsOrStreamLike._readableState === 'object') {
42
- options = { nodeStream: optionsOrStreamLike };
43
- }
44
- else {
45
- options = optionsOrStreamLike;
46
- }
47
- if (options.nodeStream) {
48
- const nodeStream = options.nodeStream;
49
- this.nodeReadableStream = nodeStream;
50
- nodeStream.on('data', data => {
51
- this.push(data);
52
- });
53
- nodeStream.on('end', () => {
54
- this.pushEnd();
55
- });
56
- options.read = function (unusedBytes) {
57
- this.nodeReadableStream.resume();
58
- };
59
- options.pause = function (unusedBytes) {
60
- this.nodeReadableStream.pause();
61
- };
62
- }
63
- if (options.read)
64
- this._read = options.read;
65
- if (options.pause)
66
- this._pause = options.pause;
67
- if (options.destroy)
68
- this._destroy = options.destroy;
69
- if (options.encoding)
70
- this.encoding = options.encoding;
71
- if (options.buffer !== undefined) {
72
- this.push(options.buffer);
73
- this.pushEnd();
74
- }
75
- }
76
- get bufSize() {
77
- return this.bufEnd - this.bufStart;
78
- }
79
- moveBuf() {
80
- if (this.bufStart !== this.bufEnd) {
81
- this.buf.copy(this.buf, 0, this.bufStart, this.bufEnd);
82
- }
83
- this.bufEnd -= this.bufStart;
84
- this.bufStart = 0;
85
- }
86
- expandBuf(newCapacity = this.bufCapacity * 2) {
87
- const newBuf = Buffer.allocUnsafe(newCapacity);
88
- this.buf.copy(newBuf, 0, this.bufStart, this.bufEnd);
89
- this.bufEnd -= this.bufStart;
90
- this.bufStart = 0;
91
- this.bufCapacity = newCapacity;
92
- this.buf = newBuf;
93
- }
94
- ensureCapacity(additionalCapacity) {
95
- if (this.bufEnd + additionalCapacity <= this.bufCapacity)
96
- return;
97
- const capacity = this.bufEnd - this.bufStart + additionalCapacity;
98
- if (capacity <= this.bufCapacity) {
99
- return this.moveBuf();
100
- }
101
- let newCapacity = this.bufCapacity * 2;
102
- while (newCapacity < capacity)
103
- newCapacity *= 2;
104
- this.expandBuf(newCapacity);
105
- }
106
- push(buf, encoding = this.encoding) {
107
- let size;
108
- if (this.atEOF)
109
- return;
110
- if (typeof buf === 'string') {
111
- size = Buffer.byteLength(buf, encoding);
112
- this.ensureCapacity(size);
113
- this.buf.write(buf, this.bufEnd);
114
- }
115
- else {
116
- size = buf.length;
117
- this.ensureCapacity(size);
118
- buf.copy(this.buf, this.bufEnd);
119
- }
120
- this.bufEnd += size;
121
- if (this.bufSize > this.readSize && size * 2 < this.bufSize)
122
- this._pause();
123
- this.resolvePush();
124
- }
125
- pushEnd() {
126
- this.atEOF = true;
127
- this.resolvePush();
128
- }
129
- pushError(err, recoverable) {
130
- if (!this.errorBuf)
131
- this.errorBuf = [];
132
- this.errorBuf.push(err);
133
- if (!recoverable)
134
- this.atEOF = true;
135
- this.resolvePush();
136
- }
137
- readError() {
138
- if (this.errorBuf) {
139
- const err = this.errorBuf.shift();
140
- if (!this.errorBuf.length)
141
- this.errorBuf = null;
142
- throw err;
143
- }
144
- }
145
- peekError() {
146
- if (this.errorBuf) {
147
- throw this.errorBuf[0];
148
- }
149
- }
150
- resolvePush() {
151
- if (!this.nextPushResolver)
152
- throw new Error(`Push after end of read stream`);
153
- this.nextPushResolver();
154
- if (this.atEOF) {
155
- this.nextPushResolver = null;
156
- return;
157
- }
158
- this.nextPush = new Promise(resolve => {
159
- this.nextPushResolver = resolve;
160
- });
161
- }
162
- _read(size = 0) {
163
- throw new Error(`ReadStream needs to be subclassed and the _read function needs to be implemented.`);
164
- }
165
- _destroy() { }
166
- _pause() { }
167
- /**
168
- * Reads until the internal buffer is non-empty. Does nothing if the
169
- * internal buffer is already non-empty.
170
- *
171
- * If `byteCount` is a number, instead read until the internal buffer
172
- * contains at least `byteCount` bytes.
173
- *
174
- * If `byteCount` is `true`, reads even if the internal buffer is
175
- * non-empty.
176
- */
177
- loadIntoBuffer(byteCount = null, readError) {
178
- this[readError ? 'readError' : 'peekError']();
179
- if (byteCount === 0)
180
- return;
181
- this.readSize = Math.max(byteCount === true ? this.bufSize + 1 : byteCount === null ? 1 : byteCount, this.readSize);
182
- if (!this.errorBuf && !this.atEOF && this.bufSize < this.readSize) {
183
- let bytes = this.readSize - this.bufSize;
184
- if (bytes === Infinity || byteCount === null || byteCount === true)
185
- bytes = null;
186
- return this.doLoad(bytes, readError);
187
- }
188
- }
189
- async doLoad(chunkSize, readError) {
190
- while (!this.errorBuf && !this.atEOF && this.bufSize < this.readSize) {
191
- if (chunkSize)
192
- void this._read(chunkSize);
193
- else
194
- void this._read();
195
- await this.nextPush;
196
- this[readError ? 'readError' : 'peekError']();
197
- }
198
- }
199
- peek(byteCount = null, encoding = this.encoding) {
200
- if (typeof byteCount === 'string') {
201
- encoding = byteCount;
202
- byteCount = null;
203
- }
204
- const maybeLoad = this.loadIntoBuffer(byteCount);
205
- if (maybeLoad)
206
- return maybeLoad.then(() => this.peek(byteCount, encoding));
207
- if (!this.bufSize && byteCount !== 0)
208
- return null;
209
- if (byteCount === null)
210
- return this.buf.toString(encoding, this.bufStart, this.bufEnd);
211
- if (byteCount > this.bufSize)
212
- byteCount = this.bufSize;
213
- return this.buf.toString(encoding, this.bufStart, this.bufStart + byteCount);
214
- }
215
- peekBuffer(byteCount = null) {
216
- const maybeLoad = this.loadIntoBuffer(byteCount);
217
- if (maybeLoad)
218
- return maybeLoad.then(() => this.peekBuffer(byteCount));
219
- if (!this.bufSize && byteCount !== 0)
220
- return null;
221
- if (byteCount === null)
222
- return this.buf.slice(this.bufStart, this.bufEnd);
223
- if (byteCount > this.bufSize)
224
- byteCount = this.bufSize;
225
- return this.buf.slice(this.bufStart, this.bufStart + byteCount);
226
- }
227
- async read(byteCount = null, encoding = this.encoding) {
228
- if (typeof byteCount === 'string') {
229
- encoding = byteCount;
230
- byteCount = null;
231
- }
232
- await this.loadIntoBuffer(byteCount, true);
233
- // This MUST NOT be awaited: we MUST synchronously clear byteCount after peeking
234
- // if the buffer is written to after peek but before clearing the buffer, the write
235
- // will be lost forever
236
- const out = this.peek(byteCount, encoding);
237
- if (out && typeof out !== 'string') {
238
- throw new Error("Race condition; you must not read before a previous read has completed");
239
- }
240
- if (byteCount === null || byteCount >= this.bufSize) {
241
- this.bufStart = 0;
242
- this.bufEnd = 0;
243
- this.readSize = 0;
244
- }
245
- else {
246
- this.bufStart += byteCount;
247
- this.readSize -= byteCount;
248
- }
249
- return out;
250
- }
251
- byChunk(byteCount) {
252
- // eslint-disable-next-line @typescript-eslint/no-this-alias
253
- const byteStream = this;
254
- return new ObjectReadStream({
255
- async read() {
256
- const next = await byteStream.read(byteCount);
257
- if (typeof next === 'string')
258
- this.push(next);
259
- else
260
- this.pushEnd();
261
- },
262
- });
263
- }
264
- byLine() {
265
- // eslint-disable-next-line @typescript-eslint/no-this-alias
266
- const byteStream = this;
267
- return new ObjectReadStream({
268
- async read() {
269
- const next = await byteStream.readLine();
270
- if (typeof next === 'string')
271
- this.push(next);
272
- else
273
- this.pushEnd();
274
- },
275
- });
276
- }
277
- delimitedBy(delimiter) {
278
- // eslint-disable-next-line @typescript-eslint/no-this-alias
279
- const byteStream = this;
280
- return new ObjectReadStream({
281
- async read() {
282
- const next = await byteStream.readDelimitedBy(delimiter);
283
- if (typeof next === 'string')
284
- this.push(next);
285
- else
286
- this.pushEnd();
287
- },
288
- });
289
- }
290
- async readBuffer(byteCount = null) {
291
- await this.loadIntoBuffer(byteCount, true);
292
- // This MUST NOT be awaited: we must synchronously clear the buffer after peeking
293
- // (see `read`)
294
- const out = this.peekBuffer(byteCount);
295
- if (out && out.then) {
296
- throw new Error("Race condition; you must not read before a previous read has completed");
297
- }
298
- if (byteCount === null || byteCount >= this.bufSize) {
299
- this.bufStart = 0;
300
- this.bufEnd = 0;
301
- }
302
- else {
303
- this.bufStart += byteCount;
304
- }
305
- return out;
306
- }
307
- async indexOf(symbol, encoding = this.encoding) {
308
- let idx = this.buf.indexOf(symbol, this.bufStart, encoding);
309
- while (!this.atEOF && (idx >= this.bufEnd || idx < 0)) {
310
- await this.loadIntoBuffer(true);
311
- idx = this.buf.indexOf(symbol, this.bufStart, encoding);
312
- }
313
- if (idx >= this.bufEnd)
314
- return -1;
315
- return idx - this.bufStart;
316
- }
317
- async readAll(encoding = this.encoding) {
318
- return (await this.read(Infinity, encoding)) || '';
319
- }
320
- peekAll(encoding = this.encoding) {
321
- return this.peek(Infinity, encoding);
322
- }
323
- async readDelimitedBy(symbol, encoding = this.encoding) {
324
- if (this.atEOF && !this.bufSize)
325
- return null;
326
- const idx = await this.indexOf(symbol, encoding);
327
- if (idx < 0) {
328
- return this.readAll(encoding);
329
- }
330
- else {
331
- const out = await this.read(idx, encoding);
332
- this.bufStart += Buffer.byteLength(symbol, 'utf8');
333
- return out;
334
- }
335
- }
336
- async readLine(encoding = this.encoding) {
337
- if (!encoding)
338
- throw new Error(`readLine must have an encoding`);
339
- let line = await this.readDelimitedBy('\n', encoding);
340
- if (line?.endsWith('\r'))
341
- line = line.slice(0, -1);
342
- return line;
343
- }
344
- destroy() {
345
- this.atEOF = true;
346
- this.bufStart = 0;
347
- this.bufEnd = 0;
348
- if (this.nextPushResolver)
349
- this.resolvePush();
350
- return this._destroy();
351
- }
352
- async next(byteCount = null) {
353
- const value = await this.read(byteCount);
354
- return { value, done: value === null };
355
- }
356
- async pipeTo(outStream, options = {}) {
357
- let value, done;
358
- while (({ value, done } = await this.next(), !done)) {
359
- await outStream.write(value);
360
- }
361
- if (!options.noEnd)
362
- return outStream.writeEnd();
363
- }
364
- }
365
- exports.ReadStream = ReadStream;
366
- class WriteStream {
367
- constructor(optionsOrStream = {}) {
368
- this.isReadable = false;
369
- this.isWritable = true;
370
- this.encoding = 'utf8';
371
- this.nodeWritableStream = null;
372
- this.drainListeners = [];
373
- let options = optionsOrStream;
374
- if (options._writableState) {
375
- options = { nodeStream: optionsOrStream };
376
- }
377
- if (options.nodeStream) {
378
- const nodeStream = options.nodeStream;
379
- this.nodeWritableStream = nodeStream;
380
- options.write = function (data) {
381
- const result = this.nodeWritableStream.write(data);
382
- if (result !== false)
383
- return undefined;
384
- if (!this.drainListeners.length) {
385
- this.nodeWritableStream.once('drain', () => {
386
- for (const listener of this.drainListeners)
387
- listener();
388
- this.drainListeners = [];
389
- });
390
- }
391
- return new Promise(resolve => {
392
- // `as () => void` is necessary because TypeScript thinks that it should be a function
393
- // that takes an undefined value as its only parameter: `(value: PromiseLike<undefined> | undefined) => void`
394
- this.drainListeners.push(resolve);
395
- });
396
- };
397
- // Prior to Node v10.12.0, attempting to close STDOUT or STDERR will throw
398
- if (nodeStream !== process.stdout && nodeStream !== process.stderr) {
399
- options.writeEnd = function () {
400
- return new Promise(resolve => {
401
- this.nodeWritableStream.end(() => resolve());
402
- });
403
- };
404
- }
405
- }
406
- if (options.write)
407
- this._write = options.write;
408
- if (options.writeEnd)
409
- this._writeEnd = options.writeEnd;
410
- }
411
- write(chunk) {
412
- return this._write(chunk);
413
- }
414
- writeLine(chunk) {
415
- if (chunk === null) {
416
- return this.writeEnd();
417
- }
418
- return this.write(chunk + '\n');
419
- }
420
- _write(chunk) {
421
- throw new Error(`WriteStream needs to be subclassed and the _write function needs to be implemented.`);
422
- }
423
- _writeEnd() { }
424
- async writeEnd(chunk) {
425
- if (chunk) {
426
- await this.write(chunk);
427
- }
428
- return this._writeEnd();
429
- }
430
- }
431
- exports.WriteStream = WriteStream;
432
- class ReadWriteStream extends ReadStream {
433
- constructor(options = {}) {
434
- super(options);
435
- this.isReadable = true;
436
- this.isWritable = true;
437
- this.nodeWritableStream = null;
438
- this.drainListeners = [];
439
- if (options.nodeStream) {
440
- const nodeStream = options.nodeStream;
441
- this.nodeWritableStream = nodeStream;
442
- options.write = function (data) {
443
- const result = this.nodeWritableStream.write(data);
444
- if (result !== false)
445
- return undefined;
446
- if (!this.drainListeners.length) {
447
- this.nodeWritableStream.once('drain', () => {
448
- for (const listener of this.drainListeners)
449
- listener();
450
- this.drainListeners = [];
451
- });
452
- }
453
- return new Promise(resolve => {
454
- this.drainListeners.push(resolve);
455
- });
456
- };
457
- // Prior to Node v10.12.0, attempting to close STDOUT or STDERR will throw
458
- if (nodeStream !== process.stdout && nodeStream !== process.stderr) {
459
- options.writeEnd = function () {
460
- return new Promise(resolve => {
461
- this.nodeWritableStream.end(() => resolve());
462
- });
463
- };
464
- }
465
- }
466
- if (options.write)
467
- this._write = options.write;
468
- if (options.writeEnd)
469
- this._writeEnd = options.writeEnd;
470
- }
471
- write(chunk) {
472
- return this._write(chunk);
473
- }
474
- writeLine(chunk) {
475
- return this.write(chunk + '\n');
476
- }
477
- _write(chunk) {
478
- throw new Error(`WriteStream needs to be subclassed and the _write function needs to be implemented.`);
479
- }
480
- /**
481
- * In a ReadWriteStream, `_read` does not need to be implemented,
482
- * because it's valid for the read stream buffer to be filled only by
483
- * `_write`.
484
- */
485
- _read(size) { }
486
- _writeEnd() { }
487
- async writeEnd() {
488
- return this._writeEnd();
489
- }
490
- }
491
- exports.ReadWriteStream = ReadWriteStream;
492
- class ObjectReadStream {
493
- constructor(optionsOrStreamLike = {}) {
494
- this.buf = [];
495
- this.readSize = 0;
496
- this.atEOF = false;
497
- this.errorBuf = null;
498
- this.isReadable = true;
499
- this.isWritable = false;
500
- this.nodeReadableStream = null;
501
- this.nextPushResolver = null;
502
- this.nextPush = new Promise(resolve => {
503
- this.nextPushResolver = resolve;
504
- });
505
- this.awaitingPush = false;
506
- let options;
507
- if (Array.isArray(optionsOrStreamLike)) {
508
- options = { buffer: optionsOrStreamLike };
509
- }
510
- else if (typeof optionsOrStreamLike._readableState === 'object') {
511
- options = { nodeStream: optionsOrStreamLike };
512
- }
513
- else {
514
- options = optionsOrStreamLike;
515
- }
516
- if (options.nodeStream) {
517
- const nodeStream = options.nodeStream;
518
- this.nodeReadableStream = nodeStream;
519
- nodeStream.on('data', data => {
520
- this.push(data);
521
- });
522
- nodeStream.on('end', () => {
523
- this.pushEnd();
524
- });
525
- options = {
526
- read() {
527
- this.nodeReadableStream.resume();
528
- },
529
- pause() {
530
- this.nodeReadableStream.pause();
531
- },
532
- };
533
- }
534
- if (options.read)
535
- this._read = options.read;
536
- if (options.pause)
537
- this._pause = options.pause;
538
- if (options.destroy)
539
- this._destroy = options.destroy;
540
- if (options.buffer !== undefined) {
541
- this.buf = options.buffer.slice();
542
- this.pushEnd();
543
- }
544
- }
545
- push(elem) {
546
- if (this.atEOF)
547
- return;
548
- this.buf.push(elem);
549
- if (this.buf.length > this.readSize && this.buf.length >= 16)
550
- this._pause();
551
- this.resolvePush();
552
- }
553
- pushEnd() {
554
- this.atEOF = true;
555
- this.resolvePush();
556
- }
557
- pushError(err, recoverable) {
558
- if (!this.errorBuf)
559
- this.errorBuf = [];
560
- this.errorBuf.push(err);
561
- if (!recoverable)
562
- this.atEOF = true;
563
- this.resolvePush();
564
- }
565
- readError() {
566
- if (this.errorBuf) {
567
- const err = this.errorBuf.shift();
568
- if (!this.errorBuf.length)
569
- this.errorBuf = null;
570
- throw err;
571
- }
572
- }
573
- peekError() {
574
- if (this.errorBuf) {
575
- throw this.errorBuf[0];
576
- }
577
- }
578
- resolvePush() {
579
- if (!this.nextPushResolver)
580
- throw new Error(`Push after end of read stream`);
581
- this.nextPushResolver();
582
- if (this.atEOF) {
583
- this.nextPushResolver = null;
584
- return;
585
- }
586
- this.nextPush = new Promise(resolve => {
587
- this.nextPushResolver = resolve;
588
- });
589
- }
590
- _read(size = 0) {
591
- throw new Error(`ReadStream needs to be subclassed and the _read function needs to be implemented.`);
592
- }
593
- _destroy() { }
594
- _pause() { }
595
- async loadIntoBuffer(count = 1, readError) {
596
- this[readError ? 'readError' : 'peekError']();
597
- if (count === true)
598
- count = this.buf.length + 1;
599
- if (this.buf.length >= count)
600
- return;
601
- this.readSize = Math.max(count, this.readSize);
602
- while (!this.errorBuf && !this.atEOF && this.buf.length < this.readSize) {
603
- const readResult = this._read();
604
- if (readResult) {
605
- await readResult;
606
- }
607
- else {
608
- await this.nextPush;
609
- }
610
- this[readError ? 'readError' : 'peekError']();
611
- }
612
- }
613
- async peek() {
614
- if (this.buf.length)
615
- return this.buf[0];
616
- await this.loadIntoBuffer();
617
- return this.buf[0];
618
- }
619
- async read() {
620
- if (this.buf.length)
621
- return this.buf.shift();
622
- await this.loadIntoBuffer(1, true);
623
- if (!this.buf.length)
624
- return null;
625
- return this.buf.shift();
626
- }
627
- async peekArray(count = null) {
628
- await this.loadIntoBuffer(count === null ? 1 : count);
629
- return this.buf.slice(0, count === null ? Infinity : count);
630
- }
631
- async readArray(count = null) {
632
- await this.loadIntoBuffer(count === null ? 1 : count, true);
633
- const out = this.buf.slice(0, count === null ? Infinity : count);
634
- this.buf = this.buf.slice(out.length);
635
- return out;
636
- }
637
- async readAll() {
638
- await this.loadIntoBuffer(Infinity, true);
639
- const out = this.buf;
640
- this.buf = [];
641
- return out;
642
- }
643
- async peekAll() {
644
- await this.loadIntoBuffer(Infinity);
645
- return this.buf.slice();
646
- }
647
- destroy() {
648
- this.atEOF = true;
649
- this.buf = [];
650
- this.resolvePush();
651
- return this._destroy();
652
- }
653
- // eslint-disable-next-line no-restricted-globals
654
- [Symbol.asyncIterator]() { return this; }
655
- async next() {
656
- if (this.buf.length)
657
- return { value: this.buf.shift(), done: false };
658
- await this.loadIntoBuffer(1, true);
659
- if (!this.buf.length)
660
- return { value: undefined, done: true };
661
- return { value: this.buf.shift(), done: false };
662
- }
663
- async pipeTo(outStream, options = {}) {
664
- let value, done;
665
- while (({ value, done } = await this.next(), !done)) {
666
- await outStream.write(value);
667
- }
668
- if (!options.noEnd)
669
- return outStream.writeEnd();
670
- }
671
- }
672
- exports.ObjectReadStream = ObjectReadStream;
673
- class ObjectWriteStream {
674
- constructor(optionsOrStream = {}) {
675
- this.isReadable = false;
676
- this.isWritable = true;
677
- this.nodeWritableStream = null;
678
- let options = optionsOrStream;
679
- if (options._writableState) {
680
- options = { nodeStream: optionsOrStream };
681
- }
682
- if (options.nodeStream) {
683
- const nodeStream = options.nodeStream;
684
- this.nodeWritableStream = nodeStream;
685
- options.write = function (data) {
686
- const result = this.nodeWritableStream.write(data);
687
- if (result === false) {
688
- return new Promise(resolve => {
689
- this.nodeWritableStream.once('drain', () => {
690
- resolve();
691
- });
692
- });
693
- }
694
- };
695
- // Prior to Node v10.12.0, attempting to close STDOUT or STDERR will throw
696
- if (nodeStream !== process.stdout && nodeStream !== process.stderr) {
697
- options.writeEnd = function () {
698
- return new Promise(resolve => {
699
- this.nodeWritableStream.end(() => resolve());
700
- });
701
- };
702
- }
703
- }
704
- if (options.write)
705
- this._write = options.write;
706
- if (options.writeEnd)
707
- this._writeEnd = options.writeEnd;
708
- }
709
- write(elem) {
710
- if (elem === null) {
711
- return this.writeEnd();
712
- }
713
- return this._write(elem);
714
- }
715
- _write(elem) {
716
- throw new Error(`WriteStream needs to be subclassed and the _write function needs to be implemented.`);
717
- }
718
- _writeEnd() { }
719
- async writeEnd(elem) {
720
- if (elem !== undefined) {
721
- await this.write(elem);
722
- }
723
- return this._writeEnd();
724
- }
725
- }
726
- exports.ObjectWriteStream = ObjectWriteStream;
727
- class ObjectReadWriteStream extends ObjectReadStream {
728
- constructor(options = {}) {
729
- super(options);
730
- this.isReadable = true;
731
- this.isWritable = true;
732
- this.nodeWritableStream = null;
733
- if (options.write)
734
- this._write = options.write;
735
- if (options.writeEnd)
736
- this._writeEnd = options.writeEnd;
737
- }
738
- write(elem) {
739
- return this._write(elem);
740
- }
741
- _write(elem) {
742
- throw new Error(`WriteStream needs to be subclassed and the _write function needs to be implemented.`);
743
- }
744
- /** In a ReadWriteStream, _read does not need to be implemented. */
745
- _read() { }
746
- _writeEnd() { }
747
- async writeEnd() {
748
- return this._writeEnd();
749
- }
750
- }
751
- exports.ObjectReadWriteStream = ObjectReadWriteStream;
752
- function readAll(nodeStream, encoding) {
753
- return new ReadStream(nodeStream).readAll(encoding);
754
- }
755
- exports.readAll = readAll;
756
- function stdin() {
757
- return new ReadStream(process.stdin);
758
- }
759
- exports.stdin = stdin;
760
- function stdout() {
761
- return new WriteStream(process.stdout);
762
- }
763
- exports.stdout = stdout;
764
- function stdpipe(stream) {
765
- const promises = [];
766
- if (stream.pipeTo) {
767
- promises.push(stream.pipeTo(stdout()));
768
- }
769
- if (stream.write) {
770
- promises.push(stdin().pipeTo(stream));
771
- }
772
- return Promise.all(promises);
773
- }
774
- exports.stdpipe = stdpipe;
13
+ __exportStar(require("@pkmn/streams"), exports);
775
14
  //# sourceMappingURL=streams.js.map