Skip to content

Commit 996942f

Browse files
authored
fix: dont use JSON parse if symbolicate payload is of type json (#1137)
* fix: dont use JSON parse if symbolicate payload is of type json * chore: changeset
1 parent acd69db commit 996942f

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

.changeset/strong-chefs-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack-dev-server": patch
3+
---
4+
5+
Support `application/json` type of payload for `/symbolicate` requests

packages/dev-server/src/plugins/symbolicate/sybmolicatePlugin.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
import type { FastifyInstance } from 'fastify';
1+
import type { FastifyInstance, FastifyRequest } from 'fastify';
22
import fastifyPlugin from 'fastify-plugin';
33
import type { Server } from '../../types.js';
44
import { Symbolicator } from './Symbolicator.js';
55
import type { ReactNativeStackFrame } from './types.js';
66

7+
interface SymbolicateRequestBody {
8+
stack: ReactNativeStackFrame[];
9+
}
10+
11+
function getStackFromRequestBody(request: FastifyRequest) {
12+
let body: SymbolicateRequestBody;
13+
14+
if (request.headers['content-type'] === 'application/json') {
15+
// RN >= 0.79 uses application/json
16+
body = request.body as SymbolicateRequestBody;
17+
} else {
18+
// RN < 0.79 uses text/plain
19+
body = JSON.parse(request.body as string) as SymbolicateRequestBody;
20+
}
21+
22+
return body.stack;
23+
}
24+
725
async function symbolicatePlugin(
826
instance: FastifyInstance,
927
{
@@ -15,13 +33,8 @@ async function symbolicatePlugin(
1533
const symbolicator = new Symbolicator(delegate.symbolicator);
1634

1735
instance.post('/symbolicate', async (request, reply) => {
18-
// React Native sends stack as JSON but tests content-type to text/plain, so
19-
// we cannot use JSON schema to validate the body.
20-
2136
try {
22-
const { stack } = JSON.parse(request.body as string) as {
23-
stack: ReactNativeStackFrame[];
24-
};
37+
const stack = getStackFromRequestBody(request);
2538
const platform = Symbolicator.inferPlatformFromStack(stack);
2639
if (!platform) {
2740
request.log.debug({ msg: 'Received stack', stack });

0 commit comments

Comments
 (0)