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

Remove dependency on long package #99

Merged
merged 2 commits into from
Aug 30, 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
60 changes: 26 additions & 34 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
import prettier from 'eslint-plugin-prettier'
import typescriptEslint from '@typescript-eslint/eslint-plugin'
import tsParser from '@typescript-eslint/parser'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import js from '@eslint/js'
import { FlatCompat } from '@eslint/eslintrc'
import eslint from '@eslint/js'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'
import tseslint from 'typescript-eslint'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
})

export default [
...compat.extends(
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
'plugin:prettier/recommended',
'plugin:unicorn/recommended',
),
export default tseslint.config(
{
ignores: [
'webpack.config.js',
'dist/*',
'esm/*',
'example/*',
'eslint.config.mjs',
],
},
{
plugins: {
prettier,
'@typescript-eslint': typescriptEslint,
},

languageOptions: {
parser: tsParser,
ecmaVersion: 5,
sourceType: 'script',

parserOptions: {
project: './tsconfig.lint.json',
project: ['./tsconfig.lint.json'],
tsconfigRootDir: import.meta.dirname,
},
},
},
eslint.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.stylisticTypeChecked,
...tseslint.configs.strictTypeChecked,

eslintPluginUnicorn.configs['flat/recommended'],
{
rules: {
'@typescript-eslint/no-unused-vars': [
'warn',
Expand Down Expand Up @@ -101,6 +90,9 @@ export default [
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/restrict-plus-operands': 'off',
'no-empty': 'off',
},
},
]
)
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
"src"
],
"scripts": {
"test": "vitest run",
"coverage": "npm test -- --coverage",
"lint": "eslint --report-unused-disable-directives --max-warnings 0 src test",
"test": "vitest",
"lint": "eslint --report-unused-disable-directives --max-warnings 0",
"clean": "rimraf dist esm",
"build:esm": "tsc --target es2018 --outDir esm",
"build:es5": "tsc --target es2015 --module commonjs --outDir dist",
"build": "npm run build:esm && npm run build:es5",
"prebuild": "npm run clean && npm run lint",
"preversion": "npm run lint && npm test && npm run build",
"preversion": "npm run lint && npm test run && npm run build",
"version": "standard-changelog && git add CHANGELOG.md",
"postversion": "git push --follow-tags"
},
Expand All @@ -43,7 +42,6 @@
"@gmod/bgzf-filehandle": "^1.4.4",
"crc": "^4.3.2",
"generic-filehandle": "^3.0.0",
"long": "^4.0.0",
"quick-lru": "^4.0.0"
},
"devDependencies": {
Expand Down
9 changes: 7 additions & 2 deletions src/bai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function reg2bins(beg: number, end: number) {
[73 + (beg >> 20), 73 + (end >> 20)],
[585 + (beg >> 17), 585 + (end >> 17)],
[4681 + (beg >> 14), 4681 + (end >> 14)],
]
] as const
}

