Skip to content

Commit

Permalink
fix(serve): load client from given path in config, support cjs __dirname
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalygashkov committed Dec 11, 2024
1 parent 5e916ab commit 2e47607
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dev": "wxt",
"dev:firefox": "wxt -b firefox",
"build": "npm run build:lib && npm run build:chrome",
"build:lib": "tsup --entry.lib src/lib/main.ts --entry.cli src/cli/main.ts --format cjs,esm --dts --clean --minify --minify-identifiers",
"build:lib": "tsup --entry.lib src/lib/main.ts --entry.cli src/cli/main.ts --format cjs,esm --shims --dts --clean",
"build:chrome": "wxt build",
"build:firefox": "wxt build -b firefox",
"zip": "wxt zip",
Expand Down
19 changes: 11 additions & 8 deletions src/cli/commands/serve/api/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFile } from 'node:fs/promises';
import { basename } from 'node:path';
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import { Session } from '../../../../lib/session';
Expand Down Expand Up @@ -27,19 +28,21 @@ const plugin: FastifyPluginAsyncTypebox = async (server) => {
const secretKey = request.headers['x-secret-key'] as string;
const clientName = request.body.client;
const user = config.users[secretKey];
const hasClient = config.clients.some((client: string) =>
client.includes(clientName),
const clientPath = config.clients.find((client: string) =>
basename(client).includes(clientName),
);
const hasUserClient = user?.clients.includes(clientName);
if (!hasClient || !hasUserClient) {
const clientAllowed = user?.clients.includes(clientName);
if (!clientPath || !clientAllowed) {
return reply.forbidden(
'Client is not found or you are not authorized to use it.',
);
}
const wvd = await readFile('client.wvd');
const client = clients.get(clientName) || (await Client.fromPacked(wvd));
if (!clients.has(clientName!)) clients.set(clientName!, client);
const session = new Session('temporary', client);
if (!clients.has(clientName)) {
const wvd = await readFile(clientPath);
const client = await Client.fromPacked(wvd);
clients.set(clientName, client);
}
const session = new Session('temporary', clients.get(clientName)!);
const sessionKey = `${secretKey}:${session.sessionId}`;
sessions.set(sessionKey, session);
return { id: session.sessionId };
Expand Down
23 changes: 15 additions & 8 deletions src/cli/commands/serve/serve.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readdir } from 'node:fs/promises';
import { basename, extname } from 'node:path';
import fastify from 'fastify';
import AutoLoad from '@fastify/autoload';
Expand All @@ -18,18 +19,24 @@ type ServeOptions = {
export const serve = async (options: ServeOptions = {}) => {
const server = fastify({ logger: true });

await loadConfig(options.config || 'azot.config.json');
const configPath = options.config || 'azot.config.json';
await loadConfig(configPath);
if (options.client) config.clients.push(options.client);
if (options.secret && options.client) {
const clientFilename = basename(options.client);
if (!config.clients.length) {
const files = await readdir(process.cwd());
const clientPath = files.find((file) => file.endsWith('.wvd'));
if (clientPath) config.clients.push(clientPath);
}
if (options.secret) {
const anonymousUser = { name: 'anonymous', clients: [] };
const user = config.users[options.secret] || anonymousUser;
const clientFilename = basename(options.client || config.clients.at(-1)!);
const clientName = clientFilename.replace(extname(clientFilename), '');
config.users[options.secret] = {
name: 'anonymous',
clients: [clientName],
};
if (!user.clients.length) user.clients.push(clientName);
config.users[options.secret] = user;
}

server.register(AutoLoad, { dir: `${import.meta.dirname}/api` });
server.register(AutoLoad, { dir: `${import.meta.dirname || __dirname}/api` });
server.register(RateLimit, { max: 100, timeWindow: '1 minute' });
server.register(Helmet, { global: true });
server.register(Sensible);
Expand Down

0 comments on commit 2e47607

Please sign in to comment.