🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(shared): propagate GCS source-stream errors through pipeline() (#16995)
Niklas Semmler85e3b6a11de8
diff --git a/packages/shared/src/server/services/StorageService.test.ts b/packages/shared/src/server/services/StorageService.test.tsindex a1b8a313db..c7f838544e 100644--- a/packages/shared/src/server/services/StorageService.test.ts+++ b/packages/shared/src/server/services/StorageService.test.ts@@ -1,5 +1,5 @@import { createHash } from "crypto";-import { Readable } from "stream";+import { PassThrough, Readable } from "stream";import { afterEach, describe, expect, it, vi } from "vitest";@@ -325,6 +325,79 @@ describe("GoogleCloudStorageService signed-URL retry", () => {});});+/**+ * Source-stream errors on GCS uploads must reject the awaited upload instead+ * of escaping as an uncaught 'error' event. `.pipe().on("error")` only+ * listens on the destination; `pipeline()` forwards source errors too.+ */+describe("GoogleCloudStorageService.uploadFile source stream errors", () => {+ it("rejects when the source stream fails without leaking process events", async () => {+ const service = StorageServiceFactory.getInstance({+ accessKeyId: undefined,+ secretAccessKey: undefined,+ bucketName: "test-bucket",+ endpoint: undefined,+ region: undefined,+ forcePathStyle: false,+ useGoogleCloudStorage: true,+ awsSse: undefined,+ awsSseKmsKeyId: undefined,+ });++ (+ service as unknown as { bucket: { file: (name: string) => unknown } }+ ).bucket = {+ file: () => ({+ createWriteStream: () => new PassThrough(),+ }),+ };++ const unhandled: unknown[] = [];+ const uncaught: unknown[] = [];+ const onUnhandled = (reason: unknown) => {+ unhandled.push(reason);+ };+ const onUncaught = (err: unknown) => {+ uncaught.push(err);+ };+ process.on("unhandledRejection", onUnhandled);+ process.on("uncaughtException", onUncaught);++ try {+ const source = new Readable({+ read() {+ this.destroy(new Error("source stream failed"));+ },+ });++ await expect(+ (+ service as unknown as {+ uploadFile(params: {+ fileName: string;+ fileType: string;+ data: Readable;+ }): Promise<void>;+ }+ ).uploadFile({+ fileName: "export.json",+ fileType: "application/json",+ data: source,+ }),+ ).rejects.toThrow();++ await new Promise<void>((resolve) => {+ setImmediate(resolve);+ });+ expect(unhandled).toHaveLength(0);+ expect(uncaught).toHaveLength(0);+ } finally {+ process.off("unhandledRejection", onUnhandled);+ process.off("uncaughtException", onUncaught);+ }+ });+});+/*** Regression tests for the DeleteObjects checksum sent to S3-compatible* stores.diff --git a/packages/shared/src/server/services/StorageService.ts b/packages/shared/src/server/services/StorageService.tsindex cd3da4d6c6..7f73629977 100644--- a/packages/shared/src/server/services/StorageService.ts+++ b/packages/shared/src/server/services/StorageService.ts@@ -1,4 +1,5 @@import { Readable } from "stream";+import { pipeline } from "stream/promises";import {DeleteObjectsCommand,GetObjectCommand,@@ -1136,19 +1137,8 @@ class GoogleCloudStorageService implements StorageService {await file.save(data, options);return;} else if (data instanceof Readable) {- return new Promise((resolve, reject) => {- const writeStream = file.createWriteStream(options);-- data- .pipe(writeStream)- .on("error", (err: unknown) => {- reject(err);- })- .on("finish", () => {- resolve();- });- return;- });+ await pipeline(data, file.createWriteStream(options));+ return;}throw new Error("Unsupported data type. Must be Readable or string.");