Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,3 @@ | ||
require('iconv-lite').extendNodeEncodings(); | ||
var Dauria = function(){ | ||
@@ -5,2 +7,15 @@ if (!(this instanceof Dauria)) return new Dauria(); | ||
var urldecodeBuffer = function(urlencoded){ | ||
var arrBuffers = urlencoded.split( | ||
/(%[0-9A-Fa-f]{2})/ | ||
).map(function(fragment, idx){ | ||
if( idx % 2 === 0 ){ // simple string fragment's index: 0, 2, 4... | ||
return Buffer(fragment, 'binary'); | ||
} else { // regex-captured fragment's index: 1, 3, 5... | ||
return Buffer(fragment.replace(/%/g, ''), 'hex'); | ||
} | ||
}); | ||
return Buffer.concat(arrBuffers); | ||
}; | ||
Dauria.prototype.getBase64DataURI = function(sourceBuffer, MIME){ | ||
@@ -12,2 +27,73 @@ if( typeof MIME === 'undefined' ) MIME = 'application/octet-stream'; | ||
Dauria.prototype.parseDataURI = function(dataURI){ | ||
if( dataURI.indexOf('data:') !== 0 ){ | ||
throw new Error(this.errors.MISSING_PREFIX); | ||
} | ||
var commaSplit = dataURI.slice('data:'.length).split(','); | ||
if( commaSplit.length < 2 ) throw new Error(this.errors.MISSING_COMMA); | ||
var beforeData = commaSplit.shift(); | ||
var encodedData = commaSplit.join(','); | ||
var semicolonSplit = beforeData.split(/;\s*/); | ||
var base64 = false; | ||
if( | ||
semicolonSplit.length >= 2 && | ||
semicolonSplit[semicolonSplit.length - 1] === 'base64' | ||
){ | ||
base64 = true; | ||
semicolonSplit.pop(); | ||
} | ||
var decodedBuffer; | ||
if( base64 ){ | ||
decodedBuffer = Buffer(encodedData, 'base64'); | ||
} else { // not base64, i.e. urlencoded | ||
decodedBuffer = urldecodeBuffer(encodedData); | ||
} | ||
if( semicolonSplit.length === 1 && semicolonSplit[0] === '' ){ | ||
semicolonSplit = [ 'text/plain', 'charset=US-ASCII' ]; | ||
} | ||
var MIME = semicolonSplit[0]; | ||
var mediaType = semicolonSplit.join(';'); | ||
if( MIME.toLowerCase().indexOf('text/') !== 0 ){ // not a text | ||
return { | ||
'MIME': MIME, | ||
'mediaType': mediaType, | ||
'buffer': decodedBuffer, | ||
'text': null, | ||
'charset': null | ||
}; | ||
} | ||
// we have a text; determine its encoding: | ||
semicolonSplit.shift(); | ||
semicolonSplit = semicolonSplit.map(function(urlparam){ | ||
if( urlparam.toLowerCase().indexOf('charset=') !== 0 ){ | ||
return null; // not a charset parameter, drop it | ||
} | ||
var charset = urlparam.slice('charset='.length); | ||
return charset; | ||
}).filter(function(charset){ | ||
return charset !== null; | ||
}); | ||
if( semicolonSplit.length === 0 ) semicolonSplit = ['US-ASCII']; | ||
var decodedText; | ||
if( Buffer.isEncoding(semicolonSplit[0]) ){ | ||
decodedText = decodedBuffer.toString( semicolonSplit[0] ); | ||
} else { | ||
decodedText = null; | ||
} | ||
return { | ||
'MIME': MIME, | ||
'mediaType': mediaType, | ||
'buffer': decodedBuffer, | ||
'charset': semicolonSplit[0], | ||
'text': decodedText | ||
}; | ||
}; | ||
Dauria.prototype.errors = { | ||
MISSING_PREFIX: 'Cannot find "data:" in the beginning of the URL!', | ||
MISSING_COMMA: 'Cannot find a comma in the given URL!' | ||
}; | ||
module.exports = new Dauria(); |
{ | ||
"name": "dauria", | ||
"main": "dauria.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Node.js module for Data URI applications. It performs conversions between Node.js Buffers and RFC2397-compliant Data URIs.", | ||
@@ -13,2 +13,5 @@ "keywords": ["data uri", "data url", "data uris", "data urls", "rfc2397", "rfc 2397"], | ||
}, | ||
"dependencies": { | ||
"iconv-lite": "~0.4.3" | ||
}, | ||
"scripts": { | ||
@@ -15,0 +18,0 @@ "pretest": "jshint dauria.js test/", |
This Node.js module for <b>Da</b>ta <b>URI a</b>pplications is called **Dauria** (after a part of [Transbaikal](http://en.wikipedia.org/wiki/Transbaikal)). | ||
It performs conversions between Node.js Buffers and [RFC2397-compliant](http://tools.ietf.org/html/rfc2397) Data URIs. | ||
It performs conversions between Node.js [Buffers](http://nodejs.org/docs/latest/api/buffer.html) and [RFC2397-compliant](http://tools.ietf.org/html/rfc2397) Data URIs, or vice versa. | ||
@@ -19,3 +19,3 @@ ## Installing Dauria | ||
When you `require()` the installed module, you get an object that has the following method: | ||
When you `require()` the installed module, you get an object that has the following methods: | ||
@@ -28,4 +28,22 @@ ### getBase64DataURI(sourceBuffer, MIME) | ||
### parseDataURI(dataURI) | ||
Parses the given Data URI and returns an object with the following properties: | ||
* `MIME` — MIME content type in the form `type/subtype` as explained in [RFC2045 Section 5.2](http://tools.ietf.org/html/rfc2045#section-5.2). If not given in the URI, `MIME` becomes `'text/plain'` by default (as recommended by [RFC2397](http://tools.ietf.org/html/rfc2397) in section 2). | ||
* `mediaType` — MIME content type with the semicolon-separated list of parameters (if any) in the form `parameter=value` (some values may appear urlencoded, and Dauria does not decode them). If not given in the URI, `mediaType` becomes `'text/plain;charset=US-ASCII'` by default (as recommended by [RFC2397](http://tools.ietf.org/html/rfc2397) in section 2). | ||
* `buffer` — Node.js [Buffer](http://nodejs.org/docs/latest/api/buffer.html) containing the data decoded from the given Data URI. Hexadecimal URL encoding (such as `'%20'` for a whitespace) and base64 encoding are both supported (the latter must be indicated by the string `';base64'` before the first comma in the given Data URI). | ||
* `charset` — the value of the first `'charset=...'` parameter encountered in `mediaType`. If `mediaType` does not contain any charset parameters, `charset` becomes `'US-ASCII'`. However, if `MIME` does not start with `'text/'`, then `charset` becomes `null` regardless of any parameters. | ||
* `text` — a JavaScript string containing the text decoded from `buffer` using `charset`. However, if `MIME` does not start with `'text/'`, then `text` becomes `null`. It also becomes `null` if the [iconv-lite](https://github.com/ashtuchkin/iconv-lite) module does not know the encountered `charset`. | ||
If the given `dataURI` is not in fact a Data URI (does not start with `'data:'` or does not contain a comma), an error is thrown. | ||
## Testing Dauria | ||
[![(build testing status)](https://travis-ci.org/Mithgol/dauria.svg?branch=master)](https://travis-ci.org/Mithgol/dauria) | ||
The tests are not included in the npm package of the module (to keep it small). Use the version from GitHub. | ||
@@ -43,4 +61,6 @@ | ||
MIT license (see the `LICENSE` file), with the following exception: | ||
MIT license (see the `LICENSE` file), with the following exceptions: | ||
* The file `test/red-dot-5px.png` is taken from Wikipedia where it has been [released into the public domain.](http://en.wikipedia.org/wiki/File%3aRed-dot-5px.png) | ||
* The file `test/red-dot-5px.png` is taken from Wikipedia where it has been [released into the public domain](http://en.wikipedia.org/wiki/File%3aRed-dot-5px.png) by [Johan Elisson.](http://en.wikipedia.org/wiki/User%3aJohan_Elisson) | ||
* The file `test/larry.gif` is decoded from [RFC2397](http://tools.ietf.org/html/rfc2397) where it was given as an example. (RFC2397's Full Copyright Statement permits publishing and distribution of derivative works that assist in its implementation.) |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
10483
87
64
0
1
+ Addediconv-lite@~0.4.3
+ Addediconv-lite@0.4.24(transitive)
+ Addedsafer-buffer@2.1.2(transitive)