Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
serialize() {
return JSON.stringify({
contestant: this.contestant,
phoneNumber: this.phoneNumber
});
}
static deserialize(data) {
return new Vote(data);
}
}
Vote.invalid = new Vote();
dc.init()
.readData(dc.http.readPostData(handler, "/api/vote"))
.map(
(data) => new Vote(data)
)
.filter(
(vote) => vote !== Vote.invalid
)
.group(
(vote) => vote.phoneNumber
)
.nagle()
.reduce(
(cache, votes) => votes.forEach((vote) => cache.replace(vote)),
dc.hashCache((vote) => vote.phoneNumber)
)
.use((cache) => cache.changeStream())
/**
* In a TV show people may vote on one of contestants.
*
* The server above allows to create a distributed and fault-tolerant service
* with extreme ease.
*
* At the start the service exposes an url to post data to.
* In the middle it does streamed calculations grouped by phone number and contestants.
* At the end it exposes data over a websocket and http get urls.
*/
const dc = require("dc");
const http = require("http");
let handler = dc.http.requestHandler();
http.createServer(handler).listen(8080);
class Vote extends dc.http.PostData {
constructor(data) {
super();
if (!data)
return this;
try {
const voteData = data instanceof Object && data;
if (voteData && voteData.contestant && voteData.phoneNumber && Object.keys(voteData).length === 2) {
this.contestant = voteData.contestant;
this.phoneNumber = voteData.phoneNumber;
return this;
const dc = require("dc");
const http = require("http");
let handler = dc.http.requestHandler();
http.createServer(handler).listen(8080);
class WeatherData extends dc.http.PostData {
constructor(data) {
super();
this.iana = data.iana;
this.windDirection = data.windDirection;
this.windSpeed = data.windSpeed;
}
validate() {
return (
typeof this.windSpeed === "number" && this.windSpeed >= 0 && this.windSpeed < 150 &&
typeof this.windDirection === "number" && this.windDirection >= 0 && this.windDirection < 360
(stream) => stream.map(
(sampleList) => sampleList.latest
).join().reduce(
(acc, weather) => (acc[weather.iana] = weather, acc)
).expose(
dc.http.exposeData("/weather/{iana}", (input, iana) => input[iana])
)
).reduce(