@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.
- package/package.json +5 -1
- package/src/guards/applyTypeGuardExtras.ts +0 -9
- package/src/guards/array.ts +0 -5
- package/src/guards/assert.ts +0 -83
- package/src/guards/class.ts +0 -56
- package/src/guards/guard-tools.ts +0 -78
- package/src/guards/index.ts +0 -7
- package/src/guards/primitive.ts +0 -77
- package/src/guards/types.ts +0 -15
- package/src/helpers/Deferred.ts +0 -149
- package/src/helpers/index.ts +0 -1
- package/src/index.ts +0 -3
- package/src/logging/Appender.ts +0 -5
- package/src/logging/Formatter.ts +0 -8
- package/src/logging/InternalLogger.ts +0 -6
- package/src/logging/Level.ts +0 -19
- package/src/logging/LogRecord.ts +0 -17
- package/src/logging/Logger.ts +0 -222
- package/src/logging/LoggingManager.ts +0 -306
- package/src/logging/appenders/ConsoleAppender.ts +0 -110
- package/src/logging/appenders/DebugAppender.ts +0 -254
- package/src/logging/appenders/FileAppender.ts +0 -281
- package/src/logging/appenders/aws-firehose/AWSFirehoseAppender.ts +0 -176
- package/src/logging/appenders/aws-firehose/AWSFirehoseCredentialManager.ts +0 -132
- package/src/logging/appenders/index.ts +0 -3
- package/src/logging/context/LogContext.ts +0 -74
- package/src/logging/context/LogContextContainer.ts +0 -28
- package/src/logging/context/LogContextProvider.ts +0 -43
- package/src/logging/context/index.ts +0 -3
- package/src/logging/context/providers/node/NodeContextProvider.ts +0 -45
- package/src/logging/context/providers/node/index.ts +0 -3
- package/src/logging/examples/example-debug-appender.ts +0 -19
- package/src/logging/examples/example-file-appender.ts +0 -19
- package/src/logging/getLogger.ts +0 -9
- package/src/logging/index-node.ts +0 -2
- package/src/logging/index.ts +0 -11
- package/src/logging/util.ts +0 -6
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { FirehoseClient, PutRecordBatchCommand } from "@aws-sdk/client-firehose"
|
|
2
|
-
import { Deferred } from "../../../helpers/index.js"
|
|
3
|
-
import { getInternalLogger } from "../../InternalLogger.js"
|
|
4
|
-
import EventEmitter3 from "eventemitter3"
|
|
5
|
-
|
|
6
|
-
const API_BASE = "https://log-ingest.shared.wire-dev.com"
|
|
7
|
-
const CRED_BUFFER_PADDING_MS = 3 * 60 * 1000 // 3 minutes
|
|
8
|
-
|
|
9
|
-
const log = getInternalLogger(import.meta.filename)
|
|
10
|
-
|
|
11
|
-
export interface AWSCredentials {
|
|
12
|
-
accessKeyId: string
|
|
13
|
-
secretAccessKey: string
|
|
14
|
-
sessionToken: string
|
|
15
|
-
streamName: string
|
|
16
|
-
region: string
|
|
17
|
-
expiration: string
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface AWSFirehoseCredentialManagerEventMap {
|
|
21
|
-
received: (creds: AWSCredentials) => void
|
|
22
|
-
expired: () => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class AWSFirehoseCredentialManager extends EventEmitter3<AWSFirehoseCredentialManagerEventMap> {
|
|
26
|
-
|
|
27
|
-
private refreshTimer: ReturnType<typeof setTimeout> = null;
|
|
28
|
-
private loadDeferred: Deferred<AWSCredentials> = null;
|
|
29
|
-
|
|
30
|
-
forceUpdateCredentials(): void {
|
|
31
|
-
if (this.loadDeferred && !this.loadDeferred.isSettled()) {
|
|
32
|
-
this.loadDeferred.reject(new Error("force update"))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
this.loadDeferred = null
|
|
36
|
-
this.scheduleRefresh(1)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
getCredentials(): AWSCredentials {
|
|
40
|
-
if (this.loadDeferred && this.loadDeferred.isFulfilled()) {
|
|
41
|
-
const creds = this.loadDeferred.getResult(),
|
|
42
|
-
expMs = Date.parse(creds.expiration)
|
|
43
|
-
|
|
44
|
-
if (expMs <= Date.now()) {
|
|
45
|
-
log.info("Credentials expired")
|
|
46
|
-
this.loadDeferred = null
|
|
47
|
-
this.scheduleRefresh(1)
|
|
48
|
-
this.emit("expired")
|
|
49
|
-
return null
|
|
50
|
-
}
|
|
51
|
-
return creds
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
this.scheduleRefresh(1)
|
|
55
|
-
return null
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
hasCredentials(): boolean {
|
|
59
|
-
return this.getCredentials() != null
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
private async load(): Promise<AWSCredentials> {
|
|
63
|
-
if (this.loadDeferred && !this.loadDeferred.isRejected())
|
|
64
|
-
return this.loadDeferred.promise
|
|
65
|
-
|
|
66
|
-
const deferred = this.loadDeferred = new Deferred<AWSCredentials>()
|
|
67
|
-
try {
|
|
68
|
-
const r = await fetch(API_BASE + "/creds", {
|
|
69
|
-
method: "GET",
|
|
70
|
-
credentials: "include",
|
|
71
|
-
})
|
|
72
|
-
if (!r.ok) throw new Error(`creds http ${r.status}`)
|
|
73
|
-
const creds: AWSCredentials = await r.json()
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.scheduleRefresh(creds)
|
|
77
|
-
log.info(`Got creds, expire=${creds.expiration}, stream=${creds.streamName}, region=${creds.region}`)
|
|
78
|
-
deferred.resolve(creds)
|
|
79
|
-
this.emit("received", creds)
|
|
80
|
-
return deferred.promise
|
|
81
|
-
} catch (e) {
|
|
82
|
-
deferred.reject(e)
|
|
83
|
-
this.loadDeferred = null
|
|
84
|
-
throw e
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
scheduleRefresh(credsOrMillis: AWSCredentials | number): void {
|
|
88
|
-
if (this.refreshTimer) {
|
|
89
|
-
clearTimeout(this.refreshTimer)
|
|
90
|
-
this.refreshTimer = null
|
|
91
|
-
}
|
|
92
|
-
if (!credsOrMillis)
|
|
93
|
-
return
|
|
94
|
-
|
|
95
|
-
let refreshInMs: number
|
|
96
|
-
if (typeof credsOrMillis === "number") {
|
|
97
|
-
refreshInMs = credsOrMillis
|
|
98
|
-
} else {
|
|
99
|
-
const
|
|
100
|
-
creds = credsOrMillis,
|
|
101
|
-
expMs = Date.parse(creds.expiration)
|
|
102
|
-
|
|
103
|
-
refreshInMs = expMs - Date.now() - CRED_BUFFER_PADDING_MS
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
refreshInMs = Math.max(100, refreshInMs)
|
|
108
|
-
|
|
109
|
-
this.refreshTimer = setTimeout(() => {
|
|
110
|
-
this.loadDeferred = null
|
|
111
|
-
this.load().catch((e: Error) => log.error(`refresh failed: ${e.message}`, e))
|
|
112
|
-
}, refreshInMs)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
getStreamName(): string {
|
|
116
|
-
return this.getCredentials()?.streamName
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
getRegion(): string {
|
|
120
|
-
return this.getCredentials()?.region
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
destroy(): void {
|
|
124
|
-
this.scheduleRefresh(null)
|
|
125
|
-
if (!this.loadDeferred.isSettled())
|
|
126
|
-
this.loadDeferred.reject(new Error("destroyed"))
|
|
127
|
-
this.loadDeferred = null
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
export default AWSFirehoseCredentialManager
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { Appender } from "../Appender.js"
|
|
2
|
-
import { Level } from "../Level.js"
|
|
3
|
-
import { LogContextProvider } from "./LogContextProvider.js"
|
|
4
|
-
import { LogContextContainer } from "./LogContextContainer.js"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export class LogContext {
|
|
12
|
-
/**
|
|
13
|
-
* Threshold override level
|
|
14
|
-
*
|
|
15
|
-
* @type {Level}
|
|
16
|
-
*/
|
|
17
|
-
readonly thresholdLevel: Level
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* If specified, only categories that match will
|
|
21
|
-
* use this context
|
|
22
|
-
*
|
|
23
|
-
* @type {RegExp}
|
|
24
|
-
*/
|
|
25
|
-
readonly pattern: RegExp
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Exclusive, only use this config in context. Defaults to `false`
|
|
29
|
-
*
|
|
30
|
-
* @type {boolean}
|
|
31
|
-
*/
|
|
32
|
-
readonly exclusive: RegExp
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Use a logger context within
|
|
36
|
-
*
|
|
37
|
-
* @param {() => Promise<T>} fn
|
|
38
|
-
* @returns {Promise<T>}
|
|
39
|
-
*/
|
|
40
|
-
use<T = unknown>(fn: () => Promise<T>) {
|
|
41
|
-
return LogContextContainer.runInContext(this, fn)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Creates a new logger context
|
|
46
|
-
*
|
|
47
|
-
* @param {Appender[]} appenders - additional appenders for the context
|
|
48
|
-
* @param {LoggerContextOptions} options -
|
|
49
|
-
*/
|
|
50
|
-
protected constructor(
|
|
51
|
-
readonly appenders: Appender[],
|
|
52
|
-
options: LoggerContextOptions
|
|
53
|
-
) {
|
|
54
|
-
Object.assign(this, {
|
|
55
|
-
exclusive: false,
|
|
56
|
-
thresholdLevel: null,
|
|
57
|
-
pattern: null,
|
|
58
|
-
...options
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Create a new logger context
|
|
64
|
-
*
|
|
65
|
-
* @param {Appender[]} appenders
|
|
66
|
-
* @param {LoggerContextOptions} options
|
|
67
|
-
* @returns {LogContext}
|
|
68
|
-
*/
|
|
69
|
-
static with(appenders: Appender[], options: LoggerContextOptions = {}) {
|
|
70
|
-
return new LogContext(appenders, options)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export type LoggerContextOptions = Partial<Pick<LogContext, "pattern" | "exclusive">>
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { LogContext } from "./LogContext.js"
|
|
2
|
-
import { DefaultLogContextProvider, LogContextProvider } from "./LogContextProvider.js"
|
|
3
|
-
|
|
4
|
-
const state = {
|
|
5
|
-
provider: DefaultLogContextProvider as LogContextProvider
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export namespace LogContextContainer {
|
|
10
|
-
|
|
11
|
-
export function setProvider(provider: LogContextProvider) {
|
|
12
|
-
state.provider = provider ?? DefaultLogContextProvider
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function getProvider():LogContextProvider {
|
|
16
|
-
return state.provider ?? DefaultLogContextProvider
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function currentContext(): Array<LogContext> {
|
|
20
|
-
return getProvider().currentContext?.() ?? []
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function runInContext<T = unknown>(context: LogContext, fn: () => Promise<T>): Promise<T> {
|
|
24
|
-
const provider = getProvider()
|
|
25
|
-
return provider.runInContext(context,fn)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// noinspection JSValidateJSDoc
|
|
2
|
-
|
|
3
|
-
import type { LogContext } from "./LogContext"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export interface LogContextProvider {
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Get current context stack
|
|
11
|
-
*
|
|
12
|
-
* @returns {any[]}
|
|
13
|
-
*/
|
|
14
|
-
currentContext(): Array<LogContext>
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Run in context
|
|
18
|
-
*
|
|
19
|
-
* @param {LogContext} context
|
|
20
|
-
* @param {() => Promise<T>} fn
|
|
21
|
-
* @returns {Promise<T>}
|
|
22
|
-
*/
|
|
23
|
-
runInContext<T = unknown>(context: LogContext, fn: () => Promise<T>): Promise<T>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Dummy/Noop context provider
|
|
29
|
-
*
|
|
30
|
-
* @type {{currentContext(): Array<LogContext>, runInContext<T=unknown>(context: LogContext, fn: () => Promise<T>): Promise<T>}}
|
|
31
|
-
*/
|
|
32
|
-
export const DefaultLogContextProvider:LogContextProvider = {
|
|
33
|
-
currentContext(): Array<LogContext> {
|
|
34
|
-
return []
|
|
35
|
-
},
|
|
36
|
-
runInContext<T = unknown>(context: LogContext, fn: () => Promise<T>): Promise<T> {
|
|
37
|
-
if (process.env.NODE_ENV === "development") {
|
|
38
|
-
console.warn(`Attempting to use LogContext without any installed provider`)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return fn()
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "async_hooks"
|
|
2
|
-
import { uniq } from "lodash"
|
|
3
|
-
import type { LogContext } from "../../LogContext"
|
|
4
|
-
import type { LogContextProvider } from "../../LogContextProvider"
|
|
5
|
-
import { LogContextContainer } from "../../LogContextContainer.js"
|
|
6
|
-
|
|
7
|
-
const contextStorage = new AsyncLocalStorage<Array<LogContext>>()
|
|
8
|
-
|
|
9
|
-
function currentContextStore(): Array<LogContext> {
|
|
10
|
-
return contextStorage.getStore() ?? []
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
class NodeContextProvider implements LogContextProvider {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Get current context stack
|
|
18
|
-
*
|
|
19
|
-
* @returns {any[]}
|
|
20
|
-
*/
|
|
21
|
-
currentContext(): Array<LogContext> {
|
|
22
|
-
return currentContextStore() ?? []
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Run in context
|
|
27
|
-
*
|
|
28
|
-
* @param {LogContext} context
|
|
29
|
-
* @param {() => Promise<T>} fn
|
|
30
|
-
* @returns {Promise<T>}
|
|
31
|
-
*/
|
|
32
|
-
runInContext<T = unknown>(
|
|
33
|
-
context: LogContext,
|
|
34
|
-
fn: () => Promise<T>
|
|
35
|
-
): Promise<T> {
|
|
36
|
-
const contexts = currentContextStore()
|
|
37
|
-
return contextStorage.run<Promise<T>, []>(uniq([...contexts, context]), fn)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const instance = new NodeContextProvider()
|
|
42
|
-
|
|
43
|
-
LogContextContainer.setProvider(instance)
|
|
44
|
-
|
|
45
|
-
export {}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { getLogger } from "../getLogger.js"
|
|
2
|
-
import { LevelNames } from "../Level.js"
|
|
3
|
-
import { getLoggingManager } from "../LoggingManager.js"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
getLoggingManager().configure({
|
|
7
|
-
// Default appenders list is [ConsoleAppender],
|
|
8
|
-
// so the following is not needed and only
|
|
9
|
-
// remains as an example:
|
|
10
|
-
//
|
|
11
|
-
// appenders: [new ConsoleAppender()],
|
|
12
|
-
rootLevel: "trace"
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
const log = getLogger(__filename)
|
|
16
|
-
|
|
17
|
-
LevelNames.forEach((name) =>
|
|
18
|
-
log[name].call(log, `example %s`, name)
|
|
19
|
-
)
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { FileAppender } from "../appenders/FileAppender.js"
|
|
2
|
-
import { getLogger } from "../getLogger.js"
|
|
3
|
-
import { LevelNames } from "../Level.js"
|
|
4
|
-
import { getLoggingManager } from "../LoggingManager.js"
|
|
5
|
-
|
|
6
|
-
async function run() {
|
|
7
|
-
const manager = getLoggingManager()
|
|
8
|
-
const fileAppender = new FileAppender({
|
|
9
|
-
enableRolling: true,
|
|
10
|
-
maxFiles: 4,
|
|
11
|
-
maxSize: 20480
|
|
12
|
-
})
|
|
13
|
-
manager.setAppenders(fileAppender).setRootLevel("trace")
|
|
14
|
-
|
|
15
|
-
const log = getLogger(__filename)
|
|
16
|
-
await fileAppender.close()
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
run().catch((err) => console.error(`failed`, err))
|
package/src/logging/getLogger.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Logger, LoggerOptions } from "./Logger"
|
|
2
|
-
import { getLoggingManager } from "./LoggingManager.js"
|
|
3
|
-
|
|
4
|
-
export function getLogger(
|
|
5
|
-
category: string,
|
|
6
|
-
options: Partial<LoggerOptions> = {}
|
|
7
|
-
): Logger {
|
|
8
|
-
return getLoggingManager().getLogger(category, options)
|
|
9
|
-
}
|
package/src/logging/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from "./Appender.js"
|
|
2
|
-
export * from "./Formatter.js"
|
|
3
|
-
export * from "./Level.js"
|
|
4
|
-
export * from "./LogRecord.js"
|
|
5
|
-
export * from "./Logger.js"
|
|
6
|
-
export * from "./LoggingManager.js"
|
|
7
|
-
export * from "./appenders/index.js"
|
|
8
|
-
export * from "./getLogger.js"
|
|
9
|
-
export * from "./util.js"
|
|
10
|
-
export * from "./context/index.js"
|
|
11
|
-
|
package/src/logging/util.ts
DELETED