changing typescript output to es5

This commit is contained in:
hibas123 2018-03-04 01:09:59 +01:00
parent 88efc8c7fb
commit 96b050254f
3 changed files with 578 additions and 237 deletions

279
index.js
View File

@ -1,10 +1,55 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
const rsa = require("node-rsa");
var rsa = require("node-rsa");
require("isomorphic-fetch");
const btb = require("blob-to-buffer");
class SecureFile {
constructor(server, username, private_key) {
var btb = require("blob-to-buffer");
var SecureFile = /** @class */ (function () {
function SecureFile(server, username, private_key) {
this.Server = server;
if (this.Server.endsWith("/")) {
this.Server += "api";
@ -15,42 +60,89 @@ class SecureFile {
this.Username = username;
this.PrivateKey = private_key;
}
async getCode() {
let code_res = await fetch(this.Server + "/code?username=" + this.Username);
SecureFile.prototype.getCode = function () {
return __awaiter(this, void 0, void 0, function () {
var code_res, code, r;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch(this.Server + "/code?username=" + this.Username)];
case 1:
code_res = _a.sent();
statusParser(code_res);
//ToDo check status Codes
let code = (await code_res.json()).code;
let r = new rsa(this.PrivateKey, "pkcs1-pem");
return { code: code, signature: r.sign(code).toString("base64") };
return [4 /*yield*/, code_res.json()];
case 2:
code = (_a.sent()).code;
r = new rsa(this.PrivateKey, "pkcs1-pem");
return [2 /*return*/, { code: code, signature: r.sign(code).toString("base64") }];
}
async makeRequest(endpoint, method, query, body) {
let code = await this.getCode();
});
});
};
SecureFile.prototype.makeRequest = function (endpoint, method, query, body) {
return __awaiter(this, void 0, void 0, function () {
var code, query_str, first, key;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getCode()];
case 1:
code = _a.sent();
query.code = code.code;
query.signature = code.signature;
let query_str = "?";
let first = true;
for (let key in query) {
query_str = "?";
first = true;
for (key in query) {
if (!first)
query_str += "&";
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
first = false;
}
return await fetch(this.Server + endpoint + query_str, { method: method, body: body });
return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body })];
case 2: return [2 /*return*/, _a.sent()];
}
async test() {
let res = await this.makeRequest("/test", "GET", {}, undefined);
});
});
};
SecureFile.prototype.test = function () {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.makeRequest("/test", "GET", {}, undefined)];
case 1:
res = _a.sent();
statusParser(res);
return await res.json();
return [4 /*yield*/, res.json()];
case 2: return [2 /*return*/, _a.sent()];
}
async list(folder) {
});
});
};
SecureFile.prototype.list = function (folder) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!folder)
folder = "root";
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined);
return [4 /*yield*/, this.makeRequest("/files", "GET", { folder: folder }, undefined)];
case 1:
res = _a.sent();
statusParser(res);
return await res.json();
return [4 /*yield*/, res.json()];
case 2: return [2 /*return*/, _a.sent()];
}
async create(name, data, type, folder, encrypt = true, preview) {
let params = { type: type, name: name, no_encryption: !encrypt };
});
});
};
SecureFile.prototype.create = function (name, data, type, folder, encrypt, preview) {
if (encrypt === void 0) { encrypt = true; }
return __awaiter(this, void 0, void 0, function () {
var params, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
params = { type: type, name: name, no_encryption: !encrypt };
if (preview) {
params.preview = preview;
}
@ -60,70 +152,133 @@ class SecureFile {
if (!encrypt) {
params.no_encryption = true;
}
let res = await this.makeRequest("/files", "POST", params, data);
return [4 /*yield*/, this.makeRequest("/files", "POST", params, data)];
case 1:
res = _a.sent();
statusParser(res);
return res.json();
}
async get(id, version) {
let res;
if (typeof version === "string") {
res = await this.makeRequest(`/files/${id}/history/${version}`, "GET", {}, undefined);
}
else {
res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
return [2 /*return*/, res.json()];
}
});
});
};
SecureFile.prototype.get = function (id, version) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(typeof version === "string")) return [3 /*break*/, 2];
return [4 /*yield*/, this.makeRequest("/files/" + id + "/history/" + version, "GET", {}, undefined)];
case 1:
res = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this.makeRequest("/files/" + id, "GET", {}, undefined)];
case 3:
res = _a.sent();
_a.label = 4;
case 4:
statusParser(res);
if (res.buffer) {
return res.buffer();
return [2 /*return*/, res.buffer()];
}
else {
return new Promise(async (resolve, reject) => {
btb(await res.blob(), (err, buffer) => {
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = btb;
return [4 /*yield*/, res.blob()];
case 1:
_a.apply(void 0, [_b.sent(), function (err, buffer) {
if (err)
reject(err);
else
resolve(buffer);
}]);
return [2 /*return*/];
}
});
}); })];
}
return [2 /*return*/];
}
});
});
}
}
async update(id, data, preview) {
let put = {};
};
SecureFile.prototype.update = function (id, data, preview) {
return __awaiter(this, void 0, void 0, function () {
var put, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
put = {};
if (preview)
put.preview = preview;
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
return [4 /*yield*/, this.makeRequest("/files/" + id, "PUT", put, data)];
case 1:
res = _a.sent();
statusParser(res);
return res.json();
return [2 /*return*/, res.json()];
}
async delete(id) {
let res = await this.makeRequest("/files/" + id, "DELETE", {}, undefined);
});
});
};
SecureFile.prototype.delete = function (id) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.makeRequest("/files/" + id, "DELETE", {}, undefined)];
case 1:
res = _a.sent();
statusParser(res);
return res.json();
return [2 /*return*/, res.json()];
}
async history(id) {
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}, undefined);
});
});
};
SecureFile.prototype.history = function (id) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.makeRequest("/files/" + id + "/history", "GET", {}, undefined)];
case 1:
res = _a.sent();
statusParser(res);
return res.json();
}
return [2 /*return*/, res.json()];
}
});
});
};
return SecureFile;
}());
exports.default = SecureFile;
class Unauthorized extends Error {
constructor() {
super("Not authorized");
}
var Unauthorized = /** @class */ (function (_super) {
__extends(Unauthorized, _super);
function Unauthorized() {
return _super.call(this, "Not authorized") || this;
}
return Unauthorized;
}(Error));
exports.Unauthorized = Unauthorized;
class NotFound extends Error {
constructor() {
super("Not found");
}
var NotFound = /** @class */ (function (_super) {
__extends(NotFound, _super);
function NotFound() {
return _super.call(this, "Not found") || this;
}
return NotFound;
}(Error));
exports.NotFound = NotFound;
class BadRequest extends Error {
constructor() {
super("Bad request");
}
var BadRequest = /** @class */ (function (_super) {
__extends(BadRequest, _super);
function BadRequest() {
return _super.call(this, "Bad request") || this;
}
return BadRequest;
}(Error));
exports.BadRequest = BadRequest;
function statusParser(res) {
if (res.status !== 200) {

278
test.js
View File

@ -1,149 +1,331 @@
"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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
const rsa = require("node-rsa");
const index_1 = require("./index");
const fs_1 = require("fs");
const buffer_1 = require("buffer");
let sf;
const testname = "ouiavgbsop687463743";
const testdata = new buffer_1.Buffer("Ich bin ein Test");
const newTestData = new buffer_1.Buffer("neue test daten");
const testprev = new buffer_1.Buffer("Ich bin...");
let testid;
let testver;
let testver2;
const testfolder = "iabos";
let ftestid;
let e_testid;
let private_key;
var rsa = require("node-rsa");
var index_1 = require("./index");
var fs_1 = require("fs");
var buffer_1 = require("buffer");
var sf;
var testname = "ouiavgbsop687463743";
var testdata = new buffer_1.Buffer("Ich bin ein Test");
var newTestData = new buffer_1.Buffer("neue test daten");
var testprev = new buffer_1.Buffer("Ich bin...");
var testid;
var testver;
var testver2;
var testfolder = "iabos";
var ftestid;
var e_testid;
var private_key;
module.exports = {
setUp: function (finished) {
let pk = fs_1.readFileSync("./private.pem");
var pk = fs_1.readFileSync("./private.pem");
private_key = pk.toString("utf8");
sf = new index_1.default("http://localhost:3005", "test", private_key);
finished();
},
create: async function (test) {
create: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(2);
let res = await sf.create(testname, testdata, "text", undefined, false, testprev);
return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, false, testprev)];
case 1:
res = _a.sent();
test.ok(res);
test.ok(res._id);
testid = res._id;
testver = res.version;
test.done();
return [2 /*return*/];
}
});
});
},
test: async function (test) {
test: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(2);
let res = await sf.test();
return [4 /*yield*/, sf.test()];
case 1:
res = _a.sent();
test.ok(res.test);
test.equal(res.user, "test");
test.done();
return [2 /*return*/];
}
});
});
},
get: async function (test) {
get: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(2);
let res = await sf.get(testid);
return [4 /*yield*/, sf.get(testid)];
case 1:
res = _a.sent();
test.ok(res);
test.equal(res.toString(), testdata.toString());
test.done();
return [2 /*return*/];
}
});
});
},
list: async function (test) {
list: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(4);
let res = await sf.list();
return [4 /*yield*/, sf.list()];
case 1:
res = _a.sent();
test.ok(res);
test.ok(Array.isArray(res), "Is from type Array");
test.ok(res.length > 0, "Do elements exist?");
res.forEach(e => {
res.forEach(function (e) {
if (e._id === testid) {
test.ok(true, "Element is in List");
}
});
test.done();
return [2 /*return*/];
}
});
});
},
update: async function (test) {
update: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res, res2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(5);
let res = await sf.update(testid, newTestData);
return [4 /*yield*/, sf.update(testid, newTestData)];
case 1:
res = _a.sent();
test.ok(res);
test.ok(res._id);
test.notEqual(res.version, testver, "Is new version_id generated?");
testver2 = res.version;
let res2 = await sf.get(testid);
return [4 /*yield*/, sf.get(testid)];
case 2:
res2 = _a.sent();
test.ok(res2);
test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?");
test.done();
return [2 /*return*/];
}
});
});
},
history: async function (test) {
history: function (test) {
return __awaiter(this, void 0, void 0, function () {
var his, arch;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(7);
let his = await sf.history(testid);
return [4 /*yield*/, sf.history(testid)];
case 1:
his = _a.sent();
test.ok(his);
test.ok(his.file);
test.ok(his.history);
test.equal(his.history.length, 1);
test.equal(his.history[0].version, testver);
test.equal(his.file.version, testver2, "Is correct version");
let arch = await sf.get(testid, testver);
return [4 /*yield*/, sf.get(testid, testver)];
case 2:
arch = _a.sent();
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
test.done();
return [2 /*return*/];
}
});
});
},
delete: async function (test) {
delete: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(1);
let res = await sf.delete(testid);
return [4 /*yield*/, sf.delete(testid)];
case 1:
res = _a.sent();
test.ok(res);
test.done();
return [2 /*return*/];
}
});
});
},
folder: {
create: async function (test) {
create: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(2);
let res = await sf.create(testname, testdata, "text", testfolder, false, testprev);
return [4 /*yield*/, sf.create(testname, testdata, "text", testfolder, false, testprev)];
case 1:
res = _a.sent();
test.ok(res);
test.ok(res._id);
ftestid = res._id;
testver = res.version;
test.done();
return [2 /*return*/];
}
});
});
},
list: async function (test) {
list: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(4);
let res = await sf.list(testfolder);
return [4 /*yield*/, sf.list(testfolder)];
case 1:
res = _a.sent();
test.ok(res);
test.ok(Array.isArray(res), "Is from type Array");
test.ok(res.length > 0, "Do elements exist?");
res.forEach(e => {
res.forEach(function (e) {
if (e._id === ftestid) {
test.ok(true, "Element is in List");
}
});
test.done();
return [2 /*return*/];
}
});
});
},
delete: async function (test) {
delete: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(1);
let res = await sf.delete(ftestid);
return [4 /*yield*/, sf.delete(ftestid)];
case 1:
res = _a.sent();
test.ok(res);
test.done();
return [2 /*return*/];
}
});
});
},
},
encryption: {
create: async function (test) {
create: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(2);
let res = await sf.create(testname, testdata, "text", undefined, true, testprev);
return [4 /*yield*/, sf.create(testname, testdata, "text", undefined, true, testprev)];
case 1:
res = _a.sent();
test.ok(res);
test.ok(res._id);
e_testid = res._id;
testver = res.version;
test.done();
return [2 /*return*/];
}
});
});
},
get: async function (test) {
let r = new rsa(private_key, "pkcs1-pem");
get: function (test) {
return __awaiter(this, void 0, void 0, function () {
var r, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
r = new rsa(private_key, "pkcs1-pem");
test.expect(2);
let res = await sf.get(e_testid);
return [4 /*yield*/, sf.get(e_testid)];
case 1:
res = _a.sent();
res = r.decrypt(res);
test.ok(res);
test.equal(res.toString(), testdata.toString());
test.done();
return [2 /*return*/];
}
});
});
},
delete: async function (test) {
delete: function (test) {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
test.expect(1);
let res = await sf.delete(e_testid);
return [4 /*yield*/, sf.delete(e_testid)];
case 1:
res = _a.sent();
test.ok(res);
test.done();
return [2 /*return*/];
}
});
});
},
}
};

View File

@ -1,13 +1,17 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation: */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"lib": [
"es6",
"dom"
],
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */