Skip to content

Commit

Permalink
Merge branch 'release/v0.55.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Jul 5, 2024
2 parents 1e3b54d + 7913e6e commit 1b9f927
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zerva/monorepo",
"version": "0.55.4",
"version": "0.55.5",
"private": true,
"scripts": {
"build": "pnpm -r --filter=@zerva/* build",
Expand Down
2 changes: 1 addition & 1 deletion zerva-bin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zerva/bin",
"type": "module",
"version": "0.55.3",
"version": "0.55.5",
"description": "🌱 Zerva Command Line Tool",
"author": {
"email": "[email protected]",
Expand Down
2 changes: 2 additions & 0 deletions zerva-bin/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { BuildOptions, Plugin } from 'esbuild'
import { context } from 'esbuild'
import { yamlPlugin } from 'esbuild-plugin-yaml'
import displayNotification from 'display-notification'
import { valueToBoolean } from 'zeed'
import type { ZervaConf } from './config'

const DEFAULT_EXCLUDE = [
Expand Down Expand Up @@ -247,6 +248,7 @@ const __dirname = (await import("node:path")).dirname(__filename)`
'process.env.ZERVA_DEVELOPMENT': String(!config.build),
'process.env.ZERVA_PRODUCTION': String(config.build),
'process.env.ZERVA_VERSION': `"${config.version}"`,
'DEBUG': String(valueToBoolean(process.env.DEFINE, false)),
...config.define,
} as any,
minify: config.build,
Expand Down
2 changes: 1 addition & 1 deletion zerva-websocket/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zerva/websocket",
"type": "module",
"version": "0.55.3",
"version": "0.55.4",
"description": "🌱 Zerva websocket module",
"author": {
"name": "Dirk Holtwick",
Expand Down
11 changes: 6 additions & 5 deletions zerva-websocket/src/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// (C)opyright 2021-07-15 Dirk Holtwick, holtwick.it. All rights reserved.

import { on, serve, serveStop, setContext } from '@zerva/core'
import { useHttp } from '@zerva/http'
import WebSocket from 'ws'
import { Uint8ArrayToString, createPromise, stringToUInt8Array, waitOn } from 'zeed'
import { Uint8ArrayToString, createPromise, stringToUInt8Array, useDispose, waitOn } from 'zeed'
import { WebSocketConnection } from './client'
import { useWebSocket } from './server'
import { webSocketPath } from './_types'

// @ts-expect-error xxx
globalThis.WebSocket = WebSocket

// const log = Logger("test:module")

const port = 8886
const webSocketPath = '/testClient'
const url = `ws://localhost:${port}${webSocketPath}`

describe('connection', () => {
const dispose = useDispose()

beforeAll(async () => {
setContext() // Avoid conflict of multiple registration

useHttp({ port })
useWebSocket({})
dispose.add(useWebSocket({ path: webSocketPath }))

on('webSocketConnect', ({ channel }) => {
channel.on('message', (msg) => {
Expand All @@ -37,6 +37,7 @@ describe('connection', () => {

afterAll(async () => {
await serveStop()
await dispose()
})

it('should ping', async () => {
Expand Down
5 changes: 4 additions & 1 deletion zerva-websocket/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { LogConfig, LoggerInterface } from 'zeed'
import { Channel, LogLevelInfo, LoggerFromConfig, createPromise, equalBinary, getTimestamp, isBrowser, useDispose, useEventListener } from 'zeed'
import { pingMessage, pongMessage, webSocketPath, wsReadyStateConnecting, wsReadyStateOpen } from './_types'
import { useSingletonFlag } from './singleton'

// See lib0 and y-websocket for initial implementation

Expand Down Expand Up @@ -31,7 +32,7 @@ const PERFORM_RETRY = true

export class WebSocketConnection extends Channel {
public ws?: WebSocket
public url: string | URL
public url: string
public shouldConnect = true
public isConnected = false
public lastMessageReceived = 0
Expand All @@ -57,6 +58,8 @@ export class WebSocketConnection extends Channel {
this.opt = opt
this.url = url ?? getWebsocketUrlFromLocation(path)

this.dispose.add(useSingletonFlag(`_zerva_websocket_client_${this.url}`, this.log))

if (isBrowser()) {
this.dispose.add(useEventListener(window, 'beforeunload', () => this.dispose()))
this.dispose.add(useEventListener(window, 'focus', () => this.ping()))
Expand Down
2 changes: 0 additions & 2 deletions zerva-websocket/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// (C)opyright 2021-07-15 Dirk Holtwick, holtwick.it. All rights reserved.

export * from './_types'
export * from './server'
export * from './client'
6 changes: 2 additions & 4 deletions zerva-websocket/src/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// (C)opyright 2021-07-15 Dirk Holtwick, holtwick.it. All rights reserved.

import { on, serve, serveStop, setContext } from '@zerva/core'
import { useHttp } from '@zerva/http'
import WebSocket from 'ws'
Expand Down Expand Up @@ -106,7 +104,7 @@ describe('module', () => {
expect(err.message).toBe('fakeError')
}

channel.dispose()
await channel.dispose()
})

it('should ping', async () => {
Expand All @@ -117,6 +115,6 @@ describe('module', () => {
await sleep(2000)
// 2000 / 600 = 3.333...
expect(channel.pingCount).toBe(3)
channel.dispose()
await channel.dispose()
}, 5000)
})
12 changes: 8 additions & 4 deletions zerva-websocket/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// (C)opyright 2021 Dirk Holtwick, holtwick.it. All rights reserved.

import '@zerva/http'
import type { Buffer } from 'node:buffer'
import { URL } from 'node:url'
import { assertModules, emit, on, once, register } from '@zerva/core'
import type WebSocket from 'ws'
import { WebSocketServer } from 'ws'
import type { LogConfig, LogLevelAliasType, LoggerInterface, UseDispose } from 'zeed'
import { Channel, LogLevelInfo, LoggerFromConfig, equalBinary, uname, useDispose, uuid } from 'zeed'
import { pingMessage, pongMessage, websocketName, wsReadyStateConnecting, wsReadyStateOpen } from './_types'
import '@zerva/http'
import { pingMessage, pongMessage, websocketName, wsReadyStateOpen } from './_types'
import { useSingletonFlag } from './singleton'

const moduleName = 'websocket'

Expand Down Expand Up @@ -219,6 +218,9 @@ export function useWebSocket(config: ZWebSocketConfig = {}) {
path = `/${path}`
config.path = path

const dispose = useDispose()
dispose.add(useSingletonFlag(`_zerva_websocket_server_${config.path}`))

on('httpInit', ({ http }) => {
log(`init path=${path}`)

Expand Down Expand Up @@ -274,4 +276,6 @@ export function useWebSocket(config: ZWebSocketConfig = {}) {
log.info('server stop done')
})
})

return dispose
}
8 changes: 8 additions & 0 deletions zerva-websocket/src/singleton.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useSingletonFlag } from './singleton'

describe('singleton.spec', () => {
it('should only once', async () => {
const dispose = useSingletonFlag('test')
dispose()
})
})
25 changes: 25 additions & 0 deletions zerva-websocket/src/singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { LoggerInterface } from 'zeed'

/** Make sure the flag is only in use once globally. */
export function useSingletonFlag(name: string, log?: LoggerInterface) {
const _global = globalThis as any
if (_global._zerva_singleton_flags == null)
_global._zerva_singleton_flags = {}

if (_global._zerva_singleton_flags[name] === true) {
const msg = `Singleton named '${name}' called twice!`
log?.error(msg)
throw new Error(msg)
}

_global._zerva_singleton_flags[name] = true

return () => {
if (_global._zerva_singleton_flags[name] !== true) {
const msg = `Singleton named '${name}' has been disposed more than once!`
log?.error(msg)
throw new Error(msg)
}
_global._zerva_singleton_flags[name] = false
}
}

0 comments on commit 1b9f927

Please sign in to comment.