Skip to content

Commit

Permalink
fix: use isomorphic solution for base64 encoding (#2526)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
char0n authored May 6, 2022
1 parent 0959c66 commit 4832f32
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 19 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"browser": {
"./src/http/fold-formdata-to-request.node.js": "./src/http/fold-formdata-to-request.browser.js",
"./lib/http/fold-formdata-to-request.node.js": "./lib/http/fold-formdata-to-request.browser.js",
"./es/http/fold-formdata-to-request.node.js": "./es/http/fold-formdata-to-request.browser.js"
"./es/http/fold-formdata-to-request.node.js": "./es/http/fold-formdata-to-request.browser.js",
"./src/helpers/btoa.node.js": "./src/helpers/btoa.browser.js",
"./lib/helpers/btoa.node.js": "./lib/helpers/btoa.browser.js",
"./es/helpers/btoa.node.js": "./es/helpers/btoa.browser.js"
},
"main": "lib/commonjs.js",
"module": "es/index.js",
Expand Down Expand Up @@ -106,7 +109,6 @@
},
"dependencies": {
"@babel/runtime-corejs3": "^7.11.2",
"btoa": "^1.2.1",
"cookie": "~0.5.0",
"cross-fetch": "^3.1.5",
"deepmerge": "~4.2.2",
Expand Down
2 changes: 1 addition & 1 deletion src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SWAGGER2_PARAMETER_BUILDERS from './swagger2/parameter-builders.js';
import * as OAS3_PARAMETER_BUILDERS from './oas3/parameter-builders.js';
import oas3BuildRequest from './oas3/build-request.js';
import swagger2BuildRequest from './swagger2/build-request.js';
import { getOperationRaw, legacyIdFromPathMethod, isOAS3 } from '../helpers.js';
import { getOperationRaw, legacyIdFromPathMethod, isOAS3 } from '../helpers/index.js';

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

Expand Down
3 changes: 2 additions & 1 deletion src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// `src/execute/index.js#buildRequest`
import { isPlainObject } from 'is-plain-object';
import get from 'lodash/get';
import btoa from 'btoa';

import btoa from '../../helpers/btoa.node.js';

export default function buildRequest(options, req) {
const { operation, requestBody, securities, spec, attachContentTypeForEmptyPayload } = options;
Expand Down
2 changes: 1 addition & 1 deletion src/execute/swagger2/build-request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import btoa from 'btoa';
import btoa from '../../helpers/btoa.node.js';

// This function runs after the common function,
// `src/execute/index.js#buildRequest`
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/btoa.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-undef, no-restricted-globals */

const globalObject = (() => {
// new standardized access to the global object
if (typeof globalThis !== 'undefined') {
return globalThis;
}

// WebWorker specific access
if (typeof self !== 'undefined') {
return self;
}

return window;
})();

const { btoa } = globalObject;

export default btoa;
15 changes: 15 additions & 0 deletions src/helpers/btoa.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Buffer } from 'buffer';

const btoa = (val) => {
let buffer;

if (val instanceof Buffer) {
buffer = val;
} else {
buffer = Buffer.from(val.toString(), 'binary');
}

return buffer.toString('base64');
};

export default btoa;
File renamed without changes.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Resolver, { clearCache } from './resolver.js';
import resolveSubtree from './subtree-resolver/index.js';
import { makeApisTagOperation } from './interfaces.js';
import { execute, buildRequest, baseUrl } from './execute/index.js';
import { opId } from './helpers.js';
import { opId } from './helpers/index.js';

Swagger.http = Http;
Swagger.makeHttp = makeHttp.bind(null, Swagger.http);
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eachOperation, opId } from './helpers.js';
import { eachOperation, opId } from './helpers/index.js';

const nullFn = () => null;

Expand Down
2 changes: 1 addition & 1 deletion src/resolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Http from './http/index.js';
import mapSpec, { plugins } from './specmap/index.js';
import { normalizeSwagger } from './helpers.js';
import { normalizeSwagger } from './helpers/index.js';
import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from './constants.js';

export function makeFetchJSON(http, opts = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/subtree-resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import get from 'lodash/get';

import resolve from '../resolver.js';
import { normalizeSwagger } from '../helpers.js';
import { normalizeSwagger } from '../helpers/index.js';

export default async function resolveSubtree(obj, path, opts = {}) {
const {
Expand Down
2 changes: 1 addition & 1 deletion test/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Readable } from 'stream';
import AbortController from 'abort-controller';

import { execute, buildRequest, self as stubs } from '../../src/execute/index.js';
import { normalizeSwagger } from '../../src/helpers.js';
import { normalizeSwagger } from '../../src/helpers/index.js';

// Supported shape... { spec, operationId, parameters, securities, fetch }
// One can use operationId or pathItem + method
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeSwagger, getOperationRaw, idFromPathMethod } from '../src/helpers.js';
import { normalizeSwagger, getOperationRaw, idFromPathMethod } from '../src/helpers/index.js';

describe('helpers', () => {
describe('idFromPathMethod', () => {
Expand Down
3 changes: 1 addition & 2 deletions test/oas3/execute/authorization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import btoa from 'btoa';

import btoa from '../../../src/helpers/btoa.node.js';
import { buildRequest } from '../../../src/execute/index.js';

// OAS 3.0 Authorization
Expand Down
2 changes: 1 addition & 1 deletion test/oas3/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isOAS3, isSwagger2 } from '../../src/helpers.js';
import { isOAS3, isSwagger2 } from '../../src/helpers/index.js';

describe('helpers - OpenAPI Specification 3.0', () => {
describe('isOAS3', () => {
Expand Down

0 comments on commit 4832f32

Please sign in to comment.