From ac26d4737978e9a8547bd375ee721d8d985319f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Bj=C3=B6rklund?= Date: Tue, 9 Aug 2011 14:36:15 +0200 Subject: [PATCH] [refactor] Use javascript instead of coffescript, removing that dependency. --- .gitignore | 1 - Cakefile | 6 -- package.json | 6 +- snappy.coffee | 65 ------------- snappy.js | 121 ++++++++++++++++++++++++ test.coffee | 195 -------------------------------------- test.js | 257 ++++++++++++++++++++++++++++++++++++++++++++++++++ wscript | 4 +- 8 files changed, 381 insertions(+), 274 deletions(-) delete mode 100644 Cakefile delete mode 100644 snappy.coffee create mode 100644 snappy.js delete mode 100644 test.coffee create mode 100644 test.js diff --git a/.gitignore b/.gitignore index 4310183..c1225c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ build/* .lock-wscript -/snappy.js /lib/config.h /lib/config.log /lib/config.status diff --git a/Cakefile b/Cakefile deleted file mode 100644 index 2ed1445..0000000 --- a/Cakefile +++ /dev/null @@ -1,6 +0,0 @@ -{exec, spawn} = require('child_process') - -task 'compile', 'Compile Coffeescript source javascript', -> - coffee1 = spawn 'coffee', '-c -b snappy.coffee'.split(' ') - coffee1.stdout.on 'data', (data) -> console.log data.toString() - coffee1.stderr.on 'data', (data) -> console.log data.toString() diff --git a/package.json b/package.json index e8db14f..a57da49 100644 --- a/package.json +++ b/package.json @@ -13,14 +13,12 @@ "lib": "." }, "scripts": { - "test": "vows test.coffee --spec" + "test": "vows test.js --spec" }, "engines": { "node": ">= 0.4.5" }, - "dependencies": { - "coffee-script": ">= 1.1.0" - }, + "dependencies": {}, "devDependencies": { "vows": ">= 0.5.8" } diff --git a/snappy.coffee b/snappy.coffee deleted file mode 100644 index d8382a6..0000000 --- a/snappy.coffee +++ /dev/null @@ -1,65 +0,0 @@ -### - Copyright (c) 2011 David Björklund - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -### - -binding = require('./build/default/binding') - -# Parsers -exports.parsers = parsers = - json: (buffer) -> - return JSON.parse buffer - string: (buffer) -> - return buffer.toString("utf8") - raw: (buffer)-> - return buffer - -# Compress -exports.compress = (input, callback) -> - unless typeof input is 'string' or Buffer.isBuffer input - input = JSON.stringify(input) - binding.compress(input, callback) - -exports.compressSync = (input, callback) -> - unless typeof input is 'string' or Buffer.isBuffer input - input = JSON.stringify(input) - binding.compressSync(input, callback) - -# isValidCompressed -exports.isValidCompressed = (input, callback) -> - binding.isValidCompressed(input, callback) - -exports.isValidCompressedSync = (input, callback) -> - binding.isValidCompressedSync(input, callback) - -# de-/uncompress -exports.uncompress = (compressed, callback, parse = parsers.raw) -> - binding.uncompress(compressed, (err, data) -> - data = parse(data) if data? - callback(err, data) - ) -exports.decompress = exports.uncompress - -exports.uncompressSync = (compressed, callback, parse = parsers.raw) -> - binding.uncompressSync(compressed, (err, data) -> - data = parse(data) if data? - callback(err, data) - ) -exports.decompressSync = exports.uncompressSync diff --git a/snappy.js b/snappy.js new file mode 100644 index 0000000..bb19a7d --- /dev/null +++ b/snappy.js @@ -0,0 +1,121 @@ +/* + Copyright (c) 2011 David Björklund + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* + * The c++-module. + */ +var binding = require('./build/default/binding'); + +/* + * Parsers, used to parse a buffer when decompressing. + */ +var parsers = exports.parsers = { + json: function(buffer) { + return JSON.parse(buffer); + }, + string: function(buffer) { + return buffer.toString("utf8"); + }, + raw: function(buffer) { + return buffer; + } +}; + +/** + * Compress asyncronous. + * If input isn't a string or buffer, automatically convert to buffer by using + * JSON.stringify. + */ +exports.compress = function(input, callback) { + if (!(typeof input === 'string' || Buffer.isBuffer(input))) { + input = JSON.stringify(input); + } + return binding.compress(input, callback); +}; + +/** + * Compress syncronous. + * If input isn't a string or buffer, automatically convert to buffer by using + * JSON.stringify. + */ +exports.compressSync = function(input, callback) { + if (!(typeof input === 'string' || Buffer.isBuffer(input))) { + input = JSON.stringify(input); + } + return binding.compressSync(input, callback); +}; + +/** + * Asyncronous decide if a buffer is compressed in a correct way. + */ +exports.isValidCompressed = function(input, callback) { + return binding.isValidCompressed(input, callback); +}; + +/** + * Syncronous decide if a buffer is compressed in a correct way. + */ +exports.isValidCompressedSync = function(input, callback) { + return binding.isValidCompressedSync(input, callback); +}; + +/** + * Asyncronous uncompress previously compressed data. + * A parser can be attached. If no parser is attached, return buffer. + */ +exports.uncompress = function(compressed, callback, parse) { + if (parse == null) { + parse = parsers.raw; + } + return binding.uncompress(compressed, function(err, data) { + if (data != null) { + data = parse(data); + } + return callback(err, data); + }); +}; + +/** + * Alias to uncompress. + */ +exports.decompress = exports.uncompress; + +/** + * Syncronous uncompress previously compressed data. + * A parser can be attached. If no parser is attached, return buffer. + */ +exports.uncompressSync = function(compressed, callback, parse) { + if (parse == null) { + parse = parsers.raw; + } + return binding.uncompressSync(compressed, function(err, data) { + if (data != null) { + data = parse(data); + } + return callback(err, data); + }); +}; + +/** + * Alias to decompressSync. + */ +exports.decompressSync = exports.uncompressSync; diff --git a/test.coffee b/test.coffee deleted file mode 100644 index f30877d..0000000 --- a/test.coffee +++ /dev/null @@ -1,195 +0,0 @@ -### -Copyright (c) 2011 David Björklund - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -### - -vows = require 'vows' -assert = require 'assert' -snappy = require './snappy' - -assert.isBuffer = (buf) -> - assert.instanceOf buf, Buffer - -assert.isError = (err) -> - assert.instanceOf err, Error -string = "foo foo foo Fasfa daos asd foo foo foo asdasf bar bar aarr" -buffer = new Buffer(string) -json = {"foo" : "bar", "fou" : 0, "shou" : "ho ho", "what?" : ["hey", "you"]} - -["", "Sync"].forEach (sync) -> - # Set function pointer to synchronous or asyncronous version. - # So for example, first compress will mean snappy.compress but when run number - # two, it will mean snappy.compressSync - compress = snappy["compress#{sync}"] - decompress = snappy["decompress#{sync}"] - isValidCompressed = snappy["isValidCompressed#{sync}"] - title = if sync is "" then "asyncronous" else "synchrosnous" - vows.describe("snappy (#{title} versions)").addBatch( - "compress": - "Buffer": - topic: () -> compress(buffer, @callback) - 'should not have errors': (err, compressed) -> - assert.isNull err - - 'should result in a buffer': (err, compressed) -> - assert.isBuffer compressed - - 'and isValidCompressed': - topic: (compressed) -> isValidCompressed(compressed, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in true': (err, result) -> - assert.isTrue result - - 'and decompress (string-parser)': - topic: (compressed) -> - decompress(compressed, @callback, snappy.parsers.string) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in a string': (err, result) -> - assert.isString result - - 'should equal the original when parsed': (err, result) -> - assert.strictEqual result, buffer.toString("utf8") - - 'and decompress (no parser)': - topic: (compressed) -> decompress(compressed, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in a buffer': (err, result) -> - assert.isBuffer result - - 'should equal the original': (err, result) -> - assert.strictEqual result.toString("utf8"), buffer.toString("utf8") - - "json": - topic: () -> compress(json, @callback) - 'should not have errors': (err, compressed) -> - assert.isNull err - - 'should result in a buffer': (err, compressed) -> - assert.isBuffer compressed - - 'and isValidCompressed': - topic: (compressed) -> isValidCompressed(compressed, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in true': (err, result) -> - assert.isTrue result - - 'and decompress (json-parser)': - topic: (compressed) -> - decompress(compressed, @callback, snappy.parsers.json) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in json': (err, result) -> - # TODO: Fix proper test - assert.instanceOf result, Object - - 'should equal the original': (err, result) -> - assert.deepEqual result, json - - 'and decompress (string-parser)': - topic: (compressed) -> - decompress(compressed, @callback, snappy.parsers.string) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in a string': (err, result) -> - assert.isString result - - 'should equal the original when parsed': (err, result) -> - assert.deepEqual JSON.parse(result), json - - 'and decompress (no parser)': - topic: (compressed) -> decompress(compressed, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in a buffer': (err, result) -> - assert.isBuffer result - - 'should equal the original when parsed': (err, result) -> - assert.deepEqual JSON.parse(result), json - - "string": - topic: () -> compress(string, @callback) - 'should not have errors': (err, compressed) -> - assert.isNull err - - 'should result in a buffer': (err, compressed) -> - assert.isBuffer compressed - - 'and isValidCompressed': - topic: (compressed) -> isValidCompressed(compressed, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in true': (err, result) -> - assert.isTrue result - - 'and decompress (string-parser)': - topic: (compressed) -> - decompress(compressed, @callback, snappy.parsers.string) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in a string': (err, result) -> - assert.isString result - - 'should equal the original': (err, result) -> - assert.strictEqual result, string - - 'and decompress (no parser)': - topic: (compressed) -> decompress(compressed, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in a Buffer': (err, result) -> - assert.isBuffer result - - 'should equal the original when parsed': (err, result) -> - string2 = result.toString("utf8") - assert.strictEqual string2, string - - "decompress": - "buffer (invalid)": - topic: () -> decompress(buffer, @callback, snappy.parsers.string) - 'should have error': (err, result) -> - assert.isError err - - 'should have "Invalid input"-error': (err, result) -> - assert.strictEqual err.message ,"Invalid input" - - "isValidCompressed": - "buffer (invalid)": - topic: () -> isValidCompressed(buffer, @callback) - 'should not have errors': (err, result) -> - assert.isNull err - - 'should result in false': (err, result) -> - assert.isFalse result - - ).export(module) diff --git a/test.js b/test.js new file mode 100644 index 0000000..31d0e90 --- /dev/null +++ b/test.js @@ -0,0 +1,257 @@ +/* + Copyright (c) 2011 David Björklund + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +var snappy = require('./snappy'); +var vows = require('vows'); +var assert = require('assert'); + +// Convenient helper methods +assert.isBuffer = function(buf) { + return assert.instanceOf(buf, Buffer); +}; +assert.isError = function(err) { + return assert.instanceOf(err, Error); +}; + +// Test data +var string = "foo foo foo Fasfa daos asd foo foo foo asdasf bar bar aarr"; +var buffer = new Buffer(string); +var json = { + "foo": "bar", + "fou": 0, + "shou": "ho ho", + "what?": ["hey", "you"] +}; + +// Use same test to test both sync and async versions of the snappy-methods. +["", "Sync"].forEach(function(sync) { + // + // Define method aliases to synchronous or asyncronous versions of the + // methods. For example, compress will point to snappy.compress or + // snappy.compressSync + // + var compress = snappy["compress" + sync]; + var decompress = snappy["decompress" + sync]; + var isValidCompressed = snappy["isValidCompressed" + sync]; + + // Give the test runs a good title + var title = sync === "" ? "asyncronous" : "synchrosnous"; + + // Describe the test suite + return vows.describe("snappy (" + title + " versions)").addBatch({ + "compress": { + "Buffer": { + topic: function() { + return compress(buffer, this.callback); + }, + 'should not have errors': function(err, compressed) { + return assert.isNull(err); + }, + 'should result in a buffer': function(err, compressed) { + return assert.isBuffer(compressed); + }, + 'and isValidCompressed': { + topic: function(compressed) { + return isValidCompressed(compressed, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in true': function(err, result) { + return assert.isTrue(result); + } + }, + 'and decompress (string-parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback, snappy.parsers.string); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in a string': function(err, result) { + return assert.isString(result); + }, + 'should equal the original when parsed': function(err, result) { + console.log("'", result, " ", buffer.toString("utf8"), "'"); + return assert.strictEqual(result, buffer.toString("utf8")); + } + }, + 'and decompress (no parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in a buffer': function(err, result) { + return assert.isBuffer(result); + }, + 'should equal the original': function(err, result) { + return assert.strictEqual(result.toString("utf8"), buffer.toString("utf8")); + } + } + }, + "json": { + topic: function() { + return compress(json, this.callback); + }, + 'should not have errors': function(err, compressed) { + return assert.isNull(err); + }, + 'should result in a buffer': function(err, compressed) { + return assert.isBuffer(compressed); + }, + 'and isValidCompressed': { + topic: function(compressed) { + return isValidCompressed(compressed, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in true': function(err, result) { + return assert.isTrue(result); + } + }, + 'and decompress (json-parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback, snappy.parsers.json); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in json': function(err, result) { + return assert.instanceOf(result, Object); + }, + 'should equal the original': function(err, result) { + return assert.deepEqual(result, json); + } + }, + 'and decompress (string-parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback, snappy.parsers.string); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in a string': function(err, result) { + return assert.isString(result); + }, + 'should equal the original when parsed': function(err, result) { + return assert.deepEqual(JSON.parse(result), json); + } + }, + 'and decompress (no parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in a buffer': function(err, result) { + return assert.isBuffer(result); + }, + 'should equal the original when parsed': function(err, result) { + return assert.deepEqual(JSON.parse(result), json); + } + } + }, + "string": { + topic: function() { + return compress(string, this.callback); + }, + 'should not have errors': function(err, compressed) { + return assert.isNull(err); + }, + 'should result in a buffer': function(err, compressed) { + return assert.isBuffer(compressed); + }, + 'and isValidCompressed': { + topic: function(compressed) { + return isValidCompressed(compressed, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in true': function(err, result) { + return assert.isTrue(result); + } + }, + 'and decompress (string-parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback, snappy.parsers.string); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in a string': function(err, result) { + return assert.isString(result); + }, + 'should equal the original': function(err, result) { + return assert.strictEqual(result, string); + } + }, + 'and decompress (no parser)': { + topic: function(compressed) { + return decompress(compressed, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in a Buffer': function(err, result) { + return assert.isBuffer(result); + }, + 'should equal the original when parsed': function(err, result) { + var string2; + string2 = result.toString("utf8"); + return assert.strictEqual(string2, string); + } + } + } + }, + "decompress": { + "buffer (invalid)": { + topic: function() { + return decompress(buffer, this.callback, snappy.parsers.string); + }, + 'should have error': function(err, result) { + return assert.isError(err); + }, + 'should have "Invalid input"-error': function(err, result) { + return assert.strictEqual(err.message, "Invalid input"); + } + } + }, + "isValidCompressed": { + "buffer (invalid)": { + topic: function() { + return isValidCompressed(buffer, this.callback); + }, + 'should not have errors': function(err, result) { + return assert.isNull(err); + }, + 'should result in false': function(err, result) { + return assert.isFalse(result); + } + } + } + }).export(module); +}); diff --git a/wscript b/wscript index c08eb8e..bf77456 100644 --- a/wscript +++ b/wscript @@ -24,7 +24,5 @@ def build(bld): obj.includes = 'lib/' obj.install_path = None - Utils.exec_command('cake compile') - def test(tsk): - Utils.exec_command('vows test.coffee --spec') + Utils.exec_command('vows test.js --spec')