node-red-contrib-tcp-escpos 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Copyright 2023 Doth-J
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
+
5
+ THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # node-red-contrib-tcp-escpos [![npm](https://img.shields.io/npm/v/node-red-contrib-tcp-escpos.svg)](https://www.npmjs.com/package/node-red-contrib-tcp-escpos) [<img height="20px" src="https://nodered.org/about/resources/media/node-red-hexagon.png">](https://flows.nodered.org/node/node-red-contrib-tcp-escpos)
@@ -0,0 +1,6 @@
1
+ <svg width="800" height="800" version="1.1" viewBox="0 0 211.67 211.67" xmlns="http://www.w3.org/2000/svg">
2
+ <g transform="matrix(.96078 0 0 .96078 4.1504 -8.1354)">
3
+ <rect x="50.333" y="64.934" width="111" height="132.41" rx="10.52" ry="10.52" fill="#1a1a1a" stroke-width="1.025" style="paint-order:markers fill stroke" />
4
+ <rect x="73.005" y="39.904" width="65.659" height="35.913" rx="0" ry="0" fill="#fff" stroke-width="1.2384" style="paint-order:markers fill stroke" />
5
+ </g>
6
+ </svg>
@@ -0,0 +1,62 @@
1
+ <script type="text/javascript" id="node-tcp-escpos">
2
+ RED.nodes.registerType('tcp escpos', {
3
+ category: 'network',
4
+ color: '#a9a9a9',
5
+ inputs: 1,
6
+ inputLabels: 'instructions',
7
+ icon: 'node.svg',
8
+ align: 'right',
9
+ defaults: {
10
+ name: { value: '' },
11
+ host: { value: '' },
12
+ type: { value: 'text' },
13
+ payload: { value: "I'm Mr. Printer.\nLook at me!" },
14
+ },
15
+ label: function () {
16
+ return this.name || 'tcp escpos'
17
+ },
18
+ })
19
+ </script>
20
+ <script type="text/html" data-template-name="tcp escpos">
21
+ <div class="form-row">
22
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
23
+ <input type="text" id="node-input-name" placeholder="Name" />
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-input-host"><i class="fa fa-dot-circle-o"></i> Host</label>
27
+ <input type="text" id="node-input-host" placeholder="192.168.1.101:9100" />
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-input-type"><i class="fa fa-play-circle"></i> Type</label>
31
+ <select id="node-input-type">
32
+ <option value="text">Text</option>
33
+ <option value="image">Image</option>
34
+ <option value="buffer">Buffer</option>
35
+ </select>
36
+ </div>
37
+ <div class="form-row">
38
+ <label for="node-input-payload"
39
+ ><i class="fa fa-dot-circle-o"></i> Payload</label
40
+ >
41
+ <textarea id="node-input-payload"></textarea>
42
+ </div>
43
+ </script>
44
+ <script type="text/html" data-help-name="tcp escpos">
45
+ <p>Typescript example Node-RED node</p>
46
+ <h3>Inputs</h3>
47
+ <dl class="message-properties">
48
+ <dt>
49
+ payload
50
+ <span class="property-type">boolean</span>
51
+ </dt>
52
+ <h3>Outputs</h3>
53
+ <dl class="message-properties">
54
+ <dt>
55
+ payload
56
+ <span class="property-type">object</span>
57
+ </dt>
58
+ <h3>Details</h3>
59
+ <p>Some more information about the node.</p>
60
+ </dl>
61
+ </dl>
62
+ </script>
package/nodes/node.js ADDED
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ const node_net_1 = __importDefault(require("node:net"));
15
+ const node_url_1 = require("node:url");
16
+ const connectTcp = (options) => new Promise((resolve, reject) => {
17
+ const socket = node_net_1.default.createConnection(options);
18
+ socket.once('connect', () => resolve(socket));
19
+ socket.once('error', (error) => reject(error));
20
+ });
21
+ const allowedTypes = ['text', 'image', 'buffer'];
22
+ module.exports = function (RED) {
23
+ function TcpEscposNode(configuration) {
24
+ RED.nodes.createNode(this, configuration);
25
+ this.on('close', () => {
26
+ this.status({});
27
+ });
28
+ this.on('input', (message, _send, done) => __awaiter(this, void 0, void 0, function* () {
29
+ const error = yield (() => __awaiter(this, void 0, void 0, function* () {
30
+ try {
31
+ const payload = (() => {
32
+ const type = allowedTypes.find((type) => type === message.type) ||
33
+ configuration.type;
34
+ const payload = message.payload || configuration.payload;
35
+ if (type === 'text') {
36
+ throw new Error('Text type not implemented yet');
37
+ }
38
+ if (type === 'image') {
39
+ throw new Error('Image type not implemented yet');
40
+ }
41
+ if (type === 'buffer') {
42
+ if (Array.isArray(payload)) {
43
+ return Buffer.from(payload);
44
+ }
45
+ return Buffer.from(payload, 'base64');
46
+ }
47
+ return type;
48
+ })();
49
+ this.status({ fill: 'yellow', shape: 'dot', text: 'connecting…' });
50
+ const hostname = message.host || configuration.host;
51
+ if (!hostname) {
52
+ throw new Error('Host is not defined');
53
+ }
54
+ const url = new node_url_1.URL(`tcp://${hostname}`);
55
+ const { hostname: host } = url;
56
+ const port = Number(url.port) || 9100;
57
+ const socket = yield connectTcp({ host, port });
58
+ this.status({ fill: 'yellow', shape: 'dot', text: 'sending…' });
59
+ yield new Promise((resolve, reject) => {
60
+ socket.write(payload, (error) => {
61
+ if (error) {
62
+ reject(error);
63
+ }
64
+ else {
65
+ resolve();
66
+ }
67
+ });
68
+ });
69
+ yield new Promise((resolve) => {
70
+ socket.end(resolve);
71
+ });
72
+ }
73
+ catch (error) {
74
+ if (error instanceof Error) {
75
+ return error;
76
+ }
77
+ }
78
+ return undefined;
79
+ }))();
80
+ if (error) {
81
+ this.status({ fill: 'red', shape: 'dot', text: 'failed' });
82
+ this.error(error.message, message);
83
+ }
84
+ else {
85
+ this.status({ fill: 'green', shape: 'dot', text: 'sent' });
86
+ }
87
+ done === null || done === void 0 ? void 0 : done(error);
88
+ }));
89
+ }
90
+ RED.nodes.registerType('tcp escpos', TcpEscposNode);
91
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "node-red-contrib-tcp-escpos",
3
+ "version": "0.0.1",
4
+ "description": "Escpos over TCP/IP node for Node-RED",
5
+ "devDependencies": {
6
+ "@types/node": "^18.14.0",
7
+ "@types/node-red": "^1.2.1",
8
+ "typescript": "^4.9.5"
9
+ },
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "prepare": "npm run build",
13
+ "dev": "tsc --watch",
14
+ "start": "npm run dev"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/FilipChalupa/node-red-contrib-tcp-escpos.git"
19
+ },
20
+ "keywords": [
21
+ "node-red",
22
+ "typescript",
23
+ "tcp",
24
+ "escpos",
25
+ "printer"
26
+ ],
27
+ "author": "Filip Chalupa",
28
+ "license": "ISC",
29
+ "files": [
30
+ "nodes"
31
+ ],
32
+ "engines": {
33
+ "node": ">=18.0.0"
34
+ },
35
+ "node-red": {
36
+ "version": ">=4.0.0",
37
+ "nodes": {
38
+ "node": "nodes/node.js"
39
+ }
40
+ }
41
+ }