Skip to content

Commit 4832f32

Browse files
authored
fix: use isomorphic solution for base64 encoding (#2526)
btoa npm dependency was using Buffer without an import. This causes probles in bundler environemnt and Node.js polyfills needs to be applied. Refs swagger-api/swagger-editor#3042
1 parent 0959c66 commit 4832f32

File tree

16 files changed

+50
-19
lines changed

16 files changed

+50
-19
lines changed

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"browser": {
66
"./src/http/fold-formdata-to-request.node.js": "./src/http/fold-formdata-to-request.browser.js",
77
"./lib/http/fold-formdata-to-request.node.js": "./lib/http/fold-formdata-to-request.browser.js",
8-
"./es/http/fold-formdata-to-request.node.js": "./es/http/fold-formdata-to-request.browser.js"
8+
"./es/http/fold-formdata-to-request.node.js": "./es/http/fold-formdata-to-request.browser.js",
9+
"./src/helpers/btoa.node.js": "./src/helpers/btoa.browser.js",
10+
"./lib/helpers/btoa.node.js": "./lib/helpers/btoa.browser.js",
11+
"./es/helpers/btoa.node.js": "./es/helpers/btoa.browser.js"
912
},
1013
"main": "lib/commonjs.js",
1114
"module": "es/index.js",
@@ -106,7 +109,6 @@
106109
},
107110
"dependencies": {
108111
"@babel/runtime-corejs3": "^7.11.2",
109-
"btoa": "^1.2.1",
110112
"cookie": "~0.5.0",
111113
"cross-fetch": "^3.1.5",
112114
"deepmerge": "~4.2.2",

src/execute/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SWAGGER2_PARAMETER_BUILDERS from './swagger2/parameter-builders.js';
99
import * as OAS3_PARAMETER_BUILDERS from './oas3/parameter-builders.js';
1010
import oas3BuildRequest from './oas3/build-request.js';
1111
import swagger2BuildRequest from './swagger2/build-request.js';
12-
import { getOperationRaw, legacyIdFromPathMethod, isOAS3 } from '../helpers.js';
12+
import { getOperationRaw, legacyIdFromPathMethod, isOAS3 } from '../helpers/index.js';
1313

1414
const arrayOrEmpty = (ar) => (Array.isArray(ar) ? ar : []);
1515

src/execute/oas3/build-request.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// `src/execute/index.js#buildRequest`
33
import { isPlainObject } from 'is-plain-object';
44
import get from 'lodash/get';
5-
import btoa from 'btoa';
5+
6+
import btoa from '../../helpers/btoa.node.js';
67

78
export default function buildRequest(options, req) {
89
const { operation, requestBody, securities, spec, attachContentTypeForEmptyPayload } = options;

src/execute/swagger2/build-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import btoa from 'btoa';
1+
import btoa from '../../helpers/btoa.node.js';
22

33
// This function runs after the common function,
44
// `src/execute/index.js#buildRequest`

src/helpers/btoa.browser.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-undef, no-restricted-globals */
2+
3+
const globalObject = (() => {
4+
// new standardized access to the global object
5+
if (typeof globalThis !== 'undefined') {
6+
return globalThis;
7+
}
8+
9+
// WebWorker specific access
10+
if (typeof self !== 'undefined') {
11+
return self;
12+
}
13+
14+
return window;
15+
})();
16+
17+
const { btoa } = globalObject;
18+
19+
export default btoa;

src/helpers/btoa.node.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Buffer } from 'buffer';
2+
3+
const btoa = (val) => {
4+
let buffer;
5+
6+
if (val instanceof Buffer) {
7+
buffer = val;
8+
} else {
9+
buffer = Buffer.from(val.toString(), 'binary');
10+
}
11+
12+
return buffer.toString('base64');
13+
};
14+
15+
export default btoa;
File renamed without changes.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Resolver, { clearCache } from './resolver.js';
55
import resolveSubtree from './subtree-resolver/index.js';
66
import { makeApisTagOperation } from './interfaces.js';
77
import { execute, buildRequest, baseUrl } from './execute/index.js';
8-
import { opId } from './helpers.js';
8+
import { opId } from './helpers/index.js';
99

1010
Swagger.http = Http;
1111
Swagger.makeHttp = makeHttp.bind(null, Swagger.http);

src/interfaces.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { eachOperation, opId } from './helpers.js';
1+
import { eachOperation, opId } from './helpers/index.js';
22

33
const nullFn = () => null;
44

0 commit comments

Comments
 (0)