Skip to content

Commit

Permalink
fix: update path parsing to match Gatsby (#232)
Browse files Browse the repository at this point in the history
* fix: update path parsing to match Gatsby

* fix: snapidoo ✨

* chore: extend test timeout

* chore: delete unused test dir

* chore: disable node 14 tests because of segfaults in gatsby develop
  • Loading branch information
ascorbic authored Jan 21, 2022
1 parent aaabfa0 commit 48f41ce
Show file tree
Hide file tree
Showing 50 changed files with 56 additions and 819 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest] #, windows-latest] # TODO add an e2etest.bat file
node-version: [14.15.0, '*']
node-version: ['*']
exclude:
- os: macOS-latest
node-version: 14.15.0
Expand Down
13 changes: 9 additions & 4 deletions plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@
"linkfs": "^2.1.0",
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
"path-to-regexp": "^0.1.7",
"tempy": "^1.0.0"
},
"devDependencies": {
"@gatsbyjs/reach-router": "^1.3.6",
"@types/fs-extra": "^9.0.12",
"@types/multer": "^1.4.7",
"gatsby": "^4.3.0",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"typescript": "^4.5.2"
},
"peerDependencies": {
"@gatsbyjs/reach-router": "^1.3.6"
}
}
56 changes: 28 additions & 28 deletions plugin/src/templates/api/gatsbyFunction.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import pathToRegexp from 'path-to-regexp'
import { existsSync } from 'fs'
import path from 'path'

import { match as reachMatch } from '@gatsbyjs/reach-router/lib/utils'
import { HandlerEvent } from '@netlify/functions'
import bodyParser from 'co-body'
import multer from 'multer'
import path from 'path'
import { existsSync } from 'fs'
import { proxyRequest } from './utils'

import {
proxyRequest,
AugmentedGatsbyFunctionResponse,
AugmentedGatsbyFunctionRequest,
} from './utils'
import { HandlerEvent } from '@netlify/functions'

const parseForm = multer().any()

Expand All @@ -34,17 +36,17 @@ export async function gatsbyFunction(
) {
req.body = await bodyParser(req as unknown as Request)
}
} catch (e) {
console.log('Error parsing body', e, req)
} catch (error) {
console.log('Error parsing body', error, req)
}

let pathFragment = decodeURIComponent(req.url).replace('/api/', '')
const pathFragment = decodeURIComponent(req.url).replace('/api/', '')

let functions
try {
// @ts-ignore This is generated in the user's site
functions = require('../../../.cache/functions/manifest.json') // eslint-disable-line node/no-missing-require, node/no-unpublished-require
} catch (e) {
} catch {
return {
statusCode: 404,
body: 'Could not load function manifest',
Expand All @@ -59,24 +61,23 @@ export async function gatsbyFunction(
if (!functionObj) {
// Check if there's any matchPaths that match.
// We loop until we find the first match.

functions.some((f) => {
let exp
const keys = []
if (f.matchPath) {
exp = pathToRegexp(f.matchPath, keys, {})
}
if (exp && exp.exec(pathFragment) !== null) {
functionObj = f
const matches = [...pathFragment.match(exp)].slice(1)
const newParams = {}
matches.forEach((match, index) => (newParams[keys[index].name] = match))
req.params = newParams

return true
} else {
return false
const matchResult = reachMatch(f.matchPath, pathFragment)
if (matchResult) {
req.params = matchResult.params
if (req.params[`*`]) {
// Backwards compatability for v3
// TODO remove in v5
req.params[`0`] = req.params[`*`]
}
functionObj = f

return true
}
}

return false
})
}

Expand Down Expand Up @@ -109,20 +110,19 @@ export async function gatsbyFunction(

try {
// Make sure it's hot and fresh from the filesystem
delete require.cache[require.resolve(pathToFunction)]
const fn = require(pathToFunction)

const fnToExecute = (fn && fn.default) || fn

await Promise.resolve(fnToExecute(req, res))
} catch (e) {
console.error(e)
} catch (error) {
console.error(error)
// Don't send the error if that would cause another error.
if (!res.headersSent) {
res
.status(500)
.send(
`Error when executing function "${functionObj.originalRelativeFilePath}": "${e.message}"`,
`Error when executing function "${functionObj.originalRelativeFilePath}": "${error.message}"`,
)
}
}
Expand Down

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions plugin/test/fixtures/custom-functions-directory/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion plugin/test/fixtures/custom-functions-directory/.nvmrc

This file was deleted.

54 changes: 0 additions & 54 deletions plugin/test/fixtures/custom-functions-directory/README.md

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 48f41ce

Please sign in to comment.