Changing fetch package for better web support

This commit is contained in:
Fabian Stamm 2019-01-20 22:54:01 +01:00
parent b76808022f
commit a8d5382ac3
3 changed files with 27 additions and 65 deletions

72
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/secure-file-wrapper",
"version": "2.1.0",
"version": "2.3.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -10,12 +10,6 @@
"integrity": "sha512-2Y8uPt0/jwjhQ6EiluT0XCri1Dbplr0ZxfFXUz+ye13gaqE8u5gL5ppao1JrUYr9cIip5S6MvQzBS7Kke7U9VA==",
"dev": true
},
"@types/isomorphic-fetch": {
"version": "0.0.34",
"resolved": "https://registry.npmjs.org/@types/isomorphic-fetch/-/isomorphic-fetch-0.0.34.tgz",
"integrity": "sha1-PDSD5gbAQTeEOOlRRk8A5OYHBtY=",
"dev": true
},
"@types/mocha": {
"version": "5.2.5",
"resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-5.2.5.tgz",
@ -106,6 +100,22 @@
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true
},
"cross-fetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.0.0.tgz",
"integrity": "sha512-P8HyKlMwT1ed9LqEWlJu+zfcxfn0KI4Nl4nxyvu1a8sg4vgtHdwhElZOgSNzoar44zQMdliZcve4QG/04AUi9Q==",
"requires": {
"node-fetch": "2.3.0",
"whatwg-fetch": "3.0.0"
},
"dependencies": {
"whatwg-fetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz",
"integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q=="
}
}
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
@ -130,14 +140,6 @@
"integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==",
"dev": true
},
"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.13"
}
},
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
@ -188,11 +190,6 @@
"integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=",
"dev": true
},
"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=="
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
@ -209,31 +206,6 @@
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
"dev": true
},
"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.0.1",
"whatwg-fetch": ">=0.10.0"
},
"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.11",
"is-stream": "^1.0.1"
}
}
}
},
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
@ -283,6 +255,11 @@
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",
"dev": true
},
"node-fetch": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.3.0.tgz",
"integrity": "sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA=="
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@ -330,11 +307,6 @@
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA=="
},
"whatwg-fetch": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz",
"integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ="
},
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/secure-file-wrapper",
"version": "2.3.0",
"version": "2.3.1",
"main": "lib/index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT",
@ -12,12 +12,11 @@
"test": "mocha lib/test.js"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"cross-fetch": "^3.0.0",
"uuid": "^3.3.2"
},
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/isomorphic-fetch": "^0.0.34",
"@types/mocha": "^5.2.2",
"@types/node": "^10.12.18",
"@types/node-fetch": "^2.1.4",

View File

@ -1,5 +1,6 @@
import Observable from "./observable";
import Lock from "./lock";
import fetch from "cross-fetch"
export interface IFileVersion {
version: string;
@ -66,8 +67,6 @@ export class BadRequest extends Error {
}
}
import * as fetch from "isomorphic-fetch";
function statusParser(res: Response) {
if (res.status !== 200) {
switch (res.status) {
@ -131,13 +130,11 @@ export default class SecureFileWrapper {
"cache-control": "no-cache"
};
let body_n;
if (body) {
headers["Content-Type"] = "application/octet-stream"
body_n = Buffer ? Buffer.from(body instanceof ArrayBuffer ? body : body.buffer) : body;
}
try {
let res = await fetch(this.server + endpoint + query_str, { method, body: body_n, headers });
let res = await fetch(this.server + endpoint + query_str, { method, body, headers });
if (res.status === 401 && !second) {
await this.getJWT();
return this.makeRequest(endpoint, method, query, body, true);
@ -206,13 +203,7 @@ export default class SecureFileWrapper {
res = await this.makeRequest("/files/" + id, "GET", {});
}
if (res.arrayBuffer) {
return res.arrayBuffer()
} else {
let blob: Buffer = await (<any>res).buffer()
// console.log(blob.length);
return Uint8Array.from(blob).buffer;
}
}
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date, old = false): Promise<IFile> {