@service-broker/webclient 2.0.1 → 2.1.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/dist/index.d.ts CHANGED
@@ -1,13 +1,19 @@
1
- interface ServiceFilter {
1
+ export interface ServiceSelector {
2
2
  name: string;
3
3
  capabilities?: string[];
4
4
  }
5
5
  export interface ServiceHandler {
6
- (request: Message): Message | void | Promise<Message | void>;
6
+ (request: MessageWithHeader): Message | void | Promise<Message | void>;
7
7
  }
8
- interface Message {
9
- header?: Record<string, unknown>;
10
- payload?: string;
8
+ type MessageHeader = Record<string, unknown>;
9
+ type MessagePayload = string;
10
+ export interface MessageWithHeader {
11
+ header: MessageHeader;
12
+ payload?: MessagePayload;
13
+ }
14
+ export interface Message {
15
+ header?: MessageHeader;
16
+ payload?: MessagePayload;
11
17
  }
12
18
  export declare class ServiceBroker {
13
19
  private readonly url;
@@ -25,14 +31,15 @@ export declare class ServiceBroker {
25
31
  private onServiceResponse;
26
32
  private onServiceRequest;
27
33
  private send;
28
- request(service: ServiceFilter, req: Message): Promise<Message>;
29
- requestTo(endpointId: string | null, service: ServiceFilter, req: Message): Promise<Message>;
30
- advertise(service: ServiceFilter, handler: ServiceHandler): void;
34
+ request(service: ServiceSelector, req: Message): Promise<Message>;
35
+ requestTo(endpointId: string | null, service: ServiceSelector, req: Message): Promise<Message>;
36
+ advertise(service: ServiceSelector, handler: ServiceHandler): void;
31
37
  unadvertise(serviceName: string): void;
32
38
  setServiceHandler(serviceName: string, handler: ServiceHandler): void;
33
39
  publish(topic: string, text: string): void;
34
40
  subscribe(topic: string, handler: (text: string) => void): void;
35
41
  unsubscribe(topic: string): void;
42
+ status(): Promise<any>;
36
43
  isConnected(): boolean;
37
44
  addConnectListener(listener: Function): void;
38
45
  }
package/dist/index.js CHANGED
@@ -84,7 +84,12 @@ export class ServiceBroker {
84
84
  Promise.resolve(provider.handler(msg))
85
85
  .then(res => {
86
86
  if (msg.header.id) {
87
- this.send(Object.assign(Object.assign({}, res === null || res === void 0 ? void 0 : res.header), { to: msg.header.from, id: msg.header.id, type: "ServiceResponse" }), res === null || res === void 0 ? void 0 : res.payload);
87
+ this.send({
88
+ ...res?.header,
89
+ to: msg.header.from,
90
+ id: msg.header.id,
91
+ type: "ServiceResponse"
92
+ }, res?.payload);
88
93
  }
89
94
  })
90
95
  .catch(err => {
@@ -133,7 +138,7 @@ export class ServiceBroker {
133
138
  };
134
139
  if (endpointId)
135
140
  header.to = endpointId;
136
- this.send(Object.assign(Object.assign({}, req.header), header), req.payload);
141
+ this.send({ ...req.header, ...header }, req.payload);
137
142
  return promise;
138
143
  }
139
144
  advertise(service, handler) {
@@ -174,6 +179,12 @@ export class ServiceBroker {
174
179
  unsubscribe(topic) {
175
180
  return this.unadvertise("#" + topic);
176
181
  }
182
+ async status() {
183
+ const id = ++this.pendingIdGen;
184
+ this.send({ id, type: "SbStatusRequest" });
185
+ const res = await new Promise((fulfill, reject) => this.pendingResponses.set(id, { fulfill, reject }));
186
+ return JSON.parse(res.payload);
187
+ }
177
188
  isConnected() {
178
189
  return this.ws != null;
179
190
  }
package/package.json CHANGED
@@ -1,19 +1,22 @@
1
1
  {
2
2
  "name": "@service-broker/webclient",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Browser ESM client library for communicating with the service broker",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
7
10
  "repository": {
8
11
  "type": "git",
9
- "url": "git+https://github.com/service-broker/service-broker-webclient.git"
12
+ "url": "git+https://github.com/service-broker/webclient.git"
10
13
  },
11
14
  "author": "Hai Phan <hai.phan@gmail.com>",
12
15
  "license": "MIT",
13
16
  "bugs": {
14
- "url": "https://github.com/service-broker/service-broker-webclient/issues"
17
+ "url": "https://github.com/service-broker/webclient/issues"
15
18
  },
16
- "homepage": "https://github.com/service-broker/service-broker-webclient#readme",
19
+ "homepage": "https://github.com/service-broker/webclient#readme",
17
20
  "devDependencies": {
18
21
  "typescript": "^5.8.3"
19
22
  }
package/src/index.ts CHANGED
@@ -1,21 +1,24 @@
1
1
 
2
- interface ServiceFilter {
2
+ export interface ServiceSelector {
3
3
  name: string
4
4
  capabilities?: string[]
5
5
  }
6
6
 
7
7
  export interface ServiceHandler {
8
- (request: Message): Message | void | Promise<Message | void>
8
+ (request: MessageWithHeader): Message | void | Promise<Message | void>
9
9
  }
10
10
 
11
- interface MessageWithHeader {
12
- header: Record<string, unknown>
13
- payload?: string
11
+ type MessageHeader = Record<string, unknown>
12
+ type MessagePayload = string
13
+
14
+ export interface MessageWithHeader {
15
+ header: MessageHeader
16
+ payload?: MessagePayload
14
17
  }
15
18
 
16
- interface Message {
17
- header?: Record<string, unknown>
18
- payload?: string
19
+ export interface Message {
20
+ header?: MessageHeader
21
+ payload?: MessagePayload
19
22
  }
20
23
 
21
24
  interface PendingResponse {
@@ -41,12 +44,12 @@ function messageFromString(text: string): MessageWithHeader {
41
44
  export class ServiceBroker {
42
45
 
43
46
  private readonly providers = new Map<string, {
44
- advertisedService?: ServiceFilter
47
+ advertisedService?: ServiceSelector
45
48
  handler: ServiceHandler
46
49
  }>()
47
50
  private ws: WebSocket | null = null
48
51
  private readonly connectListeners: Function[] = []
49
- private pendingSend: Message[] = []
52
+ private pendingSend: MessageWithHeader[] = []
50
53
  private readonly pendingResponses = new Map<number, PendingResponse>()
51
54
  private pendingIdGen = 0
52
55
 
@@ -106,7 +109,7 @@ export class ServiceBroker {
106
109
  }
107
110
 
108
111
  private onServiceRequest(msg: MessageWithHeader) {
109
- const service = msg.header.service as ServiceFilter
112
+ const service = msg.header.service as ServiceSelector
110
113
  const provider = this.providers.get(service.name)
111
114
  if (provider) {
112
115
  Promise.resolve(provider.handler(msg))
@@ -137,7 +140,7 @@ export class ServiceBroker {
137
140
  }
138
141
  }
139
142
 
140
- private send(header: Message['header'], payload?: Message['payload']) {
143
+ private send(header: MessageHeader, payload?: MessagePayload) {
141
144
  if (!this.ws) {
142
145
  this.pendingSend.push({ header, payload })
143
146
  return;
@@ -151,16 +154,16 @@ export class ServiceBroker {
151
154
  }
152
155
 
153
156
 
154
- request(service: ServiceFilter, req: Message) {
157
+ request(service: ServiceSelector, req: Message) {
155
158
  return this.requestTo(null, service, req);
156
159
  }
157
160
 
158
- requestTo(endpointId: string | null, service: ServiceFilter, req: Message) {
161
+ requestTo(endpointId: string | null, service: ServiceSelector, req: Message) {
159
162
  const id = ++this.pendingIdGen
160
163
  const promise = new Promise<Message>((fulfill, reject) => {
161
164
  this.pendingResponses.set(id, { fulfill, reject })
162
165
  })
163
- const header: Message['header'] = {
166
+ const header: MessageHeader = {
164
167
  id: id,
165
168
  type: "ServiceRequest",
166
169
  service
@@ -170,7 +173,7 @@ export class ServiceBroker {
170
173
  return promise;
171
174
  }
172
175
 
173
- advertise(service: ServiceFilter, handler: ServiceHandler) {
176
+ advertise(service: ServiceSelector, handler: ServiceHandler) {
174
177
  if (this.providers.has(service.name)) {
175
178
  throw new Error(service.name + " provider already exists")
176
179
  }
@@ -214,6 +217,15 @@ export class ServiceBroker {
214
217
  return this.unadvertise("#" + topic)
215
218
  }
216
219
 
220
+ async status() {
221
+ const id = ++this.pendingIdGen
222
+ this.send({ id, type: "SbStatusRequest" })
223
+ const res = await new Promise<Message>((fulfill, reject) =>
224
+ this.pendingResponses.set(id, { fulfill, reject })
225
+ )
226
+ return JSON.parse(res.payload!)
227
+ }
228
+
217
229
  isConnected() {
218
230
  return this.ws != null
219
231
  }
package/tsconfig.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "module": "esnext",
4
- "target": "es2017",
5
- "strict": true,
4
+ "target": "es2020",
6
5
  "moduleResolution": "bundler",
6
+ "strict": true,
7
7
  "declaration": true,
8
8
  "newLine": "LF",
9
9
  "rootDir": "src",