Skip to content

Commit

Permalink
feat: ✨ add support for custom methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDorian committed Jul 6, 2022
1 parent 22015ed commit 293225f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import * as varint from 'varint';

export default class BufWrapper {
export default class BufWrapper<
Plugins extends BufWrapperPlugins = BufWrapperPlugins
> {
/**
* The wrapped NodeJS buffer
*/
public buffer: Buffer;
/**
* Current offset (used for reading)
*/
public offset: number;
/**
* Options that apply to the current `BufWrapper` instance
* */
public options?: BufWrapperOptions<Plugins>;
/**
* Installed plugins so you can access them from this object
*/
public plugins?: Plugins;

/** Current offset */
private offset: number;
/** List of buffers, used for the `oneConcat` option */
private buffers: Buffer[];
/** Options that apply to the current `BufWrapper` instance */
private options?: BufWrapperOptions;

/**
* Create a new buffer wrapper instance
* @param buffer The NodeJS buffer to wrap, optional
* @param options Options to apply to the buffer wrapper, optional
*/
public constructor(buffer?: Buffer, options?: BufWrapperOptions) {
public constructor(buffer?: Buffer, options?: BufWrapperOptions<Plugins>) {
this.buffer = buffer || Buffer.alloc(0);
this.offset = 0;
this.buffers = [];
this.options = options;
this.plugins = options?.plugins;
}

/**
Expand Down Expand Up @@ -432,13 +443,19 @@ export default class BufWrapper {
* array.
* @param value The buffers to write (array of buffers)
*/
private writeToBuffer(...buffers: Buffer[]): void {
public writeToBuffer(...buffers: Buffer[]): void {
if (this.options?.oneConcat === true) this.buffers.push(...buffers);
else this.buffer = Buffer.concat([this.buffer, ...buffers]);
}
}

interface BufWrapperOptions {
type BufWrapperPlugins = {
[key: string]: {
[key: string]: (buf: BufWrapper, ...args: any[]) => any;
};
};

interface BufWrapperOptions<Plugins> {
/**
* Whether or not to run the `Buffer#concat` method when writing.
* When set to `true`, you will have to call the `BufWrapper#finish`
Expand All @@ -449,4 +466,9 @@ interface BufWrapperOptions {
* to the buffer.
*/
oneConcat?: boolean;
/**
* Plugins you want to install on the BufferWrapper intance
* you are about to create
*/
plugins?: Plugins;
}
52 changes: 52 additions & 0 deletions test/plugins.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const BufWrapper = require('../dist').default;
const { assert } = require('chai');

const CStringPlugin = {
/**
* @param {BufWrapper} buf
* @param {String} string
*/
writeCString(buf, string) {
const buffer = Buffer.alloc(Buffer.byteLength(string) + 1);
buffer.write(string);
buf.writeToBuffer(buffer);
},

/**
* @param {BufWrapper} buf
*/
readCString(buf) {
let cursor = buf.offset;
let length = 0;
while (buf.buffer[cursor] !== 0x00) {
length++;
cursor++;
}
const bytes = buf.buffer.slice(buf.offset, length);
buf.offset += length + 1;
return bytes.toString();
},
};

describe('Plugins', () => {
it('CString Write', () => {
const buf = new BufWrapper(null, {
plugins: { CStringPlugin },
});

buf.plugins.CStringPlugin.writeCString(buf, 'Hello World!');

assert.equal(buf.buffer.toString('hex'), '48656c6c6f20576f726c642100');
});

it('CString Read', () => {
const buf = new BufWrapper(
Buffer.from('48656c6c6f20576f726c642100', 'hex'),
{
plugins: { CStringPlugin },
}
);

assert.equal(buf.plugins.CStringPlugin.readCString(buf), 'Hello World!');
});
});

0 comments on commit 293225f

Please sign in to comment.