@wireio/shared 1.0.2 → 1.0.3

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.
Files changed (39) hide show
  1. package/package.json +5 -1
  2. package/src/guards/applyTypeGuardExtras.ts +0 -9
  3. package/src/guards/array.ts +0 -5
  4. package/src/guards/assert.ts +0 -83
  5. package/src/guards/class.ts +0 -56
  6. package/src/guards/guard-tools.ts +0 -78
  7. package/src/guards/index.ts +0 -7
  8. package/src/guards/primitive.ts +0 -77
  9. package/src/guards/types.ts +0 -15
  10. package/src/helpers/Deferred.ts +0 -149
  11. package/src/helpers/index.ts +0 -1
  12. package/src/index.ts +0 -3
  13. package/src/logging/Appender.ts +0 -5
  14. package/src/logging/Formatter.ts +0 -8
  15. package/src/logging/InternalLogger.ts +0 -6
  16. package/src/logging/Level.ts +0 -19
  17. package/src/logging/LogRecord.ts +0 -17
  18. package/src/logging/Logger.ts +0 -222
  19. package/src/logging/LoggingManager.ts +0 -306
  20. package/src/logging/appenders/ConsoleAppender.ts +0 -110
  21. package/src/logging/appenders/DebugAppender.ts +0 -254
  22. package/src/logging/appenders/FileAppender.ts +0 -281
  23. package/src/logging/appenders/aws-firehose/AWSFirehoseAppender.ts +0 -176
  24. package/src/logging/appenders/aws-firehose/AWSFirehoseCredentialManager.ts +0 -132
  25. package/src/logging/appenders/index.ts +0 -3
  26. package/src/logging/context/LogContext.ts +0 -74
  27. package/src/logging/context/LogContextContainer.ts +0 -28
  28. package/src/logging/context/LogContextProvider.ts +0 -43
  29. package/src/logging/context/index.ts +0 -3
  30. package/src/logging/context/providers/node/NodeContextProvider.ts +0 -45
  31. package/src/logging/context/providers/node/index.ts +0 -3
  32. package/src/logging/examples/example-debug-appender.ts +0 -19
  33. package/src/logging/examples/example-file-appender.ts +0 -19
  34. package/src/logging/getLogger.ts +0 -9
  35. package/src/logging/index-node.ts +0 -2
  36. package/src/logging/index.ts +0 -11
  37. package/src/logging/util.ts +0 -6
  38. package/tsconfig.json +0 -9
  39. package/tsconfig.tsbuildinfo +0 -1
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@wireio/shared",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
+ "files": [
9
+ "lib",
10
+ "README.md"
11
+ ],
8
12
  "type": "module",
9
13
  "types": "lib/index.d.ts",
10
14
  "module": "lib/index.js",
