Fixed Buffer reference error in the browser

This commit is contained in:
Eric Allam
2023-11-06 17:51:47 +00:00
parent 329944841f
commit 578d2e5410
5 changed files with 29 additions and 36 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/core": patch
---
Fixed Buffer reference error
+10 -1
View File
@@ -3,7 +3,6 @@ import {
ConnectionAuth,
EndpointHeadersSchema,
ErrorWithStackSchema,
HttpSourceRequest,
HttpSourceResponseSchema,
IndexEndpointResponseSchema,
NormalizedResponseSchema,
@@ -21,6 +20,16 @@ import {
import { performance } from "node:perf_hooks";
import { safeBodyFromResponse, safeParseBodyFromResponse } from "~/utils/json";
import { logger } from "./logger.server";
import { z } from "zod";
const HttpSourceRequestSchema = z.object({
url: z.string().url(),
method: z.string(),
headers: z.record(z.string()),
rawBody: z.instanceof(Buffer).optional().nullable(),
});
export type HttpSourceRequest = z.infer<typeof HttpSourceRequestSchema>;
export class EndpointApiError extends Error {
constructor(message: string, stack?: string) {
@@ -1,12 +1,10 @@
import { z } from "zod";
import type { PrismaClient } from "~/db.server";
import { prisma } from "~/db.server";
import { resolveSourceConnection } from "~/models/sourceConnection.server";
import { EndpointApi } from "../endpointApi.server";
import { IngestSendEvent } from "../events/ingestSendEvent.server";
import { getSecretStore } from "../secrets/secretStore.server";
import { resolveApiConnection, resolveRunConnection } from "~/models/runConnection.server";
import { ConnectionAuth } from "@trigger.dev/sdk";
import { resolveSourceConnection } from "~/models/sourceConnection.server";
export class DeliverHttpSourceRequestService {
#prismaClient: PrismaClient;
+13 -23
View File
@@ -119,28 +119,6 @@ function safeJsonClone(obj: unknown) {
}
}
function formattedDateTime() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const milliseconds = date.getMilliseconds();
// Make sure the time is always 2 digits
const formattedHours = hours < 10 ? `0${hours}` : hours;
const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes;
const formattedSeconds = seconds < 10 ? `0${seconds}` : seconds;
const formattedMilliseconds =
milliseconds < 10
? `00${milliseconds}`
: milliseconds < 100
? `0${milliseconds}`
: milliseconds;
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}.${formattedMilliseconds}`;
}
// If args is has a single item that is an object, return that object
function structureArgs(args: Array<Record<string, unknown>>, filteredKeys: string[] = []) {
if (args.length === 0) {
@@ -187,7 +165,7 @@ function prettyPrintBytes(value: unknown): string {
return "skipped size";
}
const sizeInBytes = Buffer.byteLength(JSON.stringify(value), "utf8");
const sizeInBytes = getSizeInBytes(value);
if (sizeInBytes < 1024) {
return `${sizeInBytes} bytes`;
@@ -203,3 +181,15 @@ function prettyPrintBytes(value: unknown): string {
return `${(sizeInBytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}
function getSizeInBytes(value: unknown) {
const jsonString = JSON.stringify(value);
if (typeof window === "undefined") {
// Node.js environment
return Buffer.byteLength(jsonString, "utf8");
} else {
// Browser environment
return new TextEncoder().encode(jsonString).length;
}
}
-9
View File
@@ -137,15 +137,6 @@ export type HandleTriggerSource = z.infer<typeof HandleTriggerSourceSchema>;
export type TriggerSource = z.infer<typeof TriggerSourceSchema>;
export const HttpSourceRequestSchema = z.object({
url: z.string().url(),
method: z.string(),
headers: z.record(z.string()),
rawBody: z.instanceof(Buffer).optional().nullable(),
});
export type HttpSourceRequest = z.infer<typeof HttpSourceRequestSchema>;
export const HttpSourceRequestHeadersSchema = z.object({
"x-ts-key": z.string(),
"x-ts-dynamic-id": z.string().optional(),