Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Commit

Permalink
Bake to iTXt, allow double baking.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Nov 6, 2013
1 parent f2fab1f commit 3b94cb2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"bin": "bin/oven",
"dependencies": {
"request": "~2.11.4",
"streampng": "0.1.6",
"streampng": "0.1.7",
"optimist": "~0.3.5",
"cheerio": "~0.12.3",
"concat-stream": "~1.1.0",
Expand Down
23 changes: 10 additions & 13 deletions png.js
Expand Up @@ -17,9 +17,8 @@ function bake(options, callback) {
if (!data)
return callback(new Error('must pass a `data` or `url` option'));

if (typeof data === 'object') {
if (typeof data === 'object')
data = JSON.stringify(data);
}

const png = streampng(buffer);
const chunk = createChunk(data);
Expand All @@ -40,13 +39,9 @@ function bake(options, callback) {
png.on('end', function () {
if (hadError) return;

if (existingChunk) {
const msg = util.format('This image already has a chunk with the `%s` keyword (contains: %j)', KEYWORD, chunk.text);
const error = new Error(msg);
error.code = 'IMAGE_ALREADY_BAKED';
error.contents = existingChunk.text;
return callback(error);
}
if (existingChunk)
existingChunk.set('text', chunk.text)

return png.out(callback);
})
}
Expand All @@ -61,6 +56,7 @@ function extract(img, callback) {
return;
found = true;
png.removeListener('tEXt', textListener);
png.removeListener('iTXt', textListener);
return callback(null, chunk.text);
}

Expand All @@ -78,13 +74,14 @@ function extract(img, callback) {
}

png.on('tEXt', textListener);
png.on('iTXt', textListener);
png.once('end', endListener);
png.once('error', errorListener);
}

function createChunk(url) {
return streampng.Chunk.tEXt({
function createChunk(data) {
return streampng.Chunk.iTXt({
keyword: KEYWORD,
text: url
});
text: data
})
}
18 changes: 5 additions & 13 deletions test/png.test.js
Expand Up @@ -10,14 +10,6 @@ const IMG_PATH = pathutil.join(__dirname, 'unbaked.png');
const ASSERTION_URL = "http://example.org";
const ASSERTION = { verify: {} };

test('png.createChunk', function (t) {
var chunk = png.createChunk(ASSERTION_URL);
t.same(chunk.keyword, png.keyword, 'uses the right keyword');
t.same(chunk.text, ASSERTION_URL, 'assigns the text properly');
t.ok(chunk instanceof streampng.Chunk.tEXt, 'makes a tEXt chunk');
t.end();
});

test('png.extract', function (t) {
var nonImageBuffer = new Buffer('Totally not a png');
png.extract(nonImageBuffer, function (err, url) {
Expand Down Expand Up @@ -80,11 +72,11 @@ test('png.bake: takes a stream', function (t) {
test('png.bake: do not bake something twice', function (t) {
preheat(function (_, img) {
png.bake({image: img, url: 'wut'}, function (err, baked) {
t.ok(err, 'should be an error');
t.notOk(baked, 'should not get an image back');
t.same(err.code, 'IMAGE_ALREADY_BAKED', 'should get correct error code');
t.same(err.contents, ASSERTION_URL, 'should get the contents of the chunk');
t.end();
png.extract(baked, function (err, url) {
t.notOk(err, 'there should be a matching iTXt chunk');
t.same(url, 'wut', 'should be able to find the right url');
t.end();
});
})
});
});
Expand Down

0 comments on commit 3b94cb2

Please sign in to comment.