Moved some utility functions to a separate file

This commit is contained in:
Alexander Cerutti
2019-04-28 12:15:24 +02:00
parent 37a9a051c6
commit baf096c380
2 changed files with 125 additions and 111 deletions

View File

@@ -1,9 +1,7 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const { promisify } = require("util"); const { promisify } = require("util");
const { EOL } = require("os");
const stream = require("stream"); const stream = require("stream");
const moment = require("moment");
const forge = require("node-forge"); const forge = require("node-forge");
const archiver = require("archiver"); const archiver = require("archiver");
const debug = require("debug"); const debug = require("debug");
@@ -16,6 +14,11 @@ const loadDebug = debug("passkit:load");
const schema = require("./schema"); const schema = require("./schema");
const formatMessage = require("./messages"); const formatMessage = require("./messages");
const FieldsArray = require("./fieldsArray"); const FieldsArray = require("./fieldsArray");
const {
assignLength, generateStringFile,
removeHidden, dateToW3CString,
isValidRGB, parsePEM
} = require("./utils");
const readdir = promisify(fs.readdir); const readdir = promisify(fs.readdir);
const readFile = promisify(fs.readFile); const readFile = promisify(fs.readFile);
@@ -766,114 +769,6 @@ function readCertificates(certificates) {
}); });
} }
/**
* Parses the PEM-formatted passed text (certificates)
*
* @function parsePEM
* @params {String} element - Text content of .pem files
* @params {String=} passphrase - passphrase for the key
* @returns {Object} The parsed certificate or key in node forge format
*/
function parsePEM(pemName, element, passphrase) {
if (pemName === "signerKey" && passphrase) {
return forge.pki.decryptRsaPrivateKey(element, String(passphrase));
} else {
return forge.pki.certificateFromPem(element);
}
}
/**
* Checks if an rgb value is compliant with CSS-like syntax
*
* @function isValidRGB
* @params {String} value - string to analyze
* @returns {Boolean} True if valid rgb, false otherwise
*/
function isValidRGB(value) {
if (!value || typeof value !== "string") {
return false;
}
const rgb = value.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
if (!rgb) {
return false;
}
return rgb.slice(1, 4).every(v => Math.abs(Number(v)) <= 255);
}
/**
* Converts a date to W3C Standard format
*
* @function dateToW3Cstring
* @params {String} date - The date to be parsed
* @params {String} [format] - a custom format
* @returns {String|undefined} The parsed string if the parameter is valid,
* undefined otherwise
*/
function dateToW3CString(date, format) {
if (typeof date !== "string") {
return "";
}
const parsedDate = moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format();
if (parsedDate === "Invalid date") {
return undefined;
}
return parsedDate;
}
/**
* Apply a filter to arg0 to remove hidden files names (starting with dot)
*
* @function removeHidden
* @params {String[]} from - list of file names
* @return {String[]}
*/
function removeHidden(from) {
return from.filter(e => e.charAt(0) !== ".");
}
/**
* Creates a buffer of translations in Apple .strings format
*
* @function generateStringFile
* @params {Object} lang - structure containing related to ISO 3166 alpha-2 code for the language
* @returns {Buffer} Buffer to be written in pass.strings for language in lang
* @see https://apple.co/2M9LWVu - String Resources
*/
function generateStringFile(lang) {
if (!Object.keys(lang).length) {
return Buffer.from("", "utf8");
}
// Pass.strings format is the following one for each row:
// "key" = "value";
const strings = Object.keys(lang)
.map(key => `"${key}" = "${lang[key].replace(/"/g, /\\"/)}";`);
return Buffer.from(strings.join(EOL), "utf8");
}
/**
* Creates a new object with custom length property
* @param {number} value - the length
* @param {Array<Object<string, any>>} source - the main sources of properties
*/
function assignLength(length, ...sources) {
return Object.assign({ length }, ...sources);
}
/** /**
* Automatically generates barcodes for all the types given common info * Automatically generates barcodes for all the types given common info
* *

119
src/utils.js Normal file
View File

@@ -0,0 +1,119 @@
const moment = require("moment");
const { EOL } = require("os");
/**
* Parses the PEM-formatted passed text (certificates)
*
* @function parsePEM
* @params {String} element - Text content of .pem files
* @params {String=} passphrase - passphrase for the key
* @returns {Object} The parsed certificate or key in node forge format
*/
function parsePEM(pemName, element, passphrase) {
if (pemName === "signerKey" && passphrase) {
return forge.pki.decryptRsaPrivateKey(element, String(passphrase));
} else {
return forge.pki.certificateFromPem(element);
}
}
/**
* Checks if an rgb value is compliant with CSS-like syntax
*
* @function isValidRGB
* @params {String} value - string to analyze
* @returns {Boolean} True if valid rgb, false otherwise
*/
function isValidRGB(value) {
if (!value || typeof value !== "string") {
return false;
}
const rgb = value.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
if (!rgb) {
return false;
}
return rgb.slice(1, 4).every(v => Math.abs(Number(v)) <= 255);
}
/**
* Converts a date to W3C Standard format
*
* @function dateToW3Cstring
* @params {String} date - The date to be parsed
* @params {String} [format] - a custom format
* @returns {String|undefined} The parsed string if the parameter is valid,
* undefined otherwise
*/
function dateToW3CString(date, format) {
if (typeof date !== "string") {
return "";
}
const parsedDate = moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format();
if (parsedDate === "Invalid date") {
return undefined;
}
return parsedDate;
}
/**
* Apply a filter to arg0 to remove hidden files names (starting with dot)
*
* @function removeHidden
* @params {String[]} from - list of file names
* @return {String[]}
*/
function removeHidden(from) {
return from.filter(e => e.charAt(0) !== ".");
}
/**
* Creates a buffer of translations in Apple .strings format
*
* @function generateStringFile
* @params {Object} lang - structure containing related to ISO 3166 alpha-2 code for the language
* @returns {Buffer} Buffer to be written in pass.strings for language in lang
* @see https://apple.co/2M9LWVu - String Resources
*/
function generateStringFile(lang) {
if (!Object.keys(lang).length) {
return Buffer.from("", "utf8");
}
// Pass.strings format is the following one for each row:
// "key" = "value";
const strings = Object.keys(lang)
.map(key => `"${key}" = "${lang[key].replace(/"/g, /\\"/)}";`);
return Buffer.from(strings.join(EOL), "utf8");
}
/**
* Creates a new object with custom length property
* @param {number} value - the length
* @param {Array<Object<string, any>>} source - the main sources of properties
*/
function assignLength(length, ...sources) {
return Object.assign({ length }, ...sources);
}
module.exports = {
assignLength,
generateStringFile,
removeHidden,
dateToW3CString,
isValidRGB,
parsePEM
};