@posara/printer-agent
v0.5.0
Published
POSARA Horizon print agent — one command to print receipts, invoices and labels on this computer's printers
Downloads
1,931
Maintainers
Readme
@posara/printer-agent
Prints POSARA Horizon receipts, invoices and labels (ESC/POS, ZPL, TSPL, text) on the printers attached to this computer.
npx @posara/printer-agent --token <TOKEN> --url https://posara-horizon-t1.dcn01.ctdn.dev --installCreate the token in Printer Management → Agents (it is shown once, with this command pre-filled). Requires Node.js 20+. Windows, macOS and Linux.
--install registers a background service that starts automatically and restarts on failure. Without it the agent runs in the foreground.
| | Service | Starts |
|---|---|---|
| Windows | A real Windows service (PosaraPrinterAgent, run by WinSW) | At boot, no login needed |
| macOS | launchd agent | At login |
| Linux | systemd user service | At boot (needs loginctl enable-linger, done for you when permitted) |
Windows: installing needs administrator rights. Run the command from any terminal and approve the Windows prompt (or run it from an Administrator terminal). The agent's code goes to %ProgramData%\posara-printer-agent\service, which only administrators can change, because the service runs as SYSTEM; your config, queue and log stay in %LOCALAPPDATA%\posara-printer-agent. The service wrapper is downloaded once from its GitHub release and checked against a pinned hash (offline: download WinSW.NET461.exe and set POSARA_WINSW_PATH). An older scheduled-task install is replaced when you run install again. Printers installed only for your own Windows user are not visible to the service; install them for all users.
Commands
| | |
|---|---|
| run (default) | Run in the foreground |
| install / uninstall | Add or remove the background service |
| status | Service state and queue size |
| logs | Recent log lines |
| printers | Printers found on this computer |
| test-print --printer <name> | Print a test page |
| version / check-update | Show the installed version / check for a newer one |
| update | Install the latest version and restart the service |
| restart | Restart the background service |
update and restart need no administrator rights, even on Windows: when the service cannot be controlled directly they leave a request the running service acts on within seconds.
Updates
A service checks for a newer release when it starts and every hour, and installs it when its queue is empty (turn off with --no-auto-update). It downloads the package straight from the npm registry and checks its integrity hash (npm is only a fallback for networks that need its proxy settings), keeps the previous build, then exits so the service manager starts the new one: launchd and systemd restart on any exit, and the Windows service restarts on the agent's exit code 75. A new build that starts three times without staying healthy for 90 seconds is replaced by the previous one. Updating changes only the agent's code; reinstall (install) to change the service definition itself.
Options: --poll-only (HTTP polling instead of websocket), --insecure (allow http:// to a non-local server), or set POSARA_PRINTER_TOKEN / POSARA_URL. After the first run the settings are saved, so the service needs no flags.
Printer addresses
The printer name field takes more than an installed printer's name:
| Name | Goes to |
|---|---|
| EPSON_TM_T20 | the operating system's queue (CUPS raw / Windows spooler) |
| tcp://192.168.1.50 or tcp://192.168.1.50:9100 | a LAN or Wi-Fi printer on its raw port (9100 by default); also network:host:port and tcp://[ipv6]:port |
| usb://04b8:0e15 or usb://04b8:0e15/SERIAL | a USB printer straight through libusb, no OS queue needed |
printer-agent printers lists installed printers and the USB printers that are plugged in, with the address to use. Network sends retry twice and say why they failed (refused, unreachable, timed out).
USB uses the optional usb package (prebuilt libusb binaries; it installs with the agent). On Windows install the WinUSB driver for the printer with Zadig; on Linux add a udev rule for the printer (or run the agent as root); macOS needs nothing. The SERIAL part tells two printers of one model apart.
Text and encoding
Text jobs go out as UTF-8 by default. A job from the local endpoint can set encoding: "latin1" and codepage: n (sends ESC t n first) for printers on a single-byte code page. Raw ZPL/TSPL/ESC/POS bytes are never re-encoded: the browser sends them as base64, so UTF-8 labels and Latin-1 labels both arrive as written.
How it works
- Transport: websocket (
/printer), falling back to HTTP polling (/api/print-agent/claim) whenever the socket is down. Results are always confirmed over HTTP. - Queue: jobs are written to a local log before printing. If the printer is off or out of paper, the job is retried with exponential backoff (5 s → 5 min, 6 tries); later jobs for the same printer keep their order, other printers keep flowing. A crash or reboot resumes where it stopped.
- Exactly once: a finished job's result is held until POSARA confirms it, and the server ignores duplicates, so an offline server never causes a reprint.
- Printers: local queue names (CUPS on macOS/Linux, the Windows spooler), or
tcp://host:9100for a network printer. Leave the printer name empty for the system default.
Local endpoint (POS "Printer Agent" mode)
While running, the agent also listens on http://localhost:9191 (GET /api/health, GET /api/printers, POST /api/print) so the POS in a browser on the same computer can print directly: in POS → Print Settings choose Printer Agent and keep the URL http://localhost:9191. Change the port with --local-port, or turn it off with --no-local. Only the POSARA address you set up the agent with may call it from a browser; other websites are refused with a 403 that names the refused address. Pages served from this computer (http://localhost:3000, http://127.0.0.1:…) are always allowed. If you open POSARA from a different address (another domain), add it with --allow-origin https://that-address (repeatable; saved for next time). Chrome and Edge allow an https page to call localhost; Safari does not (use cloud printing there).
Security
- Outbound connections, plus the local endpoint above, which listens on 127.0.0.1 only, checks the Host header (DNS rebinding) and refuses browser requests from any origin other than your POSARA address.
https://is required (plainhttp://only for localhost, or with--insecure). Redirects are never followed with the token attached.- The token is stored in the per-user data folder with
0600permissions and is never logged. POSARA stores only its SHA-256 hash. - Tokens can be revoked (delete the agent in Printer Management) or given an expiry; a revoked token stops working immediately.
- Print data is sent to the printer as raw bytes; printer names are passed as arguments, never through a shell.
Data folder: %LOCALAPPDATA%\posara-printer-agent, ~/Library/Application Support/posara-printer-agent, or ~/.local/state/posara-printer-agent.
