From 7a8b8bdf361df0dca88357f29da19b96f7c7acae Mon Sep 17 00:00:00 2001 From: Martin Donadieu Date: Wed, 14 Aug 2024 15:38:27 +0100 Subject: [PATCH 1/4] fix: add explicit import to support CF workers --- src/sdk/client/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sdk/client/index.ts b/src/sdk/client/index.ts index 7d5ff01..01e237b 100644 --- a/src/sdk/client/index.ts +++ b/src/sdk/client/index.ts @@ -1,4 +1,5 @@ import fetch from 'cross-fetch'; +import { Buffer } from 'node:buffer'; import type { AnalyticsOptions, AuthenticationOptions } from '../interfaces'; import { NotAuthorizedError, RateLimitedError } from './errors'; From 406bca29e3baa8c38b594306cbb61e20df9553f1 Mon Sep 17 00:00:00 2001 From: Martin Donadieu Date: Wed, 14 Aug 2024 15:44:38 +0100 Subject: [PATCH 2/4] fix: use old shool one to pass tests --- src/sdk/client/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdk/client/index.ts b/src/sdk/client/index.ts index 01e237b..5a85605 100644 --- a/src/sdk/client/index.ts +++ b/src/sdk/client/index.ts @@ -1,5 +1,5 @@ import fetch from 'cross-fetch'; -import { Buffer } from 'node:buffer'; +import { Buffer } from 'buffer'; import type { AnalyticsOptions, AuthenticationOptions } from '../interfaces'; import { NotAuthorizedError, RateLimitedError } from './errors'; From dec0c920aaf7fb5a26810732779dfa4743094eb8 Mon Sep 17 00:00:00 2001 From: Martin Donadieu Date: Wed, 14 Aug 2024 16:05:42 +0100 Subject: [PATCH 3/4] fix: solve portability better by using btoa when available --- src/sdk/client/index.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/sdk/client/index.ts b/src/sdk/client/index.ts index 5a85605..63886ff 100644 --- a/src/sdk/client/index.ts +++ b/src/sdk/client/index.ts @@ -1,8 +1,17 @@ import fetch from 'cross-fetch'; -import { Buffer } from 'buffer'; import type { AnalyticsOptions, AuthenticationOptions } from '../interfaces'; import { NotAuthorizedError, RateLimitedError } from './errors'; +function encodeBase64(str: string): string { + if (typeof btoa === 'function') { + return btoa(str); + } else if (typeof Buffer !== 'undefined') { + return Buffer.from(str).toString('base64'); + } else { + throw new Error('Base64 encoding is not supported in this environment'); + } +} + export class BentoClient { private readonly _headers: HeadersInit = {}; private readonly _baseUrl: string = 'https://app.bentonow.com/api/v1'; @@ -90,9 +99,8 @@ export class BentoClient { * @returns HeadersInit */ private _extractHeaders(authentication: AuthenticationOptions): HeadersInit { - const authenticationKey = Buffer.from( - `${authentication.publishableKey}:${authentication.secretKey}` - ).toString('base64'); + const authenticationKey = encodeBase64(`${authentication.publishableKey}:${authentication.secretKey}`); + return { Authorization: `Basic ${authenticationKey}`, From fe9ac14904776201c918fd9dc7a43b49987b7351 Mon Sep 17 00:00:00 2001 From: Martin Donadieu Date: Wed, 14 Aug 2024 18:16:43 +0100 Subject: [PATCH 4/4] fix: add fallback implementation for others env --- src/sdk/client/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/sdk/client/index.ts b/src/sdk/client/index.ts index 63886ff..d3fa38c 100644 --- a/src/sdk/client/index.ts +++ b/src/sdk/client/index.ts @@ -8,7 +8,22 @@ function encodeBase64(str: string): string { } else if (typeof Buffer !== 'undefined') { return Buffer.from(str).toString('base64'); } else { - throw new Error('Base64 encoding is not supported in this environment'); + // Fallback implementation + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + let output = ''; + for (let block = 0, charCode, i = 0, map = chars; + str.charAt(i | 0) || (map = '=', i % 1); + output += map.charAt(63 & block >> 8 - i % 1 * 8)) { + + charCode = str.charCodeAt(i += 3/4); + + if (charCode > 0xFF) { + throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."); + } + + block = block << 8 | charCode; + } + return output; } }