Skip to content

Commit e2a3c4c

Browse files
authored
linter: remove compatibility rules for older Node.js versions (#131)
* linter: remove compatibility rules for older Node.js versions Signed-off-by: Sebastian Beltran <[email protected]> * fix: initialize buffer variable for more global scope Signed-off-by: Sebastian Beltran <[email protected]> --------- Signed-off-by: Sebastian Beltran <[email protected]>
1 parent 27526d6 commit e2a3c4c

File tree

8 files changed

+151
-157
lines changed

8 files changed

+151
-157
lines changed

eslint.config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,5 @@ const neostandard = require('neostandard')
44
module.exports = [
55
...neostandard({
66
env: ['mocha'],
7-
}),
8-
{
9-
rules: {
10-
'object-shorthand': ['off'], // Compatibility with older code
11-
'no-var': ['off'], // Compatibility with older code
12-
'no-redeclare': ['off'], // Because we use var for compatibility with node 0.10
13-
}
14-
}
7+
})
158
]

index.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*/
1414

1515
const { AsyncResource } = require('async_hooks')
16-
var bytes = require('bytes')
17-
var createError = require('http-errors')
18-
var iconv = require('iconv-lite')
16+
const bytes = require('bytes')
17+
const createError = require('http-errors')
18+
const iconv = require('iconv-lite')
1919

2020
/**
2121
* Module exports.
@@ -29,7 +29,7 @@ module.exports = getRawBody
2929
* @private
3030
*/
3131

32-
var ICONV_ENCODING_MESSAGE_REGEXP = /^Encoding not recognized: /
32+
const ICONV_ENCODING_MESSAGE_REGEXP = /^Encoding not recognized: /
3333

3434
/**
3535
* Get the decoder for a given encoding.
@@ -49,7 +49,7 @@ function getDecoder (encoding) {
4949

5050
// the encoding was not found
5151
throw createError(415, 'specified encoding unsupported', {
52-
encoding: encoding,
52+
encoding,
5353
type: 'encoding.unsupported'
5454
})
5555
}
@@ -65,8 +65,8 @@ function getDecoder (encoding) {
6565
*/
6666

