-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
527 additions
and
284 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
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,59 @@ | ||
// Deno.Kv api is unstable | ||
/// <reference lib="deno.unstable" /> | ||
|
||
import type { | ||
DownloadArtifacts, | ||
InstallArtifacts, | ||
InstallConfigLite, | ||
PortManifestX, | ||
} from "./types.ts"; | ||
|
||
export type InstallRow = { | ||
installId: string; | ||
conf: InstallConfigLite; | ||
manifest: PortManifestX; | ||
installArts?: InstallArtifacts; | ||
downloadArts: DownloadArtifacts; | ||
progress: "downloaded" | "installed"; | ||
}; | ||
|
||
export abstract class InstallsDb { | ||
abstract all(): Promise<InstallRow[]>; | ||
abstract get(id: string): Promise<InstallRow | undefined>; | ||
abstract set(id: string, row: InstallRow): Promise<void>; | ||
abstract delete(id: string): Promise<void>; | ||
abstract [Symbol.dispose](): void; | ||
} | ||
|
||
export async function installsDbKv(path: string): Promise<InstallsDb> { | ||
const kv = await Deno.openKv(path); | ||
return new DenoKvInstallsDb(kv); | ||
} | ||
|
||
class DenoKvInstallsDb extends InstallsDb { | ||
prefix = "installs"; | ||
constructor(public kv: Deno.Kv) { | ||
super(); | ||
} | ||
async all(): Promise<InstallRow[]> { | ||
const iterator = this.kv.list<InstallRow>({ prefix: [this.prefix] }); | ||
return (await Array.fromAsync(iterator)).map((ent) => ent.value); | ||
} | ||
async get(id: string) { | ||
const val = await this.kv.get<InstallRow>([this.prefix, id]); | ||
if (!val.value) return; | ||
return val.value; | ||
} | ||
async set(id: string, row: InstallRow) { | ||
const res = await this.kv.set([this.prefix, id], row); | ||
if (!res.ok) { | ||
throw new Error("Error on to Deno.Kv.set"); | ||
} | ||
} | ||
async delete(id: string) { | ||
await this.kv.delete([this.prefix, id]); | ||
} | ||
[Symbol.dispose](): void { | ||
this.kv.close(); | ||
} | ||
} |
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
Oops, something went wrong.