mirror of
https://github.com/strapi/strapi.git
synced 2025-07-23 09:00:19 +00:00
fix: Data transfer stream backpressure from providers
This commit is contained in:
parent
9e14912c58
commit
c76dc9cb2b
@ -383,44 +383,37 @@ export const createPushController = handlerControllerFactory<Partial<PushHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const item of payload) {
|
for (const item of payload) {
|
||||||
/**
|
const { action, assetID } = item;
|
||||||
* This queues the given callback function to be executed in the next iteration
|
|
||||||
* of the event loop, immediately after the current operation completes.
|
|
||||||
*/
|
|
||||||
process.nextTick(async () => {
|
|
||||||
const { action, assetID } = item;
|
|
||||||
|
|
||||||
if (!assetsStream) {
|
if (!assetsStream) {
|
||||||
throw new Error('Stream not defined');
|
throw new Error('Stream not defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action === 'start') {
|
if (action === 'start') {
|
||||||
this.assets[assetID] = { ...item.data, stream: new PassThrough() };
|
this.assets[assetID] = { ...item.data, stream: new PassThrough() };
|
||||||
writeAsync(assetsStream, this.assets[assetID]);
|
writeAsync(assetsStream, this.assets[assetID]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action === 'stream') {
|
if (action === 'stream') {
|
||||||
// The buffer has gone through JSON operations and is now of shape { type: "Buffer"; data: UInt8Array }
|
// The buffer has gone through JSON operations and is now of shape { type: "Buffer"; data: UInt8Array }
|
||||||
// We need to transform it back into a Buffer instance
|
// We need to transform it back into a Buffer instance
|
||||||
const rawBuffer = item.data as unknown as { type: 'Buffer'; data: Uint8Array };
|
const rawBuffer = item.data as unknown as { type: 'Buffer'; data: Uint8Array };
|
||||||
const chunk = Buffer.from(rawBuffer.data);
|
const chunk = Buffer.from(rawBuffer.data);
|
||||||
|
await writeAsync(this.assets[assetID].stream, chunk);
|
||||||
|
}
|
||||||
|
|
||||||
await writeAsync(this.assets[assetID].stream, chunk);
|
if (action === 'end') {
|
||||||
}
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
const { stream: assetStream } = this.assets[assetID];
|
||||||
if (action === 'end') {
|
assetStream
|
||||||
await new Promise<void>((resolve, reject) => {
|
.on('close', () => {
|
||||||
const { stream: assetStream } = this.assets[assetID];
|
delete this.assets[assetID];
|
||||||
assetStream
|
resolve();
|
||||||
.on('close', () => {
|
})
|
||||||
delete this.assets[assetID];
|
.on('error', reject)
|
||||||
resolve();
|
.end();
|
||||||
})
|
});
|
||||||
.on('error', reject)
|
}
|
||||||
.end();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user