Skip to content

Commit d13c815

Browse files
committed
Handle URL parsing errors
1 parent 732cb36 commit d13c815

3 files changed

Lines changed: 79 additions & 73 deletions

File tree

index.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,34 @@ const logIt = (e, level = 'fatal') => {
7373
}
7474

7575
const parseUrl = (req) => {
76-
const url = decodeURIComponent(req.url).slice(baseChars)
77-
const sIdx = url.indexOf('/')
78-
const qIdx = url.indexOf('?')
79-
const modIdx = (sIdx === -1 || (qIdx !== -1 && sIdx > qIdx)) ? qIdx : sIdx
80-
const lastIdx = url.length - 1
81-
82-
let id
83-
if (sIdx === modIdx) {
84-
const postMod = url.slice(sIdx + 1)
85-
if (postMod) {
86-
let nextMod = postMod.indexOf('/')
87-
if (nextMod === -1) nextMod = postMod.indexOf('?')
88-
id = nextMod === -1 ? postMod : postMod.slice(0, nextMod)
76+
try {
77+
const url = decodeURIComponent(req.url).slice(baseChars)
78+
const sIdx = url.indexOf('/')
79+
const qIdx = url.indexOf('?')
80+
const modIdx = (sIdx === -1 || (qIdx !== -1 && sIdx > qIdx)) ? qIdx : sIdx
81+
const lastIdx = url.length - 1
82+
83+
let id
84+
if (sIdx === modIdx) {
85+
const postMod = url.slice(sIdx + 1)
86+
if (postMod) {
87+
let nextMod = postMod.indexOf('/')
88+
if (nextMod === -1) nextMod = postMod.indexOf('?')
89+
id = nextMod === -1 ? postMod : postMod.slice(0, nextMod)
90+
}
8991
}
90-
}
9192

92-
const noMod = modIdx === -1
93-
const name = noMod ? url : url.slice(0, modIdx)
94-
const modifier = noMod || modIdx === lastIdx ? '' : url.charAt(modIdx)
95-
const action = scrud[`${req.method}${modifier}`]
96-
const params = tinyParams(req.url)
97-
const data = { url, name, action, id, params }
93+
const noMod = modIdx === -1
94+
const name = noMod ? url : url.slice(0, modIdx)
95+
const modifier = noMod || modIdx === lastIdx ? '' : url.charAt(modIdx)
96+
const action = scrud[`${req.method}${modifier}`]
97+
const params = tinyParams(req.url)
98+
const data = { url, name, action, id, params }
9899

99-
return data
100+
return data
101+
} catch (error) {
102+
return { error }
103+
}
100104
}
101105

102106
const callPgFunc = (name, params, req, altPgPool) => {
@@ -282,7 +286,10 @@ function handleRequest (req, res) {
282286
return ackPreflight(res, origin, header('access-control-request-headers'))
283287
}
284288

285-
const { name, action, id, params } = parseUrl(req)
289+
const { name, action, id, params, error: parseError } = parseUrl(req)
290+
291+
if (parseError) return sendErr(res, parseError)
292+
286293
const resource = resources[name]
287294

288295
if (!resource || !action) return fourOhFour(res)

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scrud",
3-
"version": "10.1.0",
3+
"version": "10.2.0",
44
"description": "SCRUD API server, fast, light, capable",
55
"engines": {
66
"node": ">=8.0.0"
@@ -26,18 +26,18 @@
2626
"homepage": "http://scrud.doesdev.com",
2727
"dependencies": {
2828
"jsonwebtoken": "~9.0.2",
29-
"pg": "~8.16.0",
29+
"pg": "~8.16.2",
3030
"tiny-params": "~4.0.0"
3131
},
3232
"devDependencies": {
33-
"axios": "^1.9.0",
33+
"axios": "^1.10.0",
3434
"dotenv": "^16.5.0",
35-
"get-scrud": "^4.1.0",
35+
"get-scrud": "^5.1.0",
3636
"hash-wasm": "^4.12.0",
3737
"husky": "^9.1.7",
3838
"import-fresh": "^3.3.1",
3939
"mvt": "^4.2.1",
4040
"standard": "^17.1.2"
4141
},
4242
"packageManager": "yarn@3.5.1"
43-
}
43+
}

yarn.lock

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,14 @@ __metadata:
294294
languageName: node
295295
linkType: hard
296296

297-
"axios@npm:1.9.0, axios@npm:^1.9.0":
298-
version: 1.9.0
299-
resolution: "axios@npm:1.9.0"
297+
"axios@npm:^1.10.0":
298+
version: 1.10.0
299+
resolution: "axios@npm:1.10.0"
300300
dependencies:
301301
follow-redirects: ^1.15.6
302302
form-data: ^4.0.0
303303
proxy-from-env: ^1.1.0
304-
checksum: 631f02c9c279f2ae90637a4989cc9d75c1c27aefd16b6e8eb90f98a4d0bddaccfd1cb1387be12101d1ab0f9bbf0c47e2451b4de0cf2870462a7d9ed3de8da3f2
304+
checksum: b5fd840d499469bf968e44b8ac96f4b363c6aa4c791a50834c086a7cffbc2d77fe24f27af1aba46c3e1f4840aaf991461fc27537990596b93dea0f4df3245a86
305305
languageName: node
306306
linkType: hard
307307

@@ -1150,14 +1150,13 @@ __metadata:
11501150
languageName: node
11511151
linkType: hard
11521152

1153-
"get-scrud@npm:^4.1.0":
1154-
version: 4.1.0
1155-
resolution: "get-scrud@npm:4.1.0"
1153+
"get-scrud@npm:^5.1.0":
1154+
version: 5.1.0
1155+
resolution: "get-scrud@npm:5.1.0"
11561156
dependencies:
1157-
axios: 1.9.0
1158-
pico-ms: ^1.1.1
1159-
ricks-bricks: ^1.1.0
1160-
checksum: 97d168d6355c51f2136e25b4ed2bc55b57108542a97e118a0ea13a40479a71a202cc5c21f1fdd2337d75f3d80a77c1e101759f86ef51eb19bbb368bf190c96f2
1157+
pico-ms: ^2.0.1
1158+
ricks-bricks: ^2.0.0
1159+
checksum: d17ad6b1358d3aeeb85a639ba940e3934d6300569e6d74fb7edc8398ffcd9c90e2b23735fa3d4314e7a9ec4d5bd792d2c40daac9b3635aa3e58458de1ff1b72a
11611160
languageName: node
11621161
linkType: hard
11631162

@@ -2111,17 +2110,17 @@ __metadata:
21112110
languageName: node
21122111
linkType: hard
21132112

2114-
"pg-cloudflare@npm:^1.2.5":
2115-
version: 1.2.5
2116-
resolution: "pg-cloudflare@npm:1.2.5"
2117-
checksum: 1d10b067f71f678975441dc63d318dd4307ce1ca5780ceb9fdb0eea3986cdc80ec09612847e7022d74548d6cc0f4eea75b76d8d763aa94a1ec29869fbf1b1ee1
2113+
"pg-cloudflare@npm:^1.2.6":
2114+
version: 1.2.6
2115+
resolution: "pg-cloudflare@npm:1.2.6"
2116+
checksum: da9aada107210d8ae6a1c3bbaf539f9896dda7ebaa922738563bcf9acf2f3d5920dc7c57826fe31f14222daff58159b0d175cdf905f96e139e9110458e815845
21182117
languageName: node
21192118
linkType: hard
21202119

2121-
"pg-connection-string@npm:^2.9.0":
2122-
version: 2.9.0
2123-
resolution: "pg-connection-string@npm:2.9.0"
2124-
checksum: c87a25b532e6e575087cb6b9032a7d13e26c5679e997a17bbaf3b646b86ec045f18646971331cc049b5cf58bf97c62b61d44898ac6cbc747242e5a76cd88a8ef
2120+
"pg-connection-string@npm:^2.9.1":
2121+
version: 2.9.1
2122+
resolution: "pg-connection-string@npm:2.9.1"
2123+
checksum: 23e63951f866ea400b227976be596963c5e68b84dc161df0aa3e36fe2dc281f405e5121d71ba9d2f27973e25a46dbb219056fd91080505bcadc9ae98c9663cf3
21252124
languageName: node
21262125
linkType: hard
21272126

@@ -2132,19 +2131,19 @@ __metadata:
21322131
languageName: node
21332132
linkType: hard
21342133

2135-
"pg-pool@npm:^3.10.0":
2136-
version: 3.10.0
2137-
resolution: "pg-pool@npm:3.10.0"
2134+
"pg-pool@npm:^3.10.1":
2135+
version: 3.10.1
2136+
resolution: "pg-pool@npm:3.10.1"
21382137
peerDependencies:
21392138
pg: ">=8.0"
2140-
checksum: f2958ae9ba97f2514548d8f7ccd0fc6ab1299143e8d5a976f5888c3dbf22bdf4c577e40212b1403e5add7489ce276d16c1a23ef987089c4c43d5a2eaddd6bb73
2139+
checksum: 98135a7384be40886bba7100b9ce1a74671ff3877390f68e6db6d50ea56a7f524f7e44e01c02d61efeda97d9dc22d6115d0c66aa7f3cf5b8e892424862d0111a
21412140
languageName: node
21422141
linkType: hard
21432142

2144-
"pg-protocol@npm:^1.10.0":
2145-
version: 1.10.0
2146-
resolution: "pg-protocol@npm:1.10.0"
2147-
checksum: 535a1c5517ac9b797110004463b458a5a3e2fb24b80f567ff0ef7a5417914711515a9279f91983251422ef9fe7a436113e5e9ea66fa6d9d608a468081c14c50d
2143+
"pg-protocol@npm:^1.10.2":
2144+
version: 1.10.2
2145+
resolution: "pg-protocol@npm:1.10.2"
2146+
checksum: 309f8a021e1ab309fce7e5890477d329403240fb0bf62c5a2202c54546dbb8d0c50fc2c528d41590f936b1fd78cf0685b0f42b4a0f80ab35751f7764ee7fe319
21482147
languageName: node
21492148
linkType: hard
21502149

@@ -2161,14 +2160,14 @@ __metadata:
21612160
languageName: node
21622161
linkType: hard
21632162

2164-
"pg@npm:~8.16.0":
2165-
version: 8.16.0
2166-
resolution: "pg@npm:8.16.0"
2163+
"pg@npm:~8.16.2":
2164+
version: 8.16.2
2165+
resolution: "pg@npm:8.16.2"
21672166
dependencies:
2168-
pg-cloudflare: ^1.2.5
2169-
pg-connection-string: ^2.9.0
2170-
pg-pool: ^3.10.0
2171-
pg-protocol: ^1.10.0
2167+
pg-cloudflare: ^1.2.6
2168+
pg-connection-string: ^2.9.1
2169+
pg-pool: ^3.10.1
2170+
pg-protocol: ^1.10.2
21722171
pg-types: 2.2.0
21732172
pgpass: 1.0.5
21742173
peerDependencies:
@@ -2179,7 +2178,7 @@ __metadata:
21792178
peerDependenciesMeta:
21802179
pg-native:
21812180
optional: true
2182-
checksum: c1acf8de42d163e27f4362f71c0301e8eea86e352ac1de8e500ace9ecc1317b239dadd320745aa4a2549239d84e3273c5b7cb17c341d62290683bf1d469bac5d
2181+
checksum: 51be8837dd5ae1e85ee6261b50f70a51c5b0efbf0b16e35f2331494e4a1d4d7b97dff69317e07977767a3415660035198983bcef3857ebe09e00d3ed2b4ec159
21832182
languageName: node
21842183
linkType: hard
21852184

@@ -2192,10 +2191,10 @@ __metadata:
21922191
languageName: node
21932192
linkType: hard
21942193

2195-
"pico-ms@npm:^1.1.1":
2196-
version: 1.1.1
2197-
resolution: "pico-ms@npm:1.1.1"
2198-
checksum: 558f3e644f151e40f0466174b7dab9d6d0d16648e7d15d692c616007a2d410e9f88c74b40c6ab60a4a6f4474674c36359a6451704b3eb9496c86d77a136d5f79
2194+
"pico-ms@npm:^2.0.1":
2195+
version: 2.0.1
2196+
resolution: "pico-ms@npm:2.0.1"
2197+
checksum: 576414fbef05d9ef1d04b49849b623ed69e0e4e8dc4534614917f8d110315dbdc22201fa24e582a4dd34610ecc309f6e259b8889740314087624360fdabde5ce
21992198
languageName: node
22002199
linkType: hard
22012200

@@ -2400,10 +2399,10 @@ __metadata:
24002399
languageName: node
24012400
linkType: hard
24022401

2403-
"ricks-bricks@npm:^1.1.0":
2404-
version: 1.1.0
2405-
resolution: "ricks-bricks@npm:1.1.0"
2406-
checksum: 4b73362bc081384479ebe7f88bd705e05abc84c2010b90924a10ce5d5fb4539cf9d24cb3ffc2d137627a39a6658be6f872b95962069ea90622d90470685ba8f4
2402+
"ricks-bricks@npm:^2.0.0":
2403+
version: 2.0.0
2404+
resolution: "ricks-bricks@npm:2.0.0"
2405+
checksum: ae55b7c5ea2633340b814eaa622ea0b2984f2e9171eb156c92bd9f4ff3cbaec17c01fe28f6f4c82be65382e785b652d7b7c3f611ac7298079897a363b052f62b
24072406
languageName: node
24082407
linkType: hard
24092408

@@ -2461,15 +2460,15 @@ __metadata:
24612460
version: 0.0.0-use.local
24622461
resolution: "scrud@workspace:."
24632462
dependencies:
2464-
axios: ^1.9.0
2463+
axios: ^1.10.0
24652464
dotenv: ^16.5.0
2466-
get-scrud: ^4.1.0
2465+
get-scrud: ^5.1.0
24672466
hash-wasm: ^4.12.0
24682467
husky: ^9.1.7
24692468
import-fresh: ^3.3.1
24702469
jsonwebtoken: ~9.0.2
24712470
mvt: ^4.2.1
2472-
pg: ~8.16.0
2471+
pg: ~8.16.2
24732472
standard: ^17.1.2
24742473
tiny-params: ~4.0.0
24752474
languageName: unknown

0 commit comments

Comments
 (0)