@feathersjs/transport-commons 5.0.0-pre.9 → 5.0.0
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 +193 -227
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/client.d.ts +1 -1
- package/lib/channels/channel/base.d.ts +1 -3
- package/lib/channels/channel/base.js +2 -2
- package/lib/channels/channel/base.js.map +1 -1
- package/lib/channels/channel/combined.d.ts +2 -1
- package/lib/channels/channel/combined.js +2 -2
- package/lib/channels/channel/combined.js.map +1 -1
- package/lib/channels/index.d.ts +14 -12
- package/lib/channels/index.js +15 -10
- package/lib/channels/index.js.map +1 -1
- package/lib/channels/mixins.d.ts +3 -3
- package/lib/channels/mixins.js +3 -3
- package/lib/channels/mixins.js.map +1 -1
- package/lib/client.d.ts +2 -2
- package/lib/client.js +10 -12
- package/lib/client.js.map +1 -1
- package/lib/http.d.ts +14 -9
- package/lib/http.js +30 -20
- package/lib/http.js.map +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.js +8 -2
- package/lib/index.js.map +1 -1
- package/lib/routing/index.d.ts +9 -4
- package/lib/routing/index.js +26 -16
- package/lib/routing/index.js.map +1 -1
- package/lib/routing/router.d.ts +4 -1
- package/lib/routing/router.js +37 -4
- package/lib/routing/router.js.map +1 -1
- package/lib/socket/index.d.ts +1 -2
- package/lib/socket/index.js +10 -10
- package/lib/socket/index.js.map +1 -1
- package/lib/socket/utils.d.ts +1 -2
- package/lib/socket/utils.js +43 -53
- package/lib/socket/utils.js.map +1 -1
- package/package.json +19 -14
- package/src/channels/channel/base.ts +28 -31
- package/src/channels/channel/combined.ts +32 -31
- package/src/channels/index.ts +67 -61
- package/src/channels/mixins.ts +49 -46
- package/src/client.ts +70 -70
- package/src/http.ts +55 -43
- package/src/index.ts +6 -5
- package/src/routing/index.ts +45 -28
- package/src/routing/router.ts +80 -42
- package/src/socket/index.ts +43 -44
- package/src/socket/utils.ts +71 -59
package/src/socket/utils.ts
CHANGED
|
@@ -1,114 +1,126 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import {
|
|
2
|
+
HookContext,
|
|
3
|
+
Application,
|
|
4
|
+
RealTimeConnection,
|
|
5
|
+
createContext,
|
|
6
|
+
getServiceOptions
|
|
7
|
+
} from '@feathersjs/feathers'
|
|
8
|
+
import { NotFound, MethodNotAllowed, BadRequest } from '@feathersjs/errors'
|
|
9
|
+
import { createDebug } from '@feathersjs/commons'
|
|
10
|
+
import isEqual from 'lodash/isEqual'
|
|
11
|
+
import { CombinedChannel } from '../channels/channel/combined'
|
|
12
|
+
|
|
13
|
+
const debug = createDebug('@feathersjs/transport-commons')
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_PARAMS_POSITION = 1
|
|
11
16
|
|
|
12
17
|
export const paramsPositions: { [key: string]: number } = {
|
|
13
18
|
find: 0,
|
|
14
19
|
update: 2,
|
|
15
20
|
patch: 2
|
|
16
|
-
}
|
|
21
|
+
}
|
|
17
22
|
|
|
18
|
-
export function normalizeError
|
|
19
|
-
const hasToJSON = typeof e.toJSON === 'function'
|
|
20
|
-
const result = hasToJSON ? e.toJSON() : {}
|
|
23
|
+
export function normalizeError(e: any) {
|
|
24
|
+
const hasToJSON = typeof e.toJSON === 'function'
|
|
25
|
+
const result = hasToJSON ? e.toJSON() : {}
|
|
21
26
|
|
|
22
27
|
if (!hasToJSON) {
|
|
23
|
-
Object.getOwnPropertyNames(e).forEach(key => {
|
|
24
|
-
result[key] = e[key]
|
|
25
|
-
})
|
|
28
|
+
Object.getOwnPropertyNames(e).forEach((key) => {
|
|
29
|
+
result[key] = e[key]
|
|
30
|
+
})
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
if (process.env.NODE_ENV === 'production') {
|
|
29
|
-
delete result.stack
|
|
34
|
+
delete result.stack
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
delete result.hook
|
|
37
|
+
delete result.hook
|
|
33
38
|
|
|
34
|
-
return result
|
|
39
|
+
return result
|
|
35
40
|
}
|
|
36
41
|
|
|
37
|
-
export function getDispatcher
|
|
42
|
+
export function getDispatcher(emit: string, socketMap: WeakMap<RealTimeConnection, any>, socketKey?: any) {
|
|
38
43
|
return function (event: string, channel: CombinedChannel, context: HookContext, data?: any) {
|
|
39
|
-
debug(`Dispatching '${event}' to ${channel.length} connections`)
|
|
44
|
+
debug(`Dispatching '${event}' to ${channel.length} connections`)
|
|
40
45
|
|
|
41
|
-
channel.connections.forEach(connection => {
|
|
46
|
+
channel.connections.forEach((connection) => {
|
|
42
47
|
// The reference between connection and socket is set in `app.setup`
|
|
43
|
-
const socket = socketKey ? connection[socketKey] : socketMap.get(connection)
|
|
48
|
+
const socket = socketKey ? connection[socketKey] : socketMap.get(connection)
|
|
44
49
|
|
|
45
50
|
if (socket) {
|
|
46
|
-
const eventName = `${context.path || ''} ${event}`.trim()
|
|
51
|
+
const eventName = `${context.path || ''} ${event}`.trim()
|
|
47
52
|
|
|
48
|
-
let result = channel.dataFor(connection) || context.dispatch || context.result
|
|
53
|
+
let result = channel.dataFor(connection) || context.dispatch || context.result
|
|
49
54
|
|
|
50
55
|
// If we are getting events from an array but try to dispatch individual data
|
|
51
56
|
// try to get the individual item to dispatch from the correct index.
|
|
52
57
|
if (!Array.isArray(data) && Array.isArray(context.result) && Array.isArray(result)) {
|
|
53
|
-
result = context.result.find(resultData => isEqual(resultData, data))
|
|
58
|
+
result = context.result.find((resultData) => isEqual(resultData, data))
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
debug(`Dispatching '${eventName}' to Socket ${socket.id} with`, result)
|
|
61
|
+
debug(`Dispatching '${eventName}' to Socket ${socket.id} with`, result)
|
|
57
62
|
|
|
58
|
-
socket[emit](eventName, result)
|
|
63
|
+
socket[emit](eventName, result)
|
|
59
64
|
}
|
|
60
|
-
})
|
|
61
|
-
}
|
|
65
|
+
})
|
|
66
|
+
}
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
export async function runMethod
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
export async function runMethod(
|
|
70
|
+
app: Application,
|
|
71
|
+
connection: RealTimeConnection,
|
|
72
|
+
path: string,
|
|
73
|
+
method: string,
|
|
74
|
+
args: any[]
|
|
75
|
+
) {
|
|
76
|
+
const trace = `method '${method}' on service '${path}'`
|
|
77
|
+
const methodArgs = args.slice(0)
|
|
78
|
+
const callback =
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
80
|
+
typeof methodArgs[methodArgs.length - 1] === 'function' ? methodArgs.pop() : function () {}
|
|
81
|
+
|
|
82
|
+
debug(`Running ${trace}`, connection, args)
|
|
71
83
|
|
|
72
84
|
const handleError = (error: any) => {
|
|
73
|
-
debug(`Error in ${trace}`, error)
|
|
74
|
-
callback(normalizeError(error))
|
|
75
|
-
}
|
|
85
|
+
debug(`Error in ${trace}`, error)
|
|
86
|
+
callback(normalizeError(error))
|
|
87
|
+
}
|
|
76
88
|
|
|
77
89
|
try {
|
|
78
|
-
const lookup = app.lookup(path)
|
|
90
|
+
const lookup = app.lookup(path)
|
|
79
91
|
|
|
80
92
|
// No valid service was found throw a NotFound error
|
|
81
93
|
if (lookup === null) {
|
|
82
|
-
throw new NotFound(`Service '${path}' not found`)
|
|
94
|
+
throw new NotFound(`Service '${path}' not found`)
|
|
83
95
|
}
|
|
84
96
|
|
|
85
|
-
const { service, params: route = {} } = lookup
|
|
86
|
-
const { methods } = getServiceOptions(service)
|
|
97
|
+
const { service, params: route = {} } = lookup
|
|
98
|
+
const { methods } = getServiceOptions(service)
|
|
87
99
|
|
|
88
100
|
// Only service methods are allowed
|
|
89
101
|
if (!methods.includes(method)) {
|
|
90
|
-
throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`)
|
|
102
|
+
throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`)
|
|
91
103
|
}
|
|
92
104
|
|
|
93
|
-
const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION
|
|
94
|
-
const query = methodArgs[position]
|
|
105
|
+
const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION
|
|
106
|
+
const query = Object.assign({}, methodArgs[position])
|
|
95
107
|
// `params` have to be re-mapped to the query and added with the route
|
|
96
|
-
const params = Object.assign({ query, route, connection }, connection)
|
|
108
|
+
const params = Object.assign({ query, route, connection }, connection)
|
|
97
109
|
|
|
98
110
|
// `params` is always the last parameter. Error if we got more arguments.
|
|
99
|
-
if (methodArgs.length >
|
|
100
|
-
throw new BadRequest(`Too many arguments for '${method}' method`)
|
|
111
|
+
if (methodArgs.length > position + 1) {
|
|
112
|
+
throw new BadRequest(`Too many arguments for '${method}' method`)
|
|
101
113
|
}
|
|
102
114
|
|
|
103
|
-
methodArgs[position] = params
|
|
115
|
+
methodArgs[position] = params
|
|
104
116
|
|
|
105
|
-
const ctx = createContext(service, method)
|
|
106
|
-
const returnedCtx: HookContext = await (service as any)[method](...methodArgs, ctx)
|
|
107
|
-
const result = returnedCtx.dispatch || returnedCtx.result
|
|
117
|
+
const ctx = createContext(service, method)
|
|
118
|
+
const returnedCtx: HookContext = await (service as any)[method](...methodArgs, ctx)
|
|
119
|
+
const result = returnedCtx.dispatch || returnedCtx.result
|
|
108
120
|
|
|
109
|
-
debug(`Returned successfully ${trace}`, result)
|
|
110
|
-
callback(null, result)
|
|
111
|
-
} catch (error) {
|
|
112
|
-
handleError(error)
|
|
121
|
+
debug(`Returned successfully ${trace}`, result)
|
|
122
|
+
callback(null, result)
|
|
123
|
+
} catch (error: any) {
|
|
124
|
+
handleError(error)
|
|
113
125
|
}
|
|
114
126
|
}
|