buffer functions

This commit is contained in:
vss-devel 2019-02-26 19:55:24 +03:00
parent 41a029c1ba
commit c97d36e1be

View File

@ -200,11 +200,6 @@ function mimeFromData ( data ) {
}
function writeUIntLE( buf, value, offset, byteLength ) {
offset = offset || 0
if ( typeof value == 'string' ) {
byteLength = buf.write( value, offset )
return offset + byteLength
}
if ( byteLength == 8 ) {
var low = (( value & 0xffffffff ) >>> 1 ) * 2 + ( value & 0x1 ) // unsigned
var high = ( value - low ) / 0x100000000
@ -216,6 +211,26 @@ function writeUIntLE( buf, value, offset, byteLength ) {
}
}
function toBuffer( value, byteLength ) {
const buf = Buffer.allocUnsafe( byteLength )
writeUIntLE( buf, value, 0, byteLength )
return buf
}
function chunksToBuffer( list ) {
const chunks = []
for ( const item of list ) {
let buf
if ( Array.isArray( item )) {
buf = toBuffer( ...item )
} else {
buf = Buffer.from( item )
}
chunks.push( buf )
}
return Buffer.concat( chunks )
}
function spawn ( command, args, input ) { // after https://github.com/panosoft/spawn-promise
var child = childProcess.spawn( command, args )