Add fetch and stream to temporary file
This commit is contained in:
47
src/index.ts
47
src/index.ts
@@ -1 +1,46 @@
|
||||
import { log } from './logger'
|
||||
import { log } from './logger.js'
|
||||
import { Readable } from 'node:stream'
|
||||
import { createWriteStream } from 'node:fs'
|
||||
import { finished } from 'node:stream/promises'
|
||||
|
||||
import { getLatestPackageName, getRequestStream } from './sources/gitea.js'
|
||||
import { processPisStream } from './process.js'
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const packageInfo = await getLatestPackageName();
|
||||
log('INFO', `Latest PIS Package: ${packageInfo.name}`);
|
||||
|
||||
if (!packageInfo.assets[0]?.browser_download_url) {
|
||||
log('ERROR', `No attachments found for release ${packageInfo.name}`);
|
||||
process.exit(9);
|
||||
}
|
||||
|
||||
const inputStream: Readable = await getRequestStream(packageInfo.assets[0].browser_download_url);
|
||||
|
||||
const outputPath = './review_output.ndjson';
|
||||
log('DEBUG', `Processing stream to: ${outputPath}`);
|
||||
|
||||
await writeToTemporaryFile(processPisStream(inputStream), outputPath);
|
||||
log('DEBUG', 'Done');
|
||||
} catch (err) {
|
||||
log('ERROR', 'Fatal error in pipeline: ', err);
|
||||
process.exit(7);
|
||||
}
|
||||
}
|
||||
|
||||
async function writeToTemporaryFile(generator: AsyncGenerator<any>, path: string) {
|
||||
const writer = createWriteStream(path);
|
||||
for await (const record of generator) {
|
||||
const line = JSON.stringify(record) + '\n';
|
||||
|
||||
if (!writer.write(line)) {
|
||||
await new Promise((resolve) => writer.once('drain', resolve));
|
||||
}
|
||||
}
|
||||
|
||||
writer.end();
|
||||
return finished(writer);
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user