Browse Source

storage-node: fix linter warnings

Shamil Gadelshin 4 years ago
parent
commit
6695f78199

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

@@ -17,11 +17,12 @@ module.exports = {
 		"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"
+		"import/no-extraneous-dependencies": "off",
+		"import/no-nodejs-modules": "off" // nodejs project
 	},
 	"overrides": [
 		{
-			"files": ["**/test/ranges.js", ],
+			"files": ["**/test/ranges.js", "**/test/lru.js", "**/test/fs/walk.js"],
 			"rules": {
 				// Disabling Rules because of used chai lib:
 				// https://stackoverflow.com/questions/45079454/no-unused-expressions-in-mocha-chai-unit-test-using-standardjs

+ 4 - 4
storage-node/packages/cli/bin/cli.js

@@ -152,9 +152,9 @@ const commands = {
 	// needs to be updated to take a content id and resolve it a potential set
 	// of providers that has it, and select one (possibly try more than one provider)
 	// to fetch it from the get api url of a provider..
-	download: async (api, url, content_id, filename) => {
+	download: async (api, url, contentId, filename) => {
 		const request = require('request')
-		url = `${url}asset/v0/${content_id}`
+		url = `${url}asset/v0/${contentId}`
 		debug('Downloading URL', chalk.green(url), 'to', chalk.green(filename))
 
 		const f = fs.createWriteStream(filename)
@@ -194,9 +194,9 @@ const commands = {
 		})
 	},
 	// similar to 'download' function
-	head: async (api, url, content_id) => {
+	head: async (api, url, contentId) => {
 		const request = require('request')
-		url = `${url}asset/v0/${content_id}`
+		url = `${url}asset/v0/${contentId}`
 		debug('Checking URL', chalk.green(url), '...')
 
 		const opts = {

+ 2 - 2
storage-node/packages/colossus/bin/cli.js

@@ -122,9 +122,9 @@ function get_storage(runtime_api) {
 	const { Storage } = require('@joystream/storage-node-backend')
 
 	const options = {
-		resolve_content_id: async (content_id) => {
+		resolve_content_id: async (contentId) => {
 			// Resolve via API
-			const obj = await runtime_api.assets.getDataObject(content_id)
+			const obj = await runtime_api.assets.getDataObject(contentId)
 			if (!obj || obj.isNone) {
 				return
 			}

+ 10 - 10
storage-node/packages/colossus/lib/sync.js

@@ -30,8 +30,8 @@ async function sync_callback(api, storage) {
 	const providerId = api.storageProviderId
 
 	// Iterate over all sync objects, and ensure they're synced.
-	const allChecks = knownContentIds.map(async (content_id) => {
-		let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, content_id)
+	const allChecks = knownContentIds.map(async (contentId) => {
+		let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, contentId)
 
 		// get the data object
 		// make sure the data object was Accepted by the liaison,
@@ -40,7 +40,7 @@ async function sync_callback(api, storage) {
 		let fileLocal
 		try {
 			// check if we have content or not
-			const stats = await storage.stat(content_id)
+			const stats = await storage.stat(contentId)
 			fileLocal = stats.local
 		} catch (err) {
 			// on error stating or timeout
@@ -51,7 +51,7 @@ async function sync_callback(api, storage) {
 
 		if (!fileLocal) {
 			try {
-				await storage.synchronize(content_id)
+				await storage.synchronize(contentId)
 			} catch (err) {
 				// duplicate logging
 				// debug(err.message)
@@ -63,25 +63,25 @@ async function sync_callback(api, storage) {
 
 		if (!relationship) {
 			// create relationship
-			debug(`Creating new storage relationship for ${content_id.encode()}`)
+			debug(`Creating new storage relationship for ${contentId.encode()}`)
 			try {
-				relationshipId = await api.assets.createAndReturnStorageRelationship(role_addr, providerId, content_id)
+				relationshipId = await api.assets.createAndReturnStorageRelationship(role_addr, providerId, contentId)
 				await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true)
 			} catch (err) {
-				debug(`Error creating new storage relationship ${content_id.encode()}: ${err.stack}`)
+				debug(`Error creating new storage relationship ${contentId.encode()}: ${err.stack}`)
 				return
 			}
 		} else if (!relationship.ready) {
-			debug(`Updating storage relationship to ready for ${content_id.encode()}`)
+			debug(`Updating storage relationship to ready for ${contentId.encode()}`)
 			// update to ready. (Why would there be a relationship set to ready: false?)
 			try {
 				await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true)
 			} catch (err) {
-				debug(`Error setting relationship ready ${content_id.encode()}: ${err.stack}`)
+				debug(`Error setting relationship ready ${contentId.encode()}: ${err.stack}`)
 			}
 		} else {
 			// we already have content and a ready relationship set. No need to do anything
-			// debug(`content already stored locally ${content_id.encode()}`);
+			// debug(`content already stored locally ${contentId.encode()}`);
 		}
 	})
 

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

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

+ 0 - 2
storage-node/packages/runtime-api/identities.js

@@ -20,8 +20,6 @@
 
 const path = require('path')
 const fs = require('fs')
-// const readline = require('readline')
-
 const debug = require('debug')('joystream:runtime:identities')
 const { Keyring } = require('@polkadot/keyring')
 const util_crypto = require('@polkadot/util-crypto')

+ 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 (let item of accept) {
+	for (const 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 (let item of reject) {
+	for (const item of reject) {
 		if (mime_matches(item, provided)) {
 			debug('Content type matches', item, 'which is unacceptable.')
 			return false

+ 15 - 15
storage-node/packages/storage/storage.js

@@ -244,10 +244,10 @@ class Storage {
 	/*
 	 * Resolve content ID with timeout.
 	 */
-	async _resolve_content_id_with_timeout(timeout, content_id) {
+	async _resolve_content_id_with_timeout(timeout, contentId) {
 		return await this._with_specified_timeout(timeout, async (resolve, reject) => {
 			try {
-				resolve(await this._resolve_content_id(content_id))
+				resolve(await this._resolve_content_id(contentId))
 			} catch (err) {
 				reject(err)
 			}
@@ -257,8 +257,8 @@ class Storage {
 	/*
 	 * Stat a content ID.
 	 */
-	async stat(content_id, timeout) {
-		const resolved = await this._resolve_content_id_with_timeout(timeout, content_id)
+	async stat(contentId, timeout) {
+		const resolved = await this._resolve_content_id_with_timeout(timeout, contentId)
 
 		return await this._with_specified_timeout(timeout, (resolve, reject) => {
 			this.ipfs.files.stat(`/ipfs/${resolved}`, { withLocal: true }, (err, res) => {
@@ -274,8 +274,8 @@ class Storage {
 	/*
 	 * Return the size of a content ID.
 	 */
-	async size(content_id, timeout) {
-		const stat = await this.stat(content_id, timeout)
+	async size(contentId, timeout) {
+		const stat = await this.stat(contentId, timeout)
 		return stat.size
 	}
 
@@ -301,21 +301,21 @@ class Storage {
 	 * an explicit `cleanup()` function that removes temporary files as well,
 	 * in case comitting is not desired.
 	 */
-	async open(content_id, mode, timeout) {
+	async open(contentId, mode, timeout) {
 		if (mode != 'r' && mode != 'w') {
 			throw Error('The only supported modes are "r", "w" and "a".')
 		}
 
 		// Write stream
 		if (mode === 'w') {
-			return await this._create_write_stream(content_id, timeout)
+			return await this._create_write_stream(contentId, timeout)
 		}
 
 		// Read stream - with file type detection
-		return await this._create_read_stream(content_id, timeout)
+		return await this._create_read_stream(contentId, timeout)
 	}
 
-	async _create_write_stream(content_id) {
+	async _create_write_stream(contentId) {
 		// IPFS wants us to just dump a stream into its storage, then returns a
 		// content ID (of its own).
 		// We need to instead return a stream immediately, that we eventually
@@ -326,8 +326,8 @@ class Storage {
 		})
 	}
 
-	async _create_read_stream(content_id, timeout) {
-		const resolved = await this._resolve_content_id_with_timeout(timeout, content_id)
+	async _create_read_stream(contentId, timeout) {
+		const resolved = await this._resolve_content_id_with_timeout(timeout, contentId)
 
 		let found = false
 		return await this._with_specified_timeout(timeout, (resolve, reject) => {
@@ -347,7 +347,7 @@ class Storage {
 			})
 			ls.on('end', () => {
 				if (!found) {
-					const err = new Error('No matching content found for', content_id)
+					const err = new Error('No matching content found for', contentId)
 					debug(err)
 					reject(err)
 				}
@@ -359,8 +359,8 @@ class Storage {
 	/*
 	 * Synchronize the given content ID
 	 */
-	async synchronize(content_id) {
-		const resolved = await this._resolve_content_id_with_timeout(this._timeout, content_id)
+	async synchronize(contentId) {
+		const resolved = await this._resolve_content_id_with_timeout(this._timeout, contentId)
 
 		// validate resolved id is proper ipfs_cid, not null or empty string
 

+ 4 - 4
storage-node/packages/storage/test/storage.js

@@ -31,9 +31,9 @@ const { Storage } = require('@joystream/storage-node-backend')
 
 const IPFS_CID_REGEX = /^Qm[1-9A-HJ-NP-Za-km-z]{44}$/
 
-function write(store, content_id, contents, callback) {
+function write(store, contentId, contents, callback) {
 	store
-		.open(content_id, 'w')
+		.open(contentId, 'w')
 		.then((stream) => {
 			stream.on('finish', () => {
 				stream.commit()
@@ -61,7 +61,7 @@ function read_all(stream) {
 	})
 }
 
-function create_known_object(content_id, contents, callback) {
+function create_known_object(contentId, contents, callback) {
 	let hash
 	const store = Storage.create({
 		resolve_content_id: () => {
@@ -69,7 +69,7 @@ function create_known_object(content_id, contents, callback) {
 		},
 	})
 
-	write(store, content_id, contents, (the_hash) => {
+	write(store, contentId, contents, (the_hash) => {
 		hash = the_hash
 
 		callback(store, hash)

+ 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.
-	let 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.
-	res = path.join(base, res);
+	res = path.join(base, res)
 	debug('Result', res)
 	return res
 }

+ 3 - 4
storage-node/packages/util/stripEndingSlash.js

@@ -1,10 +1,9 @@
 // return url with last `/` removed
 function removeEndingForwardSlash(url) {
-	const st = new String(url)
-	if (st.endsWith('/')) {
-		return st.substring(0, st.length - 1)
+	if (url.endsWith('/')) {
+		return url.substring(0, url.length - 1)
 	}
-	return st.toString()
+	return url.toString()
 }
 
 module.exports = removeEndingForwardSlash

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

@@ -19,6 +19,8 @@
 'use strict'
 
 const expect = require('chai').expect
+// Disabling the rule because of the 'temp' package API.
+// eslint-disable-next-line no-unused-vars
 const temp = require('temp').track()
 
 const fs = require('fs')

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

@@ -138,7 +138,7 @@ describe('util/lru', function () {
 
 			// 'foo' is older than 'bar' right now, so should be pruned first. But
 			// if we get 'foo', it would be 'bar' that has to go.
-			const _ = cache.get('foo')
+			cache.get('foo')
 
 			// Makes debugging a bit more obvious
 			await sleep()

+ 13 - 0
storage-node/packages/util/test/stripEndingSlash.js

@@ -0,0 +1,13 @@
+'use strict'
+
+const expect = require('chai').expect
+const stripEndingSlash = require('@joystream/storage-utils/stripEndingSlash')
+
+describe('stripEndingSlash', function () {
+	it('stripEndingSlash should keep URL without the slash', function () {
+		expect(stripEndingSlash('http://keep.one')).to.equal('http://keep.one')
+	})
+	it('stripEndingSlash should remove ending slash', function () {
+		expect(stripEndingSlash('http://strip.one/')).to.equal('http://strip.one')
+	})
+})