tidewave 0.1.0 → 0.3.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/dist/core.d.ts ADDED
@@ -0,0 +1,77 @@
1
+ import ts from 'typescript';
2
+ export type Program = ts.Program;
3
+ export type TypeChecker = ts.TypeChecker;
4
+ export type SourceFile = ts.SourceFile;
5
+ export type Runtime = 'node' | 'bun' | 'deno';
6
+ export interface ModuleRequest {
7
+ readonly specifier: string;
8
+ readonly source: string;
9
+ readonly runtime?: Runtime;
10
+ }
11
+ export interface ResolvedModule {
12
+ readonly path: string;
13
+ readonly format: 'commonjs' | 'module' | 'typescript';
14
+ readonly content?: string;
15
+ }
16
+ export interface InternalResolvedModule {
17
+ readonly sourceFile: ts.SourceFile;
18
+ readonly program: ts.Program;
19
+ }
20
+ export interface ExtractionRequest {
21
+ readonly module: string;
22
+ readonly symbol: string;
23
+ readonly member?: string;
24
+ readonly isStatic?: boolean;
25
+ }
26
+ export interface SymbolInfo {
27
+ readonly name: string;
28
+ readonly kind: string;
29
+ readonly type: string;
30
+ readonly documentation?: string;
31
+ readonly signature?: string;
32
+ readonly location: string;
33
+ readonly jsDoc?: string;
34
+ }
35
+ export interface ExtractorOptions {
36
+ readonly prefix?: string;
37
+ }
38
+ export interface ResolveError {
39
+ readonly success: false;
40
+ readonly error: {
41
+ readonly code: 'MODULE_NOT_FOUND' | 'INVALID_SPECIFIER' | 'RESOLUTION_FAILED';
42
+ readonly message: string;
43
+ readonly details?: unknown;
44
+ };
45
+ }
46
+ export interface ExtractError {
47
+ readonly error: {
48
+ readonly code: 'SYMBOL_NOT_FOUND' | 'PARSE_ERROR' | 'INVALID_REQUEST' | 'MODULE_NOT_FOUND' | 'MEMBER_NOT_FOUND' | 'TYPE_ERROR';
49
+ readonly message: string;
50
+ readonly details?: unknown;
51
+ };
52
+ }
53
+ export interface EvaluationRequest {
54
+ code: string;
55
+ args: unknown[];
56
+ timeout: number;
57
+ }
58
+ export interface EvaluatedModuleResult {
59
+ success: boolean;
60
+ result: string | null;
61
+ stdout: string;
62
+ stderr: string;
63
+ }
64
+ export type ResolveResult = ResolvedModule | ResolveError;
65
+ export type ExtractResult = SymbolInfo | ExtractError;
66
+ export type InternalResolveResult = InternalResolvedModule | ResolveError;
67
+ export declare function isError(result: ResolveResult | ExtractResult): boolean;
68
+ export declare function isResolveError(result: ResolveResult | InternalResolveResult): result is ResolveError;
69
+ export declare function isExtractError(result: ExtractResult): result is ExtractError;
70
+ export declare function resolveError(specifier: ModuleRequest['specifier'], source: ModuleRequest['source']): ResolveError;
71
+ export declare function createExtractError(code: ExtractError['error']['code'], message: string, details?: unknown): ExtractError;
72
+ export interface TidewaveConfig {
73
+ port?: number;
74
+ host?: string;
75
+ allowRemoteAccess?: boolean;
76
+ allowedOrigins?: string[];
77
+ }
package/dist/core.js ADDED
@@ -0,0 +1,64 @@
1
+ import { createRequire } from "node:module";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
29
+
30
+ // src/core.ts
31
+ function isError(result) {
32
+ return result != null && "success" in result && result.success === false;
33
+ }
34
+ function isResolveError(result) {
35
+ return result != null && "success" in result && result.success === false;
36
+ }
37
+ function isExtractError(result) {
38
+ return result != null && "error" in result;
39
+ }
40
+ function resolveError(specifier, source) {
41
+ return {
42
+ success: false,
43
+ error: {
44
+ code: "MODULE_NOT_FOUND",
45
+ message: `Cannot resolve module '${specifier}' from '${source}'`
46
+ }
47
+ };
48
+ }
49
+ function createExtractError(code, message, details) {
50
+ return {
51
+ error: {
52
+ code,
53
+ message,
54
+ details
55
+ }
56
+ };
57
+ }
58
+ export {
59
+ resolveError,
60
+ isResolveError,
61
+ isExtractError,
62
+ isError,
63
+ createExtractError
64
+ };
@@ -0,0 +1,2 @@
1
+ import type { EvaluatedModuleResult, EvaluationRequest } from '../core';
2
+ export declare function executeIsolated(request: EvaluationRequest): Promise<EvaluatedModuleResult>;
@@ -0,0 +1,104 @@
1
+ import { createRequire } from "node:module";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
29
+
30
+ // src/evaluation/eval_worker.ts
31
+ process.on("message", async ({ code, args }) => {
32
+ if (!process.send) {
33
+ console.error("[Tidewave] Unable to establish communication channel with code-executor.");
34
+ process.exit(1);
35
+ }
36
+ try {
37
+ const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
38
+ const fn = new AsyncFunction(code);
39
+ const result = await fn(...args);
40
+ process.send({
41
+ type: "result",
42
+ success: true,
43
+ data: (result || null) && result
44
+ });
45
+ } catch (error) {
46
+ process.send({
47
+ type: "result",
48
+ success: false,
49
+ data: new String(error)
50
+ });
51
+ }
52
+ process.exit(0);
53
+ });
54
+
55
+ // src/evaluation/code_executor.ts
56
+ import { fork } from "child_process";
57
+ async function executeIsolated(request) {
58
+ return new Promise((resolve) => {
59
+ const workerPath = __require.resolve("./eval_worker");
60
+ const child = fork(workerPath, { silent: true });
61
+ const evaluation = {
62
+ success: false,
63
+ result: null,
64
+ stdout: "",
65
+ stderr: ""
66
+ };
67
+ child.stdout?.on("data", (data) => {
68
+ evaluation.stdout += data.toString();
69
+ });
70
+ child.stderr?.on("data", (data) => {
71
+ evaluation.stderr += data.toString();
72
+ });
73
+ child.on("message", (msg) => {
74
+ if (msg.type === "result") {
75
+ const { data, success } = msg;
76
+ evaluation.result = data;
77
+ evaluation.success = success;
78
+ }
79
+ });
80
+ child.on("exit", (code) => {
81
+ resolve({
82
+ success: evaluation.success && code === 0,
83
+ result: evaluation.result,
84
+ stdout: evaluation.stdout.trim(),
85
+ stderr: evaluation.stderr.trim()
86
+ });
87
+ });
88
+ const { timeout } = request;
89
+ const timeoutId = setTimeout(() => {
90
+ child.kill("SIGKILL");
91
+ resolve({
92
+ success: false,
93
+ result: `Evaluation timed out after ${timeout} milliseconds`,
94
+ stdout: evaluation.stdout,
95
+ stderr: evaluation.stderr
96
+ });
97
+ }, timeout);
98
+ child.on("exit", () => clearTimeout(timeoutId));
99
+ child.send(request);
100
+ });
101
+ }
102
+ export {
103
+ executeIsolated
104
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,53 @@
1
+ import { createRequire } from "node:module";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
29
+
30
+ // src/evaluation/eval_worker.ts
31
+ process.on("message", async ({ code, args }) => {
32
+ if (!process.send) {
33
+ console.error("[Tidewave] Unable to establish communication channel with code-executor.");
34
+ process.exit(1);
35
+ }
36
+ try {
37
+ const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
38
+ const fn = new AsyncFunction(code);
39
+ const result = await fn(...args);
40
+ process.send({
41
+ type: "result",
42
+ success: true,
43
+ data: (result || null) && result
44
+ });
45
+ } catch (error) {
46
+ process.send({
47
+ type: "result",
48
+ success: false,
49
+ data: new String(error)
50
+ });
51
+ }
52
+ process.exit(0);
53
+ });
@@ -0,0 +1,2 @@
1
+ import { type Request, type Response, type NextFn } from '..';
2
+ export declare function handleMcp(req: Request, res: Response, next: NextFn): Promise<void>;
@@ -0,0 +1,6 @@
1
+ import { type Request, type Response, type NextFn } from '../index';
2
+ export declare function handleShell(req: Request, res: Response, next: NextFn): Promise<void>;
3
+ export declare function getShellCommand(command: string): {
4
+ cmd: string;
5
+ args: string[];
6
+ };
@@ -0,0 +1,15 @@
1
+ import type { ServerResponse } from 'http';
2
+ import type { IncomingMessage, NextFunction, Server } from 'connect';
3
+ import type { TidewaveConfig } from '../core';
4
+ export interface Request extends IncomingMessage {
5
+ body?: Record<string, unknown>;
6
+ }
7
+ export type Response = ServerResponse<IncomingMessage>;
8
+ export type NextFn = NextFunction;
9
+ export declare const ENDPOINT: "/tidewave";
10
+ export type Handler = (req: Request, res: Response, next: NextFn) => Promise<void>;
11
+ export declare const HANDLERS: Record<string, Handler>;
12
+ export declare function configureServer(server?: Server, config?: TidewaveConfig): Server;
13
+ export declare function serve(server: Server, config?: TidewaveConfig): void;
14
+ export declare function checkSecurity(config: TidewaveConfig): (req: Request, res: Response, next: NextFn) => void;
15
+ export declare function methodNotAllowed(res: Response): void;