diff --git a/rpc/js/src/dial.ts b/rpc/js/src/dial.ts index 73182baf..f68208ab 100644 --- a/rpc/js/src/dial.ts +++ b/rpc/js/src/dial.ts @@ -228,19 +228,20 @@ class authenticatedTransport implements Transport { method: MethodInfo, signal: AbortSignal | undefined, timeoutMs: number | undefined, - header: Headers, + header: HeadersInit | undefined, message: PartialMessage, contextValues?: ContextValues ): Promise> { - this.extraHeaders.forEach((key: string, value: string) => { - header.set(key, value); + let newHeaders = cloneHeaders(header); + this.extraHeaders.forEach((value: string, key: string) => { + newHeaders.set(key, value); }); return this.transport.unary( service, method, signal, timeoutMs, - header, + newHeaders, message, contextValues ); @@ -254,25 +255,50 @@ class authenticatedTransport implements Transport { method: MethodInfo, signal: AbortSignal | undefined, timeoutMs: number | undefined, - header: Headers, + header: HeadersInit | undefined, input: AsyncIterable>, contextValues?: ContextValues ): Promise> { - this.extraHeaders.forEach((key: string, value: string) => { - header.set(key, value); + let newHeaders = cloneHeaders(header); + this.extraHeaders.forEach((value: string, key: string) => { + newHeaders.set(key, value); }); return this.transport.stream( service, method, signal, timeoutMs, - header, + newHeaders, input, contextValues ); } } +function cloneHeaders(headers: HeadersInit | undefined): Headers { + let cloned = new Headers(); + if (headers) { + if (headers !== undefined) { + if (Array.isArray(headers)) { + for (const [key, value] of headers) { + cloned.append(key, value); + } + } else if ("forEach" in headers) { + if (typeof headers.forEach == "function") { + headers.forEach((value, key) => { + cloned.append(key, value); + }); + } + } else { + for (const [key, value] of Object.entries(headers)) { + cloned.append(key, value); + } + } + } + } + return cloned; +} + export interface WebRTCConnection { transport: Transport; peerConnection: RTCPeerConnection;