test(service-worker): ensure `SwTestHarness#parseUrl()` behaves the same on browser and Node.js (#27080)
PR Close #27080
This commit is contained in:
parent
b3dda0ebc1
commit
415de9a291
|
@ -95,8 +95,13 @@ export class SwTestHarness implements ServiceWorkerGlobalScope, Adapter, Context
|
||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
static envIsSupported(): boolean {
|
static envIsSupported(): boolean {
|
||||||
return (typeof URL === 'function') ||
|
if (typeof URL === 'function') {
|
||||||
(typeof require === 'function' && typeof require('url')['parse'] === 'function');
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// In older Node.js versions, the `URL` global does not exist. We can use `url` instead.
|
||||||
|
const url = (typeof require === 'function') && require('url');
|
||||||
|
return url && (typeof url.parse === 'function') && (typeof url.resolve === 'function');
|
||||||
}
|
}
|
||||||
|
|
||||||
time: number;
|
time: number;
|
||||||
|
@ -176,13 +181,14 @@ export class SwTestHarness implements ServiceWorkerGlobalScope, Adapter, Context
|
||||||
}
|
}
|
||||||
|
|
||||||
parseUrl(url: string, relativeTo?: string): {origin: string, path: string} {
|
parseUrl(url: string, relativeTo?: string): {origin: string, path: string} {
|
||||||
if (typeof URL === 'function') {
|
const parsedUrl: URL = (typeof URL === 'function') ?
|
||||||
const obj = new URL(url, relativeTo);
|
new URL(url, relativeTo) :
|
||||||
return {origin: obj.origin, path: obj.pathname};
|
require('url').parse(require('url').resolve(relativeTo || '', url));
|
||||||
} else {
|
|
||||||
const obj = require('url').parse(url);
|
return {
|
||||||
return {origin: `${obj.protocol}//${obj.host}`, path: obj.pathname};
|
origin: parsedUrl.origin || `${parsedUrl.protocol}//${parsedUrl.host}`,
|
||||||
}
|
path: parsedUrl.pathname,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async skipWaiting(): Promise<void> { this.skippedWaiting = true; }
|
async skipWaiting(): Promise<void> { this.skippedWaiting = true; }
|
||||||
|
|
Loading…
Reference in New Issue