@eliware/common 15.0.0 → 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 +54 -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,18 @@
|
|
|
1
|
+
import createRegistrationRegistry from "./registry.mjs";
|
|
2
|
+
|
|
3
|
+
function createRegistrationLifecycle() {
|
|
4
|
+
const registrations = createRegistrationRegistry();
|
|
5
|
+
return {
|
|
6
|
+
get(processObj) {
|
|
7
|
+
return registrations.get(processObj);
|
|
8
|
+
},
|
|
9
|
+
set(processObj, registration) {
|
|
10
|
+
registrations.set(processObj, registration);
|
|
11
|
+
},
|
|
12
|
+
delete(processObj) {
|
|
13
|
+
return registrations.delete(processObj);
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default createRegistrationLifecycle;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function createRegistrationSetupState() {
|
|
2
|
+
let setupComplete = false;
|
|
3
|
+
let cleanupPending = false;
|
|
4
|
+
return {
|
|
5
|
+
markSetupComplete() {
|
|
6
|
+
setupComplete = true;
|
|
7
|
+
},
|
|
8
|
+
isSetupComplete() {
|
|
9
|
+
return setupComplete;
|
|
10
|
+
},
|
|
11
|
+
setCleanupPending() {
|
|
12
|
+
cleanupPending = true;
|
|
13
|
+
},
|
|
14
|
+
isCleanupPending() {
|
|
15
|
+
return cleanupPending;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default createRegistrationSetupState;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const states = new WeakMap();
|
|
2
|
+
|
|
3
|
+
function createRegistrationState() {
|
|
4
|
+
const values = { removed: false, ready: false };
|
|
5
|
+
const registration = {};
|
|
6
|
+
states.set(registration, values);
|
|
7
|
+
Object.defineProperties(registration, {
|
|
8
|
+
removed: { enumerable: true, get: () => values.removed },
|
|
9
|
+
ready: { enumerable: true, get: () => values.ready },
|
|
10
|
+
});
|
|
11
|
+
return registration;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function setRegistrationReady(registration, value) {
|
|
15
|
+
const values = states.get(registration);
|
|
16
|
+
if (values) values.ready = value;
|
|
17
|
+
else registration.ready = value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function setRegistrationRemoved(registration, value) {
|
|
21
|
+
const values = states.get(registration);
|
|
22
|
+
if (values) values.removed = value;
|
|
23
|
+
else registration.removed = value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
Object.assign(createRegistrationState, { setRegistrationReady, setRegistrationRemoved });
|
|
27
|
+
|
|
28
|
+
export default createRegistrationState;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import logger from "../log/index.mjs";
|
|
2
|
+
import { defaultEvents } from "./constants.mjs";
|
|
3
|
+
import normalizeEvents from "./validation/events.mjs";
|
|
4
|
+
import validateProcessObject from "./validation/process-object.mjs";
|
|
5
|
+
import validateLogger from "./validation/logger.mjs";
|
|
6
|
+
import validateSignal from "./validation/signal.mjs";
|
|
7
|
+
import createCleanup from "./cleanup.mjs";
|
|
8
|
+
import createEventHandlers from "./event-handlers.mjs";
|
|
9
|
+
import createRegistrationLifecycle from "./registration/lifecycle.mjs";
|
|
10
|
+
import createRegistrationState from "./registration/state.mjs";
|
|
11
|
+
import createEventState from "./registration/events.mjs";
|
|
12
|
+
import createRegistrationSetupState from "./registration/setup-state.mjs";
|
|
13
|
+
import installHandlers from "./setup.mjs";
|
|
14
|
+
import finalizeRegistration from "./finalize.mjs";
|
|
15
|
+
|
|
16
|
+
const registrations = createRegistrationLifecycle();
|
|
17
|
+
|
|
18
|
+
function registerHandlers({
|
|
19
|
+
processObj = process,
|
|
20
|
+
log = logger,
|
|
21
|
+
events,
|
|
22
|
+
once = false,
|
|
23
|
+
signal,
|
|
24
|
+
} = {}) {
|
|
25
|
+
const selected = normalizeEvents(events);
|
|
26
|
+
const remove = validateProcessObject(processObj);
|
|
27
|
+
validateLogger(log);
|
|
28
|
+
validateSignal(signal);
|
|
29
|
+
const existing = registrations.get(processObj);
|
|
30
|
+
if (existing) {
|
|
31
|
+
if (!existing.ready) throw new Error("registration setup is incomplete");
|
|
32
|
+
return existing;
|
|
33
|
+
}
|
|
34
|
+
const add = once && typeof processObj.once === "function" ? "once" : "on";
|
|
35
|
+
const oneShot = once;
|
|
36
|
+
const registration = createRegistrationState();
|
|
37
|
+
const eventState = createEventState();
|
|
38
|
+
const setupState = createRegistrationSetupState();
|
|
39
|
+
const { fired, detachedEvents, addedEvents } = eventState;
|
|
40
|
+
const handlers = createEventHandlers({
|
|
41
|
+
selected,
|
|
42
|
+
log,
|
|
43
|
+
oneShot,
|
|
44
|
+
add,
|
|
45
|
+
registration,
|
|
46
|
+
eventState,
|
|
47
|
+
setupState,
|
|
48
|
+
});
|
|
49
|
+
registrations.set(processObj, registration);
|
|
50
|
+
const cleanup = createCleanup({
|
|
51
|
+
processObj,
|
|
52
|
+
remove,
|
|
53
|
+
selected,
|
|
54
|
+
handlers,
|
|
55
|
+
signal,
|
|
56
|
+
registrations,
|
|
57
|
+
registration,
|
|
58
|
+
detachedEvents,
|
|
59
|
+
});
|
|
60
|
+
registration.removeHandlers = cleanup.removeHandlers;
|
|
61
|
+
installHandlers({
|
|
62
|
+
processObj,
|
|
63
|
+
add,
|
|
64
|
+
remove,
|
|
65
|
+
selected,
|
|
66
|
+
handlers,
|
|
67
|
+
detachedEvents,
|
|
68
|
+
addedEvents,
|
|
69
|
+
registration,
|
|
70
|
+
registrations,
|
|
71
|
+
fired,
|
|
72
|
+
oneShot,
|
|
73
|
+
isCleanupPending: setupState.isCleanupPending,
|
|
74
|
+
markSetupComplete: setupState.markSetupComplete,
|
|
75
|
+
});
|
|
76
|
+
return finalizeRegistration({ signal, log, registration, registrations, processObj });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { defaultEvents };
|
|
80
|
+
export default registerHandlers;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import rollbackRegistration from "./rollback.mjs";
|
|
2
|
+
import createRegistrationState from "../registration/state.mjs";
|
|
3
|
+
|
|
4
|
+
function installWithRollback({
|
|
5
|
+
processObj,
|
|
6
|
+
add,
|
|
7
|
+
remove,
|
|
8
|
+
selected,
|
|
9
|
+
handlers,
|
|
10
|
+
detachedEvents,
|
|
11
|
+
addedEvents,
|
|
12
|
+
registration,
|
|
13
|
+
registrations,
|
|
14
|
+
fired,
|
|
15
|
+
oneShot,
|
|
16
|
+
isCleanupPending,
|
|
17
|
+
markSetupComplete,
|
|
18
|
+
}) {
|
|
19
|
+
const attachedEvents = new Set();
|
|
20
|
+
try {
|
|
21
|
+
for (const event of selected) {
|
|
22
|
+
let added;
|
|
23
|
+
try {
|
|
24
|
+
added = processObj[add](event, handlers[event]);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
attachedEvents.add(event);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
if (added === false) throw new Error(`processObj rejected listener for ${event}`);
|
|
30
|
+
addedEvents.add(event);
|
|
31
|
+
attachedEvents.add(event);
|
|
32
|
+
}
|
|
33
|
+
markSetupComplete();
|
|
34
|
+
if (isCleanupPending() || (oneShot && fired.size === selected.length))
|
|
35
|
+
registration.removeHandlers();
|
|
36
|
+
createRegistrationState.setRegistrationReady(registration, true);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
rollbackRegistration({
|
|
39
|
+
error,
|
|
40
|
+
attachedEvents,
|
|
41
|
+
processObj,
|
|
42
|
+
remove,
|
|
43
|
+
handlers,
|
|
44
|
+
detachedEvents,
|
|
45
|
+
registration,
|
|
46
|
+
registrations,
|
|
47
|
+
});
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default installWithRollback;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import attachFailure from "../failure.mjs";
|
|
2
|
+
import removeAttachedHandlers from "../cleanup/attached-handlers.mjs";
|
|
3
|
+
import createRegistrationState from "../registration/state.mjs";
|
|
4
|
+
|
|
5
|
+
function rollbackRegistration({
|
|
6
|
+
error,
|
|
7
|
+
attachedEvents,
|
|
8
|
+
processObj,
|
|
9
|
+
remove,
|
|
10
|
+
handlers,
|
|
11
|
+
detachedEvents = new Set(),
|
|
12
|
+
registration,
|
|
13
|
+
registrations,
|
|
14
|
+
}) {
|
|
15
|
+
const { removalError: rollbackError, hasRemovalError: rollbackFailed } = removeAttachedHandlers({
|
|
16
|
+
processObj,
|
|
17
|
+
remove,
|
|
18
|
+
events: attachedEvents,
|
|
19
|
+
handlers,
|
|
20
|
+
shouldSkip: () => false,
|
|
21
|
+
markRemoved: (event) => detachedEvents.add(event),
|
|
22
|
+
});
|
|
23
|
+
for (const event of Object.keys(handlers)) {
|
|
24
|
+
if (!attachedEvents.has(event)) detachedEvents.add(event);
|
|
25
|
+
}
|
|
26
|
+
if (rollbackFailed) {
|
|
27
|
+
createRegistrationState.setRegistrationReady(registration, true);
|
|
28
|
+
registrations.set(processObj, registration);
|
|
29
|
+
attachFailure(error, "rollbackError", rollbackError);
|
|
30
|
+
attachFailure(error, "registration", registration);
|
|
31
|
+
} else registrations.delete(processObj);
|
|
32
|
+
return rollbackError;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default rollbackRegistration;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defaultEvents } from "../constants.mjs";
|
|
2
|
+
|
|
3
|
+
function normalizeEvents(events) {
|
|
4
|
+
if (events === undefined) return defaultEvents;
|
|
5
|
+
if (!Array.isArray(events)) throw new TypeError("events must be an array");
|
|
6
|
+
const requested = [...new Set(events)];
|
|
7
|
+
if (!requested.length)
|
|
8
|
+
throw new RangeError("events must include at least one supported process event");
|
|
9
|
+
const unsupported = requested.filter((event) => !defaultEvents.includes(event));
|
|
10
|
+
if (unsupported.length === requested.length)
|
|
11
|
+
throw new RangeError("events must include at least one supported process event");
|
|
12
|
+
if (unsupported.length)
|
|
13
|
+
throw new RangeError(`Unsupported process event(s): ${unsupported.join(", ")}`);
|
|
14
|
+
return requested;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default normalizeEvents;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function validateProcessObject(processObj) {
|
|
2
|
+
if (typeof processObj.on !== "function")
|
|
3
|
+
throw new TypeError("processObj must provide an on method");
|
|
4
|
+
if (typeof processObj.off !== "function")
|
|
5
|
+
throw new TypeError("processObj must provide an off method");
|
|
6
|
+
if (typeof processObj.listeners !== "function")
|
|
7
|
+
throw new TypeError("processObj must provide a listeners method");
|
|
8
|
+
return "off";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default validateProcessObject;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function validateSignal(signal) {
|
|
2
|
+
if (!signal) return;
|
|
3
|
+
if (typeof signal.addEventListener !== "function")
|
|
4
|
+
throw new TypeError("signal must provide addEventListener");
|
|
5
|
+
if (typeof signal.removeEventListener !== "function")
|
|
6
|
+
throw new TypeError("signal must provide removeEventListener");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default validateSignal;
|
package/src/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
2
|
export { fs };
|
|
3
|
-
export { createLogger, default as log } from "
|
|
3
|
+
export { createLogger, default as log } from "./log/index.mjs";
|
|
4
4
|
export type {
|
|
5
5
|
LoggerWithConfiguredLevels,
|
|
6
6
|
RedactionOptions,
|
|
7
7
|
SafeSerializedValue,
|
|
8
|
-
} from "
|
|
8
|
+
} from "./log/index.mjs";
|
|
9
9
|
export {
|
|
10
10
|
path,
|
|
11
11
|
createPathHelpers,
|
|
@@ -15,15 +15,15 @@ export {
|
|
|
15
15
|
resolvePath,
|
|
16
16
|
relativePath,
|
|
17
17
|
fileUrlToPath,
|
|
18
|
-
} from "./path.mjs";
|
|
19
|
-
export type { PathLogger, PathHelpersOptions, PathHelpers } from "
|
|
18
|
+
} from "./path/index.mjs";
|
|
19
|
+
export type { PathLogger, PathHelpersOptions, PathHelpers } from "./path/index.mjs";
|
|
20
20
|
export {
|
|
21
21
|
createSnowflakeGenerator,
|
|
22
22
|
generate,
|
|
23
23
|
default as snowflake,
|
|
24
24
|
constants,
|
|
25
|
-
} from "
|
|
26
|
-
export type { SnowflakeGeneratorOptions, SnowflakeConstants } from "
|
|
25
|
+
} from "./snowflake/index.mjs";
|
|
26
|
+
export type { SnowflakeGeneratorOptions, SnowflakeConstants } from "./snowflake/index.mjs";
|
|
27
27
|
export {
|
|
28
28
|
createPolicy,
|
|
29
29
|
defaultPolicy,
|
|
@@ -32,16 +32,16 @@ export {
|
|
|
32
32
|
redactText,
|
|
33
33
|
replaceLiteralSecret,
|
|
34
34
|
safeSerialize,
|
|
35
|
-
} from "
|
|
35
|
+
} from "./redact/index.mjs";
|
|
36
36
|
export type {
|
|
37
37
|
RedactionPolicy,
|
|
38
38
|
SerializationOptions,
|
|
39
39
|
StructuredRedactionOptions,
|
|
40
40
|
TextRedactionOptions,
|
|
41
|
-
} from "
|
|
42
|
-
export { default as registerHandlers } from "./errors.mjs";
|
|
43
|
-
export { defaultEvents } from "./errors.mjs";
|
|
44
|
-
export { default as registerSignals } from "
|
|
41
|
+
} from "./redact/index.mjs";
|
|
42
|
+
export { default as registerHandlers } from "./errors/index.mjs";
|
|
43
|
+
export { defaultEvents } from "./errors/index.mjs";
|
|
44
|
+
export { default as registerSignals } from "./signals/index.mjs";
|
|
45
45
|
export type {
|
|
46
46
|
Logger,
|
|
47
47
|
ProcessEvent,
|
|
@@ -49,11 +49,11 @@ export type {
|
|
|
49
49
|
ProcessLike,
|
|
50
50
|
RegisterHandlersOptions,
|
|
51
51
|
RegisteredHandlers,
|
|
52
|
-
} from "
|
|
52
|
+
} from "./errors/index.mjs";
|
|
53
53
|
export type {
|
|
54
54
|
NodeSignal,
|
|
55
55
|
ProcessLike as SignalsProcessLike,
|
|
56
56
|
SignalsLogger,
|
|
57
57
|
RegisterSignalsOptions,
|
|
58
58
|
SignalsRegistration,
|
|
59
|
-
} from "
|
|
59
|
+
} from "./signals/index.mjs";
|
package/src/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
|
|
3
|
+
export { fs };
|
|
4
|
+
export { default as log, createLogger } from "./log/index.mjs";
|
|
3
5
|
export {
|
|
4
6
|
default as path,
|
|
5
7
|
createPathHelpers,
|
|
@@ -9,13 +11,13 @@ export {
|
|
|
9
11
|
resolvePath,
|
|
10
12
|
relativePath,
|
|
11
13
|
fileUrlToPath,
|
|
12
|
-
} from "./path.mjs";
|
|
14
|
+
} from "./path/index.mjs";
|
|
13
15
|
export {
|
|
14
16
|
default as snowflake,
|
|
15
17
|
createSnowflakeGenerator,
|
|
16
18
|
generate,
|
|
17
19
|
constants,
|
|
18
|
-
} from "
|
|
20
|
+
} from "./snowflake/index.mjs";
|
|
19
21
|
export {
|
|
20
22
|
createPolicy,
|
|
21
23
|
defaultPolicy,
|
|
@@ -24,6 +26,6 @@ export {
|
|
|
24
26
|
redactText,
|
|
25
27
|
replaceLiteralSecret,
|
|
26
28
|
safeSerialize,
|
|
27
|
-
} from "
|
|
28
|
-
export { defaultEvents, default as registerHandlers } from "./errors.mjs";
|
|
29
|
-
export { default as registerSignals } from "./signals.mjs";
|
|
29
|
+
} from "./redact/index.mjs";
|
|
30
|
+
export { defaultEvents, default as registerHandlers } from "./errors/index.mjs";
|
|
31
|
+
export { default as registerSignals } from "./signals/index.mjs";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
import jsonFormat from "../formats/json.mjs";
|
|
3
|
+
import textFormat from "../formats/text.mjs";
|
|
4
|
+
|
|
5
|
+
const buildLogger = ({ level, transports, format, timestamp, redact }) =>
|
|
6
|
+
winston.createLogger({
|
|
7
|
+
level,
|
|
8
|
+
format:
|
|
9
|
+
format === "json" ? jsonFormat(redact, timestamp) : winston.format.printf(textFormat(redact)),
|
|
10
|
+
transports,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export default buildLogger;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import createDefaultTransports from "./default-options.mjs";
|
|
2
|
+
|
|
3
|
+
const normalizeLoggerOptions = ({ level, transports, format, timestamp, keys }) => {
|
|
4
|
+
const configuredTransports = transports ?? createDefaultTransports();
|
|
5
|
+
return {
|
|
6
|
+
level,
|
|
7
|
+
transports: configuredTransports,
|
|
8
|
+
format,
|
|
9
|
+
timestamp,
|
|
10
|
+
redact: keys === undefined ? {} : { keys },
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default normalizeLoggerOptions;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
|
|
3
|
+
function validateLoggerOptions(options) {
|
|
4
|
+
const { level, transports, format, timestamp } = options;
|
|
5
|
+
const keys = Object.hasOwn(options, "keys") ? options.keys : options.redact?.keys;
|
|
6
|
+
if (format !== "text" && format !== "json") throw new TypeError("format must be text or json");
|
|
7
|
+
if (typeof timestamp !== "boolean") throw new TypeError("timestamp must be a boolean");
|
|
8
|
+
if (!Array.isArray(transports)) throw new TypeError("transports must be an array");
|
|
9
|
+
if (keys !== undefined && !Array.isArray(keys)) throw new TypeError("keys must be an array");
|
|
10
|
+
if (keys?.some((key) => typeof key !== "string"))
|
|
11
|
+
throw new TypeError("keys must contain only strings");
|
|
12
|
+
if (!Object.hasOwn(winston.config.npm.levels, level))
|
|
13
|
+
throw new TypeError(`Unknown logger level "${level}"`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default validateLoggerOptions;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import patchLoggerLevels from "./logger-levels.mjs";
|
|
2
|
+
import normalizeLoggerOptions from "./configuration/normalize-options.mjs";
|
|
3
|
+
import validateLoggerOptions from "./configuration/validate-options.mjs";
|
|
4
|
+
import buildLogger from "./configuration/build-logger.mjs";
|
|
5
|
+
|
|
6
|
+
const createLogger = ({
|
|
7
|
+
level = "info",
|
|
8
|
+
transports,
|
|
9
|
+
format = "text",
|
|
10
|
+
timestamp = false,
|
|
11
|
+
keys,
|
|
12
|
+
} = {}) => {
|
|
13
|
+
const options = normalizeLoggerOptions({ level, transports, format, timestamp, keys });
|
|
14
|
+
validateLoggerOptions(options);
|
|
15
|
+
return patchLoggerLevels(buildLogger(options));
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default createLogger;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { safeSerialize } from "../../redact/index.mjs";
|
|
2
|
+
|
|
3
|
+
function createJsonRecord(info, redact) {
|
|
4
|
+
const { level, message } = info;
|
|
5
|
+
const splatMetadata = (info[Symbol.for("splat")] ?? []).flatMap((entry) =>
|
|
6
|
+
entry && typeof entry === "object" ? Object.entries(entry) : [],
|
|
7
|
+
);
|
|
8
|
+
const infoMetadata = Object.entries(info).filter(([key]) => key !== "level" && key !== "message");
|
|
9
|
+
const metadata = Object.fromEntries([...splatMetadata, ...infoMetadata]);
|
|
10
|
+
const safeMessage = typeof message === "string" ? message : safeSerialize(message, redact);
|
|
11
|
+
const safeMetadata = safeSerialize(metadata, redact);
|
|
12
|
+
const hasReservedField =
|
|
13
|
+
Object.hasOwn(safeMetadata, "level") || Object.hasOwn(safeMetadata, "message");
|
|
14
|
+
return hasReservedField
|
|
15
|
+
? { level, message: safeMessage, metadata: safeMetadata }
|
|
16
|
+
: { level, message: safeMessage, ...safeMetadata };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default createJsonRecord;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
import { safeSerialize } from "../../redact/index.mjs";
|
|
3
|
+
import createJsonRecord from "./json-record.mjs";
|
|
4
|
+
|
|
5
|
+
const messageSymbol = Symbol.for("message");
|
|
6
|
+
const sanitizedSymbol = Symbol("sanitized");
|
|
7
|
+
|
|
8
|
+
const jsonFormat = (redact, timestamp) => {
|
|
9
|
+
// Sanitize eagerly so every emitted record has deterministic, transport-independent metadata.
|
|
10
|
+
const sanitize = winston.format((info) => {
|
|
11
|
+
info[sanitizedSymbol] = createJsonRecord(info, redact);
|
|
12
|
+
return info;
|
|
13
|
+
});
|
|
14
|
+
const output = winston.format((info) => {
|
|
15
|
+
try {
|
|
16
|
+
const record = info[sanitizedSymbol];
|
|
17
|
+
info[messageSymbol] = JSON.stringify(record);
|
|
18
|
+
} catch {
|
|
19
|
+
const fallback = {
|
|
20
|
+
level: info.level ?? "[Unserializable]",
|
|
21
|
+
message: safeSerialize(info.message, redact),
|
|
22
|
+
};
|
|
23
|
+
try {
|
|
24
|
+
info[messageSymbol] = JSON.stringify(fallback);
|
|
25
|
+
} catch {
|
|
26
|
+
info[messageSymbol] = '{"level":"[Unserializable]","message":"[Unserializable]"}';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return info;
|
|
30
|
+
});
|
|
31
|
+
return winston.format.combine(
|
|
32
|
+
...(timestamp ? [winston.format.timestamp()] : []),
|
|
33
|
+
sanitize(),
|
|
34
|
+
output(),
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export default jsonFormat;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { safeSerialize } from "../../redact/index.mjs";
|
|
2
|
+
|
|
3
|
+
function serializeMessage(message, redact) {
|
|
4
|
+
if (typeof message === "string") return message;
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
return JSON.stringify(safeSerialize(message, redact));
|
|
8
|
+
} catch {
|
|
9
|
+
return "[Unserializable]";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default serializeMessage;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { safeSerialize } from "../../redact/index.mjs";
|
|
2
|
+
import serializeMessage from "./message.mjs";
|
|
3
|
+
|
|
4
|
+
const textFormat =
|
|
5
|
+
(redact) =>
|
|
6
|
+
({ level, message, ...meta }) => {
|
|
7
|
+
const messageText = serializeMessage(message, redact);
|
|
8
|
+
let output = `[${typeof level === "string" ? level.toUpperCase() : "INFO"}] ${messageText}`;
|
|
9
|
+
const safeMeta = safeSerialize(meta, redact);
|
|
10
|
+
const keys = Object.keys(safeMeta);
|
|
11
|
+
if (keys.length > 0) {
|
|
12
|
+
try {
|
|
13
|
+
output += " " + JSON.stringify(safeMeta);
|
|
14
|
+
} catch {
|
|
15
|
+
output += " [Unserializable]";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return output;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default textFormat;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { SerializationOptions as RedactSerializationOptions } from "../redact/index.mjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Factory to create a Winston logger instance.
|
|
5
|
+
* @param options Optional logger options.
|
|
6
|
+
* @param options.level Log level (default: 'info'; invalid values throw a TypeError)
|
|
7
|
+
* @param options.transports Array of winston transports (default: Console)
|
|
8
|
+
* @param options.timestamp Add a timestamp to JSON records. The value must be a boolean.
|
|
9
|
+
* @returns winston.Logger
|
|
10
|
+
*
|
|
11
|
+
* Note: All logger methods accept either (message, metaObject) or (message, primitive/array),
|
|
12
|
+
* where primitives/arrays are wrapped as { value: ... }.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createLogger(options?: {
|
|
15
|
+
level?: string;
|
|
16
|
+
transports?: import("winston").transport[];
|
|
17
|
+
format?: "text" | "json";
|
|
18
|
+
timestamp?: boolean;
|
|
19
|
+
keys?: string[];
|
|
20
|
+
}): import("winston").Logger & {
|
|
21
|
+
debug(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
22
|
+
info(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
23
|
+
warn(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
24
|
+
error(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
createPolicy,
|
|
29
|
+
defaultPolicy,
|
|
30
|
+
redactHeaders,
|
|
31
|
+
redactText,
|
|
32
|
+
redactValue,
|
|
33
|
+
replaceLiteralSecret,
|
|
34
|
+
} from "../redact/index.mjs";
|
|
35
|
+
|
|
36
|
+
/** Runtime-configured Winston levels are available dynamically; standard levels are typed explicitly above and custom levels remain runtime-discovered. */
|
|
37
|
+
export type LoggerWithConfiguredLevels = import("winston").Logger & { [level: string]: unknown };
|
|
38
|
+
|
|
39
|
+
/** Recursive JSON-safe result returned by safeSerialize. */
|
|
40
|
+
export type SafeSerializedValue =
|
|
41
|
+
null | boolean | number | string | SafeSerializedValue[] | { [key: string]: SafeSerializedValue };
|
|
42
|
+
|
|
43
|
+
export type {
|
|
44
|
+
RedactionPolicy,
|
|
45
|
+
SerializationOptions,
|
|
46
|
+
StructuredRedactionOptions,
|
|
47
|
+
TextRedactionOptions,
|
|
48
|
+
} from "../redact/index.mjs";
|
|
49
|
+
|
|
50
|
+
/** Options accepted by safeSerialize. This alias preserves the original @eliware/log type name. */
|
|
51
|
+
export type RedactionOptions = RedactSerializationOptions;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Safely serializes metadata without invoking toJSON.
|
|
55
|
+
* Error values include name, message, stack, and enumerable custom properties, subject to redaction and limits.
|
|
56
|
+
*/
|
|
57
|
+
export declare function safeSerialize(
|
|
58
|
+
value: unknown,
|
|
59
|
+
options?: RedactionOptions,
|
|
60
|
+
): SafeSerializedValue;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Default logger instance.
|
|
64
|
+
*
|
|
65
|
+
* All logger methods accept either (message, metaObject) or (message, primitive/array),
|
|
66
|
+
* where primitives/arrays are wrapped as { value: ... }.
|
|
67
|
+
*/
|
|
68
|
+
export declare const log: import("winston").Logger & {
|
|
69
|
+
debug(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
70
|
+
info(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
71
|
+
warn(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
72
|
+
error(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
|
|
73
|
+
} & LoggerWithConfiguredLevels;
|
|
74
|
+
export default log;
|