Home Manual Reference Source Test
import Corrode from 'corrode/src/index.js'
public class | source

Corrode

Extends:

readable-stream~TransformCorrodeBase → Corrode

Corrode A batteries-included library for reading your binary data. It helps you converting that blob-mess into readable data.

It inherits from CorrodeBase to make the abstraction between these two components more obvious. CorrodeBase does all the dirty low-level work reading bytes, managing jobs, etc. This class however doesn't manipulate the CorrodeBase#jobs-array or messes in other ways with the CorrodeBase#jobLoop.

This class builds on loop and tap to provide more complex functionality.

Test:

Static Member Summary

Static Public Members
public static

assertions-object

public static

object holding all the user-defined extensions.

public static

mappers-object

public static

Static Method Summary

Static Public Methods
public static

addExtension(name: string, fn: function(...args: *))

adds an extension to corrode.

Constructor Summary

Public Constructor
public

constructor(options: object)

initializes mappers, assertions and extensions

Member Summary

Public Members
public

assert: Object<bound Function>

public

ext: Object<bound Function>

public
public

map: Object<bound Function>

Method Summary

Public Methods
public

helper utility logging the current vars

public

fromBuffer(buffer: Buffer, done: function(error: *, data: object)): Corrode

helper utility, parsing a given buffer async

public

pointer(name: string, obj: Object | Array, type: string): Corrode

pushes a pointer-job onto the job-array returns an item of an accessable type (object property, array element) being given an accessable type.

public

sets CorrodeBase#streamOffset to a given absolute position like skip, but absolute If you want to set the offset to something lower than the current offset, you should enable CorrodeBase#isSeeking asap, as otherwise there's no guarantee that the buffer is still available or already flushed.

public

repeat(name: string, length: number, fn: function(end: function, discard: function, i: number)): Corrode

pushes a repeat-job onto the job-array a repeat-job repeats itself a given number of times and then ends.

public

terminatedBuffer(name: string, terminator: number, discardTerminator: boolean): Corrode

pushes a terminatedBuffer-job onto the job-array The returned Buffer will be a slice (not a copy) of the underlying CorrodeBase#streamBuffer internally this method uses the isSeeking-property to prevent flushing of the underlying buffer.

public

terminatedString(name: string, terminator: number, discardTerminator: boolean, encoding: string): Corrode

pushes a terminatedString-job onto the job-array Returns a string ranging from the current offset until the given terminator is found

Inherited Summary

From class CorrodeBase
public static

static constant for big endian

public static

static constant for little endian

public static

Defaults Object for available options CorrodeBase#constructor

public get

vars: Object | *: *

get variables of the current stack

public set

vars(val: Object | *)

replace the current value

public

