@sshkeda/check 4.0.0 → 4.1.0
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/docs/how-to-grant-a-permission.md +1 -0
- package/package.json +1 -1
- package/src/internal/confine-node-console-to-its-console-export.ts +77 -0
- package/src/internal/define-the-lint-plugin-and-configs.ts +4 -2
- package/src/internal/report-code-grants-nothing-uses.ts +32 -4
- package/src/lib/tasks/shared/project-config-and-tool-errors.ts +9 -0
- package/src/lib/tasks/shared/query-the-typescript-project.ts +10 -15
- package/src/lib/tasks/shared/watch-the-typescript-api-process.ts +177 -0
- package/src/shared/bans-each-permission-lifts.ts +23 -1
- package/src/shared/record-and-grant-permissions-by-path.ts +14 -1
|
@@ -25,4 +25,5 @@ export default lintConfig({
|
|
|
25
25
|
- `trustsUnverifiedTypes`: Allows trusting a value's type without checking it, through `Schema.declare`.
|
|
26
26
|
- `handlesEveryFailure`: Allows catching every error, through `Effect.catch`, `Effect.exit`, a forked fiber or `Effect.runPromise`.
|
|
27
27
|
- `fallsBackToFixedValues`: Allows answering every failure, or a whole library error such as `PlatformError`, with a fixed value that never reads the failure.
|
|
28
|
+
- `replacesTheConsole`: Allows `Console` from `node:console`, and reading, calling and assigning these members of the global `console` by name: `log`, `warn`, `error`, `info`, `debug`, `dir`, `dirxml`, `table`, `trace`, `assert`, `clear`, `count`, `countReset`, `group`, `groupCollapsed`, `groupEnd`, `time`, `timeEnd` and `timeLog`.
|
|
28
29
|
- `isGeneratedByATool`: Check skips this path, so it can't have any other permission.
|
package/package.json
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Context, CreateRule, ESTree } from "@oxlint/plugins";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
equivalentModuleSpecifiers,
|
|
6
|
+
REPLACES_THE_CONSOLE,
|
|
7
|
+
} from "../shared/bans-each-permission-lifts.ts";
|
|
8
|
+
import { CONSOLE_REPLACED } from "../shared/record-and-grant-permissions-by-path.ts";
|
|
9
|
+
import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-rule-options.ts";
|
|
10
|
+
import {
|
|
11
|
+
importsOnlyTypes,
|
|
12
|
+
moduleExportName,
|
|
13
|
+
staticKeyName,
|
|
14
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
15
|
+
|
|
16
|
+
const NODE_CONSOLE = "node:console";
|
|
17
|
+
const CONSOLE_EXPORT = "Console";
|
|
18
|
+
const NODE_CONSOLE_SPECIFIERS: ReadonlySet<string> = new Set(
|
|
19
|
+
equivalentModuleSpecifiers(NODE_CONSOLE),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const NODE_CONSOLE_OPTIONS = Schema.Struct({
|
|
23
|
+
console: Schema.optionalKey(Schema.Literal(CONSOLE_REPLACED)),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const isTheConsoleExport = (specifier: ESTree.ImportDeclaration["specifiers"][number]) =>
|
|
27
|
+
specifier.type === "ImportSpecifier" && moduleExportName(specifier.imported) === CONSOLE_EXPORT;
|
|
28
|
+
|
|
29
|
+
export const noNodeConsole = {
|
|
30
|
+
meta: {
|
|
31
|
+
type: "problem",
|
|
32
|
+
messages: {
|
|
33
|
+
outsideTheGrant: `${CONSOLE_EXPORT} from {{module}} is used outside a file allowed ${REPLACES_THE_CONSOLE}`,
|
|
34
|
+
forbiddenForm: `{{module}} is imported in a way no file may. A file allowed ${REPLACES_THE_CONSOLE} may import only ${CONSOLE_EXPORT} from ${NODE_CONSOLE}`,
|
|
35
|
+
},
|
|
36
|
+
schema: ruleOptionsJsonSchema(NODE_CONSOLE_OPTIONS),
|
|
37
|
+
},
|
|
38
|
+
create(context: Pick<Context, "options" | "report" | "sourceCode">) {
|
|
39
|
+
const isGranted =
|
|
40
|
+
decodeRuleOptions(NODE_CONSOLE_OPTIONS, context.options)?.console === CONSOLE_REPLACED;
|
|
41
|
+
const report = (source: ESTree.Node, module: string, isTheConsoleOnly: boolean) => {
|
|
42
|
+
if (!isTheConsoleOnly) {
|
|
43
|
+
context.report({ node: source, messageId: "forbiddenForm", data: { module } });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!isGranted) {
|
|
47
|
+
context.report({ node: source, messageId: "outsideTheGrant", data: { module } });
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
ImportDeclaration(node: ESTree.ImportDeclaration) {
|
|
52
|
+
const isNodeConsoleWithValues =
|
|
53
|
+
NODE_CONSOLE_SPECIFIERS.has(node.source.value) &&
|
|
54
|
+
!importsOnlyTypes(node, context.sourceCode);
|
|
55
|
+
if (!isNodeConsoleWithValues) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const valueSpecifiers = node.specifiers.filter(
|
|
59
|
+
(specifier) => specifier.type !== "ImportSpecifier" || specifier.importKind !== "type",
|
|
60
|
+
);
|
|
61
|
+
report(
|
|
62
|
+
node.source,
|
|
63
|
+
node.source.value,
|
|
64
|
+
valueSpecifiers.length > 0 &&
|
|
65
|
+
valueSpecifiers.every((specifier) => isTheConsoleExport(specifier)),
|
|
66
|
+
);
|
|
67
|
+
},
|
|
68
|
+
ImportExpression(node: ESTree.ImportExpression) {
|
|
69
|
+
const module = staticKeyName(node.source);
|
|
70
|
+
const loadsNodeConsole = module !== undefined && NODE_CONSOLE_SPECIFIERS.has(module);
|
|
71
|
+
if (loadsNodeConsole) {
|
|
72
|
+
report(node.source, module, false);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
} satisfies CreateRule;
|
|
@@ -2,6 +2,7 @@ import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
|
2
2
|
import { Record } from "effect";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
CONSOLE,
|
|
5
6
|
everyBanOf,
|
|
6
7
|
IMPORT_META,
|
|
7
8
|
moduleSpecifiersOfGlobalObject,
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
permissionBanRuleId,
|
|
19
20
|
FAILURES_AS_RESULTS_RULE,
|
|
20
21
|
FIXED_FALLBACKS_RULE,
|
|
22
|
+
NODE_CONSOLE_RULE,
|
|
21
23
|
STDIO_SERVICE_RULE,
|
|
22
24
|
TEST_TIMEOUTS_RULE,
|
|
23
25
|
TYPE_PREDICATES_RULE,
|
|
@@ -60,6 +62,7 @@ import { noVagueFileNames } from "./ban-vague-file-names.ts";
|
|
|
60
62
|
import { permissionBanRule } from "./ban-what-a-file-is-not-granted.ts";
|
|
61
63
|
import { noWhile } from "./ban-while-do-while-and-for-loops.ts";
|
|
62
64
|
import { noSharedHolders } from "./ban-writing-captured-objects-inside-effect-callbacks.ts";
|
|
65
|
+
import { noNodeConsole } from "./confine-node-console-to-its-console-export.ts";
|
|
63
66
|
import {
|
|
64
67
|
noServiceDeclarations,
|
|
65
68
|
noUnusedServiceGrants,
|
|
@@ -105,8 +108,6 @@ export const RESTRICTED_IMPORT_PATHS = RESTRICTED_PROPERTIES.flatMap(({ object,
|
|
|
105
108
|
})),
|
|
106
109
|
);
|
|
107
110
|
|
|
108
|
-
const CONSOLE = "console";
|
|
109
|
-
|
|
110
111
|
const GLOBALS_WHOSE_MEMBERS_OTHER_RULES_BAN = ["Atomics", "Intl", "Number"] as const;
|
|
111
112
|
|
|
112
113
|
const GLOBALS_WHOSE_MEMBERS_PERMISSIONS_BAN = Object.values(PERMISSION_BANS)
|
|
@@ -147,6 +148,7 @@ const NAMED_RULES = {
|
|
|
147
148
|
"no-declared-values": noDeclaredValues,
|
|
148
149
|
[TYPE_PREDICATES_RULE]: noTypePredicates,
|
|
149
150
|
[STDIO_SERVICE_RULE]: noStdioService,
|
|
151
|
+
[NODE_CONSOLE_RULE]: noNodeConsole,
|
|
150
152
|
[FAILURES_AS_RESULTS_RULE]: noFailuresAsResults,
|
|
151
153
|
"no-overloads": noOverloads,
|
|
152
154
|
"no-re-exports": noReExports,
|
|
@@ -2,6 +2,7 @@ import type { Context, CreateRule, Visitor } from "@oxlint/plugins";
|
|
|
2
2
|
import { Schema } from "effect";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
CONSOLE,
|
|
5
6
|
HANDLES_EVERY_FAILURE,
|
|
6
7
|
PERMISSION_BANS,
|
|
7
8
|
RULES_EACH_PERMISSION_LIFTS,
|
|
@@ -14,6 +15,8 @@ import {
|
|
|
14
15
|
CODE_GRANTS,
|
|
15
16
|
FAILURES_AS_RESULTS_RULE,
|
|
16
17
|
INSIDE_THE_TEST_HOME,
|
|
18
|
+
NO_CONSOLE_RULE,
|
|
19
|
+
NODE_CONSOLE_RULE,
|
|
17
20
|
rulesACodeGrantChanges,
|
|
18
21
|
SCHEDULES_RULE,
|
|
19
22
|
SLEEP_RULE,
|
|
@@ -30,17 +33,41 @@ import { noClockReadsAndSleeps } from "./ban-reading-the-clock-and-sleeping-outs
|
|
|
30
33
|
import { noSchedules } from "./ban-timer-schedules-without-deadlines.ts";
|
|
31
34
|
import { noTypePredicates } from "./ban-type-predicates.ts";
|
|
32
35
|
import { mergedVisitors, permissionBanRule } from "./ban-what-a-file-is-not-granted.ts";
|
|
36
|
+
import { noNodeConsole } from "./confine-node-console-to-its-console-export.ts";
|
|
33
37
|
import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-rule-options.ts";
|
|
38
|
+
import { isGlobalReference, memberName } from "./resolve-what-an-expression-imports.ts";
|
|
34
39
|
|
|
35
|
-
const
|
|
40
|
+
const NO_CONSOLE_OPTIONS = Schema.Struct({
|
|
41
|
+
allow: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const consoleMembersNoConsoleReports = {
|
|
45
|
+
create(context: Pick<Context, "options" | "report" | "sourceCode">) {
|
|
46
|
+
const allowed = new Set(decodeRuleOptions(NO_CONSOLE_OPTIONS, context.options)?.allow);
|
|
47
|
+
return {
|
|
48
|
+
MemberExpression(node) {
|
|
49
|
+
const name = memberName(node);
|
|
50
|
+
const isReported =
|
|
51
|
+
isGlobalReference(node.object, context.sourceCode, CONSOLE) &&
|
|
52
|
+
(name === undefined || !allowed.has(name));
|
|
53
|
+
if (isReported) {
|
|
54
|
+
context.report({ node, message: name ?? CONSOLE });
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
} satisfies Visitor;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const RULES_CODE_GRANTS_CHANGE = new Map<
|
|
36
62
|
string,
|
|
37
63
|
{
|
|
38
64
|
readonly create: (context: Pick<Context, "options" | "report" | "sourceCode">) => Visitor;
|
|
39
65
|
}
|
|
40
|
-
>
|
|
41
|
-
Object.entries({
|
|
66
|
+
>([
|
|
67
|
+
...Object.entries({
|
|
42
68
|
[PERMISSION_BANS[HANDLES_EVERY_FAILURE].ruleName]: permissionBanRule(HANDLES_EVERY_FAILURE),
|
|
43
69
|
[PERMISSION_BANS[TRUSTS_UNVERIFIED_TYPES].ruleName]: permissionBanRule(TRUSTS_UNVERIFIED_TYPES),
|
|
70
|
+
[NODE_CONSOLE_RULE]: noNodeConsole,
|
|
44
71
|
[FAILURES_AS_RESULTS_RULE]: noFailuresAsResults,
|
|
45
72
|
[UNNAMED_FALLBACKS_RULE]: noUnnamedFallbacks,
|
|
46
73
|
[SLEEP_RULE]: effectOperationRule(SLEEP_RULE),
|
|
@@ -50,7 +77,8 @@ const RULES_CODE_GRANTS_CHANGE: ReadonlyMap<
|
|
|
50
77
|
[RULES_EACH_PERMISSION_LIFTS[SHORT_NAMES]]: noAbbreviations,
|
|
51
78
|
[TYPE_PREDICATES_RULE]: noTypePredicates,
|
|
52
79
|
}).map(([name, rule]) => [`sshkeda/${name}`, rule] as const),
|
|
53
|
-
|
|
80
|
+
[NO_CONSOLE_RULE, consoleMembersNoConsoleReports],
|
|
81
|
+
]);
|
|
54
82
|
|
|
55
83
|
const UNUSED_CODE_GRANT_OPTIONS = Schema.Struct({
|
|
56
84
|
permissions: Schema.optionalKey(Schema.Array(Schema.Literals(CODE_GRANTS))),
|
|
@@ -13,6 +13,15 @@ export class TypeScriptApiFailed extends Schema.TaggedError<TypeScriptApiFailed>
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export class TypeScriptApiProcessStopped extends Schema.TaggedError<TypeScriptApiProcessStopped>()(
|
|
17
|
+
"TypeScriptApiProcessStopped",
|
|
18
|
+
{ reason: Schema.String },
|
|
19
|
+
) {
|
|
20
|
+
public override get message() {
|
|
21
|
+
return this.reason;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
export class TypeScriptReturnedNothing extends Schema.TaggedError<TypeScriptReturnedNothing>()(
|
|
17
26
|
"TypeScriptReturnedNothing",
|
|
18
27
|
{ operation: Schema.String },
|
|
@@ -33,7 +33,7 @@ import {
|
|
|
33
33
|
isTypeLiteralNode,
|
|
34
34
|
isVariableDeclaration,
|
|
35
35
|
} from "typescript/unstable/ast/is";
|
|
36
|
-
import {
|
|
36
|
+
import { SymbolFlags } from "typescript/unstable/async";
|
|
37
37
|
import type { Checker, Program, Symbol as TypeScriptSymbol, Type } from "typescript/unstable/async";
|
|
38
38
|
|
|
39
39
|
import { isDeclarationFile, isScript, packagePathOf } from "./classify-files-by-role.ts";
|
|
@@ -48,11 +48,13 @@ import {
|
|
|
48
48
|
ProgramFilesOutsideProject,
|
|
49
49
|
ProjectReferencesUndecodable,
|
|
50
50
|
TypeScriptApiFailed,
|
|
51
|
+
TypeScriptApiProcessStopped,
|
|
51
52
|
TypeScriptConfigUnparsable,
|
|
52
53
|
TypeScriptProjectFileMissing,
|
|
53
54
|
TypeScriptReturnedNothing,
|
|
54
55
|
} from "./project-config-and-tool-errors.ts";
|
|
55
56
|
import { absolutePathOf } from "./resolve-absolute-paths.ts";
|
|
57
|
+
import { openedApi } from "./watch-the-typescript-api-process.ts";
|
|
56
58
|
|
|
57
59
|
export const TYPESCRIPT_PROJECT_FILE = "tsconfig.json";
|
|
58
60
|
|
|
@@ -91,10 +93,16 @@ const PROJECT_REFERENCES = Schema.Struct({
|
|
|
91
93
|
references: Schema.optionalKey(Schema.Array(Schema.Struct({ path: Schema.String }))),
|
|
92
94
|
});
|
|
93
95
|
|
|
96
|
+
const isProcessStopped = Schema.is(TypeScriptApiProcessStopped);
|
|
97
|
+
|
|
94
98
|
export const typeScriptRequest = <Value>(operation: string, request: () => Promise<Value>) =>
|
|
95
99
|
Effect.tryPromise({
|
|
96
100
|
try: request,
|
|
97
|
-
catch: (cause) =>
|
|
101
|
+
catch: (cause) =>
|
|
102
|
+
new TypeScriptApiFailed({
|
|
103
|
+
operation,
|
|
104
|
+
reason: isProcessStopped(cause) ? cause.reason : String(cause),
|
|
105
|
+
}),
|
|
98
106
|
});
|
|
99
107
|
|
|
100
108
|
export const typeMembers = (type: Type) =>
|
|
@@ -204,19 +212,6 @@ export const projectConfigFiles = (projectFile: string) =>
|
|
|
204
212
|
return configFiles;
|
|
205
213
|
});
|
|
206
214
|
|
|
207
|
-
const openedApi = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
208
|
-
Effect.acquireRelease(
|
|
209
|
-
Effect.sync(
|
|
210
|
-
() =>
|
|
211
|
-
new API(
|
|
212
|
-
sourceTextOverrides.size === 0
|
|
213
|
-
? {}
|
|
214
|
-
: { fs: { readFile: (fileName) => sourceTextOverrides.get(fileName) } },
|
|
215
|
-
),
|
|
216
|
-
),
|
|
217
|
-
(openApi) => Effect.promise(() => openApi.close()),
|
|
218
|
-
);
|
|
219
|
-
|
|
220
215
|
const parseCompilerOptionsAndInputs = (configFile: string) =>
|
|
221
216
|
Effect.gen(function* () {
|
|
222
217
|
const path = yield* Path.Path;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Context, Deferred, Effect, Option, Predicate, Schema } from "effect";
|
|
2
|
+
import type { Duration } from "effect";
|
|
3
|
+
import { API } from "typescript/unstable/async";
|
|
4
|
+
import type { InternalAPI } from "typescript/unstable/async";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
TypeScriptApiFailed,
|
|
8
|
+
TypeScriptApiProcessStopped,
|
|
9
|
+
} from "./project-config-and-tool-errors.ts";
|
|
10
|
+
|
|
11
|
+
const TYPESCRIPT_REQUEST_DEADLINE = "10 minutes";
|
|
12
|
+
|
|
13
|
+
const EXIT_DEADLINE = "10 seconds";
|
|
14
|
+
|
|
15
|
+
const STARTING = "start";
|
|
16
|
+
|
|
17
|
+
const CLIENT_CHANGED = "the TypeScript API client no longer exposes the process Check watches";
|
|
18
|
+
|
|
19
|
+
const CLOSED = "the TypeScript API was closed";
|
|
20
|
+
|
|
21
|
+
const OUTPUT_CLOSED = "the TypeScript API process closed its output";
|
|
22
|
+
|
|
23
|
+
const TERMINATING_SIGNAL = "SIGKILL";
|
|
24
|
+
|
|
25
|
+
const API_CLIENT_FIELD = "client";
|
|
26
|
+
|
|
27
|
+
const CLIENT_PROCESS_FIELD = "process";
|
|
28
|
+
|
|
29
|
+
export const TypeScriptApiLaunch = Context.Reference<{
|
|
30
|
+
readonly tsserverPath?: string;
|
|
31
|
+
readonly requestDeadline: `${number} ${Duration.Unit}`;
|
|
32
|
+
}>("@sshkeda/check/TypeScriptApiLaunch", {
|
|
33
|
+
defaultValue: () => ({ requestDeadline: TYPESCRIPT_REQUEST_DEADLINE }),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const CLIENT_MEMBERS = [
|
|
37
|
+
"connect",
|
|
38
|
+
"apiRequest",
|
|
39
|
+
"close",
|
|
40
|
+
] as const satisfies readonly (keyof ConstructorParameters<typeof InternalAPI>[0])[];
|
|
41
|
+
|
|
42
|
+
const TYPESCRIPT_API_CLIENT = Schema.declare(
|
|
43
|
+
(value): value is ConstructorParameters<typeof InternalAPI>[0] =>
|
|
44
|
+
CLIENT_MEMBERS.every((member) => Predicate.hasProperty(value, member)),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const PROCESS_MEMBERS = ["once", "on", "kill", "stdout"] as const;
|
|
48
|
+
|
|
49
|
+
const isWatchableProcess = Schema.is(
|
|
50
|
+
Schema.declare(
|
|
51
|
+
(
|
|
52
|
+
value,
|
|
53
|
+
): value is {
|
|
54
|
+
readonly stdout: { once(event: "close", listener: () => void): void } | null;
|
|
55
|
+
once(
|
|
56
|
+
event: "exit",
|
|
57
|
+
listener: (code: number | null, signal: NodeJS.Signals | null) => void,
|
|
58
|
+
): void;
|
|
59
|
+
on(event: "error", listener: (error: Error) => void): void;
|
|
60
|
+
kill(signal: NodeJS.Signals): boolean;
|
|
61
|
+
} => PROCESS_MEMBERS.every((member) => Predicate.hasProperty(value, member)),
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const stopWith = (
|
|
66
|
+
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
67
|
+
child: { kill(signal: NodeJS.Signals): boolean },
|
|
68
|
+
reason: string,
|
|
69
|
+
) => {
|
|
70
|
+
Deferred.doneUnsafe(stopped, Effect.fail(new TypeScriptApiProcessStopped({ reason })));
|
|
71
|
+
child.kill(TERMINATING_SIGNAL);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const exitReason = (code: number | null, signal: NodeJS.Signals | null, outputClosed: boolean) => {
|
|
75
|
+
if (code !== null) {
|
|
76
|
+
return `the TypeScript API process exited with code ${code}`;
|
|
77
|
+
}
|
|
78
|
+
return outputClosed ? OUTPUT_CLOSED : `the TypeScript API process exited on signal ${signal}`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const watchedProcess = (
|
|
82
|
+
client: ConstructorParameters<typeof InternalAPI>[0],
|
|
83
|
+
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
84
|
+
exited: Deferred.Deferred<void>,
|
|
85
|
+
) => {
|
|
86
|
+
const child: unknown = client[CLIENT_PROCESS_FIELD];
|
|
87
|
+
if (!isWatchableProcess(child)) {
|
|
88
|
+
return client.close().then(() => Option.none());
|
|
89
|
+
}
|
|
90
|
+
const output = { closed: false };
|
|
91
|
+
child.once("exit", (code, signal) => {
|
|
92
|
+
stopWith(stopped, child, exitReason(code, signal, output.closed));
|
|
93
|
+
Deferred.doneUnsafe(exited, Effect.void);
|
|
94
|
+
});
|
|
95
|
+
child.on("error", (error) => {
|
|
96
|
+
stopWith(stopped, child, `the TypeScript API process failed: ${error.message}`);
|
|
97
|
+
});
|
|
98
|
+
child.stdout?.once("close", () => {
|
|
99
|
+
output.closed = true;
|
|
100
|
+
child.kill(TERMINATING_SIGNAL);
|
|
101
|
+
});
|
|
102
|
+
return Promise.resolve(Option.some(child));
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const answered = <Answer>(
|
|
106
|
+
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
107
|
+
child: Parameters<typeof stopWith>[1],
|
|
108
|
+
requestDeadline: `${number} ${Duration.Unit}`,
|
|
109
|
+
request: () => Promise<Answer>,
|
|
110
|
+
) =>
|
|
111
|
+
Effect.gen(function* () {
|
|
112
|
+
if (yield* Deferred.isDone(stopped)) {
|
|
113
|
+
return yield* Deferred.await(stopped);
|
|
114
|
+
}
|
|
115
|
+
return yield* Effect.raceFirst(Effect.promise(request), Deferred.await(stopped)).pipe(
|
|
116
|
+
Effect.timeoutOrElse({
|
|
117
|
+
duration: requestDeadline,
|
|
118
|
+
orElse: () =>
|
|
119
|
+
Effect.sync(() => {
|
|
120
|
+
stopWith(
|
|
121
|
+
stopped,
|
|
122
|
+
child,
|
|
123
|
+
`the TypeScript API process did not answer within ${requestDeadline}`,
|
|
124
|
+
);
|
|
125
|
+
}).pipe(Effect.andThen(Deferred.await(stopped))),
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const startedSession = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
131
|
+
Effect.gen(function* () {
|
|
132
|
+
const { requestDeadline, ...spawnOptions } = yield* TypeScriptApiLaunch;
|
|
133
|
+
const api = new API(
|
|
134
|
+
sourceTextOverrides.size === 0
|
|
135
|
+
? spawnOptions
|
|
136
|
+
: { ...spawnOptions, fs: { readFile: (fileName) => sourceTextOverrides.get(fileName) } },
|
|
137
|
+
);
|
|
138
|
+
const client = yield* Schema.decodeUnknownEffect(TYPESCRIPT_API_CLIENT)(
|
|
139
|
+
api[API_CLIENT_FIELD],
|
|
140
|
+
).pipe(
|
|
141
|
+
Effect.mapError(
|
|
142
|
+
() => new TypeScriptApiFailed({ operation: STARTING, reason: CLIENT_CHANGED }),
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
const stopped = yield* Deferred.make<never, TypeScriptApiProcessStopped>();
|
|
146
|
+
const exited = yield* Deferred.make<void>();
|
|
147
|
+
const watched = yield* Effect.tryPromise({
|
|
148
|
+
try: () => client.connect().then(() => watchedProcess(client, stopped, exited)),
|
|
149
|
+
catch: (cause) => new TypeScriptApiFailed({ operation: STARTING, reason: String(cause) }),
|
|
150
|
+
});
|
|
151
|
+
if (Option.isNone(watched)) {
|
|
152
|
+
return yield* new TypeScriptApiFailed({ operation: STARTING, reason: CLIENT_CHANGED });
|
|
153
|
+
}
|
|
154
|
+
const child = watched.value;
|
|
155
|
+
const runPromise = Effect.runPromiseWith(yield* Effect.context());
|
|
156
|
+
const send = client.apiRequest.bind(client);
|
|
157
|
+
client.apiRequest = <Answer>(...request: Parameters<typeof send>) =>
|
|
158
|
+
runPromise(answered(stopped, child, requestDeadline, () => send<Answer>(...request)));
|
|
159
|
+
return { api, client, child, stopped, exited };
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const closedSession = ({
|
|
163
|
+
client,
|
|
164
|
+
child,
|
|
165
|
+
stopped,
|
|
166
|
+
exited,
|
|
167
|
+
}: Effect.Success<ReturnType<typeof startedSession>>) =>
|
|
168
|
+
Effect.gen(function* () {
|
|
169
|
+
stopWith(stopped, child, CLOSED);
|
|
170
|
+
yield* Effect.promise(() => client.close());
|
|
171
|
+
yield* Deferred.await(exited).pipe(Effect.timeoutOption(EXIT_DEADLINE));
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
export const openedApi = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
175
|
+
Effect.acquireRelease(startedSession(sourceTextOverrides), closedSession).pipe(
|
|
176
|
+
Effect.map(({ api }) => api),
|
|
177
|
+
);
|
|
@@ -109,6 +109,29 @@ export const CREATE_REQUIRE_VALUE_USAGE = "createRequire used other than by a ca
|
|
|
109
109
|
|
|
110
110
|
export const TRUSTS_UNVERIFIED_TYPES = "trustsUnverifiedTypes";
|
|
111
111
|
export const WRITES_TO_THE_TERMINAL = "writesToTheTerminal";
|
|
112
|
+
export const REPLACES_THE_CONSOLE = "replacesTheConsole";
|
|
113
|
+
export const CONSOLE = "console";
|
|
114
|
+
export const CONSOLE_METHODS = [
|
|
115
|
+
"log",
|
|
116
|
+
"warn",
|
|
117
|
+
"error",
|
|
118
|
+
"info",
|
|
119
|
+
"debug",
|
|
120
|
+
"dir",
|
|
121
|
+
"dirxml",
|
|
122
|
+
"table",
|
|
123
|
+
"trace",
|
|
124
|
+
"assert",
|
|
125
|
+
"clear",
|
|
126
|
+
"count",
|
|
127
|
+
"countReset",
|
|
128
|
+
"group",
|
|
129
|
+
"groupCollapsed",
|
|
130
|
+
"groupEnd",
|
|
131
|
+
"time",
|
|
132
|
+
"timeEnd",
|
|
133
|
+
"timeLog",
|
|
134
|
+
] as const;
|
|
112
135
|
export const READS_CONFIG = "readsConfig";
|
|
113
136
|
const LOCKS_FILES = "locksFiles";
|
|
114
137
|
export const HANDLES_EVERY_FAILURE = "handlesEveryFailure";
|
|
@@ -344,7 +367,6 @@ const GRANTABLE_BANS = {
|
|
|
344
367
|
"{{usage}} writes to the terminal in a way no file may; write through the Effect Stdio service in a file allowed writesToTheTerminal",
|
|
345
368
|
bans: [
|
|
346
369
|
effectMembersBan([["Console"]]),
|
|
347
|
-
moduleBan(["node:console"]),
|
|
348
370
|
globalMembersBan("process", ["stdout", "stderr"]),
|
|
349
371
|
globalMembersBan("Bun", ["stdout", "stderr"]),
|
|
350
372
|
],
|
|
@@ -3,10 +3,12 @@ import { Array as Arrays, Result, Schema, SchemaIssue } from "effect";
|
|
|
3
3
|
import {
|
|
4
4
|
BANS_NO_GRANT_LIFTS,
|
|
5
5
|
bansNoGrantLifts,
|
|
6
|
+
CONSOLE_METHODS,
|
|
6
7
|
GRANTABLE_BAN_NAMES,
|
|
7
8
|
HANDLES_EVERY_FAILURE,
|
|
8
9
|
PERMISSION_BANS,
|
|
9
10
|
READS_CONFIG,
|
|
11
|
+
REPLACES_THE_CONSOLE,
|
|
10
12
|
RULES_EACH_PERMISSION_LIFTS,
|
|
11
13
|
SHORT_NAMES,
|
|
12
14
|
TRUSTS_UNVERIFIED_TYPES,
|
|
@@ -29,6 +31,9 @@ export const UNNAMED_FALLBACKS_RULE = "no-unnamed-fallbacks";
|
|
|
29
31
|
export const FIXED_FALLBACKS = "fallsBackToFixedValues";
|
|
30
32
|
export const FIXED_FALLBACKS_RULE = "no-fixed-fallbacks";
|
|
31
33
|
export const FIXED_FALLBACKS_GRANTED = "granted";
|
|
34
|
+
export const NO_CONSOLE_RULE = "eslint/no-console";
|
|
35
|
+
export const NODE_CONSOLE_RULE = "no-node-console";
|
|
36
|
+
export const CONSOLE_REPLACED = "replaced";
|
|
32
37
|
const SCHEDULES = "pollsWithADeadline";
|
|
33
38
|
const TIMERS = "waitsOnTimers";
|
|
34
39
|
export const SCHEDULES_RULE = "no-schedules";
|
|
@@ -56,6 +61,7 @@ export const PERMISSION_NAMES = [
|
|
|
56
61
|
TRUSTS_UNVERIFIED_TYPES,
|
|
57
62
|
HANDLES_EVERY_FAILURE,
|
|
58
63
|
FIXED_FALLBACKS,
|
|
64
|
+
REPLACES_THE_CONSOLE,
|
|
59
65
|
GENERATED,
|
|
60
66
|
] as const;
|
|
61
67
|
|
|
@@ -87,7 +93,7 @@ const severityOnceGranted = (permission: keyof typeof PERMISSION_BANS) =>
|
|
|
87
93
|
|
|
88
94
|
const ruleSeverities = (
|
|
89
95
|
severities: Readonly<
|
|
90
|
-
Record<string, readonly ["error", Readonly<Record<string, string>>] | "off">
|
|
96
|
+
Record<string, readonly ["error", Readonly<Record<string, string[] | string>>] | "off">
|
|
91
97
|
>,
|
|
92
98
|
) => Object.entries(severities);
|
|
93
99
|
|
|
@@ -127,6 +133,12 @@ const rulesGrantedBy = (permission: (typeof PERMISSION_NAMES)[number]) => {
|
|
|
127
133
|
[`sshkeda/${FIXED_FALLBACKS_RULE}`]: ["error", { fixedFallbacks: FIXED_FALLBACKS_GRANTED }],
|
|
128
134
|
} as const);
|
|
129
135
|
}
|
|
136
|
+
if (permission === REPLACES_THE_CONSOLE) {
|
|
137
|
+
return ruleSeverities({
|
|
138
|
+
[`sshkeda/${NODE_CONSOLE_RULE}`]: ["error", { console: CONSOLE_REPLACED }],
|
|
139
|
+
[NO_CONSOLE_RULE]: ["error", { allow: [...CONSOLE_METHODS] }],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
130
142
|
if (permission === TRUSTS_UNVERIFIED_TYPES) {
|
|
131
143
|
return ruleSeverities({
|
|
132
144
|
[permissionBanRuleId(permission)]: "off",
|
|
@@ -152,6 +164,7 @@ export const CODE_GRANTS = [
|
|
|
152
164
|
SCHEDULES,
|
|
153
165
|
SHORT_NAMES,
|
|
154
166
|
TRUSTS_UNVERIFIED_TYPES,
|
|
167
|
+
REPLACES_THE_CONSOLE,
|
|
155
168
|
] as const;
|
|
156
169
|
|
|
157
170
|
const isCodeGrant = Schema.is(Schema.Literals(CODE_GRANTS));
|