forked from hibas123/SecureFileWrapper
Adding web support
This commit is contained in:
parent
3e97877edd
commit
d778a36ea8
1
index.d.ts
vendored
1
index.d.ts
vendored
@ -1,4 +1,5 @@
|
||||
/// <reference types="node" />
|
||||
import "isomorphic-fetch";
|
||||
export interface File {
|
||||
_id: string;
|
||||
type: "binary" | "text";
|
||||
|
19
index.js
19
index.js
@ -1,7 +1,8 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const rsa = require("node-rsa");
|
||||
const node_fetch_1 = require("node-fetch");
|
||||
require("isomorphic-fetch");
|
||||
const btb = require("blob-to-buffer");
|
||||
class SecureFile {
|
||||
constructor(server, username, private_key) {
|
||||
this.Server = server;
|
||||
@ -9,7 +10,7 @@ class SecureFile {
|
||||
this.PrivateKey = private_key;
|
||||
}
|
||||
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
|
||||
let code = (await code_res.json()).code;
|
||||
let r = new rsa(this.PrivateKey, "pkcs1-pem");
|
||||
@ -27,7 +28,7 @@ class SecureFile {
|
||||
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
|
||||
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() {
|
||||
let res = await this.makeRequest("/test", "GET", {}, undefined);
|
||||
@ -65,8 +66,20 @@ class SecureFile {
|
||||
res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
|
||||
}
|
||||
statusParser(res);
|
||||
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) {
|
||||
let put = {};
|
||||
if (preview)
|
||||
|
15
index.ts
15
index.ts
@ -1,5 +1,7 @@
|
||||
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 {
|
||||
_id: string;
|
||||
@ -84,7 +86,16 @@ export default class SecureFile {
|
||||
res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
|
||||
}
|
||||
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> {
|
||||
|
51
package-lock.json
generated
Normal file
51
package-lock.json
generated
Normal 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="
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "secure-file-wrapper",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.4",
|
||||
"main": "index.js",
|
||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||
"license": "MIT",
|
||||
@ -10,10 +10,13 @@
|
||||
"test": "nodeunit test.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"node-fetch": "^1.7.3",
|
||||
"blob-to-buffer": "^1.2.7",
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"node-rsa": "^0.4.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/blob-to-buffer": "^1.2.0",
|
||||
"@types/isomorphic-fetch": "^0.0.34",
|
||||
"@types/node": "^9.4.0",
|
||||
"@types/node-fetch": "^1.6.7",
|
||||
"@types/node-rsa": "^0.4.1",
|
||||
|
Loading…
Reference in New Issue
Block a user