-
Notifications
You must be signed in to change notification settings - Fork 1
/
rclone.js
104 lines (96 loc) · 2.79 KB
/
rclone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2018-2021 Trần Nguyễn Sơn. All rights reserved. MIT license.
/**
* Deno wrapper for rclone using WebAssembly build.
*
* @example
* ```ts
* import { Rclone } from "https://deno.land/x/rclone/rclone.js";
*
* const rclone = new Rclone();
* rclone.rc("core/version");
* ```
*
* By default, provided [rclone.wasm](https://deno.land/x/rclone/rclone.wasm)
* module is used. This can be changed by providing another compiled module in
* the constructor.
*
* @example
* ```ts
* import { Rclone } from "https://deno.land/x/rclone/";
*
* const module = await WebAssembly.compileStreaming(fetch("https://deno.land/x/rclone/rclone.wasm"));
* const rclone = new Rclone(module);
*
* rclone.rc("core/version");
* ```
*
* The default module provides the following backends:
*
* - alias
* - chunker
* - crypt
* - ftp
* - http
* - memory
* - union
*/
import "https://raw.githubusercontent.com/rclone/rclone/master/fs/rc/js/wasm_exec.js";
/** Provides a default WASM module. */
const wasm = await WebAssembly.compileStreaming(
fetch(new URL("./rclone.wasm", import.meta.url)),
);
/** A Rclone instance from a compiled WebAssemble module. */
export class Rclone extends WebAssembly.Instance {
/**
* Create a rclone instance.
* @param {WebAssembly.Module} [module] A WebAssembly module to use.
*/
constructor(module = wasm) {
// Patches for rclone.
globalThis.document ??= {};
globalThis.rcValidResolve ??= function () {
// Invoked by rclone at the end of initialization.
};
// Instantiates WASM module.
const go = new Go(); // From `wasm_exec.js`
super(module, go.importObject);
go.run(this);
}
/** Remote controls rclone
*
* ```ts
* import { Rclone } from "./rclone.js";
* const { rc } = new Rclone();
* console.log("core/version", rc("core/version", null))
* console.log("rc/noop", rc("rc/noop", {"string":"one",number:2}))
* console.log("operations/mkdir", rc("operations/mkdir", {"fs":":memory:","remote":"bucket"}))
* console.log("operations/list", rc("operations/list", {"fs":":memory:","remote":"bucket"}))
* ```
*
* @param {string} command The command to run.
* @param {Object|null} [args]
*/
rc(command, args) {
return globalThis.rc(command, args);
}
}
// Learn more at https://deno.land/manual/examples/module_metadata#concepts
if (import.meta.main) {
const { rc } = new Rclone();
const [command, ...args] = Deno.args;
const params = {};
let argCount = args.length;
while (argCount--) {
const arg = args[argCount];
if (arg.includes("=")) {
const [key, value] = arg.split("=");
if (isNaN(Number(value))) {
params[key] = value;
} else {
params[key] = Number(value);
}
args.splice(argCount, 1);
}
}
console.log(rc(command, params));
}