Jelajahi Sumber

storage-node: fix linter errors

Shamil Gadelshin 4 tahun lalu
induk
melakukan
a3a7e686b2

+ 15 - 2
storage-node/.eslintrc.js

@@ -14,6 +14,19 @@ module.exports = {
         "plugin:prettier/recommended"
     ],
 	"rules": {
-		"import/no-commonjs": "off" // remove after converting to TS.
-	}
+		"import/no-commonjs": "off", // remove after converting to TS.
+		// Disabling Rules because of monorepo environment:
+		// https://github.com/benmosher/eslint-plugin-import/issues/1174
+		"import/no-extraneous-dependencies": "off"
+	},
+	"overrides": [
+		{
+			"files": ["**/test/ranges.js", ],
+			"rules": {
+				// Disabling Rules because of used chai lib:
+				// https://stackoverflow.com/questions/45079454/no-unused-expressions-in-mocha-chai-unit-test-using-standardjs
+				"no-unused-expressions": "off",
+			}
+		}
+	]
 };

+ 2 - 1
storage-node/packages/colossus/paths/discover/v0/{id}.js

@@ -21,8 +21,9 @@ module.exports = function (runtime) {
 
 		// Resolve Service Information
 		async get(req, res) {
+			let parsedId;
 			try {
-				var parsedId = parseInt(req.params.id)
+				parsedId = parseInt(req.params.id);
 			} catch (err) {
 				return res.status(400).end()
 			}

+ 2 - 2
storage-node/packages/storage/filter.js

@@ -57,7 +57,7 @@ function mime_matches(acceptable, provided) {
 function mime_matches_any(accept, reject, provided) {
 	// Pass accept
 	let accepted = false
-	for (var item of accept) {
+	for (let item of accept) {
 		if (mime_matches(item, provided)) {
 			debug('Content type matches', item, 'which is acceptable.')
 			accepted = true
@@ -69,7 +69,7 @@ function mime_matches_any(accept, reject, provided) {
 	}
 
 	// Don't pass reject
-	for (var item of reject) {
+	for (let item of reject) {
 		if (mime_matches(item, provided)) {
 			debug('Content type matches', item, 'which is unacceptable.')
 			return false

+ 2 - 2
storage-node/packages/util/fs/resolve.js

@@ -36,7 +36,7 @@ function resolve(base, name) {
 
 	// In a firs step, we strip leading slashes from the name, because they're
 	// just saying "relative to the base" in our use case.
-	var res = name.replace(/^\/+/, '')
+	let res = name.replace(/^\/+/, '');
 	debug('Stripped', res)
 
 	// At this point resolving the path should stay within the base we specify.
@@ -57,7 +57,7 @@ function resolve(base, name) {
 	debug('Relative', res)
 
 	// Finally we can join this relative name to the requested base.
-	var res = path.join(base, res)
+	res = path.join(base, res);
 	debug('Result', res)
 	return res
 }

+ 18 - 18
storage-node/packages/util/ranges.js

@@ -50,38 +50,38 @@ function _parse_range(range) {
 
 /*
  * Parse a range header value, e.g. unit=ranges, where ranges
- * are a comman separated list of individual ranges, and unit is any
+ * are a comma separated list of individual ranges, and unit is any
  * custom unit string. If the unit (and equal sign) are not given, assume
  * 'bytes'.
  */
-function parse(range_str) {
+function parse(rangeStr) {
 	const res = {}
-	debug('Parse range header value:', range_str)
-	const matches = range_str.match(/^(([^\s]+)=)?((?:(?:\d+-\d+|-\d+|\d+-),?)+)$/u)
+	debug('Parse range header value:', rangeStr)
+	const matches = rangeStr.match(/^(([^\s]+)=)?((?:(?:\d+-\d+|-\d+|\d+-),?)+)$/u)
 	if (!matches) {
-		throw new Error(`Not a valid range header: ${range_str}`)
+		throw new Error(`Not a valid range header: ${rangeStr}`)
 	}
 
 	res.unit = matches[2] || 'bytes'
-	res.range_str = matches[3]
+	res.rangeStr = matches[3]
 	res.ranges = []
 
 	// Parse individual ranges
 	const ranges = []
-	res.range_str.split(',').forEach((range) => {
+	res.rangeStr.split(',').forEach((range) => {
 		ranges.push(_parse_range(range))
 	})
 
 	// Merge ranges into result.
-	ranges.forEach((new_range) => {
-		debug('Found range:', new_range)
+	ranges.forEach((newRange) => {
+		debug('Found range:', newRange)
 
-		let is_merged = false
+		let isMerged = false
 		for (const i in res.ranges) {
 			const old_range = res.ranges[i]
 
 			// Skip if the new range is fully separate from the old range.
-			if (old_range[1] + 1 < new_range[0] || new_range[1] + 1 < old_range[0]) {
+			if (old_range[1] + 1 < newRange[0] || newRange[1] + 1 < old_range[0]) {
 				debug('Range does not overlap with', old_range)
 				continue
 			}
@@ -89,15 +89,15 @@ function parse(range_str) {
 			// If we know they're adjacent or overlapping, we construct the
 			// merged range from the lower start and the higher end of both
 			// ranges.
-			const merged = [Math.min(old_range[0], new_range[0]), Math.max(old_range[1], new_range[1])]
+			const merged = [Math.min(old_range[0], newRange[0]), Math.max(old_range[1], newRange[1])]
 			res.ranges[i] = merged
-			is_merged = true
-			debug('Merged', new_range, 'into', old_range, 'as', merged)
+			isMerged = true
+			debug('Merged', newRange, 'into', old_range, 'as', merged)
 		}
 
-		if (!is_merged) {
+		if (!isMerged) {
 			debug('Non-overlapping range!')
-			res.ranges.push(new_range)
+			res.ranges.push(newRange)
 		}
 	})
 
@@ -117,9 +117,9 @@ function parse(range_str) {
 /*
  * Async version of parse().
  */
-function parseAsync(range_str, cb) {
+function parseAsync(rangeStr, cb) {
 	try {
-		return cb(parse(range_str))
+		return cb(parse(rangeStr))
 	} catch (err) {
 		return cb(null, err)
 	}

+ 0 - 1
storage-node/packages/util/test/fs/resolve.js

@@ -18,7 +18,6 @@
 
 'use strict'
 
-const mocha = require('mocha')
 const expect = require('chai').expect
 const path = require('path')
 

+ 0 - 1
storage-node/packages/util/test/fs/walk.js

@@ -18,7 +18,6 @@
 
 'use strict'
 
-const mocha = require('mocha')
 const expect = require('chai').expect
 const temp = require('temp').track()
 

+ 0 - 1
storage-node/packages/util/test/lru.js

@@ -18,7 +18,6 @@
 
 'use strict'
 
-const mocha = require('mocha')
 const expect = require('chai').expect
 
 const lru = require('@joystream/storage-utils/lru')

+ 3 - 4
storage-node/packages/util/test/pagination.js

@@ -18,9 +18,8 @@
 
 'use strict'
 
-const mocha = require('mocha')
 const expect = require('chai').expect
-const mock_http = require('node-mocks-http')
+const mockHttp = require('node-mocks-http')
 
 const pagination = require('@joystream/storage-utils/pagination')
 
@@ -65,7 +64,7 @@ describe('util/pagination', function () {
 
 	describe('paginate()', function () {
 		it('should add pagination links to a response object', function () {
-			const req = mock_http.createRequest({
+			const req = mockHttp.createRequest({
 				method: 'GET',
 				url: '/foo?limit=10',
 				query: {
@@ -87,7 +86,7 @@ describe('util/pagination', function () {
 		})
 
 		it('should add a last pagination link when requested', function () {
-			const req = mock_http.createRequest({
+			const req = mockHttp.createRequest({
 				method: 'GET',
 				url: '/foo?limit=10&offset=15',
 				query: {

+ 62 - 63
storage-node/packages/util/test/ranges.js

@@ -18,10 +18,9 @@
 
 'use strict'
 
-const mocha = require('mocha')
 const expect = require('chai').expect
-const mock_http = require('node-mocks-http')
-const stream_buffers = require('stream-buffers')
+const mockHttp = require('node-mocks-http')
+const streamBuffers = require('stream-buffers')
 
 const ranges = require('@joystream/storage-utils/ranges')
 
@@ -29,24 +28,24 @@ describe('util/ranges', function () {
 	describe('parse()', function () {
 		it('should parse a full range', function () {
 			// Range with unit
-			var range = ranges.parse('bytes=0-100')
+			let range = ranges.parse('bytes=0-100')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-100')
+			expect(range.rangeStr).to.equal('0-100')
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(100)
 
 			// Range without unit
-			var range = ranges.parse('0-100')
+			range = ranges.parse('0-100')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-100')
+			expect(range.rangeStr).to.equal('0-100')
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(100)
 
 			// Range with custom unit
 			//
-			var range = ranges.parse('foo=0-100')
+			range = ranges.parse('foo=0-100')
 			expect(range.unit).to.equal('foo')
-			expect(range.range_str).to.equal('0-100')
+			expect(range.rangeStr).to.equal('0-100')
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(100)
 		})
@@ -61,7 +60,7 @@ describe('util/ranges', function () {
 		it('should parse a range without end', function () {
 			const range = ranges.parse('0-')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-')
+			expect(range.rangeStr).to.equal('0-')
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.be.undefined
 		})
@@ -69,7 +68,7 @@ describe('util/ranges', function () {
 		it('should parse a range without start', function () {
 			const range = ranges.parse('-100')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('-100')
+			expect(range.rangeStr).to.equal('-100')
 			expect(range.ranges[0][0]).to.be.undefined
 			expect(range.ranges[0][1]).to.equal(100)
 		})
@@ -77,7 +76,7 @@ describe('util/ranges', function () {
 		it('should parse multiple ranges', function () {
 			const range = ranges.parse('0-10,30-40,60-80')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-10,30-40,60-80')
+			expect(range.rangeStr).to.equal('0-10,30-40,60-80')
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(10)
 			expect(range.ranges[1][0]).to.equal(30)
@@ -88,33 +87,33 @@ describe('util/ranges', function () {
 
 		it('should merge overlapping ranges', function () {
 			// Two overlapping ranges
-			var range = ranges.parse('0-20,10-30')
+			let range = ranges.parse('0-20,10-30')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-20,10-30')
+			expect(range.rangeStr).to.equal('0-20,10-30')
 			expect(range.ranges).to.have.lengthOf(1)
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(30)
 
 			// Three overlapping ranges
-			var range = ranges.parse('0-15,10-25,20-30')
+			range = ranges.parse('0-15,10-25,20-30')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-15,10-25,20-30')
+			expect(range.rangeStr).to.equal('0-15,10-25,20-30')
 			expect(range.ranges).to.have.lengthOf(1)
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(30)
 
 			// Three overlapping ranges, reverse order
-			var range = ranges.parse('20-30,10-25,0-15')
+			range = ranges.parse('20-30,10-25,0-15')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('20-30,10-25,0-15')
+			expect(range.rangeStr).to.equal('20-30,10-25,0-15')
 			expect(range.ranges).to.have.lengthOf(1)
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(30)
 
 			// Adjacent ranges
-			var range = ranges.parse('0-10,11-20')
+			range = ranges.parse('0-10,11-20')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('0-10,11-20')
+			expect(range.rangeStr).to.equal('0-10,11-20')
 			expect(range.ranges).to.have.lengthOf(1)
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(20)
@@ -123,7 +122,7 @@ describe('util/ranges', function () {
 		it('should sort ranges', function () {
 			const range = ranges.parse('10-30,0-5')
 			expect(range.unit).to.equal('bytes')
-			expect(range.range_str).to.equal('10-30,0-5')
+			expect(range.rangeStr).to.equal('10-30,0-5')
 			expect(range.ranges).to.have.lengthOf(2)
 			expect(range.ranges[0][0]).to.equal(0)
 			expect(range.ranges[0][1]).to.equal(5)
@@ -134,15 +133,15 @@ describe('util/ranges', function () {
 
 	describe('send()', function () {
 		it('should send full files on request', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({})
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({})
 
 			// End-of-stream callback
 			const opts = {
 				name: 'test.file',
 				type: 'application/test',
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -159,14 +158,14 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 
 		it('should send a range spanning the entire file on request', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({})
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({})
 
 			// End-of-stream callback
 			const opts = {
@@ -176,7 +175,7 @@ describe('util/ranges', function () {
 					ranges: [[0, 12]],
 				},
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -195,14 +194,14 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 
 		it('should send a small range on request', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({})
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({})
 
 			// End-of-stream callback
 			const opts = {
@@ -212,7 +211,7 @@ describe('util/ranges', function () {
 					ranges: [[1, 11]], // Cut off first and last letter
 				},
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -231,14 +230,14 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 
 		it('should send ranges crossing buffer boundaries', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({
 				chunkSize: 3, // Setting a chunk size smaller than the range should
 				// not impact the test.
 			})
@@ -251,7 +250,7 @@ describe('util/ranges', function () {
 					ranges: [[1, 11]], // Cut off first and last letter
 				},
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -270,14 +269,14 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 
 		it('should send multiple ranges', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({})
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({})
 
 			// End-of-stream callback
 			const opts = {
@@ -290,7 +289,7 @@ describe('util/ranges', function () {
 					], // Slice two ranges out
 				},
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -316,14 +315,14 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 
 		it('should deal with ranges without end', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({})
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({})
 
 			// End-of-stream callback
 			const opts = {
@@ -333,7 +332,7 @@ describe('util/ranges', function () {
 					ranges: [[5, undefined]], // Skip the first part, but read until end
 				},
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -351,14 +350,14 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 
 		it('should ignore ranges without start', function (done) {
-			const res = mock_http.createResponse({})
-			const in_stream = new stream_buffers.ReadableStreamBuffer({})
+			const res = mockHttp.createResponse({})
+			const inStream = new streamBuffers.ReadableStreamBuffer({})
 
 			// End-of-stream callback
 			const opts = {
@@ -368,7 +367,7 @@ describe('util/ranges', function () {
 					ranges: [[undefined, 5]], // Only last five
 				},
 			}
-			ranges.send(res, in_stream, opts, function (err) {
+			ranges.send(res, inStream, opts, function (err) {
 				expect(err).to.not.exist
 
 				// HTTP handling
@@ -385,9 +384,9 @@ describe('util/ranges', function () {
 			})
 
 			// Simulate file stream
-			in_stream.emit('open')
-			in_stream.put('Hello, world!')
-			in_stream.stop()
+			inStream.emit('open')
+			inStream.put('Hello, world!')
+			inStream.stop()
 		})
 	})
 })