The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(server): propagate stream destroy to the socket behind the SDK checksum wrapper (#25207)
Thomas Trompetted0f1f75d163e
diff --git a/packages/twenty-server/package.json b/packages/twenty-server/package.jsonindex e56f3a78722..0d164e49626 100644--- a/packages/twenty-server/package.json+++ b/packages/twenty-server/package.json@@ -179,6 +179,7 @@"@nestjs/cli": "^11.0.16","@nestjs/schematics": "^11.0.9","@nestjs/testing": "11.2.1",+ "@smithy/util-stream": "4.5.16","@swc/cli": "^0.8.1","@swc/core": "^1.15.11","@swc/jest": "^0.2.39",diff --git a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.ts b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.tsindex 848ceea62a9..38cc5f86374 100644--- a/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.ts+++ b/packages/twenty-server/src/engine/core-modules/file-storage/drivers/s3.driver.ts@@ -36,6 +36,7 @@ import {} from 'src/engine/core-modules/file-storage/interfaces/file-storage-exception';import { type ByteRange } from 'src/engine/core-modules/file-storage/types/byte-range.type';import { buildAwsRequestHandlerOptions } from 'src/utils/aws-request-handler.util';+import { propagateDestroyToSource } from 'src/utils/propagate-destroy-to-source.util';export interface S3DriverOptions extends S3ClientConfig {bucketName: string;@@ -109,7 +110,7 @@ export class S3Driver implements StorageDriver {throw new Error('Unable to get file stream');}- return file.Body;+ return propagateDestroyToSource(file.Body);} catch (error) {if (error.name === 'NoSuchKey') {throw new FileStorageException(diff --git a/packages/twenty-server/src/utils/__tests__/propagate-destroy-to-source.util.spec.ts b/packages/twenty-server/src/utils/__tests__/propagate-destroy-to-source.util.spec.tsnew file mode 100644index 00000000000..cc7c9fbe33d--- /dev/null+++ b/packages/twenty-server/src/utils/__tests__/propagate-destroy-to-source.util.spec.ts@@ -0,0 +1,83 @@+import { once } from 'events';+import { PassThrough, Readable } from 'stream';++import { ChecksumStream } from '@smithy/util-stream';++import { propagateDestroyToSource } from 'src/utils/propagate-destroy-to-source.util';++jest.useRealTimers();++const onceWithin = (stream: Readable, event: string) =>+ Promise.race([+ once(stream, event),+ new Promise((_resolve, reject) =>+ setTimeout(+ () => reject(new Error(`${event} not emitted within 1s`)),+ 1000,+ ).unref(),+ ),+ ]);++describe('propagateDestroyToSource', () => {+ it('returns the same stream instance', () => {+ const stream = new PassThrough();++ expect(propagateDestroyToSource(stream)).toBe(stream);+ });++ it('destroys the wrapped source when the wrapper is destroyed unread', async () => {+ const source = new PassThrough();+ const wrapper = new PassThrough() as PassThrough & { source: Readable };++ wrapper.source = source;++ propagateDestroyToSource(wrapper).destroy();+ await onceWithin(source, 'close');++ expect(source.destroyed).toBe(true);+ });++ it('leaves a fully consumed source alone so its socket stays reusable', async () => {+ const source = new PassThrough({ autoDestroy: false });+ const wrapper = new PassThrough() as PassThrough & { source: Readable };++ wrapper.source = source;+ source.resume();+ source.end();+ await onceWithin(source, 'end');++ propagateDestroyToSource(wrapper).destroy();+ await onceWithin(wrapper, 'close');++ expect(source.readableEnded).toBe(true);+ expect(source.destroyed).toBe(false);+ });++ it('tolerates a stream with no source', async () => {+ const stream = new PassThrough();++ propagateDestroyToSource(stream).destroy();+ await onceWithin(stream, 'close');++ expect(stream.destroyed).toBe(true);+ });++ it('reaches the response stream behind a real SDK ChecksumStream', async () => {+ const source = new PassThrough();+ const wrapper = new ChecksumStream({+ source,+ checksum: {+ update: () => {},+ digest: async () => new Uint8Array(),+ reset: () => {},+ },+ checksumSourceLocation: 'x-amz-checksum-crc32',+ expectedChecksum: 'AA==',+ });++ propagateDestroyToSource(wrapper).destroy();+ await onceWithin(source, 'close');++ expect(source.destroyed).toBe(true);+ });+});diff --git a/packages/twenty-server/src/utils/propagate-destroy-to-source.util.ts b/packages/twenty-server/src/utils/propagate-destroy-to-source.util.tsnew file mode 100644index 00000000000..4cb53e95204--- /dev/null+++ b/packages/twenty-server/src/utils/propagate-destroy-to-source.util.ts@@ -0,0 +1,20 @@+import { type Readable } from 'stream';++import { isDefined } from 'twenty-shared/utils';++type ReadableWithSource = Readable & { source?: Readable };++export const propagateDestroyToSource = <TStream extends Readable>(+ stream: TStream,+): TStream => {+ stream.once('close', () => {+ const source = (stream as ReadableWithSource).source;++ if (isDefined(source) && !source.destroyed && !source.readableEnded) {+ source.on('error', () => {});+ source.destroy();+ }+ });++ return stream;+};diff --git a/yarn.lock b/yarn.lockindex c878e86612c..050d37ffc85 100644--- a/yarn.lock+++ b/yarn.lock@@ -18262,17 +18262,7 @@ __metadata:languageName: nodelinkType: hard-"@smithy/util-stream@npm:^4.5.10":- version: 4.6.3- resolution: "@smithy/util-stream@npm:4.6.3"- dependencies:- "@smithy/core": "npm:^3.24.3"- tslib: "npm:^2.6.2"- checksum: 10c0/7a0ab21330e6bc8a8ff85747c860fd917df4696735e19a858fa00550068af3f0b9705c429e46db56e8ec14b80f741f4efb64a178980d8f6312985920902c402d- languageName: node- linkType: hard--"@smithy/util-stream@npm:^4.5.16":+"@smithy/util-stream@npm:4.5.16, @smithy/util-stream@npm:^4.5.16":version: 4.5.16resolution: "@smithy/util-stream@npm:4.5.16"dependencies:@@ -18288,6 +18278,16 @@ __metadata:languageName: nodelinkType: hard+"@smithy/util-stream@npm:^4.5.10":+ version: 4.6.3+ resolution: "@smithy/util-stream@npm:4.6.3"+ dependencies:+ "@smithy/core": "npm:^3.24.3"+ tslib: "npm:^2.6.2"+ checksum: 10c0/7a0ab21330e6bc8a8ff85747c860fd917df4696735e19a858fa00550068af3f0b9705c429e46db56e8ec14b80f741f4efb64a178980d8f6312985920902c402d+ languageName: node+ linkType: hard+"@smithy/util-uri-escape@npm:^4.2.1":version: 4.2.1resolution: "@smithy/util-uri-escape@npm:4.2.1"@@ -49804,6 +49804,7 @@ __metadata:"@sentry/nestjs": "npm:^10.59.0""@sentry/node": "npm:^10.59.0""@sentry/profiling-node": "npm:^10.59.0"+ "@smithy/util-stream": "npm:4.5.16""@sniptt/guards": "npm:0.2.0""@swc/cli": "npm:^0.8.1""@swc/core": "npm:^1.15.11"