-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5405 from snyk/fix/stream-dep-graphs
fix: write dep-graph payloads to stdout stream
- Loading branch information
Showing
8 changed files
with
159 additions
and
45 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
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,37 @@ | ||
import { Readable } from 'stream'; | ||
|
||
export class ConcatStream extends Readable { | ||
private current: Readable | undefined; | ||
private queue: Readable[] = []; | ||
|
||
constructor(...streams: Readable[]) { | ||
super({ objectMode: false }); // Adjust objectMode if needed | ||
this.queue.push(...streams); | ||
} | ||
|
||
append(...streams: Readable[]): void { | ||
this.queue.push(...streams); | ||
if (!this.current) { | ||
this._read(); | ||
} | ||
} | ||
|
||
_read(size?: number): void { | ||
if (this.current) { | ||
return; | ||
} | ||
|
||
this.current = this.queue.shift(); | ||
if (!this.current) { | ||
this.push(null); | ||
return; | ||
} | ||
|
||
this.current.on('data', (chunk) => this.push(chunk)); | ||
this.current.on('end', () => { | ||
this.current = undefined; | ||
this._read(size); | ||
}); | ||
this.current.on('error', (err) => this.emit('error', err)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
test/jest/unit/lib/ecosystems/__snapshots__/common.spec.ts.snap
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,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`printUnmanagedDepGraph fn should print the dep-graph 1`] = ` | ||
"DepGraph data: | ||
{"schemaVersion":"1.2.0","pkgManager":{"name":"cpp"},"pkgs":[{"id":"[email protected]","info":{"name":"root-node","version":"0.0.0"}},{"id":"https://ftp.gnu.org|[email protected]","info":{"name":"https://ftp.gnu.org|cpio","version":"2.12"}}],"graph":{"rootNodeId":"root-node","nodes":[{"nodeId":"root-node","pkgId":"[email protected]","deps":[{"nodeId":"https://ftp.gnu.org|[email protected]"}]},{"nodeId":"https://ftp.gnu.org|[email protected]","pkgId":"https://ftp.gnu.org|[email protected]","deps":[]}]}} | ||
DepGraph target: | ||
foo/bar | ||
DepGraph end | ||
" | ||
`; |
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,31 @@ | ||
import { Readable, Writable } from 'stream'; | ||
|
||
import { ConcatStream } from '../../../../src/lib/stream'; | ||
|
||
describe('ConcatStream', () => { | ||
it('should create a readable stream', () => { | ||
const stream = new ConcatStream(); | ||
|
||
expect(stream).toBeInstanceOf(Readable); | ||
}); | ||
|
||
it('should concatenate readable streams', async () => { | ||
const stream = new ConcatStream(); | ||
const chunks = jest.fn(); | ||
const out = new Writable({ | ||
write: (chunk, enc, done) => { | ||
chunks(chunk.toString()); | ||
done(); | ||
}, | ||
}); | ||
|
||
stream.append(Readable.from('foo'), Readable.from('bar')); | ||
|
||
await new Promise((res) => { | ||
stream.pipe(out).on('finish', res); | ||
}); | ||
|
||
expect(chunks).toHaveBeenCalledWith('foo'); | ||
expect(chunks).toHaveBeenCalledWith('bar'); | ||
}); | ||
}); |