-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
fileReader.ts
27 lines (23 loc) · 1017 Bytes
/
fileReader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import * as fs from 'fs';
import * as zlib from 'zlib';
import { PassThrough, pipeline as pipelineCB } from 'stream';
import { promisify } from 'util';
const pipeline = promisify(pipelineCB);
const defaultEncoding: BufferEncoding = 'utf8';
export async function readFile(filename: string, encoding: BufferEncoding = defaultEncoding): Promise<string> {
const isGzip = filename.match(/\.gz$/i);
const fileStream = fs.createReadStream(filename);
const zip = isGzip ? zlib.createGunzip() : new PassThrough();
const t = pipeline(fileStream, zip, streamToText(encoding));
return await t;
}
function streamToText(encoding: BufferEncoding): (source: fs.ReadStream) => Promise<string> {
return async function (source: fs.ReadStream): Promise<string> {
const chunks: string[] = [];
source.setEncoding(encoding); // Work with strings rather than `Buffer`s.
for await (const chunk of source) {
chunks.push(chunk);
}
return chunks.join('');
};
}