Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Jan 1, 2024
1 parent 6690452 commit ba5af2e
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 117 deletions.
88 changes: 46 additions & 42 deletions packages/ice/src/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Connection {

constructor(
public iceControlling: boolean,
options?: Partial<IceOptions>
options?: Partial<IceOptions>,
) {
this.options = {
...defaultOptions,
Expand Down Expand Up @@ -108,15 +108,15 @@ export class Connection {
const { interfaceAddresses } = this.options;
if (interfaceAddresses) {
const filteredAddresses = address.filter((check) =>
Object.values(interfaceAddresses).includes(check)
Object.values(interfaceAddresses).includes(check),
);
if (filteredAddresses.length) {
address = filteredAddresses;
}
}
if (this.options.additionalHostAddresses) {
address = Array.from(
new Set([...this.options.additionalHostAddresses, ...address])
new Set([...this.options.additionalHostAddresses, ...address]),
);
}

Expand All @@ -125,7 +125,7 @@ export class Connection {
component,
address,
5,
cb
cb,
);
this.localCandidates = [...this.localCandidates, ...candidates];
}
Expand All @@ -140,7 +140,7 @@ export class Connection {
component: number,
addresses: string[],
timeout = 5,
cb?: (candidate: Candidate) => void
cb?: (candidate: Candidate) => void,
) {
let candidates: Candidate[] = [];

Expand All @@ -152,7 +152,7 @@ export class Connection {
await protocol.connectionMade(
isIPv4(address),
this.options.portRange,
this.options.interfaceAddresses
this.options.interfaceAddresses,
);
} catch (error) {
log("protocol STUN", error);
Expand All @@ -171,14 +171,14 @@ export class Connection {
candidatePriority(component, "host"),
address,
protocol.getExtraInfo()[1],
"host"
"host",
);

candidates.push(protocol.localCandidate);
if (cb) {
cb(protocol.localCandidate);
}
})
}),
);

let candidatePromises: Promise<Candidate | void>[] = [];
Expand All @@ -195,7 +195,7 @@ export class Connection {
) {
const candidate = await serverReflexiveCandidate(
protocol,
stunServer
stunServer,
).catch((error) => log("error", error));
if (candidate && cb) cb(candidate);

Expand All @@ -207,7 +207,7 @@ export class Connection {
}
}).catch((error) => {
log("query STUN server", error);
})
}),
);
candidatePromises.push(...stunPromises);
}
Expand All @@ -222,7 +222,7 @@ export class Connection {
{
portRange: this.options.portRange,
interfaceAddresses: this.options.interfaceAddresses,
}
},
);
this.protocols.push(protocol);

