@feathersjs/socketio 5.0.0-pre.20 → 5.0.0-pre.23
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/CHANGELOG.md +24 -0
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/middleware.js +7 -3
- package/lib/middleware.js.map +1 -1
- package/package.json +12 -12
- package/src/index.ts +59 -49
- package/src/middleware.ts +32 -27
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,30 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.23](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.22...v5.0.0-pre.23) (2022-06-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @feathersjs/socketio
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [5.0.0-pre.22](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.21...v5.0.0-pre.22) (2022-05-24)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @feathersjs/socketio
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# [5.0.0-pre.21](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.20...v5.0.0-pre.21) (2022-05-23)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @feathersjs/socketio
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
6
30
|
# [5.0.0-pre.20](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.19...v5.0.0-pre.20) (2022-05-04)
|
|
7
31
|
|
|
8
32
|
|
package/lib/index.js
CHANGED
|
@@ -25,7 +25,7 @@ function configureSocketio(port, options, config) {
|
|
|
25
25
|
const socketMap = new WeakMap();
|
|
26
26
|
// Promise that resolves with the Socket.io `io` instance
|
|
27
27
|
// when `setup` has been called (with a server)
|
|
28
|
-
const done = new Promise(resolve => {
|
|
28
|
+
const done = new Promise((resolve) => {
|
|
29
29
|
const { listen, setup } = app;
|
|
30
30
|
Object.assign(app, {
|
|
31
31
|
async listen(...args) {
|
|
@@ -40,7 +40,7 @@ function configureSocketio(port, options, config) {
|
|
|
40
40
|
},
|
|
41
41
|
async setup(server, ...rest) {
|
|
42
42
|
if (!this.io) {
|
|
43
|
-
const io = this.io = new socket_io_1.Server(port || server, options);
|
|
43
|
+
const io = (this.io = new socket_io_1.Server(port || server, options));
|
|
44
44
|
io.use((0, middleware_1.disconnect)(app, getParams));
|
|
45
45
|
io.use((0, middleware_1.params)(app, socketMap));
|
|
46
46
|
io.use((0, middleware_1.authentication)(app, getParams));
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/lib/middleware.js
CHANGED
|
@@ -18,7 +18,9 @@ const params = (_app, socketMap) => (socket, next) => {
|
|
|
18
18
|
};
|
|
19
19
|
exports.params = params;
|
|
20
20
|
const authentication = (app, getParams, settings = {}) => (socket, next) => {
|
|
21
|
-
const service = app.defaultAuthentication
|
|
21
|
+
const service = app.defaultAuthentication
|
|
22
|
+
? app.defaultAuthentication(settings.service)
|
|
23
|
+
: null;
|
|
22
24
|
if (service === null) {
|
|
23
25
|
return next();
|
|
24
26
|
}
|
|
@@ -27,7 +29,8 @@ const authentication = (app, getParams, settings = {}) => (socket, next) => {
|
|
|
27
29
|
if (authStrategies.length === 0) {
|
|
28
30
|
return next();
|
|
29
31
|
}
|
|
30
|
-
service
|
|
32
|
+
service
|
|
33
|
+
.parse(socket.handshake, null, ...authStrategies)
|
|
31
34
|
.then(async (authentication) => {
|
|
32
35
|
if (authentication) {
|
|
33
36
|
debug('Parsed authentication from HTTP header', authentication);
|
|
@@ -38,7 +41,8 @@ const authentication = (app, getParams, settings = {}) => (socket, next) => {
|
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
43
|
next();
|
|
41
|
-
})
|
|
44
|
+
})
|
|
45
|
+
.catch(next);
|
|
42
46
|
};
|
|
43
47
|
exports.authentication = authentication;
|
|
44
48
|
//# sourceMappingURL=middleware.js.map
|
package/lib/middleware.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":";;;AACA,iDAAiD;AAGjD,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,iCAAiC,CAAC,CAAA;AAQrD,MAAM,UAAU,GACrB,CAAC,GAAgB,EAAE,SAAuB,EAAE,EAAE,CAAC,CAAC,MAAsB,EAAE,IAAkB,EAAE,EAAE;IAC5F,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IAC1E,IAAI,EAAE,CAAA;AACR,CAAC,CAAA;AAJU,QAAA,UAAU,cAIpB;AAEI,MAAM,MAAM,GACjB,CAAC,IAAiB,EAAE,SAA4B,EAAE,EAAE,CAAC,CAAC,MAAsB,EAAE,IAAkB,EAAE,EAAE;IAClG,MAAM,CAAC,QAAQ,GAAG;QAChB,QAAQ,EAAE,UAAU;QACpB,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,OAAO;KAClC,CAAA;IAED,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IAEtC,IAAI,EAAE,CAAA;AACR,CAAC,CAAA;AAVU,QAAA,MAAM,UAUhB;AAEI,MAAM,cAAc,GACzB,CAAC,GAAgB,EAAE,SAAuB,EAAE,WAAgB,EAAE,EAAE,EAAE,CAClE,CAAC,MAAsB,EAAE,IAAkB,EAAE,EAAE;IAC7C,MAAM,OAAO,GAAI,GAAW,CAAC,qBAAqB;QAChD,CAAC,CAAE,GAAW,CAAC,qBAAqB,CAAC,QAAQ,CAAC,OAAO,CAAC;QACtD,CAAC,CAAC,IAAI,CAAA;IAER,IAAI,OAAO,KAAK,IAAI,EAAE;QACpB,OAAO,IAAI,EAAE,CAAA;KACd;IAED,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAA;IACpC,MAAM,cAAc,GAAG,MAAM,CAAC,eAAe,IAAI,MAAM,CAAC,cAAc,IAAI,EAAE,CAAA;IAE5E,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE;QAC/B,OAAO,IAAI,EAAE,CAAA;KACd;IAED,OAAO;SACJ,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,GAAG,cAAc,CAAC;SAChD,IAAI,CAAC,KAAK,EAAE,cAAmB,EAAE,EAAE;QAClC,IAAI,cAAc,EAAE;YAClB,KAAK,CAAC,wCAAwC,EAAE,cAAc,CAAC,CAAA;YAC/D,MAAM,CAAC,QAAQ,CAAC,cAAc,GAAG,cAAc,CAAA;YAC/C,MAAM,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE;gBACnC,QAAQ,EAAE,UAAU;gBACpB,UAAU,EAAE,SAAS,CAAC,MAAM,CAAC;aAC9B,CAAC,CAAA;SACH;QAED,IAAI,EAAE,CAAA;IACR,CAAC,CAAC;SACD,KAAK,CAAC,IAAI,CAAC,CAAA;AAChB,CAAC,CAAA;AAjCU,QAAA,cAAc,kBAiCxB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/socketio",
|
|
3
3
|
"description": "The Feathers Socket.io real-time API provider",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.23",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -52,22 +52,22 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
56
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
57
|
-
"@feathersjs/transport-commons": "^5.0.0-pre.
|
|
58
|
-
"socket.io": "^4.5.
|
|
55
|
+
"@feathersjs/commons": "^5.0.0-pre.23",
|
|
56
|
+
"@feathersjs/feathers": "^5.0.0-pre.23",
|
|
57
|
+
"@feathersjs/transport-commons": "^5.0.0-pre.23",
|
|
58
|
+
"socket.io": "^4.5.1"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@feathersjs/express": "^5.0.0-pre.
|
|
62
|
-
"@feathersjs/memory": "^5.0.0-pre.
|
|
63
|
-
"@feathersjs/tests": "^5.0.0-pre.
|
|
61
|
+
"@feathersjs/express": "^5.0.0-pre.23",
|
|
62
|
+
"@feathersjs/memory": "^5.0.0-pre.23",
|
|
63
|
+
"@feathersjs/tests": "^5.0.0-pre.23",
|
|
64
64
|
"@types/mocha": "^9.1.1",
|
|
65
|
-
"@types/node": "^17.0.
|
|
65
|
+
"@types/node": "^17.0.40",
|
|
66
66
|
"lodash": "^4.17.21",
|
|
67
67
|
"mocha": "^10.0.0",
|
|
68
68
|
"shx": "^0.3.4",
|
|
69
|
-
"socket.io-client": "^4.5.
|
|
70
|
-
"typescript": "^4.
|
|
69
|
+
"socket.io-client": "^4.5.1",
|
|
70
|
+
"typescript": "^4.7.3"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "a60910bd730b88053ca6648337095f1ca1e3b39f"
|
|
73
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,93 +1,103 @@
|
|
|
1
|
-
import http from 'http'
|
|
2
|
-
import { Server, ServerOptions } from 'socket.io'
|
|
3
|
-
import { createDebug } from '@feathersjs/commons'
|
|
4
|
-
import { Application } from '@feathersjs/feathers'
|
|
5
|
-
import { socket } from '@feathersjs/transport-commons'
|
|
1
|
+
import http from 'http'
|
|
2
|
+
import { Server, ServerOptions } from 'socket.io'
|
|
3
|
+
import { createDebug } from '@feathersjs/commons'
|
|
4
|
+
import { Application } from '@feathersjs/feathers'
|
|
5
|
+
import { socket } from '@feathersjs/transport-commons'
|
|
6
6
|
|
|
7
|
-
import { disconnect, params, authentication, FeathersSocket } from './middleware'
|
|
7
|
+
import { disconnect, params, authentication, FeathersSocket } from './middleware'
|
|
8
8
|
|
|
9
|
-
const debug = createDebug('@feathersjs/socketio')
|
|
9
|
+
const debug = createDebug('@feathersjs/socketio')
|
|
10
10
|
|
|
11
11
|
declare module '@feathersjs/feathers/lib/declarations' {
|
|
12
|
-
interface Application<Services, Settings> {
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
interface Application<Services, Settings> {
|
|
13
|
+
// eslint-disable-line
|
|
14
|
+
io: Server
|
|
15
|
+
listen(options: any): Promise<http.Server>
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
function configureSocketio
|
|
19
|
-
function configureSocketio
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
function configureSocketio(callback?: (io: Server) => void): (app: Application) => void
|
|
20
|
+
function configureSocketio(
|
|
21
|
+
options: number | Partial<ServerOptions>,
|
|
22
|
+
callback?: (io: Server) => void
|
|
23
|
+
): (app: Application) => void
|
|
24
|
+
function configureSocketio(
|
|
25
|
+
port: number,
|
|
26
|
+
options?: Partial<ServerOptions>,
|
|
27
|
+
callback?: (io: Server) => void
|
|
28
|
+
): (app: Application) => void
|
|
29
|
+
function configureSocketio(port?: any, options?: any, config?: any) {
|
|
22
30
|
if (typeof port !== 'number') {
|
|
23
|
-
config = options
|
|
24
|
-
options = port
|
|
25
|
-
port = null
|
|
31
|
+
config = options
|
|
32
|
+
options = port
|
|
33
|
+
port = null
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
if (typeof options !== 'object') {
|
|
29
|
-
config = options
|
|
30
|
-
options = {}
|
|
37
|
+
config = options
|
|
38
|
+
options = {}
|
|
31
39
|
}
|
|
32
40
|
|
|
33
41
|
return (app: Application) => {
|
|
34
42
|
// Function that gets the connection
|
|
35
|
-
const getParams = (socket: FeathersSocket) => socket.feathers
|
|
43
|
+
const getParams = (socket: FeathersSocket) => socket.feathers
|
|
36
44
|
// A mapping from connection to socket instance
|
|
37
|
-
const socketMap = new WeakMap()
|
|
45
|
+
const socketMap = new WeakMap()
|
|
38
46
|
// Promise that resolves with the Socket.io `io` instance
|
|
39
47
|
// when `setup` has been called (with a server)
|
|
40
|
-
const done = new Promise(resolve => {
|
|
41
|
-
const { listen, setup } = app as any
|
|
48
|
+
const done = new Promise((resolve) => {
|
|
49
|
+
const { listen, setup } = app as any
|
|
42
50
|
|
|
43
51
|
Object.assign(app, {
|
|
44
|
-
async listen
|
|
52
|
+
async listen(this: any, ...args: any[]) {
|
|
45
53
|
if (typeof listen === 'function') {
|
|
46
54
|
// If `listen` already exists
|
|
47
55
|
// usually the case when the app has been expressified
|
|
48
|
-
return listen.call(this, ...args)
|
|
56
|
+
return listen.call(this, ...args)
|
|
49
57
|
}
|
|
50
58
|
|
|
51
|
-
const server = http.createServer()
|
|
59
|
+
const server = http.createServer()
|
|
52
60
|
|
|
53
|
-
await this.setup(server)
|
|
61
|
+
await this.setup(server)
|
|
54
62
|
|
|
55
|
-
return server.listen(...args)
|
|
63
|
+
return server.listen(...args)
|
|
56
64
|
},
|
|
57
65
|
|
|
58
|
-
async setup
|
|
66
|
+
async setup(this: any, server: http.Server, ...rest: any[]) {
|
|
59
67
|
if (!this.io) {
|
|
60
|
-
const io = this.io = new Server(port || server, options)
|
|
68
|
+
const io = (this.io = new Server(port || server, options))
|
|
61
69
|
|
|
62
|
-
io.use(disconnect(app, getParams))
|
|
63
|
-
io.use(params(app, socketMap))
|
|
64
|
-
io.use(authentication(app, getParams))
|
|
70
|
+
io.use(disconnect(app, getParams))
|
|
71
|
+
io.use(params(app, socketMap))
|
|
72
|
+
io.use(authentication(app, getParams))
|
|
65
73
|
|
|
66
74
|
// In Feathers it is easy to hit the standard Node warning limit
|
|
67
75
|
// of event listeners (e.g. by registering 10 services).
|
|
68
76
|
// So we set it to a higher number. 64 should be enough for everyone.
|
|
69
|
-
io.sockets.setMaxListeners(64)
|
|
77
|
+
io.sockets.setMaxListeners(64)
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
if (typeof config === 'function') {
|
|
73
|
-
debug('Calling SocketIO configuration function')
|
|
74
|
-
config.call(this, this.io)
|
|
81
|
+
debug('Calling SocketIO configuration function')
|
|
82
|
+
config.call(this, this.io)
|
|
75
83
|
}
|
|
76
84
|
|
|
77
|
-
resolve(this.io)
|
|
85
|
+
resolve(this.io)
|
|
78
86
|
|
|
79
|
-
return setup.call(this, server, ...rest)
|
|
87
|
+
return setup.call(this, server, ...rest)
|
|
80
88
|
}
|
|
81
|
-
})
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
app.configure(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
app.configure(
|
|
93
|
+
socket({
|
|
94
|
+
done,
|
|
95
|
+
socketMap,
|
|
96
|
+
getParams,
|
|
97
|
+
emit: 'emit'
|
|
98
|
+
})
|
|
99
|
+
)
|
|
100
|
+
}
|
|
91
101
|
}
|
|
92
102
|
|
|
93
|
-
export = configureSocketio
|
|
103
|
+
export = configureSocketio
|
package/src/middleware.ts
CHANGED
|
@@ -1,59 +1,64 @@
|
|
|
1
|
-
import { Application, Params } from '@feathersjs/feathers'
|
|
2
|
-
import { createDebug } from '@feathersjs/commons'
|
|
3
|
-
import { Socket } from 'socket.io'
|
|
1
|
+
import { Application, Params } from '@feathersjs/feathers'
|
|
2
|
+
import { createDebug } from '@feathersjs/commons'
|
|
3
|
+
import { Socket } from 'socket.io'
|
|
4
4
|
|
|
5
|
-
const debug = createDebug('@feathersjs/socketio/middleware')
|
|
5
|
+
const debug = createDebug('@feathersjs/socketio/middleware')
|
|
6
6
|
|
|
7
|
-
export type ParamsGetter = (socket: Socket) => any
|
|
8
|
-
export type NextFunction = (err?: any) => void
|
|
7
|
+
export type ParamsGetter = (socket: Socket) => any
|
|
8
|
+
export type NextFunction = (err?: any) => void
|
|
9
9
|
export interface FeathersSocket extends Socket {
|
|
10
10
|
feathers?: Params & { [key: string]: any }
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export const disconnect =
|
|
14
|
-
(socket: FeathersSocket, next: NextFunction) => {
|
|
15
|
-
socket.once('disconnect', () => app.emit('disconnect', getParams(socket)))
|
|
16
|
-
next()
|
|
13
|
+
export const disconnect =
|
|
14
|
+
(app: Application, getParams: ParamsGetter) => (socket: FeathersSocket, next: NextFunction) => {
|
|
15
|
+
socket.once('disconnect', () => app.emit('disconnect', getParams(socket)))
|
|
16
|
+
next()
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export const params =
|
|
20
|
-
(socket: FeathersSocket, next: NextFunction) => {
|
|
19
|
+
export const params =
|
|
20
|
+
(_app: Application, socketMap: WeakMap<any, any>) => (socket: FeathersSocket, next: NextFunction) => {
|
|
21
21
|
socket.feathers = {
|
|
22
22
|
provider: 'socketio',
|
|
23
23
|
headers: socket.handshake.headers
|
|
24
|
-
}
|
|
24
|
+
}
|
|
25
25
|
|
|
26
|
-
socketMap.set(socket.feathers, socket)
|
|
26
|
+
socketMap.set(socket.feathers, socket)
|
|
27
27
|
|
|
28
|
-
next()
|
|
28
|
+
next()
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export const authentication =
|
|
31
|
+
export const authentication =
|
|
32
|
+
(app: Application, getParams: ParamsGetter, settings: any = {}) =>
|
|
32
33
|
(socket: FeathersSocket, next: NextFunction) => {
|
|
33
|
-
const service = (app as any).defaultAuthentication
|
|
34
|
+
const service = (app as any).defaultAuthentication
|
|
35
|
+
? (app as any).defaultAuthentication(settings.service)
|
|
36
|
+
: null
|
|
34
37
|
|
|
35
38
|
if (service === null) {
|
|
36
|
-
return next()
|
|
39
|
+
return next()
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
const config = service.configuration
|
|
40
|
-
const authStrategies = config.parseStrategies || config.authStrategies || []
|
|
42
|
+
const config = service.configuration
|
|
43
|
+
const authStrategies = config.parseStrategies || config.authStrategies || []
|
|
41
44
|
|
|
42
45
|
if (authStrategies.length === 0) {
|
|
43
|
-
return next()
|
|
46
|
+
return next()
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
service
|
|
49
|
+
service
|
|
50
|
+
.parse(socket.handshake, null, ...authStrategies)
|
|
47
51
|
.then(async (authentication: any) => {
|
|
48
52
|
if (authentication) {
|
|
49
|
-
debug('Parsed authentication from HTTP header', authentication)
|
|
50
|
-
socket.feathers.authentication = authentication
|
|
53
|
+
debug('Parsed authentication from HTTP header', authentication)
|
|
54
|
+
socket.feathers.authentication = authentication
|
|
51
55
|
await service.create(authentication, {
|
|
52
56
|
provider: 'socketio',
|
|
53
57
|
connection: getParams(socket)
|
|
54
|
-
})
|
|
58
|
+
})
|
|
55
59
|
}
|
|
56
60
|
|
|
57
|
-
next()
|
|
58
|
-
})
|
|
61
|
+
next()
|
|
62
|
+
})
|
|
63
|
+
.catch(next)
|
|
59
64
|
}
|