@@ -23,8 +23,9 @@ import { getAgentClaim, parseDocumentID, getAllVersions, getAllDescendants } fro
2323const deleteObj = async function ( req , res , next ) {
2424 let id
2525 let err = { message : `` }
26+ const safeBody = structuredClone ( req . body )
2627 try {
27- id = req . params [ "_id" ] ?? parseDocumentID ( JSON . parse ( JSON . stringify ( req . body ) ) [ "@id" ] ) ?? parseDocumentID ( JSON . parse ( JSON . stringify ( req . body ) ) [ "id" ] )
28+ id = req . params [ "_id" ] ?? parseDocumentID ( safeBody ?. [ "@id" ] ) ?? parseDocumentID ( safeBody ?. [ "id" ] )
2829 } catch ( error ) {
2930 return next ( utils . createExpressError ( error ) )
3031 }
@@ -37,7 +38,7 @@ const deleteObj = async function(req, res, next) {
3738 return next ( utils . createExpressError ( error ) )
3839 }
3940 if ( null !== originalObject ) {
40- let safe_original = JSON . parse ( JSON . stringify ( originalObject ) )
41+ let safe_original = structuredClone ( originalObject )
4142 if ( utils . isDeleted ( safe_original ) ) {
4243 err = Object . assign ( err , {
4344 message : `The object you are trying to delete is already deleted. ${ err . message } ` ,
@@ -61,7 +62,7 @@ const deleteObj = async function(req, res, next) {
6162 }
6263 let preserveID = safe_original [ "@id" ]
6364 let deletedFlag = { } //The __deleted flag is a JSONObject
64- deletedFlag [ "object" ] = JSON . parse ( JSON . stringify ( originalObject ) )
65+ deletedFlag [ "object" ] = structuredClone ( originalObject )
6566 deletedFlag [ "deletor" ] = agentRequestingDelete
6667 deletedFlag [ "time" ] = new Date ( Date . now ( ) ) . toISOString ( ) . replace ( "Z" , "" )
6768 let deletedObject = {
@@ -125,7 +126,7 @@ async function healHistoryTree(obj) {
125126 const nextIdForQuery = parseDocumentID ( nextID )
126127 const objToUpdate = await db . findOne ( { "$or" :[ { "_id" : nextIdForQuery } , { "__rerum.slug" : nextIdForQuery } ] } )
127128 if ( null !== objToUpdate ) {
128- let fixHistory = JSON . parse ( JSON . stringify ( objToUpdate ) )
129+ let fixHistory = structuredClone ( objToUpdate )
129130 if ( objToDeleteisRoot ) {
130131 //This means this next object must become root.
131132 //Strictly, all history trees must have num(root) > 0.
@@ -158,7 +159,7 @@ async function healHistoryTree(obj) {
158159 let previousIdForQuery = parseDocumentID ( previous_id )
159160 const objToUpdate2 = await db . findOne ( { "$or" :[ { "_id" : previousIdForQuery } , { "__rerum.slug" : previousIdForQuery } ] } )
160161 if ( null !== objToUpdate2 ) {
161- let fixHistory2 = JSON . parse ( JSON . stringify ( objToUpdate2 ) )
162+ let fixHistory2 = structuredClone ( objToUpdate2 )
162163 let origNextArray = fixHistory2 [ "__rerum" ] [ "history" ] [ "next" ]
163164 let newNextArray = [ ...origNextArray ]
164165 newNextArray = newNextArray . filter ( id => id !== obj [ "@id" ] )
@@ -197,7 +198,7 @@ async function newTreePrime(obj) {
197198 // fail silently
198199 }
199200 for ( const d of descendants ) {
200- let objWithUpdate = JSON . parse ( JSON . stringify ( d ) )
201+ let objWithUpdate = structuredClone ( d )
201202 objWithUpdate [ "__rerum" ] [ "history" ] [ "prime" ] = primeID
202203 let result = await db . replaceOne ( { "_id" : d [ "_id" ] } , objWithUpdate )
203204 if ( result . modifiedCount === 0 ) {
0 commit comments