Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/workerd/api/blob.c++
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <workerd/io/observer.h>
#include <workerd/util/mimetype.h>

#include <cmath>

namespace workerd::api {

namespace {
Expand Down Expand Up @@ -182,7 +184,7 @@ kj::String normalizeType(kj::String type) {
// type contains non-printable-ASCII characters we should discard it, and otherwise we should
// lower-case it.
for (char& c: type) {
if (static_cast<signed char>(c) < 0x20) {
if (auto u = static_cast<kj::byte>(c); u < 0x20 || u > 0x7e) {
// Throw it away.
return nullptr;
} else if ('A' <= c && c <= 'Z') {
Expand All @@ -193,6 +195,18 @@ kj::String normalizeType(kj::String type) {
return kj::mv(type);
}

// Converts a slice() start or end argument to an offset into a Blob of the given size, per
// https://w3c.github.io/FileAPI/#slice-blob. The argument is a WebIDL [Clamp] long long,
// so it is rounded to the nearest integer with ties to even, and NaN becomes 0. A negative
// value counts back from the end. Clamping to [0, size] covers the long long range, which
// a Blob can never exceed.
size_t relativeOffset(double value, double size) {
if (std::isnan(value)) return 0;
value = std::nearbyint(value);
if (value < 0) value += size;
return static_cast<size_t>(kj::max(0.0, kj::min(value, size)));
}

} // namespace

Blob::Blob(kj::String type): ownData(Empty{}), type(kj::mv(type)) {}
Expand Down Expand Up @@ -251,8 +265,8 @@ kj::ArrayPtr<const byte> Blob::getData(jsg::Lock& js) const {
}

jsg::Ref<Blob> Blob::slice(jsg::Lock& js,
jsg::Optional<int> maybeStart,
jsg::Optional<int> maybeEnd,
jsg::Optional<double> maybeStart,
jsg::Optional<double> maybeEnd,
jsg::Optional<kj::String> type) {

auto normalizedType = normalizeType(kj::mv(type).orDefault(nullptr));
Expand All @@ -262,21 +276,9 @@ jsg::Ref<Blob> Blob::slice(jsg::Lock& js,
return js.alloc<Blob>(kj::mv(normalizedType));
}

int start = maybeStart.orDefault(0);
int end = maybeEnd.orDefault(bytes.size());

if (start < 0) {
// Negative value interpreted as offset from end.
start += bytes.size();
}
if (end < 0) {
// Negative value interpreted as offset from end.
end += bytes.size();
}

// Clamp start and end to range.
start = kj::max(0, kj::min(start, static_cast<int>(bytes.size())));
end = kj::max(start, kj::min(end, static_cast<int>(bytes.size())));
double size = bytes.size();
size_t start = relativeOffset(maybeStart.orDefault(0), size);
size_t end = kj::max(start, relativeOffset(maybeEnd.orDefault(size), size));

// We run with KJ_IREQUIRE checks enabled in production, which will catch
// out of bounds start/end ... but since we're clamping them above, this
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Blob: public jsg::Object {
}

jsg::Ref<Blob> slice(jsg::Lock& js,
jsg::Optional<int> start,
jsg::Optional<int> end,
jsg::Optional<double> start,
jsg::Optional<double> end,
jsg::Optional<kj::String> type);

// Each of the consumption methods (arrayBuffer, bytes, text) create copies of
Expand Down
21 changes: 21 additions & 0 deletions src/workerd/api/tests/blob-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ export const test1 = {
strictEqual(new Blob([], { type: 'FoO\u0019/bAr' }).type, '');
strictEqual(new Blob([], { type: 'FoO\u0020/bAr' }).type, 'foo /bar');
strictEqual(new Blob([], { type: 'FoO\u007e/bAr' }).type, 'foo\u007e/bar');
strictEqual(new Blob([], { type: 'FoO\u007f/bAr' }).type, '');
strictEqual(new Blob([], { type: 'FoO\u0080/bAr' }).type, '');
strictEqual(new File([], 'foo.txt', { type: 'FoO/bAr' }).type, 'foo/bar');
strictEqual(blob2.slice(1, 2, 'FoO/bAr').type, 'foo/bar');
strictEqual(blob2.slice(1, 2, 'FoO\u007f/bAr').type, '');
},
};

Expand Down Expand Up @@ -382,3 +384,22 @@ export const endings = {
}
},
};

export const sliceClamp = {
async test() {
const blob = new Blob(['abcd']);
const slice = async (...args) => await blob.slice(...args).text();

strictEqual(await slice(1.5, 2.5), '');
strictEqual(await slice(0.5, 3.5), 'abcd');
strictEqual(await slice(-1.5), 'cd');
strictEqual(await slice(NaN, NaN), '');
strictEqual(await slice(0, Infinity), 'abcd');
strictEqual(await slice(Infinity), '');
strictEqual(await slice(-Infinity, 2), 'ab');
strictEqual(await slice(1, 2 ** 32), 'bcd');
strictEqual(await slice(-(2 ** 53)), 'abcd');
strictEqual(await slice(2 ** 64), '');
strictEqual(await slice('1', '3'), 'bc');
},
};
27 changes: 4 additions & 23 deletions src/wpt/FileAPI-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export default {
'instead of as a WebIDL sequence<BlobPart> iterated through @@iterator. So iterables are rejected, ' +
"a length getter isn't called, and changes made to the array during conversion aren't reflected. " +
'Transfer lists are not supported by structuredClone. Blob::Options in blob.h declares type before endings, ' +
'so the options are read in that order rather than lexicographically. ' +
'normalizeType() in blob.c++ only discards types containing characters below U+0020, so U+007F is kept.',
'so the options are read in that order rather than lexicographically.',
expectedFailures: [
'A plain object with @@iterator should be treated as a sequence for the blobParts argument.',
'A plain object with custom @@iterator should be treated as a sequence for the blobParts argument.',
Expand All @@ -47,7 +46,6 @@ export default {
'Changes to the blobParts array should be reflected in the returned Blob (unshift).',
'Passing a FrozenArray as the blobParts array should work (FrozenArray<MessagePort>).',
'options properties should be accessed in lexicographic order.',
'Blob with type "image/gif;\x7F"',
],
},
'blob/Blob-in-worker.worker.js': {
Expand All @@ -58,20 +56,7 @@ export default {
},
'blob/Blob-newobject.any.js': {},
'blob/Blob-slice-overflow.any.js': {},
'blob/Blob-slice.any.js': {
comment:
'Blob.slice() takes int start/end (blob.h) and truncates fractional values; the spec uses [Clamp] long long, which rounds half to even. ' +
'normalizeType() in blob.c++ only discards types containing characters below U+0020, so U+007F is kept.',
expectedFailures: [
'Slicing test: slice (1,1).',
'Slicing test: slice (1,3).',
'Slicing test: slice (1,5).',
'Slicing test: slice (1,7).',
'Slicing test: slice (1,8).',
'Slicing test: slice (1,9).',
'Invalid contentType ("te\x7Fxt/plain")',
],
},
'blob/Blob-slice.any.js': {},
'blob/Blob-stream.any.js': {},
'blob/Blob-text.any.js': {
comment:
Expand All @@ -89,12 +74,8 @@ export default {
'file/File-constructor-endings.any.js': {},
'file/File-constructor.any.js': {
comment:
'fileBits is converted as kj::Array rather than a WebIDL sequence, so a custom @@iterator is ignored. ' +
'normalizeType() in blob.c++ only discards types containing characters below U+0020, so U+007F is kept.',
expectedFailures: [
'Custom @@iterator',
'Using type in File constructor: ascii/nonprintable\x7F',
],
'fileBits is converted as kj::Array rather than a WebIDL sequence, so a custom @@iterator is ignored.',
expectedFailures: ['Custom @@iterator'],
},
'file/Worker-read-file-constructor.worker.js': {
comment: 'FileReader is not implemented',
Expand Down
Loading