Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated dependencies, added eslint, rollup, plus some fixes #521

Merged
merged 11 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 168 additions & 151 deletions .Gruntfile.babel.js

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"presets": [["env", {
"targets": {
"browsers": [
"last 2 versions"
]
}
}]],
"plugins": ["transform-flow-strip-types", "transform-inline-environment-variables", "transform-runtime", "transform-class-properties", "transform-object-rest-spread"]
"presets": ["@babel/preset-flow", "@babel/preset-env"],
"plugins": ["transform-inline-environment-variables", "@babel/plugin-transform-runtime", "@babel/plugin-proposal-class-properties", "@babel/plugin-proposal-object-rest-spread", "@babel/plugin-syntax-dynamic-import"]
}
15 changes: 9 additions & 6 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
.*/node_modules/.*
.*/dist/.*
.*/ignored/.*
.*/frontend/_static/.*
.*/frontend/simple/assets/.*
.*/historical/.*
.*/backend/discard/.*
.*/frontend/simple/controller/backend/interface.js
.*/frontend/simple/utils/crypto.js
.*/frontend/simple/utils/vuex-queue.js
# .*/test/frontend.js
.*/frontend/assets/.*
.*/frontend/controller/backend/interface.js
.*/frontend/controller/utils/primus.js
.*/frontend/utils/crypto.js
.*/frontend/utils/vuex-queue.js
.*/frontend/utils/flow-typer.js
# .*/test/backend.js
.*/test/frontend.js
.*.test.js

[libs]
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "9"
- "10"
# https://docs.travis-ci.com/user/gui-and-headless-browsers/#Using-xvfb-to-Run-Tests-That-Require-a-GUI
# to specify screen resolution, add this to before_script instead of init.d/xvfb start:
# - "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16"
Expand Down
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// =======================

// https://babeljs.io/docs/setup/#babel_register
require('babel-register')({
// https://babeljs.io/docs/en/next/babel-register
require('@babel/register')({
// if we do not set cache: false, then the ports on process.env.(API_URL, etc)
// won't get updated correctly. There's a hack that seems to work, which is to
// simply console.log those values in backend/server.js and elsewhere,
Expand Down
4 changes: 2 additions & 2 deletions backend/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://hapijs.com/tutorials/auth
// https://hapijs.com/tutorials/plugins

import {verify, b64ToStr} from '../shared/functions.js'
import { verify, b64ToStr } from '../shared/functions.js'

const Boom = require('boom')

Expand All @@ -12,7 +12,7 @@ exports.plugin = {
server.auth.scheme('gi-auth', function (server, options) {
return {
authenticate: function (request, h) {
const {authorization} = request.headers
const { authorization } = request.headers
if (!authorization) h.unauthenticated(Boom.unauthorized('Missing authorization'))

var [scheme, json] = authorization.split(/\s+/)
Expand Down
8 changes: 4 additions & 4 deletions backend/database.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

// import fs from 'fs'
import {Readable} from 'stream'
import {GIMessage} from '../shared/GIMessage.js'
import { Readable } from 'stream'
import { GIMessage } from '../shared/GIMessage.js'
import sbp from '../shared/sbp.js'
import '../shared/domains/okTurtles/data/index.js'
import {strToB64} from '../shared/functions.js'
import { strToB64 } from '../shared/functions.js'

// TODO: use some fast key/value store
// TODO: just use the file system! store the json of each message to disk as a file with its hash as the file name
Expand All @@ -19,7 +19,7 @@ const get = (key: string) => sbp('okTurtles.data/get', key)
const set = (key: string, value: string) => sbp('okTurtles.data/set', key, value)

export function addLogEntry (entry: GIMessage): string {
const {previousHEAD} = entry.message()
const { previousHEAD } = entry.message()
var contractID: string = previousHEAD ? entry.message().contractID : entry.hash()
if (get(entry.hash())) {
console.warn(`entry exists: ${entry.hash()}`)
Expand Down
13 changes: 9 additions & 4 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module.exports = server.loaded.then(() => {
console.log(chalk.bold('backend startup sequence complete.'))
})

// when spawned via grunt, listen for message to cleanly shutdown and relinquish port
process.on('message', function () {
const shutdownFn = function () {
console.log('message received in child, shutting down...')
var primus = server.hapi.primus
primus.on('close', async function () {
Expand All @@ -31,8 +30,14 @@ process.on('message', function () {
process.exit(1)
}
})
primus.destroy({timeout: 1000}) // TODO: close: false ?
})
primus.destroy({ timeout: 1000 }) // TODO: close: false ?
}

// sent by nodemon
process.on('SIGUSR2', shutdownFn)

// when spawned via grunt, listen for message to cleanly shutdown and relinquish port
process.on('message', shutdownFn)

process.on('uncaughtException', (err) => {
console.error('[server] Unhandled exception:', err, err.stack)
Expand Down
39 changes: 29 additions & 10 deletions backend/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@
// primus events: https://github.com/primus/primus#events
// https://github.com/primus/primus-emit (better than primus-emitter)

import {bold} from 'chalk'
import {RESPONSE_TYPE} from '../shared/constants'
import {makeResponse as reply, setupPrimus} from '../shared/functions'
import { bold } from 'chalk'
import { RESPONSE_TYPE } from '../shared/constants.js'
import { makeResponse as reply } from '../shared/functions.js'
import Primus from 'primus'

const {ERROR, SUCCESS, SUB, UNSUB, PUB} = RESPONSE_TYPE
const { ERROR, SUCCESS, SUB, UNSUB, PUB } = RESPONSE_TYPE

// TODO: decide whether it's better to switch to HTTP2 intead of using websockets
// generate and save primus client file
// https://github.com/primus/primus#client-library
// this function is also used in .Grunfile.babel.js
// to save the corresponding frontend version of the primus.js file
export function setupPrimus (server: Object, saveAndDestroy: boolean = false) {
var primus = new Primus(server, {
transformer: 'websockets',
rooms: { wildcard: false }
})
primus.plugin('rooms', require('primus-rooms'))
primus.plugin('responder', require('primus-responder'))
if (saveAndDestroy) {
primus.save(require('path').join(__dirname, '../frontend/controller/utils/primus.js'))
primus.destroy()
}
return primus
}

// TODO: decide whether it's better to switch to HTTP2 intead of using websockets — NOTE: it probably is (makes it easier to self-host? also more sbp-friendly single-api-endpoint design?)
// https://www.reddit.com/r/rust/comments/5p6a8z/a_hyper_update_v010_last_week_tokio_soon/
//
// NOTE: primus-rooms can be used with primus-multiplex
// primus-multiplex makes it so that the server can have
// multiple channels, and each channel then can have multiple rooms.
// https://github.com/cayasso/primus-multiplex/blob/master/examples/node/rooms/index.js

module.exports = function (hapi: Object) {
export default function (hapi: Object) {
var primus = setupPrimus(hapi.listener)
// make it possible to access primus via: hapi.primus
hapi.decorate('server', 'primus', primus)
Expand All @@ -32,15 +51,15 @@ module.exports = function (hapi: Object) {

primus.on('connection', function (spark) {
// spark is the new connection. https://github.com/primus/primus#sparkheaders
const {id, address} = spark
const { id, address } = spark
// console.log('connection has the following headers', headers)
console.log(bold(`[pubsub] ${id} connected from:`), address)

// https://github.com/swissmanu/primus-responder
spark.on('request', async function (req, done) {
try {
var {type, data: {contractID}} = req
var success = reply(SUCCESS, {type, id})
var { type, data: { contractID } } = req
var success = reply(SUCCESS, { type, id })
console.log(bold(`[pubsub] REQUEST '${type}' from '${id}'`), req)
switch (type) {
case SUB:
Expand Down Expand Up @@ -76,7 +95,7 @@ module.exports = function (hapi: Object) {
console.log(bold.yellow(`[pubsub] ${id} leaveallrooms`))
// this gets called on spark.leaveAll and 'disconnection'
rooms.forEach(contractID => {
primus.room(contractID).write(reply(UNSUB, {contractID, id}))
primus.room(contractID).write(reply(UNSUB, { contractID, id }))
})
})

Expand Down
10 changes: 5 additions & 5 deletions backend/routes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* globals logger */

import {GIMessage} from '../shared/GIMessage.js'
import { GIMessage } from '../shared/GIMessage.js'
import * as db from './database.js'
const Boom = require('boom')
const Joi = require('joi')

// NOTE: We could get rid of this RESTful API and just rely on pubsub.js to do this
// —BUT HTTP2 might be better than websockets and so we keep this around.
// See related TODO in pubsub.js and the reddit discussion link.
module.exports = function (server: Object) {
export default function (server: Object) {
server.route({
path: '/event',
method: ['PUT', 'POST'],
Expand All @@ -33,7 +33,7 @@ module.exports = function (server: Object) {
method: ['GET'],
handler: async function (request, h) {
try {
const {contractID, since} = request.params
const { contractID, since } = request.params
var stream = db.streamEntriesSince(contractID, since)
// "On an HTTP server, make sure to manually close your streams if a request is aborted."
// From: http://knexjs.org/#Interfaces-Streams
Expand Down Expand Up @@ -63,12 +63,12 @@ module.exports = function (server: Object) {
} } },
handler: function (request, h) {
try {
const {name, value} = request.payload
const { name, value } = request.payload
if (db.lookupName(name)) {
return Boom.conflict('exists')
} else {
db.registerName(name, value)
return {name, value}
return { name, value }
}
} catch (err) {
logger(err)
Expand Down
12 changes: 6 additions & 6 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import * as db from './database.js'
import Hapi from 'hapi'
import GiAuth from './auth.js'
import {GIMessage} from '../shared/GIMessage.js'
import {makeResponse} from '../shared/functions.js'
import {RESPONSE_TYPE} from '../shared/constants.js'
import {bold} from 'chalk'
import { GIMessage } from '../shared/GIMessage.js'
import { makeResponse } from '../shared/functions.js'
import { RESPONSE_TYPE } from '../shared/constants.js'
import { bold } from 'chalk'

// NOTE: migration guides for Hapi v16 -> v17:
// https://github.com/hapijs/hapi/issues/3658
Expand Down Expand Up @@ -36,8 +36,8 @@ hapi.decorate('server', 'handleEntry', function (entry: GIMessage) {

// https://hapijs.com/tutorials/plugins
export const loaded = hapi.register(GiAuth).then(() => {
require('./routes')(hapi)
require('./pubsub')(hapi)
require('./routes.js').default(hapi)
require('./pubsub.js').default(hapi)
return hapi.start().then(() => {
console.log('API server running at:', hapi.info.uri)
})
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion frontend/_static/README.md

This file was deleted.

Loading