Skip to content

Commit 457eabe

Browse files
Jake ChampionJakeChampion
Jake Champion
authored andcommitted
feat: implement Request.prototype.setCacheKey
This methods is used to set the cache key that will be used when attempting to satisfy this request from a cached response. It is the equivalent of the VCL property `req.hash`.
1 parent f544714 commit 457eabe

File tree

6 files changed

+717
-1
lines changed

6 files changed

+717
-1
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <vector>
1010

1111
#include "js-compute-builtins.h"
12+
#include "picosha2.h"
1213
#include "rust-url/rust-url.h"
1314

1415
#include "js/Array.h"
@@ -1128,6 +1129,34 @@ JSString *method(JSContext *cx, HandleObject obj) {
11281129
return JS::GetReservedSlot(obj, Slots::Method).toString();
11291130
}
11301131

1132+
bool set_cache_key(JSContext *cx, HandleObject self, HandleValue cache_key_val) {
1133+
MOZ_ASSERT(is_instance(self));
1134+
size_t key_len;
1135+
// Convert the key argument into a String following https://tc39.es/ecma262/#sec-tostring
1136+
JS::UniqueChars keyString = encode(cx, cache_key_val, &key_len);
1137+
if (!keyString) {
1138+
return false;
1139+
}
1140+
std::string key(keyString.get(), key_len);
1141+
std::string hex_str;
1142+
picosha2::hash256_hex_string(key, hex_str);
1143+
1144+
JSObject *headers = RequestOrResponse::headers<Headers::Mode::ProxyToRequest>(cx, self);
1145+
if (!headers) {
1146+
return false;
1147+
}
1148+
RootedObject headers_val(cx, headers);
1149+
RootedValue name_val(cx, JS::StringValue(JS_NewStringCopyN(cx, "fastly-xqd-cache-key", 20)));
1150+
RootedValue value_val(cx,
1151+
JS::StringValue(JS_NewStringCopyN(cx, hex_str.c_str(), hex_str.length())));
1152+
if (!Headers::detail::append_header_value(cx, headers_val, name_val, value_val,
1153+
"Request.prototype.setCacheKey")) {
1154+
return false;
1155+
}
1156+
1157+
return true;
1158+
}
1159+
11311160
bool set_cache_override(JSContext *cx, HandleObject self, HandleValue cache_override_val) {
11321161
MOZ_ASSERT(is_instance(self));
11331162
if (!builtins::CacheOverride::is_instance(cache_override_val)) {
@@ -1245,12 +1274,25 @@ bool setCacheOverride(JSContext *cx, unsigned argc, Value *vp) {
12451274
return true;
12461275
}
12471276

1277+
bool setCacheKey(JSContext *cx, unsigned argc, Value *vp) {
1278+
METHOD_HEADER(1)
1279+
1280+
if (!set_cache_key(cx, self, args[0])) {
1281+
return false;
1282+
}
1283+
1284+
args.rval().setUndefined();
1285+
return true;
1286+
}
1287+
12481288
const JSFunctionSpec methods[] = {
12491289
JS_FN("arrayBuffer", bodyAll<RequestOrResponse::BodyReadResult::ArrayBuffer>, 0,
12501290
JSPROP_ENUMERATE),
12511291
JS_FN("json", bodyAll<RequestOrResponse::BodyReadResult::JSON>, 0, JSPROP_ENUMERATE),
12521292
JS_FN("text", bodyAll<RequestOrResponse::BodyReadResult::Text>, 0, JSPROP_ENUMERATE),
1253-
JS_FN("setCacheOverride", setCacheOverride, 3, JSPROP_ENUMERATE), JS_FS_END};
1293+
JS_FN("setCacheOverride", setCacheOverride, 3, JSPROP_ENUMERATE),
1294+
JS_FN("setCacheKey", setCacheKey, 3, JSPROP_ENUMERATE),
1295+
JS_FS_END};
12541296

12551297
const JSPropertySpec properties[] = {JS_PSG("method", method_get, JSPROP_ENUMERATE),
12561298
JS_PSG("url", url_get, JSPROP_ENUMERATE),

0 commit comments

Comments
 (0)