Skip to content

Commit 1f2adf5

Browse files
author
cemayan
committedFeb 13, 2019
initial commit
0 parents  commit 1f2adf5

7 files changed

+304
-0
lines changed
 

‎.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**/logs
2+
**/*.log
3+
node_modules
4+
.vscode
5+
.idea
6+
.DS_Store
7+
.cache-loader
8+
dist
9+
input

‎.npmignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.*.swp
2+
._*
3+
.DS_Store
4+
.git
5+
.hg
6+
.npmrc
7+
.lock-wscript
8+
.svn
9+
.wafpickle-*
10+
config.gypi
11+
CVS
12+
npm-debug.log
13+
src
14+
input

‎README.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Async Chunk Reader
2+
3+
This library allows you to read large amounts of data in chunks.
4+
5+
## Install
6+
```console
7+
npm install --save async-chunk-reader
8+
```
9+
10+
## API
11+
---
12+
13+
**init(parameters : InitParameters)**
14+
15+
> input : InitParameters
16+
17+
- **chunk_size** : String
18+
- **input_file** : String | Stream
19+
- **encoding** : String
20+
21+
**get()**
22+
23+
> output : Async Iterator
24+
25+
26+
## Import
27+
28+
### with require :
29+
```javascript
30+
const reader = require('async-chunk-reader')
31+
```
32+
33+
### with import :
34+
```javascript
35+
import * as reader from "async-chunk-reader"
36+
```
37+
38+
## Usage
39+
---
40+
41+
### with path :
42+
43+
```javascript
44+
async function main(){
45+
46+
const x = await reader
47+
.init({
48+
chunk_size: 100000,
49+
input_file: 'input/mobile_network_201805.csv.gz'
50+
})
51+
.get()
52+
53+
for await(let chunk of x){
54+
console.log(chunk.length)
55+
}
56+
}
57+
58+
main();
59+
60+
```
61+
62+
### with stream :
63+
64+
```javascript
65+
async function main(){
66+
67+
const x = await reader
68+
.init({
69+
input_file: process.stdin
70+
})
71+
.get()
72+
73+
for await(let chunk of x){
74+
console.log(chunk.length)
75+
}
76+
}
77+
78+
main();
79+
80+
```
81+
82+
83+
### with string :
84+
85+
```javascript
86+
async function main(){
87+
88+
const x = await reader
89+
.init({
90+
input_file: "Some string"
91+
})
92+
.get()
93+
94+
for await(let chunk of x){
95+
console.log(chunk.length)
96+
}
97+
}
98+
99+
main();
100+
101+
```
102+
103+
104+
105+

‎package-lock.json

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "async-chunk-reader",
3+
"version": "0.1.1",
4+
"description": "Async chunk reader",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "tsc"
9+
},
10+
"homepage": "https://github.com/cemayan/async-chunk-reader",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/cemayan/async-chunk-reader.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/cemayan/async-chunk-reader/issues"
17+
},
18+
"keywords": [
19+
"chunk",
20+
"iterator",
21+
"stream-reader",
22+
"async-reader",
23+
"stream"
24+
],
25+
"license": "MIT",
26+
"author": {
27+
"name": "Cem Ayan",
28+
"email": "cem_ayan@outlook.com",
29+
"url": "http://github.com/cemayan"
30+
},
31+
"devDependencies": {
32+
"typescript": "^3.2.2",
33+
"@types/node": "^10.12.18"
34+
}
35+
}

‎src/index.ts

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import fs from "fs";
2+
import path from "path"
3+
import readline from "readline"
4+
import zlib from "zlib"
5+
import { Stream } from "stream";
6+
import events from "events";
7+
8+
const gunzip = zlib.createGunzip();
9+
10+
export interface InitParameters{
11+
chunk_size : number,
12+
encoding : string,
13+
input_file : String|Stream,
14+
}
15+
16+
export interface ReaderParameters{
17+
input_file : String|Stream
18+
}
19+
20+
21+
events.EventEmitter.defaultMaxListeners = 1000;
22+
const default_global_settings = {chunk_size : 10000, encoding : "utf8" };
23+
24+
export function init(parameters : InitParameters){
25+
default_global_settings.chunk_size = parameters.chunk_size;
26+
default_global_settings.encoding = parameters.encoding;
27+
28+
return { get(){
29+
30+
let fileStream;
31+
32+
if(typeof(parameters.input_file) == "string"){
33+
if(path.extname(<string>parameters.input_file)==".gz"){
34+
fileStream = fs.createReadStream(<string>parameters.input_file).pipe(gunzip);
35+
}
36+
else{
37+
fileStream = fs.createReadStream(<string>parameters.input_file);
38+
}
39+
40+
fileStream.on('error', (err) => {
41+
throw err;
42+
})
43+
44+
return readLineByStream(fileStream);
45+
}
46+
else if(parameters.input_file instanceof Stream ){
47+
fileStream = parameters.input_file;
48+
49+
fileStream.on('error', (err) => {
50+
throw err;
51+
})
52+
53+
return readLineByStream(fileStream);
54+
}
55+
return this;
56+
57+
}
58+
}
59+
}
60+
61+
62+
function readLineByStream(fileStream) {
63+
64+
return {
65+
[Symbol.iterator]() {
66+
return this;
67+
},
68+
next() {
69+
const rl = readline.createInterface({
70+
input: fileStream,
71+
});
72+
73+
let async_itr = rl[Symbol.asyncIterator]();
74+
let data = readMoreData(async_itr);
75+
return {value:data, done : false}
76+
}
77+
}
78+
}
79+
80+
async function readMoreData(asyncIterator : AsyncIterator<any>):Promise<Array<any>>{
81+
82+
let count = 0;
83+
let arr:Array<object> = [];
84+
85+
while(count < default_global_settings.chunk_size){
86+
const resolved_obj = await asyncIterator.next();
87+
if(!resolved_obj.done){
88+
arr.push(resolved_obj);
89+
}
90+
count++;
91+
}
92+
return arr;
93+
}
94+
95+

‎tsconfig.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "3.2.2",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"lib": ["es2015", "es2016","esnext.asynciterable"],
6+
"target": "es6",
7+
"types": ["node"],
8+
"skipLibCheck": true,
9+
"strictNullChecks": true,
10+
"alwaysStrict": true,
11+
"noImplicitThis": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"noImplicitReturns": true,
14+
"noUnusedLocals": true,
15+
"experimentalDecorators": true,
16+
"sourceMap": true,
17+
"esModuleInterop": true,
18+
"rootDir": "src",
19+
"outDir": "dist"
20+
},
21+
"exclude": [
22+
"**/node_modules",
23+
//"**/*.d.ts"
24+
]
25+
}
26+

0 commit comments

Comments
 (0)