Skip to content

Commit b1669c9

Browse files
committed
chore: remove wyt dependency
1 parent 14d8bbe commit b1669c9

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"utils"
2525
],
2626
"dependencies": {
27-
"@forivall/wyt": "^2.0.2",
2827
"gatsby-source-filesystem": "2.11.0",
2928
"google-oauth2-env-vars": "^1.3.0",
3029
"googleapis": "67.1.1",

utils/google-drive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const _chunk = require("lodash/chunk")
44
const _flatten = require("lodash/flatten")
55
const GoogleOAuth2 = require("google-oauth2-env-vars")
66
const yamljs = require("yamljs")
7-
const wyt = require("@forivall/wyt")
87

98
const {ENV_TOKEN_VAR} = require("./constants")
9+
const {wait} = require("./wait")
1010

1111
const MIME_TYPE_DOCUMENT = "application/vnd.google-apps.document"
1212
const MIME_TYPE_FOLDER = "application/vnd.google-apps.folder"
@@ -147,7 +147,7 @@ async function getGoogleDrive() {
147147
*/
148148

149149
// 10 per 1.5 seconds.
150-
const rateLimit = wyt(10, 1500)
150+
const rateLimit = wait(10, 1500)
151151
const BATCH_SIZE = 100
152152
/**
153153
* @param {import('..').Options & FetchDocumentsOptions} options

utils/wait.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function sleep(ms) {
2+
return new Promise((resolve) => {
3+
setTimeout(() => resolve(), ms)
4+
})
5+
}
6+
7+
function reload(requestsPerInterval, interval, state) {
8+
const throughput = requestsPerInterval / interval
9+
const now = Date.now()
10+
const reloadTime = now - state.lastReload
11+
const reloadedRequests = reloadTime * throughput
12+
const newAvalableRequests = state.availableRequests + reloadedRequests
13+
14+
return {
15+
...state,
16+
lastReload: now,
17+
availableRequests: Math.min(newAvalableRequests, requestsPerInterval),
18+
}
19+
}
20+
21+
function wait(requestsPerInterval, interval) {
22+
const timePerRequest = interval / requestsPerInterval
23+
24+
let state = {
25+
lastReload: Date.now(),
26+
availableRequests: requestsPerInterval,
27+
}
28+
29+
async function waitRequest(requests = 1) {
30+
if (requests > requestsPerInterval) {
31+
throw new Error(
32+
"Requests can not be greater than the number of requests per interval"
33+
)
34+
}
35+
36+
state = reload(requestsPerInterval, interval, state)
37+
38+
const requestsToWait = Math.max(0, requests - state.availableRequests)
39+
const wait = Math.ceil(requestsToWait * timePerRequest)
40+
41+
state.availableRequests -= requests
42+
await sleep(wait)
43+
return wait
44+
}
45+
46+
return waitRequest
47+
}
48+
49+
exports.wait = wait

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,6 @@
478478
minimatch "^3.0.4"
479479
strip-json-comments "^3.1.1"
480480

481-
"@forivall/wyt@^2.0.2":
482-
version "2.0.2"
483-
resolved "https://registry.yarnpkg.com/@forivall/wyt/-/wyt-2.0.2.tgz#2516bfe7cecb21605447dfb37039ab3eea16dab3"
484-
integrity sha512-P5WLG9xdDf5cwzdZPLycrubCwStc1xlSnG1U4FVvCoK1owEClCJU9bK/BU2eD6VeUQy8naK00yt6YbVJqZHWPg==
485-
486481
"@iarna/cli@^1.2.0":
487482
version "1.2.0"
488483
resolved "https://registry.yarnpkg.com/@iarna/cli/-/cli-1.2.0.tgz#0f7af5e851afe895104583c4ca07377a8094d641"

0 commit comments

Comments
 (0)