Skip to content

Commit a9dc19d

Browse files
github1github1
authored andcommitted
fix typing issues
1 parent f252ded commit a9dc19d

File tree

3 files changed

+142
-85
lines changed

3 files changed

+142
-85
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "meshage",
3-
"version": "0.4.6",
3+
"version": "0.4.3",
44
"description": "A simple service mesh. Messages sent within the service mesh can be consistently partitioned across members of the cluster.",
55
"main": "./index.js",
66
"types": "./index.d.ts",
@@ -54,4 +54,4 @@
5454
"type": "git",
5555
"url": "https://github.com/github1/meshage"
5656
}
57-
}
57+
}

src/backends/http/http-support.ts

Lines changed: 136 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
SubjectMessageEnvelope,
99
SubjectMessageHeader,
1010
SubjectMessageOptions,
11-
toMeshBackendProvision
11+
toMeshBackendProvision,
1212
} from '../../';
1313
import * as express from 'express';
1414
import * as bodyParser from 'body-parser';
@@ -17,61 +17,82 @@ import * as httplib from 'http';
1717
import * as urllib from 'url';
1818
import * as normalizeUrl from 'normalize-url';
1919

20-
const log : debug.Debugger = debug('meshage')
21-
.extend('http');
20+
const log: debug.Debugger = debug('meshage').extend('http');
2221

2322
interface PreparedHttpMessage {
24-
message : SubjectMessage;
25-
headerData : SubjectMessageHeader;
23+
message: SubjectMessage;
24+
headerData: SubjectMessageHeader;
2625
}
2726

2827
class HttpMeshBackend extends MeshBackendBase {
28+
private app: express.Express;
29+
private server: httplib.Server;
2930

30-
private app : express.Express;
31-
private server : httplib.Server;
32-
33-
constructor(private readonly meshPrivateInternal : MeshBackend, private readonly port : number) {
31+
constructor(
32+
private readonly meshPrivateInternal: MeshBackend,
33+
private readonly port: number
34+
) {
3435
super();
3536
// tslint:disable-next-line:no-unsafe-any
3637
this.handlers = this.meshPrivateInternal['handlers'];
3738
}
3839

39-
private static processParameters(reqParams : { [key : string] : string } = {}, reqBody : {}) : { [key : string] : string } {
40-
return Object.keys(reqParams)
41-
.reduce((params : { [key : string] : string }, key : string) => {
42-
params[key] = reqParams[key].replace(/{([^}]+)}/g, (m : string, token : string) => {
43-
// tslint:disable-next-line:no-parameter-reassignment
44-
token = token.replace(/^body\./, '');
45-
// tslint:disable-next-line:no-unsafe-any
46-
return reqBody[token] || token;
47-
});
40+
private static processParameters(
41+
reqParams: { [key: string]: string } = {},
42+
reqBody: {}
43+
): { [key: string]: string } {
44+
return Object.keys(reqParams).reduce(
45+
(params: { [key: string]: string }, key: string) => {
46+
params[key] = reqParams[key].replace(
47+
/{([^}]+)}/g,
48+
(m: string, token: string) => {
49+
// tslint:disable-next-line:no-parameter-reassignment
50+
token = token.replace(/^body\./, '');
51+
// tslint:disable-next-line:no-unsafe-any
52+
return reqBody[token] || token;
53+
}
54+
);
4855
return params;
49-
}, {});
56+
},
57+
{}
58+
);
5059
}
5160

52-
private static prepareHttpMessage<T>(req : express.Request) : PreparedHttpMessage {
61+
private static prepareHttpMessage<T>(
62+
req: express.Request
63+
): PreparedHttpMessage {
5364
// tslint:disable-next-line:no-any
54-
let reqUrl : string;
65+
let reqUrl: string;
5566
if (process.env.PUBLIC_URL) {
56-
reqUrl = normalizeUrl([process.env.PUBLIC_URL, req.originalUrl].join('/'));
67+
reqUrl = normalizeUrl(
68+
[process.env.PUBLIC_URL, req.originalUrl].join('/')
69+
);
5770
} else {
5871
reqUrl = urllib.format({
5972
protocol: req.protocol,
6073
host: req.headers.host,
61-
pathname: req.originalUrl
74+
pathname: req.originalUrl,
6275
});
6376
if (req.originalUrl.search(/\?/) >= 0) {
6477
reqUrl = reqUrl.replace(/%3F/g, '?');
6578
}
6679
}
6780
// tslint:disable-next-line:no-unsafe-any
68-
const params : { [key : string] : string } = this.processParameters(req.params, req.body);
81+
const params: { [key: string]: string } = this.processParameters(
82+
req.params,
83+
req.body
84+
);
6985
// tslint:disable-next-line:no-unsafe-any no-any
70-
const query : { [key : string] : string } = this.processParameters(req.query as any, req.body);
86+
const query: { [key: string]: string } = this.processParameters(
87+
req.query as any,
88+
req.body
89+
);
7190
// tslint:disable-next-line:no-unsafe-any
72-
const messageName : string = query.messageName ? `${query.messageName}` : req.body.name;
91+
const messageName: string = query.messageName
92+
? `${query.messageName}`
93+
: req.body.name;
7394
return {
74-
message: {...req.body},
95+
message: { ...req.body },
7596
headerData: {
7697
uid: '', // replaced later,
7798
subject: params.subject,
@@ -82,14 +103,14 @@ class HttpMeshBackend extends MeshBackendBase {
82103
url: req.url,
83104
publicUrl: reqUrl,
84105
params,
85-
query
86-
}
87-
}
106+
query,
107+
},
108+
},
88109
};
89110
}
90111

