Skip to content

Commit 670fc69

Browse files
committed
fix: check type
1 parent e3b31ac commit 670fc69

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/files/NodeFilesHandler.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,56 @@ import { FormDataRequest } from 'types/API';
44

55
export class NodeFilesHandler extends BaseFilesHandler {
66
private async convertReadableStream(readableStream: ReadableStream): Promise<NodeJS.ReadableStream> {
7-
const { Readable } = await import('stream');
8-
const reader = readableStream.getReader();
7+
try {
8+
if (typeof window === 'undefined') {
9+
const { Readable } = await import('stream');
10+
const reader = readableStream.getReader();
911

10-
return new Readable({
11-
async read() {
12-
const { done, value } = await reader.read();
13-
if (done) {
14-
this.push(null);
15-
} else {
16-
this.push(value);
17-
}
18-
},
19-
});
12+
return new Readable({
13+
async read() {
14+
const { done, value } = await reader.read();
15+
if (done) {
16+
this.push(null);
17+
} else {
18+
this.push(value);
19+
}
20+
},
21+
});
22+
} else {
23+
throw new Error('Stream conversion is not supported in browser environment');
24+
}
25+
} catch (error) {
26+
console.error('Error in convertReadableStream:', error);
27+
throw error;
28+
}
2029
}
2130

2231
async prepareFormDataRequest(file: FilePathOrFileObject): Promise<FormDataRequest> {
2332
console.log('Preparing form data request for Node.js');
2433
try {
25-
const FormData = await import('form-data').then((m) => m.default || m);
34+
const FormData = await import('form-data').then(m => m.default || m);
2635
console.log('Successfully imported form-data module');
27-
36+
2837
const formData = new FormData();
2938
console.log('Created new FormData instance');
3039

3140
if (typeof file === 'string') {
32-
const fs = await import('fs').then((m) => m.default || m);
33-
if (!fs.existsSync(file)) {
34-
throw new Error(`File not found: ${file}`);
41+
if (typeof window === 'undefined') {
42+
const fs = await import('fs').then(m => m.default || m);
43+
if (!fs.existsSync(file)) {
44+
throw new Error(`File not found: ${file}`);
45+
}
46+
console.log(`Appending file from path: ${file}`);
47+
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
48+
} else {
49+
throw new Error('File system operations are not supported in browser environment');
3550
}
36-
console.log(`Appending file from path: ${file}`);
37-
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
3851
} else if (file && typeof file === 'object') {
3952
console.log('Processing file object:', file);
4053
if ('buffer' in file) {
41-
// Handle Node.js file-like object
4254
console.log('Appending file from buffer');
4355
formData.append('file', file.buffer, { filename: file.name, contentType: file.type });
4456
} else if ('stream' in file && typeof file.stream === 'function') {
45-
// Handle File object
4657
console.log('Converting and appending file from stream');
4758
const nodeStream = await this.convertReadableStream(file.stream());
4859
formData.append('file', nodeStream, { filename: file.name, contentType: file.type });

0 commit comments

Comments
 (0)