first commit
This commit is contained in:
10
build/node_modules/generate-service-worker/utils/hash.js
generated
vendored
Normal file
10
build/node_modules/generate-service-worker/utils/hash.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
const crypto = require('crypto');
|
||||
|
||||
function buildHashFromConfig(config) {
|
||||
return crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(config))
|
||||
.digest('hex');
|
||||
}
|
||||
|
||||
module.exports = buildHashFromConfig;
|
||||
102
build/node_modules/generate-service-worker/utils/validate.js
generated
vendored
Normal file
102
build/node_modules/generate-service-worker/utils/validate.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
/* eslint-disable no-throw-literal */
|
||||
|
||||
function arrayOfTypeValidation(validator) {
|
||||
return withRequired(function arrayOfType(value) {
|
||||
if (!Array.isArray(value)) {
|
||||
throw `Value ${value} must be an array.`;
|
||||
}
|
||||
value.every(validator);
|
||||
});
|
||||
}
|
||||
|
||||
function oneOfTypeValidation(types) {
|
||||
return withRequired(function oneOf(value) {
|
||||
const isValidType = types.some(function (Type) {
|
||||
try {
|
||||
Type(value);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (!isValidType) {
|
||||
throw `Value ${value} not a valid type.`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function oneOfValidation(list) {
|
||||
return withRequired(function oneOf(value) {
|
||||
if (list.indexOf(value) === -1) {
|
||||
throw `Value ${value} not a valid option from list: ${list.join(', ')}.`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function shapeValidation(objShape) {
|
||||
return withRequired(function shape(value) {
|
||||
if (value && typeof value !== 'object') {
|
||||
throw `Value <${value}> must be an object.`;
|
||||
}
|
||||
Object.keys(objShape).forEach(function shapeKeyValidation(key) {
|
||||
try {
|
||||
objShape[key](value[key]);
|
||||
} catch (e) {
|
||||
if (objShape[key].name === 'shape') {
|
||||
throw e;
|
||||
} else {
|
||||
throw `Key: "${key}" failed with "${e}"`;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function booleanValidation(value) {
|
||||
if (!value || typeof value !== 'boolean') {
|
||||
throw `Value ${value} must be of type "boolean".`;
|
||||
}
|
||||
}
|
||||
|
||||
function objectValidation(value) {
|
||||
if (!value || typeof value !== 'object') {
|
||||
throw `Value ${value} must be non-null "object".`;
|
||||
}
|
||||
}
|
||||
|
||||
function stringValidation(value) {
|
||||
if (typeof value !== 'string') {
|
||||
throw `Value ${value} must be of type "string".`;
|
||||
}
|
||||
}
|
||||
|
||||
function numberValidation(value) {
|
||||
if (typeof value !== 'number') {
|
||||
throw `Value ${value} must be of type "number".`;
|
||||
}
|
||||
}
|
||||
|
||||
function withRequired(_validator) {
|
||||
function validator(value) {
|
||||
return value === undefined || _validator(value);
|
||||
}
|
||||
|
||||
validator.required = function requiredValidator(value) {
|
||||
if (value === undefined) {
|
||||
throw 'Value cannot be undefined.';
|
||||
}
|
||||
_validator(value);
|
||||
};
|
||||
return validator;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
boolean: withRequired(booleanValidation),
|
||||
object: withRequired(objectValidation),
|
||||
number: withRequired(numberValidation),
|
||||
string: withRequired(stringValidation),
|
||||
arrayOfType: arrayOfTypeValidation,
|
||||
oneOf: oneOfValidation,
|
||||
oneOfType: oneOfTypeValidation,
|
||||
shape: shapeValidation
|
||||
};
|
||||
54
build/node_modules/generate-service-worker/utils/validators.js
generated
vendored
Normal file
54
build/node_modules/generate-service-worker/utils/validators.js
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
const V = require('./validate');
|
||||
|
||||
const StrategyShape = V.shape({
|
||||
type: V.oneOf(['offline-only', 'fallback-only', 'prefer-cache', 'race']).required,
|
||||
matches: V.arrayOfType(V.string).required
|
||||
});
|
||||
|
||||
const CacheShape = V.shape({
|
||||
offline: V.boolean,
|
||||
precache: V.arrayOfType(V.string),
|
||||
strategy: V.arrayOfType(StrategyShape)
|
||||
});
|
||||
|
||||
const NotificationsShape = V.shape({
|
||||
default: V.shape({
|
||||
title: V.string.required,
|
||||
body: V.string,
|
||||
icon: V.string,
|
||||
tag: V.string,
|
||||
data: V.shape({
|
||||
url: V.string
|
||||
})
|
||||
}).required,
|
||||
duration: V.number,
|
||||
fallbackURL: V.string
|
||||
});
|
||||
|
||||
const LogShape = V.shape({
|
||||
installed: V.string,
|
||||
notificationClicked: V.string,
|
||||
notificationReceived: V.string,
|
||||
requestOptions: V.object
|
||||
});
|
||||
|
||||
function validate(config) {
|
||||
if (!config.template) {
|
||||
if (config.cache && !config.cache.template) {
|
||||
CacheShape(config.cache);
|
||||
}
|
||||
if (config.notifications && !config.notifications.template) {
|
||||
NotificationsShape(config.notifications);
|
||||
}
|
||||
if (config.log) {
|
||||
LogShape(config.log);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CacheShape: CacheShape,
|
||||
NotificationsShape: NotificationsShape,
|
||||
LogShape: LogShape,
|
||||
validate: validate
|
||||
};
|
||||
Reference in New Issue
Block a user