Skip to content

Commit

Permalink
fix enoent
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Aug 20, 2021
1 parent e955764 commit 7b12c96
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 79 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
"private": true,
"resolutions": {
"theme-ui": "0.4.0-rc.14",
"csstype": "2.6.17"
"csstype": "2.6.17",
"fs-extra": "9.1.0"
},
"scripts": {
"bootstrap": "npm-run-all -s check-versions \"lerna-prepare -- --{@}\" --",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"ci-info": "2.0.0",
"configstore": "^5.0.1",
"file-type": "^16.5.3",
"fs-extra": "^8.1.0",
"fs-extra": "^9.1.0",
"node-object-hash": "^2.3.8",
"proper-lockfile": "^4.1.2",
"tmp": "^0.2.1",
Expand Down
28 changes: 23 additions & 5 deletions packages/gatsby-core-utils/src/fetch-remote-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ const requestRemoteNode = (
// Called if we stall for 30s without receiving any data
const handleTimeout = async (): Promise<void> => {
fsWriteStream.close()
fs.removeSync(tmpFilename)

try {
await fs.remove(tmpFilename)
} catch (err) {
// ignore error
}

if (attempt < STALL_RETRY_LIMIT) {
// Retry by calling ourself recursively
resolve(
Expand Down Expand Up @@ -109,11 +115,19 @@ const requestRemoteNode = (
responseStream.pipe(fsWriteStream)

// If there's a 400/500 response or other error.
responseStream.on(`error`, error => {
responseStream.on(`error`, async error => {
if (timeout) {
clearTimeout(timeout)
}
fs.removeSync(tmpFilename)

// close the stream
fsWriteStream.close()

try {
await fs.remove(tmpFilename)
} catch (err) {
// ignore error
}
reject(error)
})

Expand All @@ -127,12 +141,16 @@ const requestRemoteNode = (
responseStream.on(`response`, response => {
resetTimeout()

fsWriteStream.on(`finish`, () => {
fsWriteStream.on(`finish`, async () => {
fsWriteStream.close()

// We have an incomplete download
if (!haveAllBytesBeenWritten) {
fs.removeSync(tmpFilename)
try {
await fs.remove(tmpFilename)
} catch (err) {
// ignore error
}

if (attempt < INCOMPLETE_RETRY_LIMIT) {
resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import os from "os"
import fs from "fs"
import fs from "fs-extra"
import path from "path"

import nock from "nock"
Expand Down Expand Up @@ -32,13 +32,14 @@ let cache
const host = `https://images.ctfassets.net`

describe(`fetch-contentful-assets`, () => {
beforeAll(() => {
beforeAll(async () => {
cache = createMockCache()
await fs.ensureDir(cache.directory)
})

afterAll(() => {
afterAll(async () => {
if (cache) {
fs.removeSync(cache.directory)
await fs.remove(cache.directory)
}
})

Expand All @@ -47,7 +48,7 @@ describe(`fetch-contentful-assets`, () => {
reporter.verbose.mockClear()
})

test(`resolves regular response`, async () => {
it(`resolves regular response`, async () => {
const path = `/resolves.jpg`
const url = [host, path].join(``)
const scope = nock(host).get(path).reply(200)
Expand All @@ -58,7 +59,7 @@ describe(`fetch-contentful-assets`, () => {
expect(scope.isDone()).toBeTruthy()
})

test(`returns from cache on second call`, async () => {
it(`returns from cache on second call`, async () => {
const path = `/resolves-from-cache.jpg`
const url = [host, path].join(``)
const scope = nock(host).get(path).twice().reply(200)
Expand All @@ -73,7 +74,7 @@ describe(`fetch-contentful-assets`, () => {
])
})

test(`does not retry for no reason`, async () => {
it(`does not retry for no reason`, async () => {
const path = `/no-retry.jpg`
const url = [host, path].join(``)
const scope = nock(host).get(path).twice().reply(200)
Expand All @@ -84,7 +85,7 @@ describe(`fetch-contentful-assets`, () => {
expect(scope.isDone()).toBeFalsy()
})

test(`does not retry on 404`, async () => {
it(`does not retry on 404`, async () => {
const path = `/no-retry-on-404.jpg`
const url = [host, path].join(``)
const scope = nock(host).get(path).twice().reply(404)
Expand All @@ -102,7 +103,7 @@ describe(`fetch-contentful-assets`, () => {
])
})

test(`does retry on 503`, async () => {
it(`does retry on 503`, async () => {
const path = `/retry-on-503.jpg`
const url = [host, path].join(``)

Expand All @@ -121,7 +122,7 @@ describe(`fetch-contentful-assets`, () => {
expect(scope.isDone()).toBeTruthy()
})

test(`stops retry after 3 attempts`, async () => {
it(`stops retry after 3 attempts`, async () => {
const path = `/stop-retry-after-3-attempts.jpg`
const url = [host, path].join(``)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-filesystem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"better-queue": "^3.8.10",
"chokidar": "^3.4.3",
"file-type": "^16.0.0",
"fs-extra": "^8.1.0",
"fs-extra": "^9.1.0",
"gatsby-core-utils": "^2.13.0-next.0",
"got": "^9.6.0",
"md5-file": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ describe(`create-file-node-from-buffer`, () => {
expect(() => {
createFileNodeFromBuffer({
...defaultArgs,
buffer: createMockBuffer(`test`),
getCache: () => createMockCache(),
})
}).not.toThrow()
Expand All @@ -168,6 +169,7 @@ describe(`create-file-node-from-buffer`, () => {
expect(() => {
createFileNodeFromBuffer({
...defaultArgs,
buffer: createMockBuffer(`test`),
cache: createMockCache(),
})
}).not.toThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jest.mock(`../create-file-node`, () => {
createFileNode: jest.fn(),
}
})
const reporter = require(`gatsby/reporter`)
const reporter = {}

const got = require(`got`)
const createRemoteFileNode = require(`../create-remote-file-node`)
Expand Down Expand Up @@ -285,6 +285,8 @@ describe(`create-remote-file-node`, () => {
createRemoteFileNode({
...defaultArgs,
getCache: () => createMockCache(),
}).catch(err => {
// ignore the url error
})
}).not.toThrow()
})
Expand All @@ -294,6 +296,8 @@ describe(`create-remote-file-node`, () => {
createRemoteFileNode({
...defaultArgs,
cache: createMockCache(),
}).catch(err => {
// ignore the url error
})
}).not.toThrow()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const fs = require(`fs-extra`)
const { createContentDigest } = require(`gatsby-core-utils`)
const { createContentDigest, fetchRemoteFile } = require(`gatsby-core-utils`)
const path = require(`path`)
const { isWebUri } = require(`valid-url`)
const Queue = require(`better-queue`)
const { fetchRemoteFile } = require(`gatsby-core-utils`)
const { createFileNode } = require(`./create-file-node`)
const { getRemoteFileExtension, createFilePath } = require(`./utils`)

Expand Down
60 changes: 2 additions & 58 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12688,51 +12688,7 @@ fs-exists-sync@^0.1.0:
resolved "https://registry.yarnpkg.com/fs-exists-sync/-/fs-exists-sync-0.1.0.tgz#982d6893af918e72d08dec9e8673ff2b5a8d6add"
integrity sha1-mC1ok6+RjnLQjeyehnP/K1qNat0=

fs-extra@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.0.tgz#0f0afb290bb3deb87978da816fcd3c7797f3a817"
integrity sha512-lk2cUCo8QzbiEWEbt7Cw3m27WMiRG321xsssbcIpfMhpRjrlC08WBOVQqj1/nQYYNnPtyIhP1oqLO3QwT2tPCw==
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@8.1.0, fs-extra@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
dependencies:
at-least-node "^1.0.0"
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^1.0.0"

fs-extra@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-1.0.0.tgz#cd3ce5f7e7cb6145883fcae3191e9877f8587950"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^2.1.0"
klaw "^1.0.0"

fs-extra@^4.0.2:
version "4.0.3"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@^9.0.1, fs-extra@^9.1.0:
fs-extra@6.0.0, fs-extra@8.1.0, fs-extra@9.0.1, fs-extra@9.1.0, fs-extra@^1.0.0, fs-extra@^4.0.2, fs-extra@^8.1.0, fs-extra@^9.0.1, fs-extra@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"
integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==
Expand Down Expand Up @@ -13497,7 +13453,7 @@ got@^9.6.0:
to-readable-stream "^1.0.0"
url-parse-lax "^3.0.0"

graceful-fs@^4.0.0, graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9, graceful-fs@^4.2.0, graceful-fs@^4.2.2, graceful-fs@^4.2.3, graceful-fs@^4.2.4:
graceful-fs@^4.0.0, graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.2, graceful-fs@^4.2.3, graceful-fs@^4.2.4:
version "4.2.4"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==
Expand Down Expand Up @@ -17011,12 +16967,6 @@ json5@^2.0.0, json5@^2.1.0, json5@^2.1.2, json5@^2.1.3:
dependencies:
minimist "^1.2.5"

jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
optionalDependencies:
graceful-fs "^4.1.6"

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down Expand Up @@ -17173,12 +17123,6 @@ kind-of@^6.0.0, kind-of@^6.0.2, kind-of@^6.0.3:
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==

klaw@^1.0.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439"
optionalDependencies:
graceful-fs "^4.1.9"

kleur@4.1.4, kleur@^4.1.3:
version "4.1.4"
resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.4.tgz#8c202987d7e577766d039a8cd461934c01cda04d"
Expand Down

0 comments on commit 7b12c96

Please sign in to comment.