Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: add Struct static types that can be extended. #7

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
{
"env": {
"es6": true,
"es2022": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readable",
"BigInt": "readable",
"BigInt64Array": "readable",
"BigUint64Array": "readable",
"queueMicrotask": "readable",
"SharedArrayBuffer": "readable",
"TextEncoder": "readable",
"TextDecoder": "readable"
},
"overrides": [
{
"files": ["*.mjs"],
Expand Down Expand Up @@ -44,9 +34,8 @@
}
}
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 10,
"ecmaVersion": 13,
"ecmaFeatures": {
"globalReturn": true
},
Expand Down
16 changes: 16 additions & 0 deletions lib/bufio.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,32 @@ exports.BufferWriter = BufferWriter;
exports.StaticWriter = StaticWriter;
exports.Struct = Struct;

/**
* @param {Buffer} data
* @param {Boolean} [zeroCopy]
* @returns {BufferReader}
*/

exports.read = function read(data, zeroCopy) {
return new BufferReader(data, zeroCopy);
};

/**
* @param {Number} [size]
* @returns {BufferWriter|StaticWriter}
*/

exports.write = function write(size) {
return size != null
? new StaticWriter(size)
: new BufferWriter();
};

/**
* @param {Number} size
* @returns {StaticWriter}
*/

exports.pool = function pool(size) {
return StaticWriter.pool(size);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ class BufferReader {
/**
* Read a string.
* @param {Number} size
* @param {BufferEncoding} enc - Any buffer-supported encoding.
* @param {BufferEncoding?} [enc] - Any buffer-supported encoding.
* @returns {String}
*/

Expand All @@ -968,7 +968,7 @@ class BufferReader {

/**
* Read a 32-byte hash.
* @param {BufferEncoding} enc - `"hex"` or `null`.
* @param {BufferEncoding} [enc] - `"hex"` or `null`.
* @returns {Buffer|String}
*/

Expand Down
92 changes: 89 additions & 3 deletions lib/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ class Struct {
}

/**
* @param {*} [extra]
* @returns {*}
*/

format() {
format(extra) {
return this.getJSON();
}

Expand Down Expand Up @@ -239,34 +240,98 @@ class Struct {
* Static API
*/

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {BufferReader} br
* @param {*} [extra]
* @returns {T}
*/

static read(br, extra) {
return new this().read(br, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {Buffer} data
* @param {*} [extra]
* @returns {T}
*/

static decode(data, extra) {
return new this().decode(data, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {String} str
* @param {*} [extra]
* @returns {T}
*/

static fromHex(str, extra) {
return new this().fromHex(str, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {String} str
* @param {*} [extra]
* @returns {T}
*/

static fromBase64(str, extra) {
return new this().fromBase64(str, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {String} str
* @param {*} [extra]
* @returns {T}
*/

static fromString(str, extra) {
return new this().fromString(str, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {Object} json
* @param {*} [extra]
* @returns {T}
*/

static fromJSON(json, extra) {
return new this().fromJSON(json, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {Object} options
* @param {*} [extra]
* @returns {T}
*/

static fromOptions(options, extra) {
return new this().fromOptions(options, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {Object} options
* @param {*} [extra]
* @returns {T}
*/

static from(options, extra) {
return new this().from(options, extra);
}
Expand Down Expand Up @@ -318,19 +383,40 @@ class Struct {
* Static Aliases
*/

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {BufferReader} br
* @param {*} [extra]
* @returns {T}
*/

static fromReader(br, extra) {
return this.read(br, extra);
return new this().read(br, extra);
}

/**
* @template {Struct} T
* @this {new (...args: any[]) => T}
* @param {Buffer} data
* @param {*} [extra]
* @returns {T}
*/

static fromRaw(data, extra) {
return this.decode(data, extra);
return new this().decode(data, extra);
}
}

/*
* Helpers
*/

/**
* @param {Number} size
* @returns {Number}
*/

function size64(size) {
const expect = ((4 * size / 3) + 3) & ~3;
return expect >>> 0;
Expand Down
Loading