Skip to content

Commit 27a2235

Browse files
authored
refactor(webui): Migrate S3Manager to new Fastify architecture. (#1098)
1 parent 65dc855 commit 27a2235

File tree

4 files changed

+30
-38
lines changed

4 files changed

+30
-38
lines changed

components/webui/server/src/app.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {
33
FastifyPluginAsync,
44
} from "fastify";
55

6-
import settings from "../settings.json" with {type: "json"};
7-
import S3Manager from "./plugins/S3Manager.js";
86
import staticRoutes from "./routes/static.js";
97

108

@@ -20,16 +18,6 @@ import staticRoutes from "./routes/static.js";
2018
const FastifyV1App: FastifyPluginAsync = async (
2119
fastify: FastifyInstance
2220
) => {
23-
if ("test" !== process.env.NODE_ENV) {
24-
await fastify.register(
25-
S3Manager,
26-
{
27-
region: settings.StreamFilesS3Region,
28-
profile: settings.StreamFilesS3Profile,
29-
}
30-
);
31-
}
32-
3321
// Register the routes
3422
await fastify.register(staticRoutes);
3523
};

components/webui/server/src/plugins/S3Manager.ts renamed to components/webui/server/src/fastify-v2/plugins/app/S3Manager/index.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import {
33
S3Client,
44
} from "@aws-sdk/client-s3";
55
import {getSignedUrl} from "@aws-sdk/s3-request-presigner";
6-
import fastifyPlugin from "fastify-plugin";
6+
import fp from "fastify-plugin";
77

8-
import {Nullable} from "../typings/common.js";
8+
import settings from "../../../../../settings.json" with {type: "json"};
9+
import {Nullable} from "../../../../typings/common.js";
10+
import {PRE_SIGNED_URL_EXPIRY_TIME_SECONDS} from "./typings.js";
911

1012

11-
/**
12-
* Expiry time in seconds for pre-signed URLs.
13-
*/
14-
const PRE_SIGNED_URL_EXPIRY_TIME_SECONDS = 3600;
15-
1613
/**
1714
* Class to manage Simple Storage Service (S3) objects.
1815
*/
@@ -61,24 +58,27 @@ class S3Manager {
6158
}
6259
}
6360

64-
/**
65-
* Initializes a Fastify plugin, which decorates the application with an S3 manager at the
66-
* "s3Manager" property only when all plugin options are valid.
67-
*/
68-
export default fastifyPlugin(
69-
async (app, options: {region: Nullable<string>; profile: Nullable<string>}) => {
70-
const {region, profile} = options;
71-
if (null === region) {
72-
return;
73-
}
74-
75-
console.log(`Initializing S3Manager with region="${region}" and profile="${profile}"...`);
76-
app.decorate("s3Manager", new S3Manager(region, profile));
77-
}
78-
);
79-
8061
declare module "fastify" {
8162
interface FastifyInstance {
82-
s3Manager?: S3Manager;
63+
S3Manager?: S3Manager;
8364
}
8465
}
66+
67+
export default fp(
68+
(fastify) => {
69+
const region = settings.StreamFilesS3Region;
70+
const profile = settings.StreamFilesS3Profile;
71+
72+
// Only decorate if the region is set (i.e. s3 support is configured in package)
73+
// Disable no-unnecessary-condition since linter doesn't understand that settings
74+
// values are not hardcoded.
75+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
76+
if (null !== region && "" !== region) {
77+
fastify.log.info(
78+
{region, profile},
79+
"Initializing S3Manager"
80+
);
81+
fastify.decorate("S3Manager", new S3Manager(region, profile));
82+
}
83+
},
84+
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Expiry time in seconds for pre-signed URLs.
3+
*/
4+
export const PRE_SIGNED_URL_EXPIRY_TIME_SECONDS = 3600;

components/webui/server/src/fastify-v2/routes/api/stream-files/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
6969
}
7070
}
7171

72-
if (fastify.hasDecorator("s3Manager") && "undefined" !== typeof fastify.s3Manager) {
73-
streamMetadata.path = await fastify.s3Manager.getPreSignedUrl(
72+
if (fastify.hasDecorator("S3Manager") && "undefined" !== typeof fastify.S3Manager) {
73+
streamMetadata.path = await fastify.S3Manager.getPreSignedUrl(
7474
`s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}`
7575
);
7676
} else {

0 commit comments

Comments
 (0)