Skip to content
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

fix: set tar header size #972

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/utils/tar-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export async function writeTar(collection: Collection, tarStream: TarStream) {
}
await tarStream.endFile()
stream.close()
} else if (item.file) {
await tarStream.appendFile(new Uint8Array(await item.file.arrayBuffer()))
await tarStream.endFile()
} else {
throw new Error('Invalid collection item')
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tar.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function createHeader(path: string, size: number): Uint8Array {
}

// Initialize header with zeros
const header = new Uint8Array()
const header = new Uint8Array(512)
header.fill(0, 0, 512)

// File name, truncated to 100 characters if necessary
Expand Down
15 changes: 15 additions & 0 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ describe('Bee class', () => {
expect(file.name).toBe(directoryStructure[0].path)
expect(file.data.text()).toBe('hello-CID-world')
})

it('should upload files', async function () {
const files: File[] = [
{
arrayBuffer: async () => new Uint8Array([1, 2, 3]),
name: 'hello.txt',
type: 'text/plain',
lastModified: Date.now(),
size: 3,
},
] as unknown as File[]
const uploadResult = await bee.uploadFiles(getPostageBatch(), files)
const data = await bee.downloadFile(uploadResult.reference, files[0].name)
expect(data.data.hex()).toBe('010203')
})
})

describe('tags', () => {
Expand Down
43 changes: 43 additions & 0 deletions test/integration/tar.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Binary } from 'cafe-utility'
import { PassThrough } from 'stream'
import * as NodeTar from '../../src/utils/tar'
import * as BrowserTar from '../../src/utils/tar.browser'

async function passThroughToUint8Array(passThrough: PassThrough): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
const chunks: Uint8Array[] = []
passThrough.on('data', (chunk: Uint8Array) => {
chunks.push(chunk)
})
passThrough.on('end', () => {
resolve(Binary.concatBytes(...chunks))
})
passThrough.on('error', reject)
})
}

describe('Tar', () => {
it('should write tar', async () => {
const browserStream = new BrowserTar.TarStream()
const nodeStream = new NodeTar.TarStream()
const nodePromise = passThroughToUint8Array(nodeStream.output)
const data = new Uint8Array([1, 2, 3, 4, 5])
browserStream.beginFile('test.txt', data.length)
nodeStream.beginFile('test.txt', data.length)
await browserStream.appendFile(data)
await nodeStream.appendFile(data)
await browserStream.endFile()
await nodeStream.endFile()
await browserStream.end()
await nodeStream.end()
const browserOutput = browserStream.output
expect(browserOutput).toBeInstanceOf(Uint8Array)
expect(browserOutput).toHaveLength(2048)
const nodeOutput = nodeStream.output
expect(nodeOutput).toBeInstanceOf(PassThrough)
const nodeBytes = await nodePromise
expect(nodeBytes).toBeInstanceOf(Uint8Array)
expect(nodeBytes).toHaveLength(2048)
expect(browserOutput).toEqual(nodeBytes)
})
})
Loading