-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
236 lines (208 loc) · 6.31 KB
/
utils.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
import { FILEBASE_API_URL } from './constants.ts'
import aws4 from 'aws4'
import { Buffer } from 'node:buffer'
import type { RequiredArgs } from './types.ts'
import { createHash, createHmac } from 'node:crypto'
import type { AwsCredentialIdentity, HttpRequest as IHttpRequest, QueryParameterBag } from '@smithy/types'
export const parseUrl = (
url: string | URL,
): Pick<IHttpRequest, 'hostname' | 'port' | 'protocol' | 'path' | 'query'> => {
if (typeof url === 'string') {
return parseUrl(new URL(url))
}
const { hostname, pathname, port, protocol } = url as URL
return {
hostname,
port: port ? parseInt(port) : undefined,
protocol,
path: pathname,
query: undefined,
}
}
export const generateFilebaseRequestOptions = (
token: string,
requestOptions: aws4.Request & { key?: string },
) => {
const [accessKeyId, secretAccessKey] = atob(token).split(':')
if (!accessKeyId || !secretAccessKey) {
throw new Error('Missing access key ID and secret access key')
}
aws4.sign(requestOptions, { accessKeyId, secretAccessKey })
return requestOptions
}
const te = new TextEncoder()
export const toUint8Array = (
data: string | ArrayBuffer | ArrayBufferView,
): Uint8Array => {
if (typeof data === 'string') {
return te.encode(data)
}
if (ArrayBuffer.isView(data)) {
return new Uint8Array(
data.buffer,
data.byteOffset,
data.byteLength / Uint8Array.BYTES_PER_ELEMENT,
)
}
return new Uint8Array(data)
}
const hexEncode = (c: string) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
const escapeUri = (uri: string): string =>
// AWS percent-encodes some extra non-standard characters in a URI
encodeURIComponent(uri).replace(/[!'()*]/g, hexEncode)
export const buildQueryString = (query: QueryParameterBag): string => {
const parts: string[] = []
for (let key of Object.keys(query).sort()) {
const value = query[key]
key = escapeUri(key)
if (Array.isArray(value)) {
for (let i = 0, iLen = value.length; i < iLen; i++) {
parts.push(`${key}=${escapeUri(value[i])}`)
}
} else {
let qsEntry = key
if (value || typeof value === 'string') {
qsEntry += `=${escapeUri(value)}`
}
parts.push(qsEntry)
}
}
return parts.join('&')
}
export const castSourceData = (
toCast: string | Buffer | ArrayBuffer,
encoding?: NodeJS.BufferEncoding,
): Buffer => {
if (Buffer.isBuffer(toCast)) return toCast
if (typeof toCast === 'string') {
return Buffer.from(toCast, encoding)
}
if (ArrayBuffer.isView(toCast)) {
return Buffer.from(toCast.buffer, toCast.byteOffset, toCast.byteLength)
}
return Buffer.from(toCast)
}
export const formatUrl = (
request: Omit<IHttpRequest, 'headers' | 'method'>,
): string => {
const { port, query } = request
let { protocol, path, hostname } = request
if (protocol && protocol.slice(-1) !== ':') {
protocol += ':'
}
if (port) {
hostname += `:${port}`
}
if (path && path.charAt(0) !== '/') {
path = `/${path}`
}
let queryString = query ? buildQueryString(query) : ''
if (queryString && queryString[0] !== '?') {
queryString = `?${queryString}`
}
let auth = ''
if (request.username != null || request.password != null) {
const username = request.username ?? ''
const password = request.password ?? ''
auth = `${username}:${password}@`
}
let fragment = ''
if (request.fragment) {
fragment = `#${request.fragment}`
}
return `${protocol}//${auth}${hostname}${path}${queryString}${fragment}`
}
export const fromEnv = (filebaseToken: string): () => AwsCredentialIdentity => {
return () => {
const [accessKeyId, secretAccessKey] = atob(filebaseToken).split(':')
if (!accessKeyId || !secretAccessKey) {
throw new Error('Missing access key ID and secret access key')
}
return {
accessKeyId,
secretAccessKey,
}
}
}
export const createBucket = async (
{ bucketName, apiUrl, token }: RequiredArgs,
) => {
let requestOptions: aws4.Request = {
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
region: 'us-east-1',
method: 'PUT',
service: 's3',
headers: {
'Content-Length': 0,
},
}
requestOptions = generateFilebaseRequestOptions(token, requestOptions)
return await fetch(`https://${requestOptions.host}/`, requestOptions as RequestInit)
.then((res) => res.status == 200)
}
const sign = (key: Uint8Array | string, msg: string): Uint8Array =>
new Uint8Array(createHmac('sha256', key).update(msg).digest())
function getSignatureKey(secretKey: string, date: string, region: string, service: string): Uint8Array {
const kDate = sign(`AWS4${secretKey}`, date)
const kRegion = sign(kDate, region)
const kService = sign(kRegion, service)
return sign(kService, 'aws4_request')
}
export function presignRequest(
request: IHttpRequest,
{
accessKeyId,
secretAccessKey,
region,
expiresIn = 3600,
}: {
accessKeyId: string
secretAccessKey: string
region: string
expiresIn?: number
},
): IHttpRequest {
const method = request.method.toUpperCase()
const host = request.hostname
const now = new Date()
const amzDate = now.toISOString().replace(/[:-]|\.\d{3}/g, '')
const dateStamp = amzDate.slice(0, 8)
const credentialScope = `${dateStamp}/${region}/s3/aws4_request`
const credential = `${accessKeyId}/${credentialScope}`
const signedHeaders = 'host'
const query: Record<string, string> = {
'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
'X-Amz-Credential': credential,
'X-Amz-Date': amzDate,
'X-Amz-Expires': String(expiresIn),
'X-Amz-SignedHeaders': signedHeaders,
}
const canonicalQuery = Object.entries(query)
.sort()
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&')
const canonicalRequest = [
method,
request.path,
canonicalQuery,
`host:${host}`,
'',
signedHeaders,
'UNSIGNED-PAYLOAD',
].join('\n')
const stringToSign = [
'AWS4-HMAC-SHA256',
amzDate,
credentialScope,
createHash('sha256').update(canonicalRequest).digest('hex'),
].join('\n')
const signingKey = getSignatureKey(secretAccessKey, dateStamp, region, 's3')
const signature = createHmac('sha256', signingKey).update(stringToSign).digest('hex')
return {
...request,
query: {
...query,
'X-Amz-Signature': signature,
},
}
}