Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update api/web-socket to use jsg::JsValue #1072

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/workerd/api/web-socket.c++
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void WebSocket::initConnection(jsg::Lock& js, kj::Promise<PackedWebSocket> prom)
farNative->closedIncoming = true;

// Sets readyState to CLOSED.
reportError(js, kj::mv(e));
reportError(js, jsg::JsValue(e.getHandle(js)).addRef(js));

dispatchEventImpl(js,
jsg::alloc<CloseEvent>(1006, kj::str("Failed to establish websocket connection"),
Expand Down Expand Up @@ -644,9 +644,9 @@ kj::Maybe<kj::StringPtr> WebSocket::getExtensions() {
return extensions.map([](kj::StringPtr value){ return value; });
}

kj::Maybe<v8::Local<v8::Value>> WebSocket::deserializeAttachment(jsg::Lock& js) {
kj::Maybe<jsg::JsValue> WebSocket::deserializeAttachment(jsg::Lock& js) {
return serializedAttachment.map([&](kj::ArrayPtr<byte> attachment)
-> v8::Local<v8::Value> {
-> jsg::JsValue {
jsg::Deserializer deserializer(js, attachment, nullptr, nullptr,
jsg::Deserializer::Options {
.version = 15,
Expand All @@ -657,12 +657,12 @@ kj::Maybe<v8::Local<v8::Value>> WebSocket::deserializeAttachment(jsg::Lock& js)
});
}

void WebSocket::serializeAttachment(jsg::Lock& js, v8::Local<v8::Value> attachment) {
void WebSocket::serializeAttachment(jsg::Lock& js, jsg::JsValue attachment) {
jsg::Serializer serializer(js, jsg::Serializer::Options {
.version = 15,
.omitHeader = false,
});
serializer.write(js, jsg::JsValue(attachment));
serializer.write(js, attachment);
auto released = serializer.release();
JSG_REQUIRE(released.data.size() <= MAX_ATTACHMENT_SIZE, Error,
"A WebSocket 'attachment' cannot be larger than ", MAX_ATTACHMENT_SIZE, " bytes." \
Expand Down Expand Up @@ -723,7 +723,7 @@ void WebSocket::ensurePumping(jsg::Lock& js) {
// We have a hibernatable websocket -- we don't want to dispatch a regular error event.
tryReleaseNative(js);
} else {
reportError(js, kj::mv(exception));
reportError(js, jsg::JsValue(exception.getHandle(js)).addRef(js));
}
});
}
Expand Down Expand Up @@ -840,10 +840,12 @@ kj::Promise<kj::Maybe<kj::Exception>> WebSocket::readLoop() {
jsg::Lock& js = wLock;
KJ_SWITCH_ONEOF(message) {
KJ_CASE_ONEOF(text, kj::String) {
dispatchEventImpl(js, jsg::alloc<MessageEvent>(js, js.wrapString(text)));
dispatchEventImpl(js,
jsg::alloc<MessageEvent>(js, js.str(text)));
}
KJ_CASE_ONEOF(data, kj::Array<byte>) {
dispatchEventImpl(js, jsg::alloc<MessageEvent>(js, js.wrapBytes(kj::mv(data))));
dispatchEventImpl(js,
jsg::alloc<MessageEvent>(js, jsg::JsValue(js.bytes(kj::mv(data)).getHandle(js))));
}
KJ_CASE_ONEOF(close, kj::WebSocket::Close) {
native.closedIncoming = true;
Expand Down Expand Up @@ -883,11 +885,10 @@ void ErrorEvent::visitForGc(jsg::GcVisitor& visitor) {
}

void WebSocket::reportError(jsg::Lock& js, kj::Exception&& e) {
jsg::Value err = js.exceptionToJs(kj::cp(e));
reportError(js, kj::mv(err));
reportError(js, js.exceptionToJsValue(kj::cp(e)));
}

void WebSocket::reportError(jsg::Lock& js, jsg::Value err) {
void WebSocket::reportError(jsg::Lock& js, jsg::JsRef<jsg::JsValue> err) {
// If this is the first error, raise the error event.
if (error == nullptr) {
auto msg = kj::str(v8::Exception::CreateMessage(js.v8Isolate, err.getHandle(js))->Get());
Expand Down
38 changes: 23 additions & 15 deletions src/workerd/api/web-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ template <typename T> struct DeferredProxy;

class MessageEvent: public Event {
public:
MessageEvent(jsg::Lock& js, v8::Local<v8::Value> data)
: Event("message"), data(js.v8Isolate, data) {}
MessageEvent(jsg::Lock& js, kj::String type, v8::Local<v8::Value> data)
: Event(kj::mv(type)), data(js.v8Isolate, data) {}
MessageEvent(jsg::Lock& js, const jsg::JsValue& data)
: Event("message"), data(jsg::JsRef(js, data)) {}
MessageEvent(jsg::Lock& js, jsg::JsRef<jsg::JsValue> data)
: Event("message"), data(kj::mv(data)) {}
MessageEvent(jsg::Lock& js, kj::String type, const jsg::JsValue& data)
: Event(kj::mv(type)), data(jsg::JsRef(js, kj::mv(data))) {}
MessageEvent(jsg::Lock& js, kj::String type, jsg::JsRef<jsg::JsValue> data)
: Event(kj::mv(type)), data(kj::mv(data)) {}

struct Initializer {
v8::Local<v8::Value> data;
jsg::JsRef<jsg::JsValue> data;

JSG_STRUCT(data);
JSG_STRUCT_TS_OVERRIDE(MessageEventInit {
Expand All @@ -36,10 +40,10 @@ class MessageEvent: public Event {
static jsg::Ref<MessageEvent> constructor(jsg::Lock& js,
kj::String type,
Initializer initializer) {
return jsg::alloc<MessageEvent>(js, kj::mv(type), initializer.data);
return jsg::alloc<MessageEvent>(js, kj::mv(type), kj::mv(initializer.data));
}

v8::Local<v8::Value> getData(jsg::Lock& js) { return data.getHandle(js); }
jsg::JsValue getData(jsg::Lock& js) { return data.getHandle(js); }

jsg::Unimplemented getOrigin() { return jsg::Unimplemented(); }
jsg::Unimplemented getLastEventId() { return jsg::Unimplemented(); }
Expand All @@ -64,7 +68,11 @@ class MessageEvent: public Event {
}

private:
jsg::Value data;
jsg::JsRef<jsg::JsValue> data;

void visitForGc(jsg::GcVisitor& visitor) {
visitor.visit(data);
}
};

class CloseEvent: public Event {
Expand Down Expand Up @@ -112,7 +120,7 @@ class CloseEvent: public Event {

class ErrorEvent: public Event {
public:
ErrorEvent(jsg::Lock& js, kj::String&& message, jsg::Value error)
ErrorEvent(jsg::Lock& js, kj::String&& message, jsg::JsRef<jsg::JsValue> error)
: Event("error"), message(kj::mv(message)), error(kj::mv(error)) {}

static jsg::Ref<ErrorEvent> constructor() = delete;
Expand All @@ -123,7 +131,7 @@ class ErrorEvent: public Event {
kj::StringPtr getMessage() { return message; }
int getLineno() { return 0; }
int getColno() { return 0; }
v8::Local<v8::Value> getError(jsg::Lock& js) { return error.getHandle(js); }
jsg::JsValue getError(jsg::Lock& js) { return error.getHandle(js); }


JSG_RESOURCE_TYPE(ErrorEvent) {
Expand All @@ -141,7 +149,7 @@ class ErrorEvent: public Event {

private:
kj::String message;
jsg::Value error;
jsg::JsRef<jsg::JsValue> error;

void visitForGc(jsg::GcVisitor& visitor);
};
Expand Down Expand Up @@ -379,11 +387,11 @@ class WebSocket: public EventTarget {

// Used to get/set the attachment for hibernation.
// If the object isn't serialized, it will not survive hibernation.
void serializeAttachment(jsg::Lock& js, v8::Local<v8::Value> attachment);
void serializeAttachment(jsg::Lock& js, jsg::JsValue attachment);

// Used to get/set the attachment for hibernation.
// If the object isn't serialized, it will not survive hibernation.
kj::Maybe<v8::Local<v8::Value>> deserializeAttachment(jsg::Lock& js);
kj::Maybe<jsg::JsValue> deserializeAttachment(jsg::Lock& js);

// Used to get/store the last auto request/response timestamp for this WebSocket.
// These methods are c++ only and are not exposed to our js interface.
Expand Down Expand Up @@ -629,7 +637,7 @@ class WebSocket: public EventTarget {
IoOwn<Native> farNative;

// If any error has occurred.
kj::Maybe<jsg::Value> error;
kj::Maybe<jsg::JsRef<jsg::JsValue>> error;

struct GatedMessage {
kj::Maybe<kj::Promise<void>> outputLock; // must wait for this before actually sending
Expand Down Expand Up @@ -674,7 +682,7 @@ class WebSocket: public EventTarget {
kj::Promise<kj::Maybe<kj::Exception>> readLoop();

void reportError(jsg::Lock& js, kj::Exception&& e);
void reportError(jsg::Lock& js, jsg::Value err);
void reportError(jsg::Lock& js, jsg::JsRef<jsg::JsValue> err);

void assertNoError(jsg::Lock& js);
};
Expand Down
Loading