-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_proxy.ts
64 lines (55 loc) · 1.62 KB
/
create_proxy.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
import { buildConnectionString, config, containerPattern } from "./config.ts";
import {
notAllowedFactory,
notConfigured,
unauthorizedContainer,
} from "./errors.ts";
import { AzureStorage } from "azure_storage_client/storage.ts";
import { CreateProxyOptions } from "./types.ts";
export const createProxy =
(options?: CreateProxyOptions) =>
async (request: Request): Promise<Response> => {
const {
key,
account,
containers,
auth,
handlers,
fallback,
suffix,
} = config(
options,
);
if (!account) {
return notConfigured();
}
const method = request.method.toUpperCase();
const methods = new Set(handlers.keys());
if (!methods.has(method)) {
return notAllowedFactory(methods)(request);
}
const unauthorized = auth ? await auth(request) : undefined;
if (unauthorized && unauthorized?.status >= 400) {
return unauthorized;
}
const match = containerPattern.exec(request.url);
if (match?.pathname) {
const { container, path } = match.pathname.groups;
if (containers && !containers.has(container)) {
return unauthorizedContainer();
}
const handler = handlers.get(method);
if (!handler) {
return notAllowedFactory(methods)(request);
}
const storage = new AzureStorage(
buildConnectionString({ account, key, suffix }),
);
return handler({ request, storage, container, path });
}
// No matching container, provide fallback or 405
if (fallback) {
return fallback(request);
}
return notAllowedFactory(methods)(request);
};