mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
refactoring and adding check to pull as well
This commit is contained in:
parent
b581df8424
commit
6058822ad0
@ -35,14 +35,12 @@ export const createDispatcher = (ws: WebSocket) => {
|
||||
const uuid = randomUUID();
|
||||
const payload = { ...message, uuid };
|
||||
let numberOfTimesMessageWasSent = 0;
|
||||
let responseWasReceived = false;
|
||||
|
||||
if (options.attachTransfer) {
|
||||
Object.assign(payload, { transferID: state.transfer?.id });
|
||||
}
|
||||
|
||||
const stringifiedPayload = JSON.stringify(payload);
|
||||
|
||||
ws.send(stringifiedPayload, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
@ -50,28 +48,26 @@ export const createDispatcher = (ws: WebSocket) => {
|
||||
});
|
||||
|
||||
const sendPeriodically = () => {
|
||||
setTimeout(() => {
|
||||
if (!responseWasReceived) {
|
||||
if (numberOfTimesMessageWasSent < 5) {
|
||||
numberOfTimesMessageWasSent += 1;
|
||||
ws.send(stringifiedPayload);
|
||||
sendPeriodically();
|
||||
} else {
|
||||
reject(new ProviderError('error', 'Request timed out'));
|
||||
if (numberOfTimesMessageWasSent < 5) {
|
||||
numberOfTimesMessageWasSent += 1;
|
||||
ws.send(stringifiedPayload, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
}
|
||||
}
|
||||
}, 50000);
|
||||
});
|
||||
} else {
|
||||
reject(new ProviderError('error', 'Request timed out'));
|
||||
}
|
||||
};
|
||||
const interval = setInterval(sendPeriodically, 30000);
|
||||
|
||||
const onResponse = (raw: RawData) => {
|
||||
responseWasReceived = true;
|
||||
numberOfTimesMessageWasSent = 0;
|
||||
const response: server.Message<U> = JSON.parse(raw.toString());
|
||||
if (response.uuid === uuid) {
|
||||
clearInterval(interval);
|
||||
if (response.error) {
|
||||
return reject(new ProviderError('error', response.error.message));
|
||||
}
|
||||
|
||||
resolve(response.data ?? null);
|
||||
} else {
|
||||
ws.once('message', onResponse);
|
||||
@ -79,7 +75,6 @@ export const createDispatcher = (ws: WebSocket) => {
|
||||
};
|
||||
|
||||
ws.once('message', onResponse);
|
||||
sendPeriodically();
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -8,6 +8,11 @@ type BufferLike = Parameters<WebSocket['send']>[0];
|
||||
export interface TransferState {
|
||||
id?: string;
|
||||
startedAt?: number;
|
||||
response?: {
|
||||
uuid?: string;
|
||||
e?: Error | null;
|
||||
data?: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
export interface Handler {
|
||||
@ -19,11 +24,8 @@ export interface Handler {
|
||||
get startedAt(): TransferState['startedAt'];
|
||||
set startedAt(id: TransferState['startedAt']);
|
||||
|
||||
// Add message UUIDs
|
||||
addUUID(uuid: string): void;
|
||||
|
||||
// Check if a message UUID exists
|
||||
hasUUID(uuid: string): boolean;
|
||||
get response(): TransferState['response'];
|
||||
set response(response: TransferState['response']);
|
||||
|
||||
/**
|
||||
* Returns whether a transfer is currently in progress or not
|
||||
|
@ -70,6 +70,12 @@ export const createPullController = handlerControllerFactory<Partial<PullHandler
|
||||
await this.respond(undefined, new Error('Missing uuid in message'));
|
||||
}
|
||||
|
||||
const previousResponse = proto.response;
|
||||
if (previousResponse?.uuid === msg.uuid) {
|
||||
await this.respond(previousResponse?.uuid, previousResponse.e, previousResponse.data);
|
||||
return;
|
||||
}
|
||||
|
||||
const { uuid, type } = msg;
|
||||
|
||||
// Regular command message (init, end, status)
|
||||
|
@ -205,13 +205,13 @@ export const createPushController = handlerControllerFactory<Partial<PushHandler
|
||||
await this.respond(undefined, new Error('Missing uuid in message'));
|
||||
}
|
||||
|
||||
if (proto.hasUUID(msg.uuid)) {
|
||||
await this.respond(msg.uuid);
|
||||
const previousResponse = proto.response;
|
||||
if (previousResponse?.uuid === msg.uuid) {
|
||||
await this.respond(previousResponse?.uuid, previousResponse.e, previousResponse.data);
|
||||
return;
|
||||
}
|
||||
|
||||
const { uuid, type } = msg;
|
||||
proto.addUUID(uuid);
|
||||
// Regular command message (init, end, status)
|
||||
if (type === 'command') {
|
||||
const { command } = msg;
|
||||
|
@ -79,7 +79,6 @@ export const handlerControllerFactory =
|
||||
return async (ctx: Context) => {
|
||||
handleWSUpgrade(wss, ctx, (ws) => {
|
||||
const state: TransferState = { id: undefined };
|
||||
const messageuuids = new Set<string>();
|
||||
|
||||
const prototype: Handler = {
|
||||
// Transfer ID
|
||||
@ -100,12 +99,12 @@ export const handlerControllerFactory =
|
||||
state.startedAt = timestamp;
|
||||
},
|
||||
|
||||
addUUID(uuid) {
|
||||
messageuuids.add(uuid);
|
||||
get response() {
|
||||
return state.response;
|
||||
},
|
||||
|
||||
hasUUID(uuid) {
|
||||
return messageuuids.has(uuid);
|
||||
set response(response) {
|
||||
state.response = response;
|
||||
},
|
||||
|
||||
isTransferStarted() {
|
||||
@ -135,7 +134,11 @@ export const handlerControllerFactory =
|
||||
reject(new Error('Missing uuid for this message'));
|
||||
return;
|
||||
}
|
||||
|
||||
this.response = {
|
||||
uuid,
|
||||
data,
|
||||
e,
|
||||
};
|
||||
const payload = JSON.stringify({
|
||||
uuid,
|
||||
data: data ?? null,
|
||||
@ -208,6 +211,7 @@ export const handlerControllerFactory =
|
||||
cleanup() {
|
||||
this.transferID = undefined;
|
||||
this.startedAt = undefined;
|
||||
this.response = undefined;
|
||||
},
|
||||
|
||||
teardown() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user