yaml-crypt
Advanced tools
Comparing version 0.4.2 to 0.4.3
{ | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"ecmaVersion": 8, | ||
"sourceType": "module" | ||
}, | ||
"extends": [ | ||
"eslint:recommended" | ||
], | ||
"extends": ["eslint:recommended"], | ||
"env": { | ||
"node": true | ||
"node": true, | ||
"es6": true | ||
}, | ||
@@ -12,0 +11,0 @@ "rules": { |
@@ -7,4 +7,11 @@ export as namespace yamlcrypt; | ||
export function loadConfig(opts?: LoadConfigOptions): Config; | ||
export function loadFile(path: string, opts?: LoadFileOptions): Promise<any>; | ||
export interface LoadFileOptions { | ||
config?: Config; | ||
loadAll?: boolean; | ||
} | ||
export function loadConfig(opts?: LoadConfigOptions): Promise<Config>; | ||
export interface LoadConfigOptions { | ||
@@ -11,0 +18,0 @@ path?: string; |
const { homedir } = require("os"); | ||
const { readFileSync } = require("fs"); | ||
const { readFile } = require("fs"); | ||
const { join } = require("path"); | ||
@@ -22,6 +22,13 @@ | ||
function loadConfig({ home, path } = {}) { | ||
async function loadFile(path, { config, loadAll } = { loadAll: false }) { | ||
const cfg = config ? config : await loadConfig(); | ||
const content = await readFileAsync(path); | ||
const yc = yamlcrypt(cfg); | ||
return loadAll ? yc.decryptAll(content) : yc.decrypt(content); | ||
} | ||
async function loadConfig({ home, path } = {}) { | ||
let content = null; | ||
if (path) { | ||
content = readFileSync(path); | ||
content = await readFileAsync(path); | ||
} else { | ||
@@ -31,3 +38,3 @@ let h = home ? home : homedir(); | ||
try { | ||
content = readFileSync(join(h, ".yaml-crypt", filename)); | ||
content = await readFileAsync(join(h, ".yaml-crypt", filename)); | ||
break; | ||
@@ -51,2 +58,14 @@ } catch (e) { | ||
function readFileAsync(path) { | ||
return new Promise((resolve, reject) => { | ||
readFile(path, (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
} | ||
function yamlcrypt({ keys, encryptionKey } = {}) { | ||
@@ -349,2 +368,3 @@ const normalizedKeys = normalizeKeys(keys); | ||
generateKey, | ||
loadFile, | ||
loadConfig, | ||
@@ -351,0 +371,0 @@ yamlcrypt, |
{ | ||
"name": "yaml-crypt", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Encrypt and decrypt YAML documents", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -11,3 +11,3 @@ const fs = require("fs"); | ||
const { loadConfig, yamlcrypt } = require("../lib/yaml-crypt"); | ||
const { loadFile, loadConfig, yamlcrypt } = require("../lib/yaml-crypt"); | ||
const { setupCrypto, decryptBranca } = require("./crypto-util"); | ||
@@ -18,15 +18,15 @@ | ||
describe("yaml-crypt", () => { | ||
it("should load the config file", () => { | ||
const config = loadConfig(); | ||
it("should load the config file", async () => { | ||
const config = await loadConfig(); | ||
expect(config).to.not.be.null; | ||
}); | ||
it("should load the config file from the given path", () => { | ||
it("should load the config file from the given path", async () => { | ||
const configFile = tmp.fileSync(); | ||
fs.writeFileSync(configFile.name, "keys:\n - key: 123"); | ||
const config = loadConfig({ path: configFile.name }); | ||
const config = await loadConfig({ path: configFile.name }); | ||
expect(config).to.not.be.null; | ||
}); | ||
it("should load the config file from home", () => { | ||
it("should load the config file from home", async () => { | ||
const home = tmp.dirSync(); | ||
@@ -38,3 +38,3 @@ fs.mkdirSync(`${home.name}/.yaml-crypt`); | ||
); | ||
const config = loadConfig({ home: home.name }); | ||
const config = await loadConfig({ home: home.name }); | ||
expect(config).to.not.be.null; | ||
@@ -44,12 +44,17 @@ expect(config.keys).to.have.lengthOf(1); | ||
it("should throw an error when the config file is not readable", () => { | ||
it("should throw an error when the config file is not readable", async () => { | ||
const home = tmp.dirSync(); | ||
fs.mkdirSync(`${home.name}/.yaml-crypt`); | ||
fs.mkdirSync(`${home.name}/.yaml-crypt/config.yaml`); | ||
expect(() => loadConfig({ home: home.name })).to.throw(/illegal operation/); | ||
try { | ||
await loadConfig({ home: home.name }); | ||
expect.fail("exception expected!"); | ||
} catch (e) { | ||
expect(e.message).to.match(/illegal operation/); | ||
} | ||
}); | ||
it("should return the default config file", () => { | ||
it("should return the default config file", async () => { | ||
const home = tmp.dirSync(); | ||
const config = loadConfig({ home: home.name }); | ||
const config = await loadConfig({ home: home.name }); | ||
expect(config).to.not.be.null; | ||
@@ -59,2 +64,19 @@ expect(config.keys).to.be.undefined; | ||
it("should read the decrypted content via loadFile", async () => { | ||
const config = { keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }; | ||
const result = await loadFile("./test/test-1b.yaml-crypt", { config }); | ||
expect(result.key1.toString()).to.equal("Hello, world!"); | ||
expect(result.key2.toString()).to.equal("Hello, world!"); | ||
}); | ||
it("should read the decrypted content via loadFile (all)", async () => { | ||
const config = { keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }; | ||
const result = await loadFile("./test/test-2.yaml-crypt", { | ||
config, | ||
loadAll: true | ||
}); | ||
expect(result[0].first.toString()).to.equal("Hello, world!"); | ||
expect(result[1].other.toString()).to.equal("Hello, world!"); | ||
}); | ||
it("should read the decrypted content", () => { | ||
@@ -61,0 +83,0 @@ const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69365
1936