Skip to content

Commit

Permalink
Update code to new xo conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Aug 31, 2023
1 parent f71b36f commit 6a679df
Show file tree
Hide file tree
Showing 33 changed files with 76 additions and 97 deletions.
2 changes: 1 addition & 1 deletion __tests__/__helpers__/rm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const fs = require('fs')
const fs = require('node:fs')

module.exports = path => fs.promises.rm(path, {force: true, recursive: true})
4 changes: 2 additions & 2 deletions __tests__/__helpers__/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const http = require('http')
const path = require('path')
const http = require('node:http')
const path = require('node:path')
const listen = require('test-listen')
const serveStatic = require('serve-static')
const finalhandler = require('finalhandler')
Expand Down
3 changes: 1 addition & 2 deletions __tests__/analyze.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path')
const path = require('node:path')
const analyze = require('../lib/analyze')
const options = require('../lib/options')

const {serveFile} = require('./__helpers__/server')

describe('analyze', () => {
Expand Down
5 changes: 2 additions & 3 deletions __tests__/http/analyzers/atom.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const path = require('path')
const path = require('node:path')
const analyzeAtom = require('../../../lib/http/analyzers/atom')
const fetch = require('../../../lib/http/fetch')

const {serveFile} = require('../../__helpers__/server')

const options = {
userAgent: 'plunger/test',
logger: {
log: () => {}
log() {}
},
timeout: {}
}
Expand Down
5 changes: 2 additions & 3 deletions __tests__/http/analyzers/index-of.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path')
const path = require('node:path')
const analyzeIndexOf = require('../../../lib/http/analyzers/index-of')
const fetch = require('../../../lib/http/fetch')

const {serveFile} = require('../../__helpers__/server')

const options = {
Expand All @@ -10,7 +9,7 @@ const options = {
/Index of/
],
logger: {
log: () => {}
log() {}
},
timeout: {}
}
Expand Down
5 changes: 2 additions & 3 deletions __tests__/http/fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path')
const url = require('url')
const path = require('node:path')
const url = require('node:url')
const fetch = require('../../lib/http/fetch')

const {serveFile, serveEmpty, serveRedirect} = require('../__helpers__/server')

const options = {
Expand Down
9 changes: 4 additions & 5 deletions __tests__/http/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path')
const path = require('node:path')
const analyzeUrl = require('../../lib/http')

const {serveFile} = require('../__helpers__/server')
const rm = require('../__helpers__/rm')

Expand All @@ -11,7 +10,7 @@ const options = {
/Index of/
],
logger: {
log: () => {}
log() {}
},
timeout: {},
maxDownloadSize: 100 * 1024 * 1024
Expand Down Expand Up @@ -71,7 +70,7 @@ describe('http', () => {
const token = {url}

await analyzeUrl(token, {...options, cache: {
getUrlCache: token => {
getUrlCache(token) {
token.hacked = true
}
}})
Expand All @@ -87,7 +86,7 @@ describe('http', () => {
const token = {url}

await analyzeUrl(token, {...options, cache: {
setUrlCache: token => {
setUrlCache(token) {
token.hacked = true
}
}})
Expand Down
4 changes: 2 additions & 2 deletions __tests__/path/analyzers/archive.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require('path')
const path = require('node:path')
const analyzeArchive = require('../../../lib/path/analyzers/archive')
const {createTempDirectory} = require('../../../lib/util/tmpdir')
const rm = require('../../__helpers__/rm')

const options = {
logger: {
log: () => {}
log() {}
},
extractArchives: true
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/path/analyzers/directory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('node:path')
const analyzeDirectory = require('../../../lib/path/analyzers/directory')

describe('path.analyzers.directory', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/path/analyzers/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('node:path')
const analyzeTypes = require('../../../lib/path/analyzers/types')

describe('path.analyzers.file', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/path/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require('path')
const path = require('node:path')
const analyzePath = require('../../lib/path')
const rm = require('../__helpers__/rm')

const options = {
digestAlgorithm: 'md5',
logger: {
log: () => {}
log() {}
},
extractArchives: true
}
Expand Down
21 changes: 8 additions & 13 deletions __tests__/util/archive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('node:path')
const {isArchive, unarchive} = require('../../lib/util/archive')
const {createTempDirectory} = require('../../lib/util/tmpdir')
const rm = require('../__helpers__/rm')
Expand All @@ -17,8 +17,7 @@ describe('util.archive', () => {
})

it('should return false if there are no valid types', () => {
const types = [{
}]
const types = [{}]

expect(isArchive(types)).toBeFalsy()
})
Expand All @@ -31,17 +30,13 @@ describe('util.archive', () => {
expect(isArchive(types)).toBeTruthy()
})

it('should error when trying to unarchive a file that does not exist', () => {
return expect(
unarchive(path.resolve(__dirname, '../__fixtures__/doesnt-exist'))
).rejects.toThrow('Could not extract archive')
})
it('should error when trying to unarchive a file that does not exist', () => expect(
unarchive(path.resolve(__dirname, '../__fixtures__/doesnt-exist'))
).rejects.toThrow('Could not extract archive'))

it('should error when trying to unarchive an invalid archive', () => {
return expect(
unarchive(path.resolve(__dirname, '../__fixtures__/file.txt'))
).rejects.toThrow('Could not extract archive')
})
it('should error when trying to unarchive an invalid archive', () => expect(
unarchive(path.resolve(__dirname, '../__fixtures__/file.txt'))
).rejects.toThrow('Could not extract archive'))

it('should extract an archive and return the unarchived path', async () => {
const tmp = await unarchive(path.resolve(__dirname, '../__fixtures__/file.zip'))
Expand Down
4 changes: 2 additions & 2 deletions __tests__/util/tmpdir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const os = require('os')
const path = require('path')
const os = require('node:os')
const path = require('node:path')
const tmpdir = require('../../lib/util/tmpdir')
const rm = require('../__helpers__/rm')

Expand Down
1 change: 0 additions & 1 deletion lib/analyze.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const bluebird = require('bluebird')

const analyzeHttp = require('./http')
const analyzePath = require('./path')

Expand Down
2 changes: 1 addition & 1 deletion lib/http/analyzers/atom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const {is} = require('type-is')
const {pipe} = require('mississippi')
const FeedParser = require('feedparser')

const bufferize = require('../bufferize')

function extractItems(token) {
Expand All @@ -15,6 +14,7 @@ function extractItems(token) {
if (error.message === 'Not a feed') {
return resolve({isAtom: false})
}

return reject(error)
}

Expand Down
1 change: 1 addition & 0 deletions lib/http/analyzers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function analyzeBody(token, options) {
token.warning = error.message
token.analyzed = true
}

options.logger.log('file:download:end', token)

// After downloading the file, the token will be re-analyzed by the fs
Expand Down
11 changes: 5 additions & 6 deletions lib/http/analyzers/index-of.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

const url = require('url')
const url = require('node:url')
const {is} = require('type-is')
const {pipe} = require('mississippi')
const htmlparser = require('htmlparser2')
const normalize = require('normalize-url')

const bufferize = require('../bufferize')

function stripQuery(location) {
const parsed = new url.URL(location)
const parsed = new URL(location)
parsed.search = ''

try {
Expand All @@ -18,7 +17,7 @@ function stripQuery(location) {
removeTrailingSlash: false,
stripHash: true
})
} catch (error) {
} catch {
// Url could not be normalized, return original URL.
// See https://github.com/sindresorhus/normalize-url/issues/24
return parsed.toString()
Expand All @@ -32,15 +31,15 @@ function extractLinks(token, options) {
let baseUrl = stripQuery(token.finalUrl)

const parser = new htmlparser.WritableStream({
ontext: text => {
ontext(text) {
const matches = options.indexOfMatches || []

if (matches.some(match => text.trim().match(match))) {
isIndexOf = true
}
},

onopentag: (tag, attributes) => {
onopentag(tag, attributes) {
if (tag === 'base' && attributes.href) {
baseUrl = stripQuery(url.resolve(baseUrl, attributes.href))
}
Expand Down
2 changes: 1 addition & 1 deletion lib/http/analyzers/name.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const url = require('url')
const url = require('node:url')
const contentDisposition = require('content-disposition')
const rfc2047 = require('rfc2047')

Expand Down
6 changes: 3 additions & 3 deletions lib/http/analyzers/types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const path = require('path')
const path = require('node:path')
const contentType = require('content-type')
const mime = require('mime-types')

Expand All @@ -19,14 +19,14 @@ function analyzeType(token) {
mime: ct.type,
source: 'http:content-type'
})
} catch (error) {
} catch {
// The Content-Type header was invalid, this is no big deal
// We could log it to tell people that their headers are not valid
}

// Finally, we try to extract the extension from the filename
if (token.fileName) {
const ext = path.extname(token.fileName).substring(1)
const ext = path.extname(token.fileName).slice(1)

if (ext) {
types.push({
Expand Down
1 change: 1 addition & 0 deletions lib/http/bufferize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const {Buffer} = require('node:buffer')

async function bufferize(token, func) {
const chunks = []
Expand Down
7 changes: 3 additions & 4 deletions lib/http/download.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const fs = require('fs')
const path = require('path')
const {promisify} = require('util')
const fs = require('node:fs')
const path = require('node:path')
const {promisify} = require('node:util')
const {pipe} = require('mississippi')
const hasha = require('hasha')

const {bytesLimit, computeDigest} = require('../util/streams')

const writeFile = promisify(fs.writeFile)
Expand Down
16 changes: 7 additions & 9 deletions lib/http/fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const http = require('http')
const https = require('https')
const http = require('node:http')
const https = require('node:https')
const {hasBody} = require('type-is')
const got = require('got')

Expand Down Expand Up @@ -70,11 +70,7 @@ async function fetch(token, options) {
}

if (options.lastModified) {
if (options.lastModified instanceof Date && isFinite(options.lastModified)) {
headers['If-Modified-Since'] = options.lastModified.toUTCString()
} else {
headers['If-Modified-Since'] = options.lastModified
}
headers['If-Modified-Since'] = options.lastModified instanceof Date && isFinite(options.lastModified) ? options.lastModified.toUTCString() : options.lastModified
}

if (options.etag) {
Expand Down Expand Up @@ -102,18 +98,20 @@ async function fetch(token, options) {
}

switch (response.statusCode) {
case 304:
case 304: {
token.unchanged = true
token.analyzed = true
break
}

default:
default: {
if (hasBody(response)) {
token.response = response
return
}

throw new Error('There was no body in the response')
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions lib/http/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

const fetch = require('./fetch')

const analyzeName = require('./analyzers/name')
const analyzeTypes = require('./analyzers/types')

const analyzeIndexOf = require('./analyzers/index-of')
const analyzeAtom = require('./analyzers/atom')

const analyzeFile = require('./analyzers/file')

async function analyzeHttp(token, options) {
Expand Down Expand Up @@ -41,8 +38,6 @@ async function analyzeHttp(token, options) {
await options.cache.setUrlCache(token, options)
}
}
} catch (error) {
throw error
} finally {
delete token.response
delete token.buffer
Expand Down
5 changes: 2 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const path = require('path')
const url = require('url')

const path = require('node:path')
const url = require('node:url')
const analyze = require('./analyze')
const defaultOptions = require('./options')

Expand Down

0 comments on commit 6a679df

Please sign in to comment.