-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (93 loc) · 2.77 KB
/
index.js
File metadata and controls
98 lines (93 loc) · 2.77 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const sharp = require('sharp');
const strategy = {
balanced: 0,
'min-height': 1,
minHeight: 1,
'min-width': 2,
minWidth: 2
}
const correction = {
auto: 0,
medium: 1,
high: 2
}
class rMQR {
async generate(text, options={}) {
if ((typeof text).toLowerCase() !== 'string') {
throw new Error('Expected String recived instance of '+typeof text);
}
if (text.length < 1) {
throw new Error('Input must be atleast one letter');
}
if (text.length > (150 - (options?.correction === correction.high ? 76 : 0))) {
throw new Error('Input is too long');
}
if (options.strategy) {
if (!Object.values(strategy).includes(options.strategy)) {
throw new Error('Unknown strategy');
}
}
if (options.correction) {
if (!Object.values(correction).includes(options.correction)) {
throw new Error('Unknown correction');
}
}
// Fetch for now because implementing the whole qr specification is a *bit* hard
let req = await fetch('https://asia-northeast1-rmqr-generator.cloudfunctions.net/generate-rmqr-code', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
text: text,
versionStrategy: ['balanced','minimize_height','minimize_width'][options?.strategy ?? 0],
errorCorrectionLevel: ['auto','m','h'][options?.correction ?? 0]
})
});
let res = await req.json();
return res;
}
toImage(data, format='png', type='buffer', size=8) {
let alias = {
jpg: 'jpeg'
}
if (Object.keys(alias).includes(format)) format = alias[format];
return new Promise((resolve, reject) => {
let qr = new sharp(Buffer.from(data.qr.flat().map(p=>!p*255)), {
raw: {
width: data.width,
height: data.height,
channels: 1
}
})
.toFormat(format)
.resize(data.width*size, data.height*size, {
kernel: sharp.kernel.nearest
})
.toBuffer()
.then(outputBuffer => {
Object.keys(alias).map(key => {alias[alias[key]] = key; delete alias[key]});
if (Object.keys(alias).includes(format)) format = alias[format];
switch (type) {
case 'buffer':
resolve(outputBuffer);
break;
case 'uri':
resolve(`data:image/${format};base64,${outputBuffer.toString('base64')}`);
break;
default:
reject('Unsoported type');
break;
}
})
.catch(err=>{
reject(err);
})
});
}
}
module.exports = {
rmqr: rMQR,
strategy: strategy,
correction: correction
}