@pathscale/wss-adapter 1.0.6 → 1.0.9
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 +3 -0
- package/dist/wssAdapter.js +31 -32
- package/package.json +1 -1
- package/types/index.ts +5 -0
- package/wssAdapter.ts +34 -46
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;
|
|
@@ -63,6 +65,7 @@ interface IStore {
|
|
|
63
65
|
services: IServices;
|
|
64
66
|
sequence: ISequence;
|
|
65
67
|
sessions: ISessions;
|
|
68
|
+
subscriptions: ISubscriptions;
|
|
66
69
|
pendingPromises: IPendingPromises;
|
|
67
70
|
onError: (error: {
|
|
68
71
|
error: number;
|
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 () {
|
|
@@ -42,6 +43,9 @@ wssAdapter.configure = function (configuration) {
|
|
|
42
43
|
},
|
|
43
44
|
disconnect: function () { return disconnectHandler(serviceName); },
|
|
44
45
|
};
|
|
46
|
+
if (serviceConfig.subscriptions) {
|
|
47
|
+
store.subscriptions[serviceName] = serviceConfig.subscriptions;
|
|
48
|
+
}
|
|
45
49
|
// construct sessions objects that contain a proxy so you can ask unknown property
|
|
46
50
|
// intended use: `wssAdapter.sessions.admin.updatePassword({ newPassword: 'hotdog6737637' })`
|
|
47
51
|
wssAdapter.sessions[serviceName] = new Proxy({}, {
|
|
@@ -82,6 +86,7 @@ var disconnectHandler = function (serviceName) {
|
|
|
82
86
|
};
|
|
83
87
|
var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
84
88
|
var _a;
|
|
89
|
+
if (params === void 0) { params = {}; }
|
|
85
90
|
var methodCode = (_a = Object.entries(serviceConfig.methods)
|
|
86
91
|
.map(function (_a) {
|
|
87
92
|
var code = _a[0], info = _a[1];
|
|
@@ -94,25 +99,10 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
94
99
|
if (!methodCode) {
|
|
95
100
|
throw new Error("method ".concat(methodName, " not available in ").concat(serviceName, " service"));
|
|
96
101
|
}
|
|
97
|
-
var purgedParams = {};
|
|
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
|
-
}
|
|
111
|
-
}
|
|
112
102
|
var payload = {
|
|
113
103
|
method: Number.parseInt(methodCode),
|
|
114
104
|
seq: store.sequence.getSeq(),
|
|
115
|
-
params:
|
|
105
|
+
params: params,
|
|
116
106
|
};
|
|
117
107
|
console.log("".concat(serviceName, "::").concat(methodName, " sends:"), payload);
|
|
118
108
|
return new Promise(function (resolve, reject) {
|
|
@@ -129,23 +119,32 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
129
119
|
};
|
|
130
120
|
var receiveHandler = function (event) {
|
|
131
121
|
var response = JSON.parse(event.data);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
122
|
+
if (response.method || response.method === 0) {
|
|
123
|
+
console.log("app::".concat(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
|
+
console.log(code);
|
|
128
|
+
var executor = store.pendingPromises[response.seq];
|
|
129
|
+
clearTimeout(store.pendingPromises[response.seq].toHandler);
|
|
130
|
+
delete store.pendingPromises[response.seq];
|
|
131
|
+
executor.resolve(payload);
|
|
132
|
+
};
|
|
133
|
+
// handle error
|
|
134
|
+
if (error) {
|
|
135
|
+
onError(response);
|
|
136
|
+
}
|
|
137
|
+
else if (done) {
|
|
138
|
+
var code = response.method - 1;
|
|
139
|
+
resolve(response, code);
|
|
140
|
+
}
|
|
145
141
|
}
|
|
146
|
-
else if (
|
|
147
|
-
|
|
148
|
-
|
|
142
|
+
else if (response.resource) {
|
|
143
|
+
console.log("app::".concat(response.resource, " got:"), response);
|
|
144
|
+
var resource = response.resource.split('@')[0];
|
|
145
|
+
//@ts-ignore
|
|
146
|
+
var executor = store.subscriptions.app[resource];
|
|
147
|
+
executor === null || executor === void 0 ? void 0 : executor(response);
|
|
149
148
|
}
|
|
150
149
|
};
|
|
151
150
|
function onError(response) {
|
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
|
|
@@ -66,6 +70,7 @@ interface IStore {
|
|
|
66
70
|
services: IServices
|
|
67
71
|
sequence: ISequence
|
|
68
72
|
sessions: ISessions
|
|
73
|
+
subscriptions: ISubscriptions
|
|
69
74
|
pendingPromises: IPendingPromises
|
|
70
75
|
onError: (error: { error: number; message: string }) => void
|
|
71
76
|
}
|
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,
|
|
@@ -49,6 +50,10 @@ wssAdapter.configure = (configuration) => {
|
|
|
49
50
|
disconnect: () => disconnectHandler(serviceName),
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
if (serviceConfig.subscriptions) {
|
|
54
|
+
store.subscriptions[serviceName] = serviceConfig.subscriptions
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
// construct sessions objects that contain a proxy so you can ask unknown property
|
|
53
58
|
// intended use: `wssAdapter.sessions.admin.updatePassword({ newPassword: 'hotdog6737637' })`
|
|
54
59
|
wssAdapter.sessions[serviceName] = new Proxy(
|
|
@@ -111,7 +116,7 @@ const sendHandler = (
|
|
|
111
116
|
serviceName: string,
|
|
112
117
|
serviceConfig: IServiceConfig,
|
|
113
118
|
methodName: string,
|
|
114
|
-
params: Record<string, unknown>
|
|
119
|
+
params: Record<string, unknown> = {}
|
|
115
120
|
) => {
|
|
116
121
|
const methodCode = Object.entries(serviceConfig.methods)
|
|
117
122
|
.map(([code, info]) => ({ code, info }))
|
|
@@ -123,38 +128,10 @@ const sendHandler = (
|
|
|
123
128
|
)
|
|
124
129
|
}
|
|
125
130
|
|
|
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
131
|
const payload = {
|
|
155
132
|
method: Number.parseInt(methodCode),
|
|
156
133
|
seq: store.sequence.getSeq(),
|
|
157
|
-
params:
|
|
134
|
+
params: params,
|
|
158
135
|
}
|
|
159
136
|
|
|
160
137
|
console.log(`${serviceName}::${methodName} sends:`, payload)
|
|
@@ -175,25 +152,36 @@ const sendHandler = (
|
|
|
175
152
|
|
|
176
153
|
const receiveHandler = (event: { data: string }) => {
|
|
177
154
|
const response = JSON.parse(event.data)
|
|
178
|
-
console.log(`app::${response.method} got:`, response)
|
|
179
155
|
|
|
180
|
-
|
|
181
|
-
|
|
156
|
+
if (response.method || response.method === 0) {
|
|
157
|
+
console.log(`app::${response.method} got:`, response)
|
|
182
158
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
159
|
+
const error = response.method === 0
|
|
160
|
+
const done = response.method.toString().endsWith('1')
|
|
161
|
+
|
|
162
|
+
const resolve = (payload: unknown, code: number) => {
|
|
163
|
+
console.log(code)
|
|
164
|
+
const executor = store.pendingPromises[response.seq]
|
|
165
|
+
clearTimeout(store.pendingPromises[response.seq].toHandler)
|
|
166
|
+
delete store.pendingPromises[response.seq]
|
|
167
|
+
executor.resolve(payload)
|
|
168
|
+
}
|
|
190
169
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
170
|
+
// handle error
|
|
171
|
+
if (error) {
|
|
172
|
+
onError(response)
|
|
173
|
+
} else if (done) {
|
|
174
|
+
const code = response.method - 1
|
|
175
|
+
resolve(response, code)
|
|
176
|
+
}
|
|
177
|
+
} else if (response.resource) {
|
|
178
|
+
console.log(`app::${response.resource} got:`, response)
|
|
179
|
+
const resource = response.resource.split('@')[0]
|
|
180
|
+
//@ts-ignore
|
|
181
|
+
const executor = store.subscriptions.app[resource] as (
|
|
182
|
+
payload: unknown
|
|
183
|
+
) => void
|
|
184
|
+
executor?.(response)
|
|
197
185
|
}
|
|
198
186
|
}
|
|
199
187
|
|