Skip to content

Commit 3993730

Browse files
fix(core): Harden RunEvent schema and add backward-compatibility transform
1 parent 45c7318 commit 3993730

File tree

6 files changed

+114
-79
lines changed

6 files changed

+114
-79
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"@trigger.dev/core": patch
2+
"@trigger.dev/core": minor
33
---
44

55
Define RunEvent schema and update ApiClient to use it

packages/cli-v3/src/commands/deploy.ts

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,8 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
501501
const version = deployment.version;
502502

503503
const rawDeploymentLink = `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project}/deployments/${deployment.shortCode}`;
504-
const rawTestLink = `${authorization.dashboardUrl}/projects/v3/${
505-
resolvedConfig.project
506-
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`;
504+
const rawTestLink = `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project
505+
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`;
507506

508507
const deploymentLink = cliLink("View deployment", rawDeploymentLink);
509508
const testLink = cliLink("Test tasks", rawTestLink);
@@ -720,8 +719,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
720719
}
721720
} else {
722721
outro(
723-
`Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"} ${
724-
isLinksSupported ? `| ${deploymentLink} | ${testLink}` : ""
722+
`Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"} ${isLinksSupported ? `| ${deploymentLink} | ${testLink}` : ""
725723
}`
726724
);
727725

@@ -745,18 +743,16 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
745743
TRIGGER_VERSION: version,
746744
TRIGGER_DEPLOYMENT_SHORT_CODE: deployment.shortCode,
747745
TRIGGER_DEPLOYMENT_URL: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project}/deployments/${deployment.shortCode}`,
748-
TRIGGER_TEST_URL: `${authorization.dashboardUrl}/projects/v3/${
749-
resolvedConfig.project
750-
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
746+
TRIGGER_TEST_URL: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project
747+
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
751748
},
752749
outputs: {
753750
deploymentVersion: version,
754751
workerVersion: version,
755752
deploymentShortCode: deployment.shortCode,
756753
deploymentUrl: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project}/deployments/${deployment.shortCode}`,
757-
testUrl: `${authorization.dashboardUrl}/projects/v3/${
758-
resolvedConfig.project
759-
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
754+
testUrl: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project
755+
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
760756
needsPromotion: options.skipPromotion ? "true" : "false",
761757
},
762758
});
@@ -799,8 +795,7 @@ async function failDeploy(
799795
checkLogsForErrors(logs);
800796

801797
outro(
802-
`${chalkError(`${prefix}:`)} ${
803-
error.message
798+
`${chalkError(`${prefix}:`)} ${error.message
804799
}. Full build logs have been saved to ${logPath}`
805800
);
806801

@@ -1100,9 +1095,8 @@ async function handleNativeBuildServerDeploy({
11001095
const deployment = initializeDeploymentResult.data;
11011096

11021097
const rawDeploymentLink = `${dashboardUrl}/projects/v3/${config.project}/deployments/${deployment.shortCode}`;
1103-
const rawTestLink = `${dashboardUrl}/projects/v3/${config.project}/test?environment=${
1104-
options.env === "prod" ? "prod" : "stg"
1105-
}`;
1098+
const rawTestLink = `${dashboardUrl}/projects/v3/${config.project}/test?environment=${options.env === "prod" ? "prod" : "stg"
1099+
}`;
11061100

11071101
const exposedDeploymentLink = isLinksSupported
11081102
? cliLink(chalk.bold(rawDeploymentLink), rawDeploymentLink)
@@ -1155,8 +1149,9 @@ async function handleNativeBuildServerDeploy({
11551149
const [readSessionError, readSession] = await tryCatch(
11561150
stream.readSession(
11571151
{
1158-
start: { from: { seqNum: 0 }, clamp: true },
1159-
stop: { waitSecs: 60 * 20 }, // 20 minutes
1152+
seq_num: 0,
1153+
clamp: true,
1154+
wait: 60 * 20, // 20 minutes
11601155
},
11611156
{ signal: abortController.signal }
11621157
)
@@ -1167,8 +1162,7 @@ async function handleNativeBuildServerDeploy({
11671162
log.warn(`Failed streaming build logs, open the deployment in the dashboard to view the logs`);
11681163

11691164
outro(
1170-
`Version ${deployment.version} is being deployed ${
1171-
isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
1165+
`Version ${deployment.version} is being deployed ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
11721166
}`
11731167
);
11741168

@@ -1193,7 +1187,7 @@ async function handleNativeBuildServerDeploy({
11931187

11941188
switch (event.type) {
11951189
case "log": {
1196-
if (record.seqNum === 0) {
1190+
if (record.seq_num === 0) {
11971191
$queuedSpinner.stop("Build started");
11981192
console.log("│");
11991193
queuedSpinnerStopped = true;
@@ -1214,10 +1208,10 @@ async function handleNativeBuildServerDeploy({
12141208
level === "error"
12151209
? chalk.bold(chalkError(message))
12161210
: level === "warn"
1217-
? chalkWarning(message)
1218-
: level === "debug"
1219-
? chalkGrey(message)
1220-
: message;
1211+
? chalkWarning(message)
1212+
: level === "debug"
1213+
? chalkGrey(message)
1214+
: message;
12211215

12221216
// We use console.log here instead of clack's logger as the current version does not support changing the line spacing.
12231217
// And the logs look verbose with the default spacing.
@@ -1250,8 +1244,7 @@ async function handleNativeBuildServerDeploy({
12501244
log.error("Failed dequeueing build, please try again shortly");
12511245

12521246
throw new OutroCommandError(
1253-
`Version ${deployment.version} ${
1254-
isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
1247+
`Version ${deployment.version} ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
12551248
}`
12561249
);
12571250
}
@@ -1266,8 +1259,7 @@ async function handleNativeBuildServerDeploy({
12661259
}
12671260

12681261
throw new OutroCommandError(
1269-
`Version ${deployment.version} ${
1270-
isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
1262+
`Version ${deployment.version} ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
12711263
}`
12721264
);
12731265
}
@@ -1293,13 +1285,12 @@ async function handleNativeBuildServerDeploy({
12931285
}
12941286

12951287
outro(
1296-
`Version ${deployment.version} was deployed ${
1297-
isLinksSupported
1298-
? `| ${cliLink("Test tasks", rawTestLink)} | ${cliLink(
1299-
"View deployment",
1300-
rawDeploymentLink
1301-
)}`
1302-
: ""
1288+
`Version ${deployment.version} was deployed ${isLinksSupported
1289+
? `| ${cliLink("Test tasks", rawTestLink)} | ${cliLink(
1290+
"View deployment",
1291+
rawDeploymentLink
1292+
)}`
1293+
: ""
13031294
}`
13041295
);
13051296
return process.exit(0);
@@ -1313,14 +1304,13 @@ async function handleNativeBuildServerDeploy({
13131304
chalk.bold(
13141305
chalkError(
13151306
"Deployment failed" +
1316-
(finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "")
1307+
(finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "")
13171308
)
13181309
)
13191310
);
13201311

13211312
throw new OutroCommandError(
1322-
`Version ${deployment.version} deployment failed ${
1323-
isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
1313+
`Version ${deployment.version} deployment failed ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
13241314
}`
13251315
);
13261316
}
@@ -1333,14 +1323,13 @@ async function handleNativeBuildServerDeploy({
13331323
chalk.bold(
13341324
chalkError(
13351325
"Deployment timed out" +
1336-
(finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "")
1326+
(finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "")
13371327
)
13381328
)
13391329
);
13401330

13411331
throw new OutroCommandError(
1342-
`Version ${deployment.version} deployment timed out ${
1343-
isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
1332+
`Version ${deployment.version} deployment timed out ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
13441333
}`
13451334
);
13461335
}
@@ -1353,14 +1342,13 @@ async function handleNativeBuildServerDeploy({
13531342
chalk.bold(
13541343
chalkError(
13551344
"Deployment was canceled" +
1356-
(finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "")
1345+
(finalDeploymentEvent.message ? `: ${finalDeploymentEvent.message}` : "")
13571346
)
13581347
)
13591348
);
13601349

13611350
throw new OutroCommandError(
1362-
`Version ${deployment.version} deployment canceled ${
1363-
isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
1351+
`Version ${deployment.version} deployment canceled ${isLinksSupported ? `| ${cliLink("View deployment", rawDeploymentLink)}` : ""
13641352
}`
13651353
);
13661354
}
@@ -1379,13 +1367,12 @@ async function handleNativeBuildServerDeploy({
13791367
}
13801368

13811369
outro(
1382-
`Version ${deployment.version} ${
1383-
isLinksSupported
1384-
? `| ${cliLink("Test tasks", rawTestLink)} | ${cliLink(
1385-
"View deployment",
1386-
rawDeploymentLink
1387-
)}`
1388-
: ""
1370+
`Version ${deployment.version} ${isLinksSupported
1371+
? `| ${cliLink("Test tasks", rawTestLink)} | ${cliLink(
1372+
"View deployment",
1373+
rawDeploymentLink
1374+
)}`
1375+
: ""
13891376
}`
13901377
);
13911378
return process.exit(0);

packages/cli-v3/tsc_output.txt

16.7 KB
Binary file not shown.

packages/core/src/v3/realtimeStreams/streamsWriterV2.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export class StreamsWriterV2<T = any> implements StreamsWriter {
6161
accessToken: options.accessToken,
6262
...(options.endpoint
6363
? {
64-
endpoints: {
65-
account: options.endpoint,
66-
basin: options.endpoint,
67-
},
68-
}
64+
endpoints: {
65+
account: options.endpoint,
66+
basin: options.endpoint,
67+
},
68+
}
6969
: {}),
7070
});
7171
this.flushIntervalMs = options.flushIntervalMs ?? 200;
@@ -135,7 +135,7 @@ export class StreamsWriterV2<T = any> implements StreamsWriter {
135135
const stream = basin.stream(this.options.stream);
136136

137137
const session = await stream.appendSession({
138-
maxInflightBytes: this.maxInflightBytes,
138+
maxQueuedBytes: this.maxInflightBytes,
139139
});
140140

141141
this.sessionWritable = session.writable;
@@ -152,7 +152,7 @@ export class StreamsWriterV2<T = any> implements StreamsWriter {
152152
return;
153153
}
154154
// Convert each chunk to JSON string and wrap in AppendRecord
155-
controller.enqueue(AppendRecord.string({ body: JSON.stringify({ data: chunk, id: nanoid(7) }) }));
155+
controller.enqueue(AppendRecord.make(JSON.stringify({ data: chunk, id: nanoid(7) })));
156156
},
157157
})
158158
)
@@ -169,9 +169,9 @@ export class StreamsWriterV2<T = any> implements StreamsWriter {
169169
const lastAcked = session.lastAckedPosition();
170170

171171
if (lastAcked?.end) {
172-
const recordsWritten = lastAcked.end.seqNum;
172+
const recordsWritten = lastAcked.end.seq_num;
173173
this.log(
174-
`[S2MetadataStream] Written ${recordsWritten} records, ending at seqNum=${lastAcked.end.seqNum}`
174+
`[S2MetadataStream] Written ${recordsWritten} records, ending at seq_num=${lastAcked.end.seq_num}`
175175
);
176176
}
177177
} catch (error) {
@@ -223,5 +223,5 @@ async function* streamToAsyncIterator<T>(stream: ReadableStream<T>): AsyncIterab
223223
function safeReleaseLock(reader: ReadableStreamDefaultReader<any>) {
224224
try {
225225
reader.releaseLock();
226-
} catch (error) {}
226+
} catch (error) { }
227227
}

packages/core/src/v3/schemas/api-type.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { InitializeDeploymentRequestBody, RunEvent, ListRunEventsResponse } from "./api.js";
2+
import { InitializeDeploymentRequestBody, RunEvent, ListRunEventsResponse, ListRunEventsResponseWithStringDates } from "./api.js";
33
import type { InitializeDeploymentRequestBody as InitializeDeploymentRequestBodyType } from "./api.js";
44

55
describe("InitializeDeploymentRequestBody", () => {
@@ -197,6 +197,18 @@ describe("RunEvent Schema", () => {
197197
expect(result.startTime.toISOString()).toBe("2024-03-14T00:00:00.000Z");
198198
});
199199

200+
it("should handle Date object", () => {
201+
const now = new Date();
202+
const result = RunEvent.safeParse({
203+
...validEvent,
204+
startTime: now,
205+
});
206+
expect(result.success).toBe(true);
207+
if (result.success) {
208+
expect(result.data.startTime.toISOString()).toBe(now.toISOString());
209+
}
210+
});
211+
200212
it("handles bigint nanosecond startTime", () => {
201213
const event = { ...validEvent, startTime: 1710374400000000000n };
202214
const result = RunEvent.parse(event as any);
@@ -238,6 +250,19 @@ describe("RunEvent Schema", () => {
238250
const result = RunEvent.parse(eventWithSlug);
239251
expect(result.taskSlug).toBe("my-task");
240252
});
253+
254+
it("ListRunEventsResponseWithStringDates correctly transforms Dates to strings", () => {
255+
const rawResponse = {
256+
events: [validEvent],
257+
};
258+
259+
const parsed = ListRunEventsResponse.parse(rawResponse);
260+
expect(parsed.events[0]!.startTime).toBeInstanceOf(Date);
261+
262+
const legacy = ListRunEventsResponseWithStringDates.parse(rawResponse);
263+
expect(typeof legacy.events[0]!.startTime).toBe("string");
264+
expect(legacy.events[0]!.startTime).toBe(parsed.events[0]!.startTime.toISOString());
265+
});
241266
});
242267

243268
describe("ListRunEventsResponse Schema", () => {

0 commit comments

Comments
 (0)