Skip to content

Unexpected change in R2Checksums types #2211

@Cherry

Description

@Cherry

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 #2201. cc @jasnell

Metadata

Metadata

Labels

typesRelated to @cloudflare/workers-types

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions