Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/webui/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"scripts": {
"build": "tsc",
"build:watch": "npm run build -- -w",
"dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/server/src/fastify-v2/app.js",
"dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/server/src/app.js",
"dev": "npm run build && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:build:watch\" \"npm:dev:start\"",
"start": "npm run build && fastify start -l info dist/server/src/fastify-v2/app.js",
"start": "npm run build && fastify start -l info dist/server/src/app.js",
"standalone": "npm run build && node --env-file=.env dist/server/src/main.js",
"lint:check": "eslint . --max-warnings 0",
"lint:fix": "npm run lint:check -- --fix",
"test": "NODE_ENV=test tap --include='**/*test.ts'"
"test": "tap --include='**/*test.ts'"
},
"author": "YScope Inc. <[email protected]>",
"license": "Apache-2.0",
Expand Down
111 changes: 96 additions & 15 deletions components/webui/server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,106 @@
// Reference: https://github.com/fastify/demo/blob/main/src/app.ts

import path from "node:path";

import {fastifyAutoload} from "@fastify/autoload";
import {
FastifyInstance,
FastifyPluginAsync,
FastifyPluginOptions,
} from "fastify";
import {StatusCodes} from "http-status-codes";

import staticRoutes from "./routes/static.js";

const RATE_LIMIT_MAX_REQUESTS = 3;
const RATE_LIMIT_TIME_WINDOW_MS = 500;

/**
* Creates the Fastify app with the given options.
*
* TODO: Once old webui code is refactored to new modlular fastify style, this plugin should be
* removed.
* Registers all plugins and routes.
*
* @param fastify
* @return
* @param opts
*/
const FastifyV1App: FastifyPluginAsync = async (
fastify: FastifyInstance
) => {
// Register the routes
await fastify.register(staticRoutes);
};

export default FastifyV1App;
// eslint-disable-next-line max-lines-per-function
export default async function serviceApp (
fastify: FastifyInstance,
opts: FastifyPluginOptions
) {
// Option only serves testing purpose. It's used in testing to expose all decorators to the
// test app. Some decorators may not be exposed in production.
delete opts.skipOverride;

// Loads all external plugins. Registered first as application plugins might depend on them.
await fastify.register(fastifyAutoload, {
dir: path.join(import.meta.dirname, "plugins/external"),
options: {...opts},
});

// Loads all application plugins.
fastify.register(fastifyAutoload, {
dir: path.join(import.meta.dirname, "plugins/app"),
options: {...opts},
});

// Loads all routes.
fastify.register(fastifyAutoload, {
autoHooks: true,
cascadeHooks: true,
dir: path.join(import.meta.dirname, "routes"),
options: {...opts},
});

fastify.setErrorHandler((err, request, reply) => {
fastify.log.error(
{
err: err,
request: {
method: request.method,
url: request.url,
query: request.query,
params: request.params,
},
},
"Unhandled error occurred"
);

if ("undefined" !== typeof err.statusCode &&
Number(StatusCodes.INTERNAL_SERVER_ERROR) > err.statusCode
) {
reply.code(err.statusCode);

return err.message;
}

reply.internalServerError();

return {
message: "Internal Server Error",
};
});

// An attacker could search for valid URLs if 404 error handling is not rate limited.
fastify.setNotFoundHandler(
{
preHandler: fastify.rateLimit({
max: RATE_LIMIT_MAX_REQUESTS,
timeWindow: RATE_LIMIT_TIME_WINDOW_MS,
}),
},
(request, reply) => {
request.log.warn(
{
request: {
method: request.method,
url: request.url,
query: request.query,
params: request.params,
},
},
"Resource not found"
);

reply.notFound();

return {message: "Not Found"};
}
);
}
113 changes: 0 additions & 113 deletions components/webui/server/src/fastify-v2/app.ts

This file was deleted.

2 changes: 1 addition & 1 deletion components/webui/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import closeWithGrace from "close-with-grace";
import fastify from "fastify";
import fp from "fastify-plugin";

import serviceApp from "./fastify-v2/app.js";
import serviceApp from "./app.js";


const DEFAULT_FASTIFY_CLOSE_GRACE_DELAY = 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {FastifyInstance} from "fastify";
import fp from "fastify-plugin";
import {ResultSetHeader} from "mysql2";

import settings from "../../../../../settings.json" with {type: "json"};
import settings from "../../../../settings.json" with {type: "json"};
import {
QUERY_JOB_STATUS,
QUERY_JOB_STATUS_WAITING_STATES,
QUERY_JOB_TYPE,
QUERY_JOBS_TABLE_COLUMN_NAMES,
QueryJob,
} from "../../../../typings/query.js";
} from "../../../typings/query.js";
import {JOB_COMPLETION_STATUS_POLL_INTERVAL_MILLIS} from "./typings.js";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
import {getSignedUrl} from "@aws-sdk/s3-request-presigner";
import fp from "fastify-plugin";

