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

@console-one/segment

v0.1.1

Published

Multi-format composite path grammar — the substrate addressing primitive. One engine walks paths, streams, and cross-regime references via format-tagged transitions.

Downloads

258

Readme

@console-one/segment

Multi-format composite path grammar. The substrate addressing primitive.

What this is

A path is not a string. It's a walk across a multi-dimensional lattice where each dimension has its own syntactic rules. This package makes that walk a first-class citizen:

  • Segment grammar — declarative, composable, compile-time-verified path patterns built from from / after / upto / before / between / across / either / path / match / any. Same vocabulary as stream parsers.
  • PathFormat — a named delimiter convention (. for JSON, / for files, : for resources). Formats are data, serialisable, round-trippable.
  • Composite keys — strings like [format=resource][type=Workspace]acme[type=File][format=json]@.docs.overview that span multiple formats in one address. buildCompositeKey / parseCompositeKey round-trip.
  • Regime — a format plus the rules that apply inside it. Extensible payload: consumers attach whatever rules they need (parser grammars, admission laws, visibility gates, resolution policies). Cross-format transitions in a composite key dispatch regime changes at the boundary.
  • ScopeVisitor — a tree visitor with enter / inside / exit / outside lifecycle, walks a ResourceMapScope along declared dimensions, emitting MASK / ENFORCE / SHARD commands at each entry.

Why this exists

In a substrate where cells live at coordinates in a multi-dimensional lattice — structural path, temporal block, cross-sequence identity, partition, stage/version, type refinement — you need ONE addressing primitive. The alternative is a different addressing convention per dimension, which is what happens when you split your kernel by feature instead of by abstraction.

Segment collapses that. Every dimension is a PathFormat. Every rule set is a Regime. Crossing from one dimension to another is a format transition in a composite key — same syntax, different rules. New dimensions arrive as new formats, zero kernel changes.

Quick tour

Build and match a segment

import { $ } from '@console-one/segment'

const braced = $.between('{', '}').any().as('field').build()

braced.match('{hello}')
// => { path: 'hello',           // exclusive bounds: match span is inner only
//      query: WrappedSegment,
//      value: [...],
//      links: { field: [...] } } // named capture via .as('field')

braced.toJSON()
// => { type: 'Segment#Wrapped',
//      value: { config: { prefix: '{', suffix: '}', excludes: true },
//               inner: { type: 'Segment#Wildcard', value: {} },
//               name: 'field' } }

The builder's TypeScript type narrows with each method call. Calling .from() twice, or .build() without an inner segment, is a compile error.

Composite keys across formats

import {
  buildCompositeKey, parseCompositeKey,
  JSONFormat, ResourceFormat
} from '@console-one/segment'

const routes = [
  { type: 'Workspace', route: ['acme'] },
  { type: 'File',      route: ['docs', 'overview'], format: JSONFormat },
]
const key = buildCompositeKey(routes, ResourceFormat)
// '[format=resource][type=Workspace]acme[type=File][format=json]@.docs.overview'

const { routes: parsed } = parseCompositeKey(key)
// deep-equal to the input routes

Regime — same address, different rules

import {
  regime, RegimeRegistry, trace,
  JSONFormat, ResourceFormat,
  buildCompositeKey
} from '@console-one/segment'

type Rules = { policy: string }

const devRegistry = new RegimeRegistry()
  .register(regime(ResourceFormat, { policy: 'read-only' }))
  .register(regime(JSONFormat,     { policy: 'mutable' }))

const prodRegistry = new RegimeRegistry()
  .register(regime(ResourceFormat, { policy: 'admin-only' }))
  .register(regime(JSONFormat,     { policy: 'public' }))

const key = buildCompositeKey(
  [{ type: 'Workspace', route: ['acme'] },
   { type: 'File',      route: ['doc'], format: JSONFormat }],
  ResourceFormat
)

trace<Rules>(key, devRegistry).map(t => t.regime.rules.policy)
// => ['read-only', 'mutable']

trace<Rules>(key, prodRegistry).map(t => t.regime.rules.policy)
// => ['admin-only', 'public']

The same address, under two regime registrations, yields different rules. That is the substrate claim made testable: format tags are a real dispatch mechanism.

ScopeVisitor — walking a resource tree

import {
  ResourceMapScope, ScopeVisitor,
  ScopeUpdateCommandType, EmitCommand
} from '@console-one/segment'

