open-index/open-npm huggingface dataset:
npm package: @scale-codec/definition-compiler
Description: SCALE-codec types namespace definition compiler
Latest version: 4.0.1

# @scale-codec/definition-compiler ![build status](https://img.shields.io/github/checks-status/soramitsu/scale-codec-js-library/master) ![version](https://img.shields.io/npm/v/@scale-codec/definition-compiler) ![license](https://img.shields.io/npm/l/@scale-codec/definition-compiler)

Code generation for SCALE namespaces.

## Installation

Available on NPM:

```shell
npm i @scale-codec/definition-compiler
```

## Features

- Support all codecs provided by `@scale-codec/core`
- Support type aliases
- Support circular references between types
- Support type imports from other places
- Each compiled type is opaque (except aliases)
- Defaults are configurable, including "std" types and the runtime library itself

## Example

Having the following namespace in Rust:

```rust
struct Person {
    name: String,
    age: u8,
    document: PersonDocument
}

enum PersonDocument {
    Id(u8),
    Passport(Passport)
}

struct Passport(u32, u32);

type PersonsMap = HashMap<u8, Person>;

type PersonsVec = Vec<Person>;

struct PublicKey {
    // Fixed-len array
    payload: [u8; 32]
}
```

You can define a schema for it and compile it into TypeScript module:

```ts
import { NamespaceDefinition, renderNamespaceDefinition } from '@scale-codec/definition-compiler'

const schema: NamespaceDefinition = {
  Person: {
    t: 'struct',
    fields: [
      {
        name: 'name',
        ref: 'Str',
      },
      {
        name: 'age',
        ref: 'U8',
      },
      {
        name: 'document',
        ref: 'PersonDocument',
      },
    ],
  },
  PersonDocument: {
    t: 'enum',
    variants: [
      {
        name: 'Id',
        discriminant: 0,
        ref: 'U8',
      },
      {
        name: 'Passport',
        discriminant: 1,
        ref: 'Passport',
      },
    ],
  },
  Passport: {
    t: 'tuple',
    items: ['U32', 'U32'],
  },
  PersonsMap: {
    t: 'map',
    key: 'U8',
    value: 'Person',
  },
  PersonsVec: {
    t: 'vec',
    item: 'Person',
  },
  PublicKey: {
    t: 'struct',
    fields: [
      {
        name: 'payload',
        ref: 'Array_u8_32',
      },
    ],
  },
  Array_u8_32: {
    t: 'array',
    item: 'U8',
    len: 32,
  },
}

const code = renderNamespaceDefinition(schema)
```

`code` could be written into a module and then used as:

```ts
import { Person, PersonDocument } from './compiled-module'

function printPersonName(person: Person) {
    console.log(person.name)
}

const person = Person({
  name: 'James',
  age: 52,
  document: PersonDocument('Id', 911),
})

const bytes: Uint8Array = Person.toBuffer(person)
```

**Note:** all compiled codecs are also _factories_ to define values. Values themselves are defined as _opaque_ types, i.e. they could be defined either through its factory (`Person({...})`), or through explicit type casting (`{...} as Person`).

## API

