better path mapping & error logging, clean CLI functions from www #146

pull/147/head
robinmoisson 2022-11-23 23:20:46 +01:00
rodzic 1162e35e2c
commit 4f81530106
8 zmienionych plików z 70 dodań i 117 usunięć

Wyświetl plik

@ -1,6 +1,8 @@
const fs = require("fs");
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
const path = require("path");
const {renderTemplate} = require("../lib/formater.js");
const { generateRandomSalt } = cryptoEngine;
/**
@ -99,3 +101,59 @@ function getSalt(namedArgs, config) {
return generateRandomSalt();
}
exports.getSalt = getSalt;
/**
* A dead-simple alternative to webpack or rollup for inlining simple
* CommonJS modules in a browser <script>.
* - Removes all lines containing require().
* - Wraps the module in an immediately invoked function that returns `exports`.
*
* @param {string} modulePath - path from staticrypt root directory
*/
function convertCommonJSToBrowserJS(modulePath) {
const rootDirectory = path.join(__dirname, '..');
const resolvedPath = path.join(rootDirectory, ...modulePath.split("/")) + ".js";
if (!fs.existsSync(resolvedPath)) {
exitEarly(`Failure: could not find module to convert at path "${resolvedPath}"`);
}
const moduleText = fs
.readFileSync(resolvedPath, "utf8")
.replaceAll(/^.*\brequire\(.*$\n/gm, "");
return `
((function(){
const exports = {};
${moduleText}
return exports;
})())
`.trim();
}
exports.convertCommonJSToBrowserJS = convertCommonJSToBrowserJS;
/**
* Fill the template with provided data and writes it to output file.
*
* @param {Object} data
* @param {string} outputFilePath
* @param {string} inputFilePath
*/
function genFile(data, outputFilePath, inputFilePath) {
let templateContents;
try {
templateContents = fs.readFileSync(inputFilePath, "utf8");
} catch (e) {
exitEarly("Failure: could not read template!");
}
const renderedTemplate = renderTemplate(templateContents, data);
try {
fs.writeFileSync(outputFilePath, renderedTemplate);
} catch (e) {
exitEarly("Failure: could not generate output file!");
}
}
exports.genFile = genFile;

Wyświetl plik

@ -11,8 +11,7 @@ require('dotenv').config();
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
const codec = require("../lib/codec");
const { convertCommonJSToBrowserJS, genFile} = require("../lib/formater");
const { exitEarly, isOptionSetByUser, getPassword, getFileContent, getSalt} = require("./helpers");
const { convertCommonJSToBrowserJS, exitEarly, isOptionSetByUser, genFile, getPassword, getFileContent, getSalt} = require("./helpers");
const { generateRandomSalt } = cryptoEngine;
const { encode } = codec.init(cryptoEngine);
@ -192,8 +191,8 @@ const data = {
encrypted: encryptedMessage,
instructions: namedArgs.instructions,
is_remember_enabled: namedArgs.noremember ? "false" : "true",
js_codec: convertCommonJSToBrowserJS("../lib/codec"),
js_crypto_engine: convertCommonJSToBrowserJS("../lib/cryptoEngine/cryptojsEngine"),
js_codec: convertCommonJSToBrowserJS("lib/codec"),
js_crypto_engine: convertCommonJSToBrowserJS("lib/cryptoEngine/cryptojsEngine"),
label_error: namedArgs.labelError,
passphrase_placeholder: namedArgs.passphrasePlaceholder,
remember_duration_in_days: namedArgs.remember,

Wyświetl plik

@ -349,7 +349,7 @@ exports.init = init;
var decode = codec.init(cryptoEngine).decode;
// variables to be filled when generating the file
var encryptedMsg = '01f9008b256609f2bfd27da9643e2718817d28fb4b194104090d3f485c2623cbb629bd9fa1fe315a50e0f7dd50c4c98eU2FsdGVkX1+ZSG96042ErwkyOo9O7uOc/v2KMt4XH1MQIYfg4VW0Rb7somvWeSOYTps0rc5M56su4rSdzrwfaaaYMvdNdj9CGtL0vKKl3zrOnfZYCnFhT/1Y/Dg0CTaC2o1erNl50uzwvY/98mwNPQLMu+zgn4EjkzayWdNIiQ7zFR28JcBSILlBlnrpIm16c8A0MVj7SdoSXnYtWrviOw==',
var encryptedMsg = '76c0544deb5605a336e09f4579904448a97a7ed8ad58d764d519a4c4aa10960aa1d943725d76599ecb21a7bf57853aa6U2FsdGVkX1+e6Qjrw0mQlBXmulS5Mga0HtsQ2NiVEV144HhClfGUm9pI28u+7XSraW1jxEIXRDWkcnIEdK98u5eBCC2iMVJneDe/z6Ep3JHuPYIYmRIXzdBUDPUpsnITOz9xpnRrEIPXPuHUExbOzDhNzkiBTvI3pjpJwl51ApCxRU/THans3lzSb2NldDNxInogvY5IRcY8Z63tJMtsvQ==',
salt = 'b93bbaf35459951c47721d1f3eaeb5b9',
labelError = 'Bad password!',
isRememberEnabled = true,

Wyświetl plik

@ -339,32 +339,7 @@ exports.init = init;
<script id="formater">
window.formater = ((function(){
const exports = {};
/**
* A dead-simple alternative to webpack or rollup for inlining simple
* CommonJS modules in a browser <script>.
* - Wraps the module in an immediately invoked function that returns `exports`.
*
* @param {string} modulePath
*/
function convertCommonJSToBrowserJS(modulePath) {
const resolvedPath = path.join(__dirname, ...modulePath.split("/")) + ".js";
const moduleText = fs
.readFileSync(resolvedPath, "utf8")
.replaceAll(/^.*\brequire\(.*$\n/gm, "");
return `
((function(){
const exports = {};
${moduleText}
return exports;
})())
`.trim();
}
exports.convertCommonJSToBrowserJS = convertCommonJSToBrowserJS;
/**
/**
* Replace the placeholder tags (between '{tag}') in the template string with provided data.
*
* @param {string} templateString
@ -383,31 +358,6 @@ function renderTemplate(templateString, data) {
}
exports.renderTemplate = renderTemplate;
/**
* Fill the template with provided data and writes it to output file.
*
* @param {Object} data
* @param {string} outputFilePath
* @param {string} inputFilePath
*/
function genFile(data, outputFilePath, inputFilePath) {
let templateContents;
try {
templateContents = fs.readFileSync(inputFilePath, "utf8");
} catch (e) {
exitEarly("Failure: could not read template!");
}
const renderedTemplate = renderTemplate(templateContents, data);
try {
fs.writeFileSync(outputFilePath, renderedTemplate);
} catch (e) {
exitEarly("Failure: could not generate output file!");
}
}
exports.genFile = genFile;
return exports;
})())

Wyświetl plik

@ -1,32 +1,3 @@
const path = require("path");
const fs = require("fs");
const { exitEarly } = require("../cli/helpers");
/**
* A dead-simple alternative to webpack or rollup for inlining simple
* CommonJS modules in a browser <script>.
* - Removes all lines containing require().
* - Wraps the module in an immediately invoked function that returns `exports`.
*
* @param {string} modulePath
*/
function convertCommonJSToBrowserJS(modulePath) {
const resolvedPath = path.join(__dirname, ...modulePath.split("/")) + ".js";
const moduleText = fs
.readFileSync(resolvedPath, "utf8")
.replaceAll(/^.*\brequire\(.*$\n/gm, "");
return `
((function(){
const exports = {};
${moduleText}
return exports;
})())
`.trim();
}
exports.convertCommonJSToBrowserJS = convertCommonJSToBrowserJS;
/**
* Replace the placeholder tags (between '{tag}') in the template string with provided data.
*
@ -46,28 +17,3 @@ function renderTemplate(templateString, data) {
}
exports.renderTemplate = renderTemplate;
/**
* Fill the template with provided data and writes it to output file.
*
* @param {Object} data
* @param {string} outputFilePath
* @param {string} inputFilePath
*/
function genFile(data, outputFilePath, inputFilePath) {
let templateContents;
try {
templateContents = fs.readFileSync(inputFilePath, "utf8");
} catch (e) {
exitEarly("Failure: could not read template!");
}
const renderedTemplate = renderTemplate(templateContents, data);
try {
fs.writeFileSync(outputFilePath, renderedTemplate);
} catch (e) {
exitEarly("Failure: could not generate output file!");
}
}
exports.genFile = genFile;

4
package-lock.json wygenerowano
Wyświetl plik

@ -1,12 +1,12 @@
{
"name": "staticrypt",
"version": "2.3.0",
"version": "2.3.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "staticrypt",
"version": "2.3.0",
"version": "2.3.1",
"license": "MIT",
"dependencies": {
"crypto-js": "3.1.9-1",

Wyświetl plik

@ -1,6 +1,6 @@
{
"name": "staticrypt",
"version": "2.3.0",
"version": "2.3.1",
"description": "Based on the [crypto-js](https://github.com/brix/crypto-js) library, StatiCrypt uses AES-256 to encrypt your input with your passphrase and put it in a HTML file with a password prompt that can decrypted in-browser (client side).",
"main": "index.js",
"files": [

Wyświetl plik

@ -1,9 +1,9 @@
const { convertCommonJSToBrowserJS, genFile} = require("../lib/formater");
const { convertCommonJSToBrowserJS, genFile } = require("../cli/helpers.js");
const data = {
js_codec: convertCommonJSToBrowserJS("../lib/codec"),
js_crypto_engine: convertCommonJSToBrowserJS("../lib/cryptoEngine/cryptojsEngine"),
js_formater: convertCommonJSToBrowserJS("../lib/formater"),
js_codec: convertCommonJSToBrowserJS("lib/codec"),
js_crypto_engine: convertCommonJSToBrowserJS("lib/cryptoEngine/cryptojsEngine"),
js_formater: convertCommonJSToBrowserJS("lib/formater"),
};
genFile(data, "./index.html", "./scripts/index_template.html");