@adviser/cement 0.2.34 → 0.2.36

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,7 @@ import {
9
9
  logValue,
10
10
  Serialized,
11
11
  WithLogger,
12
- removeSelfRef,
12
+ // sanitizeSerialize,
13
13
  Sized,
14
14
  Lengthed,
15
15
  LogValue,
@@ -67,7 +67,13 @@ export class JSONFormatter implements LogFormatter {
67
67
  this._space = space;
68
68
  }
69
69
  format(attr: LogSerializable): Uint8Array {
70
- return this._txtEnDe.encode(JSON.stringify(attr, removeSelfRef(), this._space) + "\n");
70
+ let ret: string;
71
+ try {
72
+ ret = JSON.stringify(attr, null, this._space);
73
+ } catch (e) {
74
+ ret = JSON.stringify({ internal: { message: (e as Error).message, stack: (e as Error).stack } });
75
+ }
76
+ return this._txtEnDe.encode(ret + "\n");
71
77
  }
72
78
  }
73
79
 
@@ -79,7 +85,7 @@ export class YAMLFormatter implements LogFormatter {
79
85
  this._space = space;
80
86
  }
81
87
  format(attr: LogSerializable): Uint8Array {
82
- return this._txtEnDe.encode("---\n" + YAML.stringify(attr, removeSelfRef(), this._space) + "\n");
88
+ return this._txtEnDe.encode("---\n" + YAML.stringify(attr, null, this._space) + "\n");
83
89
  }
84
90
  }
85
91
 
@@ -162,7 +168,7 @@ export class LoggerImpl implements Logger {
162
168
  }
163
169
 
164
170
  Attributes(): Record<string, unknown> {
165
- return JSON.parse(JSON.stringify(this._attributes, removeSelfRef()));
171
+ return JSON.parse(JSON.stringify(this._attributes, null));
166
172
  // return Array.from(Object.entries(this._attributes)).reduce(
167
173
  // (acc, [key, value]) => {
168
174
  // if (value instanceof LogValue) {
@@ -259,10 +265,43 @@ export class LoggerImpl implements Logger {
259
265
  }
260
266
  return this;
261
267
  }