[`@scale-codec/definition-compiler` API](https://soramitsu.github.io/scale-codec-js-library/api/modules/scale_codec_definition_compiler)
npm package: @scale-codec/definition-runtime
Description: Runtime for compiled result from @scale-codec/definition-compiler
Latest version: 4.0.1

# @scale-codec/definition-runtime ![build status](https://img.shields.io/github/checks-status/soramitsu/scale-codec-js-library/master) ![version](https://img.shields.io/npm/v/@scale-codec/definition-runtime) ![license](https://img.shields.io/npm/l/@scale-codec/definition-runtime)

Runtime for `@scale-codec/definition-compiler`. Includes standard codecs (such as `U8`, `Str`, `Bool`, `Unit` etc), codec factories and [logging facilities](#tracking-and-logging).

## Installation

Available on NPM:

```shell
npm i @scale-codec/definition-compiler
```

## Usage

Usually, this package is paired with the output of the compiler. It is the library that the compiled code imports utilities from.

For advanced usage you can explore [`@scale-codec/definition-runtime` API](https://soramitsu.github.io/scale-codec-js-library/api/modules/scale_codec_definition_runtime).

## Tracking and Logging

All codecs use tracking API. For now, there is no need to track encoding, but you can use this API to track the decoding process. This might be useful for debugging purposes. To see what is happening, a _tracker_ should be mounted. Trackers are not mounted by default, and the runtime cost in that case is minimal.

This library provides `Logger`[^1], a tracker implementation that logs decoding to the console. The following code creates a logger instance and "mounts" it as a current tracker:

```ts
import { Logger } from '@scale-codec/definition-runtime'

// some options could be passed
new Logger().mount()
```

[^1]: While `Logger` has quite large amount of code within it (more than the rest of the runtime library), **it is tree-shakeable**, i.e. if you bundle the code that doesn't use `Logger`, it will be tree-shaken.

After the tracker is mounted, it will print debug information on how encoding/decoding is going. Here are screenshots of how it looks in Node.js and in Browser:

![in Node.js](logger-demo/img/node.png)

![in Firefox Devtools](logger-demo/img/firefox.png)

Tracking API is exposed from the library and could be used for custom implementations as well.
npm package: @scale-codec/enum
Description: Simple JavaScript abstraction around Rust Enums
Latest version: 2.1.1

# `@scale-codec/enum` ![build status](https://img.shields.io/github/checks-status/soramitsu/scale-codec-js-library/master) ![version](https://img.shields.io/npm/v/@scale-codec/enum) ![license](https://img.shields.io/npm/l/@scale-codec/enum)

Small tagged-union library for TypeScript.

## Features

- **Type narrowing** and **exhaustiveness check** (due to the library following TypeScript's [discriminated union pattern](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions))
- Creation of type-safe variants
- Type inference during variant creation

## Installation

Available on NPM:

```shell
npm i @scale-codec/enum
```

## Example

**Enum in Rust:**

```rust
enum Event {
  PageLoaded,
  KeyPress(String),
  MouseClick { x: u32, y: u32 }
}
```

**Enum in TypeScript:**

```ts
import { Enumerate, variant } from '@scale-codec/enum'

// Define the enum
type Event = Enumerate<{
  PageLoaded: []
  KeyPress: [string]
  MouseClick: [{ x: number; y: number }]
}>

// Construct an actual value
const event1: Event = variant('KeyPress', '<enter>')
const event2 = variant<Event>('MouseClick', { x: 5, y: 10 })
const event3 = variant<Event>('PageLoaded')

// Access the content
if (event1.tag === 'MouseClick') {
  // Types are narrowed
  const { x, y } = event1.content
}
```

## API

[`@scale-codec/enum` API](https://soramitsu.github.io/scale-codec-js-library/api/modules/scale_codec_enum)
npm package: @scale-codec/namespace-codegen
Description: Tool for automatization of SCALE-namespace generation
Latest version: 0.1.0

# @scale-codec/namespace-codegen

Automatization tool for `@scale-codec/namespace` definition code generation.

### Usage

```ts
import { generate } from '@scale-codec/namespace-codegen';
import def from './def';
import fs from 'fs/promises';
import path from 'path';

(async function () {
    const code = generate(def, {
        namespaceTypeName: 'Namespace',
        namespaceValueName: 'types',
        importLib: '@scale-codec/namespace',
    });

    await fs.writeFile(path.join(__dirname, 'ns.ts'), code);
})().catch((err) => {
    console.error('Generation failed', err);
    process.exit(1);
});
```
npm package: @scale-codec/namespace-codegen-cli
Description: CLI for @scale-codec/namespace-codegen
Latest version: 0.1.0

# @scale-codec/namespace-codegen-cli

CLI for `@scale-codec/namespace-codegen`.

### Install

```sh
npm i --save-dev @scale-codec/namespace-codegen-cli
```

### Usage (after installation)

```
$ npx generate-scale-namespace --help
generate-scale-namespace/0.1.0

Usage:
  $ generate-scale-namespace

Commands:
    Generate namespace from definition

For more info, run any command with the `--help` flag:
  $ generate-scale-namespace --help

Options:
  -i, --input <file>                   [string] Path to input file with namespace definition. Allowed types: .json, .(c|m)?js, .ts
  -o, --output <file>                  [string] Path to the output TypeScript file
  -f, --force                          [boolean] Force overwrite output path if it exists
  --genNamespaceType <identificator>   [string] Name of generated namespace type
  --genNamespaceValue <identificator>  [string] Name of generated namespace value
  --genImportFrom [packageName]        [string] Library that contains namespace implementation (default: @scale-codec/namespace)
  --genCamel                           [boolean] Use 'camelCase' for struct fields or not
  -h, --help                           Display this message
  -v, --version                        Display version number
```

### TODO

-   Maybe use another CLI helper that works fine with required arguments.
npm package: @scale-codec/util
Description: Shared @scale-codec utilities
Latest version: 1.1.2

# @scale-codec/util ![build status](https://img.shields.io/github/checks-status/soramitsu/scale-codec-js-library/master) ![version](https://img.shields.io/npm/v/@scale-codec/util) ![license](https://img.shields.io/npm/l/@scale-codec/util)

Shared utilities used across `@scale-codec/*` libraries

## Installation

Available on NPM:

```bash
npm i @scale-codec/util
```

## API

[`@scale-codec/util` API](https://soramitsu.github.io/scale-codec-js-library/api/modules/scale_codec_util)