export default class BAI extends IndexFile {
Expand Down Expand Up @@ -125,6 +125,7 @@ export default class BAI extends IndexFile {
const range = start !== undefined
const indexData = await this.parse(opts)
const seqIdx = indexData.indices[seqId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!seqIdx) {
return []
}
Expand Down Expand Up @@ -167,10 +168,12 @@ export default class BAI extends IndexFile {
}

const indexData = await this.parse(opts)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!indexData) {
return []
}
const ba = indexData.indices[refId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!ba) {
return []
}
Expand All @@ -182,6 +185,7 @@ export default class BAI extends IndexFile {
// Find chunks in overlapping bins. Leaf bins (< 4681) are not pruned
for (const [start, end] of overlappingBins) {
for (let bin = start; bin <= end; bin++) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (ba.binIndex[bin]) {
const binChunks = ba.binIndex[bin]
for (const binChunk of binChunks) {
Expand All @@ -199,6 +203,7 @@ export default class BAI extends IndexFile {
const maxLin = Math.min(max >> 14, nintv - 1)
for (let i = minLin; i <= maxLin; ++i) {
const vp = ba.linearIndex[i]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (vp && (!lowest || vp.compareTo(lowest) < 0)) {
lowest = vp
}
Expand All @@ -209,7 +214,7 @@ export default class BAI extends IndexFile {

async parse(opts: BaseOpts = {}) {
if (!this.setupP) {
this.setupP = this._parse(opts).catch(e => {
this.setupP = this._parse(opts).catch((e: unknown) => {
this.setupP = undefined
throw e
})
Expand Down
4 changes: 3 additions & 1 deletion src/bamFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default class BamFile {

getHeader(opts?: BaseOpts) {
if (!this.headerP) {
this.headerP = this.getHeaderPre(opts).catch(e => {
this.headerP = this.getHeaderPre(opts).catch((e: unknown) => {
this.headerP = undefined
throw e
})
Expand Down Expand Up @@ -430,6 +430,7 @@ export default class BamFile {
const blockEnd = blockStart + 4 + blockSize - 1

// increment position to the current decompressed status
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (dpositions) {
while (blockStart + chunk.minv.dataPosition >= dpositions[pos++]) {}
pos--
Expand Down Expand Up @@ -470,6 +471,7 @@ export default class BamFile {
chunk.minv.dataPosition +
1
: // must be slice, not subarray for buffer polyfill on web
// eslint-disable-next-line @typescript-eslint/no-deprecated
crc32.signed(ba.slice(blockStart, blockEnd)),
})

Expand Down
8 changes: 5 additions & 3 deletions src/csi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ export default class CSI extends IndexFile {
}

const indexData = await this.parse(opts)
const ba = indexData?.indices[refId]
const ba = indexData.indices[refId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!ba) {
return []
}
Expand All @@ -173,6 +174,7 @@ export default class CSI extends IndexFile {
// Find chunks in overlapping bins. Leaf bins (< 4681) are not pruned
for (const [start, end] of overlappingBins) {
for (let bin = start; bin <= end; bin++) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (ba.binIndex[bin]) {
const binChunks = ba.binIndex[bin]
for (const c of binChunks) {
Expand Down Expand Up @@ -210,14 +212,14 @@ export default class CSI extends IndexFile {
`query ${beg}-${end} is too large for current binning scheme (shift ${this.minShift}, depth ${this.depth}), try a smaller query or a coarser index binning scheme`,
)
}
bins.push([b, e])
bins.push([b, e] as const)
}
return bins
}

async parse(opts: BaseOpts = {}) {
if (!this.setupP) {
this.setupP = this._parse(opts).catch(e => {
this.setupP = this._parse(opts).catch((e: unknown) => {
this.setupP = undefined
throw e
})
Expand Down
2 changes: 1 addition & 1 deletion src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class BamRecord {
if (this.isPaired()) {
tags.push('next_segment_position', 'pair_orientation')
}
tags = tags.concat(this._tagList || [])
tags = tags.concat(this._tagList)

for (const k of Object.keys(this.data)) {
if (!k.startsWith('_') && k !== 'next_seq_id') {
Expand Down
21 changes: 3 additions & 18 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import Long from 'long'
import Chunk from './chunk'
import VirtualOffset from './virtualOffset'

export function timeout(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}

export function longToNumber(long: Long) {
if (
long.greaterThan(Number.MAX_SAFE_INTEGER) ||
long.lessThan(Number.MIN_SAFE_INTEGER)
) {
throw new Error('integer overflow')
}
return long.toNumber()
}

/**
* Properly check if the given AbortSignal is aborted.
* Per the standard, if the signal reads as aborted,
Expand Down Expand Up @@ -113,13 +102,9 @@ export function optimizeChunks(chunks: Chunk[], lowest?: VirtualOffset) {
}

export function parsePseudoBin(bytes: Buffer, offset: number) {
const lineCount = longToNumber(
Long.fromBytesLE(
Array.prototype.slice.call(bytes, offset, offset + 8),
true,
),
)
return { lineCount }
return {
lineCount: Number(bytes.readBigInt64LE(offset)),
}
}

export function findFirstData(
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"include": ["src"],
"compilerOptions": {
"outDir": "dist",
"target":"es2018",
"target": "es2018",
"types": ["node"],
"lib": ["dom", "esnext"],
"declaration": true,
Expand Down
Loading
Loading