forked from cloudflare/miniflare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r2Object.ts
323 lines (292 loc) · 11.1 KB
/
r2Object.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { Blob } from "buffer";
import crypto from "crypto";
import { arrayBuffer } from "stream/consumers";
import { ReadableStream } from "stream/web";
import { TextDecoder } from "util";
import { waitForOpenInputGate } from "@miniflare/shared";
import { Headers } from "undici";
import { R2Conditional, R2Range } from "./bucket";
export interface R2ConditionalUnparsed {
etagMatches?: string | string[];
etagDoesNotMatch?: string | string[];
uploadedBefore?: string | Date;
uploadedAfter?: string | Date;
}
/**
* Metadata that's automatically rendered into R2 HTTP API endpoints.
* ```
* * contentType -> content-type
* * contentLanguage -> content-language
* etc...
* ```
* This data is echoed back on GET responses based on what was originally
* assigned to the object (and can typically also be overriden when issuing
* the GET request).
*/
export interface R2HTTPMetadata {
contentType?: string;
contentLanguage?: string;
contentDisposition?: string;
contentEncoding?: string;
cacheControl?: string;
cacheExpiry?: Date;
}
export interface R2ObjectMetadata {
// The object’s key.
key: string;
// Random unique string associated with a specific upload of a key.
version: string;
// Size of the object in bytes.
size: number;
// The etag associated with the object upload.
etag: string;
// The object’s etag, in quotes so as to be returned as a header.
httpEtag: string;
// A Date object representing the time the object was uploaded.
uploaded: Date;
// Various HTTP headers associated with the object. Refer to HTTP Metadata.
httpMetadata: R2HTTPMetadata;
// A map of custom, user-defined metadata associated with the object.
customMetadata: Record<string, string>;
// If a GET request was made with a range option, this will be added
range?: R2Range;
}
const decoder = new TextDecoder();
// NOTE: Incase multipart is ever added to the worker
// refer to https://stackoverflow.com/questions/12186993/what-is-the-algorithm-to-compute-the-amazon-s3-etag-for-a-file-larger-than-5gb/19896823#19896823
export function createHash(input: Uint8Array): string {
return crypto.createHash("md5").update(input).digest("hex");
}
export function createVersion(): string {
const size = 32;
return crypto.randomBytes(size).toString("base64").slice(0, size);
}
// when pulling from storage, we need to convert date strings to Date objects
export function parseR2ObjectMetadata(meta: R2ObjectMetadata): void {
meta.uploaded = new Date(meta.uploaded);
if (meta.httpMetadata.cacheExpiry) {
meta.httpMetadata.cacheExpiry = new Date(meta.httpMetadata.cacheExpiry);
}
}
export function parseHttpMetadata(
httpMetadata?: R2HTTPMetadata | Headers
): R2HTTPMetadata {
if (httpMetadata === undefined) return {};
if (httpMetadata instanceof Headers) {
const cExpiry = httpMetadata.get("cache-expiry");
return {
contentType: httpMetadata.get("content-type") ?? undefined,
contentLanguage: httpMetadata.get("content-language") ?? undefined,
contentDisposition: httpMetadata.get("content-disposition") ?? undefined,
contentEncoding: httpMetadata.get("content-encoding") ?? undefined,
cacheControl: httpMetadata.get("cache-control") ?? undefined,
cacheExpiry: cExpiry ? new Date(cExpiry) : undefined,
};
} else {
httpMetadata = { ...httpMetadata };
// remove variables that are not part of the HTTP metadata
const httpMetadataList = [
"contentType",
"contentLanguage",
"contentDisposition",
"contentEncoding",
"cacheControl",
"cacheExpiry",
];
for (const key of Object.keys(httpMetadata)) {
if (!httpMetadataList.includes(key)) {
delete httpMetadata[key as keyof R2HTTPMetadata];
}
}
return httpMetadata;
}
}
// false -> the condition testing "failed"
export function testR2Conditional(
conditional: R2Conditional,
metadata?: R2ObjectMetadata
): boolean {
const { etagMatches, etagDoesNotMatch, uploadedBefore, uploadedAfter } =
conditional;
// If the object doesn't exist
if (metadata === undefined) {
// the etagDoesNotMatch and uploadedBefore automatically pass
// etagMatches and uploadedAfter automatically fail if they exist
return etagMatches === undefined && uploadedAfter === undefined;
}
const { etag, uploaded } = metadata;
// ifMatch check
const ifMatch = etagMatches ? matchStrings(etagMatches, etag) : null;
if (ifMatch === false) return false;
// ifNoMatch check
const ifNoneMatch = etagDoesNotMatch
? !matchStrings(etagDoesNotMatch, etag)
: null;
if (ifNoneMatch === false) return false;
// ifUnmodifiedSince check
if (
ifMatch !== true && // if "ifMatch" is true, we ignore date checking
uploadedBefore !== undefined &&
uploaded > uploadedBefore
) {
return false;
}
// ifModifiedSince check
if (
ifNoneMatch !== true && // if "ifNoneMatch" is true, we ignore date checking
uploadedAfter !== undefined &&
uploaded < uploadedAfter
) {
return false;
}
return true;
}
function matchStrings(a: string | string[], b: string): boolean {
if (typeof a === "string") return a === b;
else return a.includes(b);
}
// headers can be a list: e.g. ["if-match", "a, b, c"] -> "if-match: [a, b, c]"
function parseHeaderArray(input: string): string | string[] {
// split if comma found, otherwise return input
if (!input.includes(",")) return stripQuotes(input);
return input.split(",").map((x) => stripQuotes(x));
}
function stripQuotes(input: string): string {
input = input.trim();
if (input[0] === '"') input = input.slice(1);
if (input[input.length - 1] === '"') input = input.slice(0, -1);
return input;
}
export function parseOnlyIf(
onlyIf?: R2ConditionalUnparsed | R2Conditional | Headers
): R2Conditional {
if (onlyIf === undefined) return {};
if (onlyIf instanceof Headers) {
onlyIf = {
etagMatches: onlyIf.get("if-match") ?? undefined,
etagDoesNotMatch: onlyIf.get("if-none-match") ?? undefined,
uploadedBefore: onlyIf.get("if-unmodified-since") ?? undefined,
uploadedAfter: onlyIf.get("if-modified-since") ?? undefined,
};
}
// if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c']
if (typeof onlyIf.etagMatches === "string") {
onlyIf.etagMatches = parseHeaderArray(onlyIf.etagMatches);
} else if (Array.isArray(onlyIf.etagMatches)) {
// otherwise if an array, strip the quotes
onlyIf.etagMatches = onlyIf.etagMatches.map((x) => stripQuotes(x));
}
// if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c']
if (typeof onlyIf.etagDoesNotMatch === "string") {
onlyIf.etagDoesNotMatch = parseHeaderArray(onlyIf.etagDoesNotMatch);
} else if (Array.isArray(onlyIf.etagDoesNotMatch)) {
// otherwise if an array, strip the quotes
onlyIf.etagDoesNotMatch = onlyIf.etagDoesNotMatch.map((x) =>
stripQuotes(x)
);
}
// if string, convert to date
if (typeof onlyIf.uploadedBefore === "string") {
onlyIf.uploadedBefore = new Date(stripQuotes(onlyIf.uploadedBefore));
}
// if string, convert to date
if (typeof onlyIf.uploadedAfter === "string") {
onlyIf.uploadedAfter = new Date(stripQuotes(onlyIf.uploadedAfter));
}
return onlyIf as R2Conditional;
}
/**
* R2Object is created when you PUT an object into an R2 bucket.
* R2Object represents the metadata of an object based on the information
* provided by the uploader. Every object that you PUT into an R2 bucket
* will have an R2Object created.
*/
export class R2Object {
// The object’s key.
readonly key: string;
// Random unique string associated with a specific upload of a key.
readonly version: string;
// Size of the object in bytes.
readonly size: number;
// The etag associated with the object upload.
readonly etag: string;
// The object’s etag, in quotes so as to be returned as a header.
readonly httpEtag: string;
// A Date object representing the time the object was uploaded.
readonly uploaded: Date;
// Various HTTP headers associated with the object. Refer to
// https://developers.cloudflare.com/r2/runtime-apis/#http-metadata.
readonly httpMetadata: R2HTTPMetadata;
// A map of custom, user-defined metadata associated with the object.
readonly customMetadata: Record<string, string>;
// If a GET request was made with a range option, this will be added
readonly range?: R2Range;
constructor(metadata: R2ObjectMetadata) {
this.key = metadata.key;
this.version = metadata.version;
this.size = metadata.size;
this.etag = metadata.etag;
this.httpEtag = metadata.httpEtag;
this.uploaded = metadata.uploaded;
this.httpMetadata = metadata.httpMetadata;
this.customMetadata = metadata.customMetadata;
this.range = metadata.range;
}
// Retrieves the httpMetadata from the R2Object and applies their corresponding
// HTTP headers to the Headers input object. Refer to HTTP Metadata.
writeHttpMetadata(headers: Headers): void {
for (const [key, value] of Object.entries(this.httpMetadata)) {
const camelToDash = key.replace(/([A-Z])/g, "-$1").toLowerCase();
headers.set(camelToDash, value);
}
}
}
export class R2ObjectBody extends R2Object {
// The object’s value.
readonly body: ReadableStream<Uint8Array>;
// Whether the object’s value has been consumed or not.
readonly bodyUsed: boolean = false;
constructor(metadata: R2ObjectMetadata, value: Uint8Array) {
super(metadata);
// To maintain readonly, we build this clever work around to update upon consumption.
const setBodyUsed = (): void => {
(this.bodyUsed as R2ObjectBody["bodyUsed"]) = true;
};
// convert value to readable stream
this.body = new ReadableStream<Uint8Array>({
type: "bytes" as any,
// Delay enqueuing chunk until it's actually requested so we can wait
// for the input gate to open before delivering it
async pull(controller) {
await waitForOpenInputGate();
if (value.byteLength) controller.enqueue(value);
controller.close();
// Not documented in MDN but if there's an ongoing request that's waiting,
// we need to tell it that there were 0 bytes delivered so that it unblocks
// and notices the end of stream.
// @ts-expect-error `byobRequest` has type `undefined` in `@types/node`
controller.byobRequest?.respond(0);
setBodyUsed();
},
});
}
// Returns a Promise that resolves to an ArrayBuffer containing the object’s value.
async arrayBuffer(): Promise<ArrayBuffer> {
if (this.bodyUsed) throw new TypeError("Body already used.");
// @ts-expect-error ReadableStream is missing properties
return arrayBuffer(this.body);
}
// Returns a Promise that resolves to an string containing the object’s value.
async text(): Promise<string> {
return decoder.decode(await this.arrayBuffer());
}
// Returns a Promise that resolves to the given object containing the object’s value.
async json<T>(): Promise<T> {
return JSON.parse(await this.text());
}
// Returns a Promise that resolves to a binary Blob containing the object’s value.
async blob(): Promise<Blob> {
const ab = await this.arrayBuffer();
return new Blob([new Uint8Array(ab)]);
}
}