-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exception thrown for large buffer -> base64 transforms (#29)
- Loading branch information
1 parent
3c3f882
commit fc01172
Showing
7 changed files
with
95 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'cf-bindings-proxy': patch | ||
--- | ||
|
||
Fix a maximum stack call exception from buffer -> base64 conversion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'cf-bindings-proxy': patch | ||
--- | ||
|
||
Fix blob -> base64 call for the arraybuffer not being awaited. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,67 @@ | ||
import type { PropertyCall } from './proxy'; | ||
|
||
/** | ||
* Transforms data from one format to another. | ||
* | ||
* @param data Data to transform. | ||
* @param transform Transform to apply. | ||
* @returns Transformed data. | ||
*/ | ||
export const transformData = (data: unknown, transform: { from: string; to: string }): unknown => { | ||
export const transformData = async ( | ||
data: unknown, | ||
transform: { from: string; to: string }, | ||
): Promise<unknown> => { | ||
if (transform.from === 'buffer' && transform.to === 'base64') { | ||
return btoa(String.fromCharCode(...new Uint8Array(data as ArrayBuffer))); | ||
const bytes = new Uint8Array(data as ArrayBuffer); | ||
let binary = ''; | ||
for (let i = 0; i < bytes.byteLength; i++) { | ||
binary += String.fromCharCode(bytes[i] as number); | ||
} | ||
return btoa(binary); | ||
} | ||
|
||
if (transform.from === 'base64' && transform.to === 'buffer') { | ||
return Uint8Array.from(atob(data as string), (c) => c.charCodeAt(0)).buffer; | ||
} | ||
|
||
if (transform.from === 'blob' && transform.to === 'base64') { | ||
const buffer = (data as Blob).arrayBuffer(); | ||
const buffer = await (data as Blob).arrayBuffer(); | ||
return transformData(buffer, { from: 'buffer', to: 'base64' }); | ||
} | ||
|
||
if (transform.from === 'base64' && transform.to === 'blob') { | ||
const buffer = transformData(data, { from: 'base64', to: 'buffer' }) as ArrayBuffer; | ||
const buffer = (await transformData(data, { from: 'base64', to: 'buffer' })) as ArrayBuffer; | ||
return new Blob([buffer]); | ||
} | ||
|
||
return data; | ||
}; | ||
|
||
/** | ||
* Prepares the argument's data to be sent over HTTP via the binding proxy. | ||
* This will transform any `ArrayBuffer` or `Blob` to `base64` and add the `transform` property. | ||
* | ||
* @param data The data to prepare. | ||
*/ | ||
export const prepareDataForProxy = async ( | ||
rawData: PropertyCallArg['data'], | ||
fallback: PropertyCallArg, | ||
): Promise<PropertyCallArg> => { | ||
if (rawData instanceof ArrayBuffer) { | ||
return { | ||
transform: { from: 'base64', to: 'buffer' }, | ||
data: await transformData(rawData, { from: 'buffer', to: 'base64' }), | ||
}; | ||
} | ||
|
||
if (rawData instanceof Blob) { | ||
return { | ||
transform: { from: 'base64', to: 'blob' }, | ||
data: await transformData(rawData, { from: 'blob', to: 'base64' }), | ||
}; | ||
} | ||
|
||
return fallback; | ||
}; | ||
|
||
type PropertyCallArg = PropertyCall['args'][0]; |