Created
November 19, 2015 02:14
-
-
Save Tim-Machine/85ee34e05c20839a90d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is ran on the server to check validity of the string | |
| var convertDataToBinary, parseBluePrint; | |
| convertDataToBinary = function(dataURI) { | |
| var array, i, raw, rawLength; | |
| raw = window.atob(dataURI); | |
| rawLength = raw.length; | |
| array = new Uint8Array(new ArrayBuffer(rawLength)); | |
| i = 0; | |
| while (i < rawLength) { | |
| array[i] = raw.charCodeAt(i); | |
| i++; | |
| } | |
| return array; | |
| }; | |
| parseBluePrint = function(string) { | |
| var data, error; | |
| if (string.length >= Meteor.settings["public"].maxArgSize) { | |
| throw new Error('incorrect blueprint format'); | |
| } | |
| data = convertDataToBinary(string); | |
| try { | |
| return pako.ungzip(data); | |
| } catch (_error) { | |
| error = _error; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ | |
| example script execution | |
| lua server.lua <bluprintstring> | |
| This is also based almost directly off of the blueprint string mod | |
| this will return properly formated json. | |
| ]]-- | |
| JSON = (loadfile "json.lua")() -- http://regex.info/blog/lua/json | |
| local serpent = require "serpent0272" -- https://github.com/pkulchenko/serpent | |
| local inflate = require "deflatelua" -- https://github.com/davidm/lua-compress-deflatelua | |
| local base64 = require "base64" -- https://gist.github.com/bortels/1436940 | |
| function parse(data) | |
| if (string.sub(data, 1, 8) ~= "do local") then | |
| -- Decompress string | |
| local output = {} | |
| local input = base64.dec(data) | |
| local status, result = pcall(inflate.gunzip, { input = input, output = function(byte) output[#output+1] = string.char(byte) end }) | |
| if (status) then | |
| data = table.concat(output) | |
| else | |
| return nil | |
| end | |
| end | |
| local status, result = serpent.load(data) | |
| if (not status) then | |
| return nil | |
| end | |
| json = JSON:encode(result) | |
| print(json) | |
| end | |
| parse(arg[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment