diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..da18355 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,39 @@ +version: 2 +jobs: + build: + docker: + - image: docker:latest + steps: + - checkout + - setup_remote_docker: + docker_layer_caching: true + - run: + name: Docker building + command: | + if [ "$CIRCLE_BRANCH" = "master" ]; then docker build -t dougley/bezerk:latest .; else docker build -t dougley/bezerk:$CIRCLE_BRANCH .; fi + docker login -u $DOCKER_USER -p $DOCKER_PASS + if [ "$CIRCLE_BRANCH" = "master" ]; then docker push dougley/bezerk:latest; else docker push dougley/bezerk:$CIRCLE_BRANCH; fi + tagged-build: + docker: + - image: docker:latest + steps: + - checkout + - setup_remote_docker: + docker_layer_caching: true + - run: + name: Docker building + command: | + docker build -t dougley/bezerk:$(git describe --abbrev=0 --tags) . + docker login -u $DOCKER_USER -p $DOCKER_PASS + docker push dougley/bezerk:$(git describe --abbrev=0 --tags) +workflows: + version: 2 + test_n_build: + jobs: + - build + - tagged-build: + filters: + branches: + ignore: /.*/ + tags: + only: /v.+/ diff --git a/.gitignore b/.gitignore index c5eb594..09dea3d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,11 +2,14 @@ logs *.log npm-debug.log* +yarn-debug.log* +yarn-error.log* # Runtime data pids *.pid *.seed +*.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov @@ -14,17 +17,48 @@ lib-cov # Coverage directory used by tools like istanbul coverage +# nyc test coverage +.nyc_output + # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt +# Bower dependency directory (https://bower.io/) +bower_components + # node-waf configuration .lock-wscript -# Compiled binary addons (http://nodejs.org/api/addons.html) +# Compiled binary addons (https://nodejs.org/api/addons.html) build/Release -# Dependency directory -# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git -node_modules +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next +# idea files .idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4065984 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM node:9 +MAINTAINER hello@dougley.com + +WORKDIR /app + +CMD ["node", "index.js"] + +COPY package.json /app/ + +# Create working dir +RUN mkdir -p /app +COPY . /app + +RUN npm i diff --git a/index.js b/index.js index f9f38e7..f29a180 100644 --- a/index.js +++ b/index.js @@ -7,16 +7,19 @@ const Server = new WSS({ port: process.env.BEZERK_PORT }) -const sockets = new Map() - Server.on('connection', socket => { socket.on('message', msg => handle(socket, msg)) + socket.on('close', () => { + console.log(Server.clients) + }) + socket.on('error', console.error) send(socket, { op: '1001' }) }) function handle (socket, msg) { + if (socket.readyState !== 1) return try { msg = JSON.parse(msg) validate(socket, msg) @@ -35,7 +38,7 @@ function handle (socket, msg) { }) if (msg.c.shard) { socket.type = 'shard' - sockets.set(`shard:${msg.c.shard}`, socket) + socket.shardid = msg.c.shard } else { socket.type = 'listener' } @@ -65,20 +68,21 @@ function handle (socket, msg) { case '2005': { // REQUEST_APPLY if (socket.type === 'listener') { if (msg.c.shard !== undefined) { - if (sockets.has(`shard:${msg.c.shard}`)) { - send(sockets.get(`shard:${msg.c.shard}`), { - op: '2001', // REQUEST - c: msg.c - }) - } else { - send(socket, { - op: '5000' // CANNOT_COMPLY - }) - } + Server.clients.forEach(x => { + if (x.type === 'shard' && x.shardid) { + return send(x, { + op: '2001', // REQUEST + c: msg.c + }) + } + }) + send(socket, { + op: '5000' // CANNOT_COMPLY + }) } else { - sockets.forEach((value, key) => { - if (value.startsWith('shard:')) { - send(key, { + Server.clients.forEach(x => { + if (x.type === 'shard') { + send(x, { op: '2001', // REQUEST c: msg.c }) @@ -92,11 +96,13 @@ function handle (socket, msg) { } function send (socket, payload) { + if (socket.readyState !== 1) return if (typeof payload === 'object') payload = JSON.stringify(payload) socket.send(payload) } function validate (socket, msg) { + if (socket.readyState !== 1) return if (msg.op === undefined) throw new Error() if (msg.c === undefined) throw new Error() return true