Skip to content

Commit

Permalink
fix: set tar header size (#972)
Browse files Browse the repository at this point in the history
* fix: set tar header size

* style: add async

* test: add uploadFiles test

* fix: support files with node.js writeTar
  • Loading branch information
Cafe137 authored Nov 6, 2024
1 parent 3d0511c commit dc1ab57
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
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)
})
})

0 comments on commit dc1ab57

Please sign in to comment.