diff --git a/parsers/smaz/70-smaz.html b/parsers/smaz/70-smaz.html new file mode 100644 index 00000000..f2e46887 --- /dev/null +++ b/parsers/smaz/70-smaz.html @@ -0,0 +1,33 @@ + + + + + + diff --git a/parsers/smaz/70-smaz.js b/parsers/smaz/70-smaz.js new file mode 100644 index 00000000..5d30f5a1 --- /dev/null +++ b/parsers/smaz/70-smaz.js @@ -0,0 +1,41 @@ + +module.exports = function(RED) { + "use strict"; + var smaz = require('./smaz.js'); + + var smazNode = function(n) { + RED.nodes.createNode(this,n); + this.property = n.property||"payload"; + var node = this; + this.on("input", function(msg) { + var value = RED.util.getMessageProperty(msg,node.property); + if (value !== undefined) { + if (typeof value === "string") { + var le = value.length; + var c = Buffer.from(smaz.compress(value)); + RED.util.setMessageProperty(msg,node.property,c); + node.status({text:le +" > "+ c.length}); + node.send(msg); + } + else if (Buffer.isBuffer(value)) { + var l = value.length; + try { + var u = smaz.decompress(value); + RED.util.setMessageProperty(msg,node.property,u); + node.send(msg); + node.status({text:l +" < "+ u.length}); + } + catch (e) { + node.error("Bad decode",value); + node.status({text:"not a smaz buffer"}); + } + } + else { + node.status({text:"dropped"}); + } + } + else { node.warn("No payload found to process"); } + }); + } + RED.nodes.registerType("smaz",smazNode); +} diff --git a/parsers/smaz/LICENSE b/parsers/smaz/LICENSE new file mode 100644 index 00000000..f5b60114 --- /dev/null +++ b/parsers/smaz/LICENSE @@ -0,0 +1,14 @@ +Copyright 2016 JS Foundation and other contributors, https://js.foundation/ +Copyright 2013-2016 IBM Corp. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/parsers/smaz/README.md b/parsers/smaz/README.md new file mode 100644 index 00000000..8c037e13 --- /dev/null +++ b/parsers/smaz/README.md @@ -0,0 +1,22 @@ +node-red-node-smaz +================== + +A Node-RED node to pack and unpack strings to smaz format buffers. + +Install +------- + +Run the following command in your Node-RED user directory - typically `~/.node-red` + + npm install node-red-node-smaz + +Usage +----- + +Uses a smaz library to pack and unpack short strings to small buffers. + +**Note**: this node ONLY accepts strings - anything else will just be dropped. + +If the input is a string it converts it into a smaz buffer. + +If the input is a smaz buffer it converts it back to the original string. diff --git a/parsers/smaz/icons/parser-smaz.png b/parsers/smaz/icons/parser-smaz.png new file mode 100644 index 00000000..02b5ddc0 Binary files /dev/null and b/parsers/smaz/icons/parser-smaz.png differ diff --git a/parsers/smaz/package.json b/parsers/smaz/package.json new file mode 100644 index 00000000..d9dfb65e --- /dev/null +++ b/parsers/smaz/package.json @@ -0,0 +1,23 @@ +{ + "name" : "node-red-node-smaz", + "version" : "0.0.1", + "description" : "A Node-RED node to pack and unpack strings to smaz format", + "dependencies" : { + }, + "repository" : { + "type":"git", + "url":"https://github.com/node-red/node-red-nodes/tree/master/parsers/smaz" + }, + "license": "Apache-2.0", + "keywords": [ "node-red", "smaz" ], + "node-red" : { + "nodes" : { + "smaz": "70-smaz.js" + } + }, + "author": { + "name": "Dave Conway-Jones", + "email": "ceejay@vnet.ibm.com", + "url": "http://nodered.org" + } +} diff --git a/parsers/smaz/smaz.js b/parsers/smaz/smaz.js new file mode 100644 index 00000000..9529f9f0 --- /dev/null +++ b/parsers/smaz/smaz.js @@ -0,0 +1,122 @@ + +// Copyright (c) 2006-2009, Salvatore Sanfilippo +// All rights reserved. + +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +// * Neither the name of Smaz nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Our compression codebook + var rc = [ + " ", "the", "e", "t", "a", "of", "o", "and", "i", "n", "s", "e ", "r", " th", " t", "in", "he", "th", "h", "he ", "to", "\r\n", "l", "s ", "d", " a", "an","er", "c", " o", "d ", "on", " of", "re", "of ", "t ", ", ", "is", "u", "at", " ", "n ", "or", "which", "f", "m", "as", "it", "that", "\n", "was", "en", " ", " w", "es", " an", " i", "\r", "f ", "g", "p", "nd", " s", "nd ", "ed ", "w", "ed", "http://", "for", "te", "ing", "y ", "The", " c", "ti", "r ", "his", "st", " in", "ar", "nt", ",", " to", "y", "ng", " h", "with", "le", "al", "to ", "b", "ou", "be", "were", " b", "se", "o ", "ent", "ha", "ng ", "their", "\"", "hi", "from", " f", "in ", "de", "ion", "me", "v", ".", "ve", "all", "re ", "ri", "ro", "is ", "co", "f t", "are", "ea", ". ", "her", " m", "er ", " p", "es ", "by", "they", "di", "ra", "ic", "not", "s, ", "d t", "at ", "ce", "la", "h ", "ne", "as ", "tio", "on ", "n t", "io", "we", " a ", "om", ", a", "s o", "ur", "li", "ll", "ch", "had", "this", "e t", "g ", "e\r\n", " wh", "ere", " co", "e o", "a ", "us", " d", "ss", "\n\r\n", "\r\n\r", "=\"", " be", " e", "s a", "ma", "one", "t t", "or ", "but", "el", "so", "l ", "e s", "s,", "no", "ter", " wa", "iv", "ho", "e a", " r", "hat", "s t", "ns", "ch ", "wh", "tr", "ut", "/", "have", "ly ", "ta", " ha", " on", "tha", "-", " l", "ati", "en ", "pe", " re", "there", "ass", "si", " fo", "wa", "ec", "our", "who", "its", "z", "fo", "rs", ">", "ot", "un", "<", "im", "th ", "nc", "ate", "><", "ver", "ad", " we", "ly", "ee", " n", "id", " cl", "ac", "il", "", "=\'"]; + +var cb = rc.reduce(function(result, item, index, array) { + result[item] = index; + return result; +}, {}) + +var smaz = module.exports = { + codebook: cb, + reverse_codebook: rc, + flush_verbatim: function(verbatim) { + var output = []; + if (verbatim.length > 1) { + output.push(255); + output.push(verbatim.length-1); + } else { + output.push(254); + } + var k = 0; + for (; k < verbatim.length; k++) { + output.push(verbatim.charCodeAt(k)); + } + return output; + }, + + compress: function(input) { + var verbatim = ""; + var output = []; + var input_index = 0; + + while (input_index < input.length) { + // Try to lookup substrings into the hash table, starting from the + // longer to the shorter substrings + var encoded = false; + var j = 7; + + if (input.length-input_index < 7) { + j = input.length-input_index; + } + + for (; j > 0; j--) { + var code = smaz.codebook[input.substr(input_index,j)]; + if (code != undefined) { + // Match found in the hash table, + // Flush verbatim bytes if needed + if (verbatim) { + output = output.concat(smaz.flush_verbatim(verbatim)); + verbatim = ""; + } + // Emit the byte + output.push(code); + + input_index += j; + + encoded = true; + break; + } + } + if (!encoded) { + // Match not found - add the byte to the verbatim buffer + verbatim += input[input_index]; + input_index++; + + // Flush if we reached the verbatim bytes length limit + if (verbatim.length == 256) { + output = output.concat(smaz.flush_verbatim(verbatim)); + verbatim = ""; + } + } + } + // Flush verbatim bytes if needed + if (verbatim) { + output = output.concat(smaz.flush_verbatim(verbatim)); + verbatim = ""; + } + return new Uint8Array(output); + }, + + decompress: function(input) { + var output = ""; + var i = 0; + while (i < input.length) { + if (input[i] === 254) { + // Verbatim byte + if (i+1 >= input.length) { + throw "Malformed smaz."; + } + output += String.fromCharCode(input[i+1]); + i += 2; + } else if(input[i] === 255) { + // Verbatim string + var j; + if (i+input[i+1]+2 >= input.length) { + throw "Malformed smaz."; + } + for (j = 0; j < input[i+1]+1; j++) { + output += String.fromCharCode(input[i+2+j]); + } + i += 3+input[i+1]; + } else { + // Codebook entry + output += smaz.reverse_codebook[input[i]]; + i++; + } + } + return output; + } +};