Skip to content

Commit

Permalink
Add implementation for Brioche.download() global function
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Jul 19, 2024
1 parent f5578a7 commit c02603f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/std/core/global.bri
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ declare global {
* ```
*/
function glob(...patterns: string[]): Recipe<Directory>;

/**
* Download a file from a URL. Unlike `std.download`, this function does
* not take a hash, and it **must** be called with a constant string URL.
* The hash of the downloaded file will be saved in the lockfile.
*
* ## Example
*
* ```typescript
* const file = Brioche.download("http://example.com/");
* ```
*/
function download(url: string): Recipe<File>;
}
}

Expand Down Expand Up @@ -155,3 +168,24 @@ declare global {
},
});
};
(globalThis as any).Brioche.download ??= (url: string): Recipe<File> => {
const sourceFrame = source({ depth: 1 }).at(0);
if (sourceFrame === undefined) {
throw new Error(`Could not find source file to download ${url}`);
}

const sourceFile = sourceFrame.fileName;

return createRecipe(["file"], {
sourceDepth: 1,
briocheSerialize: async () => {
return await (globalThis as any).Deno.core.ops.op_brioche_get_static(
sourceFile,
{
type: "download",
url,
},
);
},
});
};

0 comments on commit c02603f

Please sign in to comment.