forked from hibas123/SecureFileWrapper
208 lines
7.1 KiB
JavaScript
208 lines
7.1 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const observable_1 = require("./observable");
|
|
const lock_1 = require("./lock");
|
|
class NoConnection extends Error {
|
|
constructor() {
|
|
super("No connection");
|
|
this.type = "noconnection";
|
|
}
|
|
}
|
|
exports.NoConnection = NoConnection;
|
|
class Unauthorized extends Error {
|
|
constructor() {
|
|
super("Not authorized");
|
|
this.type = "unauthorized";
|
|
}
|
|
}
|
|
exports.Unauthorized = Unauthorized;
|
|
class NoPermission extends Error {
|
|
constructor() {
|
|
super("No permission");
|
|
this.type = "nopermission";
|
|
}
|
|
}
|
|
exports.NoPermission = NoPermission;
|
|
class NotFound extends Error {
|
|
constructor() {
|
|
super("Not found");
|
|
this.type = "notfound";
|
|
}
|
|
}
|
|
exports.NotFound = NotFound;
|
|
class BadRequest extends Error {
|
|
constructor() {
|
|
super("Bad request");
|
|
this.type = "badrequest";
|
|
}
|
|
}
|
|
exports.BadRequest = BadRequest;
|
|
const fetch = require("isomorphic-fetch");
|
|
function statusParser(res) {
|
|
if (res.status !== 200) {
|
|
switch (res.status) {
|
|
case 400:
|
|
throw new BadRequest();
|
|
case 404:
|
|
throw new NotFound();
|
|
case 403:
|
|
throw new NoPermission();
|
|
case 401:
|
|
throw new Unauthorized();
|
|
default:
|
|
throw new Error(res.statusText);
|
|
}
|
|
}
|
|
}
|
|
class SecureFileWrapper {
|
|
constructor(server) {
|
|
this.server = server;
|
|
this._jwtObservableServer = new observable_1.default();
|
|
this.jwtObservable = this._jwtObservableServer.getPublicApi();
|
|
this.auth_lock = new lock_1.default();
|
|
if (this.server.endsWith("/")) {
|
|
this.server += "api/v1";
|
|
}
|
|
else {
|
|
this.server += "/api/v1";
|
|
}
|
|
}
|
|
getJWT() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
if (!this.auth_lock.locked) {
|
|
let lock = yield this.auth_lock.getLock();
|
|
this._jwtObservableServer.send((jwt) => {
|
|
this.jwt = jwt;
|
|
lock.release();
|
|
});
|
|
}
|
|
yield this.auth_lock.getLock().then(lock => lock.release());
|
|
});
|
|
}
|
|
makeRequest(endpoint, method, query, body, second = false) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
if (!this.jwt || this.jwt === undefined) {
|
|
yield this.getJWT();
|
|
}
|
|
query.jwt = this.jwt;
|
|
let query_str = "?";
|
|
let first = true;
|
|
for (let key in query) {
|
|
if (!first)
|
|
query_str += "&";
|
|
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
|
|
first = false;
|
|
}
|
|
var headers = {
|
|
"pragme": "no-cache",
|
|
"cache-control": "no-cache"
|
|
};
|
|
let body_n;
|
|
if (body) {
|
|
headers["Content-Type"] = "application/octet-stream";
|
|
body_n = Buffer ? Buffer.from(body instanceof ArrayBuffer ? body : body.buffer) : body;
|
|
}
|
|
try {
|
|
let res = yield fetch(this.server + endpoint + query_str, { method, body: body_n, headers });
|
|
if (res.status === 401 && !second) {
|
|
yield this.getJWT();
|
|
return this.makeRequest(endpoint, method, query, body, true);
|
|
}
|
|
else {
|
|
statusParser(res);
|
|
return res;
|
|
}
|
|
}
|
|
catch (err) {
|
|
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
|
|
throw new NoConnection();
|
|
throw err;
|
|
}
|
|
});
|
|
}
|
|
// async test(jwt): Promise<{ user: string, test: true }> {
|
|
// let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
|
|
// statusParser(res);
|
|
// return await res.json();
|
|
// }
|
|
list(folder) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let query = {};
|
|
if (folder)
|
|
query.folder = folder;
|
|
let res = yield this.makeRequest("/files", "GET", query);
|
|
let d = yield res.json();
|
|
return d.files;
|
|
});
|
|
}
|
|
create(name, data, type, folder, preview) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let params = { type: type, name: name };
|
|
if (preview) {
|
|
params.preview = preview;
|
|
}
|
|
if (folder) {
|
|
params.folder = folder;
|
|
}
|
|
let res = yield this.makeRequest("/files", "POST", params, data);
|
|
return (yield res.json()).file;
|
|
});
|
|
}
|
|
get(id, version) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let res;
|
|
if (typeof version === "string") {
|
|
res = yield this.makeRequest(`/files/${id}/history/${version}`, "GET", {});
|
|
}
|
|
else {
|
|
res = yield this.makeRequest("/files/" + id, "GET", {});
|
|
}
|
|
if (res.arrayBuffer) {
|
|
return res.arrayBuffer();
|
|
}
|
|
else {
|
|
let blob = yield res.buffer();
|
|
// console.log(blob.length);
|
|
return Uint8Array.from(blob).buffer;
|
|
}
|
|
});
|
|
}
|
|
update(id, data, preview) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let put = {};
|
|
if (preview)
|
|
put.preview = preview;
|
|
let res = yield this.makeRequest("/files/" + id, "PUT", put, data);
|
|
let json = yield res.json();
|
|
return json.file;
|
|
});
|
|
}
|
|
delete(id) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let res = yield this.makeRequest("/files/" + id, "DELETE", {});
|
|
return res.json();
|
|
});
|
|
}
|
|
history(id) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let res = yield this.makeRequest(`/files/${id}/history`, "GET", {});
|
|
statusParser(res);
|
|
return res.json();
|
|
});
|
|
}
|
|
restore(id, version) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
yield this.makeRequest(`/files/${id}/history/${version}/restore`, "PUT", {});
|
|
});
|
|
}
|
|
}
|
|
exports.default = SecureFileWrapper;
|
|
//# sourceMappingURL=index.js.map
|