From 4832f3249df3415b168fcd3ac45eb1a463c038b8 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Fri, 6 May 2022 12:05:20 +0200 Subject: [PATCH] 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 https://github.com/swagger-api/swagger-editor/issues/3042 --- package-lock.json | 5 ----- package.json | 6 ++++-- src/execute/index.js | 2 +- src/execute/oas3/build-request.js | 3 ++- src/execute/swagger2/build-request.js | 2 +- src/helpers/btoa.browser.js | 19 +++++++++++++++++++ src/helpers/btoa.node.js | 15 +++++++++++++++ src/{helpers.js => helpers/index.js} | 0 src/index.js | 2 +- src/interfaces.js | 2 +- src/resolver.js | 2 +- src/subtree-resolver/index.js | 2 +- test/execute/main.js | 2 +- test/helpers.js | 2 +- test/oas3/execute/authorization.js | 3 +-- test/oas3/helpers.js | 2 +- 16 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 src/helpers/btoa.browser.js create mode 100644 src/helpers/btoa.node.js rename src/{helpers.js => helpers/index.js} (100%) diff --git a/package-lock.json b/package-lock.json index 2acd03626..ec313dab6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5806,11 +5806,6 @@ "node-int64": "^0.4.0" } }, - "btoa": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/btoa/-/btoa-1.2.1.tgz", - "integrity": "sha512-SB4/MIGlsiVkMcHmT+pSmIPoNDoHg+7cMzmt3Uxt628MTz2487DKSqK/fuhFBrkuqrYv5UCEnACpF4dTFNKc/g==" - }, "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", diff --git a/package.json b/package.json index 73bc1e523..e9dd4f9b0 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/execute/index.js b/src/execute/index.js index 7f1927159..afb5a9a1d 100755 --- a/src/execute/index.js +++ b/src/execute/index.js @@ -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 : []); diff --git a/src/execute/oas3/build-request.js b/src/execute/oas3/build-request.js index ff3abb8ce..ec48ff3c3 100644 --- a/src/execute/oas3/build-request.js +++ b/src/execute/oas3/build-request.js @@ -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; diff --git a/src/execute/swagger2/build-request.js b/src/execute/swagger2/build-request.js index 2fb18c4d7..f42f9be7b 100644 --- a/src/execute/swagger2/build-request.js +++ b/src/execute/swagger2/build-request.js @@ -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` diff --git a/src/helpers/btoa.browser.js b/src/helpers/btoa.browser.js new file mode 100644 index 000000000..b51bef337 --- /dev/null +++ b/src/helpers/btoa.browser.js @@ -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; diff --git a/src/helpers/btoa.node.js b/src/helpers/btoa.node.js new file mode 100644 index 000000000..85e46f655 --- /dev/null +++ b/src/helpers/btoa.node.js @@ -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; diff --git a/src/helpers.js b/src/helpers/index.js similarity index 100% rename from src/helpers.js rename to src/helpers/index.js diff --git a/src/index.js b/src/index.js index 4e928d50d..d872dee6c 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/src/interfaces.js b/src/interfaces.js index 6dfa7b762..d04ad20bd 100644 --- a/src/interfaces.js +++ b/src/interfaces.js @@ -1,4 +1,4 @@ -import { eachOperation, opId } from './helpers.js'; +import { eachOperation, opId } from './helpers/index.js'; const nullFn = () => null; diff --git a/src/resolver.js b/src/resolver.js index 504f6d924..e2a326a71 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -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 = {}) { diff --git a/src/subtree-resolver/index.js b/src/subtree-resolver/index.js index fe2f5faa2..210ef9c1a 100644 --- a/src/subtree-resolver/index.js +++ b/src/subtree-resolver/index.js @@ -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 { diff --git a/test/execute/main.js b/test/execute/main.js index ebe8667d1..27bef2d50 100644 --- a/test/execute/main.js +++ b/test/execute/main.js @@ -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 diff --git a/test/helpers.js b/test/helpers.js index 27f0592a4..819b1f05e 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -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', () => { diff --git a/test/oas3/execute/authorization.js b/test/oas3/execute/authorization.js index 115430f68..110e5a9f4 100644 --- a/test/oas3/execute/authorization.js +++ b/test/oas3/execute/authorization.js @@ -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 diff --git a/test/oas3/helpers.js b/test/oas3/helpers.js index 76461a68c..479aa9e23 100644 --- a/test/oas3/helpers.js +++ b/test/oas3/helpers.js @@ -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', () => {