Skip to content

Commit

Permalink
Update api/web-socket to use jsg::JsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Aug 25, 2023
1 parent 8533248 commit 3a184d8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
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 @@ -374,8 +382,8 @@ class WebSocket: public EventTarget {
void send(jsg::Lock& js, kj::OneOf<kj::Array<byte>, kj::String> message);
void close(jsg::Lock& js, jsg::Optional<int> code, jsg::Optional<kj::String> reason);

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

Expand Down Expand Up @@ -616,7 +624,7 @@ class WebSocket: public EventTarget {
// - Transitions from `Accepted` to `Released` when outgoing pump is done and either both
// directions have seen "close" messages or an error has occured.

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

struct GatedMessage {
Expand Down Expand Up @@ -662,7 +670,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

0 comments on commit 3a184d8

Please sign in to comment.