@zmainer/dsh-wx-bridge 1.0.8
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/LICENSE +21 -0
- package/README.md +201 -0
- package/cordis.patch.yml +3 -0
- package/lib/client.js +440 -0
- package/lib/index.js +650 -0
- package/lib/kernel/acp-overlay-chat.yml +26 -0
- package/lib/kernel/acp-overlay.yml +22 -0
- package/lib/kernel/acp-preset-shim.mjs +162 -0
- package/lib/kernel/acp.mjs +232 -0
- package/lib/kernel/bridge.mjs +1341 -0
- package/lib/kernel/keeper.mjs +302 -0
- package/lib/kernel/second-brain-contract.txt +7 -0
- package/lib/pairing.js +225 -0
- package/lib/vendor/qr-svg.cjs +58 -0
- package/lib/vendor/qrcode-core/LICENSE +10 -0
- package/lib/vendor/qrcode-core/NOTICE.md +21 -0
- package/lib/vendor/qrcode-core/alignment-pattern.js +83 -0
- package/lib/vendor/qrcode-core/alphanumeric-data.js +59 -0
- package/lib/vendor/qrcode-core/bit-buffer.js +37 -0
- package/lib/vendor/qrcode-core/bit-matrix.js +65 -0
- package/lib/vendor/qrcode-core/byte-data.js +30 -0
- package/lib/vendor/qrcode-core/dijkstrajs.LICENSE +19 -0
- package/lib/vendor/qrcode-core/dijkstrajs.js +165 -0
- package/lib/vendor/qrcode-core/error-correction-code.js +135 -0
- package/lib/vendor/qrcode-core/error-correction-level.js +50 -0
- package/lib/vendor/qrcode-core/finder-pattern.js +22 -0
- package/lib/vendor/qrcode-core/format-info.js +29 -0
- package/lib/vendor/qrcode-core/galois-field.js +69 -0
- package/lib/vendor/qrcode-core/kanji-data.js +54 -0
- package/lib/vendor/qrcode-core/mask-pattern.js +234 -0
- package/lib/vendor/qrcode-core/mode.js +167 -0
- package/lib/vendor/qrcode-core/numeric-data.js +43 -0
- package/lib/vendor/qrcode-core/package.json +8 -0
- package/lib/vendor/qrcode-core/polynomial.js +62 -0
- package/lib/vendor/qrcode-core/qrcode.js +495 -0
- package/lib/vendor/qrcode-core/reed-solomon-encoder.js +56 -0
- package/lib/vendor/qrcode-core/regex.js +31 -0
- package/lib/vendor/qrcode-core/segments.js +330 -0
- package/lib/vendor/qrcode-core/utils.js +63 -0
- package/lib/vendor/qrcode-core/version-check.js +9 -0
- package/lib/vendor/qrcode-core/version.js +163 -0
- package/package.json +40 -0
- package/scripts/build-client.mjs +35 -0
- package/src/client.js +427 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alignment pattern are fixed reference pattern in defined positions
|
|
3
|
+
* in a matrix symbology, which enables the decode software to re-synchronise
|
|
4
|
+
* the coordinate mapping of the image modules in the event of moderate amounts
|
|
5
|
+
* of distortion of the image.
|
|
6
|
+
*
|
|
7
|
+
* Alignment patterns are present only in QR Code symbols of version 2 or larger
|
|
8
|
+
* and their number depends on the symbol version.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const getSymbolSize = require('./utils').getSymbolSize
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Calculate the row/column coordinates of the center module of each alignment pattern
|
|
15
|
+
* for the specified QR Code version.
|
|
16
|
+
*
|
|
17
|
+
* The alignment patterns are positioned symmetrically on either side of the diagonal
|
|
18
|
+
* running from the top left corner of the symbol to the bottom right corner.
|
|
19
|
+
*
|
|
20
|
+
* Since positions are simmetrical only half of the coordinates are returned.
|
|
21
|
+
* Each item of the array will represent in turn the x and y coordinate.
|
|
22
|
+
* @see {@link getPositions}
|
|
23
|
+
*
|
|
24
|
+
* @param {Number} version QR Code version
|
|
25
|
+
* @return {Array} Array of coordinate
|
|
26
|
+
*/
|
|
27
|
+
exports.getRowColCoords = function getRowColCoords (version) {
|
|
28
|
+
if (version === 1) return []
|
|
29
|
+
|
|
30
|
+
const posCount = Math.floor(version / 7) + 2
|
|
31
|
+
const size = getSymbolSize(version)
|
|
32
|
+
const intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2
|
|
33
|
+
const positions = [size - 7] // Last coord is always (size - 7)
|
|
34
|
+
|
|
35
|
+
for (let i = 1; i < posCount - 1; i++) {
|
|
36
|
+
positions[i] = positions[i - 1] - intervals
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
positions.push(6) // First coord is always 6
|
|
40
|
+
|
|
41
|
+
return positions.reverse()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns an array containing the positions of each alignment pattern.
|
|
46
|
+
* Each array's element represent the center point of the pattern as (x, y) coordinates
|
|
47
|
+
*
|
|
48
|
+
* Coordinates are calculated expanding the row/column coordinates returned by {@link getRowColCoords}
|
|
49
|
+
* and filtering out the items that overlaps with finder pattern
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* For a Version 7 symbol {@link getRowColCoords} returns values 6, 22 and 38.
|
|
53
|
+
* The alignment patterns, therefore, are to be centered on (row, column)
|
|
54
|
+
* positions (6,22), (22,6), (22,22), (22,38), (38,22), (38,38).
|
|
55
|
+
* Note that the coordinates (6,6), (6,38), (38,6) are occupied by finder patterns
|
|
56
|
+
* and are not therefore used for alignment patterns.
|
|
57
|
+
*
|
|
58
|
+
* let pos = getPositions(7)
|
|
59
|
+
* // [[6,22], [22,6], [22,22], [22,38], [38,22], [38,38]]
|
|
60
|
+
*
|
|
61
|
+
* @param {Number} version QR Code version
|
|
62
|
+
* @return {Array} Array of coordinates
|
|
63
|
+
*/
|
|
64
|
+
exports.getPositions = function getPositions (version) {
|
|
65
|
+
const coords = []
|
|
66
|
+
const pos = exports.getRowColCoords(version)
|
|
67
|
+
const posLength = pos.length
|
|
68
|
+
|
|
69
|
+
for (let i = 0; i < posLength; i++) {
|
|
70
|
+
for (let j = 0; j < posLength; j++) {
|
|
71
|
+
// Skip if position is occupied by finder patterns
|
|
72
|
+
if ((i === 0 && j === 0) || // top-left
|
|
73
|
+
(i === 0 && j === posLength - 1) || // bottom-left
|
|
74
|
+
(i === posLength - 1 && j === 0)) { // top-right
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
coords.push([pos[i], pos[j]])
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return coords
|
|
83
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const Mode = require('./mode')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Array of characters available in alphanumeric mode
|
|
5
|
+
*
|
|
6
|
+
* As per QR Code specification, to each character
|
|
7
|
+
* is assigned a value from 0 to 44 which in this case coincides
|
|
8
|
+
* with the array index
|
|
9
|
+
*
|
|
10
|
+
* @type {Array}
|
|
11
|
+
*/
|
|
12
|
+
const ALPHA_NUM_CHARS = [
|
|
13
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
14
|
+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
|
15
|
+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
|
16
|
+
' ', '$', '%', '*', '+', '-', '.', '/', ':'
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
function AlphanumericData (data) {
|
|
20
|
+
this.mode = Mode.ALPHANUMERIC
|
|
21
|
+
this.data = data
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
AlphanumericData.getBitsLength = function getBitsLength (length) {
|
|
25
|
+
return 11 * Math.floor(length / 2) + 6 * (length % 2)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
AlphanumericData.prototype.getLength = function getLength () {
|
|
29
|
+
return this.data.length
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
AlphanumericData.prototype.getBitsLength = function getBitsLength () {
|
|
33
|
+
return AlphanumericData.getBitsLength(this.data.length)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
AlphanumericData.prototype.write = function write (bitBuffer) {
|
|
37
|
+
let i
|
|
38
|
+
|
|
39
|
+
// Input data characters are divided into groups of two characters
|
|
40
|
+
// and encoded as 11-bit binary codes.
|
|
41
|
+
for (i = 0; i + 2 <= this.data.length; i += 2) {
|
|
42
|
+
// The character value of the first character is multiplied by 45
|
|
43
|
+
let value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45
|
|
44
|
+
|
|
45
|
+
// The character value of the second digit is added to the product
|
|
46
|
+
value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])
|
|
47
|
+
|
|
48
|
+
// The sum is then stored as 11-bit binary number
|
|
49
|
+
bitBuffer.put(value, 11)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// If the number of input data characters is not a multiple of two,
|
|
53
|
+
// the character value of the final character is encoded as a 6-bit binary number.
|
|
54
|
+
if (this.data.length % 2) {
|
|
55
|
+
bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = AlphanumericData
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
function BitBuffer () {
|
|
2
|
+
this.buffer = []
|
|
3
|
+
this.length = 0
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
BitBuffer.prototype = {
|
|
7
|
+
|
|
8
|
+
get: function (index) {
|
|
9
|
+
const bufIndex = Math.floor(index / 8)
|
|
10
|
+
return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) === 1
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
put: function (num, length) {
|
|
14
|
+
for (let i = 0; i < length; i++) {
|
|
15
|
+
this.putBit(((num >>> (length - i - 1)) & 1) === 1)
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
getLengthInBits: function () {
|
|
20
|
+
return this.length
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
putBit: function (bit) {
|
|
24
|
+
const bufIndex = Math.floor(this.length / 8)
|
|
25
|
+
if (this.buffer.length <= bufIndex) {
|
|
26
|
+
this.buffer.push(0)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (bit) {
|
|
30
|
+
this.buffer[bufIndex] |= (0x80 >>> (this.length % 8))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.length++
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = BitBuffer
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper class to handle QR Code symbol modules
|
|
3
|
+
*
|
|
4
|
+
* @param {Number} size Symbol size
|
|
5
|
+
*/
|
|
6
|
+
function BitMatrix (size) {
|
|
7
|
+
if (!size || size < 1) {
|
|
8
|
+
throw new Error('BitMatrix size must be defined and greater than 0')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
this.size = size
|
|
12
|
+
this.data = new Uint8Array(size * size)
|
|
13
|
+
this.reservedBit = new Uint8Array(size * size)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Set bit value at specified location
|
|
18
|
+
* If reserved flag is set, this bit will be ignored during masking process
|
|
19
|
+
*
|
|
20
|
+
* @param {Number} row
|
|
21
|
+
* @param {Number} col
|
|
22
|
+
* @param {Boolean} value
|
|
23
|
+
* @param {Boolean} reserved
|
|
24
|
+
*/
|
|
25
|
+
BitMatrix.prototype.set = function (row, col, value, reserved) {
|
|
26
|
+
const index = row * this.size + col
|
|
27
|
+
this.data[index] = value
|
|
28
|
+
if (reserved) this.reservedBit[index] = true
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns bit value at specified location
|
|
33
|
+
*
|
|
34
|
+
* @param {Number} row
|
|
35
|
+
* @param {Number} col
|
|
36
|
+
* @return {Boolean}
|
|
37
|
+
*/
|
|
38
|
+
BitMatrix.prototype.get = function (row, col) {
|
|
39
|
+
return this.data[row * this.size + col]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Applies xor operator at specified location
|
|
44
|
+
* (used during masking process)
|
|
45
|
+
*
|
|
46
|
+
* @param {Number} row
|
|
47
|
+
* @param {Number} col
|
|
48
|
+
* @param {Boolean} value
|
|
49
|
+
*/
|
|
50
|
+
BitMatrix.prototype.xor = function (row, col, value) {
|
|
51
|
+
this.data[row * this.size + col] ^= value
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check if bit at specified location is reserved
|
|
56
|
+
*
|
|
57
|
+
* @param {Number} row
|
|
58
|
+
* @param {Number} col
|
|
59
|
+
* @return {Boolean}
|
|
60
|
+
*/
|
|
61
|
+
BitMatrix.prototype.isReserved = function (row, col) {
|
|
62
|
+
return this.reservedBit[row * this.size + col]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = BitMatrix
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const Mode = require('./mode')
|
|
2
|
+
|
|
3
|
+
function ByteData (data) {
|
|
4
|
+
this.mode = Mode.BYTE
|
|
5
|
+
if (typeof (data) === 'string') {
|
|
6
|
+
this.data = new TextEncoder().encode(data)
|
|
7
|
+
} else {
|
|
8
|
+
this.data = new Uint8Array(data)
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
ByteData.getBitsLength = function getBitsLength (length) {
|
|
13
|
+
return length * 8
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
ByteData.prototype.getLength = function getLength () {
|
|
17
|
+
return this.data.length
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
ByteData.prototype.getBitsLength = function getBitsLength () {
|
|
21
|
+
return ByteData.getBitsLength(this.data.length)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
ByteData.prototype.write = function (bitBuffer) {
|
|
25
|
+
for (let i = 0, l = this.data.length; i < l; i++) {
|
|
26
|
+
bitBuffer.put(this.data[i], 8)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = ByteData
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
```
|
|
2
|
+
Dijkstra path-finding functions. Adapted from the Dijkstar Python project.
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2008
|
|
5
|
+
Wyatt Baldwin <self@wyattbaldwin.com>
|
|
6
|
+
All rights reserved
|
|
7
|
+
|
|
8
|
+
Licensed under the MIT license.
|
|
9
|
+
|
|
10
|
+
http://www.opensource.org/licenses/mit-license.php
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
14
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
15
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
16
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
18
|
+
THE SOFTWARE.
|
|
19
|
+
```
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/******************************************************************************
|
|
4
|
+
* Created 2008-08-19.
|
|
5
|
+
*
|
|
6
|
+
* Dijkstra path-finding functions. Adapted from the Dijkstar Python project.
|
|
7
|
+
*
|
|
8
|
+
* Copyright (C) 2008
|
|
9
|
+
* Wyatt Baldwin <self@wyattbaldwin.com>
|
|
10
|
+
* All rights reserved
|
|
11
|
+
*
|
|
12
|
+
* Licensed under the MIT license.
|
|
13
|
+
*
|
|
14
|
+
* http://www.opensource.org/licenses/mit-license.php
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
* THE SOFTWARE.
|
|
23
|
+
*****************************************************************************/
|
|
24
|
+
var dijkstra = {
|
|
25
|
+
single_source_shortest_paths: function(graph, s, d) {
|
|
26
|
+
// Predecessor map for each node that has been encountered.
|
|
27
|
+
// node ID => predecessor node ID
|
|
28
|
+
var predecessors = {};
|
|
29
|
+
|
|
30
|
+
// Costs of shortest paths from s to all nodes encountered.
|
|
31
|
+
// node ID => cost
|
|
32
|
+
var costs = {};
|
|
33
|
+
costs[s] = 0;
|
|
34
|
+
|
|
35
|
+
// Costs of shortest paths from s to all nodes encountered; differs from
|
|
36
|
+
// `costs` in that it provides easy access to the node that currently has
|
|
37
|
+
// the known shortest path from s.
|
|
38
|
+
// XXX: Do we actually need both `costs` and `open`?
|
|
39
|
+
var open = dijkstra.PriorityQueue.make();
|
|
40
|
+
open.push(s, 0);
|
|
41
|
+
|
|
42
|
+
var closest,
|
|
43
|
+
u, v,
|
|
44
|
+
cost_of_s_to_u,
|
|
45
|
+
adjacent_nodes,
|
|
46
|
+
cost_of_e,
|
|
47
|
+
cost_of_s_to_u_plus_cost_of_e,
|
|
48
|
+
cost_of_s_to_v,
|
|
49
|
+
first_visit;
|
|
50
|
+
while (!open.empty()) {
|
|
51
|
+
// In the nodes remaining in graph that have a known cost from s,
|
|
52
|
+
// find the node, u, that currently has the shortest path from s.
|
|
53
|
+
closest = open.pop();
|
|
54
|
+
u = closest.value;
|
|
55
|
+
cost_of_s_to_u = closest.cost;
|
|
56
|
+
|
|
57
|
+
// Get nodes adjacent to u...
|
|
58
|
+
adjacent_nodes = graph[u] || {};
|
|
59
|
+
|
|
60
|
+
// ...and explore the edges that connect u to those nodes, updating
|
|
61
|
+
// the cost of the shortest paths to any or all of those nodes as
|
|
62
|
+
// necessary. v is the node across the current edge from u.
|
|
63
|
+
for (v in adjacent_nodes) {
|
|
64
|
+
if (adjacent_nodes.hasOwnProperty(v)) {
|
|
65
|
+
// Get the cost of the edge running from u to v.
|
|
66
|
+
cost_of_e = adjacent_nodes[v];
|
|
67
|
+
|
|
68
|
+
// Cost of s to u plus the cost of u to v across e--this is *a*
|
|
69
|
+
// cost from s to v that may or may not be less than the current
|
|
70
|
+
// known cost to v.
|
|
71
|
+
cost_of_s_to_u_plus_cost_of_e = cost_of_s_to_u + cost_of_e;
|
|
72
|
+
|
|
73
|
+
// If we haven't visited v yet OR if the current known cost from s to
|
|
74
|
+
// v is greater than the new cost we just found (cost of s to u plus
|
|
75
|
+
// cost of u to v across e), update v's cost in the cost list and
|
|
76
|
+
// update v's predecessor in the predecessor list (it's now u).
|
|
77
|
+
cost_of_s_to_v = costs[v];
|
|
78
|
+
first_visit = (typeof costs[v] === 'undefined');
|
|
79
|
+
if (first_visit || cost_of_s_to_v > cost_of_s_to_u_plus_cost_of_e) {
|
|
80
|
+
costs[v] = cost_of_s_to_u_plus_cost_of_e;
|
|
81
|
+
open.push(v, cost_of_s_to_u_plus_cost_of_e);
|
|
82
|
+
predecessors[v] = u;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (typeof d !== 'undefined' && typeof costs[d] === 'undefined') {
|
|
89
|
+
var msg = ['Could not find a path from ', s, ' to ', d, '.'].join('');
|
|
90
|
+
throw new Error(msg);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return predecessors;
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
extract_shortest_path_from_predecessor_list: function(predecessors, d) {
|
|
97
|
+
var nodes = [];
|
|
98
|
+
var u = d;
|
|
99
|
+
var predecessor;
|
|
100
|
+
while (u) {
|
|
101
|
+
nodes.push(u);
|
|
102
|
+
predecessor = predecessors[u];
|
|
103
|
+
u = predecessors[u];
|
|
104
|
+
}
|
|
105
|
+
nodes.reverse();
|
|
106
|
+
return nodes;
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
find_path: function(graph, s, d) {
|
|
110
|
+
var predecessors = dijkstra.single_source_shortest_paths(graph, s, d);
|
|
111
|
+
return dijkstra.extract_shortest_path_from_predecessor_list(
|
|
112
|
+
predecessors, d);
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A very naive priority queue implementation.
|
|
117
|
+
*/
|
|
118
|
+
PriorityQueue: {
|
|
119
|
+
make: function (opts) {
|
|
120
|
+
var T = dijkstra.PriorityQueue,
|
|
121
|
+
t = {},
|
|
122
|
+
key;
|
|
123
|
+
opts = opts || {};
|
|
124
|
+
for (key in T) {
|
|
125
|
+
if (T.hasOwnProperty(key)) {
|
|
126
|
+
t[key] = T[key];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
t.queue = [];
|
|
130
|
+
t.sorter = opts.sorter || T.default_sorter;
|
|
131
|
+
return t;
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
default_sorter: function (a, b) {
|
|
135
|
+
return a.cost - b.cost;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Add a new item to the queue and ensure the highest priority element
|
|
140
|
+
* is at the front of the queue.
|
|
141
|
+
*/
|
|
142
|
+
push: function (value, cost) {
|
|
143
|
+
var item = {value: value, cost: cost};
|
|
144
|
+
this.queue.push(item);
|
|
145
|
+
this.queue.sort(this.sorter);
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Return the highest priority element in the queue.
|
|
150
|
+
*/
|
|
151
|
+
pop: function () {
|
|
152
|
+
return this.queue.shift();
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
empty: function () {
|
|
156
|
+
return this.queue.length === 0;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
// node.js module exports
|
|
163
|
+
if (typeof module !== 'undefined') {
|
|
164
|
+
module.exports = dijkstra;
|
|
165
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const ECLevel = require('./error-correction-level')
|
|
2
|
+
|
|
3
|
+
const EC_BLOCKS_TABLE = [
|
|
4
|
+
// L M Q H
|
|
5
|
+
1, 1, 1, 1,
|
|
6
|
+
1, 1, 1, 1,
|
|
7
|
+
1, 1, 2, 2,
|
|
8
|
+
1, 2, 2, 4,
|
|
9
|
+
1, 2, 4, 4,
|
|
10
|
+
2, 4, 4, 4,
|
|
11
|
+
2, 4, 6, 5,
|
|
12
|
+
2, 4, 6, 6,
|
|
13
|
+
2, 5, 8, 8,
|
|
14
|
+
4, 5, 8, 8,
|
|
15
|
+
4, 5, 8, 11,
|
|
16
|
+
4, 8, 10, 11,
|
|
17
|
+
4, 9, 12, 16,
|
|
18
|
+
4, 9, 16, 16,
|
|
19
|
+
6, 10, 12, 18,
|
|
20
|
+
6, 10, 17, 16,
|
|
21
|
+
6, 11, 16, 19,
|
|
22
|
+
6, 13, 18, 21,
|
|
23
|
+
7, 14, 21, 25,
|
|
24
|
+
8, 16, 20, 25,
|
|
25
|
+
8, 17, 23, 25,
|
|
26
|
+
9, 17, 23, 34,
|
|
27
|
+
9, 18, 25, 30,
|
|
28
|
+
10, 20, 27, 32,
|
|
29
|
+
12, 21, 29, 35,
|
|
30
|
+
12, 23, 34, 37,
|
|
31
|
+
12, 25, 34, 40,
|
|
32
|
+
13, 26, 35, 42,
|
|
33
|
+
14, 28, 38, 45,
|
|
34
|
+
15, 29, 40, 48,
|
|
35
|
+
16, 31, 43, 51,
|
|
36
|
+
17, 33, 45, 54,
|
|
37
|
+
18, 35, 48, 57,
|
|
38
|
+
19, 37, 51, 60,
|
|
39
|
+
19, 38, 53, 63,
|
|
40
|
+
20, 40, 56, 66,
|
|
41
|
+
21, 43, 59, 70,
|
|
42
|
+
22, 45, 62, 74,
|
|
43
|
+
24, 47, 65, 77,
|
|
44
|
+
25, 49, 68, 81
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
const EC_CODEWORDS_TABLE = [
|
|
48
|
+
// L M Q H
|
|
49
|
+
7, 10, 13, 17,
|
|
50
|
+
10, 16, 22, 28,
|
|
51
|
+
15, 26, 36, 44,
|
|
52
|
+
20, 36, 52, 64,
|
|
53
|
+
26, 48, 72, 88,
|
|
54
|
+
36, 64, 96, 112,
|
|
55
|
+
40, 72, 108, 130,
|
|
56
|
+
48, 88, 132, 156,
|
|
57
|
+
60, 110, 160, 192,
|
|
58
|
+
72, 130, 192, 224,
|
|
59
|
+
80, 150, 224, 264,
|
|
60
|
+
96, 176, 260, 308,
|
|
61
|
+
104, 198, 288, 352,
|
|
62
|
+
120, 216, 320, 384,
|
|
63
|
+
132, 240, 360, 432,
|
|
64
|
+
144, 280, 408, 480,
|
|
65
|
+
168, 308, 448, 532,
|
|
66
|
+
180, 338, 504, 588,
|
|
67
|
+
196, 364, 546, 650,
|
|
68
|
+
224, 416, 600, 700,
|
|
69
|
+
224, 442, 644, 750,
|
|
70
|
+
252, 476, 690, 816,
|
|
71
|
+
270, 504, 750, 900,
|
|
72
|
+
300, 560, 810, 960,
|
|
73
|
+
312, 588, 870, 1050,
|
|
74
|
+
336, 644, 952, 1110,
|
|
75
|
+
360, 700, 1020, 1200,
|
|
76
|
+
390, 728, 1050, 1260,
|
|
77
|
+
420, 784, 1140, 1350,
|
|
78
|
+
450, 812, 1200, 1440,
|
|
79
|
+
480, 868, 1290, 1530,
|
|
80
|
+
510, 924, 1350, 1620,
|
|
81
|
+
540, 980, 1440, 1710,
|
|
82
|
+
570, 1036, 1530, 1800,
|
|
83
|
+
570, 1064, 1590, 1890,
|
|
84
|
+
600, 1120, 1680, 1980,
|
|
85
|
+
630, 1204, 1770, 2100,
|
|
86
|
+
660, 1260, 1860, 2220,
|
|
87
|
+
720, 1316, 1950, 2310,
|
|
88
|
+
750, 1372, 2040, 2430
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Returns the number of error correction block that the QR Code should contain
|
|
93
|
+
* for the specified version and error correction level.
|
|
94
|
+
*
|
|
95
|
+
* @param {Number} version QR Code version
|
|
96
|
+
* @param {Number} errorCorrectionLevel Error correction level
|
|
97
|
+
* @return {Number} Number of error correction blocks
|
|
98
|
+
*/
|
|
99
|
+
exports.getBlocksCount = function getBlocksCount (version, errorCorrectionLevel) {
|
|
100
|
+
switch (errorCorrectionLevel) {
|
|
101
|
+
case ECLevel.L:
|
|
102
|
+
return EC_BLOCKS_TABLE[(version - 1) * 4 + 0]
|
|
103
|
+
case ECLevel.M:
|
|
104
|
+
return EC_BLOCKS_TABLE[(version - 1) * 4 + 1]
|
|
105
|
+
case ECLevel.Q:
|
|
106
|
+
return EC_BLOCKS_TABLE[(version - 1) * 4 + 2]
|
|
107
|
+
case ECLevel.H:
|
|
108
|
+
return EC_BLOCKS_TABLE[(version - 1) * 4 + 3]
|
|
109
|
+
default:
|
|
110
|
+
return undefined
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Returns the number of error correction codewords to use for the specified
|
|
116
|
+
* version and error correction level.
|
|
117
|
+
*
|
|
118
|
+
* @param {Number} version QR Code version
|
|
119
|
+
* @param {Number} errorCorrectionLevel Error correction level
|
|
120
|
+
* @return {Number} Number of error correction codewords
|
|
121
|
+
*/
|
|
122
|
+
exports.getTotalCodewordsCount = function getTotalCodewordsCount (version, errorCorrectionLevel) {
|
|
123
|
+
switch (errorCorrectionLevel) {
|
|
124
|
+
case ECLevel.L:
|
|
125
|
+
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 0]
|
|
126
|
+
case ECLevel.M:
|
|
127
|
+
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 1]
|
|
128
|
+
case ECLevel.Q:
|
|
129
|
+
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 2]
|
|
130
|
+
case ECLevel.H:
|
|
131
|
+
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 3]
|
|
132
|
+
default:
|
|
133
|
+
return undefined
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
exports.L = { bit: 1 }
|
|
2
|
+
exports.M = { bit: 0 }
|
|
3
|
+
exports.Q = { bit: 3 }
|
|
4
|
+
exports.H = { bit: 2 }
|
|
5
|
+
|
|
6
|
+
function fromString (string) {
|
|
7
|
+
if (typeof string !== 'string') {
|
|
8
|
+
throw new Error('Param is not a string')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const lcStr = string.toLowerCase()
|
|
12
|
+
|
|
13
|
+
switch (lcStr) {
|
|
14
|
+
case 'l':
|
|
15
|
+
case 'low':
|
|
16
|
+
return exports.L
|
|
17
|
+
|
|
18
|
+
case 'm':
|
|
19
|
+
case 'medium':
|
|
20
|
+
return exports.M
|
|
21
|
+
|
|
22
|
+
case 'q':
|
|
23
|
+
case 'quartile':
|
|
24
|
+
return exports.Q
|
|
25
|
+
|
|
26
|
+
case 'h':
|
|
27
|
+
case 'high':
|
|
28
|
+
return exports.H
|
|
29
|
+
|
|
30
|
+
default:
|
|
31
|
+
throw new Error('Unknown EC Level: ' + string)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.isValid = function isValid (level) {
|
|
36
|
+
return level && typeof level.bit !== 'undefined' &&
|
|
37
|
+
level.bit >= 0 && level.bit < 4
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.from = function from (value, defaultValue) {
|
|
41
|
+
if (exports.isValid(value)) {
|
|
42
|
+
return value
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
return fromString(value)
|
|
47
|
+
} catch (e) {
|
|
48
|
+
return defaultValue
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const getSymbolSize = require('./utils').getSymbolSize
|
|
2
|
+
const FINDER_PATTERN_SIZE = 7
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns an array containing the positions of each finder pattern.
|
|
6
|
+
* Each array's element represent the top-left point of the pattern as (x, y) coordinates
|
|
7
|
+
*
|
|
8
|
+
* @param {Number} version QR Code version
|
|
9
|
+
* @return {Array} Array of coordinates
|
|
10
|
+
*/
|
|
11
|
+
exports.getPositions = function getPositions (version) {
|
|
12
|
+
const size = getSymbolSize(version)
|
|
13
|
+
|
|
14
|
+
return [
|
|
15
|
+
// top-left
|
|
16
|
+
[0, 0],
|
|
17
|
+
// top-right
|
|
18
|
+
[size - FINDER_PATTERN_SIZE, 0],
|
|
19
|
+
// bottom-left
|
|
20
|
+
[0, size - FINDER_PATTERN_SIZE]
|
|
21
|
+
]
|
|
22
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const Utils = require('./utils')
|
|
2
|
+
|
|
3
|
+
const G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)
|
|
4
|
+
const G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1)
|
|
5
|
+
const G15_BCH = Utils.getBCHDigit(G15)
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns format information with relative error correction bits
|
|
9
|
+
*
|
|
10
|
+
* The format information is a 15-bit sequence containing 5 data bits,
|
|
11
|
+
* with 10 error correction bits calculated using the (15, 5) BCH code.
|
|
12
|
+
*
|
|
13
|
+
* @param {Number} errorCorrectionLevel Error correction level
|
|
14
|
+
* @param {Number} mask Mask pattern
|
|
15
|
+
* @return {Number} Encoded format information bits
|
|
16
|
+
*/
|
|
17
|
+
exports.getEncodedBits = function getEncodedBits (errorCorrectionLevel, mask) {
|
|
18
|
+
const data = ((errorCorrectionLevel.bit << 3) | mask)
|
|
19
|
+
let d = data << 10
|
|
20
|
+
|
|
21
|
+
while (Utils.getBCHDigit(d) - G15_BCH >= 0) {
|
|
22
|
+
d ^= (G15 << (Utils.getBCHDigit(d) - G15_BCH))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// xor final data with mask pattern in order to ensure that
|
|
26
|
+
// no combination of Error Correction Level and data mask pattern
|
|
27
|
+
// will result in an all-zero data string
|
|
28
|
+
return ((data << 10) | d) ^ G15_MASK
|
|
29
|
+
}
|