簡體   English   中英

Deno - TypeScript 文件上的編譯錯誤(deno / rust)

[英]Deno - Compile Error on TypeScript file (deno / rust)

我在名為 download.ts 的文件中有下面的 TypeScript。 它使用 deno run --allow-all download.ts 運行,但在 deno compile --allow-all download.ts 上失敗

Deno 中處理導入的方式是否會導致問題? 還是我需要以不同方式編寫的其他代碼結構? 我已經在我的機器上編譯了“hello world”示例。

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);

我在編譯過程中遇到的錯誤是:

平台:windows x86_64 版本:1.25.0 參數:["C:\Users\mjlef\.deno\bin\deno.exe", "compile", "--allow-all", "download.ts"]

線程 'main' 在 '調用Option::unwrap() on a None value'時驚慌失措,C:\Users\runneradmin.cargo\registry\src\github.com-1ecc6299db9ec823\eszip-0.25.0\src\v2.rs :512:60 注意:使用RUST_BACKTRACE=1環境變量運行以顯示回溯

更新:如果我將導入更改為

import { readerFromStreamReader, copy } 
     from "https://deno.land/std@0.149.0/streams/conversion.ts";

有用。 任何最新版本,它都會失敗。 什么是長期解決方案?

您沒有 state 您正在使用哪個版本的 Deno,並且在https://deno.land/std@0.153.0#releases上說:

標准庫目前獨立於 Deno 版本進行標記。 一旦庫穩定,這將改變。

要檢查不同版本的標准庫與 Deno CLI 的兼容性,請參閱此列表

這意味着每個版本的std庫只保證與特定版本的 Deno 兼容,所以如果您沒有使用兼容版本的 Deno 和您正在導入的std庫模塊進行編譯,那么兼容性就是不保證。

也就是說,pipe 對 Deno 中打開文件WritableStreamResponseReadableStream不需要導入:

這是一個示例,您可以復制該示例下載 Windows 的Deno v1.25.0 zip 存檔

./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");

在終端中運行:

% 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM