@ya-modbus/driver-loader 0.10.0 → 0.11.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/CHANGELOG.md +12 -0
- package/README.md +30 -3
- package/dist/cjs/src/driver-import.d.ts +14 -1
- package/dist/cjs/src/driver-import.d.ts.map +1 -1
- package/dist/cjs/src/driver-import.js +48 -0
- package/dist/cjs/src/driver-import.js.map +1 -1
- package/dist/cjs/src/entry-point.d.ts +20 -0
- package/dist/cjs/src/entry-point.d.ts.map +1 -0
- package/dist/cjs/src/entry-point.js +63 -0
- package/dist/cjs/src/entry-point.js.map +1 -0
- package/dist/cjs/src/errors.d.ts +23 -0
- package/dist/cjs/src/errors.d.ts.map +1 -1
- package/dist/cjs/src/errors.js +29 -1
- package/dist/cjs/src/errors.js.map +1 -1
- package/dist/cjs/src/index.d.ts +1 -1
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js +2 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/loader.d.ts +10 -6
- package/dist/cjs/src/loader.d.ts.map +1 -1
- package/dist/cjs/src/loader.js +24 -69
- package/dist/cjs/src/loader.js.map +1 -1
- package/dist/cjs/src/local-package.d.ts +19 -0
- package/dist/cjs/src/local-package.d.ts.map +1 -0
- package/dist/cjs/src/local-package.js +50 -0
- package/dist/cjs/src/local-package.js.map +1 -0
- package/dist/cjs/src/module-errors.d.ts +24 -0
- package/dist/cjs/src/module-errors.d.ts.map +1 -1
- package/dist/cjs/src/module-errors.js +55 -0
- package/dist/cjs/src/module-errors.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/driver-import.d.ts +14 -1
- package/dist/esm/src/driver-import.d.ts.map +1 -1
- package/dist/esm/src/driver-import.js +49 -2
- package/dist/esm/src/driver-import.js.map +1 -1
- package/dist/esm/src/entry-point.d.ts +20 -0
- package/dist/esm/src/entry-point.d.ts.map +1 -0
- package/dist/esm/src/entry-point.js +59 -0
- package/dist/esm/src/entry-point.js.map +1 -0
- package/dist/esm/src/errors.d.ts +23 -0
- package/dist/esm/src/errors.d.ts.map +1 -1
- package/dist/esm/src/errors.js +27 -0
- package/dist/esm/src/errors.js.map +1 -1
- package/dist/esm/src/index.d.ts +1 -1
- package/dist/esm/src/index.d.ts.map +1 -1
- package/dist/esm/src/index.js +1 -1
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/loader.d.ts +10 -6
- package/dist/esm/src/loader.d.ts.map +1 -1
- package/dist/esm/src/loader.js +26 -71
- package/dist/esm/src/loader.js.map +1 -1
- package/dist/esm/src/local-package.d.ts +19 -0
- package/dist/esm/src/local-package.d.ts.map +1 -0
- package/dist/esm/src/local-package.js +47 -0
- package/dist/esm/src/local-package.js.map +1 -0
- package/dist/esm/src/module-errors.d.ts +24 -0
- package/dist/esm/src/module-errors.d.ts.map +1 -1
- package/dist/esm/src/module-errors.js +54 -0
- package/dist/esm/src/module-errors.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
package/dist/cjs/src/loader.js
CHANGED
|
@@ -37,11 +37,23 @@ exports.clearDriverCache = clearDriverCache;
|
|
|
37
37
|
exports.getDriverCacheStats = getDriverCacheStats;
|
|
38
38
|
exports.loadDriver = loadDriver;
|
|
39
39
|
const promises_1 = require("fs/promises");
|
|
40
|
-
const path_1 = require("path");
|
|
41
40
|
const config_validator_js_1 = require("./config-validator.js");
|
|
42
41
|
const driver_import_js_1 = require("./driver-import.js");
|
|
43
42
|
const driver_name_js_1 = require("./driver-name.js");
|
|
44
43
|
const errors_js_1 = require("./errors.js");
|
|
44
|
+
const local_package_js_1 = require("./local-package.js");
|
|
45
|
+
/**
|
|
46
|
+
* Native `import()`, in the CommonJS build as well
|
|
47
|
+
*
|
|
48
|
+
* The CommonJS build compiles `import()` to `require()`, which cannot load a `file:` URL.
|
|
49
|
+
* Going through `Function` keeps the native `import()`, which also loads ES modules.
|
|
50
|
+
*/
|
|
51
|
+
/* istanbul ignore next */
|
|
52
|
+
const nativeImport = (specifier) => {
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval -- keeps `import()` out of the CommonJS transform
|
|
54
|
+
const importFn = new Function('specifier', 'return import(specifier)');
|
|
55
|
+
return importFn(specifier);
|
|
56
|
+
};
|
|
45
57
|
/**
|
|
46
58
|
* Default system dependencies using Node.js built-ins
|
|
47
59
|
*/
|
|
@@ -50,7 +62,7 @@ const defaultDeps = {
|
|
|
50
62
|
/* istanbul ignore next */
|
|
51
63
|
readFile: (path, encoding) => (0, promises_1.readFile)(path, encoding),
|
|
52
64
|
/* istanbul ignore next */
|
|
53
|
-
importModule: (
|
|
65
|
+
importModule: (specifier) => specifier.startsWith('file:') ? nativeImport(specifier) : Promise.resolve(`${specifier}`).then(s => __importStar(require(s))),
|
|
54
66
|
/* istanbul ignore next */
|
|
55
67
|
getCwd: () => process.cwd(),
|
|
56
68
|
};
|
|
@@ -85,76 +97,24 @@ function getDriverCacheStats() {
|
|
|
85
97
|
size: driverCache.size,
|
|
86
98
|
};
|
|
87
99
|
}
|
|
88
|
-
/**
|
|
89
|
-
* Auto-detect local driver package from package.json
|
|
90
|
-
*
|
|
91
|
-
* @param deps - System dependencies
|
|
92
|
-
* @returns Package name if valid driver package
|
|
93
|
-
* @throws Error if not a valid driver package
|
|
94
|
-
*/
|
|
95
|
-
async function detectLocalPackage(deps) {
|
|
96
|
-
try {
|
|
97
|
-
const packageJsonPath = (0, path_1.join)(deps.getCwd(), 'package.json');
|
|
98
|
-
const packageJsonContent = await deps.readFile(packageJsonPath, 'utf-8');
|
|
99
|
-
const packageJson = JSON.parse(packageJsonContent);
|
|
100
|
-
const keywords = packageJson.keywords ?? [];
|
|
101
|
-
if (!keywords.includes('ya-modbus-driver')) {
|
|
102
|
-
throw new errors_js_1.PackageJsonError('Current package is not a ya-modbus driver. ' +
|
|
103
|
-
'Add "ya-modbus-driver" to keywords in package.json');
|
|
104
|
-
}
|
|
105
|
-
const name = packageJson.name;
|
|
106
|
-
if (!name) {
|
|
107
|
-
throw new errors_js_1.PackageJsonError('package.json must have a "name" field');
|
|
108
|
-
}
|
|
109
|
-
return name;
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
if (error.code === 'ENOENT') {
|
|
113
|
-
throw new errors_js_1.PackageJsonError('package.json not found in current directory. ' +
|
|
114
|
-
'Run this command from a driver package directory or specify --driver');
|
|
115
|
-
}
|
|
116
|
-
if (error instanceof SyntaxError) {
|
|
117
|
-
throw new errors_js_1.PackageJsonError('Failed to parse package.json: invalid JSON');
|
|
118
|
-
}
|
|
119
|
-
throw error;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Try to import a module with multiple path attempts
|
|
124
|
-
*
|
|
125
|
-
* @param paths - Array of module paths to try
|
|
126
|
-
* @param deps - System dependencies
|
|
127
|
-
* @returns Imported module
|
|
128
|
-
* @throws Error if all paths fail
|
|
129
|
-
*/
|
|
130
|
-
async function tryImport(paths, deps) {
|
|
131
|
-
const errors = [];
|
|
132
|
-
for (const path of paths) {
|
|
133
|
-
try {
|
|
134
|
-
return await deps.importModule(path);
|
|
135
|
-
}
|
|
136
|
-
catch (error) {
|
|
137
|
-
errors.push(error);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
const lastError = errors[errors.length - 1];
|
|
141
|
-
throw new Error(`Failed to import module from any path. Tried: ${paths.join(', ')}\nLast error: ${lastError?.message}`, { cause: lastError });
|
|
142
|
-
}
|
|
143
100
|
/**
|
|
144
101
|
* Load a driver package dynamically
|
|
145
102
|
*
|
|
146
103
|
* Supports two modes:
|
|
147
|
-
* 1. Explicit package: `loadDriver({ driverPackage: 'ya-modbus
|
|
104
|
+
* 1. Explicit package: `loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })`
|
|
148
105
|
* 2. Auto-detect from cwd: `loadDriver({})` - reads package.json from current directory
|
|
149
106
|
*
|
|
150
|
-
* Auto-detection checks for `ya-modbus-driver` keyword in package.json keywords
|
|
107
|
+
* Auto-detection checks for `ya-modbus-driver` keyword in package.json keywords and loads
|
|
108
|
+
* the entry file that `import` of the package resolves to (`exports["."]`, else `main`,
|
|
109
|
+
* else `index.js`). The driver must be built: the entry file is JavaScript, or TypeScript
|
|
110
|
+
* that Node can run itself.
|
|
151
111
|
*
|
|
152
112
|
* @example
|
|
153
113
|
* // Auto-detect from current directory
|
|
154
114
|
* const driver = await loadDriver({})
|
|
155
115
|
*
|
|
156
116
|
* // Load explicit package
|
|
157
|
-
* const driver = await loadDriver({ driverPackage: 'ya-modbus
|
|
117
|
+
* const driver = await loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })
|
|
158
118
|
*
|
|
159
119
|
* @param options - Loading options (defaults to local package detection)
|
|
160
120
|
* @param deps - System dependencies (for testing, uses Node.js built-ins by default)
|
|
@@ -181,21 +141,15 @@ async function loadDriver(options = {}, deps = defaultDeps) {
|
|
|
181
141
|
driverModule = imported.driverModule;
|
|
182
142
|
}
|
|
183
143
|
else {
|
|
184
|
-
|
|
144
|
+
const localPackage = await (0, local_package_js_1.detectLocalPackage)(deps);
|
|
145
|
+
packageName = localPackage.name;
|
|
185
146
|
const cached = driverCache.get(packageName);
|
|
186
147
|
if (cached) {
|
|
187
148
|
cacheStats.hits++;
|
|
188
149
|
return cached;
|
|
189
150
|
}
|
|
190
151
|
cacheStats.misses++;
|
|
191
|
-
|
|
192
|
-
const importPaths = [
|
|
193
|
-
(0, path_1.join)(cwd, 'src', 'index.js'),
|
|
194
|
-
(0, path_1.join)(cwd, 'src', 'index.ts'),
|
|
195
|
-
(0, path_1.join)(cwd, 'dist', 'index.js'),
|
|
196
|
-
packageName,
|
|
197
|
-
];
|
|
198
|
-
driverModule = await tryImport(importPaths, deps);
|
|
152
|
+
driverModule = await (0, driver_import_js_1.importLocalDriver)(packageName, localPackage.entryPoint, deps);
|
|
199
153
|
}
|
|
200
154
|
if (!driverModule || typeof driverModule !== 'object') {
|
|
201
155
|
throw new errors_js_1.ValidationError('Driver package must export a createDriver function', 'createDriver');
|
|
@@ -231,6 +185,7 @@ async function loadDriver(options = {}, deps = defaultDeps) {
|
|
|
231
185
|
catch (error) {
|
|
232
186
|
if (error instanceof errors_js_1.ValidationError ||
|
|
233
187
|
error instanceof errors_js_1.DriverNotFoundError ||
|
|
188
|
+
error instanceof errors_js_1.DriverNotBuiltError ||
|
|
234
189
|
error instanceof errors_js_1.PackageJsonError) {
|
|
235
190
|
throw error;
|
|
236
191
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../../src/loader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../../src/loader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2KA,4CAIC;AAMD,kDAMC;AA0BD,gCA+GC;AApUD,0CAAsC;AAStC,+DAM8B;AAC9B,yDAA2E;AAC3E,qDAA6D;AAC7D,2CAKoB;AACpB,yDAAuD;AA2FvD;;;;;GAKG;AACH,0BAA0B;AAC1B,MAAM,YAAY,GAAG,CAAC,SAAiB,EAAoB,EAAE;IAC3D,gHAAgH;IAChH,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,WAAW,EAAE,0BAA0B,CAAwB,CAAA;IAC7F,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAA;AAC5B,CAAC,CAAA;AAED;;GAEG;AACH,0BAA0B;AAC1B,MAAM,WAAW,GAAuB;IACtC,0BAA0B;IAC1B,QAAQ,EAAE,CAAC,IAAY,EAAE,QAAiB,EAAE,EAAE,CAAC,IAAA,mBAAQ,EAAC,IAAI,EAAE,QAAQ,CAAC;IACvE,0BAA0B;IAC1B,YAAY,EAAE,CAAC,SAAiB,EAAE,EAAE,CAClC,SAAS,C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AC,sCAAsC,CAAC,CAAA;IACzD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SystemDependencies } from './loader.js';
|
|
2
|
+
/**
|
|
3
|
+
* A driver package found in the current directory
|
|
4
|
+
*/
|
|
5
|
+
export interface LocalPackage {
|
|
6
|
+
/** Name from package.json */
|
|
7
|
+
name: string;
|
|
8
|
+
/** Absolute path of the file that importing the package loads */
|
|
9
|
+
entryPoint: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Auto-detect local driver package from package.json
|
|
13
|
+
*
|
|
14
|
+
* @param deps - System dependencies
|
|
15
|
+
* @returns Package name and entry file if valid driver package
|
|
16
|
+
* @throws Error if not a valid driver package
|
|
17
|
+
*/
|
|
18
|
+
export declare function detectLocalPackage(deps: Pick<SystemDependencies, 'getCwd' | 'readFile'>): Promise<LocalPackage>;
|
|
19
|
+
//# sourceMappingURL=local-package.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-package.d.ts","sourceRoot":"","sources":["../../../src/local-package.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAErD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,iEAAiE;IACjE,UAAU,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACtC,IAAI,EAAE,IAAI,CAAC,kBAAkB,EAAE,QAAQ,GAAG,UAAU,CAAC,GACpD,OAAO,CAAC,YAAY,CAAC,CAkDvB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.detectLocalPackage = detectLocalPackage;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const entry_point_js_1 = require("./entry-point.js");
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
/**
|
|
8
|
+
* Auto-detect local driver package from package.json
|
|
9
|
+
*
|
|
10
|
+
* @param deps - System dependencies
|
|
11
|
+
* @returns Package name and entry file if valid driver package
|
|
12
|
+
* @throws Error if not a valid driver package
|
|
13
|
+
*/
|
|
14
|
+
async function detectLocalPackage(deps) {
|
|
15
|
+
try {
|
|
16
|
+
const cwd = deps.getCwd();
|
|
17
|
+
const packageJsonContent = await deps.readFile((0, node_path_1.join)(cwd, 'package.json'), 'utf-8');
|
|
18
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
19
|
+
const keywords = packageJson.keywords ?? [];
|
|
20
|
+
if (!keywords.includes('ya-modbus-driver')) {
|
|
21
|
+
throw new errors_js_1.PackageJsonError('Current package is not a ya-modbus driver. ' +
|
|
22
|
+
'Add "ya-modbus-driver" to keywords in package.json');
|
|
23
|
+
}
|
|
24
|
+
const name = packageJson.name;
|
|
25
|
+
if (!name) {
|
|
26
|
+
throw new errors_js_1.PackageJsonError('package.json must have a "name" field');
|
|
27
|
+
}
|
|
28
|
+
const entry = (0, entry_point_js_1.resolvePackageEntry)(packageJson);
|
|
29
|
+
if (entry === undefined) {
|
|
30
|
+
throw new errors_js_1.PackageJsonError('package.json "exports" has no entry for "." that can be imported. ' +
|
|
31
|
+
'Add an "import" or "default" condition to exports["."]');
|
|
32
|
+
}
|
|
33
|
+
if ((0, node_path_1.extname)(entry) === '') {
|
|
34
|
+
throw new errors_js_1.PackageJsonError(`The entry point "${entry}" in package.json has no file extension. ` +
|
|
35
|
+
'Name the built file, for example "./dist/index.js"');
|
|
36
|
+
}
|
|
37
|
+
return { name, entryPoint: (0, node_path_1.join)(cwd, entry) };
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error.code === 'ENOENT') {
|
|
41
|
+
throw new errors_js_1.PackageJsonError('package.json not found in current directory. ' +
|
|
42
|
+
'Run this command from a driver package directory or specify --driver');
|
|
43
|
+
}
|
|
44
|
+
if (error instanceof SyntaxError) {
|
|
45
|
+
throw new errors_js_1.PackageJsonError('Failed to parse package.json: invalid JSON');
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=local-package.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-package.js","sourceRoot":"","sources":["../../../src/local-package.ts"],"names":[],"mappings":";;AAuBA,gDAoDC;AA3ED,yCAAyC;AAEzC,qDAA+E;AAC/E,2CAA8C;AAa9C;;;;;;GAMG;AACI,KAAK,UAAU,kBAAkB,CACtC,IAAqD;IAErD,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAA;QACzB,MAAM,kBAAkB,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAA,gBAAI,EAAC,GAAG,EAAE,cAAc,CAAC,EAAE,OAAO,CAAC,CAAA;QAClF,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAGhD,CAAA;QAED,MAAM,QAAQ,GAAG,WAAW,CAAC,QAAQ,IAAI,EAAE,CAAA;QAC3C,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,4BAAgB,CACxB,6CAA6C;gBAC3C,oDAAoD,CACvD,CAAA;QACH,CAAC;QAED,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAA;QAC7B,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,IAAI,4BAAgB,CAAC,uCAAuC,CAAC,CAAA;QACrE,CAAC;QAED,MAAM,KAAK,GAAG,IAAA,oCAAmB,EAAC,WAAW,CAAC,CAAA;QAC9C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,MAAM,IAAI,4BAAgB,CACxB,oEAAoE;gBAClE,wDAAwD,CAC3D,CAAA;QACH,CAAC;QAED,IAAI,IAAA,mBAAO,EAAC,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;YAC1B,MAAM,IAAI,4BAAgB,CACxB,oBAAoB,KAAK,2CAA2C;gBAClE,oDAAoD,CACvD,CAAA;QACH,CAAC;QAED,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAA,gBAAI,EAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAAA;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACvD,MAAM,IAAI,4BAAgB,CACxB,+CAA+C;gBAC7C,sEAAsE,CACzE,CAAA;QACH,CAAC;QACD,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,IAAI,4BAAgB,CAAC,4CAA4C,CAAC,CAAA;QAC1E,CAAC;QACD,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -6,4 +6,28 @@
|
|
|
6
6
|
* Node names the package in quotes, in both the ESM and the CommonJS wording.
|
|
7
7
|
*/
|
|
8
8
|
export declare function isMissingPackageError(error: unknown, packageName: string): boolean;
|
|
9
|
+
/** Why a driver cannot be loaded as built JavaScript */
|
|
10
|
+
export type NotBuiltCause =
|
|
11
|
+
/** A file of the driver is missing, such as the entry file when there is no `dist` directory */
|
|
12
|
+
'missing-file'
|
|
13
|
+
/** The driver is TypeScript source and Node does not run it, because it is too old or has type stripping off */
|
|
14
|
+
| 'typescript'
|
|
15
|
+
/** Node strips types, but the TypeScript uses syntax or imports that type stripping cannot run */
|
|
16
|
+
| 'typescript-unsupported'
|
|
17
|
+
/** Node does not run TypeScript from `node_modules` */
|
|
18
|
+
| 'typescript-in-node-modules';
|
|
19
|
+
/**
|
|
20
|
+
* Find out if an import failed because the driver is not built
|
|
21
|
+
*
|
|
22
|
+
* A missing file counts when Node names it by its absolute path. A package that is not
|
|
23
|
+
* installed, or a missing dependency, is named by its package name, so neither matches.
|
|
24
|
+
* With `entryPoint` (a local driver) only the entry file itself counts. Another missing
|
|
25
|
+
* file of a built driver is a bug in the driver, which building does not fix, unless
|
|
26
|
+
* TypeScript source asks for it (importing `./x.js` when only `x.ts` exists).
|
|
27
|
+
*
|
|
28
|
+
* @param error - What the import threw
|
|
29
|
+
* @param entryPoint - Absolute path of the entry file, if it is known
|
|
30
|
+
* @returns The cause, or undefined if the error is about something else
|
|
31
|
+
*/
|
|
32
|
+
export declare function getNotBuiltCause(error: unknown, entryPoint?: string): NotBuiltCause | undefined;
|
|
9
33
|
//# sourceMappingURL=module-errors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module-errors.d.ts","sourceRoot":"","sources":["../../../src/module-errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"module-errors.d.ts","sourceRoot":"","sources":["../../../src/module-errors.ts"],"names":[],"mappings":"AAaA;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAYlF;AAED,wDAAwD;AACxD,MAAM,MAAM,aAAa;AACvB,gGAAgG;AAC9F,cAAc;AAChB,gHAAgH;GAC9G,YAAY;AACd,kGAAkG;GAChG,wBAAwB;AAC1B,uDAAuD;GACrD,4BAA4B,CAAA;AAEhC;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAiC/F"}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isMissingPackageError = isMissingPackageError;
|
|
4
|
+
exports.getNotBuiltCause = getNotBuiltCause;
|
|
4
5
|
const NOT_FOUND_CODES = new Set(['ERR_MODULE_NOT_FOUND', 'MODULE_NOT_FOUND']);
|
|
6
|
+
/** Node does not know TypeScript files: too old, or type stripping is off */
|
|
7
|
+
const UNKNOWN_TYPESCRIPT = /Unknown file extension "\.[cm]?ts"/;
|
|
8
|
+
/**
|
|
9
|
+
* What Node could not find (a package name, or the absolute path of a file on POSIX or
|
|
10
|
+
* Windows), and the file that wanted it (not in the CommonJS wording)
|
|
11
|
+
*/
|
|
12
|
+
const MISSING_TARGET = /Cannot find (?:module|package) '([^']+)'(?: imported from ([^\n]+))?/;
|
|
13
|
+
const ABSOLUTE_PATH = /^(?:[/\\]|[A-Za-z]:)/;
|
|
14
|
+
const TYPESCRIPT_FILE = /\.[cm]?tsx?$/;
|
|
5
15
|
/**
|
|
6
16
|
* Check if an import failed because the package itself is not installed
|
|
7
17
|
*
|
|
@@ -19,4 +29,49 @@ function isMissingPackageError(error, packageName) {
|
|
|
19
29
|
typeof message === 'string' &&
|
|
20
30
|
message.includes(`'${packageName}'`));
|
|
21
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Find out if an import failed because the driver is not built
|
|
34
|
+
*
|
|
35
|
+
* A missing file counts when Node names it by its absolute path. A package that is not
|
|
36
|
+
* installed, or a missing dependency, is named by its package name, so neither matches.
|
|
37
|
+
* With `entryPoint` (a local driver) only the entry file itself counts. Another missing
|
|
38
|
+
* file of a built driver is a bug in the driver, which building does not fix, unless
|
|
39
|
+
* TypeScript source asks for it (importing `./x.js` when only `x.ts` exists).
|
|
40
|
+
*
|
|
41
|
+
* @param error - What the import threw
|
|
42
|
+
* @param entryPoint - Absolute path of the entry file, if it is known
|
|
43
|
+
* @returns The cause, or undefined if the error is about something else
|
|
44
|
+
*/
|
|
45
|
+
function getNotBuiltCause(error, entryPoint) {
|
|
46
|
+
if (typeof error !== 'object' || error === null) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const { code, message } = error;
|
|
50
|
+
if (code === 'ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING') {
|
|
51
|
+
return 'typescript-in-node-modules';
|
|
52
|
+
}
|
|
53
|
+
if (code === 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX') {
|
|
54
|
+
return 'typescript-unsupported';
|
|
55
|
+
}
|
|
56
|
+
if (typeof code !== 'string' || typeof message !== 'string') {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
if (code === 'ERR_UNKNOWN_FILE_EXTENSION') {
|
|
60
|
+
if (!UNKNOWN_TYPESCRIPT.test(message)) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return message.includes('node_modules') ? 'typescript-in-node-modules' : 'typescript';
|
|
64
|
+
}
|
|
65
|
+
if (!NOT_FOUND_CODES.has(code)) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const [, missing, importer = ''] = MISSING_TARGET.exec(message) ?? [];
|
|
69
|
+
if (missing === undefined || !ABSOLUTE_PATH.test(missing)) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
if (TYPESCRIPT_FILE.test(importer)) {
|
|
73
|
+
return 'typescript-unsupported';
|
|
74
|
+
}
|
|
75
|
+
return entryPoint === undefined || missing === entryPoint ? 'missing-file' : undefined;
|
|
76
|
+
}
|
|
22
77
|
//# sourceMappingURL=module-errors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module-errors.js","sourceRoot":"","sources":["../../../src/module-errors.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"module-errors.js","sourceRoot":"","sources":["../../../src/module-errors.ts"],"names":[],"mappings":";;AAoBA,sDAYC;AA0BD,4CAiCC;AA3FD,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,sBAAsB,EAAE,kBAAkB,CAAC,CAAC,CAAA;AAE7E,6EAA6E;AAC7E,MAAM,kBAAkB,GAAG,oCAAoC,CAAA;AAE/D;;;GAGG;AACH,MAAM,cAAc,GAAG,sEAAsE,CAAA;AAC7F,MAAM,aAAa,GAAG,sBAAsB,CAAA;AAC5C,MAAM,eAAe,GA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