@nocobase/database 2.0.0-alpha.40 → 2.0.0-alpha.42
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/lib/fields/index.d.ts +1 -3
- package/lib/fields/index.js +0 -2
- package/package.json +4 -4
- package/lib/fields/encryption-field/encryption-field.d.ts +0 -20
- package/lib/fields/encryption-field/encryption-field.js +0 -110
- package/lib/fields/encryption-field/errors/EncryptionError.d.ts +0 -11
- package/lib/fields/encryption-field/errors/EncryptionError.js +0 -44
- package/lib/fields/encryption-field/index.d.ts +0 -11
- package/lib/fields/encryption-field/index.js +0 -34
- package/lib/fields/encryption-field/utils.d.ts +0 -6
- package/lib/fields/encryption-field/utils.js +0 -152
package/lib/fields/index.d.ts
CHANGED
|
@@ -27,7 +27,6 @@ import { UidFieldOptions } from './uid-field';
|
|
|
27
27
|
import { UUIDFieldOptions } from './uuid-field';
|
|
28
28
|
import { VirtualFieldOptions } from './virtual-field';
|
|
29
29
|
import { NanoidFieldOptions } from './nanoid-field';
|
|
30
|
-
import { EncryptionField } from './encryption-field';
|
|
31
30
|
import { UnixTimestampFieldOptions } from './unix-timestamp-field';
|
|
32
31
|
import { DateOnlyFieldOptions } from './date-only-field';
|
|
33
32
|
import { DatetimeNoTzFieldOptions } from './datetime-no-tz-field';
|
|
@@ -59,7 +58,6 @@ export * from './uid-field';
|
|
|
59
58
|
export * from './uuid-field';
|
|
60
59
|
export * from './virtual-field';
|
|
61
60
|
export * from './nanoid-field';
|
|
62
|
-
export * from './encryption-field';
|
|
63
61
|
export * from './unix-timestamp-field';
|
|
64
62
|
export * from './snowflake-id-field';
|
|
65
|
-
export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions |
|
|
63
|
+
export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | SnowflakeIdFieldOptions;
|
package/lib/fields/index.js
CHANGED
|
@@ -49,7 +49,6 @@ __reExport(fields_exports, require("./uid-field"), module.exports);
|
|
|
49
49
|
__reExport(fields_exports, require("./uuid-field"), module.exports);
|
|
50
50
|
__reExport(fields_exports, require("./virtual-field"), module.exports);
|
|
51
51
|
__reExport(fields_exports, require("./nanoid-field"), module.exports);
|
|
52
|
-
__reExport(fields_exports, require("./encryption-field"), module.exports);
|
|
53
52
|
__reExport(fields_exports, require("./unix-timestamp-field"), module.exports);
|
|
54
53
|
__reExport(fields_exports, require("./snowflake-id-field"), module.exports);
|
|
55
54
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -80,7 +79,6 @@ __reExport(fields_exports, require("./snowflake-id-field"), module.exports);
|
|
|
80
79
|
...require("./uuid-field"),
|
|
81
80
|
...require("./virtual-field"),
|
|
82
81
|
...require("./nanoid-field"),
|
|
83
|
-
...require("./encryption-field"),
|
|
84
82
|
...require("./unix-timestamp-field"),
|
|
85
83
|
...require("./snowflake-id-field")
|
|
86
84
|
});
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.42",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "AGPL-3.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "2.0.0-alpha.
|
|
10
|
-
"@nocobase/utils": "2.0.0-alpha.
|
|
9
|
+
"@nocobase/logger": "2.0.0-alpha.42",
|
|
10
|
+
"@nocobase/utils": "2.0.0-alpha.42",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"chalk": "^4.1.1",
|
|
13
13
|
"cron-parser": "4.4.0",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
40
40
|
"directory": "packages/database"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "9e96dd78d0c1445c8da76863e30f046be5b48d90"
|
|
43
43
|
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
import { DataTypes } from 'sequelize';
|
|
10
|
-
import { BaseColumnFieldOptions, Field } from '../field';
|
|
11
|
-
export interface EncryptionFieldOptions extends BaseColumnFieldOptions {
|
|
12
|
-
type: 'encryption';
|
|
13
|
-
hidden?: boolean;
|
|
14
|
-
}
|
|
15
|
-
export declare class EncryptionField extends Field {
|
|
16
|
-
get dataType(): DataTypes.StringDataTypeConstructor;
|
|
17
|
-
init(): void;
|
|
18
|
-
bind(): void;
|
|
19
|
-
unbind(): void;
|
|
20
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var encryption_field_exports = {};
|
|
29
|
-
__export(encryption_field_exports, {
|
|
30
|
-
EncryptionField: () => EncryptionField
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(encryption_field_exports);
|
|
33
|
-
var import_sequelize = require("sequelize");
|
|
34
|
-
var import_EncryptionError = require("./errors/EncryptionError");
|
|
35
|
-
var import_utils = require("./utils");
|
|
36
|
-
var import_field = require("../field");
|
|
37
|
-
const _EncryptionField = class _EncryptionField extends import_field.Field {
|
|
38
|
-
get dataType() {
|
|
39
|
-
return import_sequelize.DataTypes.STRING;
|
|
40
|
-
}
|
|
41
|
-
init() {
|
|
42
|
-
(0, import_utils.aesCheckKey)();
|
|
43
|
-
const { name, iv } = this.options;
|
|
44
|
-
this.writeListener = async (model) => {
|
|
45
|
-
(0, import_utils.aesCheckKey)();
|
|
46
|
-
if (!model.changed(name)) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const value = model.get(name);
|
|
50
|
-
if (value !== void 0 && value !== null) {
|
|
51
|
-
try {
|
|
52
|
-
const encrypted = await (0, import_utils.aesEncrypt)(value, iv);
|
|
53
|
-
model.set(name, encrypted);
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error(error);
|
|
56
|
-
if (error instanceof import_EncryptionError.EncryptionError) {
|
|
57
|
-
throw error;
|
|
58
|
-
} else {
|
|
59
|
-
throw new import_EncryptionError.EncryptionError("Encryption failed");
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
} else {
|
|
63
|
-
model.set(name, null);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
this.findListener = async (instances, options) => {
|
|
67
|
-
(0, import_utils.aesCheckKey)();
|
|
68
|
-
instances = Array.isArray(instances) ? instances : [instances];
|
|
69
|
-
await Promise.all(
|
|
70
|
-
instances.map(async (instance) => {
|
|
71
|
-
var _a;
|
|
72
|
-
const value = (_a = instance.get) == null ? void 0 : _a.call(instance, name);
|
|
73
|
-
if (value !== void 0 && value !== null) {
|
|
74
|
-
try {
|
|
75
|
-
instance.set(name, await (0, import_utils.aesDecrypt)(value, iv));
|
|
76
|
-
} catch (error) {
|
|
77
|
-
console.error(error);
|
|
78
|
-
if (error instanceof import_EncryptionError.EncryptionError) {
|
|
79
|
-
throw error;
|
|
80
|
-
} else {
|
|
81
|
-
throw new import_EncryptionError.EncryptionError(
|
|
82
|
-
"Decryption failed, the environment variable `ENCRYPTION_FIELD_KEY` may be incorrect"
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return instance;
|
|
88
|
-
})
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
bind() {
|
|
93
|
-
super.bind();
|
|
94
|
-
this.on("afterFind", this.findListener);
|
|
95
|
-
this.on("beforeSave", this.writeListener);
|
|
96
|
-
this.on("beforeBulkCreate", this.writeListener);
|
|
97
|
-
}
|
|
98
|
-
unbind() {
|
|
99
|
-
super.unbind();
|
|
100
|
-
this.off("afterFind", this.findListener);
|
|
101
|
-
this.off("beforeSave", this.writeListener);
|
|
102
|
-
this.off("beforeBulkCreate", this.writeListener);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
__name(_EncryptionField, "EncryptionField");
|
|
106
|
-
let EncryptionField = _EncryptionField;
|
|
107
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
-
0 && (module.exports = {
|
|
109
|
-
EncryptionField
|
|
110
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
export declare class EncryptionError extends Error {
|
|
10
|
-
constructor(message?: string, options?: ErrorOptions);
|
|
11
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var EncryptionError_exports = {};
|
|
29
|
-
__export(EncryptionError_exports, {
|
|
30
|
-
EncryptionError: () => EncryptionError
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(EncryptionError_exports);
|
|
33
|
-
const _EncryptionError = class _EncryptionError extends Error {
|
|
34
|
-
constructor(message, options) {
|
|
35
|
-
super(message, options);
|
|
36
|
-
this.name = "EncryptionError";
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
__name(_EncryptionError, "EncryptionError");
|
|
40
|
-
let EncryptionError = _EncryptionError;
|
|
41
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
-
0 && (module.exports = {
|
|
43
|
-
EncryptionError
|
|
44
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
export * from './encryption-field';
|
|
10
|
-
export * from './utils';
|
|
11
|
-
export * from './errors/EncryptionError';
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __copyProps = (to, from, except, desc) => {
|
|
15
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
-
for (let key of __getOwnPropNames(from))
|
|
17
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var encryption_field_exports = {};
|
|
25
|
-
module.exports = __toCommonJS(encryption_field_exports);
|
|
26
|
-
__reExport(encryption_field_exports, require("./encryption-field"), module.exports);
|
|
27
|
-
__reExport(encryption_field_exports, require("./utils"), module.exports);
|
|
28
|
-
__reExport(encryption_field_exports, require("./errors/EncryptionError"), module.exports);
|
|
29
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
-
0 && (module.exports = {
|
|
31
|
-
...require("./encryption-field"),
|
|
32
|
-
...require("./utils"),
|
|
33
|
-
...require("./errors/EncryptionError")
|
|
34
|
-
});
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare function aesEncrypt(text: string, ivString?: string): Promise<unknown>;
|
|
2
|
-
export declare function aesDecrypt(encrypted: string, ivString?: string): Promise<unknown>;
|
|
3
|
-
export declare function aesEncryptSync(text: string, ivString?: string): string;
|
|
4
|
-
export declare function aseDecryptSync(encrypted: string, ivString?: string): string;
|
|
5
|
-
export declare function aesCheckKey(): void;
|
|
6
|
-
export declare function checkValueAndIv(type: 'Decrypt' | 'Encrypt', value: string, iv: string): void;
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __create = Object.create;
|
|
11
|
-
var __defProp = Object.defineProperty;
|
|
12
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
-
var __export = (target, all) => {
|
|
18
|
-
for (var name in all)
|
|
19
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
-
};
|
|
21
|
-
var __copyProps = (to, from, except, desc) => {
|
|
22
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
-
for (let key2 of __getOwnPropNames(from))
|
|
24
|
-
if (!__hasOwnProp.call(to, key2) && key2 !== except)
|
|
25
|
-
__defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
|
|
26
|
-
}
|
|
27
|
-
return to;
|
|
28
|
-
};
|
|
29
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
-
mod
|
|
36
|
-
));
|
|
37
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
-
var utils_exports = {};
|
|
39
|
-
__export(utils_exports, {
|
|
40
|
-
aesCheckKey: () => aesCheckKey,
|
|
41
|
-
aesDecrypt: () => aesDecrypt,
|
|
42
|
-
aesEncrypt: () => aesEncrypt,
|
|
43
|
-
aesEncryptSync: () => aesEncryptSync,
|
|
44
|
-
aseDecryptSync: () => aseDecryptSync,
|
|
45
|
-
checkValueAndIv: () => checkValueAndIv
|
|
46
|
-
});
|
|
47
|
-
module.exports = __toCommonJS(utils_exports);
|
|
48
|
-
var import_crypto = __toESM(require("crypto"));
|
|
49
|
-
var import_EncryptionError = require("./errors/EncryptionError");
|
|
50
|
-
const algorithm = "aes-256-cbc";
|
|
51
|
-
const keyString = process.env.ENCRYPTION_FIELD_KEY || "";
|
|
52
|
-
const defaultIvString = process.env.ENCRYPTION_FIELD_IV || "Vc53-4G(rTi0vg@a";
|
|
53
|
-
const key = Buffer.from(keyString, "utf8");
|
|
54
|
-
function aesEncrypt(text, ivString = defaultIvString) {
|
|
55
|
-
checkValueAndIv("Encrypt", text, ivString);
|
|
56
|
-
return new Promise((resolve, reject) => {
|
|
57
|
-
const iv = Buffer.from(ivString, "utf8");
|
|
58
|
-
const cipher = import_crypto.default.createCipheriv(algorithm, key, iv);
|
|
59
|
-
let encrypted = "";
|
|
60
|
-
cipher.setEncoding("hex");
|
|
61
|
-
cipher.on("data", (chunk) => {
|
|
62
|
-
encrypted += chunk;
|
|
63
|
-
});
|
|
64
|
-
cipher.on("end", () => {
|
|
65
|
-
resolve(encrypted);
|
|
66
|
-
});
|
|
67
|
-
cipher.on("error", (err) => {
|
|
68
|
-
reject(err);
|
|
69
|
-
});
|
|
70
|
-
cipher.write(text);
|
|
71
|
-
cipher.end();
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
__name(aesEncrypt, "aesEncrypt");
|
|
75
|
-
function aesDecrypt(encrypted, ivString = defaultIvString) {
|
|
76
|
-
checkValueAndIv("Decrypt", encrypted, ivString);
|
|
77
|
-
return new Promise((resolve, reject) => {
|
|
78
|
-
const iv = Buffer.from(ivString, "utf8");
|
|
79
|
-
const decipher = import_crypto.default.createDecipheriv(algorithm, key, iv);
|
|
80
|
-
let decrypted = "";
|
|
81
|
-
decipher.setEncoding("utf8");
|
|
82
|
-
decipher.on("data", (chunk) => {
|
|
83
|
-
decrypted += chunk;
|
|
84
|
-
});
|
|
85
|
-
decipher.on("end", () => {
|
|
86
|
-
resolve(decrypted);
|
|
87
|
-
});
|
|
88
|
-
decipher.on("error", (err) => {
|
|
89
|
-
reject(err);
|
|
90
|
-
});
|
|
91
|
-
decipher.write(encrypted, "hex");
|
|
92
|
-
decipher.end();
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
__name(aesDecrypt, "aesDecrypt");
|
|
96
|
-
function aesEncryptSync(text, ivString = defaultIvString) {
|
|
97
|
-
checkValueAndIv("Encrypt", text, ivString);
|
|
98
|
-
const iv = Buffer.from(ivString, "utf8");
|
|
99
|
-
const cipher = import_crypto.default.createCipheriv(algorithm, key, iv);
|
|
100
|
-
let encrypted = cipher.update(text, "utf8", "hex");
|
|
101
|
-
encrypted += cipher.final("hex");
|
|
102
|
-
return encrypted;
|
|
103
|
-
}
|
|
104
|
-
__name(aesEncryptSync, "aesEncryptSync");
|
|
105
|
-
function aseDecryptSync(encrypted, ivString = defaultIvString) {
|
|
106
|
-
checkValueAndIv("Decrypt", encrypted, ivString);
|
|
107
|
-
const iv = Buffer.from(ivString, "utf8");
|
|
108
|
-
const decipher = import_crypto.default.createDecipheriv(algorithm, key, iv);
|
|
109
|
-
let decrypted = decipher.update(encrypted, "hex", "utf8");
|
|
110
|
-
decrypted += decipher.final("utf8");
|
|
111
|
-
return decrypted;
|
|
112
|
-
}
|
|
113
|
-
__name(aseDecryptSync, "aseDecryptSync");
|
|
114
|
-
function aesCheckKey() {
|
|
115
|
-
if (!keyString) {
|
|
116
|
-
throw new import_EncryptionError.EncryptionError("The environment variable `ENCRYPTION_FIELD_KEY` is required, please set it");
|
|
117
|
-
}
|
|
118
|
-
if (typeof keyString !== "string") {
|
|
119
|
-
throw new import_EncryptionError.EncryptionError("The environment variable `ENCRYPTION_FIELD_KEY` must be a string");
|
|
120
|
-
}
|
|
121
|
-
if (keyString.length !== 32) {
|
|
122
|
-
throw new import_EncryptionError.EncryptionError("The environment variable `ENCRYPTION_FIELD_KEY` must be a 32-character string");
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
__name(aesCheckKey, "aesCheckKey");
|
|
126
|
-
function checkValueAndIv(type, value, iv) {
|
|
127
|
-
const msg = `${type} Failed: `;
|
|
128
|
-
if (typeof value !== "string") {
|
|
129
|
-
throw new import_EncryptionError.EncryptionError(msg + "The value must be a string, but got " + typeof value);
|
|
130
|
-
}
|
|
131
|
-
if (type === "Decrypt") {
|
|
132
|
-
if (value.length % 2 !== 0) {
|
|
133
|
-
throw new import_EncryptionError.EncryptionError(msg + `The encrypted value is invalid, not a hex string. The value is "${value}"`);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (typeof iv !== "string") {
|
|
137
|
-
throw new import_EncryptionError.EncryptionError(msg + "The `iv` must be a string, but got " + typeof iv);
|
|
138
|
-
}
|
|
139
|
-
if (iv.length !== 16) {
|
|
140
|
-
throw new import_EncryptionError.EncryptionError(msg + "The `iv` must be a 16-character string");
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
__name(checkValueAndIv, "checkValueAndIv");
|
|
144
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
145
|
-
0 && (module.exports = {
|
|
146
|
-
aesCheckKey,
|
|
147
|
-
aesDecrypt,
|
|
148
|
-
aesEncrypt,
|
|
149
|
-
aesEncryptSync,
|
|
150
|
-
aseDecryptSync,
|
|
151
|
-
checkValueAndIv
|
|
152
|
-
});
|