-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
161 lines (146 loc) · 4.09 KB
/
mod.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
import { FILEBASE_API_URL } from './constants.ts'
import {
aws4,
ChecksumConstructor,
createHash,
createHmac,
HeaderBag,
Hmac,
IHttpRequest,
NodeHash,
QueryParameterBag,
S3RequestPresigner,
} from './deps.ts'
import { RequiredArgs } from './types.ts'
import {
castSourceData,
createBucket,
formatUrl,
fromEnv,
generateFilebaseRequestOptions,
parseUrl,
toUint8Array,
} from './utils.ts'
export class HashImpl {
#algorithmIdentifier: string
#secret?
#hash!: NodeHash | Hmac
constructor(algorithmIdentifier: string, secret?: string) {
this.#algorithmIdentifier = algorithmIdentifier
this.#secret = secret
this.reset()
}
update(toHash: string, encoding?: 'utf8' | 'ascii' | 'latin1'): void {
this.#hash.update(toUint8Array(castSourceData(toHash, encoding)))
}
digest(): Promise<Uint8Array> {
return Promise.resolve(this.#hash.digest())
}
reset(): void {
this.#hash = this.#secret
? createHmac(this.#algorithmIdentifier, castSourceData(this.#secret))
: createHash(this.#algorithmIdentifier)
}
}
class HttpRequest {
method: string
protocol: string
hostname: string
port?: number
path: string
query?: QueryParameterBag | undefined
headers: HeaderBag
username?: string
password?: string
fragment?: string
body?: unknown
constructor(options: IHttpRequest) {
this.method = options.method || 'GET'
this.hostname = options.hostname || 'localhost'
this.port = options.port
this.query = options.query || {}
this.headers = options.headers || {}
this.body = options.body
this.protocol = options.protocol
? options.protocol.slice(-1) !== ':' ? `${options.protocol}:` : options.protocol
: 'https:'
this.path = options.path ? (options.path.charAt(0) !== '/' ? `/${options.path}` : options.path) : '/'
this.username = options.username
this.password = options.password
this.fragment = options.fragment
}
}
export const createPresignedUrl = async (
{ bucketName, apiUrl, file, token }: {
file: File
} & RequiredArgs,
) => {
await createBucket({ bucketName, apiUrl, token })
const url = parseUrl(`https://${apiUrl ?? FILEBASE_API_URL}/${bucketName}/${file.name}`)
const presigner = new S3RequestPresigner({
credentials: fromEnv(token),
region: 'us-east-1',
sha256: HashImpl.bind(null, 'sha256') as unknown as ChecksumConstructor,
})
const signedUrlObject = await presigner.presign(
new HttpRequest({ ...url, method: 'PUT', headers: {} }),
{ expiresIn: 3600 },
)
return formatUrl(signedUrlObject)
}
export const uploadCar = async ({ file, ...args }: RequiredArgs & { file: File }) => {
const url = await createPresignedUrl({ ...args, file })
const res = await fetch(decodeURIComponent(url), {
method: 'PUT',
body: file,
headers: { 'x-amz-meta-import': 'car' },
})
return res
}
export const headObject = async (
{ bucketName, filename, apiUrl, token }: RequiredArgs & {
filename: string
},
): Promise<[boolean, string | null]> => {
let requestOptions: aws4.Request & { key?: string } = {
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
path: `/${filename}`,
key: `/${filename}`,
region: 'us-east-1',
method: 'HEAD',
service: 's3',
headers: {},
}
requestOptions = generateFilebaseRequestOptions(
token,
requestOptions,
)
return await fetch(
`https://${requestOptions.host}${requestOptions.path}`,
requestOptions as RequestInit,
)
.then((res) => [res.status == 200, res.headers.get('x-amz-meta-cid')])
}
export const getObject = async (
{ bucketName, filename, apiUrl, token }: RequiredArgs & {
filename: string
},
) => {
let requestOptions: aws4.Request & { key?: string } = {
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
path: `/${filename}`,
key: `/${filename}`,
region: 'us-east-1',
method: 'GET',
service: 's3',
headers: {},
}
requestOptions = generateFilebaseRequestOptions(
token,
requestOptions,
)
return await fetch(
`https://${requestOptions.host}${requestOptions.path}`,
requestOptions as RequestInit,
)
}