Skip to content

Commit

Permalink
feat: implement initial packages class
Browse files Browse the repository at this point in the history
  • Loading branch information
tale committed Feb 15, 2022
1 parent 21d3900 commit 5f78b68
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './control'
export * from './packages'
export * from './release'

/**
Expand Down Expand Up @@ -83,6 +84,7 @@ export function parseBoolean(value?: string): boolean | undefined {
/**
* Parse raw file contents of a packages file and retrieve a map of keys and values
*
* @deprecated Use the `Packages` class instead
* @param data Raw string contents from a Packages file
* @returns Map of string keys and values
*/
Expand All @@ -95,6 +97,7 @@ export function parsePackages(data: string) {
/**
* Parse raw file contents of a control file and retrieve a map of keys and values
*
* @deprecated Use the `Control` class instead
* @param data Raw string contents from a control file
* @returns Map of string keys and values
*/
Expand Down
11 changes: 11 additions & 0 deletions src/packages.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { IControl } from './control.d'

interface IPackage extends IControl {
filename: string // Filename
size: number // Size
md5?: string // MD5Sum
sha1?: string // SHA1
sha256?: string // SHA256
sha512?: string // SHA512
descriptionMd5?: string // Description-md5
}
57 changes: 57 additions & 0 deletions src/packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Control } from '.'
import type { IPackage } from './packages.d'

/**
* Class representing a single package in a packages file
*
* For keys that are documented by Debian, we can do some strict type inference.
* See: https://wiki.debian.org/DebianRepository/Format#A.22Packages.22_Indices
*
* To meet the needs of many people, `apt-parser` will handle documented keys both ways.
* It will populate the strictly typed fields and also leave the raw-string value and key.
*/
class Package extends Control implements IPackage {
// Begin Raw Implementation
filename: string
size: number
md5?: string | undefined
sha1?: string | undefined
sha256?: string | undefined
sha512?: string | undefined
descriptionMd5?: string | undefined
// End Raw Implementation

/**
* Create a type-safe Control object and populate its keys
* @param {string} rawData Contents of a control file from a debian binary
*/
constructor(rawData: string) {
super(rawData)

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

this.md5 = this.raw.get('MD5Sum')?.trim()
this.sha1 = this.raw.get('SHA1')?.trim()
this.sha256 = this.raw.get('SHA256')?.trim()
this.sha512 = this.raw.get('SHA512')?.trim()
this.descriptionMd5 = this.raw.get('Description-md5')?.trim()
}
}

/**
* Class representing a packages file
*
* For keys that are documented by Debian, we can do some strict type inference.
* See: https://wiki.debian.org/DebianRepository/Format#A.22Packages.22_Indices
*
* To meet the needs of many people, `apt-parser` will handle documented keys both ways.
* It will populate the strictly typed fields and also leave the raw-string value and key.
*/
export class Packages extends Array<Package> {
constructor(rawData: string) {
const cleanedData = rawData.replaceAll(/\r\n|\r|\n/g, '\n').replaceAll(/\0/g, '').normalize().trim()
const packageChunks = cleanedData.split('\n\n') // We know it will always be \n\n because of our cleanup
super(...packageChunks.map(chunk => new Package(chunk)))
}
}

0 comments on commit 5f78b68

Please sign in to comment.