kafjs

Embedded Event Store for Javascript

Usage no npm install needed!

<script type="module">
  import kafjs from 'https://cdn.skypack.dev/kafjs';
</script>

README

KafJS

An embedded event store for Javascript heavily inspired by Kaf.

QuickStart

  1. Install KafJS: npm i kafjs

  2. Start the embedded server:

    const kaf = require("kafjs")
    kaf.startServer(PORT, DBFOLDER, err => {
      if(err.code === "EADDRINUSE") ...
    })
    
  3. POST messages to your logfile (auto-created):

    gt; curl localhost:7749/put/mylog -d '{"data":"Put JSON Record"}'
    
  4. GET messages from your log file:

    gt; curl localhost:7749/get/mylog?from=1
    

Options

You can also start KafJS by passing in an option object instead:

kaf.startServer({
  port: port,
  dbfolder: dbfolder,
  ignore_errors: false,
}, err => {
  if(err.code === "LOADFAILED") console.error(err.errors)
  ...
})

If you set ignore_errors parameter to true, KafJS will not fail when it encounters errors while reading the existing db logs. This mimics legacy behaviour where it would ignore parse errors and try it’s best to continue.

Architecture

KafJS stores JSON data in NDJSON formatted records and loads and sends them back. With every GET request it also sends the X-KAFJS-LASTMSGSENT header so users can request the next page of records.

Errors

KafJS stores it’s own logs and errors in a log file called _kafjs. The message format is simple:

[{"t":"2020-11-21T23:32:44.876Z","err":"error message", "stack":"optional"},
 {"t":"2020-11-21T23:32:44.876Z","log":"message"}
]