Skip to content

Latest commit

 

History

History

standards

README.md

Note: JSR is frozen at 4.5.x. From 5.0.0, @eserstack/standards is published on npm under the same name (pnpm add @eserstack/standards); the JSR versions stay available but get no updates.

eserstack Foundation — eser/stack on GitHub Install: pnpm add @eserstack/standards

Cross-runtime standards and utilities for TypeScript applications. Provides common interfaces, type declarations, and utility functions for building portable applications.

🚀 Quick Start

import * as standards from "@eserstack/standards";

// Runtime abstraction on Node.js, with fallbacks for Workers and browsers
const { runtime } = standards.crossRuntime;
const content = await runtime.fs.readTextFile("config.json");
const joined = runtime.path.join("src", "utils.ts");

// String interpolation
const greeting = standards.strings.interpolate("Hello {name}!", {
  name: "World",
});

// Formatters
const size = standards.formatters.formatSize(1048576); // "1.00 MB"

// Date utilities
const relative = standards.datetimes.getRelativeTime(
  new Date(Date.now() - 3600000),
);
// "1 hour ago"

🛠 Features

  • Logging Standards: OpenTelemetry severity levels
  • Date/Time Utilities: Formatting, relative time, date comparisons
  • String Interpolation: Template-based string formatting
  • Value Formatters: Duration, size, number, and percentage formatting
  • Internationalization: Locale detection and RTL support
  • Runtime Abstraction: One runtime API on Node.js, with fallbacks for Workers and browsers
  • Route Matching: URL pattern matching utilities
  • Registry: Immutable, indexed data structures

Modules

Logging Standards

OpenTelemetry severity levels for consistent logging across applications.

import * as standards from "@eserstack/standards";

standards.logging.Severities.Trace; // 1  - Most fine-grained diagnostic
standards.logging.Severities.Debug; // 5  - Detailed troubleshooting
standards.logging.Severities.Info; // 9  - Normal operational messages
standards.logging.Severities.Notice; // 10 - Normal but significant
standards.logging.Severities.Warning; // 13 - Potential issues
standards.logging.Severities.Error; // 17 - Functionality-breaking
standards.logging.Severities.Critical; // 21 - Non-recoverable failures
standards.logging.Severities.Alert; // 22 - Immediate action required
standards.logging.Severities.Emergency; // 23 - System is unusable

standards.logging.SeverityNames[standards.logging.Severities.Error]; // "Error"

Higher severity numbers indicate more severe conditions (OpenTelemetry model).

Date/Time Utilities

Date formatting, relative time, and comparison functions. The comparison functions take an optional second argument, the reference time, which defaults to now.

import * as standards from "@eserstack/standards";

// Format dates
standards.datetimes.toISODate(new Date("2024-12-29T10:30:00Z")); // "2024-12-29"
standards.datetimes.toISODateTime(new Date("2024-12-29T10:30:00Z"));
// "2024-12-29T10:30:00.000Z"

// Relative time
standards.datetimes.getRelativeTime(new Date(Date.now() - 3600000)); // "1 hour ago"
standards.datetimes.getRelativeTime(new Date(Date.now() + 90000000)); // "in 1 day"

// Date comparisons
const yesterday = new Date(Date.now() - 86400000);
const tomorrow = new Date(Date.now() + 86400000);

standards.datetimes.isToday(new Date()); // true
standards.datetimes.isPast(yesterday); // true
standards.datetimes.isFuture(tomorrow); // true

String Interpolation

Template-based string formatting with placeholder replacement.

import * as standards from "@eserstack/standards";

// Simple interpolation
standards.strings.interpolate("Hello {name}!", { name: "World" });
// "Hello World!"

// Create reusable interpolator
const greet = standards.strings.createInterpolator("Welcome {user} to {app}!");
greet({ user: "Alice", app: "MyApp" }); // "Welcome Alice to MyApp!"
greet({ user: "Bob", app: "MyApp" }); // "Welcome Bob to MyApp!"

// Extract placeholders for validation
standards.strings.extractPlaceholders(
  "Hello {name}, you have {count} messages",
);
// ["name", "count"]

Value Formatters

Human-readable formatting for durations, sizes, numbers, and percentages.

import * as standards from "@eserstack/standards";

// Duration formatting
standards.formatters.formatDuration(500); // "500ms"
standards.formatters.formatDuration(1500); // "1.50s"
standards.formatters.formatDuration(65000); // "65.00s"

// File size formatting
standards.formatters.formatSize(500); // "500.00 B"
standards.formatters.formatSize(1536); // "1.50 KB"
standards.formatters.formatSize(1048576); // "1.00 MB"
standards.formatters.formatSize(1073741824); // "1.00 GB"

// Number formatting with thousands separators
standards.formatters.formatNumber(1000); // "1,000"
standards.formatters.formatNumber(1000000); // "1,000,000"

// Percentage formatting
standards.formatters.formatPercent(75.5); // "75.5%"
standards.formatters.formatPercent(0.75, 1, true); // "75.0%" (ratio mode)

Internationalization (i18n)

Locale detection, RTL handling, and i18n utilities.

import * as standards from "@eserstack/standards";

// Check if locale is supported
standards.i18n.isCommonLocale("en"); // true
standards.i18n.isCommonLocale("fr"); // true
standards.i18n.isCommonLocale("xx"); // false

// RTL support
standards.i18n.isRtlLocale("ar"); // true (Arabic)
standards.i18n.isRtlLocale("he"); // true (Hebrew)
standards.i18n.isRtlLocale("en"); // false

// Get text direction for CSS
standards.i18n.getTextDirection("ar"); // "rtl"
standards.i18n.getTextDirection("en"); // "ltr"

// Parse a locale tag
standards.i18n.parseLocale("en-US"); // { language: "en", region: "US" }
standards.i18n.getLanguageCode("pt-BR"); // "pt"

// Default locale
standards.i18n.DEFAULT_LOCALE; // "en"

standards.i18n.COMMON_LOCALES lists the supported locale codes.

Runtime Abstraction

One runtime API for Node.js, with limited fallbacks for Cloudflare Workers and browsers. Architecture: adapters/shared.ts (base layer) composed with the adapters node.ts, browser.ts and workerd.ts. The module imports them statically and has no top-level await, so require() loads it too. Client bundles import @eserstack/standards/cross-runtime/browser instead.

import * as standards from "@eserstack/standards";

const { runtime } = standards.crossRuntime;

// Auto-detected runtime singleton
runtime.name; // "node" | "workerd" | "browser" | "unknown"
runtime.version; // "26.13.1"

// Check capabilities before use
if (runtime.capabilities.fs) {
  const content = await runtime.fs.readTextFile("config.json");
}

if (runtime.capabilities.exec) {
  const output = await runtime.exec.exec("git", ["status"]);
}

// Environment variables
const apiKey = runtime.env.get("API_KEY");

// Process info
const cwd = runtime.process.cwd();
const args = runtime.process.args;
const isSea = runtime.process.isStandalone; // true in a single executable app

// Synchronous stat, for code that cannot await
if (runtime.capabilities.fsSync) {
  const info = runtime.fs.statSync("package.json");
}

// Create runtime with mocks for testing
const mockRuntime = await standards.crossRuntime.createRuntime({
  env: {
    get: () => "mock",
    set: () => {},
    delete: () => {},
    has: () => true,
    toObject: () => ({}),
  },
});

File operations throw typed errors: NotFoundError for a missing path, AlreadyExistsError for one that exists, and PermissionDeniedError (from EACCES or EPERM) for one the process may not access. Each carries the path; PermissionDeniedError keeps the runtime's error as cause.

import * as standards from "@eserstack/standards";

const { runtime, PermissionDeniedError } = standards.crossRuntime;

try {
  await runtime.fs.readTextFile("/etc/shadow");
} catch (error) {
  if (error instanceof PermissionDeniedError) {
    console.error(`no read access to ${error.path}`);
  }
}

File modes and live processes

runtime.fs.stat() and runtime.fs.lstat() return a FileInfo carrying mode: POSIX mode bits, or null where the platform does not report them. Windows has no execute bit, so callers must read null as "cannot tell from the mode", not as "not executable".

runtime.process.isAlive(pid) reports whether a process id is running. A pid owned by another user counts as alive; only a definite "no such process" is false. It exists because "is the process that wrote this claim still running" is not answerable from a pid alone, and trusting a recorded pid forever turns any interrupted run into permanently wrong state.

