@k67/kaitai-struct-ts 0.2.0 โ 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -24
- package/dist/index.d.mts +74 -2
- package/dist/index.d.ts +74 -2
- package/dist/index.js +1223 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1223 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +18 -20
package/README.md
CHANGED
@@ -1,24 +1,39 @@
|
|
1
|
-
|
1
|
+
<div align="center">
|
2
|
+
<img src="assets/logo.png" alt="kaitai-struct-ts" width="200"/>
|
3
|
+
|
4
|
+
# kaitai-struct-ts
|
2
5
|
|
3
|
-
[](https://www.npmjs.com/package/@k67/kaitai-struct-ts)
|
4
|
-
[](https://github.com/fabianopinto/kaitai-struct-ts/actions)
|
5
|
-
[](https://opensource.org/licenses/MIT)
|
6
|
-
[](https://www.typescriptlang.org/)
|
7
|
-
[](https://nodejs.org/)
|
6
|
+
[](https://www.npmjs.com/package/@k67/kaitai-struct-ts)
|
7
|
+
[](https://github.com/fabianopinto/kaitai-struct-ts/actions)
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
9
|
+
[](https://www.typescriptlang.org/)
|
10
|
+
[](https://nodejs.org/)
|
8
11
|
|
9
|
-
A
|
12
|
+
**A runtime interpreter for [Kaitai Struct](https://kaitai.io/) binary format definitions in TypeScript.**
|
13
|
+
</div>
|
14
|
+
|
15
|
+
---
|
10
16
|
|
11
17
|
Parse any binary data format by providing a `.ksy` (Kaitai Struct YAML) definition file - no compilation step required!
|
12
18
|
|
13
19
|
## Features
|
14
20
|
|
21
|
+
### Core Features
|
15
22
|
- ๐ **Runtime interpretation** - No code generation needed
|
16
23
|
- ๐ฆ **Zero dependencies** (runtime) - Only YAML parser for development
|
17
24
|
- ๐ฏ **TypeScript native** - Full type safety and IntelliSense support
|
18
25
|
- ๐ **Universal** - Works in Node.js and browsers
|
19
|
-
- ๐งช **Well tested** -
|
26
|
+
- ๐งช **Well tested** - 98 comprehensive tests
|
20
27
|
- ๐ **Well documented** - Clear API and examples
|
21
28
|
|
29
|
+
### Advanced Features
|
30
|
+
- โก **Expression evaluation** - Full support for Kaitai expressions
|
31
|
+
- ๐ **Switch/case types** - Dynamic type selection based on data
|
32
|
+
- ๐ **Instances** - Lazy-evaluated fields with caching
|
33
|
+
- ๐จ **Enums** - Named constants with expression access
|
34
|
+
- ๐ **Conditional parsing** - if, repeat-expr, repeat-until
|
35
|
+
- ๐ **Positioned reads** - Absolute positioning with pos attribute
|
36
|
+
|
22
37
|
## Installation
|
23
38
|
|
24
39
|
```bash
|
@@ -152,22 +167,30 @@ pnpm format
|
|
152
167
|
|
153
168
|
## Roadmap
|
154
169
|
|
155
|
-
### Phase 1: Foundation (MVP) -
|
156
|
-
- Basic parsing capability
|
157
|
-
- Fixed-size structures
|
158
|
-
- Primitive types
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
-
|
163
|
-
-
|
164
|
-
-
|
165
|
-
|
166
|
-
|
167
|
-
-
|
168
|
-
|
169
|
-
-
|
170
|
-
-
|
170
|
+
### Phase 1: Foundation (MVP) - โ
Complete
|
171
|
+
- โ
Basic parsing capability
|
172
|
+
- โ
Fixed-size structures
|
173
|
+
- โ
Primitive types (u1-u8, s1-s8, f4, f8)
|
174
|
+
- โ
String encoding support
|
175
|
+
- โ
Byte arrays and positioning
|
176
|
+
|
177
|
+
### Phase 2: Core Features - โ
Complete
|
178
|
+
- โ
Expression evaluator (full Kaitai expression language)
|
179
|
+
- โ
Conditionals (if attribute)
|
180
|
+
- โ
Enums with expression access
|
181
|
+
- โ
Repetitions (repeat-expr, repeat-until, repeat-eos)
|
182
|
+
- โ
Calculated sizes and positions
|
183
|
+
|
184
|
+
### Phase 3: Advanced Features - ๐ In Progress (30% Complete)
|
185
|
+
- โ
Switch/case type selection
|
186
|
+
- โ
Instances (lazy-evaluated fields)
|
187
|
+
- โณ Substreams and processing
|
188
|
+
- โณ Parametric types
|
189
|
+
- โณ Bit-sized integers
|
190
|
+
- โณ Type imports
|
191
|
+
- โณ Performance optimizations
|
192
|
+
|
193
|
+
**Current Status:** ~85% complete toward v1.0.0
|
171
194
|
|
172
195
|
## Contributing
|
173
196
|
|
package/dist/index.d.mts
CHANGED
@@ -299,6 +299,8 @@ interface AttributeSpec {
|
|
299
299
|
id?: string;
|
300
300
|
/** Data type to read */
|
301
301
|
type?: string | SwitchType;
|
302
|
+
/** Arguments for parametric types */
|
303
|
+
'type-args'?: Array<string | number | boolean>;
|
302
304
|
/** Size of the field (in bytes or expression) */
|
303
305
|
size?: number | string;
|
304
306
|
/** Size until specific byte value */
|
@@ -611,14 +613,17 @@ declare class Context {
|
|
611
613
|
private parentStack;
|
612
614
|
/** Current object being parsed */
|
613
615
|
private _current;
|
616
|
+
/** Enum definitions from schema */
|
617
|
+
private _enums;
|
614
618
|
/**
|
615
619
|
* Create a new execution context.
|
616
620
|
*
|
617
621
|
* @param _io - Binary stream being read
|
618
622
|
* @param _root - Root object of the parse tree
|
619
623
|
* @param _parent - Parent object (optional)
|
624
|
+
* @param enums - Enum definitions from schema (optional)
|
620
625
|
*/
|
621
|
-
constructor(_io: KaitaiStream, _root?: unknown, _parent?: unknown);
|
626
|
+
constructor(_io: KaitaiStream, _root?: unknown, _parent?: unknown, enums?: Record<string, EnumSpec>);
|
622
627
|
/**
|
623
628
|
* Get the current I/O stream.
|
624
629
|
* Accessible in expressions as `_io`.
|
@@ -682,6 +687,22 @@ declare class Context {
|
|
682
687
|
* @param value - Value to set
|
683
688
|
*/
|
684
689
|
set(name: string, value: unknown): void;
|
690
|
+
/**
|
691
|
+
* Get enum value by name.
|
692
|
+
* Used for enum access in expressions (EnumName::value).
|
693
|
+
*
|
694
|
+
* @param enumName - Name of the enum
|
695
|
+
* @param valueName - Name of the enum value
|
696
|
+
* @returns Enum value (number) or undefined
|
697
|
+
*/
|
698
|
+
getEnumValue(enumName: string, valueName: string): unknown;
|
699
|
+
/**
|
700
|
+
* Check if an enum exists.
|
701
|
+
*
|
702
|
+
* @param enumName - Name of the enum
|
703
|
+
* @returns True if enum exists
|
704
|
+
*/
|
705
|
+
hasEnum(enumName: string): boolean;
|
685
706
|
/**
|
686
707
|
* Create a child context for nested parsing.
|
687
708
|
* The current object becomes the parent in the child context.
|
@@ -737,9 +758,30 @@ declare class TypeInterpreter {
|
|
737
758
|
*
|
738
759
|
* @param stream - Binary stream to parse
|
739
760
|
* @param parent - Parent object (for nested types)
|
761
|
+
* @param typeArgs - Arguments for parametric types
|
740
762
|
* @returns Parsed object
|
741
763
|
*/
|
742
|
-
parse(stream: KaitaiStream, parent?: unknown): Record<string, unknown>;
|
764
|
+
parse(stream: KaitaiStream, parent?: unknown, typeArgs?: Array<string | number | boolean>): Record<string, unknown>;
|
765
|
+
/**
|
766
|
+
* Set up lazy-evaluated instance getters.
|
767
|
+
* Instances are computed on first access and cached.
|
768
|
+
*
|
769
|
+
* @param result - Result object to add getters to
|
770
|
+
* @param stream - Stream for parsing
|
771
|
+
* @param context - Execution context
|
772
|
+
* @private
|
773
|
+
*/
|
774
|
+
private setupInstances;
|
775
|
+
/**
|
776
|
+
* Parse an instance (lazy-evaluated field).
|
777
|
+
*
|
778
|
+
* @param instance - Instance specification
|
779
|
+
* @param stream - Stream to read from
|
780
|
+
* @param context - Execution context
|
781
|
+
* @returns Parsed or calculated value
|
782
|
+
* @private
|
783
|
+
*/
|
784
|
+
private parseInstance;
|
743
785
|
/**
|
744
786
|
* Parse a single attribute according to its specification.
|
745
787
|
*
|
@@ -782,10 +824,21 @@ declare class TypeInterpreter {
|
|
782
824
|
* @param type - Type name or switch specification
|
783
825
|
* @param stream - Stream to read from
|
784
826
|
* @param context - Execution context
|
827
|
+
* @param typeArgs - Arguments for parametric types
|
785
828
|
* @returns Parsed value
|
786
829
|
* @private
|
787
830
|
*/
|
788
831
|
private parseType;
|
832
|
+
/**
|
833
|
+
* Parse a switch type (type selection based on expression).
|
834
|
+
*
|
835
|
+
* @param switchType - Switch type specification
|
836
|
+
* @param stream - Stream to read from
|
837
|
+
* @param context - Execution context
|
838
|
+
* @returns Parsed value
|
839
|
+
* @private
|
840
|
+
*/
|
841
|
+
private parseSwitchType;
|
789
842
|
/**
|
790
843
|
* Parse a built-in type.
|
791
844
|
*
|
@@ -816,6 +869,25 @@ declare class TypeInterpreter {
|
|
816
869
|
* @private
|
817
870
|
*/
|
818
871
|
private readFloat;
|
872
|
+
/**
|
873
|
+
* Apply processing transformation to data.
|
874
|
+
* Supports basic transformations like zlib decompression.
|
875
|
+
*
|
876
|
+
* @param data - Data to process
|
877
|
+
* @param process - Processing specification
|
878
|
+
* @returns Processed data
|
879
|
+
* @private
|
880
|
+
*/
|
881
|
+
private applyProcessing;
|
882
|
+
/**
|
883
|
+
* Evaluate a value that can be an expression or literal.
|
884
|
+
*
|
885
|
+
* @param value - Value to evaluate (expression string, number, or boolean)
|
886
|
+
* @param context - Execution context
|
887
|
+
* @returns Evaluated result
|
888
|
+
* @private
|
889
|
+
*/
|
890
|
+
private evaluateValue;
|
819
891
|
}
|
820
892
|
|
821
893
|
/**
|
package/dist/index.d.ts
CHANGED
@@ -299,6 +299,8 @@ interface AttributeSpec {
|
|
299
299
|
id?: string;
|
300
300
|
/** Data type to read */
|
301
301
|
type?: string | SwitchType;
|
302
|
+
/** Arguments for parametric types */
|
303
|
+
'type-args'?: Array<string | number | boolean>;
|
302
304
|
/** Size of the field (in bytes or expression) */
|
303
305
|
size?: number | string;
|
304
306
|
/** Size until specific byte value */
|
@@ -611,14 +613,17 @@ declare class Context {
|
|
611
613
|
private parentStack;
|
612
614
|
/** Current object being parsed */
|
613
615
|
private _current;
|
616
|
+
/** Enum definitions from schema */
|
617
|
+
private _enums;
|
614
618
|
/**
|
615
619
|
* Create a new execution context.
|
616
620
|
*
|
617
621
|
* @param _io - Binary stream being read
|
618
622
|
* @param _root - Root object of the parse tree
|
619
623
|
* @param _parent - Parent object (optional)
|
624
|
+
* @param enums - Enum definitions from schema (optional)
|
620
625
|
*/
|
621
|
-
constructor(_io: KaitaiStream, _root?: unknown, _parent?: unknown);
|
626
|
+
constructor(_io: KaitaiStream, _root?: unknown, _parent?: unknown, enums?: Record<string, EnumSpec>);
|
622
627
|
/**
|
623
628
|
* Get the current I/O stream.
|
624
629
|
* Accessible in expressions as `_io`.
|
@@ -682,6 +687,22 @@ declare class Context {
|
|
682
687
|
* @param value - Value to set
|
683
688
|
*/
|
684
689
|
set(name: string, value: unknown): void;
|
690
|
+
/**
|
691
|
+
* Get enum value by name.
|
692
|
+
* Used for enum access in expressions (EnumName::value).
|
693
|
+
*
|
694
|
+
* @param enumName - Name of the enum
|
695
|
+
* @param valueName - Name of the enum value
|
696
|
+
* @returns Enum value (number) or undefined
|
697
|
+
*/
|
698
|
+
getEnumValue(enumName: string, valueName: string): unknown;
|
699
|
+
/**
|
700
|
+
* Check if an enum exists.
|
701
|
+
*
|
702
|
+
* @param enumName - Name of the enum
|
703
|
+
* @returns True if enum exists
|
704
|
+
*/
|
705
|
+
hasEnum(enumName: string): boolean;
|
685
706
|
/**
|
686
707
|
* Create a child context for nested parsing.
|
687
708
|
* The current object becomes the parent in the child context.
|
@@ -737,9 +758,30 @@ declare class TypeInterpreter {
|
|
737
758
|
*
|
738
759
|
* @param stream - Binary stream to parse
|
739
760
|
* @param parent - Parent object (for nested types)
|
761
|
+
* @param typeArgs - Arguments for parametric types
|
740
762
|
* @returns Parsed object
|
741
763
|
*/
|
742
|
-
parse(stream: KaitaiStream, parent?: unknown): Record<string, unknown>;
|
764
|
+
parse(stream: KaitaiStream, parent?: unknown, typeArgs?: Array<string | number | boolean>): Record<string, unknown>;
|
765
|
+
/**
|
766
|
+
* Set up lazy-evaluated instance getters.
|
767
|
+
* Instances are computed on first access and cached.
|
768
|
+
*
|
769
|
+
* @param result - Result object to add getters to
|
770
|
+
* @param stream - Stream for parsing
|
771
|
+
* @param context - Execution context
|
772
|
+
* @private
|
773
|
+
*/
|
774
|
+
private setupInstances;
|
775
|
+
/**
|
776
|
+
* Parse an instance (lazy-evaluated field).
|
777
|
+
*
|
778
|
+
* @param instance - Instance specification
|
779
|
+
* @param stream - Stream to read from
|
780
|
+
* @param context - Execution context
|
781
|
+
* @returns Parsed or calculated value
|
782
|
+
* @private
|
783
|
+
*/
|
784
|
+
private parseInstance;
|
743
785
|
/**
|
744
786
|
* Parse a single attribute according to its specification.
|
745
787
|
*
|
@@ -782,10 +824,21 @@ declare class TypeInterpreter {
|
|
782
824
|
* @param type - Type name or switch specification
|
783
825
|
* @param stream - Stream to read from
|
784
826
|
* @param context - Execution context
|
827
|
+
* @param typeArgs - Arguments for parametric types
|
785
828
|
* @returns Parsed value
|
786
829
|
* @private
|
787
830
|
*/
|
788
831
|
private parseType;
|
832
|
+
/**
|
833
|
+
* Parse a switch type (type selection based on expression).
|
834
|
+
*
|
835
|
+
* @param switchType - Switch type specification
|
836
|
+
* @param stream - Stream to read from
|
837
|
+
* @param context - Execution context
|
838
|
+
* @returns Parsed value
|
839
|
+
* @private
|
840
|
+
*/
|
841
|
+
private parseSwitchType;
|
789
842
|
/**
|
790
843
|
* Parse a built-in type.
|
791
844
|
*
|
@@ -816,6 +869,25 @@ declare class TypeInterpreter {
|
|
816
869
|
* @private
|
817
870
|
*/
|
818
871
|
private readFloat;
|
872
|
+
/**
|
873
|
+
* Apply processing transformation to data.
|
874
|
+
* Supports basic transformations like zlib decompression.
|
875
|
+
*
|
876
|
+
* @param data - Data to process
|
877
|
+
* @param process - Processing specification
|
878
|
+
* @returns Processed data
|
879
|
+
* @private
|
880
|
+
*/
|
881
|
+
private applyProcessing;
|
882
|
+
/**
|
883
|
+
* Evaluate a value that can be an expression or literal.
|
884
|
+
*
|
885
|
+
* @param value - Value to evaluate (expression string, number, or boolean)
|
886
|
+
* @param context - Execution context
|
887
|
+
* @returns Evaluated result
|
888
|
+
* @private
|
889
|
+
*/
|
890
|
+
private evaluateValue;
|
819
891
|
}
|
820
892
|
|
821
893
|
/**
|