Skip to content

Commit f829dd2

Browse files
authored
Merge pull request #226 from LinusU/uint8array
Avoid dependency on Buffer
2 parents eb2d499 + df2ba5a commit f829dd2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+7853
-7177
lines changed

.travis.yml

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
language: node_js
2-
2+
dist: bionic
33
node_js:
4-
- '4'
5-
- '6'
6-
- '8'
4+
- '10.13.0'
75
- '10'
6+
- '12'
87
- 'node'
9-
10-
env:
11-
- CXX=g++-4.8
12-
13-
addons:
14-
apt:
15-
sources:
16-
- ubuntu-toolchain-r-test
17-
packages:
18-
- g++-4.8
19-
20-
before_install:
21-
- sudo apt-get update
22-
- sudo apt-get install -y libgif-dev

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ QRCode.toCanvas(canvas, 'sample text', function (error) {
104104
```html
105105
<canvas id="canvas"></canvas>
106106

107-
<script src="/build/qrcode.min.js"></script>
107+
<script src="/build/qrcode.js"></script>
108108
<script>
109109
QRCode.toCanvas(document.getElementById('canvas'), 'sample text', function (error) {
110110
if (error) console.error(error)
@@ -113,7 +113,9 @@ QRCode.toCanvas(canvas, 'sample text', function (error) {
113113
</script>
114114
```
115115

116-
If you install through `npm`, precompiled files will be available in `node_modules/qrcode/build/` folder.<br>
116+
If you install through `npm`, precompiled files will be available in `node_modules/qrcode/build/` folder.
117+
118+
The precompiled bundle have support for [Internet Explorer 10+, Safari 5.1+, and all evergreen browsers](https://browserl.ist/?q=defaults%2C+IE+%3E%3D+10%2C+Safari+%3E%3D+5.1).
117119

118120
### NodeJS
119121
Require the module `qrcode`
@@ -347,7 +349,7 @@ QRCode.toFile(
347349
)
348350
```
349351

350-
TypeScript users: if you are using [@types/qrcode](https://www.npmjs.com/package/@types/qrcode), you will need to add a `// @ts-ignore` above the data segment because it expects `data: string`.
352+
TypeScript users: if you are using [@types/qrcode](https://www.npmjs.com/package/@types/qrcode), you will need to add a `// @ts-ignore` above the data segment because it expects `data: string`.
351353

352354
## Multibyte characters
353355
Support for multibyte characters isn't present in the initial QR Code standard, but is possible to encode UTF-8 characters in Byte mode.

build.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

examples/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var QRCode = require('../lib')
1+
const QRCode = require('../lib')
22

33
QRCode.toString('yo yo yo', function (error, data) {
44
if (error) {

examples/clientsideserver.js

Lines changed: 64 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
var express = require('express')
2-
var app = express()// .createServer()
3-
var http = require('http')
4-
var fs = require('fs')
5-
var QRCode = require('../lib')
6-
var canvasutil = require('canvasutil')
7-
var Canvas = require('canvas')
8-
var Image = Canvas.Image
9-
10-
var path = require('path')
11-
12-
// app.use(express.methodOverride())
13-
// app.use(express.bodyParser())
14-
// app.use(app.router)
15-
// app.use(express.static(path.resolve(__dirname, '..')))
1+
const express = require('express')
2+
const app = express()// .createServer()
3+
const http = require('http')
4+
const fs = require('fs')
5+
const QRCode = require('../lib')
6+
const canvasutil = require('canvasutil')
7+
const { createCanvas, loadImage } = require('canvas')
8+
9+
const path = require('path')
10+
11+
// app.use(express.methodOverride())
12+
// app.use(express.bodyParser())
13+
// app.use(app.router)
14+
// app.use(express.static(path.resolve(__dirname, '..')))
1615

1716
app.get('/qrcode.js', (req, res) => {
1817
res.set('content-type', 'text/javascript')
@@ -30,12 +29,12 @@ app.get('/', function (req, res) {
3029
})
3130
})
3231

33-
var effectHandlers = {}
32+
const effectHandlers = {}
3433

3534
app.get('/generate', function (req, res) {
36-
var q = req.query || {}
35+
const q = req.query || {}
3736

38-
var effect = q.effect || 'plain'
37+
let effect = q.effect || 'plain'
3938
if (!effectHandlers[effect]) {
4039
effect = 'plain'
4140
}
@@ -49,7 +48,7 @@ app.get('/generate', function (req, res) {
4948
}
5049
})
5150
} else {
52-
var msg = error.message + '\n' + error.stack
51+
const msg = error.message + '\n' + error.stack
5352
res.header('Content-Type', 'text/plain')
5453
res.send(msg)
5554
console.error(msg)
@@ -73,28 +72,28 @@ effectHandlers.bacon = function (args, cb) {
7372
}
7473

7574
effectHandlers.rounded = function (args, cb) {
76-
var canvas = new Canvas(200, 200)
75+
const canvas = createCanvas(200, 200)
7776
QRCode.toCanvas(canvas, args.text || '', function (err) {
7877
if (err) {
7978
cb(err, canvas)
8079
return
8180
}
8281

83-
var tpx = new canvasutil.PixelCore()
84-
var luma709Only = canvasutil.conversionLib.luma709Only
85-
var up = []
86-
var down = []
87-
var left = []
88-
var right = []
89-
var upPx
90-
var downPx
91-
var leftPx
92-
var rightPx
93-
var r
94-
var t
95-
var l
96-
var d
97-
var corner = 0
82+
const tpx = new canvasutil.PixelCore()
83+
const luma709Only = canvasutil.conversionLib.luma709Only
84+
const up = []
85+
const down = []
86+
const left = []
87+
const right = []
88+
let upPx
89+
let downPx
90+
let leftPx
91+
let rightPx
92+
let r
93+
let t
94+
let l
95+
let d
96+
let corner = 0
9897

9998
tpx.threshold = 100
10099

@@ -162,21 +161,21 @@ effectHandlers.rounded = function (args, cb) {
162161
}
163162
}
164163
})
165-
cb(false, canvas)
164+
cb(null, canvas)
166165
})
167166
}
168167

169168
effectHandlers.remoteImage = function (args, cb) {
170-
var src = args.src
171-
var domain
172-
var uri
169+
let src = args.src
170+
let domain
171+
let uri
173172

174173
if (!src) {
175174
cb(new Error('src required'), null)
176175
} else {
177176
if (src.indexof('://') !== -1) {
178177
src = src.split('://').unshift()
179-
var parts = src.split('/')
178+
const parts = src.split('/')
180179

181180
domain = parts.shift()
182181
uri = parts.join('/')
@@ -188,29 +187,29 @@ effectHandlers.remoteImage = function (args, cb) {
188187
return
189188
}
190189

191-
var options = {
190+
const options = {
192191
host: domain,
193192
port: 80,
194193
path: uri,
195194
method: 'GET'
196195
}
197196

198-
var req = http.request(options, function (res) {
197+
const req = http.request(options, function (res) {
199198
if (res.statusCode < 200 || res.statusCode > 299) {
200199
cb(new Error('http ' + res.statusCode + ' response code'), null)
201200
return
202201
}
203202

204203
res.setEncoding('utf8')
205204

206-
var data = ''
205+
let data = ''
207206
res.on('data', function (chunk) {
208207
data += chunk
209208
console.log('BODY: ' + chunk)
210209
})
211210

212211
res.on('complete', function () {
213-
cb(false, data)
212+
cb(null, data)
214213
})
215214

216215
res.on('error', function (error) {
@@ -223,31 +222,28 @@ effectHandlers.remoteImage = function (args, cb) {
223222
}
224223

225224
effectHandlers.image = function (args, cb) {
226-
var src = args.src || ''
227-
228-
var img = new Image()
229-
var convert = canvasutil.conversionLib
230-
img.onload = function () {
231-
var canvas = new Canvas(200, 200)
225+
loadImage(args.src || '').then((img) => {
226+
const convert = canvasutil.conversionLib
227+
const canvas = createCanvas(200, 200)
232228
QRCode.toCanvas(canvas, args.text || '', function (err) {
233229
if (err) {
234230
cb(err, false)
235231
return
236232
}
237233

238-
var codeCtx = canvas.getContext('2d')
239-
var frame = codeCtx.getImageData(0, 0, canvas.width, canvas.width)
240-
var tpx = new canvasutil.PixelCore()
241-
var baconCanvas = new Canvas(canvas.width, canvas.width)
242-
var ctx = baconCanvas.getContext('2d')
243-
var topThreshold = args.darkThreshold || 25
244-
var bottomThreshold = args.lightThreshold || 75
234+
const codeCtx = canvas.getContext('2d')
235+
const frame = codeCtx.getImageData(0, 0, canvas.width, canvas.width)
236+
const tpx = new canvasutil.PixelCore()
237+
const baconCanvas = createCanvas(canvas.width, canvas.width)
238+
const ctx = baconCanvas.getContext('2d')
239+
const topThreshold = args.darkThreshold || 25
240+
const bottomThreshold = args.lightThreshold || 75
245241

246242
tpx.threshold = 50
247243

248244
// scale image
249-
var w = canvas.width
250-
var h = canvas.height
245+
let w = canvas.width
246+
let h = canvas.height
251247

252248
if (img.width > img.height) {
253249
w = w * (canvas.height / h)
@@ -260,10 +256,10 @@ effectHandlers.image = function (args, cb) {
260256

261257
try {
262258
tpx.iterate(baconCanvas, function (px, i, l, pixels, w, h, pixelCore) {
263-
var luma = (0.2125 * px.r + 0.7154 * px.g + 0.0721 * px.b)
264-
var codeLuma = convert.luma709Only(frame.data[i * 4], frame.data[i * 4 + 1], frame.data[i * 4 + 2])
265-
var yuv
266-
var rgb
259+
const luma = (0.2125 * px.r + 0.7154 * px.g + 0.0721 * px.b)
260+
const codeLuma = convert.luma709Only(frame.data[i * 4], frame.data[i * 4 + 1], frame.data[i * 4 + 2])
261+
let yuv
262+
let rgb
267263

268264
if (codeLuma > pixelCore.threshold) {
269265
if (luma < bottomThreshold) {
@@ -292,21 +288,16 @@ effectHandlers.image = function (args, cb) {
292288
cb(err, false)
293289
}
294290

295-
cb(false, baconCanvas)
291+
cb(null, baconCanvas)
296292
})
297-
}
298-
299-
img.onerror = function (error) {
300-
error.message += ' (' + src + ')'
293+
}, (error) => {
301294
cb(error, null)
302-
}
303-
304-
img.src = src
295+
})
305296
}
306297

307298
effectHandlers.plain = function (args, cb) {
308-
var canvas = new Canvas(200, 200)
309-
var text = args.text || ''
299+
const canvas = createCanvas(200, 200)
300+
const text = args.text || ''
310301
QRCode.toCanvas(canvas, text || '', function (err) {
311302
cb(err, canvas)
312303
})

0 commit comments

Comments
 (0)