mirror of
https://github.com/yuto-yuto/node-red-contrib-password-generator.git
synced 2024-11-21 19:06:44 +01:00
fix
This commit is contained in:
parent
2e1cab23a4
commit
456f4332e3
@ -8,15 +8,15 @@ const AsciiRange = {
|
||||
max: 126,
|
||||
}
|
||||
|
||||
export async function generatePassword(size: number): Promise<string> {
|
||||
export async function generatePassword(length: number): Promise<string> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,33 @@
|
||||
<script type="text/javascript">
|
||||
const nodeName = "password-generator";
|
||||
RED.nodes.registerType(nodeName, {
|
||||
category: "function",
|
||||
color: "#D8BFD8",
|
||||
defaults: {
|
||||
name: { value: "" },
|
||||
length: { value: "", required: true, validate: RED.validators.number() },
|
||||
setTo: { value: "" },
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
icon: "font-awesome/fa-key",
|
||||
label: function () {
|
||||
return this.name || nodeName;
|
||||
},
|
||||
});
|
||||
</script>
|
||||
<script type="text/html" data-template-name="password-generator">
|
||||
<div class="form-row">
|
||||
<label for="node-input-key"><i class="fa fa-key"></i> Key prop</label>
|
||||
<input type="text" id="node-input-key" placeholder="payload.key">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-size"><i class="fa fa-database"></i> Size</label>
|
||||
<input type="text" id="node-input-size">
|
||||
<label for="node-input-length"> Length</label>
|
||||
<input type="text" id="node-input-length">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-to"><i class="fa fa-database"></i> to</label>
|
||||
<input type="text" id="node-input-to">
|
||||
<label for="node-input-setTo"><i class="fa fa-ellipsis-he"></i> to</label>
|
||||
<input type="text" id="node-input-setTo">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
@ -18,9 +36,9 @@
|
||||
|
||||
<h3>Configuration</h3>
|
||||
<dl class="message-properties">
|
||||
<dt>size</dt>
|
||||
<dd> The output size of the string. </dd>
|
||||
<dt>Length</dt>
|
||||
<dd> Password length. </dd>
|
||||
<dt>to</dt>
|
||||
<dd> The property to set password. </dd>
|
||||
<dd> The property to set generated password. </dd>
|
||||
</dl>
|
||||
</script>
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -3,12 +3,13 @@ import { PasswordGeneratorNodeProperties } from "./PasswordGeneratorNodeDef";
|
||||
|
||||
declare const RED: nodered.EditorRED;
|
||||
const nodeName = "password-generator";
|
||||
|
||||
RED.nodes.registerType<PasswordGeneratorNodeProperties>(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,
|
||||
|
@ -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]]
|
||||
|
Loading…
Reference in New Issue
Block a user