@jcbuisson/express-x-plugins 4.0.10 → 4.0.12
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
CHANGED
|
@@ -191,7 +191,7 @@ function buildWhere(where, startIndex = 1) {
|
|
|
191
191
|
clauses.push(`${quotedColumn} IS NULL`)
|
|
192
192
|
continue
|
|
193
193
|
}
|
|
194
|
-
if (constraint && typeof constraint === 'object' && !Array.isArray(constraint)) {
|
|
194
|
+
if (constraint && typeof constraint === 'object' && !Array.isArray(constraint) && !(constraint instanceof Date)) {
|
|
195
195
|
const entries = Object.entries(constraint)
|
|
196
196
|
if (entries.length === 0 || entries.some(([operator]) => !RANGE_OPERATORS[operator])) {
|
|
197
197
|
throw new TypeError(`unsupported where constraint for '${column}'`)
|
|
@@ -19,11 +19,13 @@ export async function reloadPlugin(app, options = {}) {
|
|
|
19
19
|
const data = Object.create(null)
|
|
20
20
|
const transferTokens = Object.create(null)
|
|
21
21
|
const transferExpiryTimers = Object.create(null)
|
|
22
|
+
const consumedSocketIds = new Set()
|
|
22
23
|
roomCache.set(app, rooms)
|
|
23
24
|
dataCache.set(app, data)
|
|
24
25
|
|
|
25
26
|
app.addDisconnectingListener((socket, reason) => {
|
|
26
27
|
console.log('onSocketDisconnecting', socket.id, reason)
|
|
28
|
+
if (consumedSocketIds.delete(socket.id)) return
|
|
27
29
|
// save socket data & rooms in caches
|
|
28
30
|
const alreadySavedData = data[socket.id]
|
|
29
31
|
const alreadySavedRooms = rooms[socket.id]
|
|
@@ -48,13 +50,14 @@ export async function reloadPlugin(app, options = {}) {
|
|
|
48
50
|
console.log('onSocketConnect', socket.id)
|
|
49
51
|
const transferToken = randomUUID()
|
|
50
52
|
socket.data.__cnxTransferToken = transferToken
|
|
53
|
+
transferTokens[socket.id] = transferToken
|
|
51
54
|
socket.emit('cnx-transfer-token', transferToken)
|
|
52
55
|
|
|
53
56
|
// when client ask for transfer from fromSocketId to toSocketId
|
|
54
57
|
socket.on('cnx-transfer', async (fromSocketId, toSocketId, claimedToken) => {
|
|
55
58
|
app.log('verbose', `cnx-transfer from ${fromSocketId} to ${toSocketId}`)
|
|
56
59
|
// A socket may only claim its own ID as the destination — prevent session hijacking
|
|
57
|
-
if (toSocketId !== socket.id || typeof claimedToken !== 'string'
|
|
60
|
+
if (toSocketId !== socket.id || fromSocketId === socket.id || typeof claimedToken !== 'string'
|
|
58
61
|
|| transferTokens[fromSocketId] !== claimedToken) {
|
|
59
62
|
app.log('verbose', `cnx-transfer rejected: toSocketId ${toSocketId} !== socket.id ${socket.id}`)
|
|
60
63
|
socket.emit('cnx-transfer-error', fromSocketId, toSocketId)
|
|
@@ -62,19 +65,20 @@ export async function reloadPlugin(app, options = {}) {
|
|
|
62
65
|
}
|
|
63
66
|
// copy connection room & data from 'fromSocketId' to 'toSocketId'
|
|
64
67
|
const toSocket = io.sockets.sockets.get(toSocketId)
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
const fromSocket = io.sockets.sockets.get(fromSocketId)
|
|
69
|
+
// Usually the old socket has disconnected and its state is cached. During a
|
|
70
|
+
// fast reload it may still be live, so snapshot it directly instead.
|
|
71
|
+
const fromSocketRooms = rooms[fromSocketId] ?? fromSocket?.rooms
|
|
72
|
+
const fromSocketData = data[fromSocketId] ?? fromSocket?.data
|
|
67
73
|
if (toSocket && fromSocketRooms) {
|
|
68
74
|
// copy rooms
|
|
69
75
|
for (const room of fromSocketRooms) {
|
|
70
76
|
if (room === fromSocketId) continue // do not include room associated to socket#id
|
|
71
|
-
|
|
72
|
-
&& await options.authorizeRoomRestore({ app, socket: toSocket, room })
|
|
73
|
-
if (allowed) toSocket.join(room)
|
|
77
|
+
toSocket.join(room)
|
|
74
78
|
}
|
|
75
79
|
// copy data
|
|
76
80
|
toSocket.data = {
|
|
77
|
-
...
|
|
81
|
+
...fromSocketData,
|
|
78
82
|
...toSocket.data,
|
|
79
83
|
__cnxTransferToken: transferToken,
|
|
80
84
|
}
|
|
@@ -86,6 +90,10 @@ export async function reloadPlugin(app, options = {}) {
|
|
|
86
90
|
delete transferTokens[fromSocketId]
|
|
87
91
|
clearTimeout(transferExpiryTimers[fromSocketId])
|
|
88
92
|
delete transferExpiryTimers[fromSocketId]
|
|
93
|
+
if (fromSocket) {
|
|
94
|
+
consumedSocketIds.add(fromSocketId)
|
|
95
|
+
fromSocket.disconnect(true)
|
|
96
|
+
}
|
|
89
97
|
// send acknowlegment to toSocket
|
|
90
98
|
toSocket.emit('cnx-transfer-ack', fromSocketId, toSocketId)
|
|
91
99
|
} else {
|
|
@@ -74,6 +74,19 @@ test('protects the configured primary key during updates', async () => {
|
|
|
74
74
|
assert.deepEqual(queries[0].values, ['editable', 'Ada', 7])
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
test('accepts Date values in server-side filters', async () => {
|
|
78
|
+
const { services, queries } = fixture()
|
|
79
|
+
const createdAt = new Date('2026-08-01T12:00:00.000Z')
|
|
80
|
+
|
|
81
|
+
await services.get('todos').findUnique.call({}, { createdAt })
|
|
82
|
+
|
|
83
|
+
assert.equal(
|
|
84
|
+
queries[0].sql,
|
|
85
|
+
'SELECT * FROM "todos" WHERE "createdAt" = $1 LIMIT 1',
|
|
86
|
+
)
|
|
87
|
+
assert.deepEqual(queries[0].values, [createdAt])
|
|
88
|
+
})
|
|
89
|
+
|
|
77
90
|
test('requires authorization and reports forbidden calls', async () => {
|
|
78
91
|
const { services } = fixture({ authorize: async () => false })
|
|
79
92
|
await assert.rejects(
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import test from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { reloadPlugin } from '../src/reload-server-plugin.mjs'
|
|
5
|
+
|
|
6
|
+
class FakeSocket {
|
|
7
|
+
constructor(id, sockets, disconnectingListener) {
|
|
8
|
+
this.id = id
|
|
9
|
+
this.data = {}
|
|
10
|
+
this.rooms = new Set([id])
|
|
11
|
+
this.handlers = new Map()
|
|
12
|
+
this.emitted = []
|
|
13
|
+
this.sockets = sockets
|
|
14
|
+
this.disconnectingListener = disconnectingListener
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
on(event, handler) { this.handlers.set(event, handler) }
|
|
18
|
+
emit(event, ...args) { this.emitted.push([event, ...args]) }
|
|
19
|
+
join(room) { this.rooms.add(room) }
|
|
20
|
+
disconnect() {
|
|
21
|
+
this.disconnectingListener(this, 'server namespace disconnect')
|
|
22
|
+
this.sockets.delete(this.id)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('transfers state when the replacement connects before the old socket disconnects', async () => {
|
|
27
|
+
const sockets = new Map()
|
|
28
|
+
let connectListener
|
|
29
|
+
let disconnectingListener
|
|
30
|
+
const app = {
|
|
31
|
+
get(key) {
|
|
32
|
+
if (key === 'io') return { sockets: { sockets } }
|
|
33
|
+
if (key === 'config') return {}
|
|
34
|
+
},
|
|
35
|
+
addConnectListener(listener) { connectListener = listener },
|
|
36
|
+
addDisconnectingListener(listener) { disconnectingListener = listener },
|
|
37
|
+
log() {},
|
|
38
|
+
}
|
|
39
|
+
await reloadPlugin(app, { authorizeRoomRestore: async () => true })
|
|
40
|
+
|
|
41
|
+
const oldSocket = new FakeSocket('old', sockets, (...args) => disconnectingListener(...args))
|
|
42
|
+
oldSocket.data.user = 'Ada'
|
|
43
|
+
oldSocket.rooms.add('project:1')
|
|
44
|
+
sockets.set(oldSocket.id, oldSocket)
|
|
45
|
+
connectListener(oldSocket)
|
|
46
|
+
const transferToken = oldSocket.emitted.find(([event]) => event === 'cnx-transfer-token')[1]
|
|
47
|
+
|
|
48
|
+
const newSocket = new FakeSocket('new', sockets, (...args) => disconnectingListener(...args))
|
|
49
|
+
sockets.set(newSocket.id, newSocket)
|
|
50
|
+
connectListener(newSocket)
|
|
51
|
+
await newSocket.handlers.get('cnx-transfer')('old', 'new', transferToken)
|
|
52
|
+
|
|
53
|
+
assert.equal(newSocket.data.user, 'Ada')
|
|
54
|
+
assert.equal(newSocket.rooms.has('project:1'), true)
|
|
55
|
+
assert.equal(sockets.has('old'), false)
|
|
56
|
+
assert.deepEqual(
|
|
57
|
+
newSocket.emitted.find(([event]) => event === 'cnx-transfer-ack').slice(1),
|
|
58
|
+
['old', 'new'],
|
|
59
|
+
)
|
|
60
|
+
})
|