Skip to content

Commit

Permalink
chore: code fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 26, 2024
1 parent 231646d commit 9f131e5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 45 deletions.
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
17 changes: 8 additions & 9 deletions __tests__/header.spec.ts
Original file line number Diff line number Diff line change
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,15 +134,15 @@ 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>{
const header = <EncodingHeadOptions> {
name: 'nonzzz.tsx',
uid: 0,
gid: 0,
Expand Down
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"
]
}
47 changes: 23 additions & 24 deletions src/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export const Mode = {
} as const

export const F_MODE = Mode.TU_READ | Mode.TU_WRITE | Mode.TG_READ | Mode.TO_READ

export const D_MODE = Mode.TU_READ | Mode.TU_WRITE | Mode.TU_EXEC | Mode.TG_READ | Mode.TG_EXEC | Mode.TO_READ |
Mode.TO_EXEC
Mode.TO_EXEC

export type Mode = typeof Mode[keyof typeof Mode]

export const TypeFlag = {
Expand Down Expand Up @@ -80,17 +80,17 @@ export const Magic = {

export interface EncodingHeadOptions {
name: string
mode: number,
uid: number,
gid: number,
size: number,
mtime: number,
typeflag: TypeFlag,
linkname?: string,
uname?: string,
gname?: string,
devmajor: number,
devminor: number,
mode: number
uid: number
gid: number
size: number
mtime: number
typeflag: TypeFlag
linkname?: string
uname?: string
gname?: string
devmajor: number
devminor: number
}

export interface EncodingHeadPaxOptions {
Expand All @@ -106,9 +106,9 @@ export interface DecodingHeadOptions {
}

export const ERROR_MESSAGES = {
INVALID_ENCODING_NAME: 'Invalid name. Invalid name. Please check \'name\' is a directory type.',
INVALID_ENCODING_NAME_LEN: 'Invalid name. Please check \'name\' length is less than 255 byte.',
INVALID_ENCODING_LINKNAME: 'Invalid linkname. Please check \'linkname\' length is less than 100 byte.',
INVALID_ENCODING_NAME: "Invalid name. Invalid name. Please check 'name' is a directory type.",
INVALID_ENCODING_NAME_LEN: "Invalid name. Please check 'name' length is less than 255 byte.",
INVALID_ENCODING_LINKNAME: "Invalid linkname. Please check 'linkname' length is less than 100 byte.",
INVALID_BASE256: 'Invalid base256 format',
INVALID_OCTAL_FORMAT: 'Invalid octal format',
NOT_INIT: 'Not init',
Expand All @@ -117,7 +117,7 @@ export const ERROR_MESSAGES = {

// For most scens. format ustar is useful, but when we meet the large file, we should fallback to the old gnu format.

const enc =/* @__PURE__ */ new TextEncoder()
const enc = /* @__PURE__ */ new TextEncoder()

const encodeString = enc.encode.bind(enc)

Expand Down Expand Up @@ -200,7 +200,7 @@ function paxTemplate(keyword: string, value: string) {
// | Global Extended Header Data |
// | ustar Header[typeFlag=x] |
// | Extended Header Data |
// | ustar Header[typeFlag=0] |
// | ustar Header[typeFlag=0] |
// | File Data |
// ...
export function encode(options: EncodingHeadOptions) {
Expand All @@ -211,7 +211,7 @@ export function encode(options: EncodingHeadOptions) {
}
let prefix = ''
let invalidate = false
loop:
loop:
while (name.length > 100) {
const spec = name.indexOf('/')
switch (spec) {
Expand Down Expand Up @@ -317,12 +317,12 @@ export function decode(b: Uint8Array, options?: DecodingHeadOptions) {
const gname = decodeString(b, 297, 32)
const devmajor = decodeOctal(b, 329, 8)
const devminor = decodeOctal(b, 337, 8)
const c = chksum(b)
const c = chksum(b)
if (c === 256) throw new Error(ERROR_MESSAGES.NOT_INIT)
if (c !== decodeOctal(b, 148, 8)) {
throw new Error(ERROR_MESSAGES.INVALID_CHKSUM)
}
//
//
if (Magic.T_MAGIC === decodeString(b, 257, 6)) {
if (b[345]) {
name = decodeString(b, 345, 155, filenameEncoding) + '/' + name
Expand All @@ -332,7 +332,7 @@ export function decode(b: Uint8Array, options?: DecodingHeadOptions) {
if (typeflag === TypeFlag.REG_TYPE && name[name.length - 1] === '/') {
typeflag = TypeFlag.DIR_TYPE
}

return {
name,
mode,
Expand All @@ -346,7 +346,6 @@ export function decode(b: Uint8Array, options?: DecodingHeadOptions) {
gname,
devmajor,
devminor

}
}

Expand Down
6 changes: 3 additions & 3 deletions src/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define */
class Elt <T> {
class Elt<T> {
items: Array<T | undefined>
pos: number
mask: number
Expand Down Expand Up @@ -43,7 +43,7 @@ export class List<T> {
length: number
constructor(cap: number = 16) {
this.cap = cap
this.length = 0
this.length = 0
this.head = new Elt(this.cap)
this.tail = this.head
}
Expand All @@ -55,7 +55,7 @@ export class List<T> {
this.head = prev.next
}
this.head.push(elt)
this.length++
this.length++
}

shift() {
Expand Down
8 changes: 4 additions & 4 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function createWriteableStream(options?: WritableOptions) {
}

const PACK_ERROR_MESSAGES = {
HAS_DONE: 'Can\'t add new entry after calling done()'
HAS_DONE: "Can't add new entry after calling done()"
}

// New archives should be created using REGTYPE.
Expand Down Expand Up @@ -79,7 +79,7 @@ export class Pack {
const writer = createWriteableStream({
write: (chunk, _, callback) => {
try {
this.reader.push(encode(resolvedOptions))
this.reader.push(encode(resolvedOptions))
this.reader.push(chunk)
callback()
} catch (error) {
Expand Down Expand Up @@ -116,7 +116,7 @@ class FastBytes {
private queue: List<Uint8Array>
bytesLen: number
insertedBytesLen: number

constructor() {
this.queue = createList<Uint8Array>()
this.bytesLen = 0
Expand All @@ -132,7 +132,7 @@ class FastBytes {
shift(size: number) {
if (size > this.bytesLen) {
throw new Error(FAST_BYTES_ERROR_MESSAGES.EXCEED_BYTES_LEN)
}
}
if (size === 0) {
return new Uint8Array(0)
}
Expand Down

0 comments on commit 9f131e5

Please sign in to comment.