import settings from "../../../../../settings.json" with {type: "json"};
import {Nullable} from "../../../../typings/common.js";
import settings from "../../../../settings.json" with {type: "json"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

JSON import assertion syntax may not compile under Node ≥ 20 / TS ≥ 5.4

The current syntax uses the Stage-3 with {type: "json"} clause. Node’s stable implementation – and TypeScript’s emit – expect assert { type: "json" }. Unless your build pipeline rewrites this, the file will fail to transpile / run.

-import settings from "../../../../settings.json" with {type: "json"};
+import settings from "../../../../settings.json" assert { type: "json" };

Please verify the chosen syntax against the toolchain versions in CI.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import settings from "../../../../settings.json" with {type: "json"};
-import settings from "../../../../settings.json" with {type: "json"};
+import settings from "../../../../settings.json" assert { type: "json" };
🤖 Prompt for AI Agents
In components/webui/server/src/plugins/app/S3Manager/index.ts at line 8, the
JSON import uses the syntax 'with {type: "json"}' which may not be compatible
with Node.js version 20 or TypeScript 5.4 and above. Replace this with the
stable syntax 'assert { type: "json" }' to ensure compatibility and successful
transpilation. Verify this change works with the CI toolchain versions.

import {Nullable} from "../../../typings/common.js";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Importing “.js” from TypeScript may generate incorrect dist paths

../../../typings/common.js assumes your emitted files keep the same relative depth and extension mapping. When tsc is configured with "module": "ESNext" and outDir, the compiled path often becomes ../../../typings/common.js in the output directory, but the source file is common.ts. If preserveExtensions is not enabled the import will be wrong at runtime.

Recommend importing the bare module and let TS resolve the .ts:

-import {Nullable} from "../../../typings/common.js";
+import {Nullable} from "../../../typings/common";

or add a paths alias in tsconfig.json to avoid brittle relative traversals.

🤖 Prompt for AI Agents
In components/webui/server/src/plugins/app/S3Manager/index.ts at line 9, the
import statement uses a .js extension which can cause incorrect paths in the
compiled output. To fix this, remove the .js extension from the import path so
TypeScript can resolve the .ts file correctly, or alternatively configure a path
alias in tsconfig.json to avoid fragile relative imports.

import {PRE_SIGNED_URL_EXPIRY_TIME_SECONDS} from "./typings.js";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
} from "fastify";
import fp from "fastify-plugin";

import settings from "../../../../settings.json" with {type: "json"};
import {Nullable} from "../../../typings/common.js";
import {QUERY_JOB_TYPE} from "../../../typings/query.js";
import settings from "../../../settings.json" with {type: "json"};
import {Nullable} from "../../typings/common.js";
import {QUERY_JOB_TYPE} from "../../typings/query.js";
import {
StreamFileMetadata,
StreamFilesCollection,
} from "../../../typings/stream-files.js";
} from "../../typings/stream-files.js";


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
Db,
} from "mongodb";

import {QueryId} from "../../../../../../../common/index.js";
import {QueryId} from "../../../../../../common/index.js";
import {
CLIENT_UPDATE_TIMEOUT_MILLIS,
MongoCustomSocket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
Response,
ServerToClientEvents,
SocketData,
} from "../../../../../../../common/index.js";
} from "../../../../../../common/index.js";
import MongoWatcherCollection from "./MongoWatcherCollection.js";
import {
ConnectionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
InterServerEvents,
ServerToClientEvents,
SocketData,
} from "../../../../../../../common/index.js";
} from "../../../../../../common/index.js";


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastifyMongoDb from "@fastify/mongodb";

import settings from "../../../../settings.json" with {type: "json"};
import settings from "../../../settings.json" with {type: "json"};


export const autoConfig = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "@fastify/mysql";
import {FastifyInstance} from "fastify";

import settings from "../../../../settings.json" with {type: "json"};
import settings from "../../../settings.json" with {type: "json"};


// The typing of `@fastify/mysql` needs to be manually specified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {StatusCodes} from "http-status-codes";
import {
SEARCH_SIGNAL,
type SearchResultsMetadataDocument,
} from "../../../../../../common/index.js";
import settings from "../../../../../settings.json" with {type: "json"};
import {QUERY_JOB_TYPE} from "../../../../typings/query.js";
} from "../../../../../common/index.js";
import settings from "../../../../settings.json" with {type: "json"};
import {ErrorSchema} from "../../../schemas/error.js";
import {
QueryJobCreationSchema,
QueryJobSchema,
} from "../../../schemas/search.js";
import {QUERY_JOB_TYPE} from "../../../typings/query.js";
import {SEARCH_MAX_NUM_RESULTS} from "./typings.js";
import {
createMongoIndexes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import {
SEARCH_SIGNAL,
type SearchResultsMetadataDocument,
} from "../../../../../../common/index.js";
} from "../../../../../common/index.js";


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Db} from "mongodb";

import {SEARCH_SIGNAL} from "../../../../../../common/index.js";
import {SEARCH_SIGNAL} from "../../../../../common/index.js";
import {
CreateMongoIndexesProps,
SEARCH_MAX_NUM_RESULTS,
Expand Down
Loading
Loading