1
0
Fork 0
forked from fedi/mastodon

Add streaming API in Node.js

This commit is contained in:
Eugen Rochko 2017-02-02 01:31:09 +01:00
parent 17ae7d132c
commit 0cfd3188b4
3 changed files with 802 additions and 329 deletions

View file

@ -2,31 +2,37 @@
"name": "mastodon", "name": "mastodon",
"scripts": { "scripts": {
"test": "mocha --require ./spec/javascript/setup.js --compilers js:babel-register ./spec/javascript/components/*.test.jsx", "test": "mocha --require ./spec/javascript/setup.js --compilers js:babel-register ./spec/javascript/components/*.test.jsx",
"storybook": "start-storybook -p 9001 -c storybook" "storybook": "start-storybook -p 9001 -c storybook",
"start": "babel-node ./streaming/index.js --presets es2015,stage-2"
}, },
"dependencies": { "dependencies": {
"@kadira/storybook": "^2.24.0", "@kadira/storybook": "^2.24.0",
"axios": "^0.14.0", "axios": "^0.14.0",
"babel-cli": "^6.22.2",
"babel-plugin-react-transform": "^2.0.2", "babel-plugin-react-transform": "^2.0.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4", "babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.8.0", "babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.13.2", "babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.11.1", "babel-preset-react": "^6.11.1",
"babel-preset-stage-2": "^6.22.0",
"babelify": "^7.3.0", "babelify": "^7.3.0",
"browserify": "^13.1.0", "browserify": "^13.1.0",
"browserify-incremental": "^3.1.1", "browserify-incremental": "^3.1.1",
"chai": "^3.5.0", "chai": "^3.5.0",
"chai-enzyme": "^0.5.2", "chai-enzyme": "^0.5.2",
"css-loader": "^0.26.1", "css-loader": "^0.26.1",
"dotenv": "^4.0.0",
"emojione": "latest", "emojione": "latest",
"enzyme": "^2.4.1", "enzyme": "^2.4.1",
"es6-promise": "^3.2.1", "es6-promise": "^3.2.1",
"express": "^4.14.1",
"http-link-header": "^0.5.0", "http-link-header": "^0.5.0",
"immutable": "^3.8.1", "immutable": "^3.8.1",
"intl": "^1.2.5", "intl": "^1.2.5",
"jsdom": "^9.6.0", "jsdom": "^9.6.0",
"mocha": "^3.1.1", "mocha": "^3.1.1",
"node-sass": "^4.0.0", "node-sass": "^4.0.0",
"pg": "^6.1.2",
"react": "^15.3.2", "react": "^15.3.2",
"react-addons-perf": "^15.3.2", "react-addons-perf": "^15.3.2",
"react-addons-pure-render-mixin": "^15.3.1", "react-addons-pure-render-mixin": "^15.3.1",
@ -47,6 +53,7 @@
"react-simple-dropdown": "^1.1.4", "react-simple-dropdown": "^1.1.4",
"react-storybook-addon-intl": "^0.1.0", "react-storybook-addon-intl": "^0.1.0",
"react-toggle": "^2.1.1", "react-toggle": "^2.1.1",
"redis": "^2.6.5",
"redux": "^3.6.0", "redux": "^3.6.0",
"redux-immutable": "^3.0.8", "redux-immutable": "^3.0.8",
"redux-sounds": "^1.1.1", "redux-sounds": "^1.1.1",

97
streaming/index.js Normal file
View file

@ -0,0 +1,97 @@
import dotenv from 'dotenv'
import express from 'express'
import redis from 'redis'
import pg from 'pg'
dotenv.config()
const pgConfigs = {
development: {
database: 'mastodon_development',
host: '/var/run/postgresql',
max: 10
},
production: {
user: process.env.DB_USER || 'mastodon',
password: process.env.DB_PASS || '',
database: process.env.DB_NAME || 'mastodon_production',
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
max: 10
}
}
const app = express()
const env = process.env.NODE_ENV || 'development'
const pgPool = new pg.Pool(pgConfigs[env])
const authenticationMiddleware = (req, res, next) => {
const authorization = req.get('Authorization')
if (!authorization) {
err = new Error('Missing access token')
err.statusCode = 401
return next(err)
}
const token = authorization.replace(/^Bearer /, '')
pgPool.connect((err, client, done) => {
if (err) {
return next(err)
}
client.query('SELECT oauth_access_tokens.resource_owner_id, users.account_id FROM oauth_access_tokens INNER JOIN users ON oauth_access_tokens.resource_owner_id = users.id WHERE token = $1 LIMIT 1', [token], (err, result) => {
done()
if (err) {
return next(err)
}
if (result.rows.length === 0) {
err = new Error('Invalid access token')
err.statusCode = 401
return next(err)
}
req.accountId = result.rows[0].account_id
next()
})
})
}
const errorMiddleware = (err, req, res, next) => {
res.writeHead(err.statusCode || 500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: `${err}` }))
}
const streamFrom = (id, res) => {
res.setHeader('Content-Type', 'text/event-stream')
res.setHeader('Transfer-Encoding', 'chunked')
const redisClient = redis.createClient()
redisClient.on('message', (channel, message) => {
const { event, payload } = JSON.parse(message)
res.write(`event: ${event}\n`)
res.write(`data: ${payload}\n\n`)
})
setInterval(() => res.write('\n'), 15000)
redisClient.subscribe(id)
}
app.use(authenticationMiddleware)
app.use(errorMiddleware)
app.get('/api/v1/streaming/user', (req, res) => streamFrom(`timeline:${req.accountId}`, res))
app.get('/api/v1/streaming/public', (_, res) => streamFrom('timeline:public', res))
app.get('/api/v1/streaming/hashtag', (req, res) => streamFrom(`timeline:hashtag:${req.params.tag}`, res))
app.listen(4000)

1023
yarn.lock

File diff suppressed because it is too large Load diff