Skip to content

Commit f1acaf2

Browse files
authored
Merge pull request #285 from murgatroid99/ts_style_cleanup
Fix lint errors and formatting
2 parents 5c9a792 + 881b82d commit f1acaf2

19 files changed

+604
-555
lines changed

packages/grpc-js-core/src/call-credentials-filter.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ export class CallCredentialsFilter extends BaseFilter implements Filter {
1010
private serviceUrl: string;
1111
constructor(
1212
private readonly credentials: CallCredentials,
13-
private readonly host: string,
14-
private readonly path: string) {
13+
private readonly host: string, private readonly path: string) {
1514
super();
16-
let splitPath: string[] = path.split('/');
17-
let serviceName: string = '';
15+
const splitPath: string[] = path.split('/');
16+
let serviceName = '';
1817
/* The standard path format is "/{serviceName}/{methodName}", so if we split
1918
* by '/', the first item should be empty and the second should be the
2019
* service name */
@@ -27,8 +26,9 @@ export class CallCredentialsFilter extends BaseFilter implements Filter {
2726
}
2827

2928
async sendMetadata(metadata: Promise<Metadata>): Promise<Metadata> {
30-
let credsMetadata = this.credentials.generateMetadata({ service_url: this.serviceUrl });
31-
let resultMetadata = await metadata;
29+
const credsMetadata =
30+
this.credentials.generateMetadata({service_url: this.serviceUrl});
31+
const resultMetadata = await metadata;
3232
resultMetadata.merge(await credsMetadata);
3333
return resultMetadata;
3434
}
@@ -43,8 +43,7 @@ export class CallCredentialsFilterFactory implements
4343

4444
createFilter(callStream: CallStream): CallCredentialsFilter {
4545
return new CallCredentialsFilter(
46-
this.credentials.compose(callStream.getCredentials()),
47-
callStream.getHost(),
48-
callStream.getMethod());
46+
this.credentials.compose(callStream.getCredentials()),
47+
callStream.getHost(), callStream.getMethod());
4948
}
5049
}

packages/grpc-js-core/src/call-credentials.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,59 @@ import {map, reduce} from 'lodash';
22

33
import {Metadata} from './metadata';
44

5-
export type CallMetadataOptions = { service_url: string; };
5+
export type CallMetadataOptions = {
6+
service_url: string;
7+
};
68

79
export type CallMetadataGenerator =
8-
(options: CallMetadataOptions, cb: (err: Error|null, metadata?: Metadata) => void) =>
9-
void;
10+
(options: CallMetadataOptions,
11+
cb: (err: Error|null, metadata?: Metadata) => void) => void;
1012

