-
Notifications
You must be signed in to change notification settings - Fork 196
Fix: node to use chunking for uploads #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ItzNotABug
wants to merge
4
commits into
master
Choose a base branch
from
fix-node-sync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,23 +1,110 @@ | ||
| import { File } from "node-fetch-native-with-agent"; | ||
| import { realpathSync, readFileSync } from "fs"; | ||
| import type { BinaryLike } from "crypto"; | ||
| import { basename } from "path"; | ||
| import { realpathSync } from "fs"; | ||
| import { promises as fs } from "fs"; | ||
| type BlobLike = { | ||
| size: number; | ||
| slice: (start: number, end: number) => BlobLike; | ||
| arrayBuffer: () => Promise<ArrayBuffer>; | ||
| }; | ||
|
|
||
| type InputFileSource = | ||
| | { type: 'path'; path: string } | ||
| | { type: 'buffer'; data: Buffer } | ||
| | { type: 'blob'; data: BlobLike }; | ||
|
|
||
| export class InputFile { | ||
| static fromBuffer( | ||
| parts: Blob | BinaryLike, | ||
| name: string | ||
| ): File { | ||
| return new File([parts], name); | ||
| private source: InputFileSource; | ||
| filename: string; | ||
|
|
||
| private constructor(source: InputFileSource, filename: string) { | ||
| this.source = source; | ||
| this.filename = filename; | ||
| } | ||
|
|
||
| static fromBuffer(parts: BlobLike | Buffer | Uint8Array | ArrayBuffer | string, name: string): InputFile { | ||
| if (parts && typeof (parts as BlobLike).arrayBuffer === 'function') { | ||
| return new InputFile({ type: 'blob', data: parts as BlobLike }, name); | ||
| } | ||
|
|
||
| if (Buffer.isBuffer(parts)) { | ||
| return new InputFile({ type: 'buffer', data: parts }, name); | ||
| } | ||
|
|
||
| if (typeof parts === 'string') { | ||
| return new InputFile({ type: 'buffer', data: Buffer.from(parts) }, name); | ||
| } | ||
|
|
||
| if (parts instanceof ArrayBuffer) { | ||
| return new InputFile({ type: 'buffer', data: Buffer.from(parts) }, name); | ||
| } | ||
|
|
||
| if (ArrayBuffer.isView(parts)) { | ||
| return new InputFile({ | ||
| type: 'buffer', | ||
| data: Buffer.from(parts.buffer, parts.byteOffset, parts.byteLength), | ||
| }, name); | ||
| } | ||
|
|
||
| throw new Error('Unsupported input type for InputFile.fromBuffer'); | ||
| } | ||
|
|
||
| static fromPath(path: string, name: string): File { | ||
| static fromPath(path: string, name?: string): InputFile { | ||
| const realPath = realpathSync(path); | ||
| const contents = readFileSync(realPath); | ||
| return this.fromBuffer(contents, name); | ||
| return new InputFile({ type: 'path', path: realPath }, name ?? basename(realPath)); | ||
| } | ||
|
|
||
| static fromPlainText(content: string, name: string): InputFile { | ||
| return new InputFile({ type: 'buffer', data: Buffer.from(content) }, name); | ||
| } | ||
|
|
||
| async size(): Promise<number> { | ||
| switch (this.source.type) { | ||
| case 'path': | ||
| return (await fs.stat(this.source.path)).size; | ||
| case 'buffer': | ||
| return this.source.data.length; | ||
| case 'blob': | ||
| return this.source.data.size; | ||
| } | ||
| } | ||
|
|
||
| async slice(start: number, end: number): Promise<Buffer> { | ||
| const length = end - start; | ||
|
|
||
| switch (this.source.type) { | ||
| case 'path': { | ||
| const handle = await fs.open(this.source.path, 'r'); | ||
| try { | ||
| const buffer = Buffer.alloc(length); | ||
| const result = await handle.read(buffer, 0, length, start); | ||
| return result.bytesRead === buffer.length ? buffer : buffer.subarray(0, result.bytesRead); | ||
| } finally { | ||
| await handle.close(); | ||
| } | ||
| } | ||
| case 'buffer': | ||
| return this.source.data.subarray(start, end); | ||
| case 'blob': { | ||
| const arrayBuffer = await this.source.data.slice(start, end).arrayBuffer(); | ||
| return Buffer.from(arrayBuffer); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async toFile(): Promise<File> { | ||
| const buffer = await this.toBuffer(); | ||
| return new File([buffer], this.filename); | ||
| } | ||
|
|
||
| static fromPlainText(content: string, name: string): File { | ||
| const arrayBytes = new TextEncoder().encode(content); | ||
| return this.fromBuffer(arrayBytes, name); | ||
| private async toBuffer(): Promise<Buffer> { | ||
| switch (this.source.type) { | ||
| case 'path': | ||
| return await fs.readFile(this.source.path); | ||
| case 'buffer': | ||
| return this.source.data; | ||
| case 'blob': | ||
| return Buffer.from(await this.source.data.arrayBuffer()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 139
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 5331
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 1812
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 110
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 4153
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 5378
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 48
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 576
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 1638
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 779
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 1944
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 48
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 543
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 656
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 101
🏁 Script executed:
Repository: appwrite/sdk-generator
Length of output: 1844
The entire
inputFile.tsmodule is incompatible with edge runtimes due tofsmodule imports, not justrealpathSync.The issue extends beyond
realpathSyncblocking: thefsmodule import itself (lines 2-4) will fail in edge runtimes like Cloudflare Workers or Vercel Edge Functions that don't support Node.js filesystem APIs. This makes the entireInputFileclass unavailable in these environments, even for methods likefromBufferthat don't require filesystem access.Since
client.ts.twigalready includes edge runtime detection and warnings (lines 58-59, 119-122), the SDK is designed to support edge runtimes. Either add conditional imports forfsandrealpathSync(gated by runtime detection) or provide fallback implementations for edge environments.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Based on this comment, make a PR that makes changes compatible with Edge. Goal is to prevent entire InputFile from breaking - instead, we just want exception at runtime if trying to use methods that require
fsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.