├── .eslintignore ├── .nvmrc ├── .prettierignore ├── .gitignore ├── .prettierrc.json ├── typedoc.json ├── .npmignore ├── jest.config.ts ├── .github ├── pull_request_template.md └── workflows │ ├── deploy.yml │ └── js.yml ├── tsconfig.json ├── src ├── index.ts ├── entry.ts ├── operation.ts ├── graphql.ts ├── types.ts └── session.ts ├── .eslintrc.json ├── CHANGELOG.md ├── RELEASE.md ├── package.json ├── test ├── fixtures.json └── session.test.ts ├── README.md ├── rollup.config.ts └── LICENSE /.eslintignore: -------------------------------------------------------------------------------- 1 | /lib 2 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v18.14.2 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | lib 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | docs 2 | lib 3 | node_modules 4 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "trailingComma": "all", 4 | "singleQuote": true 5 | } 6 | -------------------------------------------------------------------------------- /typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://typedoc.org/schema.json", 3 | "sort": ["visibility", "source-order", "alphabetical"] 4 | } 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.tgz 2 | .eslintignore 3 | .eslintrc.json 4 | .nvmrc 5 | .prettierignore 6 | .prettierrc.json 7 | /coverage 8 | docs 9 | jest.config.ts 10 | rollup.config.js 11 | src 12 | test 13 | tsconfig.json 14 | typedoc.json 15 | -------------------------------------------------------------------------------- /jest.config.ts: -------------------------------------------------------------------------------- 1 | import type { Config } from '@jest/types'; 2 | 3 | export default { 4 | moduleNameMapper: { 5 | '^(\\.{1,2}/.*)\\.js$': '$1', 6 | }, 7 | preset: 'ts-jest', 8 | testEnvironment: 'node', 9 | } as Config.InitialOptions; 10 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | Describe your changes here. 2 | 3 | ## 📋 Checklist 4 | 5 | - [ ] Add tests that cover your changes 6 | - [ ] Add this PR to the _Unreleased_ section in `CHANGELOG.md` 7 | - [ ] Link this PR to any issues it closes 8 | - [ ] New files contain a SPDX license header 9 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node18/tsconfig.json", 3 | "compilerOptions": { 4 | "baseUrl": ".", 5 | "outDir": "./lib", 6 | "module": "es2022", 7 | "lib": ["dom"], 8 | "resolveJsonModule": true, 9 | "sourceMap": true, 10 | "inlineSources": true, 11 | "types": ["jest", "node"] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import { OperationFields, KeyPair, initWebAssembly } from 'p2panda-js'; 4 | 5 | import { Session } from './session.js'; 6 | 7 | export { Session, OperationFields, KeyPair, initWebAssembly }; 8 | 9 | export type { Options } from './session.js'; 10 | export type { SchemaId, NextArgs, DocumentViewId, Fields } from './types.js'; 11 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "@typescript-eslint/parser", 3 | "extends": [ 4 | // Applies the recommended rules from @typescript-eslint/eslint-plugin 5 | "plugin:@typescript-eslint/recommended", 6 | // Keep prettier last to make sure its style changes are not overwritten by 7 | // other rules 8 | "plugin:prettier/recommended" 9 | ], 10 | "rules": { 11 | "@typescript-eslint/no-unused-vars": [ 12 | "error", 13 | { "ignoreRestSiblings": true } 14 | ], 15 | // Warn on prettier violations and continue with build 16 | "prettier/prettier": 1 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/entry.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import { signAndEncodeEntry } from 'p2panda-js'; 4 | 5 | import type { EncodedOperation, EntryArgs, PublishVariables } from './types.js'; 6 | 7 | /** 8 | * Sign and publish an entry given an encoded operation, key pair and entry 9 | * arguments (log id, sequence number, backlink and skiplink). 10 | * 11 | * Returns the encoded entry, encoded operation and entry hash. 12 | */ 13 | export function signAndHashEntry( 14 | operation: EncodedOperation, 15 | entryArgs: EntryArgs, 16 | ): PublishVariables { 17 | const { nextArgs, keyPair } = entryArgs; 18 | 19 | const entry = signAndEncodeEntry( 20 | { 21 | ...nextArgs, 22 | operation, 23 | }, 24 | keyPair, 25 | ); 26 | 27 | return { 28 | entry, 29 | operation, 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: pages 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | deploy: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v3 15 | 16 | - name: Setup node 17 | # This action also handles dependency caching for us 18 | uses: actions/setup-node@v3 19 | with: 20 | node-version-file: .nvmrc 21 | 22 | - name: Restore from npm cache 23 | uses: actions/cache@v3 24 | with: 25 | path: ~/.npm 26 | key: ${{ runner.os }}-deploy-${{ hashFiles('**/package-lock.json') }} 27 | 28 | - name: Install dependencies 29 | run: npm ci 30 | 31 | - name: Build documentation page 32 | run: npm run docs 33 | 34 | - name: Deploy website 35 | uses: peaceiris/actions-gh-pages@v3 36 | with: 37 | github_token: ${{ secrets.GITHUB_TOKEN }} 38 | publish_dir: ./docs 39 | 40 | -------------------------------------------------------------------------------- /src/operation.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import { encodeOperation } from 'p2panda-js'; 4 | 5 | import { signAndHashEntry } from './entry.js'; 6 | 7 | import type { 8 | CreateArgs, 9 | DeleteArgs, 10 | EntryArgs, 11 | PublishVariables, 12 | UpdateArgs, 13 | } from './types.js'; 14 | 15 | /** 16 | * Encodes and signs a CREATE operation with the given data fields. 17 | * 18 | * Returns the encoded entry and encoded operation. 19 | */ 20 | export function createOperation( 21 | args: CreateArgs, 22 | entryArgs: EntryArgs, 23 | ): PublishVariables { 24 | const operation = encodeOperation({ 25 | action: 'create', 26 | ...args, 27 | }); 28 | 29 | return signAndHashEntry(operation, entryArgs); 30 | } 31 | 32 | /** 33 | * Encodes and signs an UPDATE operation for the given document view id and 34 | * fields. 35 | * 36 | * Returns the encoded entry and encoded operation. 37 | */ 38 | export function updateOperation( 39 | args: UpdateArgs, 40 | entryArgs: EntryArgs, 41 | ): PublishVariables { 42 | const operation = encodeOperation({ 43 | action: 'update', 44 | ...args, 45 | }); 46 | 47 | return signAndHashEntry(operation, entryArgs); 48 | } 49 | 50 | /** 51 | * Encodes and signs a DELETE operation for the given document view id. 52 | * 53 | * Returns the encoded entry and encoded operation. 54 | */ 55 | export function deleteOperation( 56 | args: DeleteArgs, 57 | entryArgs: EntryArgs, 58 | ): PublishVariables { 59 | const operation = encodeOperation({ 60 | action: 'delete', 61 | ...args, 62 | }); 63 | 64 | return signAndHashEntry(operation, entryArgs); 65 | } 66 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.1.3] 11 | 12 | > Released on 2023-11-16 :package: 13 | 14 | ### Changed 15 | 16 | - Introduce method for creating blobs to `Session` [#28](https://github.com/p2panda/shirokuma/pull/28) 17 | 18 | ## [0.1.2] 19 | 20 | > Released on 2023-11-16 :package: 21 | 22 | ### Changed 23 | 24 | - Use `p2panda-js` version `0.8.0` [#27](https://github.com/p2panda/shirokuma/pull/27) 25 | 26 | ## [0.1.1] 27 | 28 | > Released on 2023-07-25 :package: 29 | 30 | ### Changed 31 | 32 | - Add bundled version for ESM and CJS builds, remove UMD and Node builds [#25](https://github.com/p2panda/shirokuma/pull/25) 33 | - Re-export `OperationFields` from `p2panda-js` [#26](https://github.com/p2panda/shirokuma/pull/26) 34 | 35 | ## [0.1.0] 36 | 37 | > Released on 2023-03-07 :package: 38 | 39 | ### Changed 40 | 41 | - Updates for breaking `p2panda-js` API changes [#4](https://github.com/p2panda/shirokuma/pull/4) 42 | - Update dependencies [#5](https://github.com/p2panda/shirokuma/pull/5) 43 | - Refactor code-base, remove caching layer, add "slim" and "inlined" builds [#12](https://github.com/p2panda/shirokuma/pull/12) 44 | - Write more about different WebAssembly contexts in `README.md` [#19](https://github.com/p2panda/shirokuma/pull/19) 45 | 46 | [unreleased]: https://github.com/p2panda/p2panda/compare/v0.1.3...HEAD 47 | [0.1.3]: https://github.com/p2panda/p2panda/releases/tag/v0.1.3 48 | [0.1.2]: https://github.com/p2panda/p2panda/releases/tag/v0.1.2 49 | [0.1.1]: https://github.com/p2panda/p2panda/releases/tag/v0.1.1 50 | [0.1.0]: https://github.com/p2panda/p2panda/releases/tag/v0.1.0 51 | -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | # Releasing shirokuma 2 | 3 | _This is an example for publising version `1.2.0`._ 4 | 5 | ## Checks and preparations 6 | 7 | 1. Check that the CI has passed on the shirokuma project's 8 | [Github page](https://github.com/p2panda/shirokuma). 9 | 2. Make sure you are on the `main` branch. 10 | 3. Make sure to run `npm install` to have the latest dependencies installed 11 | 4. Run the test suites and make sure all tests pass: `npm run test` and also test `npm run build` 12 | 5. Make sure that all examples in the `README.md` are still up-to-date with the latest API changes. 13 | 14 | ## Changelog time! 15 | 16 | 6. Check the git history for any commits on main that have not been mentioned 17 | in the _Unreleased_ section of `CHANGELOG.md` but should be. 18 | 7. Add an entry in `CHANGELOG.md` for this new release and move over all the 19 | _Unreleased_ stuff. Follow the formatting given by previous entries. 20 | 8. Remember to update the links to your release and the unreleased git log at 21 | the bottom of `CHANGELOG.md`. 22 | 23 | ## Tagging and versioning 24 | 25 | 9. Change the examples in the `README.md` which import `shirokuma` by version to use the latest version 26 | 10. Bump the package version in `package.json` using `npm version [major|minor|patch]` 27 | (this is using [semantic versioning](https://semver.org/)). 28 | 11. Push your changes including your tags using `git push origin main --tags`. 29 | 30 | ## Publishing releases 31 | 32 | 12. Copy the changelog entry you authored into Github's [new release 33 | page](https://github.com/p2panda/shirokuma/releases/new)'s description field. 34 | Title it with your version `v1.2.0`. 35 | 13. Run `npm run build`. 36 | 14. Run `npm pack --dry-run` to check the file listing you are about to publish 37 | doesn't contain any unwanted files. 38 | 15. Run `npm publish` and check the window for any birds outside your window. 39 | -------------------------------------------------------------------------------- /src/graphql.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import { GraphQLClient, gql } from 'graphql-request'; 4 | 5 | import type { NextArgs, NextArgsVariables, PublishVariables } from './types.js'; 6 | 7 | type NextArgsResponse = { 8 | nextArgs: NextArgs; 9 | }; 10 | 11 | type PublishResponse = { 12 | publish: NextArgs; 13 | }; 14 | 15 | /** 16 | * GraphQL query to retrieve arguments to create a new entry from node. 17 | */ 18 | export const GQL_NEXT_ARGS = gql` 19 | query NextArgs($publicKey: String!, $viewId: String) { 20 | nextArgs(publicKey: $publicKey, viewId: $viewId) { 21 | logId 22 | seqNum 23 | backlink 24 | skiplink 25 | } 26 | } 27 | `; 28 | 29 | /** 30 | * GraphQL mutation to publish an entry and operation and retrieve arguments 31 | * for encoding the next one. 32 | */ 33 | export const GQL_PUBLISH = gql` 34 | mutation Publish($entry: String!, $operation: String!) { 35 | publish(entry: $entry, operation: $operation) { 36 | logId 37 | seqNum 38 | backlink 39 | skiplink 40 | } 41 | } 42 | `; 43 | 44 | /** 45 | * Return arguments for constructing the next entry given author and document 46 | * view id. 47 | */ 48 | export async function nextArgs( 49 | client: GraphQLClient, 50 | variables: NextArgsVariables, 51 | ): Promise { 52 | if (!variables) { 53 | throw new Error('Query variables must be provided'); 54 | } 55 | 56 | if (!variables.publicKey) { 57 | throw new Error("Author's public key must be provided"); 58 | } 59 | 60 | const response = await client.request( 61 | GQL_NEXT_ARGS, 62 | variables, 63 | ); 64 | 65 | return response.nextArgs; 66 | } 67 | 68 | /** 69 | * Publish an encoded entry and operation. 70 | */ 71 | export async function publish( 72 | client: GraphQLClient, 73 | variables: PublishVariables, 74 | ): Promise { 75 | if (!variables) { 76 | throw new Error('Query variables must be provided'); 77 | } 78 | 79 | if (!variables.entry || !variables.operation) { 80 | throw new Error('Encoded entry and operation must be provided'); 81 | } 82 | 83 | const response = await client.request( 84 | GQL_PUBLISH, 85 | variables, 86 | ); 87 | 88 | return response.publish; 89 | } 90 | -------------------------------------------------------------------------------- /.github/workflows/js.yml: -------------------------------------------------------------------------------- 1 | name: shirokuma 2 | 3 | on: push 4 | 5 | env: 6 | cache_path: | 7 | ~/.npm 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | matrix: 15 | node: [16, 17, 18, 19] 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v3 20 | 21 | - name: Setup node ${{ matrix.node }} 22 | # This action handles node dependency caching for us 23 | uses: actions/setup-node@v3 24 | with: 25 | node-version: "${{ matrix.node }}" 26 | 27 | - name: Restore from npm cache 28 | uses: actions/cache@v3 29 | with: 30 | path: ${{ env.cache_path }} 31 | # Note that this caching action does NOT cache node dependencies. 32 | # This is done by "actions/setup-node" instead. Here we're caching 33 | # npm artifacts. 34 | key: ${{ runner.os }}-test-${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }} 35 | 36 | - name: Install dependencies 37 | run: npm ci 38 | 39 | - name: Run tests 40 | run: npm run test --coverage 41 | 42 | - if: matrix.node == 16 43 | uses: codecov/codecov-action@v2 44 | 45 | build: 46 | runs-on: ubuntu-latest 47 | 48 | steps: 49 | - name: Checkout repository 50 | uses: actions/checkout@v3 51 | 52 | - name: Setup node 53 | # This action also handles dependency caching for us 54 | uses: actions/setup-node@v3 55 | with: 56 | node-version-file: .nvmrc 57 | 58 | - name: Restore from npm cache 59 | uses: actions/cache@v3 60 | with: 61 | path: ${{ env.cache_path }} 62 | key: ${{ runner.os }}-build-${{ hashFiles('**/package-lock.json') }} 63 | 64 | - name: Install dependencies 65 | run: npm ci 66 | 67 | - name: Run build 68 | run: npm run build 69 | 70 | typecheck: 71 | runs-on: ubuntu-latest 72 | 73 | steps: 74 | - name: Checkout repository 75 | uses: actions/checkout@v3 76 | 77 | - name: Setup node 78 | # This action also handles dependency caching for us 79 | uses: actions/setup-node@v3 80 | with: 81 | node-version-file: .nvmrc 82 | 83 | - name: Restore from npm cache 84 | uses: actions/cache@v3 85 | with: 86 | path: ${{ env.cache_path }} 87 | key: ${{ runner.os }}-tsc-${{ hashFiles('**/package-lock.json') }} 88 | 89 | - name: Install dependencies 90 | run: npm ci 91 | 92 | - name: Check types 93 | run: npm run typecheck 94 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import type { EasyValues, KeyPair, OperationFields } from 'p2panda-js'; 4 | 5 | /** 6 | * Ed25519 public key of author. 7 | */ 8 | export type PublicKey = string; 9 | 10 | /** 11 | * YASMF-BLAKE3 hash of an Bamboo entry. 12 | */ 13 | export type EntryHash = string; 14 | 15 | /** 16 | * Document view id which contains one to many operation ids, either as one 17 | * string separated by underscores or as an array of strings. 18 | */ 19 | export type DocumentViewId = string | string[]; 20 | 21 | /** 22 | * Application- (custom) or System schema (constant) identifier. 23 | */ 24 | export type SchemaId = 25 | | 'schema_definition_v1' 26 | | 'schema_field_definition_v1' 27 | | 'blob_v1' 28 | | 'blob_piece_v1' 29 | | string; 30 | 31 | /** 32 | * Data fields sent within an operation. 33 | */ 34 | export type Fields = EasyValues | OperationFields; 35 | 36 | /** 37 | * Arguments required to create a new document. 38 | */ 39 | export type CreateArgs = { 40 | schemaId: SchemaId; 41 | fields: Fields; 42 | }; 43 | 44 | /** 45 | * Arguments required to update a new document. 46 | */ 47 | export type UpdateArgs = { 48 | schemaId: SchemaId; 49 | previous: DocumentViewId; 50 | fields: Fields; 51 | }; 52 | 53 | /** 54 | * Arguments required to delete a new document. 55 | */ 56 | export type DeleteArgs = { 57 | schemaId: SchemaId; 58 | previous: DocumentViewId; 59 | }; 60 | 61 | /** 62 | * Arguments required to create and sign a new Bamboo entry. 63 | */ 64 | export type EntryArgs = { 65 | keyPair: KeyPair; 66 | nextArgs: NextArgs; 67 | }; 68 | 69 | /** 70 | * Response data from `nextArgs` and `publish` GraphQL query. Contains all the 71 | * important bits to create a new Bamboo entry. 72 | */ 73 | export type NextArgs = { 74 | skiplink?: string; 75 | backlink?: string; 76 | seqNum: string | bigint | number; 77 | logId: string | bigint | number; 78 | }; 79 | 80 | /** 81 | * Bamboo entry bytes, encoded as hexadecimal string. 82 | */ 83 | export type EncodedEntry = string; 84 | 85 | /** 86 | * CBOR operation bytes, encoded as hexadecimal string. 87 | */ 88 | export type EncodedOperation = string; 89 | 90 | /** 91 | * To-be-published entry and operation data. 92 | */ 93 | export type Payload = { 94 | entry: EncodedEntry; 95 | operation: EncodedOperation; 96 | }; 97 | 98 | /** 99 | * To-be-published entry and operation data plus additional information about 100 | * the current, local document view id 101 | */ 102 | export type PayloadWithViewId = Payload & { 103 | localViewId: EntryHash; 104 | }; 105 | 106 | /** 107 | * Request data for `nextArgs` GraphQL query. 108 | */ 109 | export type NextArgsVariables = { 110 | publicKey: PublicKey; 111 | viewId?: DocumentViewId; 112 | }; 113 | 114 | /** 115 | * Request data for `publish` GraphQL mutation. 116 | */ 117 | export type PublishVariables = Payload; 118 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shirokuma", 3 | "version": "0.1.3", 4 | "description": "TypeScript SDK to easily write p2panda applications", 5 | "type": "module", 6 | "main": "./lib/cjs/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "exports": { 9 | ".": { 10 | "types": "./lib/cjs/index.d.ts", 11 | "require": "./lib/cjs/index.cjs", 12 | "import": "./lib/esm/index.js" 13 | }, 14 | "./slim": { 15 | "types": "./lib/cjs-slim/index.d.ts", 16 | "require": "./lib/node/index.cjs", 17 | "import": "./lib/esm-slim/index.js" 18 | }, 19 | "./bundle": { 20 | "types": "./lib/cjs-bundle/index.d.ts", 21 | "require": "./lib/cjs-bundle/index.cjs", 22 | "import": "./lib/esm-bundle/index.js" 23 | }, 24 | "./p2panda.wasm": "./lib/p2panda.wasm", 25 | "./package.json": "./package.json" 26 | }, 27 | "scripts": { 28 | "build": "cross-env NODE_ENV=production run-s clear rollup", 29 | "clear": "rimraf ./lib", 30 | "docs": "typedoc src/index.ts", 31 | "lint": "eslint --ext .ts .", 32 | "rollup": "rollup -c rollup.config.ts --configPlugin typescript", 33 | "test": "cross-env NODE_ENV=development jest --coverage=$npm_config_coverage", 34 | "test:watch": "nodemon --watch '../p2panda-rs/src/*' --watch './src/*' --exec 'npm test' --ext js,ts,json", 35 | "typecheck": "tsc --noEmit" 36 | }, 37 | "engines": { 38 | "node": ">= v18" 39 | }, 40 | "files": [ 41 | "lib" 42 | ], 43 | "contributors": [ 44 | "adz ", 45 | "cafca ", 46 | "sandreae " 47 | ], 48 | "license": "AGPL-3.0-or-later", 49 | "repository": { 50 | "type": "git", 51 | "url": "git+https://github.com/p2panda/shirokuma.git" 52 | }, 53 | "bugs": { 54 | "url": "https://github.com/p2panda/shirokuma/issues" 55 | }, 56 | "homepage": "https://github.com/p2panda/shirokuma#readme", 57 | "devDependencies": { 58 | "@rollup/plugin-alias": "^5.0.0", 59 | "@rollup/plugin-commonjs": "^25.0.3", 60 | "@rollup/plugin-node-resolve": "^15.1.0", 61 | "@rollup/plugin-terser": "^0.4.3", 62 | "@rollup/plugin-typescript": "^11.1.2", 63 | "@tsconfig/node18": "^18.2.0", 64 | "@types/jest": "^29.5.3", 65 | "@types/node": "^20.4.2", 66 | "@typescript-eslint/eslint-plugin": "^6.1.0", 67 | "@typescript-eslint/parser": "^6.1.0", 68 | "cross-env": "^7.0.3", 69 | "eslint": "^8.45.0", 70 | "eslint-config-prettier": "^8.8.0", 71 | "eslint-plugin-prettier": "^5.0.0", 72 | "fetch-mock-jest": "^1.5.1", 73 | "jest": "^29.6.1", 74 | "nodemon": "^3.0.1", 75 | "npm-run-all": "^4.1.5", 76 | "prettier": "^3.0.0", 77 | "rimraf": "^5.0.1", 78 | "rollup": "^3.26.3", 79 | "rollup-plugin-dts": "^5.3.0", 80 | "ts-jest": "^29.1.1", 81 | "ts-node": "^10.9.1", 82 | "tslib": "^2.6.0", 83 | "typedoc": "^0.24.8", 84 | "typescript": "^5.1.6" 85 | }, 86 | "dependencies": { 87 | "graphql-request": "^6.1.0", 88 | "graphql-web-lite": "^16.6.0-4", 89 | "p2panda-js": "^0.8.0" 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /test/fixtures.json: -------------------------------------------------------------------------------- 1 | { 2 | "entries": [ 3 | { 4 | "encodedEntry": "00b8556f266f2f05df21f4f633d0498bf3dfbb79cad3453367c3002725b1a88fd800016e0020b7c3bb56feeaef9e1d9b6f231bc1fb0eee23b3c489d02fc1df71574561e01189789e3be9455fd2a5e128e2f9c216070ee1fba1e8f8f08ccbe1d1eab19f90eb7b213bdade3173ba8592a7c5b471a7af596678fc0b7cc2a9699da34219d539c709", 5 | "entryHash": "0020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d115388", 6 | "encodedOperation": "8401007849636861745f3030323062633837346334303161643539383336346265383064313863613565636632663865393764306362353737343533386636326136663038633162346663653534a1676d657373616765764f68682c206d79206669727374206d65737361676521", 7 | "logId": "0", 8 | "seqNum": "1" 9 | }, 10 | { 11 | "encodedEntry": "00b8556f266f2f05df21f4f633d0498bf3dfbb79cad3453367c3002725b1a88fd800020020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d1153888f002035a367e9aceb09c4649e11a22a42378a270a2b1dea49d94f45ae02e8318edc813f6ac1b56b185a18eb549d245fb33e6d2ca91c445a7e5c139694d45f343793d980465583d1c291e0138dd22a4d6dcb6b328c577a2d1379aadd3c46c6fca47c09", 12 | "entryHash": "00206cb3c137ab58b7fbee24a9691dfcaa346238caa39e712d57bd63ba2a9b62d312", 13 | "encodedOperation": "8501017849636861745f30303230626338373463343031616435393833363462653830643138636135656366326638653937643063623537373435333866363261366630386331623466636535348158220020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d115388a1676d6573736167657257686963682049206e6f7720757064617465", 14 | "logId": "0", 15 | "seqNum": "2" 16 | }, 17 | { 18 | "encodedEntry": "00b8556f266f2f05df21f4f633d0498bf3dfbb79cad3453367c3002725b1a88fd8000300206cb3c137ab58b7fbee24a9691dfcaa346238caa39e712d57bd63ba2a9b62d312920020172ad281f0e66588a94a3d76fdafe110e5b9434a51696452fa87c4ba9f56039b2e5a83d6e584cda4644f59022bd9ce5e9f72f98d22db79d1e89ed443bf8a334fbe6ebcf8da7f3597598888edb529d2cc0f9522db342bbe6da4a1415ded717d00", 19 | "entryHash": "0020b5aed62a76d1e2c385e5e7ae42b057248c706473a3c5a83842e5b4feff746229", 20 | "encodedOperation": "8501017849636861745f303032306263383734633430316164353938333634626538306431386361356563663266386539376430636235373734353338663632613666303863316234666365353481582200206cb3c137ab58b7fbee24a9691dfcaa346238caa39e712d57bd63ba2a9b62d312a1676d65737361676575416e64207468656e2075706461746520616761696e", 21 | "logId": "0", 22 | "seqNum": "3" 23 | }, 24 | { 25 | "encodedEntry": "00b8556f266f2f05df21f4f633d0498bf3dfbb79cad3453367c3002725b1a88fd800040020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d1153880020b5aed62a76d1e2c385e5e7ae42b057248c706473a3c5a83842e5b4feff746229730020ae298e5cdcf06c471a03513e028452bc0e560f056fb66760471a7c7e9cfe1ed78388ff89e26b563865e0619c1bcd1e4e7db12db968c15d14d5bf2c0e9c2ab76cd148d1cb7678725dc2842a308704c0284eb6483d002b3f22877501b3dfd41c03", 26 | "entryHash": "00208cb01c0f5819812476555fbeab9dd3ab2f1c6bc441f14d9162dd2078d60c9873", 27 | "encodedOperation": "8401027849636861745f30303230626338373463343031616435393833363462653830643138636135656366326638653937643063623537373435333866363261366630386331623466636535348158220020b5aed62a76d1e2c385e5e7ae42b057248c706473a3c5a83842e5b4feff746229", 28 | "logId": "0", 29 | "seqNum": "4" 30 | } 31 | ], 32 | "nextArgs": [ 33 | { 34 | "backlink": null, 35 | "logId": "0", 36 | "seqNum": "1", 37 | "skiplink": null 38 | }, 39 | { 40 | "backlink": "0020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d115388", 41 | "logId": "0", 42 | "seqNum": "2", 43 | "skiplink": null 44 | }, 45 | { 46 | "backlink": "00206cb3c137ab58b7fbee24a9691dfcaa346238caa39e712d57bd63ba2a9b62d312", 47 | "logId": "0", 48 | "seqNum": "3", 49 | "skiplink": null 50 | }, 51 | { 52 | "backlink": "0020b5aed62a76d1e2c385e5e7ae42b057248c706473a3c5a83842e5b4feff746229", 53 | "logId": "0", 54 | "seqNum": "4", 55 | "skiplink": "0020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d115388" 56 | }, 57 | { 58 | "backlink": "00208cb01c0f5819812476555fbeab9dd3ab2f1c6bc441f14d9162dd2078d60c9873", 59 | "logId": "0", 60 | "seqNum": "5", 61 | "skiplink": null 62 | } 63 | ], 64 | "operations": [ 65 | { 66 | "action": "create", 67 | "fields": { 68 | "message": "Ohh, my first message!" 69 | }, 70 | "schemaId": "chat_0020bc874c401ad598364be80d18ca5ecf2f8e97d0cb5774538f62a6f08c1b4fce54", 71 | "version": 1 72 | }, 73 | { 74 | "action": "update", 75 | "fields": { 76 | "message": "Which I now update" 77 | }, 78 | "previous": [ 79 | "0020e5350b648e798644f6f359976646a19defc3aa7f2ceecf9a675383b13d115388" 80 | ], 81 | "schemaId": "chat_0020bc874c401ad598364be80d18ca5ecf2f8e97d0cb5774538f62a6f08c1b4fce54", 82 | "version": 1 83 | }, 84 | { 85 | "action": "update", 86 | "fields": { 87 | "message": "And then update again" 88 | }, 89 | "previous": [ 90 | "00206cb3c137ab58b7fbee24a9691dfcaa346238caa39e712d57bd63ba2a9b62d312" 91 | ], 92 | "schemaId": "chat_0020bc874c401ad598364be80d18ca5ecf2f8e97d0cb5774538f62a6f08c1b4fce54", 93 | "version": 1 94 | }, 95 | { 96 | "action": "delete", 97 | "previous": [ 98 | "0020b5aed62a76d1e2c385e5e7ae42b057248c706473a3c5a83842e5b4feff746229" 99 | ], 100 | "schemaId": "chat_0020bc874c401ad598364be80d18ca5ecf2f8e97d0cb5774538f62a6f08c1b4fce54", 101 | "version": 1 102 | } 103 | ], 104 | "privateKey": "80117cd1732b94fd897708d4826f1f6f8863efc3c70fdec4350541f00270eca0", 105 | "publicKey": "b8556f266f2f05df21f4f633d0498bf3dfbb79cad3453367c3002725b1a88fd8", 106 | "schemaId": "chat_0020bc874c401ad598364be80d18ca5ecf2f8e97d0cb5774538f62a6f08c1b4fce54" 107 | } 108 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

