through2 4.0.1 → 5.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/dependabot.yml +20 -0
- package/.github/workflows/test-and-release.yml +72 -0
- package/CHANGELOG.md +9 -0
- package/README.md +314 -83
- package/example-ndjson.js +94 -0
- package/package.json +122 -21
- package/test/test.js +404 -0
- package/test/web.js +191 -0
- package/through2.js +267 -57
- package/tsconfig.json +25 -0
- package/types/through2.d.ts +91 -0
- package/types/through2.d.ts.map +1 -0
- package/types/web.d.ts +42 -0
- package/types/web.d.ts.map +1 -0
- package/web.js +241 -0
package/package.json
CHANGED
|
@@ -1,39 +1,140 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "through2",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "Tiny utilities for inserting transformation logic into Node.js stream and Web Streams pipelines",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./through2.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./through2.js",
|
|
11
|
+
"types": "./types/through2.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./web": {
|
|
14
|
+
"import": "./web.js",
|
|
15
|
+
"types": "./types/web.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "./types/through2.d.ts",
|
|
6
19
|
"scripts": {
|
|
7
|
-
"test:node": "hundreds mocha test/test.js",
|
|
8
|
-
"test:browser": "node -e 'process.exit(process.version.startsWith(\"v8.\") ? 0 : 1)' || polendina --cleanup --runner=mocha test/test.js",
|
|
9
|
-
"test": "npm run lint && npm run test:node && npm run test:browser",
|
|
10
20
|
"lint": "standard",
|
|
11
|
-
"
|
|
21
|
+
"build:types": "tsc --build",
|
|
22
|
+
"build": "npm run build:types",
|
|
23
|
+
"test:node": "polendina-node bare-async test/test.js test/web.js",
|
|
24
|
+
"test:browser": "polendina --cleanup --runner=bare-async test/test.js test/web.js",
|
|
25
|
+
"test:ci": "npm run lint && npm run test:node && npm run test:browser",
|
|
26
|
+
"test": "npm run lint && npm run test:node"
|
|
12
27
|
},
|
|
13
28
|
"repository": {
|
|
14
29
|
"type": "git",
|
|
15
30
|
"url": "https://github.com/rvagg/through2.git"
|
|
16
31
|
},
|
|
32
|
+
"homepage": "https://github.com/rvagg/through2",
|
|
17
33
|
"keywords": [
|
|
18
34
|
"stream",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
35
|
+
"transform",
|
|
36
|
+
"transformstream",
|
|
37
|
+
"web-streams",
|
|
38
|
+
"async-iterable",
|
|
39
|
+
"pipeline"
|
|
22
40
|
],
|
|
23
41
|
"author": "Rod Vagg <r@va.gg> (https://github.com/rvagg)",
|
|
24
|
-
"license": "MIT",
|
|
25
42
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"readable-stream": "3"
|
|
43
|
+
"readable-stream": "^4.7.0"
|
|
28
44
|
},
|
|
29
45
|
"devDependencies": {
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
46
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
47
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
48
|
+
"@semantic-release/git": "^10.0.1",
|
|
49
|
+
"@semantic-release/github": "^12.0.6",
|
|
50
|
+
"@semantic-release/npm": "^13.1.5",
|
|
51
|
+
"@semantic-release/release-notes-generator": "^14.1.0",
|
|
52
|
+
"@types/node": "^25.6.0",
|
|
53
|
+
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
54
|
+
"polendina": "^3.2.21",
|
|
55
|
+
"semantic-release": "^25.0.3",
|
|
56
|
+
"standard": "^17.1.2",
|
|
57
|
+
"typescript": "^6.0.3"
|
|
58
|
+
},
|
|
59
|
+
"release": {
|
|
60
|
+
"branches": [
|
|
61
|
+
"master"
|
|
62
|
+
],
|
|
63
|
+
"plugins": [
|
|
64
|
+
[
|
|
65
|
+
"@semantic-release/commit-analyzer",
|
|
66
|
+
{
|
|
67
|
+
"preset": "conventionalcommits",
|
|
68
|
+
"releaseRules": [
|
|
69
|
+
{
|
|
70
|
+
"breaking": true,
|
|
71
|
+
"release": "major"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"revert": true,
|
|
75
|
+
"release": "patch"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"type": "feat",
|
|
79
|
+
"release": "minor"
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
"type": "fix",
|
|
83
|
+
"release": "patch"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"type": "chore",
|
|
87
|
+
"release": "patch"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"type": "docs",
|
|
91
|
+
"release": "patch"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"type": "test",
|
|
95
|
+
"release": "patch"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"scope": "no-release",
|
|
99
|
+
"release": false
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
[
|
|
105
|
+
"@semantic-release/release-notes-generator",
|
|
106
|
+
{
|
|
107
|
+
"preset": "conventionalcommits",
|
|
108
|
+
"presetConfig": {
|
|
109
|
+
"types": [
|
|
110
|
+
{
|
|
111
|
+
"type": "feat",
|
|
112
|
+
"section": "Features"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"type": "fix",
|
|
116
|
+
"section": "Bug Fixes"
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"type": "chore",
|
|
120
|
+
"section": "Trivial Changes"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"type": "docs",
|
|
124
|
+
"section": "Trivial Changes"
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
"type": "test",
|
|
128
|
+
"section": "Tests"
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
"@semantic-release/changelog",
|
|
135
|
+
"@semantic-release/npm",
|
|
136
|
+
"@semantic-release/github",
|
|
137
|
+
"@semantic-release/git"
|
|
138
|
+
]
|
|
38
139
|
}
|
|
39
140
|
}
|
package/test/test.js
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { Readable } from 'readable-stream'
|
|
2
|
+
import through2, {
|
|
3
|
+
transform,
|
|
4
|
+
objectTransform,
|
|
5
|
+
transformer
|
|
6
|
+
} from '../through2.js'
|
|
7
|
+
|
|
8
|
+
const decoder = new TextDecoder('ascii')
|
|
9
|
+
const encoder = new TextEncoder()
|
|
10
|
+
|
|
11
|
+
const eq = (actual, expected, msg) => {
|
|
12
|
+
if (actual !== expected) {
|
|
13
|
+
throw new Error(`${msg || 'eq'}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const deepEq = (actual, expected, msg) => {
|
|
18
|
+
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
|
|
19
|
+
throw new Error(`${msg || 'deepEq'}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const ok = (val, msg) => {
|
|
24
|
+
if (!val) throw new Error(msg || 'expected truthy')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function randomBytes (len) {
|
|
28
|
+
const bytes = new Uint8Array(len)
|
|
29
|
+
globalThis.crypto.getRandomValues(bytes)
|
|
30
|
+
return bytes
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function collect (stream) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
/** @type {Uint8Array[]} */
|
|
36
|
+
const chunks = []
|
|
37
|
+
stream.on('data', (chunk) => chunks.push(chunk))
|
|
38
|
+
stream.on('end', () => {
|
|
39
|
+
let total = 0
|
|
40
|
+
for (const c of chunks) total += c.length
|
|
41
|
+
const out = new Uint8Array(total)
|
|
42
|
+
let i = 0
|
|
43
|
+
for (const c of chunks) { out.set(c, i); i += c.length }
|
|
44
|
+
resolve(out)
|
|
45
|
+
})
|
|
46
|
+
stream.on('error', reject)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function collectObjects (stream) {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const objs = []
|
|
53
|
+
stream.on('data', (o) => objs.push(o))
|
|
54
|
+
stream.on('end', () => resolve(objs))
|
|
55
|
+
stream.on('error', reject)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function classicByteTransform () {
|
|
60
|
+
const stream = transform(function (chunk, _enc, cb) {
|
|
61
|
+
if (!this._i) this._i = 97
|
|
62
|
+
else this._i++
|
|
63
|
+
const out = new Uint8Array(chunk.length)
|
|
64
|
+
out.fill(this._i)
|
|
65
|
+
this.push(out)
|
|
66
|
+
cb()
|
|
67
|
+
})
|
|
68
|
+
const done = collect(stream)
|
|
69
|
+
stream.write(randomBytes(10))
|
|
70
|
+
stream.write(randomBytes(5))
|
|
71
|
+
stream.write(randomBytes(10))
|
|
72
|
+
stream.end()
|
|
73
|
+
eq(decoder.decode(await done), 'aaaaaaaaaabbbbbcccccccccc')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function classicNoopTransform () {
|
|
77
|
+
const stream = transform()
|
|
78
|
+
const done = collect(stream)
|
|
79
|
+
stream.end(encoder.encode('eeee'))
|
|
80
|
+
eq(decoder.decode(await done), 'eeee')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function classicFlush () {
|
|
84
|
+
const stream = transform(
|
|
85
|
+
function (chunk, _enc, cb) {
|
|
86
|
+
if (!this._i) this._i = 97
|
|
87
|
+
else this._i++
|
|
88
|
+
const out = new Uint8Array(chunk.length)
|
|
89
|
+
out.fill(this._i)
|
|
90
|
+
this.push(out)
|
|
91
|
+
cb()
|
|
92
|
+
},
|
|
93
|
+
function (cb) {
|
|
94
|
+
this.push(encoder.encode('end'))
|
|
95
|
+
cb()
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
const done = collect(stream)
|
|
99
|
+
stream.write(randomBytes(10))
|
|
100
|
+
stream.write(randomBytes(5))
|
|
101
|
+
stream.write(randomBytes(10))
|
|
102
|
+
stream.end()
|
|
103
|
+
eq(decoder.decode(await done), 'aaaaaaaaaabbbbbccccccccccend')
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function classicCallbackShorthand () {
|
|
107
|
+
const stream = transform((chunk, _enc, cb) => cb(null, chunk))
|
|
108
|
+
const done = collect(stream)
|
|
109
|
+
stream.end(encoder.encode('hello'))
|
|
110
|
+
eq(decoder.decode(await done), 'hello')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function asyncReturnedValuePushed () {
|
|
114
|
+
const stream = transform(async (_chunk, _enc) => encoder.encode('X'))
|
|
115
|
+
const done = collect(stream)
|
|
116
|
+
stream.write(new Uint8Array([1]))
|
|
117
|
+
stream.write(new Uint8Array([2]))
|
|
118
|
+
stream.end()
|
|
119
|
+
eq(decoder.decode(await done), 'XX')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function asyncUndefinedSkipsEmission () {
|
|
123
|
+
const stream = objectTransform(async (chunk) => chunk % 2 === 0 ? chunk : undefined)
|
|
124
|
+
const done = collectObjects(stream)
|
|
125
|
+
for (let i = 0; i < 6; i++) stream.write(i)
|
|
126
|
+
stream.end()
|
|
127
|
+
deepEq(await done, [0, 2, 4])
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export async function asyncRejectedPropagatesError () {
|
|
131
|
+
const stream = transform(async () => { throw new Error('boom') })
|
|
132
|
+
stream.write(new Uint8Array([1]))
|
|
133
|
+
await new Promise((resolve, reject) => {
|
|
134
|
+
stream.on('error', (err) => {
|
|
135
|
+
try { eq(err.message, 'boom'); resolve() } catch (e) { reject(e) }
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function asyncgenOneToOne () {
|
|
141
|
+
const stream = objectTransform(async function * (source) {
|
|
142
|
+
for await (const chunk of source) yield chunk * 2
|
|
143
|
+
})
|
|
144
|
+
const done = collectObjects(stream)
|
|
145
|
+
for (let i = 1; i <= 4; i++) stream.write(i)
|
|
146
|
+
stream.end()
|
|
147
|
+
deepEq(await done, [2, 4, 6, 8])
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function asyncgenFanOut () {
|
|
151
|
+
const stream = objectTransform(async function * (source) {
|
|
152
|
+
for await (const chunk of source) {
|
|
153
|
+
yield chunk
|
|
154
|
+
yield chunk
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
const done = collectObjects(stream)
|
|
158
|
+
stream.write('a')
|
|
159
|
+
stream.write('b')
|
|
160
|
+
stream.end()
|
|
161
|
+
deepEq(await done, ['a', 'a', 'b', 'b'])
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function asyncgenFilter () {
|
|
165
|
+
const stream = objectTransform(async function * (source) {
|
|
166
|
+
for await (const chunk of source) {
|
|
167
|
+
if (chunk % 2 === 0) yield chunk
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
const done = collectObjects(stream)
|
|
171
|
+
for (let i = 0; i < 6; i++) stream.write(i)
|
|
172
|
+
stream.end()
|
|
173
|
+
deepEq(await done, [0, 2, 4])
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function asyncgenWithClassicFlush () {
|
|
177
|
+
const stream = objectTransform(
|
|
178
|
+
async function * (source) {
|
|
179
|
+
for await (const chunk of source) yield chunk
|
|
180
|
+
},
|
|
181
|
+
function (cb) { this.push('END'); cb() }
|
|
182
|
+
)
|
|
183
|
+
const done = collectObjects(stream)
|
|
184
|
+
stream.write('a')
|
|
185
|
+
stream.write('b')
|
|
186
|
+
stream.end()
|
|
187
|
+
deepEq(await done, ['a', 'b', 'END'])
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function asyncgenThrownPropagates () {
|
|
191
|
+
const stream = objectTransform(async function * () {
|
|
192
|
+
throw new Error('gen-boom')
|
|
193
|
+
})
|
|
194
|
+
stream.write('x')
|
|
195
|
+
stream.end()
|
|
196
|
+
await new Promise((resolve, reject) => {
|
|
197
|
+
stream.on('error', (err) => {
|
|
198
|
+
try { eq(err.message, 'gen-boom'); resolve() } catch (e) { reject(e) }
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function objectTransformPassthrough () {
|
|
204
|
+
const stream = objectTransform(function (chunk, _enc, cb) {
|
|
205
|
+
this.push({ out: chunk.in + 1 })
|
|
206
|
+
cb()
|
|
207
|
+
})
|
|
208
|
+
const done = collectObjects(stream)
|
|
209
|
+
stream.write({ in: 1 })
|
|
210
|
+
stream.write({ in: 2 })
|
|
211
|
+
stream.write({ in: 3 })
|
|
212
|
+
stream.end()
|
|
213
|
+
deepEq(await done, [{ out: 2 }, { out: 3 }, { out: 4 }])
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export async function objectTransformFromReadable () {
|
|
217
|
+
const source = Readable.from([
|
|
218
|
+
{ temp: -2.2, unit: 'F' },
|
|
219
|
+
{ temp: -40, unit: 'F' },
|
|
220
|
+
{ temp: 212, unit: 'F' },
|
|
221
|
+
{ temp: 22, unit: 'C' }
|
|
222
|
+
])
|
|
223
|
+
const conv = objectTransform(async (record) => {
|
|
224
|
+
if (record.unit === 'F') {
|
|
225
|
+
record = { temp: ((record.temp - 32) * 5) / 9, unit: 'C' }
|
|
226
|
+
}
|
|
227
|
+
return record
|
|
228
|
+
})
|
|
229
|
+
source.pipe(conv)
|
|
230
|
+
const out = await collectObjects(conv)
|
|
231
|
+
deepEq(out.map((r) => Math.round(r.temp)), [-19, -40, 100, 22])
|
|
232
|
+
ok(out.every((r) => r.unit === 'C'))
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function transformerReusableFactory () {
|
|
236
|
+
const Th = transformer(function (chunk, _enc, cb) {
|
|
237
|
+
if (!this._i) this._i = 97
|
|
238
|
+
else this._i++
|
|
239
|
+
const out = new Uint8Array(chunk.length)
|
|
240
|
+
out.fill(this._i)
|
|
241
|
+
this.push(out)
|
|
242
|
+
cb()
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
const a = new Th()
|
|
246
|
+
const b = Th()
|
|
247
|
+
|
|
248
|
+
const da = collect(a); const db = collect(b)
|
|
249
|
+
a.write(randomBytes(3)); a.write(randomBytes(2)); a.end()
|
|
250
|
+
b.write(randomBytes(2)); b.write(randomBytes(3)); b.end()
|
|
251
|
+
|
|
252
|
+
eq(decoder.decode(await da), 'aaabb')
|
|
253
|
+
eq(decoder.decode(await db), 'aabbb')
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export async function transformerOptionsMerge () {
|
|
257
|
+
const Th = transformer({ objectMode: true, peek: true }, function (chunk, _enc, cb) {
|
|
258
|
+
ok(this.options.peek, 'options visible inside transform')
|
|
259
|
+
this.push({ out: chunk.in + 1 })
|
|
260
|
+
cb()
|
|
261
|
+
})
|
|
262
|
+
const inst = Th()
|
|
263
|
+
const done = collectObjects(inst)
|
|
264
|
+
inst.write({ in: 1 })
|
|
265
|
+
inst.write({ in: 2 })
|
|
266
|
+
inst.end()
|
|
267
|
+
deepEq(await done, [{ out: 2 }, { out: 3 }])
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function transformerOverrideOptions () {
|
|
271
|
+
const Th = transformer(function (chunk, _enc, cb) {
|
|
272
|
+
this.push({ out: chunk.in + 1 })
|
|
273
|
+
cb()
|
|
274
|
+
})
|
|
275
|
+
const inst = Th({ objectMode: true })
|
|
276
|
+
const done = collectObjects(inst)
|
|
277
|
+
inst.write({ in: 10 })
|
|
278
|
+
inst.end()
|
|
279
|
+
deepEq(await done, [{ out: 11 }])
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export async function transformerAsyncSupported () {
|
|
283
|
+
const Th = transformer({ objectMode: true }, async (chunk) => chunk * 10)
|
|
284
|
+
const inst = Th()
|
|
285
|
+
const done = collectObjects(inst)
|
|
286
|
+
inst.write(1); inst.write(2); inst.end()
|
|
287
|
+
deepEq(await done, [10, 20])
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export async function legacyDefault () {
|
|
291
|
+
const stream = through2((chunk, _enc, cb) => cb(null, chunk))
|
|
292
|
+
const done = collect(stream)
|
|
293
|
+
stream.end(encoder.encode('legacy'))
|
|
294
|
+
eq(decoder.decode(await done), 'legacy')
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export async function legacyDefaultObj () {
|
|
298
|
+
const stream = through2.obj(function (chunk, _enc, cb) {
|
|
299
|
+
this.push({ out: chunk.in + 1 })
|
|
300
|
+
cb()
|
|
301
|
+
})
|
|
302
|
+
const done = collectObjects(stream)
|
|
303
|
+
stream.write({ in: 1 })
|
|
304
|
+
stream.end()
|
|
305
|
+
deepEq(await done, [{ out: 2 }])
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export async function legacyDefaultCtor () {
|
|
309
|
+
const Th = through2.ctor({ objectMode: true }, function (chunk, _enc, cb) {
|
|
310
|
+
this.push({ out: chunk.in + 1 })
|
|
311
|
+
cb()
|
|
312
|
+
})
|
|
313
|
+
const inst = new Th()
|
|
314
|
+
const done = collectObjects(inst)
|
|
315
|
+
inst.write({ in: 5 })
|
|
316
|
+
inst.end()
|
|
317
|
+
deepEq(await done, [{ out: 6 }])
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export async function transformerAsyncgenSupported () {
|
|
321
|
+
// Each call gets its own generator pipeline.
|
|
322
|
+
const Doubled = transformer({ objectMode: true }, async function * (source) {
|
|
323
|
+
for await (const x of source) yield x * 2
|
|
324
|
+
})
|
|
325
|
+
const a = Doubled()
|
|
326
|
+
const b = Doubled()
|
|
327
|
+
const da = collectObjects(a); const db = collectObjects(b)
|
|
328
|
+
a.write(1); a.write(2); a.end()
|
|
329
|
+
b.write(10); b.write(20); b.end()
|
|
330
|
+
deepEq(await da, [2, 4])
|
|
331
|
+
deepEq(await db, [20, 40])
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Regression: with low readable HWM, async-gen fan-out used to deadlock
|
|
335
|
+
// awaiting `drain` (a writable event that never fires here).
|
|
336
|
+
export async function asyncgenDownstreamBackpressure () {
|
|
337
|
+
const t = objectTransform({ highWaterMark: 1 }, async function * (source) {
|
|
338
|
+
// eslint-disable-next-line no-unused-vars
|
|
339
|
+
for await (const _ of source) for (let i = 0; i < 20; i++) yield i
|
|
340
|
+
})
|
|
341
|
+
t.write('start'); t.end()
|
|
342
|
+
await new Promise((resolve) => setTimeout(resolve, 50)) // delay consumption
|
|
343
|
+
const out = await collectObjects(t)
|
|
344
|
+
deepEq(out, Array.from({ length: 20 }, (_, i) => i))
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Slow generator + many fast writes must propagate backpressure upstream
|
|
348
|
+
// (drain awaits) instead of growing an internal queue without bound.
|
|
349
|
+
export async function asyncgenWritableBackpressure () {
|
|
350
|
+
const t = objectTransform({ highWaterMark: 4 }, async function * (source) {
|
|
351
|
+
for await (const x of source) {
|
|
352
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
353
|
+
yield x
|
|
354
|
+
}
|
|
355
|
+
})
|
|
356
|
+
let drainsAwaited = 0
|
|
357
|
+
const writePromise = (async () => {
|
|
358
|
+
for (let i = 0; i < 20; i++) {
|
|
359
|
+
if (!t.write(i)) {
|
|
360
|
+
drainsAwaited++
|
|
361
|
+
await new Promise((resolve) => t.once('drain', resolve))
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
t.end()
|
|
365
|
+
})()
|
|
366
|
+
const out = await collectObjects(t)
|
|
367
|
+
await writePromise
|
|
368
|
+
deepEq(out, Array.from({ length: 20 }, (_, i) => i))
|
|
369
|
+
ok(drainsAwaited > 0, 'expected drain awaits, got 0')
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Mid-stream destroy must release pending awaits so the consumer loop exits.
|
|
373
|
+
export async function asyncgenDestroyMidStream () {
|
|
374
|
+
const t = objectTransform(async function * (source) {
|
|
375
|
+
for await (const x of source) { yield x; yield x }
|
|
376
|
+
})
|
|
377
|
+
const seen = []
|
|
378
|
+
t.on('data', (d) => {
|
|
379
|
+
seen.push(d)
|
|
380
|
+
if (seen.length === 2) t.destroy()
|
|
381
|
+
})
|
|
382
|
+
t.write(1); t.write(2); t.write(3)
|
|
383
|
+
await new Promise((resolve) => t.once('close', resolve))
|
|
384
|
+
ok(seen.length >= 2)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Destroy must finalize the user's async generator: its `finally` block
|
|
388
|
+
// must run so resources (file handles, subscriptions, etc.) can be released.
|
|
389
|
+
export async function asyncgenDestroyRunsFinally () {
|
|
390
|
+
let finalized = false
|
|
391
|
+
const t = objectTransform(async function * (source) {
|
|
392
|
+
try {
|
|
393
|
+
for await (const x of source) yield x
|
|
394
|
+
} finally {
|
|
395
|
+
finalized = true
|
|
396
|
+
}
|
|
397
|
+
})
|
|
398
|
+
t.write('a')
|
|
399
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
400
|
+
t.destroy()
|
|
401
|
+
await new Promise((resolve) => t.once('close', resolve))
|
|
402
|
+
await new Promise((resolve) => setTimeout(resolve, 20)) // let finally settle
|
|
403
|
+
ok(finalized, 'generator finally must run on destroy')
|
|
404
|
+
}
|