Skip to content

Commit fce8c79

Browse files
committed
rebuild package for new release
1 parent 9b99aed commit fce8c79

File tree

8 files changed

+74
-30
lines changed

8 files changed

+74
-30
lines changed

dist/browser/bundle.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/browser/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node/browser-utils/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var _stream = require("stream");
1111
var _readableWebToNodeStream = require("readable-web-to-node-stream");
1212

1313
async function webToNodeStream(stream, size) {
14-
if (size == undefined || size == -1) {
14+
if (size === undefined || size === -1) {
1515
return new _readableWebToNodeStream.ReadableWebToNodeStream(stream);
1616
} else {
1717
const nodeStream = new _stream.Readable({

dist/node/file-base.js

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.computeHash = computeHash;
67
exports.File = void 0;
78

89
var _data = require("./data");
@@ -26,6 +27,15 @@ const {
2627
} = require('stream');
2728

2829
class File {
30+
constructor(descriptor, {
31+
basePath
32+
} = {}) {
33+
this._descriptor = descriptor;
34+
this._basePath = basePath;
35+
this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING;
36+
this._computedHashes = {};
37+
}
38+
2939
static load(pathOrDescriptor, {
3040
basePath,
3141
format
@@ -37,14 +47,6 @@ class File {
3747
});
3848
}
3949

40-
constructor(descriptor, {
41-
basePath
42-
} = {}) {
43-
this._descriptor = descriptor;
44-
this._basePath = basePath;
45-
this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING;
46-
}
47-
4850
get descriptor() {
4951
return this._descriptor;
5052
}
@@ -99,8 +101,22 @@ class File {
99101
})();
100102
}
101103

102-
async hash(hashType = 'md5', progress) {
103-
return _computeHash(await this.stream(), this.size, hashType, progress);
104+
async hash(hashType = 'md5', progress, cache = true) {
105+
if (cache && hashType in this._computedHashes) {
106+
if (typeof progress === 'function') {
107+
progress(100);
108+
}
109+
110+
return this._computedHashes[hashType];
111+
} else {
112+
let hash = await computeHash(await this.stream(), this.size, hashType, progress);
113+
114+
if (cache && this != null) {
115+
this._computedHashes[hashType] = hash;
116+
}
117+
118+
return hash;
119+
}
104120
}
105121

106122
async hashSha256(progress) {
@@ -137,20 +153,31 @@ class File {
137153
throw new Error(`We do not have a parser for that format: ${this.descriptor.format}`);
138154
}
139155

156+
getSample() {
157+
return new Promise(async (resolve, reject) => {
158+
let smallStream = await this.rows({
159+
size: 100
160+
});
161+
resolve(await (0, _streamToArray.default)(smallStream));
162+
});
163+
}
164+
140165
async addSchema() {
141166
if (this.displayName === 'FileInline') {
142167
this.descriptor.schema = await (0, _tableschema.infer)(this.descriptor.data);
143168
return;
144169
}
145170

146-
if (this.descriptor.format === 'xlsx' && this.descriptor.sample) {
171+
let sample = await this.getSample();
172+
173+
if (this.descriptor.format === 'xlsx' && sample) {
147174
let headers = 1;
148175

149-
if ((0, _lodash.isPlainObject)(this.descriptor.sample[0])) {
150-
headers = Object.keys(this.descriptor.sample[0]);
176+
if ((0, _lodash.isPlainObject)(sample[0])) {
177+
headers = Object.keys(sample[0]);
151178
}
152179

153-
this.descriptor.schema = await (0, _tableschema.infer)(this.descriptor.sample, {
180+
this.descriptor.schema = await (0, _tableschema.infer)(sample, {
154181
headers
155182
});
156183
return;
@@ -175,14 +202,18 @@ class File {
175202

176203
exports.File = File;
177204

178-
function _computeHash(fileStream, fileSize, algorithm, progress) {
205+
function computeHash(fileStream, fileSize, algorithm, progress, encoding = 'hex') {
179206
return new Promise((resolve, reject) => {
180207
let hash = _crypto.default.createHash(algorithm);
181208

182209
let offset = 0;
183210
let totalChunkSize = 0;
184211
let chunkCount = 0;
185212

213+
if (!['hex', 'latin1', 'binary', 'base64'].includes(encoding)) {
214+
throw new Error(`Invalid encoding value: ${encoding}; Expecting 'hex', 'latin1', 'binary' or 'base64'`);
215+
}
216+
186217
const _reportProgress = new Transform({
187218
transform(chunk, encoding, callback) {
188219
if (chunkCount % 20 == 0) {
@@ -206,7 +237,7 @@ function _computeHash(fileStream, fileSize, algorithm, progress) {
206237
chunkCount += 1;
207238
hash.update(chunk);
208239
}).on('end', function () {
209-
hash = hash.digest('hex');
240+
hash = hash.digest(encoding);
210241

211242
if (typeof progress === 'function') {
212243
progress(100);

dist/node/file-interface.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class FileInterface extends _fileBase.File {
3333
return this._encoding || _data.DEFAULT_ENCODING;
3434
}
3535

36-
async stream(size) {
36+
async stream({
37+
size
38+
} = {}) {
3739
return (0, _index.webToNodeStream)(await this.descriptor.stream(), size);
3840
}
3941

dist/node/file-local.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class FileLocal extends _fileBase.File {
2525
}
2626

2727
stream({
28-
end
28+
size
2929
} = {}) {
30+
let end = size;
3031
return _fs.default.createReadStream(this.path, {
3132
start: 0,
3233
end

dist/node/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ Object.defineProperty(exports, "File", {
3939
return _fileBase.File;
4040
}
4141
});
42+
Object.defineProperty(exports, "computeHash", {
43+
enumerable: true,
44+
get: function () {
45+
return _fileBase.computeHash;
46+
}
47+
});
4248
Object.defineProperty(exports, "FileInterface", {
4349
enumerable: true,
4450
get: function () {

dist/node/parser/csv.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ async function csvParser(file, {
2323
size
2424
} = {}) {
2525
const parseOptions = await getParseOptions(file, keyed);
26-
let stream = await file.stream(size);
26+
let stream = await file.stream({
27+
size
28+
});
2729

2830
if (file.descriptor.encoding.toLowerCase().replace('-', '') === 'utf8') {
2931
return stream.pipe((0, _csvParse.default)(parseOptions));
@@ -39,11 +41,13 @@ async function guessParseOptions(file) {
3941

4042
if (file.displayName === 'FileLocal') {
4143
const stream = await file.stream({
42-
end: 50000
44+
size: 50000
4345
});
4446
text = await (0, _streamToString.default)(stream);
4547
} else if (file.displayName === 'FileInterface') {
46-
let stream = await file.stream(10);
48+
let stream = await file.stream({
49+
size: 10
50+
});
4751
text = await (0, _streamToString.default)(stream);
4852
} else if (file.displayName === 'FileRemote') {
4953
const stream = await file.stream({

0 commit comments

Comments
 (0)