Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Enable several ESLint rules #214

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module.exports = {
'array-callback-return': 0,
complexity: 0,
'consistent-this': 0,
'func-names': 0,
'func-style': 0,
'id-length': 0,
'line-comment-position': 0,
'max-nested-callbacks': 0,
Expand All @@ -17,7 +15,6 @@ module.exports = {
'no-inline-comments': 0,
'no-magic-numbers': 0,
'no-param-reassign': 0,
'no-promise-executor-return': 0,
'no-shadow': 0,
'fp/no-class': 0,
'fp/no-delete': 0,
Expand All @@ -27,7 +24,6 @@ module.exports = {
'fp/no-mutating-methods': 0,
'fp/no-mutation': 0,
'fp/no-this': 0,
'node/exports-style': 0,
'node/global-require': 0,
'node/prefer-global/process': 0,
'promise/no-callback-in-promise': 0,
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const opts = {
All operations are conveniently consumed with async/await:

```js
async function getSomeData() {
const getSomeData = async () => {
// Calls may fail!
try {
return await client.getSiteDeploy({
Expand Down Expand Up @@ -163,7 +163,7 @@ See the [authenticating](https://www.netlify.com/docs/api/#authenticating) docs

```js
// example:
async function login() {
const login = async () => {
const ticket = await api.createTicket({
clientId: CLIENT_ID,
})
Expand Down
8 changes: 4 additions & 4 deletions src/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
const fetch = require('node-fetch')

async function createAddon(settings, netlifyApiToken) {
const createAddon = async (settings, netlifyApiToken) => {
const { siteId, addon, config } = settings
const url = `https://api.netlify.com/api/v1/sites/${siteId}/services/${addon}/instances`
const response = await fetch(url, {
Expand All @@ -26,7 +26,7 @@ async function createAddon(settings, netlifyApiToken) {
return data
}

async function getAddons(siteId, netlifyApiToken) {
const getAddons = async (siteId, netlifyApiToken) => {
const url = `https://api.netlify.com/api/v1/sites/${siteId}/service-instances`
const response = await fetch(url, {
method: 'GET',
Expand All @@ -45,7 +45,7 @@ async function getAddons(siteId, netlifyApiToken) {
return data
}

async function deleteAddon(settings, netlifyApiToken) {
const deleteAddon = async (settings, netlifyApiToken) => {
const { siteId, addon, instanceId } = settings
const url = `https://api.netlify.com/api/v1/sites/${siteId}/services/${addon}/instances/${instanceId}`
const response = await fetch(url, {
Expand All @@ -64,7 +64,7 @@ async function deleteAddon(settings, netlifyApiToken) {
return response
}

async function updateAddon(settings, netlifyApiToken) {
const updateAddon = async (settings, netlifyApiToken) => {
const { siteId, addon, config, instanceId } = settings
const url = `https://api.netlify.com/api/v1/sites/${siteId}/services/${addon}/instances/${instanceId}`

Expand Down
5 changes: 3 additions & 2 deletions src/deploy/hash-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const pump = promisify(require('pump'))

const { hasherCtor, manifestCollectorCtor, fileFilterCtor, fileNormalizerCtor } = require('./hasher-segments')

module.exports = hashFiles
async function hashFiles(dir, configPath, opts) {
const hashFiles = async (dir, configPath, opts) => {
opts = {
concurrentHash: 100,
assetType: 'file',
Expand All @@ -29,3 +28,5 @@ async function hashFiles(dir, configPath, opts) {

return { files, filesShaMap }
}

module.exports = hashFiles
5 changes: 3 additions & 2 deletions src/deploy/hash-fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const pump = promisify(require('pump'))

const { hasherCtor, manifestCollectorCtor } = require('./hasher-segments')

module.exports = hashFns
async function hashFns(dir, opts) {
const hashFns = async (dir, opts) => {
opts = {
concurrentHash: 100,
assetType: 'function',
Expand Down Expand Up @@ -48,3 +47,5 @@ async function hashFns(dir, opts) {

return { functions, fnShaMap }
}

module.exports = hashFns
19 changes: 12 additions & 7 deletions src/deploy/hasher-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const map = require('through2-map').obj
const { normalizePath } = require('./util')

// a parallel transform stream segment ctor that hashes fileObj's created by folder-walker
exports.hasherCtor = ({ concurrentHash, hashAlgorithm = 'sha1' }) => {
const hasherCtor = ({ concurrentHash, hashAlgorithm = 'sha1' }) => {
const hashaOpts = { algorithm: hashAlgorithm }
if (!concurrentHash) throw new Error('Missing required opts')
return transform(concurrentHash, { objectMode: true }, (fileObj, cb) => {
Expand All @@ -20,13 +20,11 @@ exports.hasherCtor = ({ concurrentHash, hashAlgorithm = 'sha1' }) => {
}

// Inject normalized file names into normalizedPath and assetType
exports.fileNormalizerCtor = fileNormalizerCtor
function fileNormalizerCtor({ assetType = 'file' }) {
return map((fileObj) => ({ ...fileObj, assetType, normalizedPath: normalizePath(fileObj.relname) }))
}
const fileNormalizerCtor = ({ assetType = 'file' }) =>
map((fileObj) => ({ ...fileObj, assetType, normalizedPath: normalizePath(fileObj.relname) }))

// A writable stream segment ctor that normalizes file paths, and writes shaMap's
exports.manifestCollectorCtor = (filesObj, shaMap, { statusCb, assetType }) => {
const manifestCollectorCtor = (filesObj, shaMap, { statusCb, assetType }) => {
if (!statusCb || !assetType) throw new Error('Missing required options')
return objWriter((fileObj, _, cb) => {
filesObj[fileObj.normalizedPath] = fileObj.hash
Expand All @@ -49,4 +47,11 @@ exports.manifestCollectorCtor = (filesObj, shaMap, { statusCb, assetType }) => {
}

// transform stream ctor that filters folder-walker results for only files
exports.fileFilterCtor = objFilterCtor((fileObj) => fileObj.type === 'file')
const fileFilterCtor = objFilterCtor((fileObj) => fileObj.type === 'file')

module.exports = {
hasherCtor,
fileNormalizerCtor,
manifestCollectorCtor,
fileFilterCtor,
}
6 changes: 3 additions & 3 deletions src/deploy/index.browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = browserDeploy

function browserDeploy() {
const browserDeploy = function () {
throw new Error('Deploys not supported in the browser')
}

module.exports = browserDeploy
4 changes: 3 additions & 1 deletion src/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const uploadFiles = require('./upload-files')
const { waitForDiff } = require('./util')
const { waitForDeploy, getUploadList, defaultFilter } = require('./util')

module.exports = async (api, siteId, dir, opts) => {
const deploySite = async (api, siteId, dir, opts) => {
opts = {
fnDir: null,
configPath: null,
Expand Down Expand Up @@ -128,3 +128,5 @@ module.exports = async (api, siteId, dir, opts) => {
}
return deployManifest
}

module.exports = deploySite
37 changes: 19 additions & 18 deletions src/deploy/upload-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const fs = require('fs')
const backoff = require('backoff')
const pMap = require('p-map')

module.exports = uploadFiles
async function uploadFiles(api, deployId, uploadList, { concurrentUpload, statusCb, maxRetry }) {
const uploadFiles = async (api, deployId, uploadList, { concurrentUpload, statusCb, maxRetry }) => {
if (!concurrentUpload || !statusCb || !maxRetry) throw new Error('Missing required option concurrentUpload')
statusCb({
type: 'upload',
Expand Down Expand Up @@ -67,28 +66,16 @@ async function uploadFiles(api, deployId, uploadList, { concurrentUpload, status
return results
}

function retryUpload(uploadFn, maxRetry) {
return new Promise((resolve, reject) => {
const retryUpload = (uploadFn, maxRetry) =>
new Promise((resolve, reject) => {
let lastError
const fibonacciBackoff = backoff.fibonacci({
randomisationFactor: 0.5,
initialDelay: 5000,
maxDelay: 90000,
})
fibonacciBackoff.failAfter(maxRetry)

fibonacciBackoff.on('backoff', () => {
// Do something when backoff starts, e.g. show to the
// user the delay before next reconnection attempt.
})

fibonacciBackoff.on('ready', tryUpload)

fibonacciBackoff.on('fail', () => {
reject(lastError)
})

function tryUpload() {
const tryUpload = () => {
uploadFn()
.then((results) => resolve(results))
.catch((error) => {
Expand All @@ -105,6 +92,20 @@ function retryUpload(uploadFn, maxRetry) {
})
}

fibonacciBackoff.failAfter(maxRetry)

fibonacciBackoff.on('backoff', () => {
// Do something when backoff starts, e.g. show to the
// user the delay before next reconnection attempt.
})

fibonacciBackoff.on('ready', tryUpload)

fibonacciBackoff.on('fail', () => {
reject(lastError)
})

tryUpload(0, 0)
})
}

module.exports = { uploadFiles }
48 changes: 26 additions & 22 deletions src/deploy/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const flatten = require('lodash.flatten')
const pWaitFor = require('p-wait-for')

// Default filter when scanning for files
exports.defaultFilter = (filename) => {
const defaultFilter = (filename) => {
if (filename == null) return false
const n = path.basename(filename)
switch (true) {
Expand All @@ -18,7 +18,7 @@ exports.defaultFilter = (filename) => {
}

// normalize windows paths to unix paths
exports.normalizePath = (relname) => {
const normalizePath = (relname) => {
if (relname.includes('#') || relname.includes('?')) {
throw new Error(`Invalid filename ${relname}. Deployed filenames cannot contain # or ? characters`)
}
Expand All @@ -30,20 +30,11 @@ exports.normalizePath = (relname) => {
)
}

exports.waitForDiff = waitForDiff
// poll an async deployId until its done diffing
async function waitForDiff(api, deployId, siteId, timeout) {
const waitForDiff = async (api, deployId, siteId, timeout) => {
let deploy // capture ready deploy during poll

await pWaitFor(loadDeploy, {
interval: 1000,
timeout,
message: 'Timeout while waiting for deploy',
})

return deploy

async function loadDeploy() {
const loadDeploy = async () => {
const d = await api.getSiteDeploy({ siteId, deployId })

switch (d.state) {
Expand All @@ -66,12 +57,6 @@ async function waitForDiff(api, deployId, siteId, timeout) {
}
}
}
}

// Poll a deployId until its ready
exports.waitForDeploy = waitForDeploy
async function waitForDeploy(api, deployId, siteId, timeout) {
let deploy // capture ready deploy during poll

await pWaitFor(loadDeploy, {
interval: 1000,
Expand All @@ -80,8 +65,13 @@ async function waitForDeploy(api, deployId, siteId, timeout) {
})

return deploy
}

async function loadDeploy() {
// Poll a deployId until its ready
const waitForDeploy = async (api, deployId, siteId, timeout) => {
let deploy // capture ready deploy during poll

const loadDeploy = async () => {
const d = await api.getSiteDeploy({ siteId, deployId })
switch (d.state) {
// https://github.com/netlify/bitballoon/blob/master/app/models/deploy.rb#L21-L33
Expand All @@ -103,12 +93,26 @@ async function waitForDeploy(api, deployId, siteId, timeout) {
}
}
}

await pWaitFor(loadDeploy, {
interval: 1000,
timeout,
message: 'Timeout while waiting for deploy',
})

return deploy
}

// Transform the fileShaMap and fnShaMap into a generic shaMap that file-uploader.js can use
exports.getUploadList = function (required, shaMap) {
const getUploadList = (required, shaMap) => {
if (!required || !shaMap) return []
return flatten(required.map((sha) => shaMap[sha]))
}

exports.retry = async () => {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was dead code, not used anywhere.

module.exports = {
defaultFilter,
normalizePath,
waitForDiff,
waitForDeploy,
getUploadList,
}
4 changes: 3 additions & 1 deletion src/methods/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const getDelay = function (response) {
}

const sleep = function (ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

const DEFAULT_RETRY_DELAY = 5e3
Expand Down