@@ -4,11 +4,10 @@ import {
44 Inflate ,
55 deflate ,
66 deflateRaw ,
7+ gzip ,
78 inflate ,
89 inflateRaw ,
9- zlibDeflateSetDictionary ,
1010 Z_FULL_FLUSH ,
11- Z_OK ,
1211 Z_SYNC_FLUSH
1312} from '../src/index.ts' ;
1413import assert from 'assert' ;
@@ -23,19 +22,15 @@ describe('deflate misc', () => {
2322
2423 it ( 'handles a dictionary across multiple pushes' , ( ) => {
2524 const dict = Buffer . from ( 'abcd' ) ;
26- const deflate = new Deflate ( ) ;
27- deflate . onStart = function ( strm ) {
28- assert . strictEqual ( zlibDeflateSetDictionary ( strm , dict ) , Z_OK ) ;
29- } ;
25+ const deflate = new Deflate ( { dictionary : dict } ) ;
3026
3127 deflate . push ( Buffer . from ( 'hello' ) , false ) ;
3228 deflate . push ( Buffer . from ( 'hello' ) , false ) ;
3329 deflate . push ( Buffer . from ( ' world' ) , true ) ;
3430
3531 if ( deflate . err ) { throw new Error ( deflate . err ) ; }
3632
37- const inflate = new Inflate ( ) ;
38- inflate . onNeedDict = function ( ) { return dict ; } ;
33+ const inflate = new Inflate ( { dictionary : dict } ) ;
3934 inflate . push ( Buffer . from ( deflate . result ) , true ) ;
4035 assert . ok ( ! inflate . err , 'inflate error: ' + inflate . err ) ;
4136
@@ -51,42 +46,24 @@ describe('deflate misc', () => {
5146 assert . deepStrictEqual ( deflate ( sample . buffer ) , deflate ( sample ) ) ;
5247 } ) ;
5348
54- it ( 'sets a dictionary from onStart' , ( ) => {
55- const dict = new Uint8Array ( [ 0x61 , 0x62 , 0x63 , 0x64 ] ) ; // 'abcd'
56-
57- const withDictionary = new Deflate ( ) ;
58- withDictionary . onStart = function ( strm ) {
59- assert . strictEqual ( zlibDeflateSetDictionary ( strm , dict ) , Z_OK ) ;
60- } ;
61- withDictionary . push ( Buffer . from ( 'hellohello world' ) , true ) ;
62- assert . ok ( ! withDictionary . err , 'deflate error: ' + withDictionary . err ) ;
63-
64- const withoutDictionary = new Deflate ( ) ;
65- withoutDictionary . push ( Buffer . from ( 'hellohello world' ) , true ) ;
66-
67- assert . notDeepStrictEqual ( withDictionary . result , withoutDictionary . result ) ;
49+ it ( 'accepts a dictionary passed as ArrayBuffer' , ( ) => {
50+ const dict = Uint8Array . from ( 'abcd' , c => c . charCodeAt ( 0 ) ) ;
6851
69- const inflate = new Inflate ( ) ;
70- inflate . onNeedDict = function ( ) { return dict ; } ;
71- inflate . push ( Buffer . from ( withDictionary . result ) , true ) ;
72- assert . ok ( ! inflate . err , 'inflate error: ' + inflate . err ) ;
52+ const compressed = deflate ( Buffer . from ( 'hellohello world' ) , { dictionary : dict . buffer } ) ;
53+ const uncompressed = inflate ( compressed , { dictionary : dict } ) ;
7354 assert . deepStrictEqual (
7455 new Uint8Array ( Buffer . from ( 'hellohello world' ) ) ,
75- inflate . result
56+ uncompressed
7657 ) ;
7758 } ) ;
7859
7960 it ( 'throws on invalid init options' , ( ) => {
8061 assert . throws ( ( ) => new Deflate ( { level : 42 } ) ) ;
8162 } ) ;
8263
83- it ( 'reports gzip dictionary errors from onStart' , ( ) => {
84- const deflate = new Deflate ( { gzip : true } ) ;
85- deflate . onStart = function ( strm ) {
86- assert . notStrictEqual ( zlibDeflateSetDictionary ( strm , Buffer . from ( 'abcd' ) ) , Z_OK ) ;
87- } ;
88- deflate . push ( Buffer . from ( 'hello' ) , true ) ;
89- assert . ok ( ! deflate . err , 'deflate error: ' + deflate . err ) ;
64+ it ( 'throws when a dictionary is used with gzip compression' , ( ) => {
65+ assert . throws ( ( ) => new Deflate ( { gzip : true , dictionary : Buffer . from ( 'abcd' ) } ) , / d i c t i o n a r y i s n o t s u p p o r t e d w i t h g z i p / ) ;
66+ assert . throws ( ( ) => gzip ( Buffer . from ( 'hello' ) , { dictionary : Buffer . from ( 'abcd' ) } ) , / d i c t i o n a r y i s n o t s u p p o r t e d w i t h g z i p / ) ;
9067 } ) ;
9168
9269 it ( 'returns false when pushing after the stream has ended' , ( ) => {
0 commit comments