@pathscale/wss-adapter 2.0.2 → 2.0.4
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/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +57 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/wss-adapter.d.ts +4 -0
- package/dist/wss-adapter.d.ts.map +1 -0
- package/dist/wss-adapter.js +203 -0
- package/dist/wss-adapter.js.map +1 -0
- package/package.json +1 -1
- package/dist/types/index.d.ts +0 -68
- package/dist/types/index.js +0 -2
- package/dist/wssAdapter.d.ts +0 -3
- package/dist/wssAdapter.js +0 -174
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default } from './wss-adapter.js';
|
|
2
|
+
export { default as wssAdapter } from './wss-adapter.js';
|
|
3
|
+
export type { IWssAdapter, IConfiguration, IServiceConfig, IServiceAdapter, IMethodInfo, IErrors, IStore, ApiMethods, ServiceName, } from './types.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EACV,WAAW,EACX,cAAc,EACd,cAAc,EACd,eAAe,EACf,WAAW,EACX,OAAO,EACP,MAAM,EACN,UAAU,EACV,WAAW,GACZ,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface IMethodInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
parameters: string[];
|
|
4
|
+
}
|
|
5
|
+
export interface IServiceConfig {
|
|
6
|
+
remote: string;
|
|
7
|
+
methods: Record<string, IMethodInfo>;
|
|
8
|
+
subscriptions?: Record<string, (data: any) => void>;
|
|
9
|
+
onDisconnect?: (event: CloseEvent) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface IErrorCode {
|
|
12
|
+
code: number;
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
export interface IErrors {
|
|
16
|
+
language: string;
|
|
17
|
+
codes: IErrorCode[];
|
|
18
|
+
}
|
|
19
|
+
export interface IConfiguration {
|
|
20
|
+
timeout: number;
|
|
21
|
+
services: Record<string, IServiceConfig>;
|
|
22
|
+
errors: IErrors;
|
|
23
|
+
onError?: (message: string) => void;
|
|
24
|
+
}
|
|
25
|
+
export interface IStore {
|
|
26
|
+
timeout: number;
|
|
27
|
+
errors: IErrors;
|
|
28
|
+
services: Record<string, IServiceConfig>;
|
|
29
|
+
subscriptions: Record<string, Record<string, (data: any) => void>>;
|
|
30
|
+
sequence: {
|
|
31
|
+
value: number;
|
|
32
|
+
getSeq(): number;
|
|
33
|
+
decreaseSeq(): void;
|
|
34
|
+
};
|
|
35
|
+
sessions: Record<string, WebSocket>;
|
|
36
|
+
pendingPromises: Record<number, {
|
|
37
|
+
resolve: (value: any) => void;
|
|
38
|
+
reject: (reason: any) => void;
|
|
39
|
+
toHandler: number;
|
|
40
|
+
methodName: string;
|
|
41
|
+
}>;
|
|
42
|
+
onError?: (message: string) => void;
|
|
43
|
+
}
|
|
44
|
+
export interface IServiceAdapter {
|
|
45
|
+
connect: <T>(payload?: string | string[], remote?: string) => Promise<T>;
|
|
46
|
+
disconnect: () => void;
|
|
47
|
+
}
|
|
48
|
+
export type ServiceName = "app";
|
|
49
|
+
export type ApiMethods = {
|
|
50
|
+
app: Record<string, (params?: any) => Promise<any>>;
|
|
51
|
+
};
|
|
52
|
+
export interface IWssAdapter {
|
|
53
|
+
services: Record<ServiceName, IServiceAdapter>;
|
|
54
|
+
sessions: ApiMethods;
|
|
55
|
+
configure: (configuration: IConfiguration) => void;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACrC,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC,CAAC;IACpD,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;CAC5C;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,OAAO;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,UAAU,EAAE,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACzC,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACrC;AAED,MAAM,WAAW,MAAM;IACrB,OAAO,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AE,UAAU,CAAC;IACrB,SAAS,EAAE,CAAC,aAAa,EAAE,cAAc,KAAK,IAAI,CAAC;CACpD"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wss-adapter.d.ts","sourceRoot":"","sources":["../src/wss-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,WAAW,EAEZ,MAAM,YAAY,CAAC;AACpB,QAAA,MAAM,UAAU,EAAE,WAIjB,CAAC;AAsRF,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
const wssAdapter = {
|
|
2
|
+
services: {},
|
|
3
|
+
sessions: {},
|
|
4
|
+
configure() { },
|
|
5
|
+
};
|
|
6
|
+
const store = {
|
|
7
|
+
timeout: 0,
|
|
8
|
+
errors: {
|
|
9
|
+
language: "en",
|
|
10
|
+
codes: [],
|
|
11
|
+
},
|
|
12
|
+
services: {},
|
|
13
|
+
subscriptions: {},
|
|
14
|
+
sequence: {
|
|
15
|
+
value: 1,
|
|
16
|
+
getSeq() {
|
|
17
|
+
store.sequence.value += 1;
|
|
18
|
+
return store.sequence.value;
|
|
19
|
+
},
|
|
20
|
+
decreaseSeq() {
|
|
21
|
+
store.sequence.value -= 1;
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
sessions: {},
|
|
25
|
+
pendingPromises: {},
|
|
26
|
+
};
|
|
27
|
+
wssAdapter.configure = (configuration) => {
|
|
28
|
+
const { timeout, services, errors, onError } = configuration;
|
|
29
|
+
store.timeout = timeout;
|
|
30
|
+
store.errors = errors;
|
|
31
|
+
store.services = services;
|
|
32
|
+
if (onError !== undefined) {
|
|
33
|
+
store.onError = onError;
|
|
34
|
+
}
|
|
35
|
+
for (const [serviceName, serviceConfig] of Object.entries(services)) {
|
|
36
|
+
const serviceNameTyped = serviceName;
|
|
37
|
+
wssAdapter.services[serviceNameTyped] = {
|
|
38
|
+
connect: (payload, remote) => connectHandler(serviceName, serviceConfig, payload, remote),
|
|
39
|
+
disconnect: () => disconnectHandler(serviceName),
|
|
40
|
+
};
|
|
41
|
+
if (serviceConfig.subscriptions) {
|
|
42
|
+
store.subscriptions[serviceName] = serviceConfig.subscriptions;
|
|
43
|
+
}
|
|
44
|
+
wssAdapter.sessions[serviceName] = new Proxy({}, {
|
|
45
|
+
get: (_target, methodName) => (payload) => sendHandler(serviceName, serviceConfig, methodName, payload),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const connectHandler = (serviceName, serviceConfig, payload, remote) => {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
if (!payload || !Array.isArray(payload)) {
|
|
52
|
+
reject(new Error("WebSocket protocols required for authentication"));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const protocols = payload;
|
|
56
|
+
const wsConnection = new WebSocket(remote || serviceConfig.remote, protocols);
|
|
57
|
+
store.sessions[serviceName] = wsConnection;
|
|
58
|
+
wsConnection.onmessage = (event) => {
|
|
59
|
+
const response = JSON.parse(event.data);
|
|
60
|
+
if (response.type === "Immediate" &&
|
|
61
|
+
response.method === 20000 &&
|
|
62
|
+
response.params?.accessToken) {
|
|
63
|
+
const session = store.sessions[serviceName];
|
|
64
|
+
if (session) {
|
|
65
|
+
session.onmessage = receiveHandler;
|
|
66
|
+
}
|
|
67
|
+
resolve(response.params);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (response.type === "Error" || response.code) {
|
|
71
|
+
const error = store.errors.codes.find((c) => c.code === response.code);
|
|
72
|
+
const errorMsg = response.params || error?.message || response.code;
|
|
73
|
+
if (store.onError) {
|
|
74
|
+
store.onError(String(errorMsg));
|
|
75
|
+
}
|
|
76
|
+
reject(new Error(String(errorMsg)));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const session = store.sessions[serviceName];
|
|
80
|
+
if (session) {
|
|
81
|
+
session.onmessage = receiveHandler;
|
|
82
|
+
}
|
|
83
|
+
resolve(response.params);
|
|
84
|
+
};
|
|
85
|
+
wsConnection.onclose = (event) => {
|
|
86
|
+
if (!event.wasClean) {
|
|
87
|
+
reject(new Error(`WebSocket closed unexpectedly (code ${event.code || "unknown"})`));
|
|
88
|
+
}
|
|
89
|
+
serviceConfig.onDisconnect?.(event);
|
|
90
|
+
};
|
|
91
|
+
wsConnection.onerror = (event) => {
|
|
92
|
+
console.error("WebSocket error:", event);
|
|
93
|
+
reject(new Error("WebSocket connection failed"));
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
const disconnectHandler = (serviceName) => {
|
|
98
|
+
const session = store.sessions[serviceName];
|
|
99
|
+
if (session) {
|
|
100
|
+
session.close();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const sendHandler = (serviceName, serviceConfig, methodName, params = {}) => {
|
|
104
|
+
const methodEntry = Object.entries(serviceConfig.methods)
|
|
105
|
+
.map(([code, info]) => ({ code, info }))
|
|
106
|
+
.find(({ info }) => info.name === methodName);
|
|
107
|
+
if (!methodEntry) {
|
|
108
|
+
throw new Error(`method ${methodName} not available in ${serviceName} service`);
|
|
109
|
+
}
|
|
110
|
+
const methodCode = methodEntry.code;
|
|
111
|
+
const methodInfo = serviceConfig.methods[methodCode];
|
|
112
|
+
if (!methodInfo) {
|
|
113
|
+
throw new Error(`method configuration missing for ${methodName} in ${serviceName} service`);
|
|
114
|
+
}
|
|
115
|
+
const paramsArray = methodInfo.parameters.map((paramName) => params[paramName]);
|
|
116
|
+
const payload = {
|
|
117
|
+
method: Number.parseInt(methodCode),
|
|
118
|
+
seq: store.sequence.getSeq(),
|
|
119
|
+
params: paramsArray,
|
|
120
|
+
};
|
|
121
|
+
return new Promise((resolve, reject) => {
|
|
122
|
+
const session = store.sessions[serviceName];
|
|
123
|
+
if (!session) {
|
|
124
|
+
reject(new Error(`No active session for service ${serviceName}`));
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
session.send(JSON.stringify(payload));
|
|
128
|
+
store.pendingPromises[payload.seq] = {
|
|
129
|
+
resolve,
|
|
130
|
+
reject,
|
|
131
|
+
toHandler: setTimeout(() => {
|
|
132
|
+
reject(new Error(`${methodName} took too long, aborting`));
|
|
133
|
+
}, store.timeout),
|
|
134
|
+
methodName,
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
const receiveHandler = (event) => {
|
|
139
|
+
const response = JSON.parse(event.data);
|
|
140
|
+
if (response.type === "Immediate" ||
|
|
141
|
+
(response.type === "Error" && response.method) ||
|
|
142
|
+
response.method === 0) {
|
|
143
|
+
const error = response.code ||
|
|
144
|
+
response.params?.success === false ||
|
|
145
|
+
response.params?.error;
|
|
146
|
+
const resolve = (payload) => {
|
|
147
|
+
const executor = store.pendingPromises[response.seq];
|
|
148
|
+
if (executor) {
|
|
149
|
+
clearTimeout(executor.toHandler);
|
|
150
|
+
delete store.pendingPromises[response.seq];
|
|
151
|
+
executor.resolve(payload);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
if (error) {
|
|
155
|
+
onError(response);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
resolve(response);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (response.type === "Stream") {
|
|
162
|
+
console.log("Stream response:", response);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function onError(response) {
|
|
166
|
+
const error = store.errors.codes.find((c) => c.code === response.code);
|
|
167
|
+
const params = typeof response.params === "object" && response.params !== null
|
|
168
|
+
? response.params
|
|
169
|
+
: {};
|
|
170
|
+
const errorMsg = params.reason ||
|
|
171
|
+
params.error ||
|
|
172
|
+
response.params ||
|
|
173
|
+
error?.message;
|
|
174
|
+
const errorCode = response.code ?? "Error";
|
|
175
|
+
let methodName = "";
|
|
176
|
+
if (response.method) {
|
|
177
|
+
for (const serviceConfig of Object.values(store.services)) {
|
|
178
|
+
const methodKey = response.method.toString();
|
|
179
|
+
const method = serviceConfig.methods[methodKey];
|
|
180
|
+
if (method) {
|
|
181
|
+
methodName = method.name;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const fullErrorMsg = methodName
|
|
187
|
+
? `[${errorCode}]: ${methodName}: ${errorMsg}`
|
|
188
|
+
: String(errorMsg);
|
|
189
|
+
if (store.onError) {
|
|
190
|
+
store.onError(fullErrorMsg);
|
|
191
|
+
}
|
|
192
|
+
const executor = store.pendingPromises[response.seq];
|
|
193
|
+
if (executor) {
|
|
194
|
+
clearTimeout(executor.toHandler);
|
|
195
|
+
executor.reject(new Error(`${executor.methodName || ""}: ${errorMsg}`, { cause: errorCode }));
|
|
196
|
+
delete store.pendingPromises[response.seq];
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
throw new Error("Unknown request failed");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export default wssAdapter;
|
|
203
|
+
//# sourceMappingURL=wss-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wss-adapter.js","sourceRoot":"","sources":["../src/wss-adapter.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,GAAgB;IAC9B,QAAQ,EAAE,EAA0C;IACpD,QAAQ,EAAE,EAAgB;IAC1B,SAAS,KAAI,CAAC;CACf,CAAC;AAEF,MAAM,KAAK,GAAW;IACpB,OAAO,EAAE,CAAC;IACV,MAAM,EAAE;QACN,QAAQ,EAAE,IAAI;QACd,KAAK,EAAE,EAAE;KACV;IACD,QAAQ,EAAE,EAAE;IACZ,aAAa,EAAE,EAAE;IACjB,QAAQ,EAAE;QACR,KAAK,EAAE,CAAC;QACR,MAAM;YACJ,KAAK,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC,CAAC;YAC1B,OAAO,KAAK,CAAC,Q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YAC1D,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YAC7C,MAAM,MAAM,GAAG,aAAa,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YAChD,IAAI,MAAM,EAAE,CAAC;gBACX,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC;gBACzB,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,YAAY,GAAG,UAAU;QAC7B,CAAC,CAAC,IAAI,SAAS,MAAM,UAAU,KAAK,QAAQ,EAAE;QAC9C,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAErB,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;QAClB,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM,QAAQ,GAAG,KAAK,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACrD,IAAI,QAAQ,EAAE,CAAC;QACb,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QACjC,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,GAAG,QAAQ,CAAC,UAAU,IAAI,EAAE,KAAK,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;QAC9F,OAAO,KAAK,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC7C,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC;AAED,eAAe,UAAU,CAAC"}
|
package/package.json
CHANGED
package/dist/types/index.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
interface IService {
|
|
2
|
-
remote: string;
|
|
3
|
-
methods: {
|
|
4
|
-
[methodCode: string]: {
|
|
5
|
-
name: string;
|
|
6
|
-
parameters: string[];
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
declare type ISubscriptions = Record<string, unknown>;
|
|
11
|
-
interface IServiceConfig extends IService {
|
|
12
|
-
subscriptions?: ISubscriptions;
|
|
13
|
-
onDisconnect: (event: {
|
|
14
|
-
code: number;
|
|
15
|
-
reason: string;
|
|
16
|
-
wasClean: boolean;
|
|
17
|
-
}) => void | null;
|
|
18
|
-
}
|
|
19
|
-
interface IServices {
|
|
20
|
-
[serviceName: string]: IServiceConfig;
|
|
21
|
-
}
|
|
22
|
-
interface IErrors {
|
|
23
|
-
[errorCode: number]: string;
|
|
24
|
-
}
|
|
25
|
-
interface IConfiguration {
|
|
26
|
-
timeout: number;
|
|
27
|
-
services: IServices;
|
|
28
|
-
errors: IErrors;
|
|
29
|
-
}
|
|
30
|
-
interface IServiceConnect {
|
|
31
|
-
connect<T>(payload: string | string[] | undefined, remote?: string): Promise<T>;
|
|
32
|
-
disconnect: () => void;
|
|
33
|
-
}
|
|
34
|
-
interface IWssAdapter {
|
|
35
|
-
services: {
|
|
36
|
-
[serviceName: string]: IServiceConnect;
|
|
37
|
-
};
|
|
38
|
-
sessions: {
|
|
39
|
-
[serviceName: string]: Record<string, <Response, Params = {}>(payload?: Params) => Promise<Response>>;
|
|
40
|
-
};
|
|
41
|
-
configure: (configuration: IConfiguration) => void;
|
|
42
|
-
}
|
|
43
|
-
interface ISequence {
|
|
44
|
-
value: number;
|
|
45
|
-
getSeq: () => number;
|
|
46
|
-
decreaseSeq: () => void;
|
|
47
|
-
}
|
|
48
|
-
interface ISessions {
|
|
49
|
-
[serviceName: string]: WebSocket;
|
|
50
|
-
}
|
|
51
|
-
interface IPendingPromises {
|
|
52
|
-
[seq: number]: {
|
|
53
|
-
resolve: (payload: unknown) => void;
|
|
54
|
-
reject: (error: Error) => void;
|
|
55
|
-
toHandler: ReturnType<typeof setTimeout>;
|
|
56
|
-
methodName: string;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
interface IStore {
|
|
60
|
-
timeout: number;
|
|
61
|
-
errors: IErrors;
|
|
62
|
-
services: IServices;
|
|
63
|
-
sequence: ISequence;
|
|
64
|
-
sessions: ISessions;
|
|
65
|
-
subscriptions: ISubscriptions;
|
|
66
|
-
pendingPromises: IPendingPromises;
|
|
67
|
-
}
|
|
68
|
-
export { IStore, IWssAdapter, IServiceConfig, IConfiguration, IErrors, IServices, IService, };
|
package/dist/types/index.js
DELETED
package/dist/wssAdapter.d.ts
DELETED
package/dist/wssAdapter.js
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable compat/compat */
|
|
3
|
-
/* eslint-disable promise/avoid-new */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
var wssAdapter = {
|
|
7
|
-
services: {},
|
|
8
|
-
sessions: {},
|
|
9
|
-
configure: function () { },
|
|
10
|
-
};
|
|
11
|
-
var store = {
|
|
12
|
-
timeout: 0,
|
|
13
|
-
errors: {},
|
|
14
|
-
services: {},
|
|
15
|
-
subscriptions: {},
|
|
16
|
-
sequence: {
|
|
17
|
-
value: 1,
|
|
18
|
-
getSeq: function () {
|
|
19
|
-
store.sequence.value += 1;
|
|
20
|
-
return store.sequence.value;
|
|
21
|
-
},
|
|
22
|
-
decreaseSeq: function () {
|
|
23
|
-
store.sequence.value -= 1;
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
sessions: {},
|
|
27
|
-
pendingPromises: {},
|
|
28
|
-
};
|
|
29
|
-
wssAdapter.configure = function (configuration) {
|
|
30
|
-
var timeout = configuration.timeout, services = configuration.services, errors = configuration.errors;
|
|
31
|
-
// save some stuff for later retrieval
|
|
32
|
-
store.timeout = timeout;
|
|
33
|
-
store.errors = errors;
|
|
34
|
-
store.services = services;
|
|
35
|
-
var _loop_1 = function (serviceName, serviceConfig) {
|
|
36
|
-
// construct services objects with two simple functions
|
|
37
|
-
// intended use: `wssAdapter.services.admin.connect([1, 2, 3])` or `wssAdapter.services.auth.connect([1, 2, 3])`
|
|
38
|
-
wssAdapter.services[serviceName] = {
|
|
39
|
-
connect: function (payload, remote) {
|
|
40
|
-
return connectHandler(serviceName, serviceConfig, payload, remote);
|
|
41
|
-
},
|
|
42
|
-
disconnect: function () { return disconnectHandler(serviceName); },
|
|
43
|
-
};
|
|
44
|
-
if (serviceConfig.subscriptions) {
|
|
45
|
-
store.subscriptions[serviceName] = serviceConfig.subscriptions;
|
|
46
|
-
}
|
|
47
|
-
// construct sessions objects that contain a proxy so you can ask unknown property
|
|
48
|
-
// intended use: `wssAdapter.sessions.admin.updatePassword({ newPassword: 'hotdog6737637' })`
|
|
49
|
-
wssAdapter.sessions[serviceName] = new Proxy({}, {
|
|
50
|
-
get: function (target, methodName) { return function (payload) {
|
|
51
|
-
return sendHandler(serviceName, serviceConfig, methodName, payload);
|
|
52
|
-
}; },
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
for (var _i = 0, _a = Object.entries(services); _i < _a.length; _i++) {
|
|
56
|
-
var _b = _a[_i], serviceName = _b[0], serviceConfig = _b[1];
|
|
57
|
-
_loop_1(serviceName, serviceConfig);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
var connectHandler = function (serviceName, serviceConfig, payload, remote) {
|
|
61
|
-
return new Promise(function (resolve, reject) {
|
|
62
|
-
store.sessions[serviceName] = new WebSocket(remote || serviceConfig.remote, payload);
|
|
63
|
-
store.sessions[serviceName].onmessage = function (event) {
|
|
64
|
-
var _a;
|
|
65
|
-
var response = JSON.parse(event.data);
|
|
66
|
-
console.log(response);
|
|
67
|
-
if (response.params.error) {
|
|
68
|
-
reject(new Error((_a = store.errors[response.params.error]) !== null && _a !== void 0 ? _a : response.params.error));
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
store.sessions[serviceName].onmessage = receiveHandler;
|
|
72
|
-
resolve(response.params);
|
|
73
|
-
};
|
|
74
|
-
store.sessions[serviceName].onclose = function (event) {
|
|
75
|
-
var _a;
|
|
76
|
-
(_a = serviceConfig.onDisconnect) === null || _a === void 0 ? void 0 : _a.call(serviceConfig, event);
|
|
77
|
-
reject(new Error("code: ".concat(event.code, ", reason: ").concat(event.reason, ", wasClean: ").concat(event.wasClean)));
|
|
78
|
-
};
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
var disconnectHandler = function (serviceName) {
|
|
82
|
-
var _a;
|
|
83
|
-
(_a = store.sessions[serviceName]) === null || _a === void 0 ? void 0 : _a.close();
|
|
84
|
-
};
|
|
85
|
-
var sendHandler = function (serviceName, serviceConfig, methodName, params) {
|
|
86
|
-
var _a;
|
|
87
|
-
if (params === void 0) { params = {}; }
|
|
88
|
-
var methodCode = (_a = Object.entries(serviceConfig.methods)
|
|
89
|
-
.map(function (_a) {
|
|
90
|
-
var code = _a[0], info = _a[1];
|
|
91
|
-
return ({ code: code, info: info });
|
|
92
|
-
})
|
|
93
|
-
.find(function (_a) {
|
|
94
|
-
var info = _a.info;
|
|
95
|
-
return info.name === methodName;
|
|
96
|
-
})) === null || _a === void 0 ? void 0 : _a.code;
|
|
97
|
-
if (!methodCode) {
|
|
98
|
-
throw new Error("method ".concat(methodName, " not available in ").concat(serviceName, " service"));
|
|
99
|
-
}
|
|
100
|
-
var payload = {
|
|
101
|
-
method: Number.parseInt(methodCode),
|
|
102
|
-
seq: store.sequence.getSeq(),
|
|
103
|
-
params: params,
|
|
104
|
-
};
|
|
105
|
-
console.log("".concat(serviceName, "::").concat(methodName, " sends:"), payload);
|
|
106
|
-
return new Promise(function (resolve, reject) {
|
|
107
|
-
store.sessions[serviceName].send(JSON.stringify(payload));
|
|
108
|
-
// save executor so that when response for this request comes the promise can be resolved
|
|
109
|
-
store.pendingPromises[payload.seq] = {
|
|
110
|
-
resolve: resolve,
|
|
111
|
-
reject: reject,
|
|
112
|
-
toHandler: setTimeout(function () {
|
|
113
|
-
reject(new Error(methodName + ' took to long, aborting'));
|
|
114
|
-
}, store.timeout),
|
|
115
|
-
methodName: methodName,
|
|
116
|
-
};
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
var receiveHandler = function (event) {
|
|
120
|
-
var _a;
|
|
121
|
-
var response = JSON.parse(event.data);
|
|
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 || response.params.error;
|
|
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
|
-
}
|
|
140
|
-
}
|
|
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);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
function onError(response) {
|
|
149
|
-
var _a, _b;
|
|
150
|
-
var errorCode = response.params.error;
|
|
151
|
-
var errorMsg = (_a = store.errors[errorCode]) !== null && _a !== void 0 ? _a : errorCode;
|
|
152
|
-
if ([45349638, 45349637].includes(errorCode)) {
|
|
153
|
-
store.sequence.decreaseSeq();
|
|
154
|
-
}
|
|
155
|
-
// if there was only one executor saved in store.pendingPromises, then it was that request that failed
|
|
156
|
-
if (Object.keys(store.pendingPromises).length === 1) {
|
|
157
|
-
var onlyKey = Number.parseInt(Object.keys(store.pendingPromises)[0]);
|
|
158
|
-
clearTimeout(store.pendingPromises[onlyKey].toHandler);
|
|
159
|
-
store.pendingPromises[onlyKey].reject(new Error(errorMsg));
|
|
160
|
-
console.error(store.pendingPromises[onlyKey].methodName + ' failed');
|
|
161
|
-
delete store.pendingPromises[onlyKey];
|
|
162
|
-
}
|
|
163
|
-
else if (response.seq > 1 && store.pendingPromises[response.seq]) {
|
|
164
|
-
var executor = store.pendingPromises[response.seq];
|
|
165
|
-
clearTimeout(executor.toHandler);
|
|
166
|
-
executor.reject(new Error("".concat((_b = executor.methodName) !== null && _b !== void 0 ? _b : '', ": ").concat(errorCode || JSON.stringify(response.params))));
|
|
167
|
-
delete store.pendingPromises[response.seq];
|
|
168
|
-
}
|
|
169
|
-
// if there were more than one, there is no way of knowing who failed
|
|
170
|
-
else {
|
|
171
|
-
throw new Error('Unkown request failed');
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
exports.default = wssAdapter;
|