Skip to content

Instantly share code, notes, and snippets.

@tresf
Created December 20, 2021 18:31
Show Gist options
  • Save tresf/b6ba48fdf0918863bc2f90b1784b55bc to your computer and use it in GitHub Desktop.
Save tresf/b6ba48fdf0918863bc2f90b1784b55bc to your computer and use it in GitHub Desktop.
import qz from 'qz-tray'
const jwa = require('jwa')
class QzPrint {
static config = null
// TODO: setupSig can safely be combined with setupCert if needed
static async setupSig () {
// TODO: move private key to flat file, read using AJAX/fetch/etc
// privateKey = fs.readFileSync(__dirname + '/private-key.pem');
var privateKey = '-----BEGIN PRIVATE KEY-----\n''
const ecdsa = jwa('RS512') // this needs to be imported
qz.security.setSignatureAlgorithm('SHA512') // Since 2.1
qz.security.setSignaturePromise(function (toSign) {
return function (resolve, reject) {
try {
const hex = ecdsa.sign(toSign, privateKey)
// Crude base64URL to base64 conversion ? :)
const hex2 = hex.replace(/_/g, '/').replace(/-/g, '+')
console.log(hex2)
resolve(hex2)
} catch (err) {
console.error(err)
reject(err)
}
}
})
}
static setupCert () {
qz.security.setCertificatePromise(function (resolve, reject) {
// TODO: Move certificate to dedicated file, import using AJAX/fetch/etc :)
resolve('-----BEGIN CERTIFICATE-----\n' + ...);
})
}
static connect () {
return qz.websocket.connect()
}
static disconnect () {
return qz.websocket.disconnect()
}
static async listPrinters () {
const list = await qz.printers.find()
if (Array.isArray(list)) {
return list
} else {
return [list]
}
}
static async selectPrinter (printer) {
QzPrint.config = await qz.configs.create(printer)
}
static print (label) {
return qz.print(QzPrint.config, [label])
}
}
export default QzPrint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment