|
| 1 | +import { beforeEach, describe, expect, it } from "bun:test"; |
| 2 | +import { Readable, Writable } from "stream"; |
| 3 | + |
| 4 | +const ABC = new Uint8Array([0x41, 0x42, 0x43]); |
| 5 | +const DEF = new Uint8Array([0x44, 0x45, 0x46]); |
| 6 | +const GHI = new Uint8Array([0x47, 0x48, 0x49]); |
| 7 | + |
| 8 | +describe("Writable", () => { |
| 9 | + let called; |
| 10 | + |
| 11 | + function logCall(fn, id) { |
| 12 | + return function() { |
| 13 | + called[id] = (called[id] || 0) + 1; |
| 14 | + return fn.apply(this, arguments); |
| 15 | + }; |
| 16 | + } |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + called = []; |
| 20 | + }); |
| 21 | + |
| 22 | + it("should perform simple operations", () => { |
| 23 | + let n = 0; |
| 24 | + const writable = new Writable({ |
| 25 | + write: logCall((chunk, encoding, cb) => { |
| 26 | + expect(chunk instanceof Buffer).toBe(true); |
| 27 | + if (n++ === 0) { |
| 28 | + expect(String(chunk)).toBe("ABC"); |
| 29 | + } else { |
| 30 | + expect(String(chunk)).toBe("DEF"); |
| 31 | + } |
| 32 | + |
| 33 | + cb(); |
| 34 | + }, 0), |
| 35 | + }); |
| 36 | + |
| 37 | + writable.write(ABC); |
| 38 | + writable.end(DEF); |
| 39 | + expect(called).toEqual([ 2 ]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should pass in Uint8Array in object mode", () => { |
| 43 | + const writable = new Writable({ |
| 44 | + objectMode: true, |
| 45 | + write: logCall((chunk, encoding, cb) => { |
| 46 | + expect(chunk instanceof Buffer).toBe(false); |
| 47 | + expect(chunk instanceof Uint8Array).toBe(true); |
| 48 | + expect(chunk).toStrictEqual(ABC); |
| 49 | + expect(encoding).toBe("utf8"); |
| 50 | + cb(); |
| 51 | + }, 0), |
| 52 | + }); |
| 53 | + |
| 54 | + writable.end(ABC); |
| 55 | + expect(called).toEqual([ 1 ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should handle multiple writes carried out via writev()", () => { |
| 59 | + let callback; |
| 60 | + |
| 61 | + const writable = new Writable({ |
| 62 | + write: logCall((chunk, encoding, cb) => { |
| 63 | + expect(chunk instanceof Buffer).toBe(true); |
| 64 | + expect(encoding).toBe("buffer"); |
| 65 | + expect(String(chunk)).toBe("ABC"); |
| 66 | + callback = cb; |
| 67 | + }, 0), |
| 68 | + writev: logCall((chunks, cb) => { |
| 69 | + expect(chunks.length).toBe(2); |
| 70 | + expect(chunks[0].encoding).toBe("buffer"); |
| 71 | + expect(chunks[1].encoding).toBe("buffer"); |
| 72 | + expect(chunks[0].chunk + chunks[1].chunk).toBe("DEFGHI"); |
| 73 | + }, 1), |
| 74 | + }); |
| 75 | + |
| 76 | + writable.write(ABC); |
| 77 | + writable.write(DEF); |
| 78 | + writable.end(GHI); |
| 79 | + callback(); |
| 80 | + expect(called).toEqual([ 1, 1 ]); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("Readable", () => { |
| 85 | + it("should perform simple operations", () => { |
| 86 | + const readable = new Readable({ |
| 87 | + read() {} |
| 88 | + }); |
| 89 | + |
| 90 | + readable.push(DEF); |
| 91 | + readable.unshift(ABC); |
| 92 | + |
| 93 | + const buf = readable.read(); |
| 94 | + expect(buf instanceof Buffer).toBe(true); |
| 95 | + expect([ ...buf ]).toEqual([ ...ABC, ...DEF ]); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should work with setEncoding()", () => { |
| 99 | + const readable = new Readable({ |
| 100 | + read() {} |
| 101 | + }); |
| 102 | + |
| 103 | + readable.setEncoding("utf8"); |
| 104 | + |
| 105 | + readable.push(DEF); |
| 106 | + readable.unshift(ABC); |
| 107 | + |
| 108 | + const out = readable.read(); |
| 109 | + expect(out).toBe("ABCDEF"); |
| 110 | + }); |
| 111 | +}); |
0 commit comments