91112
// tslint:disable-next-line:no-any
92-
private static prepareHttpResponse(result : any, res : express.Response) {
113+
private static prepareHttpResponse(result: any, res: express.Response) {
93114
let status = 200;
94115
const resultToSend = result || {};
95116
let body = resultToSend;
@@ -117,15 +138,14 @@ class HttpMeshBackend extends MeshBackendBase {
117138
log.extend('prepareHttpResponse')('Sending %o', body);
118139
// tslint:disable-next-line:no-unsafe-any
119140
delete resultToSend.http;
120-
res.status(status)
121-
.send(body);
141+
res.status(status).send(body);
122142
}
123143

124-
public get subscriptionIds() : string[] {
144+
public get subscriptionIds(): string[] {
125145
return this.meshPrivateInternal.subscriptionIds;
126146
}
127147

128-
public async shutdown() : Promise<void> {
148+
public async shutdown(): Promise<void> {
129149
try {
130150
await this.meshPrivateInternal.shutdown();
131151
} catch (err) {
@@ -134,8 +154,8 @@ class HttpMeshBackend extends MeshBackendBase {
134154
if (this.server) {
135155
try {
136156
// tslint:disable-next-line:typedef
137-
await new Promise((resolve, reject) => {
138-
this.server.close((err : Error) => {
157+
await new Promise<void>((resolve, reject) => {
158+
this.server.close((err: Error) => {
139159
if (err) {
140160
reject(err);
141161
} else {
@@ -149,72 +169,100 @@ class HttpMeshBackend extends MeshBackendBase {
149169
}
150170
}
151171

152-
public unregister(subject : string) : Promise<void> {
172+
public unregister(subject: string): Promise<void> {
153173
return this.meshPrivateInternal.unregister(subject);
154174
}
155175

156-
protected async doRegistrations() : Promise<void> {
176+
protected async doRegistrations(): Promise<void> {
157177
if (!this.app) {
158178
this.app = express();
159179
this.app.use(bodyParser.json());
160-
this.app.use(bodyParser.urlencoded({extended: true}));
180+
this.app.use(bodyParser.urlencoded({ extended: true }));
161181
if (process.env.JEST_WORKER_ID) {
162-
this.app.use((req : express.Request, res : express.Response, next : express.NextFunction) => {
163-
res.set('Connection', 'close');
164-
next();
165-
});
182+
this.app.use(
183+
(
184+
req: express.Request,
185+
res: express.Response,
186+
next: express.NextFunction
187+
) => {
188+
res.set('Connection', 'close');
189+
next();
190+
}
191+
);
166192
}
167-
this.app.post('/api/broadcast/:subject',
168-
async (req : express.Request, res : express.Response) => {
193+
this.app.post(
194+
'/api/broadcast/:subject',
195+
async (req: express.Request, res: express.Response) => {
169196
try {
170-
const httpMessage : PreparedHttpMessage = HttpMeshBackend.prepareHttpMessage(req);
197+
const httpMessage: PreparedHttpMessage =
198+
HttpMeshBackend.prepareHttpMessage(req);
171199
if (!httpMessage.headerData.name) {
172-
res.status(400)
173-
.send({error: 'Missing message name'});
200+
res.status(400).send({ error: 'Missing message name' });
174201
} else {
175-
const result = await this.send(httpMessage.headerData.subject,
202+
const result = await this.send(
203+
httpMessage.headerData.subject,
176204
undefined,
177205
httpMessage.message,
178206
{
179-
wait: httpMessage.headerData.http.query.wait === 'true' || httpMessage.headerData.http.query.wait === undefined,
180-
timeout: httpMessage.headerData.http.query.timeout === undefined ? undefined : parseInt(`${httpMessage.headerData.http.query.timeout}`, 10),
181-
additionalHeaderData: httpMessage.headerData
182-
}, true);
207+
wait:
208+
httpMessage.headerData.http.query.wait === 'true' ||
209+
httpMessage.headerData.http.query.wait === undefined,
210+
timeout:
211+
httpMessage.headerData.http.query.timeout === undefined
212+
? undefined
213+
: parseInt(
214+
`${httpMessage.headerData.http.query.timeout}`,
215+
10
216+
),
217+
additionalHeaderData: httpMessage.headerData,
218+
},
219+
true
220+
);
183221
HttpMeshBackend.prepareHttpResponse(result, res);
184222
}
185223
} catch (err) {
186-
res.status(500)
187-
.send({error: (err as Error).message});
224+
res.status(500).send({ error: (err as Error).message });
188225
}
189-
});
190-
this.app.post('/api/:subject/:partitionKey?',
191-
async (req : express.Request, res : express.Response) => {
226+
}
227+
);
228+
this.app.post(
229+
'/api/:subject/:partitionKey?',
230+
async (req: express.Request, res: express.Response) => {
192231
try {
193-
const httpMessage : PreparedHttpMessage = HttpMeshBackend.prepareHttpMessage(req);
232+
const httpMessage: PreparedHttpMessage =
233+
HttpMeshBackend.prepareHttpMessage(req);
194234
if (!httpMessage.headerData.name) {
195-
res.status(400)
196-
.send({error: 'Missing message name'});
235+
res.status(400).send({ error: 'Missing message name' });
197236
} else {
198237
const result = await this.send(
199238
httpMessage.headerData.subject,
200239
httpMessage.headerData.partitionKey,
201240
httpMessage.message,
202241
{
203-
wait: httpMessage.headerData.http.query.wait === 'true' || httpMessage.headerData.http.query.wait === undefined,
204-
timeout: httpMessage.headerData.http.query.timeout === undefined ? undefined : parseInt(`${httpMessage.headerData.http.query.timeout}`, 10),
242+
wait:
243+
httpMessage.headerData.http.query.wait === 'true' ||
244+
httpMessage.headerData.http.query.wait === undefined,
245+
timeout:
246+
httpMessage.headerData.http.query.timeout === undefined
247+
? undefined
248+
: parseInt(
249+
`${httpMessage.headerData.http.query.timeout}`,
250+
10
251+
),
205252
keepSignals: true,
206-
additionalHeaderData: httpMessage.headerData
253+
additionalHeaderData: httpMessage.headerData,
207254
},
208-
false);
255+
false
256+
);
209257
HttpMeshBackend.prepareHttpResponse(result, res);
210258
}
211259
} catch (err) {
212-
res.status(500)
213-
.send({error: (err as Error).message});
260+
res.status(500).send({ error: (err as Error).message });
214261
}
215-
});
262+
}
263+
);
216264
// tslint:disable-next-line:typedef
217-
await new Promise((resolve, reject) => {
265+
await new Promise<void>((resolve, reject) => {
218266
try {
219267
if (this.server) {
220268
resolve();
@@ -233,22 +281,31 @@ class HttpMeshBackend extends MeshBackendBase {
233281
await this.meshPrivateInternal['doRegistrations']();
234282
}
235283

236-
protected doSend<T>(address : string,
237-
envelope : SubjectMessageEnvelope,
238-
options : SubjectMessageOptions,
239-
broadcast : boolean) : Promise<T> {
284+
protected doSend<T>(
285+
address: string,
286+
envelope: SubjectMessageEnvelope,
287+
options: SubjectMessageOptions,
288+
broadcast: boolean
289+
): Promise<T> {
240290
// tslint:disable-next-line:no-unsafe-any
241-
return this.meshPrivateInternal['doSend'](address, envelope, options, broadcast);
291+
return this.meshPrivateInternal['doSend'](
292+
address,
293+
envelope,
294+
options,
295+
broadcast
296+
);
242297
}
243-
244298
}
245299

246-
export function http(provider : MeshBackendProvider, port : number) : MeshBackendProvider {
300+
export function http(
301+
provider: MeshBackendProvider,
302+
port: number
303+
): MeshBackendProvider {
247304
return () => {
248-
const provision : MeshBackendProvision = toMeshBackendProvision(provider());
305+
const provision: MeshBackendProvision = toMeshBackendProvision(provider());
249306
return {
250307
backend: new HttpMeshBackend(provision.backend, port),
251-
callback: provision.callback
308+
callback: provision.callback,
252309
};
253310
};
254311
}

0 commit comments

Comments
 (0)