forked from hibas123/SecureFileWrapper
Adding typings and compiled files
This commit is contained in:
parent
963714d6ce
commit
ed0a4d7aa2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
yarn.lock
|
yarn.lock
|
||||||
*.js
|
|
||||||
private.pem
|
private.pem
|
||||||
|
36
index.d.ts
vendored
Normal file
36
index.d.ts
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/// <reference types="node" />
|
||||||
|
export interface File {
|
||||||
|
_id: string;
|
||||||
|
type: "binary" | "text";
|
||||||
|
name: string;
|
||||||
|
time: string;
|
||||||
|
preview: string;
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
export interface History {
|
||||||
|
file: File;
|
||||||
|
history: File[];
|
||||||
|
}
|
||||||
|
export default class SecureFile {
|
||||||
|
private Server;
|
||||||
|
private Username;
|
||||||
|
private PrivateKey;
|
||||||
|
constructor(server: string, username: string, private_key: string);
|
||||||
|
private getCode();
|
||||||
|
private makeRequest(endpoint, method, query, body);
|
||||||
|
list(): Promise<File[]>;
|
||||||
|
create(name: string, data: Buffer, type: "text" | "binary", preview?: Buffer): Promise<File>;
|
||||||
|
get(id: string, version?: string): Promise<Buffer>;
|
||||||
|
update(id: string, data: Buffer, preview?: Buffer): Promise<File>;
|
||||||
|
delete(id: string): Promise<boolean>;
|
||||||
|
history(id: string): Promise<History>;
|
||||||
|
}
|
||||||
|
export declare class Unauthorized extends Error {
|
||||||
|
constructor();
|
||||||
|
}
|
||||||
|
export declare class NotFound extends Error {
|
||||||
|
constructor();
|
||||||
|
}
|
||||||
|
export declare class BadRequest extends Error {
|
||||||
|
constructor();
|
||||||
|
}
|
104
index.js
Normal file
104
index.js
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const rsa = require("node-rsa");
|
||||||
|
const node_fetch_1 = require("node-fetch");
|
||||||
|
class SecureFile {
|
||||||
|
constructor(server, username, private_key) {
|
||||||
|
this.Server = server;
|
||||||
|
this.Username = username;
|
||||||
|
this.PrivateKey = private_key;
|
||||||
|
}
|
||||||
|
async getCode() {
|
||||||
|
let code_res = await node_fetch_1.default(this.Server + "/code?username=" + this.Username);
|
||||||
|
//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") };
|
||||||
|
}
|
||||||
|
async makeRequest(endpoint, method, query, body) {
|
||||||
|
let code = await this.getCode();
|
||||||
|
query.code = code.code;
|
||||||
|
query.signature = code.signature;
|
||||||
|
let query_str = "?";
|
||||||
|
let first = true;
|
||||||
|
for (let key in query) {
|
||||||
|
if (!first)
|
||||||
|
query_str += "&";
|
||||||
|
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
return await node_fetch_1.default(this.Server + endpoint + query_str, { method: method, body: body });
|
||||||
|
}
|
||||||
|
async list() {
|
||||||
|
let res = await this.makeRequest("/files", "GET", {}, undefined);
|
||||||
|
statusParser(res);
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
async create(name, data, type, preview) {
|
||||||
|
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview }, data);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
statusParser(res);
|
||||||
|
return res.buffer();
|
||||||
|
}
|
||||||
|
async update(id, data, preview) {
|
||||||
|
let put = {};
|
||||||
|
if (preview)
|
||||||
|
put.preview = preview;
|
||||||
|
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
async delete(id) {
|
||||||
|
let res = await this.makeRequest("/files/" + id, "DELETE", {}, undefined);
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
async history(id) {
|
||||||
|
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}, undefined);
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.default = SecureFile;
|
||||||
|
class Unauthorized extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Not authorized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.Unauthorized = Unauthorized;
|
||||||
|
class NotFound extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.NotFound = NotFound;
|
||||||
|
class BadRequest extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Bad request");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BadRequest = BadRequest;
|
||||||
|
function statusParser(res) {
|
||||||
|
if (res.status !== 200) {
|
||||||
|
switch (res.status) {
|
||||||
|
case 400:
|
||||||
|
throw new BadRequest();
|
||||||
|
case 404:
|
||||||
|
throw new NotFound();
|
||||||
|
case 403:
|
||||||
|
throw new Unauthorized();
|
||||||
|
default:
|
||||||
|
throw new Error(res.statusText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "SecureFileWrapper",
|
"name": "secure-file-wrapper",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"types": "index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"test": "nodeunit test.js"
|
"test": "nodeunit test.js"
|
||||||
|
81
test.js
Normal file
81
test.js
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
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;
|
||||||
|
module.exports = {
|
||||||
|
setUp: function (finished) {
|
||||||
|
let pk = fs_1.readFileSync("./private.pem");
|
||||||
|
sf = new index_1.default("http://localhost:3005", "test", pk.toString("utf8"));
|
||||||
|
finished();
|
||||||
|
},
|
||||||
|
create: async function (test) {
|
||||||
|
test.expect(2);
|
||||||
|
let res = await sf.create(testname, testdata, "text", testprev);
|
||||||
|
test.ok(res);
|
||||||
|
test.ok(res._id);
|
||||||
|
testid = res._id;
|
||||||
|
testver = res.version;
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
get: async function (test) {
|
||||||
|
test.expect(2);
|
||||||
|
let res = await sf.get(testid);
|
||||||
|
test.ok(res);
|
||||||
|
test.equal(res.toString(), testdata.toString());
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
list: async function (test) {
|
||||||
|
test.expect(4);
|
||||||
|
let res = await sf.list();
|
||||||
|
console.log(res);
|
||||||
|
test.ok(res);
|
||||||
|
test.ok(Array.isArray(res), "Is from type Array");
|
||||||
|
test.ok(res.length > 0, "Do elements exist?");
|
||||||
|
res.forEach(e => {
|
||||||
|
if (e._id === testid) {
|
||||||
|
test.ok(true, "Element is in List");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
update: async function (test) {
|
||||||
|
test.expect(5);
|
||||||
|
let res = await sf.update(testid, newTestData);
|
||||||
|
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);
|
||||||
|
test.ok(res2);
|
||||||
|
test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?");
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
history: async function (test) {
|
||||||
|
test.expect(7);
|
||||||
|
let his = await sf.history(testid);
|
||||||
|
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);
|
||||||
|
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
delete: async function (test) {
|
||||||
|
test.expect(1);
|
||||||
|
let res = await sf.delete(testid);
|
||||||
|
test.ok(res);
|
||||||
|
test.done();
|
||||||
|
}
|
||||||
|
};
|
@ -1,14 +1,13 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
"watch": true,
|
|
||||||
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
|
"target": "es2017", /* 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'. */
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||||
// "lib": [], /* Specify library files to be included in the compilation: */
|
// "lib": [], /* Specify library files to be included in the compilation: */
|
||||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
// "checkJs": true, /* Report errors in .js files. */
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||||
// "declaration": true, /* Generates corresponding '.d.ts' file. */
|
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||||
// "outDir": "./", /* Redirect output structure to the directory. */
|
// "outDir": "./", /* Redirect output structure to the directory. */
|
||||||
|
Loading…
Reference in New Issue
Block a user