0.0.15 • Published 4 years ago

time-streams v0.0.15

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

time-streams

A super simple protocol for small scale, low volume, most-recent-first data feeds.

Setup

yarn install

Example

Start the server:

yarn dev

Then in another shell:

curl -X POST -H "Authorization: Bearer devsekrit" -H "Content-Type: text/plain" --data "hi from curl" http://localhost:3333/posts

Now open http://localhost:3333/posts in your browser.

Using the middleware

You can mount the reader and writer middleware independently.

const express = require('express')
const { reader, writer } = require('time-streams')

const app = express()
const webroot = 'public'

app.use(reader(webroot))
app.use(writer(webroot, { createIfMissing: true }))

// serve regular files, too
app.use(express.static(webroot))

const listener = app.listen(process.env.PORT, () => {
  console.log("Listening at http://localhost:" + listener.address().port)
})

In practice, you'll likely want to protect writes somehow. This is one way:

// need a secret to post (TIME_STREAM_SECRET environment variable)
app.use((req, res, next) => {
  if (req.method !== 'POST') return next()
  const authHeader = req.get('Authorization')
  if (authHeader) {
    const secret = authHeader.split(' ')[1]
    if (secret === process.env.TIME_STREAM_SECRET) {
      return next()
    }
  }
  return res.status(401).send('Not authorized')
})

Using the file store

You can also use the file store directly.

const { FileStore } = require('time-streams')

const stream = new FileStore('posts')

stream.save({
  body: 'test-message',
  contentType: 'text/plain'
})
0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago