mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 14:53:44 +00:00
feat: replace gblob with blob http transport (#1407)
This commit is contained in:
34 files changed
+5194
-2948
No files matched your search
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,285 +0,0 @@
|
|||||||
# gblob Protocol
|
|
||||||
|
|
||||||
## Background
|
|
||||||
|
|
||||||
Gopeed extensions can resolve a download target into something that is not a normal remote URL.
|
|
||||||
Typical examples are:
|
|
||||||
|
|
||||||
- text generated by an extension
|
|
||||||
- binary data assembled in JavaScript
|
|
||||||
- a stream produced incrementally inside the extension runtime
|
|
||||||
|
|
||||||
Returning a plain `http` or `https` URL is not enough for these cases.
|
|
||||||
The `gblob` protocol provides an internal object-URL style transport so extensions can hand generated content to the downloader in a browser-like way.
|
|
||||||
|
|
||||||
The extension-facing model is intentionally close to the browser:
|
|
||||||
|
|
||||||
- `URL.createObjectURL(new Blob(...))`
|
|
||||||
- `URL.createObjectURL(readableStream)`
|
|
||||||
- `URL.createObjectURL(openReadable)`
|
|
||||||
- `URL.revokeObjectURL(url)`
|
|
||||||
|
|
||||||
The returned URL looks like:
|
|
||||||
|
|
||||||
```text
|
|
||||||
gblob:<id>
|
|
||||||
```
|
|
||||||
|
|
||||||
`gblob` is a process-local protocol. The source only exists inside the current Gopeed process.
|
|
||||||
|
|
||||||
## Design Goals
|
|
||||||
|
|
||||||
- make extension-generated data download as a normal task
|
|
||||||
- support eager `Blob` data, one-shot `ReadableStream` data, and resumable opener functions
|
|
||||||
- avoid large in-memory buffering by spooling to disk
|
|
||||||
- keep pause/continue behavior intuitive
|
|
||||||
- allow an extension to recreate a new `gblob` URL after source expiration
|
|
||||||
|
|
||||||
## High-Level Architecture
|
|
||||||
|
|
||||||
The implementation has two layers in this directory:
|
|
||||||
|
|
||||||
- [`registry.go`](./registry.go): process-local source registry and spool-file manager
|
|
||||||
- [`fetcher.go`](./fetcher.go): downloader fetcher implementation for the `gblob` scheme
|
|
||||||
|
|
||||||
The end-to-end flow is:
|
|
||||||
|
|
||||||
1. JavaScript creates a `Blob`, `ReadableStream`, or opener-function object URL.
|
|
||||||
2. Go registers a `gblob` source and assigns it a unique `gblob:<id>` URL.
|
|
||||||
3. Source bytes are written into a local spool file.
|
|
||||||
4. The downloader resolves the `gblob` URL through the `gblob` fetcher.
|
|
||||||
5. The fetcher copies bytes from the spool file into the final download target.
|
|
||||||
|
|
||||||
## Registry
|
|
||||||
|
|
||||||
The registry owns all active `gblob` sources for the current process.
|
|
||||||
|
|
||||||
Each source tracks:
|
|
||||||
|
|
||||||
- source id and `gblob` URL
|
|
||||||
- local spool-file path
|
|
||||||
- source type: `blob` or `writable_stream`
|
|
||||||
- written byte count
|
|
||||||
- source state: `open`, `closed`, `aborted`, `failed`
|
|
||||||
- revoke state
|
|
||||||
- pin count
|
|
||||||
- optional extension session reference
|
|
||||||
|
|
||||||
Key behaviors:
|
|
||||||
|
|
||||||
- `Blob` sources are written once and immediately closed.
|
|
||||||
- `ReadableStream` sources are bridged into the internal writable-source pipeline.
|
|
||||||
- opener-function sources are opened lazily and can be reopened from an offset.
|
|
||||||
- readers wait on a per-source notification channel when no new bytes are available yet.
|
|
||||||
- revocation only prevents future use; cleanup happens when the source is no longer pinned.
|
|
||||||
|
|
||||||
## Fetcher
|
|
||||||
|
|
||||||
The fetcher treats `gblob` as a local file-backed download source.
|
|
||||||
|
|
||||||
### Resolve
|
|
||||||
|
|
||||||
`Resolve()` looks up the source in the registry and builds a single-file `Resource`.
|
|
||||||
|
|
||||||
Behavior differences by source type:
|
|
||||||
|
|
||||||
- `Blob`: resolved size is the current written size
|
|
||||||
- `ReadableStream` / opener function: resolved size is whatever the extension declared; if unknown, it stays `0` until download completion updates the final size
|
|
||||||
|
|
||||||
`Range` is `true` only when the extension provides a resumable opener and marks the resource as ranged.
|
|
||||||
|
|
||||||
### Start
|
|
||||||
|
|
||||||
`Start()` opens the final target file and reads from the source spool file.
|
|
||||||
|
|
||||||
There are two resume modes:
|
|
||||||
|
|
||||||
- same `gblob` id: continue from the current target-file size
|
|
||||||
- different `gblob` id: truncate the target file and restart from byte `0`
|
|
||||||
|
|
||||||
The fetcher persists the last started source id so it can distinguish these two cases across pause/continue and task restore.
|
|
||||||
|
|
||||||
### Pause / Continue
|
|
||||||
|
|
||||||
`Pause()` stops the current copy loop by canceling the reader side.
|
|
||||||
It does not abort the `gblob` source.
|
|
||||||
|
|
||||||
If the source id stays the same, `Continue()` resumes from the existing target-file offset.
|
|
||||||
This is how normal in-process `Pause -> Continue` works.
|
|
||||||
|
|
||||||
### Source Replacement
|
|
||||||
|
|
||||||
If an extension replaces `req.url` with a new `gblob:<new-id>` before the next start, the fetcher detects the id change and restarts from scratch.
|
|
||||||
|
|
||||||
This behavior is important for recovery flows such as:
|
|
||||||
|
|
||||||
- original `gblob` source expired
|
|
||||||
- extension recreates a new `gblob` URL in `onError`
|
|
||||||
- task continues with the new source
|
|
||||||
|
|
||||||
## Lifecycle Notes
|
|
||||||
|
|
||||||
### In-process pause and continue
|
|
||||||
|
|
||||||
For the same `gblob` id:
|
|
||||||
|
|
||||||
- the partially downloaded output file is kept
|
|
||||||
- the fetcher resumes from the current file size
|
|
||||||
- opener-function sources can be reopened from the current offset
|
|
||||||
|
|
||||||
For non-reopenable one-shot `ReadableStream` sources:
|
|
||||||
|
|
||||||
- a normal in-process pause/continue only works while the same in-memory source is still alive
|
|
||||||
- they are not suitable for cross-session or reconstructable range resume
|
|
||||||
|
|
||||||
### Process restart
|
|
||||||
|
|
||||||
`gblob` sources are not persisted across process restarts.
|
|
||||||
An unfinished `gblob` task is restored without a live source behind its old URL.
|
|
||||||
|
|
||||||
The intended recovery pattern is:
|
|
||||||
|
|
||||||
1. user clicks continue
|
|
||||||
2. the old `gblob` source fails
|
|
||||||
3. extension `onError` runs
|
|
||||||
4. extension recreates a new `gblob` URL from `req.rawUrl`
|
|
||||||
5. extension updates `ctx.task.meta.req.url`
|
|
||||||
6. extension calls `ctx.task.continue()`
|
|
||||||
|
|
||||||
Because the new URL uses a different `gblob` id, the fetcher restarts from byte `0`.
|
|
||||||
|
|
||||||
## Why `rawUrl` Exists
|
|
||||||
|
|
||||||
When an extension resolves a request into `gblob:<id>`, the original site URL would otherwise be lost.
|
|
||||||
`Request.RawURL` keeps the original creation URL so lifecycle events can still reason about the original target.
|
|
||||||
|
|
||||||
Extension event URL matching prefers `req.rawUrl` when present.
|
|
||||||
|
|
||||||
This is especially useful for `onStart`, `onError`, and `onDone` on `gblob` tasks.
|
|
||||||
|
|
||||||
## Extension Usage
|
|
||||||
|
|
||||||
### 1. Create a `Blob` object URL
|
|
||||||
|
|
||||||
```js
|
|
||||||
gopeed.events.onResolve(async function (ctx) {
|
|
||||||
const blob = new Blob(["hello world"], { type: "text/plain" });
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "example",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "hello.txt",
|
|
||||||
size: 11,
|
|
||||||
req: { url }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. Create a one-shot `ReadableStream` object URL
|
|
||||||
|
|
||||||
```js
|
|
||||||
gopeed.events.onResolve(async function (ctx) {
|
|
||||||
const encoder = new TextEncoder();
|
|
||||||
const stream = new ReadableStream({
|
|
||||||
start(controller) {
|
|
||||||
controller.enqueue(encoder.encode("line 1\n"));
|
|
||||||
controller.enqueue(encoder.encode("line 2\n"));
|
|
||||||
controller.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const url = URL.createObjectURL(stream);
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "stream-example",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "stream.txt",
|
|
||||||
req: { url }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Create a resumable opener object URL
|
|
||||||
|
|
||||||
```js
|
|
||||||
gopeed.events.onResolve(async function (ctx) {
|
|
||||||
const target = "https://example.com/file.bin";
|
|
||||||
|
|
||||||
const openReadable = async (offset) => {
|
|
||||||
const headers = {};
|
|
||||||
if (offset > 0) {
|
|
||||||
headers.Range = `bytes=${offset}-`;
|
|
||||||
}
|
|
||||||
const response = await fetch(target, { headers });
|
|
||||||
if (response.status !== 200 || response.status !== 206) {
|
|
||||||
throw new Error(`unexpected response status: ${response.status}, expected ${expectedStatus}`);
|
|
||||||
}
|
|
||||||
if (!response.body) {
|
|
||||||
throw new Error("empty response body");
|
|
||||||
}
|
|
||||||
return response.body;
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "range-example",
|
|
||||||
range: true,
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "file.bin",
|
|
||||||
req: { url: URL.createObjectURL(openReadable) }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
Important:
|
|
||||||
|
|
||||||
- opener functions must return a `ReadableStream`
|
|
||||||
- do not return `response.body.getReader()`
|
|
||||||
- use opener functions for resumable / range-aware sources
|
|
||||||
- validate HTTP status codes explicitly:
|
|
||||||
first request usually expects `200`, resumed range requests usually expect `206`
|
|
||||||
|
|
||||||
### 4. Recover in `onError`
|
|
||||||
|
|
||||||
```js
|
|
||||||
gopeed.events.onError(async function (ctx) {
|
|
||||||
const req = ctx.task.meta.req;
|
|
||||||
req.url = URL.createObjectURL(new Blob(["recovered\n"]));
|
|
||||||
ctx.task.continue();
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
This creates a new `gblob` id, so the downloader restarts from byte `0`.
|
|
||||||
|
|
||||||
## Limitations
|
|
||||||
|
|
||||||
- `gblob` is not a network protocol. Range support only exists when the extension recreates bytes through a resumable opener.
|
|
||||||
- unfinished `gblob` sources do not survive process restarts.
|
|
||||||
- recovery after restart depends on extension logic recreating a new source.
|
|
||||||
- the current restart behavior is full restart on source-id change, not byte-range recovery across recreated sources.
|
|
||||||
- one-shot `ReadableStream` tasks with unknown total size report size `0` until completion updates the final size.
|
|
||||||
|
|
||||||
## Summary
|
|
||||||
|
|
||||||
`gblob` lets Gopeed extensions treat generated content as a first-class download source.
|
|
||||||
The registry handles source lifetime and disk spooling.
|
|
||||||
The fetcher turns those sources into normal download tasks with predictable pause/continue behavior.
|
|
||||||
|
|
||||||
The most important rule is simple:
|
|
||||||
|
|
||||||
- same `gblob` id means resume
|
|
||||||
- different `gblob` id means restart
|
|
||||||
|
|
||||||
And for extension authors:
|
|
||||||
|
|
||||||
- use `ReadableStream` for one-shot streaming content
|
|
||||||
- use opener functions for resumable content
|
|
||||||
- opener functions must return `ReadableStream`, not a reader
|
|
||||||
@@ -1,415 +0,0 @@
|
|||||||
package gblob
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"github.com/GopeedLab/gopeed/internal/controller"
|
|
||||||
"github.com/GopeedLab/gopeed/internal/fetcher"
|
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
|
||||||
)
|
|
||||||
|
|
||||||
type config struct{}
|
|
||||||
|
|
||||||
type Fetcher struct {
|
|
||||||
ctl *controller.Controller
|
|
||||||
registry *Registry
|
|
||||||
meta *fetcher.FetcherMeta
|
|
||||||
doneCh chan error
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
cancel context.CancelFunc
|
|
||||||
runDone chan struct{}
|
|
||||||
paused bool
|
|
||||||
closed bool
|
|
||||||
|
|
||||||
downloaded atomic.Int64
|
|
||||||
lastSourceID string
|
|
||||||
}
|
|
||||||
|
|
||||||
type fetcherData struct {
|
|
||||||
LastSourceID string `json:"lastSourceId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Setup(ctl *controller.Controller) {
|
|
||||||
f.ctl = ctl
|
|
||||||
if f.meta == nil {
|
|
||||||
f.meta = &fetcher.FetcherMeta{}
|
|
||||||
}
|
|
||||||
f.doneCh = make(chan error, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Resolve(req *base.Request, opts *base.Options) error {
|
|
||||||
if f.registry == nil {
|
|
||||||
return ErrSourceNotFound
|
|
||||||
}
|
|
||||||
src, err := f.registry.Get(req.URL)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
f.meta.Req = req
|
|
||||||
f.meta.Opts = opts
|
|
||||||
if f.meta.Opts == nil {
|
|
||||||
f.meta.Opts = &base.Options{}
|
|
||||||
}
|
|
||||||
name := f.meta.Opts.Name
|
|
||||||
if name == "" {
|
|
||||||
name = src.ID
|
|
||||||
}
|
|
||||||
snapshot := src.Snapshot()
|
|
||||||
size := snapshot.Written
|
|
||||||
if snapshot.DeclaredSize > 0 {
|
|
||||||
size = snapshot.DeclaredSize
|
|
||||||
}
|
|
||||||
if src.Type == SourceTypeWritableStream {
|
|
||||||
// Writable streams may continue producing bytes after Resolve,
|
|
||||||
// so treat the total size as unknown until the writer closes.
|
|
||||||
if snapshot.DeclaredSize == 0 {
|
|
||||||
size = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.meta.Res = &base.Resource{
|
|
||||||
Size: size,
|
|
||||||
Range: snapshot.Range,
|
|
||||||
Files: []*base.FileInfo{
|
|
||||||
{
|
|
||||||
Name: name,
|
|
||||||
Path: "",
|
|
||||||
Size: size,
|
|
||||||
Req: req,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Start() error {
|
|
||||||
if f.registry == nil {
|
|
||||||
return ErrSourceNotFound
|
|
||||||
}
|
|
||||||
currentSourceID, err := ParseURL(f.meta.Req.URL)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
f.mu.Lock()
|
|
||||||
if f.runDone != nil {
|
|
||||||
f.mu.Unlock()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
f.paused = false
|
|
||||||
runDone := make(chan struct{})
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
f.cancel = cancel
|
|
||||||
f.runDone = runDone
|
|
||||||
f.mu.Unlock()
|
|
||||||
cleanupStartErr := func(err error) error {
|
|
||||||
f.mu.Lock()
|
|
||||||
if f.runDone == runDone {
|
|
||||||
f.runDone = nil
|
|
||||||
f.cancel = nil
|
|
||||||
}
|
|
||||||
f.mu.Unlock()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
path := f.meta.SingleFilepath()
|
|
||||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
target, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
offset := int64(0)
|
|
||||||
allowResumeAcrossSource := f.meta.Res != nil && f.meta.Res.Range
|
|
||||||
if f.lastSourceID != "" && f.lastSourceID != currentSourceID && !allowResumeAcrossSource {
|
|
||||||
if err := target.Truncate(0); err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
if _, err := target.Seek(0, io.SeekStart); err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
stat, err := target.Stat()
|
|
||||||
if err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
offset = stat.Size()
|
|
||||||
if _, err := target.Seek(offset, io.SeekStart); err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.lastSourceID = currentSourceID
|
|
||||||
f.downloaded.Store(offset)
|
|
||||||
|
|
||||||
sourceOffset := offset
|
|
||||||
src, err := f.registry.Get(f.meta.Req.URL)
|
|
||||||
if err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
snapshot := src.Snapshot()
|
|
||||||
if snapshot.Written == 0 && !snapshot.Started {
|
|
||||||
if f.meta.Res != nil && f.meta.Res.Range && offset > 0 {
|
|
||||||
if err := f.registry.Reopen(f.meta.Req.URL, offset); err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
sourceOffset = 0
|
|
||||||
} else if err := f.registry.StartSource(f.meta.Req.URL); err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
src, err = f.registry.Get(f.meta.Req.URL)
|
|
||||||
if err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
snapshot = src.Snapshot()
|
|
||||||
}
|
|
||||||
if f.meta.Res != nil && f.meta.Res.Range && (snapshot.State == SourceStateAborted || snapshot.State == SourceStateFailed) {
|
|
||||||
if err := f.registry.Reopen(f.meta.Req.URL, offset); err != nil {
|
|
||||||
target.Close()
|
|
||||||
return cleanupStartErr(err)
|
|
||||||
}
|
|
||||||
sourceOffset = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
go f.copyLoop(ctx, target, offset, sourceOffset)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) copyLoop(ctx context.Context, target *os.File, targetOffset, sourceOffset int64) {
|
|
||||||
var finalErr error
|
|
||||||
|
|
||||||
defer target.Close()
|
|
||||||
defer func() {
|
|
||||||
f.mu.Lock()
|
|
||||||
runDone := f.runDone
|
|
||||||
f.runDone = nil
|
|
||||||
f.cancel = nil
|
|
||||||
paused := f.paused
|
|
||||||
closed := f.closed
|
|
||||||
f.mu.Unlock()
|
|
||||||
close(runDone)
|
|
||||||
if closed {
|
|
||||||
select {
|
|
||||||
case f.doneCh <- nil:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if paused {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case f.doneCh <- finalErr:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
src, err := f.registry.Get(f.meta.Req.URL)
|
|
||||||
if err != nil {
|
|
||||||
finalErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sourceFile, err := os.Open(src.Path)
|
|
||||||
if err != nil {
|
|
||||||
finalErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer sourceFile.Close()
|
|
||||||
|
|
||||||
buf := make([]byte, 32*1024)
|
|
||||||
for {
|
|
||||||
if err := f.registry.WaitForReadable(ctx, f.meta.Req.URL, sourceOffset); err != nil {
|
|
||||||
if errors.Is(err, context.Canceled) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
finalErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
snapshot, serr := f.registry.Get(f.meta.Req.URL)
|
|
||||||
if serr != nil {
|
|
||||||
finalErr = serr
|
|
||||||
return
|
|
||||||
}
|
|
||||||
current := snapshot.Snapshot()
|
|
||||||
for sourceOffset < current.Written {
|
|
||||||
remain := current.Written - sourceOffset
|
|
||||||
readSize := int64(len(buf))
|
|
||||||
if remain < readSize {
|
|
||||||
readSize = remain
|
|
||||||
}
|
|
||||||
n, err := sourceFile.ReadAt(buf[:readSize], sourceOffset)
|
|
||||||
if err != nil && !errors.Is(err, io.EOF) {
|
|
||||||
finalErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if n == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if _, err := target.Write(buf[:n]); err != nil {
|
|
||||||
finalErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sourceOffset += int64(n)
|
|
||||||
targetOffset += int64(n)
|
|
||||||
f.downloaded.Store(targetOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
if current.State == SourceStateClosed && sourceOffset >= current.Written {
|
|
||||||
if f.meta.Res != nil {
|
|
||||||
if f.meta.Res.Size > 0 && targetOffset < f.meta.Res.Size {
|
|
||||||
finalErr = io.ErrUnexpectedEOF
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if f.meta.Res.Size == 0 {
|
|
||||||
f.meta.Res.Size = targetOffset
|
|
||||||
}
|
|
||||||
f.meta.Res.Files[0].Size = f.meta.Res.Size
|
|
||||||
}
|
|
||||||
finalErr = nil
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if current.State == SourceStateAborted || current.State == SourceStateFailed {
|
|
||||||
err := current.Err
|
|
||||||
if err == nil {
|
|
||||||
err = ErrSourceAborted
|
|
||||||
}
|
|
||||||
finalErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Patch(req *base.Request, opts *base.Options) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Pause() error {
|
|
||||||
f.mu.Lock()
|
|
||||||
f.paused = true
|
|
||||||
cancel := f.cancel
|
|
||||||
runDone := f.runDone
|
|
||||||
f.mu.Unlock()
|
|
||||||
if cancel != nil {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
if runDone != nil {
|
|
||||||
<-runDone
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Close() error {
|
|
||||||
f.mu.Lock()
|
|
||||||
f.closed = true
|
|
||||||
cancel := f.cancel
|
|
||||||
runDone := f.runDone
|
|
||||||
f.mu.Unlock()
|
|
||||||
if cancel != nil {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
if runDone != nil {
|
|
||||||
<-runDone
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case f.doneCh <- nil:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Stats() any {
|
|
||||||
return map[string]any{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
|
||||||
return f.meta
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Progress() fetcher.Progress {
|
|
||||||
return fetcher.Progress{f.downloaded.Load()}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fetcher) Wait() error {
|
|
||||||
return <-f.doneCh
|
|
||||||
}
|
|
||||||
|
|
||||||
type FetcherManager struct {
|
|
||||||
registry *Registry
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) SetRegistry(registry *Registry) {
|
|
||||||
fm.registry = registry
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) Name() string {
|
|
||||||
return Scheme
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) Filters() []*fetcher.SchemeFilter {
|
|
||||||
return []*fetcher.SchemeFilter{
|
|
||||||
{
|
|
||||||
Type: fetcher.FilterTypeUrl,
|
|
||||||
Pattern: strings.ToUpper(Scheme),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) Build() fetcher.Fetcher {
|
|
||||||
return &Fetcher{
|
|
||||||
registry: fm.registry,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) ParseName(u string) string {
|
|
||||||
id, err := ParseURL(u)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return id
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) AutoRename() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) DefaultConfig() any {
|
|
||||||
return &config{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) Store(f fetcher.Fetcher) (data any, err error) {
|
|
||||||
_f := f.(*Fetcher)
|
|
||||||
return &fetcherData{
|
|
||||||
LastSourceID: _f.lastSourceID,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) {
|
|
||||||
return &fetcherData{}, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher {
|
|
||||||
fd := v.(*fetcherData)
|
|
||||||
return &Fetcher{
|
|
||||||
registry: fm.registry,
|
|
||||||
meta: meta,
|
|
||||||
doneCh: make(chan error, 1),
|
|
||||||
lastSourceID: fd.LastSourceID,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fm *FetcherManager) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,122 +0,0 @@
|
|||||||
package gblob
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/GopeedLab/gopeed/internal/controller"
|
|
||||||
internalfetcher "github.com/GopeedLab/gopeed/internal/fetcher"
|
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFetcherClosedSourceBeforeExpectedSizeReturnsError(t *testing.T) {
|
|
||||||
registry := NewRegistry(t.TempDir())
|
|
||||||
url, err := registry.CreateWritableStream(&CreateWritableStreamOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := registry.Write(url, []byte("abc")); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := registry.CloseSource(url); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
downloadDir := t.TempDir()
|
|
||||||
filePath := filepath.Join(downloadDir, "partial.bin")
|
|
||||||
f := &Fetcher{
|
|
||||||
registry: registry,
|
|
||||||
meta: &internalfetcher.FetcherMeta{
|
|
||||||
Req: &base.Request{URL: url},
|
|
||||||
Res: &base.Resource{
|
|
||||||
Size: 10,
|
|
||||||
Range: true,
|
|
||||||
Files: []*base.FileInfo{{Name: "partial.bin", Req: &base.Request{URL: url}}},
|
|
||||||
},
|
|
||||||
Opts: &base.Options{Path: downloadDir, Name: "partial.bin"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
f.Setup(controller.NewController())
|
|
||||||
|
|
||||||
if err := f.Start(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-f.doneCh:
|
|
||||||
if !errors.Is(err, io.ErrUnexpectedEOF) {
|
|
||||||
t.Fatalf("expected unexpected EOF, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(2 * time.Second):
|
|
||||||
t.Fatal("timeout waiting for fetcher completion")
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := os.ReadFile(filePath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(data) != "abc" {
|
|
||||||
t.Fatalf("unexpected partial file content: %q", string(data))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFetcherClosedUnknownSizeSourceCompletesSuccessfully(t *testing.T) {
|
|
||||||
registry := NewRegistry(t.TempDir())
|
|
||||||
url, err := registry.CreateWritableStream(&CreateWritableStreamOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := registry.Write(url, []byte("abcdef")); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := registry.CloseSource(url); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
downloadDir := t.TempDir()
|
|
||||||
filePath := filepath.Join(downloadDir, "stream.bin")
|
|
||||||
f := &Fetcher{
|
|
||||||
registry: registry,
|
|
||||||
meta: &internalfetcher.FetcherMeta{
|
|
||||||
Req: &base.Request{URL: url},
|
|
||||||
Res: &base.Resource{
|
|
||||||
Size: 0,
|
|
||||||
Range: false,
|
|
||||||
Files: []*base.FileInfo{{Name: "stream.bin", Req: &base.Request{URL: url}}},
|
|
||||||
},
|
|
||||||
Opts: &base.Options{Path: downloadDir, Name: "stream.bin"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
f.Setup(controller.NewController())
|
|
||||||
|
|
||||||
if err := f.Start(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-f.doneCh:
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected nil completion error, got %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(2 * time.Second):
|
|
||||||
t.Fatal("timeout waiting for fetcher completion")
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := os.ReadFile(filePath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if string(data) != "abcdef" {
|
|
||||||
t.Fatalf("unexpected stream file content: %q", string(data))
|
|
||||||
}
|
|
||||||
if got := f.meta.Res.Size; got != int64(len(data)) {
|
|
||||||
t.Fatalf("unexpected resolved size: got %d want %d", got, len(data))
|
|
||||||
}
|
|
||||||
if got := f.meta.Res.Files[0].Size; got != int64(len(data)) {
|
|
||||||
t.Fatalf("unexpected resolved file size: got %d want %d", got, len(data))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,46 +0,0 @@
|
|||||||
package gblob
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestRegistry_RevokeOpenWritableStreamAbortsAndCleansUpAfterUnpin(t *testing.T) {
|
|
||||||
registry := NewRegistry(t.TempDir())
|
|
||||||
url, err := registry.CreateWritableStream(&CreateWritableStreamOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := registry.Pin(url); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
src, err := registry.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if state := src.Snapshot().State; state != SourceStateOpen {
|
|
||||||
t.Fatalf("expected initial open state, got %s", state)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := registry.Revoke(url); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
src, err = registry.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
snapshot := src.Snapshot()
|
|
||||||
if snapshot.State != SourceStateAborted {
|
|
||||||
t.Fatalf("expected revoked open source to become aborted, got %s", snapshot.State)
|
|
||||||
}
|
|
||||||
if !errors.Is(snapshot.Err, ErrSourceRevoked) {
|
|
||||||
t.Fatalf("expected revoke error, got %v", snapshot.Err)
|
|
||||||
}
|
|
||||||
|
|
||||||
registry.Unpin(url)
|
|
||||||
if _, err := registry.Get(url); !errors.Is(err, ErrSourceNotFound) {
|
|
||||||
t.Fatalf("expected source cleanup after unpin, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+350
-95
File diff suppressed because it is too large.
Load diff
+646
-49
File diff suppressed because it is too large.
Load diff
@@ -47,21 +47,6 @@ func TestError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestURLCreateObjectURLRejectsUnsupportedType(t *testing.T) {
|
|
||||||
engine := NewEngine(nil)
|
|
||||||
defer engine.Close()
|
|
||||||
|
|
||||||
_, err := engine.RunString(`
|
|
||||||
URL.createObjectURL({});
|
|
||||||
`)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected unsupported type error")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "Unsupported object type for URL.createObjectURL: Object") {
|
|
||||||
t.Fatalf("unexpected unsupported type error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCallFunction_DetachedAsyncWorkDoesNotBlockReturn(t *testing.T) {
|
func TestCallFunction_DetachedAsyncWorkDoesNotBlockReturn(t *testing.T) {
|
||||||
engine := NewEngine(nil)
|
engine := NewEngine(nil)
|
||||||
defer engine.Close()
|
defer engine.Close()
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,278 @@
|
|||||||
|
package stream_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download/engine"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/stream"
|
||||||
|
)
|
||||||
|
|
||||||
|
type createdObjectURL struct {
|
||||||
|
opts *stream.ObjectURLOptions
|
||||||
|
open stream.ObjectURLOpener
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResponseBlobObjectURLReadsResponseStream(t *testing.T) {
|
||||||
|
const payload = "response blob payload"
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
_, _ = io.WriteString(w, payload[:8])
|
||||||
|
w.(http.Flusher).Flush() // force an unknown-length/chunked response
|
||||||
|
_, _ = io.WriteString(w, payload[8:])
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
created := make(chan createdObjectURL, 1)
|
||||||
|
engine, cleanup := newStreamTestEngine(t, created)
|
||||||
|
defer func() {
|
||||||
|
cleanup()
|
||||||
|
engine.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
value, err := engine.RunString(fmt.Sprintf(`
|
||||||
|
(async () => {
|
||||||
|
const response = await fetch(%q);
|
||||||
|
const blob = await response.blob();
|
||||||
|
return __gopeed_blob_create_object_url(blob);
|
||||||
|
})()
|
||||||
|
`, server.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if value != "blob:test-1" {
|
||||||
|
t.Fatalf("unexpected object URL: %#v", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
object := <-created
|
||||||
|
if object.opts.Size != int64(len(payload)) || object.opts.ContentType != "text/plain" {
|
||||||
|
t.Fatalf("unexpected object URL options: %#v", object.opts)
|
||||||
|
}
|
||||||
|
if !object.opts.Range {
|
||||||
|
t.Fatal("materialized response Blob should advertise range support")
|
||||||
|
}
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
reader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 0, End: -1})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
data, readErr := io.ReadAll(reader)
|
||||||
|
_ = reader.Close()
|
||||||
|
if readErr != nil {
|
||||||
|
t.Fatal(readErr)
|
||||||
|
}
|
||||||
|
if got := string(data); got != payload {
|
||||||
|
t.Fatalf("unexpected response Blob data on open %d: %q", i+1, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rangeReader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 9, End: 12})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rangeData, readErr := io.ReadAll(rangeReader)
|
||||||
|
_ = rangeReader.Close()
|
||||||
|
if readErr != nil {
|
||||||
|
t.Fatal(readErr)
|
||||||
|
}
|
||||||
|
if got, want := string(rangeData), payload[9:13]; got != want {
|
||||||
|
t.Fatalf("unexpected materialized Blob range: got %q want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResponseBlobRejectsTruncatedResponse(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set("Content-Length", "64")
|
||||||
|
_, _ = io.WriteString(w, "partial")
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
created := make(chan createdObjectURL, 1)
|
||||||
|
engine, cleanup := newStreamTestEngine(t, created)
|
||||||
|
defer func() {
|
||||||
|
cleanup()
|
||||||
|
engine.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
value, err := engine.RunString(fmt.Sprintf(`
|
||||||
|
(async () => {
|
||||||
|
const response = await fetch(%q);
|
||||||
|
try {
|
||||||
|
await response.blob();
|
||||||
|
return "resolved";
|
||||||
|
} catch (_) {
|
||||||
|
return "rejected";
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
`, server.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if value != "rejected" {
|
||||||
|
t.Fatalf("truncated response unexpectedly materialized a Blob: %#v", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyBlobObjectURL(t *testing.T) {
|
||||||
|
created := make(chan createdObjectURL, 1)
|
||||||
|
engine, cleanup := newStreamTestEngine(t, created)
|
||||||
|
defer func() {
|
||||||
|
cleanup()
|
||||||
|
engine.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
value, err := engine.RunString(`
|
||||||
|
__gopeed_blob_create_object_url(new Blob([], { type: "application/empty" }))
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if value != "blob:test-1" {
|
||||||
|
t.Fatalf("unexpected object URL: %#v", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
object := <-created
|
||||||
|
if object.opts.Size != 0 || object.opts.Range {
|
||||||
|
t.Fatalf("unexpected empty Blob options: %#v", object.opts)
|
||||||
|
}
|
||||||
|
reader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 0, End: -1})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer reader.Close()
|
||||||
|
data, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(data) != 0 {
|
||||||
|
t.Fatalf("expected empty Blob, got %q", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBlobObjectURLReaderCloseCancelsPendingJSReader(t *testing.T) {
|
||||||
|
created := make(chan createdObjectURL, 1)
|
||||||
|
engine, cleanup := newStreamTestEngine(t, created)
|
||||||
|
defer func() {
|
||||||
|
cleanup()
|
||||||
|
engine.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
value, err := engine.RunString(`
|
||||||
|
globalThis.__blobPipeCancelState = {
|
||||||
|
readCalls: 0,
|
||||||
|
cancelCalls: 0,
|
||||||
|
releaseCalls: 0,
|
||||||
|
cancelReason: "",
|
||||||
|
resolveRead: null,
|
||||||
|
};
|
||||||
|
const pendingSource = {
|
||||||
|
getReader() {
|
||||||
|
return {
|
||||||
|
read() {
|
||||||
|
__blobPipeCancelState.readCalls++;
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
__blobPipeCancelState.resolveRead = resolve;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
cancel(reason) {
|
||||||
|
__blobPipeCancelState.cancelCalls++;
|
||||||
|
__blobPipeCancelState.cancelReason = String(reason);
|
||||||
|
if (__blobPipeCancelState.resolveRead) {
|
||||||
|
__blobPipeCancelState.resolveRead({ done: true, value: undefined });
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
releaseLock() {
|
||||||
|
__blobPipeCancelState.releaseCalls++;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
__gopeed_blob_create_object_url(async () => pendingSource);
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if value != "blob:test-1" {
|
||||||
|
t.Fatalf("unexpected object URL: %#v", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
object := <-created
|
||||||
|
reader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 0, End: -1})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
readDone := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
_, readErr := reader.Read(make([]byte, 1))
|
||||||
|
readDone <- readErr
|
||||||
|
}()
|
||||||
|
|
||||||
|
waitForJSValue(t, engine, `String(__blobPipeCancelState.readCalls)`, "1")
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := reader.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case readErr := <-readDone:
|
||||||
|
if !errors.Is(readErr, context.Canceled) {
|
||||||
|
t.Fatalf("unexpected pending read result: %v", readErr)
|
||||||
|
}
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("pending Go read was not released by Close")
|
||||||
|
}
|
||||||
|
waitForJSValue(t, engine, `JSON.stringify({
|
||||||
|
cancelCalls: __blobPipeCancelState.cancelCalls,
|
||||||
|
releaseCalls: __blobPipeCancelState.releaseCalls,
|
||||||
|
cancelReason: __blobPipeCancelState.cancelReason,
|
||||||
|
})`, `{"cancelCalls":1,"releaseCalls":1,"cancelReason":"blob request closed"}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForJSValue(t *testing.T, engine *engine.Engine, expression, want string) {
|
||||||
|
t.Helper()
|
||||||
|
deadline := time.Now().Add(2 * time.Second)
|
||||||
|
var got any
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
var err error
|
||||||
|
got, err = engine.RunString(expression)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got == want {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Fatalf("timed out waiting for JavaScript value %q, got %#v", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStreamTestEngine(t *testing.T, created chan<- createdObjectURL) (*engine.Engine, func()) {
|
||||||
|
t.Helper()
|
||||||
|
var next atomic.Int64
|
||||||
|
var cleanup func()
|
||||||
|
e := engine.NewEngine(&engine.Config{
|
||||||
|
StreamConfig: &stream.Config{
|
||||||
|
CreateObjectURL: func(opts *stream.ObjectURLOptions, open stream.ObjectURLOpener) (string, error) {
|
||||||
|
created <- createdObjectURL{opts: opts, open: open}
|
||||||
|
return fmt.Sprintf("blob:test-%d", next.Add(1)), nil
|
||||||
|
},
|
||||||
|
RevokeObjectURL: func(string) error { return nil },
|
||||||
|
RegisterCleanup: func(fn func()) {
|
||||||
|
cleanup = fn
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if cleanup == nil {
|
||||||
|
t.Fatal("stream cleanup was not registered")
|
||||||
|
}
|
||||||
|
return e, cleanup
|
||||||
|
}
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
package stream
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/formdata"
|
||||||
|
"github.com/dop251/goja"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExportFetchRequestSnapshotsArrayBuffer(t *testing.T) {
|
||||||
|
runtime := goja.New()
|
||||||
|
buffer := runtime.NewArrayBuffer([]byte("before"))
|
||||||
|
request := runtime.NewObject()
|
||||||
|
setFetchRequestDefaults(t, request)
|
||||||
|
if err := request.Set("body", buffer); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta, err := exportFetchRequest(runtime, request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
buffer.Bytes()[0] = 'X'
|
||||||
|
|
||||||
|
body, ok := meta.Body.([]byte)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected byte snapshot, got %T", meta.Body)
|
||||||
|
}
|
||||||
|
if got := string(body); got != "before" {
|
||||||
|
t.Fatalf("ArrayBuffer snapshot changed after export: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExportFetchRequestSnapshotsFormData(t *testing.T) {
|
||||||
|
runtime := goja.New()
|
||||||
|
runtime.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
|
||||||
|
if err := formdata.Enable(runtime); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
value, err := runtime.RunString(`
|
||||||
|
globalThis.form = new FormData();
|
||||||
|
form.append("field", "before");
|
||||||
|
form;
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
request := runtime.NewObject()
|
||||||
|
setFetchRequestDefaults(t, request)
|
||||||
|
if err := request.Set("body", value); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta, err := exportFetchRequest(runtime, request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := runtime.RunString(`form.set("field", "after")`); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, ok := meta.Body.(*fetchFormDataSnapshot)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected FormData snapshot, got %T", meta.Body)
|
||||||
|
}
|
||||||
|
if len(body.entries) != 1 {
|
||||||
|
t.Fatalf("expected one FormData entry, got %d", len(body.entries))
|
||||||
|
}
|
||||||
|
if got := body.entries[0]; got.name != "field" || got.value != "before" {
|
||||||
|
t.Fatalf("unexpected FormData snapshot: %#v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setFetchRequestDefaults(t *testing.T, request *goja.Object) {
|
||||||
|
t.Helper()
|
||||||
|
for key, value := range map[string]any{
|
||||||
|
"url": "https://example.test",
|
||||||
|
"method": "POST",
|
||||||
|
"redirect": "follow",
|
||||||
|
"credentials": "same-origin",
|
||||||
|
} {
|
||||||
|
if err := request.Set(key, value); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSnapshotFetchBodyCopiesFileMetadata(t *testing.T) {
|
||||||
|
source := &file.File{
|
||||||
|
Reader: strings.NewReader("payload"),
|
||||||
|
Name: "before.txt",
|
||||||
|
Size: 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := snapshotFetchBody(source)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
snapshot, ok := value.(*file.File)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected file snapshot, got %T", value)
|
||||||
|
}
|
||||||
|
source.Name = "after.txt"
|
||||||
|
source.Size = 99
|
||||||
|
|
||||||
|
if snapshot.Name != "before.txt" || snapshot.Size != 7 {
|
||||||
|
t.Fatalf("file metadata was not snapshotted: %#v", snapshot)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetchRegistryCloseAll(t *testing.T) {
|
||||||
|
registry := newFetchRegistry()
|
||||||
|
streamCtx, cancel := context.WithCancel(context.Background())
|
||||||
|
body := &trackingReadCloser{}
|
||||||
|
registry.streams["active"] = &fetchStream{
|
||||||
|
body: body,
|
||||||
|
cancel: cancel,
|
||||||
|
ctx: streamCtx,
|
||||||
|
ch: make(chan fetchChunk),
|
||||||
|
}
|
||||||
|
|
||||||
|
registry.CloseAll()
|
||||||
|
registry.CloseAll()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-streamCtx.Done():
|
||||||
|
default:
|
||||||
|
t.Fatal("active fetch context was not canceled")
|
||||||
|
}
|
||||||
|
if got := body.closeCalls.Load(); got != 1 {
|
||||||
|
t.Fatalf("expected body to close once, got %d", got)
|
||||||
|
}
|
||||||
|
registry.mu.Lock()
|
||||||
|
closed := registry.closed
|
||||||
|
remaining := len(registry.streams)
|
||||||
|
registry.mu.Unlock()
|
||||||
|
if !closed || remaining != 0 {
|
||||||
|
t.Fatalf("registry not fully closed: closed=%v remaining=%d", closed, remaining)
|
||||||
|
}
|
||||||
|
if _, err := registry.Open("", nil, &fetchRequest{URL: "https://example.test"}); err == nil {
|
||||||
|
t.Fatal("expected a closed registry to reject new fetches")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetchRegistryReadPreservesChunkOrder(t *testing.T) {
|
||||||
|
registry := newFetchRegistry()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
stream := &fetchStream{
|
||||||
|
body: io.NopCloser(strings.NewReader("")),
|
||||||
|
cancel: cancel,
|
||||||
|
ctx: ctx,
|
||||||
|
ch: make(chan fetchChunk, 2),
|
||||||
|
}
|
||||||
|
stream.ch <- fetchChunk{data: []byte("abc")}
|
||||||
|
stream.ch <- fetchChunk{data: []byte("def")}
|
||||||
|
registry.streams["ordered"] = stream
|
||||||
|
defer registry.CloseAll()
|
||||||
|
|
||||||
|
var got strings.Builder
|
||||||
|
for range 4 {
|
||||||
|
chunk, done, err := registry.Read("ordered", 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if done {
|
||||||
|
t.Fatal("stream ended before buffered chunks were read")
|
||||||
|
}
|
||||||
|
got.Write(chunk)
|
||||||
|
}
|
||||||
|
if got.String() != "abcdef" {
|
||||||
|
t.Fatalf("fetch chunks were reordered: %q", got.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type trackingReadCloser struct {
|
||||||
|
closeCalls atomic.Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *trackingReadCloser) Read([]byte) (int, error) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *trackingReadCloser) Close() error {
|
||||||
|
r.closeCalls.Add(1)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
+19
-29
@@ -271,7 +271,6 @@ func (d *Downloader) triggerOnResolve(req *base.Request) (res *base.Resource, er
|
|||||||
}
|
}
|
||||||
ensureResourceRequestRawURLs(req, ctx.Res)
|
ensureResourceRequestRawURLs(req, ctx.Res)
|
||||||
ctx.Res.CalcSize(nil)
|
ctx.Res.CalcSize(nil)
|
||||||
d.applyGBlobResourceMetadata(ctx.Res)
|
|
||||||
}
|
}
|
||||||
res = ctx.Res
|
res = ctx.Res
|
||||||
},
|
},
|
||||||
@@ -279,25 +278,12 @@ func (d *Downloader) triggerOnResolve(req *base.Request) (res *base.Resource, er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) applyGBlobResourceMetadata(res *base.Resource) {
|
|
||||||
if d.gblob == nil || res == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, file := range res.Files {
|
|
||||||
if file == nil || file.Req == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_ = d.gblob.SetSize(file.Req.URL, file.Size)
|
|
||||||
_ = d.gblob.SetRange(file.Req.URL, res.Range)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Downloader) triggerOnStart(task *Task) {
|
func (d *Downloader) triggerOnStart(task *Task) {
|
||||||
doTrigger(d,
|
doTrigger(d,
|
||||||
EventOnStart,
|
EventOnStart,
|
||||||
task.Meta.Req,
|
task.Meta.Req,
|
||||||
&OnStartContext{
|
&OnStartContext{
|
||||||
Task: NewExtensionTask(d, task),
|
Task: newOnStartExtensionTask(task),
|
||||||
},
|
},
|
||||||
func(ext *Extension, gopeed *Instance, ctx *OnStartContext) {
|
func(ext *Extension, gopeed *Instance, ctx *OnStartContext) {
|
||||||
// Validate request structure
|
// Validate request structure
|
||||||
@@ -309,7 +295,6 @@ func (d *Downloader) triggerOnStart(task *Task) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) triggerOnError(task *Task, err error) {
|
func (d *Downloader) triggerOnError(task *Task, err error) {
|
||||||
@@ -317,7 +302,7 @@ func (d *Downloader) triggerOnError(task *Task, err error) {
|
|||||||
EventOnError,
|
EventOnError,
|
||||||
task.Meta.Req,
|
task.Meta.Req,
|
||||||
&OnErrorContext{
|
&OnErrorContext{
|
||||||
Task: NewExtensionTask(d, task),
|
Task: newOnErrorExtensionTask(d, task),
|
||||||
Error: err,
|
Error: err,
|
||||||
},
|
},
|
||||||
nil,
|
nil,
|
||||||
@@ -328,8 +313,8 @@ func (d *Downloader) triggerOnDone(task *Task) {
|
|||||||
doTrigger(d,
|
doTrigger(d,
|
||||||
EventOnDone,
|
EventOnDone,
|
||||||
task.Meta.Req,
|
task.Meta.Req,
|
||||||
&OnErrorContext{
|
&OnDoneContext{
|
||||||
Task: NewExtensionTask(d, task),
|
Task: newOnDoneExtensionTask(task),
|
||||||
},
|
},
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
@@ -720,7 +705,7 @@ type OnResolveContext struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type OnStartContext struct {
|
type OnStartContext struct {
|
||||||
Task *ExtensionTask `json:"task"`
|
Task *Task `json:"task"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type OnErrorContext struct {
|
type OnErrorContext struct {
|
||||||
@@ -741,28 +726,33 @@ type ExtensionTask struct {
|
|||||||
*Task
|
*Task
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExtensionTask(download *Downloader, task *Task) *ExtensionTask {
|
func cloneExtensionTask(task *Task) *Task {
|
||||||
// restricts extension scripts to only modify request info
|
|
||||||
newTask := task.clone()
|
newTask := task.clone()
|
||||||
newTask.Meta.Req = task.Meta.Req
|
newTask.Meta.Req = task.Meta.Req
|
||||||
|
return newTask
|
||||||
|
}
|
||||||
|
|
||||||
|
func newOnStartExtensionTask(task *Task) *Task {
|
||||||
|
return cloneExtensionTask(task)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newOnErrorExtensionTask(download *Downloader, task *Task) *ExtensionTask {
|
||||||
return &ExtensionTask{
|
return &ExtensionTask{
|
||||||
download: download,
|
download: download,
|
||||||
Task: newTask,
|
Task: cloneExtensionTask(task),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newOnDoneExtensionTask(task *Task) *Task {
|
||||||
|
return cloneExtensionTask(task)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *ExtensionTask) Continue() error {
|
func (t *ExtensionTask) Continue() error {
|
||||||
return t.download.Continue(&TaskFilter{
|
return t.download.Continue(&TaskFilter{
|
||||||
IDs: []string{t.ID},
|
IDs: []string{t.ID},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ExtensionTask) Pause() error {
|
|
||||||
return t.download.Pause(&TaskFilter{
|
|
||||||
IDs: []string{t.ID},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseSettings(settings []*Setting) map[string]any {
|
func parseSettings(settings []*Setting) map[string]any {
|
||||||
m := make(map[string]any)
|
m := make(map[string]any)
|
||||||
for _, s := range settings {
|
for _, s := range settings {
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package download
|
package download
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/gblob"
|
internalblob "github.com/GopeedLab/gopeed/internal/blob"
|
||||||
"github.com/GopeedLab/gopeed/pkg/download/engine"
|
"github.com/GopeedLab/gopeed/pkg/download/engine"
|
||||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/stream"
|
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/stream"
|
||||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||||
"github.com/dop251/goja"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type engineSession struct {
|
type engineSession struct {
|
||||||
@@ -96,57 +96,30 @@ func (s *engineSession) runClosers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeStreamChunk(data any) ([]byte, error) {
|
|
||||||
switch v := data.(type) {
|
|
||||||
case nil:
|
|
||||||
return nil, nil
|
|
||||||
case string:
|
|
||||||
return []byte(v), nil
|
|
||||||
case []byte:
|
|
||||||
return v, nil
|
|
||||||
case goja.ArrayBuffer:
|
|
||||||
return v.Bytes(), nil
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unsupported stream chunk type: %T", data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Downloader) newExtensionEngine() (*engine.Engine, *engineSession) {
|
func (d *Downloader) newExtensionEngine() (*engine.Engine, *engineSession) {
|
||||||
session := newEngineSession(nil)
|
session := newEngineSession(nil)
|
||||||
engineCfg := &stream.Config{
|
engineCfg := &stream.Config{
|
||||||
CreateBlobObjectURL: func(data []byte, contentType string) (string, error) {
|
CreateObjectURL: func(opts *stream.ObjectURLOptions, open stream.ObjectURLOpener) (string, error) {
|
||||||
return d.gblob.CreateBlob(data, contentType)
|
createOpts := &internalblob.CreateOptions{
|
||||||
},
|
|
||||||
CreateWritableStreamObjectURL: func(opts *stream.WritableStreamObjectURLOptions) (string, error) {
|
|
||||||
reopenable := false
|
|
||||||
if opts != nil {
|
|
||||||
reopenable = opts.Reopenable
|
|
||||||
}
|
|
||||||
return d.gblob.CreateWritableStream(&gblob.CreateWritableStreamOptions{
|
|
||||||
Session: session,
|
Session: session,
|
||||||
Reopenable: reopenable,
|
|
||||||
})
|
|
||||||
},
|
|
||||||
RegisterWritableStreamResume: func(url string, reopen func(offset int64) error) error {
|
|
||||||
return d.gblob.SetResumeOpener(url, reopen)
|
|
||||||
},
|
|
||||||
WriteWritableStreamObjectURL: func(url string, data any) error {
|
|
||||||
chunk, err := normalizeStreamChunk(data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return d.gblob.Write(url, chunk)
|
if opts != nil {
|
||||||
},
|
createOpts.ContentType = opts.ContentType
|
||||||
CloseWritableStreamObjectURL: func(url string) error {
|
createOpts.Size = opts.Size
|
||||||
return d.gblob.CloseSource(url)
|
createOpts.Range = opts.Range
|
||||||
},
|
}
|
||||||
AbortWritableStreamObjectURL: func(url string, reason string) error {
|
return d.blob.CreateOpener(func(ctx context.Context, req internalblob.OpenRequest) (io.ReadCloser, error) {
|
||||||
return d.gblob.AbortSource(url, fmt.Errorf("%s", reason))
|
return open(ctx, stream.ObjectURLOpenRequest{
|
||||||
|
Offset: req.Offset,
|
||||||
|
End: req.End,
|
||||||
|
})
|
||||||
|
}, createOpts)
|
||||||
},
|
},
|
||||||
RevokeObjectURL: func(url string) error {
|
RevokeObjectURL: func(url string) error {
|
||||||
return d.gblob.Revoke(url)
|
return d.blob.Revoke(url)
|
||||||
},
|
},
|
||||||
ProxyHandler: d.cfg.Proxy.ToHandler(),
|
ProxyHandler: d.cfg.Proxy.ToHandler(),
|
||||||
|
RegisterCleanup: session.OnClose,
|
||||||
}
|
}
|
||||||
e := engine.NewEngine(&engine.Config{
|
e := engine.NewEngine(&engine.Config{
|
||||||
ProxyConfig: d.cfg.Proxy,
|
ProxyConfig: d.cfg.Proxy,
|
||||||
|
|||||||
@@ -29,17 +29,48 @@ func injectGopeed(vm *goja.Runtime, gopeed *Instance) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
runtimeObject := vm.NewObject()
|
runtimeObject := vm.NewObject()
|
||||||
if gopeed.Runtime != nil && gopeed.Runtime.WebView != nil {
|
if gopeed.Runtime != nil {
|
||||||
|
if err := runtimeObject.Set("blob", newJSBlobRuntime(vm)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if gopeed.Runtime.WebView != nil {
|
||||||
if err := runtimeObject.Set("webview", newJSWebViewRuntime(vm, gopeed.Runtime.WebView)); err != nil {
|
if err := runtimeObject.Set("webview", newJSWebViewRuntime(vm, gopeed.Runtime.WebView)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if err := gopeedObject.Set("runtime", runtimeObject); err != nil {
|
if err := gopeedObject.Set("runtime", runtimeObject); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return vm.Set("gopeed", gopeedObject)
|
return vm.Set("gopeed", gopeedObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newJSBlobRuntime(vm *goja.Runtime) *goja.Object {
|
||||||
|
obj := vm.NewObject()
|
||||||
|
_ = obj.Set("createObjectURL", func(call goja.FunctionCall) goja.Value {
|
||||||
|
fn, ok := goja.AssertFunction(vm.Get("__gopeed_blob_create_object_url"))
|
||||||
|
if !ok {
|
||||||
|
panic(vm.ToValue(fmt.Errorf("blob runtime is not available")))
|
||||||
|
}
|
||||||
|
value, err := fn(goja.Undefined(), call.Argument(0), call.Argument(1))
|
||||||
|
if err != nil {
|
||||||
|
panic(vm.ToValue(err))
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
})
|
||||||
|
_ = obj.Set("revokeObjectURL", func(call goja.FunctionCall) goja.Value {
|
||||||
|
fn, ok := goja.AssertFunction(vm.Get("__gopeed_blob_revoke_object_url"))
|
||||||
|
if !ok {
|
||||||
|
panic(vm.ToValue(fmt.Errorf("blob runtime is not available")))
|
||||||
|
}
|
||||||
|
if _, err := fn(goja.Undefined(), call.Argument(0)); err != nil {
|
||||||
|
panic(vm.ToValue(err))
|
||||||
|
}
|
||||||
|
return goja.Undefined()
|
||||||
|
})
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
func newJSEventsRuntime(vm *goja.Runtime, events InstanceEvents) *goja.Object {
|
func newJSEventsRuntime(vm *goja.Runtime, events InstanceEvents) *goja.Object {
|
||||||
obj := vm.NewObject()
|
obj := vm.NewObject()
|
||||||
register := func(event ActivationEvent, call goja.FunctionCall) goja.Value {
|
register := func(event ActivationEvent, call goja.FunctionCall) goja.Value {
|
||||||
|
|||||||
+596
-656
File diff suppressed because it is too large.
Load diff
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/GopeedLab/gopeed/internal/fetcher"
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/bt"
|
"github.com/GopeedLab/gopeed/internal/protocol/bt"
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/ed2k"
|
"github.com/GopeedLab/gopeed/internal/protocol/ed2k"
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/gblob"
|
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/http"
|
"github.com/GopeedLab/gopeed/internal/protocol/http"
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||||
@@ -37,6 +36,9 @@ type Task struct {
|
|||||||
timer *util.Timer
|
timer *util.Timer
|
||||||
statusLock *sync.Mutex
|
statusLock *sync.Mutex
|
||||||
lock *sync.Mutex
|
lock *sync.Mutex
|
||||||
|
blobRefLock *sync.Mutex
|
||||||
|
blobURL string
|
||||||
|
runGeneration uint64
|
||||||
speedArr []int64
|
speedArr []int64
|
||||||
uploadSpeedArr []int64
|
uploadSpeedArr []int64
|
||||||
}
|
}
|
||||||
@@ -166,7 +168,6 @@ func (cfg *DownloaderConfig) Init() *DownloaderConfig {
|
|||||||
}
|
}
|
||||||
if len(cfg.FetchManagers) == 0 {
|
if len(cfg.FetchManagers) == 0 {
|
||||||
cfg.FetchManagers = []fetcher.FetcherManager{
|
cfg.FetchManagers = []fetcher.FetcherManager{
|
||||||
new(gblob.FetcherManager),
|
|
||||||
new(http.FetcherManager),
|
new(http.FetcherManager),
|
||||||
new(bt.FetcherManager),
|
new(bt.FetcherManager),
|
||||||
new(ed2k.FetcherManager),
|
new(ed2k.FetcherManager),
|
||||||
|
|||||||
+111
@@ -0,0 +1,111 @@
|
|||||||
|
gopeed.events.onResolve(async function (ctx) {
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
|
if (ctx.req.url.endsWith("/blob")) {
|
||||||
|
const blob = new Blob(["hello world"], {type: "text/plain"});
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(blob);
|
||||||
|
ctx.res = {
|
||||||
|
name: "blob-blob",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "hello.txt",
|
||||||
|
size: 11,
|
||||||
|
req: {
|
||||||
|
url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.req.url.endsWith("/opener-range")) {
|
||||||
|
const payload = encoder.encode("line 1\nline 2\nline 3\n");
|
||||||
|
const firstChunkLength = encoder.encode("line 1\n").byteLength;
|
||||||
|
let firstAttempt = true;
|
||||||
|
|
||||||
|
const open = async ({ offset = 0, end = -1 }) => new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
try {
|
||||||
|
if (offset === 0 && firstAttempt) {
|
||||||
|
firstAttempt = false;
|
||||||
|
controller.enqueue(payload.slice(0, firstChunkLength));
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 120));
|
||||||
|
controller.error(new Error("resume required"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controller.enqueue(payload.slice(offset, end >= 0 ? end + 1 : undefined));
|
||||||
|
controller.close();
|
||||||
|
} catch (err) {
|
||||||
|
controller.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(open, { size: payload.byteLength, range: true });
|
||||||
|
|
||||||
|
ctx.res = {
|
||||||
|
name: "blob-opener-range",
|
||||||
|
range: true,
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "opener-range.txt",
|
||||||
|
size: payload.byteLength,
|
||||||
|
req: {
|
||||||
|
url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.req.url.endsWith("/opener-unknown")) {
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(async ({ offset = 0 }) => new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
if (offset > 0) {
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controller.enqueue(encoder.encode("line 1\n"));
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
||||||
|
controller.enqueue(encoder.encode("line 2\n"));
|
||||||
|
controller.close();
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
ctx.res = {
|
||||||
|
name: "blob-opener-unknown",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "opener-unknown.txt",
|
||||||
|
req: {
|
||||||
|
url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
const payload = encoder.encode("line 1\nline 2\n");
|
||||||
|
controller.enqueue(payload.slice(offset, end >= 0 ? end + 1 : undefined));
|
||||||
|
controller.close();
|
||||||
|
},
|
||||||
|
}), { size: 14 });
|
||||||
|
|
||||||
|
ctx.res = {
|
||||||
|
name: "blob-opener",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "opener.txt",
|
||||||
|
size: 14,
|
||||||
|
req: {
|
||||||
|
url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
});
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gblob",
|
"name": "blob",
|
||||||
"title": "gopeed extension gblob test",
|
"title": "gopeed extension blob test",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"scripts": [
|
"scripts": [
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
const payloadSize = 262144;
|
||||||
|
const chunkSize = 4096;
|
||||||
|
const payloadText = 'x'.repeat(payloadSize);
|
||||||
|
|
||||||
|
function delayedPayloadStream(offset = 0, end = -1) {
|
||||||
|
const limit = end >= 0 ? Math.min(end + 1, payloadSize) : payloadSize;
|
||||||
|
let pos = offset;
|
||||||
|
return new ReadableStream({
|
||||||
|
async pull(controller) {
|
||||||
|
if (pos >= limit) {
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const size = Math.min(chunkSize, limit - pos);
|
||||||
|
const chunk = new Uint8Array(size);
|
||||||
|
chunk.fill(120);
|
||||||
|
pos += size;
|
||||||
|
controller.enqueue(chunk);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function fastPayloadStream(offset = 0, end = -1) {
|
||||||
|
const limit = end >= 0 ? Math.min(end + 1, payloadSize) : payloadSize;
|
||||||
|
let pos = offset;
|
||||||
|
return new ReadableStream({
|
||||||
|
pull(controller) {
|
||||||
|
if (pos >= limit) {
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const size = Math.min(chunkSize, limit - pos);
|
||||||
|
const chunk = new Uint8Array(size);
|
||||||
|
chunk.fill(120);
|
||||||
|
pos += size;
|
||||||
|
controller.enqueue(chunk);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createBlobSourceURL(delayed) {
|
||||||
|
const blob = new Blob([payloadText], { type: 'application/octet-stream' });
|
||||||
|
const originalSlice = blob.slice.bind(blob);
|
||||||
|
blob.slice = function (offset = 0, end) {
|
||||||
|
if (!delayed) {
|
||||||
|
return originalSlice(offset, end);
|
||||||
|
}
|
||||||
|
const sliceEnd = Number.isFinite(Number(end)) ? Number(end) - 1 : -1;
|
||||||
|
return {
|
||||||
|
stream() {
|
||||||
|
return delayedPayloadStream(Number(offset) || 0, sliceEnd);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
return gopeed.runtime.blob.createObjectURL(blob);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createOpenerSourceURL(delayed) {
|
||||||
|
const open = async () => delayed ? delayedPayloadStream(0, -1) : fastPayloadStream(0, -1);
|
||||||
|
return gopeed.runtime.blob.createObjectURL(open, {
|
||||||
|
size: payloadSize,
|
||||||
|
contentType: 'application/octet-stream',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRangeSourceURL(delayed) {
|
||||||
|
const open = async ({ offset = 0, end = -1 }) => delayed ? delayedPayloadStream(offset, end) : fastPayloadStream(offset, end);
|
||||||
|
return gopeed.runtime.blob.createObjectURL(open, {
|
||||||
|
size: payloadSize,
|
||||||
|
range: true,
|
||||||
|
contentType: 'application/octet-stream',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function modeFromURL(url) {
|
||||||
|
if (url.includes('/pause-rebuild-blob')) {
|
||||||
|
return 'blob';
|
||||||
|
}
|
||||||
|
if (url.includes('/pause-rebuild-opener')) {
|
||||||
|
return 'opener';
|
||||||
|
}
|
||||||
|
if (url.includes('/pause-rebuild-range')) {
|
||||||
|
return 'range';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function createPayloadURL(mode, delayed) {
|
||||||
|
switch (mode) {
|
||||||
|
case 'blob':
|
||||||
|
return createBlobSourceURL(delayed);
|
||||||
|
case 'opener':
|
||||||
|
return createOpenerSourceURL(delayed);
|
||||||
|
case 'range':
|
||||||
|
return createRangeSourceURL(delayed);
|
||||||
|
default:
|
||||||
|
throw new Error('unsupported pause rebuild mode: ' + mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gopeed.events.onResolve(async function (ctx) {
|
||||||
|
const mode = modeFromURL(ctx.req.url);
|
||||||
|
if (!mode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.res = {
|
||||||
|
name: 'blob-pause-rebuild-' + mode,
|
||||||
|
range: mode !== 'opener',
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: 'pause-rebuild-' + mode + '.bin',
|
||||||
|
size: payloadSize,
|
||||||
|
req: {
|
||||||
|
url: createPayloadURL(mode, true),
|
||||||
|
rawUrl: ctx.req.rawUrl || ctx.req.url,
|
||||||
|
labels: {
|
||||||
|
mode: 'pause-rebuild',
|
||||||
|
source: mode,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
gopeed.events.onError(async function (ctx) {
|
||||||
|
const req = ctx.task?.meta?.req;
|
||||||
|
const source = req?.labels?.source || modeFromURL(req?.rawUrl || '');
|
||||||
|
if (!req || req.labels?.mode !== 'pause-rebuild' || req.labels?.rebuilt === 'true' || !source) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
req.url = createPayloadURL(source, false);
|
||||||
|
req.labels = req.labels || {};
|
||||||
|
req.labels.started = 'true';
|
||||||
|
req.labels.rebuilt = 'true';
|
||||||
|
ctx.task.continue();
|
||||||
|
});
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"name": "blob-pause-rebuild",
|
||||||
|
"title": "gopeed extension blob pause rebuild test",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"scripts": [
|
||||||
|
{
|
||||||
|
"event": "onResolve",
|
||||||
|
"match": {
|
||||||
|
"urls": [
|
||||||
|
"*://example.com/pause-rebuild*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"entry": "index.js"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event": "onError",
|
||||||
|
"match": {
|
||||||
|
"labels": [
|
||||||
|
"mode"
|
||||||
|
],
|
||||||
|
"urls": [
|
||||||
|
"*://example.com/pause-rebuild*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"entry": "index.js"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
gopeed.events.onResolve(async function (ctx) {
|
||||||
|
const isRange = ctx.req.url.endsWith("/recover-range");
|
||||||
|
if (!isRange && !ctx.req.url.endsWith("/recover")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(async ({ offset = 0 }) => new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
if (offset > 0) {
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controller.enqueue(new TextEncoder().encode("stale-prefix-that-must-be-removed\n"));
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||||
|
controller.error(new Error("expired"));
|
||||||
|
},
|
||||||
|
}), { size: 64, range: isRange });
|
||||||
|
|
||||||
|
ctx.res = {
|
||||||
|
name: "blob-recover",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "recover.txt",
|
||||||
|
size: 64,
|
||||||
|
req: {
|
||||||
|
url,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
gopeed.events.onError(async function (ctx) {
|
||||||
|
const req = ctx.task.meta.req;
|
||||||
|
if (!req.rawUrl || (!req.rawUrl.endsWith("/recover") && !req.rawUrl.endsWith("/recover-range"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
req.labels = req.labels || {};
|
||||||
|
if (req.labels.recovered === "true") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
req.labels.recovered = "true";
|
||||||
|
req.url = gopeed.runtime.blob.createObjectURL(new Blob(["ok\n"], { type: "text/plain" }));
|
||||||
|
ctx.task.continue();
|
||||||
|
});
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gblob-recover",
|
"name": "blob-recover",
|
||||||
"title": "gopeed extension gblob recover test",
|
"title": "gopeed extension blob recover test",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"scripts": [
|
"scripts": [
|
||||||
{
|
{
|
||||||
+9
-9
@@ -1,15 +1,15 @@
|
|||||||
function createRangeGBlobUrl(target) {
|
function createRangeBlobUrl(target) {
|
||||||
return URL.createObjectURL(async (offset) => {
|
return gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
if (offset > 0) {
|
if (offset > 0 || end >= 0) {
|
||||||
headers.Range = `bytes=${offset}-`;
|
headers.Range = `bytes=${offset}-${end >= 0 ? end : ''}`;
|
||||||
}
|
}
|
||||||
const response = await fetch(target, { headers });
|
const response = await fetch(target, { headers });
|
||||||
if (!response.body) {
|
if (!response.body) {
|
||||||
throw new Error('empty response body');
|
throw new Error('empty response body');
|
||||||
}
|
}
|
||||||
return response.body;
|
return response.body;
|
||||||
});
|
}, { size: 262144, range: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
gopeed.events.onResolve(async function (ctx) {
|
gopeed.events.onResolve(async function (ctx) {
|
||||||
@@ -18,14 +18,14 @@ gopeed.events.onResolve(async function (ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.res = {
|
ctx.res = {
|
||||||
name: 'gblob-restart',
|
name: 'blob-restart',
|
||||||
range: true,
|
range: true,
|
||||||
files: [
|
files: [
|
||||||
{
|
{
|
||||||
name: 'restart.bin',
|
name: 'restart.bin',
|
||||||
size: 262144,
|
size: 262144,
|
||||||
req: {
|
req: {
|
||||||
url: createRangeGBlobUrl(ctx.req.rawUrl || ctx.req.url),
|
url: createRangeBlobUrl(ctx.req.rawUrl || ctx.req.url),
|
||||||
rawUrl: ctx.req.rawUrl || ctx.req.url,
|
rawUrl: ctx.req.rawUrl || ctx.req.url,
|
||||||
labels: {
|
labels: {
|
||||||
mode: 'restart',
|
mode: 'restart',
|
||||||
@@ -38,7 +38,7 @@ gopeed.events.onResolve(async function (ctx) {
|
|||||||
|
|
||||||
gopeed.events.onError(async function (ctx) {
|
gopeed.events.onError(async function (ctx) {
|
||||||
const req = ctx.task?.meta?.req;
|
const req = ctx.task?.meta?.req;
|
||||||
if (!req || !req.rawUrl || !req.url || !req.url.startsWith('gblob:')) {
|
if (!req || !req.rawUrl || !req.url || !req.url.includes('/__blob/')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
req.labels = req.labels || {};
|
req.labels = req.labels || {};
|
||||||
@@ -47,7 +47,7 @@ gopeed.events.onError(async function (ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
req.url = createRangeGBlobUrl(req.rawUrl);
|
req.url = createRangeBlobUrl(req.rawUrl);
|
||||||
req.labels.started = 'true';
|
req.labels.started = 'true';
|
||||||
req.labels.rebuilt = 'true';
|
req.labels.rebuilt = 'true';
|
||||||
ctx.task.continue();
|
ctx.task.continue();
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gblob-restart",
|
"name": "blob-restart",
|
||||||
"title": "gopeed extension gblob restart resume test",
|
"title": "gopeed extension blob restart resume test",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"scripts": [
|
"scripts": [
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,198 @@
|
|||||||
|
const encoder = new TextEncoder();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
|
function byteLength(value) {
|
||||||
|
return encoder.encode(value).byteLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sourceURL(rawURL) {
|
||||||
|
const url = new URL(rawURL);
|
||||||
|
url.searchParams.set("source", "1");
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
function createFetchDrainedStream(url) {
|
||||||
|
return new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(url);
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controller.enqueue(value);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
controller.error(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createFetchDrainedRangeStream(url, offset, end) {
|
||||||
|
return new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
try {
|
||||||
|
const headers = {};
|
||||||
|
if (offset > 0 || end >= 0) {
|
||||||
|
headers.Range = `bytes=${offset}-${end >= 0 ? end : ""}`;
|
||||||
|
}
|
||||||
|
const response = await fetch(url, { headers });
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controller.enqueue(value);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
controller.error(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createMultiplexStreams(url) {
|
||||||
|
let videoController;
|
||||||
|
let audioController;
|
||||||
|
let started = false;
|
||||||
|
|
||||||
|
async function pump() {
|
||||||
|
if (started || !videoController || !audioController) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
started = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url);
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
let pending = "";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pending += decoder.decode(value, { stream: true });
|
||||||
|
|
||||||
|
let newlineIndex;
|
||||||
|
while ((newlineIndex = pending.indexOf("\n")) >= 0) {
|
||||||
|
const line = pending.slice(0, newlineIndex);
|
||||||
|
pending = pending.slice(newlineIndex + 1);
|
||||||
|
if (line.startsWith("v:")) {
|
||||||
|
videoController.enqueue(encoder.encode(line.slice(2)));
|
||||||
|
} else if (line.startsWith("a:")) {
|
||||||
|
audioController.enqueue(encoder.encode(line.slice(2)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending.startsWith("v:")) {
|
||||||
|
videoController.enqueue(encoder.encode(pending.slice(2)));
|
||||||
|
} else if (pending.startsWith("a:")) {
|
||||||
|
audioController.enqueue(encoder.encode(pending.slice(2)));
|
||||||
|
}
|
||||||
|
videoController.close();
|
||||||
|
audioController.close();
|
||||||
|
} catch (error) {
|
||||||
|
videoController.error(error);
|
||||||
|
audioController.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
video: new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
videoController = controller;
|
||||||
|
pump();
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
audio: new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
audioController = controller;
|
||||||
|
pump();
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
gopeed.events.onResolve(async function (ctx) {
|
||||||
|
if (ctx.req.url.includes("/single-fetch-stream")) {
|
||||||
|
const payloadSize = Number(new URL(ctx.req.url).searchParams.get("size")) || 0;
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(async () => createFetchDrainedStream(sourceURL(ctx.req.url)), {
|
||||||
|
size: payloadSize,
|
||||||
|
});
|
||||||
|
ctx.res = {
|
||||||
|
name: "single-fetch-stream",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "single.bin",
|
||||||
|
size: payloadSize,
|
||||||
|
req: { url },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.req.url.includes("/range-fetch-stream")) {
|
||||||
|
const payloadSize = Number(new URL(ctx.req.url).searchParams.get("size")) || 0;
|
||||||
|
const url = gopeed.runtime.blob.createObjectURL(
|
||||||
|
async ({ offset = 0, end = -1 }) => createFetchDrainedRangeStream(sourceURL(ctx.req.url), offset, end),
|
||||||
|
{
|
||||||
|
size: payloadSize,
|
||||||
|
range: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
ctx.res = {
|
||||||
|
name: "range-fetch-stream",
|
||||||
|
range: true,
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "range.bin",
|
||||||
|
size: payloadSize,
|
||||||
|
req: { url },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.req.url.includes("/multiplex-fetch-stream")) {
|
||||||
|
const videoChunks = ["video-0", "video-1", "video-2", "video-3"];
|
||||||
|
const audioChunks = ["audio-0", "audio-1", "audio-2", "audio-3"];
|
||||||
|
let streams;
|
||||||
|
const openStreams = () => {
|
||||||
|
if (!streams) {
|
||||||
|
streams = createMultiplexStreams(sourceURL(ctx.req.url));
|
||||||
|
}
|
||||||
|
return streams;
|
||||||
|
};
|
||||||
|
const videoUrl = gopeed.runtime.blob.createObjectURL(async () => openStreams().video, {
|
||||||
|
size: videoChunks.reduce((sum, chunk) => sum + byteLength(chunk), 0),
|
||||||
|
});
|
||||||
|
const audioUrl = gopeed.runtime.blob.createObjectURL(async () => openStreams().audio, {
|
||||||
|
size: audioChunks.reduce((sum, chunk) => sum + byteLength(chunk), 0),
|
||||||
|
});
|
||||||
|
ctx.res = {
|
||||||
|
name: "multiplex-fetch-stream",
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: "video.bin",
|
||||||
|
size: videoChunks.reduce((sum, chunk) => sum + byteLength(chunk), 0),
|
||||||
|
req: { url: videoUrl },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "audio.bin",
|
||||||
|
size: audioChunks.reduce((sum, chunk) => sum + byteLength(chunk), 0),
|
||||||
|
req: { url: audioUrl },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "blob-stream-bridge",
|
||||||
|
"title": "gopeed extension blob stream bridge test",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"scripts": [
|
||||||
|
{
|
||||||
|
"event": "onResolve",
|
||||||
|
"match": {
|
||||||
|
"urls": [
|
||||||
|
"*://*/single-fetch-stream*",
|
||||||
|
"*://*/range-fetch-stream*",
|
||||||
|
"*://*/multiplex-fetch-stream*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"entry": "index.js"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
-183
@@ -1,183 +0,0 @@
|
|||||||
gopeed.events.onResolve(async function (ctx) {
|
|
||||||
const encoder = new TextEncoder();
|
|
||||||
|
|
||||||
if (ctx.req.url.endsWith("/blob")) {
|
|
||||||
const blob = new Blob(["hello world"], {type: "text/plain"});
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-blob",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "hello.txt",
|
|
||||||
size: 11,
|
|
||||||
req: {
|
|
||||||
url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.req.url.includes("/http-stream")) {
|
|
||||||
const source = new URL(ctx.req.url);
|
|
||||||
const target = source.searchParams.get("target");
|
|
||||||
const pair = source.searchParams.get("pair") === "1";
|
|
||||||
const resumable = ctx.req.url.includes("/http-stream-range");
|
|
||||||
const fileName = source.searchParams.get("name") || "http-stream.bin";
|
|
||||||
const headResponse = await fetch(target, {method: "HEAD"});
|
|
||||||
const headLength = parseInt(headResponse.headers.get("content-length") || "", 10);
|
|
||||||
|
|
||||||
const createOpen = () => async (offset) => {
|
|
||||||
const headers = {};
|
|
||||||
if (offset > 0) {
|
|
||||||
headers.Range = `bytes=${offset}-`;
|
|
||||||
}
|
|
||||||
const response = await fetch(target, {headers});
|
|
||||||
if (!response.body) {
|
|
||||||
throw new Error("empty response body");
|
|
||||||
}
|
|
||||||
return response.body;
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-http-stream",
|
|
||||||
range: resumable,
|
|
||||||
files: pair ? [
|
|
||||||
{
|
|
||||||
name: "pair-video.bin",
|
|
||||||
size: Number.isFinite(headLength) ? headLength : 0,
|
|
||||||
req: {
|
|
||||||
url: URL.createObjectURL(createOpen()),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "pair-audio.bin",
|
|
||||||
size: Number.isFinite(headLength) ? headLength : 0,
|
|
||||||
req: {
|
|
||||||
url: URL.createObjectURL(createOpen()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
] : [
|
|
||||||
{
|
|
||||||
name: fileName,
|
|
||||||
size: Number.isFinite(headLength) ? headLength : 0,
|
|
||||||
req: {
|
|
||||||
url: URL.createObjectURL(createOpen()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.req.url.includes("/http-blob")) {
|
|
||||||
const source = new URL(ctx.req.url);
|
|
||||||
const target = source.searchParams.get("target");
|
|
||||||
const fileName = source.searchParams.get("name") || "http-blob.bin";
|
|
||||||
const headResponse = await fetch(target, {method: "HEAD"});
|
|
||||||
const headLength = parseInt(headResponse.headers.get("content-length") || "", 10);
|
|
||||||
const response = await fetch(target);
|
|
||||||
const blob = await response.blob();
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-http-blob",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: fileName,
|
|
||||||
size: Number.isFinite(headLength) ? headLength : 0,
|
|
||||||
req: {
|
|
||||||
url: URL.createObjectURL(blob),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.req.url.endsWith("/stream-range")) {
|
|
||||||
const payload = encoder.encode("line 1\nline 2\nline 3\n");
|
|
||||||
const firstChunkLength = encoder.encode("line 1\n").byteLength;
|
|
||||||
let firstAttempt = true;
|
|
||||||
|
|
||||||
const open = async (offset) => new ReadableStream({
|
|
||||||
async start(controller) {
|
|
||||||
try {
|
|
||||||
if (offset === 0 && firstAttempt) {
|
|
||||||
firstAttempt = false;
|
|
||||||
controller.enqueue(payload.slice(0, firstChunkLength));
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 120));
|
|
||||||
controller.error(new Error("resume required"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
controller.enqueue(payload.slice(offset));
|
|
||||||
controller.close();
|
|
||||||
} catch (err) {
|
|
||||||
controller.error(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const url = URL.createObjectURL(open);
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-stream-range",
|
|
||||||
range: true,
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "stream-range.txt",
|
|
||||||
size: payload.byteLength,
|
|
||||||
req: {
|
|
||||||
url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.req.url.endsWith("/stream-unknown")) {
|
|
||||||
const url = URL.createObjectURL(new ReadableStream({
|
|
||||||
async start(controller) {
|
|
||||||
controller.enqueue(encoder.encode("line 1\n"));
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 350));
|
|
||||||
controller.enqueue(encoder.encode("line 2\n"));
|
|
||||||
controller.close();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-stream-unknown",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "stream-unknown.txt",
|
|
||||||
req: {
|
|
||||||
url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = URL.createObjectURL(new ReadableStream({
|
|
||||||
start(controller) {
|
|
||||||
controller.enqueue(encoder.encode("line 1\n"));
|
|
||||||
controller.enqueue(encoder.encode("line 2\n"));
|
|
||||||
controller.close();
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-stream",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "stream.txt",
|
|
||||||
size: 14,
|
|
||||||
req: {
|
|
||||||
url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
gopeed.events.onResolve(async function (ctx) {
|
|
||||||
if (!ctx.req.url.endsWith("/recover")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = URL.createObjectURL(new ReadableStream({
|
|
||||||
async start(controller) {
|
|
||||||
controller.enqueue(new TextEncoder().encode("stale\n"));
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
||||||
controller.error(new Error("expired"));
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
ctx.res = {
|
|
||||||
name: "gblob-recover",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
name: "recover.txt",
|
|
||||||
req: {
|
|
||||||
url,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
gopeed.events.onError(async function (ctx) {
|
|
||||||
const req = ctx.task.meta.req;
|
|
||||||
if (!req.rawUrl || !req.rawUrl.endsWith("/recover")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
req.labels = req.labels || {};
|
|
||||||
if (req.labels.recovered === "true") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.labels.recovered = "true";
|
|
||||||
req.url = URL.createObjectURL(new Blob(["recovered\n"], { type: "text/plain" }));
|
|
||||||
ctx.task.continue();
|
|
||||||
});
|
|
||||||
+20
-4
@@ -67,6 +67,10 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func currentTestDownloadFile() string {
|
||||||
|
return filepath.Join(createOpts.Path, createOpts.Name)
|
||||||
|
}
|
||||||
|
|
||||||
func TestInfo(t *testing.T) {
|
func TestInfo(t *testing.T) {
|
||||||
matchKeys := []string{"version", "runtime", "os", "arch", "inDocker"}
|
matchKeys := []string{"version", "runtime", "os", "arch", "inDocker"}
|
||||||
doTest(func() {
|
doTest(func() {
|
||||||
@@ -109,7 +113,7 @@ func TestCreateTask(t *testing.T) {
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
want := test.FileMd5(test.BuildFile)
|
want := test.FileMd5(test.BuildFile)
|
||||||
got := test.FileMd5(test.DownloadFile)
|
got := test.FileMd5(currentTestDownloadFile())
|
||||||
if want != got {
|
if want != got {
|
||||||
t.Errorf("CreateTask() got = %v, want %v", got, want)
|
t.Errorf("CreateTask() got = %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
@@ -133,7 +137,7 @@ func TestCreateDirectTask(t *testing.T) {
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
want := test.FileMd5(test.BuildFile)
|
want := test.FileMd5(test.BuildFile)
|
||||||
got := test.FileMd5(test.DownloadFile)
|
got := test.FileMd5(currentTestDownloadFile())
|
||||||
if want != got {
|
if want != got {
|
||||||
t.Errorf("CreateDirectTask() got = %v, want %v", got, want)
|
t.Errorf("CreateDirectTask() got = %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
@@ -226,7 +230,7 @@ func TestPauseAndContinueTask(t *testing.T) {
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
want := test.FileMd5(test.BuildFile)
|
want := test.FileMd5(test.BuildFile)
|
||||||
got := test.FileMd5(test.DownloadFile)
|
got := test.FileMd5(currentTestDownloadFile())
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
t.Errorf("PauseAndContinueTask() got = %v, want %v", got, want)
|
t.Errorf("PauseAndContinueTask() got = %v, want %v", got, want)
|
||||||
}
|
}
|
||||||
@@ -344,7 +348,7 @@ func TestDeleteTaskForce(t *testing.T) {
|
|||||||
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId+"?force=true", nil)
|
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId+"?force=true", nil)
|
||||||
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
||||||
checkCode(code, model.CodeTaskNotFound)
|
checkCode(code, model.CodeTaskNotFound)
|
||||||
if _, err := os.Stat(test.DownloadFile); !errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(currentTestDownloadFile()); !errors.Is(err, os.ErrNotExist) {
|
||||||
t.Errorf("DeleteTaskForce() got = %v, want %v", err, os.ErrNotExist)
|
t.Errorf("DeleteTaskForce() got = %v, want %v", err, os.ErrNotExist)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -935,10 +939,22 @@ func doTest(handler func()) {
|
|||||||
|
|
||||||
func doTest0(onStart func(cfg *model.StartConfig), handler func()) {
|
func doTest0(onStart func(cfg *model.StartConfig), handler func()) {
|
||||||
testFunc := func(storage model.Storage) {
|
testFunc := func(storage model.Storage) {
|
||||||
|
downloadDir, err := os.MkdirTemp("", "gopeed-rest-test-download-")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(downloadDir)
|
||||||
|
oldCreatePath := createOpts.Path
|
||||||
|
createOpts.Path = downloadDir
|
||||||
|
defer func() {
|
||||||
|
createOpts.Path = oldCreatePath
|
||||||
|
}()
|
||||||
|
|
||||||
var cfg = &model.StartConfig{}
|
var cfg = &model.StartConfig{}
|
||||||
cfg.Init()
|
cfg.Init()
|
||||||
cfg.Storage = storage
|
cfg.Storage = storage
|
||||||
cfg.StorageDir = ".test_storage"
|
cfg.StorageDir = ".test_storage"
|
||||||
|
cfg.DownloadConfig = (&base.DownloaderStoreConfig{DownloadDir: downloadDir}).Init()
|
||||||
cfg.WebEnable = true
|
cfg.WebEnable = true
|
||||||
if onStart != nil {
|
if onStart != nil {
|
||||||
onStart(cfg)
|
onStart(cfg)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ part 'task.g.dart';
|
|||||||
|
|
||||||
enum Status { ready, running, pause, wait, error, done }
|
enum Status { ready, running, pause, wait, error, done }
|
||||||
|
|
||||||
enum Protocol { http, bt, ed2k, gblob }
|
enum Protocol { http, bt, ed2k }
|
||||||
|
|
||||||
// ExtractStatus enum matching Go backend
|
// ExtractStatus enum matching Go backend
|
||||||
enum ExtractStatus {
|
enum ExtractStatus {
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ const _$ProtocolEnumMap = {
|
|||||||
Protocol.http: 'http',
|
Protocol.http: 'http',
|
||||||
Protocol.bt: 'bt',
|
Protocol.bt: 'bt',
|
||||||
Protocol.ed2k: 'ed2k',
|
Protocol.ed2k: 'ed2k',
|
||||||
Protocol.gblob: 'gblob',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Progress _$ProgressFromJson(Map<String, dynamic> json) => Progress(
|
Progress _$ProgressFromJson(Map<String, dynamic> json) => Progress(
|
||||||
|
|||||||
Reference in new issue
Block a user