@aaqu/node-red-aaqu-pdf 0.4.4 → 0.4.6

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/README.md CHANGED
@@ -1,11 +1,16 @@
1
+ ## Security Disclaimer
2
+
3
+ > This package processes PDF data directly within the main execution context and does not utilize sandboxing, isolated workers, or any form of execution containment.
4
+ Accordingly, it provides **no warranties or representations**, whether express or implied, regarding safety, security, performance, or suitability for handling untrusted, user-supplied, or potentially malicious PDF content.
5
+ >
6
+ > The software is provided **“as is”**, without any guarantees of reliability or protection, and **no assurance** is given that it can safely process adversarial input.
7
+ Use in security-sensitive environments or with sensitive data is **not recommended** unless independent validation, isolation, and appropriate hardening measures are implemented.
1
8
  ## Important Information
2
9
 
3
10
  Your support in the ongoing development of this library would be sincerely appreciated. 🙂
4
11
 
5
12
  [![Buy Me a Coffee](https://img.shields.io/badge/Buy%20Me%20a%20Coffee-%23FFDD00?style=for-the-badge&logo=buymeacoffee&logoColor=black)](https://buymeacoffee.com/mazuralbert)
6
13
 
7
- _**The library requires testing and monitoring of RAM usage.**_
8
-
9
14
  # @aaqu/node-red-aaqu-pdf
10
15
 
11
16
  A [Node-RED](https://nodered.org/) node for operating on a PDF document.
@@ -5,35 +5,82 @@ module.exports = function (RED) {
5
5
  RED.nodes.createNode(this, config);
6
6
 
7
7
  this.pdfPage = config.pdfPage;
8
-
9
8
  const node = this;
10
9
 
11
10
  node.on("input", async function (msg) {
12
11
  try {
12
+ node.status({ fill: "blue", shape: "dot", text: "Processing PDF..." });
13
+
14
+ // Validate payload
15
+ if (!msg || typeof msg !== "object") {
16
+ throw new Error("Incoming msg is missing or not an object.");
17
+ }
18
+
19
+ if (!msg.payload) {
20
+ throw new Error(
21
+ "msg.payload is required and must contain a PDF file.",
22
+ );
23
+ }
24
+
25
+ // Payload must be a Buffer, Uint8Array or ArrayBuffer
26
+ const validTypes =
27
+ Buffer.isBuffer(msg.payload) ||
28
+ msg.payload instanceof Uint8Array ||
29
+ msg.payload instanceof ArrayBuffer;
30
+
31
+ if (!validTypes) {
32
+ throw new Error(
33
+ "msg.payload must be a Buffer, Uint8Array or ArrayBuffer containing a PDF.",
34
+ );
35
+ }
36
+
37
+ // Load PDF
13
38
  const pdfDoc = await PDFDocument.load(msg.payload);
14
- //const pages = pdfDoc.getPageCount();
15
- // console.log(pages);
39
+ const totalPages = pdfDoc.getPageCount();
16
40
 
17
- const newPDF = await PDFDocument.create();
41
+ // Determine page number
42
+ const rawPage = msg.pdfPage ?? node.pdfPage;
43
+ const pageNum = Number(rawPage);
18
44
 
19
- const copiedPages = await newPDF.copyPages(pdfDoc, [node.pdfPage - 1]);
45
+ if (!Number.isInteger(pageNum)) {
46
+ throw new Error(
47
+ `Invalid page number: '${rawPage}'. Must be an integer.`,
48
+ );
49
+ }
50
+
51
+ if (pageNum < 1 || pageNum > totalPages) {
52
+ throw new Error(
53
+ `Page number out of range: ${pageNum}. Valid range is 1–${totalPages}.`,
54
+ );
55
+ }
56
+
57
+ const pageIndex = pageNum - 1;
58
+
59
+ // Create output PDF with selected page only
60
+ const newPDF = await PDFDocument.create();
61
+ const copiedPages = await newPDF.copyPages(pdfDoc, [pageIndex]);
20
62
  const [page] = copiedPages;
21
63
 
22
64
  newPDF.addPage(page);
23
65
  const pdfBytes = await newPDF.save();
24
66
 
67
+ // Return result
25
68
  msg.payload = Buffer.from(pdfBytes);
69
+
70
+ node.status({ fill: "green", shape: "dot", text: "Page extracted" });
26
71
  node.send(msg);
27
72
  } catch (err) {
28
- this.status({
73
+ node.status({
29
74
  fill: "red",
30
- shape: "ring",
31
- text: "Error parsing PDF",
75
+ shape: "dot",
76
+ text: "Error extracting page",
32
77
  });
33
78
 
34
- node.error(err);
79
+ // Log error with msg context
80
+ node.error(err.message || err, msg);
35
81
  }
36
82
  });
37
83
  }
84
+
38
85
  RED.nodes.registerType("pdf-get-page", PDFGetPageNode);
39
86
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aaqu/node-red-aaqu-pdf",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
4
4
  "description": "node-red pdf creator",
5
5
  "scripts": {
6
6
  "node-red": "node-red"