Comparing version 1.1.2 to 1.1.3
# CHANGELOG | ||
## 1.1.3 - delivery @27/12/2019 | ||
- change engines in package | ||
- use stream.pipeline in stream helper to avoid memory leaks | ||
- update output helper | ||
- update readme | ||
## 1.1.2 - delivery @24/12/2019 | ||
@@ -4,0 +11,0 @@ |
/** | ||
* Output helper. | ||
* | ||
* - async checkOutput(output) -> Boolean or LibError | ||
* - async generateOutput({ svg, settings }) -> String|Stream|Buffer or LibError | ||
* - async check(output) -> Boolean throw LibError | ||
* - async generate({ svg, settings }) -> String|Stream|Buffer throw LibError | ||
*/ | ||
@@ -19,2 +19,3 @@ const { dirname } = require('path'); | ||
// check | ||
const check = async function check(output) { | ||
@@ -31,3 +32,3 @@ if (svgFile.test(output)) { | ||
if (e.code === 'ENOENT') { | ||
error.setMessage(`no such directory at '${dirPath}'`); | ||
error.setMessage(`no such directory at ${dirPath}`); | ||
} else { | ||
@@ -42,3 +43,3 @@ error.setMessage(e.message); | ||
const error = new LibError(LibError.Codes.INVALID_OUTPUT); | ||
error.setMessage(`no such directory at '${dirPath}'`); | ||
error.setMessage(`no such directory at ${dirPath}`); | ||
@@ -49,3 +50,3 @@ throw error; | ||
const error = new LibError(LibError.Codes.INVALID_OUTPUT); | ||
error.setMessage(`'${output}' is not a valid output. Must be ${types.join(', ')} or a valid file path with svg extension.`); | ||
error.setMessage(`${output} is not a valid output, must be ${types.join(', ')} or a valid file path with svg extension`); | ||
@@ -58,3 +59,4 @@ throw error; | ||
const generate = async function generate({ svg, settings }) { | ||
// generate | ||
const generate = async function generate({ svg, settings } = {}) { | ||
let svgOutput; | ||
@@ -70,3 +72,3 @@ | ||
try { | ||
rstream = await createReadable(svg, { encoding: settings.encoding }); | ||
rstream = createReadable(svg, { encoding: settings.encoding }); | ||
} catch (e) { | ||
@@ -82,3 +84,7 @@ const error = new LibError(LibError.Codes.READABLE_INTERNAL); | ||
try { | ||
await pipeToWritableFile(settings.output, rstream, { encoding: settings.encoding }); | ||
await pipeToWritableFile({ | ||
rstream, | ||
path: settings.output, | ||
options: { encoding: settings.encoding }, | ||
}); | ||
@@ -85,0 +91,0 @@ svgOutput = svg; |
@@ -10,15 +10,24 @@ /** | ||
* destroy, | ||
* } = {}) -> Promise (Readable or Error) | ||
* - pipeToWritableFile(path, rstream, { | ||
* flags, | ||
* encoding, | ||
* fd, | ||
* mode, | ||
* autoClose, | ||
* } = {}) -> Promise(Writable or Error) | ||
* } = {}) -> Readable throw Error | ||
* - pipeToWritableFile({ | ||
* path, | ||
* rstream, | ||
* options: { | ||
* flags, | ||
* encoding, | ||
* fd, | ||
* mode, | ||
* autoClose, | ||
* } = {}, | ||
* }) -> Promise | ||
*/ | ||
const { Readable } = require('stream'); | ||
const stream = require('stream'); | ||
const { promisify } = require('util'); | ||
const { createWriteStream } = require('fs'); | ||
const { is } = require('./object'); | ||
const LibError = require('../LibError'); | ||
const debug = require('../debug')('bitgener-stream-helper'); | ||
const pipeline = promisify(stream.pipeline); | ||
const readableDefaultOptions = { | ||
@@ -40,2 +49,3 @@ highWaterMark: 16384, | ||
// createReadable | ||
const createReadable = function createReadable(chunk, { | ||
@@ -48,46 +58,56 @@ highWaterMark, | ||
} = {}) { | ||
return new Promise((resolve, reject) => { | ||
const options = { | ||
highWaterMark: highWaterMark || readableDefaultOptions.highWaterMark, | ||
encoding: encoding || readableDefaultOptions.encoding, | ||
objectMode: objectMode || readableDefaultOptions.objectMode, | ||
read: is(Function, read) ? read : readableDefaultOptions.read, | ||
destroy: is(Function, destroy) ? destroy : readableDefaultOptions.destroy, | ||
}; | ||
const options = { | ||
highWaterMark: highWaterMark || readableDefaultOptions.highWaterMark, | ||
encoding: encoding || readableDefaultOptions.encoding, | ||
objectMode: objectMode || readableDefaultOptions.objectMode, | ||
read: is(Function, read) ? read : readableDefaultOptions.read, | ||
destroy: is(Function, destroy) ? destroy : readableDefaultOptions.destroy, | ||
}; | ||
const rstream = new Readable(options); | ||
const rstream = new stream.Readable(options); | ||
rstream.on('error', reject); | ||
// can throw TypeError | ||
rstream.push(chunk); | ||
rstream.push(null); | ||
rstream.push(chunk); | ||
rstream.push(null); | ||
resolve(rstream); | ||
}); | ||
return rstream; | ||
}; | ||
const pipeToWritableFile = function pipeToWritableFile(path, rstream, { | ||
flags, | ||
encoding, | ||
fd, | ||
mode, | ||
autoClose, | ||
} = {}) { | ||
return new Promise((resolve, reject) => { | ||
const options = { | ||
flags: flags || writableFileDefaultOptions.flags, | ||
encoding: encoding || writableFileDefaultOptions.encoding, | ||
fd: fd || writableFileDefaultOptions.fd, | ||
mode: mode || writableFileDefaultOptions.mode, | ||
autoClose: autoClose || writableFileDefaultOptions.autoClose, | ||
}; | ||
// pipeToWritableFile | ||
const pipeToWritableFile = async function pipeToWritableFile({ | ||
path, | ||
rstream, | ||
options: { | ||
flags, | ||
encoding, | ||
fd, | ||
mode, | ||
autoClose, | ||
} = {}, | ||
}) { | ||
const options = { | ||
flags: flags || writableFileDefaultOptions.flags, | ||
encoding: encoding || writableFileDefaultOptions.encoding, | ||
fd: fd || writableFileDefaultOptions.fd, | ||
mode: mode || writableFileDefaultOptions.mode, | ||
autoClose: autoClose || writableFileDefaultOptions.autoClose, | ||
}; | ||
const wstream = createWriteStream(path, options); | ||
const wstream = createWriteStream(path, options); | ||
rstream.on('error', reject); | ||
wstream.on('error', reject); | ||
wstream.on('close', () => resolve(true)); | ||
// listen to error events | ||
wstream.once('error', (e) => { | ||
const error = new LibError(LibError.Codes.WRITABLE_INTERNAL); | ||
error.setMessage(e.message); | ||
debug(error); | ||
}); | ||
rstream.pipe(wstream); | ||
rstream.once('error', (e) => { | ||
const error = new LibError(LibError.Codes.READABLE_INTERNAL); | ||
error.setMessage(e.message); | ||
debug(error); | ||
}); | ||
await pipeline(rstream, wstream); | ||
debug(`file ${path} created`); | ||
}; | ||
@@ -94,0 +114,0 @@ |
{ | ||
"name": "bitgener", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "A lightweight and zero-dependencies pure Node.js barcode library", | ||
@@ -12,4 +12,4 @@ "author": "Adrien Valcke", | ||
"engines": { | ||
"node": ">=8.17.0", | ||
"npm": ">=6.13.4" | ||
"node": ">=10.0.0", | ||
"npm": ">=5.6.0" | ||
}, | ||
@@ -16,0 +16,0 @@ "scripts": { |
@@ -193,2 +193,4 @@ <p align="center"> | ||
In these examples please prefer a well-known and tested asynchronous logger over the use of *console* module. | ||
##### 1D barcode | ||
@@ -223,4 +225,2 @@ | ||
console.log(ret); | ||
// shows | ||
} catch (e) { | ||
@@ -260,4 +260,2 @@ console.error(e.toString()); | ||
console.log(ret); | ||
// shows | ||
} catch (e) { | ||
@@ -272,8 +270,5 @@ console.error(e.toString()); | ||
```javascript | ||
const { createWriteStream } = require('fs'); | ||
const sharp = require('sharp'); | ||
const bitgener = require('bitgener'); | ||
const wstream = createWriteStream('sharped.png'); | ||
/** | ||
@@ -319,2 +314,7 @@ * Generic function to convert the svg generated from Bitgener | ||
try { | ||
const stream = require('stream'); | ||
const { promisify } = require('util'); | ||
const wstream = stream.createWriteStream('sharped.png'); | ||
const pipeline = promisify(stream.pipeline); | ||
const { | ||
@@ -349,5 +349,6 @@ svg: buffer, | ||
rstream.pipe(wstream); | ||
// listen to rstream and wstream error events ;) | ||
// listen to rstream and wstream events ;) | ||
// use pipeline to automatically clean up streams or you're exposing your code to memory leaks | ||
await pipeline(rstream, wstream); | ||
@@ -369,4 +370,20 @@ // ... | ||
## Error codes | ||
## Error | ||
### Object structure | ||
Errors emitted by Bitgener inherit the native Error prototype. | ||
```javascript | ||
{ | ||
name, | ||
code, | ||
message, | ||
stack, | ||
toString(), | ||
} | ||
``` | ||
### Codes | ||
<table style="text-align: center; vertical-align: center"> | ||
@@ -465,3 +482,3 @@ <tr> | ||
- Language: JavaScript ES6/ES7 | ||
- VM: NodeJS Carbon (v8.17.0) and higher | ||
- VM: NodeJS v10.0.0 and higher | ||
@@ -468,0 +485,0 @@ ## Debugging |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
196175
4891
518