@pathscale/wss-adapter 1.0.1 → 1.0.4
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/dist/types/index.d.ts +55 -46
- package/dist/wssAdapter.js +23 -17
- package/package.json +1 -1
- package/types/index.ts +19 -15
- package/wssAdapter.ts +52 -26
package/dist/types/index.d.ts
CHANGED
|
@@ -1,68 +1,77 @@
|
|
|
1
1
|
interface IService {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
remote: string
|
|
3
|
+
methods: {
|
|
4
|
+
[methodCode: string]: {
|
|
5
|
+
name: string
|
|
6
|
+
parameters: string[]
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
9
|
}
|
|
10
10
|
interface IServiceConfig extends IService {
|
|
11
|
-
|
|
11
|
+
onDisconnect: (event: {
|
|
12
|
+
code: number
|
|
13
|
+
reason: string
|
|
14
|
+
wasClean: boolean
|
|
15
|
+
}) => void | null
|
|
12
16
|
}
|
|
13
17
|
interface IServices {
|
|
14
|
-
|
|
18
|
+
[serviceName: string]: IServiceConfig
|
|
15
19
|
}
|
|
16
20
|
interface IErrors {
|
|
17
|
-
|
|
21
|
+
[errorCode: number]: string
|
|
18
22
|
}
|
|
19
23
|
interface IConfiguration {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
error: number;
|
|
25
|
-
message: string;
|
|
26
|
-
}) => void;
|
|
24
|
+
timeout: number
|
|
25
|
+
services: IServices
|
|
26
|
+
errors: IErrors
|
|
27
|
+
onError: (error: { error: number; message: string }) => void
|
|
27
28
|
}
|
|
28
29
|
interface IServiceConnect {
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
connect<T>(
|
|
31
|
+
payload: string | string[] | undefined,
|
|
32
|
+
remote?: string
|
|
33
|
+
): Promise<T>
|
|
34
|
+
disconnect: () => void
|
|
31
35
|
}
|
|
32
36
|
interface IWssAdapter {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
services: {
|
|
38
|
+
[serviceName: string]: IServiceConnect
|
|
39
|
+
}
|
|
40
|
+
sessions: {
|
|
41
|
+
[serviceName: string]: any
|
|
42
|
+
}
|
|
43
|
+
configure: (configuration: IConfiguration) => void
|
|
40
44
|
}
|
|
41
45
|
interface ISequence {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
value: number
|
|
47
|
+
getSeq: () => number
|
|
48
|
+
decreaseSeq: () => void
|
|
45
49
|
}
|
|
46
50
|
interface ISessions {
|
|
47
|
-
|
|
51
|
+
[serviceName: string]: WebSocket
|
|
48
52
|
}
|
|
49
53
|
interface IPendingPromises {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
[seq: number]: {
|
|
55
|
+
resolve: (payload: unknown) => void
|
|
56
|
+
reject: (error: Error) => void
|
|
57
|
+
toHandler: ReturnType<typeof setTimeout>
|
|
58
|
+
}
|
|
55
59
|
}
|
|
56
60
|
interface IStore {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
timeout: number
|
|
62
|
+
errors: IErrors
|
|
63
|
+
services: IServices
|
|
64
|
+
sequence: ISequence
|
|
65
|
+
sessions: ISessions
|
|
66
|
+
pendingPromises: IPendingPromises
|
|
67
|
+
onError: (error: { error: number; message: string }) => void
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
IStore,
|
|
71
|
+
IWssAdapter,
|
|
72
|
+
IServiceConfig,
|
|
73
|
+
IConfiguration,
|
|
74
|
+
IErrors,
|
|
75
|
+
IServices,
|
|
76
|
+
IService,
|
|
67
77
|
}
|
|
68
|
-
export { IStore, IWssAdapter, IServiceConfig, IConfiguration, IErrors, IServices, IService };
|
package/dist/wssAdapter.js
CHANGED
|
@@ -37,8 +37,8 @@ wssAdapter.configure = function (configuration) {
|
|
|
37
37
|
// construct services objects with two simple functions
|
|
38
38
|
// intended use: `wssAdapter.services.admin.connect([1, 2, 3])` or `wssAdapter.services.auth.connect([1, 2, 3])`
|
|
39
39
|
wssAdapter.services[serviceName] = {
|
|
40
|
-
connect: function (payload) {
|
|
41
|
-
return connectHandler(serviceName, serviceConfig, payload);
|
|
40
|
+
connect: function (payload, remote) {
|
|
41
|
+
return connectHandler(serviceName, serviceConfig, payload, remote);
|
|
42
42
|
},
|
|
43
43
|
disconnect: function () { return disconnectHandler(serviceName); },
|
|
44
44
|
};
|
|
@@ -55,21 +55,25 @@ wssAdapter.configure = function (configuration) {
|
|
|
55
55
|
_loop_1(serviceName, serviceConfig);
|
|
56
56
|
}
|
|
57
57
|
};
|
|
58
|
-
var connectHandler = function (serviceName, serviceConfig, payload) {
|
|
58
|
+
var connectHandler = function (serviceName, serviceConfig, payload, remote) {
|
|
59
59
|
return new Promise(function (resolve, reject) {
|
|
60
|
-
store.sessions[serviceName] = new WebSocket(serviceConfig.remote, payload);
|
|
60
|
+
store.sessions[serviceName] = new WebSocket(remote || serviceConfig.remote, payload);
|
|
61
61
|
store.sessions[serviceName].onmessage = function (event) {
|
|
62
62
|
var _a;
|
|
63
63
|
var response = JSON.parse(event.data);
|
|
64
64
|
console.log(response);
|
|
65
65
|
if (response.params.error) {
|
|
66
|
-
reject(new Error((_a = store.errors[response.params.error]) !== null && _a !== void 0 ? _a :
|
|
66
|
+
reject(new Error((_a = store.errors[response.params.error]) !== null && _a !== void 0 ? _a : response.params.error));
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
69
|
store.sessions[serviceName].onmessage = receiveHandler;
|
|
70
70
|
resolve(response.params);
|
|
71
71
|
};
|
|
72
|
-
store.sessions[serviceName].onclose =
|
|
72
|
+
store.sessions[serviceName].onclose = function (event) {
|
|
73
|
+
var _a;
|
|
74
|
+
(_a = serviceConfig.onDisconnect) === null || _a === void 0 ? void 0 : _a.call(serviceConfig, event);
|
|
75
|
+
reject(new Error("code: ".concat(event.code, ", reason: ").concat(event.reason, ", wasClean: ").concat(event.wasClean)));
|
|
76
|
+
};
|
|
73
77
|
});
|
|
74
78
|
};
|
|
75
79
|
var disconnectHandler = function (serviceName) {
|
|
@@ -90,18 +94,20 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
90
94
|
if (!methodCode) {
|
|
91
95
|
throw new Error("method ".concat(methodName, " not available in ").concat(serviceName, " service"));
|
|
92
96
|
}
|
|
93
|
-
if (!Object.keys(params).every(function (param) {
|
|
94
|
-
return serviceConfig.methods[methodCode].parameters.includes(param);
|
|
95
|
-
})) {
|
|
96
|
-
throw new Error("method ".concat(methodCode, " is being called with missing parameters"));
|
|
97
|
-
}
|
|
98
97
|
var purgedParams = {};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
98
|
+
if (params) {
|
|
99
|
+
if (!Object.keys(params).every(function (param) {
|
|
100
|
+
return serviceConfig.methods[methodCode].parameters.includes(param);
|
|
101
|
+
})) {
|
|
102
|
+
throw new Error("method ".concat(methodCode, " is being called with missing parameters"));
|
|
103
|
+
}
|
|
104
|
+
serviceConfig.methods[methodCode].parameters.forEach(function (k) {
|
|
105
|
+
purgedParams[k] = params[k];
|
|
106
|
+
});
|
|
107
|
+
var difference = Object.keys(params).filter(function (x) { return !serviceConfig.methods[methodCode].parameters.includes(x); });
|
|
108
|
+
if (difference.length) {
|
|
109
|
+
throw new Error("method ".concat(methodCode, " is being called with unknow parameters, ").concat(difference));
|
|
110
|
+
}
|
|
105
111
|
}
|
|
106
112
|
var payload = {
|
|
107
113
|
method: Number.parseInt(methodCode),
|
package/package.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -7,51 +7,49 @@ interface IService {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
|
|
11
10
|
interface IServiceConfig extends IService {
|
|
12
|
-
onDisconnect: (
|
|
11
|
+
onDisconnect: (event: {
|
|
12
|
+
code: number
|
|
13
|
+
reason: string
|
|
14
|
+
wasClean: boolean
|
|
15
|
+
}) => void | null
|
|
13
16
|
}
|
|
14
|
-
|
|
15
17
|
interface IServices {
|
|
16
18
|
[serviceName: string]: IServiceConfig
|
|
17
19
|
}
|
|
18
|
-
|
|
19
20
|
interface IErrors {
|
|
20
21
|
[errorCode: number]: string
|
|
21
22
|
}
|
|
22
|
-
|
|
23
23
|
interface IConfiguration {
|
|
24
24
|
timeout: number
|
|
25
25
|
services: IServices
|
|
26
26
|
errors: IErrors
|
|
27
27
|
onError: (error: { error: number; message: string }) => void
|
|
28
28
|
}
|
|
29
|
-
|
|
30
29
|
interface IServiceConnect {
|
|
31
|
-
connect<T>(
|
|
30
|
+
connect<T>(
|
|
31
|
+
payload: string | string[] | undefined,
|
|
32
|
+
remote?: string
|
|
33
|
+
): Promise<T>
|
|
32
34
|
disconnect: () => void
|
|
33
35
|
}
|
|
34
|
-
|
|
35
36
|
interface IWssAdapter {
|
|
36
37
|
services: {
|
|
37
38
|
[serviceName: string]: IServiceConnect
|
|
38
39
|
}
|
|
39
40
|
sessions: {
|
|
40
|
-
[serviceName: string]:
|
|
41
|
+
[serviceName: string]: any
|
|
41
42
|
}
|
|
42
43
|
configure: (configuration: IConfiguration) => void
|
|
43
44
|
}
|
|
44
|
-
|
|
45
45
|
interface ISequence {
|
|
46
46
|
value: number
|
|
47
47
|
getSeq: () => number
|
|
48
48
|
decreaseSeq: () => void
|
|
49
49
|
}
|
|
50
|
-
|
|
51
50
|
interface ISessions {
|
|
52
51
|
[serviceName: string]: WebSocket
|
|
53
52
|
}
|
|
54
|
-
|
|
55
53
|
interface IPendingPromises {
|
|
56
54
|
[seq: number]: {
|
|
57
55
|
resolve: (payload: unknown) => void
|
|
@@ -59,7 +57,6 @@ interface IPendingPromises {
|
|
|
59
57
|
toHandler: ReturnType<typeof setTimeout>
|
|
60
58
|
}
|
|
61
59
|
}
|
|
62
|
-
|
|
63
60
|
interface IStore {
|
|
64
61
|
timeout: number
|
|
65
62
|
errors: IErrors
|
|
@@ -69,5 +66,12 @@ interface IStore {
|
|
|
69
66
|
pendingPromises: IPendingPromises
|
|
70
67
|
onError: (error: { error: number; message: string }) => void
|
|
71
68
|
}
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
export {
|
|
70
|
+
IStore,
|
|
71
|
+
IWssAdapter,
|
|
72
|
+
IServiceConfig,
|
|
73
|
+
IConfiguration,
|
|
74
|
+
IErrors,
|
|
75
|
+
IServices,
|
|
76
|
+
IService,
|
|
77
|
+
}
|
package/wssAdapter.ts
CHANGED
|
@@ -31,7 +31,7 @@ const store: IStore = {
|
|
|
31
31
|
onError() {},
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
wssAdapter.configure = configuration => {
|
|
34
|
+
wssAdapter.configure = (configuration) => {
|
|
35
35
|
const { timeout, services, errors, onError } = configuration
|
|
36
36
|
|
|
37
37
|
// save some stuff for later retrieval
|
|
@@ -44,8 +44,8 @@ wssAdapter.configure = configuration => {
|
|
|
44
44
|
// construct services objects with two simple functions
|
|
45
45
|
// intended use: `wssAdapter.services.admin.connect([1, 2, 3])` or `wssAdapter.services.auth.connect([1, 2, 3])`
|
|
46
46
|
wssAdapter.services[serviceName] = {
|
|
47
|
-
connect: <T>(payload: string | string[] | undefined) =>
|
|
48
|
-
connectHandler<T>(serviceName, serviceConfig, payload),
|
|
47
|
+
connect: <T>(payload: string | string[] | undefined, remote?: string) =>
|
|
48
|
+
connectHandler<T>(serviceName, serviceConfig, payload, remote),
|
|
49
49
|
disconnect: () => disconnectHandler(serviceName),
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -54,9 +54,10 @@ wssAdapter.configure = configuration => {
|
|
|
54
54
|
wssAdapter.sessions[serviceName] = new Proxy(
|
|
55
55
|
{},
|
|
56
56
|
{
|
|
57
|
-
get:
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
get:
|
|
58
|
+
(target, methodName: string) => (payload: Record<string, unknown>) =>
|
|
59
|
+
sendHandler(serviceName, serviceConfig, methodName, payload),
|
|
60
|
+
}
|
|
60
61
|
)
|
|
61
62
|
}
|
|
62
63
|
}
|
|
@@ -65,16 +66,24 @@ const connectHandler = <T>(
|
|
|
65
66
|
serviceName: string,
|
|
66
67
|
serviceConfig: IServiceConfig,
|
|
67
68
|
payload: string | string[] | undefined,
|
|
69
|
+
remote?: string
|
|
68
70
|
) => {
|
|
69
71
|
return new Promise((resolve, reject) => {
|
|
70
|
-
store.sessions[serviceName] = new WebSocket(
|
|
72
|
+
store.sessions[serviceName] = new WebSocket(
|
|
73
|
+
remote || serviceConfig.remote,
|
|
74
|
+
payload
|
|
75
|
+
)
|
|
71
76
|
|
|
72
77
|
store.sessions[serviceName].onmessage = function (event: { data: string }) {
|
|
73
78
|
const response = JSON.parse(event.data)
|
|
74
79
|
console.log(response)
|
|
75
80
|
|
|
76
81
|
if (response.params.error) {
|
|
77
|
-
reject(
|
|
82
|
+
reject(
|
|
83
|
+
new Error(
|
|
84
|
+
store.errors[response.params.error] ?? response.params.error
|
|
85
|
+
)
|
|
86
|
+
)
|
|
78
87
|
return
|
|
79
88
|
}
|
|
80
89
|
|
|
@@ -82,7 +91,15 @@ const connectHandler = <T>(
|
|
|
82
91
|
resolve(response.params)
|
|
83
92
|
}
|
|
84
93
|
|
|
85
|
-
store.sessions[serviceName].onclose =
|
|
94
|
+
store.sessions[serviceName].onclose = (event) => {
|
|
95
|
+
serviceConfig.onDisconnect?.(event)
|
|
96
|
+
|
|
97
|
+
reject(
|
|
98
|
+
new Error(
|
|
99
|
+
`code: ${event.code}, reason: ${event.reason}, wasClean: ${event.wasClean}`
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
}
|
|
86
103
|
}) as Promise<T>
|
|
87
104
|
}
|
|
88
105
|
|
|
@@ -94,35 +111,44 @@ const sendHandler = (
|
|
|
94
111
|
serviceName: string,
|
|
95
112
|
serviceConfig: IServiceConfig,
|
|
96
113
|
methodName: string,
|
|
97
|
-
params: Record<string, unknown
|
|
114
|
+
params: Record<string, unknown>
|
|
98
115
|
) => {
|
|
99
116
|
const methodCode = Object.entries(serviceConfig.methods)
|
|
100
117
|
.map(([code, info]) => ({ code, info }))
|
|
101
118
|
.find(({ info }) => info.name === methodName)?.code
|
|
102
119
|
|
|
103
120
|
if (!methodCode) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
!Object.keys(params).every(param =>
|
|
109
|
-
serviceConfig.methods[methodCode].parameters.includes(param),
|
|
121
|
+
throw new Error(
|
|
122
|
+
`method ${methodName} not available in ${serviceName} service`
|
|
110
123
|
)
|
|
111
|
-
) {
|
|
112
|
-
throw new Error(`method ${methodCode} is being called with missing parameters`)
|
|
113
124
|
}
|
|
114
125
|
|
|
115
126
|
const purgedParams: Record<string, unknown> = {}
|
|
116
|
-
serviceConfig.methods[methodCode].parameters.forEach(k => {
|
|
117
|
-
purgedParams[k] = params[k]
|
|
118
|
-
})
|
|
119
127
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
if (params) {
|
|
129
|
+
if (
|
|
130
|
+
!Object.keys(params).every((param) =>
|
|
131
|
+
serviceConfig.methods[methodCode].parameters.includes(param)
|
|
132
|
+
)
|
|
133
|
+
) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`method ${methodCode} is being called with missing parameters`
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
serviceConfig.methods[methodCode].parameters.forEach((k) => {
|
|
140
|
+
purgedParams[k] = params[k]
|
|
141
|
+
})
|
|
123
142
|
|
|
124
|
-
|
|
125
|
-
|
|
143
|
+
const difference = Object.keys(params).filter(
|
|
144
|
+
(x) => !serviceConfig.methods[methodCode].parameters.includes(x)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if (difference.length) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`method ${methodCode} is being called with unknow parameters, ${difference}`
|
|
150
|
+
)
|
|
151
|
+
}
|
|
126
152
|
}
|
|
127
153
|
|
|
128
154
|
const payload = {
|