@tanstack/ai-bedrock 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+ import type { AwsCredentialIdentity } from '@smithy/types'
2
+
3
+ // SigV4 header signing with WebCrypto. This is a port of the part of
4
+ // `@smithy/signature-v4` `sign()` that the Bedrock fetch wrapper uses: default
5
+ // options (path escape on, `x-amz-content-sha256` on), headers only.
6
+ // ponytail: no signing-key cache. Add one if the 5 HMAC steps per request show up in a profile.
7
+
8
+ const EMPTY_SHA256 =
9
+ 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
10
+
11
+ // SigV4 never signs these headers. Same list as `@smithy/signature-v4`.
12
+ const UNSIGNABLE_HEADERS = new Set([
13
+ 'authorization',
14
+ 'cache-control',
15
+ 'connection',
16
+ 'expect',
17
+ 'from',
18
+ 'keep-alive',
19
+ 'max-forwards',
20
+ 'pragma',
21
+ 'referer',
22
+ 'te',
23
+ 'trailer',
24
+ 'transfer-encoding',
25
+ 'upgrade',
26
+ 'user-agent',
27
+ 'x-amzn-trace-id',
28
+ ])
29
+
30
+ const encoder = new TextEncoder()
31
+
32
+ function toHex(bytes: ArrayBuffer) {
33
+ return Array.from(new Uint8Array(bytes), (byte) =>
34
+ byte.toString(16).padStart(2, '0'),
35
+ ).join('')
36
+ }
37
+
38
+ async function sha256Hex(data: BufferSource) {
39
+ return toHex(await crypto.subtle.digest('SHA-256', data))
40
+ }
41
+
42
+ async function hmac(key: BufferSource, data: string) {
43
+ const cryptoKey = await crypto.subtle.importKey(
44
+ 'raw',
45
+ key,
46
+ { name: 'HMAC', hash: 'SHA-256' },
47
+ false,
48
+ ['sign'],
49
+ )
50
+ return crypto.subtle.sign('HMAC', cryptoKey, encoder.encode(data))
51
+ }
52
+
53
+ // encodeURIComponent also encodes ! ' ( ) * for RFC 3986.
54
+ function escapeUri(value: string) {
55
+ return encodeURIComponent(value).replace(
56
+ /[!'()*]/g,
57
+ (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
58
+ )
59
+ }
60
+
61
+ // Remove empty, "." and ".." segments, then encode the path a second time.
62
+ // The wire path is already encoded once, and AWS (not S3) wants it twice.
63
+ function canonicalPath(pathname: string) {
64
+ const segments: Array<string> = []
65
+ for (const segment of pathname.split('/')) {
66
+ if (segment === '' || segment === '.') continue
67
+ if (segment === '..') segments.pop()
68
+ else segments.push(segment)
69
+ }
70
+ const trailingSlash = segments.length > 0 && pathname.endsWith('/') ? '/' : ''
71
+ return escapeUri(`/${segments.join('/')}${trailingSlash}`).replace(
72
+ /%2F/g,
73
+ '/',
74
+ )
75
+ }
76
+
77
+ // Decode each pair, encode it with the SigV4 rules, sort by key, then by value.
78
+ function canonicalQuery(search: string) {
79
+ const pairs = search
80
+ .slice(1)
81
+ .split('&')
82
+ .filter((pair) => pair !== '')
83
+ .map((pair) => {
84
+ const equals = pair.indexOf('=')
85
+ const key = equals === -1 ? pair : pair.slice(0, equals)
86
+ const value = equals === -1 ? '' : pair.slice(equals + 1)
87
+ return [
88
+ escapeUri(decodeURIComponent(key)),
89
+ escapeUri(decodeURIComponent(value)),
90
+ ]
91
+ })
92
+ pairs.sort(([keyA = '', valueA = ''], [keyB = '', valueB = '']) => {
93
+ if (keyA !== keyB) return keyA < keyB ? -1 : 1
94
+ if (valueA === valueB) return 0
95
+ return valueA < valueB ? -1 : 1
96
+ })
97
+ return pairs.map(([key, value]) => `${key}=${value}`).join('&')
98
+ }
99
+
100
+ // A stream, Blob, or FormData body cannot be hashed before it is sent.
101
+ async function payloadHash(body: BodyInit | null | undefined) {
102
+ if (body === null || body === undefined) return EMPTY_SHA256
103
+ if (typeof body === 'string') return sha256Hex(encoder.encode(body))
104
+ if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
105
+ return sha256Hex(body)
106
+ }
107
+ return 'UNSIGNED-PAYLOAD'
108
+ }
109
+
110
+ /**
111
+ * Signs an HTTP request with AWS Signature Version 4 (header auth).
112
+ *
113
+ * @param request - The request to sign. `host` always comes from `url`.
114
+ * @param credentials - Resolved AWS credentials. `sessionToken` adds `x-amz-security-token`.
115
+ * @param options - SigV4 `service` and `region`. `date` defaults to now.
116
+ * @returns The request headers plus `host`, `x-amz-date`,
117
+ * `x-amz-content-sha256`, `x-amz-security-token` (with a session token),
118
+ * and `authorization`.
119
+ *
120
+ * @example
121
+ * const headers = await signSigV4(
122
+ * { method: 'POST', url, headers: { 'content-type': 'application/json' }, body },
123
+ * await credentials(),
124
+ * { service: 'bedrock', region: 'us-east-1' },
125
+ * )
126
+ */
127
+ export async function signSigV4(
128
+ request: {
129
+ method: string
130
+ url: URL | string
131
+ headers?: HeadersInit
132
+ body?: BodyInit | null
133
+ },
134
+ credentials: AwsCredentialIdentity,
135
+ options: { service: string; region: string; date?: Date },
136
+ ) {
137
+ const url = new URL(request.url)
138
+ const headers = new Headers(request.headers)
139
+ headers.delete('authorization')
140
+ headers.delete('date')
141
+ headers.set('host', url.host)
142
+
143
+ const longDate = (options.date ?? new Date())
144
+ .toISOString()
145
+ .replace(/\.\d{3}Z$/, 'Z')
146
+ .replace(/[-:]/g, '')
147
+ const shortDate = longDate.slice(0, 8)
148
+ const scope = `${shortDate}/${options.region}/${options.service}/aws4_request`
149
+ headers.set('x-amz-date', longDate)
150
+ if (credentials.sessionToken) {
151
+ headers.set('x-amz-security-token', credentials.sessionToken)
152
+ }
153
+ const bodyHash =
154
+ headers.get('x-amz-content-sha256') ?? (await payloadHash(request.body))
155
+ headers.set('x-amz-content-sha256', bodyHash)
156
+
157
+ // Headers iterate with lowercase names in sorted order, as SigV4 needs.
158
+ const signed: Array<[string, string]> = []
159
+ for (const [name, value] of headers) {
160
+ const isUnsignable =
161
+ UNSIGNABLE_HEADERS.has(name) || /^(proxy|sec)-/.test(name)
162
+ if (isUnsignable) continue
163
+ signed.push([name, value.trim().replace(/\s+/g, ' ')])
164
+ }
165
+ const signedHeaders = signed.map(([name]) => name).join(';')
166
+ const canonicalRequest = [
167
+ request.method,
168
+ canonicalPath(url.pathname),
169
+ canonicalQuery(url.search),
170
+ ...signed.map(([name, value]) => `${name}:${value}`),
171
+ '',
172
+ signedHeaders,
173
+ bodyHash,
174
+ ].join('\n')
175
+
176
+ const stringToSign = [
177
+ 'AWS4-HMAC-SHA256',
178
+ longDate,
179
+ scope,
180
+ await sha256Hex(encoder.encode(canonicalRequest)),
181
+ ].join('\n')
182
+ let signingKey: BufferSource = encoder.encode(
183
+ `AWS4${credentials.secretAccessKey}`,
184
+ )
185
+ const scopeParts = [
186
+ shortDate,
187
+ options.region,
188
+ options.service,
189
+ 'aws4_request',
190
+ ]
191
+ for (const part of scopeParts) {
192
+ signingKey = await hmac(signingKey, part)
193
+ }
194
+ const signature = toHex(await hmac(signingKey, stringToSign))
195
+
196
+ headers.set(
197
+ 'authorization',
198
+ `AWS4-HMAC-SHA256 Credential=${credentials.accessKeyId}/${scope}, ` +
199
+ `SignedHeaders=${signedHeaders}, Signature=${signature}`,
200
+ )
201
+ return headers
202
+ }