1+ "use strict" ;
2+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3+ exports . MinipassSized = exports . SizeError = void 0 ;
4+ const minipass_1 = require ( "minipass" ) ;
5+ const isBufferEncoding = ( enc ) => typeof enc === 'string' ;
6+ class SizeError extends Error {
7+ expect ;
8+ found ;
9+ code = 'EBADSIZE' ;
10+ constructor ( found , expect , from ) {
11+ super ( `Bad data size: expected ${ expect } bytes, but got ${ found } ` ) ;
12+ this . expect = expect ;
13+ this . found = found ;
14+ Error . captureStackTrace ( this , from ?? this . constructor ) ;
15+ }
16+ get name ( ) {
17+ return 'SizeError' ;
18+ }
19+ }
20+ exports . SizeError = SizeError ;
21+ class MinipassSized extends minipass_1 . Minipass {
22+ found = 0 ;
23+ expect ;
24+ constructor ( options ) {
25+ const size = options ?. size ;
26+ if ( typeof size !== 'number' ||
27+ size > Number . MAX_SAFE_INTEGER ||
28+ isNaN ( size ) ||
29+ size < 0 ||
30+ ! isFinite ( size ) ||
31+ size !== Math . floor ( size ) ) {
32+ throw new Error ( 'invalid expected size: ' + size ) ;
33+ }
34+ //@ts -ignore
35+ super ( options ) ;
36+ if ( options . objectMode ) {
37+ throw new TypeError ( `${ this . constructor . name } streams only work with string and buffer data` ) ;
38+ }
39+ this . expect = size ;
40+ }
41+ write ( chunk , encoding , cb ) {
42+ const buffer = Buffer . isBuffer ( chunk ) ? chunk
43+ : typeof chunk === 'string' ?
44+ Buffer . from ( chunk , isBufferEncoding ( encoding ) ? encoding : 'utf8' )
45+ : chunk ;
46+ if ( typeof encoding === 'function' ) {
47+ cb = encoding ;
48+ encoding = null ;
49+ }
50+ if ( ! Buffer . isBuffer ( buffer ) ) {
51+ this . emit ( 'error' , new TypeError ( `${ this . constructor . name } streams only work with string and buffer data` ) ) ;
52+ return false ;
53+ }
54+ this . found += buffer . length ;
55+ if ( this . found > this . expect )
56+ this . emit ( 'error' , new SizeError ( this . found , this . expect ) ) ;
57+ return super . write ( chunk , encoding , cb ) ;
58+ }
59+ emit ( ev , ...args ) {
60+ if ( ev === 'end' ) {
61+ if ( this . found !== this . expect ) {
62+ this . emit ( 'error' , new SizeError ( this . found , this . expect , this . emit ) ) ;
63+ }
64+ }
65+ return super . emit ( ev , ...args ) ;
66+ }
67+ }
68+ exports . MinipassSized = MinipassSized ;
69+ //# sourceMappingURL=index.js.map
0 commit comments