Skip to content

Commit 4b8e5ce

Browse files
committed
Merge branch 'master' into grpc-node-web-release
2 parents 356b566 + 6c9acf1 commit 4b8e5ce

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

packages/grpc-js/src/channel-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { CompressionAlgorithms } from './compression-algorithms';
2121
* An interface that contains options used when initializing a Channel instance.
2222
*/
2323
export interface ChannelOptions {
24+
'grpc.allow_origin'?: string;
2425
'grpc.ssl_target_name_override'?: string;
2526
'grpc.primary_user_agent'?: string;
2627
'grpc.secondary_user_agent'?: string;

packages/grpc-js/src/server-interceptors.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ export class BaseServerInterceptingCall
546546
private connectionInfo: ConnectionInfo;
547547
private metricsRecorder = new PerRequestMetricRecorder();
548548
private shouldSendMetrics: boolean;
549+
private allowedOrigin?: string;
549550

550551
constructor(
551552
private readonly stream: http2.ServerHttp2Stream,
@@ -598,6 +599,9 @@ export class BaseServerInterceptingCall
598599
if ('grpc.max_receive_message_length' in options) {
599600
this.maxReceiveMessageSize = options['grpc.max_receive_message_length']!;
600601
}
602+
if ('grpc.allow_origin' in options) {
603+
this.allowedOrigin = options['grpc.allow_origin']!;
604+
}
601605

602606
this.host = headers[':authority'] ?? headers.host!;
603607
this.decoder = new StreamDecoder(this.maxReceiveMessageSize);
@@ -877,10 +881,17 @@ export class BaseServerInterceptingCall
877881

878882
this.metadataSent = true;
879883
const custom = metadata ? metadata.toHttp2Headers() : null;
884+
const allowedOriginHeader = this.allowedOrigin
885+
? {
886+
[http2.constants.HTTP2_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN]:
887+
this.allowedOrigin,
888+
}
889+
: null;
880890
const headers = {
881891
...defaultResponseHeaders,
882892
...defaultCompressionHeaders,
883893
...custom,
894+
...allowedOriginHeader,
884895
};
885896
this.stream.respond(headers, defaultResponseOptions);
886897
}
@@ -978,12 +989,21 @@ export class BaseServerInterceptingCall
978989
this.callEventTracker.onStreamEnd(true);
979990
this.callEventTracker.onCallEnd(status);
980991
}
992+
993+
const allowedOriginHeader = this.allowedOrigin
994+
? {
995+
[http2.constants.HTTP2_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN]:
996+
this.allowedOrigin,
997+
}
998+
: null;
999+
9811000
// Trailers-only response
9821001
const trailersToSend: http2.OutgoingHttpHeaders = {
9831002
[GRPC_STATUS_HEADER]: status.code,
9841003
[GRPC_MESSAGE_HEADER]: encodeURI(status.details),
9851004
...defaultResponseHeaders,
9861005
...statusMetadata.toHttp2Headers(),
1006+
...allowedOriginHeader,
9871007
};
9881008
this.stream.respond(trailersToSend, { endStream: true });
9891009
this.notifyOnCancel();

packages/grpc-js/src/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ export class Server {
371371
maxConcurrentStreams: this.options['grpc.max_concurrent_streams'],
372372
};
373373
}
374+
if ('allowedOrigin' in this.options) {
375+
this.options['grpc.allow_origin'] = this.options.allowedOrigin;
376+
}
374377
this.allowedOrigin = this.options.allowedOrigin;
375378
this.interceptors = this.options.interceptors ?? [];
376379
this.trace('Server constructed');

0 commit comments

Comments
 (0)