Skip to content

Commit 80975ef

Browse files
committed
Reviewer comments
1 parent 769ddbe commit 80975ef

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

src/cloudflare/internal/images-api.ts

+32-35
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ class TransformationResultImpl implements ImageTransformationResult {
4343
public image(
4444
options?: ImageTransformationOutputOptions
4545
): ReadableStream<Uint8Array> {
46-
let stream = this.bindingsResponse.body || new ReadableStream();
46+
const stream = this.bindingsResponse.body || new Blob().stream();
4747

48-
if (options?.encoding === 'base64') {
49-
stream = stream.pipeThrough(createBase64EncoderTransformStream());
50-
}
51-
52-
return stream;
48+
return options?.encoding === 'base64'
49+
? stream.pipeThrough(createBase64EncoderTransformStream())
50+
: stream;
5351
}
5452

5553
public response(): Response {
@@ -216,10 +214,10 @@ class ImagesBindingImpl implements ImagesBinding {
216214
): Promise<ImageInfoResponse> {
217215
const body = new StreamableFormData();
218216

219-
let decodedStream = stream;
220-
if (options?.encoding === 'base64') {
221-
decodedStream = stream.pipeThrough(createBase64DecoderTransformStream());
222-
}
217+
const decodedStream =
218+
options?.encoding === 'base64'
219+
? stream.pipeThrough(createBase64DecoderTransformStream())
220+
: stream;
223221

224222
body.append('image', decodedStream, { type: 'file' });
225223

@@ -254,11 +252,10 @@ class ImagesBindingImpl implements ImagesBinding {
254252
stream: ReadableStream<Uint8Array>,
255253
options?: ImageInputOptions
256254
): ImageTransformer {
257-
let decodedStream = stream;
258-
259-
if (options?.encoding === 'base64') {
260-
decodedStream = stream.pipeThrough(createBase64DecoderTransformStream());
261-
}
255+
const decodedStream =
256+
options?.encoding === 'base64'
257+
? stream.pipeThrough(createBase64DecoderTransformStream())
258+
: stream;
262259

263260
return new ImageTransformerImpl(this.fetcher, decodedStream);
264261
}
@@ -334,13 +331,15 @@ function concatUint8Arrays(a: Uint8Array, b: Uint8Array): Uint8Array {
334331
return result;
335332
}
336333

337-
class Base64Error extends Error {
338-
public constructor(cause: unknown) {
339-
if (cause instanceof Error) {
340-
super(`base64 error: ${cause.message}`, { cause });
341-
} else {
342-
super('unknown base64 error');
343-
}
334+
function base64Error(cause: unknown): Error {
335+
// @ts-expect-error: Error.isError seems to be missing from types?
336+
const isError: (_: unknown) => boolean = Error.isError; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
337+
338+
if (isError(cause)) {
339+
const e = cause as Error;
340+
return new Error(`base64 error: ${e.message}`, { cause });
341+
} else {
342+
return new Error('unknown base64 error');
344343
}
345344
}
346345

@@ -373,28 +372,28 @@ function createBase64EncoderTransformStream(
373372

374373
while (currentData.length - offset >= maxEncodeChunkSize) {
375374
const sliceToEnd = offset + maxEncodeChunkSize;
376-
const processChunk = currentData.slice(offset, sliceToEnd);
375+
const processChunk = currentData.subarray(offset, sliceToEnd);
377376
offset = sliceToEnd;
378377

379378
try {
380379
controller.enqueue(toBase64(processChunk));
381380
} catch (error) {
382-
controller.error(new Base64Error(error));
381+
controller.error(base64Error(error));
383382
buffer = null;
384383
return;
385384
}
386385
}
387386

388-
buffer = offset < currentData.length ? currentData.slice(offset) : null;
387+
buffer =
388+
offset < currentData.length ? currentData.subarray(offset) : null;
389389
},
390390

391391
flush(controller): void {
392392
if (buffer && buffer.length > 0) {
393393
try {
394394
controller.enqueue(toBase64(buffer));
395395
} catch (error) {
396-
const errMsg = error instanceof Error ? error.message : String(error);
397-
controller.error(new Error(`base64 encoding error: ${errMsg}`));
396+
controller.error(base64Error(error));
398397
}
399398
}
400399
buffer = null;
@@ -430,7 +429,7 @@ function createBase64DecoderTransformStream(
430429
}
431430
return true;
432431
} catch (error) {
433-
controller.error(new Base64Error(error));
432+
controller.error(base64Error(error));
434433
return false;
435434
}
436435
};
@@ -442,10 +441,10 @@ function createBase64DecoderTransformStream(
442441
: chunk;
443442

444443
while (base64Buffer && base64Buffer.length >= maxChunkSize) {
445-
const base64SegmentBytes = base64Buffer.slice(0, maxChunkSize);
444+
const base64SegmentBytes = base64Buffer.subarray(0, maxChunkSize);
446445
base64Buffer =
447446
base64Buffer.length > maxChunkSize
448-
? base64Buffer.slice(maxChunkSize)
447+
? base64Buffer.subarray(maxChunkSize)
449448
: null;
450449

451450
if (!decodeAndEnqueueSegment(base64SegmentBytes, controller)) {
@@ -458,13 +457,13 @@ function createBase64DecoderTransformStream(
458457
Math.floor(base64Buffer?.length ?? 0 / 4) * 4;
459458
if (remainingProcessableLength > 0) {
460459
if (base64Buffer) {
461-
const base64SegmentBytes = base64Buffer.slice(
460+
const base64SegmentBytes = base64Buffer.subarray(
462461
0,
463462
remainingProcessableLength
464463
);
465464
base64Buffer =
466465
base64Buffer.length > remainingProcessableLength
467-
? base64Buffer.slice(remainingProcessableLength)
466+
? base64Buffer.subarray(remainingProcessableLength)
468467
: null;
469468

470469
if (!decodeAndEnqueueSegment(base64SegmentBytes, controller)) {
@@ -477,9 +476,7 @@ function createBase64DecoderTransformStream(
477476

478477
flush(controller): void {
479478
if (base64Buffer && base64Buffer.length > 0) {
480-
if (!decodeAndEnqueueSegment(base64Buffer, controller)) {
481-
// Error already handled within the helper
482-
}
479+
decodeAndEnqueueSegment(base64Buffer, controller);
483480
}
484481
base64Buffer = null;
485482
},

0 commit comments

Comments
 (0)