Skip to content
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
2 changes: 1 addition & 1 deletion deno/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flatpak-contrib/flatpak-deno-generator",
"version": "1.3.3",
"version": "1.3.4",
"exports": "./src/main.ts",
"license": "MIT"
}
24 changes: 17 additions & 7 deletions deno/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export interface FlatpakData {
/** The type of the source, e.g., "file", "archive". */
type: string;
/** The URL from which to download the source. */
url: string;
url?: string;
/**
* Optional inline contents for the source.
* Used when the source is provided directly rather than downloaded from a URL.
*/
contents?: string;
/** The destination directory within the build environment where the source will be placed. */
dest: string;
/** Optional name for the downloaded file. If not provided, the name is derived from the URL. */
Expand Down Expand Up @@ -159,15 +164,20 @@ export async function npmPkgToFlatpakData(pkg: Pkg): Promise<FlatpakData[]> {
//url: https://registry.npmjs.org/@napi-rs/cli/-/cli-2.18.4.tgz
//npmPkgs;
const metaUrl = `https://registry.npmjs.org/${pkg.module}`;
const metaText = await fetch(metaUrl).then(
(r) => r.text(),
const meta = await fetch(metaUrl).then(
(r) => r.json(),
);
const meta = JSON.parse(metaText);

// "registry.json" file is a stateful file, its always updated so it will never have the same hash for ever
// the workaround is to snapshot it by inlining its content as a string, though we do some optimization here to reduce its size
// by only taking the necessary fields
const metaData = {
type: "file",
url: metaUrl,
sha256: await sha256(metaText),
type: "inline",
contents: JSON.stringify({
name: meta.name,
"dist-tags": {},
versions: { [pkg.version]: meta.versions[pkg.version] },
}),
dest: `deno_dir/npm/registry.npmjs.org/${pkg.module}`,
"dest-filename": "registry.json",
};
Expand Down
12 changes: 7 additions & 5 deletions deno/tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// LICENSE = MIT
// deno-lint-ignore-file require-await
import { assertEquals, assertMatch } from "jsr:@std/[email protected]";
import { assert, assertEquals, assertMatch } from "jsr:@std/[email protected]";
import { jsrPkgToFlatpakData, npmPkgToFlatpakData } from "../src/main.ts";

Deno.test("jsrPkgToFlatpakData returns correct flatpak data", async () => {
Expand Down Expand Up @@ -90,7 +90,7 @@ Deno.test("jsrPkgToFlatpakData returns correct flatpak data", async () => {

Deno.test("npmPkgToFlatpakData returns correct flatpak data", async () => {
// Mock fetch for npm meta
const metaJson = JSON.stringify({
const metaJson = {
versions: {
"2.18.4": {
dist: {
Expand All @@ -99,7 +99,7 @@ Deno.test("npmPkgToFlatpakData returns correct flatpak data", async () => {
},
},
},
});
};

const origFetch = globalThis.fetch;
Object.defineProperty(globalThis, "fetch", {
Expand All @@ -113,7 +113,7 @@ Deno.test("npmPkgToFlatpakData returns correct flatpak data", async () => {
: (input as Request).url;
if (url === "https://registry.npmjs.org/@napi-rs/cli") {
return {
text: async () => metaJson,
json: async () => metaJson,
} as Response;
}
throw new Error("Unexpected fetch url: " + url);
Expand All @@ -139,7 +139,9 @@ Deno.test("npmPkgToFlatpakData returns correct flatpak data", async () => {
assertEquals(data.length, 2);

// registry.json
assertEquals(data[0].url, "https://registry.npmjs.org/@napi-rs/cli");
const registryContents = data.at(0)?.contents;
assert(registryContents !== undefined);
assert("2.18.4" in JSON.parse(registryContents).versions);
assertEquals(data[0]["dest-filename"], "registry.json");

// archive
Expand Down