{
  "name": "ssb-feed-format",
  "version": "2.2.3",
  "description": "devDependency to test feed formats",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ssbc/ssb-feed-format.git"
  },
  "bugs": {
    "url": "https://github.com/ssbc/ssb-feed-format/issues"
  },
  "homepage": "https://github.com/ssbc/ssb-feed-format#readme",
  "author": "Andre Staltz <contact@staltz.com>",
  "contributors": [
    "Anders Rune Jensen <arj03@protonmail.ch>"
  ],
  "license": "LGPL-3.0",
  "keywords": [
    "ssb"
  ],
  "engines": {
    "node": ">=12"
  },
  "main": "index.js",
  "files": [
    "package.json.license",
    "LICENSES/*",
    "*.js"
  ],
  "dependencies": {
    "fast-equals": "^4.0.1",
    "multicb": "^1.2.2",
    "ssb-ref": "^2.16.0"
  },
  "devDependencies": {
    "c8": "^7.11.3",
    "husky": "4.3",
    "prettier": "^2.6.2",
    "pretty-quick": "^3.1.3",
    "ssb-keys": "^8.4.0",
    "ssb-validate2": "^0.1.2",
    "tap-arc": "^0.3.4",
    "tape": "^5.5.3"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run format-code-staged"
    }
  },
  "scripts": {
    "test": "tape test/*.js | tap-arc",
    "format-code": "prettier --write \"*.js\"",
    "format-code-staged": "pretty-quick --staged --pattern \"*.js\"",
    "coverage": "c8 --reporter=lcov npm run test"
  },
  "readme": "<!--\nSPDX-FileCopyrightText: 2022 Andre 'Staltz' Medeiros <contact@staltz.com>\n\nSPDX-License-Identifier: CC0-1.0\n-->\n\n# ssb-feed-format\n\nA tool that you install as a devDependency to check whether your feed format for\nSSB is correct and ready to be installed in ssb-db2.\n\nThis tool helps you create new feed formats that are compatible with ssb-db2 (and maybe one day other databases).\n\n## Installation\n\n```bash\nnpm install --save-dev ssb-feed-format\n```\n\n## Usage\n\n```js\nconst {check} = require('ssb-feed-format')\n\nconst myFeedFormat = {\n  // ...\n}\n\ncheck(\n  // Pass your feed format:\n  myFeedFormat,\n  // Pass a function that generates correct keys for your feed format:\n  () => ssbKeys.generate(null, null, 'myformat')\n  (err) => {\n    // `err` if the format is incorrect, else it is undefined\n  }\n)\n```\n\nYou can also pass extra \"opts\" in case your feed format demands specific ones:\n\n```js\ncheck(myFeedFormat, generateKeys, {myExtraOpt: 123}, (err) => {\n  // ...\n})\n```\n\n## Spec\n\nA **feed format** defines how to create messages that follow a particular *shape*. Every feed format is a plugin-like object with:\n\n- `name`\n- `encodings`\n- Functions to create and convert \"native messages\":\n  - `newNativeMsg`\n  - `toNativeMsg`\n  - `fromNativeMsg`\n- Encryption-related function:\n  - `toPlaintextBuffer`\n  - `fromDecryptedNativeMsg`\n- Helper functions to `get` and check `is`\n  - `getFeedId`\n  - `getMsgId`\n  - `isNativeMsg`\n  - `isAuthor`\n- Validation functions\n  - `validate()` **required**\n  - `validateOOO()` **optional**\n  - `validateBatch()` **optional**\n  - `validateOOOBatch()` **optional**\n\n:star: A **\"native message\"** (also known as `nativeMsg`) is a message owned by the feed format. The shape of the native message can be **whatever** you want for your feed format, but it must be able to **convert** from-and-to encodings such as the classic \"msg.value\" in SSB (a JSON object as specified by the protocol guide).\n\nExamples:\n\n- For \"classic\", a nativeMsg is the traditional JavaScript **object** `msgVal`\n- For \"bendybutt-v1\", a nativeMsg is a BFE-and-bencoded **buffer** with a shape determined by [bendy-butt-spec](https://github.com/ssb-ngi-pointer/bendy-butt-spec)\n\nOn top of that, there are additional restrictions to your feed format. Unfortunately, you can't express *all possible* feed formats with this tool, only a subset which fits well in ssb-db2. The following rules apply:\n\n- The native message MUST convert to the \"JS\" encoding, i.e. a JavaScript object following the [protocol guide's message format](https://ssbc.github.io/scuttlebutt-protocol-guide/#message-format)\n- Lipmaa links are NOT supported, your feed format has to do simple chaining, using the `previous` field\n- A feed ID must always be SSB URIs or classic sigil IDs\n- A message ID must always be SSB URIs or classic sigil IDs\n\n## Fields and functions\n\nYour feed format must include these properties:\n\n### `name`\n\nA **string** to name this format. Try to use computer-friendly names, not human-friendly names. Avoid spaces, and prefer lowercase and short unique names.\n\n### `encodings`\n\nAn **array** of supported encoding names, each as a **string**. This array MUST\nalways include at least the string `'js'`.\n\n### `newNativeMsg(opts)`\n\nMint a new native message and return it, based on the inputs given in the `opts` object. You can typically rely on `opts` containing the fields:\n\n- `opts.keys`: cryptographic keys owning the feed\n- `opts.keys.id`: feed ID (or \"author\" ID)\n- `opts.timestamp`: time the message is/was created. Number of milliseconds since 1 January 1970 00:00 UTC.\n- `opts.content`: free-form JavaScript object for the content part of the message\n- `opts.previous`: JS-encoded \"KVT\" of the previous message on this feed\n- `opts.hmacKey`: optional HMAC key\n\nThe native message returned by this function SHOULD be already \"valid\" and we ASSUME it *would* pass the `validate` function.\n\n### `toNativeMsg(msg, encoding)`\n\nGiven a `msg` encoded with `encoding`, this function should return a native message.\n\nWe recommend that `encoding` is allowed be undefined, in which case it would default to the value `'js'`.\n\n### `fromNativeMsg(nativeMsg, encoding)`\n\nGiven a `nativeMsg`, this function should encode it with `encoding` and return the corresponding encoded message. For instance, typically `encoding` is `'js'` and in that case it should return a classic JavaScript SSB message object.\n\nWe recommend that `encoding` is allowed be undefined, in which case it would default to the value `'js'`.\n\n### `toPlaintextBuffer(opts)`\n\nUseful in the context of encrypting your native message, this function takes `opts` (same as `newNativeMsg`'s input) and should return a Buffer representing the \"plaintext\" of the message's content, ready for encryption.\n\n### `fromDecryptedNativeMsg(plaintextBuf, nativeMsg, encoding)`\n\nUseful in the context of decrypting your native message, this function takes a\n`plaintextBuf` (buffer, already decrypted from the ciphertext), a `nativeMsg`, and the target `encoding`, and should return the corresponding encoded message.\n\nThis is typically used for \"fitting in\" the decrypted \"content\" back into the nativeMsg, as it appeared to be right before it was encrypted.\n\nWe recommend that `encoding` is allowed be undefined, in which case it would default to the value `'js'`.\n\n### `getFeedId(nativeMsg)`\n\nGiven a `nativeMsg`, this function should return a string (either a sigil ID or an SSB URI) representing the feed ID that owns that `nativeMsg`.\n\n### `getMsgId(nativeMsg)`\n\nGiven a `nativeMsg`, this function should return a string (either a sigil ID or an SSB URI) determined as the identifier for the `nativeMsg`.\n\n### `getSequence(nativeMsg)`\n\nGiven a `nativeMsg`, this function should return a non-negative integer determined as the counter for this `nativeMsg` in the feed.\n\n### `isNativeMsg(x)`\n\nGiven any JavaScript value `x`, this function should return `true` when it detects that `x` satisfies all the criteria to be considered a native message belonging to your feed format. Otherwise, should return `false`.\n\n### `isAuthor(author)`\n\nGiven a string (a sigil ID or an SSB URI), this function should return `true` if the string is uniquely belonging to this feed format, otherwise it should return `false`.\n\n### `validate(nativeMsg, previousNativeMsg, hmacKey, cb)`\n\nGiven a `nativeMsg`, its `previousNativeMsg` (in the feed's sequence of messages, also known as the \"latest message\", which COULD be `null`), and an optional `hmacKey`, this function should perform thorough validation of the `nativeMsg`, including cryptographic sig-chain verification. If validation passes, you should call `cb()` with no arguments. If validation failed, you should pass an error `err` when calling `cb(err)`.\n\n### `validateBatch(nativeMsgs, previousNativeMsg, hmacKey, cb)`\n\nTHIS FUNCTION IS OPTIONAL, YOU DON'T NEED TO IMPLEMENT IT.\n\nGiven an **array** of `nativeMsgs`, the `previousNativeMsg`, and an optional `hmacKey`, this function should perform validation of several native messages at once. If validation passes for all of them, call `cb()`. Else, call `cb(err)` with the specific error `err`.\n\n### `validateOOO(nativeMsg, hmacKey, cb)`\n\nTHIS FUNCTION IS OPTIONAL, YOU DON'T NEED TO IMPLEMENT IT.\n\nGiven a `nativeMsg` and an optional `hmacKey`, this function should perform light validation of the `nativeMsg`, except cryptographic sig-chain verification. If validation passes for all of them, call `cb()`. Else, call `cb(err)` with the specific error `err`.\n\n### `validateOOOBatch(nativeMsgs, hmacKey, cb)`\n\nTHIS FUNCTION IS OPTIONAL, YOU DON'T NEED TO IMPLEMENT IT.\n\nGiven an **array** of `nativeMsgs` and an optional `hmacKey`, this function should perform light validation of several native messages at once, except cryptographic sig-chain verification. If validation passes for all of them, call `cb()`. Else, call `cb(err)` with the specific error `err`.\n\n## License\n\nLGPL-3.0-only"
}