|
1 | 1 | import { ApiClient } from "@trigger.dev/core/v3"; |
2 | | -import { describe, it, expect } from "vitest"; |
| 2 | +import { createServer, type Server } from "node:http"; |
| 3 | +import type { AddressInfo } from "node:net"; |
| 4 | +import { afterEach, describe, it, expect } from "vitest"; |
3 | 5 | import { |
4 | 6 | offloadBatchItemPayloads, |
5 | 7 | readableStreamToAsyncIterable, |
| 8 | + trigger, |
6 | 9 | uniqueBatchTaskIdentifiers, |
7 | 10 | } from "./shared.js"; |
8 | 11 |
|
@@ -69,6 +72,126 @@ describe("offloadBatchItemPayloads", () => { |
69 | 72 | }); |
70 | 73 | }); |
71 | 74 |
|
| 75 | +describe("offloaded trigger payload paths", () => { |
| 76 | + let server: Server | undefined; |
| 77 | + |
| 78 | + afterEach(async () => { |
| 79 | + const running = server; |
| 80 | + server = undefined; |
| 81 | + if (running) { |
| 82 | + running.closeAllConnections?.(); |
| 83 | + await new Promise<void>((resolve) => running.close(() => resolve())); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + /** |
| 88 | + * Stand in for the presign route and the object store, recording the packet path |
| 89 | + * the SDK actually puts on the wire. |
| 90 | + */ |
| 91 | + const startPacketServer = async (): Promise<{ origin: string; requestedPaths: string[] }> => { |
| 92 | + const requestedPaths: string[] = []; |
| 93 | + let origin = ""; |
| 94 | + |
| 95 | + server = createServer((request, response) => { |
| 96 | + const url = new URL(request.url ?? "/", origin); |
| 97 | + |
| 98 | + if (url.pathname.startsWith("/api/v2/packets/")) { |
| 99 | + const encoded = url.pathname.slice("/api/v2/packets/".length); |
| 100 | + const storagePath = decodeURIComponent(encoded); |
| 101 | + requestedPaths.push(storagePath); |
| 102 | + |
| 103 | + response.writeHead(200, { "Content-Type": "application/json" }); |
| 104 | + response.end(JSON.stringify({ presignedUrl: `${origin}/upload`, storagePath })); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + request.resume(); |
| 109 | + request.on("end", () => { |
| 110 | + response.writeHead(200, { "Content-Type": "application/json" }); |
| 111 | + response.end(JSON.stringify({ id: "run_test" })); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + await new Promise<void>((resolve) => server!.listen(0, "127.0.0.1", resolve)); |
| 116 | + origin = `http://127.0.0.1:${(server!.address() as AddressInfo).port}`; |
| 117 | + |
| 118 | + return { origin, requestedPaths }; |
| 119 | + }; |
| 120 | + |
| 121 | + const NASTY_TASK_IDS = [ |
| 122 | + "/my-task", |
| 123 | + "jobs/my-task", |
| 124 | + "my-task/", |
| 125 | + "a//b", |
| 126 | + "jobs/../x", |
| 127 | + "..", |
| 128 | + "my task", |
| 129 | + "caf\u00e9", |
| 130 | + "\ud800", |
| 131 | + ]; |
| 132 | + |
| 133 | + it("builds a path from generated ids only, whatever the task id is", async () => { |
| 134 | + const { origin, requestedPaths } = await startPacketServer(); |
| 135 | + const apiClient = new ApiClient(origin, "tr_dev_test"); |
| 136 | + const payload = JSON.stringify({ blob: "x".repeat(200_000) }); |
| 137 | + |
| 138 | + const items = NASTY_TASK_IDS.map((task, index) => ({ |
| 139 | + index, |
| 140 | + task, |
| 141 | + payload, |
| 142 | + options: { payloadType: "application/json" }, |
| 143 | + })); |
| 144 | + |
| 145 | + const result = await offloadBatchItemPayloads(items, apiClient); |
| 146 | + |
| 147 | + expect(requestedPaths).toHaveLength(NASTY_TASK_IDS.length); |
| 148 | + |
| 149 | + for (const path of requestedPaths) { |
| 150 | + expect(path).toMatch( |
| 151 | + /^trigger\/packet_[123456789abcdefghijkmnopqrstuvwxyz]{21}\/payload\.json$/ |
| 152 | + ); |
| 153 | + expect(path).not.toContain("%"); |
| 154 | + } |
| 155 | + |
| 156 | + expect(new Set(requestedPaths).size).toBe(NASTY_TASK_IDS.length); |
| 157 | + expect(new Set(result.map((item) => item.payload))).toEqual(new Set(requestedPaths)); |
| 158 | + |
| 159 | + for (const [index, item] of result.entries()) { |
| 160 | + expect(item.options?.payloadType).toBe("application/store"); |
| 161 | + expect(item.task).toBe(NASTY_TASK_IDS[index]); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + /** |
| 166 | + * A lone surrogate is excluded here only because the trigger endpoint's own URL |
| 167 | + * builder throws on it. That happens after the payload has been offloaded, so the |
| 168 | + * offload path handles the id fine, as the batch case above shows; the uploaded |
| 169 | + * object is simply orphaned when the trigger call fails. |
| 170 | + */ |
| 171 | + const TRIGGERABLE_NASTY_TASK_IDS = NASTY_TASK_IDS.filter((taskId) => taskId !== "\ud800"); |
| 172 | + |
| 173 | + it.each(TRIGGERABLE_NASTY_TASK_IDS)( |
| 174 | + "keeps the task id out of the path for trigger() of %j", |
| 175 | + async (taskId) => { |
| 176 | + const { origin, requestedPaths } = await startPacketServer(); |
| 177 | + |
| 178 | + const handle = await trigger( |
| 179 | + taskId as never, |
| 180 | + { blob: "x".repeat(200_000) } as never, |
| 181 | + undefined, |
| 182 | + { clientConfig: { baseURL: origin, accessToken: "tr_dev_test" } } |
| 183 | + ); |
| 184 | + |
| 185 | + expect(handle.id).toBe("run_test"); |
| 186 | + expect(requestedPaths).toHaveLength(1); |
| 187 | + expect(requestedPaths[0]).toMatch( |
| 188 | + /^trigger\/packet_[123456789abcdefghijkmnopqrstuvwxyz]{21}\/payload\.json$/ |
| 189 | + ); |
| 190 | + expect(requestedPaths[0]).not.toContain("%"); |
| 191 | + } |
| 192 | + ); |
| 193 | +}); |
| 194 | + |
72 | 195 | describe("readableStreamToAsyncIterable", () => { |
73 | 196 | it("yields all values from the stream", async () => { |
74 | 197 | const values = [1, 2, 3, 4, 5]; |
|
0 commit comments