@pathscale/wss-adapter 1.0.8 → 1.0.11
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 +4 -8
- package/dist/wssAdapter.js +32 -48
- package/package.json +1 -1
- package/types/index.ts +6 -2
- package/wssAdapter.ts +38 -53
package/dist/types/index.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ interface IService {
|
|
|
7
7
|
};
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
+
declare type ISubscriptions = Record<string, unknown>;
|
|
10
11
|
interface IServiceConfig extends IService {
|
|
12
|
+
subscriptions?: ISubscriptions;
|
|
11
13
|
onDisconnect: (event: {
|
|
12
14
|
code: number;
|
|
13
15
|
reason: string;
|
|
@@ -24,10 +26,6 @@ interface IConfiguration {
|
|
|
24
26
|
timeout: number;
|
|
25
27
|
services: IServices;
|
|
26
28
|
errors: IErrors;
|
|
27
|
-
onError: (error: {
|
|
28
|
-
error: number;
|
|
29
|
-
message: string;
|
|
30
|
-
}) => void;
|
|
31
29
|
}
|
|
32
30
|
interface IServiceConnect {
|
|
33
31
|
connect<T>(payload: string | string[] | undefined, remote?: string): Promise<T>;
|
|
@@ -55,6 +53,7 @@ interface IPendingPromises {
|
|
|
55
53
|
resolve: (payload: unknown) => void;
|
|
56
54
|
reject: (error: Error) => void;
|
|
57
55
|
toHandler: ReturnType<typeof setTimeout>;
|
|
56
|
+
methodName: string;
|
|
58
57
|
};
|
|
59
58
|
}
|
|
60
59
|
interface IStore {
|
|
@@ -63,10 +62,7 @@ interface IStore {
|
|
|
63
62
|
services: IServices;
|
|
64
63
|
sequence: ISequence;
|
|
65
64
|
sessions: ISessions;
|
|
65
|
+
subscriptions: ISubscriptions;
|
|
66
66
|
pendingPromises: IPendingPromises;
|
|
67
|
-
onError: (error: {
|
|
68
|
-
error: number;
|
|
69
|
-
message: string;
|
|
70
|
-
}) => void;
|
|
71
67
|
}
|
|
72
68
|
export { IStore, IWssAdapter, IServiceConfig, IConfiguration, IErrors, IServices, IService, };
|
package/dist/wssAdapter.js
CHANGED
|
@@ -12,6 +12,7 @@ var store = {
|
|
|
12
12
|
timeout: 0,
|
|
13
13
|
errors: {},
|
|
14
14
|
services: {},
|
|
15
|
+
subscriptions: {},
|
|
15
16
|
sequence: {
|
|
16
17
|
value: 1,
|
|
17
18
|
getSeq: function () {
|
|
@@ -24,15 +25,13 @@ var store = {
|
|
|
24
25
|
},
|
|
25
26
|
sessions: {},
|
|
26
27
|
pendingPromises: {},
|
|
27
|
-
onError: function () { },
|
|
28
28
|
};
|
|
29
29
|
wssAdapter.configure = function (configuration) {
|
|
30
|
-
var timeout = configuration.timeout, services = configuration.services, errors = configuration.errors
|
|
30
|
+
var timeout = configuration.timeout, services = configuration.services, errors = configuration.errors;
|
|
31
31
|
// save some stuff for later retrieval
|
|
32
32
|
store.timeout = timeout;
|
|
33
33
|
store.errors = errors;
|
|
34
34
|
store.services = services;
|
|
35
|
-
store.onError = onError;
|
|
36
35
|
var _loop_1 = function (serviceName, serviceConfig) {
|
|
37
36
|
// construct services objects with two simple functions
|
|
38
37
|
// intended use: `wssAdapter.services.admin.connect([1, 2, 3])` or `wssAdapter.services.auth.connect([1, 2, 3])`
|
|
@@ -42,6 +41,9 @@ wssAdapter.configure = function (configuration) {
|
|
|
42
41
|
},
|
|
43
42
|
disconnect: function () { return disconnectHandler(serviceName); },
|
|
44
43
|
};
|
|
44
|
+
if (serviceConfig.subscriptions) {
|
|
45
|
+
store.subscriptions[serviceName] = serviceConfig.subscriptions;
|
|
46
|
+
}
|
|
45
47
|
// construct sessions objects that contain a proxy so you can ask unknown property
|
|
46
48
|
// intended use: `wssAdapter.sessions.admin.updatePassword({ newPassword: 'hotdog6737637' })`
|
|
47
49
|
wssAdapter.sessions[serviceName] = new Proxy({}, {
|
|
@@ -95,29 +97,6 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
95
97
|
if (!methodCode) {
|
|
96
98
|
throw new Error("method ".concat(methodName, " not available in ").concat(serviceName, " service"));
|
|
97
99
|
}
|
|
98
|
-
// const purgedParams: Record<string, unknown> = {}
|
|
99
|
-
// if (params) {
|
|
100
|
-
// if (
|
|
101
|
-
// !Object.keys(params).every((param) =>
|
|
102
|
-
// serviceConfig.methods[methodCode].parameters.includes(param)
|
|
103
|
-
// )
|
|
104
|
-
// ) {
|
|
105
|
-
// throw new Error(
|
|
106
|
-
// `method ${methodCode} is being called with missing parameters`
|
|
107
|
-
// )
|
|
108
|
-
// }
|
|
109
|
-
// serviceConfig.methods[methodCode].parameters.forEach((k) => {
|
|
110
|
-
// purgedParams[k] = params[k]
|
|
111
|
-
// })
|
|
112
|
-
// const difference = Object.keys(params).filter(
|
|
113
|
-
// (x) => !serviceConfig.methods[methodCode].parameters.includes(x)
|
|
114
|
-
// )
|
|
115
|
-
// if (difference.length) {
|
|
116
|
-
// throw new Error(
|
|
117
|
-
// `method ${methodCode} is being called with unknow parameters, ${difference}`
|
|
118
|
-
// )
|
|
119
|
-
// }
|
|
120
|
-
// }
|
|
121
100
|
var payload = {
|
|
122
101
|
method: Number.parseInt(methodCode),
|
|
123
102
|
seq: store.sequence.getSeq(),
|
|
@@ -131,44 +110,49 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
131
110
|
resolve: resolve,
|
|
132
111
|
reject: reject,
|
|
133
112
|
toHandler: setTimeout(function () {
|
|
134
|
-
reject(new Error(
|
|
113
|
+
reject(new Error(methodName + ' took to long, aborting'));
|
|
135
114
|
}, store.timeout),
|
|
115
|
+
methodName: methodName,
|
|
136
116
|
};
|
|
137
117
|
});
|
|
138
118
|
};
|
|
139
119
|
var receiveHandler = function (event) {
|
|
120
|
+
var _a;
|
|
140
121
|
var response = JSON.parse(event.data);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
122
|
+
if (response.method || response.method === 0) {
|
|
123
|
+
console.log("app::".concat(((_a = store.pendingPromises[response.seq]) === null || _a === void 0 ? void 0 : _a.methodName) || response.method, " got:"), response);
|
|
124
|
+
var error = response.method === 0;
|
|
125
|
+
var done = response.method.toString().endsWith('1');
|
|
126
|
+
var resolve = function (payload, code) {
|
|
127
|
+
var executor = store.pendingPromises[response.seq];
|
|
128
|
+
clearTimeout(store.pendingPromises[response.seq].toHandler);
|
|
129
|
+
delete store.pendingPromises[response.seq];
|
|
130
|
+
executor.resolve(payload);
|
|
131
|
+
};
|
|
132
|
+
// handle error
|
|
133
|
+
if (error) {
|
|
134
|
+
onError(response);
|
|
135
|
+
}
|
|
136
|
+
else if (done) {
|
|
137
|
+
var code = response.method - 1;
|
|
138
|
+
resolve(response, code);
|
|
139
|
+
}
|
|
154
140
|
}
|
|
155
|
-
else if (
|
|
156
|
-
var
|
|
157
|
-
|
|
141
|
+
else if (response.resource) {
|
|
142
|
+
var resource = response.resource.split('@')[0];
|
|
143
|
+
//@ts-ignore
|
|
144
|
+
var executor = store.subscriptions.app[resource];
|
|
145
|
+
executor === null || executor === void 0 ? void 0 : executor(response);
|
|
158
146
|
}
|
|
159
147
|
};
|
|
160
148
|
function onError(response) {
|
|
161
|
-
var _a
|
|
149
|
+
var _a;
|
|
162
150
|
var errorCode = response.params.error;
|
|
163
151
|
var errorMsg = (_a = store.errors[errorCode]) !== null && _a !== void 0 ? _a : errorCode;
|
|
164
152
|
if ([45349638, 45349637].includes(errorCode)) {
|
|
165
153
|
store.sequence.decreaseSeq();
|
|
166
154
|
console.log('seq has been decreased because of error');
|
|
167
155
|
}
|
|
168
|
-
(_b = store.onError) === null || _b === void 0 ? void 0 : _b.call(store, {
|
|
169
|
-
error: errorCode,
|
|
170
|
-
message: errorMsg,
|
|
171
|
-
});
|
|
172
156
|
// if there was only one executor saved in store.pendingPromises, then it was that request that failed
|
|
173
157
|
if (Object.keys(store.pendingPromises).length === 1) {
|
|
174
158
|
var onlyKey = Number.parseInt(Object.keys(store.pendingPromises)[0]);
|
package/package.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -7,7 +7,11 @@ interface IService {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
+
|
|
11
|
+
type ISubscriptions = Record<string, unknown>
|
|
12
|
+
|
|
10
13
|
interface IServiceConfig extends IService {
|
|
14
|
+
subscriptions?: ISubscriptions
|
|
11
15
|
onDisconnect: (event: {
|
|
12
16
|
code: number
|
|
13
17
|
reason: string
|
|
@@ -24,7 +28,6 @@ interface IConfiguration {
|
|
|
24
28
|
timeout: number
|
|
25
29
|
services: IServices
|
|
26
30
|
errors: IErrors
|
|
27
|
-
onError: (error: { error: number; message: string }) => void
|
|
28
31
|
}
|
|
29
32
|
interface IServiceConnect {
|
|
30
33
|
connect<T>(
|
|
@@ -58,6 +61,7 @@ interface IPendingPromises {
|
|
|
58
61
|
resolve: (payload: unknown) => void
|
|
59
62
|
reject: (error: Error) => void
|
|
60
63
|
toHandler: ReturnType<typeof setTimeout>
|
|
64
|
+
methodName: string
|
|
61
65
|
}
|
|
62
66
|
}
|
|
63
67
|
interface IStore {
|
|
@@ -66,8 +70,8 @@ interface IStore {
|
|
|
66
70
|
services: IServices
|
|
67
71
|
sequence: ISequence
|
|
68
72
|
sessions: ISessions
|
|
73
|
+
subscriptions: ISubscriptions
|
|
69
74
|
pendingPromises: IPendingPromises
|
|
70
|
-
onError: (error: { error: number; message: string }) => void
|
|
71
75
|
}
|
|
72
76
|
export {
|
|
73
77
|
IStore,
|
package/wssAdapter.ts
CHANGED
|
@@ -14,6 +14,7 @@ const store: IStore = {
|
|
|
14
14
|
timeout: 0,
|
|
15
15
|
errors: {},
|
|
16
16
|
services: {},
|
|
17
|
+
subscriptions: {},
|
|
17
18
|
|
|
18
19
|
sequence: {
|
|
19
20
|
value: 1,
|
|
@@ -28,17 +29,15 @@ const store: IStore = {
|
|
|
28
29
|
|
|
29
30
|
sessions: {},
|
|
30
31
|
pendingPromises: {},
|
|
31
|
-
onError() {},
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
wssAdapter.configure = (configuration) => {
|
|
35
|
-
const { timeout, services, errors
|
|
35
|
+
const { timeout, services, errors } = configuration
|
|
36
36
|
|
|
37
37
|
// save some stuff for later retrieval
|
|
38
38
|
store.timeout = timeout
|
|
39
39
|
store.errors = errors
|
|
40
40
|
store.services = services
|
|
41
|
-
store.onError = onError
|
|
42
41
|
|
|
43
42
|
for (const [serviceName, serviceConfig] of Object.entries(services)) {
|
|
44
43
|
// construct services objects with two simple functions
|
|
@@ -49,6 +48,10 @@ wssAdapter.configure = (configuration) => {
|
|
|
49
48
|
disconnect: () => disconnectHandler(serviceName),
|
|
50
49
|
}
|
|
51
50
|
|
|
51
|
+
if (serviceConfig.subscriptions) {
|
|
52
|
+
store.subscriptions[serviceName] = serviceConfig.subscriptions
|
|
53
|
+
}
|
|
54
|
+
|
|
52
55
|
// construct sessions objects that contain a proxy so you can ask unknown property
|
|
53
56
|
// intended use: `wssAdapter.sessions.admin.updatePassword({ newPassword: 'hotdog6737637' })`
|
|
54
57
|
wssAdapter.sessions[serviceName] = new Proxy(
|
|
@@ -123,34 +126,6 @@ const sendHandler = (
|
|
|
123
126
|
)
|
|
124
127
|
}
|
|
125
128
|
|
|
126
|
-
// const purgedParams: Record<string, unknown> = {}
|
|
127
|
-
|
|
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
|
-
// })
|
|
142
|
-
|
|
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
|
-
// }
|
|
152
|
-
// }
|
|
153
|
-
|
|
154
129
|
const payload = {
|
|
155
130
|
method: Number.parseInt(methodCode),
|
|
156
131
|
seq: store.sequence.getSeq(),
|
|
@@ -167,33 +142,48 @@ const sendHandler = (
|
|
|
167
142
|
resolve,
|
|
168
143
|
reject,
|
|
169
144
|
toHandler: setTimeout(() => {
|
|
170
|
-
reject(new Error(
|
|
145
|
+
reject(new Error(methodName + ' took to long, aborting'))
|
|
171
146
|
}, store.timeout),
|
|
147
|
+
methodName,
|
|
172
148
|
}
|
|
173
149
|
})
|
|
174
150
|
}
|
|
175
151
|
|
|
176
152
|
const receiveHandler = (event: { data: string }) => {
|
|
177
153
|
const response = JSON.parse(event.data)
|
|
178
|
-
console.log(`app::${response.method} got:`, response)
|
|
179
154
|
|
|
180
|
-
|
|
181
|
-
|
|
155
|
+
if (response.method || response.method === 0) {
|
|
156
|
+
console.log(
|
|
157
|
+
`app::${
|
|
158
|
+
store.pendingPromises[response.seq]?.methodName || response.method
|
|
159
|
+
} got:`,
|
|
160
|
+
response
|
|
161
|
+
)
|
|
182
162
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const executor = store.pendingPromises[response.seq]
|
|
186
|
-
clearTimeout(store.pendingPromises[response.seq].toHandler)
|
|
187
|
-
delete store.pendingPromises[response.seq]
|
|
188
|
-
executor.resolve(payload)
|
|
189
|
-
}
|
|
163
|
+
const error = response.method === 0
|
|
164
|
+
const done = response.method.toString().endsWith('1')
|
|
190
165
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
166
|
+
const resolve = (payload: unknown, code: number) => {
|
|
167
|
+
const executor = store.pendingPromises[response.seq]
|
|
168
|
+
clearTimeout(store.pendingPromises[response.seq].toHandler)
|
|
169
|
+
delete store.pendingPromises[response.seq]
|
|
170
|
+
executor.resolve(payload)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// handle error
|
|
174
|
+
if (error) {
|
|
175
|
+
onError(response)
|
|
176
|
+
} else if (done) {
|
|
177
|
+
const code = response.method - 1
|
|
178
|
+
resolve(response, code)
|
|
179
|
+
}
|
|
180
|
+
} else if (response.resource) {
|
|
181
|
+
const resource = response.resource.split('@')[0]
|
|
182
|
+
//@ts-ignore
|
|
183
|
+
const executor = store.subscriptions.app[resource] as (
|
|
184
|
+
payload: unknown
|
|
185
|
+
) => void
|
|
186
|
+
executor?.(response)
|
|
197
187
|
}
|
|
198
188
|
}
|
|
199
189
|
|
|
@@ -217,11 +207,6 @@ function onError(response: IResponse) {
|
|
|
217
207
|
console.log('seq has been decreased because of error')
|
|
218
208
|
}
|
|
219
209
|
|
|
220
|
-
store.onError?.({
|
|
221
|
-
error: errorCode,
|
|
222
|
-
message: errorMsg,
|
|
223
|
-
})
|
|
224
|
-
|
|
225
210
|
// if there was only one executor saved in store.pendingPromises, then it was that request that failed
|
|
226
211
|
if (Object.keys(store.pendingPromises).length === 1) {
|
|
227
212
|
const onlyKey = Number.parseInt(Object.keys(store.pendingPromises)[0])
|