0.1.2 • Published 1 year ago

@mediafish/buffer-operator v0.1.2

Weekly downloads
13
License
MIT
Repository
github
Last release
1 year ago

Build Status Coverage Status Dependency Status Development Dependency Status Known Vulnerabilities npm Downloads XO code style

buffer-operator

Provides JS functions to read/write a byte buffer (both node's Buffer and Uint8Array)

Install

NPM

Usage

const {reader, writer} = require('@mediafish/buffer-operator');

const buffer = Buffer.from([0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63]);
// Or use Uint8Array
const buffer = Uint8Array.from([0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63]);

let offset, value;
[offset, value] = reader.readNumber(buffer, 0, 1); // Specify offset and length
// offset => 1
// value => 255
[offset, value] = reader.readNumber(buffer, offset, 2, true); // signed = true
// offset => 3
// value => -1
[offset, value] = reader.readString(buffer, offset, 3);
// offset => 6
// value => 'abc'

const dest = Buffer.alloc(6);
// Or use Uint8Array
const dest = new Uint8Array(6);

offset = writer.writeNumber(255, dest, 0, 1);
// offset => 1
offset = writer.writeNumber(-1, dest, offset, 2);
// offset => 3
offset = writer.writeString('abc', dest, offset, 3);
// offset => 6
dest.equals(buffer); // => true

API

reader.readNumber(buffer, offset[, length, signed])

Read an integer from the buffer

params

NameTypeRequiredDefaultDescription
bufferBuffer or Uint8ArrayYesN/AThe buffer from which the data is read
offsetnumberYesN/AAn integer to specify the position within the buffer
lengthnumberNo4An integer to specify how many bytes to read
signedbooleanNofalseSet true to read a negative number

return value

An array containing the following pair of values | Index | Type | Description | | ----- | ------ | ------------ | | 0 | number | An integer to indicate the position from which the next data should be read | | 1 | number | The read value |

reader.readString(buffer, offset[, length, nullTerminated])

Read a string from the buffer

params

NameTypeRequiredDefaultDescription
bufferBuffer or Uint8ArrayYesN/AThe buffer from which the data is read
offsetnumberYesN/AAn integer to specify the position within the buffer
lengthnumberNobuffer.length - offsetAn integer to specify how many bytes to read
nullTerminatedbooleanNofalseSet true to stop reading when encountering zero

return value

An array containing the following pair of values | Index | Type | Description | | ----- | ------ | ------------ | | 0 | number | An integer to indicate the position from which the next data should be read | | 1 | string | The read value |

reader.subBuffer(buffer, offset[, length])

Create a sub buffer from the original one.

params

NameTypeRequiredDefaultDescription
bufferBuffer or Uint8ArrayYesN/AThe buffer from which the sub buffer is extracted
offsetnumberYesN/AAn integer to specify the position within the original buffer
lengthnumberNobuffer.length - offsetAn integer to specify how many bytes to extract

return value

The created sub buffer

reader.setOptions(obj)

Updates the option values

params

NameTypeRequiredDefaultDescription
objObjectYes{}An object holding option values which will be used to overwrite the internal option values.
supported options
NameTypeDefaultDescription
strictModebooleanfalseIf true, the function throws an error when the method invocations failed. If false, the function just logs the error and continues to run.

reader.getOptions()

Retrieves the current option values

return value

A cloned object containing the current option values

writer.writeNumber(value, buffer, offset[, length])

Write an integer to the buffer

params

NameTypeRequiredDefaultDescription
valuenumberYesN/AThe value to be written to the buffer
bufferBuffer or Uint8ArrayYesN/AThe buffer to which the data is written
offsetnumberYesN/AAn integer to specify the position within the buffer
lengthnumberNo4An integer to specify how many bytes to write

return value

An integer to indicate the position to which the next data should be written

writer.writeString(value, buffer, offset[, length])

Write a string to the buffer

params

NameTypeRequiredDefaultDescription
valuestringYesN/AThe value to be written to the buffer
bufferBuffer or Uint8ArrayYesN/AThe buffer to which the data is written
offsetnumberYesN/AAn integer to specify the position within the buffer
lengthnumberNoundefinedAn integer to specify how many bytes to write. If not specified, the data is written until the end of the buffer

return value

An integer to indicate the position to which the next data should be written

writer.copyBuffer(src, srcOffset, dst, dstOffset[, length])

Copy data from the src buffer to dst buffer

params

NameTypeRequiredDefaultDescription
srcBuffer or Uint8ArrayYesN/AThe source buffer from which the data is copied
srcOffsetnumberYesN/AAn integer to specify the position within the source buffer
dstBuffer or Uint8ArrayYesN/AThe destination buffer to which the data is copied
dstOffsetnumberYesN/AAn integer to specify the position within the destination buffer
lengthnumberNosrc.length - srcOffsetAn integer to specify how many bytes to copy.

return value

An integer to indicate the position within the dst buffer to which the next data should be written

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago