@@ -79,29 +79,29 @@ napi_status aws_napi_http_message_bind(napi_env env, napi_value exports) {
79
79
80
80
struct http_request_binding {
81
81
struct aws_http_message * native ;
82
- struct aws_allocator * allocator ;
83
82
84
83
napi_ref node_headers ;
85
84
};
86
85
87
- /* Need a special finalizer to avoid releasing a request object we don't own */
88
- static void s_napi_wrapped_http_request_finalize (napi_env env , void * finalize_data , void * finalize_hint ) {
89
- (void )env ;
86
+ static void s_napi_http_request_finalize (napi_env env , void * finalize_data , void * finalize_hint ) {
90
87
(void )finalize_hint ;
91
-
92
88
struct http_request_binding * binding = finalize_data ;
93
- struct aws_allocator * allocator = binding -> allocator ;
89
+ struct aws_allocator * allocator = aws_napi_get_allocator ();
90
+
91
+ if (binding -> node_headers != NULL ) {
92
+ napi_delete_reference (env , binding -> node_headers );
93
+ }
94
94
95
+ aws_http_message_release (binding -> native );
95
96
aws_mem_release (allocator , binding );
96
97
}
97
98
98
99
napi_status aws_napi_http_message_wrap (napi_env env , struct aws_http_message * message , napi_value * result ) {
99
100
100
101
struct http_request_binding * binding =
101
102
aws_mem_calloc (aws_napi_get_allocator (), 1 , sizeof (struct http_request_binding ));
102
- binding -> native = message ;
103
- binding -> allocator = aws_napi_get_allocator ();
104
- return aws_napi_wrap (env , & s_request_class_info , binding , s_napi_wrapped_http_request_finalize , result );
103
+ binding -> native = aws_http_message_acquire (message );
104
+ return aws_napi_wrap (env , & s_request_class_info , binding , s_napi_http_request_finalize , result );
105
105
}
106
106
107
107
struct aws_http_message * aws_napi_http_message_unwrap (napi_env env , napi_value js_object ) {
@@ -115,20 +115,6 @@ struct aws_http_message *aws_napi_http_message_unwrap(napi_env env, napi_value j
115
115
* Constructor
116
116
**********************************************************************************************************************/
117
117
118
- static void s_napi_http_request_finalize (napi_env env , void * finalize_data , void * finalize_hint ) {
119
- (void )env ;
120
-
121
- struct http_request_binding * binding = finalize_data ;
122
- struct aws_allocator * allocator = finalize_hint ;
123
-
124
- if (binding -> node_headers != NULL ) {
125
- napi_delete_reference (env , binding -> node_headers );
126
- }
127
-
128
- aws_http_message_destroy (binding -> native );
129
- aws_mem_release (allocator , binding );
130
- }
131
-
132
118
static napi_value s_request_constructor (napi_env env , const struct aws_napi_callback_info * cb_info ) {
133
119
134
120
struct aws_allocator * alloc = aws_napi_get_allocator ();
@@ -167,7 +153,7 @@ static napi_value s_request_constructor(napi_env env, const struct aws_napi_call
167
153
}
168
154
169
155
napi_value node_this = cb_info -> native_this ;
170
- AWS_NAPI_CALL (env , napi_wrap (env , node_this , binding , s_napi_http_request_finalize , alloc , NULL ), {
156
+ AWS_NAPI_CALL (env , napi_wrap (env , node_this , binding , s_napi_http_request_finalize , NULL , NULL ), {
171
157
napi_throw_error (env , NULL , "Failed to wrap HttpRequest" );
172
158
goto cleanup ;
173
159
});
@@ -177,7 +163,7 @@ static napi_value s_request_constructor(napi_env env, const struct aws_napi_call
177
163
cleanup :
178
164
if (binding ) {
179
165
if (binding -> native ) {
180
- aws_http_message_destroy (binding -> native );
166
+ aws_http_message_release (binding -> native );
181
167
}
182
168
aws_mem_release (alloc , binding );
183
169
}
@@ -238,15 +224,16 @@ static napi_value s_request_headers_get(napi_env env, void *native_this) {
238
224
239
225
napi_value result = NULL ;
240
226
if (binding -> node_headers ) {
227
+ /* This HTTP request already has a reference to the Node object for the headers */
241
228
AWS_NAPI_ENSURE (env , napi_get_reference_value (env , binding -> node_headers , & result ));
242
229
} else {
230
+ /* There is no Node object for these headers.
231
+ * Create a Node object, and a reference to it, and store that reference on this HTTP request */
243
232
struct aws_http_headers * headers = aws_http_message_get_headers (binding -> native );
244
233
AWS_NAPI_ENSURE (env , aws_napi_http_headers_wrap (env , headers , & result ));
234
+ AWS_NAPI_ENSURE (env , napi_create_reference (env , result , 1 , & binding -> node_headers ));
245
235
}
246
236
247
- /* Store the value for later */
248
- AWS_NAPI_ENSURE (env , napi_create_reference (env , result , 1 , & binding -> node_headers ));
249
-
250
237
return result ;
251
238
}
252
239
0 commit comments