Skip to content

Commit 48f41ce

Browse files
authored
fix: update path parsing to match Gatsby (#232)
* 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
1 parent aaabfa0 commit 48f41ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+56
-819
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
os: [ubuntu-latest, macOS-latest] #, windows-latest] # TODO add an e2etest.bat file
15-
node-version: [14.15.0, '*']
15+
node-version: ['*']
1616
exclude:
1717
- os: macOS-latest
1818
node-version: 14.15.0

plugin/package-lock.json

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@
5050
"linkfs": "^2.1.0",
5151
"multer": "^1.4.2",
5252
"node-fetch": "^2.6.1",
53-
"path-to-regexp": "^0.1.7",
5453
"tempy": "^1.0.0"
5554
},
5655
"devDependencies": {
56+
"@gatsbyjs/reach-router": "^1.3.6",
5757
"@types/fs-extra": "^9.0.12",
5858
"@types/multer": "^1.4.7",
5959
"gatsby": "^4.3.0",
6060
"npm-run-all": "^4.1.5",
6161
"rimraf": "^3.0.2",
6262
"typescript": "^4.5.2"
63+
},
64+
"peerDependencies": {
65+
"@gatsbyjs/reach-router": "^1.3.6"
6366
}
6467
}

plugin/src/templates/api/gatsbyFunction.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import pathToRegexp from 'path-to-regexp'
1+
import { existsSync } from 'fs'
2+
import path from 'path'
3+
4+
import { match as reachMatch } from '@gatsbyjs/reach-router/lib/utils'
5+
import { HandlerEvent } from '@netlify/functions'
26
import bodyParser from 'co-body'
37
import multer from 'multer'
4-
import path from 'path'
5-
import { existsSync } from 'fs'
6-
import { proxyRequest } from './utils'
8+
79
import {
10+
proxyRequest,
811
AugmentedGatsbyFunctionResponse,
912
AugmentedGatsbyFunctionRequest,
1013
} from './utils'
11-
import { HandlerEvent } from '@netlify/functions'
1214

1315
const parseForm = multer().any()
1416

@@ -34,17 +36,17 @@ export async function gatsbyFunction(
3436
) {
3537
req.body = await bodyParser(req as unknown as Request)
3638
}
37-
} catch (e) {
38-
console.log('Error parsing body', e, req)
39+
} catch (error) {
40+
console.log('Error parsing body', error, req)
3941
}
4042

41-
let pathFragment = decodeURIComponent(req.url).replace('/api/', '')
43+
const pathFragment = decodeURIComponent(req.url).replace('/api/', '')
4244

4345
let functions
4446
try {
4547
// @ts-ignore This is generated in the user's site
4648
functions = require('../../../.cache/functions/manifest.json') // eslint-disable-line node/no-missing-require, node/no-unpublished-require
47-
} catch (e) {
49+
} catch {
4850
return {
4951
statusCode: 404,
5052
body: 'Could not load function manifest',
@@ -59,24 +61,23 @@ export async function gatsbyFunction(
5961
if (!functionObj) {
6062
// Check if there's any matchPaths that match.
6163
// We loop until we find the first match.
62-
6364
functions.some((f) => {
64-
let exp
65-
const keys = []
6665
if (f.matchPath) {
67-
exp = pathToRegexp(f.matchPath, keys, {})
68-
}
69-
if (exp && exp.exec(pathFragment) !== null) {
70-
functionObj = f
71-
const matches = [...pathFragment.match(exp)].slice(1)
72-
const newParams = {}
73-
matches.forEach((match, index) => (newParams[keys[index].name] = match))
74-
req.params = newParams
75-
76-
return true
77-
} else {
78-
return false
66+
const matchResult = reachMatch(f.matchPath, pathFragment)
67+
if (matchResult) {
68+
req.params = matchResult.params
69+
if (req.params[`*`]) {
70+
// Backwards compatability for v3
71+
// TODO remove in v5
72+
req.params[`0`] = req.params[`*`]
73+
}
74+
functionObj = f
75+
76+
return true
77+
}
7978
}
79+
80+
return false
8081
})
8182
}
8283

@@ -109,20 +110,19 @@ export async function gatsbyFunction(
109110

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

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

117117
await Promise.resolve(fnToExecute(req, res))
118-
} catch (e) {
119-
console.error(e)
118+
} catch (error) {
119+
console.error(error)
120120
// Don't send the error if that would cause another error.
121121
if (!res.headersSent) {
122122
res
123123
.status(500)
124124
.send(
125-
`Error when executing function "${functionObj.originalRelativeFilePath}": "${e.message}"`,
125+
`Error when executing function "${functionObj.originalRelativeFilePath}": "${error.message}"`,
126126
)
127127
}
128128
}

plugin/test/fixtures/custom-functions-directory/.env.development

-4
This file was deleted.

plugin/test/fixtures/custom-functions-directory/.env.production

-1
This file was deleted.

plugin/test/fixtures/custom-functions-directory/.gitignore

-13
This file was deleted.

plugin/test/fixtures/custom-functions-directory/.nvmrc

-1
This file was deleted.

plugin/test/fixtures/custom-functions-directory/README.md

-54
This file was deleted.

plugin/test/fixtures/custom-functions-directory/e2e-tests/__snapshots__/functions.test.js.snap

-48
This file was deleted.

plugin/test/fixtures/custom-functions-directory/e2e-tests/fixtures/test.txt

-1
This file was deleted.

plugin/test/fixtures/custom-functions-directory/e2e-tests/functions.test.js

-8
This file was deleted.

0 commit comments

Comments
 (0)