Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit e58f24a

Browse files
authored
Merge pull request #2 from solana-labs/release
Update build config and release generics
2 parents 768083c + 1d02eb2 commit e58f24a

File tree

7 files changed

+2078
-72
lines changed

7 files changed

+2078
-72
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = {
99
'plugin:@typescript-eslint/recommended',
1010
],
1111
rules: {
12+
'@typescript-eslint/ban-ts-comment': 'off',
1213
'@typescript-eslint/no-explicit-any': 'off',
13-
'@typescript-eslint/explicit-module-boundary-types': 'off',
1414
'@typescript-eslint/no-unused-vars': 'off',
1515
'guard-for-in': 'off',
1616
'prefer-rest-params': 'off',

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/coverage/
33
/node_modules/
44
/lib/
5+
/docs/

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# @solana/buffer-layout
2+
3+
`@solana/buffer-layout` is a TypeScript fork of `buffer-layout`. Same API, just adds types and TypeScript docs.
4+
5+
## Installation
6+
7+
Install with `npm install @solana/buffer-layout`.
8+
9+
Development and testing is done using Node.js, supporting versions 5.10
10+
and later.
11+
112
# buffer-layout
213

314
[![NPM version](https://img.shields.io/npm/v/buffer-layout.svg)](https://www.npmjs.com/package/buffer-layout "View this project on NPM")
@@ -26,11 +37,6 @@ Layout support is provided for these types of data:
2637
* NUL-terminated C strings;
2738
* Blobs of fixed or variable-length raw data.
2839

29-
## Installation
30-
31-
Development and testing is done using Node.js, supporting versions 4.5
32-
and later. Install with `npm install buffer-layout`.
33-
3440
## Examples
3541

3642
All examples are from the `test/examples.js` unit test and assume the

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solana/buffer-layout",
3-
"version": "3.0.0",
3+
"version": "4.0.0",
44
"description": "Translation between JavaScript values and Buffers",
55
"keywords": [
66
"Buffer",
@@ -29,11 +29,14 @@
2929
"@typescript-eslint/parser": "^4.28.2",
3030
"coveralls": "^3.0.0",
3131
"eslint": "~7.30.0",
32+
"gh-pages": "^3.2.3",
3233
"istanbul": "~0.4.5",
3334
"jsdoc": "~3.5.5",
3435
"lodash": "~4.17.5",
3536
"mocha": "~5.0.4",
36-
"typescript": "~4.3.5"
37+
"shx": "^0.3.3",
38+
"typedoc": "^0.22.10",
39+
"typescript": "^4.4.4"
3740
},
3841
"engines": {
3942
"node": ">=5.10"
@@ -42,8 +45,10 @@
4245
"build": "tsc",
4346
"coverage": "npm run build && istanbul cover _mocha -- -u tdd",
4447
"coveralls": "npm run build && istanbul cover _mocha --report lcovonly -- -u tdd && cat ./coverage/lcov.info | coveralls",
48+
"docs": "shx rm -rf docs && typedoc",
4549
"eslint": "eslint src/ --ext .ts",
4650
"jsdoc": "jsdoc -c jsdoc/conf.json",
51+
"pages": "gh-pages --dist docs --dotfiles",
4752
"prepare": "npm run build",
4853
"test": "npm run build && mocha -u tdd"
4954
}

src/Layout.ts

+60-56
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function uint8ArrayToBuffer(b: Uint8Array): Buffer {
174174
*
175175
* @abstract
176176
*/
177-
export class Layout<T> {
177+
export abstract class Layout<T> {
178178
span: number;
179179
property?: string;
180180
boundConstructor_?: any;
@@ -237,9 +237,7 @@ export class Layout<T> {
237237
*
238238
* @abstract
239239
*/
240-
decode(b: Uint8Array, offset?: number): T {
241-
throw new Error('Layout is abstract');
242-
}
240+
abstract decode(b: Uint8Array, offset?: number): T;
243241

244242
/**
245243
* Encode a JavaScript value into a Uint8Array.
@@ -263,9 +261,7 @@ export class Layout<T> {
263261
*
264262
* @abstract
265263
*/
266-
encode(src: T, b: Uint8Array, offset?: number): number {
267-
throw new Error('Layout is abstract');
268-
}
264+
abstract encode(src: T, b: Uint8Array, offset?: number): number;
269265

270266
/**
271267
* Calculate the span of a specific instance of a layout.
@@ -377,6 +373,8 @@ export function nameWithProperty(name: string, lo: { property?: string }): strin
377373
* @param {Layout} layout - the {@link Layout} instance used to encode
378374
* instances of `Class`.
379375
*/
376+
// `Class` must be a constructor Function, but the assignment of a `layout_` property to it makes it difficult to type
377+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
380378
export function bindConstructorLayout<T>(Class: any, layout: Layout<T>): void {
381379
if ('function' !== typeof Class) {
382380
throw new TypeError('Class must be constructor');
@@ -428,7 +426,7 @@ export function bindConstructorLayout<T>(Class: any, layout: Layout<T>): void {
428426
* @abstract
429427
* @augments {Layout}
430428
*/
431-
export class ExternalLayout extends Layout<number> {
429+
export abstract class ExternalLayout extends Layout<number> {
432430
/**
433431
* Return `true` iff the external layout decodes to an unsigned
434432
* integer layout.
@@ -1428,6 +1426,8 @@ export class UnionLayoutDiscriminator extends UnionDiscriminator<number> {
14281426
* @augments {Layout}
14291427
*/
14301428
export class Union extends Layout<LayoutObject> {
1429+
// `property` is assigned in the Layout constructor
1430+
// @ts-ignore
14311431
property: string;
14321432
discriminator: UnionDiscriminator;
14331433
usesPrefixDiscriminator: boolean;
@@ -1648,6 +1648,8 @@ export class Union extends Layout<LayoutObject> {
16481648
}
16491649
dest = this.makeDestinationObject();
16501650
dest[dlo.property] = discr;
1651+
// defaultLayout.property can be undefined, but this is allowed by buffer-layout
1652+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16511653
dest[defaultLayout!.property!] = defaultLayout!.decode(b, offset + contentOffset);
16521654
} else {
16531655
dest = clo.decode(b, offset);
@@ -1666,13 +1668,15 @@ export class Union extends Layout<LayoutObject> {
16661668
if (undefined === vlo) {
16671669
const dlo = this.discriminator;
16681670
// this.defaultLayout is not undefined when vlo is undefined
1671+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16691672
const clo = this.defaultLayout!;
16701673
let contentOffset = 0;
16711674
if (this.usesPrefixDiscriminator) {
16721675
contentOffset = (dlo as UnionLayoutDiscriminator).layout.span;
16731676
}
16741677
dlo.encode(src[dlo.property], b, offset);
16751678
// clo.property is not undefined when vlo is undefined
1679+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16761680
return contentOffset + clo.encode(src[clo.property!], b, offset + contentOffset);
16771681
}
16781682
return vlo.encode(src, b, offset);
@@ -1752,6 +1756,8 @@ export class Union extends Layout<LayoutObject> {
17521756
* @augments {Layout}
17531757
*/
17541758
export class VariantLayout extends Layout<LayoutObject> {
1759+
// `property` is assigned in the Layout constructor
1760+
// @ts-ignore
17551761
property: string;
17561762
union: Union;
17571763
variant: number;
@@ -2033,10 +2039,10 @@ export class BitStructure extends Layout<LayoutObject> {
20332039
* Layout#property|property}.
20342040
*
20352041
* @return {Boolean} */
2042+
// `Boolean` conflicts with the native primitive type
20362043
// eslint-disable-next-line @typescript-eslint/ban-types
20372044
addBoolean(property: string): Boolean {
20382045
// This is my Boolean, not the Javascript one.
2039-
// eslint-disable-next-line no-new-wrappers
20402046
const bf = new Boolean(this, property);
20412047
this.fields.push(bf);
20422048
return bf;
@@ -2482,159 +2488,157 @@ export class Constant<T> extends Layout<T> {
24822488
}
24832489

24842490
/** Factory for {@link GreedyCount}. */
2485-
export const greedy = ((elementSpan: number, property?: string) => new GreedyCount(elementSpan, property));
2491+
export const greedy = ((elementSpan: number, property?: string): GreedyCount => new GreedyCount(elementSpan, property));
24862492

24872493
/** Factory for {@link OffsetLayout}. */
2488-
export const offset
2489-
= ((layout: Layout<number>, offset?: number, property?: string) => new OffsetLayout(layout, offset, property));
2494+
export const offset = ((layout: Layout<number>, offset?: number, property?: string): OffsetLayout =>
2495+
new OffsetLayout(layout, offset, property));
24902496

24912497
/** Factory for {@link UInt|unsigned int layouts} spanning one
24922498
* byte. */
2493-
export const u8 = ((property?: string) => new UInt(1, property));
2499+
export const u8 = ((property?: string): UInt => new UInt(1, property));
24942500

24952501
/** Factory for {@link UInt|little-endian unsigned int layouts}
24962502
* spanning two bytes. */
2497-
export const u16 = ((property?: string) => new UInt(2, property));
2503+
export const u16 = ((property?: string): UInt => new UInt(2, property));
24982504

24992505
/** Factory for {@link UInt|little-endian unsigned int layouts}
25002506
* spanning three bytes. */
2501-
export const u24 = ((property?: string) => new UInt(3, property));
2507+
export const u24 = ((property?: string): UInt => new UInt(3, property));
25022508

25032509
/** Factory for {@link UInt|little-endian unsigned int layouts}
25042510
* spanning four bytes. */
2505-
export const u32 = ((property?: string) => new UInt(4, property));
2511+
export const u32 = ((property?: string): UInt => new UInt(4, property));
25062512

25072513
/** Factory for {@link UInt|little-endian unsigned int layouts}
25082514
* spanning five bytes. */
2509-
export const u40 = ((property?: string) => new UInt(5, property));
2515+
export const u40 = ((property?: string): UInt => new UInt(5, property));
25102516

25112517
/** Factory for {@link UInt|little-endian unsigned int layouts}
25122518
* spanning six bytes. */
2513-
export const u48 = ((property?: string) => new UInt(6, property));
2519+
export const u48 = ((property?: string): UInt => new UInt(6, property));
25142520

25152521
/** Factory for {@link NearUInt64|little-endian unsigned int
25162522
* layouts} interpreted as Numbers. */
2517-
export const nu64 = ((property?: string) => new NearUInt64(property));
2523+
export const nu64 = ((property?: string): NearUInt64 => new NearUInt64(property));
25182524

25192525
/** Factory for {@link UInt|big-endian unsigned int layouts}
25202526
* spanning two bytes. */
2521-
export const u16be = ((property?: string) => new UIntBE(2, property));
2527+
export const u16be = ((property?: string): UIntBE => new UIntBE(2, property));
25222528

25232529
/** Factory for {@link UInt|big-endian unsigned int layouts}
25242530
* spanning three bytes. */
2525-
export const u24be = ((property?: string) => new UIntBE(3, property));
2531+
export const u24be = ((property?: string): UIntBE => new UIntBE(3, property));
25262532

25272533
/** Factory for {@link UInt|big-endian unsigned int layouts}
25282534
* spanning four bytes. */
2529-
export const u32be = ((property?: string) => new UIntBE(4, property));
2535+
export const u32be = ((property?: string): UIntBE => new UIntBE(4, property));
25302536

25312537
/** Factory for {@link UInt|big-endian unsigned int layouts}
25322538
* spanning five bytes. */
2533-
export const u40be = ((property?: string) => new UIntBE(5, property));
2539+
export const u40be = ((property?: string): UIntBE => new UIntBE(5, property));
25342540

25352541
/** Factory for {@link UInt|big-endian unsigned int layouts}
25362542
* spanning six bytes. */
2537-
export const u48be = ((property?: string) => new UIntBE(6, property));
2543+
export const u48be = ((property?: string): UIntBE => new UIntBE(6, property));
25382544

25392545
/** Factory for {@link NearUInt64BE|big-endian unsigned int
25402546
* layouts} interpreted as Numbers. */
2541-
export const nu64be = ((property?: string) => new NearUInt64BE(property));
2547+
export const nu64be = ((property?: string): NearUInt64BE => new NearUInt64BE(property));
25422548

25432549
/** Factory for {@link Int|signed int layouts} spanning one
25442550
* byte. */
2545-
export const s8 = ((property?: string) => new Int(1, property));
2551+
export const s8 = ((property?: string): Int => new Int(1, property));
25462552

25472553
/** Factory for {@link Int|little-endian signed int layouts}
25482554
* spanning two bytes. */
2549-
export const s16 = ((property?: string) => new Int(2, property));
2555+
export const s16 = ((property?: string): Int => new Int(2, property));
25502556

25512557
/** Factory for {@link Int|little-endian signed int layouts}
25522558
* spanning three bytes. */
2553-
export const s24 = ((property?: string) => new Int(3, property));
2559+
export const s24 = ((property?: string): Int => new Int(3, property));
25542560

25552561
/** Factory for {@link Int|little-endian signed int layouts}
25562562
* spanning four bytes. */
2557-
export const s32 = ((property?: string) => new Int(4, property));
2563+
export const s32 = ((property?: string): Int => new Int(4, property));
25582564

25592565
/** Factory for {@link Int|little-endian signed int layouts}
25602566
* spanning five bytes. */
2561-
export const s40 = ((property?: string) => new Int(5, property));
2567+
export const s40 = ((property?: string): Int => new Int(5, property));
25622568

25632569
/** Factory for {@link Int|little-endian signed int layouts}
25642570
* spanning six bytes. */
2565-
export const s48 = ((property?: string) => new Int(6, property));
2571+
export const s48 = ((property?: string): Int => new Int(6, property));
25662572

25672573
/** Factory for {@link NearInt64|little-endian signed int layouts}
25682574
* interpreted as Numbers. */
2569-
export const ns64 = ((property?: string) => new NearInt64(property));
2575+
export const ns64 = ((property?: string): NearInt64 => new NearInt64(property));
25702576

25712577
/** Factory for {@link Int|big-endian signed int layouts}
25722578
* spanning two bytes. */
2573-
export const s16be = ((property?: string) => new IntBE(2, property));
2579+
export const s16be = ((property?: string): IntBE => new IntBE(2, property));
25742580

25752581
/** Factory for {@link Int|big-endian signed int layouts}
25762582
* spanning three bytes. */
2577-
export const s24be = ((property?: string) => new IntBE(3, property));
2583+
export const s24be = ((property?: string): IntBE => new IntBE(3, property));
25782584

25792585
/** Factory for {@link Int|big-endian signed int layouts}
25802586
* spanning four bytes. */
2581-
export const s32be = ((property?: string) => new IntBE(4, property));
2587+
export const s32be = ((property?: string): IntBE => new IntBE(4, property));
25822588

25832589
/** Factory for {@link Int|big-endian signed int layouts}
25842590
* spanning five bytes. */
2585-
export const s40be = ((property?: string) => new IntBE(5, property));
2591+
export const s40be = ((property?: string): IntBE => new IntBE(5, property));
25862592

25872593
/** Factory for {@link Int|big-endian signed int layouts}
25882594
* spanning six bytes. */
2589-
export const s48be = ((property?: string) => new IntBE(6, property));
2595+
export const s48be = ((property?: string): IntBE => new IntBE(6, property));
25902596

25912597
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
25922598
* interpreted as Numbers. */
2593-
export const ns64be = ((property?: string) => new NearInt64BE(property));
2599+
export const ns64be = ((property?: string): NearInt64BE => new NearInt64BE(property));
25942600

25952601
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
2596-
export const f32 = ((property?: string) => new Float(property));
2602+
export const f32 = ((property?: string): Float => new Float(property));
25972603

25982604
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
2599-
export const f32be = ((property?: string) => new FloatBE(property));
2605+
export const f32be = ((property?: string): FloatBE => new FloatBE(property));
26002606

26012607
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
2602-
export const f64 = ((property?: string) => new Double(property));
2608+
export const f64 = ((property?: string): Double => new Double(property));
26032609

26042610
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
2605-
export const f64be = ((property?: string) => new DoubleBE(property));
2611+
export const f64be = ((property?: string): DoubleBE => new DoubleBE(property));
26062612

26072613
/** Factory for {@link Structure} values. */
2608-
export const struct
2609-
= (<T>(fields: Layout<T[keyof T]>[], property?: string, decodePrefixes?: boolean) =>
2614+
export const struct = (<T>(fields: Layout<T[keyof T]>[], property?: string, decodePrefixes?: boolean): Structure<T> =>
26102615
new Structure<T>(fields, property, decodePrefixes));
26112616

26122617
/** Factory for {@link BitStructure} values. */
2613-
export const bits
2614-
= ((word: UInt | UIntBE, msb: boolean | string, property?: string) => new BitStructure(word, msb, property));
2618+
export const bits = ((word: UInt | UIntBE, msb: boolean | string, property?: string): BitStructure =>
2619+
new BitStructure(word, msb, property));
26152620

26162621
/** Factory for {@link Sequence} values. */
2617-
export const seq
2618-
= (<T>(elementLayout: Layout<T>, count: number | ExternalLayout, property?: string) =>
2622+
export const seq = (<T>(elementLayout: Layout<T>, count: number | ExternalLayout, property?: string): Sequence<T> =>
26192623
new Sequence<T>(elementLayout, count, property));
26202624

26212625
/** Factory for {@link Union} values. */
2622-
export const union
2623-
= ((discr: Layout<LayoutObject> | UnionDiscriminator, defaultLayout: Layout<LayoutObject> | null, property: string) =>
2626+
export const union = ((discr: Layout<LayoutObject> | UnionDiscriminator,
2627+
defaultLayout: Layout<LayoutObject> | null, property: string): Union =>
26242628
new Union(discr, defaultLayout, property));
26252629

26262630
/** Factory for {@link UnionLayoutDiscriminator} values. */
2627-
export const unionLayoutDiscriminator
2628-
= ((layout: ExternalLayout, property: string) => new UnionLayoutDiscriminator(layout, property));
2631+
export const unionLayoutDiscriminator = ((layout: ExternalLayout, property: string): UnionLayoutDiscriminator =>
2632+
new UnionLayoutDiscriminator(layout, property));
26292633

26302634
/** Factory for {@link Blob} values. */
2631-
export const blob = ((length: number | ExternalLayout, property?: string) => new Blob(length, property));
2635+
export const blob = ((length: number | ExternalLayout, property?: string): Blob => new Blob(length, property));
26322636

26332637
/** Factory for {@link CString} values. */
2634-
export const cstr = ((property?: string) => new CString(property));
2638+
export const cstr = ((property?: string): CString => new CString(property));
26352639

26362640
/** Factory for {@link UTF8} values. */
2637-
export const utf8 = ((maxSpan: number, property?: string) => new UTF8(maxSpan, property));
2641+
export const utf8 = ((maxSpan: number, property?: string): UTF8 => new UTF8(maxSpan, property));
26382642

26392643
/** Factory for {@link Constant} values. */
2640-
export const constant = (<T>(value: T, property?: string) => new Constant(value, property));
2644+
export const constant = (<T>(value: T, property?: string): Constant<T> => new Constant(value, property));

0 commit comments

Comments
 (0)