262
- Bool(key: string, value: unknown): Logger {
263
- this._attributes[key] = logValue(!!value);
268
+ Bool(key: string | Record<string, unknown>, value: unknown): Logger {
269
+ this.coerceKey(key, !!value);
270
+ // this._attributes[key] = logValue(!!value);
271
+ return this;
272
+ }
273
+
274
+ Http(res: Response | Result<Response>, req?: Request, key?: string): Logger {
275
+ if (Result.Is(res)) {
276
+ if (res.isErr()) {
277
+ this.Err(res.Err());
278
+ return this;
279
+ }
280
+ res = res.Ok();
281
+ }
282
+ let reqRes: Response | { res: Response; req: Request } = res;
283
+ if (req) {
284
+ reqRes = { res, req };
285
+ }
286
+ this.Any(key || "Http", reqRes as unknown as LogSerializable);
287
+ return this;
288
+ }
289
+ Pair(x: Record<string, unknown>): Logger {
290
+ for (const key of Object.keys(x)) {
291
+ const value = x[key];
292
+ if (value instanceof LogValue) {
293
+ this._attributes[key] = value;
294
+ continue;
295
+ }
296
+ if (Result.Is(value)) {
297
+ this.Result(key, value);
298
+ continue;
299
+ }
300
+ this.Any(key, value as LogSerializable);
301
+ }
264
302
  return this;
265
303
  }
304
+
266
305
  Result<T>(key: string, res: Result<T, Error>): Logger {
267
306
  if (res.isOk()) {
268
307
  this._attributes[key] = logValue(res.Ok() as Serialized);
@@ -287,13 +326,22 @@ export class LoggerImpl implements Logger {
287
326
  return this;
288
327
  }
289
328
 
290
- Str(key: string, value?: string): Logger {
291
- this._attributes[key] = logValue(value);
329
+ private coerceKey(key: string | Record<string, unknown>, value?: unknown): void {
330
+ if (typeof key === "string") {
331
+ this._attributes[key] = logValue(value as LogValueArg);
332
+ } else {
333
+ this.Pair(key);
334
+ }
335
+ }
336
+
337
+ Str(key: string | Record<string, string>, value?: string): Logger {
338
+ this.coerceKey(key, value);
292
339
  return this;
293
340
  }
294
341
 
295
- Any(key: string, value?: string | number | boolean | LogSerializable): Logger {
296
- this._attributes[key] = logValue(value as LogValueArg);
342
+ Any(key: string | Record<string, unknown>, value?: unknown): Logger {
343
+ this.coerceKey(key, value);
344
+ //this._attributes[coerceKey(key)] = logValue(value as LogValueArg);
297
345
  return this;
298
346
  }
299
347
  Dur(key: string, nsec: number): Logger {
@@ -301,11 +349,12 @@ export class LoggerImpl implements Logger {
301
349
  // new Intl.DurationFormat("en", { style: "narrow" }).format(nsec);
302
350
  return this;
303
351
  }
304
- Uint64(key: string, value: number): Logger {
305
- this._attributes[key] = logValue(value);
352
+ Uint64(key: string | Record<string, number>, value?: number): Logger {
353
+ this.coerceKey(key, value);
354
+ //this._attributes[coerceKey(key)] = logValue(value);
306
355
  return this;
307
356
  }
308
- Int(key: string, value: number): Logger {
357
+ Int(key: string | Record<string, number>, value?: number): Logger {
309
358
  return this.Uint64(key, value);
310
359
  }
311
360
 
@@ -415,7 +464,16 @@ class WithLoggerBuilder implements WithLogger {
415
464
  return this;
416
465
  }
417
466
 
418
- Str(key: string, value?: string): WithLogger {
467
+ Http(res: Response | Result<Response>, req?: Request, key?: string): WithLogger {
468
+ this._li.Http(res, req, key);
469
+ return this;
470
+ }
471
+ Pair(x: Record<string, unknown>): WithLogger {
472
+ this._li.Pair(x);
473
+ return this;
474
+ }
475
+
476
+ Str(key: string | Record<string, string>, value?: string): WithLogger {
419
477
  this._li.Str(key, value);
420
478
  return this;
421
479
  }
@@ -434,7 +492,7 @@ class WithLoggerBuilder implements WithLogger {
434
492
  this._li.Ref(key, action);
435
493
  return this;
436
494
  }
437
- Bool(key: string, value: unknown): WithLogger {
495
+ Bool(key: string | Record<string, unknown>, value?: unknown): WithLogger {
438
496
  this._li.Bool(key, value);
439
497
  return this;
440
498
  }
@@ -446,7 +504,7 @@ class WithLoggerBuilder implements WithLogger {
446
504
  this._li.Url(url, key);
447
505
  return this;
448
506
  }
449
- Int(key: string, value: number): WithLogger {
507
+ Int(key: string | Record<string, number>, value?: number): WithLogger {
450
508
  this._li.Int(key, value);
451
509
  return this;
452
510
  }
@@ -485,7 +543,7 @@ class WithLoggerBuilder implements WithLogger {
485
543
  this._li.Timestamp();
486
544
  return this;
487
545
  }
488
- Any(key: string, value: LogSerializable): WithLogger {
546
+ Any(key: string | Record<string, unknown>, value?: unknown | LogSerializable): WithLogger {
489
547
  this._li.Any(key, value);
490
548
  return this;
491
549
  }
@@ -493,7 +551,7 @@ class WithLoggerBuilder implements WithLogger {
493
551
  this._li.Dur(key, nsec);
494
552
  return this;
495
553
  }
496
- Uint64(key: string, value: number): WithLogger {
554
+ Uint64(key: string | Record<string, number>, value?: number): WithLogger {
497
555
  this._li.Uint64(key, value);
498
556
  return this;
499
557
  }
package/src/logger.ts CHANGED
@@ -16,7 +16,12 @@ export type FnSerialized = () => Serialized | Serialized[];
16
16
  export class LogValue {
17
17
  constructor(readonly fn: FnSerialized) {}
18
18
  value(): Serialized | Serialized[] {
19
- return this.fn();
19
+ try {
20
+ // console.log("LogValue.value", this.fn.toString());
21
+ return this.fn();
22
+ } catch (e) {
23
+ return `LogValue:${(e as Error).message}`;
24
+ }
20
25
  }
21
26
  toJSON(): Serialized | Serialized[] {
22
27
  return this.value();
@@ -25,17 +30,17 @@ export class LogValue {
25
30
 
26
31
  export type LogSerializable = Record<string, LogValue | Promise<LogValue>>;
27
32
 
28
- export function removeSelfRef(lineEnd?: string): (key: unknown, val: unknown) => unknown {
29
- const cache = new Set();
30
- return function (key: unknown, value: unknown) {
31
- if (typeof value === "object" && value !== null) {
32
- // Duplicate reference found, discard key
33
- if (cache.has(value)) return "...";
34
- cache.add(value);
35
- }
36
- return lineEnd ? value + lineEnd : value;
37
- };
38
- }
33
+ // export function sanitizeSerialize(lineEnd?: string): (key: unknown, val: unknown) => unknown {
34
+ // const cache = new Set();
35
+ // return function (this: unknown, key: unknown, value: unknown) {
36
+ // if (typeof value === "object" && value !== null) {
37
+ // // Duplicate reference found, discard key
38
+ // if (cache.has(value)) return "...";
39
+ // cache.add(value);
40
+ // }
41
+ // return lineEnd ? value + lineEnd : value;
42
+ // };
43
+ // }
39
44
 
40
45
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
41
46
  export function asyncLogValue(val: () => Promise<Serialized>): Promise<LogValue> {
@@ -69,20 +74,39 @@ export function logValue(val: LogValueArg, state: Set<unknown> = new Set<unknown
69
74
  case "boolean":
70
75
  return new LogValue(() => val);
71
76
  case "object": {
77
+ if (val === null) {
78
+ return new LogValue(() => "null");
79
+ }
72
80
  if (ArrayBuffer.isView(val)) {
73
81
  return logValue(bin2string(val, 512));
74
82
  }
75
83
  if (Array.isArray(val)) {
76
- return new LogValue(() => val.map((v) => logValue(v).value() as Serialized));
84
+ return new LogValue(() => (val as Serialized[]).map((v) => logValue(v).value() as Serialized));
77
85
  }
78
- if (val === null) {
79
- return new LogValue(() => "null");
86
+ // if (val instanceof Response) {
87
+ // // my = my.clone() as unknown as LogValue | Serialized[] | null
88
+ // // const rval = my as unknown as Partial<Response>;
89
+ // // delete rval.clone
90
+ // // delete rval.blob
91
+ // }
92
+ if (val instanceof Headers) {
93
+ return new LogValue(() => Object.fromEntries(val.entries()) as unknown as Serialized);
94
+ }
95
+ if (val instanceof ReadableStream) {
96
+ return new LogValue(() => ">Stream<");
80
97
  }
98
+ if (val instanceof Promise) {
99
+ return new LogValue(() => ">Promise<");
100
+ }
101
+
81
102
  // Duplicate reference found, discard key
82
103
  if (state.has(val)) {
83
104
  return new LogValue(() => "...");
84
105
  }
85
106
  state.add(val);
107
+ if (typeof val.toJSON === "function") {
108
+ return new LogValue(() => val.toJSON());
109
+ }
86
110
 
87
111
  const res: Record<string, LogValue> = {};
88
112
  const typedVal = val as unknown as Record<string, LogValueArg>;
@@ -91,7 +115,10 @@ export function logValue(val: LogValueArg, state: Set<unknown> = new Set<unknown
91
115
  if (element instanceof LogValue) {
92
116
  res[key] = element;
93
117
  } else {
94
- res[key] = logValue(element, state);
118
+ if (typeof element !== "function") {
119
+ res[key] = logValue(element, state);
120
+ }
121
+ // res[key] = logValue(element, state);
95
122
  }
96
123
  }
97
124
  // ugly as hell cast but how declare a self-referencing type?
@@ -148,7 +175,15 @@ export interface LoggerInterface<R> {
148
175
 
149
176
  Hash(value: unknown, key?: string): R;
150
177
 
151
- Str(key: string, value?: string): R;
178
+ Str<T extends string | Record<string, string>>(key: T, value?: T extends string ? string : undefined): R;
179
+ Uint64<T extends string | Record<string, number>>(key: T, value?: T extends string ? number : undefined): R;
180
+ Int<T extends string | Record<string, number>>(key: T, value?: T extends string ? number : undefined): R;
181
+ Bool<T extends string | Record<string, unknown>>(key: T, value?: T extends string ? unknown : undefined): R;
182
+ Any<T extends string | Record<string, unknown>>(key: T, value?: T extends string ? unknown : undefined): R;
183
+
184
+ Http(res: Response | Result<Response>, req?: Request, key?: string): R;
185
+ Pair(x: Record<string, unknown>): R;
186
+
152
187
  Error(): R;
153
188
  Warn(): R;
154
189
  Debug(): R;
@@ -158,11 +193,7 @@ export interface LoggerInterface<R> {
158
193
  Err(err: unknown | Result<unknown> | Error): R; // could be Error, or something which coerces to string
159
194
  Info(): R;
160
195
  Timestamp(): R;
161
- Any(key: string, value: unknown): R;
162
196
  Dur(key: string, nsec: number): R;
163
- Uint64(key: string, value: number): R;
164
- Int(key: string, value: number): R;
165
- Bool(key: string, value: unknown): R;
166
197
  }
167
198
 
168
199
  export function IsLogger(obj: unknown): obj is Logger {
package/src/uri.ts CHANGED
@@ -3,6 +3,38 @@ import { StripCommand, stripper } from "./utils/stripper.js";
3
3
 
4
4
  type NullOrUndef = null | undefined;
5
5
 
6
+ type OneKey<K extends string, V = string> = Record<K, V>;
7
+
8
+ export interface URIInterface<R extends URIInterface<R>> {
9
+ // readonly hostname: string;
10
+ // readonly port: string;
11
+ // readonly host: string;
12
+ // readonly protocol: string;
13
+ // readonly pathname: string;
14
+ readonly getParams: Iterable<[string, string]>;
15
+
16
+ hasParam(key: string): boolean;
17
+ getParam<T extends string | undefined>(key: string | OneKey<string>, def?: T): T extends string ? string : string | undefined;
18
+ getParamResult(key: string, msgFn?: (key: string) => string): Result<string>;
19
+ getParamsResult(...keys: keysParam): Result<Record<string, string>>;
20
+ clone(): R;
21
+ asURL(): URL;
22
+ toString(): string;
23
+ toJSON(): string;
24
+ asObj(...strips: StripCommand[]): Partial<HostURIObject | PathURIObject>;
25
+ }
26
+
27
+ function coerceKey(key: string | OneKey<string>, def?: string): { key: string; def?: string } {
28
+ if (typeof key === "object") {
29
+ const keys = Object.keys(key);
30
+ if (keys.length !== 1) {
31
+ throw new Error(`Invalid key: ${JSON.stringify(key)}`);
32
+ }
33
+ return { key: keys[0], def: key[keys[0]] };
34
+ }
35
+ return { key, def: def };
36
+ }
37
+
6
38
  export interface URIObject {
7
39
  readonly style: "host" | "path";
8
40
  readonly protocol: string;
@@ -60,7 +92,7 @@ export class MutableURL extends URL {
60
92
  private _pathname: string;
61
93
  private _hasHostpart: boolean;
62
94
 
63
- readonly hash: string;
95
+ override readonly hash: string;
64
96
 
65
97
  constructor(urlStr: string) {
66
98
  super("defect://does.not.exist");
@@ -91,7 +123,7 @@ export class MutableURL extends URL {
91
123
  return new MutableURL(this.toString());
92
124
  }
93
125
 
94
- get host(): string {
126
+ override get host(): string {
95
127
  if (!this._hasHostpart) {
96
128
  throw new Error(
97
129
  `you can use hostname only if protocol is ${this.toString()} ${JSON.stringify(Array.from(hasHostPartProtocols.keys()))}`,
@@ -100,58 +132,58 @@ export class MutableURL extends URL {
100
132
  return this._sysURL.host;
101
133
  }
102
134
 
103
- get port(): string {
135
+ override get port(): string {
104
136
  if (!this._hasHostpart) {
105
137
  throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(hasHostPartProtocols.keys()))}`);
106
138
  }
107
139
  return this._sysURL.port;
108
140
  }
109
141
 
110
- set port(p: string) {
142
+ override set port(p: string) {
111
143
  if (!this._hasHostpart) {
112
144
  throw new Error(`you can use port only if protocol is ${JSON.stringify(Array.from(hasHostPartProtocols.keys()))}`);
113
145
  }
114
146
  this._sysURL.port = p;
115
147
  }
116
148
 
117
- get hostname(): string {
149
+ override get hostname(): string {
118
150
  if (!this._hasHostpart) {
119
151
  throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(hasHostPartProtocols.keys()))}`);
120
152
  }
121
153
  return this._sysURL.hostname;
122
154
  }
123
155
 
124
- set hostname(h: string) {
156
+ override set hostname(h: string) {
125
157
  if (!this._hasHostpart) {
126
158
  throw new Error(`you can use hostname only if protocol is ${JSON.stringify(Array.from(hasHostPartProtocols.keys()))}`);
127
159
  }
128
160
  this._sysURL.hostname = h;
129
161
  }
130
162
 
131
- set pathname(p: string) {
163
+ override set pathname(p: string) {
132
164
  this._pathname = p;
133
165
  }
134
166
 
135
- get pathname(): string {
167
+ override get pathname(): string {
136
168
  return this._pathname;
137
169
  }
138
170
 
139
- get protocol(): string {
171
+ override get protocol(): string {
140
172
  return this._protocol;
141
173
  }
142
174
 
143
- set protocol(p: string) {
175
+ override set protocol(p: string) {
144
176
  if (!p.endsWith(":")) {
145
177
  p = `${p}:`;
146
178
  }
147
179
  this._protocol = p;
148
180
  }
149
181
 
150
- get searchParams(): URLSearchParams {
182
+ override get searchParams(): URLSearchParams {
151
183
  return this._sysURL.searchParams;
152
184
  }
153
185
 
154
- toString(): string {
186
+ override toString(): string {
155
187
  let search = "";
156
188
  if (this._sysURL.searchParams.size) {
157
189
  for (const [key, value] of Array.from(this._sysURL.searchParams.entries()).sort((a, b) => a[0].localeCompare(b[0]))) {
@@ -205,13 +237,25 @@ function getParamResult(
205
237
  return Result.Ok(val);
206
238
  }
207
239
 
208
- type keysParam = (string | ((...keys: string[]) => string))[];
240
+ type msgFn = (...keys: string[]) => string;
241
+ type keysParam = (string | msgFn | Record<string, string>)[];
209
242
 
210
243
  function getParamsResult(
211
244
  keys: keysParam,
212
245
  getParam: { getParam: (key: string) => string | undefined },
213
246
  ): Result<Record<string, string>> {
214
- const keyStrs = keys.flat().filter((k) => typeof k === "string");
247
+ const keyDef = keys.flat().reduce(
248
+ (acc, i) => {
249
+ if (typeof i === "string") {
250
+ acc.push({ key: i });
251
+ } else if (typeof i === "object") {
252
+ acc.push(...Object.keys(i).map((k) => ({ key: k, def: i[k] })));
253
+ }
254
+ return acc;
255
+ },
256
+ [] as { key: string; def?: string }[],
257
+ );
258
+ //.filter((k) => typeof k === "string");
215
259
  const msgFn =
216
260
  keys.find((k) => typeof k === "function") ||
217
261
  ((...keys: string[]): string => {
@@ -220,12 +264,16 @@ function getParamsResult(
220
264
  });
221
265
  const errors: string[] = [];
222
266
  const result: Record<string, string> = {};
223
- for (const key of keyStrs) {
224
- const val = getParam.getParam(key);
267
+ for (const kd of keyDef) {
268
+ const val = getParam.getParam(kd.key);
225
269
  if (val === undefined) {
226
- errors.push(key);
270
+ if (kd.def) {
271
+ result[kd.key] = kd.def;
272
+ } else {
273
+ errors.push(kd.key);
274
+ }
227
275
  } else {
228
- result[key] = val;
276
+ result[kd.key] = val;
229
277
  }
230
278
  }
231
279
  if (errors.length) {
@@ -234,7 +282,7 @@ function getParamsResult(
234
282
  return Result.Ok(result);
235
283
  }
236
284
 
237
- export class BuildURI {
285
+ export class BuildURI implements URIInterface<BuildURI> {
238
286
  _url: MutableURL; // pathname needs this
239
287
  private constructor(url: MutableURL) {
240
288
  this._url = url;
@@ -359,8 +407,17 @@ export class BuildURI {
359
407
  return this._url.searchParams.has(key);
360
408
  }
361
409
 
362
- getParam(key: string): string | undefined {
363
- return falsy2undef(this._url.searchParams.get(key));
410
+ get getParams(): Iterable<[string, string]> {
411
+ return this._url.searchParams.entries();
412
+ }
413
+
414
+ getParam<T extends string | undefined>(key: string | OneKey<string>, def?: T): T extends string ? string : string | undefined {
415
+ const { key: k, def: d } = coerceKey(key, def);
416
+ let val = this._url.searchParams.get(k);
417
+ if (!falsy2undef(val) && d) {
418
+ val = d;
419
+ }
420
+ return falsy2undef(val) as T extends string ? string : string | undefined;
364
421
  }
365
422
 
366
423
  getParamResult(key: string, msgFn?: (key: string) => string): Result<string> {
@@ -383,10 +440,14 @@ export class BuildURI {
383
440
  return this.URI().asURL();
384
441
  }
385
442
 
386
- asObj(...strips: StripCommand[]): HostURIObject | PathURIObject {
443
+ asObj(...strips: StripCommand[]): Partial<HostURIObject | PathURIObject> {
387
444
  return this.URI().asObj(...strips);
388
445
  }
389
446
 
447
+ clone(): BuildURI {
448
+ return BuildURI.from(this.toString());
449
+ }
450
+
390
451
  URI(): URI {
391
452
  return URI.from(this._url);
392
453
  }
@@ -397,7 +458,7 @@ export type CoerceURI = string | URI | MutableURL | URL | BuildURI | NullOrUndef
397
458
  export const hasHostPartProtocols: Set<string> = new Set<string>(["http", "https", "ws", "wss"]);
398
459
 
399
460
  // non mutable URL Implementation
400
- export class URI {
461
+ export class URI implements URIInterface<URI> {
401
462
  static protocolHasHostpart(protocol: string): () => void {
402
463
  protocol = protocol.replace(/:$/, "");
403
464
  hasHostPartProtocols.add(protocol);
@@ -501,8 +562,14 @@ export class URI {
501
562
  hasParam(key: string): boolean {
502
563
  return this._url.searchParams.has(key);
503
564
  }
504
- getParam(key: string): string | undefined {
505
- return falsy2undef(this._url.searchParams.get(key));
565
+
566
+ getParam<T extends string | undefined>(key: string | OneKey<string>, def?: T): T extends string ? string : string | undefined {
567
+ const { key: k, def: d } = coerceKey(key, def);
568
+ let val = this._url.searchParams.get(k);
569
+ if (!falsy2undef(val) && d) {
570
+ val = d;
571
+ }
572
+ return falsy2undef(val) as T extends string ? string : string | undefined;
506
573
  }
507
574
 
508
575
  getParamResult(key: string, msgFn?: (key: string) => string): Result<string> {
@@ -530,7 +597,7 @@ export class URI {
530
597
  toJSON(): string {
531
598
  return this.toString();
532
599
  }
533
- asObj(...strips: StripCommand[]): HostURIObject | PathURIObject {
600
+ asObj(...strips: StripCommand[]): Partial<HostURIObject | PathURIObject> {
534
601
  const pathURI: PathURIObject = {
535
602
  style: "path",
536
603
  protocol: this.protocol,
@@ -543,8 +610,8 @@ export class URI {
543
610
  style: "host",
544
611
  hostname: this.hostname,
545
612
  port: this.port,
546
- }) as HostURIObject;
613
+ }) as Partial<HostURIObject>;
547
614
  }
548
- return stripper(strips, pathURI) as PathURIObject;
615
+ return stripper(strips, pathURI) as Partial<PathURIObject>;
549
616
  }
550
617
  }
@@ -24,11 +24,11 @@ export class ConsoleWriterStreamDefaultWriter implements WritableStreamDefaultWr
24
24
  this.closed = Promise.resolve(undefined);
25
25
  }
26
26
  async write(chunk?: Uint8Array | undefined): Promise<void> {
27
- const str = this.decoder.decode(chunk).trimEnd();
27
+ let strObj: string | { level: string } = this.decoder.decode(chunk).trimEnd();
28
28
  let output = "log";
29
29
  try {
30
- const decode = JSON.parse(str);
31
- output = decode.level;
30
+ strObj = JSON.parse(strObj) as { level: string };
31
+ output = strObj.level;
32
32
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
33
33
  } catch (e) {
34
34
  /* noop */
@@ -36,15 +36,15 @@ export class ConsoleWriterStreamDefaultWriter implements WritableStreamDefaultWr
36
36
  switch (output) {
37
37
  case "error":
38
38
  // eslint-disable-next-line no-console
39
- console.error(str);
39
+ console.error(strObj);
40
40
  break;
41
41
  case "warn":
42
42
  // eslint-disable-next-line no-console
43
- console.warn(str);
43
+ console.warn(strObj);
44
44
  break;
45
45
  default:
46
46
  // eslint-disable-next-line no-console
47
- console.log(str);
47
+ console.log(strObj);
48
48
  }
49
49
  }
50
50
  }
@@ -1,6 +1,9 @@
1
1
  export type StripCommand = string | RegExp;
2
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
- export function stripper<T>(strip: StripCommand | StripCommand[], obj: T): Record<string, any> {
2
+
3
+ export function stripper<T extends unknown | ArrayLike<unknown>>(
4
+ strip: StripCommand | StripCommand[],
5
+ obj: T,
6
+ ): T extends ArrayLike<unknown> ? Record<string, unknown>[] : Record<string, unknown> {
4
7
  const strips = Array.isArray(strip) ? strip : [strip];
5
8
  const restrips = strips.map((s) => {
6
9
  if (typeof s === "string") {
@@ -9,14 +12,18 @@ export function stripper<T>(strip: StripCommand | StripCommand[], obj: T): Recor
9
12
  }
10
13
  return s;
11
14
  });
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- return localStripper(undefined, restrips, obj) as Record<string, any>;
15
+ return localStripper(undefined, restrips, obj) as T extends ArrayLike<unknown>
16
+ ? Record<string, unknown>[]
17
+ : Record<string, unknown>;
14
18
  }
15
19
 
16
20
  function localStripper<T>(path: string | undefined, restrips: RegExp[], obj: T): unknown {
17
21
  if (typeof obj !== "object" || obj === null) {
18
22
  return obj;
19
23
  }
24
+ if (Array.isArray(obj)) {
25
+ return obj.map((i) => localStripper(path, restrips, i));
26
+ }
20
27
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
28
  const ret = { ...obj } as Record<string, any>;
22
29
  const matcher = (key: string, nextPath: string): boolean => {
@@ -53,16 +53,19 @@ export declare class LoggerImpl implements Logger {
53
53
  Ref(key: string, action: {
54
54
  toString: () => string;
55
55
  } | FnSerialized): Logger;
56
- Bool(key: string, value: unknown): Logger;
56
+ Bool(key: string | Record<string, unknown>, value: unknown): Logger;
57
+ Http(res: Response | Result<Response>, req?: Request, key?: string): Logger;
58
+ Pair(x: Record<string, unknown>): Logger;
57
59
  Result<T>(key: string, res: Result<T, Error>): Logger;
58
60
  Len(value: unknown, key?: string): Logger;
59
61
  Hash(value: unknown, key?: string): Logger;
60
62
  Url(url: CoerceURI, key?: string): Logger;
61
- Str(key: string, value?: string): Logger;
62
- Any(key: string, value?: string | number | boolean | LogSerializable): Logger;
63
+ private coerceKey;
64
+ Str(key: string | Record<string, string>, value?: string): Logger;
65
+ Any(key: string | Record<string, unknown>, value?: unknown): Logger;
63
66
  Dur(key: string, nsec: number): Logger;
64
- Uint64(key: string, value: number): Logger;
65
- Int(key: string, value: number): Logger;
67
+ Uint64(key: string | Record<string, number>, value?: number): Logger;
68
+ Int(key: string | Record<string, number>, value?: number): Logger;
66
69
  Flush(): Promise<void>;
67
70
  With(): WithLogger;
68
71
  _resetAttributes(fn: () => () => Uint8Array): () => Uint8Array;
@@ -1 +1 @@
1
- {"version":3,"file":"logger-impl.d.ts","sourceRoot":"","sources":["../../src/logger-impl.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,OAAO,EACP,YAAY,EACZ,eAAe,EACf,KAAK,EACL,MAAM,EAGN,UAAU,EAMV,YAAY,EACZ,YAAY,EAEb,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE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