Skip to content

Commit

Permalink
backed out buffer changes (#2640)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdarveau-MSFT authored Nov 25, 2024
1 parent 73a6890 commit d787b47
Show file tree
Hide file tree
Showing 10 changed files with 1,047 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Backed out `Buffer` removal changes",
"packageName": "@microsoft/teams-js",
"email": "109628470+noahdarveau-MSFT@users.noreply.github.com",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
}
},
"dependencies": {
"skeleton-buffer": "file:./skeleton-buffer",
"uuid": "^9.0.1"
}
}
2 changes: 1 addition & 1 deletion packages/teams-js/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = {
globals: {
PACKAGE_VERSION: packageVersion,
},
setupFilesAfterEnv: ['./jest-setup.cjs'],
//setupFilesAfterEnv: ['./jest-setup.cjs'],
};
2 changes: 1 addition & 1 deletion packages/teams-js/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default [
typescript(),
json(),
commonjs(),
nodePolyfills({ exclude: ['node_modules/**'] }),
nodePolyfills(),
],
treeshake: {
moduleSideEffects: [
Expand Down
6 changes: 3 additions & 3 deletions packages/teams-js/src/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Buffer } from 'buffer';
import * as uuid from 'uuid';

import { minAdaptiveCardVersion } from '../public/constants';
import { AdaptiveCardVersion, SdkError } from '../public/interfaces';
import * as pages from '../public/pages/pages';
import { base64ToString } from './uint8array-extras/uint8array-extras';

/**
* @internal
Expand Down Expand Up @@ -348,14 +348,14 @@ export function base64ToBlob(mimeType: string, base64String: string): Promise<Bl
* constructor expects binary data.
*/
if (mimeType.startsWith('image/')) {
const byteCharacters = base64ToString(base64String);
const byteCharacters = atob(base64String);
const byteArray = new Uint8Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteArray[i] = byteCharacters.charCodeAt(i);
}
resolve(new Blob([byteArray], { type: mimeType }));
}
const byteCharacters = base64ToString(base64String);
const byteCharacters = Buffer.from(base64String, 'base64').toString();
resolve(new Blob([byteCharacters], { type: mimeType }));
});
}
Expand Down
7 changes: 7 additions & 0 deletions packages/teams-js/webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module.exports = {
devtool: 'source-map',
resolve: {
extensions: ['.tsx', '.ts', '.js'],
fallback: {
buffer: require.resolve('skeleton-buffer/'),
},
},
module: {
rules: [
Expand Down Expand Up @@ -72,6 +75,10 @@ module.exports = {
// https://www.npmjs.com/package/webpack-subresource-integrity
new SubresourceIntegrityPlugin({ enabled: true }),

new ProvidePlugin({
Buffer: ['skeleton-buffer', 'Buffer'],
}),

// Webpackmanifest produces the json file containing asset(JS file) and its corresponding hash values(Example: https://github.com/waysact/webpack-subresource-integrity/blob/main/examples/webpack-assets-manifest/webpack.config.js)
new WebpackAssetsManifest({
integrity: true,
Expand Down
16 changes: 10 additions & 6 deletions pnpm-lock.yaml

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

94 changes: 94 additions & 0 deletions skeleton-buffer/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export class Buffer extends Uint8Array {
length: number;
write(string: string, offset?: number, length?: number, encoding?: string): number;
toString(encoding?: string, start?: number, end?: number): string;
slice(start?: number, end?: number): Buffer;

/**
* Allocates a new buffer containing the given {str}.
*
* @param str String to store in buffer.
* @param encoding encoding to use, optional. Default is 'utf8'
*/
constructor(str: string, encoding?: string);
/**
* Allocates a new buffer of {size} octets.
*
* @param size count of octets to allocate.
*/
constructor(size: number);
/**
* Allocates a new buffer containing the given {array} of octets.
*
* @param array The octets to store.
*/
constructor(array: Uint8Array);
/**
* Produces a Buffer backed by the same allocated memory as
* the given {ArrayBuffer}.
*
*
* @param arrayBuffer The ArrayBuffer with which to share memory.
*/
constructor(arrayBuffer: ArrayBuffer);
/**
* Allocates a new buffer containing the given {array} of octets.
*
* @param array The octets to store.
*/
constructor(array: any[]);
/**
* Copies the passed {buffer} data onto a new {Buffer} instance.
*
* @param buffer The buffer to copy.
*/
constructor(buffer: Buffer);
prototype: Buffer;
/**
* When passed a reference to the .buffer property of a TypedArray instance,
* the newly created Buffer will share the same allocated memory as the TypedArray.
* The optional {byteOffset} and {length} arguments specify a memory range
* within the {arrayBuffer} that will be shared by the Buffer.
*
* @param arrayBuffer The .buffer property of a TypedArray or a new ArrayBuffer()
* @param byteOffset
* @param length
*/
static from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer;
/**
* Creates a new Buffer containing the given JavaScript string {str}.
* If provided, the {encoding} parameter identifies the character encoding.
* If not provided, {encoding} defaults to 'utf8'.
*
* @param str
*/
static from(str: string, encoding?: string): Buffer;
/**
* Returns true if {obj} is a Buffer
*
* @param obj object to test.
*/
static isBuffer(obj: any): obj is Buffer;
/**
* Returns true if {encoding} is a valid encoding argument.
* Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
*
* @param encoding string to test.
*/
static isEncoding(encoding: string): boolean;
/**
* Gives the actual byte length of a string. encoding defaults to 'utf8'.
* This is not the same as String.prototype.length since that returns the number of characters in a string.
*
* @param string string to test.
* @param encoding encoding used to evaluate (defaults to 'utf8')
*/
static byteLength(string: string, encoding?: string): number;
/**
* Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents
* of the newly created Buffer are unknown and may contain sensitive data.
*
* @param size count of octets to allocate
*/
static allocUnsafe(size: number): Buffer;
}
Loading

0 comments on commit d787b47

Please sign in to comment.