Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 22, 14:30 UTC
feat(ts/core): refactor usage of node:buffer into Uint8Array, add eslint rules to enforce this
jkomyno committed
Jan 22, 2026 at 20:10 UTC
04cdce73f365d14f5c1fc02ca0ed553cd637a3d9
13 files changed
+110
-45
eslint.config.mjs
+21
@@ -19,6 +19,27 @@ export default [
19
{
20
rules: {
21
// "@typescript-eslint/no-var-requires": "off",
22
+ 'no-restricted-globals': ['error', 'Buffer'],
23
+ "no-restricted-imports": ["error", {
24
+ "paths": [
25
+ {
26
+ "name": "crypto",
27
+ "message": "Use Web Crypto API instead.",
28
+ },
29
+ {
30
+ "name": "node:crypto",
31
+ "message": "Use Web Crypto API instead.",
32
+ },
33
+ {
34
+ "name": "buffer",
35
+ "message": "Use Uint8Array instead.",
36
+ },
37
+ {
38
+ "name": "node:buffer",
39
+ "message": "Use Uint8Array instead.",
40
+ },
41
+ ],
42
+ }],
43
'@typescript-eslint/no-require-imports': 'warn',
44
'@typescript-eslint/no-unsafe-function-type': 'off',
45
'no-prototype-builtins': 'off',
ts/packages/core/eslint.config.mjs
+1
-1
@@ -1,5 +1,5 @@
1
module.exports = {
2
- extends: ['../../.eslintrc.cjs'],
2
+ extends: ['../../eslint.config.mjs'],
3
// parserOptions: {
4
// project: ['./tsconfig.json'],
5
// },
ts/packages/core/src/models/Files.node.ts
+1
-1
@@ -7,7 +7,7 @@
7
*/
8
import ComposioClient from '@composio/client';
9
import { FileDownloadData, FileUploadData } from '../types/files.types';
10
-import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../utils/fileUtils';
10
+import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../utils/fileUtils.node';
11
import { telemetry } from '../telemetry/Telemetry';
12
13
export class Files {
ts/packages/core/src/models/Files.workerd.ts
+1
-1
@@ -9,7 +9,7 @@ import ComposioClient from '@composio/client';
9
10
const UNSUPPORTED_MESSAGE =
11
'File operations (upload/download) are not supported in Cloudflare Workers or Edge runtimes. ' +
12
- 'These operations require Node.js-specific APIs (Buffer, crypto, file system) that are not available ' +
12
+ 'These operations require Node.js-specific APIs (e.g., `node:crypto`, `node:fs`) that are not available ' +
13
'in this environment. Please use a Node.js runtime for file operations.';
14
15
/**
ts/packages/core/src/platform/index.ts
+1
-1
@@ -1,2 +1,2 @@
1
-export type { Platform } from './types';
1
+export type { Platform, Uint8ArrayEncoding } from './types';
2
export { platform } from '#platform';
ts/packages/core/src/platform/node.ts
+17
-9
@@ -1,13 +1,13 @@
1
import * as fs from 'node:fs';
2
import * as os from 'node:os';
3
import * as path from 'node:path';
4
-import type { Platform } from './types';
4
+import type { Platform, Uint8ArrayEncoding } from './types';
5
6
/**
7
* Node.js platform implementation.
8
* Provides full file system and OS operations using Node.js built-in modules.
9
*/
10
-export const platform: Platform = {
10
+export const platform = {
11
supportsFileSystem: true,
12
13
homedir(): string | null {
@@ -34,18 +34,26 @@ export const platform: Platform = {
34
fs.mkdirSync(dirPath, { recursive: true });
35
},
36
37
- readFileSync(filePath: string, encoding?: BufferEncoding): string | Buffer {
38
- if (encoding) {
39
- return fs.readFileSync(filePath, encoding);
37
+ readFileSync(filePath: string, encoding?: Uint8ArrayEncoding): string | Uint8Array {
38
+ if (encoding === undefined) {
39
+ const buf = fs.readFileSync(filePath, { encoding: null });
40
+ const uint8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
41
+ return uint8;
42
}
41
- return fs.readFileSync(filePath);
43
+
44
+ const str = fs.readFileSync(filePath, { encoding });
45
+ return str;
46
},
47
44
- writeFileSync(filePath: string, content: string | Buffer, encoding?: BufferEncoding): void {
48
+ writeFileSync(
49
+ filePath: string,
50
+ content: string | Uint8Array,
51
+ encoding?: Uint8ArrayEncoding
52
+ ): void {
53
if (encoding && typeof content === 'string') {
46
- fs.writeFileSync(filePath, content, encoding);
54
+ fs.writeFileSync(filePath, content, { encoding });
55
} else {
56
fs.writeFileSync(filePath, content);
57
}
58
},
51
-};
59
+} as Platform;
ts/packages/core/src/platform/types.ts
+9
-5
@@ -39,18 +39,20 @@ export interface Platform {
39
/**
40
* Reads a file synchronously and returns its contents.
41
* @param filePath - The path to the file.
42
- * @param encoding - Optional encoding (e.g., 'utf8'). If not provided, returns a Buffer.
43
- * @returns The file contents as a string (if encoding provided) or Buffer.
42
+ * @param encoding - Optional encoding (e.g., 'utf8'). If not provided, returns an Uint8Array.
43
+ * @returns The file contents as a string (if encoding provided) or Uint8Array.
44
*/
45
- readFileSync(filePath: string, encoding?: BufferEncoding): string | Buffer;
45
+ readFileSync(filePath: string, encoding?: never): Uint8Array;
46
+ readFileSync(filePath: string, encoding: Uint8ArrayEncoding): string;
47
48
/**
49
* Writes content to a file synchronously.
50
* @param filePath - The path to the file.
50
- * @param content - The content to write (string or Buffer).
51
+ * @param content - The content to write (string or Uint8Array).
52
* @param encoding - Optional encoding for string content.
53
*/
53
- writeFileSync(filePath: string, content: string | Buffer, encoding?: BufferEncoding): void;
54
+ writeFileSync(filePath: string, content: Uint8Array, encoding?: never): void;
55
+ writeFileSync(filePath: string, content: string, encoding: Uint8ArrayEncoding): void;
56
57
/**
58
* Indicates whether this platform supports file system operations.
@@ -58,3 +60,5 @@ export interface Platform {
60
*/
61
readonly supportsFileSystem: boolean;
62
}
63
+
64
+export type Uint8ArrayEncoding = 'utf8';
ts/packages/core/src/platform/workerd.ts
+7
-3
@@ -1,4 +1,4 @@
1
-import type { Platform } from './types';
1
+import type { Platform, Uint8ArrayEncoding } from './types';
2
3
/**
4
* Cloudflare Workers / Edge runtime platform implementation.
@@ -41,14 +41,18 @@ export const platform: Platform = {
41
// No-op in edge runtimes - directories cannot be created
42
},
43
44
- readFileSync(_filePath: string, _encoding?: BufferEncoding): never {
44
+ readFileSync(_filePath: string, _encoding?: Uint8ArrayEncoding): never {
45
throw new Error(
46
'File system operations are not supported in this runtime environment (Cloudflare Workers/Edge). ' +
47
'Use environment variables or external storage services instead.'
48
);
49
},
50
51
- writeFileSync(_filePath: string, _content: string | Buffer, _encoding?: BufferEncoding): never {
51
+ writeFileSync(
52
+ _filePath: string,
53
+ _content: string | Uint8Array,
54
+ _encoding?: Uint8ArrayEncoding
55
+ ): never {
56
throw new Error(
57
'File system operations are not supported in this runtime environment (Cloudflare Workers/Edge). ' +
58
'Use environment variables or external storage services instead.'
ts/packages/core/src/utils/buffer.ts
+19
@@ -9,3 +9,22 @@ export function arrayBufferToBase64(buffer: ArrayBuffer) {
9
}
10
return btoa(binary);
11
}
12
+
13
+// Helper function to convert base64 string to Uint8Array (cross-platform)
14
+export const base64ToUint8Array = (base64: string): Uint8Array => {
15
+ const binary = atob(base64);
16
+ const bytes = new Uint8Array(binary.length);
17
+ for (let i = 0; i < binary.length; i++) {
18
+ bytes[i] = binary.charCodeAt(i);
19
+ }
20
+ return bytes;
21
+};
22
+
23
+// Helper function to convert Uint8Array to base64 string (cross-platform)
24
+export const uint8ArrayToBase64 = (bytes: Uint8Array): string => {
25
+ let binary = '';
26
+ for (let i = 0; i < bytes.length; i++) {
27
+ binary += String.fromCharCode(bytes[i]);
28
+ }
29
+ return btoa(binary);
30
+};
ts/packages/core/src/utils/fileUtils.node.ts
renamed
+28
-18
@@ -1,10 +1,12 @@
1
-import crypto from 'node:crypto';
1
+/* eslint-disable-next-line no-restricted-imports */
2
+import crypto from 'node:crypto'; // we're in a Node.js-specific module
3
+import { platform } from '#platform';
4
import ComposioClient from '@composio/client';
5
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
6
import logger from './logger';
5
-import { FileDownloadData, FileUploadData } from '../types/files.types';
7
import { getRandomShortId } from './uuid';
7
-import { platform } from '#platform';
8
+import { base64ToUint8Array, uint8ArrayToBase64 } from './buffer';
9
+import type { FileDownloadData, FileUploadData } from '../types/files.types';
10
11
// Helper function to get file extension from MIME type
12
const getExtensionFromMimeType = (mimeType: string): string => {
@@ -102,9 +104,10 @@ const readFileContent = async (
104
const content = platform.readFileSync(filePath);
105
return {
106
fileName: generateTimestampedFilename(filePath.split('.').pop() || 'txt'),
105
- content: Buffer.isBuffer(content)
106
- ? content.toString('base64')
107
- : Buffer.from(content).toString('base64'),
107
+ content:
108
+ content instanceof Uint8Array
109
+ ? uint8ArrayToBase64(content)
110
+ : uint8ArrayToBase64(new TextEncoder().encode(content)),
111
mimeType: 'application/octet-stream',
112
};
113
} catch (error) {
@@ -120,7 +123,7 @@ const readFileContentFromURL = async (
123
throw new Error(`Failed to fetch file: ${response.statusText}`);
124
}
125
const arrayBuffer = await response.arrayBuffer();
123
- const content = Buffer.from(arrayBuffer);
126
+ const content = new Uint8Array(arrayBuffer);
127
const mimeType = response.headers.get('content-type') || 'application/octet-stream';
128
129
// Extract clean filename from URL, removing query parameters
@@ -143,7 +146,7 @@ const readFileContentFromURL = async (
146
}
147
148
return {
146
- content: content.toString('base64'),
149
+ content: uint8ArrayToBase64(content),
150
mimeType,
151
fileName,
152
};
@@ -157,10 +160,11 @@ const uploadFileToS3 = async (
160
mimeType: string,
161
client: ComposioClient
162
): Promise<string> => {
163
+ const contentBytes = base64ToUint8Array(content);
164
const response = await client.files.createPresignedURL({
165
filename: fileName,
166
mimetype: mimeType,
163
- md5: crypto.createHash('md5').update(Buffer.from(content, 'base64')).digest('hex'),
167
+ md5: crypto.createHash('md5').update(contentBytes).digest('hex'),
168
tool_slug: toolSlug,
169
toolkit_slug: toolkitSlug,
170
});
@@ -169,17 +173,19 @@ const uploadFileToS3 = async (
173
174
if (type === 'new' || type === 'update') {
175
logger.debug(`Uploading ${key} file to S3: ${key}`);
172
- const buffer = Buffer.from(content, 'base64');
176
const signedURL =
177
response.type === 'update' ? response.update_presigned_url : response.new_presigned_url;
178
176
- // Upload the file using the presigned URL
179
+ // Create a new ArrayBuffer to ensure compatibility with fetch API
180
+ const uploadBuffer = new ArrayBuffer(contentBytes.byteLength);
181
+ new Uint8Array(uploadBuffer).set(contentBytes);
182
+
183
const uploadResponse = await fetch(signedURL, {
184
method: 'PUT',
179
- body: buffer,
185
+ body: uploadBuffer,
186
headers: {
187
'Content-Type': mimeType,
182
- 'Content-Length': buffer.length.toString(),
188
+ 'Content-Length': contentBytes.length.toString(),
189
},
190
});
191
@@ -201,7 +207,7 @@ const readFile = async (
207
const content = await file.arrayBuffer();
208
return {
209
fileName: file.name,
204
- content: Buffer.from(content).toString('base64'),
210
+ content: uint8ArrayToBase64(new Uint8Array(content)),
211
mimeType: file.type,
212
};
213
} else if (typeof file === 'string') {
@@ -266,7 +272,7 @@ export const downloadFileFromS3 = async ({
272
273
const extension = getExtensionFromMimeType(mimeType);
274
const fileName = generateTimestampedFilename(extension, `${toolSlug}_`);
269
- const filePath = saveFile(fileName, Buffer.from(data), true);
275
+ const filePath = saveFile(fileName, new Uint8Array(data), true);
276
return {
277
name: fileName,
278
mimeType: mimeType,
@@ -328,11 +334,15 @@ export const getComposioTempFilesDir = (createDirIfNotExists: boolean = false) =
334
/**
335
* Saves a file to the Composio directory.
336
* @param file - The name of the file to save.
331
- * @param content - The content of the file to save. Can be a string or Buffer.
337
+ * @param content - The content of the file to save. Can be a string or Uint8Array.
338
* @param isTempFile - Whether the file is a temporary file.
339
* @returns The path to the saved file.
340
*/
335
-export const saveFile = (file: string, content: string | Buffer, isTempFile: boolean = false) => {
341
+export const saveFile = (
342
+ file: string,
343
+ content: string | Uint8Array,
344
+ isTempFile: boolean = false
345
+) => {
346
try {
347
if (!platform.supportsFileSystem) {
348
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
356
357
logger.info(`Saving file to: ${filePath}`);
358
349
- if (Buffer.isBuffer(content)) {
359
+ if (content instanceof Uint8Array) {
360
platform.writeFileSync(filePath, content);
361
} else {
362
platform.writeFileSync(filePath, content, 'utf8');
ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
+1
-1
@@ -7,7 +7,7 @@ import {
7
import ComposioClient from '@composio/client';
8
import logger from '../logger';
9
import { ComposioFileUploadError } from '../../errors/FileModifierErrors';
10
-import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../fileUtils';
10
+import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../fileUtils.node';
11
import {
12
isPlainObject,
13
transformProperties,
ts/packages/core/test/tools/fileModifiers.test.ts
+3
-3
@@ -1,15 +1,15 @@
1
import { describe, it, expect, vi, beforeEach } from 'vitest';
2
-import { FileToolModifier } from '../../src/utils/modifiers/FileToolModifier';
2
+import { FileToolModifier } from '#file_tool_modifier';
3
import ComposioClient from '@composio/client';
4
import { Tool } from '../../src/types/tool.types';
5
import { ComposioFileUploadError } from '../../src/errors/FileModifierErrors';
6
-import * as fileUtils from '../../src/utils/fileUtils';
6
+import * as fileUtils from '../../src/utils/fileUtils.node';
7
import { Tools } from '../../src/models/Tools';
8
import { createTestContext, setupTest, mockToolExecution } from '../utils/toolExecuteUtils';
9
import { mockClient } from '../utils/mocks/client.mock';
10
11
// Mock the fileUtils module
12
-vi.mock('../../src/utils/fileUtils', () => ({
12
+vi.mock('../../src/utils/fileUtils.node', () => ({
13
downloadFileFromS3: vi.fn(),
14
getFileDataAfterUploadingToS3: vi.fn(),
15
}));
ts/packages/core/test/utils/fileUtils.test.ts
+1
-2
@@ -1,7 +1,6 @@
1
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
-import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils';
2
+import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils.node';
3
import ComposioClient from '@composio/client';
4
-import * as uuid from '../../src/utils/uuid';
4
5
// Mock the uuid module
6
vi.mock('../../src/utils/uuid', () => ({