@feathersjs/transport-commons 5.0.39 → 6.0.0-pre.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 +86 -0
- package/LICENSE +1 -2
- package/README.md +1 -1
- package/client.d.ts +1 -1
- package/client.js +1 -1
- package/lib/client.d.ts +1 -1
- package/lib/client.js +6 -10
- package/lib/client.js.map +1 -1
- package/lib/index.d.ts +3 -6
- package/lib/index.js +3 -45
- package/lib/index.js.map +1 -1
- package/lib/socket/index.d.ts +1 -1
- package/lib/socket/index.js +6 -13
- package/lib/socket/index.js.map +1 -1
- package/lib/socket/utils.d.ts +2 -2
- package/lib/socket/utils.js +17 -26
- package/lib/socket/utils.js.map +1 -1
- package/package.json +9 -9
- package/src/client.ts +1 -2
- package/src/index.ts +3 -7
- package/src/socket/index.ts +3 -7
- package/src/socket/utils.ts +3 -9
- package/lib/channels/channel/base.d.ts +0 -12
- package/lib/channels/channel/base.js +0 -48
- package/lib/channels/channel/base.js.map +0 -1
- package/lib/channels/channel/combined.d.ts +0 -15
- package/lib/channels/channel/combined.js +0 -45
- package/lib/channels/channel/combined.js.map +0 -1
- package/lib/channels/index.d.ts +0 -28
- package/lib/channels/index.js +0 -86
- package/lib/channels/index.js.map +0 -1
- package/lib/channels/mixins.d.ts +0 -29
- package/lib/channels/mixins.js +0 -72
- package/lib/channels/mixins.js.map +0 -1
- package/lib/http.d.ts +0 -35
- package/lib/http.js +0 -77
- package/lib/http.js.map +0 -1
- package/lib/routing/index.d.ts +0 -21
- package/lib/routing/index.js +0 -49
- package/lib/routing/index.js.map +0 -1
- package/lib/routing/router.d.ts +0 -31
- package/lib/routing/router.js +0 -114
- package/lib/routing/router.js.map +0 -1
- package/src/channels/channel/base.ts +0 -58
- package/src/channels/channel/combined.ts +0 -57
- package/src/channels/index.ts +0 -120
- package/src/channels/mixins.ts +0 -108
- package/src/http.ts +0 -95
- package/src/routing/index.ts +0 -62
- package/src/routing/router.ts +0 -148
package/lib/routing/router.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Router = exports.RouteNode = void 0;
|
|
4
|
-
const commons_1 = require("@feathersjs/commons");
|
|
5
|
-
class RouteNode {
|
|
6
|
-
constructor(name, depth) {
|
|
7
|
-
this.name = name;
|
|
8
|
-
this.depth = depth;
|
|
9
|
-
this.children = {};
|
|
10
|
-
this.placeholders = [];
|
|
11
|
-
}
|
|
12
|
-
get hasChildren() {
|
|
13
|
-
return Object.keys(this.children).length !== 0 || this.placeholders.length !== 0;
|
|
14
|
-
}
|
|
15
|
-
insert(path, data) {
|
|
16
|
-
if (this.depth === path.length) {
|
|
17
|
-
if (this.data !== undefined) {
|
|
18
|
-
throw new Error(`Path ${path.join('/')} already exists`);
|
|
19
|
-
}
|
|
20
|
-
this.data = data;
|
|
21
|
-
return this;
|
|
22
|
-
}
|
|
23
|
-
const current = path[this.depth];
|
|
24
|
-
const nextDepth = this.depth + 1;
|
|
25
|
-
if (current.startsWith(':')) {
|
|
26
|
-
// Insert a placeholder node like /messages/:id
|
|
27
|
-
const placeholderName = current.substring(1);
|
|
28
|
-
let placeholder = this.placeholders.find((p) => p.name === placeholderName);
|
|
29
|
-
if (!placeholder) {
|
|
30
|
-
placeholder = new RouteNode(placeholderName, nextDepth);
|
|
31
|
-
this.placeholders.push(placeholder);
|
|
32
|
-
}
|
|
33
|
-
return placeholder.insert(path, data);
|
|
34
|
-
}
|
|
35
|
-
const child = this.children[current] || new RouteNode(current, nextDepth);
|
|
36
|
-
this.children[current] = child;
|
|
37
|
-
return child.insert(path, data);
|
|
38
|
-
}
|
|
39
|
-
remove(path) {
|
|
40
|
-
if (path.length === this.depth) {
|
|
41
|
-
delete this.data;
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const current = path[this.depth];
|
|
45
|
-
if (current.startsWith(':')) {
|
|
46
|
-
const placeholderName = current.substring(1);
|
|
47
|
-
const placeholder = this.placeholders.find((p) => p.name === placeholderName);
|
|
48
|
-
placeholder.remove(path);
|
|
49
|
-
this.placeholders = this.placeholders.filter((p) => p !== placeholder);
|
|
50
|
-
}
|
|
51
|
-
else if (this.children[current]) {
|
|
52
|
-
const child = this.children[current];
|
|
53
|
-
child.remove(path);
|
|
54
|
-
if (!child.hasChildren) {
|
|
55
|
-
delete this.children[current];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
lookup(path, info) {
|
|
60
|
-
if (path.length === this.depth) {
|
|
61
|
-
return this.data === undefined
|
|
62
|
-
? null
|
|
63
|
-
: {
|
|
64
|
-
...info,
|
|
65
|
-
data: this.data
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
const current = path[this.depth];
|
|
69
|
-
const child = this.children[current];
|
|
70
|
-
if (child) {
|
|
71
|
-
const lookup = child.lookup(path, info);
|
|
72
|
-
if (lookup !== null) {
|
|
73
|
-
return lookup;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
// This will return the first placeholder that matches early
|
|
77
|
-
for (const placeholder of this.placeholders) {
|
|
78
|
-
const result = placeholder.lookup(path, info);
|
|
79
|
-
if (result !== null) {
|
|
80
|
-
result.params[placeholder.name] = current;
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
exports.RouteNode = RouteNode;
|
|
88
|
-
class Router {
|
|
89
|
-
constructor(root = new RouteNode('', 0)) {
|
|
90
|
-
this.root = root;
|
|
91
|
-
this.caseSensitive = true;
|
|
92
|
-
}
|
|
93
|
-
getPath(path) {
|
|
94
|
-
const result = (0, commons_1.stripSlashes)(path).split('/');
|
|
95
|
-
if (!this.caseSensitive) {
|
|
96
|
-
return result.map((p) => (p.startsWith(':') ? p : p.toLowerCase()));
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
insert(path, data) {
|
|
101
|
-
return this.root.insert(this.getPath(path), data);
|
|
102
|
-
}
|
|
103
|
-
remove(path) {
|
|
104
|
-
return this.root.remove(this.getPath(path));
|
|
105
|
-
}
|
|
106
|
-
lookup(path) {
|
|
107
|
-
if (typeof path !== 'string') {
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
return this.root.lookup(this.getPath(path), { params: {} });
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
exports.Router = Router;
|
|
114
|
-
//# sourceMappingURL=router.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/routing/router.ts"],"names":[],"mappings":";;;AAAA,iDAAkD;AAUlD,MAAa,SAAS;IAKpB,YACS,IAAY,EACZ,KAAa;QADb,SAAI,GAAJ,IAAI,CAAQ;QACZ,UAAK,GAAL,KAAK,CAAQ;QALtB,aAAQ,GAAiC,EAAE,CAAA;QAC3C,iBAAY,GAAgB,EAAE,CAAA;IAK3B,CAAC;IAEJ,IAAI,WAAW;QACb,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,KAAK,CAAC,CAAA;IAClF,CAAC;IAED,MAAM,CAAC,IAAc,EAAE,IAAO;QAC5B,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YAC/B,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC5B,MAAM,IAAI,KAAK,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;YAC1D,CAAC;YAED,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;YAChB,OAAO,IAAI,CAAA;QACb,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAA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I,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,OAAO,IAAI,CAAA;QACb,CAAC;QAED,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAA;IAC7D,CAAC;CACF;AA9BD,wBA8BC"}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'events'
|
|
2
|
-
import { RealTimeConnection } from '@feathersjs/feathers'
|
|
3
|
-
|
|
4
|
-
export class Channel extends EventEmitter {
|
|
5
|
-
connections: RealTimeConnection[]
|
|
6
|
-
data: any
|
|
7
|
-
|
|
8
|
-
constructor(connections: RealTimeConnection[] = [], data: any = null) {
|
|
9
|
-
super()
|
|
10
|
-
|
|
11
|
-
this.connections = connections
|
|
12
|
-
this.data = data
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
get length() {
|
|
16
|
-
return this.connections.length
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
leave(...connections: RealTimeConnection[]) {
|
|
20
|
-
connections.forEach((current) => {
|
|
21
|
-
if (typeof current === 'function') {
|
|
22
|
-
const callback = current as (connection: RealTimeConnection) => boolean
|
|
23
|
-
|
|
24
|
-
this.leave(...this.connections.filter(callback))
|
|
25
|
-
} else {
|
|
26
|
-
const index = this.connections.indexOf(current)
|
|
27
|
-
|
|
28
|
-
if (index !== -1) {
|
|
29
|
-
this.connections.splice(index, 1)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
if (this.length === 0) {
|
|
35
|
-
this.emit('empty')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return this
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
join(...connections: RealTimeConnection[]) {
|
|
42
|
-
connections.forEach((connection) => {
|
|
43
|
-
if (connection && this.connections.indexOf(connection) === -1) {
|
|
44
|
-
this.connections.push(connection)
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
return this
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
filter(fn: (connection: RealTimeConnection) => boolean) {
|
|
52
|
-
return new Channel(this.connections.filter(fn), this.data)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
send(data: any) {
|
|
56
|
-
return new Channel(this.connections, data)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { RealTimeConnection } from '@feathersjs/feathers'
|
|
2
|
-
import { Channel } from './base'
|
|
3
|
-
|
|
4
|
-
function collectConnections(children: Channel[]) {
|
|
5
|
-
const mappings = new WeakMap<RealTimeConnection, any>()
|
|
6
|
-
const connections: RealTimeConnection[] = []
|
|
7
|
-
|
|
8
|
-
children.forEach((channel) => {
|
|
9
|
-
channel.connections.forEach((connection) => {
|
|
10
|
-
if (!mappings.has(connection)) {
|
|
11
|
-
connections.push(connection)
|
|
12
|
-
mappings.set(connection, channel.data)
|
|
13
|
-
}
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return { connections, mappings }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class CombinedChannel extends Channel {
|
|
21
|
-
children: Channel[]
|
|
22
|
-
mappings: WeakMap<RealTimeConnection, any>
|
|
23
|
-
|
|
24
|
-
constructor(children: Channel[], data: any = null) {
|
|
25
|
-
const { mappings, connections } = collectConnections(children)
|
|
26
|
-
|
|
27
|
-
super(connections, data)
|
|
28
|
-
|
|
29
|
-
this.children = children
|
|
30
|
-
this.mappings = mappings
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
refresh() {
|
|
34
|
-
const collected = collectConnections(this.children)
|
|
35
|
-
|
|
36
|
-
return Object.assign(this, collected)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
leave(...connections: RealTimeConnection[]) {
|
|
40
|
-
return this.callChildren('leave', connections)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
join(...connections: RealTimeConnection[]) {
|
|
44
|
-
return this.callChildren('join', connections)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
dataFor(connection: RealTimeConnection) {
|
|
48
|
-
return this.mappings.get(connection)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private callChildren(method: string, connections: RealTimeConnection[]) {
|
|
52
|
-
this.children.forEach((child: any) => child[method](...connections))
|
|
53
|
-
this.refresh()
|
|
54
|
-
|
|
55
|
-
return this
|
|
56
|
-
}
|
|
57
|
-
}
|
package/src/channels/index.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { Application, FeathersService, RealTimeConnection, getServiceOptions } from '@feathersjs/feathers'
|
|
2
|
-
import { createDebug } from '@feathersjs/commons'
|
|
3
|
-
import flattenDeep from 'lodash/flattenDeep'
|
|
4
|
-
import { Channel } from './channel/base'
|
|
5
|
-
import { CombinedChannel } from './channel/combined'
|
|
6
|
-
import { channelMixin, publishMixin, keys, PublishMixin, Event, Publisher } from './mixins'
|
|
7
|
-
import EventEmitter from 'events'
|
|
8
|
-
|
|
9
|
-
const debug = createDebug('@feathersjs/transport-commons/channels')
|
|
10
|
-
const { CHANNELS } = keys
|
|
11
|
-
|
|
12
|
-
declare module '@feathersjs/feathers/lib/declarations' {
|
|
13
|
-
interface ServiceAddons<A, S> extends EventEmitter {
|
|
14
|
-
// eslint-disable-line
|
|
15
|
-
publish(publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
16
|
-
publish(event: Event, publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
17
|
-
|
|
18
|
-
registerPublisher(publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
19
|
-
registerPublisher(event: Event, publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
-
interface Application<Services, Settings> {
|
|
24
|
-
// eslint-disable-line
|
|
25
|
-
channels: string[]
|
|
26
|
-
|
|
27
|
-
channel(name: string | string[]): Channel
|
|
28
|
-
channel(...names: string[]): Channel
|
|
29
|
-
|
|
30
|
-
publish<T>(publisher: Publisher<T, this>): this
|
|
31
|
-
publish<T>(event: Event, publisher: Publisher<T, this>): this
|
|
32
|
-
|
|
33
|
-
registerPublisher<T>(publisher: Publisher<T, this>): this
|
|
34
|
-
registerPublisher<T>(event: Event, publisher: Publisher<T, this>): this
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface Params {
|
|
38
|
-
connection?: RealTimeConnection
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export { keys }
|
|
43
|
-
|
|
44
|
-
export function channels() {
|
|
45
|
-
return (app: Application) => {
|
|
46
|
-
if (typeof app.channel === 'function' && typeof app.publish === 'function') {
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
Object.assign(app, channelMixin(), publishMixin())
|
|
51
|
-
Object.defineProperty(app, 'channels', {
|
|
52
|
-
get() {
|
|
53
|
-
return Object.keys(this[CHANNELS])
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
app.mixins.push((service: FeathersService, path: string) => {
|
|
58
|
-
const { serviceEvents } = getServiceOptions(service)
|
|
59
|
-
|
|
60
|
-
if (typeof service.publish === 'function') {
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
Object.assign(service, publishMixin())
|
|
65
|
-
|
|
66
|
-
serviceEvents.forEach((event: string) => {
|
|
67
|
-
service.on(event, function (data, hook) {
|
|
68
|
-
if (!hook) {
|
|
69
|
-
// Fake hook for custom events
|
|
70
|
-
hook = { path, service, app, result: data }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
debug('Publishing event', event, hook.path)
|
|
74
|
-
|
|
75
|
-
const logError = (error: any) => debug(`Error in '${hook.path} ${event}' publisher`, error)
|
|
76
|
-
const servicePublishers = (service as unknown as PublishMixin)[keys.PUBLISHERS]
|
|
77
|
-
const appPublishers = (app as unknown as PublishMixin)[keys.PUBLISHERS]
|
|
78
|
-
// This will return the first publisher list that is not empty
|
|
79
|
-
// In the following precedence
|
|
80
|
-
const publisher =
|
|
81
|
-
// 1. Service publisher for a specific event
|
|
82
|
-
servicePublishers[event] ||
|
|
83
|
-
// 2. Service publisher for all events
|
|
84
|
-
servicePublishers[keys.ALL_EVENTS] ||
|
|
85
|
-
// 3. App publisher for a specific event
|
|
86
|
-
appPublishers[event] ||
|
|
87
|
-
// 4. App publisher for all events
|
|
88
|
-
appPublishers[keys.ALL_EVENTS] ||
|
|
89
|
-
// 5. No publisher
|
|
90
|
-
(() => {})
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
Promise.resolve(publisher(data, hook))
|
|
94
|
-
.then((result: any) => {
|
|
95
|
-
if (!result) {
|
|
96
|
-
return
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const results = Array.isArray(result)
|
|
100
|
-
? flattenDeep(result).filter(Boolean)
|
|
101
|
-
: ([result] as Channel[])
|
|
102
|
-
const channel = new CombinedChannel(results)
|
|
103
|
-
|
|
104
|
-
if (channel && channel.length > 0) {
|
|
105
|
-
app.emit('publish', event, channel, hook, data)
|
|
106
|
-
} else {
|
|
107
|
-
debug('No connections to publish to')
|
|
108
|
-
}
|
|
109
|
-
})
|
|
110
|
-
.catch(logError)
|
|
111
|
-
} catch (error: any) {
|
|
112
|
-
logError(error)
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
})
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export { Channel, CombinedChannel, RealTimeConnection }
|
package/src/channels/mixins.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
|
|
2
|
-
import { Application, HookContext, getServiceOptions, defaultServiceEvents } from '@feathersjs/feathers'
|
|
3
|
-
import { createDebug } from '@feathersjs/commons'
|
|
4
|
-
import { Channel } from './channel/base'
|
|
5
|
-
import { CombinedChannel } from './channel/combined'
|
|
6
|
-
|
|
7
|
-
const debug = createDebug('@feathersjs/transport-commons/channels/mixins')
|
|
8
|
-
const PUBLISHERS = Symbol.for('@feathersjs/transport-commons/publishers')
|
|
9
|
-
const CHANNELS = Symbol.for('@feathersjs/transport-commons/channels')
|
|
10
|
-
const ALL_EVENTS = Symbol.for('@feathersjs/transport-commons/all-events')
|
|
11
|
-
|
|
12
|
-
export const keys = {
|
|
13
|
-
PUBLISHERS: PUBLISHERS as typeof PUBLISHERS,
|
|
14
|
-
CHANNELS: CHANNELS as typeof CHANNELS,
|
|
15
|
-
ALL_EVENTS: ALL_EVENTS as typeof ALL_EVENTS
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface ChannelMixin {
|
|
19
|
-
[CHANNELS]: { [key: string]: Channel }
|
|
20
|
-
channel(...names: string[]): Channel
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function channelMixin() {
|
|
24
|
-
const mixin: ChannelMixin = {
|
|
25
|
-
[CHANNELS]: {},
|
|
26
|
-
|
|
27
|
-
channel(...names: string[]): Channel {
|
|
28
|
-
debug('Returning channels', names)
|
|
29
|
-
|
|
30
|
-
if (names.length === 0) {
|
|
31
|
-
throw new Error('app.channel needs at least one channel name')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (names.length === 1) {
|
|
35
|
-
const [name] = names
|
|
36
|
-
|
|
37
|
-
if (Array.isArray(name)) {
|
|
38
|
-
return this.channel(...name)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!this[CHANNELS][name]) {
|
|
42
|
-
const channel = new Channel()
|
|
43
|
-
|
|
44
|
-
channel.once('empty', () => {
|
|
45
|
-
channel.removeAllListeners()
|
|
46
|
-
delete this[CHANNELS][name]
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
this[CHANNELS][name] = channel
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return this[CHANNELS][name]
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const channels = names.map((name) => this.channel(name))
|
|
56
|
-
|
|
57
|
-
return new CombinedChannel(channels)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return mixin
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type Event = string | typeof ALL_EVENTS
|
|
65
|
-
|
|
66
|
-
export type Publisher<T = any, A = Application, S = any> = (
|
|
67
|
-
data: T,
|
|
68
|
-
context: HookContext<A, S>
|
|
69
|
-
) => Channel | Channel[] | void | Promise<Channel | Channel[] | void>
|
|
70
|
-
|
|
71
|
-
export interface PublishMixin<T = any> {
|
|
72
|
-
[PUBLISHERS]: { [ALL_EVENTS]?: Publisher<T>; [key: string]: Publisher<T> }
|
|
73
|
-
publish(event: Event, publisher: Publisher<T>): this
|
|
74
|
-
registerPublisher(event: Event, publisher: Publisher<T>): this
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function publishMixin() {
|
|
78
|
-
const result: PublishMixin = {
|
|
79
|
-
[PUBLISHERS]: {},
|
|
80
|
-
|
|
81
|
-
publish(...args) {
|
|
82
|
-
return this.registerPublisher(...args)
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
registerPublisher(event, publisher) {
|
|
86
|
-
debug('Registering publisher', event)
|
|
87
|
-
|
|
88
|
-
if (!publisher && typeof event === 'function') {
|
|
89
|
-
publisher = event
|
|
90
|
-
event = ALL_EVENTS
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const { serviceEvents = defaultServiceEvents } = getServiceOptions(this) || {}
|
|
94
|
-
|
|
95
|
-
if (event !== ALL_EVENTS && !serviceEvents.includes(event)) {
|
|
96
|
-
throw new Error(`'${event.toString()}' is not a valid service event`)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const publishers = this[PUBLISHERS]
|
|
100
|
-
|
|
101
|
-
publishers[event] = publisher
|
|
102
|
-
|
|
103
|
-
return this
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return result
|
|
108
|
-
}
|
package/src/http.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
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 (location !== undefined) {
|
|
66
|
-
return statusCodes.seeOther
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (!body) {
|
|
70
|
-
return statusCodes.noContent
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (context.method === 'create') {
|
|
74
|
-
return statusCodes.created
|
|
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/routing/index.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { Application, FeathersService, ServiceOptions } from '@feathersjs/feathers'
|
|
2
|
-
import { Router } from './router'
|
|
3
|
-
|
|
4
|
-
declare module '@feathersjs/feathers/lib/declarations' {
|
|
5
|
-
interface RouteLookup {
|
|
6
|
-
service: Service
|
|
7
|
-
params: { [key: string]: any }
|
|
8
|
-
}
|
|
9
|
-
|
|
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
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export * from './router'
|
|
22
|
-
|
|
23
|
-
const lookup = function (this: Application, path: string) {
|
|
24
|
-
const result = this.routes.lookup(path)
|
|
25
|
-
|
|
26
|
-
if (result === null) {
|
|
27
|
-
return null
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const {
|
|
31
|
-
params: colonParams,
|
|
32
|
-
data: { service, params: dataParams }
|
|
33
|
-
} = result
|
|
34
|
-
|
|
35
|
-
const params = dataParams ? { ...dataParams, ...colonParams } : colonParams
|
|
36
|
-
|
|
37
|
-
return { service, params }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const routing = () => (app: Application) => {
|
|
41
|
-
if (typeof app.lookup === 'function') {
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const { unuse } = app
|
|
46
|
-
|
|
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
|
-
}
|
|
54
|
-
|
|
55
|
-
// Add a mixin that registers a service on the router
|
|
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
|
-
}
|