service-worker-mock
Advanced tools
Comparing version 1.5.0 to 1.6.0
26
index.js
@@ -1,3 +0,5 @@ | ||
const url = require('url'); | ||
const URL = require('dom-urls'); | ||
const Blob = require('./models/Blob'); | ||
const Body = require('./models/Body'); | ||
const Cache = require('./models/Cache'); | ||
@@ -7,3 +9,3 @@ const CacheStorage = require('./models/CacheStorage'); | ||
const Clients = require('./models/Clients'); | ||
const Event = require('./models/Event'); | ||
const ExtendableEvent = require('./models/ExtendableEvent'); | ||
const FetchEvent = require('./models/FetchEvent'); | ||
@@ -22,6 +24,11 @@ const Headers = require('./models/Headers'); | ||
const defaults = (envOptions) => Object.assign({ | ||
locationUrl: 'https://www.test.com' | ||
}, envOptions); | ||
class ServiceWorkerGlobalScope { | ||
constructor() { | ||
constructor(envOptions) { | ||
const options = defaults(envOptions); | ||
this.listeners = {}; | ||
this.location = { origin: '/' }; | ||
this.location = new URL(options.locationUrl, options.locationBase); | ||
this.skipWaiting = () => Promise.resolve(); | ||
@@ -33,5 +40,8 @@ this.caches = new CacheStorage(); | ||
// Constructors | ||
this.Blob = Blob; | ||
this.Body = Body; | ||
this.Cache = Cache; | ||
this.Client = Client; | ||
this.Event = Event; | ||
this.Event = ExtendableEvent; | ||
this.ExtendableEvent = ExtendableEvent; | ||
this.FetchEvent = FetchEvent; | ||
@@ -47,3 +57,3 @@ this.Headers = Headers; | ||
this.ServiceWorkerGlobalScope = ServiceWorkerGlobalScope; | ||
this.URL = url.URL || url.parse; | ||
this.URL = URL; | ||
@@ -79,4 +89,4 @@ // Instance variable to avoid issues with `this` | ||
module.exports = function makeServiceWorkerEnv() { | ||
return new ServiceWorkerGlobalScope(); | ||
module.exports = function makeServiceWorkerEnv(envOptions) { | ||
return new ServiceWorkerGlobalScope(envOptions); | ||
}; |
@@ -39,3 +39,6 @@ // https://developer.mozilla.org/en-US/docs/Web/API/Cache | ||
if (typeof request === 'string') { | ||
let relativeUrl = request; | ||
request = new Request(request); | ||
// Add relative url as well | ||
this.store.set(relativeUrl, { request, response }); | ||
} | ||
@@ -42,0 +45,0 @@ |
@@ -1,10 +0,13 @@ | ||
const Event = require('./Event'); | ||
const ExtendableEvent = require('./ExtendableEvent'); | ||
const Request = require('./Request'); | ||
class FetchEvent extends Event { | ||
constructor(args) { | ||
class FetchEvent extends ExtendableEvent { | ||
constructor(type, init) { | ||
super(); | ||
if (typeof args === 'string') { | ||
this.request = { url: args }; | ||
} else if (args && typeof args === 'object') { | ||
this.request = args; | ||
this.type = type; | ||
this.isReload = init.isReload || false; | ||
if (init.request instanceof Request) { | ||
this.request = init.request; | ||
} else if (typeof init.request === 'string') { | ||
this.request = Request(init.request); | ||
} | ||
@@ -11,0 +14,0 @@ } |
// stubs https://developer.mozilla.org/en-US/docs/Web/API/Headers | ||
class Headers { | ||
get() { | ||
return ''; | ||
} | ||
} | ||
class Headers extends Map {} | ||
Headers.Headers = (meta) => new Headers(meta); | ||
module.exports = Headers; |
@@ -1,5 +0,5 @@ | ||
const Event = require('./Event'); | ||
const ExtendableEvent = require('./ExtendableEvent'); | ||
// https://developer.mozilla.org/en-US/docs/Web/API/NotificationEvent | ||
class NotificationEvent extends Event { | ||
class NotificationEvent extends ExtendableEvent { | ||
constructor(args) { | ||
@@ -6,0 +6,0 @@ super(); |
@@ -1,4 +0,4 @@ | ||
const Event = require('./Event'); | ||
const ExtendableEvent = require('./ExtendableEvent'); | ||
class PushEvent extends Event { | ||
class PushEvent extends ExtendableEvent { | ||
constructor(args) { | ||
@@ -5,0 +5,0 @@ super(); |
// stubs https://developer.mozilla.org/en-US/docs/Web/API/Request | ||
const Body = require('./Body'); | ||
const Headers = require('./Headers'); | ||
const URL = require('dom-urls'); | ||
class Request { | ||
const DEFAULT_HEADERS = [ | ||
['accept', '*/*'] | ||
]; | ||
const throwBodyUsed = () => { | ||
throw new TypeError('Failed to execute \'clone\': body is already used'); | ||
}; | ||
class Request extends Body { | ||
constructor(url, options) { | ||
this.url = url || 'http://example.com/some.js'; | ||
super(options ? options.body : undefined, options); | ||
this.url = ((url instanceof URL) ? url : new URL(url, self.location.href)).href; | ||
this.method = (options && options.method) || 'GET'; | ||
this.mode = (options && options.mode) || 'same-origin'; // FF defaults to cors | ||
this.headers = (options && options.headers) || new Headers(); | ||
this.headers = (options && options.headers) || new Headers(DEFAULT_HEADERS); | ||
} | ||
clone() { | ||
if (this.bodyUsed) throwBodyUsed('json'); | ||
return new Request(this.url, { | ||
method: this.method, | ||
mode: this.mode | ||
mode: this.mode, | ||
headers: this.headers | ||
}); | ||
@@ -20,2 +34,4 @@ } | ||
Request.Request = (url, options) => new Request(url, options); | ||
module.exports = Request; |
@@ -10,3 +10,3 @@ // stubs https://developer.mozilla.org/en-US/docs/Web/API/Response | ||
this.type = 'basic'; | ||
this.type = this.status === 0 ? 'opaque' : 'basic'; | ||
this.redirected = false; | ||
@@ -13,0 +13,0 @@ this.url = 'http://example.com/asset'; |
{ | ||
"name": "service-worker-mock", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -1,2 +0,2 @@ | ||
const Event = require('../models/Event'); | ||
const ExtendableEvent = require('../models/ExtendableEvent'); | ||
const FetchEvent = require('../models/FetchEvent'); | ||
@@ -9,3 +9,3 @@ const NotificationEvent = require('../models/NotificationEvent'); | ||
case 'fetch': | ||
return new FetchEvent(args); | ||
return new FetchEvent('fetch', { request: args }); | ||
case 'notificationclick': | ||
@@ -16,3 +16,3 @@ return new NotificationEvent(args); | ||
default: | ||
return new Event(); | ||
return new ExtendableEvent(); | ||
} | ||
@@ -19,0 +19,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20187
23
514