Skip to content

Commit

Permalink
service.ts: add /elevateService method
Browse files Browse the repository at this point in the history
  • Loading branch information
throwaway96 committed Mar 5, 2024
1 parent ef463f8 commit 7d58dca
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ function hashString(data: string, algorithm: string): string {
/**
* Elevates a package by name.
*/
async function elevateService(pkg: string): Promise<void> {
if (runningAsRoot) {
console.info('Elevating service...');
await asyncExecFile(path.join(__dirname, 'elevate-service'), [pkg]);
} else {
async function elevateService(pkg: string): Promise<boolean> {
if (!runningAsRoot) {
console.error('Trying to elevate service without running as root. Skipping.');
return false;
}

console.info('Elevating service...');
await asyncExecFile(path.join(__dirname, 'elevate-service'), [pkg]);
return true;
}

/**
Expand Down Expand Up @@ -706,6 +708,35 @@ function runService() {
return { returnValue: true };
}),
);

/**
* Elevates the service specified by "id".
*/
type ElevateServicePayload = { id: string };
service.register(
'elevateService',
tryRespond(async (message: Message) => {
if (!('id' in message.payload)) {
throw new Error('missing "id"');
} else if (typeof message.payload['id'] !== 'string') {

Check failure on line 721 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

["id"] is better written in dot notation
throw new Error('"id" is not a string');
} else if (message.payload['id'] === '') {

Check failure on line 723 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

["id"] is better written in dot notation
throw new Error('"id" is empty');
}

if (!runningAsRoot) {
throw new Error('not running as root');
}

const payload = message.payload as ElevateServicePayload;

const status = await elevateService(payload.id);

if (!status) {
throw new Error('elevateService() failed');
}
}),
);
}

if (process.argv[2] === 'self-update') {
Expand Down

0 comments on commit 7d58dca

Please sign in to comment.