@e-mc/document 0.11.7 → 0.12.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 +12 -12
- package/index.js +86 -79
- package/package.json +7 -7
- package/parse/dom.js +12 -18
- package/parse/index.js +64 -53
- package/parse/types/index.d.ts +4 -6
- package/transform/index.js +67 -51
- package/util.d.ts +3 -3
- package/util.js +21 -7
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @e-mc/document
|
|
2
2
|
|
|
3
|
-
* NodeJS
|
|
4
|
-
*
|
|
3
|
+
* NodeJS 18
|
|
4
|
+
* ES2022
|
|
5
5
|
|
|
6
6
|
## General Usage
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.12.0/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { DataSource, ViewEngine } from "./squared";
|
|
@@ -61,7 +61,7 @@ interface IDocument extends IClient<IFileManager, DocumentModule, TransformCallb
|
|
|
61
61
|
cloudUpload?(state: IScopeOrigin<IFileManager, ICloud>, file: ExternalAsset, url: string, active: boolean): Promise<boolean>;
|
|
62
62
|
cloudFinalize?(state: IScopeOrigin<IFileManager, ICloud>): Promise<unknown[]>;
|
|
63
63
|
watchInit?(watch: IFileGroup<ExternalAsset>, assets: ExternalAsset[], sanitize?: boolean): WatchInitResult | undefined;
|
|
64
|
-
watchModified?(watch: IFileGroup<ExternalAsset>, assets?: ExternalAsset[]): PostFinalizeCallback;
|
|
64
|
+
watchModified?(watch: IFileGroup<ExternalAsset>, assets?: ExternalAsset[], recursive?: [string, string[]][]): PostFinalizeCallback;
|
|
65
65
|
set dataSource(value: DataSource[]);
|
|
66
66
|
get dataSource(): DataSource[];
|
|
67
67
|
set imports(value);
|
|
@@ -185,14 +185,14 @@ NOTE: **@e-mc/document** is an abstract base class and cannot be instantiated. *
|
|
|
185
185
|
|
|
186
186
|
## References
|
|
187
187
|
|
|
188
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
189
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
190
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
191
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
192
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
193
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
194
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
195
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
188
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/squared.d.ts
|
|
189
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/asset.d.ts
|
|
190
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/core.d.ts
|
|
191
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/document.d.ts
|
|
192
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/filemanager.d.ts
|
|
193
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/logger.d.ts
|
|
194
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/settings.d.ts
|
|
195
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/watch.d.ts
|
|
196
196
|
|
|
197
197
|
## LICENSE
|
|
198
198
|
|
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const db_1 = require("@e-mc/db");
|
|
|
11
11
|
const transform_1 = require("@e-mc/document/transform");
|
|
12
12
|
const parse_1 = require("@e-mc/document/parse");
|
|
13
13
|
const util_1 = require("@e-mc/document/util");
|
|
14
|
+
const kDocument = Symbol.for('document:constructor');
|
|
14
15
|
const CACHE_PACKAGE = {};
|
|
15
16
|
const CACHE_TEMPLATE = {};
|
|
16
17
|
const CACHE_EXTERNAL = {};
|
|
@@ -54,19 +55,23 @@ const fixHint = (warningCount, errorCount, value) => warningCount > 0 || errorCo
|
|
|
54
55
|
const compareValue = (value) => value.replace(/["'()]/g, '').trim();
|
|
55
56
|
const toPath = (uri, hostname) => path.resolve(hostname[1], uri.substring(normalizeDir(hostname[0]).length).split('?')[0]);
|
|
56
57
|
const toBase64 = (value) => 'data:application/json;base64,' + Buffer.from(value).toString('base64');
|
|
57
|
-
const toPosix = (value) => value.replace(/(?:^\\|\\+)/g, '/');
|
|
58
|
+
const toPosix = (value) => core_1.Client.PLATFORM_WIN32 ? value.trim().replace(/(?:^\\|\\+)/g, '/') : value;
|
|
58
59
|
const joinString = (name, ...values) => (name ? name + ': ' : '') + values.reduce((a, b) => b ? a + (a ? ' -> ' : '') + b : a, '');
|
|
59
60
|
const truncateString = (value, width) => value.length > width ? value.substring(0, width - 3) + '...' : value.padStart(width);
|
|
60
|
-
const isFunction = (value) => typeof value === 'function';
|
|
61
61
|
class TransformCache {
|
|
62
|
+
config;
|
|
63
|
+
cacheData;
|
|
64
|
+
cacheDir;
|
|
65
|
+
encoding;
|
|
66
|
+
formatKey;
|
|
67
|
+
formatType = 0;
|
|
68
|
+
hashKey = '';
|
|
62
69
|
constructor(config, cacheData, cacheDir, encoding, formatKey) {
|
|
63
70
|
this.config = config;
|
|
64
71
|
this.cacheData = cacheData;
|
|
65
72
|
this.cacheDir = cacheDir;
|
|
66
73
|
this.encoding = encoding;
|
|
67
74
|
this.formatKey = formatKey;
|
|
68
|
-
this.formatType = 0;
|
|
69
|
-
this.hashKey = '';
|
|
70
75
|
}
|
|
71
76
|
set(type, data) {
|
|
72
77
|
let pathname;
|
|
@@ -151,15 +156,16 @@ class TransformCache {
|
|
|
151
156
|
}
|
|
152
157
|
}
|
|
153
158
|
class TransformConfig {
|
|
159
|
+
moduleName;
|
|
160
|
+
algorithm = undefined;
|
|
161
|
+
etag = false;
|
|
162
|
+
expires = 0;
|
|
163
|
+
renew = false;
|
|
164
|
+
limit = Infinity;
|
|
165
|
+
exclude = {};
|
|
166
|
+
include = {};
|
|
154
167
|
constructor(moduleName) {
|
|
155
168
|
this.moduleName = moduleName;
|
|
156
|
-
this.algorithm = undefined;
|
|
157
|
-
this.etag = false;
|
|
158
|
-
this.expires = 0;
|
|
159
|
-
this.renew = false;
|
|
160
|
-
this.limit = Infinity;
|
|
161
|
-
this.exclude = {};
|
|
162
|
-
this.include = {};
|
|
163
169
|
}
|
|
164
170
|
init(options) {
|
|
165
171
|
let enabled = false, algorithm, etag, expires, renew, limit, include, exclude;
|
|
@@ -326,6 +332,7 @@ class TransformConfig {
|
|
|
326
332
|
}
|
|
327
333
|
}
|
|
328
334
|
class Document extends core_1.Client {
|
|
335
|
+
static [kDocument] = true;
|
|
329
336
|
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
330
337
|
if (limit > 0 && CACHE_TOTAL < limit) {
|
|
331
338
|
return 0;
|
|
@@ -377,7 +384,7 @@ class Document extends core_1.Client {
|
|
|
377
384
|
}
|
|
378
385
|
try {
|
|
379
386
|
const args = [instance];
|
|
380
|
-
if (ext
|
|
387
|
+
if ((0, types_1.isFunction)(ext, true)) {
|
|
381
388
|
args.push(__dirname);
|
|
382
389
|
}
|
|
383
390
|
else if (core_1.Client.enabled("node.require.inline")) {
|
|
@@ -427,7 +434,7 @@ class Document extends core_1.Client {
|
|
|
427
434
|
}
|
|
428
435
|
}
|
|
429
436
|
}
|
|
430
|
-
let code = data.code.replace(transform_1.SourceMap.
|
|
437
|
+
let code = data.code.replace(transform_1.SourceMap.SOURCE_MAPPING_URL, (...capture) => {
|
|
431
438
|
flags |= 1;
|
|
432
439
|
if (capture[2] && capture[5]) {
|
|
433
440
|
css = true;
|
|
@@ -598,14 +605,14 @@ class Document extends core_1.Client {
|
|
|
598
605
|
}
|
|
599
606
|
return result;
|
|
600
607
|
}
|
|
608
|
+
Db = null;
|
|
609
|
+
_assets = null;
|
|
610
|
+
_dataSource = null;
|
|
611
|
+
_transformConfig = null;
|
|
612
|
+
_mimeMap = null;
|
|
613
|
+
_imports = null;
|
|
601
614
|
constructor(data) {
|
|
602
615
|
super(data);
|
|
603
|
-
this.Db = null;
|
|
604
|
-
this._assets = null;
|
|
605
|
-
this._dataSource = null;
|
|
606
|
-
this._transformConfig = null;
|
|
607
|
-
this._mimeMap = null;
|
|
608
|
-
this._imports = null;
|
|
609
616
|
const transform = this.settingsOf('transform', 'cache');
|
|
610
617
|
if (transform !== undefined) {
|
|
611
618
|
this.customize({ transform });
|
|
@@ -678,9 +685,9 @@ class Document extends core_1.Client {
|
|
|
678
685
|
let instance;
|
|
679
686
|
if (handler && handler !== "@e-mc/db") {
|
|
680
687
|
try {
|
|
681
|
-
const
|
|
682
|
-
if (
|
|
683
|
-
instance = new
|
|
688
|
+
const Module = require(handler);
|
|
689
|
+
if (core_1.Client.constructorOf(Module, 'clientdb')) {
|
|
690
|
+
instance = new Module(db, database);
|
|
684
691
|
}
|
|
685
692
|
else {
|
|
686
693
|
throw (0, types_1.errorMessage)(this.moduleName, "Not a Db constructor", handler);
|
|
@@ -710,7 +717,7 @@ class Document extends core_1.Client {
|
|
|
710
717
|
if (!ignoreExtensions) {
|
|
711
718
|
return this;
|
|
712
719
|
}
|
|
713
|
-
if (ignoreExtensions === true || ignoreExtensions === this.moduleName ||
|
|
720
|
+
if (ignoreExtensions === true || ignoreExtensions === this.moduleName || Array.isArray(ignoreExtensions) && ignoreExtensions.includes(this.moduleName)) {
|
|
714
721
|
this._extensions = [];
|
|
715
722
|
}
|
|
716
723
|
else {
|
|
@@ -730,7 +737,10 @@ class Document extends core_1.Client {
|
|
|
730
737
|
if (this.aborted || (0, types_1.isString)(name) && this.settingsOf(name, 'abort') !== true) {
|
|
731
738
|
return;
|
|
732
739
|
}
|
|
733
|
-
|
|
740
|
+
if (name instanceof Error) {
|
|
741
|
+
reason ||= name;
|
|
742
|
+
}
|
|
743
|
+
super.abort(reason);
|
|
734
744
|
}
|
|
735
745
|
customize(options) {
|
|
736
746
|
const transform = options.transform;
|
|
@@ -793,25 +803,24 @@ class Document extends core_1.Client {
|
|
|
793
803
|
}
|
|
794
804
|
resolveDir(name, ...paths) {
|
|
795
805
|
let baseDir = this.settings.directory?.[name];
|
|
796
|
-
if (
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
+
if (baseDir) {
|
|
807
|
+
try {
|
|
808
|
+
baseDir = path.resolve(baseDir);
|
|
809
|
+
const username = this.host?.username;
|
|
810
|
+
let result;
|
|
811
|
+
if (username) {
|
|
812
|
+
const leading = path.join(baseDir, 'users', username);
|
|
813
|
+
if (fs.existsSync(result = path.join(leading, ...paths))) {
|
|
814
|
+
return result;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
if (fs.existsSync(result = path.join(baseDir, ...paths))) {
|
|
806
818
|
return result;
|
|
807
819
|
}
|
|
808
820
|
}
|
|
809
|
-
|
|
810
|
-
return result;
|
|
821
|
+
catch {
|
|
811
822
|
}
|
|
812
823
|
}
|
|
813
|
-
catch {
|
|
814
|
-
}
|
|
815
824
|
}
|
|
816
825
|
asSourceFile(value, options) {
|
|
817
826
|
let encoding, cache;
|
|
@@ -854,7 +863,7 @@ class Document extends core_1.Client {
|
|
|
854
863
|
catch {
|
|
855
864
|
}
|
|
856
865
|
}
|
|
857
|
-
if ((0, types_1.isObject)(result) || typeof result === 'function' &&
|
|
866
|
+
if ((0, types_1.isObject)(result) || (0, types_1.isFunction)(result, true) || typeof result === 'function' && this.hasEval('function')) {
|
|
858
867
|
return result;
|
|
859
868
|
}
|
|
860
869
|
}
|
|
@@ -864,8 +873,8 @@ class Document extends core_1.Client {
|
|
|
864
873
|
return null;
|
|
865
874
|
}
|
|
866
875
|
findVersion(name, fallback = '') {
|
|
867
|
-
let result;
|
|
868
876
|
const versions = this.module.versions;
|
|
877
|
+
let result;
|
|
869
878
|
if (versions) {
|
|
870
879
|
if (Array.isArray(name)) {
|
|
871
880
|
for (const value of name) {
|
|
@@ -900,34 +909,33 @@ class Document extends core_1.Client {
|
|
|
900
909
|
}
|
|
901
910
|
const importsStrict = this.getUserSettings()?.imports_strict ?? this.settings.imports_strict;
|
|
902
911
|
const scopes = (importsStrict ? this.findSourceScope(uri, imports) : []).concat([imports]);
|
|
903
|
-
const
|
|
912
|
+
const protocol = core_1.Client.isURL(uri);
|
|
904
913
|
let result;
|
|
905
914
|
for (const scope of scopes) {
|
|
906
915
|
for (const url in scope) {
|
|
907
916
|
if (uri === url && (0, types_1.isString)(result = scope[url])) {
|
|
908
917
|
if (importsStrict) {
|
|
909
|
-
if (
|
|
910
|
-
|
|
918
|
+
if (core_1.Client.isPath(result) && !(protocol ? url : toPosix(url)).endsWith('/')) {
|
|
919
|
+
return toPosix(result);
|
|
911
920
|
}
|
|
912
|
-
|
|
921
|
+
continue;
|
|
913
922
|
}
|
|
914
923
|
return result;
|
|
915
924
|
}
|
|
916
925
|
}
|
|
917
926
|
}
|
|
918
|
-
const remote = toPosix(uri);
|
|
919
927
|
const found = [];
|
|
920
928
|
if (importsStrict) {
|
|
921
|
-
const
|
|
929
|
+
const remote = protocol ? uri : toPosix(uri);
|
|
922
930
|
for (const scope of scopes) {
|
|
923
931
|
for (const url in scope) {
|
|
924
932
|
const local = protocol ? url : toPosix(url);
|
|
925
|
-
if (
|
|
933
|
+
if (local.endsWith('/') && remote.startsWith(local) && (result = scope[url]) && result.endsWith('/')) {
|
|
926
934
|
if (protocol) {
|
|
927
935
|
found.push([url, result]);
|
|
928
936
|
}
|
|
929
937
|
else if (core_1.Client.isPath(result = path.join(result, remote.substring(local.length)))) {
|
|
930
|
-
return
|
|
938
|
+
return toPosix(result);
|
|
931
939
|
}
|
|
932
940
|
}
|
|
933
941
|
}
|
|
@@ -962,7 +970,7 @@ class Document extends core_1.Client {
|
|
|
962
970
|
result = toPath(uri, found[0]);
|
|
963
971
|
}
|
|
964
972
|
if (result) {
|
|
965
|
-
return
|
|
973
|
+
return toPosix(result);
|
|
966
974
|
}
|
|
967
975
|
}
|
|
968
976
|
}
|
|
@@ -990,7 +998,7 @@ class Document extends core_1.Client {
|
|
|
990
998
|
if (item.bundleIndex === 0) {
|
|
991
999
|
const localUri = item.localUri;
|
|
992
1000
|
try {
|
|
993
|
-
source = fs.readFileSync(localUri, file.encoding ||= '
|
|
1001
|
+
source = fs.readFileSync(localUri, file.encoding ||= 'utf8');
|
|
994
1002
|
if (this.resolveUri && (0, types_1.isArray)(file.trailingContent)) {
|
|
995
1003
|
let trailing;
|
|
996
1004
|
[source, trailing] = this.resolveUri(file, source, (0, util_1.concatString)(file.trailingContent));
|
|
@@ -1014,7 +1022,7 @@ class Document extends core_1.Client {
|
|
|
1014
1022
|
}
|
|
1015
1023
|
}
|
|
1016
1024
|
}
|
|
1017
|
-
if (sourceFile.length > 0 && (sourceFile[0] && (mainFile = sourceFile[0][0]) || !invalid) && (!this.
|
|
1025
|
+
if (sourceFile.length > 0 && (sourceFile[0] && (mainFile = sourceFile[0][0]) || !invalid) && (!this.hasPermission('fs') && !process.permission || !sourceFile.some(item => item[0] && !this.canRead(item[0])))) {
|
|
1018
1026
|
return { code, sourceFile: !invalid ? sourceFile : undefined, sourcesRelativeTo: mainFile && path.dirname(mainFile) };
|
|
1019
1027
|
}
|
|
1020
1028
|
};
|
|
@@ -1091,20 +1099,18 @@ class Document extends core_1.Client {
|
|
|
1091
1099
|
else {
|
|
1092
1100
|
target = viewEngine;
|
|
1093
1101
|
}
|
|
1094
|
-
if (!(
|
|
1102
|
+
if (!(0, types_1.isPlainObject)(target) || !(0, types_1.isString)(target.name)) {
|
|
1095
1103
|
this.abort('view_engine');
|
|
1096
1104
|
const from = typeof viewEngine === 'string' ? viewEngine : this.moduleName;
|
|
1097
1105
|
this.writeFail(["Unable to load configuration", from], (0, types_1.errorMessage)('view-engine', from, "Unknown"));
|
|
1098
1106
|
return null;
|
|
1099
1107
|
}
|
|
1100
|
-
|
|
1101
|
-
if (length === 0 && !target.outputEmpty) {
|
|
1108
|
+
if (data.length === 0 && !target.outputEmpty) {
|
|
1102
1109
|
return '';
|
|
1103
1110
|
}
|
|
1104
1111
|
try {
|
|
1105
1112
|
const context = require(target.name);
|
|
1106
|
-
|
|
1107
|
-
const { compile, output } = options;
|
|
1113
|
+
const { compile, output } = target.options || {};
|
|
1108
1114
|
if (this.settingsOf('view_engine', 'coerce') === true) {
|
|
1109
1115
|
if (compile) {
|
|
1110
1116
|
(0, types_1.coerceObject)(compile, stored);
|
|
@@ -1113,13 +1119,10 @@ class Document extends core_1.Client {
|
|
|
1113
1119
|
(0, types_1.coerceObject)(output, stored);
|
|
1114
1120
|
}
|
|
1115
1121
|
}
|
|
1116
|
-
if (typeof expires === 'string') {
|
|
1117
|
-
expires = (0, types_1.parseExpires)(expires);
|
|
1118
|
-
}
|
|
1119
1122
|
const username = this.host?.username || '';
|
|
1120
1123
|
const cache = CACHE_TEMPLATE[target.name] ||= {};
|
|
1121
1124
|
const cacheKey = (username ? username + ':' : '') + (0, types_1.hashKey)(template + (compile ? core_1.Client.asString(compile) : ''));
|
|
1122
|
-
let result = '',
|
|
1125
|
+
let result = '', valid = false, expires = typeof target.expires === 'string' ? (0, types_1.parseExpires)(target.expires) : target.expires, render;
|
|
1123
1126
|
if (expires !== 0) {
|
|
1124
1127
|
render = cache[cacheKey];
|
|
1125
1128
|
}
|
|
@@ -1127,11 +1130,16 @@ class Document extends core_1.Client {
|
|
|
1127
1130
|
delete cache[cacheKey];
|
|
1128
1131
|
}
|
|
1129
1132
|
if (!render) {
|
|
1130
|
-
|
|
1133
|
+
if ((0, types_1.isArray)(compile)) {
|
|
1134
|
+
render = typeof context.compileSync === 'function' ? context.compileSync(template, ...compile) : await context.compile(template, ...compile);
|
|
1135
|
+
}
|
|
1136
|
+
else {
|
|
1137
|
+
render = typeof context.compileSync === 'function' ? context.compileSync(template, compile) : await context.compile(template, compile);
|
|
1138
|
+
}
|
|
1131
1139
|
if (expires !== 0) {
|
|
1132
1140
|
cache[cacheKey] = render;
|
|
1133
|
-
if (
|
|
1134
|
-
expires = typeof expires === 'number' && expires !== Infinity ? expires : 0;
|
|
1141
|
+
if (this.hasPermission('memory')) {
|
|
1142
|
+
expires = typeof expires === 'number' && expires !== Infinity ? Math.min(expires, core_1.Client.MAX_TIMEOUT) : 0;
|
|
1135
1143
|
}
|
|
1136
1144
|
else {
|
|
1137
1145
|
expires = 60000;
|
|
@@ -1148,7 +1156,8 @@ class Document extends core_1.Client {
|
|
|
1148
1156
|
valid = true;
|
|
1149
1157
|
}
|
|
1150
1158
|
};
|
|
1151
|
-
|
|
1159
|
+
const singleRow = !!target.singleRow;
|
|
1160
|
+
for (let i = 0, j = 0; i < data.length; ++i) {
|
|
1152
1161
|
let row = data[i];
|
|
1153
1162
|
if ((0, types_1.isPlainObject)(row)) {
|
|
1154
1163
|
row.__index__ ??= ++j;
|
|
@@ -1157,7 +1166,7 @@ class Document extends core_1.Client {
|
|
|
1157
1166
|
}
|
|
1158
1167
|
}
|
|
1159
1168
|
else if (!(0, types_1.isObject)(row)) {
|
|
1160
|
-
this.addLog(types_1.STATUS_TYPE.WARN, joinString('view engine', target.name,
|
|
1169
|
+
this.addLog(types_1.STATUS_TYPE.WARN, joinString('view engine', target.name, core_1.Client.asString(row) || "Unknown"), { source: `row #${i + 1}` });
|
|
1161
1170
|
continue;
|
|
1162
1171
|
}
|
|
1163
1172
|
if (!singleRow) {
|
|
@@ -1177,16 +1186,18 @@ class Document extends core_1.Client {
|
|
|
1177
1186
|
}
|
|
1178
1187
|
async transform(type, code, format, options = {}) {
|
|
1179
1188
|
let transform = this.settings.transform, data;
|
|
1180
|
-
if (!(transform
|
|
1189
|
+
if (!(0, types_1.isObject)(transform) || !(0, types_1.isObject)(data = transform[type])) {
|
|
1181
1190
|
return;
|
|
1182
1191
|
}
|
|
1183
|
-
|
|
1192
|
+
if (typeof format === 'string') {
|
|
1193
|
+
format = format.trim().split(/\s*\+\s*/);
|
|
1194
|
+
}
|
|
1184
1195
|
const username = this.host?.username || '';
|
|
1185
1196
|
const config = this._transformConfig;
|
|
1186
1197
|
const cacheData = config && options.cacheData;
|
|
1187
1198
|
let cache = null, excludeKey;
|
|
1188
1199
|
if (cacheData) {
|
|
1189
|
-
const cacheDir =
|
|
1200
|
+
const cacheDir = this.hasPermission('memory.user') || this.cacheDir || this.hasPermission('memory');
|
|
1190
1201
|
if (cacheDir && !CACHE_EXTERNAL[excludeKey = this.moduleName + '_' + type + '_' + format] && config.valid(type, format)) {
|
|
1191
1202
|
const { uri, etag } = cacheData;
|
|
1192
1203
|
const encoding = (0, types_1.getEncoding)(cacheData.encoding);
|
|
@@ -1219,9 +1230,8 @@ class Document extends core_1.Client {
|
|
|
1219
1230
|
series.init(this, __dirname);
|
|
1220
1231
|
let valid = false, excluded = false, userData, userImports, ignoreCache = false, storedLog, sourceFiles;
|
|
1221
1232
|
if (username && (0, types_1.isObject)(transform = this.getUserSettings()?.transform)) {
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
userData = undefined;
|
|
1233
|
+
if ((0, types_1.isObject)(transform[type])) {
|
|
1234
|
+
userData = transform[type];
|
|
1225
1235
|
}
|
|
1226
1236
|
userImports = transform.imports;
|
|
1227
1237
|
}
|
|
@@ -1234,9 +1244,6 @@ class Document extends core_1.Client {
|
|
|
1234
1244
|
break;
|
|
1235
1245
|
}
|
|
1236
1246
|
const name = format[i];
|
|
1237
|
-
if (!name) {
|
|
1238
|
-
continue;
|
|
1239
|
-
}
|
|
1240
1247
|
let [plugin, baseConfig, outputConfig, baseSettings] = this.findConfig(data, name, type);
|
|
1241
1248
|
if (userData) {
|
|
1242
1249
|
const [a, b, c, d] = this.findConfig(userData, name, type);
|
|
@@ -1278,7 +1285,7 @@ class Document extends core_1.Client {
|
|
|
1278
1285
|
}
|
|
1279
1286
|
const out = series.out;
|
|
1280
1287
|
let bypassLog = false, failed = false;
|
|
1281
|
-
if (typeof value !== 'string' || (0, types_1.isArray)(out.sourceFiles) && this.
|
|
1288
|
+
if (typeof value !== 'string' || (0, types_1.isArray)(out.sourceFiles) && (this.hasPermission('fs') || process.permission) && out.sourceFiles.some(item => !this.canRead(item, { ownPermissionOnly: true }))) {
|
|
1282
1289
|
failed = true;
|
|
1283
1290
|
ignoreCache = true;
|
|
1284
1291
|
this.writeFail(["Unable to transform document", plugin], (0, types_1.errorMessage)(plugin, name, typeof value === 'string' ? "Unsupported access" : "Empty"), { type: 4, startTime });
|
|
@@ -1333,7 +1340,7 @@ class Document extends core_1.Client {
|
|
|
1333
1340
|
series.packageName = plugin;
|
|
1334
1341
|
if (typeof baseConfig === 'function') {
|
|
1335
1342
|
series.baseConfig = outputConfig;
|
|
1336
|
-
if (baseConfig
|
|
1343
|
+
if ((0, types_1.isFunction)(baseConfig, true)) {
|
|
1337
1344
|
next(await baseConfig(context, source, series));
|
|
1338
1345
|
}
|
|
1339
1346
|
else {
|
|
@@ -1386,7 +1393,7 @@ class Document extends core_1.Client {
|
|
|
1386
1393
|
}
|
|
1387
1394
|
else {
|
|
1388
1395
|
transformer = context;
|
|
1389
|
-
context =
|
|
1396
|
+
context = null;
|
|
1390
1397
|
}
|
|
1391
1398
|
if ((0, types_1.isObject)(transformer) && typeof transformer.default === 'function') {
|
|
1392
1399
|
transformer = transformer.default;
|
|
@@ -1405,7 +1412,7 @@ class Document extends core_1.Client {
|
|
|
1405
1412
|
}
|
|
1406
1413
|
}
|
|
1407
1414
|
series.baseConfig = baseConfig;
|
|
1408
|
-
next(await transformer(context, source, series));
|
|
1415
|
+
next(await transformer(context || this, source, series));
|
|
1409
1416
|
}
|
|
1410
1417
|
}
|
|
1411
1418
|
catch (err) {
|
|
@@ -1416,7 +1423,7 @@ class Document extends core_1.Client {
|
|
|
1416
1423
|
}
|
|
1417
1424
|
}
|
|
1418
1425
|
}
|
|
1419
|
-
else {
|
|
1426
|
+
else if (name) {
|
|
1420
1427
|
abortTransform();
|
|
1421
1428
|
if (plugin) {
|
|
1422
1429
|
this.writeFail("Unable to load configuration", (0, types_1.errorMessage)(plugin, name, "Invalid config"), 4);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/document",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Document constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,13 +20,13 @@
|
|
|
20
20
|
"license": "BSD-3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.
|
|
24
|
-
"@e-mc/db": "0.
|
|
25
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/core": "0.12.0",
|
|
24
|
+
"@e-mc/db": "0.12.0",
|
|
25
|
+
"@e-mc/types": "0.12.0",
|
|
26
26
|
"chalk": "4.1.2",
|
|
27
|
-
"domhandler": "
|
|
28
|
-
"domutils": "
|
|
29
|
-
"htmlparser2": "^
|
|
27
|
+
"domhandler": "*",
|
|
28
|
+
"domutils": "*",
|
|
29
|
+
"htmlparser2": "^10.0.0",
|
|
30
30
|
"js-yaml": "^4.1.0",
|
|
31
31
|
"picomatch": "^4.0.2"
|
|
32
32
|
}
|
package/parse/dom.js
CHANGED
|
@@ -20,10 +20,10 @@ class DomWriter extends index_1.XmlWriter {
|
|
|
20
20
|
}
|
|
21
21
|
return !TAG_VOID.includes(tagName);
|
|
22
22
|
}
|
|
23
|
-
static normalize(source,
|
|
24
|
-
let ignoreTagGroup;
|
|
25
|
-
if (
|
|
26
|
-
({ newline, ignoreChar = ''
|
|
23
|
+
static normalize(source, options) {
|
|
24
|
+
let newline, ignoreChar = '', ignoreTagGroup, escapeEntities;
|
|
25
|
+
if (options) {
|
|
26
|
+
({ newline, ignoreTagGroup, escapeEntities, ignoreChar = '' } = options);
|
|
27
27
|
}
|
|
28
28
|
for (const tag of REGEXP_VOID) {
|
|
29
29
|
source = source.replace(tag, (...capture) => index_1.XmlWriter.getNewlineString(capture[1], capture[2], newline));
|
|
@@ -101,20 +101,14 @@ class DomWriter extends index_1.XmlWriter {
|
|
|
101
101
|
static getElementsByTagName(tagName, nodes, recurse, limit) {
|
|
102
102
|
return domutils.getElementsByTagName(tagName, nodes, recurse, limit);
|
|
103
103
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
parser ||= { ...PARSER_OPTIONS };
|
|
104
|
+
documentElement = null;
|
|
105
|
+
ignoreTagName = "title|style|script";
|
|
106
|
+
initOpts;
|
|
107
|
+
rootName = "html";
|
|
108
|
+
ignoreCaseTagName = true;
|
|
109
|
+
constructor(documentName, source, elements, options = {}) {
|
|
110
|
+
const { normalize, escapeEntities, stripComments, ignoreTagGroup, parser = { ...PARSER_OPTIONS } } = options;
|
|
113
111
|
super(documentName, source, elements, { parser });
|
|
114
|
-
this.documentElement = null;
|
|
115
|
-
this.ignoreTagName = "title|style|script";
|
|
116
|
-
this.rootName = "html";
|
|
117
|
-
this.ignoreCaseTagName = true;
|
|
118
112
|
const items = [];
|
|
119
113
|
let outerXml = '', documentElement, offsetMap, startIndex = -1;
|
|
120
114
|
for (const item of elements) {
|
|
@@ -226,10 +220,10 @@ class DomWriter extends index_1.XmlWriter {
|
|
|
226
220
|
}
|
|
227
221
|
exports.DomWriter = DomWriter;
|
|
228
222
|
class HtmlElement extends index_1.XmlElement {
|
|
223
|
+
_documentType = "HTML";
|
|
229
224
|
constructor(documentName, node, attributes, options = {}) {
|
|
230
225
|
options.parser ||= { ...PARSER_OPTIONS };
|
|
231
226
|
super(documentName, node, attributes, { ...options, tagVoid: TAG_VOID.includes(node.tagName) });
|
|
232
|
-
this._documentType = "HTML";
|
|
233
227
|
}
|
|
234
228
|
getTagOffset(source) {
|
|
235
229
|
switch (this.node.append?.tagName || this.tagName) {
|
package/parse/index.js
CHANGED
|
@@ -107,6 +107,9 @@ const isCount = (value) => typeof value === 'number' && value > 0 && value !== I
|
|
|
107
107
|
const hasId = (id, source, startIndex, endIndex) => source.substring(startIndex, endIndex).indexOf(id) !== -1;
|
|
108
108
|
const escapeTagName = (value) => value.replace(/[-.]/g, capture => capture === '-' ? '\\x2d' : '\\' + capture);
|
|
109
109
|
class XmlWriter {
|
|
110
|
+
documentName;
|
|
111
|
+
_source;
|
|
112
|
+
elements;
|
|
110
113
|
static get PATTERN_ATTRNAME() {
|
|
111
114
|
return PATTERN_ATTRNAME;
|
|
112
115
|
}
|
|
@@ -129,12 +132,16 @@ class XmlWriter {
|
|
|
129
132
|
return PATTERN_TRAILINGSPACE;
|
|
130
133
|
}
|
|
131
134
|
static replaceMatch(match, source, content = '', options) {
|
|
135
|
+
let pattern;
|
|
132
136
|
if (typeof content === 'number') {
|
|
133
137
|
content = match[content] || '';
|
|
134
138
|
}
|
|
139
|
+
else if (content instanceof RegExp) {
|
|
140
|
+
pattern = content;
|
|
141
|
+
content = '';
|
|
142
|
+
}
|
|
135
143
|
let index = match.index, leading = index > 0 ? source.substring(0, index) : '', trailing = source.substring(index + match[0].length);
|
|
136
144
|
if (options) {
|
|
137
|
-
let pattern;
|
|
138
145
|
if ((0, types_1.isPlainObject)(options)) {
|
|
139
146
|
if (options.trimLeading) {
|
|
140
147
|
const length = leading.length;
|
|
@@ -144,14 +151,14 @@ class XmlWriter {
|
|
|
144
151
|
if (options.trimTrailing) {
|
|
145
152
|
trailing = trailing.trimStart();
|
|
146
153
|
}
|
|
147
|
-
pattern
|
|
154
|
+
pattern ||= options.pattern;
|
|
148
155
|
}
|
|
149
156
|
else {
|
|
150
157
|
pattern = options;
|
|
151
158
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
159
|
+
}
|
|
160
|
+
if (pattern) {
|
|
161
|
+
pattern.lastIndex = index + content.length;
|
|
155
162
|
}
|
|
156
163
|
return leading + content + trailing;
|
|
157
164
|
}
|
|
@@ -397,16 +404,17 @@ class XmlWriter {
|
|
|
397
404
|
while (match = pattern.exec(source)) {
|
|
398
405
|
if (stripXml) {
|
|
399
406
|
source = this.replaceMatch(match, source, '', { pattern, trimLeading: true });
|
|
400
|
-
continue;
|
|
401
407
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
408
|
+
else {
|
|
409
|
+
const type = match[1] && 'comment' || match[2] && 'cdata' || 'node';
|
|
410
|
+
const startIndex = match.index;
|
|
411
|
+
let endIndex = startIndex + match[0].length;
|
|
412
|
+
if (type !== 'node') {
|
|
413
|
+
result.push({ type, outerXml: match[0], startIndex, endIndex: endIndex - 1 });
|
|
414
|
+
}
|
|
415
|
+
else if ((endIndex = findCloseIndex(source, match[3], endIndex, ignoreCase)[0]) !== -1) {
|
|
416
|
+
result.push({ type, outerXml: source.substring(startIndex, endIndex + 1), startIndex, endIndex });
|
|
417
|
+
}
|
|
410
418
|
}
|
|
411
419
|
}
|
|
412
420
|
if (stripXml) {
|
|
@@ -450,25 +458,26 @@ class XmlWriter {
|
|
|
450
458
|
static isSpace(ch) {
|
|
451
459
|
return ch === ' ' || ch === '\n' || ch === '\t' || ch === '\f' || ch === '\r' || ch === '\v';
|
|
452
460
|
}
|
|
453
|
-
|
|
461
|
+
modifyCount = 0;
|
|
462
|
+
failCount = 0;
|
|
463
|
+
errors = [];
|
|
464
|
+
newline = '\n';
|
|
465
|
+
ignoreTagName = '';
|
|
466
|
+
ignoreTagGroup = undefined;
|
|
467
|
+
parser;
|
|
468
|
+
ignoreCaseTagName = false;
|
|
469
|
+
rootName = '';
|
|
470
|
+
_tagCount = Object.create(null);
|
|
471
|
+
_hasInvalidContent = true;
|
|
472
|
+
_ignoreFlag = 0;
|
|
473
|
+
_appendCount = 0;
|
|
474
|
+
_invalidContent = null;
|
|
475
|
+
#patternIgnore = null;
|
|
476
|
+
#writeStartIndex = -1;
|
|
477
|
+
constructor(documentName, _source, elements, options) {
|
|
454
478
|
this.documentName = documentName;
|
|
479
|
+
this._source = _source;
|
|
455
480
|
this.elements = elements;
|
|
456
|
-
this.modifyCount = 0;
|
|
457
|
-
this.failCount = 0;
|
|
458
|
-
this.errors = [];
|
|
459
|
-
this.newline = '\n';
|
|
460
|
-
this.ignoreTagName = '';
|
|
461
|
-
this.ignoreTagGroup = undefined;
|
|
462
|
-
this.ignoreCaseTagName = false;
|
|
463
|
-
this.rootName = '';
|
|
464
|
-
this._tagCount = Object.create(null);
|
|
465
|
-
this._hasInvalidContent = true;
|
|
466
|
-
this._ignoreFlag = 0;
|
|
467
|
-
this._appendCount = 0;
|
|
468
|
-
this._invalidContent = null;
|
|
469
|
-
this._patternIgnore = null;
|
|
470
|
-
this._writeStartIndex = -1;
|
|
471
|
-
this._source = source;
|
|
472
481
|
this.parser = options?.parser;
|
|
473
482
|
}
|
|
474
483
|
init(offsetMap) {
|
|
@@ -496,7 +505,7 @@ class XmlWriter {
|
|
|
496
505
|
}
|
|
497
506
|
getInvalidArea() {
|
|
498
507
|
if ((this._ignoreFlag & IGNORE_FLAG.INTERIOR) === 0 && this._hasInvalidContent) {
|
|
499
|
-
const startIndex = this
|
|
508
|
+
const startIndex = this.#writeStartIndex;
|
|
500
509
|
if (isIndex(startIndex) && this._invalidContent && !this._invalidContent.some(item => item.startIndex >= startIndex)) {
|
|
501
510
|
return this._invalidContent;
|
|
502
511
|
}
|
|
@@ -626,12 +635,12 @@ class XmlWriter {
|
|
|
626
635
|
this.renameTag(node, element.tagName);
|
|
627
636
|
}
|
|
628
637
|
this.update(node, outerXml, append, element.tagOffset);
|
|
629
|
-
if (element.innerXml && !element.remove && (element.hasModifiedContent() || this
|
|
638
|
+
if (element.innerXml && !element.remove && (element.hasModifiedContent() || this.#patternIgnore?.test(element.tagName))) {
|
|
630
639
|
this._hasInvalidContent = true;
|
|
631
|
-
this
|
|
640
|
+
this.#writeStartIndex = -1;
|
|
632
641
|
}
|
|
633
642
|
else {
|
|
634
|
-
this
|
|
643
|
+
this.#writeStartIndex = node.startIndex;
|
|
635
644
|
}
|
|
636
645
|
element.reset();
|
|
637
646
|
return true;
|
|
@@ -943,7 +952,7 @@ class XmlWriter {
|
|
|
943
952
|
value = value.substring(1);
|
|
944
953
|
}
|
|
945
954
|
this.ignoreTagName = tagName + value;
|
|
946
|
-
this
|
|
955
|
+
this.#patternIgnore = null;
|
|
947
956
|
}
|
|
948
957
|
resetPosition(startIndex = -1) {
|
|
949
958
|
const rootName = this.rootName;
|
|
@@ -1044,7 +1053,7 @@ class XmlWriter {
|
|
|
1044
1053
|
return XmlWriter.getNameOfId(this.documentName);
|
|
1045
1054
|
}
|
|
1046
1055
|
get patternIgnore() {
|
|
1047
|
-
return this.ignoreTagName ? this
|
|
1056
|
+
return this.ignoreTagName ? this.#patternIgnore ||= new RegExp(`^(?:${this.ignoreTagName})$`, this.ignoreCaseTagName ? 'i' : '') : null;
|
|
1048
1057
|
}
|
|
1049
1058
|
set source(value) {
|
|
1050
1059
|
if (value !== this._source) {
|
|
@@ -1061,6 +1070,8 @@ class XmlWriter {
|
|
|
1061
1070
|
}
|
|
1062
1071
|
exports.XmlWriter = XmlWriter;
|
|
1063
1072
|
class XmlElement {
|
|
1073
|
+
documentName;
|
|
1074
|
+
node;
|
|
1064
1075
|
static writeAttributes(attrs, escapeEntities) {
|
|
1065
1076
|
let result = '';
|
|
1066
1077
|
for (const [key, value] of attrs) {
|
|
@@ -1075,24 +1086,24 @@ class XmlElement {
|
|
|
1075
1086
|
}
|
|
1076
1087
|
return result;
|
|
1077
1088
|
}
|
|
1078
|
-
|
|
1089
|
+
newline = '\n';
|
|
1090
|
+
parser;
|
|
1091
|
+
_modified = true;
|
|
1092
|
+
_tagName = '';
|
|
1093
|
+
_nameOfId;
|
|
1094
|
+
_innerXml = '';
|
|
1095
|
+
_remove = false;
|
|
1096
|
+
_tagVoid = null;
|
|
1097
|
+
_prevTagName = null;
|
|
1098
|
+
_prevInnerXml = null;
|
|
1099
|
+
_append = undefined;
|
|
1100
|
+
_tagOffset = undefined;
|
|
1101
|
+
_ignoreCase;
|
|
1102
|
+
_documentType = '';
|
|
1103
|
+
_attributes = new Map();
|
|
1104
|
+
constructor(documentName, node, attributes, { parser, tagVoid } = {}) {
|
|
1079
1105
|
this.documentName = documentName;
|
|
1080
1106
|
this.node = node;
|
|
1081
|
-
this.newline = '\n';
|
|
1082
|
-
this._modified = true;
|
|
1083
|
-
this._tagName = '';
|
|
1084
|
-
this._innerXml = '';
|
|
1085
|
-
this._remove = false;
|
|
1086
|
-
this._tagVoid = null;
|
|
1087
|
-
this._prevTagName = null;
|
|
1088
|
-
this._prevInnerXml = null;
|
|
1089
|
-
this._append = undefined;
|
|
1090
|
-
this._tagOffset = undefined;
|
|
1091
|
-
this._documentType = '';
|
|
1092
|
-
this._attributes = new Map();
|
|
1093
|
-
if ((0, types_1.isObject)(tagVoid)) {
|
|
1094
|
-
({ tagVoid, parser } = tagVoid);
|
|
1095
|
-
}
|
|
1096
1107
|
if (parser) {
|
|
1097
1108
|
this.parser = parser;
|
|
1098
1109
|
}
|
package/parse/types/index.d.ts
CHANGED
|
@@ -150,6 +150,7 @@ export interface XmlWriterConstructor {
|
|
|
150
150
|
readonly PATTERN_QUOTEVALUE: string;
|
|
151
151
|
readonly PATTERN_COMMENT: string;
|
|
152
152
|
readonly PATTERN_TRAILINGSPACE: string;
|
|
153
|
+
replaceMatch(match: RegExpMatchArray, source: string, pattern: RegExp): string;
|
|
153
154
|
replaceMatch(match: RegExpMatchArray, source: string, content: number | string, options: ReplaceMatchOptions): string;
|
|
154
155
|
replaceMatch(match: RegExpMatchArray, source: string, content?: number | string, pattern?: RegExp): string;
|
|
155
156
|
escapeXmlString(value: string, ampersand?: boolean): string;
|
|
@@ -168,8 +169,8 @@ export interface XmlWriterConstructor {
|
|
|
168
169
|
getCommentsAndCDATA(source: string, tagPattern?: string | string[] | [string | undefined, string[] | undefined], ignoreCase?: boolean, stripXml?: boolean): SourceContent[];
|
|
169
170
|
isEqual(node: XmlTagNode, other: XmlTagNode, ignoreCase: boolean): boolean;
|
|
170
171
|
isEqual(node: XmlTagNode, other: XmlTagNode, documentName?: string, ignoreCase?: boolean): boolean;
|
|
171
|
-
isIndex(value:
|
|
172
|
-
isCount(value:
|
|
172
|
+
isIndex(value: unknown): value is number;
|
|
173
|
+
isCount(value: unknown): value is number;
|
|
173
174
|
isSpace(ch: string): boolean;
|
|
174
175
|
readonly prototype: IXmlWriter;
|
|
175
176
|
new(documentName: string, source: string, elements: XmlTagNode[], options?: XmlWriterOptions): IXmlWriter;
|
|
@@ -214,7 +215,6 @@ export interface XmlElementConstructor {
|
|
|
214
215
|
writeAttributes(attrs: AttributeMap | AttributeList, escapeEntities?: boolean): string;
|
|
215
216
|
readonly prototype: IXmlElement;
|
|
216
217
|
new(documentName: string, node: XmlTagNode, attributes?: PlainObject, options?: XmlElementOptions): IXmlElement;
|
|
217
|
-
new(documentName: string, node: XmlTagNode, attributes?: PlainObject, tagVoid?: boolean, parser?: ParserOptions): IXmlElement;
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
export class IDomWriter extends IXmlWriter {
|
|
@@ -224,13 +224,11 @@ export class IDomWriter extends IXmlWriter {
|
|
|
224
224
|
|
|
225
225
|
export interface DomWriterConstructor extends XmlWriterConstructor {
|
|
226
226
|
hasInnerXml(tagName: string, ignoreCase?: boolean): boolean;
|
|
227
|
-
normalize(source: string, options
|
|
228
|
-
normalize(source: string, newline?: string, ignoreChar?: string, escapeEntities?: boolean): string;
|
|
227
|
+
normalize(source: string, options?: NormalizeOptions): string;
|
|
229
228
|
getDocumentElement(source: string, parser?: ParserOptions): ParserResult;
|
|
230
229
|
getElementsByTagName(tagName: string | ((name: string) => boolean), nodes: AnyNode | AnyNode[], recurse?: boolean, limit?: number): Element[];
|
|
231
230
|
readonly prototype: IDomWriter;
|
|
232
231
|
new(documentName: string, source: string, elements: XmlTagNode[], options?: DomWriterOptions): IDomWriter;
|
|
233
|
-
new(documentName: string, source: string, elements: XmlTagNode[], normalize?: boolean | string, escapeEntities?: boolean, parser?: ParserOptions): IDomWriter;
|
|
234
232
|
}
|
|
235
233
|
|
|
236
234
|
export class IHtmlElement extends IXmlElement {}
|
package/transform/index.js
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _a, _b, _c, _d;
|
|
3
2
|
exports.SourceMap = exports.TransformSeries = void 0;
|
|
4
3
|
const path = require("node:path");
|
|
5
4
|
const types_1 = require("@e-mc/types");
|
|
6
5
|
const core_1 = require("@e-mc/core");
|
|
7
|
-
const
|
|
8
|
-
const kMap = Symbol('map');
|
|
9
|
-
const kOut = Symbol('out');
|
|
10
|
-
const kOptions = Symbol('options');
|
|
11
|
-
const kSourceMap = Symbol('sourceMap');
|
|
12
|
-
const kMetadata = Symbol('metadata');
|
|
13
|
-
const kModulesDir = Symbol('modulesDir');
|
|
14
|
-
const kUsername = Symbol('username');
|
|
15
|
-
const kProductionRelease = Symbol('productionRelease');
|
|
6
|
+
const kTransformSeries = Symbol.for('transformseries:constructor');
|
|
16
7
|
function parseMap(data, value) {
|
|
17
8
|
let [mimeType, encoding] = data.split(';');
|
|
18
9
|
if (!encoding && !mimeType.includes('/')) {
|
|
@@ -31,6 +22,11 @@ function toJSON(data) {
|
|
|
31
22
|
}
|
|
32
23
|
}
|
|
33
24
|
class Out {
|
|
25
|
+
sourceFiles;
|
|
26
|
+
ignoreCache;
|
|
27
|
+
messageAppend;
|
|
28
|
+
logAppend;
|
|
29
|
+
logQueued;
|
|
34
30
|
reset() {
|
|
35
31
|
this.sourceFiles = undefined;
|
|
36
32
|
this.ignoreCache = undefined;
|
|
@@ -40,30 +36,37 @@ class Out {
|
|
|
40
36
|
}
|
|
41
37
|
}
|
|
42
38
|
class TransformSeries extends core_1.Module {
|
|
39
|
+
type;
|
|
40
|
+
static [kTransformSeries] = true;
|
|
41
|
+
baseConfig = {};
|
|
42
|
+
outputConfig = {};
|
|
43
|
+
supplementChunks = [];
|
|
44
|
+
_threadable = true;
|
|
45
|
+
#code;
|
|
46
|
+
#modulesDir = '';
|
|
47
|
+
#username = '';
|
|
48
|
+
#sourceMap;
|
|
49
|
+
#out;
|
|
50
|
+
#options;
|
|
51
|
+
#metadata = {};
|
|
52
|
+
#productionRelease;
|
|
43
53
|
constructor(type, code, options) {
|
|
44
54
|
super();
|
|
45
55
|
this.type = type;
|
|
46
|
-
this
|
|
47
|
-
this
|
|
48
|
-
this
|
|
49
|
-
this
|
|
50
|
-
this[_a] = {};
|
|
51
|
-
this[_b] = '';
|
|
52
|
-
this[_c] = '';
|
|
53
|
-
this[kCode] = code;
|
|
54
|
-
this[kOut] = new Out();
|
|
55
|
-
this[kOptions] = options;
|
|
56
|
-
this[kProductionRelease] = !!options.productionRelease;
|
|
56
|
+
this.#code = code;
|
|
57
|
+
this.#out = new Out();
|
|
58
|
+
this.#options = options;
|
|
59
|
+
this.#productionRelease = !!options.productionRelease;
|
|
57
60
|
if (SourceMap.isRaw(options.sourceMap)) {
|
|
58
61
|
const series = new SourceMap(code);
|
|
59
62
|
series.map = options.sourceMap;
|
|
60
|
-
this
|
|
63
|
+
this.#sourceMap = series;
|
|
61
64
|
}
|
|
62
65
|
else {
|
|
63
|
-
this
|
|
66
|
+
this.#sourceMap = options.sourceMap instanceof SourceMap ? options.sourceMap : this.createSourceMap(code);
|
|
64
67
|
}
|
|
65
68
|
if (options.metadata) {
|
|
66
|
-
this
|
|
69
|
+
this.#metadata = (0, types_1.isPlainObject)(options.metadata) ? { ...options.metadata } : options.metadata;
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
reset() {
|
|
@@ -72,21 +75,21 @@ class TransformSeries extends core_1.Module {
|
|
|
72
75
|
this.outputConfig = {};
|
|
73
76
|
this.version = '';
|
|
74
77
|
this.packageName = '';
|
|
75
|
-
this
|
|
78
|
+
this.#out.reset();
|
|
76
79
|
}
|
|
77
80
|
init(instance, dirname) {
|
|
78
|
-
if (instance.
|
|
81
|
+
if (instance.hasPermission('fs')) {
|
|
79
82
|
this.permission = core_1.Permission.clone(instance.permission, true);
|
|
80
83
|
}
|
|
81
84
|
this.abortable = instance.abortable;
|
|
82
85
|
this._moduleName = instance.moduleName + ':transform:' + this.type;
|
|
83
86
|
if (dirname) {
|
|
84
|
-
this
|
|
87
|
+
this.#modulesDir = dirname;
|
|
85
88
|
}
|
|
86
89
|
const host = instance.host;
|
|
87
90
|
if (host) {
|
|
88
91
|
host.retain(this);
|
|
89
|
-
this
|
|
92
|
+
this.#username = host.username;
|
|
90
93
|
}
|
|
91
94
|
core_1.AbortComponent.attach(instance, this.signal);
|
|
92
95
|
return this;
|
|
@@ -104,6 +107,13 @@ class TransformSeries extends core_1.Module {
|
|
|
104
107
|
return this.options.getSourceFiles(imports);
|
|
105
108
|
}
|
|
106
109
|
}
|
|
110
|
+
getTempDir(pathname, filename, createDir) {
|
|
111
|
+
if (arguments.length === 0) {
|
|
112
|
+
pathname = this.packageName || true;
|
|
113
|
+
createDir = true;
|
|
114
|
+
}
|
|
115
|
+
return super.getTempDir(pathname, filename, createDir);
|
|
116
|
+
}
|
|
107
117
|
toBaseConfig(all = true) {
|
|
108
118
|
const { outputConfig, external } = this;
|
|
109
119
|
const baseConfig = this.baseConfig;
|
|
@@ -163,25 +173,25 @@ class TransformSeries extends core_1.Module {
|
|
|
163
173
|
this.host?.release(this);
|
|
164
174
|
}
|
|
165
175
|
set code(value) {
|
|
166
|
-
this
|
|
176
|
+
this.#code = value;
|
|
167
177
|
}
|
|
168
178
|
get code() {
|
|
169
|
-
return this
|
|
179
|
+
return this.#code;
|
|
170
180
|
}
|
|
171
181
|
get sourceMap() {
|
|
172
|
-
return this
|
|
182
|
+
return this.#sourceMap;
|
|
173
183
|
}
|
|
174
184
|
get metadata() {
|
|
175
|
-
return this
|
|
185
|
+
return this.#metadata;
|
|
176
186
|
}
|
|
177
187
|
get out() {
|
|
178
|
-
return this
|
|
188
|
+
return this.#out;
|
|
179
189
|
}
|
|
180
190
|
get options() {
|
|
181
|
-
return this
|
|
191
|
+
return this.#options;
|
|
182
192
|
}
|
|
183
193
|
get productionRelease() {
|
|
184
|
-
return this
|
|
194
|
+
return this.#productionRelease;
|
|
185
195
|
}
|
|
186
196
|
get imported() {
|
|
187
197
|
return !!this.options.imported;
|
|
@@ -224,7 +234,7 @@ class TransformSeries extends core_1.Module {
|
|
|
224
234
|
}
|
|
225
235
|
get packageVersion() {
|
|
226
236
|
const packageName = this.packageName;
|
|
227
|
-
return packageName ? core_1.Module.getPackageVersion(packageName, this
|
|
237
|
+
return packageName ? core_1.Module.getPackageVersion(packageName, { startDir: this.#modulesDir }) : '0.0.0';
|
|
228
238
|
}
|
|
229
239
|
set host(value) {
|
|
230
240
|
this._host = value;
|
|
@@ -233,15 +243,17 @@ class TransformSeries extends core_1.Module {
|
|
|
233
243
|
return this._host;
|
|
234
244
|
}
|
|
235
245
|
get username() {
|
|
236
|
-
return this
|
|
246
|
+
return this.#username;
|
|
237
247
|
}
|
|
238
248
|
}
|
|
239
249
|
exports.TransformSeries = TransformSeries;
|
|
240
|
-
_a = kMetadata, _b = kModulesDir, _c = kUsername;
|
|
241
250
|
class SourceMap {
|
|
251
|
+
static get SOURCE_MAPPING_URL() {
|
|
252
|
+
return /[\r\n]*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\r?\n?/g;
|
|
253
|
+
}
|
|
242
254
|
static findSourceMap(code, uri) {
|
|
243
255
|
if (code ||= uri && core_1.Module.readText(uri)) {
|
|
244
|
-
const pattern = this.
|
|
256
|
+
const pattern = this.SOURCE_MAPPING_URL;
|
|
245
257
|
pattern.lastIndex = 0;
|
|
246
258
|
let match;
|
|
247
259
|
while (match = pattern.exec(code)) {
|
|
@@ -261,7 +273,7 @@ class SourceMap {
|
|
|
261
273
|
}
|
|
262
274
|
}
|
|
263
275
|
static removeSourceMappingURL(value) {
|
|
264
|
-
const pattern = this.
|
|
276
|
+
const pattern = this.SOURCE_MAPPING_URL;
|
|
265
277
|
pattern.lastIndex = 0;
|
|
266
278
|
const match = pattern.exec(value);
|
|
267
279
|
return match ? [value.substring(0, match.index) + value.substring(match.index + match[0].length), (match[3] || '') + match[4], match[3] && toJSON(parseMap(match[3], match[4])) || null] : [value];
|
|
@@ -269,10 +281,12 @@ class SourceMap {
|
|
|
269
281
|
static isRaw(map) {
|
|
270
282
|
return (0, types_1.isObject)(map) && (0, types_1.isString)(map.mappings);
|
|
271
283
|
}
|
|
284
|
+
code;
|
|
285
|
+
output = new Map();
|
|
286
|
+
sourceMappingURL = '';
|
|
287
|
+
#map = undefined;
|
|
288
|
+
#start;
|
|
272
289
|
constructor(code, uri, remove) {
|
|
273
|
-
this.output = new Map();
|
|
274
|
-
this.sourceMappingURL = '';
|
|
275
|
-
this[_d] = undefined;
|
|
276
290
|
if (typeof uri === 'boolean') {
|
|
277
291
|
remove = uri;
|
|
278
292
|
uri = undefined;
|
|
@@ -288,12 +302,12 @@ class SourceMap {
|
|
|
288
302
|
if (SourceMap.isRaw(map)) {
|
|
289
303
|
this.nextMap('unknown', code, map);
|
|
290
304
|
}
|
|
291
|
-
this
|
|
305
|
+
this.#start = code;
|
|
292
306
|
this.code = code;
|
|
293
307
|
}
|
|
294
308
|
reset(restore) {
|
|
295
309
|
if (restore) {
|
|
296
|
-
this.code = this
|
|
310
|
+
this.code = this.#start;
|
|
297
311
|
}
|
|
298
312
|
this.map = undefined;
|
|
299
313
|
this.sourceMappingURL = '';
|
|
@@ -302,7 +316,7 @@ class SourceMap {
|
|
|
302
316
|
nextMap(name, code, map, sourceMappingURL = '', emptySources) {
|
|
303
317
|
if (!SourceMap.isRaw(map)) {
|
|
304
318
|
try {
|
|
305
|
-
map = JSON.parse(Buffer.isBuffer(map) ? map.toString('
|
|
319
|
+
map = JSON.parse(Buffer.isBuffer(map) ? map.toString('utf8') : map.toString());
|
|
306
320
|
}
|
|
307
321
|
catch {
|
|
308
322
|
}
|
|
@@ -310,6 +324,10 @@ class SourceMap {
|
|
|
310
324
|
return false;
|
|
311
325
|
}
|
|
312
326
|
}
|
|
327
|
+
if (typeof sourceMappingURL === 'boolean') {
|
|
328
|
+
emptySources = sourceMappingURL;
|
|
329
|
+
sourceMappingURL = '';
|
|
330
|
+
}
|
|
313
331
|
if (emptySources) {
|
|
314
332
|
map.sources = [""];
|
|
315
333
|
}
|
|
@@ -327,16 +345,14 @@ class SourceMap {
|
|
|
327
345
|
}
|
|
328
346
|
set map(value) {
|
|
329
347
|
if (!value) {
|
|
330
|
-
this
|
|
348
|
+
this.#map = undefined;
|
|
331
349
|
}
|
|
332
350
|
else if (SourceMap.isRaw(value)) {
|
|
333
|
-
this
|
|
351
|
+
this.#map = value;
|
|
334
352
|
}
|
|
335
353
|
}
|
|
336
354
|
get map() {
|
|
337
|
-
return this
|
|
355
|
+
return this.#map;
|
|
338
356
|
}
|
|
339
357
|
}
|
|
340
358
|
exports.SourceMap = SourceMap;
|
|
341
|
-
_d = kMap;
|
|
342
|
-
SourceMap.RE_SOURCE_MAPPING_URL = /[\r\n]*(?:(\/\/)|(\/\*))\s*[#@][ ]+sourceMappingURL=(data:[^,]+,)?(\S+)\s*(\*\/)?\r?\n?/g;
|
package/util.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
declare namespace util {
|
|
2
|
-
/** @deprecated Types.IMPORT_MAP */
|
|
3
|
-
const IMPORTS: Record<string, string | undefined>;
|
|
4
2
|
const PATTERN_PARENTHESIS: string;
|
|
5
|
-
function loadPlugins<T = unknown>(plugins: string | unknown[]): T[];
|
|
3
|
+
function loadPlugins<T = unknown>(plugins: string | unknown[], map?: AnyObject): T[];
|
|
6
4
|
function replaceAll(source: string, valueOf: (name: string) => string, opening?: string, closing?: string): string;
|
|
7
5
|
function concatString(values: string[] | string | undefined, newline?: string): string;
|
|
8
6
|
function spliceString(source: string, startIndex: number, endIndex: number, content: string, pattern?: RegExp): string;
|
|
9
7
|
function spliceMatch(source: string, match: RegExpExecArray, content: string, pattern?: RegExp): string;
|
|
10
8
|
function splitEnclosing(value: string, pattern?: string | RegExp, options?: { trim?: boolean; start?: number; startWith?: number; count?: number } | boolean | number, opening?: string, closing?: string): string[];
|
|
9
|
+
function trimQuote(value: string): string;
|
|
10
|
+
function wrapQuote(value: string, quote?: string): string;
|
|
11
11
|
function appendSuffix(filename: string, value: string, separator?: string): string;
|
|
12
12
|
function getIndent(value: string, spaces?: number): string;
|
|
13
13
|
function getNewline(value: string): string;
|
package/util.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.isObject = exports.
|
|
2
|
+
exports.isObject = exports.PATTERN_PARENTHESIS = void 0;
|
|
3
3
|
exports.loadPlugins = loadPlugins;
|
|
4
4
|
exports.replaceAll = replaceAll;
|
|
5
5
|
exports.concatString = concatString;
|
|
6
6
|
exports.spliceString = spliceString;
|
|
7
7
|
exports.spliceMatch = spliceMatch;
|
|
8
8
|
exports.splitEnclosing = splitEnclosing;
|
|
9
|
+
exports.trimQuote = trimQuote;
|
|
10
|
+
exports.wrapQuote = wrapQuote;
|
|
9
11
|
exports.appendSuffix = appendSuffix;
|
|
10
12
|
exports.getIndent = getIndent;
|
|
11
13
|
exports.getNewline = getNewline;
|
|
@@ -13,11 +15,10 @@ exports.getHashData = getHashData;
|
|
|
13
15
|
exports.hasValue = hasValue;
|
|
14
16
|
exports.getModuleName = getModuleName;
|
|
15
17
|
exports.removeInternalProperties = removeInternalProperties;
|
|
16
|
-
const path = require("path");
|
|
18
|
+
const path = require("node:path");
|
|
17
19
|
const types_1 = require("@e-mc/types");
|
|
18
|
-
Object.defineProperty(exports, "IMPORTS", { enumerable: true, get: function () { return types_1.IMPORT_MAP; } });
|
|
19
20
|
Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return types_1.isObject; } });
|
|
20
|
-
function loadPlugins(plugins) {
|
|
21
|
+
function loadPlugins(plugins, map) {
|
|
21
22
|
const result = [];
|
|
22
23
|
const items = (Array.isArray(plugins) ? plugins : [plugins]).map(value => {
|
|
23
24
|
let name, options;
|
|
@@ -33,7 +34,11 @@ function loadPlugins(plugins) {
|
|
|
33
34
|
else if (typeof value === 'function') {
|
|
34
35
|
return [value];
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
+
if (name) {
|
|
38
|
+
const method = map?.[name];
|
|
39
|
+
return [typeof method === 'function' ? method : name, options];
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
37
42
|
});
|
|
38
43
|
for (const plugin of items) {
|
|
39
44
|
if (plugin) {
|
|
@@ -178,6 +183,15 @@ function splitEnclosing(value, pattern, options, opening = '(', closing = ')') {
|
|
|
178
183
|
}
|
|
179
184
|
return result;
|
|
180
185
|
}
|
|
186
|
+
function trimQuote(value) {
|
|
187
|
+
value = value.trim();
|
|
188
|
+
const first = value[0];
|
|
189
|
+
const last = value[value.length - 1];
|
|
190
|
+
return first === last && (first === '"' || first === "'") ? value.substring(1, value.length - 1).trim() : value;
|
|
191
|
+
}
|
|
192
|
+
function wrapQuote(value, quote = '"') {
|
|
193
|
+
return quote + value.replace(new RegExp(`(?<!\\\\)${quote}`, 'g'), `\\${quote}`) + quote;
|
|
194
|
+
}
|
|
181
195
|
function appendSuffix(filename, value, separator = '-') {
|
|
182
196
|
const ext = path.extname(filename);
|
|
183
197
|
return ext && value ? filename.substring(0, filename.length - ext.length) + separator + value + ext : filename;
|
|
@@ -212,7 +226,7 @@ function hasValue(target, ...values) {
|
|
|
212
226
|
return false;
|
|
213
227
|
}
|
|
214
228
|
function getModuleName(err) {
|
|
215
|
-
const match = err instanceof Error && /Cannot find module '([^']+)'/.exec(err.message);
|
|
229
|
+
const match = err instanceof Error && /Cannot find (?:module|package) '([^']+)'/.exec(err.message);
|
|
216
230
|
if (match) {
|
|
217
231
|
return match[1];
|
|
218
232
|
}
|
|
@@ -220,7 +234,7 @@ function getModuleName(err) {
|
|
|
220
234
|
function removeInternalProperties(value, retaining) {
|
|
221
235
|
if ((0, types_1.isObject)(value)) {
|
|
222
236
|
for (const prop in value) {
|
|
223
|
-
if (Object.
|
|
237
|
+
if (Object.hasOwn(value, prop)) {
|
|
224
238
|
if (retaining) {
|
|
225
239
|
if (!retaining.includes(prop)) {
|
|
226
240
|
delete value[prop];
|