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

feat: pax #3

Merged
merged 5 commits into from
Aug 26, 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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

It's an implementation based on the `ustar` format. This package only provides low-level API's.

### Usage

```ts
// packing
import { createPack, createExtract } from 'tar-mini'

const pack = createPack()

pack.add(new Uint8Array(512), {
// options
})

pack.done()

// extracting

const extract = createExtract()

extract.on('entry', (head, file) => {
// todo
})

pack.receiver.pipe(extract.receiver)
```

### Sponsors

<p align="center">
Expand Down
44 changes: 35 additions & 9 deletions __tests__/header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { ERROR_MESSAGES, F_MODE, TypeFlag, encode } from '../src'
import { ERROR_MESSAGES, F_MODE, TypeFlag, decodePax, decode as decodeTar, encode, encodePax } from '../src'
import type { EncodingHeadOptions } from '../src'

function randomDir(len: number) {
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Headers', () => {
const decode = decoder.decode.bind(decoder)
const mtime = Math.floor(Date.now() / 1000)
it('Normal', () => {
const header = <EncodingHeadOptions>{
const header = <EncodingHeadOptions> {
name: 'foo.tsx',
uid: 0,
gid: 0,
Expand All @@ -70,17 +70,16 @@ describe('Headers', () => {
mode: F_MODE,
uname: 'nonzzz',
gname: 'admin'

}

}

const block = encode(header)
expect(block.length).toBe(512)
expect(decode(block.subarray(0, 100)).replace(/\0+$/, '')).toBe('foo.tsx')
expect(decode(block.subarray(265, 265 + 32)).replace(/\0+$/, '')).toBe('nonzzz')
expect(decode(block.subarray(297, 297 + 32)).replace(/\0+$/, '')).toBe('admin')
})
it('Directory', () => {
const header = <EncodingHeadOptions>{
const header = <EncodingHeadOptions> {
name: 'nao',
uid: 0,
gid: 0,
Expand All @@ -101,7 +100,7 @@ describe('Headers', () => {
})
it('Long Name File But Not Direcotry', () => {
const filename = 'a'.repeat(98) + '.tsx'
const header = <EncodingHeadOptions>{
const header = <EncodingHeadOptions> {
name: filename,
uid: 0,
gid: 0,
Expand All @@ -121,7 +120,7 @@ describe('Headers', () => {
const dir = randomDir(100)
const filename = 'nonzzz.tsx'
const { prefix, name } = getPrefixAndName(dir + filename)
const header = <EncodingHeadOptions>{
const header = <EncodingHeadOptions> {
name: dir + filename,
uid: 0,
gid: 0,
Expand All @@ -135,12 +134,39 @@ describe('Headers', () => {
uname: 'nonzzz',
gname: 'admin'
}

const block = encode(header)
expect(block.length).toBe(512)
expect(decode(block.subarray(0, 100)).replace(/\0+$/, '')).toBe(name)
expect(decode(block.subarray(345, 345 + 155)).replace(/\0+$/, '')).toBe(prefix)
})
it('Large File', () => {
const size = Math.pow(2, 33)
const header = <EncodingHeadOptions> {
name: 'nonzzz.tsx',
uid: 0,
gid: 0,
size,
mtime,
typeflag: TypeFlag.AREG_TYPE,
linkname: '',
devmajor: 0,
devminor: 0,
mode: F_MODE,
uname: 'nonzzz',
gname: 'admin'
}
const block = encode(header)
const { size: decodeSize } = decodeTar(block)
expect(decodeSize).toBe(size)
})
it('Pax Header', () => {
const binary = encodePax({ name: 'nonzzz.tsx', linkname: '1', pax: { kanno: 'hello world' } })
const pax = decodePax(binary)
expect(pax.path).toBe('nonzzz.tsx')
expect(pax.kanno).toBe('hello world')
expect(pax.linkpath).toBe('1')
})
})
})
})
2 changes: 2 additions & 0 deletions __tests__/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ describe('Stream', () => {
})
}

pack.done()

extract.on('entry', (head, file) => {
const content = assets[head.name]
expect(content).toBe(textDecode.decode(file))
Expand Down
23 changes: 18 additions & 5 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
{
"json": {
},
"lineWidth": 140,
"typescript": {
"semiColons": "asi",
"indentWidth": 2,
"quoteStyle": "preferSingle",
"useTabs": false,
"trailingCommas": "never",
"module.sortImportDeclarations": "maintain",
"importDeclaration.sortNamedImports": "maintain",
"operatorPosition": "maintain",
"jsx.quoteStyle": "preferDouble",
"jsx.bracketPosition": "maintain",
"functionDeclaration.spaceBeforeParentheses": false
},
"markdown": {
},
"toml": {
},
"excludes": [
"**/node_modules",
"**/*-lock.json",
".yarn/*"
"**/*-lock.json"
],
"plugins": [
"https://plugins.dprint.dev/json-0.19.3.wasm",
"https://plugins.dprint.dev/markdown-0.17.1.wasm",
"https://plugins.dprint.dev/toml-0.6.2.wasm"
"https://plugins.dprint.dev/typescript-0.90.5.wasm",
"https://plugins.dprint.dev/json-0.19.2.wasm",
"https://plugins.dprint.dev/markdown-0.17.0.wasm"
]
}
Loading
Loading