@wener/utils 1.1.8 → 1.1.9
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/dist/LICENSE.txt +1 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/system/index.js +1 -1
- package/dist/system/index.js.map +1 -1
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/lib/asyncs/createLazyPromise.js +3 -1
- package/lib/asyncs/createLazyPromise.js.map +1 -1
- package/lib/crypto/getRandomValues.js +2 -12
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/crypto/randomUUID.js +1 -1
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/crypto/ulid.js +3 -3
- package/lib/crypto/ulid.js.map +1 -1
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/logging/createChildLogger.js +2 -2
- package/lib/logging/createChildLogger.js.map +1 -1
- package/lib/logging/createLogger.js +26 -0
- package/lib/logging/createLogger.js.map +1 -0
- package/lib/server/polyfillFetch.js +17 -24
- package/lib/server/polyfillFetch.js.map +1 -1
- package/lib/validations/parseBoolean.js +31 -0
- package/lib/validations/parseBoolean.js.map +1 -0
- package/lib/validations/parseTimestamp.js +25 -0
- package/lib/validations/parseTimestamp.js.map +1 -0
- package/package.json +5 -8
- package/src/asyncs/createLazyPromise.ts +2 -1
- package/src/asyncs/isThenable.ts +4 -0
- package/src/crypto/getRandomValues.ts +13 -11
- package/src/crypto/randomUUID.ts +1 -1
- package/src/crypto/ulid.test.ts +11 -3
- package/src/crypto/ulid.ts +5 -4
- package/src/index.ts +5 -1
- package/src/logging/createChildLogger.ts +2 -2
- package/src/logging/{createWriteLogger.ts → createLogger.ts} +19 -3
- package/src/logging/logger.test.ts +5 -3
- package/src/server/polyfillBrowser.test.ts +5 -1
- package/src/server/polyfillFetch.ts +22 -24
- package/src/validations/parseBoolean.ts +30 -0
- package/src/validations/parseTimestamp.test.ts +7 -0
- package/src/validations/parseTimestamp.ts +29 -0
- package/lib/logging/createWriteLogger.js +0 -13
- package/lib/logging/createWriteLogger.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -34,6 +34,8 @@ export { isDefined } from './validations/isDefined';
|
|
|
34
34
|
export { isEmptyObject } from './validations/isEmptyObject';
|
|
35
35
|
export { isUUID } from './validations/isUUID';
|
|
36
36
|
export { isPlainObject } from './validations/isPlainObject';
|
|
37
|
+
export { parseTimestamp } from './validations/parseTimestamp';
|
|
38
|
+
export { parseBoolean } from './validations/parseBoolean';
|
|
37
39
|
|
|
38
40
|
// modules
|
|
39
41
|
export { parseModuleId, type ParsedModuleId } from './modules/parseModuleId';
|
|
@@ -41,7 +43,7 @@ export { isModule, type Module } from './modules/isModule';
|
|
|
41
43
|
|
|
42
44
|
// logging
|
|
43
45
|
export { type Logger, type LogLevel } from './logging/Logger';
|
|
44
|
-
export {
|
|
46
|
+
export { createLogger } from './logging/createLogger';
|
|
45
47
|
export { createNoopLogger } from './logging/createNoopLogger';
|
|
46
48
|
export { createChildLogger } from './logging/createChildLogger';
|
|
47
49
|
|
|
@@ -79,3 +81,5 @@ export { isULID, createULID, ulid, parseULID } from './crypto/ulid';
|
|
|
79
81
|
|
|
80
82
|
// misc
|
|
81
83
|
export { createRandom } from './maths/random';
|
|
84
|
+
|
|
85
|
+
export type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Logger, LoggerWithChild } from './Logger';
|
|
2
|
-
import {
|
|
2
|
+
import { createLogger } from './createLogger';
|
|
3
3
|
|
|
4
4
|
export function createChildLogger(l: Logger, ctx: object): LoggerWithChild {
|
|
5
5
|
if (l.child) {
|
|
6
6
|
return l.child(ctx) as LoggerWithChild;
|
|
7
7
|
}
|
|
8
|
-
return
|
|
8
|
+
return createLogger((o) => {
|
|
9
9
|
const { level, values, ...c } = o;
|
|
10
10
|
if (Object.keys(c).length) {
|
|
11
11
|
l[level](c, ...values);
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import type { LoggerWithChild, LogLevel } from './Logger';
|
|
2
2
|
|
|
3
|
-
export function
|
|
4
|
-
write: (o: { level: LogLevel; values: any[] } & Record<string | symbol, any>) => void
|
|
3
|
+
export function createLogger(
|
|
4
|
+
write: (o: { level: LogLevel; values: any[] } & Record<string | symbol, any>) => void = ({
|
|
5
|
+
level,
|
|
6
|
+
values,
|
|
7
|
+
...ctx
|
|
8
|
+
}) => {
|
|
9
|
+
({ values, ...ctx } = merge(ctx, values));
|
|
10
|
+
console[level]?.(...values, ctx);
|
|
11
|
+
},
|
|
5
12
|
context: object = {},
|
|
6
13
|
): LoggerWithChild {
|
|
7
14
|
return {
|
|
@@ -10,6 +17,15 @@ export function createWriteLogger(
|
|
|
10
17
|
info: (...values) => write({ ...context, level: 'info', values }),
|
|
11
18
|
warn: (...values) => write({ ...context, level: 'warn', values }),
|
|
12
19
|
error: (...values) => write({ ...context, level: 'error', values }),
|
|
13
|
-
child: (ctx) =>
|
|
20
|
+
child: (ctx) => createLogger(write, { ...context, ...ctx }),
|
|
14
21
|
};
|
|
15
22
|
}
|
|
23
|
+
|
|
24
|
+
// logger.info({name:'wener'},'message')
|
|
25
|
+
// merge initial context with message object
|
|
26
|
+
function merge(ctx: any, values: any[]) {
|
|
27
|
+
if (values[0] && typeof values[0] === 'object') {
|
|
28
|
+
return { ...ctx, ...values[0], values: values.slice(1) };
|
|
29
|
+
}
|
|
30
|
+
return { ...ctx, values };
|
|
31
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
2
|
import { createChildLogger } from './createChildLogger';
|
|
3
|
-
import {
|
|
3
|
+
import { createLogger } from './createLogger';
|
|
4
4
|
|
|
5
5
|
test('logger', (t) => {
|
|
6
6
|
{
|
|
7
7
|
const logs: any[] = [];
|
|
8
|
-
const base =
|
|
8
|
+
const base = createLogger((o) => logs.push(o));
|
|
9
9
|
const l = createChildLogger(base, { c: 'test' });
|
|
10
10
|
l.info('hello');
|
|
11
11
|
t.deepEqual(logs.shift(), { level: 'info', values: ['hello'], c: 'test' });
|
|
@@ -15,7 +15,7 @@ test('logger', (t) => {
|
|
|
15
15
|
createChildLogger(console, { c: 'test' }).info('hello');
|
|
16
16
|
{
|
|
17
17
|
let pass = 0;
|
|
18
|
-
const l =
|
|
18
|
+
const l = createLogger(
|
|
19
19
|
(o) => {
|
|
20
20
|
pass++;
|
|
21
21
|
t.log(`${o.level}: [${[o.m, o.c].filter(Boolean).join('.') || 'default'}]`, ...o.values);
|
|
@@ -30,5 +30,7 @@ test('logger', (t) => {
|
|
|
30
30
|
t.is(pass, 2);
|
|
31
31
|
createChildLogger(l, { m: 'Child' }).info('nice 3');
|
|
32
32
|
t.is(pass, 3);
|
|
33
|
+
|
|
34
|
+
createLogger().child({ name: 'wener' }).info({ x: 1 }, 'Nice');
|
|
33
35
|
}
|
|
34
36
|
});
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
|
+
import * as nodeFetch from 'node-fetch';
|
|
2
3
|
import { polyfillBrowser } from './polyfillBrowser';
|
|
4
|
+
import { polyfillFetch } from './polyfillFetch';
|
|
3
5
|
|
|
4
|
-
test.before(async () => {
|
|
6
|
+
test.before(async (t) => {
|
|
7
|
+
t.true(polyfillFetch(nodeFetch));
|
|
8
|
+
t.false(polyfillFetch());
|
|
5
9
|
await polyfillBrowser();
|
|
6
10
|
});
|
|
7
11
|
|
|
@@ -1,28 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
import type { MaybePromise } from '../asyncs/MaybePromise';
|
|
2
|
+
|
|
3
|
+
export function polyfillFetch(nodeFetch: typeof import('node-fetch')): boolean;
|
|
4
|
+
export function polyfillFetch(nodeFetch?: undefined): Promise<boolean>;
|
|
5
|
+
export function polyfillFetch(nodeFetch?: typeof import('node-fetch')): MaybePromise<boolean> {
|
|
2
6
|
if ('fetch' in globalThis) {
|
|
3
7
|
return false;
|
|
4
8
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Response,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
FetchError,
|
|
23
|
-
FormData,
|
|
24
|
-
Blob,
|
|
25
|
-
File,
|
|
26
|
-
});
|
|
27
|
-
return true;
|
|
9
|
+
// sync mode
|
|
10
|
+
if (nodeFetch) {
|
|
11
|
+
const { default: fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = nodeFetch;
|
|
12
|
+
Object.assign(globalThis, {
|
|
13
|
+
fetch,
|
|
14
|
+
Response,
|
|
15
|
+
Headers,
|
|
16
|
+
Request,
|
|
17
|
+
AbortError,
|
|
18
|
+
FetchError,
|
|
19
|
+
FormData,
|
|
20
|
+
Blob,
|
|
21
|
+
File,
|
|
22
|
+
});
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
return import('node-fetch').then((v) => polyfillFetch(v));
|
|
28
26
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function parseBoolean(s: string | boolean | number | null | undefined, strict: true): boolean | undefined;
|
|
2
|
+
export function parseBoolean(s: string | boolean | number | null | undefined): boolean;
|
|
3
|
+
export function parseBoolean(s?: string | boolean | number | null, strict = false): boolean | undefined {
|
|
4
|
+
if (typeof s === 'boolean') {
|
|
5
|
+
return s;
|
|
6
|
+
}
|
|
7
|
+
if (typeof s === 'string') {
|
|
8
|
+
switch (s.toLowerCase()) {
|
|
9
|
+
case 'f':
|
|
10
|
+
case 'false':
|
|
11
|
+
case '0':
|
|
12
|
+
return false;
|
|
13
|
+
case '1':
|
|
14
|
+
case 't':
|
|
15
|
+
case 'true':
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
} else if (typeof s === 'number') {
|
|
19
|
+
switch (s) {
|
|
20
|
+
case 0:
|
|
21
|
+
return false;
|
|
22
|
+
case 1:
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (strict) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return Boolean(s);
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function parseTimestamp(raw?: string | number | Date): Date | undefined {
|
|
2
|
+
if (!raw) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
if (raw instanceof Date) {
|
|
6
|
+
return raw;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (typeof raw === 'string' && /^[0-9.]+$/.test(raw)) {
|
|
10
|
+
let n = parseFloat(raw);
|
|
11
|
+
const len = Math.floor(n).toString().length;
|
|
12
|
+
/*
|
|
13
|
+
最常见的是 10 位和 13 位
|
|
14
|
+
9999999999 - 10*9 - 1970-04-26
|
|
15
|
+
99999999999 - 11*9 - 1973
|
|
16
|
+
999999999999 - 12*9 - 2001
|
|
17
|
+
*/
|
|
18
|
+
if (len <= 11) {
|
|
19
|
+
n *= 1000;
|
|
20
|
+
}
|
|
21
|
+
return new Date(n);
|
|
22
|
+
} else if (typeof raw === 'string') {
|
|
23
|
+
const date = new Date(raw);
|
|
24
|
+
if (!isNaN(+date)) {
|
|
25
|
+
return date;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`parseTimestamp: invalid "${raw}"`);
|
|
29
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
function createWriteLogger(write, context = {}) {
|
|
2
|
-
return {
|
|
3
|
-
trace: (...values) => write({ ...context, level: "trace", values }),
|
|
4
|
-
debug: (...values) => write({ ...context, level: "debug", values }),
|
|
5
|
-
info: (...values) => write({ ...context, level: "info", values }),
|
|
6
|
-
warn: (...values) => write({ ...context, level: "warn", values }),
|
|
7
|
-
error: (...values) => write({ ...context, level: "error", values }),
|
|
8
|
-
child: (ctx) => createWriteLogger(write, { ...context, ...ctx })
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export { createWriteLogger };
|
|
13
|
-
//# sourceMappingURL=createWriteLogger.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createWriteLogger.js","sources":["../../src/logging/createWriteLogger.ts"],"sourcesContent":["import type { LoggerWithChild, LogLevel } from './Logger';\n\nexport function createWriteLogger(\n write: (o: { level: LogLevel; values: any[] } & Record<string | symbol, any>) => void,\n context: object = {},\n): LoggerWithChild {\n return {\n trace: (...values) => write({ ...context, level: 'trace', values }),\n debug: (...values) => write({ ...context, level: 'debug', values }),\n info: (...values) => write({ ...context, level: 'info', values }),\n warn: (...values) => write({ ...context, level: 'warn', values }),\n error: (...values) => write({ ...context, level: 'error', values }),\n child: (ctx) => createWriteLogger(write, { ...context, ...ctx }),\n };\n}\n"],"names":[],"mappings":"AAEO,SAAS,iBACd,CAAA,KAAA,EACA,OAAkB,GAAA,EACD,EAAA;AACjB,EAAO,OAAA;AAAA,IACL,KAAA,EAAO,CAAI,GAAA,MAAA,KAAW,KAAM,CAAA,EAAE,GAAG,OAAS,EAAA,KAAA,EAAO,OAAS,EAAA,MAAA,EAAQ,CAAA;AAAA,IAClE,KAAA,EAAO,CAAI,GAAA,MAAA,KAAW,KAAM,CAAA,EAAE,GAAG,OAAS,EAAA,KAAA,EAAO,OAAS,EAAA,MAAA,EAAQ,CAAA;AAAA,IAClE,IAAA,EAAM,CAAI,GAAA,MAAA,KAAW,KAAM,CAAA,EAAE,GAAG,OAAS,EAAA,KAAA,EAAO,MAAQ,EAAA,MAAA,EAAQ,CAAA;AAAA,IAChE,IAAA,EAAM,CAAI,GAAA,MAAA,KAAW,KAAM,CAAA,EAAE,GAAG,OAAS,EAAA,KAAA,EAAO,MAAQ,EAAA,MAAA,EAAQ,CAAA;AAAA,IAChE,KAAA,EAAO,CAAI,GAAA,MAAA,KAAW,KAAM,CAAA,EAAE,GAAG,OAAS,EAAA,KAAA,EAAO,OAAS,EAAA,MAAA,EAAQ,CAAA;AAAA,IAClE,KAAA,EAAO,CAAC,GAAA,KAAQ,iBAAkB,CAAA,KAAA,EAAO,EAAE,GAAG,OAAA,EAAS,GAAG,GAAA,EAAK,CAAA;AAAA,GACjE,CAAA;AACF;;;;"}
|