mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-28 05:36:34 +00:00
patch: refactor api to use a single topic
This commit is contained in:
parent
b1d2bdaa06
commit
6582260355
@ -128,35 +128,45 @@ function startApiAndSpawnChild({
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
ipc.serve();
|
ipc.serve();
|
||||||
|
|
||||||
// log is special message which brings back the logs from the child process and prints them to the console
|
// parse and route messages
|
||||||
ipc.server.on('log', (message: string) => {
|
const messagesHandler: any = {
|
||||||
console.log(message);
|
log: (message: any) => {
|
||||||
|
console.log(message);
|
||||||
|
},
|
||||||
|
|
||||||
|
error: (error: any) => {
|
||||||
|
terminateServer(ipc.server);
|
||||||
|
const errorObject = errors.fromJSON(error);
|
||||||
|
reject(errorObject);
|
||||||
|
},
|
||||||
|
|
||||||
|
// once api is ready (means child process is connected) we pass the emit and terminate function to the caller
|
||||||
|
ready: (_: any, socket: any) => {
|
||||||
|
const emit = (type: string, payload: any) => {
|
||||||
|
ipc.server.emit(socket, 'message', { type, payload });
|
||||||
|
};
|
||||||
|
resolve({
|
||||||
|
emit,
|
||||||
|
terminateServer: () => terminateServer(ipc.server),
|
||||||
|
registerHandler,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
ipc.server.on('message', (data: any, socket: any) => {
|
||||||
|
const message = messagesHandler[data.type];
|
||||||
|
if (message) {
|
||||||
|
message(data.payload, socket);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown message type: ${data.type}`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// api to register more handlers with callbacks
|
// api to register more handlers with callbacks
|
||||||
const registerHandler = (event: string, handler: any) => {
|
const registerHandler = (event: string, handler: any) => {
|
||||||
ipc.server.on(event, handler);
|
messagesHandler[event] = handler;
|
||||||
};
|
};
|
||||||
|
|
||||||
// once api is ready (means child process is connected) we pass the emit and terminate function to the caller
|
|
||||||
ipc.server.on('ready', (_: any, socket) => {
|
|
||||||
const emit = (channel: string, data: any) => {
|
|
||||||
ipc.server.emit(socket, channel, data);
|
|
||||||
};
|
|
||||||
resolve({
|
|
||||||
emit,
|
|
||||||
terminateServer: () => terminateServer(ipc.server),
|
|
||||||
registerHandler,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// on api error we terminate
|
|
||||||
ipc.server.on('error', (error: any) => {
|
|
||||||
terminateServer(ipc.server);
|
|
||||||
const errorObject = errors.fromJSON(error);
|
|
||||||
reject(errorObject);
|
|
||||||
});
|
|
||||||
|
|
||||||
// when the api is started we spawn the child process
|
// when the api is started we spawn the child process
|
||||||
ipc.server.on('start', async () => {
|
ipc.server.on('start', async () => {
|
||||||
try {
|
try {
|
||||||
|
@ -48,11 +48,16 @@ ipc.config.stopRetrying = 0;
|
|||||||
const DISCONNECT_DELAY = 100;
|
const DISCONNECT_DELAY = 100;
|
||||||
const IPC_SERVER_ID = process.env.IPC_SERVER_ID as string;
|
const IPC_SERVER_ID = process.env.IPC_SERVER_ID as string;
|
||||||
|
|
||||||
|
console.log('starting ');
|
||||||
|
if (!IPC_SERVER_ID) {
|
||||||
|
console.log('IPC_SERVER_ID is not defined, exiting');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Send a message to the IPC server
|
* @summary Send a message to the IPC server
|
||||||
*/
|
*/
|
||||||
function emit(channel: string, message?: any) {
|
function emit(type: string, payload?: any) {
|
||||||
ipc.of[IPC_SERVER_ID].emit(channel, message);
|
ipc.of[IPC_SERVER_ID].emit('message', { type, payload });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,49 +134,59 @@ ipc.connectTo(IPC_SERVER_ID, () => {
|
|||||||
await terminate(SUCCESS);
|
await terminate(SUCCESS);
|
||||||
});
|
});
|
||||||
|
|
||||||
ipc.of[IPC_SERVER_ID].on('sourceMetadata', async (params) => {
|
const messagesHandler: any = {
|
||||||
const { selected, SourceType, auth } = JSON.parse(params);
|
scan: () => {
|
||||||
try {
|
startScanning();
|
||||||
const sourceMatadata = await getSourceMetadata(
|
},
|
||||||
selected,
|
|
||||||
SourceType,
|
write: async (options: WriteOptions) => {
|
||||||
auth,
|
// Remove leftover tmp files older than 1 hour
|
||||||
);
|
cleanup(Date.now() - 60 * 60 * 1000);
|
||||||
emitSourceMetadata(sourceMatadata);
|
|
||||||
} catch (error: any) {
|
let exitCode = SUCCESS;
|
||||||
emitFail(error);
|
|
||||||
|
ipc.of[IPC_SERVER_ID].on('cancel', () => onAbort(exitCode));
|
||||||
|
|
||||||
|
ipc.of[IPC_SERVER_ID].on('skip', () => onSkip(exitCode));
|
||||||
|
|
||||||
|
const results = await write(options);
|
||||||
|
|
||||||
|
if (results.errors.length > 0) {
|
||||||
|
results.errors = results.errors.map((error: any) => {
|
||||||
|
return toJSON(error);
|
||||||
|
});
|
||||||
|
exitCode = GENERAL_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit('done', { results });
|
||||||
|
await delay(DISCONNECT_DELAY);
|
||||||
|
await terminate(exitCode);
|
||||||
|
},
|
||||||
|
|
||||||
|
sourceMetadata: async (params: any) => {
|
||||||
|
const { selected, SourceType, auth } = JSON.parse(params);
|
||||||
|
try {
|
||||||
|
const sourceMatadata = await getSourceMetadata(
|
||||||
|
selected,
|
||||||
|
SourceType,
|
||||||
|
auth,
|
||||||
|
);
|
||||||
|
emitSourceMetadata(sourceMatadata);
|
||||||
|
} catch (error: any) {
|
||||||
|
emitFail(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
ipc.of[IPC_SERVER_ID].on('message', async (data: any) => {
|
||||||
|
const message = messagesHandler[data.type];
|
||||||
|
if (message) {
|
||||||
|
await message(data.payload);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown message type: ${data.type}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ipc.of[IPC_SERVER_ID].on('scan', async () => {
|
|
||||||
startScanning();
|
|
||||||
});
|
|
||||||
|
|
||||||
// write handler
|
|
||||||
ipc.of[IPC_SERVER_ID].on('write', async (options: WriteOptions) => {
|
|
||||||
// Remove leftover tmp files older than 1 hour
|
|
||||||
cleanup(Date.now() - 60 * 60 * 1000);
|
|
||||||
|
|
||||||
let exitCode = SUCCESS;
|
|
||||||
|
|
||||||
ipc.of[IPC_SERVER_ID].on('cancel', () => onAbort(exitCode));
|
|
||||||
|
|
||||||
ipc.of[IPC_SERVER_ID].on('skip', () => onSkip(exitCode));
|
|
||||||
|
|
||||||
const results = await write(options);
|
|
||||||
|
|
||||||
if (results.errors.length > 0) {
|
|
||||||
results.errors = results.errors.map((error: any) => {
|
|
||||||
return toJSON(error);
|
|
||||||
});
|
|
||||||
exitCode = GENERAL_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit('done', { results });
|
|
||||||
await delay(DISCONNECT_DELAY);
|
|
||||||
await terminate(exitCode);
|
|
||||||
});
|
|
||||||
|
|
||||||
ipc.of[IPC_SERVER_ID].on('connect', () => {
|
ipc.of[IPC_SERVER_ID].on('connect', () => {
|
||||||
log(
|
log(
|
||||||
`Successfully connected to IPC server: ${IPC_SERVER_ID}, socket root ${ipc.config.socketRoot}`,
|
`Successfully connected to IPC server: ${IPC_SERVER_ID}, socket root ${ipc.config.socketRoot}`,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user