Skip to content

Commit 36fd416

Browse files
committed
fix auth
1 parent 057a935 commit 36fd416

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

rpc/js/src/dial.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,20 @@ class authenticatedTransport implements Transport {
228228
method: MethodInfo<I, O>,
229229
signal: AbortSignal | undefined,
230230
timeoutMs: number | undefined,
231-
header: Headers,
231+
header: HeadersInit | undefined,
232232
message: PartialMessage<I>,
233233
contextValues?: ContextValues
234234
): Promise<UnaryResponse<I, O>> {
235-
this.extraHeaders.forEach((key: string, value: string) => {
236-
header.set(key, value);
235+
let newHeaders = cloneHeaders(header);
236+
this.extraHeaders.forEach((value: string, key: string) => {
237+
newHeaders.set(key, value);
237238
});
238239
return this.transport.unary(
239240
service,
240241
method,
241242
signal,
242243
timeoutMs,
243-
header,
244+
newHeaders,
244245
message,
245246
contextValues
246247
);
@@ -254,25 +255,50 @@ class authenticatedTransport implements Transport {
254255
method: MethodInfo<I, O>,
255256
signal: AbortSignal | undefined,
256257
timeoutMs: number | undefined,
257-
header: Headers,
258+
header: HeadersInit | undefined,
258259
input: AsyncIterable<PartialMessage<I>>,
259260
contextValues?: ContextValues
260261
): Promise<StreamResponse<I, O>> {
261-
this.extraHeaders.forEach((key: string, value: string) => {
262-
header.set(key, value);
262+
let newHeaders = cloneHeaders(header);
263+
this.extraHeaders.forEach((value: string, key: string) => {
264+
newHeaders.set(key, value);
263265
});
264266
return this.transport.stream(
265267
service,
266268
method,
267269
signal,
268270
timeoutMs,
269-
header,
271+
newHeaders,
270272
input,
271273
contextValues
272274
);
273275
}
274276
}
275277

278+
function cloneHeaders(headers: HeadersInit | undefined): Headers {
279+
let cloned = new Headers();
280+
if (headers) {
281+
if (headers !== undefined) {
282+
if (Array.isArray(headers)) {
283+
for (const [key, value] of headers) {
284+
cloned.append(key, value);
285+
}
286+
} else if ("forEach" in headers) {
287+
if (typeof headers.forEach == "function") {
288+
headers.forEach((value, key) => {
289+
cloned.append(key, value);
290+
});
291+
}
292+
} else {
293+
for (const [key, value] of Object.entries<string>(headers)) {
294+
cloned.append(key, value);
295+
}
296+
}
297+
}
298+
}
299+
return cloned;
300+
}
301+
276302
export interface WebRTCConnection {
277303
transport: Transport;
278304
peerConnection: RTCPeerConnection;

0 commit comments

Comments
 (0)