Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

skynet-js

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

skynet-js

Sia Skynet Javascript Client

  • 0.0.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
75
increased by66.67%
Maintainers
1
Weekly downloads
 
Created
Source

skynet-js - Javascript Sia Skynet Client

A Javascript module made to simplify communication with Sia Skynet portals from the browser.

Installing

Using npm

npm install skynet-js

Using yarn

yarn add skynet-js

Development

  • Clone the repository
  • Run yarn
  • Run yarn test to run the tests

Docs: using standalone function

async upload(portalUrl, file, [options])

import { upload } from "skynet-js";

Use the portalUrl to upload file contents.

portalUrl (string) - The string portal url.

file (File) - The file to upload.

options.onUploadProgress (function) - Optional callback to track progress.

Returns a promise that resolves with a { skylink } or throws error on failure.

const onUploadProgress = (progress, { loaded, total }) => {
  console.info(`Progress ${Math.round(progress * 100)}%`);
};

try {
  const { skylink } = await upload(portalUrl, file, { onUploadProgress });
} catch (error) {
  // handle error
}

async uploadDirectory(portalUrl, directory, filename, [options])

import { uploadDirectory } from "skynet-js";

Use the portalUrl to upload directory contents as a filename.

portalUrl (string) - The string portal url.

directory (Object) - Directory map { "file1.jpeg": <File>, "subdirectory/file2.jpeg": <File> }

filename (string) - Output file name (directory name).

options.onUploadProgress (function) - Optional callback to track progress.

Returns a promise that resolves with a { skylink } or throws error on failure.

Browser example
import path from "path-browserify";

const getFilePath = (file) => file.webkitRelativePath || file.path || file.name;

const getRelativeFilePath = (file) => {
  const filePath = getFilePath(file);
  const { root, dir, base } = path.parse(filePath);
  const relative = path.normalize(dir).slice(root.length).split(path.sep).slice(1);

  return path.join(...relative, base);
};

const getRootDirectory = (file) => {
  const filePath = getFilePath(file);
  const { root, dir } = path.parse(filePath);

  return path.normalize(dir).slice(root.length).split(path.sep)[0];
};

const onUploadProgress = (progress, { loaded, total }) => {
  console.info(`Progress ${Math.round(progress * 100)}%`);
};

try {
  const filename = getRootDirectory(files[0]);
  const directory = files.reduce((acc, file) => {
    const path = getRelativeFilePath(file);

    return { ...acc, [path]: file };
  }, {});

  const { skylink } = await uploadDirectory(portalUrl, directory, filename, { onUploadProgress });
} catch (error) {
  // handle error
}
import { download } from "skynet-js";

Use the portalUrl to download skylink contents.

portalUrl (string) - The string portal url.

skylink (string) - 46 character skylink.

Returns nothing.

import { open } from "skynet-js";

Use the portalUrl to open skylink in a new browser tab. Browsers support opening natively only limited file extensions like .html or .jpg and will fallback to downloading the file.

portalUrl (string) - The string portal url.

skylink (string) - 46 character skylink.

Returns nothing.

import { getUrl } from "skynet-js";

Use the portalUrl to generate direct skylink url.

portalUrl (string) - The string portal url.

skylink (string) - 46 character skylink.

options.download (boolean) - Option to include download directive in the url that will force a download when used. Defaults to false.

import { parseSkylink } from "skynet-js";

Use the parseSkylink to extract skylink from a string.

Currently supported string types are:

  • direct skylink string, example "XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"
  • sia: prefixed string, example "sia:XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"
  • sia:// prefixed string, example "sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"
  • skylink from url, example "https://siasky.net/XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"

skylink (string) - String containing 46 character skylink.

Returns extracted skylink string or throws error.

Docs: using SkynetClient

import SkynetClient from "skynet-js";

const client = new SkynetClient("https://siasky.net");

Client implements all the standalone functions as methods with bound portalUrl so you don't need to repeat it every time.

Keywords

FAQs

Package last updated on 16 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc