servez 1.12.0 → 1.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,8 @@ Using `npx` you can run servez without installing it first:
44
44
 
45
45
  * `--dirs` Show folder listings (defaults to `true`, `--no-dirs` to disable)
46
46
 
47
+ * `--qr` Show a QR code for the root url of the server. This might help for using servez with a phone.
48
+
47
49
  * `--cors` Include CORS headers (defaults to `true`, `--no-cors` to disable)
48
50
 
49
51
  * `--local` make serve only accessible from this machine. The default
package/bin/servez CHANGED
@@ -19,6 +19,44 @@ const log = {
19
19
  },
20
20
  };
21
21
 
22
+ function genQRCode(s) {
23
+ const blockChars = [
24
+ ' ', // 0
25
+ '▘', // 1
26
+ '▝', // 2
27
+ '▀', // 3
28
+ '▖', // 4
29
+ '▌', // 5
30
+ '▞', // 6
31
+ '▛', // 7
32
+ '▗', // 8
33
+ '▚', // 9
34
+ '▐', // 10
35
+ '▜', // 11
36
+ '▄', // 12
37
+ '▙', // 13
38
+ '▟', // 14
39
+ '█', // 15
40
+ ];
41
+
42
+ const qr = QrCode.encodeText(s, Ecc.MEDIUM);
43
+ const size = ((qr.size + 1) / 2 | 0) * 2 + 2;
44
+
45
+ const lines = [];
46
+ for (let y = -2; y < size; y += 2) {
47
+ const line = [];
48
+ for (let x = -2; x < size; x += 1) {
49
+ const code = (qr.getModule(x + 0, y + 0) ? 1 : 0) |
50
+ (qr.getModule(x + 0, y + 0) ? 2 : 0) |
51
+ (qr.getModule(x + 0, y + 1) ? 4 : 0) |
52
+ (qr.getModule(x + 0, y + 1) ? 8 : 0) ;
53
+ line.push(blockChars[code]);
54
+ }
55
+ lines.push(line.join(''));
56
+ }
57
+ return c.bgWhite(c.black(lines.join('\n')));
58
+ }
59
+
22
60
  const optionSpec = {
23
61
  options: [
24
62
  { option: 'help', alias: 'h', type: 'Boolean', description: 'displays help' },
@@ -26,6 +64,7 @@ const optionSpec = {
26
64
  { option: 'version', type: 'Boolean', description: 'print version' },
27
65
  { option: 'scan', type: 'Boolean', description: 'scan for open port', default: 'true', },
28
66
  { option: 'dirs', type: 'Boolean', description: 'show directory listing', default: 'true', },
67
+ { option: 'qr', type: 'Boolean', description: 'print QR Code for root url' },
29
68
  { option: 'cors', type: 'Boolean', description: 'send CORS headers', default: 'true', },
30
69
  { option: 'local', type: 'Boolean', description: 'local machine only', default: 'false', },
31
70
  { option: 'index', type: 'Boolean', description: 'serve index.html for directories', default: 'true', },
@@ -76,6 +115,8 @@ if (args.version) {
76
115
 
77
116
  const fs = require('fs');
78
117
  const path = require('path');
118
+ const {QrCode, Ecc} = require('../lib/qrcodegen');
119
+ const hosts = [];
79
120
 
80
121
  const root = path.resolve(args._[0] || process.cwd());
81
122
  try {
@@ -97,11 +138,29 @@ process.stdin.destroy(); // this allows control-c to not print "Terminate Batch?
97
138
  process.title = `servez ${root.split(/\\|\//g).slice(-3).join(path.sep)}`;
98
139
 
99
140
  const commands = {
100
- log(data) {
101
- console.log(...data);
141
+ log(args) {
142
+ console.log(...args);
143
+ },
144
+ error(args) {
145
+ console.error(c.red(args.join(' ')));
146
+ },
147
+ host(args) {
148
+ const localRE = /\D0\.0\.0\.0.\D|\D127\.0\.0\.|\Wlocalhost\W/
149
+ const [data] = args;
150
+ const {root} = data;
151
+ if (!localRE.test(root)) {
152
+ hosts.push(root);
153
+ }
102
154
  },
103
- error(data) {
104
- console.error(c.red([...data].join(' ')));
155
+ start(data) {
156
+ if (args.qr) {
157
+ for (const host of hosts) {
158
+ log.info(`--------------\nQR code for: ${host}`);
159
+ log.info(genQRCode(host));
160
+ log.info('');
161
+ }
162
+ }
163
+ log.info('press CTRL-C to stop the server.');
105
164
  },
106
165
  };
107
166
 
@@ -0,0 +1,825 @@
1
+ /*
2
+ * QR Code generator library (TypeScript)
3
+ *
4
+ * Copyright (c) Project Nayuki. (MIT License)
5
+ * https://www.nayuki.io/page/qr-code-generator-library
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+ * this software and associated documentation files (the "Software"), to deal in
9
+ * the Software without restriction, including without limitation the rights to
10
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
+ * the Software, and to permit persons to whom the Software is furnished to do so,
12
+ * subject to the following conditions:
13
+ * - The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ * - The Software is provided "as is", without warranty of any kind, express or
16
+ * implied, including but not limited to the warranties of merchantability,
17
+ * fitness for a particular purpose and noninfringement. In no event shall the
18
+ * authors or copyright holders be liable for any claim, damages or other
19
+ * liability, whether in an action of contract, tort or otherwise, arising from,
20
+ * out of or in connection with the Software or the use or other dealings in the
21
+ * Software.
22
+ */
23
+ "use strict";
24
+ /*---- QR Code symbol class ----*/
25
+ /*
26
+ * A QR Code symbol, which is a type of two-dimension barcode.
27
+ * Invented by Denso Wave and described in the ISO/IEC 18004 standard.
28
+ * Instances of this class represent an immutable square grid of dark and light cells.
29
+ * The class provides static factory functions to create a QR Code from text or binary data.
30
+ * The class covers the QR Code Model 2 specification, supporting all versions (sizes)
31
+ * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
32
+ *
33
+ * Ways to create a QR Code object:
34
+ * - High level: Take the payload data and call QrCode.encodeText() or QrCode.encodeBinary().
35
+ * - Mid level: Custom-make the list of segments and call QrCode.encodeSegments().
36
+ * - Low level: Custom-make the array of data codeword bytes (including
37
+ * segment headers and final padding, excluding error correction codewords),
38
+ * supply the appropriate version number, and call the QrCode() constructor.
39
+ * (Note that all ways require supplying the desired error correction level.)
40
+ */
41
+ class QrCode {
42
+ /*-- Constructor (low level) and fields --*/
43
+ // Creates a new QR Code with the given version number,
44
+ // error correction level, data codeword bytes, and mask number.
45
+ // This is a low-level API that most users should not use directly.
46
+ // A mid-level API is the encodeSegments() function.
47
+ constructor(
48
+ // The version number of this QR Code, which is between 1 and 40 (inclusive).
49
+ // This determines the size of this barcode.
50
+ version,
51
+ // The error correction level used in this QR Code.
52
+ errorCorrectionLevel, dataCodewords, msk) {
53
+ this.version = version;
54
+ this.errorCorrectionLevel = errorCorrectionLevel;
55
+ // The modules of this QR Code (false = light, true = dark).
56
+ // Immutable after constructor finishes. Accessed through getModule().
57
+ this.modules = [];
58
+ // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
59
+ this.isFunction = [];
60
+ // Check scalar arguments
61
+ if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)
62
+ throw "Version value out of range";
63
+ if (msk < -1 || msk > 7)
64
+ throw "Mask value out of range";
65
+ this.size = version * 4 + 17;
66
+ // Initialize both grids to be size*size arrays of Boolean false
67
+ let row = [];
68
+ for (let i = 0; i < this.size; i++)
69
+ row.push(false);
70
+ for (let i = 0; i < this.size; i++) {
71
+ this.modules.push(row.slice()); // Initially all light
72
+ this.isFunction.push(row.slice());
73
+ }
74
+ // Compute ECC, draw modules
75
+ this.drawFunctionPatterns();
76
+ const allCodewords = this.addEccAndInterleave(dataCodewords);
77
+ this.drawCodewords(allCodewords);
78
+ // Do masking
79
+ if (msk == -1) { // Automatically choose best mask
80
+ let minPenalty = 1000000000;
81
+ for (let i = 0; i < 8; i++) {
82
+ this.applyMask(i);
83
+ this.drawFormatBits(i);
84
+ const penalty = this.getPenaltyScore();
85
+ if (penalty < minPenalty) {
86
+ msk = i;
87
+ minPenalty = penalty;
88
+ }
89
+ this.applyMask(i); // Undoes the mask due to XOR
90
+ }
91
+ }
92
+ assert(0 <= msk && msk <= 7);
93
+ this.mask = msk;
94
+ this.applyMask(msk); // Apply the final choice of mask
95
+ this.drawFormatBits(msk); // Overwrite old format bits
96
+ this.isFunction = [];
97
+ }
98
+ /*-- Static factory functions (high level) --*/
99
+ // Returns a QR Code representing the given Unicode text string at the given error correction level.
100
+ // As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
101
+ // Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
102
+ // QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
103
+ // ecl argument if it can be done without increasing the version.
104
+ static encodeText(text, ecl) {
105
+ const segs = QrSegment.makeSegments(text);
106
+ return QrCode.encodeSegments(segs, ecl);
107
+ }
108
+ // Returns a QR Code representing the given binary data at the given error correction level.
109
+ // This function always encodes using the binary segment mode, not any text mode. The maximum number of
110
+ // bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
111
+ // The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
112
+ static encodeBinary(data, ecl) {
113
+ const seg = QrSegment.makeBytes(data);
114
+ return QrCode.encodeSegments([seg], ecl);
115
+ }
116
+ /*-- Static factory functions (mid level) --*/
117
+ // Returns a QR Code representing the given segments with the given encoding parameters.
118
+ // The smallest possible QR Code version within the given range is automatically
119
+ // chosen for the output. Iff boostEcl is true, then the ECC level of the result
120
+ // may be higher than the ecl argument if it can be done without increasing the
121
+ // version. The mask number is either between 0 to 7 (inclusive) to force that
122
+ // mask, or -1 to automatically choose an appropriate mask (which may be slow).
123
+ // This function allows the user to create a custom sequence of segments that switches
124
+ // between modes (such as alphanumeric and byte) to encode text in less space.
125
+ // This is a mid-level API; the high-level API is encodeText() and encodeBinary().
126
+ static encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
127
+ if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION)
128
+ || mask < -1 || mask > 7)
129
+ throw "Invalid value";
130
+ // Find the minimal version number to use
131
+ let version;
132
+ let dataUsedBits;
133
+ for (version = minVersion;; version++) {
134
+ const dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8; // Number of data bits available
135
+ const usedBits = QrSegment.getTotalBits(segs, version);
136
+ if (usedBits <= dataCapacityBits) {
137
+ dataUsedBits = usedBits;
138
+ break; // This version number is found to be suitable
139
+ }
140
+ if (version >= maxVersion) // All versions in the range could not fit the given data
141
+ throw "Data too long";
142
+ }
143
+ // Increase the error correction level while the data still fits in the current version number
144
+ for (const newEcl of [Ecc.MEDIUM, Ecc.QUARTILE, Ecc.HIGH]) { // From low to high
145
+ if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8)
146
+ ecl = newEcl;
147
+ }
148
+ // Concatenate all segments to create the data bit string
149
+ let bb = [];
150
+ for (const seg of segs) {
151
+ appendBits(seg.mode.modeBits, 4, bb);
152
+ appendBits(seg.numChars, seg.mode.numCharCountBits(version), bb);
153
+ for (const b of seg.getData())
154
+ bb.push(b);
155
+ }
156
+ assert(bb.length == dataUsedBits);
157
+ // Add terminator and pad up to a byte if applicable
158
+ const dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
159
+ assert(bb.length <= dataCapacityBits);
160
+ appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
161
+ appendBits(0, (8 - bb.length % 8) % 8, bb);
162
+ assert(bb.length % 8 == 0);
163
+ // Pad with alternating bytes until data capacity is reached
164
+ for (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
165
+ appendBits(padByte, 8, bb);
166
+ // Pack bits into bytes in big endian
167
+ let dataCodewords = [];
168
+ while (dataCodewords.length * 8 < bb.length)
169
+ dataCodewords.push(0);
170
+ bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << (7 - (i & 7)));
171
+ // Create the QR Code object
172
+ return new QrCode(version, ecl, dataCodewords, mask);
173
+ }
174
+ /*-- Accessor methods --*/
175
+ // Returns the color of the module (pixel) at the given coordinates, which is false
176
+ // for light or true for dark. The top left corner has the coordinates (x=0, y=0).
177
+ // If the given coordinates are out of bounds, then false (light) is returned.
178
+ getModule(x, y) {
179
+ return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];
180
+ }
181
+ /*-- Private helper methods for constructor: Drawing function modules --*/
182
+ // Reads this object's version field, and draws and marks all function modules.
183
+ drawFunctionPatterns() {
184
+ // Draw horizontal and vertical timing patterns
185
+ for (let i = 0; i < this.size; i++) {
186
+ this.setFunctionModule(6, i, i % 2 == 0);
187
+ this.setFunctionModule(i, 6, i % 2 == 0);
188
+ }
189
+ // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
190
+ this.drawFinderPattern(3, 3);
191
+ this.drawFinderPattern(this.size - 4, 3);
192
+ this.drawFinderPattern(3, this.size - 4);
193
+ // Draw numerous alignment patterns
194
+ const alignPatPos = this.getAlignmentPatternPositions();
195
+ const numAlign = alignPatPos.length;
196
+ for (let i = 0; i < numAlign; i++) {
197
+ for (let j = 0; j < numAlign; j++) {
198
+ // Don't draw on the three finder corners
199
+ if (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))
200
+ this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
201
+ }
202
+ }
203
+ // Draw configuration data
204
+ this.drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
205
+ this.drawVersion();
206
+ }
207
+ // Draws two copies of the format bits (with its own error correction code)
208
+ // based on the given mask and this object's error correction level field.
209
+ drawFormatBits(mask) {
210
+ // Calculate error correction code and pack bits
211
+ const data = this.errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
212
+ let rem = data;
213
+ for (let i = 0; i < 10; i++)
214
+ rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
215
+ const bits = (data << 10 | rem) ^ 0x5412; // uint15
216
+ assert(bits >>> 15 == 0);
217
+ // Draw first copy
218
+ for (let i = 0; i <= 5; i++)
219
+ this.setFunctionModule(8, i, getBit(bits, i));
220
+ this.setFunctionModule(8, 7, getBit(bits, 6));
221
+ this.setFunctionModule(8, 8, getBit(bits, 7));
222
+ this.setFunctionModule(7, 8, getBit(bits, 8));
223
+ for (let i = 9; i < 15; i++)
224
+ this.setFunctionModule(14 - i, 8, getBit(bits, i));
225
+ // Draw second copy
226
+ for (let i = 0; i < 8; i++)
227
+ this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
228
+ for (let i = 8; i < 15; i++)
229
+ this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
230
+ this.setFunctionModule(8, this.size - 8, true); // Always dark
231
+ }
232
+ // Draws two copies of the version bits (with its own error correction code),
233
+ // based on this object's version field, iff 7 <= version <= 40.
234
+ drawVersion() {
235
+ if (this.version < 7)
236
+ return;
237
+ // Calculate error correction code and pack bits
238
+ let rem = this.version; // version is uint6, in the range [7, 40]
239
+ for (let i = 0; i < 12; i++)
240
+ rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
241
+ const bits = this.version << 12 | rem; // uint18
242
+ assert(bits >>> 18 == 0);
243
+ // Draw two copies
244
+ for (let i = 0; i < 18; i++) {
245
+ const color = getBit(bits, i);
246
+ const a = this.size - 11 + i % 3;
247
+ const b = Math.floor(i / 3);
248
+ this.setFunctionModule(a, b, color);
249
+ this.setFunctionModule(b, a, color);
250
+ }
251
+ }
252
+ // Draws a 9*9 finder pattern including the border separator,
253
+ // with the center module at (x, y). Modules can be out of bounds.
254
+ drawFinderPattern(x, y) {
255
+ for (let dy = -4; dy <= 4; dy++) {
256
+ for (let dx = -4; dx <= 4; dx++) {
257
+ const dist = Math.max(Math.abs(dx), Math.abs(dy)); // Chebyshev/infinity norm
258
+ const xx = x + dx;
259
+ const yy = y + dy;
260
+ if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size)
261
+ this.setFunctionModule(xx, yy, dist != 2 && dist != 4);
262
+ }
263
+ }
264
+ }
265
+ // Draws a 5*5 alignment pattern, with the center module
266
+ // at (x, y). All modules must be in bounds.
267
+ drawAlignmentPattern(x, y) {
268
+ for (let dy = -2; dy <= 2; dy++) {
269
+ for (let dx = -2; dx <= 2; dx++)
270
+ this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
271
+ }
272
+ }
273
+ // Sets the color of a module and marks it as a function module.
274
+ // Only used by the constructor. Coordinates must be in bounds.
275
+ setFunctionModule(x, y, isDark) {
276
+ this.modules[y][x] = isDark;
277
+ this.isFunction[y][x] = true;
278
+ }
279
+ /*-- Private helper methods for constructor: Codewords and masking --*/
280
+ // Returns a new byte string representing the given data with the appropriate error correction
281
+ // codewords appended to it, based on this object's version and error correction level.
282
+ addEccAndInterleave(data) {
283
+ const ver = this.version;
284
+ const ecl = this.errorCorrectionLevel;
285
+ if (data.length != QrCode.getNumDataCodewords(ver, ecl))
286
+ throw "Invalid argument";
287
+ // Calculate parameter numbers
288
+ const numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
289
+ const blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver];
290
+ const rawCodewords = Math.floor(QrCode.getNumRawDataModules(ver) / 8);
291
+ const numShortBlocks = numBlocks - rawCodewords % numBlocks;
292
+ const shortBlockLen = Math.floor(rawCodewords / numBlocks);
293
+ // Split data into blocks and append ECC to each block
294
+ let blocks = [];
295
+ const rsDiv = QrCode.reedSolomonComputeDivisor(blockEccLen);
296
+ for (let i = 0, k = 0; i < numBlocks; i++) {
297
+ let dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
298
+ k += dat.length;
299
+ const ecc = QrCode.reedSolomonComputeRemainder(dat, rsDiv);
300
+ if (i < numShortBlocks)
301
+ dat.push(0);
302
+ blocks.push(dat.concat(ecc));
303
+ }
304
+ // Interleave (not concatenate) the bytes from every block into a single sequence
305
+ let result = [];
306
+ for (let i = 0; i < blocks[0].length; i++) {
307
+ blocks.forEach((block, j) => {
308
+ // Skip the padding byte in short blocks
309
+ if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
310
+ result.push(block[i]);
311
+ });
312
+ }
313
+ assert(result.length == rawCodewords);
314
+ return result;
315
+ }
316
+ // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
317
+ // data area of this QR Code. Function modules need to be marked off before this is called.
318
+ drawCodewords(data) {
319
+ if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8))
320
+ throw "Invalid argument";
321
+ let i = 0; // Bit index into the data
322
+ // Do the funny zigzag scan
323
+ for (let right = this.size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
324
+ if (right == 6)
325
+ right = 5;
326
+ for (let vert = 0; vert < this.size; vert++) { // Vertical counter
327
+ for (let j = 0; j < 2; j++) {
328
+ const x = right - j; // Actual x coordinate
329
+ const upward = ((right + 1) & 2) == 0;
330
+ const y = upward ? this.size - 1 - vert : vert; // Actual y coordinate
331
+ if (!this.isFunction[y][x] && i < data.length * 8) {
332
+ this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
333
+ i++;
334
+ }
335
+ // If this QR Code has any remainder bits (0 to 7), they were assigned as
336
+ // 0/false/light by the constructor and are left unchanged by this method
337
+ }
338
+ }
339
+ }
340
+ assert(i == data.length * 8);
341
+ }
342
+ // XORs the codeword modules in this QR Code with the given mask pattern.
343
+ // The function modules must be marked and the codeword bits must be drawn
344
+ // before masking. Due to the arithmetic of XOR, calling applyMask() with
345
+ // the same mask value a second time will undo the mask. A final well-formed
346
+ // QR Code needs exactly one (not zero, two, etc.) mask applied.
347
+ applyMask(mask) {
348
+ if (mask < 0 || mask > 7)
349
+ throw "Mask value out of range";
350
+ for (let y = 0; y < this.size; y++) {
351
+ for (let x = 0; x < this.size; x++) {
352
+ let invert;
353
+ switch (mask) {
354
+ case 0:
355
+ invert = (x + y) % 2 == 0;
356
+ break;
357
+ case 1:
358
+ invert = y % 2 == 0;
359
+ break;
360
+ case 2:
361
+ invert = x % 3 == 0;
362
+ break;
363
+ case 3:
364
+ invert = (x + y) % 3 == 0;
365
+ break;
366
+ case 4:
367
+ invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;
368
+ break;
369
+ case 5:
370
+ invert = x * y % 2 + x * y % 3 == 0;
371
+ break;
372
+ case 6:
373
+ invert = (x * y % 2 + x * y % 3) % 2 == 0;
374
+ break;
375
+ case 7:
376
+ invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
377
+ break;
378
+ default: throw "Unreachable";
379
+ }
380
+ if (!this.isFunction[y][x] && invert)
381
+ this.modules[y][x] = !this.modules[y][x];
382
+ }
383
+ }
384
+ }
385
+ // Calculates and returns the penalty score based on state of this QR Code's current modules.
386
+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
387
+ getPenaltyScore() {
388
+ let result = 0;
389
+ // Adjacent modules in row having same color, and finder-like patterns
390
+ for (let y = 0; y < this.size; y++) {
391
+ let runColor = false;
392
+ let runX = 0;
393
+ let runHistory = [0, 0, 0, 0, 0, 0, 0];
394
+ for (let x = 0; x < this.size; x++) {
395
+ if (this.modules[y][x] == runColor) {
396
+ runX++;
397
+ if (runX == 5)
398
+ result += QrCode.PENALTY_N1;
399
+ else if (runX > 5)
400
+ result++;
401
+ }
402
+ else {
403
+ this.finderPenaltyAddHistory(runX, runHistory);
404
+ if (!runColor)
405
+ result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;
406
+ runColor = this.modules[y][x];
407
+ runX = 1;
408
+ }
409
+ }
410
+ result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * QrCode.PENALTY_N3;
411
+ }
412
+ // Adjacent modules in column having same color, and finder-like patterns
413
+ for (let x = 0; x < this.size; x++) {
414
+ let runColor = false;
415
+ let runY = 0;
416
+ let runHistory = [0, 0, 0, 0, 0, 0, 0];
417
+ for (let y = 0; y < this.size; y++) {
418
+ if (this.modules[y][x] == runColor) {
419
+ runY++;
420
+ if (runY == 5)
421
+ result += QrCode.PENALTY_N1;
422
+ else if (runY > 5)
423
+ result++;
424
+ }
425
+ else {
426
+ this.finderPenaltyAddHistory(runY, runHistory);
427
+ if (!runColor)
428
+ result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;
429
+ runColor = this.modules[y][x];
430
+ runY = 1;
431
+ }
432
+ }
433
+ result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * QrCode.PENALTY_N3;
434
+ }
435
+ // 2*2 blocks of modules having same color
436
+ for (let y = 0; y < this.size - 1; y++) {
437
+ for (let x = 0; x < this.size - 1; x++) {
438
+ const color = this.modules[y][x];
439
+ if (color == this.modules[y][x + 1] &&
440
+ color == this.modules[y + 1][x] &&
441
+ color == this.modules[y + 1][x + 1])
442
+ result += QrCode.PENALTY_N2;
443
+ }
444
+ }
445
+ // Balance of dark and light modules
446
+ let dark = 0;
447
+ for (const row of this.modules)
448
+ dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
449
+ const total = this.size * this.size; // Note that size is odd, so dark/total != 1/2
450
+ // Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%
451
+ const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
452
+ assert(0 <= k && k <= 9);
453
+ result += k * QrCode.PENALTY_N4;
454
+ assert(0 <= result && result <= 2568888); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
455
+ return result;
456
+ }
457
+ /*-- Private helper functions --*/
458
+ // Returns an ascending list of positions of alignment patterns for this version number.
459
+ // Each position is in the range [0,177), and are used on both the x and y axes.
460
+ // This could be implemented as lookup table of 40 variable-length lists of integers.
461
+ getAlignmentPatternPositions() {
462
+ if (this.version == 1)
463
+ return [];
464
+ else {
465
+ const numAlign = Math.floor(this.version / 7) + 2;
466
+ const step = (this.version == 32) ? 26 :
467
+ Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
468
+ let result = [6];
469
+ for (let pos = this.size - 7; result.length < numAlign; pos -= step)
470
+ result.splice(1, 0, pos);
471
+ return result;
472
+ }
473
+ }
474
+ // Returns the number of data bits that can be stored in a QR Code of the given version number, after
475
+ // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
476
+ // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
477
+ static getNumRawDataModules(ver) {
478
+ if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)
479
+ throw "Version number out of range";
480
+ let result = (16 * ver + 128) * ver + 64;
481
+ if (ver >= 2) {
482
+ const numAlign = Math.floor(ver / 7) + 2;
483
+ result -= (25 * numAlign - 10) * numAlign - 55;
484
+ if (ver >= 7)
485
+ result -= 36;
486
+ }
487
+ assert(208 <= result && result <= 29648);
488
+ return result;
489
+ }
490
+ // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
491
+ // QR Code of the given version number and error correction level, with remainder bits discarded.
492
+ // This stateless pure function could be implemented as a (40*4)-cell lookup table.
493
+ static getNumDataCodewords(ver, ecl) {
494
+ return Math.floor(QrCode.getNumRawDataModules(ver) / 8) -
495
+ QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] *
496
+ QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
497
+ }
498
+ // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
499
+ // implemented as a lookup table over all possible parameter values, instead of as an algorithm.
500
+ static reedSolomonComputeDivisor(degree) {
501
+ if (degree < 1 || degree > 255)
502
+ throw "Degree out of range";
503
+ // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
504
+ // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].
505
+ let result = [];
506
+ for (let i = 0; i < degree - 1; i++)
507
+ result.push(0);
508
+ result.push(1); // Start off with the monomial x^0
509
+ // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
510
+ // and drop the highest monomial term which is always 1x^degree.
511
+ // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
512
+ let root = 1;
513
+ for (let i = 0; i < degree; i++) {
514
+ // Multiply the current product by (x - r^i)
515
+ for (let j = 0; j < result.length; j++) {
516
+ result[j] = QrCode.reedSolomonMultiply(result[j], root);
517
+ if (j + 1 < result.length)
518
+ result[j] ^= result[j + 1];
519
+ }
520
+ root = QrCode.reedSolomonMultiply(root, 0x02);
521
+ }
522
+ return result;
523
+ }
524
+ // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
525
+ static reedSolomonComputeRemainder(data, divisor) {
526
+ let result = divisor.map(_ => 0);
527
+ for (const b of data) { // Polynomial division
528
+ const factor = b ^ result.shift();
529
+ result.push(0);
530
+ divisor.forEach((coef, i) => result[i] ^= QrCode.reedSolomonMultiply(coef, factor));
531
+ }
532
+ return result;
533
+ }
534
+ // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
535
+ // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
536
+ static reedSolomonMultiply(x, y) {
537
+ if (x >>> 8 != 0 || y >>> 8 != 0)
538
+ throw "Byte out of range";
539
+ // Russian peasant multiplication
540
+ let z = 0;
541
+ for (let i = 7; i >= 0; i--) {
542
+ z = (z << 1) ^ ((z >>> 7) * 0x11D);
543
+ z ^= ((y >>> i) & 1) * x;
544
+ }
545
+ assert(z >>> 8 == 0);
546
+ return z;
547
+ }
548
+ // Can only be called immediately after a light run is added, and
549
+ // returns either 0, 1, or 2. A helper function for getPenaltyScore().
550
+ finderPenaltyCountPatterns(runHistory) {
551
+ const n = runHistory[1];
552
+ assert(n <= this.size * 3);
553
+ const core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
554
+ return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
555
+ + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
556
+ }
557
+ // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
558
+ finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
559
+ if (currentRunColor) { // Terminate dark run
560
+ this.finderPenaltyAddHistory(currentRunLength, runHistory);
561
+ currentRunLength = 0;
562
+ }
563
+ currentRunLength += this.size; // Add light border to final run
564
+ this.finderPenaltyAddHistory(currentRunLength, runHistory);
565
+ return this.finderPenaltyCountPatterns(runHistory);
566
+ }
567
+ // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
568
+ finderPenaltyAddHistory(currentRunLength, runHistory) {
569
+ if (runHistory[0] == 0)
570
+ currentRunLength += this.size; // Add light border to initial run
571
+ runHistory.pop();
572
+ runHistory.unshift(currentRunLength);
573
+ }
574
+ }
575
+ /*-- Constants and tables --*/
576
+ // The minimum version number supported in the QR Code Model 2 standard.
577
+ QrCode.MIN_VERSION = 1;
578
+ // The maximum version number supported in the QR Code Model 2 standard.
579
+ QrCode.MAX_VERSION = 40;
580
+ // For use in getPenaltyScore(), when evaluating which mask is best.
581
+ QrCode.PENALTY_N1 = 3;
582
+ QrCode.PENALTY_N2 = 3;
583
+ QrCode.PENALTY_N3 = 40;
584
+ QrCode.PENALTY_N4 = 10;
585
+ QrCode.ECC_CODEWORDS_PER_BLOCK = [
586
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
587
+ //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
588
+ [-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
589
+ [-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
590
+ [-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
591
+ [-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], // High
592
+ ];
593
+ QrCode.NUM_ERROR_CORRECTION_BLOCKS = [
594
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
595
+ //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
596
+ [-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
597
+ [-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
598
+ [-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
599
+ [-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81], // High
600
+ ];
601
+ // Appends the given number of low-order bits of the given value
602
+ // to the given buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
603
+ function appendBits(val, len, bb) {
604
+ if (len < 0 || len > 31 || val >>> len != 0)
605
+ throw "Value out of range";
606
+ for (let i = len - 1; i >= 0; i--) // Append bit by bit
607
+ bb.push((val >>> i) & 1);
608
+ }
609
+ // Returns true iff the i'th bit of x is set to 1.
610
+ function getBit(x, i) {
611
+ return ((x >>> i) & 1) != 0;
612
+ }
613
+ // Throws an exception if the given condition is false.
614
+ function assert(cond) {
615
+ if (!cond)
616
+ throw "Assertion error";
617
+ }
618
+ /*---- Data segment class ----*/
619
+ /*
620
+ * A segment of character/binary/control data in a QR Code symbol.
621
+ * Instances of this class are immutable.
622
+ * The mid-level way to create a segment is to take the payload data
623
+ * and call a static factory function such as QrSegment.makeNumeric().
624
+ * The low-level way to create a segment is to custom-make the bit buffer
625
+ * and call the QrSegment() constructor with appropriate values.
626
+ * This segment class imposes no length restrictions, but QR Codes have restrictions.
627
+ * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
628
+ * Any segment longer than this is meaningless for the purpose of generating QR Codes.
629
+ */
630
+ class QrSegment {
631
+ /*-- Constructor (low level) and fields --*/
632
+ // Creates a new QR Code segment with the given attributes and data.
633
+ // The character count (numChars) must agree with the mode and the bit buffer length,
634
+ // but the constraint isn't checked. The given bit buffer is cloned and stored.
635
+ constructor(
636
+ // The mode indicator of this segment.
637
+ mode,
638
+ // The length of this segment's unencoded data. Measured in characters for
639
+ // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
640
+ // Always zero or positive. Not the same as the data's bit length.
641
+ numChars,
642
+ // The data bits of this segment. Accessed through getData().
643
+ bitData) {
644
+ this.mode = mode;
645
+ this.numChars = numChars;
646
+ this.bitData = bitData;
647
+ if (numChars < 0)
648
+ throw "Invalid argument";
649
+ this.bitData = bitData.slice(); // Make defensive copy
650
+ }
651
+ /*-- Static factory functions (mid level) --*/
652
+ // Returns a segment representing the given binary data encoded in
653
+ // byte mode. All input byte arrays are acceptable. Any text string
654
+ // can be converted to UTF-8 bytes and encoded as a byte mode segment.
655
+ static makeBytes(data) {
656
+ let bb = [];
657
+ for (const b of data)
658
+ appendBits(b, 8, bb);
659
+ return new QrSegment(Mode.BYTE, data.length, bb);
660
+ }
661
+ // Returns a segment representing the given string of decimal digits encoded in numeric mode.
662
+ static makeNumeric(digits) {
663
+ if (!QrSegment.isNumeric(digits))
664
+ throw "String contains non-numeric characters";
665
+ let bb = [];
666
+ for (let i = 0; i < digits.length;) { // Consume up to 3 digits per iteration
667
+ const n = Math.min(digits.length - i, 3);
668
+ appendBits(parseInt(digits.substr(i, n), 10), n * 3 + 1, bb);
669
+ i += n;
670
+ }
671
+ return new QrSegment(Mode.NUMERIC, digits.length, bb);
672
+ }
673
+ // Returns a segment representing the given text string encoded in alphanumeric mode.
674
+ // The characters allowed are: 0 to 9, A to Z (uppercase only), space,
675
+ // dollar, percent, asterisk, plus, hyphen, period, slash, colon.
676
+ static makeAlphanumeric(text) {
677
+ if (!QrSegment.isAlphanumeric(text))
678
+ throw "String contains unencodable characters in alphanumeric mode";
679
+ let bb = [];
680
+ let i;
681
+ for (i = 0; i + 2 <= text.length; i += 2) { // Process groups of 2
682
+ let temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
683
+ temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
684
+ appendBits(temp, 11, bb);
685
+ }
686
+ if (i < text.length) // 1 character remaining
687
+ appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
688
+ return new QrSegment(Mode.ALPHANUMERIC, text.length, bb);
689
+ }
690
+ // Returns a new mutable list of zero or more segments to represent the given Unicode text string.
691
+ // The result may use various segment modes and switch modes to optimize the length of the bit stream.
692
+ static makeSegments(text) {
693
+ // Select the most efficient segment encoding automatically
694
+ if (text == "")
695
+ return [];
696
+ else if (QrSegment.isNumeric(text))
697
+ return [QrSegment.makeNumeric(text)];
698
+ else if (QrSegment.isAlphanumeric(text))
699
+ return [QrSegment.makeAlphanumeric(text)];
700
+ else
701
+ return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))];
702
+ }
703
+ // Returns a segment representing an Extended Channel Interpretation
704
+ // (ECI) designator with the given assignment value.
705
+ static makeEci(assignVal) {
706
+ let bb = [];
707
+ if (assignVal < 0)
708
+ throw "ECI assignment value out of range";
709
+ else if (assignVal < (1 << 7))
710
+ appendBits(assignVal, 8, bb);
711
+ else if (assignVal < (1 << 14)) {
712
+ appendBits(0b10, 2, bb);
713
+ appendBits(assignVal, 14, bb);
714
+ }
715
+ else if (assignVal < 1000000) {
716
+ appendBits(0b110, 3, bb);
717
+ appendBits(assignVal, 21, bb);
718
+ }
719
+ else
720
+ throw "ECI assignment value out of range";
721
+ return new QrSegment(Mode.ECI, 0, bb);
722
+ }
723
+ // Tests whether the given string can be encoded as a segment in numeric mode.
724
+ // A string is encodable iff each character is in the range 0 to 9.
725
+ static isNumeric(text) {
726
+ return QrSegment.NUMERIC_REGEX.test(text);
727
+ }
728
+ // Tests whether the given string can be encoded as a segment in alphanumeric mode.
729
+ // A string is encodable iff each character is in the following set: 0 to 9, A to Z
730
+ // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
731
+ static isAlphanumeric(text) {
732
+ return QrSegment.ALPHANUMERIC_REGEX.test(text);
733
+ }
734
+ /*-- Methods --*/
735
+ // Returns a new copy of the data bits of this segment.
736
+ getData() {
737
+ return this.bitData.slice(); // Make defensive copy
738
+ }
739
+ // (Package-private) Calculates and returns the number of bits needed to encode the given segments at
740
+ // the given version. The result is infinity if a segment has too many characters to fit its length field.
741
+ static getTotalBits(segs, version) {
742
+ let result = 0;
743
+ for (const seg of segs) {
744
+ const ccbits = seg.mode.numCharCountBits(version);
745
+ if (seg.numChars >= (1 << ccbits))
746
+ return Infinity; // The segment's length doesn't fit the field's bit width
747
+ result += 4 + ccbits + seg.bitData.length;
748
+ }
749
+ return result;
750
+ }
751
+ // Returns a new array of bytes representing the given string encoded in UTF-8.
752
+ static toUtf8ByteArray(str) {
753
+ str = encodeURI(str);
754
+ let result = [];
755
+ for (let i = 0; i < str.length; i++) {
756
+ if (str.charAt(i) != "%")
757
+ result.push(str.charCodeAt(i));
758
+ else {
759
+ result.push(parseInt(str.substr(i + 1, 2), 16));
760
+ i += 2;
761
+ }
762
+ }
763
+ return result;
764
+ }
765
+ }
766
+ /*-- Constants --*/
767
+ // Describes precisely all strings that are encodable in numeric mode.
768
+ QrSegment.NUMERIC_REGEX = /^[0-9]*$/;
769
+ // Describes precisely all strings that are encodable in alphanumeric mode.
770
+ QrSegment.ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/;
771
+ // The set of all legal characters in alphanumeric mode,
772
+ // where each character value maps to the index in the string.
773
+ QrSegment.ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
774
+ /*---- Public helper enumeration ----*/
775
+ /*
776
+ * The error correction level in a QR Code symbol. Immutable.
777
+ */
778
+ class Ecc {
779
+ /*-- Constructor and fields --*/
780
+ constructor(
781
+ // In the range 0 to 3 (unsigned 2-bit integer).
782
+ ordinal,
783
+ // (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
784
+ formatBits) {
785
+ this.ordinal = ordinal;
786
+ this.formatBits = formatBits;
787
+ }
788
+ }
789
+ /*-- Constants --*/
790
+ Ecc.LOW = new Ecc(0, 1); // The QR Code can tolerate about 7% erroneous codewords
791
+ Ecc.MEDIUM = new Ecc(1, 0); // The QR Code can tolerate about 15% erroneous codewords
792
+ Ecc.QUARTILE = new Ecc(2, 3); // The QR Code can tolerate about 25% erroneous codewords
793
+ Ecc.HIGH = new Ecc(3, 2); // The QR Code can tolerate about 30% erroneous codewords
794
+ /*---- Public helper enumeration ----*/
795
+ /*
796
+ * Describes how a segment's data bits are interpreted. Immutable.
797
+ */
798
+ class Mode {
799
+ /*-- Constructor and fields --*/
800
+ constructor(
801
+ // The mode indicator bits, which is a uint4 value (range 0 to 15).
802
+ modeBits,
803
+ // Number of character count bits for three different version ranges.
804
+ numBitsCharCount) {
805
+ this.modeBits = modeBits;
806
+ this.numBitsCharCount = numBitsCharCount;
807
+ }
808
+ /*-- Method --*/
809
+ // (Package-private) Returns the bit width of the character count field for a segment in
810
+ // this mode in a QR Code at the given version number. The result is in the range [0, 16].
811
+ numCharCountBits(ver) {
812
+ return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
813
+ }
814
+ }
815
+ /*-- Constants --*/
816
+ Mode.NUMERIC = new Mode(0x1, [10, 12, 14]);
817
+ Mode.ALPHANUMERIC = new Mode(0x2, [9, 11, 13]);
818
+ Mode.BYTE = new Mode(0x4, [8, 16, 16]);
819
+ Mode.KANJI = new Mode(0x8, [8, 10, 12]);
820
+ Mode.ECI = new Mode(0x7, [0, 0, 0]);
821
+
822
+ exports.QrCode = QrCode;
823
+ exports.Ecc = Ecc;
824
+ exports.QrSegment = QrSegment;
825
+ exports.Mode = Mode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "servez",
3
- "version": "1.12.0",
3
+ "version": "1.13.2",
4
4
  "description": "A simple command line server to replace http-server",
5
5
  "scripts": {
6
6
  "start": "node ./bin/servez"
@@ -26,7 +26,7 @@
26
26
  "ansi-colors": "^4.1.1",
27
27
  "color-support": "^1.1.3",
28
28
  "optionator": "^0.8.2",
29
- "servez-lib": "^2.4.0"
29
+ "servez-lib": "^2.5.0"
30
30
  },
31
31
  "bin": {
32
32
  "servez": "./bin/servez"
@@ -19,8 +19,8 @@ function sendCmd(cmd, data) {
19
19
 
20
20
  c.enabled = useColors;
21
21
  const logger = {
22
- log: (...args) => sendCmd('log', [...args]),
23
- error: (...args) => sendCmd('error', [...args]),
22
+ log: (...args) => sendCmd('log', args),
23
+ error: (...args) => sendCmd('error', args),
24
24
  c,
25
25
  };
26
26
 
@@ -29,8 +29,7 @@ const server = new Servez(Object.assign({
29
29
  dataDir,
30
30
  logger,
31
31
  }, args));
32
- server.on('start', () => {
33
- logger.log('press CTRL-C to stop the server.');
34
- });
32
+ server.on('host', (...args) => sendCmd('host', args));
33
+ server.on('start', (...args) => sendCmd('start', args));
35
34
 
36
35