Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0428ed3

Browse files
author
c42f373607bf2b
committedOct 10, 2017
Add .depUrls + processZlib makes use of pako if configured
1 parent 7a3533f commit 0428ed3

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed
 

‎KaitaiStream.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,26 @@ var KaitaiStream = function(arrayBuffer, byteOffset) {
3131
this.pos = 0;
3232
this.alignToByte();
3333
};
34+
35+
3436
KaitaiStream.prototype = {};
3537

38+
/**
39+
Dependency configuration data. Holds urls for (optional) dynamic loading
40+
of code dependencies from a remote server. For use by (static) processing functions.
41+
42+
Caller should the supported keys to the asset urls as needed.
43+
NOTE: `depUrls` is a static property of KaitaiStream (the factory),like the various
44+
processing functions. It is NOT part of the prototype of instances.
45+
@type {Object}
46+
*/
47+
KaitaiStream.depUrls = {
48+
// processZlib uses this and expected a link to a copy of pako.
49+
// specifically the pako_inflate.min.js script at:
50+
// https://raw.githubusercontent.com/nodeca/pako/master/dist/pako_inflate.min.js
51+
zlib: undefined
52+
};
53+
3654
/**
3755
Virtual byte length of the KaitaiStream backing buffer.
3856
Updated to be max of original buffer size and last written size.
@@ -562,15 +580,31 @@ KaitaiStream.processRotateLeft = function(data, amount, groupSize) {
562580
}
563581

564582
KaitaiStream.processZlib = function(buf) {
565-
if (typeof KaitaiStream.zlib === 'undefined')
566-
KaitaiStream.zlib = require('zlib');
567-
if (buf instanceof Uint8Array) {
568-
var b = new Buffer(buf.buffer);
583+
if (typeof require !== 'undefined') {
584+
// require is available - we're running under node
585+
if (typeof KaitaiStream.zlib === 'undefined')
586+
KaitaiStream.zlib = require('zlib');
587+
if (buf instanceof Uint8Array) {
588+
var b = new Buffer(buf.buffer);
589+
} else {
590+
var b = buf;
591+
}
592+
// use node's zlib module API
593+
var r = KaitaiStream.zlib.inflateSync(b);
594+
return r;
569595
} else {
570-
var b = buf;
596+
// no require() - assume we're running as a web worker in browser.
597+
// user should have configured KaitaiStream.depUrls.zlib, if not
598+
// we'll throw.
599+
if (typeof KaitaiStream.zlib === 'undefined'
600+
&& typeof KaitaiStream.depUrls.zlib !== 'undefined') {
601+
importScripts(KaitaiStream.depUrls.zlib);
602+
KaitaiStream.zlib = pako;
603+
}
604+
// use pako API
605+
r = KaitaiStream.zlib.inflate(buf);
606+
return r;
571607
}
572-
var r = KaitaiStream.zlib.inflateSync(b);
573-
return r;
574608
}
575609

576610
// ========================================================================

0 commit comments

Comments
 (0)
Please sign in to comment.