'use strict'; const zlib = require('zlib'); /** * Build a posprint:// URL from a .pos document (object or JSON string). * * Encoding: UTF-8 JSON → raw deflate → base64url (no padding). * * @param {object|string} pos * @param {{ noconfirm?: boolean, origin?: string }} [options] * @returns {string} */ function posprintUrl(pos, options = {}) { const json = typeof pos === 'string' ? pos : JSON.stringify(pos); const compressed = zlib.deflateRawSync(Buffer.from(json, 'utf8'), { level: 9 }); const b64 = compressed.toString('base64url'); let url = `posprint://print?v=1&d=${b64}`; if (options.noconfirm) { url += '&noconfirm=1'; } if (options.origin) { const host = normalizeOrigin(options.origin); if (host) { url += `&origin=${encodeURIComponent(host)}`; } } return url; } function normalizeOrigin(raw) { const s = String(raw).trim(); if (!s) return null; try { if (/^https?:\/\//i.test(s)) { return new URL(s).hostname.toLowerCase(); } } catch (_) { /* fall through */ } return s.split('/')[0].split(':')[0].toLowerCase() || null; } module.exports = { posprintUrl };