@darthcav/ts-utils 0.10.10 → 0.12.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/README.md +170 -39
- package/dist/index.d.ts +16 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/loggers/getConsoleLogger.d.ts +10 -4
- package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
- package/dist/loggers/getConsoleLogger.js +36 -9
- package/dist/loggers/getConsoleLogger.js.map +1 -1
- package/dist/loggers/getDummyLogger.d.ts +14 -3
- package/dist/loggers/getDummyLogger.d.ts.map +1 -1
- package/dist/loggers/getDummyLogger.js +29 -11
- package/dist/loggers/getDummyLogger.js.map +1 -1
- package/dist/main.d.ts +83 -51
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +200 -40
- package/dist/main.js.map +1 -1
- package/dist/millisecondsToString.d.ts +1 -1
- package/dist/millisecondsToString.d.ts.map +1 -1
- package/dist/millisecondsToString.js +1 -1
- package/dist/millisecondsToString.js.map +1 -1
- package/dist/monitorMemory.d.ts +25 -7
- package/dist/monitorMemory.d.ts.map +1 -1
- package/dist/monitorMemory.js +52 -14
- package/dist/monitorMemory.js.map +1 -1
- package/dist/noop.d.ts +1 -2
- package/dist/noop.d.ts.map +1 -1
- package/dist/noop.js +1 -2
- package/dist/noop.js.map +1 -1
- package/dist/{os-utils.d.ts → osRelease.d.ts} +11 -7
- package/dist/osRelease.d.ts.map +1 -0
- package/dist/osRelease.js +153 -0
- package/dist/osRelease.js.map +1 -0
- package/dist/type-testing/Assert.d.ts +23 -0
- package/dist/type-testing/Assert.d.ts.map +1 -0
- package/dist/type-testing/Assert.js +2 -0
- package/dist/type-testing/Assert.js.map +1 -0
- package/dist/type-testing/Equal.d.ts +25 -0
- package/dist/type-testing/Equal.d.ts.map +1 -0
- package/dist/type-testing/Equal.js +2 -0
- package/dist/type-testing/Equal.js.map +1 -0
- package/dist/type-testing/Extends.d.ts +22 -0
- package/dist/type-testing/Extends.d.ts.map +1 -0
- package/dist/type-testing/Extends.js +2 -0
- package/dist/type-testing/Extends.js.map +1 -0
- package/dist/type-testing/IsAny.d.ts +17 -0
- package/dist/type-testing/IsAny.d.ts.map +1 -0
- package/dist/type-testing/IsAny.js +2 -0
- package/dist/type-testing/IsAny.js.map +1 -0
- package/dist/type-testing/IsNever.d.ts +17 -0
- package/dist/type-testing/IsNever.d.ts.map +1 -0
- package/dist/type-testing/IsNever.js +2 -0
- package/dist/type-testing/IsNever.js.map +1 -0
- package/dist/type-testing/IsUnknown.d.ts +20 -0
- package/dist/type-testing/IsUnknown.d.ts.map +1 -0
- package/dist/type-testing/IsUnknown.js +2 -0
- package/dist/type-testing/IsUnknown.js.map +1 -0
- package/dist/type-testing/Not.d.ts +17 -0
- package/dist/type-testing/Not.d.ts.map +1 -0
- package/dist/type-testing/Not.js +2 -0
- package/dist/type-testing/Not.js.map +1 -0
- package/dist/types.d.ts +42 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +61 -16
- package/dist/types.js.map +1 -1
- package/package.json +10 -12
- package/src/index.ts +26 -7
- package/src/loggers/getConsoleLogger.ts +41 -8
- package/src/loggers/getDummyLogger.ts +31 -11
- package/src/main.ts +249 -126
- package/src/millisecondsToString.ts +1 -4
- package/src/monitorMemory.ts +58 -19
- package/src/noop.ts +1 -3
- package/src/osRelease.ts +172 -0
- package/src/type-testing/Assert.ts +22 -0
- package/src/type-testing/Equal.ts +27 -0
- package/src/type-testing/Extends.ts +21 -0
- package/src/type-testing/IsAny.ts +16 -0
- package/src/type-testing/IsNever.ts +16 -0
- package/src/type-testing/IsUnknown.ts +21 -0
- package/src/type-testing/Not.ts +16 -0
- package/src/types.ts +65 -17
- package/dist/os-utils.d.ts.map +0 -1
- package/dist/os-utils.js +0 -94
- package/dist/os-utils.js.map +0 -1
- package/src/os-utils.ts +0 -110
package/README.md
CHANGED
|
@@ -29,18 +29,25 @@ npm install @darthcav/ts-utils
|
|
|
29
29
|
### `getConsoleLogger`
|
|
30
30
|
|
|
31
31
|
Configures logging and returns a `Logger` for the given category name. Records at or above
|
|
32
|
-
`lowestLevel` are written to the console
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
`lowestLevel` are written to the console with RFC 3339 timestamps. The internal `logtape/meta`
|
|
33
|
+
logger is silenced. The function is safe to call more than once — each call reconfigures logtape
|
|
34
|
+
from scratch (the most recent call wins).
|
|
35
|
+
|
|
36
|
+
ANSI colors are used only when both stdout and stderr are terminals that support them (respecting
|
|
37
|
+
`NO_COLOR`, `NODE_DISABLE_COLORS`, and `TERM=dumb`), so logs redirected to files or pipes stay free
|
|
38
|
+
of escape codes. Set `FORCE_COLOR` to override: `0` or `false` disables colors, any other value
|
|
39
|
+
enables them.
|
|
35
40
|
|
|
36
41
|
```ts
|
|
37
42
|
import { getConsoleLogger, main } from "@darthcav/ts-utils"
|
|
38
43
|
|
|
39
44
|
const logger = await getConsoleLogger("my-app")
|
|
40
45
|
|
|
41
|
-
main("my-app", logger,
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
main("my-app", logger, {
|
|
47
|
+
launcher: () => {
|
|
48
|
+
logger.info(`Application is running`)
|
|
49
|
+
// start servers, connect to databases, etc.
|
|
50
|
+
},
|
|
44
51
|
})
|
|
45
52
|
```
|
|
46
53
|
|
|
@@ -53,16 +60,27 @@ const logger = await getConsoleLogger("my-app", "debug")
|
|
|
53
60
|
### `monitorMemory`
|
|
54
61
|
|
|
55
62
|
Starts a periodic interval that logs process uptime and memory usage (in bytes) through a
|
|
56
|
-
`"monitorMemory"` child category of the given logger. The interval defaults to every 24 hours
|
|
63
|
+
`"monitorMemory"` child category of the given logger. The interval defaults to every 24 hours and
|
|
64
|
+
must be between one minute (`1 / 60`) and about 596.5 hours (2³¹ − 1 ms, the longest delay
|
|
65
|
+
`setInterval` supports); anything outside that range throws a `RangeError`.
|
|
66
|
+
|
|
67
|
+
It returns a function that stops the monitoring. The timer is unreferenced, so monitoring alone does
|
|
68
|
+
not keep the process alive. Besides the human-readable message, each report attaches its figures as
|
|
69
|
+
structured properties (`uptime` and `uptimeMs`; `rss`, `heapTotal`, `heapUsed`, and `external`), so
|
|
70
|
+
sinks that keep properties, such as JSON Lines, can query them.
|
|
57
71
|
|
|
58
72
|
```ts
|
|
59
73
|
import { getConsoleLogger, main, monitorMemory } from "@darthcav/ts-utils"
|
|
60
74
|
|
|
61
75
|
const logger = await getConsoleLogger("my-app")
|
|
62
76
|
|
|
63
|
-
main("my-app", logger,
|
|
64
|
-
|
|
65
|
-
|
|
77
|
+
main("my-app", logger, {
|
|
78
|
+
launcher: () => {
|
|
79
|
+
monitorMemory(logger) // every 24 hours
|
|
80
|
+
const stop = monitorMemory(logger, 1) // every hour
|
|
81
|
+
// later, e.g. during shutdown:
|
|
82
|
+
stop()
|
|
83
|
+
},
|
|
66
84
|
})
|
|
67
85
|
```
|
|
68
86
|
|
|
@@ -112,14 +130,50 @@ const payload: RuntimeObject = {
|
|
|
112
130
|
}
|
|
113
131
|
```
|
|
114
132
|
|
|
133
|
+
### Type guards and narrowing helpers
|
|
134
|
+
|
|
135
|
+
Narrow `unknown` values (e.g. parsed JSON) without type assertions:
|
|
136
|
+
|
|
137
|
+
- `isRuntimeObject` / `isString` are type guards for use in conditions.
|
|
138
|
+
- `asRuntimeObject` / `asString` return the value, or `undefined` when it does not match, for use
|
|
139
|
+
with `?.` and `??`.
|
|
140
|
+
- `toRuntimeObjectArray` keeps only the plain-object elements of an array.
|
|
141
|
+
|
|
142
|
+
A runtime object is a plain object (an object literal, a `JSON.parse` result, or an
|
|
143
|
+
`Object.create(null)` object); arrays, class instances, and built-ins such as `Date` or `Map` are
|
|
144
|
+
rejected. Keys are not sanitized: an own `__proto__` or `constructor` key from untrusted JSON is
|
|
145
|
+
kept, so guard against those keys before merging the value into other objects.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import {
|
|
149
|
+
asRuntimeObject,
|
|
150
|
+
asString,
|
|
151
|
+
isRuntimeObject,
|
|
152
|
+
isString,
|
|
153
|
+
toRuntimeObjectArray,
|
|
154
|
+
} from "@darthcav/ts-utils"
|
|
155
|
+
|
|
156
|
+
const body: unknown = JSON.parse(input)
|
|
157
|
+
|
|
158
|
+
if (isRuntimeObject(body) && isString(body["name"])) {
|
|
159
|
+
console.log(body["name"].toUpperCase()) // narrowed to string
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const name = asString(asRuntimeObject(body)?.["name"]) ?? "anonymous"
|
|
163
|
+
const items = toRuntimeObjectArray(asRuntimeObject(body)?.["items"])
|
|
164
|
+
```
|
|
165
|
+
|
|
115
166
|
### `osRelease`
|
|
116
167
|
|
|
117
168
|
Returns OS release information for the current platform as an `OsRelease` object, or `null` on
|
|
118
|
-
unsupported platforms or when
|
|
169
|
+
unsupported platforms or when no os-release file can be read on Linux (it never throws for a missing
|
|
170
|
+
or unreadable file).
|
|
119
171
|
|
|
120
172
|
The `OsRelease` type exposes three normalized fields — `name`, `version`, and `arch` — present on
|
|
121
|
-
all platforms. On Linux,
|
|
122
|
-
`ID_LIKE`) are also
|
|
173
|
+
all platforms. On Linux, the data comes from `/etc/os-release`, falling back to
|
|
174
|
+
`/usr/lib/os-release`, and all raw key-value pairs (e.g. `PRETTY_NAME`, `ID`, `ID_LIKE`) are also
|
|
175
|
+
accessible by string index. Comment lines are ignored, single- and double-quoted values are
|
|
176
|
+
unquoted, and keys that are not upper-case shell variable names (such as `__proto__`) are skipped.
|
|
123
177
|
|
|
124
178
|
```ts
|
|
125
179
|
import { osRelease } from "@darthcav/ts-utils"
|
|
@@ -135,8 +189,9 @@ if (info) {
|
|
|
135
189
|
|
|
136
190
|
### `getDummyLogger`
|
|
137
191
|
|
|
138
|
-
Returns a no-op `Logger` useful as a placeholder in tests. All logging methods are
|
|
139
|
-
`isEnabledFor` always returns `false`. `getChild` and `with` return the same dummy logger
|
|
192
|
+
Returns a no-op `Logger` (synchronously) useful as a placeholder in tests. All logging methods are
|
|
193
|
+
no-ops and `isEnabledFor` always returns `false`. `getChild` and `with` return the same dummy logger
|
|
194
|
+
instance. Each call returns a new instance, so mocking a method on one does not affect others.
|
|
140
195
|
|
|
141
196
|
```ts
|
|
142
197
|
import { getDummyLogger } from "@darthcav/ts-utils"
|
|
@@ -148,14 +203,36 @@ const logger = getDummyLogger()
|
|
|
148
203
|
### `main`
|
|
149
204
|
|
|
150
205
|
Bootstraps an application process: logs startup information, optionally registers handlers for
|
|
151
|
-
`SIGINT` and `SIGTERM
|
|
152
|
-
|
|
153
|
-
|
|
206
|
+
`SIGINT` and `SIGTERM`, always registers handlers for `uncaughtException` and `unhandledRejection`,
|
|
207
|
+
optionally starts memory monitoring, then runs an optional launcher function.
|
|
208
|
+
|
|
209
|
+
The process title, PID, name, and `NODE_ENV` are logged at `info` level. The Node.js process options
|
|
210
|
+
(`execArgv` and `NODE_OPTIONS`) are logged at `debug` level only, since they can reveal sensitive
|
|
211
|
+
flags such as `--inspect=0.0.0.0`.
|
|
212
|
+
|
|
213
|
+
Before exiting, `main` flushes and disposes the configured logtape sinks (for at most
|
|
214
|
+
`flushTimeoutMs`, 3 seconds by default), so buffered or asynchronous sinks keep the final messages.
|
|
215
|
+
On `SIGINT`/`SIGTERM` it logs the signal at `info` level and then re-raises it, so the process ends
|
|
216
|
+
as killed by that signal (exit status 130/143), which shells and supervisors such as systemd treat
|
|
217
|
+
as a clean stop. Uncaught exceptions, unhandled rejections, and launcher failures exit with status
|
|
218
|
+
`1`. A second signal or error during the flush exits immediately.
|
|
219
|
+
|
|
220
|
+
Optional settings are passed as a `MainOptions` object:
|
|
221
|
+
|
|
222
|
+
| Option | Type | Default | Description |
|
|
223
|
+
| ---------------------------- | ----------------------------- | ------- | ---------------------------------------------------------- |
|
|
224
|
+
| `launcher` | `() => void \| Promise<void>` | — | Runs after the handlers are registered; may be async. |
|
|
225
|
+
| `monitorMemoryHours` | `number` | `0` | Logs memory usage every N hours; `0` disables it. |
|
|
226
|
+
| `defaultInterruptionHandler` | `boolean` | `true` | Register the `SIGINT`/`SIGTERM` handlers. |
|
|
227
|
+
| `flushTimeoutMs` | `number` | `3000` | Longest wait for the log sinks to flush (0 to 2³¹ − 1 ms). |
|
|
154
228
|
|
|
155
|
-
|
|
156
|
-
`
|
|
157
|
-
|
|
158
|
-
|
|
229
|
+
An out-of-range `monitorMemoryHours` (negative, `NaN`, or outside `monitorMemory`'s range) or
|
|
230
|
+
`flushTimeoutMs` throws a `RangeError` before any handler is registered. If the launcher throws or
|
|
231
|
+
its promise rejects, the failure is logged and the process exits with status `1`.
|
|
232
|
+
|
|
233
|
+
`main` returns a teardown function that removes the handlers it registered and stops the memory
|
|
234
|
+
monitoring (it does not exit the process). Only one `main` call can be active at a time: calling it
|
|
235
|
+
again before the teardown throws, instead of registering a second set of handlers.
|
|
159
236
|
|
|
160
237
|
```ts
|
|
161
238
|
import { getLogger } from "@logtape/logtape"
|
|
@@ -163,14 +240,64 @@ import { main } from "@darthcav/ts-utils"
|
|
|
163
240
|
|
|
164
241
|
const logger = getLogger(["my-app"])
|
|
165
242
|
|
|
166
|
-
main(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
main("my-app", logger
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
main("my-app", logger,
|
|
173
|
-
|
|
243
|
+
// Call main() once per process. Alternatives:
|
|
244
|
+
|
|
245
|
+
// 1. All defaults:
|
|
246
|
+
main("my-app", logger)
|
|
247
|
+
|
|
248
|
+
// 2. An async launcher and memory logging every 2 hours:
|
|
249
|
+
main("my-app", logger, {
|
|
250
|
+
launcher: async () => {
|
|
251
|
+
await connectToDatabase()
|
|
252
|
+
startServer()
|
|
253
|
+
},
|
|
254
|
+
monitorMemoryHours: 2,
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
// 3. Manage graceful shutdown in the application instead:
|
|
258
|
+
const teardown = main("my-app", logger, {
|
|
259
|
+
launcher: () => startServer(),
|
|
260
|
+
defaultInterruptionHandler: false,
|
|
261
|
+
})
|
|
262
|
+
process.once("SIGTERM", async () => {
|
|
263
|
+
await server.close()
|
|
264
|
+
teardown()
|
|
265
|
+
})
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
The positional forms of earlier versions (e.g. `main("my-app", logger, () => startServer(), 2)`)
|
|
269
|
+
were removed in 0.11.0; passing anything other than an options object throws a `TypeError`.
|
|
270
|
+
|
|
271
|
+
### Type testing
|
|
272
|
+
|
|
273
|
+
Type-level helpers to check types at compile time, e.g. in test files. They are types only, so they
|
|
274
|
+
have no runtime cost and add no dependency to your tests:
|
|
275
|
+
|
|
276
|
+
- `Assert<T>` type-checks only when `T` is exactly `true`.
|
|
277
|
+
- `Equal<X, Y>` is `true` when `X` and `Y` are identical types. It is stricter than mutual
|
|
278
|
+
assignability: `any` only equals `any`, and `readonly` and `?` modifiers must match.
|
|
279
|
+
- `Extends<X, Y>` is `true` when `X` is assignable to `Y` (unions are compared as a whole).
|
|
280
|
+
- `Not<T>` negates a predicate result.
|
|
281
|
+
- `IsAny<T>`, `IsNever<T>`, and `IsUnknown<T>` detect those special types.
|
|
282
|
+
|
|
283
|
+
Declare each assertion as an (unused) type alias; a failing one is reported by `tsc` (e.g.
|
|
284
|
+
`tsc --noEmit`), not by the test runner, since Node.js strips types without checking them. Use
|
|
285
|
+
`@ts-expect-error` to assert that an assertion fails.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
import {
|
|
289
|
+
type Assert,
|
|
290
|
+
asString,
|
|
291
|
+
type Equal,
|
|
292
|
+
type IsUnknown,
|
|
293
|
+
type Not,
|
|
294
|
+
} from "@darthcav/ts-utils"
|
|
295
|
+
|
|
296
|
+
type _AsString = Assert<Equal<ReturnType<typeof asString>, string | undefined>>
|
|
297
|
+
type _NotAny = Assert<Not<Equal<ReturnType<typeof asString>, any>>>
|
|
298
|
+
type _Parsed = Assert<IsUnknown<Parameters<typeof asString>[0]>>
|
|
299
|
+
// @ts-expect-error -- `string` is not `number`
|
|
300
|
+
type _Fails = Assert<Equal<string, number>>
|
|
174
301
|
```
|
|
175
302
|
|
|
176
303
|
For the full API reference see the [API Documentation][pages-url].
|
|
@@ -202,14 +329,18 @@ npm run doc
|
|
|
202
329
|
|
|
203
330
|
```
|
|
204
331
|
src/
|
|
205
|
-
index.ts
|
|
206
|
-
main.ts
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
332
|
+
index.ts # Public API entry point
|
|
333
|
+
main.ts # Process bootstrap (main)
|
|
334
|
+
millisecondsToString.ts # Duration formatting
|
|
335
|
+
monitorMemory.ts # Periodic memory reporting
|
|
336
|
+
noop.ts # No-op function
|
|
337
|
+
osRelease.ts # OS release utilities
|
|
338
|
+
types.ts # RuntimeObject type and type guards
|
|
339
|
+
loggers/ # Logger utilities
|
|
340
|
+
type-testing/ # Type-level assertion helpers
|
|
341
|
+
__tests__/ # Test files
|
|
342
|
+
dist/ # Compiled output (generated)
|
|
343
|
+
public/ # Documentation output (generated)
|
|
213
344
|
```
|
|
214
345
|
|
|
215
346
|
## License
|
|
@@ -218,7 +349,7 @@ public/ # Documentation output (generated)
|
|
|
218
349
|
|
|
219
350
|
[node-version]: https://img.shields.io/badge/node-%3E%3D26-orange.svg?style=flat-square
|
|
220
351
|
[node-url]: https://nodejs.org
|
|
221
|
-
[version-image]: https://img.shields.io/badge/version-0.
|
|
352
|
+
[version-image]: https://img.shields.io/badge/version-0.12.0-blue.svg?style=flat-square
|
|
222
353
|
[ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
|
|
223
354
|
[coverage-badge]:
|
|
224
355
|
https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
|
package/dist/index.d.ts
CHANGED
|
@@ -3,13 +3,20 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
|
-
import getConsoleLogger from "./loggers/getConsoleLogger.ts";
|
|
7
|
-
import getDummyLogger from "./loggers/getDummyLogger.ts";
|
|
8
|
-
import { type LauncherFunction, main } from "./main.ts";
|
|
9
|
-
import millisecondsToString from "./millisecondsToString.ts";
|
|
10
|
-
import monitorMemory from "./monitorMemory.ts";
|
|
11
|
-
import noop from "./noop.ts";
|
|
12
|
-
import { type OsRelease, osRelease } from "./
|
|
13
|
-
import
|
|
14
|
-
|
|
6
|
+
import { getConsoleLogger } from "./loggers/getConsoleLogger.ts";
|
|
7
|
+
import { getDummyLogger } from "./loggers/getDummyLogger.ts";
|
|
8
|
+
import { type LauncherFunction, type MainOptions, main } from "./main.ts";
|
|
9
|
+
import { millisecondsToString } from "./millisecondsToString.ts";
|
|
10
|
+
import { monitorMemory } from "./monitorMemory.ts";
|
|
11
|
+
import { noop } from "./noop.ts";
|
|
12
|
+
import { type OsRelease, osRelease } from "./osRelease.ts";
|
|
13
|
+
import type { Assert } from "./type-testing/Assert.ts";
|
|
14
|
+
import type { Equal } from "./type-testing/Equal.ts";
|
|
15
|
+
import type { Extends } from "./type-testing/Extends.ts";
|
|
16
|
+
import type { IsAny } from "./type-testing/IsAny.ts";
|
|
17
|
+
import type { IsNever } from "./type-testing/IsNever.ts";
|
|
18
|
+
import type { IsUnknown } from "./type-testing/IsUnknown.ts";
|
|
19
|
+
import type { Not } from "./type-testing/Not.ts";
|
|
20
|
+
import { asRuntimeObject, asString, isRuntimeObject, isString, type RuntimeObject, toRuntimeObjectArray } from "./types.ts";
|
|
21
|
+
export { type Assert, asRuntimeObject, asString, type Equal, type Extends, getConsoleLogger, getDummyLogger, type IsAny, type IsNever, type IsUnknown, isRuntimeObject, isString, type LauncherFunction, type MainOptions, main, millisecondsToString, monitorMemory, type Not, noop, type OsRelease, osRelease, type RuntimeObject, toRuntimeObjectArray, };
|
|
15
22
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,WAAW,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,KAAK,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACtD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EACH,eAAe,EACf,QAAQ,EACR,eAAe,EACf,QAAQ,EACR,KAAK,aAAa,EAClB,oBAAoB,EACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,EACH,KAAK,MAAM,EACX,eAAe,EACf,QAAQ,EACR,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,gBAAgB,EAChB,cAAc,EACd,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,SAAS,EACd,eAAe,EACf,QAAQ,EACR,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,KAAK,GAAG,EACR,IAAI,EACJ,KAAK,SAAS,EACd,SAAS,EACT,KAAK,aAAa,EAClB,oBAAoB,GACvB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
|
-
import getConsoleLogger from "./loggers/getConsoleLogger.js";
|
|
7
|
-
import getDummyLogger from "./loggers/getDummyLogger.js";
|
|
6
|
+
import { getConsoleLogger } from "./loggers/getConsoleLogger.js";
|
|
7
|
+
import { getDummyLogger } from "./loggers/getDummyLogger.js";
|
|
8
8
|
import { main } from "./main.js";
|
|
9
|
-
import millisecondsToString from "./millisecondsToString.js";
|
|
10
|
-
import monitorMemory from "./monitorMemory.js";
|
|
11
|
-
import noop from "./noop.js";
|
|
12
|
-
import { osRelease } from "./
|
|
13
|
-
import { asRuntimeObject, asString, toRuntimeObjectArray, } from "./types.js";
|
|
14
|
-
export { asRuntimeObject, asString, getConsoleLogger, getDummyLogger, main, millisecondsToString, monitorMemory, noop, osRelease, toRuntimeObjectArray, };
|
|
9
|
+
import { millisecondsToString } from "./millisecondsToString.js";
|
|
10
|
+
import { monitorMemory } from "./monitorMemory.js";
|
|
11
|
+
import { noop } from "./noop.js";
|
|
12
|
+
import { osRelease } from "./osRelease.js";
|
|
13
|
+
import { asRuntimeObject, asString, isRuntimeObject, isString, toRuntimeObjectArray, } from "./types.js";
|
|
14
|
+
export { asRuntimeObject, asString, getConsoleLogger, getDummyLogger, isRuntimeObject, isString, main, millisecondsToString, monitorMemory, noop, osRelease, toRuntimeObjectArray, };
|
|
15
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAA2C,IAAI,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAkB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAQ1D,OAAO,EACH,eAAe,EACf,QAAQ,EACR,eAAe,EACf,QAAQ,EAER,oBAAoB,GACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,EAEH,eAAe,EACf,QAAQ,EAGR,gBAAgB,EAChB,cAAc,EAId,eAAe,EACf,QAAQ,EAGR,IAAI,EACJ,oBAAoB,EACpB,aAAa,EAEb,IAAI,EAEJ,SAAS,EAET,oBAAoB,GACvB,CAAA"}
|
|
@@ -2,9 +2,15 @@ import { type Logger, type LogLevel } from "@logtape/logtape";
|
|
|
2
2
|
/**
|
|
3
3
|
* Configures logging and returns a {@link Logger} for the given category name.
|
|
4
4
|
*
|
|
5
|
-
* Records at or above `lowestLevel` are written to the console
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Records at or above `lowestLevel` are written to the console with RFC 3339
|
|
6
|
+
* timestamps and full level names. The internal `logtape/meta` logger is
|
|
7
|
+
* silenced.
|
|
8
|
+
*
|
|
9
|
+
* ANSI colors are used only when both stdout and stderr are terminals that
|
|
10
|
+
* support them (respecting `NO_COLOR`, `NODE_DISABLE_COLORS`, and `TERM=dumb`),
|
|
11
|
+
* so output redirected to files or pipes stays free of escape codes. Set
|
|
12
|
+
* `FORCE_COLOR` to override: `"0"` or `"false"` disables colors, any other
|
|
13
|
+
* value enables them. The decision is made when this function is called.
|
|
8
14
|
*
|
|
9
15
|
* Logging is configured with `reset: true`, so this function is safe to call
|
|
10
16
|
* more than once: each call reconfigures logtape from scratch and the most
|
|
@@ -15,5 +21,5 @@ import { type Logger, type LogLevel } from "@logtape/logtape";
|
|
|
15
21
|
* @param lowestLevel - The minimum log level to output. Defaults to `"info"`.
|
|
16
22
|
* @returns A promise resolving to a logger scoped to the given category.
|
|
17
23
|
*/
|
|
18
|
-
export
|
|
24
|
+
export declare function getConsoleLogger(name: string, lowestLevel?: LogLevel): Promise<Logger>;
|
|
19
25
|
//# sourceMappingURL=getConsoleLogger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getConsoleLogger.d.ts","sourceRoot":"","sources":["../../src/loggers/getConsoleLogger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getConsoleLogger.d.ts","sourceRoot":"","sources":["../../src/loggers/getConsoleLogger.ts"],"names":[],"mappings":"AACA,OAAO,EAMH,KAAK,MAAM,EACX,KAAK,QAAQ,EAEhB,MAAM,kBAAkB,CAAA;AA2BzB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,gBAAgB,CAClC,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,QAAiB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAoBjB"}
|
|
@@ -1,10 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import { configure, getAnsiColorFormatter, getConsoleSink, getLogger, getTextFormatter, } from "@logtape/logtape";
|
|
3
|
+
/** Formatter options shared by the colored and the plain formatter. */
|
|
4
|
+
const FORMATTER_OPTIONS = {
|
|
5
|
+
timestamp: "rfc3339",
|
|
6
|
+
level: "FULL",
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Decides whether console output should use ANSI colors.
|
|
10
|
+
*
|
|
11
|
+
* `FORCE_COLOR` wins when set (`"0"` or `"false"` disable colors, any other
|
|
12
|
+
* value enables them). Otherwise colors are used only when both stdout and
|
|
13
|
+
* stderr are terminals that support them, since the console sink writes
|
|
14
|
+
* warnings and errors to stderr. `hasColors()` also honors `NO_COLOR`,
|
|
15
|
+
* `NODE_DISABLE_COLORS`, and `TERM=dumb`.
|
|
16
|
+
*/
|
|
17
|
+
function useColors() {
|
|
18
|
+
const force = process.env["FORCE_COLOR"];
|
|
19
|
+
if (force !== undefined) {
|
|
20
|
+
return force !== "0" && force !== "false";
|
|
21
|
+
}
|
|
22
|
+
return [process.stdout, process.stderr].every((stream) => stream.isTTY === true && stream.hasColors());
|
|
23
|
+
}
|
|
2
24
|
/**
|
|
3
25
|
* Configures logging and returns a {@link Logger} for the given category name.
|
|
4
26
|
*
|
|
5
|
-
* Records at or above `lowestLevel` are written to the console
|
|
6
|
-
*
|
|
7
|
-
*
|
|
27
|
+
* Records at or above `lowestLevel` are written to the console with RFC 3339
|
|
28
|
+
* timestamps and full level names. The internal `logtape/meta` logger is
|
|
29
|
+
* silenced.
|
|
30
|
+
*
|
|
31
|
+
* ANSI colors are used only when both stdout and stderr are terminals that
|
|
32
|
+
* support them (respecting `NO_COLOR`, `NODE_DISABLE_COLORS`, and `TERM=dumb`),
|
|
33
|
+
* so output redirected to files or pipes stays free of escape codes. Set
|
|
34
|
+
* `FORCE_COLOR` to override: `"0"` or `"false"` disables colors, any other
|
|
35
|
+
* value enables them. The decision is made when this function is called.
|
|
8
36
|
*
|
|
9
37
|
* Logging is configured with `reset: true`, so this function is safe to call
|
|
10
38
|
* more than once: each call reconfigures logtape from scratch and the most
|
|
@@ -15,14 +43,13 @@ import { configure, getAnsiColorFormatter, getConsoleSink, getLogger, } from "@l
|
|
|
15
43
|
* @param lowestLevel - The minimum log level to output. Defaults to `"info"`.
|
|
16
44
|
* @returns A promise resolving to a logger scoped to the given category.
|
|
17
45
|
*/
|
|
18
|
-
export
|
|
46
|
+
export async function getConsoleLogger(name, lowestLevel = "info") {
|
|
19
47
|
await configure({
|
|
20
48
|
sinks: {
|
|
21
49
|
console: getConsoleSink({
|
|
22
|
-
formatter:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}),
|
|
50
|
+
formatter: useColors()
|
|
51
|
+
? getAnsiColorFormatter(FORMATTER_OPTIONS)
|
|
52
|
+
: getTextFormatter(FORMATTER_OPTIONS),
|
|
26
53
|
}),
|
|
27
54
|
},
|
|
28
55
|
loggers: [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getConsoleLogger.js","sourceRoot":"","sources":["../../src/loggers/getConsoleLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,SAAS,EACT,qBAAqB,EACrB,cAAc,EACd,SAAS,
|
|
1
|
+
{"version":3,"file":"getConsoleLogger.js","sourceRoot":"","sources":["../../src/loggers/getConsoleLogger.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAA;AAClC,OAAO,EACH,SAAS,EACT,qBAAqB,EACrB,cAAc,EACd,SAAS,EACT,gBAAgB,GAInB,MAAM,kBAAkB,CAAA;AAEzB,uEAAuE;AACvE,MAAM,iBAAiB,GAAyB;IAC5C,SAAS,EAAE,SAAS;IACpB,KAAK,EAAE,MAAM;CAChB,CAAA;AAED;;;;;;;;GAQG;AACH,SAAS,SAAS;IACd,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAA;IACxC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACtB,OAAO,KAAK,KAAK,GAAG,IAAI,KAAK,KAAK,OAAO,CAAA;IAC7C,CAAC;IACD,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,CACzC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,KAAK,IAAI,IAAI,MAAM,CAAC,SAAS,EAAE,CAC1D,CAAA;AACL,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAClC,IAAY,EACZ,cAAwB,MAAM;IAE9B,MAAM,SAAS,CAAC;QACZ,KAAK,EAAE;YACH,OAAO,EAAE,cAAc,CAAC;gBACpB,SAAS,EAAE,SAAS,EAAE;oBAClB,CAAC,CAAC,qBAAqB,CAAC,iBAAiB,CAAC;oBAC1C,CAAC,CAAC,gBAAgB,CAAC,iBAAiB,CAAC;aAC5C,CAAC;SACL;QACD,OAAO,EAAE;YACL,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,SAAS,CAAC,EAAE,WAAW,EAAE;YACjD;gBACI,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC;gBAC7B,KAAK,EAAE,EAAE;gBACT,WAAW,EAAE,UAAU;aAC1B;SACJ;QACD,KAAK,EAAE,IAAI;KACd,CAAC,CAAA;IACF,OAAO,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;AAC5B,CAAC"}
|
|
@@ -3,10 +3,21 @@ import type { Logger } from "@logtape/logtape";
|
|
|
3
3
|
* Returns a no-op {@link Logger} useful as a placeholder in tests.
|
|
4
4
|
*
|
|
5
5
|
* All logging methods (`trace`, `debug`, `info`, `warn`, `warning`, `error`,
|
|
6
|
-
* `fatal`) are no-ops
|
|
7
|
-
*
|
|
6
|
+
* `fatal`) are no-ops; the ones logtape types as returning a promise return an
|
|
7
|
+
* already-settled one. `isEnabledFor` always returns `false`. `getChild` and
|
|
8
|
+
* `with` return the same dummy logger instance.
|
|
9
|
+
*
|
|
10
|
+
* Each call returns a new instance, so mocking a method on one (e.g. with
|
|
11
|
+
* `mock.method(logger, "info")`) does not affect others.
|
|
8
12
|
*
|
|
9
13
|
* @returns A logger whose methods are all no-ops.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { getDummyLogger, main } from "@darthcav/ts-utils"
|
|
18
|
+
*
|
|
19
|
+
* main("test-app", getDummyLogger())
|
|
20
|
+
* ```
|
|
10
21
|
*/
|
|
11
|
-
export
|
|
22
|
+
export declare function getDummyLogger(): Logger;
|
|
12
23
|
//# sourceMappingURL=getDummyLogger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDummyLogger.d.ts","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAuB,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"getDummyLogger.d.ts","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAuB,MAAM,kBAAkB,CAAA;AAYnE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAiBvC"}
|
|
@@ -1,24 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Already-settled promise returned by every logging method. It satisfies the
|
|
3
|
+
* overloads that return `Promise<void>` (asynchronous lazy properties), just as
|
|
4
|
+
* a real logtape logger does for a disabled level.
|
|
5
|
+
*/
|
|
6
|
+
const settled = Promise.resolve();
|
|
7
|
+
/** A logging method that accepts every logtape overload and does nothing. */
|
|
8
|
+
const log = (..._args) => settled;
|
|
2
9
|
/**
|
|
3
10
|
* Returns a no-op {@link Logger} useful as a placeholder in tests.
|
|
4
11
|
*
|
|
5
12
|
* All logging methods (`trace`, `debug`, `info`, `warn`, `warning`, `error`,
|
|
6
|
-
* `fatal`) are no-ops
|
|
7
|
-
*
|
|
13
|
+
* `fatal`) are no-ops; the ones logtape types as returning a promise return an
|
|
14
|
+
* already-settled one. `isEnabledFor` always returns `false`. `getChild` and
|
|
15
|
+
* `with` return the same dummy logger instance.
|
|
16
|
+
*
|
|
17
|
+
* Each call returns a new instance, so mocking a method on one (e.g. with
|
|
18
|
+
* `mock.method(logger, "info")`) does not affect others.
|
|
8
19
|
*
|
|
9
20
|
* @returns A logger whose methods are all no-ops.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { getDummyLogger, main } from "@darthcav/ts-utils"
|
|
25
|
+
*
|
|
26
|
+
* main("test-app", getDummyLogger())
|
|
27
|
+
* ```
|
|
10
28
|
*/
|
|
11
|
-
export
|
|
29
|
+
export function getDummyLogger() {
|
|
12
30
|
const logger = {
|
|
13
31
|
category: [],
|
|
14
32
|
parent: null,
|
|
15
|
-
trace:
|
|
16
|
-
debug:
|
|
17
|
-
info:
|
|
18
|
-
warn:
|
|
19
|
-
warning:
|
|
20
|
-
error:
|
|
21
|
-
fatal:
|
|
33
|
+
trace: log,
|
|
34
|
+
debug: log,
|
|
35
|
+
info: log,
|
|
36
|
+
warn: log,
|
|
37
|
+
warning: log,
|
|
38
|
+
error: log,
|
|
39
|
+
fatal: log,
|
|
22
40
|
emit: (_record) => { },
|
|
23
41
|
getChild: () => logger,
|
|
24
42
|
isEnabledFor: (_level) => false,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDummyLogger.js","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getDummyLogger.js","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAA;AAEhD,6EAA6E;AAC7E,MAAM,GAAG,GAAG,CAAC,GAAG,KAAyB,EAAiB,EAAE,CAAC,OAAO,CAAA;AAEpE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,cAAc;IAC1B,MAAM,MAAM,GAAW;QACnB,QAAQ,EAAE,EAAE;QACZ,MAAM,EAAE,IAAI;QACZ,KAAK,EAAE,GAAG;QACV,KAAK,EAAE,GAAG;QACV,IAAI,EAAE,GAAG;QACT,IAAI,EAAE,GAAG;QACT,OAAO,EAAE,GAAG;QACZ,KAAK,EAAE,GAAG;QACV,KAAK,EAAE,GAAG;QACV,IAAI,EAAE,CAAC,OAAoC,EAAQ,EAAE,GAAE,CAAC;QACxD,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM;QACtB,YAAY,EAAE,CAAC,MAAgB,EAAW,EAAE,CAAC,KAAK;QAClD,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM;KACrB,CAAA;IACD,OAAO,MAAM,CAAA;AACjB,CAAC"}
|