@pathscale/wss-adapter 1.0.1 → 1.0.2
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 +6 -2
- package/dist/wssAdapter.js +6 -2
- package/package.json +1 -1
- package/types/index.ts +14 -2
- package/wssAdapter.ts +34 -15
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;
|
|
@@ -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
|
@@ -63,13 +63,17 @@ var connectHandler = function (serviceName, serviceConfig, payload) {
|
|
|
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
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 {
|
|
@@ -70,4 +74,12 @@ interface IStore {
|
|
|
70
74
|
onError: (error: { error: number; message: string }) => void
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
export {
|
|
77
|
+
export {
|
|
78
|
+
IStore,
|
|
79
|
+
IWssAdapter,
|
|
80
|
+
IServiceConfig,
|
|
81
|
+
IConfiguration,
|
|
82
|
+
IErrors,
|
|
83
|
+
IServices,
|
|
84
|
+
IService,
|
|
85
|
+
}
|
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
|
|
@@ -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
|
}
|
|
@@ -64,7 +65,7 @@ wssAdapter.configure = configuration => {
|
|
|
64
65
|
const connectHandler = <T>(
|
|
65
66
|
serviceName: string,
|
|
66
67
|
serviceConfig: IServiceConfig,
|
|
67
|
-
payload: string | string[] | undefined
|
|
68
|
+
payload: string | string[] | undefined
|
|
68
69
|
) => {
|
|
69
70
|
return new Promise((resolve, reject) => {
|
|
70
71
|
store.sessions[serviceName] = new WebSocket(serviceConfig.remote, payload)
|
|
@@ -74,7 +75,11 @@ const connectHandler = <T>(
|
|
|
74
75
|
console.log(response)
|
|
75
76
|
|
|
76
77
|
if (response.params.error) {
|
|
77
|
-
reject(
|
|
78
|
+
reject(
|
|
79
|
+
new Error(
|
|
80
|
+
store.errors[response.params.error] ?? response.params.error
|
|
81
|
+
)
|
|
82
|
+
)
|
|
78
83
|
return
|
|
79
84
|
}
|
|
80
85
|
|
|
@@ -82,7 +87,15 @@ const connectHandler = <T>(
|
|
|
82
87
|
resolve(response.params)
|
|
83
88
|
}
|
|
84
89
|
|
|
85
|
-
store.sessions[serviceName].onclose =
|
|
90
|
+
store.sessions[serviceName].onclose = (event) => {
|
|
91
|
+
serviceConfig.onDisconnect?.(event)
|
|
92
|
+
|
|
93
|
+
reject(
|
|
94
|
+
new Error(
|
|
95
|
+
`code: ${event.code}, reason: ${event.reason}, wasClean: ${event.wasClean}`
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
}
|
|
86
99
|
}) as Promise<T>
|
|
87
100
|
}
|
|
88
101
|
|
|
@@ -94,35 +107,41 @@ const sendHandler = (
|
|
|
94
107
|
serviceName: string,
|
|
95
108
|
serviceConfig: IServiceConfig,
|
|
96
109
|
methodName: string,
|
|
97
|
-
params: Record<string, unknown
|
|
110
|
+
params: Record<string, unknown>
|
|
98
111
|
) => {
|
|
99
112
|
const methodCode = Object.entries(serviceConfig.methods)
|
|
100
113
|
.map(([code, info]) => ({ code, info }))
|
|
101
114
|
.find(({ info }) => info.name === methodName)?.code
|
|
102
115
|
|
|
103
116
|
if (!methodCode) {
|
|
104
|
-
throw new Error(
|
|
117
|
+
throw new Error(
|
|
118
|
+
`method ${methodName} not available in ${serviceName} service`
|
|
119
|
+
)
|
|
105
120
|
}
|
|
106
121
|
|
|
107
122
|
if (
|
|
108
|
-
!Object.keys(params).every(param =>
|
|
109
|
-
serviceConfig.methods[methodCode].parameters.includes(param)
|
|
123
|
+
!Object.keys(params).every((param) =>
|
|
124
|
+
serviceConfig.methods[methodCode].parameters.includes(param)
|
|
110
125
|
)
|
|
111
126
|
) {
|
|
112
|
-
throw new Error(
|
|
127
|
+
throw new Error(
|
|
128
|
+
`method ${methodCode} is being called with missing parameters`
|
|
129
|
+
)
|
|
113
130
|
}
|
|
114
131
|
|
|
115
132
|
const purgedParams: Record<string, unknown> = {}
|
|
116
|
-
serviceConfig.methods[methodCode].parameters.forEach(k => {
|
|
133
|
+
serviceConfig.methods[methodCode].parameters.forEach((k) => {
|
|
117
134
|
purgedParams[k] = params[k]
|
|
118
135
|
})
|
|
119
136
|
|
|
120
137
|
const difference = Object.keys(params).filter(
|
|
121
|
-
x => !serviceConfig.methods[methodCode].parameters.includes(x)
|
|
138
|
+
(x) => !serviceConfig.methods[methodCode].parameters.includes(x)
|
|
122
139
|
)
|
|
123
140
|
|
|
124
141
|
if (difference.length) {
|
|
125
|
-
throw new Error(
|
|
142
|
+
throw new Error(
|
|
143
|
+
`method ${methodCode} is being called with unknow parameters, ${difference}`
|
|
144
|
+
)
|
|
126
145
|
}
|
|
127
146
|
|
|
128
147
|
const payload = {
|