@thing-king/metaform

1.0.126 • Public • Published

@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 }
})();

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.1260latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.1260
1.0.1250
1.0.1240
1.0.1230
1.0.1220
1.0.1210
1.0.1200
1.0.1190
1.0.1170
1.0.1160
1.0.1150
1.0.1140
1.0.1130
1.0.1120
1.0.1110
1.0.1100
1.0.1090
1.0.1080
1.0.1070
1.0.1060
1.0.1050
1.0.1040
1.0.1030
1.0.1020
1.0.1010
1.0.1000
1.0.990
1.0.980
1.0.970
1.0.960
1.0.950
1.0.940
1.0.930
1.0.920
1.0.910
1.0.900
1.0.890
1.0.880
1.0.870
1.0.860
1.0.850
1.0.840
1.0.830
1.0.820
1.0.810
1.0.800
1.0.790
1.0.780
1.0.770
1.0.760
1.0.750
1.0.740
1.0.730
1.0.720
1.0.710
1.0.700
1.0.690
1.0.680
1.0.670
1.0.660
1.0.650
1.0.640
1.0.630
1.0.620
1.0.610
1.0.600
1.0.590
1.0.580
1.0.570
1.0.560
1.0.550
1.0.540
1.0.530
1.0.520
1.0.510
1.0.500
1.0.490
1.0.480
1.0.470
1.0.440
1.0.430
1.0.420
1.0.410
1.0.400
1.0.390
1.0.380
1.0.370
1.0.360
1.0.350
1.0.340
1.0.330
1.0.320
1.0.310
1.0.300
1.0.290
1.0.280
1.0.270
1.0.260
1.0.250
1.0.240
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.160
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.41
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @thing-king/metaform

Weekly Downloads

1

Version

1.0.126

License

ISC

Unpacked Size

128 MB

Total Files

104

Last publish

Collaborators

  • savant