11/*!
2- * Notepack.io 2. 3.0
3- * (c) 2014-2020 Damien Arrachequesne
2+ * Notepack.io 3.0.1
3+ * (c) 2014-2022 Damien Arrachequesne
44 * Released under the MIT License.
55 */
66( function ( root ) {
148148 length = this . _view . getUint8 ( this . _offset ) ;
149149 type = this . _view . getInt8 ( this . _offset + 1 ) ;
150150 this . _offset += 2 ;
151+ if ( type === - 1 ) {
152+ // timestamp 96
153+ var ns = this . _view . getUint32 ( this . _offset ) ;
154+ hi = this . _view . getInt32 ( this . _offset + 4 ) ;
155+ lo = this . _view . getUint32 ( this . _offset + 8 ) ;
156+ this . _offset += 12 ;
157+ return new Date ( ( hi * 0x100000000 + lo ) * 1e3 + ns / 1e6 ) ;
158+ }
151159 return [ type , this . _bin ( length ) ] ;
152160 case 0xc8 :
153161 length = this . _view . getUint16 ( this . _offset ) ;
213221 type = this . _view . getInt8 ( this . _offset ) ;
214222 this . _offset += 1 ;
215223 if ( type === 0x00 ) {
224+ // custom encoding for 'undefined' (kept for backward-compatibility)
216225 this . _offset += 1 ;
217226 return void 0 ;
218227 }
224233 case 0xd6 :
225234 type = this . _view . getInt8 ( this . _offset ) ;
226235 this . _offset += 1 ;
236+ if ( type === - 1 ) {
237+ // timestamp 32
238+ value = this . _view . getUint32 ( this . _offset ) ;
239+ this . _offset += 4 ;
240+ return new Date ( value * 1e3 ) ;
241+ }
227242 return [ type , this . _bin ( 4 ) ] ;
228243 case 0xd7 :
229244 type = this . _view . getInt8 ( this . _offset ) ;
230245 this . _offset += 1 ;
231246 if ( type === 0x00 ) {
247+ // custom date encoding (kept for backward-compatibility)
232248 hi = this . _view . getInt32 ( this . _offset ) * Math . pow ( 2 , 32 ) ;
233249 lo = this . _view . getUint32 ( this . _offset + 4 ) ;
234250 this . _offset += 8 ;
235251 return new Date ( hi + lo ) ;
236252 }
253+ if ( type === - 1 ) {
254+ // timestamp 64
255+ hi = this . _view . getUint32 ( this . _offset ) ;
256+ lo = this . _view . getUint32 ( this . _offset + 4 ) ;
257+ this . _offset += 8 ;
258+ var s = ( hi & 0x3 ) * 0x100000000 + lo ;
259+ return new Date ( s * 1e3 + ( hi >>> 2 ) / 1e6 ) ;
260+ }
237261 return [ type , this . _bin ( 8 ) ] ;
238262 case 0xd8 :
239263 type = this . _view . getInt8 ( this . _offset ) ;
287311 return value ;
288312 }
289313
314+ var TIMESTAMP32_MAX_SEC = 0x100000000 - 1 ; // 32-bit unsigned int
315+ var TIMESTAMP64_MAX_SEC = 0x400000000 - 1 ; // 34-bit unsigned int
316+
290317 function utf8Write ( view , offset , str ) {
291318 var c = 0 ;
292319 for ( var i = 0 , l = str . length ; i < l ; i ++ ) {
463490 return size ;
464491 }
465492
466- // fixext 8 / Date
467493 if ( value instanceof Date ) {
468- var time = value . getTime ( ) ;
469- hi = Math . floor ( time / Math . pow ( 2 , 32 ) ) ;
470- lo = time >>> 0 ;
471- bytes . push ( 0xd7 , 0 , hi >> 24 , hi >> 16 , hi >> 8 , hi , lo >> 24 , lo >> 16 , lo >> 8 , lo ) ;
472- return 10 ;
494+ var ms = value . getTime ( ) ;
495+ var s = Math . floor ( ms / 1e3 ) ;
496+ var ns = ( ms - s * 1e3 ) * 1e6 ;
497+
498+ if ( s >= 0 && ns >= 0 && s <= TIMESTAMP64_MAX_SEC ) {
499+ if ( ns === 0 && s <= TIMESTAMP32_MAX_SEC ) {
500+ // timestamp 32
501+ bytes . push ( 0xd6 , 0xff , s >> 24 , s >> 16 , s >> 8 , s ) ;
502+ return 6 ;
503+ } else {
504+ // timestamp 64
505+ hi = s / 0x100000000 ;
506+ lo = s & 0xffffffff ;
507+ bytes . push ( 0xd7 , 0xff , ns >> 22 , ns >> 14 , ns >> 6 , hi , lo >> 24 , lo >> 16 , lo >> 8 , lo ) ;
508+ return 10 ;
509+ }
510+ } else {
511+ // timestamp 96
512+ hi = Math . floor ( s / 0x100000000 ) ;
513+ lo = s >>> 0 ;
514+ bytes . push ( 0xc7 , 0x0c , 0xff , ns >> 24 , ns >> 16 , ns >> 8 , ns , hi >> 24 , hi >> 16 , hi >> 8 , hi , lo >> 24 , lo >> 16 , lo >> 8 , lo ) ;
515+ return 15 ;
516+ }
473517 }
474518
475519 if ( value instanceof ArrayBuffer ) {
505549 var allKeys = Object . keys ( value ) ;
506550 for ( i = 0 , l = allKeys . length ; i < l ; i ++ ) {
507551 key = allKeys [ i ] ;
508- if ( typeof value [ key ] !== 'function' ) {
552+ if ( value [ key ] !== undefined && typeof value [ key ] !== 'function' ) {
509553 keys . push ( key ) ;
510554 }
511555 }
541585 bytes . push ( value ? 0xc3 : 0xc2 ) ;
542586 return 1 ;
543587 }
544- // fixext 1 / undefined
545588 if ( type === 'undefined' ) {
546- bytes . push ( 0xd4 , 0 , 0 ) ;
547- return 3 ;
589+ bytes . push ( 0xc0 ) ;
590+ return 1 ;
591+ }
592+ // custom types like BigInt (typeof value === 'bigint')
593+ if ( typeof value . toJSON === 'function' ) {
594+ return _encode ( bytes , defers , value . toJSON ( ) ) ;
548595 }
549596 throw new Error ( 'Could not encode' ) ;
550597 }
590637 }
591638
592639
593- } ) ( self ) ;
640+ } ) ( self ) ;
0 commit comments