6767
function getRawBody (stream, options, callback) {
68-
var done = callback
69-
var opts = options || {}
68+
let done = callback
69+
let opts = options || {}
7070

7171
// light validation
7272
if (stream === undefined) {
@@ -98,15 +98,15 @@ function getRawBody (stream, options, callback) {
9898
}
9999

100100
// get encoding
101-
var encoding = opts.encoding !== true
101+
const encoding = opts.encoding !== true
102102
? opts.encoding
103103
: 'utf-8'
104104

105105
// convert the limit to an integer
106-
var limit = bytes.parse(opts.limit)
106+
const limit = bytes.parse(opts.limit)
107107

108108
// convert the expected length to an integer
109-
var length = opts.length != null && !isNaN(opts.length)
109+
const length = opts.length != null && !isNaN(opts.length)
110110
? parseInt(opts.length, 10)
111111
: null
112112

@@ -152,17 +152,18 @@ function halt (stream) {
152152
*/
153153

154154
function readStream (stream, encoding, length, limit, callback) {
155-
var complete = false
156-
var sync = true
155+
let buffer
156+
let complete = false
157+
let sync = true
157158

158159
// check the length and limit options.
159160
// note: we intentionally leave the stream paused,
160161
// so users should handle the stream themselves.
161162
if (limit !== null && length !== null && length > limit) {
162163
return done(createError(413, 'request entity too large', {
163164
expected: length,
164-
length: length,
165-
limit: limit,
165+
length,
166+
limit,
166167
type: 'entity.too.large'
167168
}))
168169
}
@@ -172,7 +173,7 @@ function readStream (stream, encoding, length, limit, callback) {
172173
// stream._decoder: streams1
173174
// state.encoding: streams2
174175
// state.decoder: streams2, specifically < 0.10.6
175-
var state = stream._readableState
176+
const state = stream._readableState
176177
if (stream._decoder || (state && (state.encoding || state.decoder))) {
177178
// developer error
178179
return done(createError(500, 'stream encoding should not be set', {
@@ -186,16 +187,16 @@ function readStream (stream, encoding, length, limit, callback) {
186187
}))
187188
}
188189

189-
var received = 0
190-
var decoder
190+
let received = 0
191+
let decoder
191192

192193
try {
193194
decoder = getDecoder(encoding)
194195
} catch (err) {
195196
return done(err)
196197
}
197198

198-
var buffer = decoder
199+
buffer = decoder
199200
? ''
200201
: []
201202

@@ -210,10 +211,10 @@ function readStream (stream, encoding, length, limit, callback) {
210211
sync = false
211212

212213
function done () {
213-
var args = new Array(arguments.length)
214+
const args = new Array(arguments.length)
214215

215216
// copy arguments
216-
for (var i = 0; i < args.length; i++) {
217+
for (let i = 0; i < args.length; i++) {
217218
args[i] = arguments[i]
218219
}
219220

@@ -244,8 +245,8 @@ function readStream (stream, encoding, length, limit, callback) {
244245
done(createError(400, 'request aborted', {
245246
code: 'ECONNABORTED',
246247
expected: length,
247-
length: length,
248-
received: received,
248+
length,
249+
received,
249250
type: 'request.aborted'
250251
}))
251252
}
@@ -257,8 +258,8 @@ function readStream (stream, encoding, length, limit, callback) {
257258

258259
if (limit !== null && received > limit) {
259260
done(createError(413, 'request entity too large', {
260-
limit: limit,
261-
received: received,
261+
limit,
262+
received,
262263
type: 'entity.too.large'
263264
}))
264265
} else if (decoder) {
@@ -275,12 +276,12 @@ function readStream (stream, encoding, length, limit, callback) {
275276
if (length !== null && received !== length) {
276277
done(createError(400, 'request size did not match content length', {
277278
expected: length,
278-
length: length,
279-
received: received,
279+
length,
280+
received,
280281
type: 'request.size.invalid'
281282
}))
282283
} else {
283-
var string = decoder
284+
const string = decoder
284285
? buffer + (decoder.end() || '')
285286
: Buffer.concat(buffer)
286287
done(null, string)

scripts/version-history.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

3-
var fs = require('fs')
4-
var path = require('path')
3+
const fs = require('fs')
4+
const path = require('path')
55

6-
var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md')
7-
var MD_HEADER_REGEXP = /^====*$/
8-
var VERSION = process.env.npm_package_version
9-
var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/
6+
const HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md')
7+
const MD_HEADER_REGEXP = /^====*$/
8+
const VERSION = process.env.npm_package_version
9+
const VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/
1010

11-
var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n')
11+
const historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n')
1212

1313
if (!MD_HEADER_REGEXP.test(historyFileLines[1])) {
1414
console.error('Missing header in HISTORY.md')
@@ -21,7 +21,7 @@ if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) {
2121
}
2222

2323
if (historyFileLines[0].indexOf('x') !== -1) {
24-
var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$')
24+
const versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$')
2525

2626
if (!versionCheckRegExp.test(VERSION)) {
2727
console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0])
@@ -35,25 +35,25 @@ historyFileLines[1] = repeat('=', historyFileLines[0].length)
3535
fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n'))
3636

3737
function getLocaleDate () {
38-
var now = new Date()
38+
const now = new Date()
3939

4040
return zeroPad(now.getFullYear(), 4) + '-' +
4141
zeroPad(now.getMonth() + 1, 2) + '-' +
4242
zeroPad(now.getDate(), 2)
4343
}
4444

4545
function repeat (str, length) {
46-
var out = ''
46+
let out = ''
4747

48-
for (var i = 0; i < length; i++) {
48+
for (let i = 0; i < length; i++) {
4949
out += str
5050
}
5151

5252
return out
5353
}
5454

5555
function zeroPad (number, length) {
56-
var num = number.toString()
56+
let num = number.toString()
5757

5858
while (num.length < length) {
5959
num = '0' + num

test/flowing.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var assert = require('assert')
2-
var Readable = require('stream').Readable
3-
var Writable = require('stream').Writable
1+
const assert = require('assert')
2+
const Readable = require('stream').Readable
3+
const Writable = require('stream').Writable
44

5-
var getRawBody = require('../')
5+
const getRawBody = require('../')
66

7-
var defaultLimit = 1024 * 1024
7+
const defaultLimit = 1024 * 1024
88

99
describe('stream flowing', function () {
1010
this.timeout(5000)
1111

1212
describe('when limit lower then length', function (done) {
1313
it('should stop the steam flow', function (done) {
14-
var stream = createInfiniteStream()
14+
const stream = createInfiniteStream()
1515

1616
getRawBody(stream, {
1717
limit: defaultLimit,
@@ -31,8 +31,8 @@ describe('stream flowing', function () {
3131
})
3232

3333
it('should halt flowing stream', function (done) {
34-
var stream = createInfiniteStream(true)
35-
var dest = createBlackholeStream()
34+
const stream = createInfiniteStream(true)
35+
const dest = createBlackholeStream()
3636

3737
// pipe the stream
3838
stream.pipe(dest)
@@ -54,7 +54,7 @@ describe('stream flowing', function () {
5454

5555
describe('when stream has encoding set', function (done) {
5656
it('should stop the steam flow', function (done) {
57-
var stream = createInfiniteStream()
57+
const stream = createInfiniteStream()
5858
stream.setEncoding('utf8')
5959

6060
getRawBody(stream, {
@@ -73,7 +73,7 @@ describe('stream flowing', function () {
7373

7474
describe('when stream has limit', function (done) {
7575
it('should stop the steam flow', function (done) {
76-
var stream = createInfiniteStream()
76+
const stream = createInfiniteStream()
7777

7878
getRawBody(stream, {
7979
limit: defaultLimit
@@ -92,7 +92,7 @@ describe('stream flowing', function () {
9292

9393
describe('when stream has limit', function (done) {
9494
it('should stop the steam flow', function (done) {
95-
var stream = createInfiniteStream()
95+
const stream = createInfiniteStream()
9696

9797
getRawBody(stream, function (err, body) {
9898
assert.ok(err)
@@ -110,13 +110,13 @@ describe('stream flowing', function () {
110110
})
111111

112112
function createChunk () {
113-
var base = Math.random().toString(32)
114-
var KB_4 = 32 * 4
115-
var KB_8 = KB_4 * 2
116-
var KB_16 = KB_8 * 2
117-
var KB_64 = KB_16 * 4
113+
const base = Math.random().toString(32)
114+
const KB_4 = 32 * 4
115+
const KB_8 = KB_4 * 2
116+
const KB_16 = KB_8 * 2
117+
const KB_64 = KB_16 * 4
118118

119-
var rand = Math.random()
119+
const rand = Math.random()
120120
if (rand < 0.25) {
121121
return repeat(base, KB_4)
122122
} else if (rand < 0.5) {
@@ -133,7 +133,7 @@ function createChunk () {
133133
}
134134

135135
function createBlackholeStream () {
136-
var stream = new Writable()
136+
const stream = new Writable()
137137
stream._write = function (chunk, encoding, cb) {
138138
cb()
139139
}
@@ -142,12 +142,12 @@ function createBlackholeStream () {
142142
}
143143

144144
function createInfiniteStream (paused) {
145-
var stream = new Readable()
145+
const stream = new Readable()
146146
stream._read = function () {
147-
var rand = 2 + Math.floor(Math.random() * 10)
147+
const rand = 2 + Math.floor(Math.random() * 10)
148148

149149
setTimeout(function () {
150-
for (var i = 0; i < rand; i++) {
150+
for (let i = 0; i < rand; i++) {
151151
stream.push(createChunk())
152152
}
153153
}, 100)

0 commit comments

Comments
 (0)