Skip to content

Commit

Permalink
add logic to find newest service
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Oct 4, 2023
1 parent 8843548 commit 3719559
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-olives-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl": patch
---

Fix version normalization of services in FCL
7 changes: 2 additions & 5 deletions packages/fcl/src/current-user/build-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {fetchServices} from "./fetch-services"
import {mergeServices} from "./merge-services"
import {USER_PRAGMA} from "../normalizers/service/__vsn"
import {normalizeService} from "../normalizers/service/service"
import {serviceOfType} from "./service-of-type"

function deriveCompositeId(authn) {
return rlp
Expand All @@ -20,10 +21,6 @@ function normalizeData(data) {
return data
}

function findService(type, services) {
return services.find(d => d.type === type)
}

export async function buildUser(data) {
data = normalizeData(data)

Expand All @@ -34,7 +31,7 @@ export async function buildUser(data) {
.map(service => normalizeService(service, data))
.filter(Boolean)

const authn = findService("authn", services)
const authn = serviceOfType(services, "authn")

return {
...USER_PRAGMA,
Expand Down
29 changes: 28 additions & 1 deletion packages/fcl/src/current-user/service-of-type.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
import {invariant} from "@onflow/util-invariant"

function compareVersions(a, b) {
function helper(a, b) {
if (a.length === 0) return 0
if (a[0] > b[0]) return true
if (a[0] < b[0]) return false
return helper(a.slice(1), b.slice(1))
}
const aVsn = a.split(".")
const bVsn = b.split(".")

invariant(aVsn.length === 3, "Invalid version: " + a)
invariant(bVsn.length === 3, "Invalid version: " + b)

return helper(a.split("."), b.split("."))
}

export function serviceOfType(services = [], type) {
return services.find(service => service.type === type)
// Find the greatest version of the service type
return services.reduce(
(service, mostRecent) =>
!mostRecent ||
(service.type === type &&
compareVersions(service.f_vsn, mostRecent.f_vsn))
? service
: mostRecent,
null
)
}

0 comments on commit 3719559

Please sign in to comment.