Skip to content

Commit c491648

Browse files
committed
implement various Blob methods
Support for `stream`, `text` and `bytes`.
1 parent 1085950 commit c491648

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/browser/file/Blob.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const is_windows = builtin.os.tag == .windows;
2525
const Page = @import("../page.zig").Page;
2626
const js = @import("../js/js.zig");
2727

28+
const ReadableStream = @import("../streams/ReadableStream.zig");
29+
2830
/// https://w3c.github.io/FileAPI/#blob-section
2931
/// https://developer.mozilla.org/en-US/docs/Web/API/Blob
3032
const Blob = @This();
@@ -131,6 +133,38 @@ fn writeBlobParts(
131133
}
132134
}
133135

136+
// TODO: Blob.arrayBuffer.
137+
// https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer
138+
139+
/// Returns a ReadableStream which upon reading returns the data
140+
/// contained within the Blob.
141+
pub fn _stream(self: *const Blob, page: *Page) !*ReadableStream {
142+
const stream = try ReadableStream.constructor(null, null, page);
143+
try stream.queue.append(page.arena, .{
144+
.uint8array = .{ .values = self.slice },
145+
});
146+
return stream;
147+
}
148+
149+
/// Returns a Promise that resolves with a string containing
150+
/// the contents of the blob, interpreted as UTF-8.
151+
pub fn _text(self: *const Blob, page: *Page) !js.Promise {
152+
const resolver = page.js.createPromiseResolver(.none);
153+
try resolver.resolve(self.slice);
154+
return resolver.promise();
155+
}
156+
157+
/// Extension to Blob; works on Firefox and Safari.
158+
/// https://developer.mozilla.org/en-US/docs/Web/API/Blob/bytes
159+
/// Returns a Promise that resolves with a Uint8Array containing
160+
/// the contents of the blob as an array of bytes.
161+
pub fn _bytes(self: *const Blob, page: *Page) !js.Promise {
162+
const resolver = page.js.createPromiseResolver(.none);
163+
try resolver.resolve(js.TypedArray(u8){ .values = self.slice });
164+
return resolver.promise();
165+
}
166+
167+
/// Returns the size of the Blob in bytes.
134168
pub fn get_size(self: *const Blob) usize {
135169
return self.slice.len;
136170
}

0 commit comments

Comments
 (0)