@service-broker/webclient 1.0.0
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/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +184 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Service Broker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
interface ServiceFilter {
|
|
3
|
+
name: string
|
|
4
|
+
capabilities?: string[]
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ServiceAdvert {
|
|
8
|
+
name: string
|
|
9
|
+
capabilities?: string[]
|
|
10
|
+
priority: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ServiceHandler {
|
|
14
|
+
(msg: Message): Partial<Message>|void|Promise<Partial<Message>|void>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Message {
|
|
18
|
+
header: any
|
|
19
|
+
payload: any
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare class ServiceBroker {
|
|
23
|
+
constructor(url: string, logger: {error: Console["error"], debug: Console["debug"]});
|
|
24
|
+
request<T>(service: ServiceFilter, req: unknown): Promise<T>;
|
|
25
|
+
requestTo<T>(endpointId: string, service: ServiceFilter, req: unknown): Promise<T>;
|
|
26
|
+
advertise(service: ServiceAdvert, handler: ServiceHandler): Promise<void>;
|
|
27
|
+
unadvertise(serviceName: string): Promise<void>;
|
|
28
|
+
setHandler(serviceName: string, handler: ServiceHandler): void;
|
|
29
|
+
publish(topic: string, text: string): Promise<void>;
|
|
30
|
+
subscribe(topic: string, handler: (text: string) => void): Promise<void>;
|
|
31
|
+
unsubscribe(topic: string): Promise<void>;
|
|
32
|
+
isConnected(): boolean;
|
|
33
|
+
addConnectListener(listener: () => void): void;
|
|
34
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
exports.ServiceBroker = ServiceBroker;
|
|
2
|
+
|
|
3
|
+
function ServiceBroker(url, logger) {
|
|
4
|
+
var pending = {};
|
|
5
|
+
var pendingIdGen = 0;
|
|
6
|
+
var providers = {};
|
|
7
|
+
var ws;
|
|
8
|
+
var connectListeners = [];
|
|
9
|
+
var pendingSend = [];
|
|
10
|
+
connect();
|
|
11
|
+
|
|
12
|
+
function connect() {
|
|
13
|
+
var conn = new WebSocket(url);
|
|
14
|
+
conn.onopen = onOpen.bind(null, conn);
|
|
15
|
+
conn.onerror = function() {
|
|
16
|
+
logger.error("Failed to connect to service broker, retrying in 15");
|
|
17
|
+
setTimeout(connect, 15000);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function onOpen(conn) {
|
|
22
|
+
ws = conn;
|
|
23
|
+
ws.onerror = logger.error;
|
|
24
|
+
ws.onclose = onClose;
|
|
25
|
+
ws.onmessage = onMessage;
|
|
26
|
+
for (var i=0; i<connectListeners.length; i++) connectListeners[i]();
|
|
27
|
+
for (var i=0; i<pendingSend.length; i++) send(pendingSend[i].header, pendingSend[i].payload);
|
|
28
|
+
pendingSend = [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function onClose() {
|
|
32
|
+
ws = null;
|
|
33
|
+
logger.error("Lost connection to service broker, reconnecting");
|
|
34
|
+
setTimeout(connect, 0);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function onMessage(e) {
|
|
38
|
+
var msg = messageFromString(e.data);
|
|
39
|
+
logger.debug("<<", msg.header, msg.payload);
|
|
40
|
+
if (msg.header.type == "ServiceResponse") onServiceResponse(msg);
|
|
41
|
+
else if (msg.header.type == "ServiceRequest") onServiceRequest(msg);
|
|
42
|
+
else if (msg.header.type == "SbStatusResponse") onServiceResponse(msg);
|
|
43
|
+
else if (msg.header.error) onServiceResponse(msg);
|
|
44
|
+
else logger.error("Unhandled", msg.header);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function messageFromString(text) {
|
|
48
|
+
var index = text.indexOf('\n');
|
|
49
|
+
if (index == -1) return {header: JSON.parse(text)};
|
|
50
|
+
else return {header: JSON.parse(text.substr(0,index)), payload: text.substr(index+1)};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function onServiceResponse(msg) {
|
|
54
|
+
if (pending[msg.header.id]) {
|
|
55
|
+
if (msg.header.error) pending[msg.header.id].reject(new Error(msg.header.error));
|
|
56
|
+
else pending[msg.header.id].fulfill(msg);
|
|
57
|
+
delete pending[msg.header.id];
|
|
58
|
+
}
|
|
59
|
+
else logger.error("Response received but no pending request", msg.header);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function onServiceRequest(msg) {
|
|
63
|
+
if (providers[msg.header.service.name]) {
|
|
64
|
+
Promise.resolve(providers[msg.header.service.name].handler(msg))
|
|
65
|
+
.then(function(res) {
|
|
66
|
+
if (!res) res = {};
|
|
67
|
+
if (msg.header.id) {
|
|
68
|
+
var header = {
|
|
69
|
+
to: msg.header.from,
|
|
70
|
+
id: msg.header.id,
|
|
71
|
+
type: "ServiceResponse"
|
|
72
|
+
};
|
|
73
|
+
send(Object.assign({}, res.header, header), res.payload);
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
.catch(function(err) {
|
|
77
|
+
if (msg.header.id) {
|
|
78
|
+
send({
|
|
79
|
+
to: msg.header.from,
|
|
80
|
+
id: msg.header.id,
|
|
81
|
+
type: "ServiceResponse",
|
|
82
|
+
error: err.message
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
else logger.error(err.message, msg.header);
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
else logger.error("No handler for service " + msg.header.service.name);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
this.request = function(service, req) {
|
|
94
|
+
return this.requestTo(null, service, req);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.requestTo = function(endpointId, service, req) {
|
|
98
|
+
var id = ++pendingIdGen;
|
|
99
|
+
var promise = new Promise(function(fulfill, reject) {
|
|
100
|
+
pending[id] = {fulfill: fulfill, reject: reject};
|
|
101
|
+
})
|
|
102
|
+
var header = {
|
|
103
|
+
id: id,
|
|
104
|
+
type: "ServiceRequest",
|
|
105
|
+
service: service
|
|
106
|
+
};
|
|
107
|
+
if (endpointId) header.to = endpointId;
|
|
108
|
+
send(Object.assign({}, req.header, header), req.payload);
|
|
109
|
+
return promise;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function send(header, payload) {
|
|
113
|
+
if (!ws) {
|
|
114
|
+
pendingSend.push({header: header, payload: payload});
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
logger.debug(">>", header, payload);
|
|
118
|
+
ws.send(JSON.stringify(header) + (payload ? "\n"+payload : ""));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
this.advertise = function(service, handler) {
|
|
124
|
+
if (providers[service.name]) throw new Error(service.name + " provider already exists");
|
|
125
|
+
providers[service.name] = {
|
|
126
|
+
advertisedService: service,
|
|
127
|
+
handler: handler
|
|
128
|
+
}
|
|
129
|
+
return send({
|
|
130
|
+
type: "SbAdvertiseRequest",
|
|
131
|
+
services: Object.keys(providers)
|
|
132
|
+
.map(function(x) {return providers[x].advertisedService})
|
|
133
|
+
.filter(function(x) {return x})
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this.unadvertise = function(serviceName) {
|
|
138
|
+
if (!providers[serviceName]) throw new Error(serviceName + " provider not exists");
|
|
139
|
+
delete providers[serviceName];
|
|
140
|
+
return send({
|
|
141
|
+
type: "SbAdvertiseRequest",
|
|
142
|
+
services: Object.keys(providers)
|
|
143
|
+
.map(function(x) {return providers[x].advertisedService})
|
|
144
|
+
.filter(function(x) {return x})
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.setHandler = function(serviceName, handler) {
|
|
149
|
+
if (providers[serviceName]) throw new Error("Handler already exists");
|
|
150
|
+
providers[serviceName] = {
|
|
151
|
+
handler: handler
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
this.publish = function(topic, text) {
|
|
158
|
+
return send({
|
|
159
|
+
type: "ServiceRequest",
|
|
160
|
+
service: {name: "#"+topic}
|
|
161
|
+
},
|
|
162
|
+
text);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.subscribe = function(topic, handler) {
|
|
166
|
+
return this.advertise({name: "#"+topic}, function(msg) {
|
|
167
|
+
handler(msg.payload);
|
|
168
|
+
return null;
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.unsubscribe = function(topic) {
|
|
173
|
+
return this.unadvertise("#"+topic);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
this.isConnected = function() {
|
|
177
|
+
return ws != null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.addConnectListener = function(listener) {
|
|
181
|
+
connectListeners.push(listener);
|
|
182
|
+
if (this.isConnected()) listener();
|
|
183
|
+
}
|
|
184
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@service-broker/webclient",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Browser JavaScript library for communicating with the service broker",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/service-broker/service-broker-webclient.git"
|
|
10
|
+
},
|
|
11
|
+
"author": "Hai Phan <hai.phan@gmail.com>",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/service-broker/service-broker-webclient/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/service-broker/service-broker-webclient#readme"
|
|
17
|
+
}
|