@pallasoft/ps-utilities 2.16.0 → 2.17.1
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/dist/common/constants/index.d.ts +1 -0
- package/dist/common/constants/index.js +1 -0
- package/dist/common/constants/portable-text-config.d.ts +19 -0
- package/dist/common/constants/portable-text-config.js +20 -0
- package/dist/common/crypto-service.d.ts +1 -0
- package/dist/common/crypto-service.js +10 -0
- package/dist/common/envelope-crypto-service.js +2 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js +1 -0
- package/dist/common/portable-text-service.d.ts +9 -0
- package/dist/common/portable-text-service.js +110 -0
- package/package.json +3 -1
|
@@ -32,3 +32,4 @@ __exportStar(require("./http-header"), exports);
|
|
|
32
32
|
__exportStar(require("./http-method"), exports);
|
|
33
33
|
__exportStar(require("./relative-time-config"), exports);
|
|
34
34
|
__exportStar(require("./request-client-config"), exports);
|
|
35
|
+
__exportStar(require("./portable-text-config"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const PortableTextConfig: {
|
|
2
|
+
readonly linkRel: "noopener noreferrer";
|
|
3
|
+
readonly linkTarget: "_blank";
|
|
4
|
+
readonly paragraphSeparator: "\n\n";
|
|
5
|
+
readonly lineSeparator: "\n";
|
|
6
|
+
readonly paragraphSplitPattern: RegExp;
|
|
7
|
+
readonly lineBreakPattern: RegExp;
|
|
8
|
+
readonly bulletPrefix: "- ";
|
|
9
|
+
readonly quotePrefix: "> ";
|
|
10
|
+
readonly numberSuffix: ". ";
|
|
11
|
+
readonly keyPrefix: {
|
|
12
|
+
readonly block: "b";
|
|
13
|
+
readonly span: "s";
|
|
14
|
+
};
|
|
15
|
+
readonly normalStyle: "normal";
|
|
16
|
+
readonly quoteStyle: "blockquote";
|
|
17
|
+
readonly bulletItem: "bullet";
|
|
18
|
+
readonly numberItem: "number";
|
|
19
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortableTextConfig = void 0;
|
|
4
|
+
var ps_models_1 = require("@pallasoft/ps-models");
|
|
5
|
+
exports.PortableTextConfig = {
|
|
6
|
+
linkRel: 'noopener noreferrer',
|
|
7
|
+
linkTarget: '_blank',
|
|
8
|
+
paragraphSeparator: '\n\n',
|
|
9
|
+
lineSeparator: '\n',
|
|
10
|
+
paragraphSplitPattern: /\n[ \t]*\n/,
|
|
11
|
+
lineBreakPattern: /\r\n?/g,
|
|
12
|
+
bulletPrefix: '- ',
|
|
13
|
+
quotePrefix: '> ',
|
|
14
|
+
numberSuffix: '. ',
|
|
15
|
+
keyPrefix: { block: 'b', span: 's' },
|
|
16
|
+
normalStyle: ps_models_1.Common.PortableTextConfig.styles[0],
|
|
17
|
+
quoteStyle: ps_models_1.Common.PortableTextConfig.styles[1],
|
|
18
|
+
bulletItem: ps_models_1.Common.PortableTextConfig.listItems[0],
|
|
19
|
+
numberItem: ps_models_1.Common.PortableTextConfig.listItems[1],
|
|
20
|
+
};
|
|
@@ -11,6 +11,7 @@ export declare class CryptoService {
|
|
|
11
11
|
private static _dataKeyLength;
|
|
12
12
|
private static _payloadAlgorithm;
|
|
13
13
|
private static _aad;
|
|
14
|
+
static requireKey(key: string | undefined, name: string): string;
|
|
14
15
|
private static _getAdditionalData;
|
|
15
16
|
private static _parsePayload;
|
|
16
17
|
private static _deriveKey;
|
|
@@ -41,6 +41,12 @@ var base64_service_1 = require("./base64-service");
|
|
|
41
41
|
var CryptoService = /** @class */ (function () {
|
|
42
42
|
function CryptoService() {
|
|
43
43
|
}
|
|
44
|
+
CryptoService.requireKey = function (key, name) {
|
|
45
|
+
if (typeof key !== 'string' || key.trim().length === 0) {
|
|
46
|
+
throw new Error("".concat(name, " is required and must not be empty"));
|
|
47
|
+
}
|
|
48
|
+
return key;
|
|
49
|
+
};
|
|
44
50
|
CryptoService._getAdditionalData = function () {
|
|
45
51
|
return this._textEncoder.encode(this._aad);
|
|
46
52
|
};
|
|
@@ -84,6 +90,7 @@ var CryptoService = /** @class */ (function () {
|
|
|
84
90
|
return __generator(this, function (_a) {
|
|
85
91
|
switch (_a.label) {
|
|
86
92
|
case 0:
|
|
93
|
+
this.requireKey(cryptoKey, 'Encryption key');
|
|
87
94
|
iv = crypto.getRandomValues(new Uint8Array(this._ivLength));
|
|
88
95
|
salt = crypto.getRandomValues(new Uint8Array(this._saltLength));
|
|
89
96
|
return [4 /*yield*/, this._deriveKey(cryptoKey, salt)];
|
|
@@ -127,6 +134,7 @@ var CryptoService = /** @class */ (function () {
|
|
|
127
134
|
return __generator(this, function (_d) {
|
|
128
135
|
switch (_d.label) {
|
|
129
136
|
case 0:
|
|
137
|
+
this.requireKey(dataKey, 'Data key');
|
|
130
138
|
iv = crypto.getRandomValues(new Uint8Array(this._ivLength));
|
|
131
139
|
_b = (_a = crypto.subtle).encrypt;
|
|
132
140
|
_c = [{ name: this._algorithm, iv: iv, additionalData: this._getAdditionalData() }];
|
|
@@ -150,6 +158,7 @@ var CryptoService = /** @class */ (function () {
|
|
|
150
158
|
return __generator(this, function (_d) {
|
|
151
159
|
switch (_d.label) {
|
|
152
160
|
case 0:
|
|
161
|
+
this.requireKey(dataKey, 'Data key');
|
|
153
162
|
payload = this._parsePayload(encryptedPayload);
|
|
154
163
|
_b = (_a = crypto.subtle).decrypt;
|
|
155
164
|
_c = [{
|
|
@@ -227,6 +236,7 @@ var CryptoService = /** @class */ (function () {
|
|
|
227
236
|
return __generator(this, function (_a) {
|
|
228
237
|
switch (_a.label) {
|
|
229
238
|
case 0:
|
|
239
|
+
this.requireKey(cryptoKey, 'Encryption key');
|
|
230
240
|
payload = this._parsePayload(encryptedPayload);
|
|
231
241
|
return [4 /*yield*/, this._deriveKey(cryptoKey, base64_service_1.Base64Service.decode(payload.salt))];
|
|
232
242
|
case 1:
|
|
@@ -55,6 +55,7 @@ var EnvelopeCryptoService = /** @class */ (function () {
|
|
|
55
55
|
return __generator(this, function (_a) {
|
|
56
56
|
switch (_a.label) {
|
|
57
57
|
case 0:
|
|
58
|
+
crypto_service_1.CryptoService.requireKey(masterKey, 'Master key');
|
|
58
59
|
dataKey = crypto_service_1.CryptoService.generateDataKey();
|
|
59
60
|
return [4 /*yield*/, crypto_service_1.CryptoService.encryptValue(dataKey, masterKey)];
|
|
60
61
|
case 1:
|
|
@@ -71,6 +72,7 @@ var EnvelopeCryptoService = /** @class */ (function () {
|
|
|
71
72
|
return __generator(this, function (_a) {
|
|
72
73
|
switch (_a.label) {
|
|
73
74
|
case 0:
|
|
75
|
+
crypto_service_1.CryptoService.requireKey(masterKey, 'Master key');
|
|
74
76
|
cached = this._cache.get(wrappedDataKey);
|
|
75
77
|
if (cached)
|
|
76
78
|
return [2 /*return*/, cached];
|
package/dist/common/index.d.ts
CHANGED
package/dist/common/index.js
CHANGED
|
@@ -40,3 +40,4 @@ __exportStar(require("./error-response-service"), exports);
|
|
|
40
40
|
__exportStar(require("./relative-time-service"), exports);
|
|
41
41
|
__exportStar(require("./request-client-service"), exports);
|
|
42
42
|
__exportStar(require("./json-service"), exports);
|
|
43
|
+
__exportStar(require("./portable-text-service"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PortableTextBlock } from '@portabletext/types';
|
|
2
|
+
export declare class PortableTextService {
|
|
3
|
+
static toPlainText(blocks: PortableTextBlock[]): string;
|
|
4
|
+
static fromPlainText(text: string): PortableTextBlock[];
|
|
5
|
+
static isEmpty(blocks: PortableTextBlock[] | null | undefined): boolean;
|
|
6
|
+
static toHtml(blocks: PortableTextBlock[]): string;
|
|
7
|
+
private static blockText;
|
|
8
|
+
private static isSafeLink;
|
|
9
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortableTextService = void 0;
|
|
4
|
+
var ps_models_1 = require("@pallasoft/ps-models");
|
|
5
|
+
var to_html_1 = require("@portabletext/to-html");
|
|
6
|
+
var portable_text_config_1 = require("./constants/portable-text-config");
|
|
7
|
+
var PortableTextService = /** @class */ (function () {
|
|
8
|
+
function PortableTextService() {
|
|
9
|
+
}
|
|
10
|
+
PortableTextService.toPlainText = function (blocks) {
|
|
11
|
+
var _this = this;
|
|
12
|
+
var lines = [];
|
|
13
|
+
var counters = {};
|
|
14
|
+
var previousWasList = false;
|
|
15
|
+
blocks.forEach(function (block) {
|
|
16
|
+
if (block._type !== ps_models_1.Common.PortableTextConfig.blockType)
|
|
17
|
+
return;
|
|
18
|
+
var text = _this.blockText(block);
|
|
19
|
+
var level = typeof block.level === 'number' ? block.level : 1;
|
|
20
|
+
var isNumber = block.listItem === portable_text_config_1.PortableTextConfig.numberItem;
|
|
21
|
+
var isBullet = block.listItem === portable_text_config_1.PortableTextConfig.bulletItem;
|
|
22
|
+
var isList = isNumber || isBullet;
|
|
23
|
+
if (!isList) {
|
|
24
|
+
counters[level] = 0;
|
|
25
|
+
if (lines.length > 0)
|
|
26
|
+
lines.push(portable_text_config_1.PortableTextConfig.paragraphSeparator);
|
|
27
|
+
lines.push(block.style === portable_text_config_1.PortableTextConfig.quoteStyle
|
|
28
|
+
? portable_text_config_1.PortableTextConfig.quotePrefix + text
|
|
29
|
+
: text);
|
|
30
|
+
previousWasList = false;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
counters[level] = isNumber
|
|
34
|
+
? (typeof counters[level] === 'number' ? counters[level] : 0) + 1
|
|
35
|
+
: 0;
|
|
36
|
+
var prefix = isNumber
|
|
37
|
+
? "".concat(counters[level]).concat(portable_text_config_1.PortableTextConfig.numberSuffix)
|
|
38
|
+
: portable_text_config_1.PortableTextConfig.bulletPrefix;
|
|
39
|
+
var indent = ' '.repeat(level - 1);
|
|
40
|
+
if (lines.length > 0)
|
|
41
|
+
lines.push(previousWasList ? portable_text_config_1.PortableTextConfig.lineSeparator : portable_text_config_1.PortableTextConfig.paragraphSeparator);
|
|
42
|
+
lines.push(indent + prefix + text);
|
|
43
|
+
previousWasList = true;
|
|
44
|
+
});
|
|
45
|
+
return lines.join('');
|
|
46
|
+
};
|
|
47
|
+
PortableTextService.fromPlainText = function (text) {
|
|
48
|
+
return text
|
|
49
|
+
.replace(portable_text_config_1.PortableTextConfig.lineBreakPattern, portable_text_config_1.PortableTextConfig.lineSeparator)
|
|
50
|
+
.split(portable_text_config_1.PortableTextConfig.paragraphSplitPattern)
|
|
51
|
+
.map(function (paragraph) { return paragraph.trim(); })
|
|
52
|
+
.filter(function (paragraph) { return paragraph.length > 0; })
|
|
53
|
+
.map(function (paragraph, index) { return ({
|
|
54
|
+
_type: ps_models_1.Common.PortableTextConfig.blockType,
|
|
55
|
+
_key: "".concat(portable_text_config_1.PortableTextConfig.keyPrefix.block).concat(index),
|
|
56
|
+
style: portable_text_config_1.PortableTextConfig.normalStyle,
|
|
57
|
+
markDefs: [],
|
|
58
|
+
children: [
|
|
59
|
+
{
|
|
60
|
+
_type: ps_models_1.Common.PortableTextConfig.spanType,
|
|
61
|
+
_key: "".concat(portable_text_config_1.PortableTextConfig.keyPrefix.span).concat(index),
|
|
62
|
+
text: paragraph,
|
|
63
|
+
marks: [],
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
}); });
|
|
67
|
+
};
|
|
68
|
+
PortableTextService.isEmpty = function (blocks) {
|
|
69
|
+
var _this = this;
|
|
70
|
+
return (!Array.isArray(blocks) || blocks.every(function (block) { return _this.blockText(block).trim().length === 0; }));
|
|
71
|
+
};
|
|
72
|
+
PortableTextService.toHtml = function (blocks) {
|
|
73
|
+
var _this = this;
|
|
74
|
+
return (0, to_html_1.toHTML)(blocks, {
|
|
75
|
+
onMissingComponent: false,
|
|
76
|
+
components: {
|
|
77
|
+
marks: {
|
|
78
|
+
link: function (_a) {
|
|
79
|
+
var children = _a.children, value = _a.value;
|
|
80
|
+
var href = typeof (value === null || value === void 0 ? void 0 : value.href) === 'string' ? value.href : '';
|
|
81
|
+
return _this.isSafeLink(href)
|
|
82
|
+
? "<a href=\"".concat((0, to_html_1.escapeHTML)(href), "\" rel=\"").concat(portable_text_config_1.PortableTextConfig.linkRel, "\" target=\"").concat(portable_text_config_1.PortableTextConfig.linkTarget, "\">").concat(children, "</a>")
|
|
83
|
+
: children;
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
PortableTextService.blockText = function (block) {
|
|
90
|
+
return Array.isArray(block.children)
|
|
91
|
+
? block.children
|
|
92
|
+
.map(function (child) {
|
|
93
|
+
return child._type === ps_models_1.Common.PortableTextConfig.spanType && typeof child.text === 'string'
|
|
94
|
+
? child.text
|
|
95
|
+
: '';
|
|
96
|
+
})
|
|
97
|
+
.join('')
|
|
98
|
+
: '';
|
|
99
|
+
};
|
|
100
|
+
PortableTextService.isSafeLink = function (href) {
|
|
101
|
+
try {
|
|
102
|
+
return ps_models_1.Common.PortableTextConfig.linkProtocols.includes(new URL(href).protocol);
|
|
103
|
+
}
|
|
104
|
+
catch (_a) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
return PortableTextService;
|
|
109
|
+
}());
|
|
110
|
+
exports.PortableTextService = PortableTextService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pallasoft/ps-utilities",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.17.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -77,6 +77,8 @@
|
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
79
|
"@esbuild-plugins/tsconfig-paths": "^0.1.2",
|
|
80
|
+
"@pallasoft/ps-models": "^1.4.0",
|
|
81
|
+
"@portabletext/to-html": "^4.0.1",
|
|
80
82
|
"dayjs": "^1.11.19",
|
|
81
83
|
"esbuild": "^0.27.0",
|
|
82
84
|
"libphonenumber-js": "^1.13.14"
|