This document is the normative contract for the two public entrypoints:
src/index.jssrc/auto.js
If behavior in this document and code diverge, treat code as source of truth and update this document immediately.
m7-js-lib is a normalization-first utility runtime. It provides stable, defensive primitives that convert uncertain input into predictable shapes so downstream code can stay linear and resilient.
Core design intent:
- Prefer canonical coercion over repetitive defensive branching.
- Keep helper behavior predictable under malformed or partial input.
- Reserve strict failure for explicit opt-in boundaries.
This library is a singleton by design and by intent.
- There is exactly one
libobject per loaded module instance. - The project does not provide or support a multi-instance factory.
- Reinitialization rebuilds the same singleton object; it does not create a second instance.
Non-singleton usage is out of scope for this package.
index.js is the explicit, side-effect-free entrypoint.
default:lib(singleton object reference)- named:
lib - named:
init
- Importing
src/index.jsmust not initialize the runtime. - Initialization occurs only when
init()is called.
Signature:
init(opts = {})Accepted options:
opts.force(boolean): force rebuild of the singleton lifecycle.
Behavior:
- If already initialized and
force !== true, returnlibimmediately. - If initialization is in progress and
force !== true, throw:"[lib.init] Initialization already in progress"
- If
force === truewhile initialization is in progress, throw:"[lib.init] Cannot force re-init while initialization is in progress"
- If
force === trueand already initialized:- Clear existing enumerable keys on
lib. - Reset internal initialized state.
- Clear existing enumerable keys on
- Build runtime modules onto the existing
libreference. - On success:
- Set internal initialized state to true.
- Set non-enumerable
lib._initialized = true. - Return
lib.
- On failure:
- Clear partially built enumerable keys from
lib. - Set
lib._initialized = false. - Reset initialized state.
- Rethrow original error.
- Clear partially built enumerable keys from
- Always clear the internal "initializing" guard in
finally.
lib._initializedexists and is non-enumerable.falsebefore successful bootstrap.trueafter successful bootstrap.- Resets to
falseif bootstrap fails or forced rebuild begins.
auto.js is the convenience entrypoint with import-time initialization.
default:lib- named:
lib - named:
init
- Importing
src/auto.jsimmediately callsinit(). - This entrypoint is intentionally side-effectful.
Choose one entrypoint strategy per integration boundary.
- Use
src/index.jswhen you need explicit lifecycle control. - Use
src/auto.jswhen you want import-time readiness.
Recommended examples:
// Explicit lifecycle (recommended for shared libraries)
import lib, { init } from "./src/index.js";
init();// Auto-bootstrap (recommended for app entrypoints)
import lib from "./src/auto.js";- Stable singleton object identity for a given loaded module instance.
- Idempotent
init()when not forcing rebuild. - Deterministic error behavior for re-entrant init.
- Cross-bundle singleton unification if package duplication occurs in build output.
- Compatibility with non-singleton instantiation patterns.
Future user docs, architecture docs, and prose summaries must align with this contract.
Required language in downstream docs:
- "Singleton by intent"
- "
index.jsis side-effect free" - "
auto.jsauto-initializes on import"