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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@resourcexjs/type

v2.5.5

Published

ResourceX Type System - Type handlers and serialization

Readme

@resourcexjs/type

Type system for ResourceX with sandbox-compatible execution.

Installation

bun add @resourcexjs/type

Overview

The @resourcexjs/type package provides the type system for ResourceX, managing how different resource types are resolved and executed in sandboxed environments.

Key Concepts

  • BundledType: Pre-bundled resource type ready for sandbox execution
  • TypeHandlerChain: Type registry managing type lookup and registration
  • ResolveContext: Serializable context passed to resolvers in sandbox
  • Builtin Types: Text, JSON, and Binary types are included by default

Usage

Using TypeHandlerChain

import { TypeHandlerChain } from "@resourcexjs/type";

// Create a new chain (builtin types included)
const chain = TypeHandlerChain.create();

// Check if type is supported
chain.canHandle("text"); // true
chain.canHandle("json"); // true
chain.canHandle("binary"); // true

// Builtin aliases
chain.canHandle("txt"); // true (alias for text)
chain.canHandle("config"); // true (alias for json)
chain.canHandle("bin"); // true (alias for binary)

// Get handler
const handler = chain.getHandler("text");
console.log(handler.name); // "text"
console.log(handler.code); // bundled resolver code

// Get all supported types
const types = chain.getSupportedTypes();
// ["text", "txt", "plaintext", "json", "config", "manifest", "binary", "bin", "blob", "raw"]

Registering Custom Types

import { TypeHandlerChain, bundleResourceType } from "@resourcexjs/type";

// Bundle a resource type from source file
const promptType = await bundleResourceType("./prompt.type.ts");

// Register with chain
const chain = TypeHandlerChain.create();
chain.register(promptType);

// Now the chain can handle the new type
chain.canHandle("prompt"); // true

Bundling Resource Types

Resource types must be bundled before registration. The bundler converts a .type.ts source file into a BundledType with executable code:

import { bundleResourceType } from "@resourcexjs/type";

// Bundle from source file
const myType = await bundleResourceType("./my-resource.type.ts");
// Returns: { name, aliases, description, schema, code }

Source file format (my-resource.type.ts):

export default {
  name: "prompt",
  aliases: ["deepractice-prompt"],
  description: "AI Prompt template",

  async resolve(ctx) {
    // ctx.manifest - resource metadata
    // ctx.files - extracted files as Record<string, Uint8Array>
    const content = new TextDecoder().decode(ctx.files["content"]);
    return content;
  },
};

Accessing Builtin Types Directly

import { textType, jsonType, binaryType, builtinTypes } from "@resourcexjs/type";

// Individual types
console.log(textType.name); // "text"
console.log(jsonType.aliases); // ["config", "manifest"]
console.log(binaryType.description); // "Binary content"

// All builtin types as array
for (const type of builtinTypes) {
  console.log(type.name);
}

API Reference

TypeHandlerChain

Type registry managing type lookup and registration.

Static Methods

TypeHandlerChain.create(): TypeHandlerChain

Create a new TypeHandlerChain instance with builtin types.

const chain = TypeHandlerChain.create();

Instance Methods

register(type: BundledType): void

Register a custom type.

Throws: ResourceTypeError if type name or alias already exists.

chain.register(promptType);
canHandle(typeName: string): boolean

Check if a type is supported.

chain.canHandle("text"); // true
chain.canHandle("unknown"); // false
getHandler(typeName: string): BundledType

Get handler for a type.

Throws: ResourceTypeError if type not supported.

const handler = chain.getHandler("text");
getHandlerOrUndefined(typeName: string): BundledType | undefined

Get handler or undefined if not found.

const handler = chain.getHandlerOrUndefined("unknown"); // undefined
getSupportedTypes(): string[]

Get all supported type names (including aliases).

const types = chain.getSupportedTypes();
clear(): void

Clear all registered types (for testing).

