@hyperfrontend/string-utils/node

Node Module

Node.js-optimized implementations of the @hyperfrontend/string-utils encoding API, built directly on Buffer.

Overview

This entry point exposes the same UTF-8 and base64 helpers as the browser submodule, but defers all encoding work to Buffer.from(...) and buffer.toString(...). Routing through Buffer avoids the binary-string round-trips that browsers need (TextEncoder → btoa → atob → TextDecoder) and produces fewer intermediate allocations on the server.

URL-safe base64 transformation is shared with the browser submodule and uses loop-based character substitution rather than regex, keeping behavior linear in input length.

Usage

import { toBase64, fromBase64, utf8StringToUint8Array, base64ToUint8Array } from '@hyperfrontend/string-utils/node'

// UTF-8 safe base64
toBase64('こんにちは') // => '44GT44KT44Gr44Gh44Gv'

// URL-safe base64 without padding (suitable for JWT-style payloads)
toBase64('{"userId":123}', true, false) // => 'eyJ1c2VySWQiOjEyM30'

// Round-trip through bytes for crypto workflows
const bytes = utf8StringToUint8Array('Hello')
const sameBytes = base64ToUint8Array(toBase64('Hello'))

Use this entry point in Node.js processes (and any runtime that ships a Buffer global). Browser, Web Worker, and edge-runtime consumers should import from @hyperfrontend/string-utils/browser so bundlers can drop the Buffer paths entirely.

API Reference§

ƒ Functions

§function

base64ToUint8Array(base64: string): Uint8Array

Converts a base64 encoded string to a Uint8Array (Node.js implementation). Supports both standard and URL-safe base64 encoding.
Parameters
NameTypeDescription
§base64
stringThe base64 encoded string to convert
Returns
Uint8Array
The decoded Uint8Array
Examples

Standard base64

const bytes = base64ToUint8Array('SGVsbG8=')
// => Uint8Array([72, 101, 108, 108, 111]) // 'Hello'

URL-safe base64 (without padding)

const bytes = base64ToUint8Array('SGVsbG8')
// => Uint8Array([72, 101, 108, 108, 111]) // 'Hello'
§function

fromBase64(text: string): string

Decodes a base64 encoded string to a UTF-8 string (Node.js implementation). Supports both standard and URL-safe base64 encoding.
Parameters
NameTypeDescription
§text
stringThe base64 encoded string to decode
Returns
string
The decoded UTF-8 string
Examples

Standard base64

const message = fromBase64('SGVsbG8sIFdvcmxkIQ==')
// => 'Hello, World!'

URL-safe base64

const token = fromBase64('eyJ1c2VySWQiOjEyM30')
// => '{"userId":123}'
§function

toBase64(text: string, urlSafe: boolean, keepPadding: boolean): string

Encodes a UTF-8 string to base64 format (Node.js implementation). Supports optional URL-safe encoding and padding control.
Parameters
NameTypeDescription
§text
stringThe UTF-8 string to encode
§urlSafe
booleanWhether to use URL-safe base64 encoding (replaces + and / with - and _)(default: false)
§keepPadding
booleanWhether to keep padding characters (=) in the output(default: false)
Returns
string
The base64 encoded string
Examples

Standard base64

const encoded = toBase64('Hello, World!')
// => 'SGVsbG8sIFdvcmxkIQ=='

URL-safe without padding (for URLs/tokens)

const token = toBase64('{"userId":123}', true, false)
// => 'eyJ1c2VySWQiOjEyM30'
§function

uint8ArrayToBase64(bytes: Uint8Array, urlSafe: boolean, keepPadding: boolean): string

Converts a Uint8Array to a base64 encoded string (Node.js implementation). Supports optional URL-safe encoding and padding control.
Parameters
NameTypeDescription
§bytes
Uint8ArrayThe Uint8Array to encode
§urlSafe
booleanWhether to use URL-safe base64 encoding (replaces + and / with - and _)(default: false)
§keepPadding
booleanWhether to keep padding characters (=) in the output(default: false)
Returns
string
The base64 encoded string
Examples

Standard base64

const bytes = new Uint8Array([72, 101, 108, 108, 111])
const encoded = uint8ArrayToBase64(bytes)
// => 'SGVsbG8='

URL-safe without padding

const bytes = new Uint8Array([72, 101, 108, 108, 111])
const token = uint8ArrayToBase64(bytes, true, false)
// => 'SGVsbG8'
§function

utf8StringToUint8Array(text: string): Uint8Array

Converts a UTF-8 string to a Uint8Array (Node.js implementation).
Parameters
NameTypeDescription
§text
stringThe UTF-8 string to convert
Returns
Uint8Array
The encoded Uint8Array
Example

Encoding string to bytes (Node.js)

const bytes = utf8StringToUint8Array('Hello')
// => Uint8Array([72, 101, 108, 108, 111])
§function

arrayBufferToUtf8String(uint8Array: ArrayBuffer): string

Converts an ArrayBuffer to a UTF-8 encoded string.
Parameters
NameTypeDescription
§uint8Array
ArrayBufferThe ArrayBuffer to convert
Returns
string
The decoded UTF-8 string
Example

Converting ArrayBuffer to string

const encoder = new TextEncoder()
const buffer = encoder.encode('Hello, World!').buffer
const decoded = arrayBufferToUtf8String(buffer)
// => 'Hello, World!'
§function

uint8ArrayToUtf8String(uint8Array: Uint8Array): string

Converts a Uint8Array to a UTF-8 encoded string.
Parameters
NameTypeDescription
§uint8Array
Uint8ArrayThe Uint8Array to convert
Returns
string
The decoded UTF-8 string
Example

Converting Uint8Array to string

const bytes = new Uint8Array([72, 101, 108, 108, 111])
const text = uint8ArrayToUtf8String(bytes)
// => 'Hello'