Skip to content

Commit ab7846e

Browse files
author
Guy Bedford
committed
cache override implementation
1 parent 0214d9e commit ab7846e

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

runtime/fastly/builtins/cache-override.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ void CacheOverride::set_pci(JSObject *self, bool pci) {
7979
JS::SetReservedSlot(self, CacheOverride::Slots::PCI, JS::BooleanValue(pci));
8080
}
8181

82+
JS::Value CacheOverride::beforeSend(JSObject *self) {
83+
MOZ_ASSERT(is_instance(self));
84+
return JS::GetReservedSlot(self, Slots::BeforeSend);
85+
}
86+
87+
void CacheOverride::set_beforeSend(JSObject *self, JSObject *fn) {
88+
MOZ_ASSERT(is_instance(self));
89+
JS::SetReservedSlot(self, Slots::BeforeSend, JS::ObjectValue(*fn));
90+
}
91+
92+
JS::Value CacheOverride::afterSend(JSObject *self) {
93+
MOZ_ASSERT(is_instance(self));
94+
return JS::GetReservedSlot(self, Slots::AfterSend);
95+
}
96+
97+
void CacheOverride::set_afterSend(JSObject *self, JSObject *fn) {
98+
MOZ_ASSERT(is_instance(self));
99+
JS::SetReservedSlot(self, Slots::AfterSend, JS::ObjectValue(*fn));
100+
}
101+
82102
host_api::CacheOverrideTag CacheOverride::abi_tag(JSObject *self) {
83103
host_api::CacheOverrideTag tag;
84104

@@ -285,6 +305,64 @@ bool CacheOverride::pci_set(JSContext *cx, JS::HandleObject self, JS::HandleValu
285305
return true;
286306
}
287307

308+
bool CacheOverride::before_send_get(JSContext *cx, JS::HandleObject self,
309+
JS::MutableHandleValue rval) {
310+
if (self == proto_obj) {
311+
return api::throw_error(cx, api::Errors::WrongReceiver, "beforeSend get", "CacheOverride");
312+
}
313+
rval.set(CacheOverride::beforeSend(self));
314+
return true;
315+
}
316+
317+
bool CacheOverride::before_send_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
318+
JS::MutableHandleValue rval) {
319+
if (self == proto_obj) {
320+
return api::throw_error(cx, api::Errors::WrongReceiver, "beforeSend set", "CacheOverride");
321+
}
322+
if (!CacheOverride::ensure_override(cx, self, "beforeSend"))
323+
return false;
324+
if (val.isUndefined()) {
325+
JS::SetReservedSlot(self, Slots::BeforeSend, val);
326+
} else if (!val.isObject() || !JS::IsCallable(&val.toObject())) {
327+
JS_ReportErrorUTF8(cx, "CacheOverride: beforeSend must be a function");
328+
return false;
329+
} else {
330+
CacheOverride::set_beforeSend(self, &val.toObject());
331+
}
332+
333+
rval.set(CacheOverride::beforeSend(self));
334+
return true;
335+
}
336+
337+
bool CacheOverride::after_send_get(JSContext *cx, JS::HandleObject self,
338+
JS::MutableHandleValue rval) {
339+
if (self == proto_obj) {
340+
return api::throw_error(cx, api::Errors::WrongReceiver, "afterSend get", "CacheOverride");
341+
}
342+
rval.set(CacheOverride::afterSend(self));
343+
return true;
344+
}
345+
346+
bool CacheOverride::after_send_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
347+
JS::MutableHandleValue rval) {
348+
if (self == proto_obj) {
349+
return api::throw_error(cx, api::Errors::WrongReceiver, "afterSend set", "CacheOverride");
350+
}
351+
if (!CacheOverride::ensure_override(cx, self, "afterSend"))
352+
return false;
353+
if (val.isUndefined()) {
354+
JS::SetReservedSlot(self, Slots::AfterSend, val);
355+
} else if (!val.isObject() || !JS::IsCallable(&val.toObject())) {
356+
JS_ReportErrorUTF8(cx, "CacheOverride: afterSend must be a function");
357+
return false;
358+
} else {
359+
CacheOverride::set_afterSend(self, &val.toObject());
360+
}
361+
362+
rval.set(CacheOverride::afterSend(self));
363+
return true;
364+
}
365+
288366
template <auto accessor_fn>
289367
bool CacheOverride::accessor_get(JSContext *cx, unsigned argc, JS::Value *vp) {
290368
METHOD_HEADER(0)
@@ -308,6 +386,10 @@ const JSPropertySpec CacheOverride::properties[] = {
308386
JS_PSGS("surrogateKey", accessor_get<surrogate_key_get>, accessor_set<surrogate_key_set>,
309387
JSPROP_ENUMERATE),
310388
JS_PSGS("pci", accessor_get<pci_get>, accessor_set<pci_set>, JSPROP_ENUMERATE),
389+
JS_PSGS("beforeSend", accessor_get<before_send_get>, accessor_set<before_send_set>,
390+
JSPROP_ENUMERATE),
391+
JS_PSGS("afterSend", accessor_get<after_send_get>, accessor_set<after_send_set>,
392+
JSPROP_ENUMERATE),
311393
JS_STRING_SYM_PS(toStringTag, "CacheOverride", JSPROP_READONLY),
312394
JS_PS_END};
313395

@@ -345,6 +427,16 @@ bool CacheOverride::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
345427
if (!JS_GetProperty(cx, override_init, "pci", &val) || !pci_set(cx, self, val, &val)) {
346428
return false;
347429
}
430+
431+
if (!JS_GetProperty(cx, override_init, "beforeSend", &val) ||
432+
!before_send_set(cx, self, val, &val)) {
433+
return false;
434+
}
435+
436+
if (!JS_GetProperty(cx, override_init, "afterSend", &val) ||
437+
!after_send_set(cx, self, val, &val)) {
438+
return false;
439+
}
348440
}
349441

