Skip to content

Implement Builder API for creating tar archives#90

Merged
Brooooooklyn merged 5 commits intomainfrom
copilot/fix-b81ab372-054e-4c14-ba01-63cd89375f81
Aug 11, 2025
Merged

Implement Builder API for creating tar archives#90
Brooooooklyn merged 5 commits intomainfrom
copilot/fix-b81ab372-054e-4c14-ba01-63cd89375f81

Conversation

Copy link
Contributor

Copilot AI commented Aug 11, 2025

This PR implements the Builder API for creating tar archives, addressing issue #3. The implementation provides a complete solution for programmatically creating tar archives from various sources.

New Builder Class

The Builder API mirrors the functionality of Rust's tar::Builder and provides the following methods:

export class Builder {
  constructor(output?: string)           // Create builder with optional file output
  appendData(name: string, data: Uint8Array): void     // Add raw data
  appendFile(name: string, src: string): void          // Add file from disk  
  appendDirAll(name: string, src: string): void        // Add directory recursively
  finish(): Array<number> | null                       // Finalize archive
}

Usage Examples

Creating archives in memory:

import { Builder } from '@napi-rs/tar'

const builder = new Builder()
builder.appendData('hello.txt', Buffer.from('Hello, world!'))
builder.appendFile('package.json', './package.json')
builder.appendDirAll('src', './src')

const archiveData = builder.finish() // Returns Uint8Array

Creating archives to file:

const builder = new Builder('./output.tar')
builder.appendData('readme.txt', Buffer.from('Archive contents'))
builder.finish() // Returns null, data written to file

Key Features

  • Dual output modes: Create archives in memory or write directly to files
  • Multiple content sources: Add raw data, files from disk, or entire directories
  • Unicode support: Handles international filenames correctly
  • Large file support: Tested with multi-megabyte files
  • Path handling: Supports subdirectory structures within archives
  • Full compatibility: Archives created by Builder can be read by the existing Archive class

Implementation Details

The Rust implementation uses a BuilderOutput enum to handle both file and memory-based outputs:

pub enum BuilderOutput {
  File(File),
  Buffer(Cursor<Vec<u8>>),
}

This allows the same Builder interface to work seamlessly with both output modes while maintaining optimal performance.

Testing

Added comprehensive test coverage with 11 new tests covering:

  • Basic archive creation with various content types
  • File vs memory output modes
  • Large data handling (1MB+ files)
  • Unicode filename support
  • Directory recursion
  • Mixed content scenarios
  • Empty archive handling
  • End-to-end compatibility with Archive reader

All existing tests continue to pass, ensuring backward compatibility.

Documentation

Updated README with complete Builder API documentation and usage examples. TypeScript definitions are automatically generated from the Rust implementation.

Closes #3


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits August 11, 2025 02:43
Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement @napi-rs/tar/issues/3 add tests Implement Builder API for creating tar archives Aug 11, 2025
Copilot AI requested a review from Brooooooklyn August 11, 2025 02:52
@Brooooooklyn
Copy link
Member

@copilot run cargo fmt, and skip three failing tests on wasi:

[fail]: Builder - should append files from disk Error thrown in test
  ✘ [fail]: Builder - should append directories Error thrown in test
  ✘ [fail]: Builder - should handle mixed content types Error thrown in test

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 11, 2025

@copilot run cargo fmt, and skip three failing tests on wasi:

...

Done! Ran cargo fmt to fix formatting and added WASI skips to the three failing Builder tests. All tests now pass. (a5fc706)

@Brooooooklyn Brooooooklyn marked this pull request as ready for review August 11, 2025 03:26
@Brooooooklyn Brooooooklyn merged commit dce5bb0 into main Aug 11, 2025
44 checks passed
@Brooooooklyn Brooooooklyn deleted the copilot/fix-b81ab372-054e-4c14-ba01-63cd89375f81 branch August 11, 2025 03:27
Sphinm pushed a commit to Sphinm/tar-cross-platform that referenced this pull request Aug 31, 2025
* Initial plan

* Initial analysis and plan for Builder API implementation

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>

* Implement Builder API with comprehensive tests

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>

* Clean up test artifacts and update gitignore

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>

* Run cargo fmt and skip three Builder tests on WASI

Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Builder API

2 participants