@bytescale/sdk 3.60.0 → 3.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/dist/browser/cjs/main.js +508 -161
- package/dist/browser/esm/main.mjs +508 -161
- package/dist/node/cjs/main.js +351 -97
- package/dist/node/esm/main.mjs +351 -97
- package/dist/types/private/AuthSessionState.d.ts +2 -0
- package/dist/types/private/Scheduler.d.ts +12 -3
- package/dist/types/private/dtos/AuthSwConfigEntryDto.d.ts +3 -1
- package/dist/types/private/model/AuthSession.d.ts +4 -0
- package/dist/types/private/model/AuthSessionConfigBase.d.ts +8 -0
- package/dist/types/public/browser/AuthManagerBrowser.d.ts +3 -0
- package/dist/types/public/shared/generated/runtime.d.ts +6 -0
- package/dist/worker/cjs/main.js +351 -97
- package/dist/worker/esm/main.mjs +351 -97
- package/package.json +1 -1
- package/tests/ApiClientAuth.test.ts +115 -2
- package/tests/AuthManagerBrowser.test.ts +145 -2
- package/tests/AuthServiceWorkerRequestScope.test.ts +259 -0
- package/tests/AuthServiceWorkerRewrite.test.ts +17 -152
- package/tests/Scheduler.test.ts +135 -0
- package/tests/UploadManagerAuth.test.ts +30 -0
- package/tests/fixtures/auth-sw-3.61.0.js +272 -0
- package/tests/utils/AuthServiceWorkerHarness.ts +183 -0
package/README.md
CHANGED
|
@@ -432,6 +432,97 @@ With JWTs, the user can also perform API requests, such as file deletions, as th
|
|
|
432
432
|
|
|
433
433
|
[Learn more about the `AuthManager` and JWTs »](https://www.bytescale.com/docs/auth)
|
|
434
434
|
|
|
435
|
+
### Authenticate private URL aliases while keeping public CDN requests cacheable
|
|
436
|
+
|
|
437
|
+
Automatic (`AuthSessionConfigAuto`) and manual (`AuthSessionConfigManual`) configurations both accept
|
|
438
|
+
`requestUrlPrefixes?: string[]`. This restricts **service-worker authentication** by matching the original
|
|
439
|
+
`event.request.url`, before `urlRewriteRules` are applied. The rewritten destination must still match the
|
|
440
|
+
configuration's CDN URL and account, and the configuration must not have expired.
|
|
441
|
+
|
|
442
|
+
- Omit the field (or set it to `undefined`) to preserve existing behavior.
|
|
443
|
+
- Set it to `[]` to authenticate no requests through the service worker.
|
|
444
|
+
- Provide multiple prefixes to match any of them using case-sensitive `startsWith` comparisons.
|
|
445
|
+
|
|
446
|
+
Prefixes follow the same string-array validation as `sourceUrlPrefixes`: they are not parsed or normalized.
|
|
447
|
+
Use complete URL prefixes with a trailing `/` when restricting an origin or directory. An empty string matches
|
|
448
|
+
any URL. `sourceUrlPrefixes` independently restricts the initiating page or iframe URL; when both fields are
|
|
449
|
+
provided, both must match. Request prefixes alone do not require an initiating client, so new-tab downloads
|
|
450
|
+
can use them.
|
|
451
|
+
|
|
452
|
+
Serve the current Bytescale auth service worker as `/auth-sw.js` on your application's origin, then initialize
|
|
453
|
+
an automatic configuration:
|
|
454
|
+
|
|
455
|
+
```javascript
|
|
456
|
+
import { AuthManager } from "@bytescale/sdk";
|
|
457
|
+
|
|
458
|
+
await AuthManager.beginAuthSession({
|
|
459
|
+
serviceWorkerScript: "/auth-sw.js",
|
|
460
|
+
urlRewriteRules: [
|
|
461
|
+
{ fromUrlPrefix: "https://app.example.com/media-auth/", toUrlPrefix: "https://upcdn.io/" },
|
|
462
|
+
{ fromUrlPrefix: "https://app.example.com/download/", toUrlPrefix: "https://upcdn.io/" }
|
|
463
|
+
],
|
|
464
|
+
authConfigs: async () => [
|
|
465
|
+
{
|
|
466
|
+
accountId: "A123abc", // Replace with your Bytescale account ID.
|
|
467
|
+
authConfigId: undefined,
|
|
468
|
+
authUrl: "https://app.example.com/auth", // Your endpoint returning a JWT as text/plain.
|
|
469
|
+
authHeaders: async () => ({}),
|
|
470
|
+
requestUrlPrefixes: ["https://app.example.com/media-auth/", "https://app.example.com/download/"],
|
|
471
|
+
enableCookieAuth: false
|
|
472
|
+
}
|
|
473
|
+
]
|
|
474
|
+
});
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
For a manual configuration, replace `authUrl` and `authHeaders` with your `getAuthorizationToken` callback;
|
|
478
|
+
keep `requestUrlPrefixes` and the other fields:
|
|
479
|
+
|
|
480
|
+
```javascript
|
|
481
|
+
const manualConfig = {
|
|
482
|
+
accountId: "A123abc",
|
|
483
|
+
authConfigId: undefined,
|
|
484
|
+
requestUrlPrefixes: ["https://app.example.com/media-auth/", "https://app.example.com/download/"],
|
|
485
|
+
getAuthorizationToken: async () => {
|
|
486
|
+
const response = await fetch("/auth");
|
|
487
|
+
return await response.text();
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
With the example configuration:
|
|
493
|
+
|
|
494
|
+
| Original resource URL | Final destination | AuthManager-injected headers |
|
|
495
|
+
| ------------------------------------------------------------------------ | -------------------------------------------------------- | ---------------------------- |
|
|
496
|
+
| `https://upcdn.io/A123abc/image/example.jpg` | Unchanged | None |
|
|
497
|
+
| `https://app.example.com/media-auth/A123abc/image/example.jpg` | `https://upcdn.io/A123abc/image/example.jpg` | Account A123abc's JWT |
|
|
498
|
+
| `https://app.example.com/download/A123abc/raw/example.jpg?download=true` | `https://upcdn.io/A123abc/raw/example.jpg?download=true` | Account A123abc's JWT |
|
|
499
|
+
| `https://app.example.com/media-auth/B123abc/image/example.jpg` | `https://upcdn.io/B123abc/image/example.jpg` | None |
|
|
500
|
+
|
|
501
|
+
The download alias supports a normal link after initialization:
|
|
502
|
+
|
|
503
|
+
```html
|
|
504
|
+
<a href="https://app.example.com/download/A123abc/raw/example.jpg?download=true" target="_blank" rel="noopener">
|
|
505
|
+
Download
|
|
506
|
+
</a>
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
Rewriting uses the first matching rule once and continues even when authentication does not match. Responses
|
|
510
|
+
remain streamed, and range headers, query parameters, and navigation behavior are preserved. A rejected request
|
|
511
|
+
restriction allows the worker to consider later authentication entries. Existing rules preventing duplicate
|
|
512
|
+
service-worker destinations within an AuthManager session still apply.
|
|
513
|
+
|
|
514
|
+
This field does not change API-client authentication, cookie authentication, token acquisition, or refresh.
|
|
515
|
+
It does not remove caller-provided headers or existing CDN cookies. Leave cookie authentication disabled and
|
|
516
|
+
avoid other broad authentication configurations for CDN requests you want to keep unauthenticated.
|
|
517
|
+
|
|
518
|
+
**Compatibility and rollout:** deploy the auth worker shipped with SDK **3.62.0 or later** before enabling
|
|
519
|
+
`requestUrlPrefixes` in the application. Update any self-hosted worker copies and allow the updated worker to
|
|
520
|
+
activate in existing browser sessions. A distinct compatibility marker makes older workers skip the entire
|
|
521
|
+
request-restricted configuration, even when `sourceUrlPrefixes` is also present. They cannot silently turn it
|
|
522
|
+
into broad authentication. Private downloads may remain unauthenticated until the worker is updated; versions
|
|
523
|
+
without rewrite support also cannot serve aliases. The updated worker can recover these restrictions and
|
|
524
|
+
rewrite rules from persisted state after restart, and continues to support older configurations.
|
|
525
|
+
|
|
435
526
|
## UrlBuilder
|
|
436
527
|
|
|
437
528
|
Use the `UrlBuilder` to construct URLs for your uploaded files:
|