2020-07-24 02:03:47 -04:00
|
|
|
#include <stdbool.h>
|
2021-01-06 02:02:00 -05:00
|
|
|
#include <stdio.h>
|
2020-07-24 00:20:24 -04:00
|
|
|
#include <stdlib.h>
|
2020-07-24 02:03:47 -04:00
|
|
|
#include <string.h>
|
2020-07-24 00:20:24 -04:00
|
|
|
|
2020-07-24 02:08:14 -04:00
|
|
|
#define NAPI_VERSION 1
|
2020-07-24 00:20:24 -04:00
|
|
|
#include <node_api.h>
|
|
|
|
|
|
|
|
typedef struct Cfg Cfg;
|
|
|
|
|
|
|
|
#include <minify_html_ffi.h>
|
2020-07-24 02:03:47 -04:00
|
|
|
|
|
|
|
static inline void* assert_malloc(size_t bytes) {
|
|
|
|
void* ptr = malloc(bytes);
|
|
|
|
if (ptr == NULL) {
|
2021-01-06 01:13:07 -05:00
|
|
|
fprintf(stderr, "[Node.js minify-html] Internal assertion error: failed to allocate memory\n");
|
|
|
|
exit(67);
|
2020-07-24 02:03:47 -04:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void assert_ok(napi_status status) {
|
|
|
|
if (status != napi_ok) {
|
2021-01-06 01:13:07 -05:00
|
|
|
fprintf(stderr, "[Node.js minify-html] Internal assertion error: N-API status not OK\n");
|
|
|
|
exit(67);
|
2020-07-24 02:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void js_cfg_finalizer(napi_env env, void* finalize_data, void* _finalize_hint) {
|
|
|
|
ffi_drop_cfg((Cfg const*) finalize_data);
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:33:17 -04:00
|
|
|
void js_output_buf_finalizer(napi_env env, void* _finalize_data, void* finalize_hint) {
|
|
|
|
ffi_output* metadata = (ffi_output*) finalize_hint;
|
|
|
|
ffi_drop_output(metadata);
|
2020-07-24 02:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2021-08-07 05:48:07 -04:00
|
|
|
#define GET_CFG_PROP(prop) \
|
|
|
|
bool prop = false; \
|
|
|
|
napi_value prop##_value; \
|
|
|
|
if (napi_get_named_property(env, obj_arg, #prop, &prop##_value) == napi_ok) { \
|
|
|
|
/* It's OK if this fails. */ napi_get_value_bool(env, prop##_value, &prop); \
|
2021-01-07 08:26:02 -05:00
|
|
|
}
|
|
|
|
|
2021-08-10 03:45:37 -04:00
|
|
|
GET_CFG_PROP(do_not_minify_doctype);
|
2021-08-07 11:45:25 -04:00
|
|
|
GET_CFG_PROP(ensure_spec_compliant_unquoted_attribute_values);
|
2021-08-07 05:48:07 -04:00
|
|
|
GET_CFG_PROP(keep_closing_tags);
|
|
|
|
GET_CFG_PROP(keep_comments);
|
|
|
|
GET_CFG_PROP(keep_html_and_head_opening_tags);
|
|
|
|
GET_CFG_PROP(keep_spaces_between_attributes);
|
|
|
|
GET_CFG_PROP(minify_css);
|
|
|
|
GET_CFG_PROP(minify_js);
|
|
|
|
GET_CFG_PROP(remove_bangs);
|
|
|
|
GET_CFG_PROP(remove_processing_instructions);
|
|
|
|
|
|
|
|
Cfg const* cfg = ffi_create_cfg(
|
2021-08-10 03:45:37 -04:00
|
|
|
do_not_minify_doctype,
|
2021-08-07 11:45:25 -04:00
|
|
|
ensure_spec_compliant_unquoted_attribute_values,
|
2021-08-07 05:48:07 -04:00
|
|
|
keep_closing_tags,
|
|
|
|
keep_comments,
|
|
|
|
keep_html_and_head_opening_tags,
|
|
|
|
keep_spaces_between_attributes,
|
|
|
|
minify_css,
|
|
|
|
minify_js,
|
|
|
|
remove_bangs,
|
|
|
|
remove_processing_instructions
|
|
|
|
);
|
2020-07-24 02:03:47 -04:00
|
|
|
|
|
|
|
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(napi_env env, napi_callback_info info) {
|
|
|
|
napi_value undefined = get_undefined(env);
|
2021-01-06 02:02:00 -05:00
|
|
|
napi_value min_buf_rv = undefined;
|
2020-07-24 02:03:47 -04:00
|
|
|
|
|
|
|
void* src_data_copy = 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];
|
|
|
|
|
2021-08-07 21:29:31 -04:00
|
|
|
void* src_data;
|
2020-07-24 02:03:47 -04:00
|
|
|
size_t src_data_len;
|
|
|
|
if (napi_is(env, src_arg, napi_is_buffer)) {
|
|
|
|
// Get pointer to bytes in buffer.
|
2021-08-07 21:29:31 -04:00
|
|
|
if (napi_get_buffer_info(env, src_arg, &src_data, &src_data_len) != napi_ok || src_data == NULL) {
|
2020-07-24 02:03:47 -04:00
|
|
|
assert_ok(napi_throw_type_error(env, NULL, "Failed to read source buffer"));
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
2021-08-07 21:29:31 -04:00
|
|
|
src_data = src_data_copy;
|
2020-07-24 02:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2021-08-07 10:33:17 -04:00
|
|
|
// Run minifier.
|
2021-08-07 21:29:31 -04:00
|
|
|
ffi_output const* output = ffi_minify(src_data, src_data_len, cfg);
|
2020-07-24 02:03:47 -04:00
|
|
|
|
|
|
|
// Create minified buffer with copied memory.
|
2021-08-07 10:33:17 -04:00
|
|
|
if (napi_create_external_buffer(env, output->len, output->data, js_output_buf_finalizer, (void*) output, &min_buf_rv) != napi_ok) {
|
2020-07-24 02:03:47 -04:00
|
|
|
assert_ok(napi_throw_error(env, NULL, "Failed to create minified buffer"));
|
|
|
|
goto rollback;
|
|
|
|
}
|
|
|
|
|
2020-07-24 23:40:37 -04:00
|
|
|
goto cleanup;
|
2020-07-24 02:03:47 -04:00
|
|
|
|
|
|
|
rollback:
|
2020-07-24 23:40:37 -04:00
|
|
|
|
|
|
|
cleanup:
|
2021-08-07 21:29:31 -04:00
|
|
|
free(src_data_copy);
|
2020-07-24 23:40:37 -04:00
|
|
|
return min_buf_rv;
|
2020-07-24 02:03:47 -04:00
|
|
|
}
|
|
|
|
|
2020-07-24 02:29:25 -04:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-07-24 02:03:47 -04:00
|
|
|
napi_value node_module_init(napi_env env, napi_value exports) {
|
2020-07-24 02:29:25 -04:00
|
|
|
define_method(env, exports, "createConfiguration", node_method_create_configuration);
|
|
|
|
define_method(env, exports, "minify", node_method_minify);
|
2020-07-24 02:03:47 -04:00
|
|
|
return exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, node_module_init)
|