|
6 | 6 | createBase64DecoderTransformStream, |
7 | 7 | createBase64EncoderTransformStream, |
8 | 8 | } from 'cloudflare-internal:streaming-base64'; |
| 9 | +import { withSpan } from 'cloudflare-internal:tracing-helpers'; |
9 | 10 |
|
10 | 11 | type Fetcher = { |
11 | 12 | fetch: typeof fetch; |
@@ -233,40 +234,49 @@ class ImagesBindingImpl implements ImagesBinding { |
233 | 234 | stream: ReadableStream<Uint8Array>, |
234 | 235 | options?: ImageInputOptions |
235 | 236 | ): Promise<ImageInfoResponse> { |
236 | | - const body = new StreamableFormData(); |
237 | | - |
238 | | - const decodedStream = |
239 | | - options?.encoding === 'base64' |
240 | | - ? stream.pipeThrough(createBase64DecoderTransformStream()) |
241 | | - : stream; |
242 | | - |
243 | | - body.append('image', decodedStream, { type: 'file' }); |
| 237 | + return await withSpan('images_info', async (span) => { |
| 238 | + const body = new StreamableFormData(); |
| 239 | + |
| 240 | + const decodedStream = |
| 241 | + options?.encoding === 'base64' |
| 242 | + ? stream.pipeThrough(createBase64DecoderTransformStream()) |
| 243 | + : stream; |
| 244 | + |
| 245 | + span.setAttribute('cloudflare.images.info.encoding', options?.encoding); |
| 246 | + |
| 247 | + body.append('image', decodedStream, { type: 'file' }); |
| 248 | + |
| 249 | + const response = await this.#fetcher.fetch( |
| 250 | + 'https://js.images.cloudflare.com/info', |
| 251 | + { |
| 252 | + method: 'POST', |
| 253 | + headers: { |
| 254 | + 'content-type': body.contentType(), |
| 255 | + }, |
| 256 | + body: body.stream(), |
| 257 | + } |
| 258 | + ); |
244 | 259 |
|
245 | | - const response = await this.#fetcher.fetch( |
246 | | - 'https://js.images.cloudflare.com/info', |
247 | | - { |
248 | | - method: 'POST', |
249 | | - headers: { |
250 | | - 'content-type': body.contentType(), |
251 | | - }, |
252 | | - body: body.stream(), |
253 | | - } |
254 | | - ); |
| 260 | + await throwErrorIfErrorResponse('INFO', response); |
255 | 261 |
|
256 | | - await throwErrorIfErrorResponse('INFO', response); |
| 262 | + const r = (await response.json()) as RawInfoResponse; |
257 | 263 |
|
258 | | - const r = (await response.json()) as RawInfoResponse; |
| 264 | + span.setAttribute('cloudflare.images.info.format', r.format); |
259 | 265 |
|
260 | | - if ('file_size' in r) { |
261 | | - return { |
262 | | - fileSize: r.file_size, |
263 | | - width: r.width, |
264 | | - height: r.height, |
265 | | - format: r.format, |
266 | | - }; |
267 | | - } |
| 266 | + if ('file_size' in r) { |
| 267 | + span.setAttribute('cloudflare.images.info.file_size', r.file_size); |
| 268 | + span.setAttribute('cloudflare.images.info.width', r.width); |
| 269 | + span.setAttribute('cloudflare.images.info.height', r.height); |
| 270 | + return { |
| 271 | + fileSize: r.file_size, |
| 272 | + width: r.width, |
| 273 | + height: r.height, |
| 274 | + format: r.format, |
| 275 | + }; |
| 276 | + } |
268 | 277 |
|
269 | | - return r; |
| 278 | + return r; |
| 279 | + }); |
270 | 280 | } |
271 | 281 |
|
272 | 282 | input( |
|
0 commit comments