Skip to content

Commit 40557ff

Browse files
authored
Always include cookies when communicating with signaling server (#629)
1 parent 6291f44 commit 40557ff

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

e2e/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createRobotClient } from '../../src/main';
33
const main = async () => {
44
const machine = await createRobotClient({
55
host: 'e2e-ts-sdk',
6+
serviceHost: 'http://localhost:9090',
67
signalingAddress: 'http://localhost:9090',
78
iceServers: [{ urls: 'stun:global.stun.twilio.com:3478' }],
89
});

src/robot/client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,13 @@ export class RobotClient extends EventDispatcher implements Robot {
709709
opts.webrtcOptions.signalingCredentials = opts.credentials;
710710
}
711711

712+
const signalingAddress =
713+
this.webrtcOptions.signalingAddress || this.serviceHost;
712714
const webRTCConn = await dialWebRTC(
713-
this.webrtcOptions.signalingAddress || this.serviceHost,
715+
signalingAddress,
714716
this.webrtcOptions.host,
715-
opts
717+
opts,
718+
this.serviceHost !== '' && signalingAddress !== this.serviceHost
716719
);
717720

718721
/*

src/rpc/dial.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,17 @@ interface TransportInitOptions {
103103

104104
export const dialDirect = async (
105105
address: string,
106-
opts?: DialOptions
106+
opts?: DialOptions,
107+
transportCredentialsInclude = false
107108
): Promise<Transport> => {
108109
validateDialOptions(opts);
109110
const createTransport =
110111
globalThis.VIAM?.GRPC_TRANSPORT_FACTORY ?? createGrpcWebTransport;
111112

112113
const transportOpts = {
113114
baseUrl: address,
115+
// Default is "same-origin", which does not include localhost:*.
116+
credentials: 'same-origin' as RequestCredentials,
114117
};
115118

116119
// Client already has access token with no external auth, skip Authenticate process.
@@ -133,6 +136,10 @@ export const dialDirect = async (
133136
opts === undefined ||
134137
(opts.credentials === undefined && opts.accessToken === undefined)
135138
) {
139+
// With same-origin, services running on other ports that expect cookies from App would otherwise not receive them.
140+
if (transportCredentialsInclude) {
141+
transportOpts.credentials = 'include';
142+
}
136143
return createTransport(transportOpts);
137144
}
138145

@@ -305,10 +312,15 @@ export interface WebRTCConnection {
305312
const getOptionalWebRTCConfig = async (
306313
signalingAddress: string,
307314
callOpts: CallOptions,
308-
dialOpts?: DialOptions
315+
dialOpts?: DialOptions,
316+
transportCredentialsInclude = false
309317
): Promise<WebRTCConfig> => {
310318
const optsCopy = { ...dialOpts } as DialOptions;
311-
const directTransport = await dialDirect(signalingAddress, optsCopy);
319+
const directTransport = await dialDirect(
320+
signalingAddress,
321+
optsCopy,
322+
transportCredentialsInclude
323+
);
312324

313325
const signalingClient = createClient(SignalingService, directTransport);
314326
try {
@@ -333,7 +345,8 @@ const getOptionalWebRTCConfig = async (
333345
export const dialWebRTC = async (
334346
signalingAddress: string,
335347
host: string,
336-
dialOpts?: DialOptions
348+
dialOpts?: DialOptions,
349+
transportCredentialsInclude = false
337350
): Promise<WebRTCConnection> => {
338351
const usableSignalingAddress = signalingAddress.replace(/\/$/u, '');
339352
validateDialOptions(dialOpts);
@@ -356,7 +369,8 @@ export const dialWebRTC = async (
356369
const webrtcOpts = await processWebRTCOpts(
357370
usableSignalingAddress,
358371
callOpts,
359-
dialOpts
372+
dialOpts,
373+
transportCredentialsInclude
360374
);
361375
// then derive options specifically for signaling against our target.
362376
const exchangeOpts = processSignalingExchangeOpts(
@@ -373,7 +387,11 @@ export const dialWebRTC = async (
373387

374388
let directTransport: Transport;
375389
try {
376-
directTransport = await dialDirect(usableSignalingAddress, exchangeOpts);
390+
directTransport = await dialDirect(
391+
usableSignalingAddress,
392+
exchangeOpts,
393+
transportCredentialsInclude
394+
);
377395
} catch (error) {
378396
pc.close();
379397
throw error;
@@ -429,13 +447,15 @@ export const dialWebRTC = async (
429447
const processWebRTCOpts = async (
430448
signalingAddress: string,
431449
callOpts: CallOptions,
432-
dialOpts?: DialOptions
450+
dialOpts?: DialOptions,
451+
transportCredentialsInclude = false
433452
): Promise<DialWebRTCOptions> => {
434453
// Get TURN servers, if any.
435454
const config = await getOptionalWebRTCConfig(
436455
signalingAddress,
437456
callOpts,
438-
dialOpts
457+
dialOpts,
458+
transportCredentialsInclude
439459
);
440460
const additionalIceServers: RTCIceServer[] = config.additionalIceServers.map(
441461
(ice) => {

0 commit comments

Comments
 (0)