Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dsw

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dsw - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

sandbox/images/icon-128.png

96

dist/dsw.js

@@ -44,3 +44,4 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

var DSWManager = void 0,
PWASettings = void 0;
PWASettings = void 0,
goFetch = void 0;

@@ -55,5 +56,6 @@ // finds the real size of an utf-8 string

var cacheManager = {
setup: function setup(DSWMan, PWASet) {
setup: function setup(DSWMan, PWASet, ftch) {
PWASettings = PWASet;
DSWManager = DSWMan;
goFetch = ftch;
DEFAULT_CACHE_VERSION = PWASettings.dswVersion || '1';

@@ -93,3 +95,3 @@ _indexeddbManager2.default.setup(cacheManager);

var cloned = response.clone();
return caches.open(cacheManager.mountCacheId(rule)).then(function (cache) {
return caches.open(typeof rule == 'string' ? rule : cacheManager.mountCacheId(rule)).then(function (cache) {
cache.put(request, cloned);

@@ -143,2 +145,3 @@ return response;

if (response && response.status == 200) {
// with success or not(saving it), we resolve it
var done = function done(_) {

@@ -149,21 +152,22 @@ resolve(response);

// store it in the indexedDB
_indexeddbManager2.default.save(rule.name, response.clone()).then(done).catch(done); // if failed saving, we still have the reponse to deliver
_indexeddbManager2.default.save(rule.name, response.clone(), request, rule).then(done).catch(done); // if failed saving, we still have the reponse to deliver
} else {
debugger;
// TODO: treat the not found requests
}
// if it failed, we can look for a fallback
url = request.url;
pathName = new URL(url).pathname;
return DSWManager.treatBadPage(response, pathName, event);
}
}
// let's look for it in our cache, and then in the database
// (we use the cache, just so we can user)
_indexeddbManager2.default.get(rule.name, request).then(function (result) {
debugger;
// if we did have it in the indexedDB
if (result) {
// we use it
console.log('found something');
// TODO: use it
return treatFetch(result);
} else {
// if it was not stored, let's fetch it
// fetching
request = DSWManager.createRequest(request, event, matching);
result = fetch(request, opts).then(treatFetch).catch(treatFetch);
return fetch(request, opts).then(treatFetch).catch(treatFetch);
}

@@ -360,2 +364,3 @@ });

var DEFAULT_DB_NAME = 'defaultDSWDB';
var INDEXEDDB_REQ_IDS = 'indexeddb-id-request';
var dbs = {};

@@ -402,6 +407,6 @@ var cacheManager;

if (config.indexes) {
baseData.keyPath = config.indexes;
if (config.key) {
baseData.keyPath = config.key;
}
if (!config.indexes || config.autoIncrement) {
if (!config.key || config.autoIncrement) {
baseData.autoIncrement = true;

@@ -420,4 +425,18 @@ }

} else if (event.oldVersion === 0) {
// if it is the first time it is creating it
db.createObjectStore(config.name, baseData);
(function () {
// if it is the first time it is creating it
var objectStore = db.createObjectStore(config.name, baseData);
// in case there are indexes defined, we create them
if (config.indexes) {
config.indexes.forEach(function (index) {
if (typeof index == 'string') {
objectStore.createIndex(index, index, {});
} else {
objectStore.createIndex(index.name, index.path || index.name, index.options);
}
});
}
// we will also make the key, an index
objectStore.createIndex(config.key, config.key, { unique: true });
})();
}

@@ -437,8 +456,31 @@

var store = getObjectStore(dbName);
// TODO: look for cached keys, then find them in the db
caches.match(request).then(function (result) {});
resolve();
caches.match(request).then(function (result) {
if (result) {
result.json().then(function (obj) {
// if the request was in cache, we now have got
// the id=value for the indexes(keys) to look for,
// in the indexedDB!
var store = getObjectStore(dbName),
index = store.index(obj.key),
getter = index.get(obj.value);
// in case we did get the content from indexedDB
// let's create a new Response out of it!
getter.onsuccess = function (event) {
resolve(new Response(JSON.stringify(event.target.result), {
headers: { 'Content-Type': 'application/json' }
}));
};
getter.onerror = function (event) {
// if we did not find it (or faced a problem) in
// indexeddb, we leave it to the network
resolve();
};
});
} else {
resolve();
}
});
});
},
save: function save(dbName, data) {
save: function save(dbName, data, request, rule) {
return new Promise(function (resolve, reject) {

@@ -454,2 +496,10 @@

req.onsuccess = function () {
var tmp = {};
var key = rule.action.indexedDB.key || 'id';
tmp.key = key;
tmp.value = obj[key];
cacheManager.put(INDEXEDDB_REQ_IDS, request, new Response(JSON.stringify(tmp), {
headers: { 'Content-Type': 'application/json' }
}));
resolve();

@@ -464,4 +514,2 @@ };

});
console.log(dbName, data);
});

@@ -561,3 +609,3 @@ }

// current referencies
_cacheManager2.default.setup(DSWManager, PWASettings);
_cacheManager2.default.setup(DSWManager, PWASettings, _goFetch2.default);
_strategies2.default.setup(DSWManager, _cacheManager2.default, _goFetch2.default);

@@ -564,0 +612,0 @@

@@ -150,3 +150,37 @@ {

}
}
},
// Let's use IndexedDB to store some data
"userData": {
"match": { "path": "\/api\/user\/.*" },
"options": { "credentials": "same-origin"},
// We will try to keep it up to date.
// DSW will look for it online, and if not possible, then look in the
// cached object in IndexedDB.
"strategy": "offline-first",
"apply": {
"indexedDB": {
// The IndexedDB name
"name": "userData",
// The version of it. If you change it, your db will be updated.
"version": "3",
// The _json_ data we are expecting in this example gives us
// and id, and we will use it as our kay for the IndexedDB structure.
"key": "id",
// but we will also define some indexes
"indexes": [
// one of the indexes is the property "name"
"name",
// the other index is the property "twitter"...
{
"name": "twitter",
"path": "twitter",
// ...but for this one, we want to specify that it is unique.
"options": {
"unique": true
}
}
]
}
}
},
}

