lib0 Isomorphic Utility Functions
lib0 is a monorepo offering a comprehensive collection of isomorphic utility functions designed for both Node.js and browser environments. These utilities cover fundamental programming needs such as array manipulation, efficient binary encoding/decoding, cryptographic operations, assertions, and more. The package is currently at version `0.2.117` on npm, representing its stable release line. However, an active development branch is progressing towards `v1.0.0`, with frequent release candidates (e.g., `v1.0.0-rc.12`) introducing significant architectural changes, particularly within its `delta` and `schema` modules. It supports both CommonJS and ESM module formats. Its primary differentiator lies in providing a robust, performance-optimized, and isomorphic toolkit, minimizing the need for environment-specific code paths and enabling effective dead code elimination by bundlers. The project appears to have an active development cadence, pushing out release candidates regularly.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported. Instead change the require of ... to a dynamic import() or an ESM import statement.
cause Attempting to use `require()` to import a `lib0` subpath module in an environment or bundler setup that expects ESM, or when `lib0` itself resolves to its ESM entrypoint for that subpath.fixChange your import statement from `const { someFunc } = require('lib0/module');` to `import { someFunc } from 'lib0/module';`. Ensure your project's `tsconfig.json` (if using TypeScript) and bundler are configured for ESM output where appropriate. -
TypeError: (0 , _lib0_error.assert) is not a function
cause This error often occurs due to issues with how bundlers or transpilers handle named exports from subpath imports, sometimes related to tree-shaking configurations or incorrect module interop settings.fixVerify your bundler (e.g., Webpack, Rollup, esbuild) configuration has `esModuleInterop: true` in TypeScript, and that tree-shaking rules are not overly aggressive or misconfigured for `lib0`'s subpath exports. Ensure direct named imports (`import { assert } from 'lib0/error'`) are used rather than default imports or namespace imports if only specific functions are needed.
Warnings
- breaking The upcoming `v1.0.0` major version introduces significant breaking changes, particularly within the `delta` and `schema` modules. Users relying on these specific functionalities in `0.x` versions will need to adapt their code when upgrading to `1.0.0` or later release candidates.
- breaking lib0 requires Node.js version `16` or higher. Projects running on older Node.js runtimes will encounter compatibility issues, including syntax errors or missing global APIs.
- gotcha While lib0 ships with both CommonJS and ESM module formats, misconfigured bundlers or incorrect `package.json` configurations in consumer projects can lead to module resolution errors (e.g., `ERR_REQUIRE_ESM`) when attempting to `require()` an ESM-only entrypoint or vice-versa.
Install
-
npm install lib0 -
yarn add lib0 -
pnpm add lib0
Imports
- assert
import * as error from 'lib0/error'; error.assert(...)
import { assert } from 'lib0/error' - createEncoder, writeVarUint
const { createEncoder } = require('lib0/encoding')import { createEncoder, writeVarUint } from 'lib0/encoding' - from
import { from as arrayFrom } from 'lib0/array'import { from } from 'lib0/array'
Quickstart
import { assert } from 'lib0/error';
import { createEncoder, writeVarUint, writeString } from 'lib0/encoding';
import { createDecoder, readVarUint, readString } from 'lib0/decoding';
// Basic assertion
assert(1 + 1 === 2, 'Arithmetic should work!');
console.log('Assertion passed: 1 + 1 === 2');
// Encoding and decoding a simple value
const encoder = createEncoder();
writeVarUint(encoder, 12345);
writeString(encoder, 'Hello, lib0!');
const encodedData = encoder.buf;
console.log(`Encoded data (Uint8Array): ${encodedData}`);
const decoder = createDecoder(encodedData);
const decodedNumber = readVarUint(decoder);
const decodedString = readString(decoder);
console.log(`Decoded number: ${decodedNumber}`);
console.log(`Decoded string: ${decodedString}`);
assert(decodedNumber === 12345, 'Decoded number mismatch');
assert(decodedString === 'Hello, lib0!', 'Decoded string mismatch');
console.log('Encoding and decoding successful!');
// Using an array utility
import { from } from 'lib0/array';
const iterable = new Set([1, 2, 3]);
const arr = from(iterable);
assert(arr.length === 3 && arr[0] === 1, 'Array conversion failed');
console.log(`Converted iterable to array: ${arr}`);