Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
1359 lines (1219 loc) · 40.3 KB
/
Copy pathserver.js
File metadata and controls
1359 lines (1219 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const http = require("http");
const path = require("path");
const fs = require("fs/promises");
const dns = require("dns").promises;
const net = require("net");
const os = require("os");
const { exec, execFile } = require("child_process");
const { randomUUID } = require("crypto");
const { URL } = require("url");
const PORT = process.env.PORT || 3210;
const HOST = process.env.HOST || "127.0.0.1";
const PUBLIC_DIR = path.join(__dirname, "public");
const DEFAULT_PORTS = [631, 9100, 80, 443];
const DEFAULT_JSON_BODY_LIMIT = 64 * 1024;
const MAX_PRINT_FILE_BYTES = 20 * 1024 * 1024;
const PAPER_STATUS_OID = "1.3.6.1.2.1.43.8.2.1.10";
const PAPER_LEVEL_OID = "1.3.6.1.2.1.43.8.2.1.9";
const sessions = new Map();
function nowIso() {
return new Date().toISOString();
}
function sendJson(res, statusCode, body) {
const payload = JSON.stringify(body);
res.writeHead(statusCode, {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": Buffer.byteLength(payload),
"Cache-Control": "no-store"
});
res.end(payload);
}
function sendText(res, statusCode, text) {
res.writeHead(statusCode, {
"Content-Type": "text/plain; charset=utf-8",
"Content-Length": Buffer.byteLength(text)
});
res.end(text);
}
function isValidHost(input) {
if (typeof input !== "string") {
return false;
}
const host = input.trim();
if (!host || host.length > 253) {
return false;
}
// Basic allow-list for hostnames and IP-like strings.
return /^[A-Za-z0-9.-]+$/.test(host);
}
async function readJsonBody(req, maxBytes = DEFAULT_JSON_BODY_LIMIT) {
let raw = "";
for await (const chunk of req) {
raw += chunk;
if (raw.length > maxBytes) {
throw new Error("Body demasiado grande");
}
}
if (!raw) {
return {};
}
try {
return JSON.parse(raw);
} catch (error) {
throw new Error("JSON inválido");
}
}
function parsePorts(input) {
const ports = Array.isArray(input) ? input : DEFAULT_PORTS;
const parsed = ports
.map((value) => Number(value))
.filter((port) => Number.isInteger(port) && port >= 1 && port <= 65535);
return parsed.length > 0 ? [...new Set(parsed)] : DEFAULT_PORTS;
}
function parseHostFromDeviceUri(deviceUri) {
if (typeof deviceUri !== "string" || !deviceUri.trim()) {
return null;
}
const raw = deviceUri.trim();
try {
const parsed = new URL(raw);
return parsed.hostname || null;
} catch (error) {
const ipv4 = raw.match(/\b\d{1,3}(?:\.\d{1,3}){3}\b/);
return ipv4 ? ipv4[0] : null;
}
}
function normalizeQueueName(queueName) {
if (typeof queueName !== "string") {
return "";
}
const value = queueName.trim();
return /^[\w.-]{1,120}$/.test(value) ? value : "";
}
function sanitizeOptionKey(key) {
if (typeof key !== "string") {
return null;
}
const value = key.trim();
return /^[A-Za-z][A-Za-z0-9-]*$/.test(value) ? value : null;
}
function sanitizeOptionValue(value) {
if (typeof value !== "string") {
return null;
}
const normalized = value.trim();
return /^[A-Za-z0-9_.:-]+$/.test(normalized) ? normalized : null;
}
function parseCupsOptionChoices(text) {
const tokens = String(text || "").trim().split(/\s+/).filter(Boolean);
const options = [];
let defaultValue = null;
tokens.forEach((token) => {
const raw = token.startsWith("*") ? token.slice(1) : token;
if (!raw) {
return;
}
options.push(raw);
if (token.startsWith("*")) {
defaultValue = raw;
}
});
return { options: [...new Set(options)], defaultValue };
}
function parseCapabilitiesOutput(output) {
const raw = {};
String(output || "")
.split(/\r?\n/)
.filter((line) => line.trim())
.forEach((line) => {
const match = line.match(/^([^\/:\s]+)(?:\/([^:]+))?:\s*(.+)$/);
if (!match) {
return;
}
const key = match[1];
const label = match[2] ? match[2].trim() : key;
const values = parseCupsOptionChoices(match[3]);
raw[key.toLowerCase()] = {
key,
label,
options: values.options,
defaultValue: values.defaultValue
};
});
const pickByKeys = (keys) => {
for (const key of keys) {
if (raw[key]) {
return raw[key];
}
}
return null;
};
return {
orientation: pickByKeys(["orientation-requested"]),
pageSize: pickByKeys(["pagesize", "media"]),
color: pickByKeys(["print-color-mode", "colormodel", "outputmode"]),
inputSlot: pickByKeys(["inputslot"]),
sides: pickByKeys(["sides"]),
rawOptions: raw
};
}
async function listSystemPrinters() {
const [statusResult, deviceResult] = await Promise.all([
execFileResult("lpstat", ["-p", "-d"], 10_000),
execFileResult("lpstat", ["-v"], 10_000)
]);
const combinedText = [
statusResult.stdout,
statusResult.stderr,
deviceResult.stdout,
deviceResult.stderr
].join("\n");
if (combinedText.toLowerCase().includes("scheduler is not running")) {
throw new Error("CUPS scheduler no está activo. Inicia CUPS (`sudo service cups start`).");
}
if (!statusResult.ok && !statusResult.stdout.trim() && !deviceResult.stdout.trim()) {
throw new Error(statusResult.stderr.trim() || "No fue posible consultar impresoras del sistema.");
}
const printers = new Map();
const defaultMatch = combinedText.match(/system default destination:\s*(\S+)/i);
const defaultPrinter = defaultMatch ? defaultMatch[1] : null;
statusResult.stdout.split(/\r?\n/).forEach((line) => {
const match = line.match(/^printer\s+(\S+)\s+is\s+(.+)$/i);
if (!match) {
return;
}
const name = match[1];
const statusText = match[2].trim();
const existing = printers.get(name) || { name };
existing.statusText = statusText;
existing.isDefault = name === defaultPrinter;
printers.set(name, existing);
});
deviceResult.stdout.split(/\r?\n/).forEach((line) => {
const match = line.match(/^device for\s+(\S+):\s*(.+)$/i);
if (!match) {
return;
}
const name = match[1];
const deviceUri = match[2].trim();
const existing = printers.get(name) || { name };
existing.deviceUri = deviceUri;
existing.host = parseHostFromDeviceUri(deviceUri);
existing.isDefault = name === defaultPrinter;
printers.set(name, existing);
});
const items = Array.from(printers.values()).sort((a, b) => {
if (a.isDefault && !b.isDefault) {
return -1;
}
if (!a.isDefault && b.isDefault) {
return 1;
}
return a.name.localeCompare(b.name);
});
return {
defaultPrinter,
printers: items,
source: "cups"
};
}
async function getPrinterCapabilities(printerName) {
const safeName = normalizeQueueName(printerName);
if (!safeName) {
throw new Error("Nombre de impresora/cola inválido.");
}
const result = await execFileResult("lpoptions", ["-p", safeName, "-l"], 12_000);
if (!result.ok && !result.stdout.trim()) {
throw new Error(result.stderr.trim() || `No fue posible leer capacidades de ${safeName}.`);
}
const capabilities = parseCapabilitiesOutput(result.stdout);
return {
printerName: safeName,
capabilities
};
}
function execFileResult(command, args, timeout = 12000) {
return new Promise((resolve) => {
execFile(
command,
args,
{
timeout,
maxBuffer: 4 * 1024 * 1024
},
(error, stdout, stderr) => {
resolve({
ok: !error,
error,
stdout: stdout || "",
stderr: stderr || "",
command,
args
});
}
);
});
}
function isMissingCommand(error) {
return Boolean(error && (error.code === "ENOENT" || error.code === 127));
}
function extractIntegers(text) {
return String(text || "")
.split(/\r?\n/)
.map((line) => {
const match = line.match(/-?\d+/);
return match ? Number(match[0]) : null;
})
.filter((value) => Number.isInteger(value));
}
function mapInputStatusCode(code) {
const map = {
"-3": "some",
"-2": "unknownVendor",
1: "other",
2: "unknown",
3: "available",
4: "availableStandby",
5: "notAvailable"
};
return map[code] || `code:${code}`;
}
function sanitizeFileName(fileName) {
const normalized = path.basename(String(fileName || "documento"));
const cleaned = normalized.replace(/[^\w.-]/g, "_");
const safe = cleaned.length > 0 ? cleaned : "documento";
return safe.slice(0, 120);
}
function resolveColorIntent(printOptions) {
const options = printOptions && typeof printOptions === "object" ? printOptions : {};
if (options.colorMode === "mono" || options.colorMode === "color") {
return options.colorMode;
}
const candidate = String(options.colorOptionValue || "").toLowerCase();
if (!candidate) {
return null;
}
if (/(gray|grey|mono|black|bw|grayscale|greyscale|kgray|fastgray)/.test(candidate)) {
return "mono";
}
if (/(color|colour|rgb|cmyk)/.test(candidate)) {
return "color";
}
return null;
}
function isRawFriendlyExtension(fileName) {
const ext = path.extname(String(fileName || "")).toLowerCase();
const supported = new Set([".prn", ".pcl", ".pclm", ".ps", ".txt"]);
return supported.has(ext);
}
function decodeBase64Payload(payload) {
if (typeof payload !== "string" || !payload.trim()) {
throw new Error("No se recibió contenido de archivo.");
}
const clean = payload.trim().replace(/\s/g, "");
const buffer = Buffer.from(clean, "base64");
if (!buffer.length) {
throw new Error("Archivo vacío o base64 inválido.");
}
return buffer;
}
function escapePowershellLiteral(value) {
return `'${String(value).replace(/'/g, "''")}'`;
}
async function probePaperStatusSnmp(host) {
const statusResult = await execFileResult("snmpwalk", ["-v1", "-c", "public", "-OQv", host, PAPER_STATUS_OID], 9000);
if (statusResult.error && isMissingCommand(statusResult.error)) {
return {
supported: false,
paperState: "unknown",
message: "No fue posible validar papel: falta `snmpwalk` en el sistema."
};
}
if (!statusResult.ok && !statusResult.stdout.trim()) {
return {
supported: true,
paperState: "unknown",
message: "SNMP no respondió. Habilita SNMP en la impresora o usa HP Smart para validar bandeja."
};
}
const levelResult = await execFileResult("snmpwalk", ["-v1", "-c", "public", "-OQv", host, PAPER_LEVEL_OID], 9000);
const statusCodes = extractIntegers(statusResult.stdout);
const levels = extractIntegers(levelResult.stdout);
const mappedStatus = statusCodes.map(mapInputStatusCode);
const emptyByStatus = statusCodes.some((code) => code === 5);
const emptyByLevel = levels.some((value) => value === 0);
const hasAvailable = statusCodes.some((code) => code === 3 || code === 4);
const hasPositiveLevel = levels.some((value) => value > 0);
const hasVendorUnknown = statusCodes.some((code) => code < 0) || levels.some((value) => value < 0);
if (emptyByStatus || emptyByLevel) {
return {
supported: true,
paperState: "no_paper",
message: "La bandeja principal parece sin papel o no disponible.",
details: { trayStatus: mappedStatus, trayLevels: levels }
};
}
if (hasAvailable) {
return {
supported: true,
paperState: "paper_ok",
message: "La impresora reporta bandeja disponible.",
details: { trayStatus: mappedStatus, trayLevels: levels }
};
}
if (hasPositiveLevel) {
return {
supported: true,
paperState: "paper_ok",
message: "La impresora reporta nivel de hojas mayor a 0.",
details: { trayStatus: mappedStatus, trayLevels: levels }
};
}
if (hasVendorUnknown) {
return {
supported: true,
paperState: "unknown",
message: "El modelo responde SNMP pero no expone estado de papel confiable (valor vendor como -2/-3).",
details: { trayStatus: mappedStatus, trayLevels: levels }
};
}
return {
supported: true,
paperState: "unknown",
message: "No se pudo confirmar papel con precisión (estado SNMP ambiguo).",
details: { trayStatus: mappedStatus, trayLevels: levels }
};
}
async function probePaperStatusCups(printerName) {
const safeName = normalizeQueueName(printerName);
if (!safeName) {
return {
supported: false,
paperState: "unknown",
message: "Nombre de cola inválido para consultar estado en CUPS."
};
}
const result = await execFileResult("lpstat", ["-l", "-p", safeName], 10_000);
const text = `${result.stdout}\n${result.stderr}`;
const lower = text.toLowerCase();
if (lower.includes("scheduler is not running")) {
return {
supported: false,
paperState: "unknown",
message: "CUPS no está activo para consultar alertas de papel."
};
}
if (lower.includes("unknown destination")) {
return {
supported: false,
paperState: "unknown",
message: `La cola ${safeName} no existe en CUPS.`
};
}
const noPaperSignals = [
"media-empty",
"paper-out",
"out of paper",
"media needed",
"input tray empty"
];
if (noPaperSignals.some((signal) => lower.includes(signal))) {
return {
supported: true,
paperState: "no_paper",
message: "CUPS reporta alerta de papel vacío.",
details: { printerName: safeName, raw: text.trim() }
};
}
const alertsLine = text.split(/\r?\n/).find((line) => /^alerts:/i.test(line.trim())) || "";
const isAlertsNone = /alerts:\s*(none|ninguna|sin alertas)/i.test(alertsLine);
if (isAlertsNone) {
return {
supported: true,
paperState: "paper_ok",
message: "CUPS no reporta alerta de papel para la cola seleccionada.",
details: { printerName: safeName, alerts: alertsLine.trim() }
};
}
return {
supported: true,
paperState: "unknown",
message: "CUPS no reporta un estado concluyente de papel.",
details: { printerName: safeName, alerts: alertsLine.trim() || null }
};
}
function mergePaperStatus(snmpPaper, cupsPaper) {
if (!snmpPaper && !cupsPaper) {
return {
source: "none",
supported: false,
paperState: "unknown",
message: "Sin fuente de estado de papel disponible."
};
}
if (snmpPaper && snmpPaper.paperState !== "unknown") {
return { ...snmpPaper, source: "snmp" };
}
if (cupsPaper && cupsPaper.paperState !== "unknown") {
return { ...cupsPaper, source: "cups" };
}
if (snmpPaper) {
return { ...snmpPaper, source: "snmp" };
}
return { ...cupsPaper, source: "cups" };
}
async function detectPrinterStatus({ host, printerName }) {
const hasHost = Boolean(host);
const hasPrinter = Boolean(printerName);
const [dnsLookup, port631, port9100, snmpPaper, cupsPaper] = await Promise.all([
hasHost ? lookupHost(host) : Promise.resolve(null),
hasHost ? probeTcp(host, 631, 1800) : Promise.resolve(null),
hasHost ? probeTcp(host, 9100, 1800) : Promise.resolve(null),
hasHost ? probePaperStatusSnmp(host) : Promise.resolve(null),
hasPrinter ? probePaperStatusCups(printerName) : Promise.resolve(null)
]);
const paper = mergePaperStatus(snmpPaper, cupsPaper);
return {
host,
printerName: printerName || null,
timestamp: nowIso(),
dnsLookup,
connectivity: {
ipp631: port631,
raw9100: port9100
},
paper,
paperSources: {
snmp: snmpPaper,
cups: cupsPaper
}
};
}
function appendUnixPrintOptions(lpArgs, printOptions) {
const options = printOptions && typeof printOptions === "object" ? printOptions : {};
const orientationMap = {
portrait: "3",
landscape: "4",
"reverse-portrait": "5",
"reverse-landscape": "6"
};
const appliedOptions = [];
const seen = new Set();
const normalizedColorIntent = resolveColorIntent(options);
const pushOption = (keyInput, valueInput) => {
const key = sanitizeOptionKey(keyInput);
const value = sanitizeOptionValue(valueInput);
if (!key || !value) {
return;
}
const dedupeKey = `${key.toLowerCase()}=${value.toLowerCase()}`;
if (seen.has(dedupeKey)) {
return;
}
seen.add(dedupeKey);
lpArgs.push("-o", `${key}=${value}`);
appliedOptions.push(`${key}=${value}`);
};
if (orientationMap[options.orientation]) {
pushOption("orientation-requested", orientationMap[options.orientation]);
}
if (options.pageSize) {
pushOption(options.pageSizeOptionKey || "media", options.pageSize);
}
if (options.inputSlot) {
pushOption(options.inputSlotOptionKey || "InputSlot", options.inputSlot);
}
if (options.sides) {
pushOption(options.sidesOptionKey || "sides", options.sides);
}
if (options.colorOptionKey && options.colorOptionValue) {
pushOption(options.colorOptionKey, options.colorOptionValue);
}
if (normalizedColorIntent === "mono") {
// Different HP/CUPS stacks honor different keys for monochrome.
pushOption("print-color-mode", "monochrome");
pushOption("ColorModel", "Gray");
pushOption("ColorModel", "Grayscale");
pushOption("OutputMode", "Monochrome");
pushOption("HPColorMode", "grayscale");
pushOption("cupsColorSpace", "17");
} else if (normalizedColorIntent === "color") {
pushOption("print-color-mode", "color");
pushOption("ColorModel", "RGB");
pushOption("OutputMode", "Color");
pushOption("HPColorMode", "color");
}
return appliedOptions;
}
async function maybePrepareMonochromeFile(filePath, fileName, printOptions) {
const colorIntent = resolveColorIntent(printOptions);
if (colorIntent !== "mono") {
return {
filePath,
extraFiles: [],
note: null,
applied: false
};
}
const ext = path.extname(String(fileName || "")).toLowerCase();
if (ext !== ".pdf") {
return {
filePath,
extraFiles: [],
note: "Modo B/N solicitado. La conversión forzada solo aplica automáticamente a PDF.",
applied: false
};
}
const grayPath = path.join(os.tmpdir(), `printer-ui-gray-${randomUUID()}.pdf`);
const gsArgs = [
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
"-dNOPAUSE",
"-dBATCH",
"-dSAFER",
"-sColorConversionStrategy=Gray",
"-dProcessColorModel=/DeviceGray",
"-dAutoFilterColorImages=false",
"-dAutoFilterGrayImages=false",
"-sColorImageFilter=FlateEncode",
"-sGrayImageFilter=FlateEncode",
`-sOutputFile=${grayPath}`,
filePath
];
const gsResult = await execFileResult("gs", gsArgs, 120_000);
if (gsResult.ok) {
return {
filePath: grayPath,
extraFiles: [grayPath],
note: "PDF convertido a escala de grises antes de enviar a la impresora.",
applied: true
};
}
await fs.unlink(grayPath).catch(() => {});
if (isMissingCommand(gsResult.error)) {
return {
filePath,
extraFiles: [],
note: "No se pudo forzar escala de grises porque falta `ghostscript` (`gs`).",
applied: false
};
}
return {
filePath,
extraFiles: [],
note: `Falló conversión a B/N previa (ghostscript): ${gsResult.stderr.trim() || gsResult.error.message}`,
applied: false
};
}
async function printWithUnix(filePath, copies, printerName, printOptions) {
const lpArgs = [];
if (printerName) {
lpArgs.push("-d", printerName);
}
if (copies > 1) {
lpArgs.push("-n", String(copies));
}
const appliedOptions = appendUnixPrintOptions(lpArgs, printOptions);
lpArgs.push(filePath);
const lpResult = await execFileResult("lp", lpArgs, 60_000);
if (lpResult.ok) {
return {
method: "lp",
stdout: lpResult.stdout.trim(),
stderr: lpResult.stderr.trim(),
appliedOptions
};
}
if (!isMissingCommand(lpResult.error)) {
throw new Error(lpResult.stderr.trim() || lpResult.error.message || "Falló comando lp.");
}
const lprArgs = [];
if (printerName) {
lprArgs.push("-P", printerName);
}
if (copies > 1) {
lprArgs.push("-#", String(copies));
}
lprArgs.push(filePath);
const lprResult = await execFileResult("lpr", lprArgs, 60_000);
if (!lprResult.ok) {
if (isMissingCommand(lprResult.error)) {
throw new Error("No hay comandos de impresión disponibles (`lp`/`lpr`) en este sistema.");
}
throw new Error(lprResult.stderr.trim() || lprResult.error.message || "Falló comando lpr.");
}
return {
method: "lpr",
stdout: lprResult.stdout.trim(),
stderr: lprResult.stderr.trim(),
note: "Opciones avanzadas no se aplican con fallback `lpr`; usa `lp` para control fino.",
appliedOptions: []
};
}
async function printWithWindows(filePath, printerName, copies) {
const printerLiteral = printerName ? escapePowershellLiteral(printerName) : null;
const fileLiteral = escapePowershellLiteral(filePath);
const safeCopies = Number.isInteger(copies) ? Math.min(Math.max(copies, 1), 30) : 1;
const command = printerLiteral
? `$ErrorActionPreference='Stop'; for($i=0; $i -lt ${safeCopies}; $i++){ $p = Start-Process -FilePath ${fileLiteral} -Verb PrintTo -ArgumentList ${printerLiteral} -PassThru; Wait-Process -Id $p.Id -Timeout 45 }`
: `$ErrorActionPreference='Stop'; for($i=0; $i -lt ${safeCopies}; $i++){ $p = Start-Process -FilePath ${fileLiteral} -Verb Print -PassThru; Wait-Process -Id $p.Id -Timeout 45 }`;
const result = await execFileResult("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", command], 70_000);
if (!result.ok) {
if (isMissingCommand(result.error)) {
throw new Error("No se encontró PowerShell para imprimir en Windows.");
}
throw new Error(result.stderr.trim() || result.error.message || "Falló impresión con PowerShell.");
}
return {
method: "powershell",
stdout: result.stdout.trim(),
stderr: result.stderr.trim()
};
}
async function submitPrintJob({ host, fileName, fileDataBase64, copies, printerName, printOptions }) {
const safeFileName = sanitizeFileName(fileName);
const fileBuffer = decodeBase64Payload(fileDataBase64);
if (fileBuffer.length > MAX_PRINT_FILE_BYTES) {
throw new Error(`El archivo excede el límite de ${Math.round(MAX_PRINT_FILE_BYTES / (1024 * 1024))} MB.`);
}
const safeCopies = Number.isInteger(copies) ? Math.min(Math.max(copies, 1), 30) : 1;
const queue = typeof printerName === "string" ? printerName.trim() : "";
const extension = path.extname(safeFileName) || ".bin";
const tempPath = path.join(os.tmpdir(), `printer-ui-${randomUUID()}${extension}`);
const tempFiles = [tempPath];
const notes = [];
let printFilePath = tempPath;
await fs.writeFile(tempPath, fileBuffer);
try {
const monoPrep = await maybePrepareMonochromeFile(tempPath, safeFileName, printOptions);
if (monoPrep.filePath !== tempPath) {
printFilePath = monoPrep.filePath;
tempFiles.push(...monoPrep.extraFiles);
}
if (monoPrep.note) {
notes.push(monoPrep.note);
}
let printResult;
try {
printResult = os.platform() === "win32"
? await printWithWindows(printFilePath, queue, safeCopies)
: await printWithUnix(printFilePath, safeCopies, queue, printOptions);
} catch (error) {
const isNoSystemPrinter =
String(error && error.message ? error.message : "").includes("No hay comandos de impresión disponibles")
|| String(error && error.message ? error.message : "").includes("No se encontró PowerShell");
if (!isNoSystemPrinter) {
throw error;
}
if (!isRawFriendlyExtension(safeFileName)) {
throw new Error(
"No hay spooler local (`lp/lpr`) y el modo RAW 9100 no es confiable para este tipo de archivo. " +
"Instala CUPS (`lp`/`lpr`) o imprime por HP Smart/controlador del sistema."
);
}
if (!host) {
throw new Error("No hay spooler local y falta host/IP para fallback RAW 9100.");
}
// Fallback when local spooler tools are unavailable:
// send bytes directly to JetDirect/RAW port 9100 on the printer.
printResult = await printWithRawSocket(host, fileBuffer, safeCopies);
}
return {
fileName: safeFileName,
sizeBytes: fileBuffer.length,
copies: safeCopies,
queue: queue || null,
note: notes.length > 0 ? notes.join(" ") : null,
...printResult
};
} finally {
const uniqueTempFiles = [...new Set(tempFiles)];
await Promise.all(uniqueTempFiles.map((file) => fs.unlink(file).catch(() => {})));
}
}
function writeBufferToRawSocket(host, port, buffer, timeoutMs = 15_000) {
return new Promise((resolve, reject) => {
const started = Date.now();
const socket = new net.Socket();
let done = false;
const finish = (error) => {
if (done) {
return;
}
done = true;
socket.destroy();
if (error) {
reject(error);
return;
}
resolve({ elapsedMs: Date.now() - started });
};
socket.setTimeout(timeoutMs);
socket.once("timeout", () => finish(new Error("Timeout enviando datos al puerto 9100.")));
socket.once("error", (error) => finish(error));
socket.connect(port, host, () => {
socket.write(buffer, (error) => {
if (error) {
finish(error);
return;
}
socket.end(() => finish(null));
});
});
});
}
async function printWithRawSocket(host, fileBuffer, copies) {
const rawPort = await probeTcp(host, 9100, 2200);
if (!rawPort.ok) {
throw new Error("No hay spooler local y la impresora no aceptó conexión RAW (9100).");
}
let lastElapsed = 0;
for (let i = 0; i < copies; i += 1) {
const sent = await writeBufferToRawSocket(host, 9100, fileBuffer);
lastElapsed = sent.elapsedMs;
}
return {
method: "raw-9100",
stdout: `Enviado por socket RAW a ${host}:9100`,
stderr: "",
elapsedMs: lastElapsed,
note: "Este modo depende de que la impresora soporte el formato enviado (por ejemplo PDF)."
};
}
function parsePingOutput(text) {
const normalized = text.replace(/\r/g, "");
const unixAvg = normalized.match(/(?:min\/avg\/max(?:\/(?:mdev|stddev))?)\s*=\s*[\d.]+\/([\d.]+)\//i);
if (unixAvg) {
return { avgMs: Number(unixAvg[1]), packetLoss: parsePacketLoss(normalized) };
}
const windowsAvg = normalized.match(/Average\s*=\s*(\d+)\s*ms/i);
if (windowsAvg) {
return { avgMs: Number(windowsAvg[1]), packetLoss: parsePacketLoss(normalized) };
}
return { avgMs: null, packetLoss: parsePacketLoss(normalized) };
}
function parsePacketLoss(text) {
const unixLoss = text.match(/(\d+(?:\.\d+)?)%\s*packet loss/i);
if (unixLoss) {
return Number(unixLoss[1]);
}
const windowsLoss = text.match(/Lost\s*=\s*\d+\s*\((\d+)%\s*loss\)/i);
if (windowsLoss) {
return Number(windowsLoss[1]);
}
return null;
}
async function runPing(host, count = 3, timeoutMs = 1500) {
const isWindows = os.platform() === "win32";
const timeoutSec = Math.max(1, Math.ceil(timeoutMs / 1000));
const command = isWindows
? `ping -n ${count} -w ${timeoutMs} ${host}`
: `ping -c ${count} -W ${timeoutSec} ${host}`;
return new Promise((resolve) => {
const started = Date.now();
exec(command, { timeout: timeoutMs * count + 2500 }, (error, stdout, stderr) => {
const elapsedMs = Date.now() - started;
const parsed = parsePingOutput(`${stdout}\n${stderr}`);
resolve({
ok: !error,
avgMs: parsed.avgMs,
packetLoss: parsed.packetLoss,
elapsedMs,
command,
error: error ? error.message : null
});
});
});
}
async function lookupHost(host) {
const started = Date.now();
try {
const result = await dns.lookup(host);
return {
ok: true,
address: result.address,
family: result.family,
elapsedMs: Date.now() - started,
error: null
};
} catch (error) {
return {
ok: false,
address: null,
family: null,
elapsedMs: Date.now() - started,
error: error.message
};
}
}
function probeTcp(host, port, timeoutMs = 2200) {
return new Promise((resolve) => {
const started = Date.now();
const socket = new net.Socket();
let settled = false;
const finish = (ok, errorMessage) => {
if (settled) {
return;
}
settled = true;
socket.destroy();
resolve({
port,
ok,
elapsedMs: Date.now() - started,
error: errorMessage || null
});
};
socket.setTimeout(timeoutMs);
socket.once("connect", () => finish(true));
socket.once("timeout", () => finish(false, "Timeout"));
socket.once("error", (error) => finish(false, error.code || error.message));
socket.connect(port, host);
});
}
function buildRecommendations(diagnostics) {
const tips = [];
const { dnsLookup, ping, tcp } = diagnostics;
const openPorts = tcp.filter((entry) => entry.ok);
const closedPorts = tcp.filter((entry) => !entry.ok);
if (!dnsLookup.ok) {
tips.push("No se resolvió el hostname. Prueba con la IP fija de la impresora para evitar búsquedas lentas.");
} else if (dnsLookup.elapsedMs > 120) {
tips.push("La resolución DNS tardó más de lo ideal. Configurar IP estática suele mejorar el primer intento de impresión.");
}
if (ping.ok && ping.avgMs !== null && ping.avgMs > 80) {
tips.push("El ping promedio es alto. Acerca impresora y router o usa banda 2.4 GHz estable para menor latencia.");
}
if (ping.packetLoss !== null && ping.packetLoss > 0) {
tips.push("Hay pérdida de paquetes. Revisa interferencia Wi-Fi o saturación de red para evitar colas lentas.");
}
if (openPorts.length === 0) {
tips.push("No respondió ningún puerto común (631/9100/80/443). Verifica que la impresora esté en la misma red.");
} else {
const slowestOpen = openPorts.reduce((acc, item) => (item.elapsedMs > acc.elapsedMs ? item : acc), openPorts[0]);
if (slowestOpen.elapsedMs > 700) {
tips.push("La impresora parece entrar en suspensión profunda. Activa Fast Path para mantenerla despierta.");
}
}
const has631 = openPorts.some((entry) => entry.port === 631);
const has9100 = openPorts.some((entry) => entry.port === 9100);
if (!has631 && has9100) {