Compare commits
21 Commits
Author | SHA1 | Date |
---|---|---|
Fabian | 933ecc050a | |
Fabian | 38b2cd8fa4 | |
Fabian | 4b7c9ec6cd | |
Fabian Stamm | 92dba5274e | |
Fabian Stamm | c63ff06453 | |
Fabian Stamm | 8e0b859408 | |
Fabian Stamm | a8d5382ac3 | |
Fabian Stamm | b76808022f | |
Fabian Stamm | 8dbc2bcb7f | |
Fabian Stamm | 2c4a0203d5 | |
Fabian Stamm | 500bb33689 | |
Fabian Stamm | 9b57728892 | |
Unknown | 8de05e6b7f | |
Fabian Stamm | baa1f106e6 | |
Fabian Stamm | 7a8cc08d4a | |
Fabian Stamm | 7f403f4163 | |
Fabian Stamm | f24645a6eb | |
Fabian Stamm | f46f4982e9 | |
Fabian Stamm | a1241afd28 | |
Fabian Stamm | f11334f814 | |
Fabian Stamm | ebde530f76 |
|
@ -1,3 +1,3 @@
|
|||
tsconfig.json
|
||||
src/
|
||||
node_modules/
|
||||
.vscode/
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"cSpell.enabled": false
|
||||
}
|
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "secure-file-wrapper",
|
||||
"version": "2.0.0",
|
||||
"name": "@hibas123/secure-file-wrapper",
|
||||
"version": "2.5.1",
|
||||
"main": "lib/index.js",
|
||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||
"license": "MIT",
|
||||
|
@ -8,20 +8,22 @@
|
|||
"scripts": {
|
||||
"build": "tsc",
|
||||
"watch": "tsc --watch",
|
||||
"prepublish": "tsc",
|
||||
"prepublishOnly": "tsc",
|
||||
"test": "mocha lib/test.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"isomorphic-fetch": "^2.2.1"
|
||||
"@hibas123/utils": "^2.1.0",
|
||||
"cross-fetch": "^3.0.4",
|
||||
"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",
|
||||
"@types/mocha": "^5.2.7",
|
||||
"@types/node": "^12.0.12",
|
||||
"@types/node-fetch": "^2.3.7",
|
||||
"@types/uuid": "^3.4.5",
|
||||
"chai": "^4.1.2",
|
||||
"mocha": "^5.2.0",
|
||||
"typescript": "^3.2.2"
|
||||
"mocha": "^6.1.4",
|
||||
"typescript": "^3.5.2"
|
||||
}
|
||||
}
|
105
src/index.ts
105
src/index.ts
|
@ -1,5 +1,6 @@
|
|||
import Observable from "./observable";
|
||||
import Lock from "./lock";
|
||||
import { Observable, Lock } from "@hibas123/utils";
|
||||
//this references global on node and window in browser
|
||||
const fetch = typeof window !== "undefined" && typeof window.fetch !== undefined ? window.fetch : require("cross-fetch").default;
|
||||
|
||||
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) {
|
||||
|
@ -85,13 +84,15 @@ function statusParser(res: Response) {
|
|||
}
|
||||
}
|
||||
|
||||
export type JWTCallback = (err: Error | null | string, jwt: string) => void;
|
||||
|
||||
export default class SecureFileWrapper {
|
||||
private _jwtObservableServer: Observable<(jwt: string) => void> = new Observable();
|
||||
private _jwtObservableServer: Observable<JWTCallback> = new Observable();
|
||||
jwtObservable = this._jwtObservableServer.getPublicApi();
|
||||
|
||||
jwt: string;
|
||||
private jwt: string;
|
||||
|
||||
auth_lock = new Lock();
|
||||
private auth_lock = new Lock();
|
||||
|
||||
constructor(private server: string) {
|
||||
if (this.server.endsWith("/")) {
|
||||
|
@ -104,10 +105,18 @@ export default class SecureFileWrapper {
|
|||
public async getJWT() {
|
||||
if (!this.auth_lock.locked) {
|
||||
let lock = await this.auth_lock.getLock();
|
||||
this._jwtObservableServer.send((jwt: string) => {
|
||||
this.jwt = jwt;
|
||||
lock.release();
|
||||
});
|
||||
await new Promise((yes, no) => {
|
||||
this._jwtObservableServer.send((err: Error | null | string, jwt: string) => {
|
||||
if (err) {
|
||||
this.jwt = undefined;
|
||||
no(err);
|
||||
}
|
||||
else {
|
||||
this.jwt = jwt;
|
||||
yes();
|
||||
}
|
||||
});
|
||||
}).finally(() => lock.release())
|
||||
}
|
||||
|
||||
await this.auth_lock.getLock().then(lock => lock.release())
|
||||
|
@ -131,13 +140,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);
|
||||
|
@ -152,6 +159,19 @@ export default class SecureFileWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
private fixIFileVersion(version: IFileVersion): IFileVersion {
|
||||
version.time = new Date(version.time)
|
||||
return version;
|
||||
}
|
||||
|
||||
private fixIFile(file: IFile): IFile {
|
||||
file.active.time = new Date(file.active.time)
|
||||
if (file.versions) {
|
||||
file.versions = file.versions.map(e => this.fixIFileVersion(e))
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
// async test(jwt): Promise<{ user: string, test: true }> {
|
||||
// let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
|
||||
// statusParser(res);
|
||||
|
@ -162,21 +182,27 @@ export default class SecureFileWrapper {
|
|||
let query: any = {}
|
||||
if (folder) query.folder = folder;
|
||||
let res = await this.makeRequest("/files", "GET", query);
|
||||
let d = await res.json();
|
||||
return d.files;
|
||||
let d: { files: IFile[] } = await res.json();
|
||||
return d.files.map(e => this.fixIFile(e));
|
||||
}
|
||||
|
||||
async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string): Promise<IFile> {
|
||||
async create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string, id?: string, date?: Date): Promise<IFile> {
|
||||
let params: any = { type: type, name: name };
|
||||
if (preview) {
|
||||
if (preview)
|
||||
params.preview = preview;
|
||||
}
|
||||
if (folder) {
|
||||
|
||||
if (folder)
|
||||
params.folder = folder;
|
||||
}
|
||||
|
||||
if (id)
|
||||
params.id = id
|
||||
|
||||
if (date)
|
||||
params.date = date.toJSON()
|
||||
|
||||
|
||||
let res = await this.makeRequest("/files", "POST", params, data);
|
||||
return (await res.json()).file;
|
||||
return this.fixIFile((await res.json()).file);
|
||||
}
|
||||
|
||||
async get(id: string, version?: string): Promise<ArrayBuffer> {
|
||||
|
@ -187,37 +213,38 @@ 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;
|
||||
}
|
||||
return res.arrayBuffer()
|
||||
}
|
||||
|
||||
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string): Promise<IFile> {
|
||||
let put: any = {};
|
||||
if (preview) put.preview = preview;
|
||||
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
|
||||
async update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string, date?: Date, old = false): Promise<IFile> {
|
||||
let params: any = { old };
|
||||
if (preview) params.preview = preview;
|
||||
if (date)
|
||||
params.date = date.toJSON()
|
||||
let res = await this.makeRequest("/files/" + id, "PUT", params, data);
|
||||
|
||||
let json = await res.json()
|
||||
return json.file;
|
||||
return this.fixIFile(json.file);
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<boolean> {
|
||||
async delete(id: string): Promise<void> {
|
||||
let res = await this.makeRequest("/files/" + id, "DELETE", {});
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async history(id: string): Promise<IHistory> {
|
||||
let res = await this.makeRequest(`/files/${id}/history`, "GET", {});
|
||||
statusParser(res);
|
||||
return res.json();
|
||||
let data: IHistory = await res.json();
|
||||
data.file = this.fixIFile(data.file)
|
||||
data.history = data.history.map(v => this.fixIFileVersion(v));
|
||||
return data;
|
||||
}
|
||||
|
||||
async restore(id: string, version: string) {
|
||||
await this.makeRequest(`/files/${id}/history/${version}/restore`, "PUT", {});
|
||||
}
|
||||
|
||||
async clean(id: string, val: number | Date): Promise<void> {
|
||||
let query = typeof val === "number" ? { count: val } : { date: val.toISOString() };
|
||||
return this.makeRequest(`/files/${id}/history/clean`, "PUT", query);
|
||||
}
|
||||
}
|
36
src/lock.ts
36
src/lock.ts
|
@ -1,36 +0,0 @@
|
|||
export type Release = { release: () => void };
|
||||
export default class Lock {
|
||||
private _locked: boolean = false;
|
||||
get locked() {
|
||||
return this._locked;
|
||||
}
|
||||
private toCome: (() => void)[] = [];
|
||||
|
||||
constructor() {
|
||||
this.release = this.release.bind(this);
|
||||
}
|
||||
|
||||
async getLock(): Promise<Release> {
|
||||
if (!this._locked) return { release: this.lock() };
|
||||
else {
|
||||
return new Promise<Release>((resolve) => {
|
||||
this.toCome.push(() => {
|
||||
resolve({ release: this.lock() });
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private lock() {
|
||||
this._locked = true;
|
||||
return this.release;
|
||||
}
|
||||
|
||||
private async release() {
|
||||
if (this.toCome.length > 0) {
|
||||
this.toCome.shift()();
|
||||
} else {
|
||||
this._locked = false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
export type ObserverCallback<T> = (data: T) => void;
|
||||
|
||||
export default class Observable<T = any> {
|
||||
private subscriber: ObserverCallback<T[]>[] = [];
|
||||
private events: T[] = [];
|
||||
private timeout = undefined;
|
||||
|
||||
constructor(private collect: boolean = true, private collect_intervall: number = 100) { }
|
||||
|
||||
getPublicApi() {
|
||||
return {
|
||||
subscribe: (callback: ObserverCallback<T[]>) => {
|
||||
if (this.subscriber.indexOf(callback) < 0)
|
||||
this.subscriber.push(callback)
|
||||
},
|
||||
unsubscribe: (callback: ObserverCallback<T[]>) => {
|
||||
let idx = this.subscriber.indexOf(callback);
|
||||
if (idx >= 0) {
|
||||
this.subscriber.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
send(data: T) {
|
||||
if (!this.collect)
|
||||
this.subscriber.forEach(e => e([data]));
|
||||
else {
|
||||
this.events.push(data);
|
||||
if (!this.timeout) {
|
||||
this.timeout = setTimeout(() => {
|
||||
this.subscriber.forEach(e => e(this.events));
|
||||
this.timeout = 0;
|
||||
}, this.collect_intervall);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
59
src/test.ts
59
src/test.ts
|
@ -1,5 +1,5 @@
|
|||
import SecureFile, { NotFound } from "./index";
|
||||
|
||||
import * as v4 from "uuid/v4"
|
||||
import { TextEncoder, TextDecoder } from "util";
|
||||
|
||||
const testname = "ouiavgbsop687463743"
|
||||
|
@ -7,6 +7,7 @@ const encoder = new TextEncoder();
|
|||
const decoder = new TextDecoder();
|
||||
const testdata = encoder.encode("Ich bin ein Test");
|
||||
const newTestData = encoder.encode("neue test daten");
|
||||
const newTestDataOld = encoder.encode("neue test daten asd");
|
||||
const testprev = "Ich bin...";
|
||||
|
||||
const testfolder = "iabos";
|
||||
|
@ -53,6 +54,15 @@ function test(sf: SecureFile) {
|
|||
expect(found, "Element not in List").to.be.true;
|
||||
})
|
||||
|
||||
it("update to history", async () => {
|
||||
let res = await sf.update(testid, newTestDataOld, undefined, undefined, true);
|
||||
expect(res, "No data returned").to.exist;
|
||||
expect(res._id, "_id missing").to.exist;
|
||||
expect(res.active.version, "New version was created").to.equal(testver);
|
||||
let res2 = await sf.get(testid);
|
||||
expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(testdata));
|
||||
})
|
||||
|
||||
it("update", async () => {
|
||||
let res = await sf.update(testid, newTestData, undefined);
|
||||
expect(res, "No data returned").to.exist;
|
||||
|
@ -68,9 +78,9 @@ function test(sf: SecureFile) {
|
|||
expect(his, "no data returned").to.exist;
|
||||
expect(his.file, "file not set").to.exist;
|
||||
expect(his.history, "history not set").to.exist;
|
||||
expect(his.history.length, `Not expected history length. Expected 1 got ${his.history.length}`).to.be.equal(1);
|
||||
expect(his.history.length, `Not expected history length. Expected 1 got ${his.history.length}`).to.be.equal(2);
|
||||
|
||||
expect(his.history[0].version, "Wrong version on history").to.be.equal(testver);
|
||||
expect(his.history[1].version, "Wrong version on history").to.be.equal(testver);
|
||||
expect(his.file.active.version, "Wrong version on file").to.be.equal(testver2);
|
||||
});
|
||||
|
||||
|
@ -88,8 +98,42 @@ function test(sf: SecureFile) {
|
|||
})
|
||||
|
||||
it("delete", async () => {
|
||||
let res = await sf.delete(testid);
|
||||
expect(res, "Res not set").to.exist;
|
||||
await sf.delete(testid);
|
||||
})
|
||||
|
||||
|
||||
describe("fixed id", () => {
|
||||
let id = v4();
|
||||
it("create", async () => {
|
||||
let res = await sf.create(testname, testdata, "text", undefined, testprev, id)
|
||||
expect(res, "Res isnnot set").to.exist;
|
||||
expect(res._id, "Res has no _id").to.exist;
|
||||
expect(res._id, "Res has invalid _id").to.be.equal(id)
|
||||
})
|
||||
|
||||
it("get", async () => {
|
||||
let res = await sf.get(id);
|
||||
expect(res, "No data returned").to.exist;
|
||||
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
||||
})
|
||||
})
|
||||
|
||||
describe("predefined date", () => {
|
||||
let id = v4();
|
||||
it("create", async () => {
|
||||
const date = new Date("2017-01-01T00:00:00.000Z")
|
||||
let res = await sf.create(testname, testdata, "text", undefined, testprev, id, date)
|
||||
expect(res, "Res isnnot set").to.exist;
|
||||
expect(res._id, "Res has no _id").to.exist;
|
||||
expect(res._id, "Res has invalid _id").to.be.equal(id)
|
||||
expect(res.active.time.toJSON()).to.be.equal(date.toJSON())
|
||||
})
|
||||
|
||||
it("list", async () => {
|
||||
let res = await sf.get(id);
|
||||
expect(res, "No data returned").to.exist;
|
||||
expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
|
||||
})
|
||||
})
|
||||
|
||||
describe("folder", () => {
|
||||
|
@ -115,8 +159,7 @@ function test(sf: SecureFile) {
|
|||
})
|
||||
|
||||
it("delete", async () => {
|
||||
let res = await sf.delete(ftestid);
|
||||
expect(res, "Res not set").to.exist;
|
||||
await sf.delete(ftestid);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
@ -124,7 +167,7 @@ function test(sf: SecureFile) {
|
|||
describe("SecureFile Tests", function () {
|
||||
let sf = new SecureFile("http://localhost:3004");
|
||||
sf.jwtObservable.subscribe((callback) => {
|
||||
callback[0]("TESTJWT");
|
||||
callback(null, "TESTJWT");
|
||||
})
|
||||
test(sf)
|
||||
})
|
|
@ -1,9 +1,12 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
/* Basic Options */
|
||||
"target": "es6", /* 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'. */
|
||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
"target": "es6",
|
||||
/* 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'. */
|
||||
"declaration": true,
|
||||
/* Generates corresponding '.d.ts' file. */
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
|
|
Reference in New Issue