shirokuma

2 | 3 |
4 | 5 | TypeScript framework to easily write p2panda applications 6 | 7 |
8 | 9 |
10 | 11 |
12 | 13 | 14 | CI Status 15 | 16 | 17 | 18 | NPM version 19 | 20 |
21 | 22 | 37 | 38 |
39 | 40 | This library provides all tools required to write an [`p2panda`] application in TypeScript, running in any modern web browser. 41 | 42 | [`p2panda`]: https://aquadoggo.p2panda.org/ 43 | 44 | ## Installation 45 | 46 | To install `shirokuma` run: 47 | 48 | ``` 49 | npm i shirokuma 50 | ``` 51 | 52 | ## Example 53 | 54 | ```typescript 55 | import { KeyPair, Session, initWebAssembly } from 'shirokuma'; 56 | 57 | // This method needs to be run once before to initialise the embedded 58 | // WebAssembly code in this package 59 | await initWebAssembly(); 60 | 61 | // This example uses the "chat" schema at which this hash is pointing 62 | const CHAT_SCHEMA = 63 | 'chat_message_0020a654068b26617ebd6574b1b03853193ccab2295a983bc85a5891793422832655'; 64 | 65 | // Create a key pair 66 | const keyPair = new KeyPair(); 67 | 68 | // Open a long running connection to a p2panda node and configure it so all 69 | // calls in this session are executed using that key pair 70 | const session = new Session('http://localhost:2020/graphql').setKeyPair(keyPair); 71 | 72 | // Compose your operation payload, according to chosen schema 73 | const fields = { 74 | message: 'Hi there', 75 | }; 76 | 77 | // Create and send a new chat message to the node 78 | await session.create(fields, { schemaId: CHAT_SCHEMA }); 79 | ``` 80 | 81 | ## Usage 82 | 83 | `shirokuma` runs both in NodeJS and web browsers and comes as a ES and CommonJS 84 | module. It can easily be integrated into Webpack, Rollup or other tools. 85 | 86 | Since `shirokuma` contains WebAssembly code, it is necessary to initialise it 87 | before using the methods in the Browser. This initialisation step is not 88 | required in NodeJS contexts. 89 | 90 | To make this step a little bit easier `shirokuma` inlines the WebAssembly code 91 | as a base64 string which gets decoded automatically during initialisation. For 92 | manual initialisation the package also comes with "slim" versions where you 93 | need to provide a path to the ".wasm" file yourself. 94 | 95 | ### Browser 96 | 97 | To quickly get started, you can run `shirokuma` in any modern browser as an ES module like that. Note that this uses the bundled version, with all 3rd party dependencies included plus the WebAssembly code itself: 98 | 99 | ```html 100 | 108 | ``` 109 | 110 | ### NodeJS 111 | 112 | ```javascript 113 | import { KeyPair } from 'shirokuma'; 114 | const keyPair = new KeyPair(); 115 | console.log(keyPair.publicKey()); 116 | ``` 117 | 118 | ### Bundlers 119 | 120 | ```javascript 121 | import { initWebAssembly, KeyPair } from 'shirokuma'; 122 | 123 | // This only needs to be done once before using all `shirokuma` methods 124 | await initWebAssembly(); 125 | 126 | const keyPair = new KeyPair(); 127 | console.log(keyPair.publicKey()); 128 | ``` 129 | 130 | ### Manually load `.wasm` 131 | 132 | Running `shirokuma` automatically inlines the WebAssembly inside the JavaScript 133 | file, encoded as a base64 string. While this works for most developers, it also 134 | doubles the size of the imported file. To avoid larger payloads and decoding 135 | times you can load the `.wasm` file manually by using a "slim" version. For 136 | this you need to initialise the module by passing the path to the file into 137 | `initWebAssembly`: 138 | 139 | ```javascript 140 | // Import from `slim` module to manually initialise WebAssembly code 141 | import { initWebAssembly, KeyPair } from 'shirokuma/slim'; 142 | import wasm from 'shirokuma/p2panda.wasm'; 143 | 144 | // When running shirokuma in the browser, this method needs to run once 145 | // before using all other methods 146 | await initWebAssembly(wasm); 147 | 148 | const keyPair = new KeyPair(); 149 | console.log(keyPair.publicKey()); 150 | ``` 151 | 152 | ## Development 153 | 154 | ```bash 155 | # Install dependencies 156 | npm install 157 | 158 | # Check code formatting 159 | npm run lint 160 | 161 | # Run tests 162 | npm test 163 | 164 | # Bundle js package 165 | npm run build 166 | ``` 167 | 168 | ### Documentation 169 | 170 | ```bash 171 | # Generate documentation 172 | npm run docs 173 | 174 | # Show documentation in browser 175 | npx serve ./docs 176 | ``` 177 | 178 | ## License 179 | 180 | GNU Affero General Public License v3.0 [`AGPL-3.0-or-later`](LICENSE) 181 | 182 | ## Supported by 183 | 184 |
185 |
186 | 187 | 188 | *This project has received funding from the European Union’s Horizon 2020 189 | research and innovation programme within the framework of the NGI-POINTER 190 | Project funded under grant agreement No 871528* 191 | -------------------------------------------------------------------------------- /rollup.config.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import fs from 'fs'; 4 | import path from 'path'; 5 | 6 | import pluginAlias from '@rollup/plugin-alias'; 7 | import pluginCommonJS from '@rollup/plugin-commonjs'; 8 | import pluginDts from 'rollup-plugin-dts'; 9 | import pluginTerser from '@rollup/plugin-terser'; 10 | import pluginTypeScript from '@rollup/plugin-typescript'; 11 | import { nodeResolve as pluginNodeResolve } from '@rollup/plugin-node-resolve'; 12 | 13 | import type { 14 | RollupOptions, 15 | Plugin, 16 | ModuleFormat, 17 | OutputOptions, 18 | } from 'rollup'; 19 | 20 | type BuildMode = 'inline' | 'slim' | 'bundle'; 21 | 22 | type BuildName = string; 23 | 24 | type Config = { 25 | format: ModuleFormat; 26 | mode: BuildMode; 27 | }; 28 | 29 | const SRC_DIR = 'src'; 30 | const DIST_DIR = 'lib'; 31 | const BUILD_FILE_NAME = 'index'; 32 | const USE_SOURCEMAP = true; 33 | 34 | // Helper to load `package.json` file 35 | const pkg = JSON.parse( 36 | fs.readFileSync(new URL('./package.json', import.meta.url), { 37 | encoding: 'utf8', 38 | }), 39 | ); 40 | 41 | // Plugin to copy `p2panda.wasm` file from `p2panda-js` into export. 42 | function pluginCopyWasm(): Plugin { 43 | return { 44 | name: 'copy-wasm', 45 | resolveImportMeta: () => `""`, 46 | generateBundle() { 47 | fs.mkdirSync(DIST_DIR, { recursive: true }); 48 | fs.copyFileSync( 49 | path.resolve('./node_modules/p2panda-js/lib/p2panda.wasm'), 50 | path.resolve(`${DIST_DIR}/p2panda.wasm`), 51 | ); 52 | }, 53 | }; 54 | } 55 | 56 | // Returns the name of the sub-directory which will be created in the target 57 | // folder for each build. 58 | function getBuildName({ format, mode }: Config): BuildName { 59 | if (mode === 'bundle') { 60 | return `${format}-bundle`; 61 | } else if (mode === 'inline') { 62 | return format; 63 | } else { 64 | return `${format}-slim`; 65 | } 66 | } 67 | 68 | // Returns the output file options for each build. 69 | function getOutputs({ format, mode }: Config): OutputOptions[] { 70 | const result: OutputOptions[] = []; 71 | 72 | const dirName = getBuildName({ format, mode }); 73 | const sourcemap = USE_SOURCEMAP; 74 | 75 | // Determine suffix of output files. For CommonJS builds we choose `.cjs`. 76 | const ext = format === 'cjs' ? 'cjs' : 'js'; 77 | 78 | result.push({ 79 | name: pkg.name, 80 | file: `${DIST_DIR}/${dirName}/${BUILD_FILE_NAME}.${ext}`, 81 | format, 82 | sourcemap, 83 | }); 84 | 85 | // Provide a minified version for all builds 86 | result.push({ 87 | name: pkg.name, 88 | file: `${DIST_DIR}/${dirName}/${BUILD_FILE_NAME}.min.js`, 89 | format, 90 | sourcemap, 91 | // @ts-expect-error: TypeScript misinterprets the module configuration 92 | plugins: [pluginTerser()], 93 | }); 94 | 95 | return result; 96 | } 97 | 98 | function getPlugins({ mode }: Config): Plugin[] { 99 | const result: Plugin[] = []; 100 | 101 | // Convert external CommonJS- to ES6 modules 102 | result.push( 103 | // @ts-expect-error: TypeScript misinterprets the module configuration 104 | pluginCommonJS({ 105 | extensions: ['.js', '.ts'], 106 | }), 107 | ); 108 | 109 | // In umd builds we're bundling the dependencies as well, we need this plugin 110 | // here to help locating external dependencies 111 | if (mode === 'bundle') { 112 | result.push( 113 | pluginNodeResolve({ 114 | // Use the "browser" module resolutions in the dependencies' package.json 115 | browser: true, 116 | }), 117 | ); 118 | } 119 | 120 | // graphql-web-lite provides an alias package that can be swapped in for 121 | // the standard graphql package in client-side applications. It aims to 122 | // reduce the size of imports that are in common use by GraphQL clients 123 | // and users, while still providing most graphql exports that are used 124 | // in other contexts. 125 | const entries = [{ find: 'graphql', replacement: 'graphql-web-lite' }]; 126 | 127 | // Whenever we want to build a "slim" version of shirokuma we have to import 128 | // the "slim" version of p2panda-js. 129 | // 130 | // The "slim" version does not contain the WebAssembly inlined (as a base64 131 | // string) and is therefore smaller. 132 | if (mode === 'slim') { 133 | entries.push({ find: 'p2panda-js', replacement: 'p2panda-js/slim' }); 134 | } 135 | 136 | result.push( 137 | // @ts-expect-error: TypeScript misinterprets the module configuration 138 | pluginAlias({ 139 | entries, 140 | }), 141 | ); 142 | 143 | // Compile TypeScript source code to JavaScript 144 | // @ts-expect-error: TypeScript misinterprets the module configuration 145 | result.push(pluginTypeScript()); 146 | 147 | // We only need to copy this once 148 | result.push(pluginCopyWasm()); 149 | 150 | return result; 151 | } 152 | 153 | function config({ format, mode }: Config): RollupOptions[] { 154 | const result: RollupOptions[] = []; 155 | 156 | // Determine entry point in `src` 157 | const input = `./${SRC_DIR}/index.ts`; 158 | 159 | // Determine where files of this build get written to 160 | const output = getOutputs({ format, mode }); 161 | 162 | // Determine plugins which will be used to process this build 163 | const plugins = getPlugins({ format, mode }); 164 | 165 | // We build modules in two versions: 1. with no 3rd party dependencies included 166 | // and 2. bundled inside of them. 167 | // 168 | // Bundling them is somewhat a shame, but we hope that this will account for 169 | // the "quick" uses of shirokuma. 170 | const external = mode === 'bundle' ? [] : Object.keys(pkg.dependencies); 171 | 172 | // Package build 173 | result.push({ 174 | input, 175 | output, 176 | plugins, 177 | external, 178 | }); 179 | 180 | // Generate TypeScript definition file for each build 181 | const dirName = getBuildName({ format, mode }); 182 | result.push({ 183 | input, 184 | output: { 185 | file: `${DIST_DIR}/${dirName}/${BUILD_FILE_NAME}.d.ts`, 186 | format, 187 | }, 188 | plugins: [pluginDts()], 189 | }); 190 | 191 | return result; 192 | } 193 | 194 | export default [ 195 | ...config({ 196 | format: 'cjs', 197 | mode: 'inline', 198 | }), 199 | ...config({ 200 | format: 'cjs', 201 | mode: 'slim', 202 | }), 203 | ...config({ 204 | format: 'cjs', 205 | mode: 'bundle', 206 | }), 207 | ...config({ 208 | format: 'esm', 209 | mode: 'inline', 210 | }), 211 | ...config({ 212 | format: 'esm', 213 | mode: 'slim', 214 | }), 215 | ...config({ 216 | format: 'esm', 217 | mode: 'bundle', 218 | }), 219 | ]; 220 | -------------------------------------------------------------------------------- /test/session.test.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | /* eslint-disable @typescript-eslint/ban-ts-comment */ 4 | 5 | import fetchMockJest from 'fetch-mock-jest'; 6 | 7 | import fixtures from './fixtures.json'; 8 | 9 | import { Session, KeyPair } from '../src/index.js'; 10 | import { GQL_PUBLISH, GQL_NEXT_ARGS } from '../src/graphql.js'; 11 | 12 | import type { DocumentViewId, Fields } from '../src/types.js'; 13 | import type { FetchMockStatic } from 'fetch-mock'; 14 | 15 | /** 16 | * Set up GraphQL server mock. It will respond to: 17 | * - query `nextArgs`: always returns entry args for sequence number 6 18 | * - mutation `publish` always returns a response as if sequence number 5 had 19 | * been published. 20 | */ 21 | jest.mock('node-fetch', () => fetchMockJest.sandbox()); 22 | // eslint-disable-next-line @typescript-eslint/no-var-requires 23 | const fetchMock: FetchMockStatic = require('node-fetch'); 24 | 25 | // @ts-ignore 26 | fetchMock.config.matchPartialBody = true; 27 | 28 | describe('Session', () => { 29 | it('requires an endpoint parameter', () => { 30 | expect(() => { 31 | // @ts-ignore: We deliberately use the API wrong here 32 | new Session(); 33 | }).toThrow('Missing `endpoint` parameter for creating a session'); 34 | 35 | expect(() => { 36 | new Session(''); 37 | }).toThrow('Missing `endpoint` parameter for creating a session'); 38 | }); 39 | }); 40 | 41 | describe('nextArgs', () => { 42 | beforeEach(() => { 43 | fetchMock.mock( 44 | { 45 | name: 'nextArgs', 46 | url: 'http://localhost:2020/graphql', 47 | body: { 48 | query: GQL_NEXT_ARGS, 49 | variables: { 50 | publicKey: fixtures.publicKey, 51 | viewId: fixtures.entries[0].entryHash, 52 | }, 53 | }, 54 | }, 55 | { 56 | data: { 57 | nextArgs: fixtures.nextArgs[4], 58 | }, 59 | }, 60 | ); 61 | }); 62 | 63 | afterEach(() => { 64 | // @ts-ignore 65 | fetchMock.mockReset(); 66 | }); 67 | 68 | it('returns next entry arguments from node', async () => { 69 | const session = new Session('http://localhost:2020/graphql'); 70 | 71 | const nextArgs = await session.nextArgs( 72 | fixtures.publicKey, 73 | fixtures.entries[0].entryHash, 74 | ); 75 | const expectedArgs = fixtures.nextArgs[4]; 76 | expect(nextArgs.skiplink).toEqual(expectedArgs.skiplink); 77 | expect(nextArgs.backlink).toEqual(expectedArgs.backlink); 78 | expect(nextArgs.seqNum).toEqual(expectedArgs.seqNum); 79 | expect(nextArgs.logId).toEqual(expectedArgs.logId); 80 | }); 81 | }); 82 | 83 | describe('publish', () => { 84 | beforeEach(() => { 85 | fetchMock.mock( 86 | { 87 | name: 'publish', 88 | url: 'http://localhost:2020/graphql', 89 | body: { 90 | query: GQL_PUBLISH, 91 | variables: { 92 | entry: fixtures.entries[3].encodedEntry, 93 | operation: fixtures.entries[3].encodedOperation, 94 | }, 95 | }, 96 | }, 97 | { 98 | data: { 99 | nextArgs: fixtures.nextArgs[4], 100 | }, 101 | }, 102 | ); 103 | }); 104 | 105 | afterEach(() => { 106 | // @ts-ignore 107 | fetchMock.mockReset(); 108 | }); 109 | 110 | it('can publish entries and retreive the next document view id', async () => { 111 | const session = new Session('http://localhost:2020/graphql').setKeyPair( 112 | new KeyPair(fixtures.privateKey), 113 | ); 114 | 115 | const viewId = await session.publish( 116 | fixtures.entries[3].encodedEntry, 117 | fixtures.entries[3].encodedOperation, 118 | ); 119 | 120 | expect(viewId).toEqual(fixtures.entries[3].entryHash); 121 | }); 122 | 123 | it('throws when publishing without all required parameters', async () => { 124 | const session = new Session('http://localhost:2020/graphql').setKeyPair( 125 | new KeyPair(fixtures.privateKey), 126 | ); 127 | 128 | await expect( 129 | // @ts-ignore: We deliberately use the API wrong here 130 | session.publish(null, fixtures.entries[3].encodedOperation), 131 | ).rejects.toThrow(); 132 | await expect( 133 | // @ts-ignore: We deliberately use the API wrong here 134 | session.publish(fixtures.entries[3].encodedEntry, null), 135 | ).rejects.toThrow(); 136 | }); 137 | }); 138 | 139 | describe('create', () => { 140 | let session: Session; 141 | 142 | beforeEach(() => { 143 | fetchMock 144 | .mock( 145 | { 146 | name: 'nextArgs', 147 | url: 'http://localhost:2020/graphql', 148 | body: { query: GQL_NEXT_ARGS }, 149 | }, 150 | { 151 | data: { 152 | nextArgs: fixtures.nextArgs[0], 153 | }, 154 | }, 155 | ) 156 | .mock( 157 | { 158 | name: 'publish', 159 | url: 'http://localhost:2020/graphql', 160 | body: { query: GQL_PUBLISH }, 161 | }, 162 | { 163 | data: { 164 | nextArgs: fixtures.nextArgs[1], 165 | }, 166 | }, 167 | ); 168 | }); 169 | 170 | afterEach(() => { 171 | // @ts-ignore 172 | fetchMock.mockReset(); 173 | }); 174 | 175 | beforeEach(async () => { 176 | session = new Session('http://localhost:2020/graphql'); 177 | session.setKeyPair(new KeyPair(fixtures.privateKey)); 178 | }); 179 | 180 | it('returns the id of the created document', async () => { 181 | const fields = fixtures.operations[0].fields as Fields; 182 | 183 | await expect( 184 | session.create(fields, { 185 | schemaId: fixtures.schemaId, 186 | }), 187 | ).resolves.toBe(fixtures.entries[0].entryHash); 188 | }); 189 | 190 | it('throws when missing a required parameter', async () => { 191 | await expect( 192 | // @ts-ignore: We deliberately use the API wrong here 193 | session.setKeyPair(new KeyPair()).create(), 194 | ).rejects.toThrow(); 195 | }); 196 | }); 197 | 198 | describe('update', () => { 199 | let session: Session; 200 | 201 | beforeEach(() => { 202 | fetchMock 203 | .mock( 204 | { 205 | name: 'nextArgs', 206 | url: 'http://localhost:2020/graphql', 207 | body: { query: GQL_NEXT_ARGS }, 208 | }, 209 | { 210 | data: { 211 | nextArgs: fixtures.nextArgs[1], 212 | }, 213 | }, 214 | ) 215 | .mock( 216 | { 217 | name: 'publish', 218 | url: 'http://localhost:2020/graphql', 219 | body: { query: GQL_PUBLISH }, 220 | }, 221 | { 222 | data: { 223 | nextArgs: fixtures.nextArgs[2], 224 | }, 225 | }, 226 | ); 227 | }); 228 | 229 | afterEach(() => { 230 | // @ts-ignore 231 | fetchMock.mockReset(); 232 | }); 233 | 234 | beforeEach(async () => { 235 | session = new Session('http://localhost:2020/graphql'); 236 | session.setKeyPair(new KeyPair(fixtures.privateKey)); 237 | }); 238 | 239 | it('returns the local view id of the updated document', async () => { 240 | const fields = fixtures.operations[1].fields as Fields; 241 | const previous = fixtures.operations[1].previous as DocumentViewId; 242 | 243 | await expect( 244 | session.update(fields, previous, { 245 | schemaId: fixtures.schemaId, 246 | }), 247 | ).resolves.toBe(fixtures.entries[1].entryHash); 248 | }); 249 | 250 | it('throws when missing a required parameter', async () => { 251 | await expect( 252 | // @ts-ignore: We deliberately use the API wrong here 253 | session.update(null, { schemaId: fixtures.schemaId }), 254 | ).rejects.toThrow(); 255 | 256 | await expect( 257 | // @ts-ignore: We deliberately use the API wrong here 258 | session.update({ 259 | message: 'Doing it wrong', 260 | }), 261 | ).rejects.toThrow(); 262 | }); 263 | }); 264 | 265 | describe('delete', () => { 266 | let session: Session; 267 | 268 | beforeEach(() => { 269 | fetchMock 270 | .mock( 271 | { 272 | name: 'nextArgs', 273 | url: 'http://localhost:2020/graphql', 274 | body: { query: GQL_NEXT_ARGS }, 275 | }, 276 | { 277 | data: { 278 | nextArgs: fixtures.nextArgs[3], 279 | }, 280 | }, 281 | ) 282 | .mock( 283 | { 284 | name: 'publish', 285 | url: 'http://localhost:2020/graphql', 286 | body: { query: GQL_PUBLISH }, 287 | }, 288 | { 289 | data: { 290 | nextArgs: fixtures.nextArgs[4], 291 | }, 292 | }, 293 | ); 294 | }); 295 | 296 | afterEach(() => { 297 | // @ts-ignore 298 | fetchMock.mockReset(); 299 | }); 300 | 301 | beforeEach(async () => { 302 | session = new Session('http://localhost:2020/graphql'); 303 | session.setKeyPair(new KeyPair(fixtures.privateKey)); 304 | }); 305 | 306 | it('returns the local view id of the deleted document', async () => { 307 | const previous = fixtures.operations[3].previous as string[]; 308 | 309 | await expect( 310 | session.delete(previous, { 311 | schemaId: fixtures.schemaId, 312 | }), 313 | ).resolves.toBe(fixtures.entries[3].entryHash); 314 | 315 | expect(await session.setSchemaId(fixtures.schemaId).delete(previous)) 316 | .resolves; 317 | }); 318 | 319 | it('throws when missing a required parameter', async () => { 320 | await expect( 321 | // @ts-ignore: We deliberately use the API wrong here 322 | session.delete(null, { schemaId: fixtures.schemaId }), 323 | ).rejects.toThrow(); 324 | 325 | await expect( 326 | // @ts-ignore: We deliberately use the API wrong here 327 | session.delete(null), 328 | ).rejects.toThrow(); 329 | }); 330 | }); 331 | -------------------------------------------------------------------------------- /src/session.ts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | import { GraphQLClient } from 'graphql-request'; 4 | import { generateHash, KeyPair, OperationFields } from 'p2panda-js'; 5 | 6 | import { 7 | createOperation, 8 | deleteOperation, 9 | updateOperation, 10 | } from './operation.js'; 11 | import { nextArgs, publish } from './graphql.js'; 12 | 13 | import type { 14 | DocumentViewId, 15 | EncodedEntry, 16 | EncodedOperation, 17 | Fields, 18 | NextArgs, 19 | PublicKey, 20 | SchemaId, 21 | } from './types.js'; 22 | 23 | const MAX_BLOB_PIECE_LENGTH = 256 * 1000; 24 | 25 | /** 26 | * Options we can pass in into methods which will override the globally set 27 | * options for that session for that method call. 28 | */ 29 | export type Options = { 30 | /** 31 | * Key pair which is used for that method call to sign the entry. 32 | */ 33 | keyPair: KeyPair; 34 | 35 | /** 36 | * Schema Id which is used for that method call to identify the schema of the 37 | * document. 38 | */ 39 | schemaId: SchemaId; 40 | }; 41 | 42 | /** 43 | * Communicate with the p2panda network through a `Session` instance. 44 | * 45 | * `Session` provides a high-level interface to create data in the p2panda 46 | * network by creating, updating and deleting documents following data schemas. 47 | * It also provides a low-level API for creating entries on the Bamboo 48 | * append-only log structure. 49 | * 50 | * A session is configured with the URL of a p2panda node, which may be running 51 | * locally or on a remote machine. It is possible to set a fixed key pair 52 | * and/or data schema for a session by calling `setKeyPair()` and `setSchemaId()` 53 | * or you can also configure these through the `options` parameter of 54 | * methods. 55 | */ 56 | export class Session { 57 | /** 58 | * Address of a p2panda node that we can connect to. 59 | */ 60 | readonly endpoint: string; 61 | 62 | /** 63 | * A GraphQL client connected to the configured endpoint. 64 | */ 65 | readonly client: GraphQLClient; 66 | 67 | /** 68 | * Initiates a new session. 69 | * 70 | * @param endpoint - URL of p2panda node 71 | * @returns Session instance 72 | */ 73 | constructor(endpoint: Session['endpoint']) { 74 | if (!endpoint) { 75 | throw new Error('Missing `endpoint` parameter for creating a session'); 76 | } 77 | 78 | this.endpoint = endpoint; 79 | this.client = new GraphQLClient(endpoint); 80 | } 81 | 82 | /** 83 | * Globally configured schema id which is used as a default for all requests. 84 | */ 85 | #schemaId: SchemaId | null = null; 86 | 87 | /** 88 | * Returns currently configured schema id. 89 | * 90 | * Throws if no schema id is configured. 91 | * 92 | * @returns SchemaId instance 93 | */ 94 | get schemaId(): SchemaId { 95 | if (!this.#schemaId) { 96 | throw new Error( 97 | 'Configure a schema id with `session.schemaId()` or with the `options` ' + 98 | 'parameter on methods.', 99 | ); 100 | } 101 | 102 | return this.#schemaId; 103 | } 104 | 105 | /** 106 | * Set a schema id for this whole session. 107 | * 108 | * This value will be used if no other schema id is defined through a methods 109 | * `options` parameter. 110 | * 111 | * @param id - schema id 112 | * @returns Session instance 113 | */ 114 | setSchemaId(id: SchemaId): Session { 115 | this.#schemaId = id; 116 | return this; 117 | } 118 | 119 | /** 120 | * Globally configured key pair which is used as a default for all requests. 121 | */ 122 | #keyPair: KeyPair | null = null; 123 | 124 | /** 125 | * Returns currently configured key pair. 126 | * 127 | * Throws if no key pair is configured. 128 | * 129 | * @returns KeyPair instance 130 | */ 131 | get keyPair(): KeyPair { 132 | if (!this.#keyPair) { 133 | throw new Error( 134 | 'Configure a key pair with `session.keyPair()` or with the `options` ' + 135 | 'parameter on methods.', 136 | ); 137 | } 138 | 139 | return this.#keyPair; 140 | } 141 | 142 | /** 143 | * Set a fixed key pair for this session, which will be used by methods 144 | * unless a different key pair is configured through their `options` 145 | * parameters. 146 | * 147 | * This does not check the integrity or type of the supplied key pair! 148 | * 149 | * @param keyPair - key pair instance generated using the `KeyPair` class. 150 | * @returns Session instance 151 | */ 152 | setKeyPair(keyPair: KeyPair): Session { 153 | this.#keyPair = keyPair; 154 | return this; 155 | } 156 | 157 | /** 158 | * Return arguments for constructing the next entry given public key and 159 | * document view id. 160 | * 161 | * @param publicKey - public key of the author 162 | * @param viewId - optional document view id 163 | * @returns NextArgs arguments used to create a new entry 164 | */ 165 | async nextArgs( 166 | publicKey: PublicKey, 167 | viewId?: DocumentViewId, 168 | ): Promise { 169 | if (!publicKey) { 170 | throw new Error("Author's public key must be provided"); 171 | } 172 | 173 | const result = await nextArgs(this.client, { 174 | publicKey, 175 | viewId, 176 | }); 177 | 178 | return result; 179 | } 180 | 181 | /** 182 | * Publish an encoded entry and operation. 183 | * 184 | * This method returns the "local" document view id, which represents the 185 | * latest version of the document we've created, updated or deleted. 186 | * 187 | * It is "local" because it is the "latest version" from our perspective. 188 | * Concurrent updates through other peers might have happend but we didn't 189 | * know about them in the moment we've published our operation. 190 | * 191 | * If concurrent edits by other peers should also be referred to (as is 192 | * often the case) then the document view id should be accessed via the 193 | * GraphQL client API. 194 | * 195 | * @param entry - encoded and signed entry, represented as hexadecimal 196 | * string 197 | * @param operation - encoded CBOR operation, represented as hexadecimal 198 | * string 199 | * @returns DocumentViewId - local document view id 200 | */ 201 | async publish( 202 | entry: EncodedEntry, 203 | operation: EncodedOperation, 204 | ): Promise { 205 | if (!entry || !operation) { 206 | throw new Error('Encoded entry and operation must be provided'); 207 | } 208 | 209 | // Publish entry with operation payload and retreive next entry arguments 210 | // for future updates on that document 211 | await publish(this.client, { entry, operation }); 212 | const localViewId = generateHash(entry); 213 | 214 | // Return document view id of the "latest" version from our perspective, 215 | // hence "local" 216 | return localViewId; 217 | } 218 | 219 | /** 220 | * Creates a new document with the given fields and matching schema id. 221 | * 222 | * @param fields - application data to publish with the new entry, needs to 223 | * match schema 224 | * @param options - overrides globally set options for this method call 225 | * @param options.keyPair - will be used to sign the new entry 226 | * @param options.schemaId - will be used as the matching schema identifier 227 | * @returns Document id of the document we've created 228 | * @example 229 | * ``` 230 | * const endpoint = 'http://localhost:2020/graphql'; 231 | * const keyPair = new KeyPair(); 232 | * const schemaId = 'chat_00206394434d78553bd064c8ea9a61d2b9622826909966ae895eb1c8b692b335d919'; 233 | * 234 | * const fields = { 235 | * message: 'ahoy' 236 | * }; 237 | * 238 | * await new Session(endpoint) 239 | * .setKeyPair(keyPair) 240 | * .create(fields, { schemaId }); 241 | * ``` 242 | */ 243 | async create( 244 | fields: Fields, 245 | options?: Partial, 246 | ): Promise { 247 | if (!fields) { 248 | throw new Error('Fields must be provided'); 249 | } 250 | 251 | const schemaId = options?.schemaId || this.schemaId; 252 | const keyPair = options?.keyPair || this.keyPair; 253 | 254 | // Retreive next entry arguments 255 | const publicKey = keyPair.publicKey(); 256 | const nextArgs = await this.nextArgs(publicKey); 257 | 258 | // Sign and encode entry with CREATE operation 259 | const { entry, operation } = createOperation( 260 | { 261 | schemaId, 262 | fields, 263 | }, 264 | { 265 | keyPair, 266 | nextArgs, 267 | }, 268 | ); 269 | 270 | // Publish entry and operation on node, return the "local" document view id 271 | // which in this case will be the id of the document we've just created 272 | const localViewId = await this.publish(entry, operation); 273 | return localViewId; 274 | } 275 | 276 | /** 277 | * Updates a document with the given fields and matching schema id. 278 | * 279 | * The document to be updated is identified by the `previous` parameter which contains 280 | * the most recent known document view id. 281 | * 282 | * @param fields - data to publish with the new entry, needs to match schema 283 | * @param previous - array or string of operation ids identifying the tips of 284 | * all currently un-merged branches in the document graph 285 | * @param options - overrides globally set options for this method call 286 | * @param options.keyPair - will be used to sign the new entry 287 | * @param options.schemaId - will be used as the matching schema identifier 288 | * @returns Document view id, pointing at the exact version of the document 289 | * we've just updated 290 | * @example 291 | * ``` 292 | * const endpoint = 'http://localhost:2020/graphql'; 293 | * const keyPair = new KeyPair(); 294 | * const schemaId = 'chat_00206394434d78553bd064c8ea9a61d2b9622826909966ae895eb1c8b692b335d919'; 295 | * 296 | * const session = new Session(endpoint) 297 | * .setKeyPair(keyPair) 298 | * .setSchemaId(schemaId); 299 | * 300 | * // Create a new document first 301 | * const viewId = await session.create({ 302 | * message: 'ahoy!' 303 | * }); 304 | * 305 | * // Use the `viewId` to point our update at the document we've just created 306 | * await session.update({ 307 | * message: 'ahoy, my friend!' 308 | * }, viewId); 309 | * ``` 310 | */ 311 | async update( 312 | fields: Fields, 313 | previous: DocumentViewId, 314 | options?: Partial, 315 | ): Promise { 316 | if (!fields) { 317 | throw new Error('Fields must be provided'); 318 | } 319 | 320 | if (!previous) { 321 | throw new Error('Document view id must be provided'); 322 | } 323 | 324 | const schemaId = options?.schemaId || this.schemaId; 325 | const keyPair = options?.keyPair || this.keyPair; 326 | 327 | // Retreive next entry arguments 328 | const publicKey = keyPair.publicKey(); 329 | const nextArgs = await this.nextArgs(publicKey, previous); 330 | 331 | // Sign and encode entry with UPDATE operation 332 | const { entry, operation } = updateOperation( 333 | { 334 | schemaId, 335 | previous, 336 | fields, 337 | }, 338 | { 339 | keyPair, 340 | nextArgs, 341 | }, 342 | ); 343 | 344 | // Publish entry and operation on node, return the "local" document view id 345 | // which in this case will be the id of the update we've just made 346 | const localViewId = await this.publish(entry, operation); 347 | return localViewId; 348 | } 349 | 350 | /** 351 | * Deletes a document. 352 | * 353 | * The document to be deleted is identified by the `previous` parameter 354 | * which contains the most recent known document view id. 355 | * 356 | * @param previous - array or string of operation ids identifying the tips of 357 | * all currently un-merged branches in the document graph 358 | * @param options - overrides globally set options for this method call 359 | * @param options.keyPair - will be used to sign the new entry 360 | * @param options.schemaId - will be used as the matching schema identifier 361 | * @returns Document view id, pointing at the exact version of the document 362 | * we've just deleted 363 | * @example 364 | * ``` 365 | * const endpoint = 'http://localhost:2020/graphql'; 366 | * const keyPair = new KeyPair(); 367 | * const schemaId = 'chat_00206394434d78553bd064c8ea9a61d2b9622826909966ae895eb1c8b692b335d919'; 368 | * 369 | * const session = new Session(endpoint) 370 | * .setKeyPair(keyPair) 371 | * .setSchemaId(schemaId); 372 | * 373 | * // Create a new document first 374 | * const viewId = await session.create({ 375 | * message: 'ahoy!' 376 | * }); 377 | * 378 | * // Use the `viewId` to point our deletion at the document we've just created 379 | * await session.delete(viewId); 380 | * ``` 381 | */ 382 | async delete( 383 | previous: DocumentViewId, 384 | options?: Partial, 385 | ): Promise { 386 | if (!previous) { 387 | throw new Error('Document view id must be provided'); 388 | } 389 | 390 | const schemaId = options?.schemaId || this.schemaId; 391 | const keyPair = options?.keyPair || this.keyPair; 392 | 393 | // Retreive next entry arguments 394 | const publicKey = keyPair.publicKey(); 395 | const nextArgs = await this.nextArgs(publicKey, previous); 396 | 397 | // Sign and encode entry with DELETE operation 398 | const { entry, operation } = deleteOperation( 399 | { 400 | schemaId, 401 | previous, 402 | }, 403 | { 404 | keyPair, 405 | nextArgs, 406 | }, 407 | ); 408 | 409 | // Publish entry and operation on node, return the "local" document view id 410 | // which in this case will be the id of the deletion we've just made 411 | const localViewId = await this.publish(entry, operation); 412 | return localViewId; 413 | } 414 | 415 | /** 416 | * Publish a blob. 417 | * 418 | * The blob byte array is split into 256kb long pieces which are each published 419 | * individually, following this the blob document itself is published. Included 420 | * metadata is `mime_type` [str] and `length` [int] representing the complete 421 | * byte length of the blob file. 422 | * 423 | * @param blob - blob data to be published 424 | * @param options - overrides globally set options for this method call 425 | * @param options.keyPair - will be used to sign the new entry 426 | * @returns Document id of the blob we've created 427 | * @example 428 | * ``` 429 | * const endpoint = 'http://localhost:2020/graphql'; 430 | * const keyPair = new KeyPair(); 431 | * 432 | * const session = await new Session(endpoint) 433 | * .setKeyPair(keyPair) 434 | * .create(fields, { schemaId }); 435 | * 436 | * const input = document.querySelector('input'); 437 | * const blob = input.files[0]; 438 | * await session.createBlob(blob); 439 | * ``` 440 | 441 | */ 442 | async createBlob( 443 | blob: Blob, 444 | options?: Partial, 445 | ): Promise { 446 | if (!blob) { 447 | throw new Error('blob must be provided'); 448 | } 449 | 450 | const mimetype = blob.type; 451 | const bytes = await intoBytes(blob); 452 | const keyPair = options?.keyPair || this.keyPair; 453 | 454 | // Retrieve next entry arguments 455 | const publicKey = keyPair.publicKey(); 456 | 457 | const pieces = []; 458 | 459 | // Get the length and calculate the total number of pieces, based on the 460 | // maximum allowed piece size. 461 | const length = bytes.byteLength; 462 | const expected_pieces = Math.ceil(length / MAX_BLOB_PIECE_LENGTH); 463 | 464 | for (let index = 0; index < expected_pieces; index++) { 465 | const start = index * MAX_BLOB_PIECE_LENGTH; 466 | const end = start + MAX_BLOB_PIECE_LENGTH; 467 | 468 | // Take a slice from the blob bytes, these are the bytes we will publish 469 | // as a blob piece. 470 | const pieceBytes = bytes.slice(start, end); 471 | 472 | // Compose a blob piece operation. 473 | const fields = new OperationFields(); 474 | fields.insert('data', 'bytes', pieceBytes); 475 | 476 | const nextArgs = await this.nextArgs(publicKey); 477 | 478 | // Sign and encode entry with CREATE operation 479 | const { entry, operation } = createOperation( 480 | { 481 | schemaId: 'blob_piece_v1', 482 | fields, 483 | }, 484 | { 485 | keyPair, 486 | nextArgs, 487 | }, 488 | ); 489 | 490 | // Publish the blob piece and push it's id to the pieces array. 491 | const viewId = await this.publish(entry, operation); 492 | pieces.push([viewId.toString()]); 493 | } 494 | 495 | // Compose a blob operation, using the array of blob pieces we published 496 | // in the previous step. 497 | const fields = new OperationFields(); 498 | fields.insert('mime_type', 'str', mimetype); 499 | fields.insert('length', 'int', length); 500 | fields.insert('pieces', 'pinned_relation_list', pieces); 501 | 502 | const nextArgs = await this.nextArgs(publicKey); 503 | 504 | // Sign and encode entry with CREATE operation 505 | const { entry, operation } = createOperation( 506 | { 507 | schemaId: 'blob_v1', 508 | fields, 509 | }, 510 | { 511 | keyPair, 512 | nextArgs, 513 | }, 514 | ); 515 | 516 | // Publish the blob and return it's document view id. 517 | const viewId = await this.publish(entry, operation); 518 | 519 | return viewId; 520 | } 521 | } 522 | 523 | // Helper method for converting a blob into a Uint8Array. 524 | const intoBytes = async (blob: Blob): Promise => { 525 | const reader = new FileReader(); 526 | 527 | return new Promise((resolve, reject) => { 528 | reader.onload = (e) => { 529 | const arrayBuffer = e.target?.result; 530 | const array = new Uint8Array(arrayBuffer as ArrayBuffer); 531 | resolve(array); 532 | }; 533 | reader.onerror = (e) => { 534 | reject(e); 535 | }; 536 | 537 | reader.readAsArrayBuffer(blob); 538 | }); 539 | }; 540 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | p2panda, a peer-to-peer communications protocol for secure, 633 | energy-efficient, offline- and local-first web applications. 634 | Copyright (C) 2021 p2panda contributors 635 | 636 | This program is free software: you can redistribute it and/or modify 637 | it under the terms of the GNU Affero General Public License as published by 638 | the Free Software Foundation, either version 3 of the License, or 639 | (at your option) any later version. 640 | 641 | This program is distributed in the hope that it will be useful, 642 | but WITHOUT ANY WARRANTY; without even the implied warranty of 643 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 644 | GNU Affero General Public License for more details. 645 | 646 | You should have received a copy of the GNU Affero General Public License 647 | along with this program. If not, see . 648 | 649 | Also add information on how to contact you by electronic and paper mail. 650 | 651 | If your software can interact with users remotely through a computer 652 | network, you should also make sure that it provides a way for users to 653 | get its source. For example, if your program is a web application, its 654 | interface could display a "Source" link that leads users to an archive 655 | of the code. There are many ways you could offer source, and different 656 | solutions will be better for different programs; see section 13 for the 657 | specific requirements. 658 | 659 | You should also get your employer (if you work as a programmer) or school, 660 | if any, to sign a "copyright disclaimer" for the program, if necessary. 661 | For more information on this, and how to apply and follow the GNU AGPL, see 662 | . 663 | --------------------------------------------------------------------------------