| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {assert} from 'workbox-core/_private/assert.js';
|
|---|
| 10 | import {MapLikeObject} from 'workbox-core/types.js';
|
|---|
| 11 | import '../_version.js';
|
|---|
| 12 |
|
|---|
| 13 | type SerializableProperties =
|
|---|
| 14 | | 'method'
|
|---|
| 15 | | 'referrer'
|
|---|
| 16 | | 'referrerPolicy'
|
|---|
| 17 | | 'mode'
|
|---|
| 18 | | 'credentials'
|
|---|
| 19 | | 'cache'
|
|---|
| 20 | | 'redirect'
|
|---|
| 21 | | 'integrity'
|
|---|
| 22 | | 'keepalive';
|
|---|
| 23 |
|
|---|
| 24 | const serializableProperties: SerializableProperties[] = [
|
|---|
| 25 | 'method',
|
|---|
| 26 | 'referrer',
|
|---|
| 27 | 'referrerPolicy',
|
|---|
| 28 | 'mode',
|
|---|
| 29 | 'credentials',
|
|---|
| 30 | 'cache',
|
|---|
| 31 | 'redirect',
|
|---|
| 32 | 'integrity',
|
|---|
| 33 | 'keepalive',
|
|---|
| 34 | ];
|
|---|
| 35 |
|
|---|
| 36 | export interface RequestData extends MapLikeObject {
|
|---|
| 37 | url: string;
|
|---|
| 38 | headers: MapLikeObject;
|
|---|
| 39 | body?: ArrayBuffer;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * A class to make it easier to serialize and de-serialize requests so they
|
|---|
| 44 | * can be stored in IndexedDB.
|
|---|
| 45 | *
|
|---|
| 46 | * Most developers will not need to access this class directly;
|
|---|
| 47 | * it is exposed for advanced use cases.
|
|---|
| 48 | */
|
|---|
| 49 | class StorableRequest {
|
|---|
| 50 | private readonly _requestData: RequestData;
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Converts a Request object to a plain object that can be structured
|
|---|
| 54 | * cloned or JSON-stringified.
|
|---|
| 55 | *
|
|---|
| 56 | * @param {Request} request
|
|---|
| 57 | * @return {Promise<StorableRequest>}
|
|---|
| 58 | */
|
|---|
| 59 | static async fromRequest(request: Request): Promise<StorableRequest> {
|
|---|
| 60 | const requestData: RequestData = {
|
|---|
| 61 | url: request.url,
|
|---|
| 62 | headers: {},
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | // Set the body if present.
|
|---|
| 66 | if (request.method !== 'GET') {
|
|---|
| 67 | // Use ArrayBuffer to support non-text request bodies.
|
|---|
| 68 | // NOTE: we can't use Blobs becuse Safari doesn't support storing
|
|---|
| 69 | // Blobs in IndexedDB in some cases:
|
|---|
| 70 | // https://github.com/dfahlander/Dexie.js/issues/618#issuecomment-398348457
|
|---|
| 71 | requestData.body = await request.clone().arrayBuffer();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | // Convert the headers from an iterable to an object.
|
|---|
| 75 | for (const [key, value] of request.headers.entries()) {
|
|---|
| 76 | requestData.headers[key] = value;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // Add all other serializable request properties
|
|---|
| 80 | for (const prop of serializableProperties) {
|
|---|
| 81 | if (request[prop] !== undefined) {
|
|---|
| 82 | requestData[prop] = request[prop];
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | return new StorableRequest(requestData);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Accepts an object of request data that can be used to construct a
|
|---|
| 91 | * `Request` but can also be stored in IndexedDB.
|
|---|
| 92 | *
|
|---|
| 93 | * @param {Object} requestData An object of request data that includes the
|
|---|
| 94 | * `url` plus any relevant properties of
|
|---|
| 95 | * [requestInit]{@link https://fetch.spec.whatwg.org/#requestinit}.
|
|---|
| 96 | */
|
|---|
| 97 | constructor(requestData: RequestData) {
|
|---|
| 98 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 99 | assert!.isType(requestData, 'object', {
|
|---|
| 100 | moduleName: 'workbox-background-sync',
|
|---|
| 101 | className: 'StorableRequest',
|
|---|
| 102 | funcName: 'constructor',
|
|---|
| 103 | paramName: 'requestData',
|
|---|
| 104 | });
|
|---|
| 105 | assert!.isType(requestData.url, 'string', {
|
|---|
| 106 | moduleName: 'workbox-background-sync',
|
|---|
| 107 | className: 'StorableRequest',
|
|---|
| 108 | funcName: 'constructor',
|
|---|
| 109 | paramName: 'requestData.url',
|
|---|
| 110 | });
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // If the request's mode is `navigate`, convert it to `same-origin` since
|
|---|
| 114 | // navigation requests can't be constructed via script.
|
|---|
| 115 | if (requestData['mode'] === 'navigate') {
|
|---|
| 116 | requestData['mode'] = 'same-origin';
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | this._requestData = requestData;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Returns a deep clone of the instances `_requestData` object.
|
|---|
| 124 | *
|
|---|
| 125 | * @return {Object}
|
|---|
| 126 | */
|
|---|
| 127 | toObject(): RequestData {
|
|---|
| 128 | const requestData = Object.assign({}, this._requestData);
|
|---|
| 129 | requestData.headers = Object.assign({}, this._requestData.headers);
|
|---|
| 130 | if (requestData.body) {
|
|---|
| 131 | requestData.body = requestData.body.slice(0);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | return requestData;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Converts this instance to a Request.
|
|---|
| 139 | *
|
|---|
| 140 | * @return {Request}
|
|---|
| 141 | */
|
|---|
| 142 | toRequest(): Request {
|
|---|
| 143 | return new Request(this._requestData.url, this._requestData);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Creates and returns a deep clone of the instance.
|
|---|
| 148 | *
|
|---|
| 149 | * @return {StorableRequest}
|
|---|
| 150 | */
|
|---|
| 151 | clone(): StorableRequest {
|
|---|
| 152 | return new StorableRequest(this.toObject());
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | export {StorableRequest};
|
|---|