Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
257 lines (225 loc) · 7.49 KB
/
Copy pathutils.ts
File metadata and controls
257 lines (225 loc) · 7.49 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
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
import { FILEBASE_API_URL } from './constants.ts'
import type { RequiredArgs } from './types.ts'
import * as crypto from 'node:crypto'
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 type RequestOptions = {
host: string
region: string
service: string
method: string
path: string
headers: Record<string, string | number>
key?: string
}
export const generateFilebaseRequestOptions = (
token: string,
requestOptions: RequestOptions,
) => {
const [accessKeyId, secretAccessKey] = atob(token).split(':')
if (!accessKeyId || !secretAccessKey) {
throw new Error('Missing access key ID and secret access key')
}
const now = new Date()
const amzDate = now.toISOString().replace(/[:-]|\.\d{3}/g, '') + 'Z'
const dateStamp = amzDate.slice(0, 8)
// Payload hash (empty string for PUT with no body)
const payloadHash = crypto.createHash('sha256').update('').digest('hex')
// Canonical request
const canonicalHeaders = `content-length:${
requestOptions.headers['Content-Length']
}\nhost:${requestOptions.host}\nx-amz-content-sha256:${payloadHash}\nx-amz-date:${amzDate}\n`
const signedHeaders = 'content-length;host;x-amz-content-sha256;x-amz-date'
const canonicalRequest = [
requestOptions.method,
requestOptions.path,
'', // query string
canonicalHeaders,
signedHeaders,
payloadHash,
].join('\n')
const hashedCanonicalRequest = crypto.createHash('sha256').update(canonicalRequest).digest('hex')
// String to sign
const credentialScope = `${dateStamp}/${requestOptions.region}/${requestOptions.service}/aws4_request`
const stringToSign = [
'AWS4-HMAC-SHA256',
amzDate,
credentialScope,
hashedCanonicalRequest,
].join('\n')
// Signing key
const kDate = crypto.createHmac('sha256', 'AWS4' + secretAccessKey).update(dateStamp).digest()
const kRegion = crypto.createHmac('sha256', kDate).update(requestOptions.region).digest()
const kService = crypto.createHmac('sha256', kRegion).update(requestOptions.service).digest()
const kSigning = crypto.createHmac('sha256', kService).update('aws4_request').digest()
const signature = crypto.createHmac('sha256', kSigning).update(stringToSign).digest('hex')
// Fill in the headers
requestOptions.headers['X-Amz-Content-Sha256'] = payloadHash
requestOptions.headers['X-Amz-Date'] = amzDate
requestOptions.headers['Authorization'] =
`AWS4-HMAC-SHA256 Credential=${accessKeyId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`
return requestOptions
}
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 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(':').map((s) => s.trim())
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: RequestOptions = {
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
region: 'us-east-1',
method: 'PUT',
service: 's3',
headers: {
'Content-Length': 0,
},
path: '/',
}
requestOptions = generateFilebaseRequestOptions(token, requestOptions)
return await fetch(`https://${requestOptions.host}/`, requestOptions as unknown 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,
},
}
}