Skip to content

Commit 527f3fb

Browse files
authored
fix: set log level on params (#456)
* Deprecation of log_level to logLevel * Exports type of log level supported * Fixes log level being sent in URL params * Changes heartbeat to default to 25s
1 parent 3910cd0 commit 527f3fb

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

src/RealtimeClient.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
DEFAULT_TIMEOUT,
88
SOCKET_STATES,
99
TRANSPORTS,
10-
VERSION,
1110
VSN,
1211
WS_CLOSE_NORMAL,
12+
LOG_LEVEL,
1313
} from './lib/constants'
14+
1415
import Serializer from './lib/serializer'
1516
import Timer from './lib/timer'
1617

@@ -26,23 +27,7 @@ export type Channel = {
2627
updated_at: string
2728
id: number
2829
}
29-
30-
export type RealtimeClientOptions = {
31-
transport?: WebSocketLikeConstructor
32-
timeout?: number
33-
heartbeatIntervalMs?: number
34-
logger?: Function
35-
encode?: Function
36-
decode?: Function
37-
reconnectAfterMs?: Function
38-
headers?: { [key: string]: string }
39-
params?: { [key: string]: any }
40-
log_level?: 'info' | 'debug' | 'warn' | 'error'
41-
fetch?: Fetch
42-
worker?: boolean
43-
workerUrl?: string
44-
accessToken?: () => Promise<string | null>
45-
}
30+
export type LogLevel = LOG_LEVEL
4631

4732
export type RealtimeMessage = {
4833
topic: string
@@ -72,6 +57,25 @@ export interface WebSocketLikeError {
7257
type: string
7358
}
7459

60+
export type RealtimeClientOptions = {
61+
transport?: WebSocketLikeConstructor
62+
timeout?: number
63+
heartbeatIntervalMs?: number
64+
logger?: Function
65+
encode?: Function
66+
decode?: Function
67+
reconnectAfterMs?: Function
68+
headers?: { [key: string]: string }
69+
params?: { [key: string]: any }
70+
//Deprecated: Use it in favour of correct casing `logLevel`
71+
log_level?: LogLevel
72+
logLevel?: LogLevel
73+
fetch?: Fetch
74+
worker?: boolean
75+
workerUrl?: string
76+
accessToken?: () => Promise<string | null>
77+
}
78+
7579
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'
7680
const WORKER_SCRIPT = `
7781
addEventListener("message", (e) => {
@@ -89,12 +93,13 @@ export default class RealtimeClient {
8993
params?: { [key: string]: string } = {}
9094
timeout: number = DEFAULT_TIMEOUT
9195
transport: WebSocketLikeConstructor | null
92-
heartbeatIntervalMs: number = 30000
96+
heartbeatIntervalMs: number = 25000
9397
heartbeatTimer: ReturnType<typeof setInterval> | undefined = undefined
9498
pendingHeartbeatRef: string | null = null
9599
ref: number = 0
96100
reconnectTimer: Timer
97101
logger: Function = noop
102+
logLevel?: LogLevel
98103
encode: Function
99104
decode: Function
100105
reconnectAfterMs: Function
@@ -129,6 +134,7 @@ export default class RealtimeClient {
129134
* @param options.headers The optional headers to pass when connecting.
130135
* @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
131136
* @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
137+
* @param options.logLevel Sets the log level for Realtime
132138
* @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
133139
* @param options.decode The function to decode incoming messages. Defaults to Serializer's decode.
134140
* @param options.reconnectAfterMs he optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
@@ -147,6 +153,11 @@ export default class RealtimeClient {
147153
if (options?.headers) this.headers = { ...this.headers, ...options.headers }
148154
if (options?.timeout) this.timeout = options.timeout
149155
if (options?.logger) this.logger = options.logger
156+
if (options?.logLevel || options?.log_level) {
157+
this.logLevel = options.logLevel || options.log_level
158+
this.params = { ...this.params, log_level: this.logLevel as string }
159+
}
160+
150161
if (options?.heartbeatIntervalMs)
151162
this.heartbeatIntervalMs = options.heartbeatIntervalMs
152163

src/lib/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ export enum CONNECTION_STATE {
4444
Closing = 'closing',
4545
Closed = 'closed',
4646
}
47+
48+
export enum LOG_LEVEL {
49+
Info = 'info',
50+
Warn = 'warn',
51+
Error = 'error',
52+
}

test/socket.test.ts renamed to test/client.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import crypto from 'crypto'
77

88
import RealtimeClient from '../src/RealtimeClient'
99
import jwt from 'jsonwebtoken'
10-
import { CHANNEL_STATES } from '../src/lib/constants'
10+
import { CHANNEL_STATES, LOG_LEVEL } from '../src/lib/constants'
1111

1212
function generateJWT(exp: string): string {
1313
return jwt.sign({}, 'your-256-bit-secret', {
@@ -55,7 +55,7 @@ describe('constructor', () => {
5555
})
5656
assert.equal(socket.transport, null)
5757
assert.equal(socket.timeout, 10000)
58-
assert.equal(socket.heartbeatIntervalMs, 30000)
58+
assert.equal(socket.heartbeatIntervalMs, 25000)
5959
assert.equal(typeof socket.logger, 'function')
6060
assert.equal(typeof socket.reconnectAfterMs, 'function')
6161
})
@@ -749,3 +749,34 @@ describe('custom encoder and decoder', () => {
749749
})
750750
})
751751
})
752+
753+
describe('log operations', () => {
754+
test('calls the logger with the correct arguments', () => {
755+
const mockLogger = vi.fn()
756+
socket = new RealtimeClient(url, { logger: mockLogger })
757+
758+
socket.log('testKind', 'testMessage', { testData: 'test' })
759+
760+
expect(mockLogger).toHaveBeenCalledWith('testKind', 'testMessage', {
761+
testData: 'test',
762+
})
763+
})
764+
test('changing log_level sends proper params in URL', () => {
765+
socket = new RealtimeClient(url, { log_level: LOG_LEVEL.Warn })
766+
767+
assert.equal(socket.logLevel, LOG_LEVEL.Warn)
768+
assert.equal(
769+
socket.endpointURL(),
770+
`${url}/websocket?log_level=warn&vsn=1.0.0`
771+
)
772+
})
773+
test('changing logLevel sends proper params in URL', () => {
774+
socket = new RealtimeClient(url, { logLevel: LOG_LEVEL.Warn })
775+
776+
assert.equal(socket.logLevel, LOG_LEVEL.Warn)
777+
assert.equal(
778+
socket.endpointURL(),
779+
`${url}/websocket?log_level=warn&vsn=1.0.0`
780+
)
781+
})
782+
})

0 commit comments

Comments
 (0)