forked from wilsonzlin/minify-html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.c
More file actions
284 lines (236 loc) · 8.67 KB
/
binding.c
File metadata and controls
284 lines (236 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAPI_VERSION 1
#include <node_api.h>
typedef struct Cfg Cfg;
#include <minify_html_ffi.h>
static inline void* assert_malloc(size_t bytes) {
void* ptr = malloc(bytes);
if (ptr == NULL) {
fprintf(stderr, "[Node.js minify-html] Internal assertion error: failed to allocate memory\n");
exit(67);
}
return ptr;
}
static inline void assert_ok(napi_status status) {
if (status != napi_ok) {
fprintf(stderr, "[Node.js minify-html] Internal assertion error: N-API status not OK\n");
exit(67);
}
}
static inline napi_value get_undefined(napi_env env) {
napi_value undefined;
assert_ok(napi_get_undefined(env, &undefined));
return undefined;
}
typedef napi_status (*napi_is_pred)(napi_env env, napi_value value, bool* result);
static inline bool napi_is(napi_env env, napi_value value, napi_is_pred pred) {
bool res;
assert_ok(pred(env, value, &res));
return res;
}
typedef struct js_min_buf_metadata {
napi_ref src_buf_ref;
} js_min_buf_metadata;
void js_cfg_finalizer(napi_env env, void* finalize_data, void* _finalize_hint) {
ffi_drop_cfg((Cfg const*) finalize_data);
}
void js_min_buf_finalizer(napi_env env, void* _finalize_data, void* finalize_hint) {
js_min_buf_metadata* metadata = (js_min_buf_metadata*) finalize_hint;
assert_ok(napi_delete_reference(env, metadata->src_buf_ref));
free(metadata);
}
void js_copy_min_buf_finalizer(napi_env env, void* _finalize_data, void* finalize_hint) {
free(finalize_hint);
}
static inline void throw_js_ffi_error(napi_env env, ffi_error const* min_err) {
napi_value js_min_err_msg;
assert_ok(napi_create_string_utf8(env, (char const*) min_err->message, min_err->message_len, &js_min_err_msg));
napi_value js_min_err;
assert_ok(napi_create_error(env, NULL, js_min_err_msg, &js_min_err));
napi_value js_min_err_pos;
assert_ok(napi_create_int64(env, min_err->position, &js_min_err_pos));
assert_ok(napi_set_named_property(env, js_min_err, "position", js_min_err_pos));
assert_ok(napi_throw(env, js_min_err));
}
napi_value node_method_create_configuration(napi_env env, napi_callback_info info) {
napi_value undefined = get_undefined(env);
size_t argc = 1;
napi_value argv[1];
napi_value _this;
void* _data;
// Get the arguments.
if (napi_get_cb_info(env, info, &argc, argv, &_this, &_data) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to get callback info"));
return undefined;
}
napi_value obj_arg = argv[0];
// Get `minifyJs` property.
bool minify_js = false;
napi_value minify_js_value;
if (napi_get_named_property(env, obj_arg, "minifyJs", &minify_js_value) == napi_ok) {
// It's OK if this fails.
napi_get_value_bool(env, minify_js_value, &minify_js);
}
// Get `minifyCss` property.
bool minify_css = false;
napi_value minify_css_value;
if (napi_get_named_property(env, obj_arg, "minifyCss", &minify_css_value) == napi_ok) {
// It's OK if this fails.
napi_get_value_bool(env, minify_css_value, &minify_css);
}
Cfg const* cfg = ffi_create_cfg(minify_js, minify_css);
napi_value js_cfg;
if (napi_create_external(env, (void*) cfg, js_cfg_finalizer, NULL, &js_cfg) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to create return value"));
return undefined;
}
return js_cfg;
}
napi_value node_method_minify_in_place(napi_env env, napi_callback_info info) {
napi_value undefined = get_undefined(env);
napi_value min_buf_rv = undefined;
bool buffer_arg_ref_set = false;
napi_ref buffer_arg_ref;
js_min_buf_metadata* min_buf_meta = NULL;
ffi_error const* min_err = NULL;
size_t argc = 2;
napi_value argv[2];
napi_value _this;
void* _data;
// Get the arguments.
if (napi_get_cb_info(env, info, &argc, argv, &_this, &_data) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to get callback info"));
goto rollback;
}
napi_value buffer_arg = argv[0];
napi_value js_cfg_arg = argv[1];
// Ensure buffer lives along side minified buffer (both point to same memory).
if (napi_create_reference(env, buffer_arg, 1, &buffer_arg_ref) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to create reference for source buffer"));
goto rollback;
}
buffer_arg_ref_set = true;
// Get pointer to bytes in buffer.
void* buffer_data;
size_t buffer_len;
if (napi_get_buffer_info(env, buffer_arg, &buffer_data, &buffer_len) != napi_ok || buffer_data == NULL) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to read source buffer"));
goto rollback;
}
// Get Cfg.
void* cfg_raw;
if (napi_get_value_external(env, js_cfg_arg, &cfg_raw) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to get configuration"));
goto rollback;
}
Cfg const* cfg = (Cfg const*) cfg_raw;
// Run minifier in place.
size_t min_len;
min_err = ffi_in_place(buffer_data, buffer_len, cfg, &min_len);
if (min_err != NULL) {
throw_js_ffi_error(env, min_err);
goto rollback;
}
// Create minified buffer with underlying source memory but minified length.
min_buf_meta = assert_malloc(sizeof(js_min_buf_metadata));
min_buf_meta->src_buf_ref = buffer_arg_ref;
if (napi_create_external_buffer(env, min_len, buffer_data, js_min_buf_finalizer, min_buf_meta, &min_buf_rv) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to create minified buffer"));
goto rollback;
}
goto cleanup;
rollback:
if (buffer_arg_ref_set) {
// Release source buffer.
assert_ok(napi_delete_reference(env, buffer_arg_ref));
}
free(min_buf_meta);
cleanup:
if (min_err != NULL) {
ffi_drop_ffi_error(min_err);
}
return min_buf_rv;
}
napi_value node_method_minify(napi_env env, napi_callback_info info) {
napi_value undefined = get_undefined(env);
napi_value min_buf_rv = undefined;
void* src_data_copy = NULL;
ffi_error const* min_err = NULL;
size_t argc = 2;
napi_value argv[2];
napi_value _this;
void* _data;
// Get the arguments.
if (napi_get_cb_info(env, info, &argc, argv, &_this, &_data) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to get callback info"));
goto rollback;
}
napi_value src_arg = argv[0];
napi_value js_cfg_arg = argv[1];
size_t src_data_len;
if (napi_is(env, src_arg, napi_is_buffer)) {
// Get pointer to bytes in buffer.
void* buffer_data;
if (napi_get_buffer_info(env, src_arg, &buffer_data, &src_data_len) != napi_ok || buffer_data == NULL) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to read source buffer"));
goto rollback;
}
src_data_copy = assert_malloc(src_data_len);
memcpy(src_data_copy, buffer_data, src_data_len);
} else {
// Assume string.
if (napi_get_value_string_utf8(env, src_arg, NULL, 0, &src_data_len) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to read source string"));
goto rollback;
}
src_data_copy = assert_malloc(src_data_len + 1);
size_t bytes_copied;
if (napi_get_value_string_utf8(env, src_arg, src_data_copy, src_data_len + 1, &bytes_copied) != napi_ok
|| bytes_copied != src_data_len) {
assert_ok(napi_throw_error(env, NULL, "Failed to copy source string"));
goto rollback;
}
}
// Get Cfg.
void* cfg_raw;
if (napi_get_value_external(env, js_cfg_arg, &cfg_raw) != napi_ok) {
assert_ok(napi_throw_type_error(env, NULL, "Failed to get configuration"));
goto rollback;
}
Cfg const* cfg = (Cfg const*) cfg_raw;
// Run minifier in place.
size_t min_len;
min_err = ffi_in_place(src_data_copy, src_data_len, cfg, &min_len);
if (min_err != NULL) {
throw_js_ffi_error(env, min_err);
goto rollback;
}
// Create minified buffer with copied memory.
if (napi_create_external_buffer(env, min_len, src_data_copy, js_copy_min_buf_finalizer, src_data_copy, &min_buf_rv) != napi_ok) {
assert_ok(napi_throw_error(env, NULL, "Failed to create minified buffer"));
goto rollback;
}
goto cleanup;
rollback:
free(src_data_copy);
cleanup:
if (min_err != NULL) {
ffi_drop_ffi_error(min_err);
}
return min_buf_rv;
}
static inline void define_method(napi_env env, napi_value exports, char const* name, napi_callback cb) {
napi_value js_fn;
assert_ok(napi_create_function(env, name, NAPI_AUTO_LENGTH, cb, NULL, &js_fn));
assert_ok(napi_set_named_property(env, exports, name, js_fn));
}
napi_value node_module_init(napi_env env, napi_value exports) {
define_method(env, exports, "createConfiguration", node_method_create_configuration);
define_method(env, exports, "minify", node_method_minify);
define_method(env, exports, "minifyInPlace", node_method_minify_in_place);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, node_module_init)