-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: preserve exact bytes of request bodies (if possible)
- Loading branch information
Showing
10 changed files
with
92 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = (data) => { | ||
try { | ||
return JSON.parse(Buffer.from(data)); | ||
} catch (e) { | ||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const test = require('tap').test; | ||
|
||
const tryJSONParse = require('../../lib/try-json-parse'); | ||
|
||
test('tryJSONParse', t => { | ||
const data = { | ||
number: '123', | ||
animals: ['dog', 'cat'], | ||
complex: { nested: 'data' }, | ||
}; | ||
|
||
const dataAsJson = JSON.stringify(data); | ||
const dataAsBuffer = Buffer.from(dataAsJson); | ||
const dataAsUint8Array = Uint8Array.from(dataAsBuffer); // primus with EJSON returns data as Uint8Arrays | ||
|
||
t.same(tryJSONParse(dataAsJson), data, 'Strings parse'); | ||
t.same(tryJSONParse(dataAsBuffer), data, 'Buffers parse'); | ||
t.same(tryJSONParse(dataAsUint8Array), data, 'Uint8Arrays parse'); | ||
t.same(tryJSONParse(undefined), {}, 'undefined parses as empty'); | ||
t.same(tryJSONParse(null), {}, 'null parses as empty'); | ||
t.same(tryJSONParse(data), {}, 'objects parse as empty'); | ||
t.same(tryJSONParse("nonsense"), {}, 'malformed strings parse as empty'); | ||
|
||
t.end(); | ||
}); |