Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove node.js dependency in browser #1081

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions packages/docusaurus-theme-openapi-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/crypto-js": "^4.1.0",
"@types/file-saver": "^2.0.5",
"@types/lodash": "^4.14.176",
"@types/pako": "^2.0.3",
"concurrently": "^5.2.0",
"docusaurus-plugin-openapi-docs": "^4.3.3",
"docusaurus-plugin-sass": "^0.2.3",
Expand All @@ -47,10 +48,11 @@
"crypto-js": "^4.1.1",
"file-saver": "^2.0.5",
"lodash": "^4.17.20",
"node-polyfill-webpack-plugin": "^3.0.0",
"pako": "^2.1.0",
"postman-code-generators": "^1.10.1",
"postman-collection": "^4.4.0",
"prism-react-renderer": "^2.3.0",
"process": "^0.11.10",
"react-hook-form": "^7.43.8",
"react-live": "^4.0.0",
"react-magic-dropzone": "^1.0.1",
Expand All @@ -62,7 +64,6 @@
"sass": "^1.80.4",
"sass-loader": "^16.0.2",
"unist-util-visit": "^5.0.0",
"webpack": "^5.61.0",
"xml-formatter": "^2.6.1"
},
"peerDependencies": {
Expand Down
14 changes: 10 additions & 4 deletions packages/docusaurus-theme-openapi-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import path from "path";

import type { Plugin } from "@docusaurus/types";

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");

export default function docusaurusThemeOpenAPI(): Plugin<void> {
return {
name: "docusaurus-theme-openapi",
Expand Down Expand Up @@ -41,7 +39,11 @@ export default function docusaurusThemeOpenAPI(): Plugin<void> {
// Avoid conflicts with docusaurus-plugin-sass
if (sassLoaderRule.length === 0) {
return {
plugins: [new NodePolyfillPlugin()],
plugins: [
new utils.currentBundler.instance.ProvidePlugin({
process: require.resolve("process/browser"),
})
],
module: {
rules: [
{
Expand All @@ -60,7 +62,11 @@ export default function docusaurusThemeOpenAPI(): Plugin<void> {
};
}
return {
plugins: [new NodePolyfillPlugin()],
plugins: [
new utils.currentBundler.instance.ProvidePlugin({
process: require.resolve("process/browser"),
})
],
};
},
};
Expand Down
21 changes: 16 additions & 5 deletions packages/docusaurus-theme-openapi-docs/src/theme/ApiItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* LICENSE file in the root directory of this source tree.
* ========================================================================== */

import zlib from "zlib";

import React from "react";

import BrowserOnly from "@docusaurus/BrowserOnly";
Expand All @@ -23,13 +21,16 @@ import type { Props } from "@theme/DocItem";
import DocItemMetadata from "@theme/DocItem/Metadata";
import SkeletonLoader from "@theme/SkeletonLoader";
import clsx from "clsx";
import { ServerObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
import { ParameterObject } from "docusaurus-plugin-openapi-docs/src/openapi/types";
import {
ParameterObject,
ServerObject,
} from "docusaurus-plugin-openapi-docs/src/openapi/types";
import type { ApiItem as ApiItemType } from "docusaurus-plugin-openapi-docs/src/types";
import type {
DocFrontMatter,
ThemeConfig,
} from "docusaurus-theme-openapi-docs/src/types";
import { ungzip } from "pako";
import { Provider } from "react-redux";

import { createStoreWithoutState, createStoreWithState } from "./store";
Expand All @@ -52,6 +53,16 @@ interface SampleFrontMatter extends DocFrontMatter {
readonly sample?: any;
}

function base64ToUint8Array(base64: string) {
const binary = atob(base64);
const len = binary.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}

// @ts-ignore
export default function ApiItem(props: Props): JSX.Element {
const docHtmlClassName = `docs-doc-id-${props.content.metadata.id}`;
Expand All @@ -65,7 +76,7 @@ export default function ApiItem(props: Props): JSX.Element {
if (api) {
try {
api = JSON.parse(
zlib.inflateSync(Buffer.from(api as any, "base64") as any).toString()
new TextDecoder().decode(ungzip(base64ToUint8Array(api as any)))
);
} catch {}
}
Expand Down
Loading