@e-mc/document 0.12.9 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @e-mc/document
2
2
 
3
- * NodeJS 18
3
+ * NodeJS 18.20.5 LTS
4
4
  * ES2022
5
5
 
6
6
  ## General Usage
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.12.9/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.13.1/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { DataSource, ViewEngine } from "./squared";
@@ -30,7 +30,7 @@ interface IDocument extends IClient<IFileManager, DocumentModule, TransformCallb
30
30
  init(assets: ExternalAsset[], config?: HostInitConfig): this;
31
31
  customize(options?: CustomizeOptions): void;
32
32
  findConfig(data: object, name: string, type?: string): PluginConfig;
33
- loadConfig(data: object, name: string): ConfigOrTransformer | null | undefined;
33
+ loadConfig(data: object, name: string): ConfigOrTransformer | undefined;
34
34
  asSourceFile(value: string, cache: boolean): unknown;
35
35
  asSourceFile(value: string, options?: AsSourceFileOptions): unknown;
36
36
  findVersion(name: string | string[], fallback?: string): string;
@@ -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.12.9/lib/squared.d.ts
189
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/asset.d.ts
190
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/core.d.ts
191
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/document.d.ts
192
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/filemanager.d.ts
193
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/logger.d.ts
194
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/settings.d.ts
195
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/watch.d.ts
188
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/squared.d.ts
189
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/asset.d.ts
190
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/core.d.ts
191
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/document.d.ts
192
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/filemanager.d.ts
193
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/logger.d.ts
194
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/settings.d.ts
195
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/watch.d.ts
196
196
 
197
197
  ## LICENSE
198
198
 
package/index.js CHANGED
@@ -13,6 +13,7 @@ const parse_1 = require("@e-mc/document/parse");
13
13
  const util_1 = require("@e-mc/document/util");
14
14
  const kDocument = Symbol.for('document:constructor');
15
15
  const CACHE_PACKAGE = {};
16
+ const CACHE_CONFIG = new Map();
16
17
  const CACHE_TEMPLATE = {};
17
18
  const CACHE_VIEWENGINE = new Map();
18
19
  const CACHE_EXTERNAL = {};
@@ -41,14 +42,9 @@ function normalizeDir(value, check) {
41
42
  }
42
43
  return value.endsWith(sep) ? value : value + sep;
43
44
  }
44
- function getConfigObject(instance, data, name, target, cache) {
45
- if (instance.settingsOf('transform', 'coerce') === true) {
46
- (0, types_1.coerceObject)(target, cache);
47
- }
48
- return (0, types_1.cloneObject)(data[name] = target, true);
49
- }
50
- function errorConfig(instance, ...message) {
51
- instance.addLog(2, joinString('config', ...message));
45
+ function cloneConfigItems(items) {
46
+ const [a, b, c] = items;
47
+ return [a, (typeof b === 'object' ? (0, types_1.cloneObject)(b, true) : b), typeof c === 'object' ? (0, types_1.cloneObject)(c, true) : c];
52
48
  }
53
49
  const isExcluded = (type, name, config, cacheData) => !(config?.hasType(type, name) || cacheData?.override === true);
54
50
  const wrapCount = (value) => chalk.grey('(') + value + chalk.grey(')');
@@ -231,7 +227,7 @@ class TransformConfig {
231
227
  }
232
228
  }
