Skip to content

Commit 9471039

Browse files
authored
Merge pull request #2797 from mikalai-snap/ms/2794-replace-fetch
Replaced fetch() with direct data access
2 parents 906e988 + 8ca7290 commit 9471039

File tree

8 files changed

+48
-46
lines changed

8 files changed

+48
-46
lines changed

src/lib/output/plugins/HierarchyPlugin.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { RendererComponent } from "../components.js";
33
import { RendererEvent } from "../events.js";
44
import { writeFile } from "../../utils/index.js";
55
import { DefaultTheme } from "../themes/default/DefaultTheme.js";
6-
import { gzip } from "zlib";
7-
import { promisify } from "util";
86

97
import type { Renderer } from "../index.js";
108
import {
@@ -13,8 +11,7 @@ import {
1311
getUniquePath,
1412
} from "../themes/lib.js";
1513
import type { DeclarationReflection } from "../../models/index.js";
16-
17-
const gzipP = promisify(gzip);
14+
import { compressJson } from "../../utils/compress.js";
1815

1916
interface JsonHierarchyElement {
2017
name: string;
@@ -102,13 +99,9 @@ export class HierarchyPlugin extends RendererComponent {
10299
"hierarchy.js",
103100
);
104101

105-
const gz = await gzipP(Buffer.from(JSON.stringify(hierarchy)));
106-
107102
await writeFile(
108103
hierarchyJs,
109-
`window.hierarchyData = "data:application/octet-stream;base64,${gz.toString(
110-
"base64",
111-
)}"`,
104+
`window.hierarchyData = "${await compressJson(hierarchy)}"`,
112105
);
113106
}
114107
}

src/lib/output/plugins/JavascriptIndexPlugin.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ import { RendererComponent } from "../components.js";
1212
import { IndexEvent, RendererEvent } from "../events.js";
1313
import { Option, writeFile } from "../../utils/index.js";
1414
import { DefaultTheme } from "../themes/default/DefaultTheme.js";
15-
import { gzip } from "zlib";
16-
import { promisify } from "util";
1715
import type { Renderer } from "../index.js";
1816
import { GroupPlugin } from "../../converter/plugins/GroupPlugin.js";
1917
import { CategoryPlugin } from "../../converter/plugins/CategoryPlugin.js";
20-
21-
const gzipP = promisify(gzip);
18+
import { compressJson } from "../../utils/compress.js";
2219

