@tblabs/storage 5.0.1 → 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/Dataset.js CHANGED
@@ -24,7 +24,7 @@ export class Dataset {
24
24
  this._db.Log(`Fetching "${id}"...`);
25
25
  const queryResult = await this._db.Send(new GetQuery(this._db.DatabaseDir, this.dataset, id));
26
26
  if (!queryResult.IsSuccess) {
27
- throw new Error(`Could not fetch "${id}" from "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
27
+ throw new Error(`Could not fetch file: ${queryResult.ErrorMessage}`);
28
28
  }
29
29
  const rawItem = queryResult.Result;
30
30
  const item = this._converter.FromRaw(rawItem);
@@ -35,7 +35,7 @@ export class Dataset {
35
35
  this._db.Log(`Searching for "${id}"...`);
36
36
  const queryResult = await this._db.Send(new FindQuery(id, this._db.DatabaseDir));
37
37
  if (!queryResult.IsSuccess) {
38
- throw new Error(`Could not find "${id}" in "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
38
+ throw new Error(`Could not find file: ${queryResult.ErrorMessage}`);
39
39
  }
40
40
  const rawItem = queryResult.Result;
41
41
  const item = this._converter.FromRaw(rawItem);
@@ -46,7 +46,7 @@ export class Dataset {
46
46
  this._db.Log(`Fetching all from "${this.DatasetPath}"...`);
47
47
  const queryResult = await this._db.Send(new ListQuery(this._db.DatabaseDir, this.dataset));
48
48
  if (!queryResult.IsSuccess) {
49
- throw new Error(`Could not fetch items from "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
49
+ throw new Error(`Could not fetch items: ${queryResult.ErrorMessage}`);
50
50
  }
51
51
  const rawItems = queryResult.Result;
52
52
  const items = rawItems.map((raw) => this._converter.FromRaw(raw));
@@ -70,7 +70,7 @@ export class Dataset {
70
70
  this._db.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
71
71
  const result = await this._db.Send(new AddCommand(this._db.DatabaseDir, this.dataset, id, item));
72
72
  if (!result.IsSuccess) {
73
- throw new Error(`Could not add "${id}" to "${this.DatasetPath}": ${result.ErrorMessage}`);
73
+ throw new Error(`Could not add file: ${result.ErrorMessage}`);
74
74
  }
75
75
  this._db.Log(`"${id}" added to "${this.DatasetPath}".`);
76
76
  }
@@ -79,7 +79,7 @@ export class Dataset {
79
79
  this._db.Log(`Updating "${id}"...`);
80
80
  const result = await this._db.Send(new UpdateCommand(this._db.DatabaseDir, this.dataset, id, item));
81
81
  if (!result.IsSuccess) {
82
- throw new Error(`Could not update "${id}" in "${this.DatasetPath}": ${result.ErrorMessage}`);
82
+ throw new Error(`Could not update file: ${result.ErrorMessage}`);
83
83
  }
84
84
  this._db.Log(`"${id}" updated in "${this.DatasetPath}".`);
85
85
  }
@@ -88,7 +88,7 @@ export class Dataset {
88
88
  this._db.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
89
89
  const result = await this._db.Send(new DeleteCommand(this._db.DatabaseDir, this.dataset, id));
90
90
  if (!result.IsSuccess) {
91
- throw new Error(`Could not delete "${id}" from "${this.DatasetPath}": ${result.ErrorMessage}`);
91
+ throw new Error(`Could not delete file: ${result.ErrorMessage}`);
92
92
  }
93
93
  this._db.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
94
94
  }
@@ -96,7 +96,7 @@ export class Dataset {
96
96
  this._db.Log(`Dropping "${this.DatasetPath}"...`);
97
97
  const result = await this._db.Send(new DropCommand(this._db.DatabaseDir, this.dataset));
98
98
  if (!result.IsSuccess) {
99
- throw new Error(`Could not drop "${this.DatasetPath}": ${result.ErrorMessage}`);
99
+ throw new Error(`Could not drop dataset: ${result.ErrorMessage}`);
100
100
  }
101
101
  this._db.Log(`"${this.DatasetPath}" dropped.`);
102
102
  }
@@ -0,0 +1,50 @@
1
+ import { ISender } from "./MessageBus/ISender";
2
+ export type IEntitiesSet = string[] | "all" | "none";
3
+ export type ICommandPrivilege = {
4
+ Datasets?: IEntitiesSet;
5
+ Files?: IEntitiesSet;
6
+ };
7
+ export interface IStorageUserPrivileges {
8
+ Add: {
9
+ Datasets: IEntitiesSet;
10
+ };
11
+ Update: {
12
+ Files: IEntitiesSet;
13
+ };
14
+ Delete: {
15
+ Files: IEntitiesSet;
16
+ };
17
+ List: {
18
+ Datasets: IEntitiesSet;
19
+ };
20
+ Get: {
21
+ Files: IEntitiesSet;
22
+ };
23
+ Drop: {
24
+ Datasets: IEntitiesSet;
25
+ };
26
+ Move: {
27
+ Datasets: IEntitiesSet;
28
+ Files: IEntitiesSet;
29
+ };
30
+ Create: {
31
+ Datasets: IEntitiesSet;
32
+ };
33
+ Exists: {
34
+ Datasets: IEntitiesSet;
35
+ };
36
+ Remove: {
37
+ Datasets: IEntitiesSet;
38
+ };
39
+ Register: boolean;
40
+ }
41
+ export interface IStorageUser extends ISender {
42
+ Id: string;
43
+ Database: string;
44
+ Name: string;
45
+ Role: string;
46
+ Icon: string;
47
+ Password: string;
48
+ Privileges: IStorageUserPrivileges;
49
+ Tags: string[];
50
+ }
@@ -0,0 +1,2 @@
1
+ ;
2
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Message } from "../MessageBus/Message";
2
+ import { dir } from "../Types/dir";
3
+ export declare class LoginQuery extends Message {
4
+ constructor(Database: dir, Password: string);
5
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from "../MessageBus/Message";
2
+ export class LoginQuery extends Message {
3
+ constructor(Database, Password) {
4
+ super("Login", { Database, Password });
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ import { IStorageUserPrivileges } from "../IStorageUser";
2
+ import { Message } from "../MessageBus/Message";
3
+ import { dir } from "../Types/dir";
4
+ export declare class RegisterCommand extends Message {
5
+ constructor(Database: dir, Id: string, Role: string, Name: string, Password: string, Privileges: IStorageUserPrivileges, Icon?: string, Tags?: string[]);
6
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from "../MessageBus/Message";
2
+ export class RegisterCommand extends Message {
3
+ constructor(Database, Id, Role, Name, Password, Privileges, Icon, Tags) {
4
+ super("Register", { Database, Id, Role, Icon, Name, Password, Privileges, Tags });
5
+ }
6
+ }
@@ -3,68 +3,28 @@ import { IMessage } from "./MessageBus/IMessage";
3
3
  import { ISender } from "./MessageBus/ISender";
4
4
  import { MessageBus } from "./MessageBus/MessageBus";
5
5
  import { MessageBusResponse } from "./MessageBus/MessageBusResponse";
6
- import { Message } from "./MessageBus/Message";
7
6
  import { Dataset } from "./Dataset";
8
- export type IEntitiesSet = string[] | "all" | "none";
9
- export interface IUser {
10
- Id: string;
11
- Name: string;
12
- Database: string;
13
- Privileges: {
14
- Add: {
15
- Datasets: IEntitiesSet;
16
- };
17
- Update: {
18
- Datasets: IEntitiesSet;
19
- Files: IEntitiesSet;
20
- };
21
- Delete: {
22
- Datasets: IEntitiesSet;
23
- Files: IEntitiesSet;
24
- };
25
- List: {
26
- Datasets: IEntitiesSet;
27
- };
28
- Get: {
29
- Files: IEntitiesSet;
30
- };
31
- Drop: {
32
- Datasets: IEntitiesSet;
33
- };
34
- Move: {
35
- Datasets: IEntitiesSet;
36
- Files: IEntitiesSet;
37
- };
38
- Backup: {
39
- Datasets: IEntitiesSet;
40
- };
41
- Find: {
42
- Files: IEntitiesSet;
43
- };
44
- Create: {
45
- Datasets: IEntitiesSet;
46
- };
47
- Exists: {
48
- Datasets: IEntitiesSet;
49
- };
50
- Remove: {
51
- Datasets: IEntitiesSet;
52
- };
53
- };
54
- }
55
- export declare class AuthQuery extends Message {
56
- constructor(Database: dir, Password: string);
57
- }
7
+ import { IStorageUser, IStorageUserPrivileges } from "./IStorageUser";
58
8
  export declare class OnlineStorage {
59
9
  private database;
10
+ private ANONYMOUS_USER;
11
+ private user;
60
12
  private logEnabled;
61
13
  private readonly _bus;
14
+ private onUserChange;
15
+ private readonly USER_LOCAL_STORAGE_KEY;
62
16
  get DatabaseDir(): dir;
63
17
  constructor(server: url, database: dir, sender?: ISender, bus?: MessageBus);
64
- Login(password: string): Promise<IUser>;
18
+ private IsBrowserEnvironment;
19
+ private LoadUserFromLocalStorage;
20
+ get User(): IStorageUser;
21
+ Login(password: string): Promise<IStorageUser>;
22
+ private SaveUserInLocalStorage;
23
+ OnUserChange(handler: (user: IStorageUser) => void): this;
24
+ Register(id: string, role: string, name: string, password: string, privileges: IStorageUserPrivileges, icon?: string, tags?: string[]): Promise<IStorageUser>;
65
25
  Logout(): this;
66
- IsAuthorized(): boolean;
67
- User(access: ISender): this;
26
+ get IsAuthorized(): boolean;
27
+ SetUser(sender: ISender): this;
68
28
  EnableLog(): this;
69
29
  DisableLog(): this;
70
30
  Log(...message: any[]): void;
package/OnlineStorage.js CHANGED
@@ -1,41 +1,78 @@
1
1
  import { MessageBus } from "./MessageBus/MessageBus";
2
2
  import { CreateCommand, ExistsCommand, RemoveCommand } from "./Messages/AddDirCommand";
3
- import { Message } from "./MessageBus/Message";
4
3
  import { MoveCommand } from "./Messages/MoveCommand";
5
- export class AuthQuery extends Message {
6
- constructor(Database, Password) {
7
- super("LoginUser", { Database, Password });
8
- }
9
- }
4
+ import { LoginQuery } from "./Messages/LoginQuery";
5
+ import { RegisterCommand } from "./Messages/RegisterCommand";
10
6
  export class OnlineStorage {
11
7
  database;
8
+ ANONYMOUS_USER = { Id: "Anonymous" };
9
+ user = this.ANONYMOUS_USER;
12
10
  logEnabled = false;
13
11
  _bus;
12
+ onUserChange = [];
13
+ USER_LOCAL_STORAGE_KEY;
14
14
  get DatabaseDir() {
15
15
  return this.database;
16
16
  }
17
- constructor(server, database, sender = { Id: "Anonymous" }, bus) {
17
+ constructor(server, database, sender = this.ANONYMOUS_USER, bus) {
18
18
  this.database = database;
19
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;
20
37
  }
21
38
  async Login(password) {
22
- const result = await this._bus.SendMessage(new AuthQuery(this.DatabaseDir, password));
39
+ const result = await this._bus.SendMessage(new LoginQuery(this.DatabaseDir, password));
23
40
  if (!result.IsSuccess) {
24
41
  throw new Error(`Could not login: ${result.ErrorMessage}`);
25
42
  }
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;
57
+ }
58
+ async Register(id, role, name, password, privileges, icon, tags) {
59
+ const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, password, privileges, icon, tags));
60
+ if (!result.IsSuccess) {
61
+ throw new Error(`Could not register: ${result.ErrorMessage}`);
62
+ }
26
63
  const user = result.Result;
27
- this.User({ Id: user?.Id ?? "Anonymous" });
28
64
  return user;
29
65
  }
30
66
  Logout() {
31
- this.User({ Id: "Anonymous" });
67
+ this.SetUser(this.ANONYMOUS_USER);
68
+ this.onUserChange.forEach(handler => handler(this.ANONYMOUS_USER));
32
69
  return this;
33
70
  }
34
- IsAuthorized() {
35
- return this._bus.Sender?.Id !== "Anonymous";
71
+ get IsAuthorized() {
72
+ return this.user.Id !== this.ANONYMOUS_USER.Id;
36
73
  }
37
- User(access) {
38
- this._bus.SetSender(access);
74
+ SetUser(sender) {
75
+ this._bus.SetSender(sender);
39
76
  return this;
40
77
  }
41
78
  EnableLog() {
@@ -61,7 +98,7 @@ export class OnlineStorage {
61
98
  this.Log(`Creating dataset "${datasetName}"...`);
62
99
  const result = await this._bus.SendMessage(new CreateCommand(this.DatabaseDir, datasetName));
63
100
  if (!result.IsSuccess) {
64
- throw new Error(`Could not add dataset "${datasetName}": ${result.ErrorMessage}`);
101
+ throw new Error(`Could not add dataset: ${result.ErrorMessage}`);
65
102
  }
66
103
  this.Log(`Dataset "${datasetName}" created.`);
67
104
  }
@@ -69,7 +106,7 @@ export class OnlineStorage {
69
106
  this.Log(`Checking if dataset "${datasetName}" exists...`);
70
107
  const result = await this._bus.SendMessage(new ExistsCommand(this.DatabaseDir, datasetName));
71
108
  if (!result.IsSuccess) {
72
- throw new Error(`Could not check if dataset "${datasetName}" exists: ${result.ErrorMessage}`);
109
+ throw new Error(`Could not check if dataset exists: ${result.ErrorMessage}`);
73
110
  }
74
111
  this.Log(`Dataset "${datasetName}" ${result.Result ? "exists" : "does not exist"}.`);
75
112
  return result.Result;
@@ -78,7 +115,7 @@ export class OnlineStorage {
78
115
  this.Log(`Removing dataset "${datasetName}"...`);
79
116
  const result = await this._bus.SendMessage(new RemoveCommand(this.DatabaseDir, datasetName));
80
117
  if (!result.IsSuccess) {
81
- throw new Error(`Could not remove dataset "${datasetName}": ${result.ErrorMessage}`);
118
+ throw new Error(`Could not remove dataset: ${result.ErrorMessage}`);
82
119
  }
83
120
  this.Log(`Dataset "${datasetName}" removed.`);
84
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.0.1",
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 login && npm run pack && cd ./bin && npm publish --access public"
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
Binary file