kopia lustrzana https://github.com/robinmoisson/staticrypt
better path mapping & error logging, clean CLI functions from www #146
rodzic
1162e35e2c
commit
4f81530106
|
@ -1,6 +1,8 @@
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
|
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
|
||||||
|
const path = require("path");
|
||||||
|
const {renderTemplate} = require("../lib/formater.js");
|
||||||
const { generateRandomSalt } = cryptoEngine;
|
const { generateRandomSalt } = cryptoEngine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,3 +101,59 @@ function getSalt(namedArgs, config) {
|
||||||
return generateRandomSalt();
|
return generateRandomSalt();
|
||||||
}
|
}
|
||||||
exports.getSalt = getSalt;
|
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;
|
|
@ -11,8 +11,7 @@ require('dotenv').config();
|
||||||
|
|
||||||
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
|
const cryptoEngine = require("../lib/cryptoEngine/cryptojsEngine");
|
||||||
const codec = require("../lib/codec");
|
const codec = require("../lib/codec");
|
||||||
const { convertCommonJSToBrowserJS, genFile} = require("../lib/formater");
|
const { convertCommonJSToBrowserJS, exitEarly, isOptionSetByUser, genFile, getPassword, getFileContent, getSalt} = require("./helpers");
|
||||||
const { exitEarly, isOptionSetByUser, getPassword, getFileContent, getSalt} = require("./helpers");
|
|
||||||
const { generateRandomSalt } = cryptoEngine;
|
const { generateRandomSalt } = cryptoEngine;
|
||||||
const { encode } = codec.init(cryptoEngine);
|
const { encode } = codec.init(cryptoEngine);
|
||||||
|
|
||||||
|
@ -192,8 +191,8 @@ const data = {
|
||||||
encrypted: encryptedMessage,
|
encrypted: encryptedMessage,
|
||||||
instructions: namedArgs.instructions,
|
instructions: namedArgs.instructions,
|
||||||
is_remember_enabled: namedArgs.noremember ? "false" : "true",
|
is_remember_enabled: namedArgs.noremember ? "false" : "true",
|
||||||
js_codec: convertCommonJSToBrowserJS("../lib/codec"),
|
js_codec: convertCommonJSToBrowserJS("lib/codec"),
|
||||||
js_crypto_engine: convertCommonJSToBrowserJS("../lib/cryptoEngine/cryptojsEngine"),
|
js_crypto_engine: convertCommonJSToBrowserJS("lib/cryptoEngine/cryptojsEngine"),
|
||||||
label_error: namedArgs.labelError,
|
label_error: namedArgs.labelError,
|
||||||
passphrase_placeholder: namedArgs.passphrasePlaceholder,
|
passphrase_placeholder: namedArgs.passphrasePlaceholder,
|
||||||
remember_duration_in_days: namedArgs.remember,
|
remember_duration_in_days: namedArgs.remember,
|
||||||
|
|
|
@ -349,7 +349,7 @@ exports.init = init;
|
||||||
var decode = codec.init(cryptoEngine).decode;
|
var decode = codec.init(cryptoEngine).decode;
|
||||||
|
|
||||||
// variables to be filled when generating the file
|
// 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',
|
salt = 'b93bbaf35459951c47721d1f3eaeb5b9',
|
||||||
labelError = 'Bad password!',
|
labelError = 'Bad password!',
|
||||||
isRememberEnabled = true,
|
isRememberEnabled = true,
|
||||||
|
|
52
index.html
52
index.html
|
@ -339,32 +339,7 @@ exports.init = init;
|
||||||
<script id="formater">
|
<script id="formater">
|
||||||
window.formater = ((function(){
|
window.formater = ((function(){
|
||||||
const exports = {};
|
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.
|
* Replace the placeholder tags (between '{tag}') in the template string with provided data.
|
||||||
*
|
*
|
||||||
* @param {string} templateString
|
* @param {string} templateString
|
||||||
|
@ -383,31 +358,6 @@ function renderTemplate(templateString, data) {
|
||||||
}
|
}
|
||||||
exports.renderTemplate = renderTemplate;
|
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;
|
return exports;
|
||||||
})())
|
})())
|
||||||
|
|
|
@ -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.
|
* Replace the placeholder tags (between '{tag}') in the template string with provided data.
|
||||||
*
|
*
|
||||||
|
@ -46,28 +17,3 @@ function renderTemplate(templateString, data) {
|
||||||
}
|
}
|
||||||
exports.renderTemplate = renderTemplate;
|
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;
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "staticrypt",
|
"name": "staticrypt",
|
||||||
"version": "2.3.0",
|
"version": "2.3.1",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "staticrypt",
|
"name": "staticrypt",
|
||||||
"version": "2.3.0",
|
"version": "2.3.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"crypto-js": "3.1.9-1",
|
"crypto-js": "3.1.9-1",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "staticrypt",
|
"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).",
|
"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",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const { convertCommonJSToBrowserJS, genFile} = require("../lib/formater");
|
const { convertCommonJSToBrowserJS, genFile } = require("../cli/helpers.js");
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
js_codec: convertCommonJSToBrowserJS("../lib/codec"),
|
js_codec: convertCommonJSToBrowserJS("lib/codec"),
|
||||||
js_crypto_engine: convertCommonJSToBrowserJS("../lib/cryptoEngine/cryptojsEngine"),
|
js_crypto_engine: convertCommonJSToBrowserJS("lib/cryptoEngine/cryptojsEngine"),
|
||||||
js_formater: convertCommonJSToBrowserJS("../lib/formater"),
|
js_formater: convertCommonJSToBrowserJS("lib/formater"),
|
||||||
};
|
};
|
||||||
|
|
||||||
genFile(data, "./index.html", "./scripts/index_template.html");
|
genFile(data, "./index.html", "./scripts/index_template.html");
|
||||||
|
|
Ładowanie…
Reference in New Issue