diff options
author | 2023-06-18 00:54:50 -0300 | |
---|---|---|
committer | 2023-06-17 20:54:50 -0700 | |
commit | b2af1984ed6db162468f3dd8c6d460420d4f4a2e (patch) | |
tree | 4d0776e093ba823d79369b5e733fd5be527af900 /src/bun.js/bindings/ZigGlobalObject.cpp | |
parent | 065713aeca2ae3013bdf5b3d2f04263459631598 (diff) | |
download | bun-b2af1984ed6db162468f3dd8c6d460420d4f4a2e.tar.gz bun-b2af1984ed6db162468f3dd8c6d460420d4f4a2e.tar.zst bun-b2af1984ed6db162468f3dd8c6d460420d4f4a2e.zip |
[eventsource] SSE Client (#3074)
* fix flush
* remove logs
* add HTTP/1.1 eventsource
* fix parse spec
* multiple data in one event
* get lastEventId for reconnection
* fix parsing add reconnect
* fix reconnection retry
* add retry option
* move eventsource to builtins
* remove duplicate interface on globals.d.ts
* move test to TS
* fmt
* allow no Content-Length or Transfer Encoding
* udpate builtins
* hardcoded
* merge
* revert /src/out
* updated
* Update .gitignore
* Make the tests fail
* Cleanup EventSource getter
* fixup
* fixup TS
* fmt
* update builtins
* fix tests
* Clear existing timeouts
* Add `ref` and `unref` methods
* Use `super` to make prototype pollution slightly harder
* Reduce test timeout
* Regenerate builtins
* prettier + ref/unref
* Outdated
* forgot to commit this
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 299ad7a8c..13bb51afd 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -3241,6 +3241,55 @@ JSC_DEFINE_CUSTOM_GETTER(functionBuildMessageGetter, (JSGlobalObject * globalObj return JSValue::encode(reinterpret_cast<Zig::GlobalObject*>(globalObject)->JSBuildMessageConstructor()); } +JSC_DEFINE_CUSTOM_GETTER( + EventSource_getter, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName property)) +{ + auto& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + // If "this" is not the Global object, just return undefined + // you should not be able to reset the global object's EventSource if you muck around with prototypes + if (JSValue::decode(thisValue) != globalObject) + return JSValue::encode(JSC::jsUndefined()); + + JSC::JSFunction* getSourceEvent = JSC::JSFunction::create(vm, eventSourceGetEventSourceCodeGenerator(vm), globalObject); + RETURN_IF_EXCEPTION(scope, {}); + + JSC::MarkedArgumentBuffer args; + + auto clientData = WebCore::clientData(vm); + JSC::CallData callData = JSC::getCallData(getSourceEvent); + + NakedPtr<JSC::Exception> returnedException = nullptr; + auto result = JSC::call(globalObject, getSourceEvent, callData, globalObject->globalThis(), args, returnedException); + RETURN_IF_EXCEPTION(scope, {}); + + if (returnedException) { + throwException(globalObject, scope, returnedException.get()); + } + + RETURN_IF_EXCEPTION(scope, {}); + + if (LIKELY(result)) { + globalObject->putDirect(vm, property, result, 0); + } + + RELEASE_AND_RETURN(scope, JSValue::encode(result)); +} + +JSC_DEFINE_CUSTOM_SETTER(EventSource_setter, + (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, + JSC::EncodedJSValue value, JSC::PropertyName property)) +{ + if (JSValue::decode(thisValue) != globalObject) { + return false; + } + + auto& vm = globalObject->vm(); + globalObject->putDirect(vm, property, JSValue::decode(value), 0); + return true; +} + EncodedJSValue GlobalObject::assignToStream(JSValue stream, JSValue controller) { JSC::VM& vm = this->vm(); @@ -3538,6 +3587,8 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm) putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "$_BunCommonJSModule_$"_s), JSC::CustomGetterSetter::create(vm, BunCommonJSModule_getter, nullptr), JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, "EventSource"_s), JSC::CustomGetterSetter::create(vm, EventSource_getter, EventSource_setter), 0); + auto bufferAccessor = JSC::CustomGetterSetter::create(vm, JSBuffer_getter, JSBuffer_setter); auto realBufferAccessor = JSC::CustomGetterSetter::create(vm, JSBuffer_privateGetter, nullptr); |