@@ -31,8 +31,26 @@ var KaitaiStream = function(arrayBuffer, byteOffset) {
31
31
this . pos = 0 ;
32
32
this . alignToByte ( ) ;
33
33
} ;
34
+
35
+
34
36
KaitaiStream . prototype = { } ;
35
37
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
+
36
54
/**
37
55
Virtual byte length of the KaitaiStream backing buffer.
38
56
Updated to be max of original buffer size and last written size.
@@ -562,15 +580,31 @@ KaitaiStream.processRotateLeft = function(data, amount, groupSize) {
562
580
}
563
581
564
582
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 ;
569
595
} 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 ;
571
607
}
572
- var r = KaitaiStream . zlib . inflateSync ( b ) ;
573
- return r ;
574
608
}
575
609
576
610
// ========================================================================
0 commit comments