From d778a36ea8bc57d0bfa517d0c06f9a03e95904c9 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sun, 25 Feb 2018 12:20:30 +0100 Subject: [PATCH] Adding web support --- index.d.ts | 1 + index.js | 21 +++++++++++++++---- index.ts | 15 ++++++++++++-- package-lock.json | 51 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 +++++-- 5 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 package-lock.json diff --git a/index.d.ts b/index.d.ts index b272df8..e9f6e37 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,5 @@ /// +import "isomorphic-fetch"; export interface File { _id: string; type: "binary" | "text"; diff --git a/index.js b/index.js index 8c1630a..5753232 100644 --- a/index.js +++ b/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,7 +66,19 @@ class SecureFile { res = await this.makeRequest("/files/" + id, "GET", {}, undefined); } 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) { let put = {}; diff --git a/index.ts b/index.ts index 50b2dfe..f9cd3e7 100644 --- a/index.ts +++ b/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 ((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: string, data: Buffer, preview?: Buffer): Promise { diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..235da2f --- /dev/null +++ b/package-lock.json @@ -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=" + } + } +} diff --git a/package.json b/package.json index 30a7b55..769359e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "secure-file-wrapper", - "version": "1.0.2", + "version": "1.0.4", "main": "index.js", "author": "Fabian Stamm ", "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",