protoobject 1.1.30 → 2.0.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/README.md +142 -2
- package/lib/cjs/browser.js +28 -0
- package/lib/cjs/browser.js.map +1 -0
- package/lib/cjs/classes/proto-object-crypto.js +133 -0
- package/lib/cjs/classes/proto-object-crypto.js.map +1 -0
- package/lib/cjs/classes/proto-object-fs.js +395 -0
- package/lib/cjs/classes/proto-object-fs.js.map +1 -0
- package/lib/cjs/classes/proto-object-sqlite.js +331 -0
- package/lib/cjs/classes/proto-object-sqlite.js.map +1 -0
- package/lib/cjs/classes/proto-object-stream.js +148 -0
- package/lib/cjs/classes/proto-object-stream.js.map +1 -0
- package/lib/cjs/classes/proto-object-tcp.js +322 -0
- package/lib/cjs/classes/proto-object-tcp.js.map +1 -0
- package/lib/cjs/classes/proto-object.js.map +1 -0
- package/lib/cjs/decorators/static-implements.js.map +1 -0
- package/lib/cjs/index.js +41 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/package.json +1 -0
- package/lib/{types → cjs/types}/any-object.js.map +1 -1
- package/lib/cjs/types/collection-transformer.js.map +1 -0
- package/lib/cjs/types/dynamic-methods.js.map +1 -0
- package/lib/cjs/types/record-transformer.js.map +1 -0
- package/lib/cjs/types/static-methods.js.map +1 -0
- package/lib/cjs/types/unknown-object.js.map +1 -0
- package/lib/cjs/types/validator-function.js.map +1 -0
- package/lib/cjs/utils/protoobject-browser-storage.js +672 -0
- package/lib/cjs/utils/protoobject-browser-storage.js.map +1 -0
- package/lib/cjs/utils/protoobject-factory.js.map +1 -0
- package/lib/cjs/utils/protoobject-localstorage.js +188 -0
- package/lib/cjs/utils/protoobject-localstorage.js.map +1 -0
- package/lib/esm/browser.js +20 -0
- package/lib/esm/browser.js.map +1 -0
- package/lib/esm/classes/proto-object-crypto.js +129 -0
- package/lib/esm/classes/proto-object-crypto.js.map +1 -0
- package/lib/esm/classes/proto-object-fs.js +391 -0
- package/lib/esm/classes/proto-object-fs.js.map +1 -0
- package/lib/esm/classes/proto-object-sqlite.js +328 -0
- package/lib/esm/classes/proto-object-sqlite.js.map +1 -0
- package/lib/esm/classes/proto-object-stream.js +144 -0
- package/lib/esm/classes/proto-object-stream.js.map +1 -0
- package/lib/esm/classes/proto-object-tcp.js +317 -0
- package/lib/esm/classes/proto-object-tcp.js.map +1 -0
- package/lib/esm/classes/proto-object.js +264 -0
- package/lib/esm/classes/proto-object.js.map +1 -0
- package/lib/esm/decorators/static-implements.js +12 -0
- package/lib/esm/decorators/static-implements.js.map +1 -0
- package/lib/esm/index.js +22 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/types/any-object.js +2 -0
- package/lib/esm/types/any-object.js.map +1 -0
- package/lib/esm/types/collection-transformer.js +3 -0
- package/lib/esm/types/collection-transformer.js.map +1 -0
- package/lib/esm/types/dynamic-methods.js +3 -0
- package/lib/esm/types/dynamic-methods.js.map +1 -0
- package/lib/esm/types/record-transformer.js +3 -0
- package/lib/esm/types/record-transformer.js.map +1 -0
- package/lib/esm/types/static-methods.js +4 -0
- package/lib/esm/types/static-methods.js.map +1 -0
- package/lib/esm/types/unknown-object.js +2 -0
- package/lib/esm/types/unknown-object.js.map +1 -0
- package/lib/esm/types/validator-function.js +3 -0
- package/lib/esm/types/validator-function.js.map +1 -0
- package/lib/esm/utils/protoobject-browser-storage.js +668 -0
- package/lib/esm/utils/protoobject-browser-storage.js.map +1 -0
- package/lib/esm/utils/protoobject-factory.js +32 -0
- package/lib/esm/utils/protoobject-factory.js.map +1 -0
- package/lib/esm/utils/protoobject-localstorage.js +184 -0
- package/lib/esm/utils/protoobject-localstorage.js.map +1 -0
- package/lib/types/browser.d.ts +18 -0
- package/lib/types/browser.d.ts.map +1 -0
- package/lib/types/classes/proto-object-crypto.d.ts +99 -0
- package/lib/types/classes/proto-object-crypto.d.ts.map +1 -0
- package/lib/types/classes/proto-object-fs.d.ts +107 -0
- package/lib/types/classes/proto-object-fs.d.ts.map +1 -0
- package/lib/types/classes/proto-object-sqlite.d.ts +98 -0
- package/lib/types/classes/proto-object-sqlite.d.ts.map +1 -0
- package/lib/types/classes/proto-object-stream.d.ts +38 -0
- package/lib/types/classes/proto-object-stream.d.ts.map +1 -0
- package/lib/types/classes/proto-object-tcp.d.ts +86 -0
- package/lib/types/classes/proto-object-tcp.d.ts.map +1 -0
- package/lib/{classes → types/classes}/proto-object.d.ts +1 -0
- package/lib/types/classes/proto-object.d.ts.map +1 -0
- package/lib/{decorators → types/decorators}/static-implements.d.ts +1 -0
- package/lib/types/decorators/static-implements.d.ts.map +1 -0
- package/lib/types/index.d.ts +24 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/{any-object.d.ts → types/any-object.d.ts} +1 -0
- package/lib/types/types/any-object.d.ts.map +1 -0
- package/lib/types/{collection-transformer.d.ts → types/collection-transformer.d.ts} +1 -0
- package/lib/types/types/collection-transformer.d.ts.map +1 -0
- package/lib/types/{dynamic-methods.d.ts → types/dynamic-methods.d.ts} +1 -0
- package/lib/types/types/dynamic-methods.d.ts.map +1 -0
- package/lib/types/{record-transformer.d.ts → types/record-transformer.d.ts} +1 -0
- package/lib/types/types/record-transformer.d.ts.map +1 -0
- package/lib/types/{static-methods.d.ts → types/static-methods.d.ts} +1 -0
- package/lib/types/types/static-methods.d.ts.map +1 -0
- package/lib/types/{unknown-object.d.ts → types/unknown-object.d.ts} +1 -0
- package/lib/types/types/unknown-object.d.ts.map +1 -0
- package/lib/types/{validator-function.d.ts → types/validator-function.d.ts} +1 -0
- package/lib/types/types/validator-function.d.ts.map +1 -0
- package/lib/types/utils/protoobject-browser-storage.d.ts +118 -0
- package/lib/types/utils/protoobject-browser-storage.d.ts.map +1 -0
- package/lib/{utils → types/utils}/protoobject-factory.d.ts +1 -0
- package/lib/types/utils/protoobject-factory.d.ts.map +1 -0
- package/lib/types/utils/protoobject-localstorage.d.ts +69 -0
- package/lib/types/utils/protoobject-localstorage.d.ts.map +1 -0
- package/package.json +56 -5
- package/CHANGELOG.md +0 -191
- package/FUNDING.yml +0 -2
- package/ignoreUpdatesModules.json +0 -4
- package/lib/classes/proto-object.js.map +0 -1
- package/lib/decorators/static-implements.js.map +0 -1
- package/lib/index.d.ts +0 -14
- package/lib/index.js +0 -18
- package/lib/index.js.map +0 -1
- package/lib/types/collection-transformer.js.map +0 -1
- package/lib/types/dynamic-methods.js.map +0 -1
- package/lib/types/record-transformer.js.map +0 -1
- package/lib/types/static-methods.js.map +0 -1
- package/lib/types/unknown-object.js.map +0 -1
- package/lib/types/validator-function.js.map +0 -1
- package/lib/utils/protoobject-factory.js.map +0 -1
- /package/lib/{classes → cjs/classes}/proto-object.js +0 -0
- /package/lib/{decorators → cjs/decorators}/static-implements.js +0 -0
- /package/lib/{types → cjs/types}/any-object.js +0 -0
- /package/lib/{types → cjs/types}/collection-transformer.js +0 -0
- /package/lib/{types → cjs/types}/dynamic-methods.js +0 -0
- /package/lib/{types → cjs/types}/record-transformer.js +0 -0
- /package/lib/{types → cjs/types}/static-methods.js +0 -0
- /package/lib/{types → cjs/types}/unknown-object.js +0 -0
- /package/lib/{types → cjs/types}/validator-function.js +0 -0
- /package/lib/{utils → cjs/utils}/protoobject-factory.js +0 -0
package/README.md
CHANGED
|
@@ -20,6 +20,107 @@ Inspired by gRPC and Firebase.
|
|
|
20
20
|
npm i protoobject --save
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
## MODULE FORMATS
|
|
24
|
+
|
|
25
|
+
This library supports both **CommonJS** and **ESM** (ES Modules) formats:
|
|
26
|
+
|
|
27
|
+
### CommonJS
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
const { ProtoObject, StaticImplements, protoObjectFactory } = require('protoobject');
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### ES Modules (ESM)
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
import { ProtoObject, StaticImplements, protoObjectFactory } from 'protoobject';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The library automatically provides the correct format based on your project configuration. No additional setup required!
|
|
40
|
+
|
|
41
|
+
## 🌐 BROWSER COMPATIBILITY
|
|
42
|
+
|
|
43
|
+
ProtoObject works in **both Node.js and Browser environments**:
|
|
44
|
+
|
|
45
|
+
### Browser Usage
|
|
46
|
+
|
|
47
|
+
For browser compatibility, use the browser-specific import:
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
// Browser-compatible imports only (excludes Node.js-specific modules)
|
|
51
|
+
import {
|
|
52
|
+
ProtoObject,
|
|
53
|
+
StaticImplements,
|
|
54
|
+
protoObjectFactory
|
|
55
|
+
} from 'protoobject/browser';
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Node.js Usage
|
|
59
|
+
|
|
60
|
+
For Node.js, use the full import with all modules:
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
// Full import with all modules including Node.js-specific
|
|
64
|
+
import {
|
|
65
|
+
ProtoObject,
|
|
66
|
+
ProtoObjectSQLite,
|
|
67
|
+
ProtoObjectTCP,
|
|
68
|
+
ProtoObjectCrypto,
|
|
69
|
+
ProtoObjectFS,
|
|
70
|
+
ProtoObjectStream
|
|
71
|
+
} from 'protoobject';
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Browser Support
|
|
75
|
+
|
|
76
|
+
- ✅ Chrome 80+
|
|
77
|
+
- ✅ Firefox 75+
|
|
78
|
+
- ✅ Safari 13+
|
|
79
|
+
- ✅ Edge 80+
|
|
80
|
+
- ✅ ES6+ and ES Modules support
|
|
81
|
+
|
|
82
|
+
### What Works in Browser
|
|
83
|
+
|
|
84
|
+
- ✅ Core ProtoObject functionality
|
|
85
|
+
- ✅ JSON serialization/deserialization
|
|
86
|
+
- ✅ localStorage/sessionStorage integration
|
|
87
|
+
- ✅ Fetch API integration
|
|
88
|
+
- ✅ IndexedDB integration
|
|
89
|
+
- ✅ WebSocket integration
|
|
90
|
+
|
|
91
|
+
### What's Node.js Only
|
|
92
|
+
|
|
93
|
+
- ❌ ProtoObjectSQLite (use IndexedDB in browser)
|
|
94
|
+
- ❌ ProtoObjectTCP (use WebSockets/Fetch in browser)
|
|
95
|
+
- ❌ ProtoObjectCrypto (use Web Crypto API in browser)
|
|
96
|
+
- ❌ ProtoObjectFS (use localStorage/IndexedDB/Fetch in browser)
|
|
97
|
+
- ❌ ProtoObjectStream (use browser streams or fetch in browser)
|
|
98
|
+
|
|
99
|
+
## 📚 EXAMPLES
|
|
100
|
+
|
|
101
|
+
See the [`examples/`](./examples/) directory for comprehensive usage examples:
|
|
102
|
+
|
|
103
|
+
- **[Browser Examples](./examples/browser/)** - Browser compatibility with interactive demo
|
|
104
|
+
- **[JavaScript Examples](./examples/javascript/)** - Plain JavaScript usage with CommonJS
|
|
105
|
+
- **[TypeScript Examples](./examples/typescript/)** - Type-safe usage with decorators and interfaces
|
|
106
|
+
- **[SQL Database Examples](./examples/sql-database/)** - Database integration patterns
|
|
107
|
+
|
|
108
|
+
## 🧪 TESTING
|
|
109
|
+
|
|
110
|
+
Run all tests:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
npm test
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Run specific test suites:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
npm run test:ts # TypeScript tests
|
|
120
|
+
npm run test:js # JavaScript tests
|
|
121
|
+
npm run test:sql # SQL database tests
|
|
122
|
+
```
|
|
123
|
+
|
|
23
124
|
## DOCS
|
|
24
125
|
|
|
25
126
|
### The main methods of the ProtoObject class
|
|
@@ -453,6 +554,45 @@ export class ApplicationRecord extends BaseRecord<ApplicationRecord> {
|
|
|
453
554
|
}
|
|
454
555
|
```
|
|
455
556
|
|
|
456
|
-
##
|
|
557
|
+
## 🤝 Contributing
|
|
558
|
+
|
|
559
|
+
1. Fork the repository
|
|
560
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
561
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
562
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
563
|
+
5. Open a Pull Request
|
|
564
|
+
|
|
565
|
+
### Development Setup
|
|
566
|
+
|
|
567
|
+
```bash
|
|
568
|
+
git clone https://github.com/siarheidudko/protoobject.git
|
|
569
|
+
cd protoobject
|
|
570
|
+
npm install
|
|
571
|
+
npm run build
|
|
572
|
+
npm test
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
## 📄 License
|
|
576
|
+
|
|
577
|
+
MIT License - see [LICENSE](./LICENSE) file for details.
|
|
578
|
+
|
|
579
|
+
## 🆘 Support
|
|
580
|
+
|
|
581
|
+
- 📝 **Issues**: [GitHub Issues](https://github.com/siarheidudko/protoobject/issues)
|
|
582
|
+
- 💬 **Discussions**: [GitHub Discussions](https://github.com/siarheidudko/protoobject/discussions)
|
|
583
|
+
- 📧 **Email**: [siarhei@dudko.dev](mailto:siarhei@dudko.dev)
|
|
584
|
+
|
|
585
|
+
## 💝 Support This Project
|
|
586
|
+
|
|
587
|
+
If Redux Cluster helps you build amazing applications, consider supporting its development:
|
|
588
|
+
|
|
589
|
+
- ☕ **[Buy me a coffee](https://www.buymeacoffee.com/dudko.dev)**
|
|
590
|
+
- 💳 **[PayPal](https://paypal.me/dudkodev)**
|
|
591
|
+
- 🎯 **[Patreon](https://patreon.com/dudko_dev)**
|
|
592
|
+
- 🌐 **[More options](http://dudko.dev/donate)**
|
|
593
|
+
|
|
594
|
+
Your support helps maintain and improve Redux Cluster for the entire community!
|
|
595
|
+
|
|
596
|
+
---
|
|
457
597
|
|
|
458
|
-
|
|
598
|
+
**Made with ❤️ by [Siarhei Dudko](https://github.com/siarheidudko)**
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Browser-compatible exports of protoobject
|
|
4
|
+
* @module protoobject/browser
|
|
5
|
+
* @author Siarhei Dudko <siarhei@dudko.dev>
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ProtoObjectBrowserStorage = exports.ProtoObjectLocalStorage = exports.protoObjectFactory = exports.StaticImplements = exports.ProtoObject = void 0;
|
|
9
|
+
// Core classes (browser-compatible)
|
|
10
|
+
var proto_object_js_1 = require("./classes/proto-object.js");
|
|
11
|
+
Object.defineProperty(exports, "ProtoObject", { enumerable: true, get: function () { return proto_object_js_1.ProtoObject; } });
|
|
12
|
+
// Decorators (browser-compatible)
|
|
13
|
+
var static_implements_js_1 = require("./decorators/static-implements.js");
|
|
14
|
+
Object.defineProperty(exports, "StaticImplements", { enumerable: true, get: function () { return static_implements_js_1.StaticImplements; } });
|
|
15
|
+
// Utilities (browser-compatible)
|
|
16
|
+
var protoobject_factory_js_1 = require("./utils/protoobject-factory.js");
|
|
17
|
+
Object.defineProperty(exports, "protoObjectFactory", { enumerable: true, get: function () { return protoobject_factory_js_1.protoObjectFactory; } });
|
|
18
|
+
var protoobject_localstorage_js_1 = require("./utils/protoobject-localstorage.js");
|
|
19
|
+
Object.defineProperty(exports, "ProtoObjectLocalStorage", { enumerable: true, get: function () { return protoobject_localstorage_js_1.ProtoObjectLocalStorage; } });
|
|
20
|
+
var protoobject_browser_storage_js_1 = require("./utils/protoobject-browser-storage.js");
|
|
21
|
+
Object.defineProperty(exports, "ProtoObjectBrowserStorage", { enumerable: true, get: function () { return protoobject_browser_storage_js_1.ProtoObjectBrowserStorage; } });
|
|
22
|
+
// Note: The following modules are Node.js-specific and not available in browser:
|
|
23
|
+
// - ProtoObjectSQLite (requires node:sqlite and node:crypto)
|
|
24
|
+
// - ProtoObjectTCP (requires node:net)
|
|
25
|
+
// - ProtoObjectCrypto (requires node:crypto)
|
|
26
|
+
// - ProtoObjectFS (requires node:fs, node:path, node:events)
|
|
27
|
+
// - ProtoObjectStream (requires node:stream via @sergdudko/objectstream)
|
|
28
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,oCAAoC;AACpC,6DAAwD;AAA/C,8GAAA,WAAW,OAAA;AAEpB,kCAAkC;AAClC,0EAAqE;AAA5D,wHAAA,gBAAgB,OAAA;AAWzB,iCAAiC;AACjC,yEAAoE;AAA3D,4HAAA,kBAAkB,OAAA;AAC3B,mFAA8E;AAArE,sIAAA,uBAAuB,OAAA;AAChC,yFAIgD;AAH9C,2IAAA,yBAAyB,OAAA;AAK3B,iFAAiF;AACjF,6DAA6D;AAC7D,uCAAuC;AACvC,6CAA6C;AAC7C,6DAA6D;AAC7D,yEAAyE"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ProtoObjectCrypto - Cryptographic utilities for ProtoObject
|
|
4
|
+
* @description Provides encryption, hashing, and digital signature capabilities
|
|
5
|
+
* @author Siarhei Dudko <siarhei@dudko.dev>
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ProtoObjectCrypto = exports.HashAlgorithm = exports.EncryptionAlgorithm = void 0;
|
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
|
10
|
+
const proto_object_js_1 = require("./proto-object.js");
|
|
11
|
+
/* eslint-disable no-unused-vars */
|
|
12
|
+
/**
|
|
13
|
+
* Encryption algorithms supported
|
|
14
|
+
*/
|
|
15
|
+
var EncryptionAlgorithm;
|
|
16
|
+
(function (EncryptionAlgorithm) {
|
|
17
|
+
EncryptionAlgorithm["AES256"] = "aes-256-cbc";
|
|
18
|
+
EncryptionAlgorithm["AES192"] = "aes-192-cbc";
|
|
19
|
+
EncryptionAlgorithm["AES128"] = "aes-128-cbc";
|
|
20
|
+
})(EncryptionAlgorithm || (exports.EncryptionAlgorithm = EncryptionAlgorithm = {}));
|
|
21
|
+
/**
|
|
22
|
+
* Hash algorithms supported
|
|
23
|
+
*/
|
|
24
|
+
var HashAlgorithm;
|
|
25
|
+
(function (HashAlgorithm) {
|
|
26
|
+
HashAlgorithm["SHA256"] = "sha256";
|
|
27
|
+
HashAlgorithm["SHA512"] = "sha512";
|
|
28
|
+
HashAlgorithm["MD5"] = "md5";
|
|
29
|
+
})(HashAlgorithm || (exports.HashAlgorithm = HashAlgorithm = {}));
|
|
30
|
+
/* eslint-enable no-unused-vars */
|
|
31
|
+
/**
|
|
32
|
+
* Base class for Crypto-enabled ProtoObjects
|
|
33
|
+
*/
|
|
34
|
+
class ProtoObjectCrypto extends proto_object_js_1.ProtoObject {
|
|
35
|
+
constructor(data) {
|
|
36
|
+
super(data);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Generate hash of the object
|
|
40
|
+
* @param algorithm - Hash algorithm to use
|
|
41
|
+
* @returns Hash string
|
|
42
|
+
*/
|
|
43
|
+
generateHash(algorithm = HashAlgorithm.SHA256) {
|
|
44
|
+
const data = JSON.stringify(this.toJSON());
|
|
45
|
+
const hash = (0, node_crypto_1.createHash)(algorithm).update(data).digest("hex");
|
|
46
|
+
this._hash = hash;
|
|
47
|
+
return hash;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get stored hash
|
|
51
|
+
* @returns Hash string or undefined
|
|
52
|
+
*/
|
|
53
|
+
getHash() {
|
|
54
|
+
return this._hash;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Encrypt object data
|
|
58
|
+
* @param password - Encryption password
|
|
59
|
+
* @param algorithm - Encryption algorithm
|
|
60
|
+
* @returns Encrypted data structure
|
|
61
|
+
*/
|
|
62
|
+
encrypt(password, algorithm = EncryptionAlgorithm.AES256) {
|
|
63
|
+
const data = JSON.stringify(this.toJSON());
|
|
64
|
+
const salt = (0, node_crypto_1.randomBytes)(16);
|
|
65
|
+
const iv = (0, node_crypto_1.randomBytes)(16);
|
|
66
|
+
// Derive key from password and salt
|
|
67
|
+
const key = (0, node_crypto_1.createHash)("sha256")
|
|
68
|
+
.update(password + salt.toString("hex"))
|
|
69
|
+
.digest();
|
|
70
|
+
const cipher = (0, node_crypto_1.createCipheriv)(algorithm, key, iv);
|
|
71
|
+
let encrypted = cipher.update(data, "utf8", "hex");
|
|
72
|
+
encrypted += cipher.final("hex");
|
|
73
|
+
return {
|
|
74
|
+
algorithm,
|
|
75
|
+
data: encrypted,
|
|
76
|
+
iv: iv.toString("hex"),
|
|
77
|
+
salt: salt.toString("hex"),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Decrypt encrypted data
|
|
82
|
+
* @param encryptedData - Encrypted data structure
|
|
83
|
+
* @param password - Decryption password
|
|
84
|
+
* @returns Decrypted object data
|
|
85
|
+
*/
|
|
86
|
+
static decrypt(encryptedData, password) {
|
|
87
|
+
const { algorithm, data, iv, salt } = encryptedData;
|
|
88
|
+
// Derive key from password and salt
|
|
89
|
+
const key = (0, node_crypto_1.createHash)("sha256")
|
|
90
|
+
.update(password + salt)
|
|
91
|
+
.digest();
|
|
92
|
+
const decipher = (0, node_crypto_1.createDecipheriv)(algorithm, key, Buffer.from(iv, "hex"));
|
|
93
|
+
let decrypted = decipher.update(data, "hex", "utf8");
|
|
94
|
+
decrypted += decipher.final("utf8");
|
|
95
|
+
return JSON.parse(decrypted);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Generate random salt for encryption
|
|
99
|
+
* @param length - Salt length in bytes
|
|
100
|
+
* @returns Hex string salt
|
|
101
|
+
*/
|
|
102
|
+
static generateSalt(length = 16) {
|
|
103
|
+
return (0, node_crypto_1.randomBytes)(length).toString("hex");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Verify hash of the object
|
|
107
|
+
* @param expectedHash - Expected hash value
|
|
108
|
+
* @param algorithm - Hash algorithm used
|
|
109
|
+
* @returns True if hash matches
|
|
110
|
+
*/
|
|
111
|
+
verifyHash(expectedHash, algorithm = HashAlgorithm.SHA256) {
|
|
112
|
+
const currentHash = this.generateHash(algorithm);
|
|
113
|
+
return currentHash === expectedHash;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create encrypted backup of the object
|
|
117
|
+
* @param password - Encryption password
|
|
118
|
+
* @param algorithm - Encryption algorithm
|
|
119
|
+
* @returns Encrypted backup data
|
|
120
|
+
*/
|
|
121
|
+
createEncryptedBackup(password, algorithm = EncryptionAlgorithm.AES256) {
|
|
122
|
+
const hash = this.generateHash();
|
|
123
|
+
const encrypted = this.encrypt(password, algorithm);
|
|
124
|
+
return {
|
|
125
|
+
hash,
|
|
126
|
+
encrypted,
|
|
127
|
+
timestamp: Date.now(),
|
|
128
|
+
algorithm,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.ProtoObjectCrypto = ProtoObjectCrypto;
|
|
133
|
+
//# sourceMappingURL=proto-object-crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proto-object-crypto.js","sourceRoot":"","sources":["../../../src/classes/proto-object-crypto.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,6CAKqB;AACrB,uDAAgD;AAGhD,mCAAmC;AACnC;;GAEG;AACH,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC7B,6CAAsB,CAAA;IACtB,6CAAsB,CAAA;IACtB,6CAAsB,CAAA;AACxB,CAAC,EAJW,mBAAmB,mCAAnB,mBAAmB,QAI9B;AAED;;GAEG;AACH,IAAY,aAIX;AAJD,WAAY,aAAa;IACvB,kCAAiB,CAAA;IACjB,kCAAiB,CAAA;IACjB,4BAAW,CAAA;AACb,CAAC,EAJW,aAAa,6BAAb,aAAa,QAIxB;AAuBD,kCAAkC;AAElC;;GAEG;AACH,MAAa,iBAEX,SAAQ,6BAAc;IAGtB,YAAY,IAAiB;QAC3B,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAED;;;;OAIG;IACH,YAA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