| 1 | 'use strict';
|
| 2 |
|
| 3 | const zlib = require('zlib');
|
| 4 |
|
| 5 | /**
|
| 6 | * Build a posprint:// URL from a .pos document (object or JSON string).
|
| 7 | *
|
| 8 | * Encoding: UTF-8 JSON → raw deflate → base64url (no padding).
|
| 9 | *
|
| 10 | * @param {object|string} pos
|
| 11 | * @param {{ noconfirm?: boolean, origin?: string }} [options]
|
| 12 | * @returns {string}
|
| 13 | */
|
| 14 | function posprintUrl(pos, options = {}) {
|
| 15 | const json = typeof pos === 'string' ? pos : JSON.stringify(pos);
|
| 16 | const compressed = zlib.deflateRawSync(Buffer.from(json, 'utf8'), { level: 9 });
|
| 17 | const b64 = compressed.toString('base64url');
|
| 18 | let url = `posprint://print?v=1&d=${b64}`;
|
| 19 | if (options.noconfirm) {
|
| 20 | url += '&noconfirm=1';
|
| 21 | }
|
| 22 | if (options.origin) {
|
| 23 | const host = normalizeOrigin(options.origin);
|
| 24 | if (host) {
|
| 25 | url += `&origin=${encodeURIComponent(host)}`;
|
| 26 | }
|
| 27 | }
|
| 28 | return url;
|
| 29 | }
|
| 30 |
|
| 31 | function normalizeOrigin(raw) {
|
| 32 | const s = String(raw).trim();
|
| 33 | if (!s) return null;
|
| 34 | try {
|
| 35 | if (/^https?:\/\//i.test(s)) {
|
| 36 | return new URL(s).hostname.toLowerCase();
|
| 37 | }
|
| 38 | } catch (_) {
|
| 39 | /* fall through */
|
| 40 | }
|
| 41 | return s.split('/')[0].split(':')[0].toLowerCase() || null;
|
| 42 | }
|
| 43 |
|
| 44 | module.exports = { posprintUrl };
|