@@ -13,6 +13,7 @@ import { SemanticInternalAttributes } from "../semanticInternalAttributes.js";
1313import { TriggerTracer } from "../tracer.js" ;
1414import { zodfetch } from "../zodfetch.js" ;
1515import { flattenAttributes } from "./flattenAttributes.js" ;
16+ import superjson from "../imports/superjson.js" ;
1617
1718export type IOPacket = {
1819 data ?: string | undefined ;
@@ -32,9 +33,7 @@ export async function parsePacket(value: IOPacket, options?: ParsePacketOptions)
3233 case "application/json" :
3334 return JSON . parse ( value . data , makeSafeReviver ( options ) ) ;
3435 case "application/super+json" :
35- const { parse } = await loadSuperJSON ( ) ;
36-
37- return parse ( value . data ) ;
36+ return superjson . parse ( value . data ) ;
3837 case "text/plain" :
3938 return value . data ;
4039 case "application/store" :
@@ -58,11 +57,9 @@ export async function parsePacketAsJson(
5857 case "application/json" :
5958 return JSON . parse ( value . data , makeSafeReviver ( options ) ) ;
6059 case "application/super+json" :
61- const { parse, serialize } = await loadSuperJSON ( ) ;
62-
63- const superJsonResult = parse ( value . data ) ;
60+ const superJsonResult = superjson . parse ( value . data ) ;
6461
65- const { json } = serialize ( superJsonResult ) ;
62+ const { json } = superjson . serialize ( superJsonResult ) ;
6663
6764 return json ;
6865 case "text/plain" :
@@ -95,8 +92,7 @@ export async function stringifyIO(value: any): Promise<IOPacket> {
9592 }
9693
9794 try {
98- const { stringify } = await loadSuperJSON ( ) ;
99- const data = stringify ( value ) ;
95+ const data = superjson . stringify ( value ) ;
10096
10197 return { data, dataType : "application/super+json" } ;
10298 } catch {
@@ -302,14 +298,12 @@ export async function createPacketAttributes(
302298 [ dataTypeKey ] : packet . dataType ,
303299 } ;
304300 case "application/super+json" :
305- const { parse } = await loadSuperJSON ( ) ;
306-
307301 if ( typeof packet . data === "undefined" || packet . data === null ) {
308302 return ;
309303 }
310304
311305 try {
312- const parsed = parse ( packet . data ) as any ;
306+ const parsed = superjson . parse ( packet . data ) as any ;
313307 const jsonified = JSON . parse ( JSON . stringify ( parsed , makeSafeReplacer ( ) ) ) ;
314308
315309 const result = {
@@ -358,9 +352,7 @@ export async function createPacketAttributesAsJson(
358352 ) ;
359353 }
360354 case "application/super+json" : {
361- const { deserialize } = await loadSuperJSON ( ) ;
362-
363- const deserialized = deserialize ( data ) as any ;
355+ const deserialized = superjson . deserialize ( data ) as any ;
364356 const jsonify = safeJsonParse ( JSON . stringify ( deserialized , makeSafeReplacer ( ) ) ) ;
365357
366358 return imposeAttributeLimits (
@@ -390,18 +382,16 @@ export async function prettyPrintPacket(
390382 rawData = safeJsonParse ( rawData ) ;
391383 }
392384
393- const { deserialize } = await loadSuperJSON ( ) ;
394-
395385 const hasCircularReferences = rawData && rawData . meta && hasCircularReference ( rawData . meta ) ;
396386
397387 if ( hasCircularReferences ) {
398- return await prettyPrintPacket ( deserialize ( rawData ) , "application/json" , {
388+ return await prettyPrintPacket ( superjson . deserialize ( rawData ) , "application/json" , {
399389 ...options ,
400390 cloneReferences : false ,
401391 } ) ;
402392 }
403393
404- return await prettyPrintPacket ( deserialize ( rawData ) , "application/json" , {
394+ return await prettyPrintPacket ( superjson . deserialize ( rawData ) , "application/json" , {
405395 ...options ,
406396 cloneReferences : true ,
407397 } ) ;
@@ -512,21 +502,6 @@ function getPacketExtension(outputType: string): string {
512502 }
513503}
514504
515- async function loadSuperJSON ( ) {
516- const superjson = await import ( "superjson" ) ;
517-
518- superjson . registerCustom < Buffer , number [ ] > (
519- {
520- isApplicable : ( v ) : v is Buffer => typeof Buffer === "function" && Buffer . isBuffer ( v ) ,
521- serialize : ( v ) => [ ...v ] ,
522- deserialize : ( v ) => Buffer . from ( v ) ,
523- } ,
524- "buffer"
525- ) ;
526-
527- return superjson ;
528- }
529-
530505function safeJsonParse ( value : string ) : any {
531506 try {
532507 return JSON . parse ( value ) ;
@@ -554,7 +529,6 @@ function safeJsonParse(value: string): any {
554529 * @throws {Error } If the newPayload is not valid JSON
555530 */
556531export async function replaceSuperJsonPayload ( original : string , newPayload : string ) {
557- const superjson = await loadSuperJSON ( ) ;
558532 const originalObject = superjson . parse ( original ) ;
559533 const newPayloadObject = JSON . parse ( newPayload ) ;
560534 const { meta } = superjson . serialize ( originalObject ) ;
0 commit comments