---
title: "Utilities"
description: "Small, stable helpers under devframe/utils/*, bundled in, no npm install."
canonical_url: "https://devfra.me/references/utilities"
---
# Utilities

> Small, stable helpers under devframe/utils/\*, bundled in, no npm install.

Small, stable helpers under `devframe/utils/*`, bundled in, no `npm install`.

## Reference

### `devframe/utils/colors`

Terminal ANSI colors.

```ts
import { colors as c } from 'devframe/utils/colors'

console.log(c.green('Server ready'))
console.log(c.cyan`listening on port ${port}`)
console.log(`${c.bold(c.red('fatal:'))} something went wrong`)
```

Exports `colors` (`blue`, `cyan`, `gray`, `green`, `red`, `yellow`, `bold`, `dim`, `reset`, `underline`).

### `devframe/utils/open`

Open a URL, file, or target in the OS handler.

```ts
import { open } from 'devframe/utils/open'

await open('https://localhost:7777')
await open('./report.html', { wait: true })
```

### `devframe/utils/launch-editor`

Open a file in the editor. Target accepts `file`, `file:line`, or `file:line:column`; an editor command (e.g. `'code'`) overrides auto-detection.

```ts
import { launchEditor } from 'devframe/utils/launch-editor'

launchEditor('src/main.ts:42:7')
launchEditor('src/main.ts:42:7', 'code')
```

Auto-detection reads `LAUNCH_EDITOR`, else defaults; most reach it through the [`open` service](https://devfra.me/add-ons/services/open).

### `devframe/utils/hash`

Deterministic hash of any structured-cloneable value.

```ts
import { hash } from 'devframe/utils/hash'

const key = hash({ functionName, args })
```

### `devframe/utils/structured-clone`

JSON-safe structured-clone serialization; round-trips `Map`, `Set`, `Date`, `BigInt`, cycles, class instances.

```ts
import {
  structuredCloneDeserialize,
  structuredCloneParse,
  structuredCloneSerialize,
  structuredCloneStringify,
} from 'devframe/utils/structured-clone'

const wire = structuredCloneStringify(new Map([
  ['a', 1]
]))
const value = structuredCloneParse<Map<string, number>>(wire)
```

### `devframe/utils/nanoid`

URL-safe random ID generator (vendored, zero-dep).

```ts
import { nanoid } from 'devframe/utils/nanoid'

nanoid() // 21 chars
nanoid(10) // 10 chars
```

### `devframe/utils/crypto-token`

Cryptographically-secure token helpers on WebCrypto (browser + Node).

```ts
import { randomDigits, randomToken, timingSafeEqual } from 'devframe/utils/crypto-token'

randomToken() // 32-char hex, 128 bits of entropy; use as a bearer token
randomDigits(6) // '047204', uniform, leading zeros preserved
timingSafeEqual(input, secret) // constant-time string comparison
```

### `devframe/utils/debounce`

Promise-aware debounce, bundled in so integrations reuse it without adding a `perfect-debounce` dependency.

```ts
import { debounce } from 'devframe/utils/debounce'

const save = debounce((state: State) => write(state), 100)
save(next) // coalesces bursts; returns a promise, plus `.cancel()` / `.flush()` / `.isPending()`
```

### `devframe/utils/events`

Typed event emitter.

```ts
import { createEventEmitter } from 'devframe/utils/events'

const events = createEventEmitter<{ change: (n: number) => void }>()
const off = events.on('change', n => console.log(n))
events.emit('change', 42)
off()
```

### `devframe/utils/shared-state`

The immutable state container behind `ctx.rpc.sharedState` ([Shared State](https://devfra.me/guide/shared-state)); usable outside a running devframe.

```ts
import { createSharedState } from 'devframe/utils/shared-state'

const state = createSharedState({
  initialValue: { count: 0 }
})
state.mutate((draft) => {
  draft.count += 1
})
state.value() // => count is 1
```

### `devframe/utils/url`

URL path-joining and slash-normalization helpers (browser-safe, zero-dep).

```ts
import { joinURL, withBase, withProtocol, withTrailingSlash } from 'devframe/utils/url'

joinURL('/__devframe', '__renderers', 'vue.mjs') // '/__devframe/__renderers/vue.mjs'
withBase('meta.json', '/__devframe/') // '/__devframe/meta.json'
withProtocol('http://localhost:7777/ws', 'ws://') // 'ws://localhost:7777/ws'
```

Also exports `withLeadingSlash`, `withoutLeadingSlash`, `withoutTrailingSlash`, and `cleanDoubleSlashes`.

### `devframe/utils/streaming-channel`

Sink/reader primitives for streamed RPC payloads, via `ctx.rpc.streaming`; see [Streaming](https://devfra.me/guide/streaming).

### `devframe/utils/when`

Statically-validated when-clause expressions for conditional visibility; runtime + types ship here, consumer `when` fields are kit-side (see [When Clauses](https://devfra.me/references/when-clauses)).

## Why a `utils/*` subpath

The utilities are **stable wrappers**, not bare re-exports, so devframe can swap implementations freely.


## Sitemap

See the full [sitemap](https://devfra.me/sitemap.md) for all pages.
