@mastra/server-adapters-test-suite 0.2.2-alpha.0 → 0.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @mastra/server-adapters-test-suite
2
2
 
3
+ ## 0.3.0-alpha.1
4
+
5
+ ### Minor Changes
6
+
7
+ - Added `createStreamDisconnectTestSuite`, which checks over real HTTP that an adapter stops reading a route's stream when the client disconnects and pauses reading while the client is not reading. ([#29669](https://github.com/mastra-ai/mastra/pull/29669))
8
+
9
+ ```ts
10
+ import { createStreamDisconnectTestSuite } from '@mastra/server-adapters-test-suite';
11
+
12
+ createStreamDisconnectTestSuite({
13
+ // Serve `route` with your adapter and return its URL. Run `beforeContext` before the adapter's context middleware.
14
+ startServer: async (route, { beforeContext } = {}) => {
15
+ const { url, close } = await startMyAdapterServer(route, beforeContext);
16
+ return { url, close };
17
+ },
18
+ });
19
+ ```
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies [[`58a66b0`](https://github.com/mastra-ai/mastra/commit/58a66b01d9b08e7b59c27fc3ab68eb90be13c0ed), [`ac2a640`](https://github.com/mastra-ai/mastra/commit/ac2a6401a09e2177555369cff2f7ad15f2cc7286), [`94b1641`](https://github.com/mastra-ai/mastra/commit/94b1641919dd90455131b16cd0572ce28008781b), [`8279c30`](https://github.com/mastra-ai/mastra/commit/8279c3073737dd1c228b5df15416ca4ce7e0c00c), [`bc9d8d4`](https://github.com/mastra-ai/mastra/commit/bc9d8d482cec626ef2107f1cbd5a05febceaa491), [`8a10dfe`](https://github.com/mastra-ai/mastra/commit/8a10dfe648610a6687d1e4ecc10a0a81e3d80c4c), [`d77893a`](https://github.com/mastra-ai/mastra/commit/d77893aed38eefe2dbac21e9fffd872137ee6b92), [`791cf60`](https://github.com/mastra-ai/mastra/commit/791cf606e91badea097b883949dc717689caa49d), [`b31cbe2`](https://github.com/mastra-ai/mastra/commit/b31cbe2888f890383629d20c853e21f870dee5a6), [`bfedbfe`](https://github.com/mastra-ai/mastra/commit/bfedbfe5a8e5721cba782efb4652be84e9fecc04), [`c9140d2`](https://github.com/mastra-ai/mastra/commit/c9140d2d4d16dfd674f163cd0a83e28593969c04), [`a836d09`](https://github.com/mastra-ai/mastra/commit/a836d09a43c3c471b5bfced711731355bf26db26), [`4854e36`](https://github.com/mastra-ai/mastra/commit/4854e3641931c984a5b3941371a4f0fa58eafd0e), [`7358ad7`](https://github.com/mastra-ai/mastra/commit/7358ad73a381765572858d520f8170ab59e88514), [`efa3999`](https://github.com/mastra-ai/mastra/commit/efa3999a21fea5bfaa455325f9a548b3daf358e9), [`b93d804`](https://github.com/mastra-ai/mastra/commit/b93d804a7a406d834389e686687b06d22ea2e4f1), [`ed1cecb`](https://github.com/mastra-ai/mastra/commit/ed1cecba5c26a73600640f74e9a1c9d322271b23), [`4cd4c46`](https://github.com/mastra-ai/mastra/commit/4cd4c46e546d44c556fce4d731cac03cad4f88c4), [`9e5b108`](https://github.com/mastra-ai/mastra/commit/9e5b1082cbe2454c377b3328cd8a0085e7452d1a), [`b31cbe2`](https://github.com/mastra-ai/mastra/commit/b31cbe2888f890383629d20c853e21f870dee5a6), [`28ddc16`](https://github.com/mastra-ai/mastra/commit/28ddc161a441fdc3ecbdcc1168dee773f1f25243), [`0f496bd`](https://github.com/mastra-ai/mastra/commit/0f496bd3c1cc7f07d2e4ecb3005d480406b3d342), [`de005a4`](https://github.com/mastra-ai/mastra/commit/de005a43d8703d36c5dd25d7ddd1f618ea98bf55)]:
24
+ - @mastra/core@1.83.0-alpha.3
25
+ - @mastra/server@1.83.0-alpha.3
26
+ - @mastra/mcp@2.9.0-alpha.1
27
+
3
28
  ## 0.2.2-alpha.0
4
29
 
5
30
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { createRouteAdapterTestSuite } from './route-adapter-test-suite.js';
2
2
  export { createStructuredWakeTestSuite, type StructuredWakeTestConfig } from './structured-wake-test-suite.js';
3
3
  export { createDurableDisconnectTestSuite, type DurableDisconnectTestConfig } from './durable-disconnect-test-suite.js';
4
+ export { createStreamDisconnectTestSuite, type StreamDisconnectTestConfig } from './stream-disconnect-test-suite.js';
4
5
  export type { AdapterTestSuiteConfig, AdapterTestContext, AdapterSetupOptions, HttpRequest, HttpResponse, } from './test-helpers.js';
5
6
  export { createMCPRouteTestSuite } from './mcp-route-test-suite.js';
6
7
  export { createMCPTransportTestSuite, type MCPTransportTestConfig } from './mcp-transport-test-suite.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAE,6BAA6B,EAAE,KAAK,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC5G,OAAO,EAAE,gCAAgC,EAAE,KAAK,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AACrH,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,WAAW,EACX,YAAY,GACb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,2BAA2B,EAAE,KAAK,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACtG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACjG,OAAO,EAAE,0BAA0B,EAAE,KAAK,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACxG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAClG,OAAO,EAAE,6BAA6B,EAAE,KAAK,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAElH,OAAO,EACL,wBAAwB,EACxB,6BAA6B,EAC7B,mCAAmC,EACnC,4BAA4B,EAC5B,g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EpH,OAAO,EACL,uCAAuC,EACvC,KAAK,kCAAkC,GACxC,MAAM,yCAAyC,CAAC"}
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import * as zod from "zod";
7
7
  import { z } from "zod";
8
8
  import { z as z$1 } from "zod/v3";
9
9
  import { getZodDef, getZodTypeName, makeCoreTool } from "@mastra/core/utils";
10
- import { randomUUID } from "crypto";
10
+ import { randomBytes, randomUUID } from "crypto";
11
11
  import * as fs from "fs";
12
12
  import * as os from "os";
13
13
  import * as path from "path";
@@ -4331,6 +4331,199 @@ function createDurableDisconnectTestSuite(config) {
4331
4331
  }
4332
4332
  });
4333
4333
  }
4334
+ // src/stream-disconnect-test-suite.ts
4335
+ const incompressible = (bytes) => randomBytes(Math.ceil(bytes * 3 / 4)).toString("base64").slice(0, bytes);
4336
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
4337
+ /**
4338
+ * Covers what happens to the stream a route returns when the client goes away (#29634, #29640). The adapter has
4339
+ * to cancel the upstream stream once the client is gone; for agents with resumable streams that cancel is the only
4340
+ * signal that the HTTP observer left, and without it the upstream keeps being read into a dead socket.
4341
+ */
4342
+ function createStreamDisconnectTestSuite(config) {
4343
+ describe("streaming client disconnect", () => {
4344
+ let close;
4345
+ afterEach(async () => {
4346
+ await close?.();
4347
+ close = void 0;
4348
+ });
4349
+ async function setup({ responseType, chunks = 60, chunkBytes = 16, paceMs = 10, holdUntilDisconnect = false, compress = false }) {
4350
+ const state = {
4351
+ pulls: 0,
4352
+ canceled: false
4353
+ };
4354
+ let handlerEntered;
4355
+ const handlerStarted = new Promise((resolve) => handlerEntered = resolve);
4356
+ let handlerReturned;
4357
+ const handlerDone = new Promise((resolve) => handlerReturned = resolve);
4358
+ const padding = compress ? incompressible(chunkBytes) : "x".repeat(chunkBytes);
4359
+ const route = {
4360
+ method: "POST",
4361
+ path: "/test/disconnect",
4362
+ responseType,
4363
+ handler: async ({ abortSignal }) => {
4364
+ handlerEntered();
4365
+ if (holdUntilDisconnect) await Promise.race([new Promise((resolve) => {
4366
+ if (abortSignal?.aborted) resolve();
4367
+ abortSignal?.addEventListener("abort", () => resolve(), { once: true });
4368
+ }), sleep(2e3)]);
4369
+ handlerReturned();
4370
+ const body = new ReadableStream({
4371
+ async pull(controller) {
4372
+ if (state.pulls >= chunks) return controller.close();
4373
+ state.pulls++;
4374
+ if (paceMs) await sleep(paceMs);
4375
+ controller.enqueue(responseType === "stream" ? {
4376
+ type: "chunk",
4377
+ n: state.pulls,
4378
+ padding
4379
+ } : new TextEncoder().encode(`chunk-${state.pulls} ${padding}\n`));
4380
+ },
4381
+ cancel() {
4382
+ state.canceled = true;
4383
+ }
4384
+ });
4385
+ return responseType === "stream" ? body : new Response(body, {
4386
+ status: 200,
4387
+ headers: { "Content-Type": "text/plain" }
4388
+ });
4389
+ }
4390
+ };
4391
+ const server = await config.startServer(route, { compress });
4392
+ close = server.close;
4393
+ return {
4394
+ url: server.url,
4395
+ state,
4396
+ handlerStarted,
4397
+ handlerDone
4398
+ };
4399
+ }
4400
+ const post = (url, signal) => fetch(url, {
4401
+ method: "POST",
4402
+ headers: { "content-type": "application/json" },
4403
+ body: "{}",
4404
+ signal
4405
+ });
4406
+ it("aborts the route abort signal when the client left while earlier middleware was running", async () => {
4407
+ let hookEntered;
4408
+ const inHook = new Promise((resolve) => hookEntered = resolve);
4409
+ let releaseHook;
4410
+ const hookReleased = new Promise((resolve) => releaseHook = resolve);
4411
+ let routeRan;
4412
+ const routeResult = new Promise((resolve) => routeRan = resolve);
4413
+ const server = await config.startServer({
4414
+ method: "GET",
4415
+ path: "/test/early-close",
4416
+ responseType: "json",
4417
+ handler: async ({ abortSignal }) => {
4418
+ routeRan(abortSignal?.aborted);
4419
+ return {};
4420
+ }
4421
+ }, { beforeContext: async () => {
4422
+ hookEntered();
4423
+ await hookReleased;
4424
+ } });
4425
+ close = server.close;
4426
+ const client = new AbortController();
4427
+ const request = fetch(server.url, { signal: client.signal }).catch(() => void 0);
4428
+ await inHook;
4429
+ client.abort();
4430
+ await request;
4431
+ await sleep(100);
4432
+ releaseHook();
4433
+ const aborted = await Promise.race([routeResult, sleep(2e3).then(() => "route never ran")]);
4434
+ expect(aborted, "route abortSignal.aborted").toBe(true);
4435
+ });
4436
+ describe.each(["stream", "datastream-response"])("%s route", (responseType) => {
4437
+ it("stops reading the upstream when the client aborts mid-body", async () => {
4438
+ const { url, state } = await setup({ responseType });
4439
+ const client = new AbortController();
4440
+ await (await post(url, client.signal)).body.getReader().read();
4441
+ const pullsAtAbort = state.pulls;
4442
+ client.abort();
4443
+ await sleep(300);
4444
+ expect(state.pulls - pullsAtAbort, "upstream pulls after client abort").toBeLessThanOrEqual(2);
4445
+ expect(state.canceled, "upstream cancel()").toBe(true);
4446
+ });
4447
+ it("stops reading the upstream when the client aborts before the handler returns", async () => {
4448
+ const { url, state, handlerStarted, handlerDone } = await setup({
4449
+ responseType,
4450
+ holdUntilDisconnect: true
4451
+ });
4452
+ const client = new AbortController();
4453
+ const request = post(url, client.signal).catch(() => void 0);
4454
+ await handlerStarted;
4455
+ client.abort();
4456
+ await request;
4457
+ await handlerDone;
4458
+ await sleep(300);
4459
+ expect(state.pulls, "upstream pulls after client abort").toBeLessThanOrEqual(2);
4460
+ expect(state.canceled, "upstream cancel()").toBe(true);
4461
+ });
4462
+ it("delivers the whole body without cancelling the upstream when the client reads to the end", async () => {
4463
+ const { url, state } = await setup({
4464
+ responseType,
4465
+ chunks: 20
4466
+ });
4467
+ const text = await (await post(url)).text();
4468
+ expect(text.match(/"n":\d+|chunk-\d+/g)).toHaveLength(20);
4469
+ expect(state.canceled, "upstream cancel()").toBe(false);
4470
+ });
4471
+ it("pauses reading the upstream while a connected client is not reading, and cancels it once the client leaves", async () => {
4472
+ const { url, state } = await setup({
4473
+ responseType,
4474
+ chunks: 400,
4475
+ chunkBytes: 65536,
4476
+ paceMs: 0
4477
+ });
4478
+ const client = new AbortController();
4479
+ const response = await post(url, client.signal);
4480
+ await sleep(500);
4481
+ const pullsWhilePaused = state.pulls;
4482
+ client.abort();
4483
+ await response.body?.cancel().catch(() => {});
4484
+ expect(pullsWhilePaused, "upstream pulls while the client was not reading").toBeLessThan(400);
4485
+ await sleep(300);
4486
+ expect(state.canceled, "upstream cancel()").toBe(true);
4487
+ });
4488
+ it.runIf(config.supportsCompression)("streams a compressed response to a slow client without piling up drain listeners", async () => {
4489
+ const warnings = [];
4490
+ const onWarning = (warning) => {
4491
+ if (warning.name === "MaxListenersExceededWarning") warnings.push(warning.message);
4492
+ };
4493
+ process.on("warning", onWarning);
4494
+ try {
4495
+ const { url } = await setup({
4496
+ responseType,
4497
+ chunks: 100,
4498
+ chunkBytes: 65536,
4499
+ paceMs: 0,
4500
+ compress: true
4501
+ });
4502
+ const response = await fetch(url, {
4503
+ method: "POST",
4504
+ headers: {
4505
+ "content-type": "application/json",
4506
+ "accept-encoding": "gzip"
4507
+ },
4508
+ body: "{}"
4509
+ });
4510
+ expect(response.headers.get("content-encoding")).toBe("gzip");
4511
+ const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
4512
+ let text = "";
4513
+ for (let next = await reader.read(); !next.done; next = await reader.read()) {
4514
+ text += next.value;
4515
+ await sleep(1);
4516
+ }
4517
+ await sleep(50);
4518
+ expect(text.match(/"n":\d+|chunk-\d+/g)).toHaveLength(100);
4519
+ expect(warnings, "MaxListenersExceededWarning").toEqual([]);
4520
+ } finally {
4521
+ process.off("warning", onWarning);
4522
+ }
4523
+ });
4524
+ });
4525
+ });
4526
+ }
4334
4527
  // src/mcp-route-test-suite.ts
4335
4528
  /**
4336
4529
  * Creates a standardized integration test suite for MCP registry routes
@@ -6735,6 +6928,6 @@ function createObservabilitySignalWriteTestSuite({ startServer }) {
6735
6928
  });
6736
6929
  });
6737
6930
  }
6738
- export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
6931
+ export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamDisconnectTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
6739
6932
 
6740
6933
  //# sourceMappingURL=index.js.map