@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
|
@@ -1,5 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { pathToFileURL } from 'node:url';
|
|
2
|
+
import { DriverNotBuiltError, DriverNotFoundError } from './errors.js';
|
|
3
|
+
import { getNotBuiltCause, isMissingPackageError } from './module-errors.js';
|
|
4
|
+
const NOT_BUILT_HINTS = {
|
|
5
|
+
'missing-file': 'Drivers are loaded as built JavaScript. Build the driver first (npm run build).',
|
|
6
|
+
typescript: 'Drivers are loaded as built JavaScript. Build the driver first (npm run build), ' +
|
|
7
|
+
"or run with Node's built-in type stripping (Node 22.18+ or 24) to load TypeScript source.",
|
|
8
|
+
'typescript-unsupported': 'Drivers are loaded as built JavaScript. Build the driver first (npm run build). ' +
|
|
9
|
+
"Node's type stripping needs erasable syntax (no enums) and imports that name files with their .ts extension.",
|
|
10
|
+
'typescript-in-node-modules': 'Node does not run TypeScript from node_modules, so a package must ship built JavaScript.',
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Error for a driver that cannot be loaded because it is not built
|
|
14
|
+
*
|
|
15
|
+
* Shows what Node reported. The "imported from" part is left out when `hideImporter` is set,
|
|
16
|
+
* because then it only names the loader.
|
|
17
|
+
*/
|
|
18
|
+
function createNotBuiltError(packageName, cause, notBuilt, hideImporter = false) {
|
|
19
|
+
const firstLine = cause.message.replace(/\n[\s\S]*/, '');
|
|
20
|
+
const reason = hideImporter ? firstLine.replace(/ imported from .*$/, '') : firstLine;
|
|
21
|
+
return new DriverNotBuiltError(`Cannot load driver ${packageName}: ${reason}\n${NOT_BUILT_HINTS[notBuilt]}`, packageName, { cause });
|
|
22
|
+
}
|
|
3
23
|
/**
|
|
4
24
|
* Import the first installed package of a list of candidates
|
|
5
25
|
*
|
|
@@ -10,6 +30,7 @@ import { isMissingPackageError } from './module-errors.js';
|
|
|
10
30
|
* @param candidates - Package names to try, in order
|
|
11
31
|
* @param deps - System dependencies
|
|
12
32
|
* @returns The imported module and the name of the package it came from
|
|
33
|
+
* @throws DriverNotBuiltError if the installed package is not built
|
|
13
34
|
* @throws DriverNotFoundError if no candidate is installed or the installed one fails to load
|
|
14
35
|
*/
|
|
15
36
|
export async function importDriverPackage(name, candidates, deps) {
|
|
@@ -18,6 +39,10 @@ export async function importDriverPackage(name, candidates, deps) {
|
|
|
18
39
|
return { packageName, driverModule: await deps.importModule(packageName) };
|
|
19
40
|
}
|
|
20
41
|
catch (error) {
|
|
42
|
+
const notBuilt = getNotBuiltCause(error);
|
|
43
|
+
if (notBuilt) {
|
|
44
|
+
throw createNotBuiltError(packageName, error, notBuilt);
|
|
45
|
+
}
|
|
21
46
|
if (!isMissingPackageError(error, packageName)) {
|
|
22
47
|
const reason = error instanceof Error ? error.message : String(error);
|
|
23
48
|
throw new DriverNotFoundError(`Failed to load driver package ${packageName}: ${reason}`, packageName, { cause: error });
|
|
@@ -28,4 +53,26 @@ export async function importDriverPackage(name, candidates, deps) {
|
|
|
28
53
|
const tried = candidates.length > 1 ? `\nTried: ${candidates.join(', ')}` : '';
|
|
29
54
|
throw new DriverNotFoundError(`Driver package not found: ${name}${tried}\nInstall it with: npm install ${preferred}`, preferred);
|
|
30
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Import the entry file of the driver package in the current directory
|
|
58
|
+
*
|
|
59
|
+
* @param packageName - Name from package.json
|
|
60
|
+
* @param entryPoint - Absolute path of the entry file
|
|
61
|
+
* @param deps - System dependencies
|
|
62
|
+
* @returns The imported module
|
|
63
|
+
* @throws DriverNotBuiltError if the entry file is missing or is TypeScript that Node cannot run
|
|
64
|
+
*/
|
|
65
|
+
export async function importLocalDriver(packageName, entryPoint, deps) {
|
|
66
|
+
try {
|
|
67
|
+
return await deps.importModule(pathToFileURL(entryPoint).href);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
const notBuilt = getNotBuiltCause(error, entryPoint);
|
|
71
|
+
if (notBuilt === undefined) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
// The missing entry file is reported as imported by the loader, which is no help
|
|
75
|
+
throw createNotBuiltError(packageName, error, notBuilt, notBuilt === 'missing-file');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
31
78
|
//# sourceMappingURL=driver-import.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"driver-import.js","sourceRoot":"","sources":["../../../src/driver-import.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"driver-import.js","sourceRoot":"","sources":["../../../src/driver-import.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAGxC,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAEtE,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAsB,MAAM,oBAAoB,CAAA;AAIhG,MAAM,eAAe,GAAkC;IACrD,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,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;gBACrE,MAAM,IAAI,mBAAmB,CAC3B,iCAAiC,WAAW,KAAK,MAAM,EAAE,EACzD,WAAW,EACX,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAA;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,GAAG,UAAU,CAAA;IAC9B,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;IAC9E,MAAM,IAAI,mBAAmB,CAC3B,6BAA6B,IAAI,GAAG,KAAK,kCAAkC,SAAS,EAAE,EACtF,SAAS,CACV,CAAA;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,WAAmB,EACnB,UAAkB,EAClB,IAAgB;IAEhB,IAAI,CAAC;QACH,OAAO,MAAM,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAA;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,QAAQ,GAAG,gBAAgB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAA;QACpD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,KAAK,CAAA;QACb,CAAC;QACD,iFAAiF;QACjF,MAAM,mBAAmB,CAAC,WAAW,EAAE,KAAc,EAAE,QAAQ,EAAE,QAAQ,KAAK,cAAc,CAAC,CAAA;IAC/F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conditions Node uses for `import()` when it resolves package `exports`.
|
|
3
|
+
* Other runtimes use other conditions (for example `deno` or `bun`).
|
|
4
|
+
*/
|
|
5
|
+
export declare const IMPORT_CONDITIONS: readonly string[];
|
|
6
|
+
/** The package.json fields that decide where a package is entered */
|
|
7
|
+
export interface PackageEntryFields {
|
|
8
|
+
main?: unknown;
|
|
9
|
+
exports?: unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Find the file that `import('<package>')` loads, from a package.json
|
|
13
|
+
*
|
|
14
|
+
* Uses `exports["."]` when the package has `exports`, otherwise `main`, otherwise `index.js`,
|
|
15
|
+
* like Node does. Paths are relative to the package directory.
|
|
16
|
+
*
|
|
17
|
+
* @returns The relative path, or undefined if `exports` has no entry for these conditions
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolvePackageEntry(packageJson: PackageEntryFields, conditions?: readonly string[]): string | undefined;
|
|
20
|
+
//# sourceMappingURL=entry-point.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entry-point.d.ts","sourceRoot":"","sources":["../../../src/entry-point.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,MAAM,EAAiD,CAAA;AAEhG,qEAAqE;AACrE,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAwCD;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,WAAW,EAAE,kBAAkB,EAC/B,UAAU,GAAE,SAAS,MAAM,EAAsB,GAChD,MAAM,GAAG,SAAS,CAoBpB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conditions Node uses for `import()` when it resolves package `exports`.
|
|
3
|
+
* Other runtimes use other conditions (for example `deno` or `bun`).
|
|
4
|
+
*/
|
|
5
|
+
export const IMPORT_CONDITIONS = ['node', 'import', 'module-sync', 'default'];
|
|
6
|
+
/**
|
|
7
|
+
* Resolve a conditional `exports` target to a file path
|
|
8
|
+
*
|
|
9
|
+
* Follows the Node rules: the first key that matches a condition and leads somewhere wins.
|
|
10
|
+
* A `null` condition blocks the path, so the search stops there. In an array, a `null`
|
|
11
|
+
* result is skipped and the next item is tried.
|
|
12
|
+
*/
|
|
13
|
+
function resolveTarget(target, conditions) {
|
|
14
|
+
if (target === null) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
if (typeof target === 'string') {
|
|
18
|
+
return target.startsWith('./') ? target : undefined;
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(target)) {
|
|
21
|
+
for (const item of target) {
|
|
22
|
+
const resolved = resolveTarget(item, conditions);
|
|
23
|
+
if (typeof resolved === 'string') {
|
|
24
|
+
return resolved;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
if (typeof target === 'object') {
|
|
30
|
+
for (const [condition, value] of Object.entries(target)) {
|
|
31
|
+
const resolved = conditions.includes(condition) ? resolveTarget(value, conditions) : undefined;
|
|
32
|
+
if (resolved !== undefined) {
|
|
33
|
+
return resolved;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Find the file that `import('<package>')` loads, from a package.json
|
|
41
|
+
*
|
|
42
|
+
* Uses `exports["."]` when the package has `exports`, otherwise `main`, otherwise `index.js`,
|
|
43
|
+
* like Node does. Paths are relative to the package directory.
|
|
44
|
+
*
|
|
45
|
+
* @returns The relative path, or undefined if `exports` has no entry for these conditions
|
|
46
|
+
*/
|
|
47
|
+
export function resolvePackageEntry(packageJson, conditions = IMPORT_CONDITIONS) {
|
|
48
|
+
// Not destructured as `exports`: in the CommonJS build that would shadow the module's own exports
|
|
49
|
+
const { main, exports: exportsField } = packageJson;
|
|
50
|
+
if (exportsField === undefined || exportsField === null) {
|
|
51
|
+
return typeof main === 'string' ? main : 'index.js';
|
|
52
|
+
}
|
|
53
|
+
// `exports` is either a map of subpaths ("." and "./x") or the target of "." itself
|
|
54
|
+
const isSubpathMap = typeof exportsField === 'object' &&
|
|
55
|
+
!Array.isArray(exportsField) &&
|
|
56
|
+
Object.keys(exportsField).some((key) => key.startsWith('.'));
|
|
57
|
+
return (resolveTarget(isSubpathMap ? exportsField['.'] : exportsField, conditions) ?? undefined);
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=entry-point.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entry-point.js","sourceRoot":"","sources":["../../../src/entry-point.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAsB,CAAC,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,SAAS,CAAC,CAAA;AAQhG;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,MAAe,EAAE,UAA6B;IACnE,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAA;IACrD,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,KAAK,MAAM,IAAI,IAAI,MAAmB,EAAE,CAAC;YACvC,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;YAChD,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;gBACjC,OAAO,QAAQ,CAAA;YACjB,CAAC;QACH,CAAC;QACD,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,KAAK,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAiC,CAAC,EAAE,CAAC;YACnF,MAAM,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;YAC9F,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;gBAC3B,OAAO,QAAQ,CAAA;YACjB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,WAA+B,EAC/B,aAAgC,iBAAiB;IAEjD,kGAAkG;IAClG,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,WAAW,CAAA;IAEnD,IAAI,YAAY,KAAK,SAAS,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;QACxD,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAA;IACrD,CAAC;IAED,oFAAoF;IACpF,MAAM,YAAY,GAChB,OAAO,YAAY,KAAK,QAAQ;QAChC,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC;QAC5B,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;IAE9D,OAAO,CACL,aAAa,CACX,YAAY,CAAC,CAAC,CAAE,YAAwC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,EAC5E,UAAU,CACX,IAAI,SAAS,CACf,CAAA;AACH,CAAC"}
|
package/dist/esm/src/errors.d.ts
CHANGED
|
@@ -52,6 +52,29 @@ export declare class DriverNotFoundError extends Error {
|
|
|
52
52
|
readonly packageName: string;
|
|
53
53
|
constructor(message: string, packageName: string, options?: ErrorOptions);
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Error thrown when a driver package is installed but cannot be loaded because it is not built
|
|
57
|
+
*
|
|
58
|
+
* Drivers are loaded as built JavaScript. This covers a missing entry file (for example
|
|
59
|
+
* no `dist` directory) and TypeScript source that Node cannot run.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* import { loadDriver, DriverNotBuiltError } from '@ya-modbus/driver-loader'
|
|
64
|
+
*
|
|
65
|
+
* try {
|
|
66
|
+
* await loadDriver({}) // Auto-detect from current directory
|
|
67
|
+
* } catch (error) {
|
|
68
|
+
* if (error instanceof DriverNotBuiltError) {
|
|
69
|
+
* console.error(`Build ${error.packageName} first: npm run build`)
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare class DriverNotBuiltError extends Error {
|
|
75
|
+
readonly packageName: string;
|
|
76
|
+
constructor(message: string, packageName: string, options?: ErrorOptions);
|
|
77
|
+
}
|
|
55
78
|
/**
|
|
56
79
|
* Error thrown when package.json is not found or invalid
|
|
57
80
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,eAAgB,SAAQ,KAAK;aAGtB,KAAK,CAAC,EAAE,MAAM;gBAD9B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,MAAM,YAAA;CAMjC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aAG1B,WAAW,EAAE,MAAM;gBADnC,OAAO,EAAE,MAAM,EACC,WAAW,EAAE,MAAM,EACnC,OAAO,CAAC,EAAE,YAAY;CAMzB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM;CAK5B"}
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,eAAgB,SAAQ,KAAK;aAGtB,KAAK,CAAC,EAAE,MAAM;gBAD9B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,MAAM,YAAA;CAMjC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aAG1B,WAAW,EAAE,MAAM;gBADnC,OAAO,EAAE,MAAM,EACC,WAAW,EAAE,MAAM,EACnC,OAAO,CAAC,EAAE,YAAY;CAMzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aAG1B,WAAW,EAAE,MAAM;gBADnC,OAAO,EAAE,MAAM,EACC,WAAW,EAAE,MAAM,EACnC,OAAO,CAAC,EAAE,YAAY;CAMzB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM;CAK5B"}
|
package/dist/esm/src/errors.js
CHANGED
|
@@ -60,6 +60,33 @@ export class DriverNotFoundError extends Error {
|
|
|
60
60
|
Object.setPrototypeOf(this, DriverNotFoundError.prototype);
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Error thrown when a driver package is installed but cannot be loaded because it is not built
|
|
65
|
+
*
|
|
66
|
+
* Drivers are loaded as built JavaScript. This covers a missing entry file (for example
|
|
67
|
+
* no `dist` directory) and TypeScript source that Node cannot run.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { loadDriver, DriverNotBuiltError } from '@ya-modbus/driver-loader'
|
|
72
|
+
*
|
|
73
|
+
* try {
|
|
74
|
+
* await loadDriver({}) // Auto-detect from current directory
|
|
75
|
+
* } catch (error) {
|
|
76
|
+
* if (error instanceof DriverNotBuiltError) {
|
|
77
|
+
* console.error(`Build ${error.packageName} first: npm run build`)
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export class DriverNotBuiltError extends Error {
|
|
83
|
+
constructor(message, packageName, options) {
|
|
84
|
+
super(message, options);
|
|
85
|
+
this.packageName = packageName;
|
|
86
|
+
this.name = 'DriverNotBuiltError';
|
|
87
|
+
Object.setPrototypeOf(this, DriverNotBuiltError.prototype);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
63
90
|
/**
|
|
64
91
|
* Error thrown when package.json is not found or invalid
|
|
65
92
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC,YACE,OAAe,EACC,KAAc;QAE9B,KAAK,CAAC,OAAO,CAAC,CAAA;QAFE,UAAK,GAAL,KAAK,CAAS;QAG9B,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAA;QAC7B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAA;IACxD,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YACE,OAAe,EACC,WAAmB,EACnC,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QAHP,gBAAW,GAAX,WAAW,CAAQ;QAInC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAA;QACjC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,mBAAmB,CAAC,SAAS,CAAC,CAAA;IAC5D,CAAC;CACF;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC,YAAY,OAAe;QACzB,KAAK,CAAC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;;;;;;;;GAgBG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAA;QAC9B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAA;IACzD,CAAC;CACF"}
|
package/dist/esm/src/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { loadDriver, clearDriverCache, getDriverCacheStats } from './loader.js';
|
|
|
2
2
|
export type { LoadedDriver, LoadDriverOptions, SystemDependencies, DriverCacheStats, Logger, } from './loader.js';
|
|
3
3
|
export { validateDefaultConfig, validateSupportedConfig, validateDevices, crossValidateConfigs, } from './config-validator.js';
|
|
4
4
|
export { DRIVER_PACKAGE_NAME_RULE, assertValidDriverPackageName, isValidDriverPackageName, } from './driver-package-name.js';
|
|
5
|
-
export { ValidationError, DriverNotFoundError, PackageJsonError } from './errors.js';
|
|
5
|
+
export { ValidationError, DriverNotFoundError, DriverNotBuiltError, PackageJsonError, } from './errors.js';
|
|
6
6
|
export { getCallerPackageInfo } from './package-info.js';
|
|
7
7
|
export type { PackageInfo } from './package-info.js';
|
|
8
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAC/E,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,MAAM,GACP,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,eAAe,EACf,oBAAoB,GACrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,wBAAwB,GACzB,MAAM,0BAA0B,CAAA;AACjC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAC/E,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,MAAM,GACP,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,eAAe,EACf,oBAAoB,GACrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,wBAAwB,GACzB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AACxD,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/esm/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { loadDriver, clearDriverCache, getDriverCacheStats } from './loader.js';
|
|
2
2
|
export { validateDefaultConfig, validateSupportedConfig, validateDevices, crossValidateConfigs, } from './config-validator.js';
|
|
3
3
|
export { DRIVER_PACKAGE_NAME_RULE, assertValidDriverPackageName, isValidDriverPackageName, } from './driver-package-name.js';
|
|
4
|
-
export { ValidationError, DriverNotFoundError, PackageJsonError } from './errors.js';
|
|
4
|
+
export { ValidationError, DriverNotFoundError, DriverNotBuiltError, PackageJsonError, } from './errors.js';
|
|
5
5
|
export { getCallerPackageInfo } from './package-info.js';
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAQ/E,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,eAAe,EACf,oBAAoB,GACrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,wBAAwB,GACzB,MAAM,0BAA0B,CAAA;AACjC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAQ/E,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,eAAe,EACf,oBAAoB,GACrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,wBAAwB,GACzB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/esm/src/loader.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ export interface Logger {
|
|
|
44
44
|
* const driver1 = await loadDriver({})
|
|
45
45
|
*
|
|
46
46
|
* // Load specific package
|
|
47
|
-
* const driver2 = await loadDriver({ driverPackage: 'ya-modbus
|
|
47
|
+
* const driver2 = await loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })
|
|
48
48
|
*
|
|
49
49
|
* // With custom logger
|
|
50
50
|
* const driver3 = await loadDriver({
|
|
@@ -59,7 +59,7 @@ export interface Logger {
|
|
|
59
59
|
export interface LoadDriverOptions {
|
|
60
60
|
/**
|
|
61
61
|
* Explicit driver package name
|
|
62
|
-
* e.g., 'ya-modbus
|
|
62
|
+
* e.g., '@ya-modbus/driver-xymd1' or '@org/ya-modbus-driver-xymd1'
|
|
63
63
|
*
|
|
64
64
|
* A short name such as 'xymd1' is tried as '@ya-modbus/driver-xymd1',
|
|
65
65
|
* then 'ya-modbus-driver-xymd1', then 'xymd1'.
|
|
@@ -76,7 +76,8 @@ export interface LoadDriverOptions {
|
|
|
76
76
|
*/
|
|
77
77
|
export interface SystemDependencies {
|
|
78
78
|
readFile: (path: string, encoding: 'utf-8') => Promise<string>;
|
|
79
|
-
|
|
79
|
+
/** Import a module by package name, or by `file:` URL for the entry file of a local package */
|
|
80
|
+
importModule: (specifier: string) => Promise<unknown>;
|
|
80
81
|
getCwd: () => string;
|
|
81
82
|
}
|
|
82
83
|
/**
|
|
@@ -104,17 +105,20 @@ export declare function getDriverCacheStats(): DriverCacheStats;
|
|
|
104
105
|
* Load a driver package dynamically
|
|
105
106
|
*
|
|
106
107
|
* Supports two modes:
|
|
107
|
-
* 1. Explicit package: `loadDriver({ driverPackage: 'ya-modbus
|
|
108
|
+
* 1. Explicit package: `loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })`
|
|
108
109
|
* 2. Auto-detect from cwd: `loadDriver({})` - reads package.json from current directory
|
|
109
110
|
*
|
|
110
|
-
* Auto-detection checks for `ya-modbus-driver` keyword in package.json keywords
|
|
111
|
+
* Auto-detection checks for `ya-modbus-driver` keyword in package.json keywords and loads
|
|
112
|
+
* the entry file that `import` of the package resolves to (`exports["."]`, else `main`,
|
|
113
|
+
* else `index.js`). The driver must be built: the entry file is JavaScript, or TypeScript
|
|
114
|
+
* that Node can run itself.
|
|
111
115
|
*
|
|
112
116
|
* @example
|
|
113
117
|
* // Auto-detect from current directory
|
|
114
118
|
* const driver = await loadDriver({})
|
|
115
119
|
*
|
|
116
120
|
* // Load explicit package
|
|
117
|
-
* const driver = await loadDriver({ driverPackage: 'ya-modbus
|
|
121
|
+
* const driver = await loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })
|
|
118
122
|
*
|
|
119
123
|
* @param options - Loading options (defaults to local package detection)
|
|
120
124
|
* @param deps - System dependencies (for testing, uses Node.js built-ins by default)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../src/loader.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../src/loader.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,oBAAoB,EACpB,aAAa,EACb,cAAc,EACd,eAAe,EAChB,MAAM,yBAAyB,CAAA;AAmBhC;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,8BAA8B;IAC9B,YAAY,EAAE,oBAAoB,CAAA;IAElC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,cAAc,CAAA;IAExB,mEAAmE;IACnE,aAAa,CAAC,EAAE,aAAa,CAAA;IAE7B,kEAAkE;IAClE,eAAe,CAAC,EAAE,eAAe,CAAA;CAClC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,MAAM;IACrB,2BAA2B;IAC3B,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/B,oCAAoC;IACpC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;CAClC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAC9D,+FAA+F;IAC/F,YAAY,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IACrD,MAAM,EAAE,MAAM,MAAM,CAAA;CACrB;AA6BD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,2BAA2B;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,+BAA+B;IAC/B,IAAI,EAAE,MAAM,CAAA;CACb;AAeD;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,CAIvC;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,gBAAgB,CAMtD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,UAAU,CAC9B,OAAO,GAAE,iBAAsB,EAC/B,IAAI,GAAE,kBAAgC,GACrC,OAAO,CAAC,YAAY,CAAC,CA4GvB"}
|
package/dist/esm/src/loader.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import { readFile } from 'fs/promises';
|
|
2
|
-
import { join } from 'path';
|
|
3
2
|
import { crossValidateConfigs, outputConfigWarnings, validateDefaultConfig, validateDevices, validateSupportedConfig, } from './config-validator.js';
|
|
4
|
-
import { importDriverPackage } from './driver-import.js';
|
|
3
|
+
import { importDriverPackage, importLocalDriver } from './driver-import.js';
|
|
5
4
|
import { getDriverPackageCandidates } from './driver-name.js';
|
|
6
|
-
import { DriverNotFoundError, PackageJsonError, ValidationError } from './errors.js';
|
|
5
|
+
import { DriverNotBuiltError, DriverNotFoundError, PackageJsonError, ValidationError, } from './errors.js';
|
|
6
|
+
import { detectLocalPackage } from './local-package.js';
|
|
7
|
+
/**
|
|
8
|
+
* Native `import()`, in the CommonJS build as well
|
|
9
|
+
*
|
|
10
|
+
* The CommonJS build compiles `import()` to `require()`, which cannot load a `file:` URL.
|
|
11
|
+
* Going through `Function` keeps the native `import()`, which also loads ES modules.
|
|
12
|
+
*/
|
|
13
|
+
/* istanbul ignore next */
|
|
14
|
+
const nativeImport = (specifier) => {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval -- keeps `import()` out of the CommonJS transform
|
|
16
|
+
const importFn = new Function('specifier', 'return import(specifier)');
|
|
17
|
+
return importFn(specifier);
|
|
18
|
+
};
|
|
7
19
|
/**
|
|
8
20
|
* Default system dependencies using Node.js built-ins
|
|
9
21
|
*/
|
|
@@ -12,7 +24,7 @@ const defaultDeps = {
|
|
|
12
24
|
/* istanbul ignore next */
|
|
13
25
|
readFile: (path, encoding) => readFile(path, encoding),
|
|
14
26
|
/* istanbul ignore next */
|
|
15
|
-
importModule: (
|
|
27
|
+
importModule: (specifier) => specifier.startsWith('file:') ? nativeImport(specifier) : import(specifier),
|
|
16
28
|
/* istanbul ignore next */
|
|
17
29
|
getCwd: () => process.cwd(),
|
|
18
30
|
};
|
|
@@ -47,76 +59,24 @@ export function getDriverCacheStats() {
|
|
|
47
59
|
size: driverCache.size,
|
|
48
60
|
};
|
|
49
61
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Auto-detect local driver package from package.json
|
|
52
|
-
*
|
|
53
|
-
* @param deps - System dependencies
|
|
54
|
-
* @returns Package name if valid driver package
|
|
55
|
-
* @throws Error if not a valid driver package
|
|
56
|
-
*/
|
|
57
|
-
async function detectLocalPackage(deps) {
|
|
58
|
-
try {
|
|
59
|
-
const packageJsonPath = join(deps.getCwd(), 'package.json');
|
|
60
|
-
const packageJsonContent = await deps.readFile(packageJsonPath, 'utf-8');
|
|
61
|
-
const packageJson = JSON.parse(packageJsonContent);
|
|
62
|
-
const keywords = packageJson.keywords ?? [];
|
|
63
|
-
if (!keywords.includes('ya-modbus-driver')) {
|
|
64
|
-
throw new PackageJsonError('Current package is not a ya-modbus driver. ' +
|
|
65
|
-
'Add "ya-modbus-driver" to keywords in package.json');
|
|
66
|
-
}
|
|
67
|
-
const name = packageJson.name;
|
|
68
|
-
if (!name) {
|
|
69
|
-
throw new PackageJsonError('package.json must have a "name" field');
|
|
70
|
-
}
|
|
71
|
-
return name;
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
if (error.code === 'ENOENT') {
|
|
75
|
-
throw new PackageJsonError('package.json not found in current directory. ' +
|
|
76
|
-
'Run this command from a driver package directory or specify --driver');
|
|
77
|
-
}
|
|
78
|
-
if (error instanceof SyntaxError) {
|
|
79
|
-
throw new PackageJsonError('Failed to parse package.json: invalid JSON');
|
|
80
|
-
}
|
|
81
|
-
throw error;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Try to import a module with multiple path attempts
|
|
86
|
-
*
|
|
87
|
-
* @param paths - Array of module paths to try
|
|
88
|
-
* @param deps - System dependencies
|
|
89
|
-
* @returns Imported module
|
|
90
|
-
* @throws Error if all paths fail
|
|
91
|
-
*/
|
|
92
|
-
async function tryImport(paths, deps) {
|
|
93
|
-
const errors = [];
|
|
94
|
-
for (const path of paths) {
|
|
95
|
-
try {
|
|
96
|
-
return await deps.importModule(path);
|
|
97
|
-
}
|
|
98
|
-
catch (error) {
|
|
99
|
-
errors.push(error);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const lastError = errors[errors.length - 1];
|
|
103
|
-
throw new Error(`Failed to import module from any path. Tried: ${paths.join(', ')}\nLast error: ${lastError?.message}`, { cause: lastError });
|
|
104
|
-
}
|
|
105
62
|
/**
|
|
106
63
|
* Load a driver package dynamically
|
|
107
64
|
*
|
|
108
65
|
* Supports two modes:
|
|
109
|
-
* 1. Explicit package: `loadDriver({ driverPackage: 'ya-modbus
|
|
66
|
+
* 1. Explicit package: `loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })`
|
|
110
67
|
* 2. Auto-detect from cwd: `loadDriver({})` - reads package.json from current directory
|
|
111
68
|
*
|
|
112
|
-
* Auto-detection checks for `ya-modbus-driver` keyword in package.json keywords
|
|
69
|
+
* Auto-detection checks for `ya-modbus-driver` keyword in package.json keywords and loads
|
|
70
|
+
* the entry file that `import` of the package resolves to (`exports["."]`, else `main`,
|
|
71
|
+
* else `index.js`). The driver must be built: the entry file is JavaScript, or TypeScript
|
|
72
|
+
* that Node can run itself.
|
|
113
73
|
*
|
|
114
74
|
* @example
|
|
115
75
|
* // Auto-detect from current directory
|
|
116
76
|
* const driver = await loadDriver({})
|
|
117
77
|
*
|
|
118
78
|
* // Load explicit package
|
|
119
|
-
* const driver = await loadDriver({ driverPackage: 'ya-modbus
|
|
79
|
+
* const driver = await loadDriver({ driverPackage: '@ya-modbus/driver-xymd1' })
|
|
120
80
|
*
|
|
121
81
|
* @param options - Loading options (defaults to local package detection)
|
|
122
82
|
* @param deps - System dependencies (for testing, uses Node.js built-ins by default)
|
|
@@ -143,21 +103,15 @@ export async function loadDriver(options = {}, deps = defaultDeps) {
|
|
|
143
103
|
driverModule = imported.driverModule;
|
|
144
104
|
}
|
|
145
105
|
else {
|
|
146
|
-
|
|
106
|
+
const localPackage = await detectLocalPackage(deps);
|
|
107
|
+
packageName = localPackage.name;
|
|
147
108
|
const cached = driverCache.get(packageName);
|
|
148
109
|
if (cached) {
|
|
149
110
|
cacheStats.hits++;
|
|
150
111
|
return cached;
|
|
151
112
|
}
|
|
152
113
|
cacheStats.misses++;
|
|
153
|
-
|
|
154
|
-
const importPaths = [
|
|
155
|
-
join(cwd, 'src', 'index.js'),
|
|
156
|
-
join(cwd, 'src', 'index.ts'),
|
|
157
|
-
join(cwd, 'dist', 'index.js'),
|
|
158
|
-
packageName,
|
|
159
|
-
];
|
|
160
|
-
driverModule = await tryImport(importPaths, deps);
|
|
114
|
+
driverModule = await importLocalDriver(packageName, localPackage.entryPoint, deps);
|
|
161
115
|
}
|
|
162
116
|
if (!driverModule || typeof driverModule !== 'object') {
|
|
163
117
|
throw new ValidationError('Driver package must export a createDriver function', 'createDriver');
|
|
@@ -193,6 +147,7 @@ export async function loadDriver(options = {}, deps = defaultDeps) {
|
|
|
193
147
|
catch (error) {
|
|
194
148
|
if (error instanceof ValidationError ||
|
|
195
149
|
error instanceof DriverNotFoundError ||
|
|
150
|
+
error instanceof DriverNotBuiltError ||
|
|
196
151
|
error instanceof PackageJsonError) {
|
|
197
152
|
throw error;
|
|
198
153
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAStC,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,qBAAqB,EACrB,eAAe,EACf,uBAAuB,GACxB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAC3E,OAAO,EAAE,0BAA0B,EAAE,MAAM,kBAAkB,CAAA;AAC7D,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,GAChB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;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,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACvE,0BAA0B;IAC1B,YAAY,EAAE,CAAC,SAAiB,EAAE,EAAE,CAClC,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;IAC7E,0BAA0B;IAC1B,MAAM,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
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,47 @@
|
|
|
1
|
+
import { extname, join } from 'node:path';
|
|
2
|
+
import { resolvePackageEntry } from './entry-point.js';
|
|
3
|
+
import { PackageJsonError } from './errors.js';
|
|
4
|
+
/**
|
|
5
|
+
* Auto-detect local driver package from package.json
|
|
6
|
+
*
|
|
7
|
+
* @param deps - System dependencies
|
|
8
|
+
* @returns Package name and entry file if valid driver package
|
|
9
|
+
* @throws Error if not a valid driver package
|
|
10
|
+
*/
|
|
11
|
+
export async function detectLocalPackage(deps) {
|
|
12
|
+
try {
|
|
13
|
+
const cwd = deps.getCwd();
|
|
14
|
+
const packageJsonContent = await deps.readFile(join(cwd, 'package.json'), 'utf-8');
|
|
15
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
16
|
+
const keywords = packageJson.keywords ?? [];
|
|
17
|
+
if (!keywords.includes('ya-modbus-driver')) {
|
|
18
|
+
throw new PackageJsonError('Current package is not a ya-modbus driver. ' +
|
|
19
|
+
'Add "ya-modbus-driver" to keywords in package.json');
|
|
20
|
+
}
|
|
21
|
+
const name = packageJson.name;
|
|
22
|
+
if (!name) {
|
|
23
|
+
throw new PackageJsonError('package.json must have a "name" field');
|
|
24
|
+
}
|
|
25
|
+
const entry = resolvePackageEntry(packageJson);
|
|
26
|
+
if (entry === undefined) {
|
|
27
|
+
throw new PackageJsonError('package.json "exports" has no entry for "." that can be imported. ' +
|
|
28
|
+
'Add an "import" or "default" condition to exports["."]');
|
|
29
|
+
}
|
|
30
|
+
if (extname(entry) === '') {
|
|
31
|
+
throw new PackageJsonError(`The entry point "${entry}" in package.json has no file extension. ` +
|
|
32
|
+
'Name the built file, for example "./dist/index.js"');
|
|
33
|
+
}
|
|
34
|
+
return { name, entryPoint: join(cwd, entry) };
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error.code === 'ENOENT') {
|
|
38
|
+
throw new PackageJsonError('package.json not found in current directory. ' +
|
|
39
|
+
'Run this command from a driver package directory or specify --driver');
|
|
40
|
+
}
|
|
41
|
+
if (error instanceof SyntaxError) {
|
|
42
|
+
throw new PackageJsonError('Failed to parse package.json: invalid JSON');
|
|
43
|
+
}
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=local-package.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-package.js","sourceRoot":"","sources":["../../../src/local-package.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEzC,OAAO,EAAE,m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EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACvD,MAAM,IAAI,gBAAgB,CACxB,+CAA+C;gBAC7C,sEAAsE,CACzE,CAAA;QACH,CAAC;QACD,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,IAAI,gBAAgB,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"}
|