Skip to content

Commit

Permalink
Couple more
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 15, 2024
1 parent 57971d6 commit d3b1da6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 56 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default tseslint.config(
'*.mjs',
'example/*',
'src/htscodecs',
'coverage',
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/cramFile/codecs/external.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CramCodec, { Cursor, Cursors } from './_base'
import { CramMalformedError, CramUnimplementedError } from '../../errors'
import { CramUnimplementedError } from '../../errors'
import { CramFileBlock } from '../file'
import CramSlice from '../slice'
import { parseItf8 } from '../util'
Expand Down
3 changes: 0 additions & 3 deletions src/cramFile/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ export default class CramContainer {
const sectionParsers = getSectionParsers(majorVersion)

const block = await this.getFirstBlock()
if (block === undefined) {
return undefined
}
if (block.contentType !== 'COMPRESSION_HEADER') {
throw new CramMalformedError(
`invalid content type ${block.contentType} in compression header block`,
Expand Down
55 changes: 12 additions & 43 deletions src/cramFile/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export default class CramFile {
}
}

// can just stat this object like a filehandle
read(length: number, position: number) {
return this.file.read(length, position)
}
Expand All @@ -128,20 +127,17 @@ export default class CramFile {
}

const firstBlock = await firstContainer.getFirstBlock()
if (firstBlock === undefined) {
return parseHeaderText('')
} else {
const content = firstBlock.content
const dataView = new DataView(content.buffer)
const headerLength = dataView.getInt32(0, true)
const textStart = 4
const decoder = new TextDecoder('utf8')
const text = decoder.decode(
content.subarray(textStart, textStart + headerLength),
)
this.header = text
return parseHeaderText(text)
}

const content = firstBlock.content
const dataView = new DataView(content.buffer)
const headerLength = dataView.getInt32(0, true)
const textStart = 4
const decoder = new TextDecoder('utf8')
const text = decoder.decode(
content.subarray(textStart, textStart + headerLength),
)
this.header = text
return parseHeaderText(text)
}

async getHeaderText() {
Expand All @@ -153,7 +149,6 @@ export default class CramFile {
const { majorVersion } = await this.getDefinition()
const sectionParsers = getSectionParsers(majorVersion)
let position = sectionParsers.cramFileDefinition.maxLength
const { cramContainerHeader1 } = sectionParsers

// skip with a series of reads to the proper container
let currentContainer: CramContainer | undefined
Expand All @@ -174,9 +169,6 @@ export default class CramFile {
position = currentHeader._endPosition
for (let j = 0; j < currentHeader.numBlocks; j++) {
const block = await this.readBlock(position)
if (block === undefined) {
return undefined
}
position = block._endPosition
}
} else {
Expand Down Expand Up @@ -232,9 +224,6 @@ export default class CramFile {
position = currentHeader._endPosition
for (let j = 0; j < currentHeader.numBlocks; j++) {
const block = await this.readBlock(position)
if (block === undefined) {
break
}
position = block._endPosition
}
} else {
Expand All @@ -260,11 +249,6 @@ export default class CramFile {
const { majorVersion } = await this.getDefinition()
const sectionParsers = getSectionParsers(majorVersion)
const { cramBlockHeader } = sectionParsers
const { size: fileSize } = await this.file.stat()

if (position + cramBlockHeader.maxLength >= fileSize) {
return undefined
}

const buffer = await this.file.read(cramBlockHeader.maxLength, position)
return parseItem(buffer, cramBlockHeader.parser, 0, position)
Expand All @@ -282,16 +266,7 @@ export default class CramFile {
size = section.maxLength,
preReadBuffer?: Uint8Array,
) {
let buffer: Uint8Array
if (preReadBuffer) {
buffer = preReadBuffer
} else {
const { size: fileSize } = await this.file.stat()
if (position + size >= fileSize) {
return undefined
}
buffer = await this.file.read(size, position)
}
const buffer = preReadBuffer ?? (await this.file.read(size, position))
const data = parseItem(buffer, section.parser, 0, position)
if (data._size !== size) {
throw new CramMalformedError(
Expand Down Expand Up @@ -351,9 +326,6 @@ export default class CramFile {
const { majorVersion } = await this.getDefinition()
const sectionParsers = getSectionParsers(majorVersion)
const blockHeader = await this.readBlockHeader(position)
if (blockHeader === undefined) {
return undefined
}
const blockContentPosition = blockHeader._endPosition

const d = await this.file.read(
Expand Down Expand Up @@ -381,9 +353,6 @@ export default class CramFile {
sectionParsers.cramBlockCrc32,
blockContentPosition + blockHeader.compressedSize,
)
if (crc === undefined) {
return undefined
}
block.crc32 = crc.crc32

// check the block data crc32
Expand Down
9 changes: 0 additions & 9 deletions src/cramFile/slice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,10 @@ export default class CramSlice {
const { majorVersion } = await this.file.getDefinition()
const sectionParsers = getSectionParsers(majorVersion)
const containerHeader = await this.container.getHeader()
if (!containerHeader) {
throw new Error('no container header detected')
}

const header = await this.file.readBlock(
containerHeader._endPosition + this.containerPosition,
)
if (header === undefined) {
throw new Error('block header undefined')
}
if (header.contentType === 'MAPPED_SLICE_HEADER') {
const content = parseItem(
header.content,
Expand Down Expand Up @@ -239,9 +233,6 @@ export default class CramSlice {
const blocks: CramFileBlock[] = new Array(header.parsedContent.numBlocks)
for (let i = 0; i < blocks.length; i++) {
const block = await this.file.readBlock(blockPosition)
if (block === undefined) {
continue
}
blocks[i] = block
blockPosition = blocks[i]!._endPosition
}
Expand Down

0 comments on commit d3b1da6

Please sign in to comment.