This week, I can safely say has been the most productive yet. The first part of the week was spent in setting up only a ping message from the server. Towards the end, I managed to pass the bindings to the server and and bind the listener.

Setting up Connection

I set up a basic api at /connect/ to send ping messages at 10s intervals. To check, I was connecting to localhost:12345/connect, becuase thats where (I thought) my api was hosted. The browser console gave this error. Content Security Policy: The page’s settings blocked the loading of a resource at self (“default-src”). The CSP header determines what scripts the user is allowed to load. This is to protect against cross-site scripting attacks (XSS). The connect-src directive allows scripts to open SSE connections. However, a bunch of other changes had to be made in order to get it running on the browser. Jonas suggested trying to get everything working with curl first.

There I realized that the api was actually at /api/events/v1/connect/, and there was nothing in the docs to suggest that. All taskcluster-lib-api endpoints are at /api/<serviceName>/<version>/<route>.

Handling Server Errors

Error handling is crucial in node as it shuts down the server. Hence, I had to wrap most of my code in try-catch. And in cases of error if the head is already written(connection was successfully established) and the request is still open, send an error message, otherwise send a 5xx status code.

When client closed the connection remotely, we had to end the response. Jonas gave me a good snippet on how we can use a combination of Promises to solve this problem and avoid the write after end error.

Work on bindings

Passing bindings through curl in bash was creating an issue, where the shell would start backgroud process in bash. And I wasn’t able to terminate the connection without closing the server. Thus I wrote a simple client script using EventSource at test\api_test.js. Later this will actually contain tests.

Also the json had to be url encoded. I used the urlencode package. It is essentially translating characters like spaces, braces, etc to a universal representation. For example a space is represented by %20 as you may have observed.

For now json validation is just a function which returns true. I will need to add a check for schema in the coming week.

Listener

I was able to bind the listener, but not receiving messages. Turns out I didn’t give the proper vhost. This new parameter was also introduced in v10 of taskcluster-client. After this, I opened a PR to update their docs. Once I know what should vhost be, I am sure this will work.

Coming up..

A complete working listener, and tests. After the listener is working, we will have to make sure we take care of all errors, and a bit of cleanup. Also, I will be travelling to my hometown this Monday. Hopefully, my productivity doesn’t take a hit!