Skip to content

Commit

Permalink
fix(websockets): fix token usage in simulator (#46)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `url()` and `inflightUrl()` methods have been removed in favor of a `url` property.
  • Loading branch information
eladb authored Jan 1, 2024
1 parent 1132752 commit ada64e4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 75 deletions.
8 changes: 0 additions & 8 deletions websockets/commons/api.w
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ pub interface IWebSocket extends std.IResource {
* Sends a message through the WebSocket with inflight handling.
*/
inflight sendMessage(connectionId: str, message: str);
/**
* Retrieves the URL associated with the WebSocket on inflight.
*/
inflight inflightUrl(): str;
/**
* Retrieves the URL associated with the WebSocket on preflight.
*/
url(): str;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions websockets/lib.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Util {
let counter = new cloud.Counter(initial: 1);

let receiver = new cloud.Service(inflight () => {
let ws = Util._ws(wb.inflightUrl());
let ws = Util._ws(wb.url);

ws.on("open", () => {
log("open socket (receiver)");
Expand All @@ -75,7 +75,7 @@ let receiver = new cloud.Service(inflight () => {
}, autoStart: false) as "receive message";

let sender = new cloud.Service(inflight () => {
let ws = Util._ws(wb.inflightUrl());
let ws = Util._ws(wb.url);

ws.on("open", () => {
log("open socket (sender)");
Expand Down
22 changes: 11 additions & 11 deletions websockets/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ bring "./platform/sim.w" as sim;

pub class WebSocket impl api.IWebSocket {
inner: api.IWebSocket;

pub url: str;

new(props: api.WebSocketProps) {
let target = util.env("WING_TARGET");

if target == "tf-aws" {
this.inner = new tfaws.WebSocket_tfaws(props) as props.name;
let ws = new tfaws.WebSocket_tfaws(props) as props.name;
this.url = ws.url;
this.inner = ws;
} elif target == "awscdk" {
this.inner = new awscdk.WebSocket_awscdk(props) as props.name;
let ws = new awscdk.WebSocket_awscdk(props) as props.name;
this.url = ws.url;
this.inner = ws;
} elif target == "sim" {
this.inner = new sim.WebSocket_sim(props) as props.name;
let ws = new sim.WebSocket_sim(props) as props.name;
this.url = ws.url;
this.inner = ws;
} else {
throw "unsupported target {target}";
}
Expand All @@ -31,18 +39,10 @@ pub class WebSocket impl api.IWebSocket {
this.inner.onMessage(handler);
}

pub url(): str {
return this.inner.url();
}

pub initialize() {
this.inner.initialize();
}

pub inflight inflightUrl(): str {
return this.inner.inflightUrl();
}

pub inflight sendMessage(connectionId: str, message: str) {
this.inner.sendMessage(connectionId, message);
}
Expand Down
30 changes: 22 additions & 8 deletions websockets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions websockets/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/websockets",
"description": "WebSocket library for Wing",
"version": "0.0.2",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand All @@ -26,4 +26,4 @@
"devDependencies": {
"@types/ws": "^8.5.10"
}
}
}
17 changes: 4 additions & 13 deletions websockets/platform/awscdk.w
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub class WebSocket_awscdk impl awsapi.IAwsWebSocket {
role: awscdk.aws_iam.Role;
deployment: awscdk.aws_apigatewayv2.CfnDeployment;
region: str?;
invokeUrl: str;
pub url: str;

new(props: commons.WebSocketProps) {

Expand Down Expand Up @@ -37,10 +37,10 @@ pub class WebSocket_awscdk impl awsapi.IAwsWebSocket {
this.region = awscdk.Stack.of(this).region;
let urlSuffix = awscdk.Stack.of(this).urlSuffix;

this.invokeUrl = this.api.attrApiEndpoint;
this.url = this.api.attrApiEndpoint;

new awscdk.CfnOutput(
value: this.invokeUrl,
value: this.url,
exportName: "url"
) as "url";

Expand Down Expand Up @@ -147,17 +147,8 @@ pub class WebSocket_awscdk impl awsapi.IAwsWebSocket {
}
}

pub url(): str {
return this.invokeUrl;
}

pub inflight inflightUrl(): str {
return this.invokeUrl;
}

extern "../inflight/websocket.aws.js" static inflight _postToConnection(endpointUrl: str, connectionId: str, message: str): void;
pub inflight sendMessage(connectionId: str, message: str) {
let url = this.inflightUrl();
WebSocket_awscdk._postToConnection(url.replace("wss://", "https://"), connectionId, message);
WebSocket_awscdk._postToConnection(this.url.replace("wss://", "https://"), connectionId, message);
}
}
22 changes: 6 additions & 16 deletions websockets/platform/sim.w
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ pub class WebSocket_sim impl api.IWebSocket {
var disconnectFn: inflight(str): void;
var messageFn: inflight(str, str): void;
state: sim.State;
preflightUrlToken: str;
inflight inflightUrlToken: str;
urlStateKey: str;

pub url: str;

new(props: api.WebSocketProps) {
this.connectFn = inflight () => {};
this.disconnectFn = inflight () => {};
this.messageFn = inflight () => {};
this.state = new sim.State();
this.preflightUrlToken = "invokeUrl";
}

inflight new() {
this.inflightUrlToken = "invokeUrl";
this.urlStateKey = "url";
this.url = this.state.token(this.urlStateKey);
}

pub onConnect(handler: inflight(str): void): void {
Expand All @@ -38,24 +36,16 @@ pub class WebSocket_sim impl api.IWebSocket {
this.messageFn = handler;
}

pub url(): str {
return this.state.token(this.preflightUrlToken);
}

pub initialize() {
new cloud.Service(inflight () => {
let res = WebSocket_sim._startWebSocketApi(this.connectFn, this.disconnectFn, this.messageFn);
this.state.set(this.inflightUrlToken, res.url());
this.state.set(this.urlStateKey, res.url());
return () => {
res.close();
};
});
}

pub inflight inflightUrl(): str {
return str.fromJson(this.state.get(this.inflightUrlToken));
}

extern "./sim/wb.js" static inflight _startWebSocketApi(
connectFn: inflight (str): void,
disconnectFn: inflight (str): void,
Expand Down
21 changes: 6 additions & 15 deletions websockets/platform/tf-aws.w
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bring "./aws/api.w" as awsapi;
pub class WebSocket_tfaws impl awsapi.IAwsWebSocket {
webSocketApi: tfaws.apigatewayv2Api.Apigatewayv2Api;
role: tfaws.iamRole.IamRole;
invokeUrl: str;
pub url: str;

new(props: commons.WebSocketProps) {

Expand Down Expand Up @@ -40,7 +40,7 @@ pub class WebSocket_tfaws impl awsapi.IAwsWebSocket {
autoDeploy: true,
);

this.invokeUrl = stage.invokeUrl;
this.url = stage.invokeUrl;
}

pub onLift(host: std.IInflightHost, ops: Array<str>) {
Expand All @@ -66,7 +66,7 @@ pub class WebSocket_tfaws impl awsapi.IAwsWebSocket {
body: "ack"
};
}), env: {
"url": this.invokeUrl,
"url": this.url,
}) as "on connect";

this.addRoute(onConnectFunction, routeKey);
Expand All @@ -83,7 +83,7 @@ pub class WebSocket_tfaws impl awsapi.IAwsWebSocket {
body: "ack"
};
}), env: {
"url": this.invokeUrl,
"url": this.url,
}) as "on disconnect";

this.addRoute(onDisconnectFunction, routeKey);
Expand All @@ -100,7 +100,7 @@ pub class WebSocket_tfaws impl awsapi.IAwsWebSocket {
body: "ack"
};
}), env: {
"url": this.invokeUrl,
"url": this.url,
}) as "on message";

this.addRoute(onMessageFunction, routeKey);
Expand Down Expand Up @@ -144,17 +144,8 @@ pub class WebSocket_tfaws impl awsapi.IAwsWebSocket {
}
}

pub url(): str {
return this.invokeUrl;
}

pub inflight inflightUrl(): str {
return this.invokeUrl;
}

extern "../inflight/websocket.aws.js" static inflight _postToConnection(endpointUrl: str, connectionId: str, message: str): void;
pub inflight sendMessage(connectionId: str, message: str) {
let url = this.inflightUrl();
WebSocket_tfaws._postToConnection(url.replace("wss://", "https://"), connectionId, message);
WebSocket_tfaws._postToConnection(this.url.replace("wss://", "https://"), connectionId, message);
}
}

0 comments on commit ada64e4

Please sign in to comment.