Skip to content

Commit 1518aca

Browse files
authored
deno: Inline NPM registry metadata (469)
* Inline NPM registry metadata The registry.json file fetched from npmjs.org changes frequently, breaking Flatpak build due to hash mismatches. This change embeds a minimal snapshot of the necessary metadata directly into the Flatpak source entry using `type: "inline"` and `contents`, ensuring consistent builds. Tests are updated accordingly. * Bump version to 1.3.4
1 parent 933d757 commit 1518aca

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

deno/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@flatpak-contrib/flatpak-deno-generator",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"exports": "./src/main.ts",
55
"license": "MIT"
66
}

deno/src/main.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export interface FlatpakData {
2929
/** The type of the source, e.g., "file", "archive". */
3030
type: string;
3131
/** The URL from which to download the source. */
32-
url: string;
32+
url?: string;
33+
/**
34+
* Optional inline contents for the source.
35+
* Used when the source is provided directly rather than downloaded from a URL.
36+
*/
37+
contents?: string;
3338
/** The destination directory within the build environment where the source will be placed. */
3439
dest: string;
3540
/** Optional name for the downloaded file. If not provided, the name is derived from the URL. */
@@ -159,15 +164,20 @@ export async function npmPkgToFlatpakData(pkg: Pkg): Promise<FlatpakData[]> {
159164
//url: https://registry.npmjs.org/@napi-rs/cli/-/cli-2.18.4.tgz
160165
//npmPkgs;
161166
const metaUrl = `https://registry.npmjs.org/${pkg.module}`;
162-
const metaText = await fetch(metaUrl).then(
163-
(r) => r.text(),
167+
const meta = await fetch(metaUrl).then(
168+
(r) => r.json(),
164169
);
165-
const meta = JSON.parse(metaText);
166170

171+
// "registry.json" file is a stateful file, its always updated so it will never have the same hash for ever
172+
// the workaround is to snapshot it by inlining its content as a string, though we do some optimization here to reduce its size
173+
// by only taking the necessary fields
167174
const metaData = {
168-
type: "file",
169-
url: metaUrl,
170-
sha256: await sha256(metaText),
175+
type: "inline",
176+
contents: JSON.stringify({
177+
name: meta.name,
178+
"dist-tags": {},
179+
versions: { [pkg.version]: meta.versions[pkg.version] },
180+
}),
171181
dest: `deno_dir/npm/registry.npmjs.org/${pkg.module}`,
172182
"dest-filename": "registry.json",
173183
};

deno/tests/main.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// LICENSE = MIT
22
// deno-lint-ignore-file require-await
3-
import { assertEquals, assertMatch } from "jsr:@std/[email protected]";
3+
import { assert, assertEquals, assertMatch } from "jsr:@std/[email protected]";
44
import { jsrPkgToFlatpakData, npmPkgToFlatpakData } from "../src/main.ts";
55

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

9191
Deno.test("npmPkgToFlatpakData returns correct flatpak data", async () => {
9292
// Mock fetch for npm meta
93-
const metaJson = JSON.stringify({
93+
const metaJson = {
9494
versions: {
9595
"2.18.4": {
9696
dist: {
@@ -99,7 +99,7 @@ Deno.test("npmPkgToFlatpakData returns correct flatpak data", async () => {
9999
},
100100
},
101101
},
102-
});
102+
};
103103

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

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

145147
// archive

0 commit comments

Comments
 (0)