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

Commit b09ab23

Browse files
authored
Merge pull request #237 from netlify/lint/no-magic-numbers
Enable `no-magic-numbers` ESLint rule
2 parents 86d434a + 5cd829e commit b09ab23

File tree

8 files changed

+63
-21
lines changed

8 files changed

+63
-21
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module.exports = {
88
'max-nested-callbacks': 0,
99
'max-statements': 0,
1010
'no-await-in-loop': 0,
11-
'no-magic-numbers': 0,
1211
'no-param-reassign': 0,
1312
'fp/no-class': 0,
1413
'fp/no-let': 0,

src/deploy/constants.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Local deploy timeout: 20 mins
2+
const DEFAULT_DEPLOY_TIMEOUT = 1.2e6
3+
// Concurrent file hash calls
4+
const DEFAULT_CONCURRENT_HASH = 1e2
5+
// Number of concurrent uploads
6+
const DEFAULT_CONCURRENT_UPLOAD = 5
7+
// Number of files
8+
const DEFAULT_SYNC_LIMIT = 1e2
9+
// Number of times to retry an upload
10+
const DEFAULT_MAX_RETRY = 5
11+
12+
const UPLOAD_RANDOM_FACTOR = 0.5
13+
// 5 seconds
14+
const UPLOAD_INITIAL_DELAY = 5e3
15+
// 1.5 minute
16+
const UPLOAD_MAX_DELAY = 9e4
17+
18+
// 1 second
19+
const DEPLOY_POLL = 1e3
20+
21+
module.exports = {
22+
DEFAULT_DEPLOY_TIMEOUT,
23+
DEFAULT_CONCURRENT_HASH,
24+
DEFAULT_CONCURRENT_UPLOAD,
25+
DEFAULT_SYNC_LIMIT,
26+
DEFAULT_MAX_RETRY,
27+
UPLOAD_RANDOM_FACTOR,
28+
UPLOAD_INITIAL_DELAY,
29+
UPLOAD_MAX_DELAY,
30+
DEPLOY_POLL,
31+
}

src/deploy/hash_files.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const { promisify } = require('util')
33
const walker = require('folder-walker')
44
const pump = promisify(require('pump'))
55

6+
const { DEFAULT_CONCURRENT_HASH } = require('./constants')
67
const { hasherCtor, manifestCollectorCtor, fileFilterCtor, fileNormalizerCtor } = require('./hasher_segments')
78

89
const hashFiles = async (dir, configPath, opts) => {
910
opts = {
10-
concurrentHash: 100,
11+
concurrentHash: DEFAULT_CONCURRENT_HASH,
1112
assetType: 'file',
1213
statusCb: () => {},
1314
...opts,

src/deploy/hash_fns.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ const zipIt = require('@netlify/zip-it-and-ship-it')
55
const fromArray = require('from2-array')
66
const pump = promisify(require('pump'))
77

8+
const { DEFAULT_CONCURRENT_HASH } = require('./constants')
89
const { hasherCtor, manifestCollectorCtor } = require('./hasher_segments')
910

1011
const hashFns = async (dir, opts) => {
1112
opts = {
12-
concurrentHash: 100,
13+
concurrentHash: DEFAULT_CONCURRENT_HASH,
1314
assetType: 'function',
1415
hashAlgorithm: 'sha256',
1516
// tmpDir,

src/deploy/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ const cleanDeep = require('clean-deep')
44
const rimraf = promisify(require('rimraf'))
55
const tempy = require('tempy')
66

7+
const {
8+
DEFAULT_DEPLOY_TIMEOUT,
9+
DEFAULT_CONCURRENT_HASH,
10+
DEFAULT_CONCURRENT_UPLOAD,
11+
DEFAULT_SYNC_LIMIT,
12+
DEFAULT_MAX_RETRY,
13+
} = require('./constants')
714
const hashFiles = require('./hash_files')
815
const hashFns = require('./hash_fns')
916
const uploadFiles = require('./upload_files')
10-
const { waitForDiff } = require('./util')
11-
const { waitForDeploy, getUploadList, defaultFilter } = require('./util')
17+
const { waitForDiff, waitForDeploy, getUploadList, defaultFilter } = require('./util')
1218

1319
const deploySite = async (api, siteId, dir, opts) => {
1420
opts = {
@@ -18,17 +24,12 @@ const deploySite = async (api, siteId, dir, opts) => {
1824
// API calls this the 'title'
1925
message: undefined,
2026
tmpDir: tempy.directory(),
21-
// local deploy timeout: 20 mins
22-
deployTimeout: 1.2e6,
23-
// concurrent file hash calls
24-
concurrentHash: 100,
25-
// Number of concurrent uploads
26-
concurrentUpload: 5,
27+
deployTimeout: DEFAULT_DEPLOY_TIMEOUT,
28+
concurrentHash: DEFAULT_CONCURRENT_HASH,
29+
concurrentUpload: DEFAULT_CONCURRENT_UPLOAD,
2730
filter: defaultFilter,
28-
// number of files
29-
syncFileLimit: 100,
30-
// number of times to retry an upload
31-
maxRetry: 5,
31+
syncFileLimit: DEFAULT_SYNC_LIMIT,
32+
maxRetry: DEFAULT_MAX_RETRY,
3233
statusCb: () => {
3334
/* default to noop */
3435
// statusObj: {

src/deploy/upload_files.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const fs = require('fs')
33
const backoff = require('backoff')
44
const pMap = require('p-map')
55

6+
const { UPLOAD_RANDOM_FACTOR, UPLOAD_INITIAL_DELAY, UPLOAD_MAX_DELAY } = require('./constants')
7+
68
const uploadFiles = async (api, deployId, uploadList, { concurrentUpload, statusCb, maxRetry }) => {
79
if (!concurrentUpload || !statusCb || !maxRetry) throw new Error('Missing required option concurrentUpload')
810
statusCb({
@@ -70,9 +72,9 @@ const retryUpload = (uploadFn, maxRetry) =>
7072
new Promise((resolve, reject) => {
7173
let lastError
7274
const fibonacciBackoff = backoff.fibonacci({
73-
randomisationFactor: 0.5,
74-
initialDelay: 5000,
75-
maxDelay: 90000,
75+
randomisationFactor: UPLOAD_RANDOM_FACTOR,
76+
initialDelay: UPLOAD_INITIAL_DELAY,
77+
maxDelay: UPLOAD_MAX_DELAY,
7678
})
7779

7880
const tryUpload = async () => {

src/deploy/util.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { basename, sep } = require('path')
22

33
const pWaitFor = require('p-wait-for')
44

5+
const { DEPLOY_POLL } = require('./constants')
6+
57
// Default filter when scanning for files
68
const defaultFilter = (filePath) => {
79
if (filePath == null) {
@@ -60,7 +62,7 @@ const waitForDiff = async (api, deployId, siteId, timeout) => {
6062
}
6163

6264
await pWaitFor(loadDeploy, {
63-
interval: 1000,
65+
interval: DEPLOY_POLL,
6466
timeout,
6567
message: 'Timeout while waiting for deploy',
6668
})
@@ -97,7 +99,7 @@ const waitForDeploy = async (api, deployId, siteId, timeout) => {
9799
}
98100

99101
await pWaitFor(loadDeploy, {
100-
interval: 1000,
102+
interval: DEPLOY_POLL,
101103
timeout,
102104
message: 'Timeout while waiting for deploy',
103105
})

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class NetlifyAPI {
6464
}
6565

6666
async getAccessToken(ticket, opts) {
67-
opts = { poll: 1000, timeout: 3.6e6, ...opts }
67+
opts = { poll: DEFAULT_TICKET_POLL, timeout: DEFAULT_TICKET_TIMEOUT, ...opts }
6868

6969
const { id } = ticket
7070

@@ -98,6 +98,11 @@ class NetlifyAPI {
9898
}
9999
}
100100

101+
// 1 second
102+
const DEFAULT_TICKET_POLL = 1e3
103+
// 1 hour
104+
const DEFAULT_TICKET_TIMEOUT = 3.6e6
105+
101106
module.exports = NetlifyAPI
102107

103108
module.exports.methods = getOperations()

0 commit comments

Comments
 (0)