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
+5303
-3057
No files matched your search
@@ -0,0 +1,58 @@
|
||||
function createRangeBlobUrl(target) {
|
||||
return gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => {
|
||||
const headers = {};
|
||||
if (offset > 0 || end >= 0) {
|
||||
headers.Range = `bytes=${offset}-${end >= 0 ? end : ''}`;
|
||||
}
|
||||
const response = await fetch(target, { headers });
|
||||
if (!response.body) {
|
||||
throw new Error('empty response body');
|
||||
}
|
||||
return response.body;
|
||||
}, { size: 262144, range: true });
|
||||
}
|
||||
|
||||
gopeed.events.onResolve(async function (ctx) {
|
||||
if (!ctx.req.url.includes('/restart-range')) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.res = {
|
||||
name: 'blob-restart',
|
||||
range: true,
|
||||
files: [
|
||||
{
|
||||
name: 'restart.bin',
|
||||
size: 262144,
|
||||
req: {
|
||||
url: createRangeBlobUrl(ctx.req.rawUrl || ctx.req.url),
|
||||
rawUrl: ctx.req.rawUrl || ctx.req.url,
|
||||
labels: {
|
||||
mode: 'restart',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
gopeed.events.onError(async function (ctx) {
|
||||
const req = ctx.task?.meta?.req;
|
||||
if (!req || !req.rawUrl || !req.url || !req.url.includes('/__blob/')) {
|
||||
return;
|
||||
}
|
||||
req.labels = req.labels || {};
|
||||
if (req.labels.rebuilt === 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
req.url = createRangeBlobUrl(req.rawUrl);
|
||||
req.labels.started = 'true';
|
||||
req.labels.rebuilt = 'true';
|
||||
ctx.task.continue();
|
||||
} catch (error) {
|
||||
req.labels.rebuildError = String(error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user