【发布时间】:2021-12-25 16:47:10
【问题描述】:
我正在尝试扩展 File 类并覆盖 stream():
class MyFile extends File {
constructor (name, options) {
super(name, options)
this.customField = options.customField
}
stream () {
return new ReadableStream({
async start(controller) {
/* implementation here */
},
async pull(controller) {
/* implementation here */
},
async cancel() {
/* implementation here */
}
})
}
async text () {
// implementation here
}
async arrayBuffer () {
// implementation here
}
/* Attempt 2
get [Symbol.toStringTag] () {
return 'Blob'
}
static [Symbol.hasInstance] (object) {
return true
}
*/
}
当使用FileReader.readAsArrayBuffer() 或FileReader.readAsText() 和MyFile 时,它不会调用我的任何方法,而是只返回一个空的缓冲区/文本。然后我尝试删除extends File并在Attempt 2中添加代码,但现在抛出错误:Argument 1 ('blob') to FileReader.readAsArrayBuffer must be an instance of Blob。
【问题讨论】:
-
从您显示的代码中,无法判断。另外,我没有看到对
stream()的呼叫。 -
"我正在尝试扩展
File类并覆盖stream()" - 为什么?你想做什么?你的信息流应该做些什么不同的事情? -
@Bergi 我正在编写一个实现文件系统 API 的 Safari 扩展,并且规范说我应该返回一个
File。我无法控制网站如何处理这个File对象。 -
@osy 好的,但是您的扩展程序使用哪些文件以及如何生成它们?你不能只构造一个具有预期内容的新文件对象吗?
-
@Bergi 所以 iOS 根本不支持文件系统 API,我的扩展充当了原生文件读取 API 和 JS API 之间的桥梁。是的,我可以读取整个内容并返回一个 blob,但如果用户选择 4GB 文件怎么办?文件系统 API 的一大优势是您可以使用流 API 进行部分读取。