1- import type { FastifyInstance } from 'fastify' ;
1+ import type { FastifyInstance , FastifyRequest } from 'fastify' ;
22import fastifyPlugin from 'fastify-plugin' ;
33import type { Server } from '../../types.js' ;
44import { Symbolicator } from './Symbolicator.js' ;
55import 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+
725async 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