简体   繁体   English

如何ES6代理ArrayBuffer或Uint8Array?

[英]How to ES6 Proxy an ArrayBuffer or Uint8Array?

These work: 这些工作:

crypto.subtle.digest('SHA-512', new Uint8Array([0]))
crypto.subtle.digest('SHA-512', new Uint8Array([0]).buffer)

These don't: 这些不是:

crypto.subtle.digest('SHA-512', new Proxy(new Uint8Array([0]),{}))
crypto.subtle.digest('SHA-512', new Proxy(new Uint8Array([0]).buffer,{})

Error: 错误:

Failed to execute 'digest' on 'SubtleCrypto': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'

instanceof Uint8Array and instanceof ArrayBuffer return true in both cases. 在两种情况下, instanceof Uint8Arrayinstanceof ArrayBuffer返回true。

digest is specified by its IDL interface to only accept a BufferSource , which is either an ArrayBufferView or an ArrayBuffer . digest由其IDL接口指定为仅接受BufferSource ,它是ArrayBufferViewArrayBuffer This IDL-level typing indicates that a correct implementation will categorically reject any inputs that doesn't have the correct internal type. 此IDL级别键入表示正确的实现将断然拒绝任何没有正确内部类型的输入。

Any tricks you might want to use a Proxy for simply aren't going to work on digest . 你可能想要使用Proxy任何技巧都不适用于digest Instead, you could do proxy tricks to get the exact ArrayBuffer you want immediately before you pass in your data to digest . 相反,您可以执行代理技巧,以便在传递数据以进行digest之前立即获取所需的确切ArrayBuffer

For example, here's a proxy that fakes a buffer that differs from the buffer on its internal object. 例如,这是一个伪造buffer的代理,该buffer与其内部对象上的buffer不同。 The buffer is genuine, so it can be passed into digest , but it was created by Proxy magic: buffer是真的,所以它可以传递到digest ,但它是由Proxy magic创建的:

var proxy = new Proxy(new Uint8Array([0]), {
                          get:function(obj, prop) {
                              if(prop=="buffer"){ return new Uint8Array([42]).buffer }
                              else { return obj[prop]; }
                          }
            });
crypto.subtle.digest('SHA-512', proxy.buffer)

If it is impossible to produce the buffer (for example if it is too big to fit in RAM) you would currently have to rely on something other than SubtleCrypto. 如果无法生成缓冲区(例如,如果它太大而无法放入RAM),那么您目前必须依赖SubtleCrypto以外的其他东西。

This seems like a great point to raise with the W3C, eg, to support an update mechanism to iteratively collect input. 这似乎是与W3C一起提出的一个重点,例如,支持迭代收集输入的update机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM