Skip to content

Commit

Permalink
Merge pull request #1487 from easyops-cn/steve/range-request-flag
Browse files Browse the repository at this point in the history
fix(): detect multipart range request support
  • Loading branch information
weareoutman authored Feb 15, 2025
2 parents 32803d7 + 39e67db commit 64aff00
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
12 changes: 6 additions & 6 deletions bricks/icons/src/fa-icon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ async function resolveIconDefinition(
id: string
): Promise<IconDefinition | null> {
// istanbul ignore next: experimental
if (supportsMultipartRangeRequest) {
try {
try {
if (await supportsMultipartRangeRequest()) {
const content = await faRangeRequest.fetch(id);
return JSON.parse(content);
} catch (error) {
// eslint-disable-next-line no-console
console.warn("Failed to fetch icon by range:", id, error);
// Fallback to traditional fetch.
}
} catch (error) {
// eslint-disable-next-line no-console
console.warn("Failed to fetch icon by range:", id, error);
// Fallback to traditional fetch.
}

try {
Expand Down
68 changes: 63 additions & 5 deletions bricks/icons/src/shared/RangeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// istanbul ignore file: experimental
import { has } from "lodash";
import { getRuntime } from "@next-core/runtime";
import antdIcons from "../antd-icon/generated/icons.json";
import easyopsIcons from "../easyops-icon/generated/icons.json";
import faIcons from "../fa-icon/generated/icons.json";
import antdRanges from "../antd-icon/generated/ranges.json";
import faRanges from "../fa-icon/generated/ranges.json";
import easyopsRanges from "../easyops-icon/generated/ranges.json";
import { getRuntime } from "@next-core/runtime";

const publicPath =
process.env.NODE_ENV === "test" ? "" : __webpack_public_path__;
Expand All @@ -29,10 +29,69 @@ const SETTINGS_MAP = {
},
} as unknown as Record<string, Settings>;

export let supportsMultipartRangeRequest =
const TEST_URL = `${publicPath}manifest.json`;

let supports =
process.env.NODE_ENV !== "test" &&
getRuntime?.()?.getFeatureFlags()["icons-multipart-range-request"];

const supportsPromise = new Promise<boolean>((resolve) => {
if (!supports) {
resolve(false);
return;
}

const waitSeconds = 3;
const timeout = setTimeout(() => {
// eslint-disable-next-line no-console
console.warn(
`Multipart range request test timed out after ${waitSeconds} seconds`
);
resolve(false);
}, waitSeconds * 1000);

(async () => {
const res = await fetch(TEST_URL, {
headers: {
Range: `bytes=0-1, 3-4`,
},
});

if (!res.ok || res.status !== 206) {
// eslint-disable-next-line no-console
console.warn(
`Multipart range request test failed with status: ${res.status} ${res.statusText}`
);
resolve(false);
clearTimeout(timeout);
return;
}

const contentType = res.headers.get("Content-Type");
if (
!contentType?.match(/\bmultipart\/byteranges;\s*\S*?boundary=([^\s;]+)/)
) {
// eslint-disable-next-line no-console
console.warn(
`Multipart range request test failed with unexpected Content-Type: "${contentType}"`
);
resolve(false);
clearTimeout(timeout);
return;
}

resolve(true);
clearTimeout(timeout);
})();
});

export async function supportsMultipartRangeRequest() {
if (!supports) {
return false;
}
return await supportsPromise;
}

type Lib = "antd" | "fa" | "easyops";

interface Settings {
Expand Down Expand Up @@ -378,7 +437,6 @@ async function request(
headers: {
Range: `bytes=${bytes.join(", ")}`,
},
cache: "force-cache",
});

if (!response.ok) {
Expand All @@ -394,7 +452,7 @@ async function request(
console.error(
`Unexpected response status: ${response.status} ${response.statusText}`
);
supportsMultipartRangeRequest = false;
supports = false;
return;
}

Expand All @@ -406,7 +464,7 @@ async function request(
if (!matches) {
// eslint-disable-next-line no-console
console.error(`Unexpected Content-Type: "${contentType}"`);
supportsMultipartRangeRequest = false;
supports = false;
return;
}

Expand Down
8 changes: 4 additions & 4 deletions bricks/icons/src/shared/SvgCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ async function resolveIcon(
let content: string | undefined;

// istanbul ignore next: experimental
if (options?.id && supportsMultipartRangeRequest) {
try {
try {
if (options?.id && (await supportsMultipartRangeRequest())) {
const rangeRequest =
options.lib === "easyops" ? easyopsRangeRequest : antdRangeRequest;
content = await rangeRequest.fetch(options.id);
} catch {
// Fallback to traditional fetch.
}
} catch {
// Fallback to traditional fetch.
}

if (!content) {
Expand Down

0 comments on commit 64aff00

Please sign in to comment.