Skip to content

Commit 6f44da7

Browse files
author
Jake Champion
committed
refactor: pull out duplicated logic for validatiing backend timeout properties into a reusable function
1 parent 8133508 commit 6f44da7

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

c-dependencies/js-compute-runtime/builtins/backend.cpp

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,29 @@ bool Backend::set_host_override(JSContext *cx, JSObject *backend,
437437
return true;
438438
}
439439

440+
/// Timeouts for backends must be less than 2^32 milliseconds, or
441+
/// about a month and a half.
442+
bool Backend::set_timeout_slot(JSContext *cx, JSObject *backend, JS::HandleValue value,
443+
Backend::Slots slot, std::string property_name) {
444+
double native_value;
445+
if (!JS::ToNumber(cx, value, &native_value)) {
446+
return false;
447+
}
448+
int64_t timeout = std::round(native_value);
449+
if (timeout < 0) {
450+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BACKEND_TIMEOUT_NEGATIVE,
451+
property_name.c_str());
452+
return false;
453+
}
454+
if (timeout >= std::pow(2, 32)) {
455+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BACKEND_TIMEOUT_TOO_BIG,
456+
property_name.c_str());
457+
return false;
458+
}
459+
JS::SetReservedSlot(backend, slot, JS::Int32Value(timeout));
460+
return true;
461+
}
462+
440463
bool Backend::set_target(JSContext *cx, JSObject *backend, JS::HandleValue target_val) {
441464
if (target_val.isNullOrUndefined()) {
442465
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BACKEND_TARGET_NOT_SET);
@@ -591,8 +614,6 @@ bool Backend::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
591614
}
592615
}
593616

594-
/// Timeouts for backends must be less than 2^32 milliseconds, or
595-
/// about a month and a half.
596617
JS::RootedValue connectTimeout_val(cx);
597618
if (!JS_HasProperty(cx, configuration, "connectTimeout", &found)) {
598619
return false;
@@ -601,22 +622,10 @@ bool Backend::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
601622
if (!JS_GetProperty(cx, configuration, "connectTimeout", &connectTimeout_val)) {
602623
return false;
603624
}
604-
double connectTimeout;
605-
if (!JS::ToNumber(cx, connectTimeout_val, &connectTimeout)) {
625+
if (!Backend::set_timeout_slot(cx, backend, connectTimeout_val, Backend::Slots::ConnectTimeout,
626+
"connectTimeout")) {
606627
return false;
607628
}
608-
int64_t connectTimeoutInt = std::round(connectTimeout);
609-
if (connectTimeoutInt < 0) {
610-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
611-
JSMSG_BACKEND_CONNECT_TIMEOUT_NEGATIVE);
612-
return false;
613-
}
614-
if (connectTimeoutInt >= std::pow(2, 32)) {
615-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
616-
JSMSG_BACKEND_CONNECT_TIMEOUT_TOO_BIG);
617-
return false;
618-
}
619-
JS::SetReservedSlot(backend, Backend::Slots::ConnectTimeout, JS::Int32Value(connectTimeoutInt));
620629
}
621630

622631
/// Timeouts for backends must be less than 2^32 milliseconds, or
@@ -629,23 +638,10 @@ bool Backend::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
629638
if (!JS_GetProperty(cx, configuration, "firstByteTimeout", &firstByteTimeout_val)) {
630639
return false;
631640
}
632-
double firstByteTimeout;
633-
if (!JS::ToNumber(cx, firstByteTimeout_val, &firstByteTimeout)) {
634-
return false;
635-
}
636-
int64_t firstByteTimeoutInt = std::round(firstByteTimeout);
637-
if (firstByteTimeoutInt < 0) {
638-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
639-
JSMSG_BACKEND_FIRST_BYTE_TIMEOUT_NEGATIVE);
640-
return false;
641-
}
642-
if (firstByteTimeoutInt >= std::pow(2, 32)) {
643-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
644-
JSMSG_BACKEND_FIRST_BYTE_TIMEOUT_TOO_BIG);
641+
if (!Backend::set_timeout_slot(cx, backend, firstByteTimeout_val,
642+
Backend::Slots::FirstByteTimeout, "firstByteTimeout")) {
645643
return false;
646644
}
647-
JS::SetReservedSlot(backend, Backend::Slots::FirstByteTimeout,
648-
JS::Int32Value(firstByteTimeoutInt));
649645
}
650646

