-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pipe typings, add AsyncIterable fromDOMStream and toDOMStream imp…
…lementations (#266) * test(from-spec): fix Observable typings in from-spec * fix(asynciterable-pipe): fix asynciterable pipe typings fix #265 * feat(asynciterable-toDOMStream): add AsyncIterable#toDOMStream implementation * feat(asynciterable-fromDOMStream): add AsyncIterable.fromDOMStream implementation * chore(cleanup): remove dead comment, update dependencies * fix(asynciterable-toNodeStream): the iterator should complete so the tests can pass
- Loading branch information
1 parent
9d66807
commit 2e00c95
Showing
19 changed files
with
738 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import '../asynciterablehelpers'; | ||
import { AsyncIterable, toDOMStream } from '../Ix'; | ||
|
||
(() => { | ||
if (!toDOMStream || process.env.TEST_DOM_STREAMS !== 'true') { | ||
return test('not testing node streams because process.env.TEST_DOM_STREAMS !== "true"', () => { | ||
/**/ | ||
}); | ||
} | ||
|
||
const stringsItr = () => AsyncIterable.from([1, 2, 3]).map(i => `${i}`); | ||
const buffersItr = () => stringsItr().map(val => Buffer.from(val)); | ||
const objectsItr = () => stringsItr().map(val => ({ val })); | ||
const compare = <T>(a: T, b: T) => { | ||
let aVal = ArrayBuffer.isView(a) ? `${Buffer.from(a.buffer, a.byteOffset, a.byteLength)}` : a; | ||
let bVal = ArrayBuffer.isView(b) ? `${Buffer.from(b.buffer, b.byteOffset, b.byteLength)}` : b; | ||
// poor man's deep-equals | ||
try { | ||
expect(aVal).toEqual(bVal); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
describe(`AsyncIterable#toDOMStream`, () => { | ||
describe(`DefaultController`, () => { | ||
const expectedStrings = ['1', '2', '3']; | ||
const expectedObjects = expectedStrings.map(val => ({ val })); | ||
const expectedBuffers = expectedStrings.map(x => Buffer.from(x)); | ||
test(`yields Strings`, async () => { | ||
const expected = AsyncIterable.from(expectedStrings); | ||
const actual = stringsItr().toDOMStream(); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
test(`yields Buffers`, async () => { | ||
const expected = AsyncIterable.from(expectedBuffers); | ||
const actual = buffersItr().toDOMStream(); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
test(`yields Objects`, async () => { | ||
const expected = AsyncIterable.from(expectedObjects); | ||
const actual = objectsItr().toDOMStream(); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
}); | ||
|
||
describe(`ReadableByteStreamController (byobRequest)`, () => { | ||
const expectedStrings = ['123']; | ||
const expectedBuffers = expectedStrings.map(x => Buffer.from(x)); | ||
test(`yields Strings`, async () => { | ||
const expected = AsyncIterable.from(expectedBuffers); | ||
const actual = stringsItr() | ||
.map(x => Buffer.from(x)) | ||
.toDOMStream({ type: 'bytes' }); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
test(`yields Buffers`, async () => { | ||
const expected = AsyncIterable.from(expectedBuffers); | ||
const actual = buffersItr().toDOMStream({ type: 'bytes' }); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
}); | ||
|
||
describe(`ReadableByteStreamController (autoAllocateChunkSize)`, () => { | ||
const expectedStrings = ['123']; | ||
const expectedBuffers = expectedStrings.map(x => Buffer.from(x)); | ||
test(`yields Strings`, async () => { | ||
const expected = AsyncIterable.from(expectedBuffers); | ||
const actual = stringsItr() | ||
.map(x => Buffer.from(x)) | ||
.toDOMStream({ type: 'bytes', autoAllocateChunkSize: 1024 }); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
test(`yields Buffers`, async () => { | ||
const expected = AsyncIterable.from(expectedBuffers); | ||
const actual = buffersItr().toDOMStream({ type: 'bytes', autoAllocateChunkSize: 1024 }); | ||
await expect(actual).toEqualStream(expected, compare); | ||
}); | ||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import '../asynciterablehelpers'; | ||
import { Readable, ReadableOptions } from 'stream'; | ||
import { fromDOMStream, AsyncIterable } from '../Ix'; | ||
|
||
(() => { | ||
if (!fromDOMStream || process.env.TEST_DOM_STREAMS !== 'true') { | ||
return test('not testing node streams because process.env.TEST_DOM_STREAMS !== "true"', () => { | ||
/**/ | ||
}); | ||
} | ||
|
||
/* tslint:disable */ | ||
const { toStream } = require('web-stream-tools').default; | ||
|
||
class Counter extends Readable { | ||
private _index: number; | ||
private _max: number; | ||
|
||
constructor(options?: ReadableOptions) { | ||
super(options); | ||
this._max = 3; | ||
this._index = 0; | ||
} | ||
|
||
_read() { | ||
this.push(++this._index > this._max ? null : `${this._index}`); | ||
} | ||
} | ||
|
||
const compare = <T>(a: T, b: T) => { | ||
let aVal = ArrayBuffer.isView(a) ? `${Buffer.from(a.buffer, a.byteOffset, a.byteLength)}` : a; | ||
let bVal = ArrayBuffer.isView(b) ? `${Buffer.from(b.buffer, b.byteOffset, b.byteLength)}` : b; | ||
// poor man's deep-equals | ||
try { | ||
expect(aVal).toEqual(bVal); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
describe(`AsyncIterable#fromDOMStream`, () => { | ||
test('objectMode: true', async () => { | ||
const c = toStream(new Counter({ objectMode: true })); | ||
const xs = fromDOMStream(c) as AsyncIterable<string>; | ||
const expected = AsyncIterable.from(['1', '2', '3']); | ||
await expect(xs).toEqualStream(expected, compare); | ||
}); | ||
|
||
test('objectMode: false', async () => { | ||
const c = toStream(new Counter({ objectMode: false })); | ||
const xs = fromDOMStream(c) as AsyncIterable<Buffer>; | ||
const expected = AsyncIterable.from(['1', '2', '3'].map(s => Buffer.from(s))); | ||
await expect(xs).toEqualStream(expected, compare); | ||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.