class MaskAdminDims extends ScopeVisitor<void> {
  constructor() { super(() => {}) }
  enter(scope, diff, emit: EmitCommand) {
    if ('role' in scope && scope.role === 'admin') {
      emit({ command: ScopeUpdateCommandType.MASK, args: ['role', 'adminTools'] })
    }
  }
  inside(scope, emit) {}
  outside(scope, emit) {}
  exit(scope, diff, emit) {}
}

const map = new ResourceMapScope(
  { workspace: ['acme', 'globex'], role: ['admin', 'user'] },
  ['workspace', 'role']
)
const commands = map.start(new MaskAdminDims())
// two MASK commands, one per workspace's admin role entry

Mapping to substrate concerns

| Substrate concern | Segment primitive | |---|---| | Path addressing | Segment grammar tree | | Cross-sequence reference | Format transition to a peer-sequence regime | | Partition (state / proc / id / req / chan / proj) | One PathFormat per partition, each with its own Regime rules | | Stage / version addressing | StageFormat, VersionFormat; Cell.resolve(path) dispatches on format tag | | Admission laws | Regime.rules — a consumer attaches its admission shape | | Read-head masking | Regime.rules.visibility — consumer-defined | | Walking a scope | ScopeVisitor + ResourceMapScope.start(visitor) | | Multi-regime typed composite key | buildCompositeKey / parseCompositeKey |

None of these are hard-coded. The package ships the shape; consumers attach their semantics via the Rules generic on Regime.

Install

npm install @console-one/segment

Public surface

from '@console-one/segment'

// Segment grammar
Segment, PathSegment, WildcardSegment, GroupSegment, WrappedSegment
$                           // DSL entrypoint
SegmentKind, SegmentJSON, SegmentMatch, WrappedSegmentOptions

// Builder states (for type annotations)
BuildState, WrappedSegmentBuildState, PrefixOnlyState, SuffixOnlyState,
StateOnlyState, NeedsStateState, NeedsPrefixState, NeedsSuffixState,
TerminalState

// Path format
PathFormat, toPathFormat, toRootedPathFormat
JSONFormat, ResourceFormat, FileFormat, StageFormat, VersionFormat
defaultFormats
segmentFromString
buildCompositeKey, parseCompositeKey, Route
FormatRegistryLike

// Regime
Regime, regime, RegimeRegistry
walk, trace, WalkEvent, RegimeWalker, RegimeTrace

// Visitor
ScopeVisitor, ResourceMapScope, TracingVisitor
ScopeUpdateCommand, ScopeUpdateCommandType,
MaskCommand, EnforceCommand, ShardCommand, EmitCommand, ScopeDiff,
ShardAllocationStrategy, AllocationDiff, LifecycleEvent

Bugs fixed from the legacy pathformat

This package is a rehab of console-one-workspace/transpilationNation's server/src/core/pathformat/segment.ts plus the scope-visitor design from 20240802_parser_state's resources/scopedruleapplicator.ts. Four specific defects are fixed:

  1. WrappedSegment.toString() used to emit the boolean ${this.prefix !== undefined}. Now emits the actual prefix/suffix. (Case 6 in smoke test.)
  2. WrappedSegment.match() used to require BOTH prefix and suffix to be defined, so $.from('(') with no suffix could never match. Now each side is independently optional. (Case 4.)
  3. excludes arithmetic used to collapse 'suffix' to truthy when computing the prefix activation, inverting the exclusion semantics. Now two independent booleans per side. (Case 5.)
  4. Segment.fromString used to be a // TODO: Implement stub, blocking composite-key round-trips. Now implemented as segmentFromString for single-format paths and parseCompositeKey for multi-format composites. (Cases 10–11.)

The original file also shipped ambitious TypeScript template-literal type recursion (JoinWithDelimiter, CompositeKey, RKey, TypePath) that hit TS depth limits and had a double-[type=] emit bug. That machinery is not preserved here — the runtime composite-key round-trip works without it, and re-introducing it cleanly is its own project.

Smoke test

npm install
npm run build
npm run smoke

14 cases, each corresponding to one primitive or fix. Exits non-zero on any failure.

What this package does NOT do

  • Does not implement the concrete MASK / ENFORCE / SHARD semantics. The visitor infrastructure ships; downstream consumers attach the policy.
  • Does not integrate with @console-one/parser, @console-one/cell, @console-one/namespace, or @console-one/patchkit. Those consumers adopt Regime on their own timeline.
  • Does not support escaping of delimiter characters inside keys. Keys under a format must not contain that format's delimiter. Adding escape semantics is a v0.2 concern.
  • Does not revive the template-literal-type composite-key synthesis.

License

MIT