npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

p2panda-js

v0.8.1

Published

All the things a panda needs

Downloads

6

Readme

This library provides all tools required to write a client, node or even your own protocol implementation for the p2panda network. It is shipped both as a Rust crate p2panda-rs with WebAssembly bindings and a NPM package p2panda-js with TypeScript definitions running in NodeJS or any modern web browser.

In the future p2panda-js will have full feature parity with p2panda-rs to be able to write high-level client frameworks or node implementations in TypeScript. Until now p2panda-js provides basic methods to create, sign and encode data.

The core p2panda specification is fully functional but still under review so please be prepared for breaking API changes until we reach v1.0. Currently no p2panda implementation has recieved a security audit.

Installation

To install p2panda-js run:

npm i p2panda-js

Example

import { KeyPair, signAndEncodeEntry, encodeOperation } from 'p2panda-js';

// Id of the schema which describes the data we want to publish. This should
// already be known to the node we are publishing to.
const SCHEMA_ID =
  'profile_0020c65567ae37efea293e34a9c7d13f8f2bf23dbdc3b5c7b9ab46293111c48fc78b';

// Generate new Ed25519 key pair.
const keyPair = new KeyPair();

// Add field data to "create" operation and encode it.
const operation = encodeOperation({
  schemaId: SCHEMA_ID,
  fields: {
    username: 'Panda',
  },
});

// Create Bamboo entry (append-only log data type) with operation as payload
// and encode it.
const entry = signAndEncodeEntry({ operation }, keyPair);

console.log(entry, operation);

Usage

p2panda-js runs both in NodeJS and web browsers and comes as a ES, CommonJS or UMD module. It can easily be integrated into Webpack, Rollup or other tools.

Since p2panda-js contains WebAssembly code, it is necessary to initialise it before using the methods in the Browser. This initialisation step is not required in NodeJS contexts.

To make this step a little bit easier p2panda-js inlines the WebAssembly code as a base64 string which gets decoded automatically during initialisation. For manual initialisation the package also comes with "slim" versions where you need to provide a path to the ".wasm" file yourself, you can read about this approach further below.

NodeJS

import { KeyPair } from 'p2panda-js';
const keyPair = new KeyPair();
console.log(keyPair.publicKey());

Browser

To quickly get started you can run p2panda-js in any modern browser as an ES module like that:

<script type="module">
  import { initWebAssembly, KeyPair } from 'https://cdn.jsdelivr.net/npm/[email protected]/lib/esm/index.min.js';

  // This only needs to be done once before using all `p2panda-js` methods.
  initWebAssembly().then(() => {
    const keyPair = new KeyPair();
    console.log(keyPair.publicKey());
  });
</script>

Or use the "slim" version if you want to provide the ".wasm" file manually:

<script type="module">
  import { initWebAssembly, KeyPair } from 'https://cdn.jsdelivr.net/npm/[email protected]/lib/esm-slim/index.min.js';

  // Pass external .wasm file manually for smaller file sizes
  const wasmFile = 'https://cdn.jsdelivr.net/npm/[email protected]/lib/p2panda.wasm';
  initWebAssembly(wasmFile).then(() => {
    const keyPair = new KeyPair();
    console.log(keyPair.publicKey());
  });
</script>

Bundlers

import { initWebAssembly, KeyPair } from 'p2panda-js';

// This only needs to be done once before using all `p2panda-js` methods.
await initWebAssembly();

const keyPair = new KeyPair();
console.log(keyPair.publicKey());

Manually load .wasm

Running p2panda-js in the browser automatically inlines the WebAssembly inside the JavaScript file, encoded as a base64 string. While this works for most developers, it also doubles the size of the imported file. To avoid larger payloads and decoding times you can load the .wasm file manually by using a "slim" version. For this you need to initialise the module by passing the path to the file into initWebAssembly:

// Import from `slim` module to manually initialise WebAssembly code
import { initWebAssembly, KeyPair } from 'p2panda-js/slim';
import wasm from 'p2panda-js/p2panda.wasm';

// When running p2panda in the browser, this method needs to run once
// before using all other `p2panda-js` methods
await initWebAssembly(wasm);

const keyPair = new KeyPair();
console.log(keyPair.publicKey());

Development

Dependencies

In order to develop with the current code base p2panda-js needs to be compiled from the p2panda-rs code using wasm-bindgen. This requires a working Rust environment to be setup and wasm-bindgen to be installed. wasm-opt is only required to optimize the WebAssembly builds for production via npm run build. You can then run the following commands, the compilation occurs during the testing and build phases:

# Install dependencies
npm install

# Check code formatting
npm run lint

# Run tests, requires `wasm-bindgen`
npm test

# Compile wasm and bundle js package, requires `wasm-bindgen` and `wasm-opt`
npm run build

Documentation

# Generate documentation
npm run docs

# Show documentation in browser
npx serve ./docs

License

GNU Affero General Public License v3.0 AGPL-3.0-or-later

Supported by

This project has received funding from the European Union’s Horizon 2020 research and innovation programme within the framework of the NGI-POINTER Project funded under grant agreement No 871528 and NGI-ASSURE No 957073