chain.clear();

bundleResourceType(sourcePath, basePath?)

Bundle a resource type from a source file.

Parameters:

  • sourcePath: string - Path to the .type.ts file
  • basePath?: string - Base path for resolving relative paths (defaults to cwd)

Returns: Promise<BundledType>

const myType = await bundleResourceType("./my-resource.type.ts");

Types

BundledType

Pre-bundled resource type ready for execution:

interface BundledType {
  name: string; // Type name (e.g., "text", "json")
  aliases?: string[]; // Alternative names
  description: string; // Human-readable description
  schema?: JSONSchema; // JSON Schema for resolver arguments
  code: string; // Bundled resolver code
}

ResolveContext

Context passed to resolver in sandbox:

interface ResolveContext {
  manifest: {
    domain: string;
    path?: string;
    name: string;
    type: string;
    version: string;
  };
  files: Record<string, Uint8Array>;
}

ResolvedResource

Result object returned after resolution:

interface ResolvedResource<TArgs = void, TResult = unknown> {
  resource: unknown; // Original RXR object
  execute: (args?: TArgs) => TResult | Promise<TResult>;
  schema: TArgs extends void ? undefined : JSONSchema;
}

ResourceType

Interface for defining custom types (before bundling):

interface ResourceType<TArgs = void, TResult = unknown> {
  name: string;
  aliases?: string[];
  description: string;
  resolver: ResourceResolver<TArgs, TResult>;
}

IsolatorType

Sandbox isolation levels (configured at Registry level):

type IsolatorType = "none" | "srt" | "cloudflare" | "e2b";
  • "none": No isolation, fastest (~10ms), for development
  • "srt": OS-level isolation (~50ms), secure local dev
  • "cloudflare": Container isolation (~100ms), local Docker or edge
  • "e2b": MicroVM isolation (~150ms), production (planned)

Builtin Types

Three builtin types are included by default:

| Type | Aliases | Resolves to | Description | | -------- | -------------------- | ------------ | ------------------ | | text | txt, plaintext | string | Plain text content | | json | config, manifest | unknown | JSON content | | binary | bin, blob, raw | Uint8Array | Binary content |

Creating Custom Types

Example: Simple Type (No Arguments)

prompt.type.ts:

export default {
  name: "prompt",
  aliases: ["deepractice-prompt"],
  description: "AI Prompt template",

  async resolve(ctx) {
    const content = new TextDecoder().decode(ctx.files["content"]);
    return content;
  },
};

Example: Type with Schema

tool.type.ts:

export default {
  name: "tool",
  description: "Executable tool with arguments",
  schema: {
    type: "object",
    properties: {
      query: { type: "string", description: "Search keyword" },
      limit: { type: "number", description: "Max results", default: 10 },
    },
    required: ["query"],
  },

  async resolve(ctx) {
    // The schema is used by the executor to validate/render UI
    // The resolver returns the tool definition
    const code = new TextDecoder().decode(ctx.files["content"]);
    return { code, manifest: ctx.manifest };
  },
};

Error Handling

import { ResourceTypeError } from "@resourcexjs/type";

try {
  chain.getHandler("unknown");
} catch (error) {
  if (error instanceof ResourceTypeError) {
    console.error("Type error:", error.message);
    // "Unsupported resource type: unknown"
  }
}

Exports

// Types
export type {
  ResourceType,
  ResourceResolver,
  ResolvedResource,
  ResolveContext,
  JSONSchema,
  JSONSchemaProperty,
  BundledType,
  IsolatorType,
} from "@resourcexjs/type";

// Classes and Functions
export { TypeHandlerChain } from "@resourcexjs/type";
export { bundleResourceType } from "@resourcexjs/type";
export { ResourceTypeError } from "@resourcexjs/type";

// Builtin Types
export { textType, jsonType, binaryType, builtinTypes } from "@resourcexjs/type";

License

Apache-2.0