@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 CHANGED
@@ -1,24 +1,39 @@
1
- # kaitai-struct-ts
1
+ <div align="center">
2
+ <img src="assets/logo.png" alt="kaitai-struct-ts" width="200"/>
3
+
4
+ # kaitai-struct-ts
2
5
 
3
- [![npm version](https://badge.fury.io/js/%40k67%2Fkaitai-struct-ts.svg)](https://www.npmjs.com/package/@k67/kaitai-struct-ts)
4
- [![CI](https://github.com/fabianopinto/kaitai-struct-ts/workflows/CI/badge.svg)](https://github.com/fabianopinto/kaitai-struct-ts/actions)
5
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue.svg)](https://www.typescriptlang.org/)
7
- [![Node.js](https://img.shields.io/badge/Node.js-18%2B-green.svg)](https://nodejs.org/)
6
+ [![npm version](https://badge.fury.io/js/%40k67%2Fkaitai-struct-ts.svg)](https://www.npmjs.com/package/@k67/kaitai-struct-ts)
7
+ [![CI](https://github.com/fabianopinto/kaitai-struct-ts/workflows/CI/badge.svg)](https://github.com/fabianopinto/kaitai-struct-ts/actions)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue.svg)](https://www.typescriptlang.org/)
10
+ [![Node.js](https://img.shields.io/badge/Node.js-18%2B-green.svg)](https://nodejs.org/)
8
11
 
9
- A **runtime interpreter** for [Kaitai Struct](https://kaitai.io/) binary format definitions in TypeScript.
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** - Comprehensive test coverage
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) - Current
156
- - Basic parsing capability
157
- - Fixed-size structures
158
- - Primitive types
159
-
160
- ### Phase 2: Core Features
161
- - Expression evaluator
162
- - Conditionals and enums
163
- - Repetitions
164
- - Instances
165
-
166
- ### Phase 3: Advanced Features
167
- - Substreams and processing
168
- - Bit-sized integers
169
- - Imports
170
- - Full spec compliance
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
  /**