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

@thing-king/metaform

v1.0.126

Published

**Metaform** is a utility for executing functions _(things)_ with strict input and output type enforcement. Functions _(things)_ require definition of input and output types in a metadata object, when executed the input and output types are casted and val

Downloads

4,971

Readme

@thing-king/metaform

Overview

@thing-king/metaform is a modular framework for defining, managing, and executing data processing units known as "Things." A Thing can either function as a handler, processing inputs and generating outputs, or serve as a type definition for validating and casting data. The framework is designed for flexibility and scalability, making it ideal for use in server-side applications, WebSocket-based client-server architectures, and dynamic environments.

Installation

Install the package via npm:

npm install @thing-king/metaform

ThingContext

The ThingContext is the core environment where Things are managed and executed. It acts as a collection of Things, handling tasks such as validation, execution, and data type casting. When you load a collection of Things into a ThingContext, you can execute any Thing within it, cast data types, and ensure that all operations conform to the defined metadata.

Example:

import { ThingLoader, ThingContext } from "@thing-king/metaform";

(async () => {
    const collection = await ThingLoader.loadCollection("./things");
    const result = await collection.execute("ExampleThing", {
        input1: "test",
        input2: 15,
    });
    console.log(result); // { result: true }
})();

Defining a Thing

A Thing is defined by exporting an object containing its metadata, inputs, outputs, and an optional handler function. Things can either be data handlers or custom type definitions.

1. Handlers

If a Thing is a handler, it processes inputs and generates outputs. The handler function is where the logic resides, and it operates based on the metadata-defined inputs and outputs.

2. Type Definitions

If a Thing is a custom data type, set type = true in the export. The handler function will then determine if the input can be cast to this type. It should return either true/false for a binary decision or a value between 0 and 1 representing the degree of similarity or certainty.

3. Input and Output Metadata

  • Input Metadata: Inputs can be defined using in, ins, or inputs. Use in for a single input, and ins or inputs for multiple inputs. The metadata should specify the type of each input.

  • Output Metadata: Outputs can be defined using out, outs, or outputs. Similar to inputs, out is used for a single output, while outs or outputs are for multiple outputs.

  • Default Behavior: If no specific input or output is provided, the framework assumes in as the default input and out as the default output.

Example:

Defining a Thing as a handler:

export default {
    name: "ExampleHandler",
    in: { type: "string" },
    out: { type: "boolean" },
    handler: async function (inputs) {
        return { out: inputs.in.length > 5 };
    },
};

Defining a Thing as a type:

export default {
    name: "ExampleType",
    type: true,
    in: { type: "any" },
    handler: async function (inputs) {
        return inputs.in instanceof Array ? 1 : 0; // Returns 1 if it's an array, otherwise 0.
    },
};

WebSocket Integration

@thing-king/metaform includes support for WebSocket-based communication, enabling distributed processing of Things across different environments.

Hosting a ThingContext via WebSocket

Create a WebSocket server to host a ThingContext, allowing clients to connect and execute Things remotely.

import { WebSocketTransport, ThingLoader } from "@thing-king/metaform";

(async () => {
    const collection = await ThingLoader.loadCollection("./things");
    const host = new WebSocketTransport.Host(collection, 8080);
    await host.start();
    console.log("WebSocket server is running on ws://localhost:8080");
})();

Connecting a WebSocket Client

A client can connect to the WebSocket server to send data and receive processed results:

import { WebSocketTransport } from "@thing-king/metaform";

(async () => {
    const client = new WebSocketTransport.Client("ws://localhost:8080");
    await client.start();
    const result = await client.send("ExampleHandler", { in: "teststring" });
    console.log(result); // { out: true }
})();