@jcbuisson/express-x-client 1.5.6 → 1.5.8
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 +1 -1
- package/src/index.mjs +17 -6
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import { v4 } from 'uuid'
|
|
3
|
-
|
|
1
|
+
|
|
4
2
|
export default function expressXClient(socket, options={}) {
|
|
5
3
|
if (options.debug === undefined) options.debug = false
|
|
4
|
+
if (options.timeout === undefined) options.timeout = 5000
|
|
6
5
|
|
|
7
6
|
const waitingPromisesByUid = {}
|
|
8
7
|
const action2service2handlers = {}
|
|
@@ -127,14 +126,14 @@ export default function expressXClient(socket, options={}) {
|
|
|
127
126
|
if (retries === 0) throw new Error(`Timeout waiting for reconnection`)
|
|
128
127
|
|
|
129
128
|
// create a promise which will resolve or reject by an event 'client-response'
|
|
130
|
-
const uid =
|
|
129
|
+
const uid = generateUID(20)
|
|
131
130
|
const promise = new Promise((resolve, reject) => {
|
|
132
131
|
waitingPromisesByUid[uid] = [resolve, reject]
|
|
133
|
-
// a
|
|
132
|
+
// a timeout may also reject the promise
|
|
134
133
|
setTimeout(() => {
|
|
135
134
|
delete waitingPromisesByUid[uid]
|
|
136
135
|
reject(`Error: timeout on service '${name}', action '${action}', args: ${JSON.stringify(args)}`)
|
|
137
|
-
},
|
|
136
|
+
}, options.timeout)
|
|
138
137
|
})
|
|
139
138
|
// send request to server through websocket
|
|
140
139
|
if (options.debug) console.log('client-request', uid, name, action, args)
|
|
@@ -175,3 +174,15 @@ export default function expressXClient(socket, options={}) {
|
|
|
175
174
|
service,
|
|
176
175
|
}
|
|
177
176
|
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
function generateUID(length) {
|
|
180
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
181
|
+
let uid = '';
|
|
182
|
+
|
|
183
|
+
for (let i = 0; i < length; i++) {
|
|
184
|
+
const randomIndex = Math.floor(Math.random() * characters.length)
|
|
185
|
+
uid += characters.charAt(randomIndex)
|
|
186
|
+
}
|
|
187
|
+
return uid
|
|
188
|
+
}
|