Skip to content

Instantly share code, notes, and snippets.

@SimonRichardson
Last active December 16, 2015 00:49
Show Gist options
  • Save SimonRichardson/5350092 to your computer and use it in GitHub Desktop.
Save SimonRichardson/5350092 to your computer and use it in GitHub Desktop.
The idea is similar to how .swc works for AVM, but for js. Highly dangerous, but highly effective for packaging lots of js files and assets in one payload.
import haxe.io.Bytes;
import haxe.io.BytesInput;
import haxe.io.BytesOutput;
import format.tools.CRC32;
import format.zip.Data;
import format.zip.Reader;
import format.zip.Tools;
import format.zip.Writer;
class Test {
static function main(){
//
// >> Assume this is already been done server side.
//
var string = "(function() {alert('hello');})();";
var bytes = Bytes.ofString(string);
var entry:Entry = {
fileName : "min.js",
fileSize : bytes.length,
fileTime : Date.now(),
compressed : false,
dataSize : 0,
data : bytes,
crc32 : CRC32.encode(bytes),
extraFields : new List()
};
var entries:List<Entry> = new List();
entries.add(entry);
var bytesOutput = new BytesOutput();
var writer = new Writer(bytesOutput);
writer.writeData(entries);
var zipfileBytes = bytesOutput.getBytes();
//
// <<
//
//
// >> Read the current file and inject it into the current scope.
//
var bytesInput = new BytesInput(zipfileBytes);
var reader = new Reader(bytesInput);
var entries:List<format.zip.Data.Entry> = reader.read();
for (entry in entries) {
if (entry.fileName.lastIndexOf('.js') == entry.fileName.length - 3) {
var string = entry.data.toString();
try {
untyped __js__('eval(string)');
} catch(e : Dynamic) {
trace(e);
}
}
}
// <<
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment