@eliware/common 15.0.1 → 15.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +7 -5
- package/README.md +12 -12
- package/RELEASE_NOTES.md +46 -0
- package/docs/README.md +10 -4
- package/docs/api.md +35 -164
- package/docs/logging.md +66 -0
- package/docs/package.md +44 -30
- package/docs/paths.md +57 -0
- package/docs/process-errors.md +62 -0
- package/docs/redaction.md +115 -0
- package/docs/signals.md +67 -0
- package/docs/snowflake-ids.md +77 -0
- package/docs/troubleshooting.md +33 -29
- package/docs/usage.md +42 -138
- package/package.json +28 -8
- package/specs/README.md +6 -0
- package/specs/directives.yaml +11 -6
- package/specs/errors.yaml +23 -0
- package/specs/log.yaml +19 -0
- package/specs/path.yaml +18 -0
- package/specs/redact.yaml +20 -0
- package/specs/signals.yaml +16 -0
- package/specs/snowflake.yaml +18 -0
- package/src/errors/cleanup/abort-listener.mjs +19 -0
- package/src/errors/cleanup/attached-handlers.mjs +30 -0
- package/src/errors/cleanup/detached-state.mjs +8 -0
- package/src/errors/cleanup/finalize.mjs +5 -0
- package/src/errors/cleanup/listener.mjs +27 -0
- package/src/errors/cleanup.mjs +43 -0
- package/src/errors/constants.mjs +1 -0
- package/src/errors/event/handler.mjs +38 -0
- package/src/errors/event/one-shot.mjs +37 -0
- package/src/errors/event-handlers.mjs +33 -0
- package/src/errors/failure.mjs +10 -0
- package/src/errors/finalize.mjs +27 -0
- package/src/errors/handler-removal.mjs +18 -0
- package/src/errors/index.d.mts +28 -0
- package/src/errors/index.d.ts +28 -0
- package/src/errors/index.mjs +6 -0
- package/src/errors/logging/diagnostic.mjs +9 -0
- package/src/errors/logging/error-diagnostic.mjs +5 -0
- package/src/errors/logging/value-diagnostic.mjs +5 -0
- package/src/errors/logging.mjs +10 -0
- package/src/errors/registration/events.mjs +9 -0
- package/src/errors/registration/lifecycle.mjs +18 -0
- package/src/errors/registration/registry.mjs +5 -0
- package/src/errors/registration/setup-state.mjs +20 -0
- package/src/errors/registration/state.mjs +28 -0
- package/src/errors/registration.mjs +80 -0
- package/src/errors/setup/installation.mjs +52 -0
- package/src/errors/setup/rollback.mjs +35 -0
- package/src/errors/setup.mjs +7 -0
- package/src/errors/validation/events.mjs +17 -0
- package/src/errors/validation/logger.mjs +8 -0
- package/src/errors/validation/process-object.mjs +11 -0
- package/src/errors/validation/signal.mjs +9 -0
- package/src/index.d.ts +14 -14
- package/src/index.mjs +9 -7
- package/src/log/child-logger.mjs +5 -0
- package/src/log/configuration/build-logger.mjs +13 -0
- package/src/log/configuration/default-options.mjs +5 -0
- package/src/log/configuration/normalize-options.mjs +14 -0
- package/src/log/configuration/validate-options.mjs +16 -0
- package/src/log/create-logger.mjs +18 -0
- package/src/log/formats/json-record.mjs +19 -0
- package/src/log/formats/json.mjs +38 -0
- package/src/log/formats/message.mjs +13 -0
- package/src/log/formats/text.mjs +21 -0
- package/src/log/index.d.mts +74 -0
- package/src/log/index.d.ts +74 -0
- package/src/log/index.mjs +15 -0
- package/src/log/logger-levels.mjs +37 -0
- package/src/path/base.mjs +17 -0
- package/src/path/conversions.mjs +7 -0
- package/src/path/create-path-helpers.mjs +58 -0
- package/src/path/current-path.mjs +13 -0
- package/src/path/file-url-to-path.mjs +5 -0
- package/src/path/index.d.mts +66 -0
- package/src/path/index.d.ts +66 -0
- package/src/path/index.mjs +12 -0
- package/src/path/input-classification.mjs +11 -0
- package/src/path/operation/failure.mjs +12 -0
- package/src/path/operation/logging.mjs +24 -0
- package/src/redact/headers/inspect-header-input.mjs +17 -0
- package/src/redact/headers/redact/entries.mjs +33 -0
- package/src/redact/headers/redact/headers.mjs +12 -0
- package/src/redact/headers/valid-entries.mjs +15 -0
- package/src/redact/index.d.mts +49 -0
- package/src/redact/index.d.ts +49 -0
- package/src/redact/index.mjs +7 -0
- package/src/redact/inspection/add-truncation-marker.mjs +13 -0
- package/src/redact/inspection/copy-error-fields.mjs +20 -0
- package/src/redact/inspection/inspect/entries.mjs +15 -0
- package/src/redact/inspection/inspect/error-fields.mjs +44 -0
- package/src/redact/inspection/safe-entries.mjs +7 -0
- package/src/redact/limits/default-limits.mjs +6 -0
- package/src/redact/limits/normalize/output-limit.mjs +8 -0
- package/src/redact/limits/normalize/traversal-limits.mjs +12 -0
- package/src/redact/markers/default-marker.mjs +1 -0
- package/src/redact/policy/create/policy.mjs +9 -0
- package/src/redact/policy/create/readonly-set.mjs +31 -0
- package/src/redact/policy/default/key-names.mjs +23 -0
- package/src/redact/policy/default/policy.mjs +9 -0
- package/src/redact/policy/normalize/keys.mjs +13 -0
- package/src/redact/policy/normalize-policy.mjs +14 -0
- package/src/redact/serialization/safe-serialize.mjs +34 -0
- package/src/redact/serialization/serialize/array.mjs +13 -0
- package/src/redact/serialization/serialize/buffer.mjs +5 -0
- package/src/redact/serialization/serialize/error.mjs +12 -0
- package/src/redact/serialization/serialize/object.mjs +22 -0
- package/src/redact/serialization/serialize/primitive.mjs +21 -0
- package/src/redact/structured/redact/array/attempt.mjs +9 -0
- package/src/redact/structured/redact/array/index-name.mjs +12 -0
- package/src/redact/structured/redact/array/indices.mjs +18 -0
- package/src/redact/structured/redact/array/properties.mjs +60 -0
- package/src/redact/structured/redact/array/result.mjs +11 -0
- package/src/redact/structured/redact/array.mjs +26 -0
- package/src/redact/structured/redact/classify.mjs +13 -0
- package/src/redact/structured/redact/error-fields.mjs +6 -0
- package/src/redact/structured/redact/error.mjs +14 -0
- package/src/redact/structured/redact/object.mjs +23 -0
- package/src/redact/structured/redact/value.mjs +49 -0
- package/src/redact/text/apply/configured/rules.mjs +9 -0
- package/src/redact/text/apply/configured/text-redactions.mjs +11 -0
- package/src/redact/text/apply/redactions.mjs +17 -0
- package/src/redact/text/apply/text-rules.mjs +8 -0
- package/src/redact/text/boundary/detect-jwt.mjs +18 -0
- package/src/redact/text/boundary/finish.mjs +15 -0
- package/src/redact/text/boundary/input.mjs +10 -0
- package/src/redact/text/boundary/jwt.mjs +22 -0
- package/src/redact/text/boundary/prepare.mjs +16 -0
- package/src/redact/text/boundary/process.mjs +9 -0
- package/src/redact/text/boundary/redaction.mjs +20 -0
- package/src/redact/text/boundary/secrets.mjs +21 -0
- package/src/redact/text/limit-output.mjs +12 -0
- package/src/redact/text/patterns/secret-key-patterns.mjs +2 -0
- package/src/redact/text/redact-text.mjs +8 -0
- package/src/redact/text/replace-literal/matches.mjs +39 -0
- package/src/redact/text/replace-literal/secret.mjs +6 -0
- package/src/redact/text/replace-literal/secrets.mjs +18 -0
- package/src/redact/text/replace-literal/trie.mjs +36 -0
- package/src/redact/text/rules/built-in.mjs +8 -0
- package/src/redact/text/rules/redact/authorization.mjs +12 -0
- package/src/redact/text/rules/redact/jwt/pattern.mjs +2 -0
- package/src/redact/text/rules/redact/jwt.mjs +10 -0
- package/src/redact/text/rules/redact/key/configured-values.mjs +25 -0
- package/src/redact/text/rules/redact/key/material.mjs +9 -0
- package/src/redact/text/rules/redact/key/values.mjs +16 -0
- package/src/redact/text/rules/redact/provider-token.mjs +9 -0
- package/src/signals/index.d.mts +29 -0
- package/src/signals/index.d.ts +29 -0
- package/src/signals/index.mjs +1 -0
- package/src/signals/lifecycle/abort/aggregate-cleanup.mjs +21 -0
- package/src/signals/lifecycle/abort/cleanup.mjs +9 -0
- package/src/signals/lifecycle/abort/listener-registration.mjs +14 -0
- package/src/signals/lifecycle/abort/listener.mjs +16 -0
- package/src/signals/lifecycle/abort/registration-cleanup.mjs +14 -0
- package/src/signals/lifecycle/abort/registration.mjs +19 -0
- package/src/signals/lifecycle/before/exit-registration.mjs +36 -0
- package/src/signals/lifecycle/before/exit.mjs +33 -0
- package/src/signals/lifecycle/cleanup/process-handlers.mjs +27 -0
- package/src/signals/lifecycle/cleanup/registration.mjs +5 -0
- package/src/signals/lifecycle/cleanup.mjs +17 -0
- package/src/signals/lifecycle/defaults.mjs +1 -0
- package/src/signals/lifecycle/exit-policy.mjs +17 -0
- package/src/signals/lifecycle/handlers/rollback.mjs +14 -0
- package/src/signals/lifecycle/handlers.mjs +35 -0
- package/src/signals/lifecycle/hooks.mjs +19 -0
- package/src/signals/lifecycle/logging.mjs +11 -0
- package/src/signals/lifecycle/registration-policy.mjs +29 -0
- package/src/signals/lifecycle/registration.mjs +7 -0
- package/src/signals/lifecycle/shutdown.mjs +75 -0
- package/src/signals/lifecycle/validation/lifecycle-options.mjs +8 -0
- package/src/signals/lifecycle/validation/logger.mjs +9 -0
- package/src/signals/lifecycle/validation/options.mjs +19 -0
- package/src/signals/lifecycle/validation/registration-options.mjs +36 -0
- package/src/signals/lifecycle/validation/signals.mjs +9 -0
- package/src/signals/register.mjs +76 -0
- package/src/snowflake/constants.mjs +15 -0
- package/src/snowflake/generator-options.mjs +27 -0
- package/src/snowflake/generator.mjs +82 -0
- package/src/snowflake/index.d.mts +24 -0
- package/src/snowflake/index.d.ts +24 -0
- package/src/snowflake/index.mjs +8 -0
- package/src/snowflake/log-generation-failure.mjs +9 -0
- package/src/snowflake/logger-validation.mjs +10 -0
- package/src/snowflake/validation.mjs +18 -0
- package/src/errors.d.mts +0 -9
- package/src/errors.mjs +0 -4
- package/src/fs.mjs +0 -4
- package/src/path.d.mts +0 -12
- package/src/path.mjs +0 -22
- package/src/signals.mjs +0 -4
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const createExitPolicy = ({ processObj, exit, exitCode }) => {
|
|
2
|
+
let requested = false;
|
|
3
|
+
let called = false;
|
|
4
|
+
|
|
5
|
+
const request = () => {
|
|
6
|
+
requested = true;
|
|
7
|
+
};
|
|
8
|
+
const afterHooks = () => {
|
|
9
|
+
if (!requested || !exit || typeof processObj.exit !== "function" || called) return;
|
|
10
|
+
called = true;
|
|
11
|
+
processObj.exit(exitCode);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
return { request, afterHooks };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default createExitPolicy;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function rollbackHandlers(processObj, listeners, beforeExitListener) {
|
|
2
|
+
for (const [name, listener] of listeners) {
|
|
3
|
+
try {
|
|
4
|
+
processObj.off(name, listener);
|
|
5
|
+
} catch {}
|
|
6
|
+
}
|
|
7
|
+
if (beforeExitListener) {
|
|
8
|
+
try {
|
|
9
|
+
processObj.off("beforeExit", beforeExitListener);
|
|
10
|
+
} catch {}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default rollbackHandlers;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import rollbackHandlers from "./handlers/rollback.mjs";
|
|
2
|
+
|
|
3
|
+
const installHandlers = ({ processObj, signals, shutdown, onBeforeExit }) => {
|
|
4
|
+
let active = true;
|
|
5
|
+
const listeners = new Map();
|
|
6
|
+
let beforeExitListener;
|
|
7
|
+
try {
|
|
8
|
+
for (const name of signals) {
|
|
9
|
+
const listener = () => {
|
|
10
|
+
if (!active) return;
|
|
11
|
+
return shutdown(name);
|
|
12
|
+
};
|
|
13
|
+
listeners.set(name, listener);
|
|
14
|
+
processObj.on(name, listener);
|
|
15
|
+
}
|
|
16
|
+
beforeExitListener = (code) => {
|
|
17
|
+
if (!active) return;
|
|
18
|
+
return onBeforeExit(code);
|
|
19
|
+
};
|
|
20
|
+
processObj.on("beforeExit", beforeExitListener);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
active = false;
|
|
23
|
+
rollbackHandlers(processObj, listeners, beforeExitListener);
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
listeners,
|
|
28
|
+
onBeforeExit: beforeExitListener,
|
|
29
|
+
deactivate: () => {
|
|
30
|
+
active = false;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default installHandlers;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
const createHookRunner = ({ hooks, onError }) => {
|
|
4
|
+
const hookContext = new AsyncLocalStorage();
|
|
5
|
+
|
|
6
|
+
const run = async (receivedSignal) => {
|
|
7
|
+
for (const hook of hooks) {
|
|
8
|
+
try {
|
|
9
|
+
await hookContext.run(true, () => hook(receivedSignal));
|
|
10
|
+
} catch {
|
|
11
|
+
onError();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return { run, isRunning: () => Boolean(hookContext.getStore()) };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default createHookRunner;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const applyRegistrationPolicy = ({
|
|
2
|
+
options,
|
|
3
|
+
registration,
|
|
4
|
+
log,
|
|
5
|
+
signals,
|
|
6
|
+
exitCode,
|
|
7
|
+
exit,
|
|
8
|
+
signal,
|
|
9
|
+
shutdownHook,
|
|
10
|
+
}) => {
|
|
11
|
+
const conflicts =
|
|
12
|
+
(Object.hasOwn(options, "log") && registration.log !== log) ||
|
|
13
|
+
(Object.hasOwn(options, "signals") &&
|
|
14
|
+
(registration.signals.length !== signals.length ||
|
|
15
|
+
!registration.signals.every((name, index) => name === signals[index]))) ||
|
|
16
|
+
(Object.hasOwn(options, "exitCode") && registration.exitCode !== exitCode) ||
|
|
17
|
+
(Object.hasOwn(options, "exit") && registration.exit !== exit) ||
|
|
18
|
+
(Object.hasOwn(options, "signal") && registration.signal !== signal);
|
|
19
|
+
if (conflicts) throw new TypeError("Repeated registration must use the same lifecycle options");
|
|
20
|
+
if (shutdownHook) {
|
|
21
|
+
if (registration.api.getShuttingDown()) {
|
|
22
|
+
throw new TypeError("Cannot add a cleanup function after shutdown starts");
|
|
23
|
+
}
|
|
24
|
+
registration.hooks.push(shutdownHook);
|
|
25
|
+
}
|
|
26
|
+
return registration.api;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export default applyRegistrationPolicy;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const registrations = new WeakMap();
|
|
2
|
+
|
|
3
|
+
const getRegistration = (processObj) => registrations.get(processObj);
|
|
4
|
+
const setRegistration = (processObj, registration) => registrations.set(processObj, registration);
|
|
5
|
+
const deleteRegistration = (processObj) => registrations.delete(processObj);
|
|
6
|
+
|
|
7
|
+
export default { getRegistration, setRegistration, deleteRegistration };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import createBeforeExitRegistration from "./before/exit-registration.mjs";
|
|
2
|
+
import createHookRunner from "./hooks.mjs";
|
|
3
|
+
import createExitPolicy from "./exit-policy.mjs";
|
|
4
|
+
import createSafeLogger from "./logging.mjs";
|
|
5
|
+
|
|
6
|
+
const createShutdown = ({ hooks, log, processObj, exit, exitCode }) => {
|
|
7
|
+
let shuttingDown = false;
|
|
8
|
+
let active = true;
|
|
9
|
+
let shutdownPromise;
|
|
10
|
+
let hooksFinished = false;
|
|
11
|
+
const exitPolicy = createExitPolicy({ processObj, exit, exitCode });
|
|
12
|
+
const logSafely = createSafeLogger(log);
|
|
13
|
+
const hookRunner = createHookRunner({
|
|
14
|
+
hooks,
|
|
15
|
+
onError: () => logSafely("error", "A shutdown cleanup function failed."),
|
|
16
|
+
});
|
|
17
|
+
const runHooks = hookRunner.run;
|
|
18
|
+
const shutdown = (receivedSignal = "manual") => {
|
|
19
|
+
if (!active) return Promise.resolve();
|
|
20
|
+
if (shutdownPromise) {
|
|
21
|
+
if (hookRunner.isRunning()) return Promise.resolve();
|
|
22
|
+
logSafely("warn", `Received ${receivedSignal} during shutdown.`);
|
|
23
|
+
if (hooksFinished) exitPolicy.afterHooks();
|
|
24
|
+
return shutdownPromise;
|
|
25
|
+
}
|
|
26
|
+
shuttingDown = true;
|
|
27
|
+
logSafely("debug", `Received ${receivedSignal}. Shutting down gracefully...`);
|
|
28
|
+
let resolveShutdown;
|
|
29
|
+
let rejectShutdown;
|
|
30
|
+
shutdownPromise = new Promise((resolve, reject) => {
|
|
31
|
+
resolveShutdown = resolve;
|
|
32
|
+
rejectShutdown = reject;
|
|
33
|
+
});
|
|
34
|
+
runHooks(receivedSignal)
|
|
35
|
+
.then(() => {
|
|
36
|
+
hooksFinished = true;
|
|
37
|
+
exitPolicy.afterHooks();
|
|
38
|
+
})
|
|
39
|
+
.then(resolveShutdown, rejectShutdown);
|
|
40
|
+
return shutdownPromise;
|
|
41
|
+
};
|
|
42
|
+
const shutdownForSignal = (receivedSignal) => {
|
|
43
|
+
exitPolicy.request();
|
|
44
|
+
return shutdown(receivedSignal);
|
|
45
|
+
};
|
|
46
|
+
const onBeforeExit = createBeforeExitRegistration({
|
|
47
|
+
isActive: () => active,
|
|
48
|
+
isShuttingDown: () => shuttingDown,
|
|
49
|
+
getShutdownPromise: () => shutdownPromise,
|
|
50
|
+
setShuttingDown: (value) => {
|
|
51
|
+
shuttingDown = value;
|
|
52
|
+
},
|
|
53
|
+
setShutdownPromise: (promise) => {
|
|
54
|
+
shutdownPromise = promise;
|
|
55
|
+
},
|
|
56
|
+
logSafely,
|
|
57
|
+
runHooks,
|
|
58
|
+
onHooksFinished: () => {
|
|
59
|
+
hooksFinished = true;
|
|
60
|
+
exitPolicy.afterHooks();
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
shutdown,
|
|
66
|
+
shutdownForSignal,
|
|
67
|
+
onBeforeExit,
|
|
68
|
+
deactivate: () => {
|
|
69
|
+
active = false;
|
|
70
|
+
},
|
|
71
|
+
getShuttingDown: () => shuttingDown,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export default createShutdown;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const validateLifecycleOptions = ({ exit, exitCode }) => {
|
|
2
|
+
if (typeof exit !== "boolean") throw new TypeError("exit must be a boolean");
|
|
3
|
+
if (!Number.isInteger(exitCode) || !Number.isFinite(exitCode)) {
|
|
4
|
+
throw new TypeError("exitCode must be a finite integer");
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export default validateLifecycleOptions;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const validateOptions = ({ processObj, shutdownHook, signal }) => {
|
|
2
|
+
if (!processObj || typeof processObj.on !== "function") {
|
|
3
|
+
throw new TypeError("processObj.on must be a function");
|
|
4
|
+
}
|
|
5
|
+
if (typeof processObj.off !== "function") {
|
|
6
|
+
throw new TypeError("processObj.off must be a function");
|
|
7
|
+
}
|
|
8
|
+
if (typeof processObj.listeners !== "function") {
|
|
9
|
+
throw new TypeError("processObj.listeners must be a function");
|
|
10
|
+
}
|
|
11
|
+
if (shutdownHook !== undefined && typeof shutdownHook !== "function") {
|
|
12
|
+
throw new TypeError("shutdownHook must be a function");
|
|
13
|
+
}
|
|
14
|
+
if (signal !== undefined && (!signal || typeof signal.addEventListener !== "function")) {
|
|
15
|
+
throw new TypeError("signal must provide addEventListener");
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default validateOptions;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import logger from "../../../log/index.mjs";
|
|
2
|
+
import { defaultSignals } from "../defaults.mjs";
|
|
3
|
+
import validateLogger from "./logger.mjs";
|
|
4
|
+
import normalizeSignals from "./signals.mjs";
|
|
5
|
+
import validateOptions from "./options.mjs";
|
|
6
|
+
import validateLifecycleOptions from "./lifecycle-options.mjs";
|
|
7
|
+
|
|
8
|
+
function validateRegistrationOptions(options = {}) {
|
|
9
|
+
if (options === null || typeof options !== "object") {
|
|
10
|
+
throw new TypeError("options must be an object");
|
|
11
|
+
}
|
|
12
|
+
const {
|
|
13
|
+
processObj = process,
|
|
14
|
+
log = logger,
|
|
15
|
+
signals,
|
|
16
|
+
shutdownHook,
|
|
17
|
+
exitCode = 0,
|
|
18
|
+
exit = true,
|
|
19
|
+
signal,
|
|
20
|
+
} = options;
|
|
21
|
+
validateOptions({ processObj, shutdownHook, signal });
|
|
22
|
+
validateLifecycleOptions({ exit, exitCode });
|
|
23
|
+
validateLogger(log);
|
|
24
|
+
return {
|
|
25
|
+
options,
|
|
26
|
+
processObj,
|
|
27
|
+
log,
|
|
28
|
+
signals: normalizeSignals(signals, defaultSignals),
|
|
29
|
+
shutdownHook,
|
|
30
|
+
exitCode,
|
|
31
|
+
exit,
|
|
32
|
+
signal,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default validateRegistrationOptions;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const normalizeSignals = (signals, defaultSignals) => {
|
|
2
|
+
if (signals === undefined) return [...defaultSignals];
|
|
3
|
+
if (!Array.isArray(signals) || signals.some((signal) => typeof signal !== "string")) {
|
|
4
|
+
throw new TypeError("signals must be an array of signal names");
|
|
5
|
+
}
|
|
6
|
+
return [...new Set(signals)];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export default normalizeSignals;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import validateRegistrationOptions from "./lifecycle/validation/registration-options.mjs";
|
|
2
|
+
import __methods from "./lifecycle/registration.mjs";
|
|
3
|
+
const { getRegistration, setRegistration } = __methods;
|
|
4
|
+
import applyRegistrationPolicy from "./lifecycle/registration-policy.mjs";
|
|
5
|
+
import createShutdown from "./lifecycle/shutdown.mjs";
|
|
6
|
+
import installHandlers from "./lifecycle/handlers.mjs";
|
|
7
|
+
import createCleanup from "./lifecycle/cleanup.mjs";
|
|
8
|
+
import createAbortRegistration from "./lifecycle/abort/registration.mjs";
|
|
9
|
+
|
|
10
|
+
const registerSignals = (options = {}) => {
|
|
11
|
+
const {
|
|
12
|
+
processObj,
|
|
13
|
+
log,
|
|
14
|
+
signals: selected,
|
|
15
|
+
shutdownHook,
|
|
16
|
+
exitCode,
|
|
17
|
+
exit,
|
|
18
|
+
signal,
|
|
19
|
+
} = validateRegistrationOptions(options);
|
|
20
|
+
let registration = getRegistration(processObj);
|
|
21
|
+
if (registration) {
|
|
22
|
+
return applyRegistrationPolicy({
|
|
23
|
+
options,
|
|
24
|
+
registration,
|
|
25
|
+
log,
|
|
26
|
+
signals: selected,
|
|
27
|
+
exitCode,
|
|
28
|
+
exit,
|
|
29
|
+
signal,
|
|
30
|
+
shutdownHook,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const hooks = shutdownHook ? [shutdownHook] : [];
|
|
35
|
+
const lifecycle = createShutdown({ hooks, log, processObj, exit, exitCode });
|
|
36
|
+
const handlers = installHandlers({
|
|
37
|
+
processObj,
|
|
38
|
+
signals: selected,
|
|
39
|
+
shutdown: lifecycle.shutdownForSignal,
|
|
40
|
+
onBeforeExit: lifecycle.onBeforeExit,
|
|
41
|
+
});
|
|
42
|
+
const cleanup = createCleanup({
|
|
43
|
+
processObj,
|
|
44
|
+
...handlers,
|
|
45
|
+
deactivate: () => {
|
|
46
|
+
handlers.deactivate();
|
|
47
|
+
lifecycle.deactivate();
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const abortRegistration = createAbortRegistration({
|
|
51
|
+
signal,
|
|
52
|
+
removeHandlers: cleanup.removeHandlers,
|
|
53
|
+
});
|
|
54
|
+
const api = {
|
|
55
|
+
shutdown: lifecycle.shutdown,
|
|
56
|
+
getShuttingDown: lifecycle.getShuttingDown,
|
|
57
|
+
removeHandlers: abortRegistration.removeHandlers,
|
|
58
|
+
get removed() {
|
|
59
|
+
return abortRegistration.getRemoved();
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
registration = { hooks, api, log, exitCode, exit, signal, signals: selected };
|
|
63
|
+
setRegistration(processObj, registration);
|
|
64
|
+
try {
|
|
65
|
+
abortRegistration.attach();
|
|
66
|
+
log.debug("Registered Handlers", { signals: selected.join(", ") });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
try {
|
|
69
|
+
abortRegistration.removeHandlers();
|
|
70
|
+
} catch {}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
return registration.api;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export default registerSignals;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const DEFAULT_EPOCH = 365662380000n;
|
|
2
|
+
export const SEQUENCE_BITS = 12n;
|
|
3
|
+
export const PROCESS_BITS = 5n;
|
|
4
|
+
export const WORKER_BITS = 5n;
|
|
5
|
+
export const PROCESS_MASK = (1n << PROCESS_BITS) - 1n;
|
|
6
|
+
export const WORKER_MASK = (1n << WORKER_BITS) - 1n;
|
|
7
|
+
export const SEQUENCE_MASK = (1n << SEQUENCE_BITS) - 1n;
|
|
8
|
+
export const MAX_TIMESTAMP = (1n << 42n) - 1n;
|
|
9
|
+
|
|
10
|
+
export const constants = Object.freeze({
|
|
11
|
+
epoch: DEFAULT_EPOCH,
|
|
12
|
+
sequenceBits: SEQUENCE_BITS,
|
|
13
|
+
processBits: PROCESS_BITS,
|
|
14
|
+
workerBits: WORKER_BITS,
|
|
15
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DEFAULT_EPOCH, MAX_TIMESTAMP } from "./constants.mjs";
|
|
2
|
+
import __methods from "./validation.mjs";
|
|
3
|
+
const { validateIdentity } = __methods;
|
|
4
|
+
import validateGeneratorLogger from "./logger-validation.mjs";
|
|
5
|
+
|
|
6
|
+
const defaultNow = () => Date.now();
|
|
7
|
+
|
|
8
|
+
function validateGeneratorOptions({
|
|
9
|
+
epoch = DEFAULT_EPOCH,
|
|
10
|
+
workerId = 0,
|
|
11
|
+
processId = process.pid % 32,
|
|
12
|
+
now = defaultNow,
|
|
13
|
+
logger,
|
|
14
|
+
} = {}) {
|
|
15
|
+
if (typeof epoch === "number" && !Number.isSafeInteger(epoch))
|
|
16
|
+
throw new RangeError("epoch must be between 0 and the timestamp limit");
|
|
17
|
+
const baseEpoch = BigInt(epoch);
|
|
18
|
+
if (baseEpoch < 0n || baseEpoch > MAX_TIMESTAMP)
|
|
19
|
+
throw new RangeError("epoch must be between 0 and the timestamp limit");
|
|
20
|
+
const identity = validateIdentity(workerId, processId);
|
|
21
|
+
if (typeof now !== "function") throw new TypeError("clock source must be a function");
|
|
22
|
+
validateGeneratorLogger(logger);
|
|
23
|
+
|
|
24
|
+
return { baseEpoch, ...identity, now, logger };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default validateGeneratorOptions;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MAX_TIMESTAMP,
|
|
3
|
+
PROCESS_BITS,
|
|
4
|
+
SEQUENCE_BITS,
|
|
5
|
+
SEQUENCE_MASK,
|
|
6
|
+
WORKER_BITS,
|
|
7
|
+
} from "./constants.mjs";
|
|
8
|
+
import validateGeneratorOptions from "./generator-options.mjs";
|
|
9
|
+
import logGenerationFailure from "./log-generation-failure.mjs";
|
|
10
|
+
|
|
11
|
+
function createGenerator(options = {}) {
|
|
12
|
+
const { baseEpoch, worker, processNumber, now, logger } = validateGeneratorOptions(options);
|
|
13
|
+
|
|
14
|
+
let lastTimestamp = -1n;
|
|
15
|
+
let sequence = 0n;
|
|
16
|
+
|
|
17
|
+
logger?.info("Snowflake generator created", {
|
|
18
|
+
component: "snowflake",
|
|
19
|
+
operation: "create-generator",
|
|
20
|
+
epoch: baseEpoch.toString(),
|
|
21
|
+
workerId: worker.toString(),
|
|
22
|
+
processId: processNumber.toString(),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function readTime() {
|
|
26
|
+
const clockValue = now();
|
|
27
|
+
if (typeof clockValue === "number" && !Number.isSafeInteger(clockValue))
|
|
28
|
+
throw new RangeError("current time must be a safe integer or bigint");
|
|
29
|
+
const timestamp = BigInt(clockValue);
|
|
30
|
+
if (timestamp < baseEpoch && lastTimestamp < baseEpoch)
|
|
31
|
+
throw new RangeError("current time must not be before epoch");
|
|
32
|
+
return timestamp;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return function generate() {
|
|
36
|
+
try {
|
|
37
|
+
let timestamp = readTime();
|
|
38
|
+
if (timestamp < lastTimestamp) {
|
|
39
|
+
logger?.warn("Snowflake clock moved backward", {
|
|
40
|
+
component: "snowflake",
|
|
41
|
+
operation: "generate",
|
|
42
|
+
event: "clock-rollback",
|
|
43
|
+
timestamp: timestamp.toString(),
|
|
44
|
+
lastTimestamp: lastTimestamp.toString(),
|
|
45
|
+
});
|
|
46
|
+
timestamp = lastTimestamp;
|
|
47
|
+
}
|
|
48
|
+
let nextSequence;
|
|
49
|
+
if (timestamp === lastTimestamp) {
|
|
50
|
+
if (sequence === SEQUENCE_MASK)
|
|
51
|
+
throw new RangeError(
|
|
52
|
+
"sequence overflow. Retry when the clock returns a greater timestamp.",
|
|
53
|
+
);
|
|
54
|
+
nextSequence = sequence + 1n;
|
|
55
|
+
} else {
|
|
56
|
+
nextSequence = 0n;
|
|
57
|
+
}
|
|
58
|
+
const elapsed = timestamp - baseEpoch;
|
|
59
|
+
if (elapsed > MAX_TIMESTAMP) throw new RangeError("timestamp exceeds Snowflake capacity");
|
|
60
|
+
lastTimestamp = timestamp;
|
|
61
|
+
sequence = nextSequence;
|
|
62
|
+
const id = (
|
|
63
|
+
(elapsed << (WORKER_BITS + PROCESS_BITS + SEQUENCE_BITS)) |
|
|
64
|
+
(worker << (PROCESS_BITS + SEQUENCE_BITS)) |
|
|
65
|
+
(processNumber << SEQUENCE_BITS) |
|
|
66
|
+
nextSequence
|
|
67
|
+
).toString();
|
|
68
|
+
logger?.debug("Snowflake ID generated", {
|
|
69
|
+
component: "snowflake",
|
|
70
|
+
operation: "generate",
|
|
71
|
+
timestamp: timestamp.toString(),
|
|
72
|
+
sequence: nextSequence.toString(),
|
|
73
|
+
});
|
|
74
|
+
return id;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
logGenerationFailure(logger, error);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default createGenerator;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { LoggerWithConfiguredLevels } from "../log/index.mjs";
|
|
2
|
+
|
|
3
|
+
export interface SnowflakeGeneratorOptions {
|
|
4
|
+
epoch?: number | bigint;
|
|
5
|
+
workerId?: number | bigint;
|
|
6
|
+
processId?: number | bigint;
|
|
7
|
+
now?: () => number | bigint;
|
|
8
|
+
logger?: LoggerWithConfiguredLevels;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type { LoggerWithConfiguredLevels } from "../log/index.mjs";
|
|
12
|
+
|
|
13
|
+
export interface SnowflakeConstants {
|
|
14
|
+
readonly epoch: bigint;
|
|
15
|
+
readonly sequenceBits: bigint;
|
|
16
|
+
readonly processBits: bigint;
|
|
17
|
+
readonly workerBits: bigint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createSnowflakeGenerator(options?: SnowflakeGeneratorOptions): () => string;
|
|
21
|
+
export declare const generate: () => string;
|
|
22
|
+
export declare const snowflake: typeof generate;
|
|
23
|
+
export declare const constants: SnowflakeConstants;
|
|
24
|
+
export default generate;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { LoggerWithConfiguredLevels } from "../log/index.mjs";
|
|
2
|
+
|
|
3
|
+
export interface SnowflakeGeneratorOptions {
|
|
4
|
+
epoch?: number | bigint;
|
|
5
|
+
workerId?: number | bigint;
|
|
6
|
+
processId?: number | bigint;
|
|
7
|
+
now?: () => number | bigint;
|
|
8
|
+
logger?: LoggerWithConfiguredLevels;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type { LoggerWithConfiguredLevels } from "../log/index.mjs";
|
|
12
|
+
|
|
13
|
+
export interface SnowflakeConstants {
|
|
14
|
+
readonly epoch: bigint;
|
|
15
|
+
readonly sequenceBits: bigint;
|
|
16
|
+
readonly processBits: bigint;
|
|
17
|
+
readonly workerBits: bigint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createSnowflakeGenerator(options?: SnowflakeGeneratorOptions): () => string;
|
|
21
|
+
export declare const generate: () => string;
|
|
22
|
+
export declare const snowflake: typeof generate;
|
|
23
|
+
export declare const constants: SnowflakeConstants;
|
|
24
|
+
export default generate;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { constants } from "./constants.mjs";
|
|
2
|
+
import createGenerator from "./generator.mjs";
|
|
3
|
+
|
|
4
|
+
export const createSnowflakeGenerator = createGenerator;
|
|
5
|
+
export const generate = createGenerator();
|
|
6
|
+
export const snowflake = generate;
|
|
7
|
+
export { constants };
|
|
8
|
+
export default generate;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function validateGeneratorLogger(logger) {
|
|
2
|
+
if (
|
|
3
|
+
logger !== undefined &&
|
|
4
|
+
(!logger ||
|
|
5
|
+
["error", "warn", "info", "debug"].some((level) => typeof logger[level] !== "function"))
|
|
6
|
+
)
|
|
7
|
+
throw new TypeError("logger must provide error, warn, info, and debug methods");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default validateGeneratorLogger;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PROCESS_MASK, WORKER_MASK } from "./constants.mjs";
|
|
2
|
+
|
|
3
|
+
function validatePart(value, name, mask) {
|
|
4
|
+
const parsed = BigInt(value);
|
|
5
|
+
if (parsed < 0n || parsed > mask) {
|
|
6
|
+
throw new RangeError(`${name} must be between 0 and ${mask}`);
|
|
7
|
+
}
|
|
8
|
+
return parsed;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function validateIdentity(workerId, processId) {
|
|
12
|
+
return {
|
|
13
|
+
worker: validatePart(workerId, "workerId", WORKER_MASK),
|
|
14
|
+
processNumber: validatePart(processId, "processId", PROCESS_MASK),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default { validatePart, validateIdentity };
|
package/src/errors.d.mts
DELETED
package/src/errors.mjs
DELETED
package/src/fs.mjs
DELETED
package/src/path.d.mts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
createPathHelpers,
|
|
3
|
-
default,
|
|
4
|
-
fileUrlToPath,
|
|
5
|
-
getCurrentDirname,
|
|
6
|
-
getCurrentFilename,
|
|
7
|
-
path,
|
|
8
|
-
pathUrl,
|
|
9
|
-
relativePath,
|
|
10
|
-
resolvePath,
|
|
11
|
-
} from "@eliware/path";
|
|
12
|
-
export type { PathHelpers, PathHelpersOptions, PathLogger } from "@eliware/path";
|
package/src/path.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
path,
|
|
3
|
-
createPathHelpers,
|
|
4
|
-
pathUrl,
|
|
5
|
-
getCurrentDirname,
|
|
6
|
-
getCurrentFilename,
|
|
7
|
-
resolvePath,
|
|
8
|
-
relativePath,
|
|
9
|
-
fileUrlToPath,
|
|
10
|
-
} from "@eliware/path";
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
path,
|
|
14
|
-
createPathHelpers,
|
|
15
|
-
pathUrl,
|
|
16
|
-
getCurrentDirname,
|
|
17
|
-
getCurrentFilename,
|
|
18
|
-
resolvePath,
|
|
19
|
-
relativePath,
|
|
20
|
-
fileUrlToPath,
|
|
21
|
-
};
|
|
22
|
-
export default path;
|
package/src/signals.mjs
DELETED