1.0.126 • Published 11 months ago

@thing-king/metaform v1.0.126

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

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

11 months ago

1.0.125

11 months ago

1.0.124

11 months ago

1.0.123

11 months ago

1.0.122

11 months ago

1.0.121

11 months ago

1.0.120

11 months ago

1.0.119

11 months ago

1.0.117

11 months ago

1.0.116

11 months ago

1.0.115

11 months ago

1.0.114

11 months ago

1.0.113

11 months ago

1.0.112

11 months ago

1.0.111

11 months ago

1.0.110

11 months ago

1.0.109

11 months ago

1.0.108

11 months ago

1.0.107

11 months ago

1.0.106

11 months ago

1.0.105

11 months ago

1.0.104

11 months ago

1.0.103

11 months ago

1.0.102

11 months ago

1.0.101

11 months ago

1.0.100

12 months ago

1.0.99

12 months ago

1.0.98

12 months ago

1.0.97

12 months ago

1.0.96

12 months ago

1.0.95

12 months ago

1.0.94

12 months ago

1.0.93

12 months ago

1.0.92

12 months ago

1.0.91

12 months ago

1.0.90

12 months ago

1.0.89

12 months ago

1.0.88

12 months ago

1.0.87

12 months ago

1.0.86

12 months ago

1.0.85

12 months ago

1.0.84

12 months ago

1.0.83

12 months ago

1.0.82

12 months ago

1.0.81

12 months ago

1.0.80

12 months ago

1.0.79

12 months ago

1.0.78

12 months ago

1.0.77

12 months ago

1.0.76

12 months ago

1.0.75

12 months ago

1.0.74

12 months ago

1.0.73

12 months ago

1.0.72

12 months ago

1.0.71

12 months ago

1.0.70

12 months ago

1.0.69

12 months ago

1.0.68

12 months ago

1.0.67

12 months ago

1.0.66

12 months ago

1.0.65

12 months ago

1.0.64

12 months ago

1.0.63

12 months ago

1.0.62

12 months ago

1.0.61

12 months ago

1.0.60

12 months ago

1.0.59

12 months ago

1.0.58

12 months ago

1.0.57

12 months ago

1.0.56

12 months ago

1.0.55

12 months ago

1.0.54

12 months ago

1.0.53

12 months ago

1.0.52

12 months ago

1.0.51

12 months ago

1.0.50

12 months ago

1.0.49

12 months ago

1.0.48

12 months ago

1.0.47

12 months ago

1.0.44

12 months ago

1.0.43

12 months ago

1.0.42

12 months ago

1.0.41

12 months ago

1.0.40

12 months ago

1.0.39

12 months ago

1.0.38

12 months ago

1.0.37

12 months ago

1.0.36

12 months ago

1.0.35

12 months ago

1.0.34

12 months ago

1.0.33

12 months ago

1.0.32

12 months ago

1.0.31

12 months ago

1.0.30

12 months ago

1.0.29

12 months ago

1.0.28

12 months ago

1.0.27

12 months ago

1.0.26

12 months ago

1.0.25

12 months ago

1.0.24

12 months ago

1.0.23

12 months ago

1.0.22

12 months ago

1.0.21

12 months ago

1.0.20

12 months ago

1.0.19

12 months ago

1.0.18

12 months ago

1.0.16

12 months ago

1.0.15

12 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.12

12 months ago

1.0.11

12 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago