Skip to content

Commit 368a93a

Browse files
committed
Fix strict json error message on Node.js 19+
closes #475
1 parent 0385872 commit 368a93a

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- Node.js 16.x
3232
- Node.js 17.x
3333
- Node.js 18.x
34+
- Node.js 19.x
3435

3536
include:
3637
- name: Node.js 0.8
@@ -113,6 +114,9 @@ jobs:
113114
- name: Node.js 18.x
114115
node-version: "18.14"
115116

117+
- name: Node.js 19.x
118+
node-version: "19.7"
119+
116120
steps:
117121
- uses: actions/checkout@v3
118122

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix strict json error message on Node.js 19+
45
* deps: content-type@~1.0.5
56
- perf: skip value escaping when unnecessary
67
* deps: raw-body@2.5.2

lib/types/json.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ module.exports = json
3939

4040
var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
4141

42+
var JSON_SYNTAX_CHAR = '#'
43+
var JSON_SYNTAX_REGEXP = /#+/g
44+
4245
/**
4346
* Create a middleware to parse JSON bodies.
4447
*
@@ -152,15 +155,23 @@ function json (options) {
152155

153156
function createStrictSyntaxError (str, char) {
154157
var index = str.indexOf(char)
155-
var partial = index !== -1
156-
? str.substring(0, index) + '#'
157-
: ''
158+
var partial = ''
159+
160+
if (index !== -1) {
161+
partial = str.substring(0, index) + JSON_SYNTAX_CHAR
162+
163+
for (var i = index + 1; i < str.length; i++) {
164+
partial += JSON_SYNTAX_CHAR
165+
}
166+
}
158167

159168
try {
160169
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation')
161170
} catch (e) {
162171
return normalizeJsonSyntaxError(e, {
163-
message: e.message.replace('#', char),
172+
message: e.message.replace(JSON_SYNTAX_REGEXP, function (placeholder) {
173+
return str.substring(index, index + placeholder.length)
174+
}),
164175
stack: e.stack
165176
})
166177
}

test/json.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('bodyParser.json()', function () {
245245
.post('/')
246246
.set('Content-Type', 'application/json')
247247
.send('true')
248-
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)
248+
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)
249249
})
250250
})
251251

@@ -273,15 +273,15 @@ describe('bodyParser.json()', function () {
273273
.post('/')
274274
.set('Content-Type', 'application/json')
275275
.send('true')
276-
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace('#', 't'), done)
276+
.expect(400, '[entity.parse.failed] ' + parseError('#rue').replace(/#/g, 't'), done)
277277
})
278278

279279
it('should not parse primitives with leading whitespaces', function (done) {
280280
request(this.server)
281281
.post('/')
282282
.set('Content-Type', 'application/json')
283283
.send(' true')
284-
.expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace('#', 't'), done)
284+
.expect(400, '[entity.parse.failed] ' + parseError(' #rue').replace(/#/g, 't'), done)
285285
})
286286

287287
it('should allow leading whitespaces in JSON', function (done) {
@@ -299,7 +299,7 @@ describe('bodyParser.json()', function () {
299299
.set('X-Error-Property', 'stack')
300300
.send('true')
301301
.expect(400)
302-
.expect(shouldContainInBody(parseError('#rue').replace('#', 't')))
302+
.expect(shouldContainInBody(parseError('#rue').replace(/#/g, 't')))
303303
.end(done)
304304
})
305305
})

0 commit comments

Comments
 (0)