Comparing version 0.0.1 to 0.1.0
82
index.js
@@ -0,1 +1,4 @@ | ||
const util = require('util'); | ||
const Stream = require('stream'); | ||
const REGEX = { | ||
@@ -20,3 +23,3 @@ dataurl: /data:(.*?)(?:;charset=(.*?))?(;base64)?,(.+)/i, | ||
dataurl.convert = function (options) { | ||
function makeHeader(options) { | ||
var dataUrlTemplate = 'data:' + options.mimetype; | ||
@@ -28,6 +31,81 @@ if (options.charset) | ||
dataUrlTemplate += ','; | ||
dataUrlTemplate += options.data.toString('base64'); | ||
return dataUrlTemplate; | ||
} | ||
function makeDataUrlSync(header, data) { | ||
return (header + Buffer(data).toString('base64')); | ||
} | ||
function ConvertStream(options) { | ||
if (!(this instanceof ConvertStream)) | ||
return new ConvertStream(options); | ||
this.encoded = true && options.encoded !== false; | ||
this.charset = options.charset; | ||
this.mimetype = options.mimetype; | ||
this.header = makeHeader(options); | ||
this.headerEmitted = false; | ||
this.readable = true; | ||
this.writable = true; | ||
this._buffer = Buffer(0); | ||
this.once('pipe', function (src) { | ||
this.pause = src.pause.bind(src); | ||
this.resume = src.resume.bind(src); | ||
}.bind(this)); | ||
} | ||
util.inherits(ConvertStream, Stream); | ||
ConvertStream.prototype._emit = Stream.prototype.emit; | ||
ConvertStream.prototype.emitData = function emitData(data) { | ||
if (!this.headerEmitted) { | ||
this.emit('data', this.header); | ||
this.headerEmitted = true; | ||
this.emitData = this.emit.bind(this, 'data'); | ||
} | ||
this.emit('data', data); | ||
}; | ||
ConvertStream.prototype.convert = function convert(data) { | ||
if (!this.encoded) | ||
return data; | ||
data = Buffer.concat([this._buffer, Buffer(data)]); | ||
if (data.length < 3) { | ||
this._buffer = data; | ||
return; | ||
} | ||
const length = data.length; | ||
const remainderSize = length % 3; | ||
const offset = length - remainderSize; | ||
const current = data.slice(0, offset); | ||
this._buffer = data.slice(offset); | ||
return current.toString('base64'); | ||
}; | ||
ConvertStream.prototype.finish = function finish() { | ||
const data = this._buffer; | ||
if (!data.length) | ||
return; | ||
return this.emitData( | ||
this.encoded ? data.toString('base64') : data | ||
); | ||
}; | ||
ConvertStream.prototype.write = function write(data) { | ||
var output = this.convert(data); | ||
if (output) | ||
this.emitData(output); | ||
}; | ||
ConvertStream.prototype.end = function end(data) { | ||
if (data) | ||
this.write(data); | ||
this.finish(); | ||
this.readable = false; | ||
this.writable = false; | ||
this.emit('end'); | ||
}; | ||
dataurl.stream = function (options) { | ||
return new ConvertStream(options); | ||
}; | ||
dataurl.convert = function (options) { | ||
const header = makeHeader(options); | ||
return makeDataUrlSync(header, options.data); | ||
}; | ||
dataurl.format = dataurl.convert; | ||
dataurl.parse = function (string) { | ||
@@ -34,0 +112,0 @@ var match; |
{ | ||
"name": "dataurl", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Tools for dealing with DataURLs", | ||
@@ -22,3 +22,4 @@ "main": "index.js", | ||
"datauri", | ||
"dataurl" | ||
"dataurl", | ||
"stream" | ||
], | ||
@@ -25,0 +26,0 @@ "author": "Brian J. Brennan", |
@@ -1,8 +0,48 @@ | ||
# dataurl | ||
[![Build Status](https://secure.travis-ci.org/brianloveswords/dataurl)](http://travis-ci.org/brianloveswords/dataurl) | ||
# dataurl [![Build Status](https://travis-ci.org/brianloveswords/dataurl.png?branch=master)](https://travis-ci.org/brianloveswords/dataurl) | ||
# Install | ||
```bash | ||
$ npm install dataurl | ||
``` | ||
# Usage | ||
## dataurl.parse(string) | ||
Parses a dataurl string. Returns an object with three properties: | ||
* `data` <Buffer>: unencoded data | ||
* `mimetype` <String>: mimetype of the data, something like `'image/png'` | ||
* `charset` <String>: charset of the data, if specified | ||
If the input string isn't a valid dataURL, returns `false`. | ||
## dataurl.stream(options) | ||
Creates a Read/Write Stream for encoding data as a DataURL. | ||
Options expects up to three properties: | ||
* `mimetype` <String>: Required | ||
* `charset` <String>: Optional | ||
* `encoded` <Boolean>: Optional | ||
Resulting stream will emit a data event for the header, then emit 'data' | ||
events for each chunk (base64 encoded, if necessary) as they pass | ||
through. | ||
Example: | ||
```js | ||
fs.createReadStream(pathToSomeImage).pipe( | ||
dataurl.stream({ mimetype: 'image/png'}) | ||
).pipe(process.stderr, {end: false}); | ||
``` | ||
## dataurl.format(options)<br>dataurl.convert(options) | ||
Converts some data to a dataurl string. Options expects up to four properties | ||
* `data` <Buffer>: Required | ||
* `mimetype` <String>: Required | ||
* `charset` <String>: Optional | ||
* `encoded` <Boolean>: Optional, whether to base64 encode the data. Defaults to `true` | ||
# License | ||
@@ -9,0 +49,0 @@ |
const test = require('tap').test; | ||
const dataurl = require('../'); | ||
const fs = require('fs'); | ||
const resevoir = require('./resevoir'); | ||
@@ -8,2 +9,6 @@ const TEST_FILE = fs.readFileSync(__dirname + '/reddot.png'); | ||
function createTestStream(options) { | ||
return fs.createReadStream(__dirname + '/reddot.png', options); | ||
} | ||
test('dataurl.parse', function (t) { | ||
@@ -40,3 +45,3 @@ const file = dataurl.parse(TEST_DATAURL); | ||
test('dataurl.convert', function (t) { | ||
test('dataurl.convert, sync', function (t) { | ||
const result = dataurl.convert({ | ||
@@ -50,1 +55,24 @@ data: TEST_FILE, | ||
}); | ||
test('dataurl.convert, stream', function (t) { | ||
const inputStream = createTestStream(); | ||
const ds = dataurl.stream({mimetype:'image/png'}); | ||
const bucket = resevoir(); | ||
ds.pipe(bucket); | ||
inputStream.pipe(ds) | ||
bucket.on('done', function (contents) { | ||
t.same(contents.toString(), TEST_DATAURL, 'should have correct contents'); | ||
t.end(); | ||
}); | ||
}); | ||
test('dataurl.convert, slow stream', function (t) { | ||
const inputStream = createTestStream({ bufferSize: 1 }); | ||
const ds = dataurl.stream({mimetype:'image/png'}); | ||
inputStream.pipe(ds).pipe(resevoir()).on('done', function (contents) { | ||
t.same(contents.toString(), TEST_DATAURL, 'should have correct contents'); | ||
t.end(); | ||
}); | ||
}); | ||
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
9765
9
205
74