Skip to content

Commit 41b9e23

Browse files
committed
feat(auth): export RefreshWorker asset and support same-origin worker URL override
1 parent d4078aa commit 41b9e23

4 files changed

Lines changed: 71 additions & 2 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ import {
113113
} from 'solid-logic';
114114
```
115115

116+
## Worker Asset and Runtime Configuration
117+
118+
solid-logic publishes the OIDC refresh worker as a package export so host apps can serve it from the same origin.
119+
120+
### Worker export
121+
122+
- Package export: `solid-logic/RefreshWorker`
123+
- Built file: `dist/RefreshWorker.js`
124+
125+
Host applications should copy or serve this file so the browser can load it with a same-origin URL.
126+
127+
### Worker URL override
128+
129+
Before solid-logic initializes, applications can set:
130+
131+
```js
132+
window.__SOLID_LOGIC_WORKER_URL__ = 'https://app.example.com/RefreshWorker.js'
133+
```
134+
135+
If this override is not set, solid-logic resolves the worker URL to `./RefreshWorker.js` against `window.location.href`.
136+
137+
### Session fallback behavior
138+
139+
When worker session initialization fails, solid-logic falls back in this order:
140+
141+
1. `SessionCore` with IndexedDB-backed session database
142+
2. `SessionCore` with in-memory session database
143+
116144
# How to develop
117145

118146
Check the scripts in the `package.json` for build, watch, lint and test.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"import": "./dist/solid-logic.esm.js",
1212
"require": "./dist/solid-logic.js",
1313
"types": "./dist/index.d.ts"
14+
},
15+
"./RefreshWorker": {
16+
"import": "./dist/RefreshWorker.js",
17+
"default": "./dist/RefreshWorker.js"
1418
}
1519
},
1620
"sideEffects": false,
@@ -21,10 +25,11 @@
2125
],
2226
"scripts": {
2327
"clean": "rm -rf dist src/versionInfo.ts",
24-
"build": "npm run clean && npm run typecheck && npm run build-version && npm run build-js && npm run build-dist && npm run postbuild-js",
28+
"build": "npm run clean && npm run typecheck && npm run build-version && npm run build-js && npm run build-dist && npm run copy-worker && npm run postbuild-js",
2529
"build-version": "./timestamp.sh > src/versionInfo.ts && eslint 'src/versionInfo.ts' --fix",
2630
"build-js": "tsc",
2731
"build-dist": "webpack --progress",
32+
"copy-worker": "node -e \"const fs=require('fs');const path=require('path');const src=path.resolve('node_modules/@uvdsl/solid-oidc-client-browser/dist/esm/web/RefreshWorker.js');const dst=path.resolve('dist/RefreshWorker.js');fs.copyFileSync(src,dst);\"",
2833
"postbuild-js": "rm -f dist/versionInfo.d.ts dist/versionInfo.d.ts.map",
2934
"lint": "eslint",
3035
"lint-fix": "eslint --fix",

src/authSession/authSession.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,31 @@ class IndexedDbSessionDatabase implements SessionDatabase {
147147
}
148148
}
149149

150+
function resolveWorkerUrl (): string | URL | undefined {
151+
if (typeof window === 'undefined') return undefined
152+
153+
const explicitWorkerUrl = (window as any).__SOLID_LOGIC_WORKER_URL__
154+
if (typeof explicitWorkerUrl === 'string' && explicitWorkerUrl.trim().length > 0) {
155+
return explicitWorkerUrl
156+
}
157+
if (explicitWorkerUrl instanceof URL) {
158+
return explicitWorkerUrl
159+
}
160+
161+
try {
162+
// Default to same-origin sibling asset next to the current page URL.
163+
return new URL('./RefreshWorker.js', window.location.href).toString()
164+
} catch {
165+
return undefined
166+
}
167+
}
168+
150169
function createSession (): OidcSession {
151170
try {
152-
return new WebSession()
171+
const workerUrl = resolveWorkerUrl()
172+
return workerUrl
173+
? new WebSession(undefined, { workerUrl })
174+
: new WebSession()
153175
} catch (error) {
154176
// In some deployments, worker URL resolution can become file:// and fail cross-origin.
155177
// Fall back to SessionCore so auth still works without background refresh worker.

test/packageExports.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { readFileSync } from 'node:fs'
2+
import { resolve } from 'node:path'
3+
4+
describe('package exports', () => {
5+
it('exports RefreshWorker.js from dist', () => {
6+
const packageJsonPath = resolve(__dirname, '..', 'package.json')
7+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'))
8+
9+
expect(packageJson.exports['./RefreshWorker']).toEqual({
10+
import: './dist/RefreshWorker.js',
11+
default: './dist/RefreshWorker.js',
12+
})
13+
})
14+
})

0 commit comments

Comments
 (0)