Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 75 additions & 42 deletions app/common/adapter/binary/EdgedriverBinary.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import path from 'node:path';

import { SingletonProto } from 'egg';

import { BinaryType } from '../../enum/Binary.ts';
import { AbstractBinary, BinaryAdapter, type BinaryItem, type FetchResult } from './AbstractBinary.ts';

// Microsoft moved the Edge WebDriver download listing off the public
// Azure Blob container (the old XML listing API returns
// `PublicAccessNotPermitted` since ~2026-04-07). The new source of truth
// is a single JSON dump at https://msedgedriver.microsoft.com/listing.json
// which lists every version-prefixed driver file; individual files are
// downloadable from https://msedgedriver.microsoft.com/<name>.
const EDGEDRIVER_LISTING_URL = 'https://msedgedriver.microsoft.com/listing.json';
const EDGEDRIVER_DOWNLOAD_BASE = 'https://msedgedriver.microsoft.com/';

interface EdgedriverListingEntry {
isDirectory: boolean;
name: string;
contentLength: number;
lastModified: string;
}
interface EdgedriverListing {
items: EdgedriverListingEntry[];
generatedAt: string;
}

@SingletonProto()
@BinaryAdapter(BinaryType.Edgedriver)
export class EdgedriverBinary extends AbstractBinary {
private dirItems?: {
[key: string]: BinaryItem[];
};
// Promise-level cache for the full `listing.json` dump (~9000 entries).
// A single sync task calls `fetch('/<version>/')` for many versions;
// without this cache each call would re-download the listing.
// Reset in `initFetch` so each sync task gets fresh data.
#listingPromise?: Promise<EdgedriverListing | undefined>;

async initFetch() {
this.dirItems = undefined;
this.#listingPromise = undefined;
}

async #syncDirItems() {
Expand Down Expand Up @@ -169,51 +193,60 @@ export class EdgedriverBinary extends AbstractBinary {
}

// fetch sub dir
// /foo/ => foo/
// /126.0.2578.0/ => 126.0.2578.0/
const subDir = dir.slice(1);
// https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver?prefix=124.0.2478.97/&delimiter=/&maxresults=100&restype=container&comp=list
const url = `https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver?prefix=${encodeURIComponent(subDir)}&delimiter=/&maxresults=100&restype=container&comp=list`;
const xml = await this.requestXml(url);
return { items: this.#parseItems(xml), nextParams: null };
}

#parseItems(xml: string): BinaryItem[] {
const listing = await this.#fetchListing();
// Return undefined (not an empty-items FetchResult) on listing
// failure so the caller can distinguish "listing unavailable" from
// "this version exists but has no files".
if (!listing?.items) {
return;
}
const items: BinaryItem[] = [];
// <Blob><Name>124.0.2478.97/edgedriver_arm64.zip</Name><Url>https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver/124.0.2478.97/edgedriver_arm64.zip</Url><Properties><Last-Modified>Fri, 10 May 2024 18:35:44 GMT</Last-Modified><Etag>0x8DC712000713C13</Etag><Content-Length>9191362</Content-Length><Content-Type>application/octet-stream</Content-Type><Content-Encoding /><Content-Language /><Content-MD5>1tjPTf5JU6KKB06Qf1JOGw==</Content-MD5><Cache-Control /><BlobType>BlockBlob</BlobType><LeaseStatus>unlocked</LeaseStatus></Properties></Blob>
const fileRe =
/<Blob><Name>([^<]+?)<\/Name><Url>([^<]+?)<\/Url><Properties><Last-Modified>([^<]+?)<\/Last-Modified><Etag>(?:[^<]+?)<\/Etag><Content-Length>(\d+)<\/Content-Length>/g;
const matchItems = xml.matchAll(fileRe);
for (const m of matchItems) {
const fullname = m[1].trim();
// <Blob>
// <Name>124.0.2478.97/edgedriver_arm64.zip</Name>
// <Url>https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver/124.0.2478.97/edgedriver_arm64.zip</Url>
// <Properties>
// <Last-Modified>Fri, 10 May 2024 18:35:44 GMT</Last-Modified>
// <Etag>0x8DC712000713C13</Etag>
// <Content-Length>9191362</Content-Length>
// <Content-Type>application/octet-stream</Content-Type>
// <Content-Encoding/>
// <Content-Language/>
// <Content-MD5>1tjPTf5JU6KKB06Qf1JOGw==</Content-MD5>
// <Cache-Control/>
// <BlobType>BlockBlob</BlobType>
// <LeaseStatus>unlocked</LeaseStatus>
// </Properties>
// </Blob>
// ignore size = 0 dir
const name = path.basename(fullname);
const url = m[2].trim();
const date = m[3].trim();
const size = Number.parseInt(m[4].trim());
for (const entry of listing.items) {
if (entry.isDirectory) continue;
if (!entry.name.startsWith(subDir)) continue;
// Only direct children of `subDir`, not nested paths.
const rest = entry.name.slice(subDir.length);
if (!rest || rest.includes('/')) continue;
items.push({
name,
name: rest,
isDir: false,
url,
size,
date,
url: `${EDGEDRIVER_DOWNLOAD_BASE}${entry.name}`,
size: entry.contentLength,
date: entry.lastModified,
});
}
return items;
return { items, nextParams: null };
}

async #fetchListing(): Promise<EdgedriverListing | undefined> {
if (!this.#listingPromise) {
this.#listingPromise = this.#loadListing();
}
return this.#listingPromise;
}

