diff --git a/src/utils/tar-writer.ts b/src/utils/tar-writer.ts index 3b956e4f..4a30d4b0 100644 --- a/src/utils/tar-writer.ts +++ b/src/utils/tar-writer.ts @@ -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') } diff --git a/src/utils/tar.browser.ts b/src/utils/tar.browser.ts index 9234095e..6e9891bc 100644 --- a/src/utils/tar.browser.ts +++ b/src/utils/tar.browser.ts @@ -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 diff --git a/test/integration/bee-class.spec.ts b/test/integration/bee-class.spec.ts index a813a76d..f47d4eee 100644 --- a/test/integration/bee-class.spec.ts +++ b/test/integration/bee-class.spec.ts @@ -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', () => { diff --git a/test/integration/tar.spec.ts b/test/integration/tar.spec.ts new file mode 100644 index 00000000..d4469ea1 --- /dev/null +++ b/test/integration/tar.spec.ts @@ -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 { + 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) + }) +})