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

@buttercup/datasources

v4.1.1

Published

Buttercup archive datasources

Downloads

144

Readme

Buttercup Vault Datasources

Datasource integrations for Buttercup vaults

Buttercup Build Status npm version

About

Datasources are how Buttercup integrates with 3rd party services like Dropbox, ownCloud and Nextcloud etc.

You might want to check out the API documentation.

Supported interfaces:

  • Text: TextDatasource
  • Files: FileDatasource
  • WebDAV: WebDAVDatasource
  • OwnCloud: OwnCloudDatasource
  • Nextcloud: NextcloudDatasource
  • Dropbox: DropboxDatasource
  • Google Drive: GoogleDriveDatasource

You can easily add new, custom datasources by creating a new class and using registerDatasource to register it.

Installation

Simply run npm install @buttercup/datasources to install.

This library has @buttercup/app-env as a peer dependency - it is required for normal function. This is usually handled in combination with buttercup core.

Usage

Datasources are exported by the module so you can import only what you need:

const { Archive, Credentials } = require("buttercup");
const { TextDatasource } = require("@buttercup/datasources");

const tds = new TextDatasource(encryptedContent);
tds
    .load(Credentials.fromPassword("myPass"))
    .then(Archive.createFromHistory)
    .then(archive => {
        console.log(archive.toObject());
    });

Handling expiried authorisation tokens

Services like Google Drive use OAuth tokens which may eventually expire. You can listen for these expirations and handle their renewal by registering a listener on the AuthManager shared instance:

const { AuthManager } = require("@buttercup/datasources");

AuthManager.getSharedManager().registerHandler("googledrive", googleDriveDS => {
    // handle refreshing or fetching of tokens
    // attach them to datasource
    // return a promise
});

It should be noted that when using a datasource & workspace combination (inherent when using ArchiveManager) you should also update the workspace in the registered handler.

Registering a custom datasource

You can add your own datasources by using the registerDatasource method. Datasources must have the following properties:

  • Datasource#load(credentials): Load the stored contents (provided as a string) and return an array of Archive history items. It is often best to extend TextDatasource for this functionality.
  • Datasource#save(historyArray, credentials): Save the archive history. It is often best to extend TextDatasource for this functionality.
  • Datasource#toObject(): Output an object representation of the data source. The object should contain the content and properties necessary to serialise and deserialise it. Do not store the archive content in the object. Each object should contain a type property that is the string name of the datasource.
  • Datasource.fromObject(obj): Create a new datasource instance from an object (format should match Datasource#toObject()).
  • Datasource.fromString(str): Should create a new datasource instance from a string. This method is usually generic, as shown below.

The Datasource.fromString method will usually just use JSON to deserialise the datasource object, and this should stay the same if Datasource#toString is not overridden:

Datasource.fromString = function fromString(str, hostCredentials) {
    return Datasource.fromObject(JSON.parse(str), hostCredentials);
};

Once you have your datasource, be sure to register it:

registerDatasource("custom", CustomDatasource);

Note: The first argument "custom" should match the type property in the object presentation of the datasource.

Implementing OAuth2

If your datasource uses OAuth2 and stores tokens on it, you should use the following 2 public instance properties:

  • Datasource#token - The access token
  • Datasource#refreshToken - The refresh token, if available

Buttercup looks for these internally when doing updates to vault sources (saved on the device). If tokens are updated at some point, Buttercup will read from these to ensure the local (encrypted) copy is kept up to date.

If you update tokens at any point, make sure to emit an event: this.emit("updated"). Emit the event after the properties are updated so that Buttercup correctly performs the save process with the right values.