npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

esbuild-plugin-kaitai

v1.0.0

Published

An esbuild plugin for importing Kaitai Struct files.

Downloads

61

Readme

esbuild-plugin-kaitai

An esbuild plugin to compile Kaitai struct files.

Prerequisites

Installation

Install the plugin from your javascript package manager

# yarn
yarn add --dev esbuild-plugin-kaitai

# or npm
npm install --save-dev esbuild-plugin-kaitai

Usage

Note: The Kaitai Compiler and Runtime do not directly support Typescript yet, but there's some workarounds you can use to get everything working nicely.

  1. Replace the kaitai-struct runtime with @tschrock/kaitai-struct for typescript support

    $ npm install kaitai-struct@npm:@tschrock/kaitai-struct
  2. Write a type shim for your .ksy file.

    I have written an experimental type generator for .ksy files. It's not perfect, and will probably break for complex schemas, but it will give you a good starting point.

    Installation

    $ npm i -g @tschrock/kaitai-dts

    Usage

    $ kaitai-dts my-data.ksy
    // my-data.ksy.d.ts
    declare module "my-data.ksy" {
        class MyData {
            version: string;
            recordType: MyData.ExampleEnum;
            recordData: UInt8Array;
            constructor(_io: any, _parent?: any, _root?: any);
            _read(): void;
        }
        namespace MyData {
            enum ExampleEnum {
                ITEM_1 = 1,
                ITEM_2 = 2,
                ITEM_3 = 3,
            }
        }
    }
  3. Add the kaitai plugin to the list of plugins in your build script:

    // build.ts
    import esbuild from 'esbuild';
    import kaitaiLoader from 'esbuild-plugin-kaitai';
    
    esbuild.build({
        ...
        plugins: [
            kaitaiLoader({
                // Plugin config
            })
        ]
        ...
    });
  4. You can now directly import .ksy files in your application, and esbuild will automatically compile them.

    // myapp.ts
    import { readFileSync } from 'fs';
    import KaitaiStream from 'kaitai-struct/KaitaiStream';
    import MyData from 'my-data.ksy';
    
    const myDataBin = readFileSync("./mydata.bin")
    const myData = new MyData(new KaitaiStream(myDataBin));
    console.log(myData);
  1. Add the kaitai plugin to the list of plugins in your build script:

    // build.js
    import esbuild from 'esbuild';
    import kaitaiLoader from 'esbuild-plugin-kaitai';
    
    esbuild.build({
        ...
        plugins: [
            kaitaiLoader({
                // Plugin config
            })
        ]
        ...
    });
  2. You can now directly import .ksy files in your application, and esbuild will automatically compile them.

    // myapp.js
    import { readFileSync } from 'fs';
    import KaitaiStream from 'kaitai-struct/KaitaiStream';
    import MyData from 'my-data.ksy';
    
    const myDataBin = readFileSync("./mydata.bin")
    const myData = new MyData(new KaitaiStream(myDataBin));
    console.log(myData);
  1. Add the kaitai plugin to the list of plugins in your build script:

    // build.js
    const esbuild = require('esbuild');
    const kaitaiLoader = require('esbuild-plugin-kaitai');
    
    esbuild.build({
        ...
        plugins: [
            kaitaiLoader({
                // Plugin config
            })
        ]
        ...
    });
  2. You can now directly require .ksy files in your application, and esbuild will automatically compile them.

    // myapp.js
    const { readFileSync } = require('fs');
    const KaitaiStream  = require('kaitai-struct/KaitaiStream');
    const MyData = require('my-data.ksy');
    
    const myDataBin = readFileSync("./mydata.bin")
    const myData = new MyData(new KaitaiStream(myDataBin));
    console.log(myData);

Configuration

Using the CLI Compiler

| Option | Type | Description | |-----------------|------------|-------------| | compilerPath | string | Optional.The path to the kaitai compiler binary. | | compilerFlags | string[] | Optional.Extra CLI flags to pass to the kaitai compiler. | | compilerTimeout | number | Optional.A timeout for the compiler. |

Using the Compiler API

Note: See licensing info below

| Option | Type | Description | |------------|------------|-------------| | compiler | Compiler | Required.The kaitai compiler. | | fileLoader | FileLoader | Optional.The file loader. | | debug | boolean | Optional.Create a debug build. |

For Example:

import esbuild from 'esbuild';
import kaitaiLoader from 'esbuild-plugin-kaitai';
import KaitaiStructCompiler from 'kaitai-struct-compiler';

esbuild.build({
    ...
    plugins: [
        kaitaiLoader({
            compiler: new KaitaiStructCompiler(),
            debug: true
        })
    ]
    ...
});

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Extra Licensing Info

The reference Kaitai compiler, kaitai-struct-compiler, is licened under the GPL-3.0 license. In order to prevent license compatability issues, this project does not include or directly link to the kaitai compiler. As the user, you must provide the compiler program yourself. There are two ways you can do this:

CLI Compiler (default)

By default, this plugin will attempt to execute the Kaitai compiler program from the command line. Since the compiler runs as a separate program, this should avoid any licensing conflicts.

  1. Download and Install the Kaitai Compiler from the Kaitai website.

  2. Make sure the kaitai-struct-compiler program is in your PATH.

If the Kaitai compiler is not in your PATH, you can specify it's location using the compilerPath configuration option or KAITAI_PATH environment variable.

Compiler API

You can also provide your own compiler via the compiler configuration option. You can either provide the reference compiler (In which case your build scrips might be subject to the GPL - don't ask me, I'm not a lawyer), or you can provide an alternative, API-compatable compiler.