-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathqz-tray.js
2860 lines (2581 loc) · 139 KB
/
qz-tray.js
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
'use strict';
/**
* @version 2.2.5-SNAPSHOT
* @overview QZ Tray Connector
* @license LGPL-2.1-only
* <p/>
* Connects a web client to the QZ Tray software.
* Enables printing and device communication from javascript.
*/
var qz = (function() {
///// POLYFILLS /////
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
if (!Number.isInteger) {
Number.isInteger = function(value) {
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
};
}
///// PRIVATE METHODS /////
var _qz = {
VERSION: "2.2.5-SNAPSHOT", //must match @version above
DEBUG: false,
log: {
/** Debugging messages */
trace: function() { if (_qz.DEBUG) { console.log.apply(console, arguments); } },
/** General messages */
info: function() { console.info.apply(console, arguments); },
/** General warnings */
warn: function() { console.warn.apply(console, arguments); },
/** Debugging errors */
allay: function() { if (_qz.DEBUG) { console.warn.apply(console, arguments); } },
/** General errors */
error: function() { console.error.apply(console, arguments); }
},
//stream types
streams: {
serial: 'SERIAL', usb: 'USB', hid: 'HID', printer: 'PRINTER', file: 'FILE', socket: 'SOCKET'
},
websocket: {
/** The actual websocket object managing the connection. */
connection: null,
/** Track if a connection attempt is being cancelled. */
shutdown: false,
/** Default parameters used on new connections. Override values using options parameter on {@link qz.websocket.connect}. */
connectConfig: {
host: ["localhost", "localhost.qz.io"], //hosts QZ Tray can be running on
hostIndex: 0, //internal var - index on host array
usingSecure: true, //boolean use of secure protocol
protocol: {
secure: "wss://", //secure websocket
insecure: "ws://" //insecure websocket
},
port: {
secure: [8181, 8282, 8383, 8484], //list of secure ports QZ Tray could be listening on
insecure: [8182, 8283, 8384, 8485], //list of insecure ports QZ Tray could be listening on
portIndex: 0 //internal var - index on active port array
},
keepAlive: 60, //time between pings to keep connection alive, in seconds
retries: 0, //number of times to reconnect before failing
delay: 0 //seconds before firing a connection
},
setup: {
/** Loop through possible ports to open connection, sets web socket calls that will settle the promise. */
findConnection: function(config, resolve, reject) {
if (_qz.websocket.shutdown) {
reject(new Error("Connection attempt cancelled by user"));
return;
}
//force flag if missing ports
if (!config.port.secure.length) {
if (!config.port.insecure.length) {
reject(new Error("No ports have been specified to connect over"));
return;
} else if (config.usingSecure) {
_qz.log.error("No secure ports specified - forcing insecure connection");
config.usingSecure = false;
}
} else if (!config.port.insecure.length && !config.usingSecure) {
_qz.log.trace("No insecure ports specified - forcing secure connection");
config.usingSecure = true;
}
var deeper = function() {
if (_qz.websocket.shutdown) {
//connection attempt was cancelled, bail out
reject(new Error("Connection attempt cancelled by user"));
return;
}
config.port.portIndex++;
if ((config.usingSecure && config.port.portIndex >= config.port.secure.length)
|| (!config.usingSecure && config.port.portIndex >= config.port.insecure.length)) {
if (config.hostIndex >= config.host.length - 1) {
//give up, all hope is lost
reject(new Error("Unable to establish connection with QZ"));
return;
} else {
config.hostIndex++;
config.port.portIndex = 0;
}
}
// recursive call until connection established or all ports are exhausted
_qz.websocket.setup.findConnection(config, resolve, reject);
};
var address;
if (config.usingSecure) {
address = config.protocol.secure + config.host[config.hostIndex] + ":" + config.port.secure[config.port.portIndex];
} else {
address = config.protocol.insecure + config.host[config.hostIndex] + ":" + config.port.insecure[config.port.portIndex];
}
try {
_qz.log.trace("Attempting connection", address);
_qz.websocket.connection = new _qz.tools.ws(address);
}
catch(err) {
_qz.log.error(err);
deeper();
return;
}
if (_qz.websocket.connection != null) {
_qz.websocket.connection.established = false;
//called on successful connection to qz, begins setup of websocket calls and resolves connect promise after certificate is sent
_qz.websocket.connection.onopen = function(evt) {
if (!_qz.websocket.connection.established) {
_qz.log.trace(evt);
_qz.log.info("Established connection with QZ Tray on " + address);
_qz.websocket.setup.openConnection({ resolve: resolve, reject: reject });
if (config.keepAlive > 0) {
var interval = setInterval(function() {
if (!_qz.tools.isActive() || _qz.websocket.connection.interval !== interval) {
clearInterval(interval);
return;
}
_qz.websocket.connection.send("ping");
}, config.keepAlive * 1000);
_qz.websocket.connection.interval = interval;
}
}
};
//called during websocket close during setup
_qz.websocket.connection.onclose = function() {
// Safari compatibility fix to raise error event
if (_qz.websocket.connection && typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) {
_qz.websocket.connection.onerror();
}
};
//called for errors during setup (such as invalid ports), reject connect promise only if all ports have been tried
_qz.websocket.connection.onerror = function(evt) {
_qz.log.trace(evt);
_qz.websocket.connection = null;
deeper();
};
} else {
reject(new Error("Unable to create a websocket connection"));
}
},
/** Finish setting calls on successful connection, sets web socket calls that won't settle the promise. */
openConnection: function(openPromise) {
_qz.websocket.connection.established = true;
//called when an open connection is closed
_qz.websocket.connection.onclose = function(evt) {
_qz.log.trace(evt);
_qz.websocket.connection = null;
_qz.websocket.callClose(evt);
_qz.log.info("Closed connection with QZ Tray");
for(var uid in _qz.websocket.pendingCalls) {
if (_qz.websocket.pendingCalls.hasOwnProperty(uid)) {
_qz.websocket.pendingCalls[uid].reject(new Error("Connection closed before response received"));
}
}
//if this is set, then an explicit close call was made
if (this.promise != undefined) {
this.promise.resolve();
}
};
//called for any errors with an open connection
_qz.websocket.connection.onerror = function(evt) {
_qz.websocket.callError(evt);
};
//send JSON objects to qz
_qz.websocket.connection.sendData = function(obj) {
_qz.log.trace("Preparing object for websocket", obj);
if (obj.timestamp == undefined) {
obj.timestamp = Date.now();
if (typeof obj.timestamp !== 'number') {
obj.timestamp = new Date().getTime();
}
}
if (obj.promise != undefined) {
obj.uid = _qz.websocket.setup.newUID();
_qz.websocket.pendingCalls[obj.uid] = obj.promise;
}
// track requesting monitor
obj.position = {
x: typeof screen !== 'undefined' ? ((screen.availWidth || screen.width) / 2) + (screen.left || screen.availLeft || 0) : 0,
y: typeof screen !== 'undefined' ? ((screen.availHeight || screen.height) / 2) + (screen.top || screen.availTop || 0) : 0
};
try {
if (obj.call != undefined && obj.signature == undefined && _qz.security.needsSigned(obj.call)) {
var signObj = {
call: obj.call,
params: obj.params,
timestamp: obj.timestamp
};
//make a hashing promise if not already one
var hashing = _qz.tools.hash(_qz.tools.stringify(signObj));
if (!hashing.then) {
hashing = _qz.tools.promise(function(resolve) {
resolve(hashing);
});
}
hashing.then(function(hashed) {
return _qz.security.callSign(hashed);
}).then(function(signature) {
_qz.log.trace("Signature for call", signature);
obj.signature = signature || "";
obj.signAlgorithm = _qz.security.signAlgorithm;
_qz.signContent = undefined;
_qz.websocket.connection.send(_qz.tools.stringify(obj));
}).catch(function(err) {
_qz.log.error("Signing failed", err);
if (obj.promise != undefined) {
obj.promise.reject(new Error("Failed to sign request"));
delete _qz.websocket.pendingCalls[obj.uid];
}
});
} else {
_qz.log.trace("Signature for call", obj.signature);
//called for pre-signed content and (unsigned) setup calls
_qz.websocket.connection.send(_qz.tools.stringify(obj));
}
}
catch(err) {
_qz.log.error(err);
if (obj.promise != undefined) {
obj.promise.reject(err);
delete _qz.websocket.pendingCalls[obj.uid];
}
}
};
//receive message from qz
_qz.websocket.connection.onmessage = function(evt) {
var returned = JSON.parse(evt.data);
if (returned.uid == null) {
if (returned.type == null) {
//incorrect response format, likely connected to incompatible qz version
_qz.websocket.connection.close(4003, "Connected to incompatible QZ Tray version");
} else {
//streams (callbacks only, no promises)
switch(returned.type) {
case _qz.streams.serial:
if (!returned.event) {
returned.event = JSON.stringify({ portName: returned.key, output: returned.data });
}
_qz.serial.callSerial(JSON.parse(returned.event));
break;
case _qz.streams.socket:
_qz.socket.callSocket(JSON.parse(returned.event));
break;
case _qz.streams.usb:
if (!returned.event) {
returned.event = JSON.stringify({ vendorId: returned.key[0], productId: returned.key[1], output: returned.data });
}
_qz.usb.callUsb(JSON.parse(returned.event));
break;
case _qz.streams.hid:
_qz.hid.callHid(JSON.parse(returned.event));
break;
case _qz.streams.printer:
_qz.printers.callPrinter(JSON.parse(returned.event));
break;
case _qz.streams.file:
_qz.file.callFile(JSON.parse(returned.event));
break;
default:
_qz.log.allay("Cannot determine stream type for callback", returned);
break;
}
}
return;
}
_qz.log.trace("Received response from websocket", returned);
var promise = _qz.websocket.pendingCalls[returned.uid];
if (promise == undefined) {
_qz.log.allay('No promise found for returned response');
} else {
if (returned.error != undefined) {
promise.reject(new Error(returned.error));
} else {
promise.resolve(returned.result);
}
}
delete _qz.websocket.pendingCalls[returned.uid];
};
//send up the certificate before making any calls
//also gives the user a chance to deny the connection
function sendCert(cert) {
if (cert === undefined) { cert = null; }
//websocket setup, query what version is connected
qz.api.getVersion().then(function(version) {
_qz.websocket.connection.version = version;
_qz.websocket.connection.semver = version.toLowerCase().replace(/-rc\./g, "-rc").split(/[\\+\\.-]/g);
for(var i = 0; i < _qz.websocket.connection.semver.length; i++) {
try {
if (i == 3 && _qz.websocket.connection.semver[i].toLowerCase().indexOf("rc") == 0) {
// Handle "rc1" pre-release by negating build info
_qz.websocket.connection.semver[i] = -(_qz.websocket.connection.semver[i].replace(/\D/g, ""));
continue;
}
_qz.websocket.connection.semver[i] = parseInt(_qz.websocket.connection.semver[i]);
}
catch(ignore) {}
if (_qz.websocket.connection.semver.length < 4) {
_qz.websocket.connection.semver[3] = 0;
}
}
//algorithm can be declared before a connection, check for incompatibilities now that we have one
_qz.compatible.algorithm(true);
}).then(function() {
_qz.websocket.connection.sendData({ certificate: cert, promise: openPromise });
});
}
_qz.security.callCert().then(sendCert).catch(function(error) {
_qz.log.warn("Failed to get certificate:", error);
if (_qz.security.rejectOnCertFailure) {
openPromise.reject(error);
} else {
sendCert(null);
}
});
},
/** Generate unique ID used to map a response to a call. */
newUID: function() {
var len = 6;
return (new Array(len + 1).join("0") + (Math.random() * Math.pow(36, len) << 0).toString(36)).slice(-len)
}
},
dataPromise: function(callName, params, signature, signingTimestamp) {
return _qz.tools.promise(function(resolve, reject) {
var msg = {
call: callName,
promise: { resolve: resolve, reject: reject },
params: params,
signature: signature,
timestamp: signingTimestamp
};
_qz.websocket.connection.sendData(msg);
});
},
/** Library of promises awaiting a response, uid -> promise */
pendingCalls: {},
/** List of functions to call on error from the websocket. */
errorCallbacks: [],
/** Calls all functions registered to listen for errors. */
callError: function(evt) {
if (Array.isArray(_qz.websocket.errorCallbacks)) {
for(var i = 0; i < _qz.websocket.errorCallbacks.length; i++) {
_qz.websocket.errorCallbacks[i](evt);
}
} else {
_qz.websocket.errorCallbacks(evt);
}
},
/** List of function to call on closing from the websocket. */
closedCallbacks: [],
/** Calls all functions registered to listen for closing. */
callClose: function(evt) {
if (Array.isArray(_qz.websocket.closedCallbacks)) {
for(var i = 0; i < _qz.websocket.closedCallbacks.length; i++) {
_qz.websocket.closedCallbacks[i](evt);
}
} else {
_qz.websocket.closedCallbacks(evt);
}
}
},
printing: {
/** Default options used for new printer configs. Can be overridden using {@link qz.configs.setDefaults}. */
defaultConfig: {
//value purposes are explained in the qz.configs.setDefaults docs
bounds: null,
colorType: 'color',
copies: 1,
density: 0,
duplex: false,
fallbackDensity: null,
interpolation: 'bicubic',
jobName: null,
legacy: false,
margins: 0,
orientation: null,
paperThickness: null,
printerTray: null,
rasterize: false,
rotation: 0,
scaleContent: true,
size: null,
units: 'in',
forceRaw: false,
encoding: null,
spool: null
}
},
serial: {
/** List of functions called when receiving data from serial connection. */
serialCallbacks: [],
/** Calls all functions registered to listen for serial events. */
callSerial: function(streamEvent) {
if (Array.isArray(_qz.serial.serialCallbacks)) {
for(var i = 0; i < _qz.serial.serialCallbacks.length; i++) {
_qz.serial.serialCallbacks[i](streamEvent);
}
} else {
_qz.serial.serialCallbacks(streamEvent);
}
}
},
socket: {
/** List of functions called when receiving data from network socket connection. */
socketCallbacks: [],
/** Calls all functions registered to listen for network socket events. */
callSocket: function(socketEvent) {
if (Array.isArray(_qz.socket.socketCallbacks)) {
for(var i = 0; i < _qz.socket.socketCallbacks.length; i++) {
_qz.socket.socketCallbacks[i](socketEvent);
}
} else {
_qz.socket.socketCallbacks(socketEvent);
}
}
},
usb: {
/** List of functions called when receiving data from usb connection. */
usbCallbacks: [],
/** Calls all functions registered to listen for usb events. */
callUsb: function(streamEvent) {
if (Array.isArray(_qz.usb.usbCallbacks)) {
for(var i = 0; i < _qz.usb.usbCallbacks.length; i++) {
_qz.usb.usbCallbacks[i](streamEvent);
}
} else {
_qz.usb.usbCallbacks(streamEvent);
}
}
},
hid: {
/** List of functions called when receiving data from hid connection. */
hidCallbacks: [],
/** Calls all functions registered to listen for hid events. */
callHid: function(streamEvent) {
if (Array.isArray(_qz.hid.hidCallbacks)) {
for(var i = 0; i < _qz.hid.hidCallbacks.length; i++) {
_qz.hid.hidCallbacks[i](streamEvent);
}
} else {
_qz.hid.hidCallbacks(streamEvent);
}
}
},
printers: {
/** List of functions called when receiving data from printer connection. */
printerCallbacks: [],
/** Calls all functions registered to listen for printer events. */
callPrinter: function(streamEvent) {
if (Array.isArray(_qz.printers.printerCallbacks)) {
for(var i = 0; i < _qz.printers.printerCallbacks.length; i++) {
_qz.printers.printerCallbacks[i](streamEvent);
}
} else {
_qz.printers.printerCallbacks(streamEvent);
}
}
},
file: {
/** List of functions called when receiving info regarding file changes. */
fileCallbacks: [],
/** Calls all functions registered to listen for file events. */
callFile: function(streamEvent) {
if (Array.isArray(_qz.file.fileCallbacks)) {
for(var i = 0; i < _qz.file.fileCallbacks.length; i++) {
_qz.file.fileCallbacks[i](streamEvent);
}
} else {
_qz.file.fileCallbacks(streamEvent);
}
}
},
security: {
/** Function used to resolve promise when acquiring site's public certificate. */
certHandler: function(resolve, reject) { reject(); },
/** Called to create new promise (using {@link _qz.security.certHandler}) for certificate retrieval. */
callCert: function() {
if (typeof _qz.security.certHandler.then === 'function') {
//already a promise
return _qz.security.certHandler;
} else if (_qz.security.certHandler.constructor.name === "AsyncFunction") {
//already callable as a promise
return _qz.security.certHandler();
} else {
//turn into a promise
return _qz.tools.promise(_qz.security.certHandler);
}
},
/** Function used to create promise resolver when requiring signed calls. */
signatureFactory: function() { return function(resolve) { resolve(); } },
/** Called to create new promise (using {@link _qz.security.signatureFactory}) for signed calls. */
callSign: function(toSign) {
if (_qz.security.signatureFactory.constructor.name === "AsyncFunction") {
//use directly
return _qz.security.signatureFactory(toSign);
} else {
//use in a promise
return _qz.tools.promise(_qz.security.signatureFactory(toSign));
}
},
/** Signing algorithm used on signatures */
signAlgorithm: "SHA1",
rejectOnCertFailure: false,
needsSigned: function(callName) {
const undialoged = [
"printers.getStatus",
"printers.stopListening",
"usb.isClaimed",
"usb.closeStream",
"usb.releaseDevice",
"hid.stopListening",
"hid.isClaimed",
"hid.closeStream",
"hid.releaseDevice",
"file.stopListening",
"getVersion"
];
return callName != null && undialoged.indexOf(callName) === -1;
}
},
tools: {
/** Create a new promise */
promise: function(resolver) {
//prefer global object for historical purposes
if (typeof RSVP !== 'undefined') {
return new RSVP.Promise(resolver);
} else if (typeof Promise !== 'undefined') {
return new Promise(resolver);
} else {
_qz.log.error("Promise/A+ support is required. See qz.api.setPromiseType(...)");
}
},
/** Stub for rejecting with an Error from withing a Promise */
reject: function(error) {
return _qz.tools.promise(function(resolve, reject) {
reject(error);
});
},
stringify: function(object) {
//old versions of prototype affect stringify
var pjson = Array.prototype.toJSON;
delete Array.prototype.toJSON;
function skipKeys(key, value) {
if (key === "promise") {
return undefined;
}
return value;
}
var result = JSON.stringify(object, skipKeys);
if (pjson) {
Array.prototype.toJSON = pjson;
}
return result;
},
hash: function(data) {
//prefer global object for historical purposes
if (typeof Sha256 !== 'undefined') {
return Sha256.hash(data);
} else {
return _qz.SHA.hash(data);
}
},
ws: typeof WebSocket !== 'undefined' ? WebSocket : null,
absolute: function(loc) {
if (typeof window !== 'undefined' && typeof document.createElement === 'function') {
var a = document.createElement("a");
a.href = loc;
return a.href;
} else if (typeof exports === 'object') {
//node.js
require('path').resolve(loc);
}
return loc;
},
relative: function(data) {
for(var i = 0; i < data.length; i++) {
if (data[i].constructor === Object) {
var absolute = false;
if (data[i].data && data[i].data.search && data[i].data.search(/data:image\/\w+;base64,/) === 0) {
//upgrade from old base64 behavior
data[i].flavor = "base64";
data[i].data = data[i].data.replace(/^data:image\/\w+;base64,/, "");
} else if (data[i].flavor) {
//if flavor is known, we can directly check for absolute flavor types
if (["FILE", "XML"].indexOf(data[i].flavor.toUpperCase()) > -1) {
absolute = true;
}
} else if (data[i].format && ["HTML", "IMAGE", "PDF", "FILE", "XML"].indexOf(data[i].format.toUpperCase()) > -1) {
//if flavor is not known, all valid pixel formats default to file flavor
//previous v2.0 data also used format as what is now flavor, so we check for those values here too
absolute = true;
} else if (data[i].type && ((["PIXEL", "IMAGE", "PDF"].indexOf(data[i].type.toUpperCase()) > -1 && !data[i].format)
|| (["HTML", "PDF"].indexOf(data[i].type.toUpperCase()) > -1 && (!data[i].format || data[i].format.toUpperCase() === "FILE")))) {
//if all we know is pixel type, then it is image's file flavor
//previous v2.0 data also used type as what is now format, so we check for those value here too
absolute = true;
}
if (absolute) {
//change relative links to absolute
data[i].data = _qz.tools.absolute(data[i].data);
}
if (data[i].options && typeof data[i].options.overlay === 'string') {
data[i].options.overlay = _qz.tools.absolute(data[i].options.overlay);
}
}
}
},
/** Performs deep copy to target from remaining params */
extend: function(target) {
//special case when reassigning properties as objects in a deep copy
if (typeof target !== 'object') {
target = {};
}
for(var i = 1; i < arguments.length; i++) {
var source = arguments[i];
if (!source) { continue; }
for(var key in source) {
if (source.hasOwnProperty(key)) {
if (target === source[key]) { continue; }
if (source[key] && source[key].constructor && source[key].constructor === Object) {
var clone;
if (Array.isArray(source[key])) {
clone = target[key] || [];
} else {
clone = target[key] || {};
}
target[key] = _qz.tools.extend(clone, source[key]);
} else if (source[key] !== undefined) {
target[key] = source[key];
}
}
}
}
return target;
},
versionCompare: function(major, minor, patch, build) {
if (_qz.tools.assertActive()) {
var semver = _qz.websocket.connection.semver;
if (semver[0] != major) {
return semver[0] - major;
}
if (minor != undefined && semver[1] != minor) {
return semver[1] - minor;
}
if (patch != undefined && semver[2] != patch) {
return semver[2] - patch;
}
if (build != undefined && semver.length > 3 && semver[3] != build) {
return Number.isInteger(semver[3]) && Number.isInteger(build) ? semver[3] - build : semver[3].toString().localeCompare(build.toString());
}
return 0;
}
},
isVersion: function(major, minor, patch, build) {
return _qz.tools.versionCompare(major, minor, patch, build) == 0;
},
isActive: function() {
return !_qz.websocket.shutdown && _qz.websocket.connection != null
&& (_qz.websocket.connection.readyState === _qz.tools.ws.OPEN
|| _qz.websocket.connection.readyState === _qz.tools.ws.CONNECTING);
},
assertActive: function() {
if (_qz.tools.isActive()) {
return true;
}
// Promise won't reject on throw; yet better than 'undefined'
throw new Error("A connection to QZ has not been established yet");
},
uint8ArrayToHex: function(uint8) {
return Array.from(uint8)
.map(function(i) { return i.toString(16).padStart(2, '0'); })
.join('');
},
uint8ArrayToBase64: function(uint8) {
/**
* Adapted from Egor Nepomnyaschih's code under MIT Licence (C) 2020
* see https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
*/
var map = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
];
var result = '', i, l = uint8.length;
for (i = 2; i < l; i += 3) {
result += map[uint8[i - 2] >> 2];
result += map[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += map[((uint8[i - 1] & 0x0F) << 2) | (uint8[i] >> 6)];
result += map[uint8[i] & 0x3F];
}
if (i === l + 1) { // 1 octet yet to write
result += map[uint8[i - 2] >> 2];
result += map[(uint8[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) { // 2 octets yet to write
result += map[uint8[i - 2] >> 2];
result += map[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += map[(uint8[i - 1] & 0x0F) << 2];
result += "=";
}
return result;
},
},
compatible: {
/** Converts message format to a previous version's */
data: function(printData) {
// special handling for Uint8Array
for(var i = 0; i < printData.length; i++) {
if (printData[i].constructor === Object && printData[i].data instanceof Uint8Array) {
if (printData[i].flavor) {
var flavor = printData[i].flavor.toString().toUpperCase();
switch(flavor) {
case 'BASE64':
printData[i].data = _qz.tools.uint8ArrayToBase64(printData[i].data);
break;
case 'HEX':
printData[i].data = _qz.tools.uint8ArrayToHex(printData[i].data);
break;
default:
throw new Error("Uint8Array conversion to '" + flavor + "' is not supported.");
}
}
}
}
if(_qz.tools.versionCompare(2, 2, 4) < 0) {
for(var i = 0; i < printData.length; i++) {
if (printData[i].constructor === Object) {
// dotDensity: "double-legacy|single-legacy" since 2.2.4. Fallback to "double|single"
if (printData[i].options && typeof printData[i].options.dotDensity === 'string') {
printData[i].options.dotDensity = printData[i].options.dotDensity.toLowerCase().replace("-legacy", "");
}
}
}
}
if (_qz.tools.isVersion(2, 0)) {
/*
2.0.x conversion
-----
type=pixel -> use format as 2.0 type (unless 'command' format, which forces 2.0 'raw' type)
type=raw -> 2.0 type has to be 'raw'
if format is 'image' -> force 2.0 'image' format, ignore everything else (unsupported in 2.0)
flavor translates straight to 2.0 format (unless forced to 'raw'/'image')
*/
_qz.log.trace("Converting print data to v2.0 for " + _qz.websocket.connection.version);
for(var i = 0; i < printData.length; i++) {
if (printData[i].constructor === Object) {
if (printData[i].type && printData[i].type.toUpperCase() === "RAW" && printData[i].format && printData[i].format.toUpperCase() === "IMAGE") {
if (printData[i].flavor && printData[i].flavor.toUpperCase() === "BASE64") {
//special case for raw base64 images
printData[i].data = "data:image/compat;base64," + printData[i].data;
}
printData[i].flavor = "IMAGE"; //forces 'image' format when shifting for conversion
}
if ((printData[i].type && printData[i].type.toUpperCase() === "RAW") || (printData[i].format && printData[i].format.toUpperCase() === "COMMAND")) {
printData[i].format = "RAW"; //forces 'raw' type when shifting for conversion
}
printData[i].type = printData[i].format;
printData[i].format = printData[i].flavor;
delete printData[i].flavor;
}
}
}
},
/* Converts config defaults to match previous version */
config: function(config, dirty) {
if (_qz.tools.isVersion(2, 0)) {
if (!dirty.rasterize) {
config.rasterize = true;
}
}
if(_qz.tools.versionCompare(2, 2) < 0) {
if(config.forceRaw !== 'undefined') {
config.altPrinting = config.forceRaw;
delete config.forceRaw;
}
}
if(_qz.tools.versionCompare(2, 1, 2, 11) < 0) {
if(config.spool) {
if(config.spool.size) {
config.perSpool = config.spool.size;
delete config.spool.size;
}
if(config.spool.end) {
config.endOfDoc = config.spool.end;
delete config.spool.end;
}
delete config.spool;
}
}
return config;
},
/** Compat wrapper with previous version **/
networking: function(hostname, port, signature, signingTimestamp, mappingCallback) {
// Use 2.0
if (_qz.tools.isVersion(2, 0)) {
return _qz.tools.promise(function(resolve, reject) {
_qz.websocket.dataPromise('websocket.getNetworkInfo', {
hostname: hostname,
port: port
}, signature, signingTimestamp).then(function(data) {
if (typeof mappingCallback !== 'undefined') {
resolve(mappingCallback(data));
} else {
resolve(data);
}
}, reject);
});
}
// Wrap 2.1
return _qz.tools.promise(function(resolve, reject) {
_qz.websocket.dataPromise('networking.device', {
hostname: hostname,
port: port
}, signature, signingTimestamp).then(function(data) {
resolve({ ipAddress: data.ip, macAddress: data.mac });
}, reject);
});
},
/** Check if QZ version supports chosen algorithm */
algorithm: function(quiet) {
//if not connected yet we will assume compatibility exists for the time being
//check semver to guard race condition for pending connections
if (_qz.tools.isActive() && _qz.websocket.connection.semver) {
if (_qz.tools.isVersion(2, 0)) {
if (!quiet) {
_qz.log.warn("Connected to an older version of QZ, alternate signature algorithms are not supported");
}
return false;
}
}
return true;
}
},
/**
* Adapted from Chris Veness's code under MIT Licence (C) 2002
* see http://www.movable-type.co.uk/scripts/sha256.html
*/
SHA: {
//@formatter:off - keep this block compact
hash: function(msg) {
// add trailing '1' bit (+ 0's padding) to string [§5.1.1]
msg = _qz.SHA._utf8Encode(msg) + String.fromCharCode(0x80);
// constants [§4.2.2]
var K = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
];