@deterministic-code/datasources 0.1.6 → 0.1.10
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/dist/DriverLoader.js +40 -9
- package/package.json +1 -1
package/dist/DriverLoader.js
CHANGED
|
@@ -1,16 +1,34 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { createRequire, registerHooks } from "node:module";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
6
|
import { ProviderNotInstalledError } from "./ProviderNotInstalledError.js";
|
|
7
7
|
const require = createRequire(import.meta.url);
|
|
8
|
-
const
|
|
8
|
+
const requirers = () => [
|
|
9
|
+
createRequire(join(process.cwd(), "package.json")),
|
|
10
|
+
...(process.argv[1] ? [createRequire(process.argv[1])] : []),
|
|
11
|
+
require,
|
|
12
|
+
];
|
|
13
|
+
const missingModule = (error) => error instanceof Error &&
|
|
14
|
+
"code" in error &&
|
|
15
|
+
(error.code === "MODULE_NOT_FOUND" ||
|
|
16
|
+
error.code === "ERR_MODULE_NOT_FOUND" ||
|
|
17
|
+
error.code === "ERR_PACKAGE_PATH_NOT_EXPORTED");
|
|
9
18
|
const virtual = new Map();
|
|
10
19
|
const virtualBySource = new Map();
|
|
11
20
|
let typeScriptHook = false;
|
|
12
21
|
let compiling = false;
|
|
22
|
+
let compilingRoot = "";
|
|
13
23
|
let nextVirtual = 0;
|
|
24
|
+
/* v8 ignore start */
|
|
25
|
+
const underRoot = (url) => {
|
|
26
|
+
if (!url.startsWith("file:"))
|
|
27
|
+
return false;
|
|
28
|
+
const path = fileURLToPath(url);
|
|
29
|
+
return path === compilingRoot || path.startsWith(`${compilingRoot}/`);
|
|
30
|
+
};
|
|
31
|
+
/* v8 ignore stop */
|
|
14
32
|
const installTypeScriptHook = () => {
|
|
15
33
|
if (typeScriptHook)
|
|
16
34
|
return;
|
|
@@ -21,7 +39,7 @@ const installTypeScriptHook = () => {
|
|
|
21
39
|
const parentURL = context.parentURL ? virtual.get(context.parentURL) ?? context.parentURL : undefined;
|
|
22
40
|
const resolved = nextResolve(specifier, parentURL ? { ...context, parentURL } : context);
|
|
23
41
|
/* v8 ignore next */
|
|
24
|
-
if (!compiling || !resolved.url.endsWith(".ts"))
|
|
42
|
+
if (!compiling || !resolved.url.endsWith(".ts") || !underRoot(resolved.url))
|
|
25
43
|
return resolved;
|
|
26
44
|
const existing = virtualBySource.get(resolved.url);
|
|
27
45
|
/* v8 ignore next */
|
|
@@ -33,7 +51,7 @@ const installTypeScriptHook = () => {
|
|
|
33
51
|
return { url, shortCircuit: true };
|
|
34
52
|
},
|
|
35
53
|
load: (url, context, nextLoad) => {
|
|
36
|
-
const original = virtual.get(url) ?? (compiling && url.endsWith(".ts") ? url : undefined);
|
|
54
|
+
const original = virtual.get(url) ?? (compiling && url.endsWith(".ts") && underRoot(url) ? url : undefined);
|
|
37
55
|
/* v8 ignore next */
|
|
38
56
|
if (!original)
|
|
39
57
|
return nextLoad(url, context);
|
|
@@ -58,14 +76,27 @@ const instantiate = (catalog) => {
|
|
|
58
76
|
}
|
|
59
77
|
finally {
|
|
60
78
|
compiling = false;
|
|
79
|
+
compilingRoot = "";
|
|
61
80
|
}
|
|
62
81
|
};
|
|
63
82
|
const instantiateLoaded = (catalog) => {
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
83
|
+
let last;
|
|
84
|
+
for (const req of requirers()) {
|
|
85
|
+
try {
|
|
86
|
+
compilingRoot = dirname(req.resolve(catalog.location));
|
|
87
|
+
const loaded = req(catalog.location);
|
|
88
|
+
const Type = loaded[catalog.module];
|
|
89
|
+
if (typeof Type !== "function")
|
|
90
|
+
throw new ProviderNotInstalledError(catalog.name);
|
|
91
|
+
return new Type();
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
last = error;
|
|
95
|
+
if (error instanceof ProviderNotInstalledError || !missingModule(error))
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
throw last instanceof ProviderNotInstalledError ? last : new ProviderNotInstalledError(catalog.name);
|
|
69
100
|
};
|
|
70
101
|
export class DriverLoader {
|
|
71
102
|
static load = (catalog) => {
|