Skip to content

Commit

Permalink
feat: implement a validation skip feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tale committed Mar 15, 2022
1 parent 285ea0d commit 317f701
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
16 changes: 12 additions & 4 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@ import { MissingRequiredKeyError } from "./error"

export class CaseCopyMap extends Map<string, string> {
set(key: string, value: string): this {
super.set(`case_copying${key.toLowerCase()}`, key)
super.set(`cased_${key.toLowerCase()}`, key)
return super.set(key, value)
}

get(key: string): string | undefined {
if (super.has(key)) return super.get(key)

// Handle case insensitivity
const actualKey = super.get(`case_copying${key.toLowerCase()}`)
const actualKey = super.get(`cased_${key.toLowerCase()}`)
if (actualKey) return super.get(actualKey)
}

public get size() : number {
return super.size / 2
}
}

/**
* Modifiers to the APT constructors
*/
export interface ParserOptions {
/**
* Disables the APT key validations based on the Debian documentation site.
*/
skipValidation?: boolean
}

export class APTBase {
Expand Down Expand Up @@ -45,7 +54,6 @@ export class APTBase {
throw new MissingRequiredKeyError(key)
}
}

}

/**
Expand All @@ -64,7 +72,7 @@ export class APTBase {
entries(): IterableIterator<[string, string]> {
const builder: [string, string][] = []
for (const [key, value] of this.raw.entries()) {
if (!key.startsWith('case_copying')) {
if (!key.startsWith('cased_')) {
builder.push([key, value])
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/control.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseBoolean, parseKV } from '.'
import { APTBase } from './base'
import { APTBase, ParserOptions } from './base'

export type PriorityLevel = 'required' | 'important' | 'standard' | 'optional' | 'extra'
export type PackageType = 'deb' | 'udeb'
Expand Down Expand Up @@ -260,23 +260,24 @@ export class BinaryControl extends APTBase implements IBinaryControl {
/**
* Create a type-safe Control object and populate its keys
* @param {string} rawData Contents of a control file from a debian binary
* @param {ParserOptions} options Optional object for modifying options when constructing
*/
constructor(rawData: string) {
constructor(rawData: string, options?: ParserOptions) {
const map = parseKV(rawData)
super(map, [
super(map, options?.skipValidation ? [] : [
'Package',
'Version',
'Architecture',
'Maintainer',
'Description'
])

this.package = map.get('Package')!.trim()
this.package = map.get('Package')?.trim() ?? ''
this.source = map.get('Source')?.trim()
this.version = map.get('Version')!.trim()
this.version = map.get('Version')?.trim() ?? ''
this.section = map.get('Section')?.trim()
this.priority = map.get('Priority')?.trim() as PriorityLevel
this.architecture = map.get('Architecture')!.trim()
this.architecture = map.get('Architecture')?.trim() ?? ''
this.essential = parseBoolean(map.get('NotAutomatic')?.trim())

this.depends = map.get('Depends')?.trim().split(', ')
Expand All @@ -290,8 +291,8 @@ export class BinaryControl extends APTBase implements IBinaryControl {

const installedSize = parseInt(map.get('Installed-Size')?.trim() ?? '0')
this.installedSize = installedSize !== 0 ? installedSize : undefined
this.maintainer = map.get('Maintainer')!.trim()
this.description = map.get('Description')!
this.maintainer = map.get('Maintainer')?.trim() ?? ''
this.description = map.get('Description') ?? ''
this.homepage = map.get('Homepage')?.trim()
this.builtUsing = map.get('Built-Using')?.trim()
this.packageType = map.get('Package-Type')?.trim() as PackageType
Expand Down
10 changes: 6 additions & 4 deletions src/packages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BinaryControl, IBinaryControl } from '.'
import { ParserOptions } from './base'

export interface IPackage extends IBinaryControl {
/**
Expand Down Expand Up @@ -96,16 +97,17 @@ export class Package extends BinaryControl implements IPackage {
/**
* Create a type-safe Control object and populate its keys
* @param {string} rawData Contents of a control file from a debian binary
* @param {ParserOptions} options Optional object for modifying options when constructing
*/
constructor(rawData: string) {
constructor(rawData: string, options?: ParserOptions) {
super(rawData)
super.required = [
super.required = options?.skipValidation ? [] : [
'Filename',
'Size'
]

this.filename = this.raw.get('Filename')!.trim()
this.size = parseFloat(this.raw.get('Size')!.trim())
this.filename = this.raw.get('Filename')?.trim() ?? ''
this.size = parseFloat(this.raw.get('Size')?.trim() ?? '0')

this.md5 = this.raw.get('MD5sum')?.trim()
this.sha1 = this.raw.get('SHA1')?.trim()
Expand Down
11 changes: 6 additions & 5 deletions src/release.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseKV, parseBoolean } from '.'
import { APTBase } from './base'
import { APTBase, ParserOptions } from './base'

export type ReleaseHash = {
/**
Expand Down Expand Up @@ -257,16 +257,17 @@ export class Release extends APTBase implements IRelease {
/**
* Create a type-safe Release object and populate its keys
* @param {string} rawData Contents of a Release file from an APT repository
* @param {ParserOptions} options Optional object for modifying options when constructing
*/
constructor(rawData: string) {
constructor(rawData: string, options?: ParserOptions) {
const map = parseKV(rawData)

super(map, [
super(map, options?.skipValidation ? [] : [
'Architectures',
'Components'
])

this.architectures = map.get('Architectures')!.trim().split(' ')
this.architectures = map.get('Architectures')?.trim().split(' ') ?? []
this.noSupportForArchitectureAll = parseBoolean(map.get('No-Support-For-Architecture-All')?.trim())

this.description = map.get('Description')?.trim()
Expand All @@ -281,7 +282,7 @@ export class Release extends APTBase implements IRelease {
this.version = map.get('Version')?.trim()
this.date = map.get('Date') ? new Date(map.get('Date')!) : undefined
this.validUntil = map.get('Valid-Until') ? new Date(map.get('Valid-Until')!) : undefined
this.components = map.get('Components')!.trim().split(' ')
this.components = map.get('Components')?.trim().split(' ') ?? []

this.notAutomatic = parseBoolean(map.get('NotAutomatic')?.trim())
this.butAutomaticUpgrades = parseBoolean(map.get('ButAutomaticUpgrades')?.trim())
Expand Down

0 comments on commit 317f701

Please sign in to comment.