Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: node-jet CI
env:
NODE_VERSION: '20.8'
NODE_VERSION: '26.5'
on:
release:
types: [created]
Expand Down
2 changes: 1 addition & 1 deletion examples/balls/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<body>
<svg style="max-height: 90vh"></svg>
<form onsubmit="event.preventDefault();">
<form onsubmit="event.preventDefault()">
<button id="circle">circle!</button>
<button id="square">square!</button>
<button id="boom">boom!</button>
Expand Down
10,426 changes: 3,253 additions & 7,173 deletions package-lock.json

Large diffs are not rendered by default.

45 changes: 15 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,27 @@
"ex:balls": "npm run build && concurrently \"webpack serve --config examples/balls/webpack.config.cjs\" \"wait-on examples/balls/dist/server.cjs && node examples/balls/dist/server.cjs\""
},
"dependencies": {
"css-loader": "^7.1.2",
"css-loader": "^7.1.4",
"events": "^3.3.0",
"html-webpack-plugin": "^5.6.3",
"jsdoc": "^4.0.4",
"nanoid": "^5.1.5",
"html-webpack-plugin": "^5.6.8",
"jsdoc": "^4.0.5",
"nanoid": "^6.0.1",
"net": "^1.0.2",
"postcss-loader": "^8.1.1",
"postcss-loader": "^8.2.1",
"style-loader": "^4.0.0",
"ts-loader": "^9.5.2",
"ws": "^8.18.3"
"ts-loader": "^9.6.2",
"ws": "^8.21.3"
},
"devDependencies": {
"@eslint/compat": "^1.3.1",
"@types/d3-selection": "^3.0.11",
"@types/jest": "^30.0.0",
"@types/prettier": "^3.0.0",
"@types/uuid": "^10.0.0",
"@eslint/compat": "^2.1.0",
"@testing-library/dom": "^10.4.1",
"@types/events": "^3.0.3",
"@types/ws": "^8.18.1",
"@typescript-eslint/eslint-plugin": "^8.38.0",
"concurrently": "^9.2.0",
"d3": "^7.9.0",
"eslint": "^9.32.0",
"eslint-config-love": "^121.0.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.3",
"jest": "^30.0.5",
"jest-environment-jsdom": "^30.0.5",
"lodash": "^4.17.21",
"prettier": "^3.6.2",
"ts-jest": "^29.4.0",
"typescript": "^5.8.3",
"wait-for-expect": "^3.0.2",
"wait-on": "^8.0.4",
"webpack": "^5.101.0",
"webpack-cli": "^6.0.1",
"webpack-dev-server": "^5.2.2"
"eslint-config-love": "^155.0.0",
"jest": "^30.4.2",
"jest-environment-jsdom": "^30.4.1",
"prettier": "^3.9.6",
"ts-jest": "^29.4.12"
},
"main": "./lib/index.js",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions src/1_socket/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* istanbul ignore file */
import { WebSocket as ws } from 'ws'

import { EventEmitter as ee } from 'events'
export const isNodeJs = typeof window === 'undefined'
export const isBrowser = typeof window !== 'undefined'

export const WebSocketImpl = isNodeJs ? ws : WebSocket
export const WebSocketImpl = WebSocket

export const EventEmitter = ee
13 changes: 5 additions & 8 deletions src/1_socket/socket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* istanbul ignore file */
import { WebSocket as ws } from 'ws'

import MessageSocket from './message-socket.js'
import { isBrowser, isNodeJs } from './index.js'

