• Stars
    star
    608
  • Rank 73,735 (Top 2 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript library for aria2, "The next generation download utility."

aria2.js

JavaScript (Node.js and browsers) library for aria2, "The next generation download utility."

Introduction

aria2.js controls aria2 via its JSON-RPC interface and features

  • Node.js and browsers support
  • multiple transports
  • promise API

See aria2 methods and aria2 notifications.

Getting started

Start aria2 with rpc, example:

aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all

Browser

npm install aria2
import Aria2 from "aria2";

const aria2 = new Aria2({ WebSocket: ws, fetch: nodefetch, ...options });

You can also use node_modules/aria2/bundle.js directly in <script> and window.Aria2.

Node.js

npm install aria2 node-fetch ws
import Aria2 from "aria2";
import ws from "ws";
import nodefetch from "node-fetch";

const aria2 = new Aria2({ WebSocket: ws, fetch: nodefetch, ...options });

Usage

Default options match aria2c defaults and are

{
  host: 'localhost',
  port: 6800,
  secure: false,
  secret: '',
  path: '/jsonrpc',
}

secret is optional and refers to --rpc-secret. If you define it, it will be added to every call for you.

If the WebSocket is open (via the open method) aria2.js will use the WebSocket transport, otherwise the HTTP transport.

The "aria2." prefix can be omitted from both methods and notifications.

open

aria2.open() opens the WebSocket connection. All subsequent requests will use the WebSocket transport instead of HTTP.

aria2
  .open()
  .then(() => console.log("open"))
  .catch((err) => console.log("error", err));

close

aria2.close() closes the WebSocket connection. All subsequent requests will use the HTTP transport instead of WebSocket.

aria2
  .close()
  .then(() => console.log("closed"))
  .catch((err) => console.log("error", err));

call

aria2.call() calls a method. Parameters are provided as arguments.

Example using addUri method to download from a magnet link.

const magnet =
  "magnet:?xt=urn:btih:88594AAACBDE40EF3E2510C47374EC0AA396C08E&dn=bbb_sunflower_1080p_30fps_normal.mp4&tr=udp%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2ftracker.publicbt.com%3a80%2fannounce&ws=http%3a%2f%2fdistribution.bbb3d.renderfarming.net%2fvideo%2fmp4%2fbbb_sunflower_1080p_30fps_normal.mp4";
const [guid] = await aria2.call("addUri", [magnet], { dir: "/tmp" });

multicall

aria2.multicall() is a helper for system.multicall. It returns an array of results or throw if any of the call failed.

const multicall = [
  [methodA, param1, param2],
  [methodB, param1, param2],
];

const results = await aria2.multicall(multicall);

batch

aria2.batch() is a helper for batch. It behaves the same as multicall except it returns an array of promises which gives more flexibility in handling errors.

const batch = [
  [methodA, param1, param2],
  [methodB, param1, param2],
];

const promises = await aria2.batch(batch);

listNotifications

aria2.listNotifications() is a helper for system.listNotifications. The difference with aria2.call('listNotifications') is that it removes the "aria2." prefix from the results.

const notifications = await aria2.listNotifications();
/*
[
  'onDownloadStart',
  'onDownloadPause',
  'onDownloadStop',
  'onDownloadComplete',
  'onDownloadError',
  'onBtDownloadComplete'
]
*/

// notifications logger example
notifications.forEach((notification) => {
  aria2.on(notification, (params) => {
    console.log("aria2", notification, params);
  });
});

listMethods

aria2.listMethods() is a helper for system.listMethods. The difference with aria2.call('listMethods') is that it removes the "aria2." prefix for the results.

const methods = await aria2.listMethods();
/*
[ 'addUri',
  [...]
  'system.listNotifications' ]

*/

events

// emitted when the WebSocket is open.
aria2.on("open", () => {
  console.log("aria2 OPEN");
});

// emitted when the WebSocket is closed.
aria2.on("close", () => {
  console.log("aria2 CLOSE");
});

// emitted for every message sent.
aria2.on("output", (m) => {
  console.log("aria2 OUT", m);
});

// emitted for every message received.
aria2.on("input", (m) => {
  console.log("aria2 IN", m);
});

Additionally every aria2 notifications received will be emitted as an event (with and without the "aria2." prefix). Only available when using WebSocket, see open.

aria2.on("onDownloadStart", ([guid]) => {
  console.log("aria2 onDownloadStart", guid);
});

More Repositories

1

Tangram

Browser for your pinned tabs
JavaScript
905
star
2

Workbench

Code playground for GNOME 🛠️
JavaScript
524
star
3

Junction

Application/browser chooser
JavaScript
467
star
4

polygoat

Make JavaScript functions that support both promise and callback styles.
JavaScript
154
star
5

Playhouse

Playground for HTML/CSS/JavaScript
JavaScript
146
star
6

Commit

Commit message editor
JavaScript
133
star
7

JSON8

JSON toolkit for JavaScript.
JavaScript
104
star
8

OhMySVG

Reduce the size of SVGs
JavaScript
92
star
9

linux-minibook-x

Linux for the Chuwi MiniBook X 2023 / N100
46
star
10

troll

Libraries for GNOME JavaScript (GJS)
JavaScript
45
star
11

Retro

A customizable retro digital segment clock
JavaScript
17
star
12

lightstring

XMPP client library for browsers.
JavaScript
17
star
13

remoteStorage

Playing around with remoteStorage, eventually releasing a lightweight library
TypeScript
5
star
14

mermaid-webkitgtk

JavaScript
5
star
15

owi-535

Applications and libraries for OWI-535 aka. Robotic Arm Edge
JavaScript
4
star
16

Disappear

Free your desktop from cumbersome apps.
JavaScript
4
star
17

broadcaster.js

Server-less cross-tabs messaging
HTML
3
star
18

proctor

Node.js module to lookup CPU and memory usage of a process.
JavaScript
3
star
19

icns.js

Apple Icon Image format (icns) for JavaScript
JavaScript
3
star
20

pya

CLI application to manage and monitor devices
JavaScript
2
star
21

node-bosh

BOSH client implementation library for nodejs.
JavaScript
2
star
22

netflix-desktop-entry

A simple (Linux) desktop entry that will open Netflix with Google Chrome
Makefile
2
star
23

ubiquitous-pancake

JavaScript
2
star
24

readplist

Tiny Node.js module to read any plist file.
JavaScript
2
star
25

outofsoya

Out of soya
TypeScript
1
star
26

gjs-embed

C
1
star
27

HTMLIncludes

JavaScript
1
star
28

sliders

HTML WYSIWYG slides editor
JavaScript
1
star
29

server-stop

Closes all connections and stop the server. Node.js
JavaScript
1
star
30

HTTPClient.js

JavaScript HTTP client library for browsers and Node.js
JavaScript
1
star
31

media-loop

Firefox addon that adds a menu item to control HTML5 audio/video loop.
JavaScript
1
star
32

dezorse

Like dezalgo but let you choose your "asap" function.
JavaScript
1
star