cloudflare / workerd

The JavaScript / Wasm runtime that powers Cloudflare Workers
https://blog.cloudflare.com/workerd-open-source-workers-runtime/
Apache License 2.0
6.08k stars 289 forks source link

Unexpected change in R2Checksums types #2211

Closed Cherry closed 3 months ago

Cherry commented 3 months ago

Previously (workers-types@4.20240529.0), R2 checksums were defined like this:

declare interface R2Checksums {
  readonly md5?: ArrayBuffer;
  readonly sha1?: ArrayBuffer;
  readonly sha256?: ArrayBuffer;
  readonly sha384?: ArrayBuffer;
  readonly sha512?: ArrayBuffer;
  toJSON(): R2StringChecksums;
}

which meant code like this (pseudo) was perfectly valid:

const file = await env.MY_BUCKET.get('file.txt');
let checksum: ArrayBuffer | null = null;
if(file?.checksums?.md5){
    checksum = file.checksums.md5.slice(0, 8); // not really how this would work, but illustrates the change
}

Now (workers-types@4.20240603.0), it's defined like this:

declare interface R2Checksums {
  readonly md5?: ArrayBufferView;
  readonly sha1?: ArrayBufferView;
  readonly sha256?: ArrayBufferView;
  readonly sha384?: ArrayBufferView;
  readonly sha512?: ArrayBufferView;
  toJSON(): R2StringChecksums;
}

which implies you have to do .buffer on each of the checksums to access it, like:

checksum = file.checksums.md5.buffer.slice(0, 8);

but this then throws a TypeError: TypeError: Cannot read properties of undefined (reading 'slice')

I suspect this was caused by https://github.com/cloudflare/workerd/pull/2201. cc @jasnell

jasnell commented 3 months ago

That's odd. Will add a type override. Those should still be ArrayBuffer

jasnell commented 3 months ago

Ok, fixed merged in master. Should appear in the next release.