@@ -1,9 +0,0 @@
1
- import type { TypeGuard, TypeGuardExtras } from "./types"
2
-
3
-
4
- export function applyTypeGuardExtras<T>(fn:TypeGuard<T>):TypeGuardExtras<T> {
5
- (
6
- fn as TypeGuardExtras<T>
7
- ).lift = (o:any) => () => fn(o)
8
- return fn as TypeGuardExtras<T>
9
- }
@@ -1,5 +0,0 @@
1
- export function isArray(
2
- o: unknown
3
- ): o is Array<typeof o extends Array<infer T> ? T : any> {
4
- return Array.isArray(o)
5
- }
@@ -1,83 +0,0 @@
1
- import { isFunction, isString } from "./primitive.js"
2
-
3
- export interface AssertOptions {
4
- logErrorsToConsole: boolean
5
- }
6
-
7
- export const DefaultAssertOptions:AssertOptions = {
8
- logErrorsToConsole: false
9
- }
10
-
11
- let assertOptions:AssertOptions = {...DefaultAssertOptions}
12
-
13
- export function getAssertOptions() {
14
- return {...assertOptions}
15
- }
16
-
17
- export function setGlobalAssertOptions(options: AssertOptions) {
18
- assertOptions = {...assertOptions, ...options}
19
- }
20
-
21
- export class AssertError extends Error {
22
-
23
-
24
-
25
- constructor(message: string, public readonly cause?: Error) {
26
- super(message)
27
- }
28
- }
29
-
30
-
31
- const assertLift:AssertLift = <T>(test: (value:T) => boolean, messageProvider: string | ((value: T) => string)) => {
32
- return (nextValue: T) => {
33
- const ok = test(nextValue)
34
- if (!ok) {
35
- const message = isFunction(messageProvider) ? messageProvider(nextValue) : messageProvider ?? "no message"
36
- throw new AssertError(message)
37
- }
38
- }
39
- }
40
-
41
- export type AssertLift = <T>(test: (value:T) => boolean, message: string | ((value: T) => string)) =>
42
- (nextValue: T) => void | never
43
-
44
- export interface Assert {
45
- (
46
- test: (() => boolean) | boolean,
47
- msg?: null | ((err?: Error) => Error | string) | Error | string | undefined,
48
- overrideOptions?: Partial<AssertOptions>
49
- ): void | never
50
-
51
- lift: AssertLift
52
- }
53
-
54
-
55
- export const assert: Assert = Object.assign((
56
- test: (() => boolean) | boolean,
57
- msg?: null | ((err?: Error) => Error | string) | Error | string | undefined,
58
- overrideOptions: Partial<AssertOptions> = {}
59
- ): void | never => {
60
- const options = {...assertOptions, ...overrideOptions}
61
- let result: boolean = false
62
- let error: Error = undefined
63
-
64
- try {
65
- result = isFunction(test) ? test() : test
66
- } catch (err) {
67
- error = err
68
- if (options.logErrorsToConsole) {
69
- console.error(`Assert failed: "${test}"`, err)
70
- }
71
- }
72
-
73
- if (!result || !!error) {
74
- const errOut = !msg ? (error?.message ?? "unknown") :
75
- isFunction(msg) ? msg(error) :
76
- msg
77
- throw isString(errOut) ? new AssertError(errOut, error) : errOut
78
- }
79
- }, {
80
- lift: assertLift
81
- })
82
-
83
-
@@ -1,56 +0,0 @@
1
- /**
2
- * Is ES6+ class
3
- *
4
- * @see https://stackoverflow.com/questions/29093396/how-do-you-check-the-difference-between-an-ecmascript-6-class-and-function/49510834
5
- *
6
- * @param {any} value
7
- * @returns {boolean}
8
- */
9
- import type { ClassConstructor, TypeGuard } from "./types"
10
-
11
- export function isNativeClass <T = any>(value:any): value is ClassConstructor<T> {
12
- return typeof value === 'function' && value.toString().indexOf('class') === 0
13
- }
14
-
15
- // Character positions
16
- const INDEX_OF_FUNCTION_NAME = 9 // "function X", X is at index 9
17
- const FIRST_UPPERCASE_INDEX_IN_ASCII = 65 // A is at index 65 in ASCII
18
- const LAST_UPPERCASE_INDEX_IN_ASCII = 90 // Z is at index 90 in ASCII
19
-
20
- /**
21
- * Is Conventional Class
22
- * Looks for function with capital first letter MyClass
23
- * First letter is the 9th character
24
- * If changed, isClass must also be updated
25
- * @param {any} value
26
- * @returns {boolean}
27
- */
28
- export function isConventionalClass <T = any>(value:any): value is ClassConstructor<T> {
29
- if ( typeof value !== 'function' ) return false
30
- const c = value.toString().charCodeAt(INDEX_OF_FUNCTION_NAME)
31
- return c >= FIRST_UPPERCASE_INDEX_IN_ASCII && c <= LAST_UPPERCASE_INDEX_IN_ASCII
32
- }
33
-
34
-
35
- export function isClass<T = any>(value: any, includeConventional: boolean = true): value is ClassConstructor<T> {
36
- return isNativeClass<T>(value) || (includeConventional && isConventionalClass<T>(value))
37
- }
38
-
39
-
40
-
41
-
42
-
43
- export function createInstanceOfGuard<T, Ctor extends ClassConstructor<T>>(ctor: Ctor): ((o: any) => o is T) {
44
- return (o: any): o is T => o instanceof ctor
45
- }
46
-
47
- export function instanceOf<T extends {}, Ctor extends ClassConstructor<T>>(ctor: Ctor) {
48
- return createInstanceOfGuard<T,Ctor>(ctor)
49
- }
50
-
51
- export function createGenericGuard<T>(tester: (val:any) => val is T):TypeGuard<T>
52
- export function createGenericGuard<T>(type:{new():T}, tester: (val:any) => val is T):TypeGuard<T>
53
- export function createGenericGuard<T>(typeOrTest: ({new():T} | ((val:any) => val is T)), tester?: (val:any) => val is T) {
54
- return tester as TypeGuard<T>
55
- }
56
-
@@ -1,78 +0,0 @@
1
- import { isNil, isPromise } from "./primitive.js"
2
- import type { ErrorHandler } from "./types.js"
3
-
4
- export type GuardErrorHandler = (err:Error) => void
5
-
6
- let globalErrorHandler:GuardErrorHandler | null
7
-
8
- export function setGuardErrorHandler(errorHandler:GuardErrorHandler | null = null) {
9
- globalErrorHandler = errorHandler
10
- }
11
-
12
- export type ReturnTypeOrAny<Fn extends () => any> = (ReturnType<Fn> extends never ? unknown : ReturnType<Fn>)
13
-
14
-
15
- /**
16
- * Get a value in a guarded fashion
17
- * ensuring no exception
18
- *
19
- * @param fn
20
- * @param defaultValue
21
- * @returns {any}
22
- * @param errorHandler
23
- */
24
- export function getValue<Fn extends () => any, T extends ReturnType<Fn> >(
25
- fn:Fn,
26
- defaultValue: T = undefined,
27
- errorHandler: ErrorHandler = globalErrorHandler
28
- ):T extends Promise<infer T2> ? Promise<T2> : T {
29
-
30
- let result = null
31
-
32
- try {
33
- result = fn()
34
- if (isPromise(result))
35
- return result
36
- .catch(err => {
37
- if (errorHandler)
38
- errorHandler(err)
39
-
40
- return defaultValue
41
- })
42
- .then(value => isNil(value) ? defaultValue : value) as any
43
- } catch (err) {
44
- if (errorHandler)
45
- errorHandler(err)
46
- }
47
-
48
- return isNil(result) ?
49
- (defaultValue as any) : result
50
- }
51
-
52
-
53
- export type GuardFn = <Fn extends () => any, T extends ReturnType<Fn> >(fn:Fn, localErrorHandler?: GuardErrorHandler | null) => (void | Promise<void>)
54
- export type GuardTool = GuardFn & {
55
- lift: <T>(fn:() => T, localErrorHandler?: GuardErrorHandler | null) => ((fn:() => T, localErrorHandler?: GuardErrorHandler | null) => (void | Promise<void>))
56
- }
57
-
58
-
59
-
60
- /**
61
- * Execute a function guarded from exception
62
- *
63
- * @param fn
64
- * @param localErrorHandler
65
- * @returns {(fn:()=>any)=>(fn:()=>any)=>any}
66
- */
67
- const guardFn:GuardFn = <Fn extends () => any, T extends ReturnType<Fn> >(fn:Fn, localErrorHandler: ((err: Error) => void) | null = null):void | Promise<void> => {
68
- const value = getValue<Fn,T>(fn, undefined, localErrorHandler)
69
- if (isPromise(value))
70
- return value.then(() => undefined as void)
71
-
72
- }
73
-
74
- export const guard: GuardTool = Object.assign(guardFn, {
75
- lift: <Fn extends () => any, T extends ReturnType<Fn> >(fn:Fn, localErrorHandler?: GuardErrorHandler | null) =>
76
- (fn:Fn, localErrorHandler?: GuardErrorHandler | null): (void | Promise<void>) =>
77
- guardFn<Fn,T>(fn,localErrorHandler)
78
- })
@@ -1,7 +0,0 @@
1
- export * from "./class.js"
2
- export * from "./array.js"
3
- export * from "./types.js"
4
- export * from "./primitive.js"
5
- export * from "./types.js"
6
- export * from "./assert.js"
7
- export * from "./guard-tools.js"
@@ -1,77 +0,0 @@
1
- import _isNil from 'lodash/isNil.js'
2
- import _isObject from 'lodash/isObject.js'
3
- import _isString from 'lodash/isString.js'
4
- import _isNumber from 'lodash/isNumber.js'
5
- import _isFunction from 'lodash/isFunction.js'
6
- import { applyTypeGuardExtras } from "./applyTypeGuardExtras.js";
7
-
8
-
9
- export type UndefinedOrNull = undefined|null
10
-
11
-
12
-
13
- export function isNil(o:any):o is UndefinedOrNull {
14
- return _isNil(o)
15
- }
16
-
17
- /**
18
- * O is a valid object
19
- *
20
- * @param o
21
- */
22
- export function isDefined<T>(o:any):o is Exclude<T, UndefinedOrNull>
23
- export function isDefined(o:any):o is Exclude<any,UndefinedOrNull>
24
- export function isDefined(o:any) {
25
- return !isNil(o)
26
- }
27
-
28
- export function isObject(o:any):o is Object {
29
- return !isNil(o) && _isObject(o)
30
- }
31
-
32
- export function isPromise(o:any):o is Promise<any> {
33
- return !isNil(o) && isObject(o) && (o instanceof Promise || isFunction(o.then))
34
- }
35
-
36
-
37
- export function isObjectType<T>(o:any,type:{new():T}):o is T {
38
- return !isNil(o) && (o instanceof type || o.$$clazz === type.name)
39
- }
40
-
41
- export function isString(o:any):o is string {
42
- return !isNil(o) && _isString(o)
43
- }
44
-
45
- export function isNumber(o:any):o is number {
46
- return !isNil(o) && _isNumber(o) && !isNaN(o)
47
- }
48
-
49
- export const isFunction = applyTypeGuardExtras<Function>(function isFunction(o:any):o is Function {
50
- return !isNil(o) && _isFunction(o)
51
- })
52
-
53
- export function isSymbol(o:any):o is Symbol {
54
- return !isNil(o) && typeof o === 'symbol'
55
- }
56
-
57
- export function isBoolean(o: any): o is boolean {
58
- return typeof o === "boolean"
59
- }
60
-
61
- export function isDate(o: any): o is Date {
62
- return o instanceof Date
63
- }
64
-
65
- export type Primitive = boolean | string | number
66
-
67
- export type PrimitiveProducer<P extends Primitive = Primitive> = (...args: any[]) => P
68
-
69
- export const PrimitiveProducers = Array<PrimitiveProducer>(String, Number, Boolean)
70
-
71
- export function isPrimitiveProducer<P extends Primitive = Primitive>(o:any): o is PrimitiveProducer<P> {
72
- return PrimitiveProducers.includes(o)
73
- }
74
-
75
- export function isPrimitive(o:any): o is Primitive {
76
- return isBoolean(o) || isString(o) || isNumber(o)
77
- }
@@ -1,15 +0,0 @@
1
- export type TypeGuard<T> = (o:any) => o is T
2
-
3
- export type ClassConstructor<T> = new (...args:any[]) => T
4
-
5
- export type LiftTypeGuard<T> = () => boolean
6
-
7
- export type TypeGuardExtras<T> = TypeGuard<T> & {
8
- lift: (o:any) => LiftTypeGuard<T>
9
- }
10
-
11
- export type Optional<T> = T | null | undefined
12
-
13
- export type ErrorHandler<E extends Error = Error> = (err: E) => void
14
-
15
- export {}
@@ -1,149 +0,0 @@
1
- import { isDefined, isPromise } from "../guards/index.js"
2
-
3
- export enum DeferredStatus {
4
- PENDING = "PENDING",
5
- FULFILLED = "FULFILLED",
6
- REJECTED = "REJECTED",
7
- CANCELED = "CANCELED"
8
- }
9
-
10
- /**
11
- * Internal deferred state
12
- */
13
- interface DeferredState<T> {
14
- resolve: (result?:T) => void
15
- reject: (err:any) => void
16
- isSettled: boolean
17
- isCancelled: boolean
18
- isRejected: boolean
19
- isFulfilled: boolean
20
- result:T | null
21
- err: Error | null
22
- }
23
-
24
- /**
25
- * A deferred promise that can be resolved or rejected
26
- * externally, ideal for functions like a promise timeout
27
- */
28
- export class Deferred<T> {
29
-
30
-
31
- static async delay(millis:number) {
32
- const deferred = new Deferred<void>()
33
- setTimeout(() => deferred.resolve(),millis)
34
- await deferred.promise
35
- }
36
-
37
- static resolve<T>(value?:T): Deferred<T> {
38
- const deferred = new Deferred<T>()
39
- deferred.resolve(value)
40
- return deferred
41
- }
42
-
43
- private readonly state:DeferredState<T>
44
-
45
- readonly promise:Promise<T>
46
-
47
-
48
- constructor(promise?:Promise<T> | undefined) {
49
- this.state = {
50
- resolve: null,
51
- reject: null,
52
- isSettled: false,
53
- isCancelled: false,
54
- isRejected: false,
55
- isFulfilled: false,
56
- result: null,
57
- err: null
58
- }
59
-
60
- this.promise = new Promise<T>((resolve, reject) => {
61
- Object.assign(this.state, {
62
- resolve,
63
- reject
64
- })
65
- })
66
-
67
- if (isDefined(promise)) {
68
- if (promise instanceof Promise || isPromise(promise)) {
69
- promise
70
- .then(this.state.resolve)
71
- .catch(this.state.reject)
72
- } else {
73
- throw Error(`An existing promise was provided to Deferred constructor, but it wasn't a valid promise`)
74
- }
75
- }
76
- }
77
-
78
- isFulfilled(): boolean {
79
- return this.state.isFulfilled
80
- }
81
-
82
- isRejected(): boolean {
83
- return this.state.isRejected
84
- }
85
-
86
- isSettled(): boolean {
87
- return this.state.isSettled
88
- }
89
-
90
- isCancelled(): boolean {
91
- return this.state.isCancelled
92
- }
93
-
94
- status(): DeferredStatus {
95
- return this.isCancelled() ? DeferredStatus.CANCELED : this.isSettled() ? this.isFulfilled() ? DeferredStatus.FULFILLED : DeferredStatus.REJECTED : DeferredStatus.PENDING
96
- }
97
-
98
- cancel(): void {
99
- this.state.isSettled = true
100
- this.state.isCancelled = true
101
- }
102
-
103
- resolve(result?:T): void {
104
- if (!this.state.isSettled && !this.state.isCancelled) {
105
- Object.assign(this.state, {
106
- result: result,
107
- isSettled: true,
108
- isFulfilled: true
109
- })
110
- this.state.resolve(result)
111
- }
112
- }
113
-
114
- reject(err:any): void {
115
- if (!this.state.isSettled && !this.state.isCancelled) {
116
- Object.assign(this.state, {
117
- isSettled: true,
118
- isRejected: true,
119
- err
120
- })
121
-
122
-
123
-
124
- this.state.reject(err)
125
- }
126
- }
127
-
128
- get error() {
129
- return this.getError()
130
- }
131
-
132
- get value() {
133
- return this.getResult()
134
- }
135
-
136
- getError(): Error | undefined {
137
- if(!this.isSettled())
138
- throw Error("Deferred promise is not settled, result is not available")
139
- return this.state.err
140
- }
141
-
142
- getResult():T {
143
- if(!this.isSettled())
144
- throw Error("Deferred promise is not settled, result is not available")
145
- return this.state.result
146
- }
147
- }
148
-
149
- export default Deferred
@@ -1 +0,0 @@
1
- export * from "./Deferred.js"
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./guards/index.js"
2
- export * from "./helpers/index.js"
3
- export * from "./logging/index.js"
@@ -1,5 +0,0 @@
1
- import { LogRecord } from "./LogRecord.js"
2
-
3
- export interface Appender<Record extends LogRecord = any> {
4
- append(record: Record): void
5
- }
@@ -1,8 +0,0 @@
1
- import type { LogRecord } from "./LogRecord"
2
-
3
- export type Formatter<
4
- Output,
5
- Context extends {} = {},
6
- Data = any,
7
- Record extends LogRecord<Data> = LogRecord<Data>
8
- > = (record: Record, options?: Context) => Output
@@ -1,6 +0,0 @@
1
- const GLOBAL_CATEGORY = "GLOBAL"
2
-
3
- export function getInternalLogger(category: string = GLOBAL_CATEGORY) {
4
- category = category.split("/").pop().replace(/\.[a-z]+$/,"") ?? GLOBAL_CATEGORY
5
- return console
6
- }
@@ -1,19 +0,0 @@
1
- import { fromPairs, uniq } from "lodash"
2
-
3
- export enum Level {
4
- trace = "trace",
5
- debug = "debug",
6
- info = "info",
7
- warn = "warn",
8
- error = "error",
9
- fatal = "fatal"
10
- }
11
-
12
- export type LevelName = `${Level}`
13
- export type LevelKind = LevelName | Level
14
-
15
- export const LevelNames: Array<LevelName> = uniq(Object.values(Level))
16
- export const LevelThresholds = fromPairs(
17
- LevelNames.map((level, i) => [level, i])
18
- ) as Record<LevelKind, number>
19
- export type LevelEnableFnName = `is${Capitalize<LevelName>}Enabled`
@@ -1,17 +0,0 @@
1
- import { fromPairs, uniq } from "lodash"
2
- import type { Level, LevelKind, LevelName } from "./Level"
3
-
4
- export interface LogRecord<Data = any> {
5
- timestamp: number
6
- category: string
7
- level: LevelKind
8
- message: string
9
- args?: any[]
10
- data?: Data
11
- tags?: string[]
12
- url?: string
13
- env?: string
14
- event?: string
15
- errorMessage?: string
16
- errorStack?: string
17
- }