@e-mc/document 0.8.6 → 0.9.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/LICENSE +10 -10
- package/README.md +131 -17
- package/asset.d.ts +7 -7
- package/asset.js +0 -1
- package/index.d.ts +4 -4
- package/index.js +90 -97
- package/package.json +5 -5
- package/parse/dom.js +0 -2
- package/parse/index.js +1 -2
- package/parse/types/index.d.ts +2 -2
- package/transform/index.d.ts +7 -7
- package/transform/index.js +0 -1
- package/util.js +0 -1
package/LICENSE
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Copyright 2024 An Pham
|
|
2
|
-
|
|
3
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
-
|
|
5
|
-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
-
|
|
7
|
-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
-
|
|
9
|
-
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
-
|
|
1
|
+
Copyright 2024 An Pham
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
11
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @e-mc/document
|
|
2
2
|
|
|
3
|
-
* NodeJS 14
|
|
3
|
+
* NodeJS 14/16
|
|
4
4
|
* ES2020
|
|
5
5
|
|
|
6
6
|
## General Usage
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.9.0/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { DataSource, ViewEngine } from "./squared";
|
|
@@ -31,20 +31,22 @@ interface IDocument extends IClient<IFileManager, DocumentModule, TransformCallb
|
|
|
31
31
|
customize(options?: CustomizeOptions): void;
|
|
32
32
|
findConfig(data: object, name: string, type?: string): PluginConfig;
|
|
33
33
|
loadConfig(data: object, name: string): ConfigOrTransformer | null | undefined;
|
|
34
|
-
asSourceFile(value: string,
|
|
34
|
+
asSourceFile(value: string, cache: boolean): unknown;
|
|
35
|
+
asSourceFile(value: string, options?: AsSourceFileOptions): unknown;
|
|
35
36
|
findVersion(name: string | string[], fallback?: string): string;
|
|
36
37
|
findSourceScope(uri: string, imports: Record<string, unknown>): Record<string, string | undefined>[];
|
|
37
38
|
findSourceRoot(uri: string, imports?: Record<string, string | undefined>): string | undefined;
|
|
38
39
|
resolveDir(name: string, ...paths: string[]): string | undefined;
|
|
39
|
-
locateSourceFiles(file: ExternalAsset, code?: string, bundleContent?: string[]): (imports?: Record<string, string | undefined>) => SourceInput | undefined;
|
|
40
|
-
resolveSourceFile(file: ExternalAsset): (code?: string, imports?: Record<string, string | undefined>) => SourceInput<string> | undefined;
|
|
40
|
+
locateSourceFiles(file: ExternalAsset, code?: string, bundleContent?: string[]): ((imports?: Record<string, string | undefined>) => SourceInput | undefined);
|
|
41
|
+
resolveSourceFile(file: ExternalAsset): ((code?: string, imports?: Record<string, string | undefined>) => SourceInput<string> | undefined);
|
|
41
42
|
tryParse(source: string, format: string, options?: Record<string | number | symbol, unknown>): unknown;
|
|
42
43
|
forDb(item: DataSource): boolean;
|
|
43
44
|
hasEval(name: string): boolean;
|
|
44
45
|
settingsOf(name: keyof DocumentComponent, option: keyof DocumentComponentOption): unknown;
|
|
45
46
|
parseTemplate(viewEngine: ViewEngine | string, template: string, data: unknown[]): Promise<string | null>;
|
|
46
47
|
transform(type: string, code: string, format: string | string[], options?: TransformOutput & TransformAction): Promise<TransformResult | void>;
|
|
47
|
-
abort(
|
|
48
|
+
abort(err: Error): void;
|
|
49
|
+
abort(name?: keyof DocumentComponent, reason?: unknown): void;
|
|
48
50
|
restart(): void;
|
|
49
51
|
using?(data: IFileThread): Promise<unknown>;
|
|
50
52
|
setLocalUri?(file: ExternalAsset, replace?: boolean): void;
|
|
@@ -72,10 +74,9 @@ interface DocumentConstructor extends ModuleConstructor {
|
|
|
72
74
|
createSourceMap(code: string, remove: boolean): SourceMap;
|
|
73
75
|
createSourceMap(code: string, uri?: string, remove?: boolean): SourceMap;
|
|
74
76
|
writeSourceMap(uri: string, data: SourceCode, options?: SourceMapOptions): string | undefined;
|
|
75
|
-
updateGradle(source: string, namespaces: string[], value: string,
|
|
77
|
+
updateGradle(source: string, namespaces: string[], value: string, upgrade: boolean): string;
|
|
78
|
+
updateGradle(source: string, namespaces: string[], value: string, options?: UpdateGradleOptions): string;
|
|
76
79
|
generateLintTable(messages: LintMessage[], options: GenerateLintTableOptions): LogComponent[];
|
|
77
|
-
/* @deprecated - IDocument.using */
|
|
78
|
-
using?(this: IFileManager, instance: IDocument, file: ExternalAsset): Promise<unknown>;
|
|
79
80
|
cleanup?(this: IFileManager, instance: IDocument): Promise<unknown>;
|
|
80
81
|
sanitizeAssets?(assets: ExternalAsset[], exclusions?: ExternalAsset[]): ExternalAsset[];
|
|
81
82
|
readonly prototype: IDocument;
|
|
@@ -83,16 +84,129 @@ interface DocumentConstructor extends ModuleConstructor {
|
|
|
83
84
|
}
|
|
84
85
|
```
|
|
85
86
|
|
|
87
|
+
## Settings
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import type { PermittedDirectories } from "./core";
|
|
91
|
+
import type { DbModule, DbSettings, DocumentComponentOptions, PurgeComponent } from "./settings";
|
|
92
|
+
|
|
93
|
+
interface DocumentModule {
|
|
94
|
+
// handler: "@pi-r/chrome";
|
|
95
|
+
extensions?: string[];
|
|
96
|
+
db?: DbModule<DbSettings>;
|
|
97
|
+
eval?: {
|
|
98
|
+
function?: boolean;
|
|
99
|
+
absolute?: boolean;
|
|
100
|
+
template?: boolean;
|
|
101
|
+
userconfig?: boolean;
|
|
102
|
+
};
|
|
103
|
+
format?: {
|
|
104
|
+
uuid?: {
|
|
105
|
+
dictionary?: string;
|
|
106
|
+
pathname?: string;
|
|
107
|
+
filename?: string;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
imports?: StringMap;
|
|
111
|
+
settings?: {
|
|
112
|
+
broadcast_id?: string | string[];
|
|
113
|
+
users?: Record<string, {
|
|
114
|
+
extensions?: string[] | null;
|
|
115
|
+
imports?: StringMap;
|
|
116
|
+
imports_strict?: boolean;
|
|
117
|
+
pages?: unknown;
|
|
118
|
+
transform?: unknown;
|
|
119
|
+
view_engine?: unknown;
|
|
120
|
+
}>;
|
|
121
|
+
cache_dir?: string;
|
|
122
|
+
imports_strict?: boolean;
|
|
123
|
+
directory?: {
|
|
124
|
+
template?: string;
|
|
125
|
+
data?: string;
|
|
126
|
+
export?: string;
|
|
127
|
+
schema?: string;
|
|
128
|
+
package?: string;
|
|
129
|
+
};
|
|
130
|
+
purge?: PurgeComponent;
|
|
131
|
+
options?: DocumentComponentOptions<boolean | number>;
|
|
132
|
+
pages?: Record<string, Record<string, unknown>>;
|
|
133
|
+
transform?: {
|
|
134
|
+
html?: Record<string, Record<string, unknown>>;
|
|
135
|
+
css?: Record<string, Record<string, unknown>>;
|
|
136
|
+
js?: Record<string, Record<string, unknown>>;
|
|
137
|
+
};
|
|
138
|
+
view_engine?: Record<string, Record<string, unknown>>;
|
|
139
|
+
export?: Record<string, string | (...args: unknown[]) => unknown>;
|
|
140
|
+
};
|
|
141
|
+
permission?: PermittedDirectories;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Example usage
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
const Document = require("@e-mc/document"); // @pi-r/chrome
|
|
149
|
+
|
|
150
|
+
const assets = [
|
|
151
|
+
{ pathname: "output", filename: "image1.png", uri: "http://hostname/path/document1.png" },
|
|
152
|
+
{ pathname: "output", filename: "image2.png", uri: "http://hostname/path/document2.png" }
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
const instance = new Document({
|
|
156
|
+
eval: {
|
|
157
|
+
function: true,
|
|
158
|
+
template: true
|
|
159
|
+
},
|
|
160
|
+
imports: {
|
|
161
|
+
"http://hostname/path/": "build/"
|
|
162
|
+
},
|
|
163
|
+
settings: {
|
|
164
|
+
imports_strict: true,
|
|
165
|
+
users: {
|
|
166
|
+
"nodejs-001": {
|
|
167
|
+
imports_strict: false,
|
|
168
|
+
imports: {
|
|
169
|
+
"http://hostname/path": "build"
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
directory: {
|
|
174
|
+
template: "../chrome/template" // ../chrome/template/users/nodejs-001
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
// instance.host = new Host();
|
|
179
|
+
instance.init(assets);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
NOTE: **@e-mc/document** is an abstract base class and cannot be instantiated. **Document** is more commonly called through [@pi-r/chrome](https://www.npmjs.com/package/@pi-r/chrome).
|
|
183
|
+
|
|
184
|
+
## NodeJS 14 LTS
|
|
185
|
+
|
|
186
|
+
Any optional fail safe dependencies were removed as of `E-mc 0.9`. The code itself will still be *ES2020* and will continue to work equivalently when self-installing these dependencies:
|
|
187
|
+
|
|
188
|
+
### Under 15.4 + 16.0
|
|
189
|
+
|
|
190
|
+
```sh
|
|
191
|
+
npm i abort-controller event-target-shim
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Under 14.17 + 15.6
|
|
195
|
+
|
|
196
|
+
```sh
|
|
197
|
+
npm i uuid
|
|
198
|
+
```
|
|
199
|
+
|
|
86
200
|
## References
|
|
87
201
|
|
|
88
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
89
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
90
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
91
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
92
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
93
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
94
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
95
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
202
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/squared.d.ts
|
|
203
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/asset.d.ts
|
|
204
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/core.d.ts
|
|
205
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/document.d.ts
|
|
206
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/filemanager.d.ts
|
|
207
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/logger.d.ts
|
|
208
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/settings.d.ts
|
|
209
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/watch.d.ts
|
|
96
210
|
|
|
97
211
|
## LICENSE
|
|
98
212
|
|
package/asset.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { ExternalAsset, InitialValue } from '../types/lib/asset';
|
|
2
|
-
|
|
3
|
-
declare namespace asset {
|
|
4
|
-
function isEqual(item: ExternalAsset, other: ExternalAsset): boolean;
|
|
5
|
-
function setInitialValue(file: ExternalAsset, cacheable?: boolean): InitialValue;
|
|
6
|
-
}
|
|
7
|
-
|
|
1
|
+
import type { ExternalAsset, InitialValue } from '../types/lib/asset';
|
|
2
|
+
|
|
3
|
+
declare namespace asset {
|
|
4
|
+
function isEqual(item: ExternalAsset, other: ExternalAsset): boolean;
|
|
5
|
+
function setInitialValue(file: ExternalAsset, cacheable?: boolean): InitialValue;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
8
|
export = asset;
|
package/asset.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
exports.setInitialValue = exports.isEqual = void 0;
|
|
4
3
|
function isEqual(item, other) {
|
|
5
4
|
return !!(item.id && item.id === other.id || item.uri === other.uri && item.pathname === other.pathname && item.filename === other.filename && item.pathname && item.filename);
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DocumentConstructor, IFileManager } from '../types/lib';
|
|
2
|
-
|
|
3
|
-
declare const Document: DocumentConstructor<IFileManager>;
|
|
4
|
-
|
|
1
|
+
import type { DocumentConstructor, IFileManager } from '../types/lib';
|
|
2
|
+
|
|
3
|
+
declare const Document: DocumentConstructor<IFileManager>;
|
|
4
|
+
|
|
5
5
|
export = Document;
|
package/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
const path = require("path");
|
|
4
3
|
const fs = require("fs");
|
|
5
4
|
const pm = require("picomatch");
|
|
@@ -34,6 +33,48 @@ function deleteTransform(map, key, timeout) {
|
|
|
34
33
|
delete map[key];
|
|
35
34
|
--CACHE_TOTAL;
|
|
36
35
|
}
|
|
36
|
+
function resetTransform(config, map, key, stored) {
|
|
37
|
+
if (config.renew) {
|
|
38
|
+
if (stored.timeout) {
|
|
39
|
+
clearTimeout(stored.timeout);
|
|
40
|
+
}
|
|
41
|
+
stored.timeout = renewTransform(map, key, config.expires);
|
|
42
|
+
}
|
|
43
|
+
stored.lastAccessed = Date.now();
|
|
44
|
+
}
|
|
45
|
+
function getTransformData(stored, key, encoding) {
|
|
46
|
+
const location = stored.format[key];
|
|
47
|
+
if (typeof location === 'string') {
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(fs.readFileSync(location, encoding));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
fs.unlink(location, () => { });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return location;
|
|
57
|
+
}
|
|
58
|
+
function setTransformData(type, cacheDir, data) {
|
|
59
|
+
let pathname;
|
|
60
|
+
if (typeof cacheDir === 'string' && core_1.Client.createDir(pathname = path.join(cacheDir, 'transform', type))) {
|
|
61
|
+
try {
|
|
62
|
+
fs.writeFileSync(pathname = path.join(pathname, (0, types_1.incrementUUID)()), JSON.stringify(data));
|
|
63
|
+
return pathname;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
function hasTransformType(config, type, name, data) {
|
|
71
|
+
const include = config?.include[type];
|
|
72
|
+
if (include === true || include?.includes(name)) {
|
|
73
|
+
const exclude = config.exclude[type];
|
|
74
|
+
return !exclude || exclude === true && include !== true || Array.isArray(exclude) && !exclude.includes(name);
|
|
75
|
+
}
|
|
76
|
+
return data?.override === true;
|
|
77
|
+
}
|
|
37
78
|
function getSourceMappingURL(value, css) {
|
|
38
79
|
if (value.includes(' ')) {
|
|
39
80
|
value = encodeURIComponent(value);
|
|
@@ -91,7 +132,7 @@ class Document extends core_1.Client {
|
|
|
91
132
|
static async finalize(instance) {
|
|
92
133
|
for (const ext of instance.extensions) {
|
|
93
134
|
if (instance.aborted) {
|
|
94
|
-
return
|
|
135
|
+
return (0, types_1.createAbortError)(true);
|
|
95
136
|
}
|
|
96
137
|
try {
|
|
97
138
|
const args = [instance];
|
|
@@ -218,7 +259,7 @@ class Document extends core_1.Client {
|
|
|
218
259
|
let match;
|
|
219
260
|
while (match = pattern.exec(source)) {
|
|
220
261
|
const preceding = source.substring(lastIndex, match.index);
|
|
221
|
-
let opening = 0, closing =
|
|
262
|
+
let opening = 0, closing = 0;
|
|
222
263
|
for (let j = 0, q = preceding.length; j < q; ++j) {
|
|
223
264
|
switch (preceding[j]) {
|
|
224
265
|
case '{':
|
|
@@ -229,8 +270,11 @@ class Document extends core_1.Client {
|
|
|
229
270
|
break;
|
|
230
271
|
}
|
|
231
272
|
}
|
|
232
|
-
if (opening === closing
|
|
233
|
-
|
|
273
|
+
if (opening === closing) {
|
|
274
|
+
if (!multiple || match[1] && sanitizeValue(match[1]) === sanitizeValue(local[3])) {
|
|
275
|
+
return upgrade ? parse_1.XmlWriter.replaceMatch(match, source, value, pattern) : source;
|
|
276
|
+
}
|
|
277
|
+
lastIndex = match.index + match[0].length;
|
|
234
278
|
}
|
|
235
279
|
}
|
|
236
280
|
return spliceSource(source, lastIndex, lastIndex, ident.repeat(length) + value + newline);
|
|
@@ -311,7 +355,6 @@ class Document extends core_1.Client {
|
|
|
311
355
|
constructor(data) {
|
|
312
356
|
super(data);
|
|
313
357
|
this.Db = null;
|
|
314
|
-
this.config = {};
|
|
315
358
|
this._assets = null;
|
|
316
359
|
this._dataSource = null;
|
|
317
360
|
this._transformConfig = null;
|
|
@@ -388,7 +431,7 @@ class Document extends core_1.Client {
|
|
|
388
431
|
const handler = db.handler;
|
|
389
432
|
const database = this.dataSource.filter(this.forDb.bind(this));
|
|
390
433
|
let instance;
|
|
391
|
-
if (
|
|
434
|
+
if (handler && handler !== "@e-mc/db") {
|
|
392
435
|
try {
|
|
393
436
|
const Handler = require(handler);
|
|
394
437
|
if (isFunction(Handler) && Handler.prototype instanceof core_1.ClientDb) {
|
|
@@ -403,7 +446,7 @@ class Document extends core_1.Client {
|
|
|
403
446
|
}
|
|
404
447
|
}
|
|
405
448
|
else {
|
|
406
|
-
instance = new db_1
|
|
449
|
+
instance = new db_1(db, database);
|
|
407
450
|
}
|
|
408
451
|
if (instance) {
|
|
409
452
|
const host = this.host;
|
|
@@ -780,7 +823,7 @@ class Document extends core_1.Client {
|
|
|
780
823
|
if (Document.isFile(href, 'unc') || path.isAbsolute(href)) {
|
|
781
824
|
sourceFile.push([href]);
|
|
782
825
|
}
|
|
783
|
-
else if (
|
|
826
|
+
else if (Object.keys(imports).length) {
|
|
784
827
|
const bundleId = file.bundleId;
|
|
785
828
|
const assets = !(0, types_1.isEmpty)(bundleId) ? this.assets.filter(item => item.bundleId === bundleId).sort((a, b) => a.bundleIndex - b.bundleIndex) : [file];
|
|
786
829
|
if (!Array.isArray(bundleContent) || bundleContent.length !== assets.length - 1) {
|
|
@@ -800,7 +843,7 @@ class Document extends core_1.Client {
|
|
|
800
843
|
if (this.resolveUri && (0, types_1.isArray)(file.trailingContent)) {
|
|
801
844
|
let trailing;
|
|
802
845
|
[source, trailing] = this.resolveUri(file, source, (0, util_1.concatString)(file.trailingContent));
|
|
803
|
-
if (
|
|
846
|
+
if (trailing) {
|
|
804
847
|
source += trailing;
|
|
805
848
|
}
|
|
806
849
|
}
|
|
@@ -829,7 +872,7 @@ class Document extends core_1.Client {
|
|
|
829
872
|
return (code, imports = this.imports) => {
|
|
830
873
|
const uri = file.uri;
|
|
831
874
|
let source, sourceFile;
|
|
832
|
-
if (code &&
|
|
875
|
+
if (code && Object.keys(imports).length) {
|
|
833
876
|
const output = this.resolveImports?.(file, code, uri && this.findSourceRoot(uri, imports));
|
|
834
877
|
if (output) {
|
|
835
878
|
source = output;
|
|
@@ -962,54 +1005,32 @@ class Document extends core_1.Client {
|
|
|
962
1005
|
return null;
|
|
963
1006
|
}
|
|
964
1007
|
async transform(type, code, format, options = {}) {
|
|
1008
|
+
var _a, _b, _c;
|
|
965
1009
|
let transform = this.settings.transform, data;
|
|
966
|
-
if (!transform
|
|
1010
|
+
if (!(transform && (0, types_1.isObject)(data = transform[type]))) {
|
|
967
1011
|
return;
|
|
968
1012
|
}
|
|
969
1013
|
format = typeof format === 'string' ? format.trim().split(/\s*\+\s*/) : format.map(item => typeof item === 'string' ? item.trim() : '');
|
|
970
|
-
const moduleName = this.moduleName;
|
|
971
1014
|
const username = this.host?.username || '';
|
|
972
1015
|
const config = this._transformConfig;
|
|
973
1016
|
const cacheData = config && options.cacheData;
|
|
974
|
-
const
|
|
975
|
-
let formatKey, hashKey, excludeKey;
|
|
976
|
-
if (cacheData &&
|
|
1017
|
+
const cacheDir = username && core_1.Client.enabled("memory.settings.users", username) ? true : this.cacheDir || core_1.Client.enabled("memory.settings.users");
|
|
1018
|
+
let formatKey = '', hashKey, excludeKey;
|
|
1019
|
+
if (cacheData && cacheDir && !CACHE_EXTERNAL[excludeKey = this.moduleName + '_' + type + '_' + format] && (!config.exclude[type] || Array.isArray(config.exclude[type]) && !format.some(value => config.exclude[type].includes(value)) || Array.isArray(config.include[type]) && format.every(value => config.include[type].includes(value)))) {
|
|
977
1020
|
const { uri, etag } = cacheData;
|
|
978
1021
|
const encoding = (0, types_1.getEncoding)(cacheData.encoding);
|
|
979
1022
|
const algorithm = config.algorithm;
|
|
980
1023
|
let result, cacheName;
|
|
981
1024
|
formatKey = type + '_' + format + '_' + encoding + (username ? '_' + username : '') + core_1.Client.asString(options.external) + core_1.Client.asString(options.metadata);
|
|
982
|
-
const resetTimeout = (map, key, stored) => {
|
|
983
|
-
if (config.renew) {
|
|
984
|
-
if (stored.timeout) {
|
|
985
|
-
clearTimeout(stored.timeout);
|
|
986
|
-
}
|
|
987
|
-
stored.timeout = renewTransform(map, key, config.expires);
|
|
988
|
-
}
|
|
989
|
-
stored.lastAccessed = Date.now();
|
|
990
|
-
};
|
|
991
|
-
const getData = (stored) => {
|
|
992
|
-
const location = stored.format[formatKey];
|
|
993
|
-
if (typeof location === 'string') {
|
|
994
|
-
try {
|
|
995
|
-
return JSON.parse(fs.readFileSync(location, encoding));
|
|
996
|
-
}
|
|
997
|
-
catch {
|
|
998
|
-
fs.unlink(location, () => { });
|
|
999
|
-
return;
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
return location;
|
|
1003
|
-
};
|
|
1004
1025
|
if (config.etag && etag && uri) {
|
|
1005
|
-
const etagMap = CACHE_ETAG[moduleName] || (CACHE_ETAG[
|
|
1026
|
+
const etagMap = CACHE_ETAG[_a = this.moduleName] || (CACHE_ETAG[_a] = {});
|
|
1006
1027
|
const stored = etagMap[uri];
|
|
1007
1028
|
if (stored) {
|
|
1008
1029
|
if (stored.etag !== etag) {
|
|
1009
1030
|
deleteTransform(etagMap, uri, stored.timeout);
|
|
1010
1031
|
}
|
|
1011
|
-
else if (result =
|
|
1012
|
-
|
|
1032
|
+
else if (result = getTransformData(stored, formatKey, encoding)) {
|
|
1033
|
+
resetTransform(config, etagMap, uri, stored);
|
|
1013
1034
|
cacheName = 'etag';
|
|
1014
1035
|
}
|
|
1015
1036
|
}
|
|
@@ -1018,10 +1039,10 @@ class Document extends core_1.Client {
|
|
|
1018
1039
|
if (algorithm) {
|
|
1019
1040
|
const limit = config.limit;
|
|
1020
1041
|
if ((limit === Infinity || limit > 0 && Buffer.byteLength(code, encoding) <= limit) && (hashKey = core_1.Client.asHash(code, { algorithm, encoding }))) {
|
|
1021
|
-
const hashMap = CACHE_HASH[moduleName] || (CACHE_HASH[
|
|
1042
|
+
const hashMap = CACHE_HASH[_b = this.moduleName] || (CACHE_HASH[_b] = {});
|
|
1022
1043
|
const stored = hashMap[hashKey];
|
|
1023
|
-
if (stored && (result =
|
|
1024
|
-
|
|
1044
|
+
if (stored && (result = getTransformData(stored, formatKey, encoding))) {
|
|
1045
|
+
resetTransform(config, hashMap, hashKey, stored);
|
|
1025
1046
|
cacheName = algorithm;
|
|
1026
1047
|
}
|
|
1027
1048
|
}
|
|
@@ -1030,14 +1051,14 @@ class Document extends core_1.Client {
|
|
|
1030
1051
|
}
|
|
1031
1052
|
}
|
|
1032
1053
|
else if (uri) {
|
|
1033
|
-
const codeMap = CACHE_CODE[moduleName] || (CACHE_CODE[
|
|
1054
|
+
const codeMap = CACHE_CODE[_c = this.moduleName] || (CACHE_CODE[_c] = {});
|
|
1034
1055
|
const stored = codeMap[uri];
|
|
1035
1056
|
if (stored) {
|
|
1036
1057
|
if (stored.code !== code) {
|
|
1037
1058
|
deleteTransform(codeMap, uri, stored.timeout);
|
|
1038
1059
|
}
|
|
1039
|
-
else if (result =
|
|
1040
|
-
|
|
1060
|
+
else if (result = getTransformData(stored, formatKey, encoding)) {
|
|
1061
|
+
resetTransform(config, codeMap, uri, stored);
|
|
1041
1062
|
}
|
|
1042
1063
|
}
|
|
1043
1064
|
}
|
|
@@ -1062,18 +1083,10 @@ class Document extends core_1.Client {
|
|
|
1062
1083
|
userData = transform[type];
|
|
1063
1084
|
userImports = transform.imports;
|
|
1064
1085
|
}
|
|
1065
|
-
const
|
|
1086
|
+
const abortTransform = () => {
|
|
1066
1087
|
ignoreCache = true;
|
|
1067
1088
|
this.abort('transform');
|
|
1068
1089
|
};
|
|
1069
|
-
const includes = (name) => {
|
|
1070
|
-
const include = config?.include[type];
|
|
1071
|
-
if (include === true || include?.includes(name)) {
|
|
1072
|
-
const exclude = config.exclude[type];
|
|
1073
|
-
return !exclude || exclude === true && include !== true || Array.isArray(exclude) && !exclude.includes(name);
|
|
1074
|
-
}
|
|
1075
|
-
return cacheData?.override === true;
|
|
1076
|
-
};
|
|
1077
1090
|
for (let i = 0, length = format.length, name; i < length; ++i) {
|
|
1078
1091
|
if (this.aborted) {
|
|
1079
1092
|
break;
|
|
@@ -1107,7 +1120,7 @@ class Document extends core_1.Client {
|
|
|
1107
1120
|
}
|
|
1108
1121
|
}
|
|
1109
1122
|
if (!baseSettings) {
|
|
1110
|
-
excluded = !
|
|
1123
|
+
excluded = !hasTransformType(config, type, name, cacheData);
|
|
1111
1124
|
}
|
|
1112
1125
|
outputConfig || (outputConfig = {});
|
|
1113
1126
|
if (plugin && baseConfig) {
|
|
@@ -1130,7 +1143,7 @@ class Document extends core_1.Client {
|
|
|
1130
1143
|
else if (source !== value) {
|
|
1131
1144
|
series.code = value;
|
|
1132
1145
|
valid = true;
|
|
1133
|
-
if (out.ignoreCache && !
|
|
1146
|
+
if (out.ignoreCache && !hasTransformType(config, type, name, cacheData)) {
|
|
1134
1147
|
ignoreCache = true;
|
|
1135
1148
|
}
|
|
1136
1149
|
if ((0, types_1.isArray)(out.sourceFiles)) {
|
|
@@ -1192,7 +1205,7 @@ class Document extends core_1.Client {
|
|
|
1192
1205
|
if (!transformer) {
|
|
1193
1206
|
try {
|
|
1194
1207
|
let pkg = this.resolveDir('package', plugin + '.js') || userImports?.[plugin] || imports[plugin] || util_1.IMPORTS[plugin];
|
|
1195
|
-
if (
|
|
1208
|
+
if (pkg) {
|
|
1196
1209
|
const match = /^(@?\S+)@(\d+(?:\.\d+(?:\.\S+)?)?|[a-z]+)$/.exec(pkg);
|
|
1197
1210
|
if (match) {
|
|
1198
1211
|
pkg = match[1];
|
|
@@ -1203,20 +1216,19 @@ class Document extends core_1.Client {
|
|
|
1203
1216
|
else {
|
|
1204
1217
|
transformer = context;
|
|
1205
1218
|
context = this;
|
|
1206
|
-
pkg = undefined;
|
|
1207
|
-
}
|
|
1208
|
-
if (transformer && typeof transformer !== 'function') {
|
|
1209
|
-
transformer = transformer.default;
|
|
1210
1219
|
}
|
|
1211
1220
|
if (typeof transformer !== 'function') {
|
|
1212
|
-
|
|
1221
|
+
transformer && (transformer = transformer.default);
|
|
1222
|
+
if (typeof transformer !== 'function') {
|
|
1223
|
+
throw (0, types_1.errorMessage)(plugin, pkg || name, 'Invalid function');
|
|
1224
|
+
}
|
|
1213
1225
|
}
|
|
1214
1226
|
if (pkg) {
|
|
1215
1227
|
CACHE_PACKAGE[plugin + username] = transformer;
|
|
1216
1228
|
}
|
|
1217
1229
|
}
|
|
1218
1230
|
catch (err) {
|
|
1219
|
-
|
|
1231
|
+
abortTransform();
|
|
1220
1232
|
this.writeFail(["Unknown", username ? plugin + ':' + username : hint], err, { type: 4, startTime });
|
|
1221
1233
|
continue;
|
|
1222
1234
|
}
|
|
@@ -1226,7 +1238,7 @@ class Document extends core_1.Client {
|
|
|
1226
1238
|
}
|
|
1227
1239
|
}
|
|
1228
1240
|
catch (err) {
|
|
1229
|
-
|
|
1241
|
+
abortTransform();
|
|
1230
1242
|
this.checkPackage(err, (0, util_1.getModuleName)(err), ["Unable to transform document", hint], { type: 4, startTime });
|
|
1231
1243
|
if (i < length - 1) {
|
|
1232
1244
|
series.reset();
|
|
@@ -1234,7 +1246,7 @@ class Document extends core_1.Client {
|
|
|
1234
1246
|
}
|
|
1235
1247
|
}
|
|
1236
1248
|
else {
|
|
1237
|
-
|
|
1249
|
+
abortTransform();
|
|
1238
1250
|
if (plugin) {
|
|
1239
1251
|
this.writeFail("Unable to load configuration", (0, types_1.errorMessage)(plugin, name, 'Invalid config'), 4);
|
|
1240
1252
|
}
|
|
@@ -1269,55 +1281,43 @@ class Document extends core_1.Client {
|
|
|
1269
1281
|
const { uri, etag } = cacheData;
|
|
1270
1282
|
const formatData = { ...output, storedLog };
|
|
1271
1283
|
const lastAccessed = Date.now();
|
|
1272
|
-
const setData = () => {
|
|
1273
|
-
let pathname;
|
|
1274
|
-
if (typeof cacheType === 'string' && core_1.Client.createDir(pathname = path.join(cacheType, 'transform', type))) {
|
|
1275
|
-
try {
|
|
1276
|
-
fs.writeFileSync(pathname = path.join(pathname, (0, types_1.incrementUUID)()), JSON.stringify(formatData));
|
|
1277
|
-
return pathname;
|
|
1278
|
-
}
|
|
1279
|
-
catch {
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
return formatData;
|
|
1283
|
-
};
|
|
1284
1284
|
if (config.etag && etag && uri) {
|
|
1285
|
-
const etagMap = CACHE_ETAG[moduleName];
|
|
1285
|
+
const etagMap = CACHE_ETAG[this.moduleName];
|
|
1286
1286
|
const stored = etagMap[uri];
|
|
1287
1287
|
if (stored) {
|
|
1288
1288
|
if (stored.etag === etag) {
|
|
1289
|
-
stored.format[formatKey] =
|
|
1289
|
+
stored.format[formatKey] = setTransformData(type, cacheDir, formatData);
|
|
1290
1290
|
stored.lastAccessed = lastAccessed;
|
|
1291
1291
|
return output;
|
|
1292
1292
|
}
|
|
1293
1293
|
deleteTransform(etagMap, uri, stored.timeout);
|
|
1294
1294
|
}
|
|
1295
|
-
etagMap[uri] = { lastAccessed, etag, timeout: renewTransform(etagMap, uri, config.expires), format: { [formatKey]:
|
|
1295
|
+
etagMap[uri] = { lastAccessed, etag, timeout: renewTransform(etagMap, uri, config.expires), format: { [formatKey]: setTransformData(type, cacheDir, formatData) } };
|
|
1296
1296
|
++CACHE_TOTAL;
|
|
1297
1297
|
}
|
|
1298
1298
|
else if (hashKey) {
|
|
1299
|
-
const hashMap = CACHE_HASH[moduleName];
|
|
1299
|
+
const hashMap = CACHE_HASH[this.moduleName];
|
|
1300
1300
|
const stored = hashMap[hashKey];
|
|
1301
1301
|
if (stored) {
|
|
1302
|
-
stored.format[formatKey] =
|
|
1302
|
+
stored.format[formatKey] = setTransformData(type, cacheDir, formatData);
|
|
1303
1303
|
stored.lastAccessed = lastAccessed;
|
|
1304
1304
|
return output;
|
|
1305
1305
|
}
|
|
1306
|
-
hashMap[hashKey] = { lastAccessed, timeout: renewTransform(hashMap, hashKey, config.expires), format: { [formatKey]:
|
|
1306
|
+
hashMap[hashKey] = { lastAccessed, timeout: renewTransform(hashMap, hashKey, config.expires), format: { [formatKey]: setTransformData(type, cacheDir, formatData) } };
|
|
1307
1307
|
++CACHE_TOTAL;
|
|
1308
1308
|
}
|
|
1309
1309
|
else if (uri) {
|
|
1310
|
-
const codeMap = CACHE_CODE[moduleName];
|
|
1310
|
+
const codeMap = CACHE_CODE[this.moduleName];
|
|
1311
1311
|
const stored = codeMap[uri];
|
|
1312
1312
|
if (stored) {
|
|
1313
1313
|
if (stored.code === code) {
|
|
1314
|
-
stored.format[formatKey] =
|
|
1314
|
+
stored.format[formatKey] = setTransformData(type, cacheDir, formatData);
|
|
1315
1315
|
stored.lastAccessed = lastAccessed;
|
|
1316
1316
|
return output;
|
|
1317
1317
|
}
|
|
1318
1318
|
deleteTransform(codeMap, uri, stored.timeout);
|
|
1319
1319
|
}
|
|
1320
|
-
codeMap[uri] = { lastAccessed, code, timeout: renewTransform(codeMap, uri, config.expires), format: { [formatKey]:
|
|
1320
|
+
codeMap[uri] = { lastAccessed, code, timeout: renewTransform(codeMap, uri, config.expires), format: { [formatKey]: setTransformData(type, cacheDir, formatData) } };
|
|
1321
1321
|
++CACHE_TOTAL;
|
|
1322
1322
|
}
|
|
1323
1323
|
}
|
|
@@ -1364,18 +1364,11 @@ class Document extends core_1.Client {
|
|
|
1364
1364
|
}
|
|
1365
1365
|
}
|
|
1366
1366
|
get imports() {
|
|
1367
|
-
|
|
1368
|
-
this.imports = {};
|
|
1369
|
-
}
|
|
1370
|
-
return this._imports;
|
|
1367
|
+
return this._imports || (this._imports = {});
|
|
1371
1368
|
}
|
|
1372
1369
|
get watching() {
|
|
1373
1370
|
return this.assets.some(item => item.watch);
|
|
1374
1371
|
}
|
|
1375
1372
|
}
|
|
1376
|
-
exports.default = Document;
|
|
1377
1373
|
|
|
1378
|
-
|
|
1379
|
-
module.exports = exports.default;
|
|
1380
|
-
module.exports.default = exports.default;
|
|
1381
|
-
}
|
|
1374
|
+
module.exports = Document;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/document",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Document constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,12 +20,12 @@
|
|
|
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.9.0",
|
|
24
|
+
"@e-mc/db": "0.9.0",
|
|
25
|
+
"@e-mc/types": "0.9.0",
|
|
26
26
|
"chalk": "4.1.2",
|
|
27
27
|
"htmlparser2": "^9.1.0",
|
|
28
28
|
"js-yaml": "^4.1.0",
|
|
29
|
-
"picomatch": "^
|
|
29
|
+
"picomatch": "^4.0.2"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/parse/dom.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
exports.IGNORE_FLAG = exports.HtmlElement = exports.DomWriter = void 0;
|
|
4
3
|
const htmlparser2 = require("htmlparser2");
|
|
5
4
|
const domhandler = require("domhandler");
|
|
@@ -82,7 +81,6 @@ class DomWriter extends index_1.XmlWriter {
|
|
|
82
81
|
pattern.lastIndex += tag.length - match[0].length;
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
|
-
pattern.lastIndex = 0;
|
|
86
84
|
return source;
|
|
87
85
|
}
|
|
88
86
|
static getDocumentElement(source, parser) {
|
package/parse/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
exports.XmlElement = exports.XmlWriter = exports.IGNORE_FLAG = void 0;
|
|
4
3
|
const htmlparser2 = require("htmlparser2");
|
|
5
4
|
const domhandler = require("domhandler");
|
|
@@ -1047,7 +1046,7 @@ class XmlElement {
|
|
|
1047
1046
|
if (value === null) {
|
|
1048
1047
|
continue;
|
|
1049
1048
|
}
|
|
1050
|
-
result += '="' + (typeof value === 'string' ? escapeEntities ? XmlWriter.escapeXmlString(value) : value.replace(/"/g, '"') : module_1.
|
|
1049
|
+
result += '="' + (typeof value === 'string' ? escapeEntities ? XmlWriter.escapeXmlString(value) : value.replace(/"/g, '"') : module_1.asString(value)) + '"';
|
|
1051
1050
|
}
|
|
1052
1051
|
return result;
|
|
1053
1052
|
}
|
package/parse/types/index.d.ts
CHANGED
|
@@ -127,7 +127,7 @@ export class IXmlWriter extends IXmlBase {
|
|
|
127
127
|
renameTag(node: XmlTagNode, tagName: string): void;
|
|
128
128
|
indexTag(tagName: string, append?: TagAppend, offset?: number): void;
|
|
129
129
|
resetTag(tagName: string): void;
|
|
130
|
-
ignoreTag(value:
|
|
130
|
+
ignoreTag(value: ArrayOf<string>): void;
|
|
131
131
|
resetPosition(startIndex?: number): void;
|
|
132
132
|
getElementById(id: string, ignoreCase?: boolean, options?: TagNodeByIdOptions): Undef<SourceTagNode>;
|
|
133
133
|
getElementsByTagName(tagName: string, ignoreCase?: boolean, options?: TagNodeOptions): SourceTagNode[];
|
|
@@ -165,7 +165,7 @@ export interface XmlWriterConstructor {
|
|
|
165
165
|
getNameOfId(document: string): string;
|
|
166
166
|
getPatternId(name: string): RegExp;
|
|
167
167
|
getCommentsAndCDATA(source: string, tagPattern: unknown, ignoreCase: boolean, stripXml: true): string;
|
|
168
|
-
getCommentsAndCDATA(source: string, tagPattern?:
|
|
168
|
+
getCommentsAndCDATA(source: string, tagPattern?: ArrayOf<string> | [Undef<string>, Undef<string[]>], ignoreCase?: boolean, stripXml?: boolean): SourceContent[];
|
|
169
169
|
isEqual(node: XmlTagNode, other: XmlTagNode, ignoreCase: boolean): boolean;
|
|
170
170
|
isEqual(node: XmlTagNode, other: XmlTagNode, documentName?: string, ignoreCase?: boolean): boolean;
|
|
171
171
|
isIndex(value: Undef<number>): value is number;
|
package/transform/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { SourceMapConstructor, TransformSeriesConstructor } from '../../types/lib/document';
|
|
2
|
-
|
|
3
|
-
declare namespace transform {
|
|
4
|
-
const TransformSeries: TransformSeriesConstructor;
|
|
5
|
-
const SourceMap: SourceMapConstructor;
|
|
6
|
-
}
|
|
7
|
-
|
|
1
|
+
import type { SourceMapConstructor, TransformSeriesConstructor } from '../../types/lib/document';
|
|
2
|
+
|
|
3
|
+
declare namespace transform {
|
|
4
|
+
const TransformSeries: TransformSeriesConstructor;
|
|
5
|
+
const SourceMap: SourceMapConstructor;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
8
|
export = transform;
|
package/transform/index.js
CHANGED
package/util.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
exports.isObject = exports.removeInternalProperties = exports.getModuleName = exports.hasValue = exports.getHashData = exports.getNewline = exports.getIndent = exports.appendSuffix = exports.splitEnclosing = exports.concatString = exports.replaceAll = exports.loadPlugins = exports.IMPORTS = void 0;
|
|
4
3
|
const path = require("path");
|
|
5
4
|
const types_1 = require("@e-mc/types");
|