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

Commit 7cdd0c8

Browse files
committed
Fix unix time handling
1 parent 4a4498b commit 7cdd0c8

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/index.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const NetlifyAPI = require('./index')
55
const body = promisify(require('body'))
66
const fromString = require('from2-string')
77
const { TextHTTPError } = require('micro-api-client')
8-
const { existy } = require('./open-api/util')
8+
const { existy, unixNow } = require('./open-api/util')
99

1010
const createServer = handler => {
1111
const s = http.createServer(handler)
@@ -252,11 +252,12 @@ test.serial('test rate-limiting', async t => {
252252
try {
253253
server = createServer(async (req, res) => {
254254
if (!existy(retryAt)) {
255-
retryAt = Date.now() + randomInt(1000, 5000) //ms
255+
retryAt = unixNow() + randomInt(1, 5) //ms
256256

257257
requestRateLimit(res, retryAt)
258258
} else {
259-
const rateLimitFinished = Date.now() >= retryAt
259+
const now = unixNow()
260+
const rateLimitFinished = now >= retryAt
260261

261262
if (rateLimitFinished) {
262263
t.pass('The client made a request at or after the rate limit deadline')

src/open-api/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Headers = fetch.Headers
77
const camelCase = require('lodash.camelcase')
88
const { JSONHTTPError, TextHTTPError } = require('micro-api-client')
99
const debug = require('debug')('netlify:open-api')
10-
const { existy, sleep } = require('./util')
10+
const { existy, sleep, unixNow } = require('./util')
1111

1212
exports.methods = require('./shape-swagger')
1313

@@ -132,7 +132,9 @@ exports.generateMethod = method => {
132132
throw new Error('Header missing reset time')
133133
}
134134
debug(`resetTime: ${resetTime}`)
135-
const sleepTime = resetTime - Date.now()
135+
const now = unixNow()
136+
debug(`unixNow(): ${now}`)
137+
const sleepTime = (resetTime - now < 0 ? 0 : resetTime - now) * 1000
136138
debug(`sleeping for ${sleepTime}ms`)
137139
await sleep(sleepTime)
138140
} catch (e) {

src/open-api/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ exports.existy = val => val != null
3434

3535
// Async sleep. A whole new WORLD!
3636
exports.sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
37+
38+
exports.unixNow = () => Math.floor(new Date() / 1000)

0 commit comments

Comments
 (0)