offset in the current running chunk (CorrodeBase#streamBuffer)

public

indicates whether automatic flushes are disabled

public

indicates whether only pops are getting processed

public

array holding the jobs which are to be processed

public
public

available primitve jobs.

public

streamBuffer: BufferList

internal buffer for stream-chunks not yet processable

public

offset in the whole piped stream

public

the VariableStack holding the layers and variables of this instance

public

buffer(name: string, length: number | string): CorrodeBase

pushes a buffer-job onto the job-array the buffer-job will read a buffer with the given length starting at the current offset CorrodeBase#bufferOffset.

public

push a double job with default endianness (CorrodeBase#options.endianness) onto the job-array

public

push a double job with big endianness onto the job-array

public

push a double job with little endianness onto the job-array

public

re-starts the jobLoop with the job-list cleaned from any read jobs CorrodeBase#removeReadJobs CorrodeBase#isUnwinding

public

push a float job with default endianness (CorrodeBase#options.endianness) onto the job-array

public

push a float job with big endianness onto the job-array

public

push a float job with little endianness onto the job-array

public

push a int16 (signed) job with default endianness (CorrodeBase#options.endianness) onto the job-array

public

push a int16 (signed) job with big endianness onto the job-array

public

push a int16 (signed) job with little endianness onto the job-array

public

push a int32 (signed) job with default endianness (CorrodeBase#options.endianness) onto the job-array

public

push a int32 (signed) job with big endianness onto the job-array

public

push a int32 (signed) job with little endianness onto the job-array

public

push a int64 (signed) job with default endianness (CorrodeBase#options.endianness) onto the job-array note that in64 may be unprecise, due to number-values being double in js

public

push a int64 (signed) job with big endianness onto the job-array note that in64 may be unprecise, due to number-values being double in js

public

push a int64 (signed) job with little endianness onto the job-array note that in64 may be unprecise, due to number-values being double in js

public

push a int8 (signed) job onto the job-array

public
this method was deprecated. int8 needs no endianness, use int8() instead

push a int8 (signed) job onto the job-array

public
this method was deprecated. int8 needs no endianness, use int8() instead

push a int8 (signed) job onto the job-array

public

internal function to process the array of jobs still remaing.

public

loop(name: string, callback: function(end: function, discard: function, i: number)): CorrodeBase

push a loop-job onto the job-array a loop-job is a special kind of job allowing the developer to create loops in the current job-array.

public

push a pop-job onto the job-array the pop-job pops a layer from CorrodeBase#varStack.

public

push(name: string, value: *): CorrodeBase

push a push-job onto the job-array the push-job pushes a new layer onto CorrodeBase#varStack.

public

remove all jobs from CorrodeBase#jobs with the ability to re-add them back later.

public

purges all jobs from the job-queue, which need to read from the stream

public

push a skip-job onto the job-array skip-jobs allow the developer to skip a given number of bytes.

public

string(name: string, length: number | string, encoding: string): CorrodeBase

pushes a string-job onto the job-array the string-job will read a string with the given length starting at the current offset CorrodeBase#bufferOffset

public

tap(name: string, callback: function(...args: *), args: Array): CorrodeBase

push a tap-job onto the job-array a tap-job is a special kind of job allowing the developer to peek into the current variables and creating more complex structures and behaviour based on available information.

public

push a uint16 (unsigned) job with default endianness (CorrodeBase#options.endianness) onto the job-array

public

push a uint16 (unsigned) job with big endianness onto the job-array

public

push a uint16 (unsigned) job with little endianness onto the job-array

public

push a uint32 (unsigned) job with default endianness (CorrodeBase#options.endianness) onto the job-array

public

push a uint32 (unsigned) job with big endianness onto the job-array

public

push a uint32 (unsigned) job with little endianness onto the job-array

public

push a uint64 (unsigned) job with default endianness (CorrodeBase#options.endianness) onto the job-array note that in64 may be unprecise, due to number-values being double in js

public

push a uint64 (unsigned) job with big endianness onto the job-array note that in64 may be unprecise, due to number-values being double in js

public

push a uint64 (unsigned) job with little endianness onto the job-array note that in64 may be unprecise, due to number-values being double in js

public

push a uint8 (unsigned) job onto the job-array

public
this method was deprecated. uint8 needs no endianness, use uint8() instead

push a uint8 (unsigned) job onto the job-array

public
this method was deprecated. uint8 needs no endianness, use uint8() instead

push a uint8 (unsigned) job onto the job-array

private

_flush(done: *)

finish remaining jobs if finishJobsOnEOF is enabled

private

_moveOffset(by: *)

utility method to move both CorrodeBase#chunkOffset and CorrodeBase#streamOffset by a given amount of bytes

private

_pushJob(name: *, type: *, length: *, endianness: *): CorrodeBase

utility method to push a new job onto the job-array

private

_transform(chunk: *, encoding: *, done: *): *

add a new chunk to the internal buffer and process remaining jobs this TransformStream won't call the push-method as it is not able to guarantee correct ouptut while still reading data. this depends not on corrode, but on the way you use it.

Static Public Members

public static ASSERTIONS: Object<Function> source

assertions-object

public static EXTENSIONS: Object<Function> source

object holding all the user-defined extensions. these will get bound to the corrode-instance on calling the constructor

public static MAPPERS: Object<Function> source

mappers-object

public static vars: Object source

get variables of the current stack

Override:

CorrodeBase#vars

Static Public Methods

public static addExtension(name: string, fn: function(...args: *)) source

adds an extension to corrode. extensions are user-defined functions allowing you to better abstract upon corrode an extension can accept parameters and receives the current available variables. it can either directly return values or read them from the buffer with corrode's functions.

Params:

NameTypeAttributeDescription
name string

of the extension

fn function(...args: *)

function receiving arguments given when calling the extension

Example:

Extension reading values
Corrode.addExtension('foo', function(arg1){ this.uint8(arg1); });
(new Corrode()).ext.foo('foo_value', 'arg1');
Extension returning values
Corrode.addExtension('bar', function(arg1){ return this.vars[arg1] * this.vars[arg1] });
(new Corrode()).ext.bar('bar_value', 'arg1')

Public Constructors

public constructor(options: object) source

initializes mappers, assertions and extensions

Override:

CorrodeBase#constructor

Params:

NameTypeAttributeDescription
options object

CorrodeBase#constructor

Public Members

public assert: Object<bound Function> source

public ext: Object<bound Function> source

Test:

public isSeeking: boolean source

indicates whether automatic flushes are disabled

Override:

CorrodeBase#isSeeking

public map: Object<bound Function> source

Test:

Public Methods

public debug(): Corrode source

helper utility logging the current vars

Return:

Corrode

this

Test:

public fromBuffer(buffer: Buffer, done: function(error: *, data: object)): Corrode source

helper utility, parsing a given buffer async

Params:

NameTypeAttributeDescription
buffer Buffer

data

done function(error: *, data: object)

callback

Return:

Corrode

this

Test:

public pointer(name: string, obj: Object | Array, type: string): Corrode source

pushes a pointer-job onto the job-array returns an item of an accessable type (object property, array element) being given an accessable type. the offset used to access the accessable type is determined by reading a value of the given type

Params:

NameTypeAttributeDescription
name string

name of the item-variable

obj Object | Array

accessable type variable

type string

name of the type which should be used to read the index (int8, uint32, etc)

Return:

Corrode

this

Test:

public position(offset: number | string): Corrode source

sets CorrodeBase#streamOffset to a given absolute position like skip, but absolute If you want to set the offset to something lower than the current offset, you should enable CorrodeBase#isSeeking asap, as otherwise there's no guarantee that the buffer is still available or already flushed.

Params:

NameTypeAttributeDescription
offset number | string

as number or as string referencing a variable from CorrodeBase#vars

Return:

Corrode

this

Test:

public repeat(name: string, length: number, fn: function(end: function, discard: function, i: number)): Corrode source

pushes a repeat-job onto the job-array a repeat-job repeats itself a given number of times and then ends. it's also possible to end the job prematurely or discard it. This is nothing else than a proxy to CorrodeBase#loop

Params:

NameTypeAttributeDescription
name string
  • optional

name of the new variable-layer. if none is provided, the current layer will be used

length number

iteration-count as number or as string referencing a variable from CorrodeBase#vars

fn function(end: function, discard: function, i: number)

called until end() is called. end(true) can be used to end and discard the current loop. discard() can be used to reset the current layer (CorrodeBase#options.anonymousLoopDiscardDeep). i is the current iteration count

Return:

Corrode

this

Test:

public terminatedBuffer(name: string, terminator: number, discardTerminator: boolean): Corrode source

pushes a terminatedBuffer-job onto the job-array The returned Buffer will be a slice (not a copy) of the underlying CorrodeBase#streamBuffer internally this method uses the isSeeking-property to prevent flushing of the underlying buffer.

Params:

NameTypeAttributeDescription
name string

name of the buffer-variable

terminator number
  • optional
  • default: 0

uint8-value indicating the end of the buffer

discardTerminator boolean
  • optional
  • default: true

whether or not to include the terminator in the resulting buffer

Return:

Corrode

this

Test:

public terminatedString(name: string, terminator: number, discardTerminator: boolean, encoding: string): Corrode source

pushes a terminatedString-job onto the job-array Returns a string ranging from the current offset until the given terminator is found

Params:

NameTypeAttributeDescription
name string

name of the string-variable

terminator number
  • optional
  • default: 0

uint8-value indicating the end of the buffer

discardTerminator boolean
  • optional
  • default: true

whether or not to include the terminator in the resulting buffer

encoding string
  • optional
  • default: CorrodeBase#options.encoding

encoding encoding used to decode the string, defaults to 'utf8'. available encodings can be found here https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings

Return:

Corrode

this

Test: