Skip to content
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,19 @@ These options can be used in all parsers.
});
```

- `hidden` - If `true`, the field can be referenced by later fields (e.g. as
`length`) but is removed from the result once the parser that defines it
finishes. Fields of a nested parser that defines it can still reach it via
`$parent`, but it is gone by the time a field outside that parser runs.

```javascript
// Use the length prefix to read the array, but don't keep it
const parser = new Parser()
.uint8("length", { hidden: true })
.array("data", { type: "uint8", length: "length" });
// => { data: [...] }
```

### Context variables
You can use some special fields while parsing to traverse your structure.
These context variables will be removed after the parsing process.
Expand Down
12 changes: 10 additions & 2 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ interface ParserOptions {
offset?: number | string | ((item: any) => number);
wrapper?: (buffer: Buffer) => Buffer;
skip?: boolean;
hidden?: boolean;
}

type Types = PrimitiveTypes | ComplexTypes;
Expand Down Expand Up @@ -1149,10 +1150,17 @@ export class Parser {

// Generate consecutive fields without consuming one stack frame per field.
private generate(ctx: Context) {
this.generateField(ctx);
const fields: Parser[] = [this];
for (let parser = this.next; parser; parser = parser.next) {
parser.generateField(ctx);
fields.push(parser);
}
fields.forEach((field) => field.generateField(ctx));
// Hidden fields stay readable by later fields of this parser, then go.
fields.forEach((field) => {
if (field.options.hidden && field.varName) {
ctx.pushCode(`delete ${ctx.generateVariable(field.varName)};`);
}
});
return ctx;
}

Expand Down
53 changes: 53 additions & 0 deletions test/composite_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,59 @@ function compositeParserTests(
});
});

describe("Hidden fields", () => {
it("should drop hidden fields after they are used", () => {
const parser = new Parser()
.uint8("len", { hidden: true })
.array("xs", { type: "uint8", length: "len" });
expect(parser.parse(factory([2, 7, 8]))).toStrictEqual({ xs: [7, 8] });
});

it("should drop hidden fields in nested parsers and array items", () => {
const item = new Parser()
.uint8("len", { hidden: true })
.string("s", { length: "len" });
const parser = new Parser()
.nest("n", { type: item })
.array("items", { type: item, length: 2 });
expect(
parser.parse(factory([1, 0x61, 1, 0x62, 2, 0x63, 0x64])),
).toStrictEqual({ n: { s: "a" }, items: [{ s: "b" }, { s: "cd" }] });
});

it("should keep hidden fields visible to children via $parent", () => {
const parser = new Parser()
.useContextVars()
.uint8("len", { hidden: true })
.nest("n", {
type: new Parser().array("xs", {
type: "uint8",
length: function (this: { $parent: { len: number } }) {
return this.$parent.len;
},
}),
});
expect(parser.parse(factory([2, 7, 8]))).toStrictEqual({
n: { xs: [7, 8] },
});
});

it("should drop hidden bit fields and nested objects", () => {
const parser = new Parser()
.bit4("flags", { hidden: true })
.bit4("x")
.nest("header", {
type: new Parser().uint8("len"),
hidden: true,
})
.array("xs", { type: "uint8", length: "header.len" });
expect(parser.parse(factory([0xa5, 1, 9]))).toStrictEqual({
x: 5,
xs: [9],
});
});
});

describe("Parse other fields after bit", () => {
it("Parse uint8", () => {
const buffer = factory([0, 1, 0, 4]);
Expand Down