@@ -153,0 +187,0 @@ }

{
"name": "dsw",
"version": "1.4.1",
"version": "1.5.0",
"description": "Dynamic Service Worker, offline Progressive Web Apps much easier",

@@ -5,0 +5,0 @@ "bin": {

@@ -18,3 +18,3 @@ # Dynamic Service Worker

You can then go offline and reload the page to validate it.
[Dynamic Service Worker demo](https://dsw-demo-rwnwbdjrxg.now.sh/)
[Dynamic Service Worker demo](https://dsw-demo-jghldvhxos.now.sh)

@@ -161,3 +161,3 @@ ## Advantages

#### Cache information
#### Cache

@@ -172,8 +172,59 @@ DSW will treat the cache layer for you.

You can also define `cache: false`. This will force the request **not to be cached**.
Seens silly, but is useful when you want an exception for your cached data.
#### IndexedDB
Some times, you will request a _JSON_ and IndexedDB is the best way to store it.
To do so, you will use the `indexedDB` action in your `apply` rule.
Pass an object containing the following:
- name: The name of your IndexedDB
- version(optional): The version of your IndexedDB structure
- key: The name of the key, for the indexed data
- indexes: An array with everything you want to use as index.
Indexes may be a _String_ or an object containing:
- path: The path where to find the index in your object
- name(optional): The name of the index (if not sent, path will be used as the name)
- options(optional): Any options you want to set to your index (like `unique` or `multiEntry`)
For example:
```js
"apply": {
"indexedDB": {
"name": "userData",
"version": "3",
"key": "id",
"indexes": [
"age",
{
"name": "twitter",
"path": "twitter",
"options": {
"unique": true
}
}
]
}
}
```
In this example, we will have three indexes: age, twitter and id (created automatically as it is the key).
If you **DO NOT** want to cache your json stored in IndexedDB, set `cache: false` in your rule/apply configuration.
**How it works**
You may be wondering how it caches your data.
Well, it uses the `cacheApi` to store as requests, only your keys. When you try to use it, it will use these ids to find the stored data you want, in your indexedDB.
This way, you can access the information in your IndexedDB by yourself, while your requests will automatically deal with it, too.
# Examples
Using both `match` and `apply`, we can for do a lot of things.<br/>
Using both `match` and `apply`, we can do a lot of things.<br/>
Don't forget to re-run `dsw path-to-project` whenever you made a change to your `dswfile.js` file.

@@ -347,4 +398,55 @@

#### Use it programatically
#### Sending credentials
In case you want to send credentials or other settings to fetch, you can use the `options` property.
```js
{
"dswVersion": 2.2,
"dswRules": {
"userData": {
"match": { "path": "\/api\/user\/.*" },
"options": { "credentials": "same-origin"},
"apply": {
// apply somethig
}
}
}
```
#### Sending credentials
In case you want to send credentials or other settings to fetch, you can use the `options` property.
```js
{
"dswVersion": 2.2,
"dswRules": {
"userData": {
"match": { "path": "\/api\/user\/.*" },
"options": { "credentials": "same-origin"},
"strategy": "online-first",
"apply": {
"indexedDB": {
"name": "userData",
"version": "3",
"key": "id",
"indexes": [
"name",
{
"name": "twitter",
"path": "twitter",
"options": {
"unique": true
}
}
]
}
}
}
}
```
### Use it programatically
You can also use it programatically, specially if you intend to use or create a tool to build, like `grunt` or `gulp`.

@@ -351,0 +453,0 @@

{
"id": 123,
"name": "Felipe N. Moura",

@@ -3,0 +4,0 @@ "age": 30,

{
"id": 456,
"name": "Jaydson N. Gomes",

@@ -3,0 +4,0 @@ "age": 31,

{
"id": 789,
"name": "Franciélen Silva",
"age": 25,
"gender": "female",
"twitter": "",
"twitter": "franfcunha_",
"page": "http://google.com/"
}

@@ -5,3 +5,4 @@ const PWASettings = {

"appShell": [
'/helmet.png'
'/helmet.png',
'/index.html?homescreen=1'
],

@@ -83,8 +84,18 @@ "enforceSSL": false,

"options": { "credentials": "same-origin"},
"strategy": "fastest",
"strategy": "offline-first",
"apply": {
"indexedDB": {
"name": "userData",
"version": "2",
"indexes": ["name"]
"version": "3",
"key": "id",
"indexes": [
"name",
{
"name": "twitter",
"path": "twitter",
"options": {
"unique": true
}
}
]
}

@@ -151,3 +162,4 @@ }

var DSWManager = void 0,
PWASettings = void 0;
PWASettings = void 0,
goFetch = void 0;

@@ -162,5 +174,6 @@ // finds the real size of an utf-8 string

var cacheManager = {
setup: function setup(DSWMan, PWASet) {
setup: function setup(DSWMan, PWASet, ftch) {
PWASettings = PWASet;
DSWManager = DSWMan;
goFetch = ftch;
DEFAULT_CACHE_VERSION = PWASettings.dswVersion || '1';

@@ -200,3 +213,3 @@ _indexeddbManager2.default.setup(cacheManager);

var cloned = response.clone();
return caches.open(cacheManager.mountCacheId(rule)).then(function (cache) {
return caches.open(typeof rule == 'string' ? rule : cacheManager.mountCacheId(rule)).then(function (cache) {
cache.put(request, cloned);

@@ -250,2 +263,3 @@ return response;

if (response && response.status == 200) {
// with success or not(saving it), we resolve it
var done = function done(_) {

@@ -256,21 +270,22 @@ resolve(response);

// store it in the indexedDB
_indexeddbManager2.default.save(rule.name, response.clone()).then(done).catch(done); // if failed saving, we still have the reponse to deliver
_indexeddbManager2.default.save(rule.name, response.clone(), request, rule).then(done).catch(done); // if failed saving, we still have the reponse to deliver
} else {
debugger;
// TODO: treat the not found requests
}
// if it failed, we can look for a fallback
url = request.url;
pathName = new URL(url).pathname;
return DSWManager.treatBadPage(response, pathName, event);
}
}
// let's look for it in our cache, and then in the database
// (we use the cache, just so we can user)
_indexeddbManager2.default.get(rule.name, request).then(function (result) {
debugger;
// if we did have it in the indexedDB
if (result) {
// we use it
console.log('found something');
// TODO: use it
return treatFetch(result);
} else {
// if it was not stored, let's fetch it
// fetching
request = DSWManager.createRequest(request, event, matching);
result = fetch(request, opts).then(treatFetch).catch(treatFetch);
return fetch(request, opts).then(treatFetch).catch(treatFetch);
}

@@ -467,2 +482,3 @@ });

var DEFAULT_DB_NAME = 'defaultDSWDB';
var INDEXEDDB_REQ_IDS = 'indexeddb-id-request';
var dbs = {};

@@ -509,6 +525,6 @@ var cacheManager;

if (config.indexes) {
baseData.keyPath = config.indexes;
if (config.key) {
baseData.keyPath = config.key;
}
if (!config.indexes || config.autoIncrement) {
if (!config.key || config.autoIncrement) {
baseData.autoIncrement = true;

@@ -527,4 +543,18 @@ }

} else if (event.oldVersion === 0) {
// if it is the first time it is creating it
db.createObjectStore(config.name, baseData);
(function () {
// if it is the first time it is creating it
var objectStore = db.createObjectStore(config.name, baseData);
// in case there are indexes defined, we create them
if (config.indexes) {
config.indexes.forEach(function (index) {
if (typeof index == 'string') {
objectStore.createIndex(index, index, {});
} else {
objectStore.createIndex(index.name, index.path || index.name, index.options);
}
});
}
// we will also make the key, an index
objectStore.createIndex(config.key, config.key, { unique: true });
})();
}

@@ -544,8 +574,31 @@

var store = getObjectStore(dbName);
// TODO: look for cached keys, then find them in the db
caches.match(request).then(function (result) {});
resolve();
caches.match(request).then(function (result) {
if (result) {
result.json().then(function (obj) {
// if the request was in cache, we now have got
// the id=value for the indexes(keys) to look for,
// in the indexedDB!
var store = getObjectStore(dbName),
index = store.index(obj.key),
getter = index.get(obj.value);
// in case we did get the content from indexedDB
// let's create a new Response out of it!
getter.onsuccess = function (event) {
resolve(new Response(JSON.stringify(event.target.result), {
headers: { 'Content-Type': 'application/json' }
}));
};
getter.onerror = function (event) {
// if we did not find it (or faced a problem) in
// indexeddb, we leave it to the network
resolve();
};
});
} else {
resolve();
}
});
});
},
save: function save(dbName, data) {
save: function save(dbName, data, request, rule) {
return new Promise(function (resolve, reject) {

@@ -561,2 +614,10 @@

req.onsuccess = function () {
var tmp = {};
var key = rule.action.indexedDB.key || 'id';
tmp.key = key;
tmp.value = obj[key];
cacheManager.put(INDEXEDDB_REQ_IDS, request, new Response(JSON.stringify(tmp), {
headers: { 'Content-Type': 'application/json' }
}));
resolve();

@@ -571,4 +632,2 @@ };

});
console.log(dbName, data);
});

@@ -668,3 +727,3 @@ }

// current referencies
_cacheManager2.default.setup(DSWManager, PWASettings);
_cacheManager2.default.setup(DSWManager, PWASettings, _goFetch2.default);
_strategies2.default.setup(DSWManager, _cacheManager2.default, _goFetch2.default);

@@ -671,0 +730,0 @@

@@ -5,3 +5,4 @@ {

"appShell": [
'/helmet.png'
'/helmet.png',
'/index.html?homescreen=1'
],

@@ -83,8 +84,18 @@ "enforceSSL": false,

"options": { "credentials": "same-origin"},
"strategy": "fastest",
"strategy": "offline-first",
"apply": {
"indexedDB": {
"name": "userData",
"version": "2",
"indexes": ["name"]
"version": "3",
"key": "id",
"indexes": [
"name",
{
"name": "twitter",
"path": "twitter",
"options": {
"unique": true
}
}
]
}

@@ -91,0 +102,0 @@ }

@@ -7,3 +7,3 @@ {

{
"src": "images/icon-128x128.png",
"src": "images/icon-128.png",
"sizes": "128x128",

@@ -13,3 +13,3 @@ "type": "image/png"

{
"src": "images/apple-touch-icon.png",
"src": "images/icon-152.png",
"sizes": "152x152",

@@ -19,3 +19,3 @@ "type": "image/png"

{
"src": "images/ms-touch-icon-144x144-precomposed.png",
"src": "images/icon-144.png",
"sizes": "144x144",

@@ -25,3 +25,3 @@ "type": "image/png"

{
"src": "images/chrome-touch-icon-192x192.png",
"src": "images/icon-192.png",
"sizes": "192x192",

@@ -28,0 +28,0 @@ "type": "image/png"

@@ -7,3 +7,4 @@ import indexedDBManager from './indexeddb-manager.js';

let DSWManager,
PWASettings;
PWASettings,
goFetch;

@@ -18,5 +19,6 @@ // finds the real size of an utf-8 string

const cacheManager = {
setup: (DSWMan, PWASet)=>{
setup: (DSWMan, PWASet, ftch)=>{
PWASettings = PWASet;
DSWManager = DSWMan;
goFetch = ftch;
DEFAULT_CACHE_VERSION = PWASettings.dswVersion || '1';

@@ -58,3 +60,3 @@ indexedDBManager.setup(cacheManager);

let cloned = response.clone();
return caches.open(cacheManager.mountCacheId(rule))
return caches.open(typeof rule == 'string'? rule: cacheManager.mountCacheId(rule))
.then(function(cache) {

@@ -106,2 +108,3 @@ cache.put(request, cloned);

if (response && response.status == 200) {
// with success or not(saving it), we resolve it
let done = _=>{

@@ -112,27 +115,27 @@ resolve(response);

// store it in the indexedDB
indexedDBManager.save(rule.name, response.clone())
indexedDBManager.save(rule.name, response.clone(), request, rule)
.then(done)
.catch(done); // if failed saving, we still have the reponse to deliver
}else{
debugger;
// TODO: treat the not found requests
// if it failed, we can look for a fallback
url = request.url;
pathName = new URL(url).pathname;
return DSWManager.treatBadPage(response, pathName, event);
}
}
// let's look for it in our cache, and then in the database
// (we use the cache, just so we can user)
indexedDBManager.get(rule.name, request)
.then(result=>{
debugger;
// if we did have it in the indexedDB
if (result) {
// we use it
console.log('found something');
// TODO: use it
return treatFetch(result);
}else{
// if it was not stored, let's fetch it
// fetching
request = DSWManager.createRequest(request, event, matching);
result = fetch(request,
opts)
.then(treatFetch)
.catch(treatFetch);
return fetch(request, opts)
.then(treatFetch)
.catch(treatFetch);
}

@@ -139,0 +142,0 @@ });

const DEFAULT_DB_NAME = 'defaultDSWDB';
const INDEXEDDB_REQ_IDS = 'indexeddb-id-request';
const dbs = {};

@@ -44,6 +45,6 @@ var cacheManager;

if (config.indexes) {
baseData.keyPath = config.indexes;
if (config.key) {
baseData.keyPath = config.key;
}
if (!config.indexes || config.autoIncrement) {
if (!config.key || config.autoIncrement) {
baseData.autoIncrement = true;

@@ -63,3 +64,19 @@ }

// if it is the first time it is creating it
db.createObjectStore(config.name, baseData);
let objectStore = db.createObjectStore(config.name, baseData);
// in case there are indexes defined, we create them
if (config.indexes) {
config.indexes.forEach(index=>{
if (typeof index == 'string') {
objectStore.createIndex(index, index, {});
} else {
objectStore.createIndex(index.name,
index.path || index.name,
index.options);
}
});
}
// we will also make the key, an index
objectStore.createIndex(config.key,
config.key,
{ unique: true });
}

@@ -80,10 +97,35 @@

let store = getObjectStore(dbName);
// TODO: look for cached keys, then find them in the db
caches.match(request)
.then(result=>{});
resolve();
.then(result=>{
if(result) {
result.json().then(obj=>{
// if the request was in cache, we now have got
// the id=value for the indexes(keys) to look for,
// in the indexedDB!
let store = getObjectStore(dbName),
index = store.index(obj.key),
getter = index.get(obj.value);
// in case we did get the content from indexedDB
// let's create a new Response out of it!
getter.onsuccess = event=>{
resolve(new Response(JSON.stringify(event.target.result),
{
headers: { 'Content-Type' : 'application/json' }
})
);
};
getter.onerror = event=>{
// if we did not find it (or faced a problem) in
// indexeddb, we leave it to the network
resolve();
};
});
}else{
resolve();
}
});
});
},
save (dbName, data) {
save (dbName, data, request, rule) {
return new Promise((resolve, reject)=>{

@@ -99,2 +141,14 @@

req.onsuccess = function () {
let tmp = {};
let key = rule.action.indexedDB.key || 'id';
tmp.key = key;
tmp.value = obj[key];
cacheManager.put(INDEXEDDB_REQ_IDS,
request,
new Response(JSON.stringify(tmp),
{
headers: { 'Content-Type' : 'application/json' }
})
);
resolve();

@@ -109,4 +163,2 @@ };

});
console.log(dbName, data);
});

@@ -113,0 +165,0 @@ }

@@ -71,3 +71,3 @@ // TODO: should pre-cache or cache in the first load, some of the page's already sources (like css, js or images), or tell the user it supports offline usage, only in the next reload

// current referencies
cacheManager.setup(DSWManager, PWASettings);
cacheManager.setup(DSWManager, PWASettings, goFetch);
strategies.setup(DSWManager, cacheManager, goFetch);

@@ -74,0 +74,0 @@

@@ -7,3 +7,3 @@ {

{
"src": "images/icon-128x128.png",
"src": "images/icon-128.png",
"sizes": "128x128",

@@ -13,3 +13,3 @@ "type": "image/png"

{
"src": "images/apple-touch-icon.png",
"src": "images/icon-152.png",
"sizes": "152x152",

@@ -19,3 +19,3 @@ "type": "image/png"

{
"src": "images/ms-touch-icon-144x144-precomposed.png",
"src": "images/icon-144.png",
"sizes": "144x144",

@@ -25,3 +25,3 @@ "type": "image/png"

{
"src": "images/chrome-touch-icon-192x192.png",
"src": "images/icon-192.png",
"sizes": "192x192",

@@ -28,0 +28,0 @@ "type": "image/png"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc