UNPKG

qrfi

Version:

A CLI Wi-Fi QR Code Generator.

69 lines 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const qrcode = require("qrcode-terminal"); const qrImage = require("qr-image"); const replaceMap = { '\\': '\\\\', ',': '\\,', ':': '\\:', ';': '\\;' }; class Qrfi { constructor(wiFiOption) { this.toString = () => `WIFI:T:${this.authenticationType};S:${Qrfi.escape(this.networkSSID)};P:${Qrfi.escape(this.password)};${this.hidden ? 'true' : ''};`; this.toQR = (option) => { const { format } = Object.assign({}, Qrfi.defaultQROption, option); return new Promise((resolve, reject) => { const qrString = this.toString(); if (format === 'ascii') { qrcode.generate(qrString, resolve); } else if (format === 'svg' || format === 'png') { resolve(qrImage.imageSync(qrString, { type: format })); } else { reject(new Error(`Invalid format ${format} specified.`)); } }); }; const { authenticationType, networkSSID, password, hidden } = Object.assign({}, Qrfi.defaultWiFiOption, wiFiOption); if (!networkSSID) { throw new Error('no SSID specified.'); } if (networkSSID.length > 32) { throw new Error('Too long SSID.'); } if (authenticationType && (authenticationType !== 'WEP' && authenticationType !== 'WPA' && authenticationType !== 'nopass')) { throw new Error(`Invalid authentication type, ${authenticationType}. Authentication type should be one of WEP, WPA and nopass, or empty.`); } if (authenticationType && authenticationType !== 'nopass' && !password) { throw new Error(`no password specified against authentication type ${authenticationType}.`); } if (authenticationType === 'WEP' && password.length > 16) { throw new Error('Maximum length of password for WEP is 16.'); } if (authenticationType === 'WPA' && password.length > 63) { throw new Error('Maximum length of password for WPA-PSK/WPA2-PSK is 63.'); } this.authenticationType = authenticationType; this.networkSSID = networkSSID; this.password = password; this.hidden = hidden; } } Qrfi.defaultWiFiOption = { authenticationType: 'nopass', networkSSID: '', password: '', hidden: false }; Qrfi.defaultQROption = { format: 'ascii' }; Qrfi.escape = (str) => str .split('') .map((char) => replaceMap[char] || char) .join(''); exports.default = Qrfi; //# sourceMappingURL=index.js.map