counterfact 0.37.0 → 0.37.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/bin/counterfact.js +10 -0
- package/dist/server/module-loader.js +17 -11
- package/dist/server/uncached-import.js +6 -0
- package/dist/server/uncached-require.cjs +10 -0
- package/package.json +14 -14
package/bin/counterfact.js
CHANGED
|
@@ -10,6 +10,16 @@ import open from "open";
|
|
|
10
10
|
|
|
11
11
|
import { counterfact } from "../dist/server/app.js";
|
|
12
12
|
|
|
13
|
+
const MIN_NODE_VERSION = 17;
|
|
14
|
+
|
|
15
|
+
if (Number.parseInt(process.versions.node.split("."), 10) < MIN_NODE_VERSION) {
|
|
16
|
+
process.stdout.write(
|
|
17
|
+
`Counterfact works with Node version ${MIN_NODE_VERSION}+. You are running version ${process.version}`,
|
|
18
|
+
);
|
|
19
|
+
// eslint-disable-next-line n/no-process-exit
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
const taglinesFile = await readFile(
|
|
14
24
|
nodePath.join(
|
|
15
25
|
nodePath.dirname(fileURLToPath(import.meta.url)),
|
|
@@ -2,10 +2,12 @@ import { once } from "node:events";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import fs from "node:fs/promises";
|
|
4
4
|
import nodePath from "node:path";
|
|
5
|
-
import { pathToFileURL } from "node:url";
|
|
6
5
|
import { watch } from "chokidar";
|
|
7
6
|
import createDebug from "debug";
|
|
8
7
|
import { ContextRegistry } from "./context-registry.js";
|
|
8
|
+
import { determineModuleKind } from "./determine-module-kind.js";
|
|
9
|
+
import { uncachedImport } from "./uncached-import.js";
|
|
10
|
+
const { uncachedRequire } = await import("./uncached-require.cjs");
|
|
9
11
|
const debug = createDebug("counterfact:typescript-generator:module-loader");
|
|
10
12
|
function isContextModule(module) {
|
|
11
13
|
return "Context" in module && typeof module.Context === "function";
|
|
@@ -23,6 +25,11 @@ export class ModuleLoader extends EventTarget {
|
|
|
23
25
|
registry;
|
|
24
26
|
watcher;
|
|
25
27
|
contextRegistry;
|
|
28
|
+
uncachedImport =
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
30
|
+
async function (moduleName) {
|
|
31
|
+
throw new Error(`uncachedImport not set up; importing ${moduleName}`);
|
|
32
|
+
};
|
|
26
33
|
constructor(basePath, registry, contextRegistry = new ContextRegistry()) {
|
|
27
34
|
super();
|
|
28
35
|
this.basePath = basePath.replaceAll("\\", "/");
|
|
@@ -49,10 +56,8 @@ export class ModuleLoader extends EventTarget {
|
|
|
49
56
|
this.registry.remove(url);
|
|
50
57
|
this.dispatchEvent(new Event("remove"));
|
|
51
58
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// eslint-disable-next-line import/no-dynamic-require, no-unsanitized/method
|
|
55
|
-
import(fileUrl)
|
|
59
|
+
debug("importing module: %s", pathName);
|
|
60
|
+
this.uncachedImport(pathName)
|
|
56
61
|
// eslint-disable-next-line promise/prefer-await-to-then
|
|
57
62
|
.then((endpoint) => {
|
|
58
63
|
this.dispatchEvent(new Event(eventName));
|
|
@@ -69,7 +74,7 @@ export class ModuleLoader extends EventTarget {
|
|
|
69
74
|
})
|
|
70
75
|
// eslint-disable-next-line promise/prefer-await-to-then
|
|
71
76
|
.catch((error) => {
|
|
72
|
-
reportLoadError(error,
|
|
77
|
+
reportLoadError(error, pathName);
|
|
73
78
|
});
|
|
74
79
|
});
|
|
75
80
|
await once(this.watcher, "ready");
|
|
@@ -78,6 +83,9 @@ export class ModuleLoader extends EventTarget {
|
|
|
78
83
|
await this.watcher?.close();
|
|
79
84
|
}
|
|
80
85
|
async load(directory = "") {
|
|
86
|
+
const moduleKind = await determineModuleKind(this.basePath);
|
|
87
|
+
this.uncachedImport =
|
|
88
|
+
moduleKind === "module" ? uncachedImport : uncachedRequire;
|
|
81
89
|
if (!existsSync(nodePath.join(this.basePath, directory).replaceAll("\\", "/"))) {
|
|
82
90
|
throw new Error(`Directory does not exist ${this.basePath}`);
|
|
83
91
|
}
|
|
@@ -100,12 +108,10 @@ export class ModuleLoader extends EventTarget {
|
|
|
100
108
|
});
|
|
101
109
|
await Promise.all(imports);
|
|
102
110
|
}
|
|
103
|
-
// eslint-disable-next-line max-statements
|
|
104
111
|
async loadEndpoint(fullPath, directory, file) {
|
|
105
|
-
const fileUrl = `${pathToFileURL(fullPath).toString()}?cacheBust=${Date.now()}`;
|
|
106
112
|
try {
|
|
107
|
-
// eslint-disable-next-line
|
|
108
|
-
const endpoint = (await
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
114
|
+
const endpoint = (await this.uncachedImport(fullPath));
|
|
109
115
|
if (file.name.includes("_.context")) {
|
|
110
116
|
if (isContextModule(endpoint)) {
|
|
111
117
|
this.contextRegistry.add(`/${directory.replaceAll("\\", "/")}`,
|
|
@@ -128,7 +134,7 @@ export class ModuleLoader extends EventTarget {
|
|
|
128
134
|
// Not sure why Node throws this error. It doesn't seem to matter.
|
|
129
135
|
return;
|
|
130
136
|
}
|
|
131
|
-
process.stdout.write(`\nError loading ${
|
|
137
|
+
process.stdout.write(`\nError loading ${fullPath}:\n${String(error)}\n`);
|
|
132
138
|
}
|
|
133
139
|
}
|
|
134
140
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
export async function uncachedImport(pathName) {
|
|
3
|
+
const fileUrl = `${pathToFileURL(pathName).toString()}?cacheBust=${Date.now()}`;
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, import/no-dynamic-require, no-unsanitized/method
|
|
5
|
+
return await import(fileUrl);
|
|
6
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
uncachedRequire: function uncachedRequire(moduleName) {
|
|
5
|
+
delete require.cache[require.resolve(moduleName)];
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line n/global-require, security/detect-non-literal-require, import/no-dynamic-require
|
|
8
|
+
return Promise.resolve(require(moduleName));
|
|
9
|
+
},
|
|
10
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "counterfact",
|
|
3
|
-
"version": "0.37.
|
|
3
|
+
"version": "0.37.1",
|
|
4
4
|
"description": "a library for building a fake REST API for testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/server/counterfact.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"test": "yarn node --experimental-vm-modules ./node_modules/jest-cli/bin/jest --testPathIgnorePatterns=black-box",
|
|
32
32
|
"test:black-box": "rimraf dist && rimraf out && yarn build && yarn node --experimental-vm-modules ./node_modules/jest-cli/bin/jest black-box --forceExit --coverage=false",
|
|
33
33
|
"test:mutants": "stryker run stryker.config.json",
|
|
34
|
-
"build": "tsc && copyfiles -f \"src/client/**\" dist/client && copyfiles -f \"src/server/*.d.ts\" dist/server",
|
|
34
|
+
"build": "tsc && copyfiles -f \"src/client/**\" dist/client && copyfiles -f \"src/server/*.d.ts\" dist/server && copyfiles -f \"src/server/*.cjs\" dist/server",
|
|
35
35
|
"prepack": "yarn build",
|
|
36
36
|
"release": "npx changeset publish",
|
|
37
37
|
"prepare": "husky install",
|
|
@@ -44,34 +44,34 @@
|
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@changesets/cli": "2.27.1",
|
|
47
|
-
"@stryker-mutator/core": "8.2.
|
|
48
|
-
"@stryker-mutator/jest-runner": "8.2.
|
|
49
|
-
"@stryker-mutator/typescript-checker": "8.2.
|
|
50
|
-
"@swc/core": "1.4.
|
|
47
|
+
"@stryker-mutator/core": "8.2.6",
|
|
48
|
+
"@stryker-mutator/jest-runner": "8.2.6",
|
|
49
|
+
"@stryker-mutator/typescript-checker": "8.2.6",
|
|
50
|
+
"@swc/core": "1.4.4",
|
|
51
51
|
"@swc/jest": "0.2.36",
|
|
52
52
|
"@testing-library/dom": "9.3.4",
|
|
53
53
|
"@types/jest": "29.5.12",
|
|
54
54
|
"@types/js-yaml": "4.0.9",
|
|
55
|
-
"@types/koa": "2.
|
|
55
|
+
"@types/koa": "2.15.0",
|
|
56
56
|
"@types/koa-bodyparser": "4.3.12",
|
|
57
57
|
"@types/koa-proxy": "1.0.7",
|
|
58
58
|
"@types/koa-static": "4.0.4",
|
|
59
59
|
"copyfiles": "2.4.1",
|
|
60
|
-
"eslint": "8.
|
|
60
|
+
"eslint": "8.57.0",
|
|
61
61
|
"eslint-config-hardcore": "41.3.0",
|
|
62
62
|
"eslint-formatter-github-annotations": "0.1.0",
|
|
63
63
|
"eslint-import-resolver-typescript": "3.6.1",
|
|
64
64
|
"eslint-plugin-etc": "2.0.3",
|
|
65
65
|
"eslint-plugin-file-progress": "1.3.0",
|
|
66
66
|
"eslint-plugin-import": "2.29.1",
|
|
67
|
-
"eslint-plugin-jest": "27.
|
|
67
|
+
"eslint-plugin-jest": "27.9.0",
|
|
68
68
|
"eslint-plugin-jest-dom": "5.1.0",
|
|
69
69
|
"eslint-plugin-no-explicit-type-exports": "0.12.1",
|
|
70
|
-
"eslint-plugin-unused-imports": "3.
|
|
71
|
-
"husky": "9.0.
|
|
70
|
+
"eslint-plugin-unused-imports": "3.1.0",
|
|
71
|
+
"husky": "9.0.11",
|
|
72
72
|
"jest": "29.7.0",
|
|
73
73
|
"node-mocks-http": "1.14.1",
|
|
74
|
-
"nodemon": "3.0
|
|
74
|
+
"nodemon": "3.1.0",
|
|
75
75
|
"patch-package": "8.0.0",
|
|
76
76
|
"rimraf": "5.0.5",
|
|
77
77
|
"stryker-cli": "1.0.2",
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
"handlebars": "4.7.8",
|
|
90
90
|
"http-terminator": "3.2.0",
|
|
91
91
|
"js-yaml": "4.1.0",
|
|
92
|
-
"json-schema-faker": "0.5.
|
|
92
|
+
"json-schema-faker": "0.5.6",
|
|
93
93
|
"json-schema-ref-parser": "9.0.9",
|
|
94
94
|
"jsonwebtoken": "9.0.2",
|
|
95
95
|
"koa": "2.15.0",
|
|
@@ -97,7 +97,7 @@
|
|
|
97
97
|
"koa-proxy": "1.0.0-alpha.3",
|
|
98
98
|
"koa2-swagger-ui": "5.10.0",
|
|
99
99
|
"node-fetch": "3.3.2",
|
|
100
|
-
"open": "10.0.
|
|
100
|
+
"open": "10.0.4",
|
|
101
101
|
"prettier": "3.2.5",
|
|
102
102
|
"typescript": "5.3.3"
|
|
103
103
|
}
|