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

@signalstickers/stickers-client

v2.0.0

Published

Utilities for interacting with Signal's Sticker API for the browser and Node.

Downloads

15

Readme

Rationale

Communicating with Signal's Sticker API can be somewhat non-trivial because it uses encrypted protocol buffers. Raw responses must be decrypted using some rather esoteric cryptography, resulting in a high-friction experience for the average developer. This package aims to make using the Stickers API as straightforward as possible by providing a small set of utility functions that work in both the browser and Node.

Install

npm i @signalstickers/stickers-client

Use

It is assumed that users of this package have a general understanding of how stickers work in Signal. For more information on that topic, see this article.

This package can be used in both browser and Node. If you plan to use it in the browser, make sure you are using a bundler that supports the browser package.json field.

Because sticker packs in Signal are immutable, a response from Signal (be it for a sticker pack or an individual sticker) can be safely cached indefinitely. As such, this package implements a basic in-memory cache, so you should not need to implement caching or memoization yourself. ✨

API

This package has the following named exports:

async getStickerPackManifest(
  id: string,
  key: string
) => Promise<StickerPackManifest>

Provided a sticker pack ID and its key, returns a promise that resolves with the sticker pack's decrypted manifest.

async getStickerInPack(
  id: string,
  key: string,
  stickerId: number,
  encoding? = 'raw' | 'base64'
) => Promise<Uint8Array | string>

Provided a sticker pack ID, its key, and a sticker ID, returns a promise that resolves with the raw WebP image data for the indicated sticker.

An optional encoding parameter may be provided to indicate the desired return type. The default value of raw will return raw WebP data as a Uint8 Array. This is useful if further processing of the image data is necessary. Alternatively, if base64 is provided, a data-URI string will be returned instead. This string can be used directly as the src attribute in an <img> tag, for example.

async getEmojiForSticker(
  id: string,
  key: string,
  stickerId: number
) => Promise<string>

Provided a sticker pack ID, its key, and sticker ID, returns the emoji associated with the sticker.

Example

In this example, we'll create a simple React component that will render a single sticker. It will accept a sticker pack's ID and key as well as the ID of the sticker to render as props. We will use a one-time effect when the component mounts to fetch the image.

Note that this package caches responses from Signal, so we don't need to add any additional logic to store the result of getStickerInPack outside the component's state. If it is ever dismounted and remounted in the future with the same props, no additional network requests will be made.

import React from 'react';
import { getStickerInPack, getEmojiForSticker } from '@signalstickers/stickers-client';

export interface Props {
  packId: string;
  packKey: string;
  stickerId: number;
}

export default function Sticker({ packId, packKey, stickerId }: Props) {
  const [stickerData, setStickerData] = React.useState();
  const [stickerEmoji, setStickerEmoji] = React.useState();

  React.useEffect(() => {
    getStickerInPack(packId, packKey, stickerId, 'base64').then(setStickerData);
    getEmojiForSticker(packId, packKey, stickerId).then(setStickerEmoji);
  }, [packId, packKey, stickerId]);

  if (!stickerData || !stickerEmoji) return null;

  return (
    <img src={stickerData} alt={stickerEmoji} />
  );
};

This component could then be used thusly:

import Sticker from './Sticker';


export default () => {
  // This will render an image tag containing the indicated sticker.
  return (
    <Sticker
      packId="7be8291c4007cc73868818992596cc24"
      packKey="ca808607b39f0f1d860a8460128d3ba4022fb0536ddadf58f7e8ff8ac7ddaf56"
      stickerId={1}
    >
  )
}