feat: switched from worker API to fs based

This commit is contained in:
Morten Olsen
2024-01-12 14:14:40 +01:00
parent 6d8e5bf955
commit 5e7461c10b
36 changed files with 419 additions and 58 deletions

View File

@@ -58,7 +58,7 @@ mini-loader artifacts ls
To download a specific artifact: To download a specific artifact:
```bash ```bash
mini-loader artifacts pull <id> > myfile.txt mini-loader artifacts pull <id> myfile.txt
``` ```
Replace `<id>` with the identifier of the artifact you wish to download. Replace `<id>` with the identifier of the artifact you wish to download.
@@ -67,4 +67,4 @@ Replace `<id>` with the identifier of the artifact you wish to download.
You're now equipped to manage loads, runs, logs, and artifacts using the mini loader CLI. For advanced usage, such as managing secrets, proceed to the next section. You're now equipped to manage loads, runs, logs, and artifacts using the mini loader CLI. For advanced usage, such as managing secrets, proceed to the next section.
[Next: Managing Secrets](./managing-secrets.md) [Next: Managing Secrets](./managing-secrets.md)

View File

@@ -27,6 +27,7 @@
"@rollup/plugin-sucrase": "^5.0.2", "@rollup/plugin-sucrase": "^5.0.2",
"@trpc/client": "^10.45.0", "@trpc/client": "^10.45.0",
"commander": "^11.1.0", "commander": "^11.1.0",
"env-paths": "^3.0.0",
"inquirer": "^9.2.12", "inquirer": "^9.2.12",
"ora": "^8.0.1", "ora": "^8.0.1",
"rollup": "^4.9.4", "rollup": "^4.9.4",

View File