2320
/**
2421
* Keep this in sync with the interface in src/lib/output/themes/default/assets/typedoc/components/Search.ts
@@ -152,17 +149,13 @@ export class JavascriptIndexPlugin extends RendererComponent {
152149
"search.js",
153150
);
154151

155-
const jsonData = JSON.stringify({
152+
const data = {
156153
rows,
157154
index,
158-
});
159-
const data = await gzipP(Buffer.from(jsonData));
160-
155+
};
161156
await writeFile(
162157
jsonFileName,
163-
`window.searchData = "data:application/octet-stream;base64,${data.toString(
164-
"base64",
165-
)}";`,
158+
`window.searchData = "${await compressJson(data)}";`,
166159
);
167160

168161
if (

src/lib/output/plugins/NavigationPlugin.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import { RendererComponent } from "../components.js";
33
import { RendererEvent } from "../events.js";
44
import { writeFile } from "../../utils/index.js";
55
import { DefaultTheme } from "../themes/default/DefaultTheme.js";
6-
import { gzip } from "zlib";
7-
import { promisify } from "util";
86
import type { Renderer } from "../index.js";
9-
10-
const gzipP = promisify(gzip);
7+
import { compressJson } from "../../utils/compress.js";
118

129
export class NavigationPlugin extends RendererComponent {
1310
constructor(owner: Renderer) {
@@ -34,13 +31,10 @@ export class NavigationPlugin extends RendererComponent {
3431
const nav = (this.owner.theme as DefaultTheme).getNavigation(
3532
event.project,
3633
);
37-
const gz = await gzipP(Buffer.from(JSON.stringify(nav)));
3834

3935
await writeFile(
4036
navigationJs,
41-
`window.navigationData = "data:application/octet-stream;base64,${gz.toString(
42-
"base64",
43-
)}"`,
37+
`window.navigationData = "${await compressJson(nav)}"`,
4438
);
4539
}
4640
}

src/lib/output/themes/default/assets/typedoc/Hierarchy.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { decompressJson } from "./utils/decompress";
2+
13
declare global {
24
interface Window {
35
// Base64 encoded data url, gzipped, JSON encoded JsonHierarchy
@@ -106,14 +108,8 @@ async function buildHierarchyToggle() {
106108
);
107109
if (!container || !window.hierarchyData) return;
108110

109-
const res = await fetch(window.hierarchyData);
110-
const data = await res.arrayBuffer();
111-
const json = new Blob([data])
112-
.stream()
113-
.pipeThrough(new DecompressionStream("gzip"));
114-
115111
const baseReflId = +container.dataset.refl!;
116-
const hierarchy: JsonHierarchy = await new Response(json).json();
112+
const hierarchy: JsonHierarchy = await decompressJson(window.hierarchyData);
117113

118114
const collapsedHierarchy = container.querySelector("ul")!;
119115
const expandedHierarchy = document.createElement("ul");

src/lib/output/themes/default/assets/typedoc/Navigation.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { decompressJson } from "./utils/decompress";
2+
13
export interface NavigationElement {
24
text: string;
35
path?: string;
@@ -27,12 +29,9 @@ async function buildNav() {
2729
const container = document.getElementById("tsd-nav-container");
2830
if (!container || !window.navigationData) return;
2931

30-
const res = await fetch(window.navigationData);
31-
const data = await res.arrayBuffer();
32-
const json = new Blob([data])
33-
.stream()
34-
.pipeThrough(new DecompressionStream("gzip"));
35-
const nav: NavigationElement[] = await new Response(json).json();
32+
const nav: NavigationElement[] = await decompressJson(
33+
window.navigationData,
34+
);
3635

3736
BASE_URL = document.documentElement.dataset.base!;
3837
if (!BASE_URL.endsWith("/")) BASE_URL += "/";

src/lib/output/themes/default/assets/typedoc/components/Search.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { debounce } from "../utils/debounce.js";
22
import { Index } from "lunr";
3+
import { decompressJson } from "../utils/decompress.js";
34

45
/**
56
* Keep this in sync with the interface in src/lib/output/plugins/JavascriptIndexPlugin.ts
@@ -35,11 +36,7 @@ interface SearchState {
3536
async function updateIndex(state: SearchState, searchEl: HTMLElement) {
3637
if (!window.searchData) return;
3738

38-
const res = await fetch(window.searchData);
39-
const json = new Blob([await res.arrayBuffer()])
40-
.stream()
41-
.pipeThrough(new DecompressionStream("gzip"));
42-
const data: IData = await new Response(json).json();
39+
const data: IData = await decompressJson(window.searchData);
4340

4441
state.data = data;
4542
state.index = Index.load(data.index);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Decompresses Base64-encoded Gzip data and parses it into a JSON object.
3+
*
4+
* @param base64 - The Base64-encoded string representing the Gzip-compressed JSON string.
5+
* @returns A promise that resolves to the parsed JSON object.
6+
*/
7+
export async function decompressJson(base64: string) {
8+
const binaryData = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
9+
const blob = new Blob([binaryData]);
10+
const decompressedStream = blob
11+
.stream()
12+
.pipeThrough(new DecompressionStream("gzip"));
13+
const decompressedText = await new Response(decompressedStream).text();
14+
return JSON.parse(decompressedText);
15+
}

src/lib/utils/compress.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { gzip } from "zlib";
2+
import { promisify } from "util";
3+
4+
const gzipP = promisify(gzip);
5+
6+
/**
7+
* Compresses a JSON-serializable object into a Base64-encoded Gzip string.
8+
*
9+
* @param data - The JSON-serializable object to compress.
10+
* @returns A promise that resolves to a Base64-encoded string of the Gzip-compressed data.
11+
*/
12+
export async function compressJson(data: any) {
13+
const gz = await gzipP(Buffer.from(JSON.stringify(data)));
14+
return gz.toString("base64");
15+
}

0 commit comments

Comments
 (0)