encoder.js
function encodeBuffer( encoder, buffer_type, mime_type, buffer ) {

	// Write buffer header according to buffer length
	if (buffer.length < UINT8_MAX) {
		encoder.stream8.write( pack1b( PRIM_OP.BUFFER | buffer_type | 0x00 ) );
		encoder.stream8.write( pack1b( buffer.length ) );
		encoder.counters.dat_hdr+=2;
	} else if (buffer.length < UINT16_MAX) {
		encoder.stream8.write( pack1b( PRIM_OP.BUFFER | buffer_type | 0x08 ) );
		encoder.stream16.write( pack2b( buffer.length ) );
		encoder.counters.dat_hdr+=3;
	} else if (buffer.length < UINT32_MAX) {
		encoder.stream8.write( pack1b( PRIM_OP.BUFFER | buffer_type | 0x10 ) );
		encoder.stream32.write( pack4b( buffer.length ) );
		encoder.counters.dat_hdr+=5;
	} else {
		// 4 Gigs? Are you serious? Of course we can fit it in a Float64, but WHY?
		throw new Errors.RangeError('The buffer you are trying to encode is bigger than the supported size!');
	}

	// Write MIME Type (from string lookup table)
	if (mime_type != null) {
		encoder.stream16.write( pack2b( mime_type ) );
		encoder.counters.dat_hdr+=2;
	}

	// Write buffer
	if (buffer_type === PRIM_BUFFER_TYPE.STRING_UTF8) {

		// NOTE: UTF-8 is a special case. For optimisation
		// purposes it's better to use the 16-bit stream
		// rather than downcasting to 8-bit and then 
		// re-constructing the 16-bit stream at decoding time.
		encoder.stream16.write( packTypedArray(buffer) );

	} else {
		encoder.stream8.write( packTypedArray(buffer) );
	}

}
function encodeBuffer( encoder, buffer_type, mime_type, buffer ) {

	// Write buffer header according to buffer length
	if (buffer.length < UINT8_MAX) {
		encoder.stream8.write( pack1b( PRIM_OP.BUFFER | buffer_type | 0x00 ) );
		encoder.stream8.write( pack1b( buffer.length ) );
		encoder.counters.dat_hdr+=2;
	} else if (buffer.length < UINT16_MAX) {
		encoder.stream8.write( pack1b( PRIM_OP.BUFFER | buffer_type | 0x08 ) );
		encoder.stream16.write( pack2b( buffer.length ) );
		encoder.counters.dat_hdr+=3;
	} else if (buffer.length < UINT32_MAX) {
		encoder.stream8.write( pack1b( PRIM_OP.BUFFER | buffer_type | 0x10 ) );
		encoder.stream32.write( pack4b( buffer.length ) );
		encoder.counters.dat_hdr+=5;
	} else {
		// 4 Gigs? Are you serious? Of course we can fit it in a Float64, but WHY?
		throw new Errors.RangeError('The buffer you are trying to encode is bigger than the supported size!');
	}

	// Write MIME Type (from string lookup table)
	if (mime_type != null) {
		encoder.stream16.write( pack2b( mime_type ) );
		encoder.counters.dat_hdr+=2;
	}

	// Write buffer
	if (buffer_type === PRIM_BUFFER_TYPE.STRING_UTF8) {

		// NOTE: UTF-8 is a special case. For optimisation
		// purposes it's better to use the 16-bit stream
		// rather than downcasting to 8-bit and then 
		// re-constructing the 16-bit stream at decoding time.
		encoder.stream16.write( packTypedArray(buffer) );

	} else {
		encoder.stream8.write( packTypedArray(buffer) );
	}

}
function encodeBuffer ( encoder , buffer_type , mime_type , buffer ) { // Write buffer header according to buffer length if ( buffer . length < UINT8_MAX ) { encoder . stream8 . write ( pack1b ( PRIM_OP . BUFFER | buffer_type | 0x00 ) ) ; encoder . stream8 . write ( pack1b ( buffer . length ) ) ; encoder . counters . dat_hdr += 2 ; } else if ( buffer . length < UINT16_MAX ) { encoder . stream8 . write ( pack1b ( PRIM_OP . BUFFER | buffer_type | 0x08 ) ) ; encoder . stream16 . write ( pack2b ( buffer . length ) ) ; encoder . counters . dat_hdr += 3 ; } else if ( buffer . length < UINT32_MAX ) { encoder . stream8 . write ( pack1b ( PRIM_OP . BUFFER | buffer_type | 0x10 ) ) ; encoder . stream32 . write ( pack4b ( buffer . length ) ) ; encoder . counters . dat_hdr += 5 ; } else { // 4 Gigs? Are you serious? Of course we can fit it in a Float64, but WHY? throw new Errors . RangeError ( 'The buffer you are trying to encode is bigger than the supported size!' ) ; } // Write MIME Type (from string lookup table) if ( mime_type != null ) { encoder . stream16 . write ( pack2b ( mime_type ) ) ; encoder . counters . dat_hdr += 2 ; } // Write buffer if ( buffer_type === PRIM_BUFFER_TYPE . STRING_UTF8 ) { // NOTE: UTF-8 is a special case. For optimisation // purposes it's better to use the 16-bit stream // rather than downcasting to 8-bit and then  // re-constructing the 16-bit stream at decoding time. encoder . stream16 . write ( packTypedArray ( buffer ) ) ; } else { encoder . stream8 . write ( packTypedArray ( buffer ) ) ; } }
Encode buffer in the array
Encode buffer in the array