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

dynamic-encoder

v1.0.0

Published

A JavaScript library for dynamically generating, importing, and executing custom encoders using compact-encoding, with support for dependency injection and hash-based integrity verification.

Downloads

4

Readme

Dynamic Encoder

Dynamic Encoder is a powerful JavaScript library designed to create, import, and execute custom encoders dynamically using Data URIs. It is built specifically to work with the compact-encoding library, allowing you to dynamically generate encoders that comply with compact-encoding standards. The library provides a flexible solution for encoding and decoding data with a focus on modularity, security, and runtime dependency injection.

Key Features

  • Dynamic Module Creation: Dynamically generates JavaScript encoder modules, converting them into Data URIs.
  • Focus on Compact-Encoding: Encoders created with the library follow the compact-encoding standard, ensuring efficient and compact data handling.
  • Integrity Verification: Built-in hash-based integrity checks ensure that dynamically loaded encoders haven’t been tampered with.
  • Dependency Injection: Automatically injects cenc, b4a, and custom dependencies into the dynamically created encoder modules.

Installation

npm install dynamic-encoder

Usage with Custom Encoders

Dynamic Encoder is built for custom encoders that comply with the compact-encoding standard. The library allows you to dynamically generate, load, and execute encoders while securely handling dependencies and ensuring data integrity.

Example 1: Creating and Loading a Custom Encoder

This example demonstrates how to create and load a custom JSON encoder that follows the compact-encoding interface. The encoder has access to cenc (from compact-encoding) and b4a (buffer-to-array).

import { create, load } from 'dynamic-encoder';

// Define a custom encoder
const customEncoder = {
    encode(state, value) {
        state.buffer = b4a.from(JSON.stringify(value));  // Encode as JSON
    },
    decode(state) {
        return JSON.parse(state.buffer.toString());  // Decode from JSON
    },
    preencode(state, value) {
        state.length = b4a.byteLength(JSON.stringify(value));  // Precompute buffer length
    }
};

// Create the encoder
const { uri, hash } = await create('jsonEncoder', customEncoder);

// Load the encoder module and ensure integrity
const encoder = await load(uri, hash);

// Now you can use the encoder to encode/decode data
const state = { buffer: null };
encoder.preencode(state, { foo: "bar" });
state.buffer = b4a.alloc(state.length);
encoder.encode(state, { foo: "bar" });

const decodedValue = encoder.decode(state);
console.log(decodedValue);  // Output: { foo: "bar" }

Example 2: Handling Dependencies in Custom Encoders

In this example, we create an encoder that relies on custom dependencies. The create function automatically injects cenc, b4a, and any additional dependencies that the encoder requires.

import { create, load } from 'dynamic-encoder';

// Define a custom encoder with dependencies
const customEncoder = {
    encode(state, value) {
        state.buffer = b4a.from(value);
    },
    decode(state) {
        return state.buffer.toString();
    },
    preencode(state, value) {
        state.length = b4a.byteLength(value);
    }
};

// Create the encoder with a dependency
const { uri, hash } = await create('stringEncoder', customEncoder, {
    customDep() {
        return "injectedValue";
    }
});

// Load the encoder and inject the dependency
const encoder = await load(uri, hash, {
    customDep() {
        return "injectedValue";
    }
});

console.log(encoder.customDep());  // Output: "injectedValue"

Compact-Encoder Support

All encoders created and loaded through Dynamic Encoder follow the compact-encoding standard. Encoders must provide the following methods:

  • encode: Responsible for encoding the value into the state buffer.
  • decode: Responsible for decoding the value from the state buffer.
  • preencode: Responsible for precomputing the buffer size needed for encoding.

API

create(encoderName, encoder, dependencies, hasher)

Generates a Data URI and a hash for the given encoder. It automatically injects cenc, b4a, and any custom dependencies passed.

  • encoderName (optional): The name of the encoder.
  • encoder: The encoder object with encode, decode, and optionally preencode methods.
  • dependencies (optional): An object of dependency functions to be injected into the encoder.
  • hasher (optional): A custom hash function for integrity verification.

load(dataUri, expectedHash, hasherOrDeps, dependencies)

Dynamically imports the encoder module and verifies its integrity based on the provided hash. It automatically injects cenc, b4a, and any custom dependencies passed.

  • dataUri: The Data URI of the dynamically created encoder module.
  • expectedHash: The hash of the encoder, used for integrity verification.
  • hasherOrDeps: Either a custom hash function or an object of dependencies.
  • dependencies: If hasherOrDeps is a custom hasher, this can be an object of dependencies to inject into the encoder.

Security

  • Hash-Based Integrity: All dynamically loaded modules are verified using a hash to ensure they haven’t been tampered with. This prevents unauthorized modifications.
  • Dependency Control: cenc and b4a are exposed automatically in the module, and only the additional dependencies you specify are injected into the encoder module, ensuring that no unwanted side effects or access to sensitive APIs occur.

License

MIT License