-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement initial packages class
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |