Skip to content

Promise API (userid/promises) for 2.1 - #11

Merged
cinderblock merged 7 commits into
masterfrom
async-api
Nov 12, 2026
Merged

cinderblock merged 7 commits into
masterfrom
async-api

Conversation

@cinderblock

@cinderblock cinderblock commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Adds a Promise-based API for the next minor release, 2.1.0. Every lookup today is synchronous, so a slow user directory (LDAP, SSSD, NIS) blocks the event loop. The promise versions run on the libuv thread pool.

import { ids, gids, username } from 'userid/promises';
const { promises } = require('userid'); // the same functions
await ids('root'); // { uid: 0, gid: 0 }

Design

  • Shape: userid/promises plus require('userid').promises, like fs/promises and fs.promises. Same six functions and isSupported. No *Async functions.
  • Native: the lookups are plain C++ returning Result<T> (value or Failure) and never touch JavaScript. The sync binding throws on a Failure. The async binding validates the argument on the main thread, then runs the same lookup in a Napi::AsyncWorker that settles a promise. One function builds the error for both, so results and errors are identical.
  • Errors: identical to sync, but always a rejection: bad arguments (TypeError/RangeError), not found (ERR_USERID_NOT_FOUND), system errors (code/errno/syscall), and Windows' ERR_USERID_UNSUPPORTED_PLATFORM. Rejections carry the caller's stack.
  • No cancellation: libc lookups can't be interrupted, so an AbortSignal could only abandon the promise while the pool thread stays busy. Not offered; Promise.race makes that tradeoff explicit for callers who want it.
  • Worker threads: terminating a worker with lookups in flight used to abort the process (node-addon-api's rethrow in a dying environment). Fixed with NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS, and tested.

Tests

  • Every promise function against its sync twin on every kind of input (found, not found, each validation failure, big ids, long names), ESM and CJS.
  • 1000 lookups in flight; worker threads, including terminating one mid-lookup; async-hooks resource userid:<name> proves the thread pool path.
  • test/event-loop.test.mts: on Linux CI, LD_PRELOADs a getpwnam_r that takes 300 ms. The sync API must block the event loop; the promise API must not; 4 parallel lookups must take about 300 ms in total.
  • Smoke test: userid/promises resolves with npm, pnpm and Bun, and works in the Bun runtime.

Status

Not to be merged, tagged or published without explicit OK.

🤖 Generated with Claude Code

cinderblock and others added 6 commits November 10, 2026 18:32
Every lookup was synchronous, so a slow user directory (LDAP, SSSD,
NIS) blocked the event loop. The same six functions now also come as
promises, like fs/promises and fs.promises, and run on the libuv
thread pool.

Native: the lookups are plain C++ returning Result<T> (value or
Failure) and never touch JavaScript. Each is exported twice: sync, and
*Async, which validates the argument on the main thread and runs the
same lookup in a Napi::AsyncWorker that settles a promise. One function
turns a Failure into an error for both, so results and errors are
identical. The async-hooks resource is userid:<name>.

NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS: terminating a worker thread
with lookups in flight otherwise aborted the process, because the dying
environment can't settle the promise and node-addon-api's rethrow
escaped as a C++ exception.

JS: loading and error decoration move to lib/native.cts, shared by
lib/index.cts (sync) and lib/promises.cts. Every error, including a bad
argument or Windows' ERR_USERID_UNSUPPORTED_PLATFORM, is a rejection,
never a synchronous throw. Rejections get the caller's stack.

Tests: every promise function against its sync twin on every kind of
input; 1000 lookups in flight; worker threads, including terminating
one mid-lookup; the thread-pool resource. test/event-loop.test.mts
LD_PRELOADs a 300 ms getpwnam_r (CI, Linux) to prove the sync API
blocks the loop and the promise API doesn't. The smoke test resolves
userid/promises with each package manager and in the Bun runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cinderblock
cinderblock marked this pull request as ready for review November 12, 2026 04:20
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cinderblock
cinderblock merged commit 61f7c36 into master Nov 12, 2026
84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant