Skip to content

Commit

Permalink
rebuild package for new release
Browse files Browse the repository at this point in the history
  • Loading branch information
risenW committed Nov 25, 2020
1 parent 9b99aed commit fce8c79
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 30 deletions.
14 changes: 7 additions & 7 deletions dist/browser/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node/browser-utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _stream = require("stream");
var _readableWebToNodeStream = require("readable-web-to-node-stream");

async function webToNodeStream(stream, size) {
if (size == undefined || size == -1) {
if (size === undefined || size === -1) {
return new _readableWebToNodeStream.ReadableWebToNodeStream(stream);
} else {
const nodeStream = new _stream.Readable({
Expand Down
63 changes: 47 additions & 16 deletions dist/node/file-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.computeHash = computeHash;
exports.File = void 0;

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

class File {
constructor(descriptor, {
basePath
} = {}) {
this._descriptor = descriptor;
this._basePath = basePath;
this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING;
this._computedHashes = {};
}

static load(pathOrDescriptor, {
basePath,
format
Expand All @@ -37,14 +47,6 @@ class File {
});
}

constructor(descriptor, {
basePath
} = {}) {
this._descriptor = descriptor;
this._basePath = basePath;
this._descriptor.encoding = this.encoding || _data.DEFAULT_ENCODING;
}

get descriptor() {
return this._descriptor;
}
Expand Down Expand Up @@ -99,8 +101,22 @@ class File {
})();
}

async hash(hashType = 'md5', progress) {
return _computeHash(await this.stream(), this.size, hashType, progress);
async hash(hashType = 'md5', progress, cache = true) {
if (cache && hashType in this._computedHashes) {
if (typeof progress === 'function') {
progress(100);
}

return this._computedHashes[hashType];
} else {
let hash = await computeHash(await this.stream(), this.size, hashType, progress);

if (cache && this != null) {
this._computedHashes[hashType] = hash;
}

return hash;
}
}

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

getSample() {
return new Promise(async (resolve, reject) => {
let smallStream = await this.rows({
size: 100
});
resolve(await (0, _streamToArray.default)(smallStream));
});
}

async addSchema() {
if (this.displayName === 'FileInline') {
this.descriptor.schema = await (0, _tableschema.infer)(this.descriptor.data);
return;
}

if (this.descriptor.format === 'xlsx' && this.descriptor.sample) {
let sample = await this.getSample();

if (this.descriptor.format === 'xlsx' && sample) {
let headers = 1;

if ((0, _lodash.isPlainObject)(this.descriptor.sample[0])) {
headers = Object.keys(this.descriptor.sample[0]);
if ((0, _lodash.isPlainObject)(sample[0])) {
headers = Object.keys(sample[0]);
}

this.descriptor.schema = await (0, _tableschema.infer)(this.descriptor.sample, {
this.descriptor.schema = await (0, _tableschema.infer)(sample, {
headers
});
return;
Expand All @@ -175,14 +202,18 @@ class File {

exports.File = File;

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

let offset = 0;
let totalChunkSize = 0;
let chunkCount = 0;

if (!['hex', 'latin1', 'binary', 'base64'].includes(encoding)) {
throw new Error(`Invalid encoding value: ${encoding}; Expecting 'hex', 'latin1', 'binary' or 'base64'`);
}

const _reportProgress = new Transform({
transform(chunk, encoding, callback) {
if (chunkCount % 20 == 0) {
Expand All @@ -206,7 +237,7 @@ function _computeHash(fileStream, fileSize, algorithm, progress) {
chunkCount += 1;
hash.update(chunk);
}).on('end', function () {
hash = hash.digest('hex');
hash = hash.digest(encoding);

if (typeof progress === 'function') {
progress(100);
Expand Down
4 changes: 3 additions & 1 deletion dist/node/file-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class FileInterface extends _fileBase.File {
return this._encoding || _data.DEFAULT_ENCODING;
}

async stream(size) {
async stream({
size
} = {}) {
return (0, _index.webToNodeStream)(await this.descriptor.stream(), size);
}

Expand Down
3 changes: 2 additions & 1 deletion dist/node/file-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class FileLocal extends _fileBase.File {
}

stream({
end
size
} = {}) {
let end = size;
return _fs.default.createReadStream(this.path, {
start: 0,
end
Expand Down
6 changes: 6 additions & 0 deletions dist/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Object.defineProperty(exports, "File", {
return _fileBase.File;
}
});
Object.defineProperty(exports, "computeHash", {
enumerable: true,
get: function () {
return _fileBase.computeHash;
}
});
Object.defineProperty(exports, "FileInterface", {
enumerable: true,
get: function () {
Expand Down
10 changes: 7 additions & 3 deletions dist/node/parser/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ async function csvParser(file, {
size
} = {}) {
const parseOptions = await getParseOptions(file, keyed);
let stream = await file.stream(size);
let stream = await file.stream({
size
});

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

if (file.displayName === 'FileLocal') {
const stream = await file.stream({
end: 50000
size: 50000
});
text = await (0, _streamToString.default)(stream);
} else if (file.displayName === 'FileInterface') {
let stream = await file.stream(10);
let stream = await file.stream({
size: 10
});
text = await (0, _streamToString.default)(stream);
} else if (file.displayName === 'FileRemote') {
const stream = await file.stream({
Expand Down

0 comments on commit fce8c79

Please sign in to comment.