@e-mc/document 0.8.7 → 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 +84 -94
- 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];
|
|
@@ -314,7 +355,6 @@ class Document extends core_1.Client {
|
|
|
314
355
|
constructor(data) {
|
|
315
356
|
super(data);
|
|
316
357
|
this.Db = null;
|
|
317
|
-
this.config = {};
|
|
318
358
|
this._assets = null;
|
|
319
359
|
this._dataSource = null;
|
|
320
360
|
this._transformConfig = null;
|
|
@@ -391,7 +431,7 @@ class Document extends core_1.Client {
|
|
|
391
431
|
const handler = db.handler;
|
|
392
432
|
const database = this.dataSource.filter(this.forDb.bind(this));
|
|
393
433
|
let instance;
|
|
394
|
-
if (
|
|
434
|
+
if (handler && handler !== "@e-mc/db") {
|
|
395
435
|
try {
|
|
396
436
|
const Handler = require(handler);
|
|
397
437
|
if (isFunction(Handler) && Handler.prototype instanceof core_1.ClientDb) {
|
|
@@ -406,7 +446,7 @@ class Document extends core_1.Client {
|
|
|
406
446
|
}
|
|
407
447
|
}
|
|
408
448
|
else {
|
|
409
|
-
instance = new db_1
|
|
449
|
+
instance = new db_1(db, database);
|
|
410
450
|
}
|
|
411
451
|
if (instance) {
|
|
412
452
|
const host = this.host;
|
|
@@ -783,7 +823,7 @@ class Document extends core_1.Client {
|
|
|
783
823
|
if (Document.isFile(href, 'unc') || path.isAbsolute(href)) {
|
|
784
824
|
sourceFile.push([href]);
|
|
785
825
|
}
|
|
786
|
-
else if (
|
|
826
|
+
else if (Object.keys(imports).length) {
|
|
787
827
|
const bundleId = file.bundleId;
|
|
788
828
|
const assets = !(0, types_1.isEmpty)(bundleId) ? this.assets.filter(item => item.bundleId === bundleId).sort((a, b) => a.bundleIndex - b.bundleIndex) : [file];
|
|
789
829
|
if (!Array.isArray(bundleContent) || bundleContent.length !== assets.length - 1) {
|
|
@@ -803,7 +843,7 @@ class Document extends core_1.Client {
|
|
|
803
843
|
if (this.resolveUri && (0, types_1.isArray)(file.trailingContent)) {
|
|
804
844
|
let trailing;
|
|
805
845
|
[source, trailing] = this.resolveUri(file, source, (0, util_1.concatString)(file.trailingContent));
|
|
806
|
-
if (
|
|
846
|
+
if (trailing) {
|
|
807
847
|
source += trailing;
|
|
808
848
|
}
|
|
809
849
|
}
|
|
@@ -832,7 +872,7 @@ class Document extends core_1.Client {
|
|
|
832
872
|
return (code, imports = this.imports) => {
|
|
833
873
|
const uri = file.uri;
|
|
834
874
|
let source, sourceFile;
|
|
835
|
-
if (code &&
|
|
875
|
+
if (code && Object.keys(imports).length) {
|
|
836
876
|
const output = this.resolveImports?.(file, code, uri && this.findSourceRoot(uri, imports));
|
|
837
877
|
if (output) {
|
|
838
878
|
source = output;
|
|
@@ -965,54 +1005,32 @@ class Document extends core_1.Client {
|
|
|
965
1005
|
return null;
|
|
966
1006
|
}
|
|
967
1007
|
async transform(type, code, format, options = {}) {
|
|
1008
|
+
var _a, _b, _c;
|
|
968
1009
|
let transform = this.settings.transform, data;
|
|
969
|
-
if (!transform
|
|
1010
|
+
if (!(transform && (0, types_1.isObject)(data = transform[type]))) {
|
|
970
1011
|
return;
|
|
971
1012
|
}
|
|
972
1013
|
format = typeof format === 'string' ? format.trim().split(/\s*\+\s*/) : format.map(item => typeof item === 'string' ? item.trim() : '');
|
|
973
|
-
const moduleName = this.moduleName;
|
|
974
1014
|
const username = this.host?.username || '';
|
|
975
1015
|
const config = this._transformConfig;
|
|
976
1016
|
const cacheData = config && options.cacheData;
|
|
977
|
-
const
|
|
978
|
-
let formatKey, hashKey, excludeKey;
|
|
979
|
-
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)))) {
|
|
980
1020
|
const { uri, etag } = cacheData;
|
|
981
1021
|
const encoding = (0, types_1.getEncoding)(cacheData.encoding);
|
|
982
1022
|
const algorithm = config.algorithm;
|
|
983
1023
|
let result, cacheName;
|
|
984
1024
|
formatKey = type + '_' + format + '_' + encoding + (username ? '_' + username : '') + core_1.Client.asString(options.external) + core_1.Client.asString(options.metadata);
|
|
985
|
-
const resetTimeout = (map, key, stored) => {
|
|
986
|
-
if (config.renew) {
|
|
987
|
-
if (stored.timeout) {
|
|
988
|
-
clearTimeout(stored.timeout);
|
|
989
|
-
}
|
|
990
|
-
stored.timeout = renewTransform(map, key, config.expires);
|
|
991
|
-
}
|
|
992
|
-
stored.lastAccessed = Date.now();
|
|
993
|
-
};
|
|
994
|
-
const getData = (stored) => {
|
|
995
|
-
const location = stored.format[formatKey];
|
|
996
|
-
if (typeof location === 'string') {
|
|
997
|
-
try {
|
|
998
|
-
return JSON.parse(fs.readFileSync(location, encoding));
|
|
999
|
-
}
|
|
1000
|
-
catch {
|
|
1001
|
-
fs.unlink(location, () => { });
|
|
1002
|
-
return;
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
return location;
|
|
1006
|
-
};
|
|
1007
1025
|
if (config.etag && etag && uri) {
|
|
1008
|
-
const etagMap = CACHE_ETAG[moduleName] || (CACHE_ETAG[
|
|
1026
|
+
const etagMap = CACHE_ETAG[_a = this.moduleName] || (CACHE_ETAG[_a] = {});
|
|
1009
1027
|
const stored = etagMap[uri];
|
|
1010
1028
|
if (stored) {
|
|
1011
1029
|
if (stored.etag !== etag) {
|
|
1012
1030
|
deleteTransform(etagMap, uri, stored.timeout);
|
|
1013
1031
|
}
|
|
1014
|
-
else if (result =
|
|
1015
|
-
|
|
1032
|
+
else if (result = getTransformData(stored, formatKey, encoding)) {
|
|
1033
|
+
resetTransform(config, etagMap, uri, stored);
|
|
1016
1034
|
cacheName = 'etag';
|
|
1017
1035
|
}
|
|
1018
1036
|
}
|
|
@@ -1021,10 +1039,10 @@ class Document extends core_1.Client {
|
|
|
1021
1039
|
if (algorithm) {
|
|
1022
1040
|
const limit = config.limit;
|
|
1023
1041
|
if ((limit === Infinity || limit > 0 && Buffer.byteLength(code, encoding) <= limit) && (hashKey = core_1.Client.asHash(code, { algorithm, encoding }))) {
|
|
1024
|
-
const hashMap = CACHE_HASH[moduleName] || (CACHE_HASH[
|
|
1042
|
+
const hashMap = CACHE_HASH[_b = this.moduleName] || (CACHE_HASH[_b] = {});
|
|
1025
1043
|
const stored = hashMap[hashKey];
|
|
1026
|
-
if (stored && (result =
|
|
1027
|
-
|
|
1044
|
+
if (stored && (result = getTransformData(stored, formatKey, encoding))) {
|
|
1045
|
+
resetTransform(config, hashMap, hashKey, stored);
|
|
1028
1046
|
cacheName = algorithm;
|
|
1029
1047
|
}
|
|
1030
1048
|
}
|
|
@@ -1033,14 +1051,14 @@ class Document extends core_1.Client {
|
|
|
1033
1051
|
}
|
|
1034
1052
|
}
|
|
1035
1053
|
else if (uri) {
|
|
1036
|
-
const codeMap = CACHE_CODE[moduleName] || (CACHE_CODE[
|
|
1054
|
+
const codeMap = CACHE_CODE[_c = this.moduleName] || (CACHE_CODE[_c] = {});
|
|
1037
1055
|
const stored = codeMap[uri];
|
|
1038
1056
|
if (stored) {
|
|
1039
1057
|
if (stored.code !== code) {
|
|
1040
1058
|
deleteTransform(codeMap, uri, stored.timeout);
|
|
1041
1059
|
}
|
|
1042
|
-
else if (result =
|
|
1043
|
-
|
|
1060
|
+
else if (result = getTransformData(stored, formatKey, encoding)) {
|
|
1061
|
+
resetTransform(config, codeMap, uri, stored);
|
|
1044
1062
|
}
|
|
1045
1063
|
}
|
|
1046
1064
|
}
|
|
@@ -1065,18 +1083,10 @@ class Document extends core_1.Client {
|
|
|
1065
1083
|
userData = transform[type];
|
|
1066
1084
|
userImports = transform.imports;
|
|
1067
1085
|
}
|
|
1068
|
-
const
|
|
1086
|
+
const abortTransform = () => {
|
|
1069
1087
|
ignoreCache = true;
|
|
1070
1088
|
this.abort('transform');
|
|
1071
1089
|
};
|
|
1072
|
-
const includes = (name) => {
|
|
1073
|
-
const include = config?.include[type];
|
|
1074
|
-
if (include === true || include?.includes(name)) {
|
|
1075
|
-
const exclude = config.exclude[type];
|
|
1076
|
-
return !exclude || exclude === true && include !== true || Array.isArray(exclude) && !exclude.includes(name);
|
|
1077
|
-
}
|
|
1078
|
-
return cacheData?.override === true;
|
|
1079
|
-
};
|
|
1080
1090
|
for (let i = 0, length = format.length, name; i < length; ++i) {
|
|
1081
1091
|
if (this.aborted) {
|
|
1082
1092
|
break;
|
|
@@ -1110,7 +1120,7 @@ class Document extends core_1.Client {
|
|
|
1110
1120
|
}
|
|
1111
1121
|
}
|
|
1112
1122
|
if (!baseSettings) {
|
|
1113
|
-
excluded = !
|
|
1123
|
+
excluded = !hasTransformType(config, type, name, cacheData);
|
|
1114
1124
|
}
|
|
1115
1125
|
outputConfig || (outputConfig = {});
|
|
1116
1126
|
if (plugin && baseConfig) {
|
|
@@ -1133,7 +1143,7 @@ class Document extends core_1.Client {
|
|
|
1133
1143
|
else if (source !== value) {
|
|
1134
1144
|
series.code = value;
|
|
1135
1145
|
valid = true;
|
|
1136
|
-
if (out.ignoreCache && !
|
|
1146
|
+
if (out.ignoreCache && !hasTransformType(config, type, name, cacheData)) {
|
|
1137
1147
|
ignoreCache = true;
|
|
1138
1148
|
}
|
|
1139
1149
|
if ((0, types_1.isArray)(out.sourceFiles)) {
|
|
@@ -1195,7 +1205,7 @@ class Document extends core_1.Client {
|
|
|
1195
1205
|
if (!transformer) {
|
|
1196
1206
|
try {
|
|
1197
1207
|
let pkg = this.resolveDir('package', plugin + '.js') || userImports?.[plugin] || imports[plugin] || util_1.IMPORTS[plugin];
|
|
1198
|
-
if (
|
|
1208
|
+
if (pkg) {
|
|
1199
1209
|
const match = /^(@?\S+)@(\d+(?:\.\d+(?:\.\S+)?)?|[a-z]+)$/.exec(pkg);
|
|
1200
1210
|
if (match) {
|
|
1201
1211
|
pkg = match[1];
|
|
@@ -1206,20 +1216,19 @@ class Document extends core_1.Client {
|
|
|
1206
1216
|
else {
|
|
1207
1217
|
transformer = context;
|
|
1208
1218
|
context = this;
|
|
1209
|
-
pkg = undefined;
|
|
1210
|
-
}
|
|
1211
|
-
if (transformer && typeof transformer !== 'function') {
|
|
1212
|
-
transformer = transformer.default;
|
|
1213
1219
|
}
|
|
1214
1220
|
if (typeof transformer !== 'function') {
|
|
1215
|
-
|
|
1221
|
+
transformer && (transformer = transformer.default);
|
|
1222
|
+
if (typeof transformer !== 'function') {
|
|
1223
|
+
throw (0, types_1.errorMessage)(plugin, pkg || name, 'Invalid function');
|
|
1224
|
+
}
|
|
1216
1225
|
}
|
|
1217
1226
|
if (pkg) {
|
|
1218
1227
|
CACHE_PACKAGE[plugin + username] = transformer;
|
|
1219
1228
|
}
|
|
1220
1229
|
}
|
|
1221
1230
|
catch (err) {
|
|
1222
|
-
|
|
1231
|
+
abortTransform();
|
|
1223
1232
|
this.writeFail(["Unknown", username ? plugin + ':' + username : hint], err, { type: 4, startTime });
|
|
1224
1233
|
continue;
|
|
1225
1234
|
}
|
|
@@ -1229,7 +1238,7 @@ class Document extends core_1.Client {
|
|
|
1229
1238
|
}
|
|
1230
1239
|
}
|
|
1231
1240
|
catch (err) {
|
|
1232
|
-
|
|
1241
|
+
abortTransform();
|
|
1233
1242
|
this.checkPackage(err, (0, util_1.getModuleName)(err), ["Unable to transform document", hint], { type: 4, startTime });
|
|
1234
1243
|
if (i < length - 1) {
|
|
1235
1244
|
series.reset();
|
|
@@ -1237,7 +1246,7 @@ class Document extends core_1.Client {
|
|
|
1237
1246
|
}
|
|
1238
1247
|
}
|
|
1239
1248
|
else {
|
|
1240
|
-
|
|
1249
|
+
abortTransform();
|
|
1241
1250
|
if (plugin) {
|
|
1242
1251
|
this.writeFail("Unable to load configuration", (0, types_1.errorMessage)(plugin, name, 'Invalid config'), 4);
|
|
1243
1252
|
}
|
|
@@ -1272,55 +1281,43 @@ class Document extends core_1.Client {
|
|
|
1272
1281
|
const { uri, etag } = cacheData;
|
|
1273
1282
|
const formatData = { ...output, storedLog };
|
|
1274
1283
|
const lastAccessed = Date.now();
|
|
1275
|
-
const setData = () => {
|
|
1276
|
-
let pathname;
|
|
1277
|
-
if (typeof cacheType === 'string' && core_1.Client.createDir(pathname = path.join(cacheType, 'transform', type))) {
|
|
1278
|
-
try {
|
|
1279
|
-
fs.writeFileSync(pathname = path.join(pathname, (0, types_1.incrementUUID)()), JSON.stringify(formatData));
|
|
1280
|
-
return pathname;
|
|
1281
|
-
}
|
|
1282
|
-
catch {
|
|
1283
|
-
}
|
|
1284
|
-
}
|
|
1285
|
-
return formatData;
|
|
1286
|
-
};
|
|
1287
1284
|
if (config.etag && etag && uri) {
|
|
1288
|
-
const etagMap = CACHE_ETAG[moduleName];
|
|
1285
|
+
const etagMap = CACHE_ETAG[this.moduleName];
|
|
1289
1286
|
const stored = etagMap[uri];
|
|
1290
1287
|
if (stored) {
|
|
1291
1288
|
if (stored.etag === etag) {
|
|
1292
|
-
stored.format[formatKey] =
|
|
1289
|
+
stored.format[formatKey] = setTransformData(type, cacheDir, formatData);
|
|
1293
1290
|
stored.lastAccessed = lastAccessed;
|
|
1294
1291
|
return output;
|
|
1295
1292
|
}
|
|
1296
1293
|
deleteTransform(etagMap, uri, stored.timeout);
|
|
1297
1294
|
}
|
|
1298
|
-
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) } };
|
|
1299
1296
|
++CACHE_TOTAL;
|
|
1300
1297
|
}
|
|
1301
1298
|
else if (hashKey) {
|
|
1302
|
-
const hashMap = CACHE_HASH[moduleName];
|
|
1299
|
+
const hashMap = CACHE_HASH[this.moduleName];
|
|
1303
1300
|
const stored = hashMap[hashKey];
|
|
1304
1301
|
if (stored) {
|
|
1305
|
-
stored.format[formatKey] =
|
|
1302
|
+
stored.format[formatKey] = setTransformData(type, cacheDir, formatData);
|
|
1306
1303
|
stored.lastAccessed = lastAccessed;
|
|
1307
1304
|
return output;
|
|
1308
1305
|
}
|
|
1309
|
-
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) } };
|
|
1310
1307
|
++CACHE_TOTAL;
|
|
1311
1308
|
}
|
|
1312
1309
|
else if (uri) {
|
|
1313
|
-
const codeMap = CACHE_CODE[moduleName];
|
|
1310
|
+
const codeMap = CACHE_CODE[this.moduleName];
|
|
1314
1311
|
const stored = codeMap[uri];
|
|
1315
1312
|
if (stored) {
|
|
1316
1313
|
if (stored.code === code) {
|
|
1317
|
-
stored.format[formatKey] =
|
|
1314
|
+
stored.format[formatKey] = setTransformData(type, cacheDir, formatData);
|
|
1318
1315
|
stored.lastAccessed = lastAccessed;
|
|
1319
1316
|
return output;
|
|
1320
1317
|
}
|
|
1321
1318
|
deleteTransform(codeMap, uri, stored.timeout);
|
|
1322
1319
|
}
|
|
1323
|
-
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) } };
|
|
1324
1321
|
++CACHE_TOTAL;
|
|
1325
1322
|
}
|
|
1326
1323
|
}
|
|
@@ -1367,18 +1364,11 @@ class Document extends core_1.Client {
|
|
|
1367
1364
|
}
|
|
1368
1365
|
}
|
|
1369
1366
|
get imports() {
|
|
1370
|
-
|
|
1371
|
-
this.imports = {};
|
|
1372
|
-
}
|
|
1373
|
-
return this._imports;
|
|
1367
|
+
return this._imports || (this._imports = {});
|
|
1374
1368
|
}
|
|
1375
1369
|
get watching() {
|
|
1376
1370
|
return this.assets.some(item => item.watch);
|
|
1377
1371
|
}
|
|
1378
1372
|
}
|
|
1379
|
-
exports.default = Document;
|
|
1380
1373
|
|
|
1381
|
-
|
|
1382
|
-
module.exports = exports.default;
|
|
1383
|
-
module.exports.default = exports.default;
|
|
1384
|
-
}
|
|
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");
|