1717 * limitations under the License.
1818 */
1919
20- import Platform from './platform' ;
21- import node from 'buffer' ;
20+ import Feature from './feature' ;
21+ import NodeBuffer from './node/node-buf' ;
22+ import HeapBuffer from './browser/browser-buf' ;
2223
2324/**
2425 * This module defines a common API for dealing with binary data that
@@ -365,66 +366,6 @@ class BaseBuffer
365366 }
366367}
367368
368- /**
369- * Basic buffer implementation that should work in most any modern JS env.
370- * @access private
371- */
372- class HeapBuffer extends BaseBuffer {
373- constructor ( arg ) {
374- let buffer = arg instanceof ArrayBuffer ? arg : new ArrayBuffer ( arg )
375- super ( buffer . byteLength ) ;
376- this . _buffer = buffer ;
377- this . _view = new DataView ( this . _buffer ) ;
378- }
379-
380- putUInt8 ( position , val ) {
381- this . _view . setUint8 ( position , val ) ;
382- }
383-
384- getUInt8 ( position ) {
385- return this . _view . getUint8 ( position ) ;
386- }
387-
388- putInt8 ( position , val ) {
389- this . _view . setInt8 ( position , val ) ;
390- }
391-
392- getInt8 ( position ) {
393- return this . _view . getInt8 ( position ) ;
394- }
395-
396- getFloat64 ( position ) {
397- return this . _view . getFloat64 ( position ) ;
398- }
399-
400- putFloat64 ( position , val ) {
401- this . _view . setFloat64 ( position , val ) ;
402- }
403-
404- getSlice ( start , length ) {
405- if ( this . _buffer . slice ) {
406- return new HeapBuffer ( this . _buffer . slice ( start , start + length ) ) ;
407- } else {
408- // Some platforms (eg. phantomjs) don't support slice, so fall back to a copy
409- // We do this rather than return a SliceBuffer, because sliceBuffer cannot
410- // be passed to native network write ops etc - we need ArrayBuffer for that
411- let copy = new HeapBuffer ( length ) ;
412- for ( var i = 0 ; i < length ; i ++ ) {
413- copy . putUInt8 ( i , this . getUInt8 ( i + start ) ) ;
414- }
415- return copy ;
416- }
417- }
418-
419- /**
420- * Specific to HeapBuffer, this gets a DataView from the
421- * current position and of the specified length.
422- */
423- readView ( length ) {
424- return new DataView ( this . _buffer , this . _updatePos ( length ) , length ) ;
425- }
426- }
427-
428369/**
429370 * Represents a view as slice of another buffer.
430371 * @access private
@@ -510,73 +451,7 @@ class CombinedBuffer extends BaseBuffer {
510451 }
511452}
512453
513- /**
514- * Buffer used in a Node.js environment
515- * @access private
516- */
517- class NodeBuffer extends BaseBuffer {
518- constructor ( arg ) {
519- const buffer = newNodeJSBuffer ( arg ) ;
520- super ( buffer . length ) ;
521- this . _buffer = buffer ;
522- }
523-
524- getUInt8 ( position ) {
525- return this . _buffer . readUInt8 ( position ) ;
526- }
527-
528- getInt8 ( position ) {
529- return this . _buffer . readInt8 ( position ) ;
530- }
531-
532- getFloat64 ( position ) {
533- return this . _buffer . readDoubleBE ( position ) ;
534- }
535-
536- putUInt8 ( position , val ) {
537- this . _buffer . writeUInt8 ( val , position ) ;
538- }
539-
540- putInt8 ( position , val ) {
541- this . _buffer . writeInt8 ( val , position ) ;
542- }
543-
544- putFloat64 ( position , val ) {
545- this . _buffer . writeDoubleBE ( val , position ) ;
546- }
547-
548- putBytes ( position , val ) {
549- if ( val instanceof NodeBuffer ) {
550- let bytesToCopy = Math . min ( val . length - val . position , this . length - position ) ;
551- val . _buffer . copy (
552- this . _buffer ,
553- position ,
554- val . position ,
555- val . position + bytesToCopy ) ;
556- val . position += bytesToCopy ;
557- } else {
558- super . putBytes ( position , val ) ;
559- }
560- } ;
561-
562- getSlice ( start , length ) {
563- return new NodeBuffer ( this . _buffer . slice ( start , start + length ) ) ;
564- }
565- }
566-
567- function newNodeJSBuffer ( arg ) {
568- if ( arg instanceof node . Buffer ) {
569- return arg ;
570- } else if ( typeof arg === 'number' && typeof node . Buffer . alloc === 'function' ) {
571- // use static factory function present in newer NodeJS versions to allocate new buffer with specified size
572- return node . Buffer . alloc ( arg ) ;
573- } else {
574- // fallback to the old, potentially deprecated constructor
575- return new node . Buffer ( arg ) ;
576- }
577- }
578-
579- const _DefaultBuffer = Platform . nodeBufferAvailable ( ) ? NodeBuffer : HeapBuffer ;
454+ const DefaultBuffer = Feature . nodeBufferAvailable ( ) ? NodeBuffer : HeapBuffer ;
580455
581456/**
582457 * Allocate a new buffer using whatever mechanism is most sensible for the current platform.
@@ -585,14 +460,12 @@ const _DefaultBuffer = Platform.nodeBufferAvailable() ? NodeBuffer : HeapBuffer;
585460 * @return {BaseBuffer } new buffer
586461 */
587462function alloc ( size ) {
588- return new _DefaultBuffer ( size ) ;
463+ return new DefaultBuffer ( size ) ;
589464}
590465
591466export {
592467 BaseBuffer ,
593- HeapBuffer ,
594468 SliceBuffer ,
595469 CombinedBuffer ,
596- NodeBuffer ,
597470 alloc
598471}
0 commit comments