-
Notifications
You must be signed in to change notification settings - Fork 16
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this.
src/sdp.ts
Outdated
// import { detect } from 'detect-browser' | ||
|
||
// const browser = detect() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left over to be removed, right?
if (localCert === undefined || localCert.getFingerprints().length === 0) { | ||
throw invalidArgument('no fingerprint on local certificate') | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how this is handled in the js-libp2p codebase in general. Should we still use localCert.getFingerprints
in case the API is available (e.g. on Chrome), or should we always use the hack through the SDP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the single code path across browsers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer trying to use the best option and falling back on the hack. But not blocking this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Marco here, but it would be good to know (debug logs) which option was used if we're going to have different paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SgtPooki I've added the fallback to fetching from the SDP as requested.
@achingbrain @MarcoPolo any objections to merging here? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have firefox run this test in CI?
58276ef
to
7e26a4c
Compare
src/sdp.ts
Outdated
const fingerprintRegex = /^a=fingerprint:(?:\w+-[0-9]+)\s(?<fingerprint>(:?[0-9a-fA-F]{2})+)$/gm | ||
function getFingerprintFromSdp (sdp: string): string | undefined { | ||
const searchResult = fingerprintRegex.exec(sdp) | ||
if (searchResult == null) { | ||
return '' | ||
} | ||
|
||
return searchResult.groups?.fingerprint | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer regex.match
.
Using /g
flag with exec
can cause unexpected issues with the stored lastIndex
being updated and then causing match failures in later .exec
calls.
JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g. /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, exec() can be used to iterate over multiple matches in a string of text (with capture groups), as opposed to getting just the matching strings with String.prototype.match().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @SgtPooki . I was not aware of this. I've incorporated the change and added a simple test.
src/sdp.ts
Outdated
const fingerprintRegex = /^a=fingerprint:(?:\w+-[0-9]+)\s(?<fingerprint>(:?[0-9a-fA-F]{2})+)$/gm | ||
function getFingerprintFromSdp (sdp: string): string | undefined { | ||
const searchResult = fingerprintRegex.exec(sdp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const fingerprintRegex = /^a=fingerprint:(?:\w+-[0-9]+)\s(?<fingerprint>(:?[0-9a-fA-F]{2})+)$/gm | |
function getFingerprintFromSdp (sdp: string): string | undefined { | |
const searchResult = fingerprintRegex.exec(sdp) | |
const fingerprintRegex = /^a=fingerprint:(?:\w+-[0-9]+)\s(?<fingerprint>(:?[0-9a-fA-F]{2})+)$/m | |
function getFingerprintFromSdp (sdp: string): string | undefined { | |
const searchResult = sdp.match(fingerprintRegex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Since firefox does not support `getFingerprints`, we fetch the local fingerprint from the PeerConnection's `localDescription` field by parsing the fingerprint line from the SDP.
7e26a4c
to
3c87add
Compare
@MarcoPolo ping to re-review |
@MarcoPolo I'm still working on firefox browser-to-server tests in CI. Could we move that work to a subsequent PR? |
@MarcoPolo The browser-to-server tests is running on Firefox in CI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not an expert in the business logic of what is going on here, but i ran through the code and found a single potential failure that should be addressed.
src/sdp.ts
Outdated
const searchResult = sdp.match(fingerprintRegex) | ||
if (searchResult == null) { | ||
return '' | ||
} | ||
|
||
return searchResult.groups?.fingerprint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'' == null // false
Returning ''
on line 49 will cause the check on line 215 in src/transports.ts, if (localFingerprint == null) {
to return false. We should return undefined here, or even better:
const searchResult = sdp.match(fingerprintRegex) | |
if (searchResult == null) { | |
return '' | |
} | |
return searchResult.groups?.fingerprint | |
const searchResult = sdp.match(fingerprintRegex) | |
return searchResult?.groups?.fingerprint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
@@ -53,6 +53,11 @@ describe('SDP', () => { | |||
]) | |||
}) | |||
|
|||
it('extracts a fingerprint from sdp', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add a negative test here to validate the fallback behaviour
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this just tests the regex behaviour. While there are no explicit tests for the fallback behaviour, it gets tested when the tests are run in Firefox as part of CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm but i’ll defer to primary maintainers for final approval
@MarcoPolo bump! |
src/transport.ts
Outdated
source: { | ||
[Symbol.asyncIterator]: async function * () { | ||
for await (const list of wrappedChannel.source) { | ||
yield list.subarray() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may copy unnecessarily (I know this was here before).
Thanks a lot @SgtPooki! Your review was very helpful :) And thanks @wemeetagain for taking a look |
## [1.1.10](v1.1.9...v1.1.10) (2023-05-03) ### Bug Fixes * Fetch local fingerprint from SDP ([#109](#109)) ([3673d6c](3673d6c))
🎉 This PR is included in version 1.1.10 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Since firefox does not support
getFingerprints
, we fetch the local fingerprint from the PeerConnection'slocalDescription
field by parsing the fingerprint line from the SDP.