Expand All @@ -240,7 +240,7 @@ export class Connection {
candidateAddress[1],
"relay",
relatedAddress[0],
relatedAddress[1]
relatedAddress[1],
);
if (cb) {
cb(protocol.localCandidate);
Expand All @@ -262,10 +262,10 @@ export class Connection {
const extraCandidates = [...(await Promise.allSettled(candidatePromises))]
.filter(
(
v
v,
): v is PromiseFulfilledResult<
Awaited<(typeof candidatePromises)[number]>
> => v.status === "fulfilled"
> => v.status === "fulfilled",
)
.map((v) => v.value)
.filter((v): v is Candidate => typeof v !== "undefined");
Expand Down Expand Up @@ -333,7 +333,7 @@ export class Connection {
private unfreezeInitial() {
// # unfreeze first pair for the first component
const firstPair = this.checkList.find(
(pair) => pair.component === Math.min(...[...this._components])
(pair) => pair.component === Math.min(...[...this._components]),
);
if (!firstPair) return;
if (firstPair.state === CandidatePairState.FROZEN) {
Expand Down Expand Up @@ -375,7 +375,7 @@ export class Connection {
{
// # find the highest-priority pair that is in the frozen state
const pair = this.checkList.find(
(pair) => pair.state === CandidatePairState.FROZEN
(pair) => pair.state === CandidatePairState.FROZEN,
);
if (pair) {
pair.handle = future(this.checkStart(pair));
Expand Down Expand Up @@ -413,7 +413,7 @@ export class Connection {
await timers.setTimeout(
CONSENT_INTERVAL * (0.8 + 0.4 * Math.random()) * 1000,
undefined,
{ signal: cancelEvent.signal }
{ signal: cancelEvent.signal },
);

for (const key of this.nominatedKeys) {
Expand All @@ -424,7 +424,7 @@ export class Connection {
request,
pair.remoteAddr,
Buffer.from(this.remotePassword, "utf8"),
0
0,
);
failures = 0;
if (this.state === "disconnected") {
Expand Down Expand Up @@ -469,7 +469,7 @@ export class Connection {
this.checkListState.put(
new Promise((r) => {
r(ICE_FAILED);
})
}),
);
}

Expand Down Expand Up @@ -563,10 +563,10 @@ export class Connection {

getDefaultCandidate(component: number) {
const candidates = this.localCandidates.sort(
(a, b) => a.priority - b.priority
(a, b) => a.priority - b.priority,
);
const candidate = candidates.find(
(candidate) => candidate.component === component
(candidate) => candidate.component === component,
);
return candidate;
}
Expand All @@ -575,7 +575,7 @@ export class Connection {
message: Message,
addr: Address,
protocol: Protocol,
rawData: Buffer
rawData: Buffer,
) {
if (message.messageMethod !== methods.BINDING) {
this.respondError(message, addr, protocol, [400, "Bad Request"]);
Expand Down Expand Up @@ -629,7 +629,7 @@ export class Connection {
const response = new Message(
methods.BINDING,
classes.RESPONSE,
message.transactionId
message.transactionId,
);
response
.setAttribute("XOR-MAPPED-ADDRESS", addr)
Expand Down Expand Up @@ -677,7 +677,7 @@ export class Connection {

private pruneComponents() {
const seenComponents = new Set(
this.remoteCandidates.map((v) => v.component)
this.remoteCandidates.map((v) => v.component),
);
const missingComponents = [...difference(this._components, seenComponents)];
if (missingComponents.length > 0) {
Expand All @@ -693,7 +693,7 @@ export class Connection {
const pair = this.checkList.find(
(pair) =>
isEqual(pair.protocol, protocol) &&
isEqual(pair.remoteCandidate, remoteCandidate)
isEqual(pair.remoteCandidate, remoteCandidate),
);
return pair;
}
Expand Down Expand Up @@ -738,7 +738,7 @@ export class Connection {
if (
p.component === pair.component &&
[CandidatePairState.WAITING, CandidatePairState.FROZEN].includes(
p.state
p.state,
)
) {
this.setPairState(p, CandidatePairState.FAILED);
Expand Down Expand Up @@ -788,7 +788,7 @@ export class Connection {
this.checkListState.put(
new Promise((r) => {
r(ICE_FAILED);
})
}),
);
}
}
Expand All @@ -815,7 +815,7 @@ export class Connection {
request,
pair.remoteAddr,
Buffer.from(this.remotePassword, "utf8"),
4
4,
);
log("response", response, addr);
result.response = response;
Expand Down Expand Up @@ -863,7 +863,7 @@ export class Connection {
await pair.protocol.request(
request,
pair.remoteAddr,
Buffer.from(this.remotePassword, "utf8")
Buffer.from(this.remotePassword, "utf8"),
);
} catch (error) {
this.setPairState(pair, CandidatePairState.FAILED);
Expand Down Expand Up @@ -911,7 +911,7 @@ export class Connection {
message.getAttributeValue("PRIORITY"),
host,
port,
"prflx"
"prflx",
);
this.remoteCandidates.push(remoteCandidate);
}
Expand All @@ -928,7 +928,7 @@ export class Connection {
// 7.2.1.4. Triggered Checks
if (
[CandidatePairState.WAITING, CandidatePairState.FAILED].includes(
pair.state
pair.state,
)
) {
pair.handle = future(this.checkStart(pair));
Expand Down Expand Up @@ -989,12 +989,12 @@ export class Connection {
request: Message,
addr: Address,
protocol: Protocol,
errorCode: [number, string]
errorCode: [number, string],
) {
const response = new Message(
request.messageMethod,
classes.ERROR,
request.transactionId
request.transactionId,
);
response
.setAttribute("ERROR-CODE", errorCode)
Expand Down Expand Up @@ -1025,7 +1025,7 @@ export class CandidatePair {

constructor(
public protocol: Protocol,
public remoteCandidate: Candidate
public remoteCandidate: Candidate,
) {}

updateState(state: CandidatePairState) {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ export interface IceOptions {
filterStunResponse?: (
message: Message,
addr: Address,
protocol: Protocol
protocol: Protocol,
) => boolean;
filterCandidatePair?: (pair: CandidatePair) => boolean;
}
Expand All @@ -1104,24 +1104,28 @@ export function validateRemoteCandidate(candidate: Candidate) {

export function sortCandidatePairs(
pairs: CandidatePair[],
iceControlling: boolean
iceControlling: boolean,
) {
pairs.sort(
(a, b) =>
candidatePairPriority(
a.localCandidate,
a.remoteCandidate,
iceControlling
iceControlling,
) -
candidatePairPriority(b.localCandidate, b.remoteCandidate, iceControlling)
candidatePairPriority(
b.localCandidate,
b.remoteCandidate,
iceControlling,
),
);
}

// 5.7.2. Computing Pair Priority and Ordering Pairs
export function candidatePairPriority(
local: Candidate,
remote: Candidate,
iceControlling: boolean
iceControlling: boolean,
) {
const G = (iceControlling && local.priority) || remote.priority;
const D = (iceControlling && remote.priority) || local.priority;
Expand Down Expand Up @@ -1156,7 +1160,7 @@ function nodeIpAddress(family: number): string[] {
(details) =>
normalizeFamilyNodeV18(details.family) === family &&
!nodeIp.isLoopback(details.address) &&
!isAutoconfigurationAddress(details)
!isAutoconfigurationAddress(details),
);
return {
nic,
Expand All @@ -1183,7 +1187,7 @@ export function getHostAddresses(useIpv4: boolean, useIpv6: boolean) {

export async function serverReflexiveCandidate(
protocol: Protocol,
stunServer: Address
stunServer: Address,
) {
// """
// Query STUN server to obtain a server-reflexive candidate.
Expand All @@ -1206,7 +1210,7 @@ export async function serverReflexiveCandidate(
response.getAttributeValue("XOR-MAPPED-ADDRESS")[1],
"srflx",
localCandidate.host,
localCandidate.port
localCandidate.port,
);
} catch (error) {
// todo fix
Expand Down
Loading

0 comments on commit ba5af2e

Please sign in to comment.