You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { app, BrowserWindow, ipcMain } from 'electron'
|
|
import net from 'node:net'
|
|
import path from 'node:path'
|
|
|
|
/**
|
|
* Phase-0 spike main process. Print transport is network ESC/POS (port 9100)
|
|
* — the zero-driver path every thermal printer brand supports. USB-shared
|
|
* printers come later via the raw-spool decision (see BUILDING.md).
|
|
*/
|
|
function createWindow(): void {
|
|
const win = new BrowserWindow({
|
|
width: 1366,
|
|
height: 820,
|
|
backgroundColor: '#0f1216',
|
|
autoHideMenuBar: true,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'preload.cjs'),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
},
|
|
})
|
|
void win.loadFile(path.join(__dirname, '../dist/index.html'))
|
|
}
|
|
|
|
ipcMain.handle('printers:list', async (event) => {
|
|
const printers = await event.sender.getPrintersAsync()
|
|
return printers.map((p) => ({ name: p.name, displayName: p.displayName }))
|
|
})
|
|
|
|
ipcMain.handle('print:raw9100', (_event, host: string, port: number, bytes: Uint8Array) => {
|
|
return new Promise<string>((resolve, reject) => {
|
|
const socket = net.createConnection({ host, port, timeout: 3000 })
|
|
socket.on('connect', () => {
|
|
socket.end(Buffer.from(bytes))
|
|
})
|
|
socket.on('close', () => resolve('sent'))
|
|
socket.on('timeout', () => {
|
|
socket.destroy()
|
|
reject(new Error(`Printer ${host}:${port} timed out`))
|
|
})
|
|
socket.on('error', (err) => reject(err))
|
|
})
|
|
})
|
|
|
|
app.whenReady().then(createWindow)
|
|
app.on('window-all-closed', () => app.quit())
|