-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathutil.ts
More file actions
170 lines (146 loc) · 4.43 KB
/
util.ts
File metadata and controls
170 lines (146 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import contentDisposition from "content-disposition";
export interface ModelResponse {
// TypeScript 5.9.3: Use ReadableStream<Uint8Array> - pipeThrough results are cast
stream: ReadableStream<Uint8Array> | null;
response: Response;
}
export function parseAuthHeader(
headers: Record<string, string | string[] | undefined>,
): string | null {
const authHeader = headers["authorization"];
let authValue = null;
if (Array.isArray(authHeader)) {
authValue = authHeader[authHeader.length - 1];
} else {
authValue = authHeader;
}
if (authValue) {
const parts = authValue.split(" ");
if (parts.length !== 2) {
return null;
}
return parts[1];
}
// Anthropic uses x-api-key instead of authorization.
const apiKeyHeader = headers["x-api-key"];
if (apiKeyHeader) {
return Array.isArray(apiKeyHeader)
? apiKeyHeader[apiKeyHeader.length - 1]
: apiKeyHeader;
}
return null;
}
export function parseNumericHeader(
headers: Record<string, string | string[] | undefined>,
headerKey: string,
): number | null {
let value = headers[headerKey];
if (Array.isArray(value)) {
value = value[value.length - 1];
}
if (value !== undefined) {
try {
return parseInt(value, 10);
} catch (e) {}
}
return null;
}
// This is duplicated from app/utils/object.ts
export function isObject(value: any): value is { [key: string]: any } {
return value instanceof Object && !(value instanceof Array);
}
export function getTimestampInSeconds() {
return Math.floor(Date.now() / 1000);
}
export function flattenChunksArray(allChunks: Uint8Array[]): Uint8Array {
const flatArray = new Uint8Array(allChunks.reduce((a, b) => a + b.length, 0));
for (let i = 0, offset = 0; i < allChunks.length; i++) {
flatArray.set(allChunks[i], offset);
offset += allChunks[i].length;
}
return flatArray;
}
export function flattenChunks(allChunks: Uint8Array[]) {
const flatArray = flattenChunksArray(allChunks);
return new TextDecoder().decode(flatArray);
}
export function isEmpty(a: any): a is null | undefined {
return a === undefined || a === null;
}
export function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
export class ProxyBadRequestError extends Error {
constructor(public message: string) {
super(message);
}
}
export function parseFileMetadataFromUrl(
url: string,
): { filename: string; contentType?: string; url: URL } | undefined {
try {
// Handle empty string
if (!url || url.trim() === "") {
return undefined;
}
// Use URL to parse complex URLs rather than string splitting
let parsedUrl: URL | undefined;
try {
parsedUrl = new URL(url);
} catch (e) {
return undefined;
}
// If the URL is not http(s), file cannot be accessed
// If pathname is empty or ends with "/", there's no filename to extract
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
return undefined;
} else if (
!parsedUrl.pathname ||
parsedUrl.pathname === "/" ||
parsedUrl.pathname.endsWith("/")
) {
return undefined;
}
// Get the last segment of the path
let filename = parsedUrl.pathname.split("/").pop();
if (!filename) {
return undefined;
}
let contentType = undefined;
// Handle case where this is an S3 pre-signed URL
if (parsedUrl.searchParams.get("X-Amz-Expires") !== null) {
const disposition = contentDisposition.parse(
parsedUrl.searchParams.get("response-content-disposition") || "",
);
filename = disposition.parameters.filename
? decodeURIComponent(disposition.parameters.filename)
: filename;
contentType =
parsedUrl.searchParams.get("response-content-type") ?? undefined;
}
try {
filename = decodeURIComponent(filename);
} catch (e) {
// If the filename is not valid UTF-8, we'll just return the original filename
}
return { filename, contentType, url: parsedUrl };
} catch (e) {
return undefined;
}
}
export const writeToReadable = (response: string) => {
return new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(response));
controller.close();
},
});
};
export function _urljoin(...parts: string[]): string {
return parts
.map((x, i) =>
x.replace(/^\//, "").replace(i < parts.length - 1 ? /\/$/ : "", ""),
)
.filter((x) => x.trim() !== "")
.join("/");
}