651647
/// Timeouts for backends must be less than 2^32 milliseconds, or
@@ -658,23 +654,10 @@ bool Backend::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
658654
if (!JS_GetProperty(cx, configuration, "betweenBytesTimeout", &betweenBytesTimeout_val)) {
659655
return false;
660656
}
661-
double betweenBytesTimeout;
662-
if (!JS::ToNumber(cx, betweenBytesTimeout_val, &betweenBytesTimeout)) {
663-
return false;
664-
}
665-
int64_t betweenBytesTimeoutInt = std::round(betweenBytesTimeout);
666-
if (betweenBytesTimeoutInt < 0) {
667-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
668-
JSMSG_BACKEND_BETWEEN_BYTES_TIMEOUT_NEGATIVE);
669-
return false;
670-
}
671-
if (betweenBytesTimeoutInt >= std::pow(2, 32)) {
672-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
673-
JSMSG_BACKEND_BETWEEN_BYTES_TIMEOUT_TOO_BIG);
657+
if (!Backend::set_timeout_slot(cx, backend, betweenBytesTimeout_val,
658+
Backend::Slots::BetweenBytesTimeout, "betweenBytesTimeout")) {
674659
return false;
675660
}
676-
JS::SetReservedSlot(backend, Backend::Slots::BetweenBytesTimeout,
677-
JS::Int32Value(betweenBytesTimeoutInt));
678661
}
679662

680663
/// Has to be either: 1; 1.1; 1.2; 1.3;

c-dependencies/js-compute-runtime/builtins/backend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Backend : public BuiltinImpl<Backend> {
3636
static JS::Result<mozilla::Ok> register_dynamic_backend(JSContext *cx, JS::HandleObject request);
3737
static JSObject *create(JSContext *cx, JS::HandleObject request);
3838
static bool set_target(JSContext *cx, JSObject *backend, JS::HandleValue target_val);
39+
static bool set_timeout_slot(JSContext *cx, JSObject *backend, JS::HandleValue value,
40+
Backend::Slots slot, std::string property_name);
3941
static bool set_host_override(JSContext *cx, JSObject *backend, JS::HandleValue hostOverride_val);
4042
static bool set_name(JSContext *cx, JSObject *backend, JS::HandleValue name_val);
4143
static bool toString(JSContext *cx, unsigned argc, JS::Value *vp);

c-dependencies/js-compute-runtime/error-numbers.msg

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ MSG_DEF(JSMSG_BACKEND_HOST_OVERRIDE_EMPTY, 0, JSEXN_TYPEERR,
7070
MSG_DEF(JSMSG_BACKEND_CERTIFICATE_HOSTNAME_EMPTY, 0, JSEXN_TYPEERR, "Backend constructor: certificateHostname can not be an empty string")
7171
MSG_DEF(JSMSG_BACKEND_SNI_HOSTNAME_EMPTY, 0, JSEXN_TYPEERR, "Backend constructor: sniHostname can not be an empty string")
7272
MSG_DEF(JSMSG_BACKEND_CA_CERTIFICATE_EMPTY, 0, JSEXN_TYPEERR, "Backend constructor: caCertificate can not be an empty string")
73-
MSG_DEF(JSMSG_BACKEND_CONNECT_TIMEOUT_NEGATIVE, 0, JSEXN_RANGEERR, "Backend constructor: connectTimeout can not be a negative number")
74-
MSG_DEF(JSMSG_BACKEND_CONNECT_TIMEOUT_TOO_BIG, 0, JSEXN_RANGEERR, "Backend constructor: connectTimeout must be less than 2^32")
75-
MSG_DEF(JSMSG_BACKEND_FIRST_BYTE_TIMEOUT_NEGATIVE, 0, JSEXN_RANGEERR, "Backend constructor: firstByteTimeout can not be a negative number")
76-
MSG_DEF(JSMSG_BACKEND_FIRST_BYTE_TIMEOUT_TOO_BIG, 0, JSEXN_RANGEERR, "Backend constructor: firstByteTimeout must be less than 2^32")
77-
MSG_DEF(JSMSG_BACKEND_BETWEEN_BYTES_TIMEOUT_NEGATIVE, 0, JSEXN_RANGEERR, "Backend constructor: betweenBytesTimeout can not be a negative number")
78-
MSG_DEF(JSMSG_BACKEND_BETWEEN_BYTES_TIMEOUT_TOO_BIG, 0, JSEXN_RANGEERR, "Backend constructor: betweenBytesTimeout must be less than 2^32")
73+
MSG_DEF(JSMSG_BACKEND_TIMEOUT_NEGATIVE, 1, JSEXN_RANGEERR, "Backend constructor: {0} can not be a negative number")
74+
MSG_DEF(JSMSG_BACKEND_TIMEOUT_TOO_BIG, 1, JSEXN_RANGEERR, "Backend constructor: {0} must be less than 2^32")
7975
MSG_DEF(JSMSG_BACKEND_TLS_MIN_INVALID, 0, JSEXN_RANGEERR, "Backend constructor: tlsMinVersion must be either 1, 1.1, 1.2, or 1.3")
8076
MSG_DEF(JSMSG_BACKEND_TLS_MAX_INVALID, 0, JSEXN_RANGEERR, "Backend constructor: tlsMaxVersion must be either 1, 1.1, 1.2, or 1.3")
8177
MSG_DEF(JSMSG_BACKEND_TLS_MIN_GREATER_THAN_TLS_MAX, 0, JSEXN_RANGEERR, "Backend constructor: tlsMinVersion must be less than or equal to tlsMaxVersion")

0 commit comments

Comments
 (0)