Skip to content

Commit

Permalink
update types & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Sep 14, 2021
1 parent 0e635ea commit 936d48c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
__BASE_PATH__: true,
__ASSET_PREFIX__: true,
_CFLAGS_: true,
__GATSBY: true,
},
rules: {
"@babel/no-unused-expressions": [
Expand Down
40 changes: 39 additions & 1 deletion packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function getFileContent(file, req, options = {}) {
contentLength:
req.url.searchParams.get(`contentLength`) === `false`
? undefined
: fileContentBuffer.length,
: String(fileContentBuffer.length),
}
}

Expand Down Expand Up @@ -120,6 +120,26 @@ const server = setupServer(
ctx.status(200),
ctx.body(content)
)
}),
rest.get(`http://external.com/404.jpg`, async (req, res, ctx) => {
const content = `Page not found`

return res(
ctx.set(`Content-Type`, `text/html`),
ctx.set(`Content-Length`, String(content.length)),
ctx.status(404),
ctx.body(content)
)
}),
rest.get(`http://external.com/500.jpg`, async (req, res, ctx) => {
const content = `Server error`

return res(
ctx.set(`Content-Type`, `text/html`),
ctx.set(`Content-Length`, String(content.length)),
ctx.status(500),
ctx.body(content)
)
})
)

Expand Down Expand Up @@ -352,6 +372,24 @@ describe(`fetch-remote-file`, () => {
expect(fsMove).toBeCalledTimes(2)
})

it(`fails when 404 is triggered`, async () => {
await expect(
fetchRemoteFile({
url: `http://external.com/404.jpg`,
cache,
})
).rejects.toThrow(`Response code 404 (Not Found)`)
})

it(`fails when 500 is triggered`, async () => {
await expect(
fetchRemoteFile({
url: `http://external.com/500.jpg`,
cache,
})
).rejects.toThrow(`Response code 500 (Internal Server Error)`)
})

describe(`retries the download`, () => {
it(`Retries when gzip compression file is incomplete`, async () => {
const filePath = await fetchRemoteFile({
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby-core-utils/src/fetch-remote-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,18 @@ function requestRemoteNode(
responseStream.pipe(fsWriteStream)

// If there's a 400/500 response or other error.
// it will trigger a finish event on fsWriteStream
responseStream.on(`error`, error => {
if (timeout) {
clearTimeout(timeout)
}

fsWriteStream.close()
fs.removeSync(tmpFilename)

process.nextTick(() => {
reject(error)
})
})

responseStream.on(`response`, response => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ export interface GatsbyFunctionRequest extends IncomingMessage {
cookies: Record<string, string>
}

declare namespace NodeJS {
declare module NodeJS {
interface Global {
__GATSBY: {
buildId: string
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
// global gatsby object to use without store
global.__GATSBY = {
buildId: uuidv4(),
root: program.directory,
root: program!.directory,
}

if (isTruthy(process.env.VERBOSE)) {
Expand Down
9 changes: 9 additions & 0 deletions types/gatsby-monorepo/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
declare const _CFLAGS_: {
GATSBY_MAJOR: string
}

declare module NodeJS {
interface Global {
__GATSBY: {
buildId: string
root: string
}
}
}

0 comments on commit 936d48c

Please sign in to comment.