@feathersjs/transport-commons 5.0.0-pre.3 → 5.0.0-pre.31
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 +187 -200
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/client.d.ts +1 -1
- package/lib/channels/channel/base.js +2 -2
- package/lib/channels/channel/base.js.map +1 -1
- package/lib/channels/channel/combined.js +2 -2
- package/lib/channels/channel/combined.js.map +1 -1
- package/lib/channels/index.d.ts +12 -10
- 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 +35 -0
- package/lib/http.js +77 -0
- package/lib/http.js.map +1 -0
- package/lib/index.d.ts +3 -2
- package/lib/index.js +27 -1
- 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 +8 -3
- package/lib/routing/router.js +64 -22
- package/lib/routing/router.js.map +1 -1
- package/lib/socket/index.js +10 -10
- package/lib/socket/index.js.map +1 -1
- package/lib/socket/utils.js +43 -53
- package/lib/socket/utils.js.map +1 -1
- package/package.json +19 -15
- package/src/channels/channel/base.ts +28 -28
- package/src/channels/channel/combined.ts +31 -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 +95 -0
- package/src/index.ts +5 -4
- package/src/routing/index.ts +45 -28
- package/src/routing/router.ts +91 -42
- package/src/socket/index.ts +44 -44
- package/src/socket/utils.ts +64 -57
package/src/http.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { MethodNotAllowed } from '@feathersjs/errors/lib'
|
|
2
|
+
import { HookContext, NullableId, Params } from '@feathersjs/feathers'
|
|
3
|
+
import encodeUrl from 'encodeurl'
|
|
4
|
+
|
|
5
|
+
export const METHOD_HEADER = 'x-service-method'
|
|
6
|
+
|
|
7
|
+
export interface ServiceParams {
|
|
8
|
+
id: NullableId
|
|
9
|
+
data: any
|
|
10
|
+
params: Params
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const statusCodes = {
|
|
14
|
+
created: 201,
|
|
15
|
+
noContent: 204,
|
|
16
|
+
methodNotAllowed: 405,
|
|
17
|
+
success: 200,
|
|
18
|
+
seeOther: 303
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const knownMethods: { [key: string]: string } = {
|
|
22
|
+
post: 'create',
|
|
23
|
+
patch: 'patch',
|
|
24
|
+
put: 'update',
|
|
25
|
+
delete: 'remove'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getServiceMethod(_httpMethod: string, id: unknown, headerOverride?: string) {
|
|
29
|
+
const httpMethod = _httpMethod.toLowerCase()
|
|
30
|
+
|
|
31
|
+
if (httpMethod === 'post' && headerOverride) {
|
|
32
|
+
return headerOverride
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const mappedMethod = knownMethods[httpMethod]
|
|
36
|
+
|
|
37
|
+
if (mappedMethod) {
|
|
38
|
+
return mappedMethod
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (httpMethod === 'get') {
|
|
42
|
+
return id === null ? 'find' : 'get'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
throw new MethodNotAllowed(`Method ${_httpMethod} not allowed`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const argumentsFor = {
|
|
49
|
+
get: ({ id, params }: ServiceParams) => [id, params],
|
|
50
|
+
find: ({ params }: ServiceParams) => [params],
|
|
51
|
+
create: ({ data, params }: ServiceParams) => [data, params],
|
|
52
|
+
update: ({ id, data, params }: ServiceParams) => [id, data, params],
|
|
53
|
+
patch: ({ id, data, params }: ServiceParams) => [id, data, params],
|
|
54
|
+
remove: ({ id, params }: ServiceParams) => [id, params],
|
|
55
|
+
default: ({ data, params }: ServiceParams) => [data, params]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getStatusCode(context: HookContext, body: any, location: string | string[]) {
|
|
59
|
+
const { http = {} } = context
|
|
60
|
+
|
|
61
|
+
if (http.status) {
|
|
62
|
+
return http.status
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (context.method === 'create') {
|
|
66
|
+
return statusCodes.created
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (location !== undefined) {
|
|
70
|
+
return statusCodes.seeOther
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!body) {
|
|
74
|
+
return statusCodes.noContent
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return statusCodes.success
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function getResponse(context: HookContext) {
|
|
81
|
+
const { http = {} } = context
|
|
82
|
+
const body = context.dispatch !== undefined ? context.dispatch : context.result
|
|
83
|
+
|
|
84
|
+
let headers = http.headers || {}
|
|
85
|
+
let location = headers.Location
|
|
86
|
+
|
|
87
|
+
if (http.location !== undefined) {
|
|
88
|
+
location = encodeUrl(http.location)
|
|
89
|
+
headers = { ...headers, Location: location }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const status = getStatusCode(context, body, location)
|
|
93
|
+
|
|
94
|
+
return { status, headers, body }
|
|
95
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { socket } from './socket'
|
|
2
|
-
import { routing } from './routing'
|
|
3
|
-
import { channels } from './channels'
|
|
1
|
+
import { socket } from './socket'
|
|
2
|
+
import { routing } from './routing'
|
|
3
|
+
import { channels, Channel, CombinedChannel, RealTimeConnection } from './channels'
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export * as http from './http'
|
|
6
|
+
export { socket, routing, channels, Channel, CombinedChannel, RealTimeConnection }
|
package/src/routing/index.ts
CHANGED
|
@@ -1,45 +1,62 @@
|
|
|
1
|
-
import { Application,
|
|
2
|
-
import { Router } from './router'
|
|
1
|
+
import { Application, FeathersService, ServiceOptions } from '@feathersjs/feathers'
|
|
2
|
+
import { Router } from './router'
|
|
3
3
|
|
|
4
4
|
declare module '@feathersjs/feathers/lib/declarations' {
|
|
5
5
|
interface RouteLookup {
|
|
6
|
-
service: Service
|
|
7
|
-
params: { [key: string]:
|
|
6
|
+
service: Service
|
|
7
|
+
params: { [key: string]: any }
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
|
+
interface Application<Services, Settings> {
|
|
12
|
+
// eslint-disable-line
|
|
13
|
+
routes: Router<{
|
|
14
|
+
service: Service
|
|
15
|
+
params?: { [key: string]: any }
|
|
16
|
+
}>
|
|
17
|
+
lookup(path: string): RouteLookup
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
export * from './router'
|
|
21
|
+
export * from './router'
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
const lookup = function (this: Application, path: string) {
|
|
24
|
+
const result = this.routes.lookup(path)
|
|
25
|
+
|
|
26
|
+
if (result === null) {
|
|
27
|
+
return null
|
|
21
28
|
}
|
|
22
29
|
|
|
23
|
-
const
|
|
30
|
+
const {
|
|
31
|
+
params: colonParams,
|
|
32
|
+
data: { service, params: dataParams }
|
|
33
|
+
} = result
|
|
24
34
|
|
|
25
|
-
|
|
26
|
-
routes,
|
|
27
|
-
lookup (this: Application, path: string) {
|
|
28
|
-
const result = this.routes.lookup(path);
|
|
35
|
+
const params = dataParams ? { ...dataParams, ...colonParams } : colonParams
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
return { service, params }
|
|
38
|
+
}
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
export const routing = () => (app: Application) => {
|
|
41
|
+
if (typeof app.lookup === 'function') {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const { unuse } = app
|
|
35
46
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
app.routes = new Router()
|
|
48
|
+
app.lookup = lookup
|
|
49
|
+
app.unuse = function (path: string) {
|
|
50
|
+
app.routes.remove(path)
|
|
51
|
+
app.routes.remove(`${path}/:__id`)
|
|
52
|
+
return unuse.call(this, path)
|
|
53
|
+
}
|
|
39
54
|
|
|
40
55
|
// Add a mixin that registers a service on the router
|
|
41
|
-
app.mixins.push((service:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
56
|
+
app.mixins.push((service: FeathersService, path: string, options: ServiceOptions) => {
|
|
57
|
+
const { routeParams: params = {} } = options
|
|
58
|
+
|
|
59
|
+
app.routes.insert(path, { service, params })
|
|
60
|
+
app.routes.insert(`${path}/:__id`, { service, params })
|
|
61
|
+
})
|
|
62
|
+
}
|
package/src/routing/router.ts
CHANGED
|
@@ -1,87 +1,136 @@
|
|
|
1
|
-
import { stripSlashes } from '@feathersjs/commons'
|
|
2
|
-
import { BadRequest } from '@feathersjs/errors';
|
|
1
|
+
import { stripSlashes } from '@feathersjs/commons'
|
|
3
2
|
|
|
4
3
|
export interface LookupData {
|
|
5
|
-
params: { [key: string]: string }
|
|
4
|
+
params: { [key: string]: string }
|
|
6
5
|
}
|
|
7
6
|
|
|
8
7
|
export interface LookupResult<T> extends LookupData {
|
|
9
|
-
data?: T
|
|
8
|
+
data?: T
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export class RouteNode<T = any> {
|
|
13
|
-
data?: T
|
|
14
|
-
children: { [key: string]: RouteNode } = {}
|
|
15
|
-
|
|
12
|
+
data?: T
|
|
13
|
+
children: { [key: string]: RouteNode } = {}
|
|
14
|
+
placeholders: RouteNode[] = []
|
|
16
15
|
|
|
17
|
-
constructor
|
|
16
|
+
constructor(public name: string, public depth: number) {}
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
get hasChildren() {
|
|
19
|
+
return Object.keys(this.children).length !== 0 || this.placeholders.length !== 0
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
insert(path: string[], data: T): RouteNode<T> {
|
|
23
|
+
if (this.depth === path.length) {
|
|
24
|
+
if (this.data !== undefined) {
|
|
25
|
+
throw new Error(`Path ${path.join('/')} already exists`)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.data = data
|
|
29
|
+
return this
|
|
23
30
|
}
|
|
24
31
|
|
|
25
|
-
const
|
|
32
|
+
const current = path[this.depth]
|
|
33
|
+
const nextDepth = this.depth + 1
|
|
26
34
|
|
|
27
35
|
if (current.startsWith(':')) {
|
|
28
|
-
|
|
29
|
-
const
|
|
36
|
+
// Insert a placeholder node like /messages/:id
|
|
37
|
+
const placeholderName = current.substring(1)
|
|
38
|
+
let placeholder = this.placeholders.find((p) => p.name === placeholderName)
|
|
30
39
|
|
|
31
40
|
if (!placeholder) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
throw new BadRequest(`Can not add route with placeholder ':${name}' because placeholder ':${placeholder.name}' already exists`);
|
|
41
|
+
placeholder = new RouteNode(placeholderName, nextDepth)
|
|
42
|
+
this.placeholders.push(placeholder)
|
|
35
43
|
}
|
|
36
44
|
|
|
37
|
-
return
|
|
45
|
+
return placeholder.insert(path, data)
|
|
38
46
|
}
|
|
39
47
|
|
|
40
|
-
|
|
48
|
+
const child = this.children[current] || new RouteNode(current, nextDepth)
|
|
49
|
+
|
|
50
|
+
this.children[current] = child
|
|
41
51
|
|
|
42
|
-
return
|
|
52
|
+
return child.insert(path, data)
|
|
43
53
|
}
|
|
44
54
|
|
|
45
|
-
|
|
46
|
-
if (path.length ===
|
|
47
|
-
return
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
remove(path: string[]) {
|
|
56
|
+
if (path.length === this.depth) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const current = path[this.depth]
|
|
61
|
+
|
|
62
|
+
if (current.startsWith(':')) {
|
|
63
|
+
const placeholderName = current.substring(1)
|
|
64
|
+
const placeholder = this.placeholders.find((p) => p.name === placeholderName)
|
|
65
|
+
|
|
66
|
+
placeholder.remove(path)
|
|
67
|
+
this.placeholders = this.placeholders.filter((p) => p !== placeholder)
|
|
68
|
+
} else if (this.children[current]) {
|
|
69
|
+
const child = this.children[current]
|
|
70
|
+
|
|
71
|
+
child.remove(path)
|
|
72
|
+
|
|
73
|
+
if (!child.hasChildren) {
|
|
74
|
+
delete this.children[current]
|
|
50
75
|
}
|
|
51
76
|
}
|
|
77
|
+
}
|
|
52
78
|
|
|
53
|
-
|
|
54
|
-
|
|
79
|
+
lookup(path: string[], info: LookupData): LookupResult<T> | null {
|
|
80
|
+
if (path.length === this.depth) {
|
|
81
|
+
return this.data === undefined
|
|
82
|
+
? null
|
|
83
|
+
: {
|
|
84
|
+
...info,
|
|
85
|
+
data: this.data
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const current = path[this.depth]
|
|
90
|
+
const child = this.children[current]
|
|
55
91
|
|
|
56
92
|
if (child) {
|
|
57
|
-
|
|
93
|
+
const lookup = child.lookup(path, info)
|
|
94
|
+
|
|
95
|
+
if (lookup !== null) {
|
|
96
|
+
return lookup
|
|
97
|
+
}
|
|
58
98
|
}
|
|
59
99
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
100
|
+
// This will return the first placeholder that matches early
|
|
101
|
+
for (const placeholder of this.placeholders) {
|
|
102
|
+
const result = placeholder.lookup(path, info)
|
|
103
|
+
|
|
104
|
+
if (result !== null) {
|
|
105
|
+
result.params[placeholder.name] = current
|
|
106
|
+
return result
|
|
107
|
+
}
|
|
63
108
|
}
|
|
64
109
|
|
|
65
|
-
return null
|
|
110
|
+
return null
|
|
66
111
|
}
|
|
67
112
|
}
|
|
68
113
|
|
|
69
|
-
export class Router<T> {
|
|
70
|
-
root: RouteNode<T> = new RouteNode<T>('')
|
|
114
|
+
export class Router<T = any> {
|
|
115
|
+
constructor(public root: RouteNode<T> = new RouteNode<T>('', 0)) {}
|
|
116
|
+
|
|
117
|
+
getPath(path: string) {
|
|
118
|
+
return stripSlashes(path).split('/')
|
|
119
|
+
}
|
|
71
120
|
|
|
72
|
-
|
|
73
|
-
return
|
|
121
|
+
insert(path: string, data: T) {
|
|
122
|
+
return this.root.insert(this.getPath(path), data)
|
|
74
123
|
}
|
|
75
124
|
|
|
76
|
-
|
|
77
|
-
return this.root.
|
|
125
|
+
remove(path: string) {
|
|
126
|
+
return this.root.remove(this.getPath(path))
|
|
78
127
|
}
|
|
79
128
|
|
|
80
|
-
lookup
|
|
129
|
+
lookup(path: string) {
|
|
81
130
|
if (typeof path !== 'string') {
|
|
82
|
-
return null
|
|
131
|
+
return null
|
|
83
132
|
}
|
|
84
133
|
|
|
85
|
-
return this.root.lookup(this.getPath(path), { params: {} })
|
|
134
|
+
return this.root.lookup(this.getPath(path), { params: {} })
|
|
86
135
|
}
|
|
87
136
|
}
|
package/src/socket/index.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import { Application, getServiceOptions, Params } from '@feathersjs/feathers'
|
|
2
|
-
import { createDebug } from '@feathersjs/commons'
|
|
3
|
-
import { channels } from '../channels'
|
|
4
|
-
import { routing } from '../routing'
|
|
5
|
-
import { getDispatcher, runMethod } from './utils'
|
|
6
|
-
import { RealTimeConnection } from '../channels/channel/base'
|
|
1
|
+
import { Application, getServiceOptions, Params } from '@feathersjs/feathers'
|
|
2
|
+
import { createDebug } from '@feathersjs/commons'
|
|
3
|
+
import { channels } from '../channels'
|
|
4
|
+
import { routing } from '../routing'
|
|
5
|
+
import { getDispatcher, runMethod } from './utils'
|
|
6
|
+
import { RealTimeConnection } from '../channels/channel/base'
|
|
7
7
|
|
|
8
|
-
const debug = createDebug('@feathersjs/transport-commons')
|
|
8
|
+
const debug = createDebug('@feathersjs/transport-commons')
|
|
9
9
|
|
|
10
10
|
export interface SocketOptions {
|
|
11
|
-
done: Promise<any
|
|
12
|
-
emit: string
|
|
13
|
-
socketMap: WeakMap<RealTimeConnection, any
|
|
14
|
-
socketKey?: any
|
|
15
|
-
getParams: (socket: any) => RealTimeConnection
|
|
11
|
+
done: Promise<any>
|
|
12
|
+
emit: string
|
|
13
|
+
socketMap: WeakMap<RealTimeConnection, any>
|
|
14
|
+
socketKey?: any
|
|
15
|
+
getParams: (socket: any) => RealTimeConnection
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export function socket
|
|
18
|
+
export function socket({ done, emit, socketMap, socketKey, getParams }: SocketOptions) {
|
|
19
19
|
return (app: Application) => {
|
|
20
20
|
const leaveChannels = (connection: RealTimeConnection) => {
|
|
21
|
-
const { channels } = app
|
|
21
|
+
const { channels } = app
|
|
22
22
|
|
|
23
23
|
if (channels.length) {
|
|
24
|
-
app.channel(app.channels).leave(connection)
|
|
24
|
+
app.channel(app.channels).leave(connection)
|
|
25
25
|
}
|
|
26
|
-
}
|
|
26
|
+
}
|
|
27
27
|
|
|
28
|
-
app.configure(channels())
|
|
29
|
-
app.configure(routing())
|
|
28
|
+
app.configure(channels())
|
|
29
|
+
app.configure(routing())
|
|
30
30
|
|
|
31
|
-
app.on('publish', getDispatcher(emit, socketMap, socketKey))
|
|
32
|
-
app.on('disconnect', leaveChannels)
|
|
31
|
+
app.on('publish', getDispatcher(emit, socketMap, socketKey))
|
|
32
|
+
app.on('disconnect', leaveChannels)
|
|
33
33
|
app.on('logout', (_authResult: any, params: Params) => {
|
|
34
|
-
const { connection } = params
|
|
34
|
+
const { connection } = params
|
|
35
35
|
|
|
36
36
|
if (connection) {
|
|
37
|
-
leaveChannels(connection)
|
|
37
|
+
leaveChannels(connection)
|
|
38
38
|
}
|
|
39
|
-
})
|
|
39
|
+
})
|
|
40
40
|
|
|
41
41
|
// `connection` event
|
|
42
|
-
done.then(provider
|
|
43
|
-
app.emit('connection', getParams(connection)))
|
|
44
|
-
)
|
|
42
|
+
done.then((provider) =>
|
|
43
|
+
provider.on('connection', (connection: any) => app.emit('connection', getParams(connection)))
|
|
44
|
+
)
|
|
45
45
|
|
|
46
46
|
// `socket.emit('methodName', 'serviceName', ...args)` handlers
|
|
47
|
-
done.then(provider
|
|
48
|
-
|
|
49
|
-
const
|
|
47
|
+
done.then((provider) =>
|
|
48
|
+
provider.on('connection', (connection: any) => {
|
|
49
|
+
const methodHandlers = Object.keys(app.services).reduce((result, name) => {
|
|
50
|
+
const { methods } = getServiceOptions(app.service(name))
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
methods.forEach((method) => {
|
|
53
|
+
if (!result[method]) {
|
|
54
|
+
result[method] = (...args: any[]) => {
|
|
55
|
+
const path = args.shift()
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
debug(`Got '${method}' call for service '${path}'`)
|
|
58
|
+
runMethod(app, getParams(connection), path, method, args)
|
|
59
|
+
}
|
|
58
60
|
}
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
+
})
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
return result
|
|
64
|
+
}, {} as any)
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
};
|
|
66
|
+
Object.keys(methodHandlers).forEach((key) => connection.on(key, methodHandlers[key]))
|
|
67
|
+
})
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
70
|
}
|
package/src/socket/utils.ts
CHANGED
|
@@ -1,114 +1,121 @@
|
|
|
1
|
-
import { HookContext, Application, createContext, getServiceOptions } from '@feathersjs/feathers'
|
|
2
|
-
import { NotFound, MethodNotAllowed, BadRequest } from '@feathersjs/errors'
|
|
3
|
-
import { createDebug } from '@feathersjs/commons'
|
|
4
|
-
import isEqual from 'lodash/isEqual'
|
|
5
|
-
import { CombinedChannel } from '../channels/channel/combined'
|
|
6
|
-
import { RealTimeConnection } from '../channels/channel/base'
|
|
1
|
+
import { HookContext, Application, createContext, getServiceOptions } from '@feathersjs/feathers'
|
|
2
|
+
import { NotFound, MethodNotAllowed, BadRequest } from '@feathersjs/errors'
|
|
3
|
+
import { createDebug } from '@feathersjs/commons'
|
|
4
|
+
import isEqual from 'lodash/isEqual'
|
|
5
|
+
import { CombinedChannel } from '../channels/channel/combined'
|
|
6
|
+
import { RealTimeConnection } from '../channels/channel/base'
|
|
7
7
|
|
|
8
|
-
const debug = createDebug('@feathersjs/transport-commons')
|
|
8
|
+
const debug = createDebug('@feathersjs/transport-commons')
|
|
9
9
|
|
|
10
|
-
export const DEFAULT_PARAMS_POSITION = 1
|
|
10
|
+
export const DEFAULT_PARAMS_POSITION = 1
|
|
11
11
|
|
|
12
12
|
export const paramsPositions: { [key: string]: number } = {
|
|
13
13
|
find: 0,
|
|
14
14
|
update: 2,
|
|
15
15
|
patch: 2
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
export function normalizeError
|
|
19
|
-
const hasToJSON = typeof e.toJSON === 'function'
|
|
20
|
-
const result = hasToJSON ? e.toJSON() : {}
|
|
18
|
+
export function normalizeError(e: any) {
|
|
19
|
+
const hasToJSON = typeof e.toJSON === 'function'
|
|
20
|
+
const result = hasToJSON ? e.toJSON() : {}
|
|
21
21
|
|
|
22
22
|
if (!hasToJSON) {
|
|
23
|
-
Object.getOwnPropertyNames(e).forEach(key => {
|
|
24
|
-
result[key] = e[key]
|
|
25
|
-
})
|
|
23
|
+
Object.getOwnPropertyNames(e).forEach((key) => {
|
|
24
|
+
result[key] = e[key]
|
|
25
|
+
})
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
if (process.env.NODE_ENV === 'production') {
|
|
29
|
-
delete result.stack
|
|
29
|
+
delete result.stack
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
delete result.hook
|
|
32
|
+
delete result.hook
|
|
33
33
|
|
|
34
|
-
return result
|
|
34
|
+
return result
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
export function getDispatcher
|
|
37
|
+
export function getDispatcher(emit: string, socketMap: WeakMap<RealTimeConnection, any>, socketKey?: any) {
|
|
38
38
|
return function (event: string, channel: CombinedChannel, context: HookContext, data?: any) {
|
|
39
|
-
debug(`Dispatching '${event}' to ${channel.length} connections`)
|
|
39
|
+
debug(`Dispatching '${event}' to ${channel.length} connections`)
|
|
40
40
|
|
|
41
|
-
channel.connections.forEach(connection => {
|
|
41
|
+
channel.connections.forEach((connection) => {
|
|
42
42
|
// The reference between connection and socket is set in `app.setup`
|
|
43
|
-
const socket = socketKey ? connection[socketKey] : socketMap.get(connection)
|
|
43
|
+
const socket = socketKey ? connection[socketKey] : socketMap.get(connection)
|
|
44
44
|
|
|
45
45
|
if (socket) {
|
|
46
|
-
const eventName = `${context.path || ''} ${event}`.trim()
|
|
46
|
+
const eventName = `${context.path || ''} ${event}`.trim()
|
|
47
47
|
|
|
48
|
-
let result = channel.dataFor(connection) || context.dispatch || context.result
|
|
48
|
+
let result = channel.dataFor(connection) || context.dispatch || context.result
|
|
49
49
|
|
|
50
50
|
// If we are getting events from an array but try to dispatch individual data
|
|
51
51
|
// try to get the individual item to dispatch from the correct index.
|
|
52
52
|
if (!Array.isArray(data) && Array.isArray(context.result) && Array.isArray(result)) {
|
|
53
|
-
result = context.result.find(resultData => isEqual(resultData, data))
|
|
53
|
+
result = context.result.find((resultData) => isEqual(resultData, data))
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
debug(`Dispatching '${eventName}' to Socket ${socket.id} with`, result)
|
|
56
|
+
debug(`Dispatching '${eventName}' to Socket ${socket.id} with`, result)
|
|
57
57
|
|
|
58
|
-
socket[emit](eventName, result)
|
|
58
|
+
socket[emit](eventName, result)
|
|
59
59
|
}
|
|
60
|
-
})
|
|
61
|
-
}
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
export async function runMethod
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
export async function runMethod(
|
|
65
|
+
app: Application,
|
|
66
|
+
connection: RealTimeConnection,
|
|
67
|
+
path: string,
|
|
68
|
+
method: string,
|
|
69
|
+
args: any[]
|
|
70
|
+
) {
|
|
71
|
+
const trace = `method '${method}' on service '${path}'`
|
|
72
|
+
const methodArgs = args.slice(0)
|
|
73
|
+
const callback =
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
75
|
+
typeof methodArgs[methodArgs.length - 1] === 'function' ? methodArgs.pop() : function () {}
|
|
76
|
+
|
|
77
|
+
debug(`Running ${trace}`, connection, args)
|
|
71
78
|
|
|
72
79
|
const handleError = (error: any) => {
|
|
73
|
-
debug(`Error in ${trace}`, error)
|
|
74
|
-
callback(normalizeError(error))
|
|
75
|
-
}
|
|
80
|
+
debug(`Error in ${trace}`, error)
|
|
81
|
+
callback(normalizeError(error))
|
|
82
|
+
}
|
|
76
83
|
|
|
77
84
|
try {
|
|
78
|
-
const lookup = app.lookup(path)
|
|
85
|
+
const lookup = app.lookup(path)
|
|
79
86
|
|
|
80
87
|
// No valid service was found throw a NotFound error
|
|
81
88
|
if (lookup === null) {
|
|
82
|
-
throw new NotFound(`Service '${path}' not found`)
|
|
89
|
+
throw new NotFound(`Service '${path}' not found`)
|
|
83
90
|
}
|
|
84
91
|
|
|
85
|
-
const { service, params: route = {} } = lookup
|
|
86
|
-
const { methods } = getServiceOptions(service)
|
|
92
|
+
const { service, params: route = {} } = lookup
|
|
93
|
+
const { methods } = getServiceOptions(service)
|
|
87
94
|
|
|
88
95
|
// Only service methods are allowed
|
|
89
96
|
if (!methods.includes(method)) {
|
|
90
|
-
throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`)
|
|
97
|
+
throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`)
|
|
91
98
|
}
|
|
92
99
|
|
|
93
|
-
const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION
|
|
94
|
-
const query = methodArgs[position]
|
|
100
|
+
const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION
|
|
101
|
+
const query = Object.assign({}, methodArgs[position])
|
|
95
102
|
// `params` have to be re-mapped to the query and added with the route
|
|
96
|
-
const params = Object.assign({ query, route, connection }, connection)
|
|
103
|
+
const params = Object.assign({ query, route, connection }, connection)
|
|
97
104
|
|
|
98
105
|
// `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`)
|
|
106
|
+
if (methodArgs.length > position + 1) {
|
|
107
|
+
throw new BadRequest(`Too many arguments for '${method}' method`)
|
|
101
108
|
}
|
|
102
109
|
|
|
103
|
-
methodArgs[position] = params
|
|
110
|
+
methodArgs[position] = params
|
|
104
111
|
|
|
105
|
-
const ctx = createContext(service, method)
|
|
106
|
-
const returnedCtx: HookContext = await (service as any)[method](...methodArgs, ctx)
|
|
107
|
-
const result = returnedCtx.dispatch || returnedCtx.result
|
|
112
|
+
const ctx = createContext(service, method)
|
|
113
|
+
const returnedCtx: HookContext = await (service as any)[method](...methodArgs, ctx)
|
|
114
|
+
const result = returnedCtx.dispatch || returnedCtx.result
|
|
108
115
|
|
|
109
|
-
debug(`Returned successfully ${trace}`, result)
|
|
110
|
-
callback(null, result)
|
|
111
|
-
} catch (error) {
|
|
112
|
-
handleError(error)
|
|
116
|
+
debug(`Returned successfully ${trace}`, result)
|
|
117
|
+
callback(null, result)
|
|
118
|
+
} catch (error: any) {
|
|
119
|
+
handleError(error)
|
|
113
120
|
}
|
|
114
121
|
}
|