Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active March 26, 2022 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daliborgogic/a47ed728ec3c1fc3dea3160a9479d25b to your computer and use it in GitHub Desktop.
Save daliborgogic/a47ed728ec3c1fc3dea3160a9479d25b to your computer and use it in GitHub Desktop.
import { createServer } from 'node:http'
import { createHmac } from 'node:crypto'
import { Printer, InMemory, Drawer, Style, Align, Model } from 'escpos-buffer'
const {
PORT = 3000,
HOST = 0,
GITHUB_WEBHOOK_SECRET,
PRINTER_MODEL = 'TM-T20',
PRINTER_COLUMNS = 56
} = process.env
createServer(async (req, res) => {
let bodyChunks = []
req.on('data', chunk => bodyChunks.push(chunk))
req.on('end', async () => {
const rawBody = Buffer.concat(bodyChunks).toString('utf8')
const jsonBody = JSON.parse(rawBody)
const signature = req.headers['x-hub-signature-256']
const computedSignature =
'sha256=' +
createHmac('sha256', GITHUB_WEBHOOK_SECRET).update(rawBody).digest('hex')
if (computedSignature === signature) {
const connection = new InMemory()
const model = new Model(PRINTER_MODEL)
const printer = await Printer.CONNECT(model, connection)
await printer.setColumns(PRINTER_COLUMNS)
await printer.write(jsonBody.action)
await printer.write(jsonBody.issue?.title)
await printer.write(jsonBody.issue?.number)
await printer.feed(6)
await printer.buzzer()
await printer.cutter()
await printer.drawer(Drawer.First)
process.stdout.write(connection.buffer())
}
})
}).listen(PORT, HOST)
@daliborgogic
Copy link
Author

daliborgogic commented Mar 26, 2022

$ node index.mjs | lp -d [PRINTER_MODEL]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment