@@ -32,12 +32,15 @@ import {
32
32
} from './ffi'
33
33
import { getNativeAnoncreds } from './library'
34
34
35
- function handleReturnPointer < Return > ( returnValue : Buffer ) : Return {
35
+ function handleReturnPointer < Return > ( returnValue : Buffer , cleanupCallback ?: ( buffer : Buffer ) => void ) : Return {
36
36
if ( returnValue . address ( ) === 0 ) {
37
37
throw AnoncredsError . customError ( { message : 'Unexpected null pointer' } )
38
38
}
39
39
40
- return returnValue . deref ( ) as Return
40
+ const ret = returnValue . deref ( ) as Return
41
+ if ( cleanupCallback ) cleanupCallback ( returnValue )
42
+
43
+ return ret
41
44
}
42
45
43
46
export class NodeJSAnoncreds implements Anoncreds {
@@ -60,7 +63,7 @@ export class NodeJSAnoncreds implements Anoncreds {
60
63
this . nativeAnoncreds . anoncreds_generate_nonce ( ret )
61
64
this . handleError ( )
62
65
63
- return handleReturnPointer < string > ( ret )
66
+ return handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
64
67
}
65
68
66
69
public createSchema ( options : {
@@ -86,7 +89,7 @@ export class NodeJSAnoncreds implements Anoncreds {
86
89
this . nativeAnoncreds . anoncreds_revocation_registry_definition_get_attribute ( objectHandle , name , ret )
87
90
this . handleError ( )
88
91
89
- return handleReturnPointer < string > ( ret )
92
+ return handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
90
93
}
91
94
92
95
public credentialGetAttribute ( options : { objectHandle : ObjectHandle ; name : string } ) {
@@ -96,7 +99,7 @@ export class NodeJSAnoncreds implements Anoncreds {
96
99
this . nativeAnoncreds . anoncreds_credential_get_attribute ( objectHandle , name , ret )
97
100
this . handleError ( )
98
101
99
- return handleReturnPointer < string > ( ret )
102
+ return handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
100
103
}
101
104
102
105
public createCredentialDefinition ( options : {
@@ -215,7 +218,7 @@ export class NodeJSAnoncreds implements Anoncreds {
215
218
this . nativeAnoncreds . anoncreds_encode_credential_attributes ( attributeRawValues , ret )
216
219
this . handleError ( )
217
220
218
- const result = handleReturnPointer < string > ( ret )
221
+ const result = handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
219
222
220
223
return result . split ( ',' )
221
224
}
@@ -296,7 +299,7 @@ export class NodeJSAnoncreds implements Anoncreds {
296
299
this . nativeAnoncreds . anoncreds_create_link_secret ( ret )
297
300
this . handleError ( )
298
301
299
- return handleReturnPointer < string > ( ret )
302
+ return handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
300
303
}
301
304
302
305
public createPresentation ( options : {
@@ -639,7 +642,7 @@ export class NodeJSAnoncreds implements Anoncreds {
639
642
this . nativeAnoncreds . anoncreds_get_current_error ( ret )
640
643
this . handleError ( )
641
644
642
- return handleReturnPointer < string > ( ret )
645
+ return handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
643
646
}
644
647
645
648
private objectFromJson ( method : ( byteBuffer : Buffer , ret : Buffer ) => unknown , options : { json : string } ) {
@@ -721,9 +724,13 @@ export class NodeJSAnoncreds implements Anoncreds {
721
724
this . handleError ( )
722
725
723
726
const returnValue = handleReturnPointer < { data : Buffer ; len : number } > ( ret )
724
- const output = new Uint8Array ( byteBufferToBuffer ( returnValue ) )
727
+ const jsonAsArray = new Uint8Array ( byteBufferToBuffer ( returnValue ) )
728
+
729
+ const output = new TextDecoder ( ) . decode ( jsonAsArray )
730
+
731
+ this . nativeAnoncreds . anoncreds_buffer_free ( returnValue . data )
725
732
726
- return new TextDecoder ( ) . decode ( output )
733
+ return output
727
734
}
728
735
729
736
public getTypeName ( options : { objectHandle : ObjectHandle } ) {
@@ -734,7 +741,7 @@ export class NodeJSAnoncreds implements Anoncreds {
734
741
this . nativeAnoncreds . anoncreds_object_get_type_name ( objectHandle , ret )
735
742
this . handleError ( )
736
743
737
- return handleReturnPointer < string > ( ret )
744
+ return handleReturnPointer < string > ( ret , this . nativeAnoncreds . anoncreds_string_free )
738
745
}
739
746
740
747
public objectFree ( options : { objectHandle : ObjectHandle } ) {
0 commit comments