FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Bind Session.authFetch so it's safe to pass by reference by melvincarvalho · Pull Request #12 · JavaScriptSolidServer/solid-oidc · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .html  (1) .js  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions solid-oidc.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ export class Session extends EventTarget {
this._idpDetails = null
this._refreshPromise = null

// Bind so authFetch survives being passed by reference (drop-in fetch):
// consumers hand it to rdflib / solid-logic / rdf-dereference as a bare
// `fetch`, which would otherwise lose `this`.
this.authFetch = this.authFetch.bind(this)

// Set up event listeners
if (this.onStateChange) {
this.addEventListener(SessionEvents.STATE_CHANGE, this.onStateChange)
Expand Down
17 changes: 17 additions & 0 deletions test.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ <h1>solid-oidc tests</h1>
}
})

asyncTest('Session: authFetch survives being passed by reference (bound)', async () => {
const session = new Session()
// Bare reference, the way rdflib / solid-logic / rdf-dereference take it.
// Before binding this throws "Cannot read properties of undefined (reading '_isActive')".
const { authFetch } = session
const origFetch = globalThis.fetch
globalThis.fetch = async () => new Response(null, { status: 200 })
try {
await authFetch('https://example.com', { method: 'HEAD' })
assert(true, 'authFetch is callable when passed by reference')
} catch (e) {
assert(false, `authFetch lost its binding when passed by reference: ${e.message}`)
} finally {
globalThis.fetch = origFetch
}
})

// ==========================================================================
// Summary
// ==========================================================================
Expand Down

Back | FazBrowse Home | New Git URL