350442
args.rval().setObject(*self);

runtime/fastly/builtins/cache-override.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class CacheOverride : public builtins::BuiltinImpl<CacheOverride> {
2626
//
2727
// `PCI` is interpreted as a boolean, and a flag gets set in the hostcall's
2828
// `tag` parameter if `PCI` is true.
29-
enum Slots { Mode, TTL, SWR, SurrogateKey, PCI, Count };
29+
//
30+
// `BeforeSend` and `AfterSend` are function callbacks that can be set
31+
// to execute before and after sending the request.
32+
enum Slots { Mode, TTL, SWR, SurrogateKey, PCI, BeforeSend, AfterSend, Count };
3033

3134
enum class CacheOverrideMode { None, Pass, Override };
3235

@@ -44,6 +47,10 @@ class CacheOverride : public builtins::BuiltinImpl<CacheOverride> {
4447
static JSObject *clone(JSContext *cx, JS::HandleObject self);
4548
static JS::Value pci(JSObject *self);
4649
static void set_pci(JSObject *self, bool pci);
50+
static JS::Value beforeSend(JSObject *self);
51+
static void set_beforeSend(JSObject *self, JSObject *fn);
52+
static JS::Value afterSend(JSObject *self);
53+
static void set_afterSend(JSObject *self, JSObject *fn);
4754
static CacheOverrideMode mode(JSObject *self);
4855
static void set_mode(JSObject *self, CacheOverride::CacheOverrideMode mode);
4956
static bool mode_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval);
@@ -61,6 +68,12 @@ class CacheOverride : public builtins::BuiltinImpl<CacheOverride> {
6168
static bool pci_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval);
6269
static bool pci_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
6370
JS::MutableHandleValue rval);
71+
static bool before_send_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval);
72+
static bool before_send_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
73+
JS::MutableHandleValue rval);
74+
static bool after_send_get(JSContext *cx, JS::HandleObject self, JS::MutableHandleValue rval);
75+
static bool after_send_set(JSContext *cx, JS::HandleObject self, JS::HandleValue val,
76+
JS::MutableHandleValue rval);
6477
template <auto accessor_fn> static bool accessor_get(JSContext *cx, unsigned argc, JS::Value *vp);
6578
template <auto accessor_fn> static bool accessor_set(JSContext *cx, unsigned argc, JS::Value *vp);
6679

0 commit comments

Comments
 (0)