@pathscale/wss-adapter 1.0.8 → 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 +29 -39
- package/package.json +1 -1
- package/types/index.ts +5 -0
- package/wssAdapter.ts +32 -44
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({}, {
|
|
@@ -95,29 +99,6 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
95
99
|
if (!methodCode) {
|
|
96
100
|
throw new Error("method ".concat(methodName, " not available in ").concat(serviceName, " service"));
|
|
97
101
|
}
|
|
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
102
|
var payload = {
|
|
122
103
|
method: Number.parseInt(methodCode),
|
|
123
104
|
seq: store.sequence.getSeq(),
|
|
@@ -138,23 +119,32 @@ var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
|
138
119
|
};
|
|
139
120
|
var receiveHandler = function (event) {
|
|
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(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
|
+
}
|
|
154
141
|
}
|
|
155
|
-
else if (
|
|
156
|
-
|
|
157
|
-
|
|
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);
|
|
158
148
|
}
|
|
159
149
|
};
|
|
160
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(
|
|
@@ -123,34 +128,6 @@ 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(),
|
|
@@ -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
|
-
const executor = store.pendingPromises[response.seq]
|
|
186
|
-
clearTimeout(store.pendingPromises[response.seq].toHandler)
|
|
187
|
-
delete store.pendingPromises[response.seq]
|
|
188
|
-
executor.resolve(payload)
|
|
189
|
-
}
|
|
159
|
+
const error = response.method === 0
|
|
160
|
+
const done = response.method.toString().endsWith('1')
|
|
190
161
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
+
}
|
|
169
|
+
|
|
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
|
|