@pathscale/wss-adapter 1.0.0 → 1.0.3
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 +7 -3
- package/dist/wssAdapter.js +10 -6
- package/package.json +4 -4
- package/types/index.ts +18 -3
- package/wssAdapter.ts +40 -17
package/dist/types/index.d.ts
CHANGED
|
@@ -8,7 +8,11 @@ interface IService {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
interface IServiceConfig extends IService {
|
|
11
|
-
onDisconnect: (
|
|
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;
|
|
@@ -26,7 +30,7 @@ interface IConfiguration {
|
|
|
26
30
|
}) => void;
|
|
27
31
|
}
|
|
28
32
|
interface IServiceConnect {
|
|
29
|
-
connect<T>(payload: string | string[] | undefined): Promise<T>;
|
|
33
|
+
connect<T>(payload: string | string[] | undefined, remote?: string): Promise<T>;
|
|
30
34
|
disconnect: () => void;
|
|
31
35
|
}
|
|
32
36
|
interface IWssAdapter {
|
|
@@ -65,4 +69,4 @@ interface IStore {
|
|
|
65
69
|
message: string;
|
|
66
70
|
}) => void;
|
|
67
71
|
}
|
|
68
|
-
export { IStore, IWssAdapter, IServiceConfig, IConfiguration, IErrors, IServices, IService };
|
|
72
|
+
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) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pathscale/wss-adapter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Websocket adapter for the Pathscale WSS",
|
|
5
5
|
"main": "dist/wssAdapter.js",
|
|
6
6
|
"types": "dist/wssAdapter.d.ts",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "git+https://github.com/
|
|
17
|
+
"url": "git+https://github.com/pathscale/wss-adapter.git"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
20
|
-
"url": "https://github.com/
|
|
20
|
+
"url": "https://github.com/pathscale/wss-adapter/issues"
|
|
21
21
|
},
|
|
22
|
-
"homepage": "https://github.com/
|
|
22
|
+
"homepage": "https://github.com/pathscale/wss-adapter#readme"
|
|
23
23
|
}
|
package/types/index.ts
CHANGED
|
@@ -9,7 +9,11 @@ interface IService {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
interface IServiceConfig extends IService {
|
|
12
|
-
onDisconnect: (
|
|
12
|
+
onDisconnect: (event: {
|
|
13
|
+
code: number
|
|
14
|
+
reason: string
|
|
15
|
+
wasClean: boolean
|
|
16
|
+
}) => void | null
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
interface IServices {
|
|
@@ -28,7 +32,10 @@ interface IConfiguration {
|
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
interface IServiceConnect {
|
|
31
|
-
connect<T>(
|
|
35
|
+
connect<T>(
|
|
36
|
+
payload: string | string[] | undefined,
|
|
37
|
+
remote?: string
|
|
38
|
+
): Promise<T>
|
|
32
39
|
disconnect: () => void
|
|
33
40
|
}
|
|
34
41
|
|
|
@@ -70,4 +77,12 @@ interface IStore {
|
|
|
70
77
|
onError: (error: { error: number; message: string }) => void
|
|
71
78
|
}
|
|
72
79
|
|
|
73
|
-
export {
|
|
80
|
+
export {
|
|
81
|
+
IStore,
|
|
82
|
+
IWssAdapter,
|
|
83
|
+
IServiceConfig,
|
|
84
|
+
IConfiguration,
|
|
85
|
+
IErrors,
|
|
86
|
+
IServices,
|
|
87
|
+
IService,
|
|
88
|
+
}
|
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,41 @@ 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(
|
|
121
|
+
throw new Error(
|
|
122
|
+
`method ${methodName} not available in ${serviceName} service`
|
|
123
|
+
)
|
|
105
124
|
}
|
|
106
125
|
|
|
107
126
|
if (
|
|
108
|
-
!Object.keys(params).every(param =>
|
|
109
|
-
serviceConfig.methods[methodCode].parameters.includes(param)
|
|
127
|
+
!Object.keys(params).every((param) =>
|
|
128
|
+
serviceConfig.methods[methodCode].parameters.includes(param)
|
|
110
129
|
)
|
|
111
130
|
) {
|
|
112
|
-
throw new Error(
|
|
131
|
+
throw new Error(
|
|
132
|
+
`method ${methodCode} is being called with missing parameters`
|
|
133
|
+
)
|
|
113
134
|
}
|
|
114
135
|
|
|
115
136
|
const purgedParams: Record<string, unknown> = {}
|
|
116
|
-
serviceConfig.methods[methodCode].parameters.forEach(k => {
|
|
137
|
+
serviceConfig.methods[methodCode].parameters.forEach((k) => {
|
|
117
138
|
purgedParams[k] = params[k]
|
|
118
139
|
})
|
|
119
140
|
|
|
120
141
|
const difference = Object.keys(params).filter(
|
|
121
|
-
x => !serviceConfig.methods[methodCode].parameters.includes(x)
|
|
142
|
+
(x) => !serviceConfig.methods[methodCode].parameters.includes(x)
|
|
122
143
|
)
|
|
123
144
|
|
|
124
145
|
if (difference.length) {
|
|
125
|
-
throw new Error(
|
|
146
|
+
throw new Error(
|
|
147
|
+
`method ${methodCode} is being called with unknow parameters, ${difference}`
|
|
148
|
+
)
|
|
126
149
|
}
|
|
127
150
|
|
|
128
151
|
const payload = {
|