2017-09-28 19:18:12 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-09-28 19:18:12 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Subject} from 'rxjs';
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
import {Adapter, Context} from '../src/adapter';
|
|
|
|
import {AssetGroupConfig, Manifest} from '../src/manifest';
|
|
|
|
import {sha1} from '../src/sha1';
|
|
|
|
|
|
|
|
import {MockCacheStorage} from './cache';
|
|
|
|
import {MockHeaders, MockRequest, MockResponse} from './fetch';
|
|
|
|
import {MockServerState, MockServerStateBuilder} from './mock';
|
2020-07-06 09:55:37 -04:00
|
|
|
import {normalizeUrl, parseUrl} from './utils';
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
const EMPTY_SERVER_STATE = new MockServerStateBuilder().build();
|
|
|
|
|
|
|
|
export class MockClient {
|
|
|
|
queue = new Subject<Object>();
|
|
|
|
|
|
|
|
constructor(readonly id: string) {}
|
|
|
|
|
|
|
|
readonly messages: Object[] = [];
|
|
|
|
|
|
|
|
postMessage(message: Object): void {
|
|
|
|
this.messages.push(message);
|
|
|
|
this.queue.next(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SwTestHarnessBuilder {
|
2020-07-06 09:55:37 -04:00
|
|
|
private origin = parseUrl(this.scopeUrl).origin;
|
2017-09-28 19:18:12 -04:00
|
|
|
private server = EMPTY_SERVER_STATE;
|
2019-03-20 17:29:16 -04:00
|
|
|
private caches = new MockCacheStorage(this.origin);
|
|
|
|
|
2020-07-06 09:55:37 -04:00
|
|
|
constructor(private scopeUrl = 'http://localhost/') {}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
withCacheState(cache: string): SwTestHarnessBuilder {
|
2019-03-20 17:29:16 -04:00
|
|
|
this.caches = new MockCacheStorage(this.origin, cache);
|
2017-09-28 19:18:12 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
withServerState(state: MockServerState): SwTestHarnessBuilder {
|
|
|
|
this.server = state;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
build(): SwTestHarness {
|
2020-07-06 09:55:37 -04:00
|
|
|
return new SwTestHarness(this.server, this.caches, this.scopeUrl);
|
2020-04-13 19:40:21 -04:00
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MockClients implements Clients {
|
|
|
|
private clients = new Map<string, MockClient>();
|
|
|
|
|
|
|
|
add(clientId: string): void {
|
|
|
|
if (this.clients.has(clientId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.clients.set(clientId, new MockClient(clientId));
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
remove(clientId: string): void {
|
|
|
|
this.clients.delete(clientId);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
async get(id: string): Promise<Client> {
|
|
|
|
return this.clients.get(id)! as any as Client;
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
getMock(id: string): MockClient|undefined {
|
|
|
|
return this.clients.get(id);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
async matchAll(): Promise<Client[]> {
|
|
|
|
return Array.from(this.clients.values()) as any[] as Client[];
|
|
|
|
}
|
|
|
|
|
|
|
|
async claim(): Promise<any> {}
|
|
|
|
}
|
|
|
|
|
2020-07-06 09:55:36 -04:00
|
|
|
export class SwTestHarness extends Adapter implements ServiceWorkerGlobalScope, Context {
|
2017-09-28 19:18:12 -04:00
|
|
|
readonly clients = new MockClients();
|
|
|
|
private eventHandlers = new Map<string, Function>();
|
|
|
|
private skippedWaiting = true;
|
|
|
|
|
|
|
|
private selfMessageQueue: any[] = [];
|
2018-10-09 07:54:15 -04:00
|
|
|
autoAdvanceTime = false;
|
2018-06-18 19:38:33 -04:00
|
|
|
// TODO(issue/24571): remove '!'.
|
2020-04-13 19:40:21 -04:00
|
|
|
unregistered!: boolean;
|
2017-09-28 19:18:12 -04:00
|
|
|
readonly notifications: {title: string, options: Object}[] = [];
|
|
|
|
readonly registration: ServiceWorkerRegistration = {
|
|
|
|
active: {
|
2020-04-13 19:40:21 -04:00
|
|
|
postMessage: (msg: any) => {
|
|
|
|
this.selfMessageQueue.push(msg);
|
|
|
|
},
|
2017-09-28 19:18:12 -04:00
|
|
|
},
|
2020-07-06 09:55:36 -04:00
|
|
|
scope: this.scopeUrl,
|
2020-04-13 19:40:21 -04:00
|
|
|
showNotification:
|
|
|
|
(title: string, options: Object) => {
|
|
|
|
this.notifications.push({title, options});
|
|
|
|
},
|
|
|
|
unregister:
|
|
|
|
() => {
|
|
|
|
this.unregistered = true;
|
|
|
|
},
|
2017-09-28 19:18:12 -04:00
|
|
|
} as any;
|
|
|
|
|
|
|
|
static envIsSupported(): boolean {
|
2019-03-20 17:29:14 -04:00
|
|
|
if (typeof URL === 'function') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:52:11 -04:00
|
|
|
// If we're in a browser that doesn't support URL at this point, don't go any further
|
|
|
|
// since browser builds use requirejs which will fail on the `require` call below.
|
|
|
|
if (typeof window !== 'undefined' && window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:29:14 -04:00
|
|
|
// 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');
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:55:36 -04:00
|
|
|
get time() {
|
|
|
|
return this.mockTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
private mockTime = Date.now();
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
private timers: {
|
|
|
|
at: number,
|
|
|
|
duration: number,
|
|
|
|
fn: Function,
|
|
|
|
fired: boolean,
|
|
|
|
}[] = [];
|
|
|
|
|
2020-07-06 09:55:37 -04:00
|
|
|
parseUrl = parseUrl;
|
|
|
|
|
2019-03-20 17:29:16 -04:00
|
|
|
constructor(
|
2020-07-06 09:55:36 -04:00
|
|
|
private server: MockServerState, readonly caches: MockCacheStorage, scopeUrl: string) {
|
|
|
|
super(scopeUrl);
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async resolveSelfMessages(): Promise<void> {
|
|
|
|
while (this.selfMessageQueue.length > 0) {
|
|
|
|
const queue = this.selfMessageQueue;
|
|
|
|
this.selfMessageQueue = [];
|
2020-04-13 19:40:21 -04:00
|
|
|
await queue.reduce(async (previous, msg) => {
|
2017-09-28 19:18:12 -04:00
|
|
|
await previous;
|
|
|
|
await this.handleMessage(msg, null);
|
|
|
|
}, Promise.resolve());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async startup(firstTime: boolean = false): Promise<boolean|null> {
|
|
|
|
if (!firstTime) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let skippedWaiting: boolean = false;
|
|
|
|
if (this.eventHandlers.has('install')) {
|
|
|
|
const installEvent = new MockInstallEvent();
|
2020-04-13 19:40:21 -04:00
|
|
|
this.eventHandlers.get('install')!(installEvent);
|
2017-09-28 19:18:12 -04:00
|
|
|
await installEvent.ready;
|
|
|
|
skippedWaiting = this.skippedWaiting;
|
|
|
|
}
|
|
|
|
if (this.eventHandlers.has('activate')) {
|
|
|
|
const activateEvent = new MockActivateEvent();
|
2020-04-13 19:40:21 -04:00
|
|
|
this.eventHandlers.get('activate')!(activateEvent);
|
2017-09-28 19:18:12 -04:00
|
|
|
await activateEvent.ready;
|
|
|
|
}
|
|
|
|
return skippedWaiting;
|
|
|
|
}
|
2020-07-06 09:55:36 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
updateServerState(server?: MockServerState): void {
|
|
|
|
this.server = server || EMPTY_SERVER_STATE;
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2020-07-06 09:55:37 -04:00
|
|
|
fetch(req: RequestInfo): Promise<Response> {
|
2017-09-28 19:18:12 -04:00
|
|
|
if (typeof req === 'string') {
|
2020-07-06 09:55:37 -04:00
|
|
|
return this.server.fetch(new MockRequest(normalizeUrl(req, this.scopeUrl)));
|
2017-09-28 19:18:12 -04:00
|
|
|
} else {
|
2017-10-02 18:59:57 -04:00
|
|
|
const mockReq = req.clone() as MockRequest;
|
2020-07-06 09:55:37 -04:00
|
|
|
mockReq.url = normalizeUrl(mockReq.url, this.scopeUrl);
|
2017-10-02 18:59:57 -04:00
|
|
|
return this.server.fetch(mockReq);
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener(event: string, handler: Function): void {
|
|
|
|
this.eventHandlers.set(event, handler);
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
removeEventListener(event: string, handler?: Function): void {
|
|
|
|
this.eventHandlers.delete(event);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
newRequest(url: string, init: Object = {}): Request {
|
2020-07-06 09:55:37 -04:00
|
|
|
return new MockRequest(normalizeUrl(url, this.scopeUrl), init);
|
2020-04-13 19:40:21 -04:00
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
newResponse(body: string, init: Object = {}): Response {
|
|
|
|
return new MockResponse(body, init);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
newHeaders(headers: {[name: string]: string}): Headers {
|
|
|
|
return Object.keys(headers).reduce((mock, name) => {
|
|
|
|
mock.set(name, headers[name]);
|
|
|
|
return mock;
|
|
|
|
}, new MockHeaders());
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
async skipWaiting(): Promise<void> {
|
|
|
|
this.skippedWaiting = true;
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
waitUntil(promise: Promise<void>): void {}
|
|
|
|
|
2018-04-30 18:19:31 -04:00
|
|
|
handleFetch(req: Request, clientId: string|null = null):
|
|
|
|
[Promise<Response|undefined>, Promise<void>] {
|
2017-09-28 19:18:12 -04:00
|
|
|
if (!this.eventHandlers.has('fetch')) {
|
|
|
|
throw new Error('No fetch handler registered');
|
|
|
|
}
|
2018-04-30 18:19:31 -04:00
|
|
|
const event = new MockFetchEvent(req, clientId);
|
2020-04-13 19:40:21 -04:00
|
|
|
this.eventHandlers.get('fetch')!.call(this, event);
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2018-04-30 18:12:31 -04:00
|
|
|
if (clientId) {
|
|
|
|
this.clients.add(clientId);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:18:12 -04:00
|
|
|
return [event.response, event.ready];
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage(data: Object, clientId: string|null): Promise<void> {
|
|
|
|
if (!this.eventHandlers.has('message')) {
|
|
|
|
throw new Error('No message handler registered');
|
|
|
|
}
|
|
|
|
let event: MockMessageEvent;
|
|
|
|
if (clientId === null) {
|
|
|
|
event = new MockMessageEvent(data, null);
|
|
|
|
} else {
|
|
|
|
this.clients.add(clientId);
|
2018-04-30 18:19:31 -04:00
|
|
|
event = new MockMessageEvent(data, this.clients.getMock(clientId) || null);
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
2020-04-13 19:40:21 -04:00
|
|
|
this.eventHandlers.get('message')!.call(this, event);
|
2017-09-28 19:18:12 -04:00
|
|
|
return event.ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePush(data: Object): Promise<void> {
|
|
|
|
if (!this.eventHandlers.has('push')) {
|
|
|
|
throw new Error('No push handler registered');
|
|
|
|
}
|
|
|
|
const event = new MockPushEvent(data);
|
2020-04-13 19:40:21 -04:00
|
|
|
this.eventHandlers.get('push')!.call(this, event);
|
2017-09-28 19:18:12 -04:00
|
|
|
return event.ready;
|
|
|
|
}
|
|
|
|
|
2018-09-07 08:56:40 -04:00
|
|
|
handleClick(notification: Object, action?: string): Promise<void> {
|
|
|
|
if (!this.eventHandlers.has('notificationclick')) {
|
|
|
|
throw new Error('No notificationclick handler registered');
|
|
|
|
}
|
2018-09-21 05:11:06 -04:00
|
|
|
const event = new MockNotificationEvent(notification, action);
|
2020-04-13 19:40:21 -04:00
|
|
|
this.eventHandlers.get('notificationclick')!.call(this, event);
|
2018-09-07 08:56:40 -04:00
|
|
|
return event.ready;
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:18:12 -04:00
|
|
|
timeout(ms: number): Promise<void> {
|
2018-10-09 07:54:15 -04:00
|
|
|
const promise = new Promise<void>(resolve => {
|
2017-09-28 19:18:12 -04:00
|
|
|
this.timers.push({
|
2020-07-06 09:55:36 -04:00
|
|
|
at: this.mockTime + ms,
|
2017-09-28 19:18:12 -04:00
|
|
|
duration: ms,
|
|
|
|
fn: resolve,
|
|
|
|
fired: false,
|
|
|
|
});
|
|
|
|
});
|
2018-10-09 07:54:15 -04:00
|
|
|
|
|
|
|
if (this.autoAdvanceTime) {
|
|
|
|
this.advance(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
advance(by: number): void {
|
2020-07-06 09:55:36 -04:00
|
|
|
this.mockTime += by;
|
2017-09-28 19:18:12 -04:00
|
|
|
this.timers.filter(timer => !timer.fired)
|
2020-07-06 09:55:36 -04:00
|
|
|
.filter(timer => timer.at <= this.mockTime)
|
2017-09-28 19:18:12 -04:00
|
|
|
.forEach(timer => {
|
|
|
|
timer.fired = true;
|
|
|
|
timer.fn();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
isClient(obj: any): obj is Client {
|
|
|
|
return obj instanceof MockClient;
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
interface StaticFile {
|
|
|
|
url: string;
|
|
|
|
contents: string;
|
|
|
|
hash?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AssetGroupBuilder {
|
|
|
|
constructor(private up: ConfigBuilder, readonly name: string) {}
|
|
|
|
|
|
|
|
private files: StaticFile[] = [];
|
|
|
|
|
|
|
|
addFile(url: string, contents: string, hashed: boolean = true): AssetGroupBuilder {
|
|
|
|
const file: StaticFile = {url, contents, hash: undefined};
|
|
|
|
if (hashed) {
|
|
|
|
file.hash = sha1(contents);
|
|
|
|
}
|
|
|
|
this.files.push(file);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
finish(): ConfigBuilder {
|
|
|
|
return this.up;
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
toManifestGroup(): AssetGroupConfig {
|
|
|
|
return null!;
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ConfigBuilder {
|
|
|
|
assetGroups = new Map<string, AssetGroupBuilder>();
|
|
|
|
|
|
|
|
addAssetGroup(name: string): ConfigBuilder {
|
|
|
|
const builder = new AssetGroupBuilder(this, name);
|
|
|
|
this.assetGroups.set(name, builder);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish(): Manifest {
|
|
|
|
const assetGroups = Array.from(this.assetGroups.values()).map(group => group.toManifestGroup());
|
|
|
|
const hashTable = {};
|
|
|
|
return {
|
|
|
|
configVersion: 1,
|
2019-03-05 08:24:07 -05:00
|
|
|
timestamp: 1234567890123,
|
2020-04-13 19:40:21 -04:00
|
|
|
index: '/index.html',
|
|
|
|
assetGroups,
|
|
|
|
navigationUrls: [],
|
2020-08-24 10:04:17 -04:00
|
|
|
navigationRequestStrategy: 'performance',
|
2020-04-13 19:40:21 -04:00
|
|
|
hashTable,
|
2017-09-28 19:18:12 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class OneTimeContext implements Context {
|
|
|
|
private queue: Promise<void>[] = [];
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
waitUntil(promise: Promise<void>): void {
|
|
|
|
this.queue.push(promise);
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
get ready(): Promise<void> {
|
2020-04-13 19:40:21 -04:00
|
|
|
return (async () => {
|
2017-09-28 19:18:12 -04:00
|
|
|
while (this.queue.length > 0) {
|
|
|
|
await this.queue.shift();
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockExtendableEvent extends OneTimeContext {}
|
|
|
|
|
|
|
|
class MockFetchEvent extends MockExtendableEvent {
|
|
|
|
response: Promise<Response|undefined> = Promise.resolve(undefined);
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor(readonly request: Request, readonly clientId: string|null) {
|
|
|
|
super();
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
respondWith(promise: Promise<Response>): Promise<Response> {
|
|
|
|
this.response = promise;
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockMessageEvent extends MockExtendableEvent {
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor(readonly data: Object, readonly source: MockClient|null) {
|
|
|
|
super();
|
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockPushEvent extends MockExtendableEvent {
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor(private _data: Object) {
|
|
|
|
super();
|
|
|
|
}
|
2017-10-10 15:57:50 -04:00
|
|
|
data = {
|
|
|
|
json: () => this._data,
|
|
|
|
};
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
2020-07-06 09:55:37 -04:00
|
|
|
|
2018-09-21 05:11:06 -04:00
|
|
|
class MockNotificationEvent extends MockExtendableEvent {
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor(private _notification: any, readonly action?: string) {
|
|
|
|
super();
|
|
|
|
}
|
2018-09-28 03:43:24 -04:00
|
|
|
readonly notification = {...this._notification, close: () => undefined};
|
2018-09-07 08:56:40 -04:00
|
|
|
}
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
class MockInstallEvent extends MockExtendableEvent {}
|
|
|
|
|
|
|
|
class MockActivateEvent extends MockExtendableEvent {}
|