Dev Helpers

Developer journal

Node.js

Server-side JavaScript, streams, files, diagnostics and runtime behaviour.

← All categories

Node.js

Safely parse malformed input

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.

Capture positional arguments

Process arguments include runtime and entry-point information before user arguments. Slice off those positions before interpreting input.

Detect an unhandled event name

An event name can exist without listeners at the point of inspection. Listener checks are snapshots of registration state.

Detect ASCII-only bytes

An ASCII check operates on bytes. A string that looks simple after display normalization may still require multibyte encoding.

Parse a streamed JSON document

A streamed JSON document must be framed before parsing. A single JSON document and newline-delimited JSON have different boundaries.

Join path segments

Joining path segments handles separators across supported platforms. Decide whether the result is intended for a local filesystem or a web URL.

Collect a web stream with for-await

An async loop consumes a web stream chunk by chunk. Stream chunks need not align with logical messages or character boundaries.

Identify a Windows path separator

Windows paths have their own separator and parser. Node's explicit Windows path helpers let a Linux build process inspect them predictably.

Normalize dot segments

Normalizing dot segments simplifies the spelling of a path. It does not establish that the target exists or resolve symbolic links.

Resolve after a delay

A promise-based delay returns control while a timer waits. It establishes a minimum delay, not an exact execution timestamp.

Decode a response body

A response body is a consumable stream. Choose how to decode it before handing it to several readers.

Identify a built-in module

Built-in module detection distinguishes runtime modules from similarly named packages. That distinction matters when reporting dependencies.

Read context outside a scope

Async context is available inside the scope that established it. Code invoked outside that scope needs an explicit missing-context policy.

Recognize a PNG header

A file signature is more informative than an extension when identifying a candidate format. A matching prefix alone does not prove the whole file is valid.

Preserve a regular expression

Cloning a regular expression should preserve the source and flags your caller relies on. Stateful matching can require additional attention to position.

Check an error-first callback result

Error-first callbacks carry failure and success through separate arguments. Centralizing the error check makes the success path easier to inspect.

Allocate a filled buffer

Allocating a filled buffer makes each initial byte predictable. This matters when preparing binary fixtures or constructing headers.

Count parameter entries

The size of URLSearchParams counts entries, including repeated keys. It can differ from the number of distinct parameter names.

Read a file extension

The last extension alone cannot identify every compound file format. Treat extension parsing as a filename convention rather than content detection.

Read a diagnostic channel name

Named diagnostic channels separate event producers from subscribers. Consistent names let instrumentation attach without changing the business operation.

Resolve an absolute path

Resolving a relative path uses a base directory. A background job and a local shell may start from different working directories.

Recognize a regular expression

A regular expression may arrive from a different execution context. A runtime type check can be more reliable than inspecting a constructor name.

Read the first matching row

Reading one database row needs a defined ordering when more than one row matches. An absent match also deserves an explicit branch.

Format a diagnostic message

Formatted diagnostic messages combine fixed text and inspected values. Check how the formatter treats objects and unused arguments.

Extract a filename

The basename is the last named part of a path. Removing an extension is a separate operation with case-sensitive edge cases.

Select an ordinal suffix category

Ordinal categories vary by locale. Use the category as an input to a localized suffix table instead of attaching an English ending everywhere.

Read file metadata

File metadata describes the path at the time it was inspected. A later read can still fail if the file changes.