xmcp 0.6.13 → 0.7.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/dist/cli.js +8 -8
- package/dist/index.js +2 -2
- package/dist/runtime/adapter-express.js +2 -2
- package/dist/runtime/adapter-fastify.js +2 -2
- package/dist/runtime/adapter-nestjs.js +7 -7
- package/dist/runtime/adapter-nextjs.js +11 -11
- package/dist/runtime/http.js +3 -3
- package/dist/runtime/stdio.js +8 -8
- package/package.json +10 -11
- package/src/auth/api-key.ts +0 -120
- package/src/auth/jwt.ts +0 -52
- package/src/cli/commands/create/__tests__/create.test.ts +0 -99
- package/src/cli/commands/create/index.ts +0 -74
- package/src/cli/commands/create/templates/index.ts +0 -4
- package/src/cli/commands/create/templates/prompt.ts +0 -30
- package/src/cli/commands/create/templates/resource.ts +0 -20
- package/src/cli/commands/create/templates/tool.ts +0 -34
- package/src/cli/commands/create/templates/widget.ts +0 -33
- package/src/cli/utils/naming.ts +0 -39
- package/src/cli.ts +0 -125
- package/src/client/headers.ts +0 -61
- package/src/client/index.ts +0 -180
- package/src/client/types.ts +0 -122
- package/src/client/utils.ts +0 -133
- package/src/cloudflare.ts +0 -6
- package/src/compiler/build-telemetry.ts +0 -88
- package/src/compiler/client/component-compiler.ts +0 -250
- package/src/compiler/client/transpile.ts +0 -21
- package/src/compiler/compiler-context.ts +0 -61
- package/src/compiler/config/__tests__/config.test.ts +0 -459
- package/src/compiler/config/index.ts +0 -62
- package/src/compiler/config/injection.ts +0 -210
- package/src/compiler/config/schemas/bundler.ts +0 -12
- package/src/compiler/config/schemas/experimental/index.ts +0 -22
- package/src/compiler/config/schemas/index.ts +0 -20
- package/src/compiler/config/schemas/paths.ts +0 -35
- package/src/compiler/config/schemas/template.ts +0 -80
- package/src/compiler/config/schemas/transport/http.ts +0 -116
- package/src/compiler/config/schemas/transport/stdio.ts +0 -31
- package/src/compiler/config/schemas/typescript.ts +0 -26
- package/src/compiler/config/utils.ts +0 -168
- package/src/compiler/generate-env-code.ts +0 -5
- package/src/compiler/generate-import-code.ts +0 -187
- package/src/compiler/generate-tools-code.ts +0 -174
- package/src/compiler/get-bundler-config/get-entries.ts +0 -44
- package/src/compiler/get-bundler-config/get-externals.ts +0 -114
- package/src/compiler/get-bundler-config/get-injected-variables.ts +0 -46
- package/src/compiler/get-bundler-config/index.ts +0 -288
- package/src/compiler/get-bundler-config/plugins/index.ts +0 -140
- package/src/compiler/get-bundler-config/plugins/types/express.ts +0 -3
- package/src/compiler/get-bundler-config/plugins/types/fastify.ts +0 -4
- package/src/compiler/get-bundler-config/plugins/types/index.ts +0 -5
- package/src/compiler/get-bundler-config/plugins/types/nest.ts +0 -65
- package/src/compiler/get-bundler-config/plugins/types/next.ts +0 -52
- package/src/compiler/get-bundler-config/resolve-tsconfig-paths.ts +0 -51
- package/src/compiler/index.ts +0 -484
- package/src/compiler/on-first-build.ts +0 -54
- package/src/compiler/parse-xmcp-config.ts +0 -200
- package/src/compiler/start-http-server.ts +0 -42
- package/src/compiler/utils/__tests__/config-detection.test.ts +0 -307
- package/src/compiler/utils/__tests__/path-utils.test.ts +0 -137
- package/src/compiler/utils/config-detection.ts +0 -34
- package/src/compiler/utils/path-utils.ts +0 -22
- package/src/compiler/watcher-recovery.ts +0 -72
- package/src/host-bridge.ts +0 -36
- package/src/index.ts +0 -94
- package/src/platforms/build-cloudflare-output.ts +0 -116
- package/src/platforms/build-vercel-output.ts +0 -131
- package/src/plugins/index.ts +0 -1
- package/src/plugins/x402/index.ts +0 -24
- package/src/runtime/adapters/express/index.ts +0 -75
- package/src/runtime/adapters/fastify/__tests__/fastify.test.ts +0 -115
- package/src/runtime/adapters/fastify/index.ts +0 -78
- package/src/runtime/adapters/nestjs/index.ts +0 -16
- package/src/runtime/adapters/nestjs/xmcp.controller.ts +0 -34
- package/src/runtime/adapters/nestjs/xmcp.guard.ts +0 -101
- package/src/runtime/adapters/nestjs/xmcp.oauth.controller.ts +0 -36
- package/src/runtime/adapters/nestjs/xmcp.oauth.module.ts +0 -21
- package/src/runtime/adapters/nestjs/xmcp.oauth.service.ts +0 -36
- package/src/runtime/adapters/nestjs/xmcp.service.ts +0 -96
- package/src/runtime/adapters/nestjs/xmcp.utils.ts +0 -11
- package/src/runtime/adapters/nextjs/auth.ts +0 -265
- package/src/runtime/adapters/nextjs/handler/error-handler.ts +0 -67
- package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +0 -143
- package/src/runtime/adapters/nextjs/handler/request-converter.ts +0 -74
- package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +0 -69
- package/src/runtime/adapters/nextjs/index.ts +0 -82
- package/src/runtime/contexts/client-info-context.ts +0 -16
- package/src/runtime/contexts/http-request-context.ts +0 -21
- package/src/runtime/contexts/http-transport-context.ts +0 -18
- package/src/runtime/headers.ts +0 -42
- package/src/runtime/middlewares/utils.ts +0 -72
- package/src/runtime/platforms/cloudflare/cors.ts +0 -44
- package/src/runtime/platforms/cloudflare/middlewares/api-key.ts +0 -102
- package/src/runtime/platforms/cloudflare/middlewares/jwt.ts +0 -127
- package/src/runtime/platforms/cloudflare/types.ts +0 -23
- package/src/runtime/platforms/cloudflare/worker.ts +0 -270
- package/src/runtime/templates/home.ts +0 -746
- package/src/runtime/transports/http/base-streamable-http.ts +0 -46
- package/src/runtime/transports/http/cors/headers.ts +0 -58
- package/src/runtime/transports/http/cors/index.ts +0 -34
- package/src/runtime/transports/http/index.ts +0 -78
- package/src/runtime/transports/http/stateless-streamable-http.ts +0 -311
- package/src/runtime/transports/http/web-stateless-http.ts +0 -288
- package/src/runtime/transports/stdio/index.ts +0 -109
- package/src/runtime/utils/__tests__/client-info.test.ts +0 -146
- package/src/runtime/utils/__tests__/tool-extra.test.ts +0 -239
- package/src/runtime/utils/client-info.ts +0 -114
- package/src/runtime/utils/elicitation.ts +0 -396
- package/src/runtime/utils/ext-apps-registry.ts +0 -151
- package/src/runtime/utils/mcp-app-protocol.ts +0 -16
- package/src/runtime/utils/mcp-app-types.ts +0 -113
- package/src/runtime/utils/mcp-host-bridge.ts +0 -473
- package/src/runtime/utils/path-to-tool-name.ts +0 -69
- package/src/runtime/utils/prompt-loader.ts +0 -151
- package/src/runtime/utils/prompts.ts +0 -69
- package/src/runtime/utils/react/generate-html.ts +0 -69
- package/src/runtime/utils/react/index.ts +0 -6
- package/src/runtime/utils/request-tool-names.ts +0 -48
- package/src/runtime/utils/resource-loader.ts +0 -151
- package/src/runtime/utils/resources.ts +0 -331
- package/src/runtime/utils/server.ts +0 -107
- package/src/runtime/utils/tool-loader.ts +0 -149
- package/src/runtime/utils/tools.ts +0 -183
- package/src/runtime/utils/transformers/prompt.ts +0 -120
- package/src/runtime/utils/transformers/resource.ts +0 -196
- package/src/runtime/utils/transformers/tool.ts +0 -400
- package/src/runtime/utils/ui/__tests__/split-meta.test.ts +0 -803
- package/src/runtime/utils/ui/flatten-meta.ts +0 -44
- package/src/runtime/utils/ui/split-meta.ts +0 -78
- package/src/runtime/utils/utils/resource-uri-composer.ts +0 -204
- package/src/runtime/utils/validators/index.ts +0 -138
- package/src/telemetry/ci-info.ts +0 -6
- package/src/telemetry/debug.ts +0 -8
- package/src/telemetry/events/definitions.ts +0 -52
- package/src/telemetry/events/detached-flush.ts +0 -83
- package/src/telemetry/events/post-payload.ts +0 -141
- package/src/telemetry/events/tracker.ts +0 -281
- package/src/telemetry/index.ts +0 -7
- package/src/telemetry/metadata.ts +0 -49
- package/src/telemetry/project-id.ts +0 -35
- package/src/telemetry/storage.ts +0 -218
- package/src/types/adapter.d.ts +0 -5
- package/src/types/client-info.ts +0 -20
- package/src/types/declarations.ts +0 -37
- package/src/types/injected.d.ts +0 -62
- package/src/types/middleware.ts +0 -19
- package/src/types/prompt.ts +0 -6
- package/src/types/resource.ts +0 -15
- package/src/types/tool.ts +0 -192
- package/src/types/ui-meta.ts +0 -42
- package/src/utils/cli-icons.ts +0 -5
- package/src/utils/constants.ts +0 -31
- package/src/utils/context.ts +0 -105
- package/src/utils/detect-package-manager.ts +0 -46
- package/src/utils/file-watcher.ts +0 -66
- package/src/utils/fs-utils.ts +0 -16
- package/src/utils/path-validation.ts +0 -66
- package/src/utils/port-utils.ts +0 -43
- package/src/utils/spawn-process.ts +0 -49
- package/src/utils/subscribable.ts +0 -62
package/src/utils/context.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
-
|
|
3
|
-
type DefaultContext = Object;
|
|
4
|
-
|
|
5
|
-
type GetContext<T extends DefaultContext> = () => T;
|
|
6
|
-
|
|
7
|
-
type SetContext<T extends DefaultContext> = (data: Partial<T>) => void;
|
|
8
|
-
|
|
9
|
-
export interface Context<T extends DefaultContext> {
|
|
10
|
-
/** Run a callback with a specific context. */
|
|
11
|
-
provider: <R>(initialValue: T, callback: () => R) => R;
|
|
12
|
-
/** Get the context. */
|
|
13
|
-
getContext: GetContext<T>;
|
|
14
|
-
/** Partially update the context. */
|
|
15
|
-
setContext: SetContext<T>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface CreateContextOptions {
|
|
19
|
-
name: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const setGlobalContext = <T>(key: symbol, context: T) => {
|
|
23
|
-
(globalThis as any)[key] = context;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const getGlobalContext = <T>(key: symbol): T => {
|
|
27
|
-
return (globalThis as any)[key] as T;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Create context allows you to create scoped variables for functions.
|
|
32
|
-
* Similar to React's context API.
|
|
33
|
-
* Usage:
|
|
34
|
-
* ```ts
|
|
35
|
-
* interface MyContext {
|
|
36
|
-
* value: string;
|
|
37
|
-
* }
|
|
38
|
-
*
|
|
39
|
-
* const context = createContext({ name: "my-context" });
|
|
40
|
-
*
|
|
41
|
-
* context.provider({
|
|
42
|
-
* value: "hello",
|
|
43
|
-
* }, () => {
|
|
44
|
-
* // Do something with the context
|
|
45
|
-
* })```
|
|
46
|
-
*/
|
|
47
|
-
export function createContext<T extends Object>({
|
|
48
|
-
name,
|
|
49
|
-
}: CreateContextOptions): Context<T> {
|
|
50
|
-
const storageKey = Symbol.for(`xmcp-context-${name}`);
|
|
51
|
-
const fallbackKey = Symbol.for(`xmcp-context-${name}-fallback-store`);
|
|
52
|
-
|
|
53
|
-
if (getGlobalContext(storageKey)) {
|
|
54
|
-
return getGlobalContext<Context<T>>(storageKey);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const context = new AsyncLocalStorage<T>();
|
|
58
|
-
const fallbackStoreWrapper = (globalThis as any)[fallbackKey] ?? {
|
|
59
|
-
current: null as T | null,
|
|
60
|
-
};
|
|
61
|
-
setGlobalContext(fallbackKey, fallbackStoreWrapper);
|
|
62
|
-
|
|
63
|
-
const getContext: GetContext<T> = () => {
|
|
64
|
-
const store = context.getStore();
|
|
65
|
-
|
|
66
|
-
if (store) {
|
|
67
|
-
return store;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (fallbackStoreWrapper.current) {
|
|
71
|
-
return fallbackStoreWrapper.current;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
throw new Error(
|
|
75
|
-
`getContext() can only be used within the ${name} context.`
|
|
76
|
-
);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const setContext: SetContext<T> = (data) => {
|
|
80
|
-
const store = context.getStore() ?? fallbackStoreWrapper.current;
|
|
81
|
-
|
|
82
|
-
if (!store) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
`setContext() can only be used within the ${name} context.`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
Object.assign(store, data);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const provider = <R>(initialValue: T, callback: () => R): R => {
|
|
92
|
-
fallbackStoreWrapper.current = initialValue;
|
|
93
|
-
return context.run(initialValue, callback);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const result: Context<T> = {
|
|
97
|
-
provider,
|
|
98
|
-
getContext,
|
|
99
|
-
setContext,
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
setGlobalContext(storageKey, result);
|
|
103
|
-
|
|
104
|
-
return result;
|
|
105
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { processFolder } from "./constants";
|
|
3
|
-
import fs from "fs-extra";
|
|
4
|
-
|
|
5
|
-
export function detectPackageManager(): {
|
|
6
|
-
manager: string;
|
|
7
|
-
lockFile: string;
|
|
8
|
-
installCmd: string;
|
|
9
|
-
} {
|
|
10
|
-
const pnpmLock = path.join(processFolder, "pnpm-lock.yaml");
|
|
11
|
-
const npmLock = path.join(processFolder, "package-lock.json");
|
|
12
|
-
const yarnLock = path.join(processFolder, "yarn.lock");
|
|
13
|
-
const bunLock = path.join(processFolder, "bun.lockb");
|
|
14
|
-
|
|
15
|
-
if (fs.existsSync(bunLock)) {
|
|
16
|
-
return {
|
|
17
|
-
manager: "bun",
|
|
18
|
-
lockFile: "bun.lockb",
|
|
19
|
-
installCmd: "bun install",
|
|
20
|
-
};
|
|
21
|
-
} else if (fs.existsSync(pnpmLock)) {
|
|
22
|
-
return {
|
|
23
|
-
manager: "pnpm",
|
|
24
|
-
lockFile: "pnpm-lock.yaml",
|
|
25
|
-
installCmd: "pnpm install",
|
|
26
|
-
};
|
|
27
|
-
} else if (fs.existsSync(npmLock)) {
|
|
28
|
-
return {
|
|
29
|
-
manager: "npm",
|
|
30
|
-
lockFile: "package-lock.json",
|
|
31
|
-
installCmd: "npm install",
|
|
32
|
-
};
|
|
33
|
-
} else if (fs.existsSync(yarnLock)) {
|
|
34
|
-
return {
|
|
35
|
-
manager: "yarn",
|
|
36
|
-
lockFile: "yarn.lock",
|
|
37
|
-
installCmd: "yarn install",
|
|
38
|
-
};
|
|
39
|
-
} else {
|
|
40
|
-
return {
|
|
41
|
-
manager: "npm",
|
|
42
|
-
lockFile: "",
|
|
43
|
-
installCmd: "npm install",
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import chokidar from "chokidar";
|
|
2
|
-
import { subscribable, Subscribable } from "./subscribable";
|
|
3
|
-
|
|
4
|
-
interface WatcherInstance {
|
|
5
|
-
isReady: boolean;
|
|
6
|
-
watcher: chokidar.FSWatcher;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
interface WatcherOptions {
|
|
10
|
-
onAdd?: (path: string) => void;
|
|
11
|
-
onUnlink?: (path: string) => void;
|
|
12
|
-
onChange?: (path: string) => void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class Watcher {
|
|
16
|
-
private watchers: WatcherInstance[] = [];
|
|
17
|
-
|
|
18
|
-
isReady: boolean = false;
|
|
19
|
-
|
|
20
|
-
globalOptions: chokidar.WatchOptions = {};
|
|
21
|
-
|
|
22
|
-
onReadySubscribable: Subscribable = subscribable();
|
|
23
|
-
|
|
24
|
-
constructor(options: chokidar.WatchOptions = {}) {
|
|
25
|
-
this.globalOptions = options;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
watch(
|
|
29
|
-
path: string,
|
|
30
|
-
options: WatcherOptions,
|
|
31
|
-
chokidarOptions: chokidar.WatchOptions = {}
|
|
32
|
-
) {
|
|
33
|
-
const instance = chokidar.watch(path, {
|
|
34
|
-
...this.globalOptions,
|
|
35
|
-
...chokidarOptions,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
options.onAdd && instance.on("add", options.onAdd);
|
|
39
|
-
options.onUnlink && instance.on("unlink", options.onUnlink);
|
|
40
|
-
options.onChange && instance.on("change", options.onChange);
|
|
41
|
-
|
|
42
|
-
const watcherInstance: WatcherInstance = {
|
|
43
|
-
isReady: false,
|
|
44
|
-
watcher: instance,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
this.watchers.push(watcherInstance);
|
|
48
|
-
|
|
49
|
-
instance.on("ready", () => {
|
|
50
|
-
watcherInstance.isReady = true;
|
|
51
|
-
this.checkReady();
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public onReady(callback: () => void) {
|
|
56
|
-
this.onReadySubscribable.addCallback(callback);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
private checkReady() {
|
|
60
|
-
if (this.watchers.every((watcher) => watcher.isReady)) {
|
|
61
|
-
this.onReadySubscribable.runCallbacks();
|
|
62
|
-
|
|
63
|
-
this.onReadySubscribable.clearCallbacks();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
package/src/utils/fs-utils.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import json5 from "json5";
|
|
3
|
-
|
|
4
|
-
export function createFolder(folderPath: string) {
|
|
5
|
-
if (!fs.existsSync(folderPath)) {
|
|
6
|
-
fs.mkdirSync(folderPath, { recursive: true });
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function parseJson<T = any>(json: string) {
|
|
11
|
-
try {
|
|
12
|
-
return [json5.parse(json) as T, null] as const;
|
|
13
|
-
} catch (e) {
|
|
14
|
-
return [null, e as Error] as const;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { rootFolder } from "@/utils/constants";
|
|
2
|
-
import chalk from "chalk";
|
|
3
|
-
import fs from "fs";
|
|
4
|
-
import path from "path";
|
|
5
|
-
|
|
6
|
-
// for further addition of resources, prompts, etc, we can add more path types
|
|
7
|
-
// used for the message error to the user
|
|
8
|
-
type PathType = "tools" | "prompts" | "resources";
|
|
9
|
-
|
|
10
|
-
export function isValidPath(
|
|
11
|
-
pathStr: string | boolean | null | undefined,
|
|
12
|
-
type: PathType
|
|
13
|
-
): string | undefined {
|
|
14
|
-
function isNonEmptyString(str: unknown): str is string {
|
|
15
|
-
return typeof str === "string" && str.length > 0;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function isGlob(str: string): boolean {
|
|
19
|
-
return /[*?\[\]{}]/.test(str);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function normalizePath(p: string): string {
|
|
23
|
-
// normalize to posix (forward slashes)
|
|
24
|
-
return p.split(/\\|\//).join("/");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function resolveAndCheckExists(p: string): string {
|
|
28
|
-
const normalized = normalizePath(p);
|
|
29
|
-
const absPath = path.resolve(rootFolder, normalized);
|
|
30
|
-
if (!fs.existsSync(absPath)) {
|
|
31
|
-
exitError(
|
|
32
|
-
`The path set in xmcp.config.ts for ${type} does not exist: ${absPath}.
|
|
33
|
-
If you're not using this folder, set the ${type} path to false`
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return normalized; // still return the normalized path cause we're formatting to glob later
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (pathStr === undefined || pathStr === null || pathStr === false) {
|
|
41
|
-
return undefined;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// reject empty string
|
|
45
|
-
if (pathStr === "") {
|
|
46
|
-
exitError(`${type} path cannot be an empty string`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (isNonEmptyString(pathStr)) {
|
|
50
|
-
if (isGlob(pathStr)) {
|
|
51
|
-
exitError(
|
|
52
|
-
`If you are using a glob pattern, please use a string for the ${type} path`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
return resolveAndCheckExists(pathStr);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
exitError(`${type} path must be a non-empty string`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function exitError(message: string) {
|
|
62
|
-
console.log("");
|
|
63
|
-
console.log(chalk.red.bold(message));
|
|
64
|
-
console.log("");
|
|
65
|
-
process.exit(1);
|
|
66
|
-
}
|
package/src/utils/port-utils.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import chalk from "chalk";
|
|
2
|
-
import net from "net";
|
|
3
|
-
import { yellowArrow } from "./cli-icons";
|
|
4
|
-
|
|
5
|
-
const checkPortAvailability = (port: number, host: string) => {
|
|
6
|
-
const server = net.createServer();
|
|
7
|
-
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
server.once("error", (err) => {
|
|
10
|
-
if ("code" in err && err.code === "EADDRINUSE") {
|
|
11
|
-
// port is currently in use
|
|
12
|
-
resolve(false);
|
|
13
|
-
} else {
|
|
14
|
-
reject(err);
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
server.once("listening", () => {
|
|
19
|
-
server.close();
|
|
20
|
-
resolve(true);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
server.listen(port, host);
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export async function findAvailablePort(
|
|
28
|
-
startPort: number = 3001,
|
|
29
|
-
host: string = "127.0.0.1"
|
|
30
|
-
): Promise<number> {
|
|
31
|
-
return new Promise((resolve) => {
|
|
32
|
-
checkPortAvailability(startPort, host).then((isAvailable) => {
|
|
33
|
-
if (isAvailable) {
|
|
34
|
-
resolve(startPort);
|
|
35
|
-
} else {
|
|
36
|
-
console.log(
|
|
37
|
-
`${yellowArrow} Port ${chalk.yellow(String(startPort))} is in use, trying ${chalk.yellow(String(startPort + 1))} instead.`
|
|
38
|
-
);
|
|
39
|
-
findAvailablePort(startPort + 1).then(resolve);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
// Based on https://www.npmjs.com/package/foreground-child
|
|
2
|
-
|
|
3
|
-
// this spawns a child process that listens for SIGHUP when the
|
|
4
|
-
// parent process exits, and after 200ms, sends a SIGKILL to the
|
|
5
|
-
// child, in case it did not terminate.
|
|
6
|
-
|
|
7
|
-
import { ChildProcess, spawn } from "child_process";
|
|
8
|
-
|
|
9
|
-
const watchdogCode = String.raw`
|
|
10
|
-
const pid = parseInt(process.argv[1], 10)
|
|
11
|
-
process.title = 'node (foreground-child watchdog pid=' + pid + ')'
|
|
12
|
-
if (!isNaN(pid)) {
|
|
13
|
-
let barked = false
|
|
14
|
-
// keepalive
|
|
15
|
-
const interval = setInterval(() => {}, 60000)
|
|
16
|
-
const bark = () => {
|
|
17
|
-
clearInterval(interval)
|
|
18
|
-
if (barked) return
|
|
19
|
-
barked = true
|
|
20
|
-
process.removeListener('SIGHUP', bark)
|
|
21
|
-
setTimeout(() => {
|
|
22
|
-
try {
|
|
23
|
-
process.kill(pid, 'SIGKILL')
|
|
24
|
-
setTimeout(() => process.exit(), 200)
|
|
25
|
-
} catch (_) {}
|
|
26
|
-
}, 500)
|
|
27
|
-
})
|
|
28
|
-
process.on('SIGHUP', bark)
|
|
29
|
-
}
|
|
30
|
-
`;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Pass in a ChildProcess, and this will spawn a watchdog process that
|
|
34
|
-
* will make sure it exits if the parent does, thus preventing any
|
|
35
|
-
* dangling detached zombie processes.
|
|
36
|
-
*
|
|
37
|
-
* If the child ends before the parent, then the watchdog will terminate.
|
|
38
|
-
*/
|
|
39
|
-
export const watchdog = (child: ChildProcess) => {
|
|
40
|
-
let dogExited = false;
|
|
41
|
-
const dog = spawn(process.execPath, ["-e", watchdogCode, String(child.pid)], {
|
|
42
|
-
stdio: "ignore",
|
|
43
|
-
});
|
|
44
|
-
dog.on("exit", () => (dogExited = true));
|
|
45
|
-
child.on("exit", () => {
|
|
46
|
-
if (!dogExited) dog.kill("SIGKILL");
|
|
47
|
-
});
|
|
48
|
-
return dog;
|
|
49
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
type AnyFunction = (...args: any[]) => any;
|
|
3
|
-
|
|
4
|
-
export interface Subscribable<T extends AnyFunction = AnyFunction> {
|
|
5
|
-
addCallback: (callback: T, id?: string) => string;
|
|
6
|
-
removeCallback: (id: string | T) => void;
|
|
7
|
-
getCallbacks: () => T[];
|
|
8
|
-
getCallback: (id: string) => T;
|
|
9
|
-
getCallbackIds: () => string[];
|
|
10
|
-
clearCallbacks: () => void;
|
|
11
|
-
runCallbacks: T;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const subscribable = <
|
|
15
|
-
T extends AnyFunction = AnyFunction,
|
|
16
|
-
>(): Subscribable<T> => {
|
|
17
|
-
const callbacks: Record<string, T> = {};
|
|
18
|
-
|
|
19
|
-
const addCallback = (callback: T, id: string): string => {
|
|
20
|
-
const _id = id || crypto.randomUUID();
|
|
21
|
-
callbacks[_id] = callback;
|
|
22
|
-
return _id;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const removeCallback = (id: string | AnyFunction): void => {
|
|
26
|
-
if (typeof id === "function") {
|
|
27
|
-
const key = Object.keys(callbacks).find((k) => callbacks[k] === id);
|
|
28
|
-
if (key) delete callbacks[key];
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
delete callbacks[id];
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const getCallbacks = (): T[] => Object.values(callbacks);
|
|
36
|
-
|
|
37
|
-
const getCallback = (id: string): T => callbacks[id];
|
|
38
|
-
|
|
39
|
-
const clearCallbacks = (): void => {
|
|
40
|
-
Object.keys(callbacks).forEach((id) => {
|
|
41
|
-
removeCallback(id);
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const runCallbacks = (...params: unknown[]): void => {
|
|
46
|
-
let response = undefined as any;
|
|
47
|
-
Object.values(callbacks).forEach((callback) => {
|
|
48
|
-
response = callback(...params);
|
|
49
|
-
});
|
|
50
|
-
return response;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
addCallback,
|
|
55
|
-
removeCallback,
|
|
56
|
-
getCallback,
|
|
57
|
-
getCallbacks,
|
|
58
|
-
getCallbackIds: () => Object.keys(callbacks),
|
|
59
|
-
clearCallbacks,
|
|
60
|
-
runCallbacks: runCallbacks as unknown as T,
|
|
61
|
-
} as Subscribable<T>;
|
|
62
|
-
};
|