diff --git a/lib/PasswordGenerator.ts b/lib/PasswordGenerator.ts index 77f80a2..414cf9f 100644 --- a/lib/PasswordGenerator.ts +++ b/lib/PasswordGenerator.ts @@ -8,15 +8,15 @@ const AsciiRange = { max: 126, } -export async function generatePassword(size: number): Promise { +export async function generatePassword(length: number): Promise { let result = ""; while (true) { - const bytes = await promisify(crypto.randomBytes)(size * 2); + const bytes = await promisify(crypto.randomBytes)(length * 2); const byteArray = Array.from(bytes); const filtered = byteArray.filter(isInAsciiRange); result += String.fromCharCode(...filtered); - if (result.length >= size) { - result = result.slice(0, size); + if (result.length >= length) { + result = result.slice(0, length); break; } } diff --git a/lib/PasswordGeneratorNode.html b/lib/PasswordGeneratorNode.html index ba7dc7f..1697c12 100644 --- a/lib/PasswordGeneratorNode.html +++ b/lib/PasswordGeneratorNode.html @@ -1,15 +1,33 @@ + @@ -18,9 +36,9 @@

Configuration

-
size
-
The output size of the string.
+
Length
+
Password length.
to
-
The property to set password.
+
The property to set generated password.
diff --git a/lib/PasswordGeneratorNode.ts b/lib/PasswordGeneratorNode.ts index cc81d6b..d7e61b1 100644 --- a/lib/PasswordGeneratorNode.ts +++ b/lib/PasswordGeneratorNode.ts @@ -15,7 +15,7 @@ export = (RED: nodered.NodeAPI): void => { RED.nodes.createNode(this, config); this.on("input", async (msg: any, send, done) => { - const password = await generatePassword(config.size); + const password = await generatePassword(config.length); const valueSetPath = msg.to || config.setTo || "payload"; msg = yutolity.setValue(msg, valueSetPath, password); send(msg); diff --git a/lib/PasswordGeneratorNodeDef.ts b/lib/PasswordGeneratorNodeDef.ts index 4b7fb14..1fa3ef4 100644 --- a/lib/PasswordGeneratorNodeDef.ts +++ b/lib/PasswordGeneratorNodeDef.ts @@ -2,12 +2,12 @@ import * as nodered from "node-red"; export interface PasswordGeneratorNodeDef extends nodered.NodeDef { - size: number; + length: number; setTo?: string; } export interface PasswordGeneratorNodeProperties extends nodered.EditorNodeProperties { - size: number; + length: number; setTo?: string; } diff --git a/lib/PasswordGeneratorNodeInit.ts b/lib/PasswordGeneratorNodeInit.ts index 64ee30c..dd59ab1 100644 --- a/lib/PasswordGeneratorNodeInit.ts +++ b/lib/PasswordGeneratorNodeInit.ts @@ -3,12 +3,13 @@ import { PasswordGeneratorNodeProperties } from "./PasswordGeneratorNodeDef"; declare const RED: nodered.EditorRED; const nodeName = "password-generator"; + RED.nodes.registerType(nodeName, { category: "function", color: "#a6bbcf", defaults: { name: { value: "" }, - size: { value: "", validate: RED.validators.number() }, + length: { value: "", required: true, validate: RED.validators.number() }, setTo: { value: "" }, }, inputs: 1, @@ -17,4 +18,4 @@ RED.nodes.registerType(nodeName, { label: function () { return this.name || nodeName; }, -}); \ No newline at end of file +}); diff --git a/test/PasswordGeneratorNode_spec.ts b/test/PasswordGeneratorNode_spec.ts index 3492ee6..e3b277b 100644 --- a/test/PasswordGeneratorNode_spec.ts +++ b/test/PasswordGeneratorNode_spec.ts @@ -1,6 +1,5 @@ import "mocha"; import { expect } from "chai"; -// import * as helper from "node-red-node-test-helper"; import helper = require("node-red-node-test-helper"); import valueChangeNode = require("../lib/PasswordGeneratorNode"); @@ -20,12 +19,12 @@ describe("PasswordGeneratorNode", () => { const nodeId = "node-id"; const outNodeId = "out-node-id"; - function createFlow(args?: { size?: number, setTo?: string }) { + function createFlow(args?: { length?: number, setTo?: string }) { return [ { id: nodeId, type: "password-generator", - size: args?.size || 10, + length: args?.length || 10, setTo: args?.setTo, name: "generator-name", wires: [[outNodeId]]