first commit

This commit is contained in:
s.golasch
2023-08-01 13:49:46 +02:00
commit 1fc239fd54
20238 changed files with 3112246 additions and 0 deletions

View 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;

View 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
};

View 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
};