@@ -2,13 +2,20 @@ import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import superjson from 'superjson'; import superjson from 'superjson';
import type { Runtime } from '@morten-olsen/mini-loader-server'; import type { Runtime } from '@morten-olsen/mini-loader-server';
import type { RootRouter } from '@morten-olsen/mini-loader-server'; import type { RootRouter } from '@morten-olsen/mini-loader-server';
import { Context } from '../context/context.js';
const createClient = () => { const createClient = (context: Context) => {
if (!context.host || !context.token) {
throw new Error('Not signed in');
}
const client = createTRPCProxyClient<RootRouter>({ const client = createTRPCProxyClient<RootRouter>({
transformer: superjson, transformer: superjson,
links: [ links: [
httpBatchLink({ httpBatchLink({
url: 'http://localhost:4500/trpc', url: `${context.host}/trpc`,
headers: {
authorization: `Bearer ${context.token}`,
},
}), }),
], ],
}); });

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const list = new Command('list'); const list = new Command('list');
@@ -20,8 +21,9 @@ list
.option('-a, --limit <limit>', 'Limit', '1000') .option('-a, --limit <limit>', 'Limit', '1000')
.action(async () => { .action(async () => {
const { runId, loadId, offset, limit } = list.opts(); const { runId, loadId, offset, limit } = list.opts();
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
const artifacts = await step('Getting artifacts', async () => { const artifacts = await step('Getting artifacts', async () => {
return await client.artifacts.find.query({ return await client.artifacts.find.query({

View File

@@ -0,0 +1,32 @@
import { Command } from 'commander';
import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
import { dirname, resolve } from 'path';
import { mkdir, writeFile } from 'fs/promises';
const pull = new Command('pull');
pull
.description('Download artifact')
.argument('<artifact-id>', 'Artifact ID')
.argument('<file>', 'File to save')
.action(async (id, file) => {
const context = new Context();
const target = resolve(file);
const client = await step('Connecting to server', async () => {
return createClient(context);
});
const artifact = await step('Getting artifact', async () => {
const result = await client.artifacts.get.query(id);
if (!result) {
throw new Error('Artifact not found');
}
return result;
});
await mkdir(dirname(target), { recursive: true });
const data = Buffer.from(artifact.data, 'base64').toString('utf-8');
await writeFile(target, data, 'utf-8');
});
export { pull };

View File

@@ -0,0 +1,59 @@
import { Command } from 'commander';
import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
import inquirer from 'inquirer';
const remove = new Command('remove');
const toInt = (value?: string) => {
if (!value) {
return undefined;
}
return parseInt(value, 10);
};
remove
.alias('ls')
.description('List logs')
.option('-r, --run-id <runId>', 'Run ID')
.option('-l, --load-id <loadId>', 'Load ID')
.option('-o, --offset <offset>', 'Offset')
.option('-a, --limit <limit>', 'Limit', '1000')
.action(async () => {
const { runId, loadId, offset, limit } = remove.opts();
const context = new Context();
const client = await step('Connecting to server', async () => {
return createClient(context);
});
const response = await step('Preparing to delete', async () => {
return await client.artifacts.prepareRemove.query({
runId,
loadId,
offset: toInt(offset),
limit: toInt(limit),
});
});
if (!response.ids.length) {
console.log('No logs to delete');
return;
}
const { confirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to delete ${response.ids.length} logs?`,
},
]);
if (!confirm) {
return;
}
await step('Deleting artifacts', async () => {
await client.artifacts.remove.mutate(response);
});
});
export { remove };

View File

@@ -1,7 +1,11 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { list } from './artifacts.list.js'; import { list } from './artifacts.list.js';
import { remove } from './artifacts.remove.js';
import { pull } from './artifacts.pull.js';
const artifacts = new Command('artifacts'); const artifacts = new Command('artifacts');
artifacts.addCommand(list); artifacts.addCommand(list);
artifacts.addCommand(remove);
artifacts.addCommand(pull);
export { artifacts }; export { artifacts };

View File

@@ -1,16 +1,19 @@
import { Command } from 'commander'; import { Command } from 'commander';
import inquerer from 'inquirer'; import inquerer from 'inquirer';
import { Context } from '../../context/context.js';
import { step } from '../../utils/step.js';
const login = new Command('login'); const login = new Command('login');
login.description('Login to your account'); login.description('Login to your account');
login.action(async () => { login.action(async () => {
const context = new Context();
const { host, token } = await inquerer.prompt([ const { host, token } = await inquerer.prompt([
{ {
type: 'input', type: 'input',
name: 'host', name: 'host',
message: 'Enter the host of your server', message: 'Enter the host of your server',
default: 'http://localhost:4500', default: context.host ?? 'http://localhost:4500',
}, },
{ {
type: 'password', type: 'password',
@@ -19,7 +22,25 @@ login.action(async () => {
}, },
]); ]);
console.log(host, token); const healthResponse = await step('Getting auth status', async () => {
return await fetch(`${host}/health`, {
headers: {
authorization: `Bearer ${token}`,
},
});
});
if (!healthResponse.ok) {
throw new Error('Invalid token');
}
const health = await healthResponse.json();
if (!health.authorized) {
throw new Error('Invalid token');
}
await step('Saving login', async () => {
await context.saveLogin(host, token);
});
}); });
export { login }; export { login };

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const list = new Command('list'); const list = new Command('list');
@@ -8,11 +9,12 @@ list
.alias('ls') .alias('ls')
.description('List loads') .description('List loads')
.action(async () => { .action(async () => {
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
const loads = step('Getting data', async () => { const loads = await step('Getting data', async () => {
await client.loads.find.query({}); return await client.loads.find.query({});
}); });
console.table(loads); console.table(loads);
}); });

View File

@@ -3,6 +3,7 @@ import { resolve } from 'path';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { bundle } from '../../bundler/bundler.js'; import { bundle } from '../../bundler/bundler.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const push = new Command('push'); const push = new Command('push');
@@ -14,9 +15,10 @@ push
.option('-ai, --auto-install', 'Auto install dependencies', false) .option('-ai, --auto-install', 'Auto install dependencies', false)
.action(async (script) => { .action(async (script) => {
const opts = push.opts(); const opts = push.opts();
const context = new Context();
const location = resolve(script); const location = resolve(script);
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
const code = await step('Bundling', async () => { const code = await step('Bundling', async () => {
return await bundle({ entry: location, autoInstall: opts.autoInstall }); return await bundle({ entry: location, autoInstall: opts.autoInstall });

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const list = new Command('list'); const list = new Command('list');
@@ -22,8 +23,9 @@ list
.option('-s, --sort <order>', 'Sort', 'desc') .option('-s, --sort <order>', 'Sort', 'desc')
.action(async () => { .action(async () => {
const { runId, loadId, severities, offset, limit, order } = list.opts(); const { runId, loadId, severities, offset, limit, order } = list.opts();
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
const logs = await step('Getting logs', async () => { const logs = await step('Getting logs', async () => {
return await client.logs.find.query({ return await client.logs.find.query({

View File

@@ -0,0 +1,63 @@
import { Command } from 'commander';
import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
import inquirer from 'inquirer';
const remove = new Command('remove');
const toInt = (value?: string) => {
if (!value) {
return undefined;
}
return parseInt(value, 10);
};
remove
.alias('ls')
.description('List logs')
.option('-r, --run-id <runId>', 'Run ID')
.option('-l, --load-id <loadId>', 'Load ID')
.option('--severities <severities...>', 'Severities')
.option('-o, --offset <offset>', 'Offset')
.option('-a, --limit <limit>', 'Limit', '1000')
.option('-s, --sort <order>', 'Sort', 'desc')
.action(async () => {
const { runId, loadId, severities, offset, limit, order } = remove.opts();
const context = new Context();
const client = await step('Connecting to server', async () => {
return createClient(context);
});
const response = await step('Preparing to delete', async () => {
return await client.logs.prepareRemove.query({
runId,
loadId,
severities,
offset: toInt(offset),
limit: toInt(limit),
order,
});
});
if (!response.ids.length) {
console.log('No logs to delete');
return;
}
const { confirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to delete ${response.ids.length} logs?`,
},
]);
if (!confirm) {
return;
}
await step('Deleting logs', async () => {
await client.logs.remove.mutate(response);
});
});
export { remove };

View File

@@ -1,7 +1,9 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { list } from './logs.list.js'; import { list } from './logs.list.js';
import { remove } from './logs.remove.js';
const logs = new Command('logs'); const logs = new Command('logs');
logs.addCommand(list); logs.addCommand(list);
logs.addCommand(remove);
export { logs }; export { logs };

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const create = new Command('create'); const create = new Command('create');
@@ -8,8 +9,9 @@ create
.description('Create a new run') .description('Create a new run')
.argument('load-id', 'Load ID') .argument('load-id', 'Load ID')
.action(async (loadId) => { .action(async (loadId) => {
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
await step('Creating run', async () => { await step('Creating run', async () => {
await client.runs.create.mutate({ loadId }); await client.runs.create.mutate({ loadId });

View File

@@ -1,16 +1,18 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const list = new Command('create'); const list = new Command('list');
list list
.alias('ls') .alias('ls')
.description('Find a run') .description('Find a run')
.argument('[load-id]', 'Load ID') .argument('[load-id]', 'Load ID')
.action(async (loadId) => { .action(async (loadId) => {
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
const runs = await step('Getting runs', async () => { const runs = await step('Getting runs', async () => {
return await client.runs.find.query({ loadId }); return await client.runs.find.query({ loadId });

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const list = new Command('list'); const list = new Command('list');
@@ -18,8 +19,9 @@ list
.option('-a, --limit <limit>', 'Limit', '1000') .option('-a, --limit <limit>', 'Limit', '1000')
.action(async () => { .action(async () => {
const { offset, limit } = list.opts(); const { offset, limit } = list.opts();
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
const secrets = await step('Getting secrets', async () => { const secrets = await step('Getting secrets', async () => {
return await client.secrets.find.query({ return await client.secrets.find.query({

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const remove = new Command('remove'); const remove = new Command('remove');
@@ -8,8 +9,9 @@ remove
.alias('rm') .alias('rm')
.argument('<id>') .argument('<id>')
.action(async (id) => { .action(async (id) => {
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
await step('Removing', async () => { await step('Removing', async () => {
await client.secrets.remove.mutate({ await client.secrets.remove.mutate({

View File

@@ -1,6 +1,7 @@
import { Command } from 'commander'; import { Command } from 'commander';
import { createClient } from '../../client/client.js'; import { createClient } from '../../client/client.js';
import { step } from '../../utils/step.js'; import { step } from '../../utils/step.js';
import { Context } from '../../context/context.js';
const set = new Command('set'); const set = new Command('set');
@@ -8,8 +9,9 @@ set
.argument('<id>') .argument('<id>')
.argument('[value]') .argument('[value]')
.action(async (id, value) => { .action(async (id, value) => {
const context = new Context();
const client = await step('Connecting to server', async () => { const client = await step('Connecting to server', async () => {
return createClient(); return createClient(context);
}); });
await step('Setting secret', async () => { await step('Setting secret', async () => {
await client.secrets.set.mutate({ await client.secrets.set.mutate({

View File

@@ -0,0 +1,50 @@
import envPaths from 'env-paths';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { mkdir } from 'fs/promises';
import { dirname } from 'path';
type ContextValues = {
host: string;
token: string;
};
class Context {
#location: string;
#config?: ContextValues;
constructor() {
const paths = envPaths('dws');
this.#location = paths.config;
if (existsSync(this.#location)) {
this.#config = JSON.parse(readFileSync(this.#location, 'utf-8'));
}
}
public get host() {
return this.#config?.host;
}
public get token() {
return this.#config?.token;
}
public saveLogin = (host: string, token: string) => {
this.#config = {
...(this.#config || {}),
host,
token,
};
this.save();
};
public save = async () => {
if (!this.#config) {
return;
}
const json = JSON.stringify(this.#config);
mkdir(dirname(this.#location), { recursive: true });
writeFileSync(this.#location, json);
};
}
export { Context };

View File

@@ -4,10 +4,10 @@ const step = async <T>(message: string, fn: () => Promise<T>): Promise<T> => {
const spinner = ora(message).start(); const spinner = ora(message).start();
try { try {
const result = await fn(); const result = await fn();
spinner.succeed(); await spinner.succeed();
return result; return result;
} catch (err) { } catch (err) {
spinner.fail(); await spinner.fail();
throw err; throw err;
} }
}; };

View File

@@ -1,5 +1,5 @@
import { artifacts, logger } from '@morten-olsen/mini-loader'; import { artifacts, logger } from '@morten-olsen/mini-loader';
logger.info('Hello world'); await logger.info('Hello world');
artifacts.create('foo', 'bar'); await artifacts.create('foo', 'bar');

View File

@@ -8,8 +8,8 @@ type ArtifactCreateEvent = {
}; };
}; };
const create = (name: string, data: Buffer | string) => { const create = async (name: string, data: Buffer | string) => {
send({ await send({
type: 'artifact:create', type: 'artifact:create',
payload: { payload: {
name, name,

View File

@@ -1,7 +1,14 @@
import { workerData } from 'worker_threads'; import { existsSync } from 'fs';
import { readFile } from 'fs/promises';
const get = <T>() => { const path = process.env.INPUT_PATH;
return workerData as T; const hasInput = path ? existsSync(path) : false;
const get = () => {
if (!hasInput || !path) {
return undefined;
}
return readFile(path, 'utf-8');
}; };
const input = { const input = {

View File

@@ -9,31 +9,31 @@ type LoggerEvent = {
}; };
}; };
const sendLog = (event: LoggerEvent['payload']) => { const sendLog = async (event: LoggerEvent['payload']) => {
send({ await send({
type: 'log', type: 'log',
payload: event, payload: event,
}); });
}; };
const info = (message: string, data?: unknown) => { const info = async (message: string, data?: unknown) => {
sendLog({ await sendLog({
severity: 'info', severity: 'info',
message, message,
data, data,
}); });
}; };
const warn = (message: string, data?: unknown) => { const warn = async (message: string, data?: unknown) => {
sendLog({ await sendLog({
severity: 'warning', severity: 'warning',
message, message,
data, data,
}); });
}; };
const error = (message: string, data?: unknown) => { const error = async (message: string, data?: unknown) => {
sendLog({ await sendLog({
severity: 'error', severity: 'error',
message, message,
data, data,

View File

@@ -1,8 +1,7 @@
import { workerData } from 'worker_threads'; const secretData = JSON.parse(process.env.SECRETS || '{}');
const get = (id: string) => { const get = (id: string) => {
const items = workerData?.secrets ?? {}; return secretData[id];
return items[id];
}; };
const secrets = { const secrets = {

View File

@@ -1,8 +1,30 @@
import { parentPort } from 'worker_threads'; import { Socket, createConnection } from 'net';
const send = (data: any) => { const connect = () =>
const cleaned = JSON.parse(JSON.stringify(data)); new Promise<Socket>((resolve, reject) => {
parentPort?.postMessage(cleaned); const current = createConnection(process.env.HOST_SOCKET!);
};
current.on('connect', () => {
resolve(current);
});
current.on('error', (error) => {
reject(error);
});
});
const connectionRequest = connect();
const send = async (data: any) =>
new Promise<void>(async (resolve, reject) => {
const connection = await connectionRequest;
const cleaned = JSON.parse(JSON.stringify(data));
connection.write(JSON.stringify(cleaned), 'utf-8', (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
export { send }; export { send };

View File

@@ -16,12 +16,13 @@
} }
}, },
"devDependencies": { "devDependencies": {
"@morten-olsen/mini-loader-configs": "workspace:^",
"@morten-olsen/mini-loader": "workspace:^", "@morten-olsen/mini-loader": "workspace:^",
"@morten-olsen/mini-loader-configs": "workspace:^",
"@types/node": "^20.10.8", "@types/node": "^20.10.8",
"typescript": "^5.3.3" "typescript": "^5.3.3"
}, },
"dependencies": { "dependencies": {
"eventemitter3": "^5.0.1" "eventemitter3": "^5.0.1",
"nanoid": "^5.0.4"
} }
} }

View File

@@ -1,6 +1,11 @@
import { Worker } from 'worker_threads'; import { Worker } from 'worker_threads';
import os from 'os';
import { EventEmitter } from 'eventemitter3'; import { EventEmitter } from 'eventemitter3';
import { Event } from '@morten-olsen/mini-loader'; import { Event } from '@morten-olsen/mini-loader';
import { join } from 'path';
import { createServer } from 'http';
import { nanoid } from 'nanoid';
import { chmod, mkdir, unlink, writeFile } from 'fs/promises';
type RunEvents = { type RunEvents = {
message: (event: Event) => void; message: (event: Event) => void;
@@ -10,18 +15,40 @@ type RunEvents = {
type RunOptions = { type RunOptions = {
script: string; script: string;
input?: unknown; input?: Buffer | string;
secrets?: Record<string, string>; secrets?: Record<string, string>;
}; };
const run = async ({ script, input, secrets }: RunOptions) => { const run = async ({ script, input, secrets }: RunOptions) => {
const dataDir = join(os.tmpdir(), 'mini-loader', nanoid());
await mkdir(dataDir, { recursive: true });
await chmod(dataDir, 0o700);
const hostSocket = join(dataDir, 'host');
const server = createServer();
const inputLocation = join(dataDir, 'input');
if (input) {
await writeFile(inputLocation, input);
}
const emitter = new EventEmitter<RunEvents>(); const emitter = new EventEmitter<RunEvents>();
server.on('connection', (socket) => {
socket.on('data', (data) => {
const message = JSON.parse(data.toString());
emitter.emit('message', message);
});
});
const worker = new Worker(script, { const worker = new Worker(script, {
eval: true, eval: true,
env: secrets, env: {
HOST_SOCKET: hostSocket,
SECRETS: JSON.stringify(secrets),
INPUT_PATH: inputLocation,
},
workerData: { workerData: {
input, input,
secrets,
}, },
}); });
@@ -29,10 +56,14 @@ const run = async ({ script, input, secrets }: RunOptions) => {
worker.on('message', (message: Event) => { worker.on('message', (message: Event) => {
emitter.emit('message', message); emitter.emit('message', message);
}); });
worker.on('exit', () => { worker.on('exit', async () => {
server.close();
await unlink(hostSocket);
resolve(); resolve();
}); });
worker.on('error', (error) => { worker.on('error', async (error) => {
server.close();
await unlink(hostSocket);
reject(error); reject(error);
}); });
}); });

View File

@@ -18,6 +18,13 @@ class ArtifactRepo extends EventEmitter<ArtifactRepoEvents> {
this.#options = options; this.#options = options;
} }
public get = async (id: string) => {
const { database } = this.#options;
const db = await database.instance;
const result = await db('artifacts').where({ id }).first();
return result || null;
};
public add = async (options: AddArtifactOptions) => { public add = async (options: AddArtifactOptions) => {
const { database } = this.#options; const { database } = this.#options;
const db = await database.instance; const db = await database.instance;
@@ -59,8 +66,9 @@ class ArtifactRepo extends EventEmitter<ArtifactRepoEvents> {
query.limit(options.limit); query.limit(options.limit);
} }
const ids = await query; const result = await query;
const token = ids.map((id) => Buffer.from(id.id).toString('base64')).join('|'); const ids = result.map((row) => row.id);
const token = ids.map((id) => Buffer.from(id).toString('base64')).join('|');
const hash = createHash('sha256').update(token).digest('hex'); const hash = createHash('sha256').update(token).digest('hex');
return { return {
ids, ids,

View File

@@ -56,8 +56,9 @@ class LogRepo extends EventEmitter<LogRepoEvents> {
query.whereIn('severity', options.severities); query.whereIn('severity', options.severities);
} }
const ids = await query; const result = await query;
const token = ids.map((id) => Buffer.from(id.id).toString('base64')).join('|'); const ids = result.map((row) => row.id);
const token = ids.map((id) => Buffer.from(id).toString('base64')).join('|');
const hash = createHash('sha256').update(token).digest('hex'); const hash = createHash('sha256').update(token).digest('hex');
return { return {
ids, ids,

View File

@@ -11,12 +11,21 @@ const find = publicProcedure.input(findArtifactsSchema).query(async ({ input, ct
return result; return result;
}); });
const get = publicProcedure.input(z.string()).query(async ({ input, ctx }) => {
const { runtime } = ctx;
const { repos } = runtime;
const { artifacts } = repos;
const result = await artifacts.get(input);
return result;
});
const prepareRemove = publicProcedure.input(findArtifactsSchema).query(async ({ input, ctx }) => { const prepareRemove = publicProcedure.input(findArtifactsSchema).query(async ({ input, ctx }) => {
const { runtime } = ctx; const { runtime } = ctx;
const { repos } = runtime; const { repos } = runtime;
const { artifacts } = repos; const { artifacts } = repos;
await artifacts.prepareRemove(input); return await artifacts.prepareRemove(input);
}); });
const remove = publicProcedure const remove = publicProcedure
@@ -35,6 +44,7 @@ const remove = publicProcedure
}); });
const artifactsRouter = router({ const artifactsRouter = router({
get,
find, find,
remove, remove,
prepareRemove, prepareRemove,

View File

@@ -16,7 +16,7 @@ const prepareRemove = publicProcedure.input(findLogsSchema).query(async ({ input
const { repos } = runtime; const { repos } = runtime;
const { logs } = repos; const { logs } = repos;
await logs.prepareRemove(input); return await logs.prepareRemove(input);
}); });
const remove = publicProcedure const remove = publicProcedure

View File

@@ -14,7 +14,8 @@ const createContext = async ({ runtime }: ContextOptions) => {
if (!authorization) { if (!authorization) {
throw new Error('No authorization header'); throw new Error('No authorization header');
} }
await auth.validateToken(authorization); const [, token] = authorization.split(' ');
await auth.validateToken(token);
return { return {
runtime, runtime,
}; };

View File

@@ -54,7 +54,7 @@ class RunnerInstance extends EventEmitter<RunnerInstanceEvents> {
const { runs, secrets } = repos; const { runs, secrets } = repos;
try { try {
const { script: scriptHash, input } = await runs.getById(id); const { script: scriptHash, input } = await runs.getById(id);
const scriptLocation = resolve(config.files.location, 'script', `${scriptHash}.js`); const scriptLocation = resolve(config.files.location, 'scripts', `${scriptHash}.js`);
const script = await readFile(scriptLocation, 'utf-8'); const script = await readFile(scriptLocation, 'utf-8');
const allSecrets = await secrets.getAll(); const allSecrets = await secrets.getAll();
await runs.started(id); await runs.started(id);

View File

@@ -10,6 +10,19 @@ const createServer = async (runtime: Runtime) => {
return { hello: 'world' }; return { hello: 'world' };
}); });
server.get('/health', async (req) => {
let authorized = false;
try {
const { authorization } = req.headers;
if (authorization) {
const [, token] = authorization.split(' ');
await runtime.auth.validateToken(token);
authorized = true;
}
} catch (error) {}
return { authorized, status: 'ok' };
});
server.register(fastifyTRPCPlugin, { server.register(fastifyTRPCPlugin, {
prefix: '/trpc', prefix: '/trpc',
trpcOptions: { trpcOptions: {

11
pnpm-lock.yaml generated
View File

@@ -60,6 +60,9 @@ importers:
commander: commander:
specifier: ^11.1.0 specifier: ^11.1.0
version: 11.1.0 version: 11.1.0
env-paths:
specifier: ^3.0.0
version: 3.0.0
inquirer: inquirer:
specifier: ^9.2.12 specifier: ^9.2.12
version: 9.2.12 version: 9.2.12
@@ -132,6 +135,9 @@ importers:
eventemitter3: eventemitter3:
specifier: ^5.0.1 specifier: ^5.0.1
version: 5.0.1 version: 5.0.1
nanoid:
specifier: ^5.0.4
version: 5.0.4
devDependencies: devDependencies:
'@morten-olsen/mini-loader': '@morten-olsen/mini-loader':
specifier: workspace:^ specifier: workspace:^
@@ -2206,6 +2212,11 @@ packages:
dev: false dev: false
optional: true optional: true
/env-paths@3.0.0:
resolution: {integrity: sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dev: false
/err-code@2.0.3: /err-code@2.0.3:
resolution: {integrity: sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA==} resolution: {integrity: sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA==}
requiresBuild: true requiresBuild: true