From d8ef26dc74e3cf6f0d13f8e9902a10078f952e39 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Fri, 15 Mar 2024 17:02:00 +0700 Subject: [PATCH] UBERF-5933: Add 404 handling in case of resource direct requests (#4983) Signed-off-by: Andrey Sobolev --- dev/prod/tsconfig.json | 1 + packages/platform/src/resource.ts | 39 ++++++---------------- plugins/workbench-resources/src/connect.ts | 2 +- server/front/src/index.ts | 23 ++++++++++++- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/dev/prod/tsconfig.json b/dev/prod/tsconfig.json index d1f89d04a5..4ddf0efd12 100644 --- a/dev/prod/tsconfig.json +++ b/dev/prod/tsconfig.json @@ -9,6 +9,7 @@ "sourceMap": true, "skipLibCheck": true, "moduleResolution": "node", + "declarationDir": "./types", "allowSyntheticDefaultImports": true, "lib": [ "es2016", diff --git a/packages/platform/src/resource.ts b/packages/platform/src/resource.ts index cd903867a5..a6507aef51 100644 --- a/packages/platform/src/resource.ts +++ b/packages/platform/src/resource.ts @@ -79,42 +79,23 @@ async function loadPlugin (id: Plugin): Promise { plugin: id }) pluginLoader = monitor(status, getLocation(id)()).then(async (plugin) => { - return await retryLoading(async () => { - try { - // In case of ts-node, we have a bit different import structure, so let's check for it. - if (typeof plugin.default === 'object') { - // eslint-disable-next-line @typescript-eslint/return-await - return await (plugin as any).default.default() - } - return await plugin.default() - } catch (err: any) { - console.error(err) - throw err + try { + // In case of ts-node, we have a bit different import structure, so let's check for it. + if (typeof plugin.default === 'object') { + // eslint-disable-next-line @typescript-eslint/return-await + return await (plugin as any).default.default() } - }) + return await plugin.default() + } catch (err: any) { + console.error(err) + throw err + } }) loading.set(id, pluginLoader) } return await pluginLoader } -async function retryLoading (op: () => Promise): Promise { - let lastErr: any - for (let i = 0; i < 3; i++) { - try { - return await op() - } catch (err: any) { - if (/Loading chunk [\d]+ failed/.test(err.message)) { - // Do not report on console and try to load again. - // After a short delay - await new Promise((resolve) => setTimeout(resolve, 50)) - } - lastErr = err - } - } - throw lastErr -} - const cachedResource = new Map() /** diff --git a/plugins/workbench-resources/src/connect.ts b/plugins/workbench-resources/src/connect.ts index 7c894f4616..828c166d5f 100644 --- a/plugins/workbench-resources/src/connect.ts +++ b/plugins/workbench-resources/src/connect.ts @@ -172,7 +172,7 @@ export async function connect (title: string): Promise { console.log('Model version', version) const requiredVersion = getMetadata(presentation.metadata.RequiredVersion) - if (requiredVersion !== undefined && version !== undefined) { + if (requiredVersion !== undefined && version !== undefined && requiredVersion !== '') { console.log('checking min model version', requiredVersion) const versionStr = versionToString(version) diff --git a/server/front/src/index.ts b/server/front/src/index.ts index 881a1a65ca..3a796d5c43 100644 --- a/server/front/src/index.ts +++ b/server/front/src/index.ts @@ -24,7 +24,7 @@ import express, { Response } from 'express' import fileUpload, { UploadedFile } from 'express-fileupload' import https from 'https' import morgan from 'morgan' -import { join, resolve } from 'path' +import { extname, join, resolve } from 'path' import { cwd } from 'process' import sharp from 'sharp' import { v4 as uuid } from 'uuid' @@ -556,6 +556,27 @@ export function start ( }) app.get('*', function (request, response) { + const url = request.path.split('/').filter((it) => it !== '') + if (url.length === 1) { + const ext = extname(url[0]) + const notFoundResource = [ + '.js', + '.js.gz', + '.svg', + '.webp', + '.woff', + '.woff2', + '.svg.gz', + '.css', + '.css.gz', + '.png', + '.avif' + ] + if (notFoundResource.includes(ext)) { + response.sendStatus(404) + return + } + } response.sendFile(join(dist, 'index.html'), { maxAge: cacheControlMaxAge, etag: true,