@ridi/parser-core
Advanced tools
Comparing version 0.3.0-alpha.3 to 0.3.0-alpha.4
@@ -6,3 +6,2 @@ 'use strict'; | ||
}); | ||
exports.defaultBufferSize = undefined; | ||
@@ -13,6 +12,2 @@ var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
var _createClass2 = require('babel-runtime/helpers/createClass'); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _freeze = require('babel-runtime/core-js/object/freeze'); | ||
@@ -28,11 +23,8 @@ | ||
var Status = (0, _freeze2.default)({ | ||
PARSE: 'parse', | ||
UNZIP: 'unzip', | ||
READ: 'read' | ||
var Purpose = (0, _freeze2.default)({ | ||
READ_IN_ZIP: 'read_in_zip', | ||
READ_IN_DIR: 'read_in_dir', | ||
WRITE: 'write' | ||
}); | ||
// https://nodejs.org/api/stream.html#stream_new_stream_readable_options | ||
var defaultBufferSize = exports.defaultBufferSize = 16384; | ||
var CryptoProvider = function () { | ||
@@ -45,59 +37,19 @@ function CryptoProvider() { | ||
} | ||
// ex) | ||
// const key = ...; | ||
// this.cryptor = new Cryptor(Modes.ECB, { key }); | ||
} | ||
/** | ||
* Prefer buffer size | ||
*/ | ||
/** | ||
* Invoked when parser.parse or parser.readItem(s) or parser._unzip is called | ||
* @param {string} oldStatus | ||
* @param {string} newStatus | ||
*/ | ||
CryptoProvider.prototype.onStatusChanged = function onStatusChanged(oldStatus, newStatus) {} // eslint-disable-line | ||
// ex) | ||
// if (oldStatus !== newStatus) { | ||
// const key = ...; | ||
// const iv = ...; | ||
// this.cryptor = new Cryptor(Modes.CFB, { key, iv }); | ||
// } | ||
/** | ||
* Should execute encrypt or decrypt by condition if needed | ||
* @param {Buffer} data | ||
* Create or reuse Cryptor by condition | ||
* @param {string} filePath | ||
* @param {string} purpose | ||
* @returns {Cryptor} | ||
*/ | ||
; | ||
CryptoProvider.prototype.run = function run(data, filePath) { | ||
// eslint-disable-line | ||
// ex) | ||
// if (condition1) { | ||
// return this.encrypt(data, filePath) | ||
// } else if (condition2) { | ||
// return this.decrypt(data, filePath) | ||
// } | ||
// return data; | ||
(0, _errors.mustOverride)(); | ||
}; | ||
/** | ||
* Should encrypt data | ||
* @param {Buffer} data | ||
* @param {string} filePath | ||
*/ | ||
CryptoProvider.prototype.encrypt = function encrypt(data, filePath) { | ||
CryptoProvider.prototype.getCryptor = function getCryptor(filePath, purpose) { | ||
// eslint-disable-line | ||
// ex) | ||
// if (condition) { | ||
// return cryptor1.encrypt(data); | ||
// return new Cryptor(...); | ||
// } else { | ||
// return cryptor2.encrypt(data); | ||
// return new Cryptor(...); | ||
// } | ||
@@ -108,50 +60,29 @@ (0, _errors.mustOverride)(); | ||
/** | ||
* Should decrypt data | ||
* Should execute encrypt or decrypt by condition if needed | ||
* @param {Buffer} data | ||
* @param {string} filePath | ||
* @param {string} purpose | ||
*/ | ||
CryptoProvider.prototype.decrypt = function decrypt(data, filePath) { | ||
CryptoProvider.prototype.run = function run(data, filePath, purpose) { | ||
// eslint-disable-line | ||
// ex) | ||
// if (condition) { | ||
// return cryptor1.decrypt(data); | ||
// } else { | ||
// return cryptor2.decrypt(data); | ||
// const cryptor = this.getCryptor(filePath, status); | ||
// const padding = Cryptor.Padding.PKCS7 | ||
// if (condition1) { | ||
// return cryptor.encrypt(data, padding) | ||
// } else if (condition2) { | ||
// return cryptor.decrypt(data, padding) | ||
// } | ||
// return data; | ||
(0, _errors.mustOverride)(); | ||
}; | ||
(0, _createClass3.default)(CryptoProvider, [{ | ||
key: 'bufferSize', | ||
get: function get() { | ||
return defaultBufferSize; | ||
} | ||
/** | ||
* Update status by parser | ||
*/ | ||
}, { | ||
key: 'status', | ||
set: function set(newStatus) { | ||
var oldStatus = this.status; | ||
this._status = newStatus; | ||
this.onStatusChanged(oldStatus, newStatus); | ||
} | ||
/** | ||
* Get status | ||
*/ | ||
, | ||
get: function get() { | ||
return this._status; | ||
} | ||
}]); | ||
return CryptoProvider; | ||
}(); | ||
CryptoProvider.Status = Status; | ||
CryptoProvider.Purpose = Purpose; | ||
exports.default = CryptoProvider; | ||
exports.default = CryptoProvider; | ||
module.exports = exports.default; |
@@ -52,2 +52,3 @@ 'use strict'; | ||
var Padding = (0, _freeze2.default)({ | ||
AUTO: 'auto', | ||
PKCS7: 'pkcs7', | ||
@@ -99,2 +100,8 @@ NONE: 'none' | ||
var checkDataType = function checkDataType(data) { | ||
if (!(0, _stringContains2.default)(['Buffer', 'Uint8Array', 'Array'], (0, _typecheck.getType)(data))) { | ||
throw (0, _errors.createError)(_errors2.default.EINVAL, 'bytes type', 'reason', 'require Buffer or Uint8Array or Array'); | ||
} | ||
}; | ||
var Cryptor = function () { | ||
@@ -135,3 +142,2 @@ function Cryptor(mode, config) { | ||
(0, _validateOptions2.default)(config, mode.configTypes, true); | ||
this.padding = config.padding || Padding.NONE; | ||
this.operator = this.makeOperator(mode, config); | ||
@@ -173,6 +179,6 @@ (0, _freeze2.default)(this); | ||
Cryptor.prototype.encrypt = function encrypt(bytes) { | ||
if (!(0, _stringContains2.default)(['Buffer', 'Uint8Array', 'Array'], (0, _typecheck.getType)(bytes))) { | ||
throw (0, _errors.createError)(_errors2.default.EINVAL, 'bytes type', 'reason', 'require Buffer or Uint8Array or Array'); | ||
} | ||
if (this.padding === Padding.PKCS7) { | ||
var padding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Padding.NONE; | ||
checkDataType(bytes); | ||
if (padding === Padding.PKCS7 || padding === Padding.AUTO && bytes.length % 16 !== 0) { | ||
return this.operator.encrypt(pkcs7.pad(bytes)); | ||
@@ -184,9 +190,15 @@ } | ||
Cryptor.prototype.decrypt = function decrypt(bytes) { | ||
if (!(0, _stringContains2.default)(['Buffer', 'Uint8Array', 'Array'], (0, _typecheck.getType)(bytes))) { | ||
throw (0, _errors.createError)(_errors2.default.EINVAL, 'bytes type', 'reason', 'require Buffer or Uint8Array or Array'); | ||
var padding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Padding.NONE; | ||
checkDataType(bytes); | ||
var decryptedBytes = Buffer.from(this.operator.decrypt(bytes)); | ||
if (padding === Padding.PKCS7 || padding === Padding.AUTO) { | ||
try { | ||
return pkcs7.strip(decryptedBytes); | ||
} catch (e) { | ||
if (padding !== Padding.AUTO) { | ||
throw e; | ||
} | ||
} | ||
} | ||
var decryptedBytes = this.operator.decrypt(bytes); | ||
if (this.padding === Padding.PKCS7) { | ||
return pkcs7.strip(decryptedBytes); | ||
} | ||
return decryptedBytes; | ||
@@ -193,0 +205,0 @@ }; |
@@ -33,2 +33,4 @@ 'use strict'; | ||
var _CryptoProvider2 = _interopRequireDefault(_CryptoProvider); | ||
var _pathUtil = require('./pathUtil'); | ||
@@ -97,4 +99,3 @@ | ||
return new _promise2.default(function (resolve, reject) { | ||
var bufferSize = (0, _typecheck.isExists)(cryptoProvider) ? cryptoProvider.bufferSize : _CryptoProvider.defaultBufferSize; | ||
var stream = _fsExtra2.default.createReadStream(fullPath, { highWaterMark: bufferSize, encoding: 'binary' }); | ||
var stream = _fsExtra2.default.createReadStream(fullPath, { encoding: 'binary' }); | ||
var buffer = Buffer.from([]); | ||
@@ -104,6 +105,5 @@ stream.on('data', function (chunk) { | ||
if ((0, _typecheck.isExists)(cryptoProvider)) { | ||
buffer = Buffer.concat([buffer, cryptoProvider.run(chunk, fullPath)]); | ||
} else { | ||
buffer = Buffer.concat([buffer, chunk]); | ||
chunk = cryptoProvider.run(chunk, fullPath, _CryptoProvider2.default.Purpose.READ_IN_DIR); | ||
} | ||
buffer = Buffer.concat([buffer, chunk]); | ||
}).on('error', function (e) { | ||
@@ -155,4 +155,5 @@ return reject(e); | ||
// TODO: When input is Buffer. | ||
if ((0, _typecheck.isExists)(cryptoProvider)) { | ||
input = cryptoProvider.run(_fsExtra2.default.readFileSync(input), input); | ||
input = cryptoProvider.run(_fsExtra2.default.readFileSync(input), input, _CryptoProvider2.default.Purpose.READ_IN_DIR); | ||
} | ||
@@ -159,0 +160,0 @@ _context2.next = 4; |
@@ -35,12 +35,12 @@ 'use strict'; | ||
var buffer = Buffer.from([]); | ||
entry.stream().setEncoding('binary').on('data', function (chunk) { | ||
entry.stream() // is DuplexStream. | ||
.on('error', function (e) { | ||
_Logger2.default.error(e); | ||
reject(e); | ||
}).setEncoding('binary').on('data', function (chunk) { | ||
chunk = Buffer.from(chunk, 'binary'); | ||
if ((0, _typecheck.isExists)(_this.cryptoProvider)) { | ||
buffer = Buffer.concat([buffer, _this.cryptoProvider.run(chunk, entry.path)]); | ||
} else { | ||
buffer = Buffer.concat([buffer, chunk]); | ||
chunk = _this.cryptoProvider.run(chunk, entry.path, _CryptoProvider2.default.Purpose.READ_IN_ZIP); | ||
} | ||
}).on('error', function (e) { | ||
_Logger2.default.error(e); | ||
reject(e); | ||
buffer = Buffer.concat([buffer, chunk]); | ||
}).on('finish', function () { | ||
@@ -96,15 +96,15 @@ return resolve(buffer); | ||
}; | ||
entry.stream().on('data', function (chunk) { | ||
writeStream.on('error', onError); | ||
writeStream.on('close', function () { | ||
return resolve(); | ||
}); | ||
entry.stream() // is DuplexStream. | ||
.on('error', onError).on('data', function (chunk) { | ||
if ((0, _typecheck.isExists)(_this2.cryptoProvider)) { | ||
writeStream.write(_this2.cryptoProvider.run(chunk, entry.path)); | ||
} else { | ||
writeStream.write(chunk); | ||
chunk = _this2.cryptoProvider.run(chunk, entry.path, _CryptoProvider2.default.Purpose.WRITE); | ||
} | ||
}).on('error', onError).on('finish', function () { | ||
writeStream.write(chunk); | ||
}).on('finish', function () { | ||
return writeStream.close(); | ||
}); | ||
writeStream.on('error', onError); | ||
writeStream.on('close', function () { | ||
return resolve(); | ||
}); | ||
}); | ||
@@ -168,2 +168,6 @@ }; | ||
var _CryptoProvider = require('./CryptoProvider'); | ||
var _CryptoProvider2 = _interopRequireDefault(_CryptoProvider); | ||
var _Logger = require('./Logger'); | ||
@@ -170,0 +174,0 @@ |
{ | ||
"name": "@ridi/parser-core", | ||
"version": "0.3.0-alpha.3", | ||
"version": "0.3.0-alpha.4", | ||
"description": "Utilities for parsers", | ||
@@ -5,0 +5,0 @@ "author": { |
38839
954