Skip to content

Commit 4263bcf

Browse files
committed
getGraphqlEngine renaming
1 parent ed74e49 commit 4263bcf

File tree

1 file changed

+62
-53
lines changed
  • packages/gatsby/src/utils/page-ssr-module

1 file changed

+62
-53
lines changed

packages/gatsby/src/utils/page-ssr-module/lambda.ts

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -243,54 +243,62 @@ interface IEngineError extends Error {
243243
downloadError?: boolean
244244
}
245245

246-
async function getGraphqlEngineInner(
247-
origin: string
248-
): Promise<GraphQLEngineType> {
249-
if (cdnDatastorePath) {
250-
const cdnDatastore = `${origin}/${cdnDatastorePath}`
251-
// if this variable is set we need to download the datastore from the CDN
252-
const downloadPath = dbPath + `/data.mdb`
253-
console.log(
254-
`Downloading datastore from CDN (${cdnDatastore} -> ${downloadPath})`
255-
)
256-
257-
await fs.ensureDir(dbPath)
258-
await new Promise((resolve, reject) => {
259-
const req = get(cdnDatastore, response => {
260-
if (
261-
!response.statusCode ||
262-
response.statusCode < 200 ||
263-
response.statusCode > 299
264-
) {
265-
const engineError = new Error(
266-
`Failed to download ${cdnDatastore}: ${response.statusCode} ${
267-
response.statusMessage || ``
268-
}`
269-
) as IEngineError
270-
engineError.downloadError = true
271-
reject(engineError)
272-
return
273-
}
246+
function shouldDownloadDatastoreFromCDN(): boolean {
247+
return !!cdnDatastorePath
248+
}
274249

275-
const fileStream = fs.createWriteStream(downloadPath)
276-
streamPipeline(response, fileStream)
277-
.then(resolve)
278-
.catch(error => {
279-
console.log(`Error downloading ${cdnDatastore}`, error)
280-
const engineError = error as IEngineError
281-
engineError.downloadError = true
282-
reject(engineError)
283-
})
284-
})
250+
async function downloadDatastoreFromCDN(origin: string): Promise<void> {
251+
const cdnDatastore = `${origin}/${cdnDatastorePath}`
252+
// if this variable is set we need to download the datastore from the CDN
253+
const downloadPath = dbPath + `/data.mdb`
254+
console.log(
255+
`Downloading datastore from CDN (${cdnDatastore} -> ${downloadPath})`
256+
)
285257

286-
req.on(`error`, error => {
287-
console.log(`Error downloading ${cdnDatastore}`, error)
288-
const engineError = error as IEngineError
258+
await fs.ensureDir(dbPath)
259+
await new Promise((resolve, reject) => {
260+
const req = get(cdnDatastore, response => {
261+
if (
262+
!response.statusCode ||
263+
response.statusCode < 200 ||
264+
response.statusCode > 299
265+
) {
266+
const engineError = new Error(
267+
`Failed to download ${cdnDatastore}: ${response.statusCode} ${
268+
response.statusMessage || ``
269+
}`
270+
) as IEngineError
289271
engineError.downloadError = true
290272
reject(engineError)
291-
})
273+
return
274+
}
275+
276+
const fileStream = fs.createWriteStream(downloadPath)
277+
streamPipeline(response, fileStream)
278+
.then(resolve)
279+
.catch(error => {
280+
console.log(`Error downloading ${cdnDatastore}`, error)
281+
const engineError = error as IEngineError
282+
engineError.downloadError = true
283+
reject(engineError)
284+
})
292285
})
293-
console.log(`Downloaded datastore from CDN`)
286+
287+
req.on(`error`, error => {
288+
console.log(`Error downloading ${cdnDatastore}`, error)
289+
const engineError = error as IEngineError
290+
engineError.downloadError = true
291+
reject(engineError)
292+
})
293+
})
294+
console.log(`Downloaded datastore from CDN`)
295+
}
296+
297+
async function initializeGraphqlEngine(
298+
origin: string
299+
): Promise<GraphQLEngineType> {
300+
if (shouldDownloadDatastoreFromCDN()) {
301+
await downloadDatastoreFromCDN(origin)
294302
}
295303

296304
const graphqlEngine = new GraphQLEngine({
@@ -308,19 +316,19 @@ const originToGraphqlEnginePromise = new Map<
308316
Promise<GraphQLEngineType> | null | Error
309317
>()
310318

311-
function tryToDownloadEngineFromCollectedOrigins(): Promise<GraphQLEngineType> {
319+
function tryToInitializeGraphqlEngineFromCollectedOrigins(): Promise<GraphQLEngineType> {
312320
for (const [origin, originEngineState] of originToGraphqlEnginePromise) {
313321
if (!(originEngineState instanceof Error)) {
314322
if (originEngineState === null) {
315-
const engineForOriginPromise = getGraphqlEngineInner(origin).catch(
323+
const engineForOriginPromise = initializeGraphqlEngine(origin).catch(
316324
e => {
317325
originToGraphqlEnginePromise.set(
318326
origin,
319327
e instanceof Error ? e : new Error(e)
320328
)
321329

322330
if (e.downloadError) {
323-
return tryToDownloadEngineFromCollectedOrigins()
331+
return tryToInitializeGraphqlEngineFromCollectedOrigins()
324332
}
325333

326334
throw e
@@ -337,11 +345,9 @@ function tryToDownloadEngineFromCollectedOrigins(): Promise<GraphQLEngineType> {
337345
return Promise.reject(new Error(`No engine available`))
338346
}
339347

340-
function getGraphqlEngine(
341-
req?: GatsbyFunctionRequest
348+
function memoizedInitializeGraphqlEngine(
349+
origin: string
342350
): Promise<GraphQLEngineType> {
343-
const origin = req?.rawUrl ? new URL(req.rawUrl).origin : cdnDatastoreOrigin
344-
345351
if (!originToGraphqlEnginePromise.has(origin)) {
346352
// register origin, but for now don't init anything
347353
originToGraphqlEnginePromise.set(origin, null)
@@ -350,7 +356,7 @@ function getGraphqlEngine(
350356
if (!memoizedGraphqlEnginePromise) {
351357
// pick first non-errored entry
352358
memoizedGraphqlEnginePromise =
353-
tryToDownloadEngineFromCollectedOrigins().catch(e => {
359+
tryToInitializeGraphqlEngineFromCollectedOrigins().catch(e => {
354360
// at this point we don't have any origin that work, but maybe we will get one in future
355361
// so unset memoizedGraphqlEnginePromise as it would be not allowing any more attempts once it settled
356362
memoizedGraphqlEnginePromise = null
@@ -360,7 +366,7 @@ function getGraphqlEngine(
360366
return memoizedGraphqlEnginePromise
361367
}
362368

363-
getGraphqlEngine().catch(
369+
memoizedInitializeGraphqlEngine(cdnDatastoreOrigin).catch(
364370
() =>
365371
// we don't want to crash the process if we can't get the engine without a request
366372
null
@@ -481,7 +487,10 @@ async function engineHandler(
481487

482488
const data = await getData({
483489
pathName: pagePath,
484-
getGraphqlEngine: () => getGraphqlEngine(req),
490+
getGraphqlEngine: () =>
491+
memoizedInitializeGraphqlEngine(
492+
req?.rawUrl ? new URL(req.rawUrl).origin : cdnDatastoreOrigin
493+
),
485494
req,
486495
})
487496

0 commit comments

Comments
 (0)