Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

feat(ts/core): refactor usage of node:buffer into Uint8Array, add eslint rules to enforce this

jkomyno04cdce73f365
diff --git a/eslint.config.mjs b/eslint.config.mjs
index eb6fa1651..ddebc0850 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -19,6 +19,27 @@ export default [
{
rules: {
// "@typescript-eslint/no-var-requires": "off",
+ 'no-restricted-globals': ['error', 'Buffer'],
+ "no-restricted-imports": ["error", {
+ "paths": [
+ {
+ "name": "crypto",
+ "message": "Use Web Crypto API instead.",
+ },
+ {
+ "name": "node:crypto",
+ "message": "Use Web Crypto API instead.",
+ },
+ {
+ "name": "buffer",
+ "message": "Use Uint8Array instead.",
+ },
+ {
+ "name": "node:buffer",
+ "message": "Use Uint8Array instead.",
+ },
+ ],
+ }],
'@typescript-eslint/no-require-imports': 'warn',
'@typescript-eslint/no-unsafe-function-type': 'off',
'no-prototype-builtins': 'off',
diff --git a/ts/packages/core/eslint.config.mjs b/ts/packages/core/eslint.config.mjs
index 6af0ee0ef..81e042778 100644
--- a/ts/packages/core/eslint.config.mjs
+++ b/ts/packages/core/eslint.config.mjs
@@ -1,5 +1,5 @@
module.exports = {
- extends: ['../../.eslintrc.cjs'],
+ extends: ['../../eslint.config.mjs'],
// parserOptions: {
// project: ['./tsconfig.json'],
// },
diff --git a/ts/packages/core/src/models/Files.node.ts b/ts/packages/core/src/models/Files.node.ts
index 2c5561f94..590987194 100644
--- a/ts/packages/core/src/models/Files.node.ts
+++ b/ts/packages/core/src/models/Files.node.ts
@@ -7,7 +7,7 @@
*/
import ComposioClient from '@composio/client';
import { FileDownloadData, FileUploadData } from '../types/files.types';
-import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../utils/fileUtils';
+import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../utils/fileUtils.node';
import { telemetry } from '../telemetry/Telemetry';
export class Files {
diff --git a/ts/packages/core/src/models/Files.workerd.ts b/ts/packages/core/src/models/Files.workerd.ts
index 553dcd142..1da553f38 100644
--- a/ts/packages/core/src/models/Files.workerd.ts
+++ b/ts/packages/core/src/models/Files.workerd.ts
@@ -9,7 +9,7 @@ import ComposioClient from '@composio/client';
const UNSUPPORTED_MESSAGE =
'File operations (upload/download) are not supported in Cloudflare Workers or Edge runtimes. ' +
- 'These operations require Node.js-specific APIs (Buffer, crypto, file system) that are not available ' +
+ 'These operations require Node.js-specific APIs (e.g., `node:crypto`, `node:fs`) that are not available ' +
'in this environment. Please use a Node.js runtime for file operations.';
/**
diff --git a/ts/packages/core/src/platform/index.ts b/ts/packages/core/src/platform/index.ts
index e4e93824b..d6cb56bae 100644
--- a/ts/packages/core/src/platform/index.ts
+++ b/ts/packages/core/src/platform/index.ts
@@ -1,2 +1,2 @@
-export type { Platform } from './types';
+export type { Platform, Uint8ArrayEncoding } from './types';
export { platform } from '#platform';
diff --git a/ts/packages/core/src/platform/node.ts b/ts/packages/core/src/platform/node.ts
index b1a7df5b9..11947e322 100644
--- a/ts/packages/core/src/platform/node.ts
+++ b/ts/packages/core/src/platform/node.ts
@@ -1,13 +1,13 @@
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
-import type { Platform } from './types';
+import type { Platform, Uint8ArrayEncoding } from './types';
/**
* Node.js platform implementation.
* Provides full file system and OS operations using Node.js built-in modules.
*/
-export const platform: Platform = {
+export const platform = {
supportsFileSystem: true,
homedir(): string | null {
@@ -34,18 +34,26 @@ export const platform: Platform = {
fs.mkdirSync(dirPath, { recursive: true });
},
- readFileSync(filePath: string, encoding?: BufferEncoding): string | Buffer {
- if (encoding) {
- return fs.readFileSync(filePath, encoding);
+ readFileSync(filePath: string, encoding?: Uint8ArrayEncoding): string | Uint8Array {
+ if (encoding === undefined) {
+ const buf = fs.readFileSync(filePath, { encoding: null });
+ const uint8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
+ return uint8;
}
- return fs.readFileSync(filePath);
+
+ const str = fs.readFileSync(filePath, { encoding });
+ return str;
},
- writeFileSync(filePath: string, content: string | Buffer, encoding?: BufferEncoding): void {
+ writeFileSync(
+ filePath: string,
+ content: string | Uint8Array,
+ encoding?: Uint8ArrayEncoding
+ ): void {
if (encoding && typeof content === 'string') {
- fs.writeFileSync(filePath, content, encoding);
+ fs.writeFileSync(filePath, content, { encoding });
} else {
fs.writeFileSync(filePath, content);
}
},
-};
+} as Platform;
diff --git a/ts/packages/core/src/platform/types.ts b/ts/packages/core/src/platform/types.ts
index 6e59614dd..e8393c886 100644
--- a/ts/packages/core/src/platform/types.ts
+++ b/ts/packages/core/src/platform/types.ts
@@ -39,18 +39,20 @@ export interface Platform {
/**
* Reads a file synchronously and returns its contents.
* @param filePath - The path to the file.
- * @param encoding - Optional encoding (e.g., 'utf8'). If not provided, returns a Buffer.
- * @returns The file contents as a string (if encoding provided) or Buffer.
+ * @param encoding - Optional encoding (e.g., 'utf8'). If not provided, returns an Uint8Array.
+ * @returns The file contents as a string (if encoding provided) or Uint8Array.
*/
- readFileSync(filePath: string, encoding?: BufferEncoding): string | Buffer;
+ readFileSync(filePath: string, encoding?: never): Uint8Array;
+ readFileSync(filePath: string, encoding: Uint8ArrayEncoding): string;
/**
* Writes content to a file synchronously.
* @param filePath - The path to the file.
- * @param content - The content to write (string or Buffer).
+ * @param content - The content to write (string or Uint8Array).
* @param encoding - Optional encoding for string content.
*/
- writeFileSync(filePath: string, content: string | Buffer, encoding?: BufferEncoding): void;
+ writeFileSync(filePath: string, content: Uint8Array, encoding?: never): void;
+ writeFileSync(filePath: string, content: string, encoding: Uint8ArrayEncoding): void;
/**
* Indicates whether this platform supports file system operations.
@@ -58,3 +60,5 @@ export interface Platform {
*/
readonly supportsFileSystem: boolean;
}
+
+export type Uint8ArrayEncoding = 'utf8';
diff --git a/ts/packages/core/src/platform/workerd.ts b/ts/packages/core/src/platform/workerd.ts
index b125e6ffd..1dd4e500c 100644
--- a/ts/packages/core/src/platform/workerd.ts
+++ b/ts/packages/core/src/platform/workerd.ts
@@ -1,4 +1,4 @@
-import type { Platform } from './types';
+import type { Platform, Uint8ArrayEncoding } from './types';
/**
* Cloudflare Workers / Edge runtime platform implementation.
@@ -41,14 +41,18 @@ export const platform: Platform = {
// No-op in edge runtimes - directories cannot be created
},
- readFileSync(_filePath: string, _encoding?: BufferEncoding): never {
+ readFileSync(_filePath: string, _encoding?: Uint8ArrayEncoding): never {
throw new Error(
'File system operations are not supported in this runtime environment (Cloudflare Workers/Edge). ' +
'Use environment variables or external storage services instead.'
);
},
- writeFileSync(_filePath: string, _content: string | Buffer, _encoding?: BufferEncoding): never {
+ writeFileSync(
+ _filePath: string,
+ _content: string | Uint8Array,
+ _encoding?: Uint8ArrayEncoding
+ ): never {
throw new Error(
'File system operations are not supported in this runtime environment (Cloudflare Workers/Edge). ' +
'Use environment variables or external storage services instead.'
diff --git a/ts/packages/core/src/utils/buffer.ts b/ts/packages/core/src/utils/buffer.ts
index a8ca5715f..b2d3bda67 100644
--- a/ts/packages/core/src/utils/buffer.ts
+++ b/ts/packages/core/src/utils/buffer.ts
@@ -9,3 +9,22 @@ export function arrayBufferToBase64(buffer: ArrayBuffer) {
}
return btoa(binary);
}
+
+// Helper function to convert base64 string to Uint8Array (cross-platform)
+export const base64ToUint8Array = (base64: string): Uint8Array => {
+ const binary = atob(base64);
+ const bytes = new Uint8Array(binary.length);
+ for (let i = 0; i < binary.length; i++) {
+ bytes[i] = binary.charCodeAt(i);
+ }
+ return bytes;
+};
+
+// Helper function to convert Uint8Array to base64 string (cross-platform)
+export const uint8ArrayToBase64 = (bytes: Uint8Array): string => {
+ let binary = '';
+ for (let i = 0; i < bytes.length; i++) {
+ binary += String.fromCharCode(bytes[i]);
+ }
+ return btoa(binary);
+};
diff --git a/ts/packages/core/src/utils/fileUtils.ts b/ts/packages/core/src/utils/fileUtils.node.ts
similarity index 89%
rename from ts/packages/core/src/utils/fileUtils.ts
rename to ts/packages/core/src/utils/fileUtils.node.ts
index b4093ae5b..cd682db20 100644
--- a/ts/packages/core/src/utils/fileUtils.ts
+++ b/ts/packages/core/src/utils/fileUtils.node.ts
@@ -1,10 +1,12 @@
-import crypto from 'node:crypto';
+/* eslint-disable-next-line no-restricted-imports */
+import crypto from 'node:crypto'; // we're in a Node.js-specific module
+import { platform } from '#platform';
import ComposioClient from '@composio/client';
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
import logger from './logger';
-import { FileDownloadData, FileUploadData } from '../types/files.types';
import { getRandomShortId } from './uuid';
-import { platform } from '#platform';
+import { base64ToUint8Array, uint8ArrayToBase64 } from './buffer';
+import type { FileDownloadData, FileUploadData } from '../types/files.types';
// Helper function to get file extension from MIME type
const getExtensionFromMimeType = (mimeType: string): string => {
@@ -102,9 +104,10 @@ const readFileContent = async (
const content = platform.readFileSync(filePath);
return {
fileName: generateTimestampedFilename(filePath.split('.').pop() || 'txt'),
- content: Buffer.isBuffer(content)
- ? content.toString('base64')
- : Buffer.from(content).toString('base64'),
+ content:
+ content instanceof Uint8Array
+ ? uint8ArrayToBase64(content)
+ : uint8ArrayToBase64(new TextEncoder().encode(content)),
mimeType: 'application/octet-stream',
};
} catch (error) {
@@ -120,7 +123,7 @@ const readFileContentFromURL = async (
throw new Error(`Failed to fetch file: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
- const content = Buffer.from(arrayBuffer);
+ const content = new Uint8Array(arrayBuffer);
const mimeType = response.headers.get('content-type') || 'application/octet-stream';
// Extract clean filename from URL, removing query parameters
@@ -143,7 +146,7 @@ const readFileContentFromURL = async (
}
return {
- content: content.toString('base64'),
+ content: uint8ArrayToBase64(content),
mimeType,
fileName,
};
@@ -157,10 +160,11 @@ const uploadFileToS3 = async (
mimeType: string,
client: ComposioClient
): Promise<string> => {
+ const contentBytes = base64ToUint8Array(content);
const response = await client.files.createPresignedURL({
filename: fileName,
mimetype: mimeType,
- md5: crypto.createHash('md5').update(Buffer.from(content, 'base64')).digest('hex'),
+ md5: crypto.createHash('md5').update(contentBytes).digest('hex'),
tool_slug: toolSlug,
toolkit_slug: toolkitSlug,
});
@@ -169,17 +173,19 @@ const uploadFileToS3 = async (
if (type === 'new' || type === 'update') {
logger.debug(`Uploading ${key} file to S3: ${key}`);
- const buffer = Buffer.from(content, 'base64');
const signedURL =
response.type === 'update' ? response.update_presigned_url : response.new_presigned_url;
- // Upload the file using the presigned URL
+ // Create a new ArrayBuffer to ensure compatibility with fetch API
+ const uploadBuffer = new ArrayBuffer(contentBytes.byteLength);
+ new Uint8Array(uploadBuffer).set(contentBytes);
+
const uploadResponse = await fetch(signedURL, {
method: 'PUT',
- body: buffer,
+ body: uploadBuffer,
headers: {
'Content-Type': mimeType,
- 'Content-Length': buffer.length.toString(),
+ 'Content-Length': contentBytes.length.toString(),
},
});
@@ -201,7 +207,7 @@ const readFile = async (
const content = await file.arrayBuffer();
return {
fileName: file.name,
- content: Buffer.from(content).toString('base64'),
+ content: uint8ArrayToBase64(new Uint8Array(content)),
mimeType: file.type,
};
} else if (typeof file === 'string') {
@@ -266,7 +272,7 @@ export const downloadFileFromS3 = async ({
const extension = getExtensionFromMimeType(mimeType);
const fileName = generateTimestampedFilename(extension, `${toolSlug}_`);
- const filePath = saveFile(fileName, Buffer.from(data), true);
+ const filePath = saveFile(fileName, new Uint8Array(data), true);
return {
name: fileName,
mimeType: mimeType,
@@ -328,11 +334,15 @@ export const getComposioTempFilesDir = (createDirIfNotExists: boolean = false) =
/**
* Saves a file to the Composio directory.
* @param file - The name of the file to save.
- * @param content - The content of the file to save. Can be a string or Buffer.
+ * @param content - The content of the file to save. Can be a string or Uint8Array.
* @param isTempFile - Whether the file is a temporary file.
* @returns The path to the saved file.
*/
-export const saveFile = (file: string, content: string | Buffer, isTempFile: boolean = false) => {
+export const saveFile = (
+ file: string,
+ content: string | Uint8Array,
+ isTempFile: boolean = false
+) => {
try {
if (!platform.supportsFileSystem) {
logger.debug('File system operations are not supported in this runtime environment');
@@ -346,7 +356,7 @@ export const saveFile = (file: string, content: string | Buffer, isTempFile: boo
logger.info(`Saving file to: ${filePath}`);
- if (Buffer.isBuffer(content)) {
+ if (content instanceof Uint8Array) {
platform.writeFileSync(filePath, content);
} else {
platform.writeFileSync(filePath, content, 'utf8');
diff --git a/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts b/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
index e08507bf0..b2d4ce53b 100644
--- a/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
+++ b/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
@@ -7,7 +7,7 @@ import {
import ComposioClient from '@composio/client';
import logger from '../logger';
import { ComposioFileUploadError } from '../../errors/FileModifierErrors';
-import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../fileUtils';
+import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../fileUtils.node';
import {
isPlainObject,
transformProperties,
diff --git a/ts/packages/core/test/tools/fileModifiers.test.ts b/ts/packages/core/test/tools/fileModifiers.test.ts
index d07d3c399..63b981199 100644
--- a/ts/packages/core/test/tools/fileModifiers.test.ts
+++ b/ts/packages/core/test/tools/fileModifiers.test.ts
@@ -1,15 +1,15 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { FileToolModifier } from '../../src/utils/modifiers/FileToolModifier';
+import { FileToolModifier } from '#file_tool_modifier';
import ComposioClient from '@composio/client';
import { Tool } from '../../src/types/tool.types';
import { ComposioFileUploadError } from '../../src/errors/FileModifierErrors';
-import * as fileUtils from '../../src/utils/fileUtils';
+import * as fileUtils from '../../src/utils/fileUtils.node';
import { Tools } from '../../src/models/Tools';
import { createTestContext, setupTest, mockToolExecution } from '../utils/toolExecuteUtils';
import { mockClient } from '../utils/mocks/client.mock';
// Mock the fileUtils module
-vi.mock('../../src/utils/fileUtils', () => ({
+vi.mock('../../src/utils/fileUtils.node', () => ({
downloadFileFromS3: vi.fn(),
getFileDataAfterUploadingToS3: vi.fn(),
}));
diff --git a/ts/packages/core/test/utils/fileUtils.test.ts b/ts/packages/core/test/utils/fileUtils.test.ts
index 029ab4a5b..2bad3aaad 100644
--- a/ts/packages/core/test/utils/fileUtils.test.ts
+++ b/ts/packages/core/test/utils/fileUtils.test.ts
@@ -1,7 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils';
+import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils.node';
import ComposioClient from '@composio/client';
-import * as uuid from '../../src/utils/uuid';
// Mock the uuid module
vi.mock('../../src/utils/uuid', () => ({