I have the TypeScript below in a file called download.ts. It runs using deno run --allow-all download.ts but fails on deno compile --allow-all download.ts
Is there something about the way imports are handled in Deno that is causing the issue? or some other code structure I need to write differently? I have gotten "hello world" examples to compile on my machine.
import { readerFromStreamReader, copy } from "https://deno.land/std/streams/conversion.ts";
async function download(url: string, path: string) {
const rsp = await fetch(url);
console.log(url);
console.log(path);
const rdr = rsp.body?.getReader();
if (rdr) {
const r = readerFromStreamReader(rdr);
const f = await Deno.open(path, { create: true, write: true });
// copy from reader to file
await copy(r, f);
f.close();
}
}
const url = "https://qwerty224.s3.amazonaws.com/sample.zip";
const path = "C:/temp/sample.zip";
download(url, path);
There error I get during compile is:
Platform: windows x86_64 Version: 1.25.0 Args: ["C:\Users\mjlef\.deno\bin\deno.exe", "compile", "--allow-all", "download.ts"]
thread 'main' panicked at 'called Option::unwrap()
on a None
value', C:\Users\runneradmin.cargo\registry\src\github.com-1ecc6299db9ec823\eszip-0.25.0\src\v2.rs:512:60
note: run with RUST_BACKTRACE=1
environment variable to display a backtrace
UPDATE: If I change the import to
import { readerFromStreamReader, copy }
from "https://deno.land/[email protected]/streams/conversion.ts";
It works. Any more current version and it fails. What is the long term solution?
CodePudding user response:
You didn't state which version of Deno you're using, and at https://deno.land/[email protected]#releases it says this:
Standard library is currently tagged independently of Deno version. This will change once the library is stabilized.
To check compatibility of different version of standard library with Deno CLI see this list.
What this means is that each version of the std
library is only guaranteed to be compatible with a specific version of Deno, so if you're not compiling with the compatible versions of Deno and the std
library modules you're importing, then compatibility is not guaranteed.
That said, no imports are necessary to pipe the ReadableStream
of a Response
to the WritableStream
of an opened file in Deno:
Here's an example which you can reproduce that downloads the Deno v1.25.0 zip archive for Windows.
./example.ts
:
// I create and use a temporary file path here,
// but you can specify your own path here instead:
const tmpFilePath = await Deno.makeTempFile();
console.log({ tmpFilePath });
const denoDownloadUrl =
"https://github.com/denoland/deno/releases/download/v1.25.0/deno-x86_64-pc-windows-msvc.zip";
const response = await fetch(denoDownloadUrl);
if (!response.body) {
// Handle no response body
throw new Error("Response has no body");
}
const file = await Deno.open(tmpFilePath, { write: true });
await response.body.pipeTo(file.writable);
// The file will be automatically closed when the stream closes
console.log("File written");
Running in the terminal:
% deno run --allow-write=$TMPDIR --allow-net=github.com,objects.githubusercontent.com example.ts
{ tmpFilePath: "/var/folders/hx/8E56Lz5x1_lswn_yfyt1tsg0200eg/T/5851c62f" }
File written
% file /var/folders/hx/8E56Lz5x1_lswn_yfyt1tsg0200eg/T/5851c62f
/var/folders/hx/8E56Lz5x1_lswn_yfyt1tsg0200eg/T/5851c62f: Zip archive data, at least v2.0 to extract