@tblabs/storage 5.0.2 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OnlineStorage.d.ts +11 -3
- package/OnlineStorage.js +41 -11
- package/package.json +2 -2
- package/tblabs-storage-5.2.0.tgz +0 -0
- package/tblabs-storage-5.0.2.tgz +0 -0
package/OnlineStorage.d.ts
CHANGED
|
@@ -7,16 +7,24 @@ import { Dataset } from "./Dataset";
|
|
|
7
7
|
import { IStorageUser, IStorageUserPrivileges } from "./IStorageUser";
|
|
8
8
|
export declare class OnlineStorage {
|
|
9
9
|
private database;
|
|
10
|
+
private ANONYMOUS_USER;
|
|
11
|
+
private user;
|
|
10
12
|
private logEnabled;
|
|
11
13
|
private readonly _bus;
|
|
14
|
+
private onUserChange;
|
|
15
|
+
private readonly USER_LOCAL_STORAGE_KEY;
|
|
12
16
|
get DatabaseDir(): dir;
|
|
13
17
|
constructor(server: url, database: dir, sender?: ISender, bus?: MessageBus);
|
|
18
|
+
private IsBrowserEnvironment;
|
|
19
|
+
private LoadUserFromLocalStorage;
|
|
20
|
+
get User(): IStorageUser;
|
|
14
21
|
Login(password: string): Promise<IStorageUser>;
|
|
22
|
+
private SaveUserInLocalStorage;
|
|
23
|
+
OnUserChange(handler: (user: IStorageUser) => void): this;
|
|
15
24
|
Register(id: string, role: string, name: string, password: string, privileges: IStorageUserPrivileges, icon?: string, tags?: string[]): Promise<IStorageUser>;
|
|
16
25
|
Logout(): this;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
User(sender: ISender): this;
|
|
26
|
+
get IsAuthorized(): boolean;
|
|
27
|
+
SetUser(sender: ISender): this;
|
|
20
28
|
EnableLog(): this;
|
|
21
29
|
DisableLog(): this;
|
|
22
30
|
Log(...message: any[]): void;
|
package/OnlineStorage.js
CHANGED
|
@@ -5,23 +5,55 @@ import { LoginQuery } from "./Messages/LoginQuery";
|
|
|
5
5
|
import { RegisterCommand } from "./Messages/RegisterCommand";
|
|
6
6
|
export class OnlineStorage {
|
|
7
7
|
database;
|
|
8
|
+
ANONYMOUS_USER = { Id: "Anonymous" };
|
|
9
|
+
user = this.ANONYMOUS_USER;
|
|
8
10
|
logEnabled = false;
|
|
9
11
|
_bus;
|
|
12
|
+
onUserChange = [];
|
|
13
|
+
USER_LOCAL_STORAGE_KEY;
|
|
10
14
|
get DatabaseDir() {
|
|
11
15
|
return this.database;
|
|
12
16
|
}
|
|
13
|
-
constructor(server, database, sender =
|
|
17
|
+
constructor(server, database, sender = this.ANONYMOUS_USER, bus) {
|
|
14
18
|
this.database = database;
|
|
15
19
|
this._bus = bus || new MessageBus(server, sender);
|
|
20
|
+
this.USER_LOCAL_STORAGE_KEY = "OnlineStorage:User:" + this.database;
|
|
21
|
+
this.LoadUserFromLocalStorage();
|
|
22
|
+
}
|
|
23
|
+
IsBrowserEnvironment() {
|
|
24
|
+
return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
|
|
25
|
+
}
|
|
26
|
+
LoadUserFromLocalStorage() {
|
|
27
|
+
if (this.IsBrowserEnvironment()) {
|
|
28
|
+
const user = window.localStorage.getItem(this.USER_LOCAL_STORAGE_KEY);
|
|
29
|
+
if (user) {
|
|
30
|
+
this.user = JSON.parse(user);
|
|
31
|
+
this.SetUser(this.user || this.ANONYMOUS_USER);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
get User() {
|
|
36
|
+
return this.user;
|
|
16
37
|
}
|
|
17
38
|
async Login(password) {
|
|
18
39
|
const result = await this._bus.SendMessage(new LoginQuery(this.DatabaseDir, password));
|
|
19
40
|
if (!result.IsSuccess) {
|
|
20
41
|
throw new Error(`Could not login: ${result.ErrorMessage}`);
|
|
21
42
|
}
|
|
22
|
-
|
|
23
|
-
this.
|
|
24
|
-
|
|
43
|
+
this.user = result.Result;
|
|
44
|
+
this.SetUser(this.user || this.ANONYMOUS_USER);
|
|
45
|
+
this.SaveUserInLocalStorage();
|
|
46
|
+
this.onUserChange.forEach(handler => handler(this.user));
|
|
47
|
+
return this.user;
|
|
48
|
+
}
|
|
49
|
+
SaveUserInLocalStorage() {
|
|
50
|
+
if (this.IsBrowserEnvironment()) {
|
|
51
|
+
window.localStorage.setItem(this.USER_LOCAL_STORAGE_KEY, JSON.stringify(this.user));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
OnUserChange(handler) {
|
|
55
|
+
this.onUserChange.push(handler);
|
|
56
|
+
return this;
|
|
25
57
|
}
|
|
26
58
|
async Register(id, role, name, password, privileges, icon, tags) {
|
|
27
59
|
const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, password, privileges, icon, tags));
|
|
@@ -32,16 +64,14 @@ export class OnlineStorage {
|
|
|
32
64
|
return user;
|
|
33
65
|
}
|
|
34
66
|
Logout() {
|
|
35
|
-
this.
|
|
67
|
+
this.SetUser(this.ANONYMOUS_USER);
|
|
68
|
+
this.onUserChange.forEach(handler => handler(this.ANONYMOUS_USER));
|
|
36
69
|
return this;
|
|
37
70
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
IsAuthorized() {
|
|
42
|
-
return this._bus.Sender?.Id !== "Anonymous";
|
|
71
|
+
get IsAuthorized() {
|
|
72
|
+
return this.user.Id !== this.ANONYMOUS_USER.Id;
|
|
43
73
|
}
|
|
44
|
-
|
|
74
|
+
SetUser(sender) {
|
|
45
75
|
this._bus.SetSender(sender);
|
|
46
76
|
return this;
|
|
47
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tblabs/storage",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "online storage module with auth",
|
|
5
5
|
"license": "beerware",
|
|
6
6
|
"main": "index.js",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"pack": "cd ./bin && npm pack",
|
|
10
10
|
"clean": "rm ./bin -rf && rm *.tgz -rf",
|
|
11
11
|
"build": "npm run clean && tsc ./src/index.ts --declaration --outDir ./bin && cp ./package.json ./bin/package.json",
|
|
12
|
-
"publish:npm": "npm run build && npm
|
|
12
|
+
"publish:npm": "npm run build && npm run pack && cd ./bin && npm publish --access public"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/jest": "^30.0.0",
|
|
Binary file
|
package/tblabs-storage-5.0.2.tgz
DELETED
|
Binary file
|