Permalink
Browse files

Basic baking works.

  • Loading branch information...
1 parent 51da6b0 commit f4d24d13acb292160d90e37ff60413009f4bd9ab @brianloveswords brianloveswords committed Sep 18, 2012
Showing with 131 additions and 2 deletions.
  1. +38 −2 lib/bakery.js
  2. +93 −0 test/bakery.test.js
  3. BIN test/testimage.png
View
@@ -6,6 +6,42 @@
* Licensed under the MPL 2.0 license.
*/
-exports.awesome = function() {
- return 'awesome';
+var util = require('util');
+var streampng = require('streampng');
+var KEYWORD = 'openbadges';
+
+
+function createChunk(url) {
+ return streampng.Chunk.tEXt({
+ keyword: KEYWORD,
+ text: url
+ });
+}
+
+exports.bake = function(options, callback) {
+ var buffer = options.image;
+ var png = streampng(buffer);
+
+ // #TODO: make sure the url is set
+ var chunk = createChunk(options.url);
+ var existingChunk;
+ png.inject(chunk, function (txtChunk) {
+ if (txtChunk.keyword === KEYWORD) {
+ existingChunk = txtChunk;
+ return false;
+ }
+ });
+
+ if (existingChunk) {
+ var msg = util.format('This image already has a chunk with the `%s` keyword (contains: %j)', KEYWORD, chunk.text)
+ var error = new Error(msg);
+ error.code = 'IMAGE_ALREADY_BAKED';
+ error.contents = existingChunk.text;
+ return callback(error);
+ }
+
+ png.out(callback);
};
+
+exports.createChunk = createChunk;
+exports.KEYWORD = KEYWORD;
View
@@ -0,0 +1,93 @@
+var test = require('tap').test;
+
+var fs = require('fs');
+var streampng = require('streampng');
+var bakery = require('../lib/bakery');
+var pathutil = require('path');
+
+var IMG_PATH = pathutil.join(__dirname, 'testimage.png');
+var ASSERTION_URL = "http://example.org";
+
+function getImageStream() {
+ var imgStream = fs.createReadStream(IMG_PATH);
+ return imgStream;
+}
+
+function getImageBuffer() {
+ var imgBuffer = fs.readFileSync(IMG_PATH);
+ return imgBuffer;
+}
+
+function getBakedData(img, callback) {
+ var png = streampng(img);
+ var found = false;
+ png.on('tEXt', function (chunk) {
+ if (chunk.keyword !== 'openbadges')
+ return;
+ found = true;
+ return callback(null, chunk.text);
+ }).on('end', function () {
+ if (!found)
+ return callback(new Error('no baked data'));
+ });
+}
+
+function preheat(callback) {
+ var imgStream = getImageStream();
+ var options = {image: imgStream, url: ASSERTION_URL};
+ bakery.bake(options, callback);
+}
+
+test('bakery.createChunk', function (t) {
+ var chunk = bakery.createChunk(ASSERTION_URL);
+ t.same(chunk.keyword, bakery.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('bakery.bake: takes a buffer', function (t) {
+ var imgBuffer = getImageBuffer();
+ var options = {
+ image: imgBuffer,
+ url: ASSERTION_URL,
+ };
+ bakery.bake(options, function (err, baked) {
+ t.notOk(err, 'should not have an error');
+ t.ok(baked, 'should get back some data');
+ getBakedData(baked, function (err, url) {
+ t.notOk(err, 'there should be a matching tEXt chunk');
+ t.same(url, ASSERTION_URL, 'should be able to find the url');
+ t.end();
+ });
+ })
+});
+
+test('bakery.bake: takes a stream', function (t) {
+ var imgStream = getImageStream();
+ var options = {
+ image: imgStream,
+ url: ASSERTION_URL,
+ };
+ bakery.bake(options, function (err, baked) {
+ t.notOk(err, 'should not have an error');
+ t.ok(baked, 'should get back some data');
+ getBakedData(baked, function (err, url) {
+ t.notOk(err, 'there should be a matching tEXt chunk');
+ t.same(url, ASSERTION_URL, 'should be able to find the url');
+ t.end();
+ });
+ })
+});
+
+test('bakery.bake: do not bake something twice', function (t) {
+ preheat(function (_, img) {
+ bakery.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();
+ })
+ });
+});
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f4d24d1

Please sign in to comment.