-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add bundle command #84
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dfdcd39
ci: only create sync pr and do not merge
designcode 8f7422f
chore: lint ts files scripts folder
designcode df1510f
feat: add bundle command for downloading multiple objects as tar archive
designcode 6d811bf
fix: address PR review feedback for bundle command
designcode 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
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 |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { getStorageConfig } from '@auth/provider.js'; | ||
| import { bundle } from '@tigrisdata/storage'; | ||
| import { exitWithError } from '@utils/exit.js'; | ||
| import { getFormat, getOption } from '@utils/options.js'; | ||
| import { parseAnyPath } from '@utils/path.js'; | ||
| import { createWriteStream, existsSync, readFileSync } from 'fs'; | ||
| import { Readable } from 'stream'; | ||
| import { pipeline } from 'stream/promises'; | ||
|
|
||
| const MAX_KEYS = 5000; | ||
|
|
||
| async function readStdin(): Promise<string> { | ||
| const chunks: Buffer[] = []; | ||
| for await (const chunk of process.stdin) { | ||
| chunks.push(chunk); | ||
| } | ||
| return Buffer.concat(chunks).toString('utf-8'); | ||
| } | ||
|
|
||
| function parseKeys(content: string): string[] { | ||
| return content | ||
| .split('\n') | ||
| .map((line) => line.trim()) | ||
| .filter((line) => line.length > 0 && !line.startsWith('#')); | ||
| } | ||
|
|
||
| function detectCompression( | ||
| outputPath: string | ||
| ): 'none' | 'gzip' | 'zstd' | undefined { | ||
| if (outputPath.endsWith('.tar.gz') || outputPath.endsWith('.tgz')) { | ||
| return 'gzip'; | ||
| } | ||
| if (outputPath.endsWith('.tar.zst')) { | ||
| return 'zstd'; | ||
| } | ||
| if (outputPath.endsWith('.tar')) { | ||
| return 'none'; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export default async function bundleCommand(options: Record<string, unknown>) { | ||
| const bucketArg = getOption<string>(options, ['bucket']); | ||
| const keysArg = getOption<string>(options, ['keys', 'k']); | ||
| const outputPath = getOption<string>(options, ['output', 'o']); | ||
| const compressionArg = getOption<string>(options, ['compression']); | ||
| const onError = getOption<string>(options, ['on-error', 'onError'], 'skip'); | ||
| const format = getFormat(options); | ||
| const jsonMode = format === 'json'; | ||
|
|
||
| // stdout carries binary data when no --output | ||
| const stdoutBinary = !outputPath; | ||
|
|
||
| if (!bucketArg) { | ||
| exitWithError('Bucket is required'); | ||
| } | ||
|
|
||
| const { bucket, path: prefix } = parseAnyPath(bucketArg); | ||
|
|
||
| if (!bucket) { | ||
| exitWithError('Invalid bucket'); | ||
| } | ||
|
|
||
| // Resolve keys: file, inline, or stdin | ||
| let keys: string[]; | ||
|
|
||
| if (keysArg) { | ||
| if (existsSync(keysArg)) { | ||
| keys = parseKeys(readFileSync(keysArg, 'utf-8')); | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| keys = keysArg | ||
| .split(',') | ||
| .map((k) => k.trim()) | ||
| .filter((k) => k.length > 0); | ||
| } | ||
| } else if (!process.stdin.isTTY) { | ||
| const input = await readStdin(); | ||
| keys = parseKeys(input); | ||
| } else { | ||
| exitWithError('Keys are required. Provide via --keys or pipe to stdin.'); | ||
| } | ||
|
|
||
| // Prepend path prefix from bucket arg (e.g. t3://bucket/prefix) | ||
| if (prefix) { | ||
| const normalizedPrefix = prefix.endsWith('/') ? prefix : `${prefix}/`; | ||
| keys = keys.map((key) => `${normalizedPrefix}${key}`); | ||
| } | ||
|
|
||
| if (keys.length === 0) { | ||
| exitWithError('No keys found'); | ||
| } | ||
|
|
||
| if (keys.length > MAX_KEYS) { | ||
| exitWithError(`Too many keys (max ${MAX_KEYS}). Got ${keys.length}`); | ||
| } | ||
|
|
||
| // Resolve compression: explicit flag > auto-detect from extension > default | ||
| let compression: 'none' | 'gzip' | 'zstd' = 'none'; | ||
| if (compressionArg) { | ||
| compression = compressionArg as 'none' | 'gzip' | 'zstd'; | ||
| } else if (outputPath) { | ||
| compression = detectCompression(outputPath) ?? 'none'; | ||
| } | ||
|
|
||
| if (!stdoutBinary && !jsonMode) { | ||
| process.stderr.write(`Bundling ${keys.length} object(s)...\n`); | ||
| } | ||
|
|
||
| const config = await getStorageConfig({ withCredentialProvider: true }); | ||
|
|
||
| const { data, error } = await bundle(keys, { | ||
| config: { ...config, bucket }, | ||
| compression, | ||
| onError: onError as 'skip' | 'fail', | ||
| }); | ||
|
|
||
| if (error) { | ||
| exitWithError(error); | ||
| } | ||
|
|
||
| const nodeStream = Readable.fromWeb(data.body as ReadableStream); | ||
|
|
||
| if (outputPath) { | ||
| const writeStream = createWriteStream(outputPath); | ||
| await pipeline(nodeStream, writeStream); | ||
|
|
||
| if (jsonMode) { | ||
| console.log( | ||
| JSON.stringify({ | ||
| action: 'bundled', | ||
| bucket, | ||
| keys: keys.length, | ||
| compression, | ||
| output: outputPath, | ||
| }) | ||
| ); | ||
| } else { | ||
| console.log( | ||
| `Bundled ${keys.length} object(s) from '${bucket}' to ${outputPath}` | ||
| ); | ||
| } | ||
| } else { | ||
| await pipeline(nodeStream, process.stdout); | ||
|
|
||
| if (jsonMode) { | ||
| console.error( | ||
| JSON.stringify({ | ||
| action: 'bundled', | ||
| bucket, | ||
| keys: keys.length, | ||
| compression, | ||
| output: 'stdout', | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| process.exit(0); | ||
| } | ||
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
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.