BlueGit

timfa posprint

Files

posprint / lib / node / posprint_url.js

posprint_url.js

posprint_url.js 896 B Download Raw
1'use strict';
2
3const 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, referrer?: string }} [options]
12 * @returns {string}
13 */
14function 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.referrer) {
23 const ref = String(options.referrer).trim();
24 if (ref) {
25 url += `&referrer=${encodeURIComponent(ref)}`;
26 }
27 }
28 return url;
29}
30
31module.exports = { posprintUrl };