233
229
  if (limit !== undefined) {
234
- let value;
230
+ let value = NaN;
235
231
  if (typeof limit === 'string') {
236
232
  if (parseFloat(limit) === 0) {
237
233
  value = 0;
@@ -632,19 +628,19 @@ class Document extends core_1.Client {
632
628
  }
633
629
  if (baseUrl) {
634
630
  let pages = this.settings.pages;
635
- if ((0, types_1.isPlainObject)(users?.pages)) {
636
- pages = (0, types_1.isPlainObject)(pages) ? { ...pages, ...users.pages } : users.pages;
631
+ if (users?.pages) {
632
+ pages = pages ? { ...pages, ...users.pages } : users.pages;
637
633
  }
638
- if ((0, types_1.isPlainObject)(pages)) {
639
- let mimeMap;
640
- if ((0, types_1.isPlainObject)(mimeMap = pages[baseUrl])) {
634
+ if (pages) {
635
+ let mimeMap = pages[baseUrl];
636
+ if (mimeMap) {
641
637
  mimeMap = { ...mimeMap };
642
638
  }
643
639
  else {
644
640
  const items = [];
645
641
  for (const pattern in pages) {
646
642
  const item = pages[pattern];
647
- if ((0, types_1.isPlainObject)(item)) {
643
+ if ((0, types_1.isObject)(item)) {
648
644
  if (pattern === baseUrl) {
649
645
  items.push(item);
650
646
  }
@@ -668,10 +664,9 @@ class Document extends core_1.Client {
668
664
  }
669
665
  }
670
666
  if (mimeMap) {
671
- let merge;
672
667
  for (const item of assets) {
673
- const mimeType = item.mimeType;
674
- if (mimeType && (0, types_1.isPlainObject)(merge = mimeMap[mimeType])) {
668
+ const merge = item.mimeType && mimeMap[item.mimeType];
669
+ if ((0, types_1.isObject)(merge)) {
675
670
  Object.assign(item, merge);
676
671
  }
677
672
  }
@@ -684,7 +679,7 @@ class Document extends core_1.Client {
684
679
  const db = this.module.db ||= {};
685
680
  const handler = db.handler;
686
681
  const database = this.dataSource.filter(this.forDb.bind(this));
687
- let instance;
682
+ let instance = null;
688
683
  if (handler && handler !== "@e-mc/db") {
689
684
  try {
690
685
  const Module = require(handler);
@@ -711,25 +706,25 @@ class Document extends core_1.Client {
711
706
  else {
712
707
  instance.init();
713
708
  }
709
+ instance.on('db:fail', (err, item) => this.addLog(3, err, { source: item.table ? `${item.source}@${item.table}` : item.source }));
714
710
  this.Db = instance;
715
711
  }
716
712
  }
717
713
  this.assets = assets;
718
714
  super.init();
719
- if (!ignoreExtensions) {
720
- return this;
721
- }
722
- if (ignoreExtensions === true || ignoreExtensions === this.moduleName || Array.isArray(ignoreExtensions) && ignoreExtensions.includes(this.moduleName)) {
723
- this._extensions = [];
724
- }
725
- else {
726
- const extensions = this._extensions;
727
- if ((0, types_1.isArray)(extensions)) {
728
- if (typeof ignoreExtensions === 'string') {
729
- this._extensions = extensions.filter(value => value !== ignoreExtensions);
730
- }
731
- else if ((0, types_1.isArray)(ignoreExtensions)) {
732
- this._extensions = extensions.filter(value => !ignoreExtensions.includes(value));
715
+ if (ignoreExtensions) {
716
+ if (ignoreExtensions === true || ignoreExtensions === this.moduleName || Array.isArray(ignoreExtensions) && ignoreExtensions.includes(this.moduleName)) {
717
+ this._extensions = [];
718
+ }
719
+ else {
720
+ const extensions = this._extensions;
721
+ if ((0, types_1.isArray)(extensions)) {
722
+ if (typeof ignoreExtensions === 'string') {
723
+ this._extensions = extensions.filter(value => value !== ignoreExtensions);
724
+ }
725
+ else if ((0, types_1.isArray)(ignoreExtensions)) {
726
+ this._extensions = extensions.filter(value => !ignoreExtensions.includes(value));
727
+ }
733
728
  }
734
729
  }
735
730
  }
@@ -757,6 +752,21 @@ class Document extends core_1.Client {
757
752
  }
758
753
  }
759
754
  findConfig(data, name, type) {
755
+ const username = this.host?.username;
756
+ let cacheKey;
757
+ if (username) {
758
+ const cache = CACHE_CONFIG.get(data);
759
+ cacheKey = type ? `${name}::${type}` : name;
760
+ if (cache) {
761
+ const items = cache[username]?.[name];
762
+ if (items) {
763
+ return cloneConfigItems(items);
764
+ }
765
+ }
766
+ else {
767
+ CACHE_CONFIG.set(data, {});
768
+ }
769
+ }
760
770
  for (const plugin in data) {
761
771
  const settings = data[plugin];
762
772
  for (const attr in settings) {
@@ -764,7 +774,12 @@ class Document extends core_1.Client {
764
774
  const options = this.loadConfig(settings, attr);
765
775
  const output = this.loadConfig(settings, attr + '-output');
766
776
  if (options || output) {
767
- return [plugin, options, output, true];
777
+ const result = [plugin, options, output, true];
778
+ if (cacheKey) {
779
+ const cache = CACHE_CONFIG.get(data)[username] ||= {};
780
+ cache[cacheKey] = cloneConfigItems(result);
781
+ }
782
+ return result;
768
783
  }
769
784
  }
770
785
  }
@@ -778,7 +793,7 @@ class Document extends core_1.Client {
778
793
  return value;
779
794
  case 'object':
780
795
  if (value) {
781
- return getConfigObject(this, data, name, value, true);
796
+ return this.#getConfigObject(data, name, value, true);
782
797
  }
783
798
  break;
784
799
  case 'string': {
@@ -787,16 +802,16 @@ class Document extends core_1.Client {
787
802
  if (!name.endsWith('-output')) {
788
803
  return data[name] = result;
789
804
  }
790
- errorConfig(this, "\"output\" can only be a plain object");
805
+ this.#addConfigError("\"output\" can only be a plain object");
791
806
  }
792
807
  else if ((0, types_1.isPlainObject)(result)) {
793
- return getConfigObject(this, data, name, result, false);
808
+ return this.#getConfigObject(data, name, result, false);
794
809
  }
795
810
  else if (path.isAbsolute(value)) {
796
- errorConfig(this, name, "Unsupported access", value);
811
+ this.#addConfigError(name, "Unsupported access", value);
797
812
  }
798
813
  else {
799
- errorConfig(this, name, "Unknown");
814
+ this.#addConfigError(name, "Unknown");
800
815
  }
801
816
  break;
802
817
  }
@@ -832,29 +847,19 @@ class Document extends core_1.Client {
832
847
  else if ((0, types_1.isObject)(options)) {
833
848
  ({ encoding, cache } = options);
834
849
  }
835
- let result;
836
- if ((value = value.trim()).startsWith('npm:')) {
837
- if (!core_1.Client.enabled("node.require.npm")) {
838
- return null;
839
- }
840
- const pkgName = value.substring(4);
841
- if (!result) {
842
- try {
843
- result = require(pkgName);
844
- if (typeof result === 'function') {
845
- return Object.defineProperty(result, "__cjs__", { value: true });
846
- }
847
- }
848
- catch (err) {
849
- this.checkPackage(err, pkgName, "Unknown");
850
- }
851
- if (!result) {
850
+ let result, pkgName;
851
+ try {
852
+ if (value.startsWith('npm:')) {
853
+ if (!core_1.Client.enabled("node.require.npm")) {
852
854
  return null;
853
855
  }
856
+ result = require(pkgName = value.substring(4));
857
+ if (typeof result === 'function') {
858
+ return Object.defineProperty(result, "__cjs__", { value: true });
859
+ }
860
+ return result;
854
861
  }
855
- }
856
- try {
857
- const source = (result || this.readFile(value, { ownPermissionOnly: true, absolutePath: this.hasEval('absolute'), requireExt: true, encoding: (0, types_1.getEncoding)(encoding), cache })) || value;
862
+ const source = this.readFile(value, { ownPermissionOnly: true, absolutePath: this.hasEval('absolute'), requireExt: true, encoding: (0, types_1.getEncoding)(encoding), cache }) || value;
858
863
  if (typeof source !== 'string') {
859
864
  result = source;
860
865
  }
@@ -870,7 +875,9 @@ class Document extends core_1.Client {
870
875
  }
871
876
  }
872
877
  catch (err) {
873
- this.writeFail(["Unable to read file", path.basename(value)], err, 8192);
878
+ if (!pkgName || this.checkPackage(err, pkgName)) {
879
+ this.writeFail(["Unable to read file", path.basename(value)], err, 8192);
880
+ }
874
881
  }
875
882
  return null;
876
883
  }
@@ -1289,7 +1296,7 @@ class Document extends core_1.Client {
1289
1296
  series.outputConfig = outputConfig;
1290
1297
  const source = series.code;
1291
1298
  const startTime = process.hrtime.bigint();
1292
- const hint = plugin + ':' + name;
1299
+ const target = plugin + ':' + name;
1293
1300
  const next = (value) => {
1294
1301
  if (this.aborted) {
1295
1302
  valid = false;
@@ -1309,30 +1316,17 @@ class Document extends core_1.Client {
1309
1316
  ignoreCache = true;
1310
1317
  }
1311
1318
  if ((0, types_1.isArray)(out.sourceFiles)) {
1312
- if (!sourceFiles) {
1313
- sourceFiles = out.sourceFiles.slice(0);
1314
- }
1315
- else {
1316
- out.sourceFiles.forEach(item => {
1317
- if (!sourceFiles.includes(item)) {
1318
- sourceFiles.push(item);
1319
- }
1320
- });
1321
- }
1319
+ sourceFiles = sourceFiles ? sourceFiles.concat(out.sourceFiles.filter(item => !sourceFiles.includes(item))) : out.sourceFiles.slice(0);
1322
1320
  }
1323
1321
  }
1324
1322
  if ((0, types_1.isArray)(out.logAppend)) {
1325
- out.logAppend.forEach(log => {
1326
- this.addLog(log);
1327
- });
1328
- (storedLog ||= []).push(...out.logAppend);
1323
+ out.logAppend.forEach(log => this.addLog(log));
1324
+ storedLog = (storedLog || []).concat(out.logAppend);
1329
1325
  bypassLog = true;
1330
1326
  }
1331
1327
  if ((0, types_1.isArray)(out.logQueued)) {
1332
- out.logQueued.forEach(log => {
1333
- this.writeLog(log, true);
1334
- });
1335
- (storedLog ||= []).push(...out.logQueued);
1328
+ out.logQueued.forEach(log => this.writeLog(log, true));
1329
+ storedLog = (storedLog || []).concat(out.logQueued);
1336
1330
  bypassLog = true;
1337
1331
  }
1338
1332
  if (i < length - 1) {
@@ -1340,7 +1334,7 @@ class Document extends core_1.Client {
1340
1334
  }
1341
1335
  this.writeTimeProcess(failed ? 'CHECK' : type, joinString(plugin, name, options.filename) + (series.out.messageAppend || ''), startTime, { failed, bypassLog });
1342
1336
  };
1343
- this.formatMessage(4, type, ["Transforming source...", hint], options.filename, { hintColor: 'cyan' });
1337
+ this.formatMessage(4, type, ["Transforming source...", target], options.filename, { hintColor: 'cyan' });
1344
1338
  try {
1345
1339
  let context;
1346
1340
  try {
@@ -1377,7 +1371,7 @@ class Document extends core_1.Client {
1377
1371
  }
1378
1372
  }
1379
1373
  else {
1380
- let transformer = CACHE_PACKAGE[plugin + username];
1374
+ let transformer = CACHE_PACKAGE[target + username];
1381
1375
  if (!transformer) {
1382
1376
  try {
1383
1377
  let pkg = this.resolveDir('package', plugin + '.js') || this.resolveDir('package', plugin + '.cjs') || this.resolveDir('package', plugin + '.mjs') || userImports?.[plugin] || imports[plugin] || types_1.IMPORT_MAP[plugin];
@@ -1389,7 +1383,7 @@ class Document extends core_1.Client {
1389
1383
  }
1390
1384
  const ext = path.extname(pkg);
1391
1385
  if (ext === '.mjs') {
1392
- transformer = await (0, types_1.importESM)(pkg, true, true);
1386
+ transformer = await (0, types_1.importESM)(pkg, false, true);
1393
1387
  }
1394
1388
  else if (ext === '.cjs') {
1395
1389
  transformer = require(pkg);
@@ -1399,7 +1393,7 @@ class Document extends core_1.Client {
1399
1393
  transformer = require(pkg);
1400
1394
  }
1401
1395
  catch {
1402
- transformer = await (0, types_1.importESM)(pkg, true, true);
1396
+ transformer = await (0, types_1.importESM)(pkg, false, true);
1403
1397
  }
1404
1398
  }
1405
1399
  }
@@ -1407,19 +1401,19 @@ class Document extends core_1.Client {
1407
1401
  transformer = context;
1408
1402
  context = null;
1409
1403
  }
1410
- if ((0, types_1.isObject)(transformer) && typeof transformer.default === 'function') {
1411
- transformer = transformer.default;
1404
+ if ((0, types_1.isObject)(transformer)) {
1405
+ transformer = typeof transformer[name] === 'function' ? transformer[name] : typeof transformer[name.replaceAll('-', '_')] === 'function' ? transformer[name.replaceAll('-', '_')] : transformer.default;
1412
1406
  }
1413
- else if (typeof transformer !== 'function') {
1414
- throw (0, types_1.errorMessage)(plugin, pkg || name, "Invalid function");
1407
+ if (typeof transformer !== 'function') {
1408
+ throw (0, types_1.errorMessage)(plugin, "Invalid function", pkg || name);
1415
1409
  }
1416
1410
  if (pkg) {
1417
- CACHE_PACKAGE[plugin + username] = transformer;
1411
+ CACHE_PACKAGE[target + username] = transformer;
1418
1412
  }
1419
1413
  }
1420
1414
  catch (err) {
1421
1415
  abortTransform();
1422
- this.writeFail(["Unknown", username ? plugin + ':' + username : hint], err, { type: 4, startTime });
1416
+ this.writeFail(["Unknown", username ? target + ':' + username : target], err, { type: 4, startTime });
1423
1417
  continue;
1424
1418
  }
1425
1419
  }
@@ -1429,7 +1423,7 @@ class Document extends core_1.Client {
1429
1423
  }
1430
1424
  catch (err) {
1431
1425
  abortTransform();
1432
- this.checkPackage(err, (0, util_1.getModuleName)(err), ["Unable to transform document", hint], { type: 4, startTime });
1426
+ this.checkPackage(err, (0, util_1.getModuleName)(err), ["Unable to transform document", target], { type: 4, startTime });
1433
1427
  if (i < length - 1) {
1434
1428
  series.reset();
1435
1429
  }
@@ -1472,6 +1466,15 @@ class Document extends core_1.Client {
1472
1466
  }
1473
1467
  return output;
1474
1468
  }
1469
+ #getConfigObject(data, name, target, cache) {
1470
+ if (this.settingsOf('transform', 'coerce') === true) {
1471
+ (0, types_1.coerceObject)(target, cache);
1472
+ }
1473
+ return (0, types_1.cloneObject)(data[name] = target, true);
1474
+ }
1475
+ #addConfigError(...message) {
1476
+ this.addLog(2, joinString('config', ...message));
1477
+ }
1475
1478
  set assets(value) {
1476
1479
  this._assets = value;
1477
1480
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/document",
3
- "version": "0.12.9",
3
+ "version": "0.13.1",
4
4
  "description": "Document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -19,9 +19,9 @@
19
19
  "license": "BSD-3-Clause",
20
20
  "homepage": "https://github.com/anpham6/e-mc#readme",
21
21
  "dependencies": {
22
- "@e-mc/core": "0.12.9",
23
- "@e-mc/db": "0.12.9",
24
- "@e-mc/types": "0.12.9",
22
+ "@e-mc/core": "0.13.1",
23
+ "@e-mc/db": "0.13.1",
24
+ "@e-mc/types": "0.13.1",
25
25
  "chalk": "4.1.2",
26
26
  "domhandler": "^5.0.3",
27
27
  "domutils": "^3.2.2",
package/parse/index.js CHANGED
@@ -31,7 +31,7 @@ const REGEXP_ATTRVALUE = new RegExp(PATTERN_ATTRNAME + '\\s*' + PATTERN_ATTRVALU
31
31
  const REGEXP_TAGATTR = new RegExp(`<${PATTERN_TAGNAME}\\s+(${PATTERN_TAGOPEN}*)>`, 'g');
32
32
  function applyAttributes(attrs, data, ignoreCase) {
33
33
  for (const key in data) {
34
- attrs.set(ignoreCase && typeof key === 'string' ? key.toLowerCase() : key.toString(), data[key]);
34
+ attrs.set(ignoreCase && typeof key === 'string' ? key.toLowerCase() : key, data[key]);
35
35
  }
36
36
  }
37
37
  function deletePosition(item, rootName, startIndex) {
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  exports.SourceMap = exports.TransformSeries = void 0;
3
+ const fs = require("node:fs");
3
4
  const path = require("node:path");
4
5
  const types_1 = require("@e-mc/types");
5
6
  const core_1 = require("@e-mc/core");
6
- const kTransformSeries = Symbol.for('transformseries:constructor');
7
+ const kTransformSeries = Symbol.for('document:transform:series:constructor');
7
8
  function parseMap(data, value) {
8
9
  let [mimeType, encoding] = data.split(';');
9
10
  if (!encoding && !mimeType.includes('/')) {
@@ -125,47 +126,75 @@ class TransformSeries extends core_1.Module {
125
126
  }
126
127
  return baseConfig;
127
128
  }
128
- upgrade(context, dirname, pkgname) {
129
- if (this.version === 'latest') {
130
- try {
131
- const pathname = dirname && this.findModule(dirname, pkgname) || pkgname;
132
- if (pathname) {
133
- return require(pathname);
134
- }
135
- }
136
- catch {
129
+ upgrade(context, startDir, packageName, version) {
130
+ if ((0, types_1.isObject)(startDir)) {
131
+ ({ startDir, packageName, version } = startDir);
132
+ }
133
+ try {
134
+ const pathname = startDir && this.findModule(startDir, packageName, version || this.version) || packageName;
135
+ if (pathname) {
136
+ return require(pathname);
137
137
  }
138
138
  }
139
+ catch {
140
+ }
139
141
  return context;
140
142
  }
141
- async upgradeESM(context, dirname, pkgname) {
142
- if (this.version === 'latest') {
143
- try {
144
- const pathname = dirname && this.findModule(dirname, pkgname);
145
- let result;
146
- if (pathname) {
147
- result = await (0, types_1.importESM)(pathname, false, true);
148
- }
149
- else if (pkgname) {
150
- result = await (0, types_1.importESM)(pkgname);
151
- }
152
- if (result) {
153
- return result;
154
- }
143
+ async upgradeESM(context, startDir, packageName, version) {
144
+ if ((0, types_1.isObject)(startDir)) {
145
+ ({ startDir, packageName, version } = startDir);
146
+ }
147
+ try {
148
+ const pathname = startDir && this.findModule(startDir, packageName, version || this.version);
149
+ let result;
150
+ if (pathname) {
151
+ result = await (0, types_1.importESM)(pathname, false, true);
155
152
  }
156
- catch {
153
+ else if (packageName) {
154
+ result = await (0, types_1.importESM)(packageName);
157
155
  }
158
- return this.upgrade(context, dirname, pkgname);
159
- }
160
- return context;
161
- }
162
- findModule(dirname, pkgname) {
163
- if (path.isAbsolute(dirname)) {
164
- const pathname = path.join(dirname, 'node_modules', pkgname || this.packageName);
165
- if (core_1.Module.isDir(pathname)) {
166
- return pathname;
156
+ if (result) {
157
+ return result;
167
158
  }
168
159
  }
160
+ catch {
161
+ }
162
+ return this.upgrade(context, startDir, packageName);
163
+ }
164
+ findModule(startDir, packageName, version) {
165
+ if (path.isAbsolute(startDir)) {
166
+ version ||= 'latest';
167
+ do {
168
+ try {
169
+ const pathname = path.join(startDir, 'node_modules', packageName || this.packageName);
170
+ if (fs.statSync(pathname).isDirectory()) {
171
+ if (version === 'latest') {
172
+ return pathname;
173
+ }
174
+ const data = require(path.join(pathname, 'package.json'));
175
+ if ((0, types_1.isString)(data?.version)) {
176
+ if (version === data.version) {
177
+ return pathname;
178
+ }
179
+ if (version.startsWith('^') || version.endsWith('$')) {
180
+ if (new RegExp(version).test(data.version)) {
181
+ return pathname;
182
+ }
183
+ }
184
+ else if (version.endsWith('.') && data.version.startsWith(version)) {
185
+ return pathname;
186
+ }
187
+ }
188
+ }
189
+ }
190
+ catch (err) {
191
+ if ((0, types_1.isErrorCode)(err, 'EACCES')) {
192
+ return '';
193
+ }
194
+ }
195
+ startDir = path.dirname(startDir);
196
+ } while (startDir.length > 1);
197
+ }
169
198
  return '';
170
199
  }
171
200
  close(instance) {