async #loadListing(): Promise<EdgedriverListing | undefined> {
try {
// `AbstractBinary.requestJSON` already handles timeout / follow
// redirect / gzip / non-200 warn logging. It returns whatever
// `data` the server sent even on non-200, so we validate the
// shape before trusting it.
const listing = await this.requestJSON<EdgedriverListing>(EDGEDRIVER_LISTING_URL);
if (!listing?.items || !Array.isArray(listing.items)) {
return;
}
return listing;
} catch (err) {
this.logger.warn(
'[EdgedriverBinary.loadListing:request-failed] url: %s, error: %s',
EDGEDRIVER_LISTING_URL,
(err as Error).message,
);
// Clear the cached promise so the next sync task retries cleanly.
this.#listingPromise = undefined;
return;
}
}
}
48 changes: 44 additions & 4 deletions test/common/adapter/binary/EdgedriverBinary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ describe('test/common/adapter/binary/EdgedriverBinary.test.ts', () => {
data: await TestUtil.readFixturesFile('edgeupdates.json'),
persist: false,
});
app.mockHttpclient('https://msedgedriver.microsoft.com/listing.json', 'GET', {
data: await TestUtil.readFixturesFile('msedgedriver-listing.json'),
persist: false,
});
let result = await binary.fetch('/');
assert.deepEqual(result, {
items: [
Expand Down Expand Up @@ -63,17 +67,53 @@ describe('test/common/adapter/binary/EdgedriverBinary.test.ts', () => {
// {
// name: 'edgedriver_win64.zip',
// isDir: false,
// url: 'https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver/126.0.2578.0/edgedriver_win64.zip',
// url: 'https://msedgedriver.microsoft.com/126.0.2578.0/edgedriver_win64.zip',
// size: 9564395,
// date: 'Fri, 10 May 2024 17:04:10 GMT'
// date: '2024-05-10T17:04:10+00:00'
// }
assert.equal(item.isDir, false);
assert.match(item.name, /^edgedriver_\w+.zip$/);
assert.match(item.url, /^https:\/\//);
assert.match(item.name, /^edgedriver_[\w]+\.zip$/);
assert.match(item.url, /^https:\/\/msedgedriver\.microsoft\.com\//);
assert.ok(typeof item.size === 'number');
assert.ok(item.size > 0);
assert.ok(item.date);
}
});

it('should return undefined when listing.json request fails', async () => {
app.mockHttpclient('https://edgeupdates.microsoft.com/api/products', 'GET', {
data: await TestUtil.readFixturesFile('edgeupdates.json'),
persist: false,
});
app.mockHttpclient('https://msedgedriver.microsoft.com/listing.json', 'GET', {
data: '',
status: 500,
persist: false,
});
const result = await binary.fetch('/126.0.2578.0/');
assert.equal(result, undefined);
});

it('should cache listing.json across multiple sub-dir fetches', async () => {
app.mockHttpclient('https://edgeupdates.microsoft.com/api/products', 'GET', {
data: await TestUtil.readFixturesFile('edgeupdates.json'),
persist: false,
});
let listingCalls = 0;
const listingBuffer = await TestUtil.readFixturesFile('msedgedriver-listing.json');
app.mockHttpclient('https://msedgedriver.microsoft.com/listing.json', 'GET', () => {
listingCalls++;
return { data: listingBuffer, status: 200 };
});
// First sub-dir fetch triggers the network request.
const r1 = await binary.fetch('/126.0.2578.0/');
assert.ok(r1);
assert.ok(r1.items.length >= 3);
// Second sub-dir fetch should hit the cached listing, no extra request.
const r2 = await binary.fetch('/126.0.2578.0/');
assert.ok(r2);
assert.deepEqual(r2.items, r1.items);
assert.equal(listingCalls, 1);
});
});
});
35 changes: 35 additions & 0 deletions test/fixtures/msedgedriver-listing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"items": [
{
"isDirectory": false,
"name": "126.0.2578.0/edgedriver_arm64.zip",
"contentLength": 9085682,
"lastModified": "2024-05-10T17:03:37+00:00"
},
{
"isDirectory": false,
"name": "126.0.2578.0/edgedriver_mac64.zip",
"contentLength": 10907611,
"lastModified": "2024-05-10T16:37:33+00:00"
},
{
"isDirectory": false,
"name": "126.0.2578.0/edgedriver_mac64_m1.zip",
"contentLength": 10090619,
"lastModified": "2024-05-10T16:37:33+00:00"
},
{
"isDirectory": false,
"name": "126.0.2578.0/edgedriver_win32.zip",
"contentLength": 8759537,
"lastModified": "2024-05-10T17:04:29+00:00"
},
{
"isDirectory": false,
"name": "126.0.2578.0/edgedriver_win64.zip",
"contentLength": 9564395,
"lastModified": "2024-05-10T17:04:10+00:00"
}
],
"generatedAt": "2026-04-08T13:18:30.7241345+00:00"
}
Loading