Last active
March 26, 2022 20:48
-
-
Save daliborgogic/a47ed728ec3c1fc3dea3160a9479d25b to your computer and use it in GitHub Desktop.
GitHub Receipts | Credit @aschmelyun https://twitter.com/aschmelyun/status/1506960015063625733
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ node index.mjs | lp -d [PRINTER_MODEL]