| 322 | } |
| 323 | |
| 324 | func (r *arrayReader) Read(p []byte) (n int, err error) { |
| 325 | if r.err != nil { |
| 326 | return 0, r.err |
| 327 | } |
| 328 | if !r.read { |
| 329 | r.read = true |
| 330 | var ( |
| 331 | bCh = make(chan []byte, 1) |
| 332 | errCh = make(chan error, 1) |
| 333 | ) |
| 334 | success := js.FuncOf(func(this js.Value, args []js.Value) any { |
| 335 | // Wrap the input ArrayBuffer with a Uint8Array |
| 336 | uint8arrayWrapper := uint8Array.New(args[0]) |
| 337 | value := make([]byte, uint8arrayWrapper.Get("byteLength").Int()) |
| 338 | js.CopyBytesToGo(value, uint8arrayWrapper) |
| 339 | bCh <- value |
| 340 | return nil |
| 341 | }) |
| 342 | defer success.Release() |
| 343 | failure := js.FuncOf(func(this js.Value, args []js.Value) any { |
| 344 | // Assumes it's a TypeError. See |
| 345 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError |
| 346 | // for more information on this type. |
| 347 | // See https://fetch.spec.whatwg.org/#concept-body-consume-body for reasons this might error. |
| 348 | errCh <- errors.New(args[0].Get("message").String()) |
| 349 | return nil |
| 350 | }) |
| 351 | defer failure.Release() |
| 352 | r.arrayPromise.Call("then", success, failure) |
| 353 | select { |
| 354 | case b := <-bCh: |
| 355 | r.pending = b |
| 356 | case err := <-errCh: |
| 357 | return 0, err |
| 358 | } |
| 359 | } |
| 360 | if len(r.pending) == 0 { |
| 361 | return 0, io.EOF |
| 362 | } |
| 363 | n = copy(p, r.pending) |
| 364 | r.pending = r.pending[n:] |
| 365 | return n, nil |
| 366 | } |
| 367 | |
| 368 | func (r *arrayReader) Close() error { |
| 369 | if r.err == nil { |