Both checks go through runtime, so code that needs them does not reach for a runtime-specific global.

A signal in the options of runtime.exec.spawn and spawnChild stops the process: an abort kills it, and the call resolves with the failed result rather than rejecting. runtime.fs.makeTempDir() creates the directory inside options.dir, or inside the system temp directory when there is none.

HTTP server, WebSockets and signals

runtime.http.serve answers HTTP requests with a fetch-style handler on Node; check runtime.capabilities.http first, since Workers and browsers have no server. It resolves once the server listens, binds to 127.0.0.1 unless you pass hostname, and stops when its signal aborts. A handler that throws sends the client a 500 response, and the error is printed to stderr.

The handler gets a second argument, info. info.remoteAddr is the { hostname, port } of the peer's socket, or null when the runtime cannot tell; it is the direct peer, so behind a proxy it is the proxy unless you turn on trustProxy. info.signal aborts when the connection goes away before the response has finished, and request.signal follows it. onListen in the options is called with the bound address, the chosen port when port is 0, before serve resolves.

Option Default Effect
hostname 127.0.0.1 The address to listen on
unixSocket none A socket path to listen on, instead of hostname and port
signal none Stops the server when aborted
shutdownTimeoutMs 10000 How long a stopping server waits for in-flight requests; 0 does not wait
headersTimeoutMs 60000 Time to receive the headers, then 408; 0 turns it off
requestTimeoutMs 300000 Time to receive the whole request, then 408; 0 turns it off
keepAliveTimeoutMs 5000 How long an idle connection stays open, plus 1 s; 0 keeps it open
trustProxy off The proxies whose forwarded headers count
onListen none Called with the bound address before serve resolves

The three timeouts keep Node's defaults when you leave them out. The headers and request timeouts limit how long the client takes to send, never the handler. headersTimeoutMs cannot be above requestTimeoutMs, and serve rejects a timeout that is not a whole number, 0 or more.

Stopping. When signal aborts, the server stops listening, closes idle connections and sends each WebSocket a 1001 close frame. In-flight requests go on to finish, and their responses carry connection: close. After shutdownTimeoutMs it closes every connection that is left, which aborts their info.signal. finished resolves once the last connection has closed. A stream that never ends, such as server-sent events, holds the server until that timeout, so stop such a stream yourself if you want a faster exit.

Unix socket. With unixSocket, hostname and port are ignored, server and onListen report unixSocket with the path, "" as the hostname and 0 as the port, and the server removes the file when it stops. The file's permissions decide who can connect, so put it in a directory only your proxy can reach. serve rejects with EADDRINUSE when the path exists, so a file a crashed process left behind has to be removed first. info.remoteAddr is null on a socket unless trustProxy gives a client address.

Behind a proxy. trustProxy is off by default, and a request's forwarded headers are then never read. Turn it on with the addresses and CIDRs of your proxies, and "unix:" for a peer on a unix socket. Only a request whose socket peer is on the list has its headers read:

  • X-Forwarded-For sets info.remoteAddr. The client is the right-most address that is not on the list, so entries a client forged to the left are never reached. Its port is 0, because a proxy does not report one.
  • X-Forwarded-Proto (http or https) sets the scheme of request.url, and X-Forwarded-Host its host and port. The last value of a list counts, and an invalid one is ignored.

The Forwarded header (RFC 7239) is not read. The headers stay readable on request.headers whatever the setting, so read client data from request.url and info. List only proxies that overwrite or append to these headers: one that passes them through unchanged hands the client control of them.

import * as standards from "@eserstack/standards";

const { runtime } = standards.crossRuntime;

await runtime.http.serve(
  {
    port: 0,
    unixSocket: "/run/app/http.sock",
    trustProxy: ["unix:"],
    shutdownTimeoutMs: 30_000,
    headersTimeoutMs: 10_000,
  },
  (request, info) =>
    new Response(
      `${request.url} for ${info.remoteAddr?.hostname ?? "unknown"}`,
    ),
);

Paths. request.url keeps the path the client sent, so GET //evil.com/x has the pathname //evil.com/x on the request's own host. Do not pass a pathname to new URL(pathname, base) or put it in a Location header unchecked: both read a leading // as another host. A target in absolute form (GET http://example.com/x) names its own host, which replaces Host, as RFC 9112 requires; neither is proof of who the server is, so compare them with the hosts you serve when that matters.

Inside a handler, runtime.http.upgradeWebSocket(request) accepts a WebSocket handshake. Add the socket's listeners, then return response from the handler.

The upgrade is matched by identity: the handler has to return the same Response object that upgradeWebSocket gave it, because a fetch Response cannot carry status 101. A wrapper around the handler (for security headers, say) must pass that object through. A copy, even one with the same status and headers, refuses the handshake with the copy's status, and the socket closes with 1006 without ever opening. The same goes for the request: pass upgradeWebSocket the Request the handler received, not a clone.

runtime.process.addSignalListener and removeSignalListener listen for OS signals such as SIGINT. Node accepts every name, and a listener for a signal the platform never sends never runs.

import * as standards from "@eserstack/standards";

const { runtime } = standards.crossRuntime;
const controller = new AbortController();

const server = await runtime.http.serve(
  {
    port: 8080,
    hostname: "127.0.0.1",
    signal: controller.signal,
    onListen: ({ hostname, port }) => console.log(`http://${hostname}:${port}`),
  },
  (request, info) => {
    if (request.headers.get("upgrade")?.toLowerCase() !== "websocket") {
      return new Response(`Hello, ${info.remoteAddr?.hostname ?? "stranger"}`);
    }

    // Echo each WebSocket message back to the client.
    const { socket, response } = runtime.http.upgradeWebSocket(request);
    socket.addEventListener("message", (event) => socket.send(event.data));

    return response;
  },
);

// Ctrl+C stops the server; `finished` resolves once it has stopped.
const onInterrupt = (): void => controller.abort();
runtime.process.addSignalListener("SIGINT", onInterrupt);

await server.finished;
runtime.process.removeSignalListener("SIGINT", onInterrupt);

The socket is a WebSocket the adapter provides, and binary messages arrive as an ArrayBuffer by default. The Node adapter builds on node:http, speaks the WebSocket protocol itself and refuses a client message over 16 MiB.

CLI Execution Context

Detect how the CLI was invoked and build reproducible commands for git hooks. The invoker is dev when the package.json in the current directory has a cli script, as in a workspace that runs pnpm cli, and binary inside a single executable application.

import * as standards from "@eserstack/standards";

const cliOptions = {
  command: "eser",
  devCommand: "pnpm cli",
  npmPackage: "eser",
};

// Detect full execution context
const ctx = await standards.crossRuntime.detectExecutionContext(cliOptions);
ctx.runtime; // "node" | "compiled"
ctx.invoker; // "npm" | "npx" | "pnpm" | "pnpx" | "dev" | "binary" | "unknown"
ctx.command; // "npx eser" (canonical, reproducible command)

// Find subcommand prefix for hooks/manifest
const prefix = await standards.crossRuntime.getCliPrefix(cliOptions, [
  "noskills",
  "nos",
]);
// "npx eser noskills"  (when invoked as: npx eser noskills init)
// "pnpm cli nos"       (when invoked as: pnpm cli nos init in the workspace)

// Pure function for testing (no runtime access)
standards.crossRuntime.matchCliPrefix(
  ["noskills", "nos"],
  ["npx", "eser", "noskills", "init"],
);
// "npx eser noskills"

Route Matching

URL pattern matching for routing, with dynamic segments ([param]) and catch-all segments ([...param]). Pass the pathname as the URL has it (URL.pathname, percent-encoded). Params come back decoded, and a segment with a malformed escape such as 100% keeps its raw form. A decoded value can hold / or .., so validate it before it goes into a file path or a query. A catch-all matches zero or more segments, its bare prefix included, so a path and its normalizePath() form reach the same route.

import * as standards from "@eserstack/standards";

standards.routes.matchRoute("/stories/hello", "/stories/[slug]");
// { slug: "hello" }

standards.routes.matchRoute("/docs/a/b/c", "/docs/[...path]");
// { path: ["a", "b", "c"] }

standards.routes.matchRoute("/stories/hello%20world", "/stories/[slug]");
// { slug: "hello world" }

standards.routes.matchRoute("/docs", "/docs/[...path]");
// { path: [] }

// The first matching route wins, so list the most specific first
const match = standards.routes.findMatchingRoute("/stories/hello", [
  { path: "/stories/featured", data: "featured" },
  { path: "/stories/[slug]", data: "story" },
]);
// { route: { path: "/stories/[slug]", data: "story" }, params: { slug: "hello" } }

standards.routes.normalizePath("stories/hello/"); // "/stories/hello"

Registry

Immutable, indexed data structures for efficient lookups. A builder collects entries, and build() seals them into a read-only registry.

import * as standards from "@eserstack/standards";

type User = { name: string };

// Simple registry
const registry = standards.collections
  .createRegistryBuilder<string, User>()
  .set("alice", { name: "Alice" })
  .setLazy("bob", () => ({ name: "Bob" })) // created on first read
  .build();

registry.get("alice"); // { name: "Alice" }
registry.isLazy("bob"); // true

// Copy-on-write: a new registry, the original stays unchanged
const extended = registry.toBuilder().set("carol", { name: "Carol" }).build();

// Indexed registry for lookups by a secondary key
type Format = { name: string; extensions: string[] };

const formats = standards.collections
  .createIndexedRegistryBuilder<string, Format>([
    standards.collections.createExtensionIndex<Format>(
      "byExtension",
      (f) => f.extensions,
    ),
  ])
  .set("yaml", { name: "yaml", extensions: ["yaml", "yml"] })
  .build();

formats.getByIndex("byExtension", "yml"); // the yaml format

Other Utilities

HTTP Errors

import * as standards from "@eserstack/standards";

const error = new standards.httpError.HttpError(404, "User not found");
error.status; // 404

new standards.httpError.HttpError(503).message; // "Service Unavailable"

// Factories for common statuses
standards.httpError.notFound("User not found"); // same as the HttpError above

standards.httpError.isHttpError(error); // true

Run Modes

RunModes holds bit flags; the helpers test a mode value against them.

import * as standards from "@eserstack/standards";

const mode = standards.runModes.RunModes.Development;

if (standards.runModes.inProductionMode(mode)) {
  // Production-only code
}

standards.runModes.inDevelopmentMode(mode); // true
standards.runModes.inTestMode(mode); // false

Versions

import * as standards from "@eserstack/standards";

// true when the target version is newer than the current one
standards.versions.compareTextVersions("4.1.22", "4.1.23"); // true
standards.versions.compareTextVersions("4.1.23", "4.1.22"); // false

// true when the current version satisfies the target range
standards.versions.compareTextVersions("4.1.22", "^4.1.0"); // true

// the larger of two release types
standards.versions.maxVersion("minor", "patch"); // "minor"

File Patterns

import * as standards from "@eserstack/standards";

standards.patterns.JS_FILE_PATTERN.test("app/page.tsx"); // true
standards.patterns.replaceJsExtension("app/counter.tsx", ".js"); // "app/counter.js"
standards.patterns.hasExtension("styles.css", ["css", "scss"]); // true

Upgrading from 4.x

These changes since 4.5.2 need a change in code, commands or files that use this package. The changelog lists the changes in every package.

  • routes.matchPattern, matchRoute and findMatchingRoute return params percent-decoded, and a catch-all also matches its bare prefix. Pass the pathname percent-encoded, as URL.pathname gives it, and remove your own decodeURIComponent, or values with % get decoded twice. List a route for /docs before the catch-all if /docs should reach another route:

    import * as standards from "@eserstack/standards";
    
    const params = standards.routes.matchRoute(
      "/stories/hello%20world",
      "/stories/[slug]",
    );
    // Before: decodeURIComponent(params.slug); After: params.slug is "hello world"
    standards.routes.matchRoute("/docs", "/docs/[...path]");
    // Before: null; After: { path: [] }
  • cross-runtime: Runtime has a required http, RuntimeProcess has addSignalListener and removeSignalListener, and RuntimeCapabilities has http: boolean. A hand-built runtime or mock no longer type-checks. Spread the real objects and override what you need, add http: false to capability literals, or build the mock with await createRuntime({ ... }):

    import * as standards from "@eserstack/standards";
    
    const { runtime } = standards.crossRuntime;
    
    // Before: const fakeProcess = { cwd: () => "/tmp", ... };
    const fakeProcess = { ...runtime.process, cwd: () => "/tmp" };

🔗 For further details, visit the eserstack repository.