1113
/**
1214
* A class that represents a generic method of adding authentication-related
1315
* metadata on a per-request basis.
1416
*/
15-
export interface CallCredentials {
17+
export abstract class CallCredentials {
1618
/**
1719
* Asynchronously generates a new Metadata object.
1820
* @param options Options used in generating the Metadata object.
1921
*/
20-
generateMetadata(options: CallMetadataOptions): Promise<Metadata>;
22+
abstract generateMetadata(options: CallMetadataOptions): Promise<Metadata>;
2123
/**
2224
* Creates a new CallCredentials object from properties of both this and
2325
* another CallCredentials object. This object's metadata generator will be
2426
* called first.
2527
* @param callCredentials The other CallCredentials object.
2628
*/
27-
compose(callCredentials: CallCredentials): CallCredentials;
29+
abstract compose(callCredentials: CallCredentials): CallCredentials;
30+
31+
/**
32+
* Creates a new CallCredentials object from a given function that generates
33+
* Metadata objects.
34+
* @param metadataGenerator A function that accepts a set of options, and
35+
* generates a Metadata object based on these options, which is passed back
36+
* to the caller via a supplied (err, metadata) callback.
37+
*/
38+
static createFromMetadataGenerator(metadataGenerator: CallMetadataGenerator):
39+
CallCredentials {
40+
return new SingleCallCredentials(metadataGenerator);
41+
}
42+
43+
static createEmpty(): CallCredentials {
44+
return new EmptyCallCredentials();
45+
}
2846
}
2947

30-
class ComposedCallCredentials implements CallCredentials {
31-
constructor(private creds: CallCredentials[]) {}
48+
class ComposedCallCredentials extends CallCredentials {
49+
constructor(private creds: CallCredentials[]) {
50+
super();
51+
}
3252

3353
async generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
34-
let base: Metadata = new Metadata();
35-
let generated: Metadata[] = await Promise.all(
54+
const base: Metadata = new Metadata();
55+
const generated: Metadata[] = await Promise.all(
3656
map(this.creds, (cred) => cred.generateMetadata(options)));
37-
for (let gen of generated) {
57+
for (const gen of generated) {
3858
base.merge(gen);
3959
}
4060
return base;
@@ -45,8 +65,10 @@ class ComposedCallCredentials implements CallCredentials {
4565
}
4666
}
4767

48-
class SingleCallCredentials implements CallCredentials {
49-
constructor(private metadataGenerator: CallMetadataGenerator) {}
68+
class SingleCallCredentials extends CallCredentials {
69+
constructor(private metadataGenerator: CallMetadataGenerator) {
70+
super();
71+
}
5072

5173
generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
5274
return new Promise<Metadata>((resolve, reject) => {
@@ -65,7 +87,7 @@ class SingleCallCredentials implements CallCredentials {
6587
}
6688
}
6789

68-
class EmptyCallCredentials implements CallCredentials {
90+
class EmptyCallCredentials extends CallCredentials {
6991
generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
7092
return Promise.resolve(new Metadata());
7193
}
@@ -74,21 +96,3 @@ class EmptyCallCredentials implements CallCredentials {
7496
return other;
7597
}
7698
}
77-
78-
export namespace CallCredentials {
79-
/**
80-
* Creates a new CallCredentials object from a given function that generates
81-
* Metadata objects.
82-
* @param metadataGenerator A function that accepts a set of options, and
83-
* generates a Metadata object based on these options, which is passed back
84-
* to the caller via a supplied (err, metadata) callback.
85-
*/
86-
export function createFromMetadataGenerator(
87-
metadataGenerator: CallMetadataGenerator): CallCredentials {
88-
return new SingleCallCredentials(metadataGenerator);
89-
}
90-
91-
export function createEmpty(): CallCredentials {
92-
return new EmptyCallCredentials();
93-
}
94-
}

packages/grpc-js-core/src/call-stream.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {FilterStackFactory} from './filter-stack';
99
import {Metadata} from './metadata';
1010
import {ObjectDuplex} from './object-stream';
1111

12-
const {HTTP2_HEADER_STATUS, HTTP2_HEADER_CONTENT_TYPE, NGHTTP2_CANCEL} = http2.constants;
12+
const {HTTP2_HEADER_STATUS, HTTP2_HEADER_CONTENT_TYPE, NGHTTP2_CANCEL} =
13+
http2.constants;
1314

14-
export type Deadline = Date | number;
15+
export type Deadline = Date|number;
1516

1617
export interface CallStreamOptions {
1718
deadline: Deadline;
@@ -36,20 +37,19 @@ export interface WriteObject {
3637
/**
3738
* This interface represents a duplex stream associated with a single gRPC call.
3839
*/
39-
export type CallStream = {
40-
cancelWithStatus(status: Status, details: string): void;
41-
getPeer(): string;
40+
export type CallStream = {
41+
cancelWithStatus(status: Status, details: string): void; getPeer(): string;
4242

4343
getDeadline(): Deadline;
4444
getCredentials(): CallCredentials;
4545
/* If the return value is null, the call has not ended yet. Otherwise, it has
4646
* ended with the specified status */
47-
getStatus(): StatusObject|null;
47+
getStatus(): StatusObject | null;
4848
getMethod(): string;
4949
getHost(): string;
50-
} & EmitterAugmentation1<'metadata', Metadata>
51-
& EmitterAugmentation1<'status', StatusObject>
52-
& ObjectDuplex<WriteObject, Buffer>;
50+
}&EmitterAugmentation1<'metadata', Metadata>&
51+
EmitterAugmentation1<'status', StatusObject>&
52+
ObjectDuplex<WriteObject, Buffer>;
5353

5454
enum ReadState {
5555
NO_DATA,
@@ -60,7 +60,7 @@ enum ReadState {
6060
const emptyBuffer = Buffer.alloc(0);
6161

6262
export class Http2CallStream extends Duplex implements CallStream {
63-
public filterStack: Filter;
63+
filterStack: Filter;
6464
private statusEmitted = false;
6565
private http2Stream: http2.ClientHttp2Stream|null = null;
6666
private pendingRead = false;
@@ -76,7 +76,7 @@ export class Http2CallStream extends Duplex implements CallStream {
7676
private readPartialMessage: Buffer[] = [];
7777
private readMessageRemaining = 0;
7878

79-
private unpushedReadMessages: (Buffer|null)[] = [];
79+
private unpushedReadMessages: Array<Buffer|null> = [];
8080

8181
// Status code mapped from :status. To be used if grpc-status is not received
8282
private mappedStatusCode: Status = Status.UNKNOWN;
@@ -124,20 +124,21 @@ export class Http2CallStream extends Duplex implements CallStream {
124124
}
125125

126126
private handleTrailers(headers: http2.IncomingHttpHeaders) {
127-
let code: Status = this.mappedStatusCode;
128-
let details = '';
127+
const code: Status = this.mappedStatusCode;
128+
const details = '';
129129
let metadata: Metadata;
130130
try {
131131
metadata = Metadata.fromHttp2Headers(headers);
132132
} catch (e) {
133133
metadata = new Metadata();
134134
}
135-
let status: StatusObject = {code, details, metadata};
135+
const status: StatusObject = {code, details, metadata};
136136
this.handlingTrailers = (async () => {
137137
let finalStatus;
138138
try {
139139
// Attempt to assign final status.
140-
finalStatus = await this.filterStack.receiveTrailers(Promise.resolve(status));
140+
finalStatus =
141+
await this.filterStack.receiveTrailers(Promise.resolve(status));
141142
} catch (error) {
142143
await this.handlingHeaders;
143144
// This is a no-op if the call was already ended when handling headers.
@@ -195,17 +196,26 @@ export class Http2CallStream extends Duplex implements CallStream {
195196
try {
196197
metadata = Metadata.fromHttp2Headers(headers);
197198
} catch (error) {
198-
this.endCall({code: Status.UNKNOWN, details: error.message, metadata: new Metadata()});
199+
this.endCall({
200+
code: Status.UNKNOWN,
201+
details: error.message,
202+
metadata: new Metadata()
203+
});
199204
return;
200205
}
201206
this.handlingHeaders =
202-
this.filterStack.receiveMetadata(Promise.resolve(metadata))
203-
.then((finalMetadata) => {
204-
this.emit('metadata', finalMetadata);
205-
}).catch((error) => {
206-
this.destroyHttp2Stream();
207-
this.endCall({code: Status.UNKNOWN, details: error.message, metadata: new Metadata()});
208-
});
207+
this.filterStack.receiveMetadata(Promise.resolve(metadata))
208+
.then((finalMetadata) => {
209+
this.emit('metadata', finalMetadata);
210+
})
211+
.catch((error) => {
212+
this.destroyHttp2Stream();
213+
this.endCall({
214+
code: Status.UNKNOWN,
215+
details: error.message,
216+
metadata: new Metadata()
217+
});
218+
});
209219
}
210220
});
211221
stream.on('trailers', this.handleTrailers.bind(this));
@@ -260,6 +270,9 @@ export class Http2CallStream extends Duplex implements CallStream {
260270
canPush = this.tryPush(messageBytes, canPush);
261271
this.readState = ReadState.NO_DATA;
262272
}
273+
break;
274+
default:
275+
throw new Error('This should never happen');
263276
}
264277
}
265278
});
@@ -298,7 +311,7 @@ export class Http2CallStream extends Duplex implements CallStream {
298311
// This is OK, because status codes emitted here correspond to more
299312
// catastrophic issues that prevent us from receiving trailers in the
300313
// first place.
301-
this.endCall({code: code, details: details, metadata: new Metadata()});
314+
this.endCall({code, details, metadata: new Metadata()});
302315
});
303316
stream.on('error', (err: Error) => {
304317
this.endCall({
@@ -338,7 +351,7 @@ export class Http2CallStream extends Duplex implements CallStream {
338351
// If trailers are currently being processed, the call should be ended
339352
// by handleTrailers instead.
340353
await this.handlingTrailers;
341-
this.endCall({code: status, details: details, metadata: new Metadata()});
354+
this.endCall({code: status, details, metadata: new Metadata()});
342355
})();
343356
}
344357

0 commit comments

Comments
 (0)