Adding web support

This commit is contained in:
Fabian Stamm 2018-02-25 12:20:30 +01:00
parent 3e97877edd
commit d778a36ea8
5 changed files with 87 additions and 8 deletions

1
index.d.ts vendored
View File

@ -1,4 +1,5 @@
/// <reference types="node" /> /// <reference types="node" />
import "isomorphic-fetch";
export interface File { export interface File {
_id: string; _id: string;
type: "binary" | "text"; type: "binary" | "text";

View File

@ -1,7 +1,8 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const rsa = require("node-rsa"); const rsa = require("node-rsa");
const node_fetch_1 = require("node-fetch"); require("isomorphic-fetch");
const btb = require("blob-to-buffer");
class SecureFile { class SecureFile {
constructor(server, username, private_key) { constructor(server, username, private_key) {
this.Server = server; this.Server = server;
@ -9,7 +10,7 @@ class SecureFile {
this.PrivateKey = private_key; this.PrivateKey = private_key;
} }
async getCode() { async getCode() {
let code_res = await node_fetch_1.default(this.Server + "/code?username=" + this.Username); let code_res = await fetch(this.Server + "/code?username=" + this.Username);
//ToDo check status Codes //ToDo check status Codes
let code = (await code_res.json()).code; let code = (await code_res.json()).code;
let r = new rsa(this.PrivateKey, "pkcs1-pem"); let r = new rsa(this.PrivateKey, "pkcs1-pem");
@ -27,7 +28,7 @@ class SecureFile {
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]); query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
first = false; first = false;
} }
return await node_fetch_1.default(this.Server + endpoint + query_str, { method: method, body: body }); return await fetch(this.Server + endpoint + query_str, { method: method, body: body });
} }
async test() { async test() {
let res = await this.makeRequest("/test", "GET", {}, undefined); let res = await this.makeRequest("/test", "GET", {}, undefined);
@ -65,7 +66,19 @@ class SecureFile {
res = await this.makeRequest("/files/" + id, "GET", {}, undefined); res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
} }
statusParser(res); statusParser(res);
return res.buffer(); if (res.buffer) {
return res.buffer();
}
else {
return new Promise(async (resolve, reject) => {
btb(await res.blob(), (err, buffer) => {
if (err)
reject(err);
else
resolve(buffer);
});
});
}
} }
async update(id, data, preview) { async update(id, data, preview) {
let put = {}; let put = {};

View File

@ -1,5 +1,7 @@
import * as rsa from "node-rsa"; import * as rsa from "node-rsa";
import fetch, { Response } from "node-fetch"; import "isomorphic-fetch";
import * as btb from "blob-to-buffer"
export interface File { export interface File {
_id: string; _id: string;
@ -84,7 +86,16 @@ export default class SecureFile {
res = await this.makeRequest("/files/" + id, "GET", {}, undefined); res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
} }
statusParser(res); statusParser(res);
return res.buffer(); if ((<any>res).buffer) {
return (<any>res).buffer();
} else {
return new Promise<Buffer>(async (resolve, reject) => {
btb(await res.blob(), (err, buffer) => {
if (err) reject(err);
else resolve(buffer);
})
})
}
} }
async update(id: string, data: Buffer, preview?: Buffer): Promise<File> { async update(id: string, data: Buffer, preview?: Buffer): Promise<File> {

51
package-lock.json generated Normal file
View File

@ -0,0 +1,51 @@
{
"name": "secure-file-wrapper",
"version": "1.0.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"encoding": {
"version": "0.1.12",
"resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz",
"integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=",
"requires": {
"iconv-lite": "0.4.19"
}
},
"iconv-lite": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz",
"integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ=="
},
"is-stream": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz",
"integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ="
},
"isomorphic-fetch": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz",
"integrity": "sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=",
"requires": {
"node-fetch": "1.7.3",
"whatwg-fetch": "2.0.3"
},
"dependencies": {
"node-fetch": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
"integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
"requires": {
"encoding": "0.1.12",
"is-stream": "1.1.0"
}
}
}
},
"whatwg-fetch": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz",
"integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ="
}
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "secure-file-wrapper", "name": "secure-file-wrapper",
"version": "1.0.2", "version": "1.0.4",
"main": "index.js", "main": "index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT", "license": "MIT",
@ -10,10 +10,13 @@
"test": "nodeunit test.js" "test": "nodeunit test.js"
}, },
"dependencies": { "dependencies": {
"node-fetch": "^1.7.3", "blob-to-buffer": "^1.2.7",
"isomorphic-fetch": "^2.2.1",
"node-rsa": "^0.4.2" "node-rsa": "^0.4.2"
}, },
"devDependencies": { "devDependencies": {
"@types/blob-to-buffer": "^1.2.0",
"@types/isomorphic-fetch": "^0.0.34",
"@types/node": "^9.4.0", "@types/node": "^9.4.0",
"@types/node-fetch": "^1.6.7", "@types/node-fetch": "^1.6.7",
"@types/node-rsa": "^0.4.1", "@types/node-rsa": "^0.4.1",