Skip to content

Commit

Permalink
Add implementation for Brioche.download() global function (#75)
Browse files Browse the repository at this point in the history
* Add implementation for `Brioche.download()` global function

* Fix typo in docs for `std.download`

* Update docs on `std.download` and `Brioche.download`

* Update `Brioche.download(...)` to validate Brioche version
  • Loading branch information
kylewlacy authored Sep 26, 2024
1 parent 77e0da6 commit 97ba592
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
44 changes: 44 additions & 0 deletions packages/std/core/global.bri
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
createRecipe,
} from "./recipes";
import { source } from "./source.bri";
import { BRIOCHE_VERSION } from "./runtime.bri";
import { semverMatches } from "./semver.bri";
import { assert } from "./utils.bri";

declare global {
// eslint-disable-next-line
Expand Down Expand Up @@ -82,6 +85,21 @@ 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.
*
* See also `std.download`, which can be used even if URL is not constant.
*
* ## Example
*
* ```typescript
* const file = Brioche.download("http://example.com/");
* ```
*/
function download(url: string): Recipe<File>;
}
}

Expand Down Expand Up @@ -155,3 +173,29 @@ declare global {
},
});
};
(globalThis as any).Brioche.download ??= (url: string): Recipe<File> => {
assert(
semverMatches(BRIOCHE_VERSION, ">=0.1.2"),
"Brioche.download(...) requires Brioche v0.1.2 or greater",
);

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,
},
);
},
});
};
5 changes: 4 additions & 1 deletion packages/std/core/recipes/download.bri
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export interface DownloadOptions {

/**
* Returns a recipe that will download a URL, and return
* a file of its results. A has must be provided, and will
* a file of its results. A hash must be provided, and will
* be used to verify the downloaded contents.
*
* See also `Brioche.download`, which does not require specifying
* a hash, but only works with a constant URL.
*
* ## Example
*
* std.download({
Expand Down

0 comments on commit 97ba592

Please sign in to comment.