Expand All @@ -9,10 +9,10 @@ import { isBrowser, isNodeJs } from './index.js'
*/
export class Socket {
id = ''
sock?: WebSocket | MessageSocket | ws
sock?: WebSocket | MessageSocket
type = ''

constructor(socket?: WebSocket | MessageSocket | ws) {
constructor(socket?: WebSocket | MessageSocket) {
if (socket) {
this.sock = socket
this.type = socket.constructor.name === 'MessageSocket' ? 'ms' : 'ws'
Expand All @@ -29,15 +29,12 @@ export class Socket {
ip: string | undefined = undefined,
port: number | undefined = undefined
) => {
if (isBrowser) {
if (isBrowser || (isNodeJs && url)) {
this.sock = new WebSocket(
url || `ws://${window.location.host}:${port || 2315}`,
'jet'
)
this.type = 'ws'
} else if (isNodeJs && url) {
this.sock = new ws(url, 'jet')
this.type = 'ws'
} else {
this.sock = new MessageSocket(port || 11122, ip)
this.type = 'ms'
Expand Down Expand Up @@ -67,7 +64,7 @@ export class Socket {
if ((this.type === 'ws' && isBrowser) || this.type === 'ms') {
;(this.sock as WebSocket).addEventListener(event, cb)
} else if (this.type === 'ws' && isNodeJs) {
;(this.sock as ws).addEventListener(event as any, cb as any)
;(this.sock as WebSocket).addEventListener(event as any, cb as any)
} else {
throw Error('Could not detect socket type')
}
Expand Down
18 changes: 4 additions & 14 deletions src/1_socket/wsserver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */
import { EventEmitter, WebSocketImpl } from './index.js'
import { type WebSocket, WebSocketServer as WsServer } from 'ws'
import { EventEmitter } from './index.js'
import { WebSocketServer as WsServer } from 'ws'
import type { Server as HTTPServer } from 'http'
import { Socket } from './socket.js'

Expand Down Expand Up @@ -43,20 +43,10 @@ export class WebsocketServer extends EventEmitter {
const sock = new Socket(ws)
sock.id = `ws_${this.connectionId}`
this.connectionId++
const pingMs = this.config.wsPingInterval || 5000
let pingInterval: NodeJS.Timeout
if (pingMs) {
pingInterval = setInterval(() => {
if (ws.readyState === WebSocketImpl.OPEN) {
ws.ping()
}
}, pingMs)
}
ws.addListener('close', () => {
clearInterval(pingInterval)
ws.addEventListener('close', () => {
this.emit('disconnect', sock)
})
ws.addListener('disconnect', () => {
ws.addEventListener('disconnect', () => {
this.emit('disconnect', sock)
})

Expand Down
3 changes: 1 addition & 2 deletions src/2_jsonrpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const encode = JSON.stringify
const decode = JSON.parse

export type resultCallback =
| ((_success: boolean, _result?: object) => void)
| undefined
((_success: boolean, _result?: object) => void) | undefined

const isResultMessage = (msg: Message): msg is ResultMessage => 'result' in msg
const isErrorMessage = (msg: Message): msg is ErrorMessage => 'error' in msg
Expand Down
12 changes: 6 additions & 6 deletions src/3_jet/log.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export enum LogLevel {
'socket' = 1,
'debug',
'info',
'warn',
'error',
'none'
socket = 1,
debug,
info,
warn,
error,
none
}

type LogFunction = (...args: string[]) => void
Expand Down
16 changes: 5 additions & 11 deletions test/jsonrpc/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { sockMock } from '../mocks/sock'
import JsonRPC from '../../src/2_jsonrpc'
import { Logger } from '../../src/3_jet/log'
import { LogLevel } from '../../src/'
import waitForExpect from 'wait-for-expect'
import { waitFor } from '@testing-library/dom'
import {
ConnectionClosed,
INVALID_PARAMS_CODE,
Expand Down Expand Up @@ -148,9 +148,7 @@ describe('Testing JsonRpc', () => {
jsonrpc.send()
})

.then(() =>
waitForExpect(() => expect(sock.send).toHaveBeenCalledTimes(1))
)
.then(() => waitFor(() => expect(sock.send).toHaveBeenCalledTimes(1)))
.then(done())
sock.emit('open')
})
Expand All @@ -175,9 +173,7 @@ describe('Testing JsonRpc', () => {
jsonrpc.queue({ event: 'Add', path: 'foo', value: 1 } as any, '_f')
})

.then(() =>
waitForExpect(() => expect(sock.send).toHaveBeenCalledTimes(1))
)
.then(() => waitFor(() => expect(sock.send).toHaveBeenCalledTimes(1)))
.then(done())
sock.emit('open')
})
Expand All @@ -198,9 +194,7 @@ describe('Testing JsonRpc', () => {
})
})

.then(() =>
waitForExpect(() => expect(sock.send).toHaveBeenCalledTimes(1))
)
.then(() => waitFor(() => expect(sock.send).toHaveBeenCalledTimes(1)))
.then(() => done())
sock.emit('open')
})
Expand Down Expand Up @@ -319,7 +313,7 @@ describe('Testing JsonRpc', () => {

sock.emit('message', { data: JSON.stringify(messages) })
})
// .then(() => waitForExpect(() => expect(msgMock).toHaveBeenCalledTimes(3)))
// .then(() => waitFor(() => expect(msgMock).toHaveBeenCalledTimes(3)))
.then(() => done())
sock.emit('open')
})
Expand Down
8 changes: 4 additions & 4 deletions test/peer/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ValueType } from '../../src/3_jet/types'
import { Fetcher, invalidMethod, NotFound } from '../../src/jet'
import { fullFetcherPeer, simpleFecherPeer } from '../mocks/peer'
import { fetchSimpleId } from '../../src/3_jet/types'
import waitForExpect from 'wait-for-expect'
import { InvalidParamError } from '../../src/jet'
import { waitFor } from '@testing-library/dom'
describe('Testing Peer', () => {
describe('Should handle daemon messages', () => {
describe('Should send different messages full fetch', () => {
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('Testing Peer', () => {
.add(m)
.then(() => cbs['get'](undefined, 'fooId', { path: 'foo' }))
.then(() =>
waitForExpect(() =>
waitFor(() =>
expect(jsonRpc.respond).toHaveBeenCalledWith(
'fooId',
new invalidMethod(),
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('Testing Peer', () => {
.add(m)
.then(() => cbs['set'](undefined, 'fooId', { path: 'foo' }))
.then(() =>
waitForExpect(() =>
waitFor(() =>
expect(jsonRpc.respond).toHaveBeenCalledWith(
'fooId',
new invalidMethod(),
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Testing Peer', () => {
.add(m)
.then(() => cbs['call'](undefined, 'fooId', { path: 'foo' }))
.then(() =>
waitForExpect(() =>
waitFor(() =>
expect(jsonRpc.respond).toHaveBeenCalledWith(
'fooId',
new invalidMethod(),
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"checkJs": true,
"jsx": "react",
"declaration": true,
"rootDir": ".",
"outDir": "lib",
"noEmit": false,
"strict": true,
"noImplicitAny": true,

"noUnusedLocals": true,
"noUnusedParameters": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
Expand Down
Loading