Improve feed loader, wait for db before executing reads/updates, make taskQueue subscribable, add race, rename auth methods, fix failed wasm verify, fix localhost urls

This commit is contained in:
Jon Staab
2025-04-09 15:39:07 -07:00
parent 1bcc57d695
commit 859f7fa68f
14 changed files with 155 additions and 32 deletions
+5 -5
View File
@@ -107,11 +107,11 @@ describe("auth", () => {
})
})
describe("authenticate", () => {
describe("doAuth", () => {
it("should throw an error when there is no challenge", async () => {
const sign = vi.fn()
await expect(socket.auth.authenticate(sign)).rejects.toThrow(
await expect(socket.auth.doAuth(sign)).rejects.toThrow(
"Attempted to authenticate with no challenge",
)
})
@@ -122,7 +122,7 @@ describe("auth", () => {
socket.auth.challenge = "challenge123"
socket.auth.status = AuthStatus.PendingResponse
await expect(socket.auth.authenticate(sign)).rejects.toThrow(
await expect(socket.auth.doAuth(sign)).rejects.toThrow(
"Attempted to authenticate when auth is already auth:status:pending_response",
)
})
@@ -133,7 +133,7 @@ describe("auth", () => {
socket.auth.challenge = "challenge123"
socket.auth.status = AuthStatus.Requested
await socket.auth.authenticate(sign)
await socket.auth.doAuth(sign)
expect(socket.auth.status).toBe(AuthStatus.DeniedSignature)
})
@@ -151,7 +151,7 @@ describe("auth", () => {
return event
}
await socket.auth.authenticate(sign)
await socket.auth.doAuth(sign)
expect(socket.auth.request).toStrictEqual(event!.id)
expect(sendSpy).toHaveBeenCalledWith(["AUTH", event])
+20 -2
View File
@@ -1,5 +1,5 @@
import EventEmitter from "events"
import {on, call} from "@welshman/lib"
import {on, poll, call} from "@welshman/lib"
import {SignedEvent, StampedEvent} from "@welshman/util"
import {makeEvent, CLIENT_AUTH} from "@welshman/util"
import {isRelayAuth, isClientAuth, isRelayOk, RelayMessage} from "./message.js"
@@ -97,7 +97,7 @@ export class AuthState extends EventEmitter {
this.emit(AuthStateEvent.Status, status)
}
async authenticate(sign: (event: StampedEvent) => Promise<SignedEvent>) {
async doAuth(sign: (event: StampedEvent) => Promise<SignedEvent>) {
if (!this.challenge) {
throw new Error("Attempted to authenticate with no challenge")
}
@@ -119,6 +119,24 @@ export class AuthState extends EventEmitter {
}
}
async attemptAuth(sign: (event: StampedEvent) => Promise<SignedEvent>) {
this.socket.attemptToOpen()
await poll({
signal: AbortSignal.timeout(800),
condition: () => this.status === AuthStatus.Requested,
})
if (this.status === AuthStatus.Requested) {
await this.doAuth(sign)
}
await poll({
signal: AbortSignal.timeout(800),
condition: () => this.status !== AuthStatus.PendingResponse,
})
}
cleanup() {
this.removeAllListeners()
this._unsubscribers.forEach(call)
+1 -1
View File
@@ -14,6 +14,6 @@ export type NetContext = {
export const netContext: NetContext = {
pool: Pool.getSingleton(),
repository: Repository.getSingleton(),
isEventValid: (event, url) => Boolean(event.sig && verifyEvent(event as SignedEvent)),
isEventValid: (event, url) => verifyEvent(event),
isEventDeleted: (event, url) => netContext.repository.isDeleted(event),
}
+1 -1
View File
@@ -199,7 +199,7 @@ export const makeSocketPolicyAuth = (options: SocketPolicyAuthOptions) => (socke
const unsubscribers = [
on(socket.auth, AuthStateEvent.Status, (status: AuthStatus) => {
if (status === AuthStatus.Requested && shouldAuth(socket)) {
socket.auth.authenticate(options.sign)
socket.auth.doAuth(options.sign)
}
}),
]