Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

io.js 3.1 KB

You have to be logged in to leave a comment. Sign In
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  1. const fs = require('fs');
  2. const stream = require('stream');
  3. const childProcess = require('child_process');
  4. const ProgressBar = require('progress');
  5. const path = require('path');
  6. const through = require('through2');
  7. function openFile(file, cb) {
  8. var bar;
  9. var consumed = 0;
  10. let res = new stream.Transform({
  11. transform(chunk, enc, callback) {
  12. if (bar) {
  13. bar.tick(chunk.length + consumed);
  14. consumed = 0;
  15. } else {
  16. consumed += chunk.length;
  17. }
  18. callback(null, chunk, enc);
  19. }
  20. });
  21. fs.stat(file, (err, stats) => {
  22. if (err) {
  23. if (cb) {
  24. return cb(err);
  25. } else {
  26. throw err;
  27. }
  28. }
  29. var size = stats.size;
  30. var bn = path.basename(file);
  31. bar = new ProgressBar(bn + ' [:bar] :percent :elapseds (ETA :etas)', {
  32. total: size,
  33. width: 30,
  34. renderThrottle: 100
  35. });
  36. var stream = fs.createReadStream(file);
  37. stream.pipe(res);
  38. if (cb) {
  39. cb(null, res);
  40. }
  41. });
  42. return res;
  43. }
  44. function externalDecompress(file) {
  45. let name = path.basename(file);
  46. let gz = childProcess.spawn('gzip', ['-d'], {
  47. stdio: ['pipe', 'pipe', process.stderr]
  48. });
  49. let pv = childProcess.spawn('pv', ['-N', name, file], {
  50. stdio: ['ignore', gz.stdin, process.stderr]
  51. });
  52. return gz.stdout;
  53. }
  54. function decodeLines(decode) {
  55. var buffer = Buffer.alloc(16 * 1024);
  56. var used = 0;
  57. var dcf = decode || ((b) => b.toString());
  58. return new stream.Transform({
  59. objectMode: true,
  60. transform(chunk, enc, callback) {
  61. if (typeof(chunk) === 'string') {
  62. chunk = new Buffer(chunk);
  63. }
  64. // find new line
  65. var idx = chunk.indexOf('\n');
  66. while (idx >= 0) {
  67. let buf = chunk.slice(0, idx);
  68. if (used) {
  69. buf = Buffer.concat([buffer.slice(0, used), buf]);
  70. used = 0;
  71. }
  72. try {
  73. let payload = dcf(buf);
  74. this.push(payload);
  75. } catch (e) {
  76. return callback(e);
  77. }
  78. if (idx < buf.length - 1) {
  79. buffer = buf.slice(idx + 1);
  80. }
  81. chunk = chunk.slice(idx + 1);
  82. idx = chunk.indexOf('\n');
  83. }
  84. if (chunk.length > 0) {
  85. if (used + chunk.length > buffer.length) {
  86. var nb = Buffer.alloc(Math.max(used + chunk.length, buffer.length * 2));
  87. buffer.copy(nb, 0, 0, used);
  88. buffer = nb;
  89. }
  90. chunk.copy(buffer, used);
  91. used += chunk.length;
  92. }
  93. callback();
  94. },
  95. flush(cb) {
  96. if (used) {
  97. this.push(dcf(buffer.slice(0, used)));
  98. }
  99. cb();
  100. }
  101. });
  102. }
  103. function decodeBadUnicode() {
  104. return through((chunk, enc, cb) => {
  105. const size = chunk.length;
  106. let nbuf = Buffer.alloc(size);
  107. let nbytes = 0;
  108. for (let i = 0; i < size; i++) {
  109. let u = chunk.readUInt8(i, true);
  110. if (u < 128) {
  111. nbuf.writeUInt8(u, nbytes, true);
  112. nbytes += 1;
  113. }
  114. }
  115. cb(null, chunk);
  116. });
  117. }
  118. module.exports = {
  119. openFile: openFile,
  120. decodeLines: decodeLines,
  121. decodeBadUnicode: decodeBadUnicode,
  122. externalDecompress: externalDecompress
  123. };
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...