Issue with TypeScript file compilation in Deno (Deno / rust)

Having a TypeScript file named download.ts, I successfully run it using deno run --allow-all download.ts but encounter failure with deno compile --allow-all download.ts

Could there be an issue related to how Deno handles imports that is causing this error? Is there a specific code structure adjustment I need to make? I have managed to compile "hello world" examples on my system.

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

The error message I receive during compilation reads:

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: Switching the import statement to

import { readerFromStreamReader, copy } 
     from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0939484a0d0ced1d4d9ced0">[email protected]</a>/streams/conversion.ts";

resolves the issue. However, any newer version causes a failure. What is the permanent solution?

Answer №1

Without specifying the version of Deno being used, it's important to note that according to https://deno.land/[email protected]#releases, the standard library is tagged independently of the Deno version, with plans for change once the library stabilizes.

The compatibility between different versions of the standard library and Deno CLI can be confirmed by referring to this list.

To ensure compatibility, each version of the `std` library is guaranteed to work with a specific version of Deno. If the compatible versions are not aligned when compiling or importing `std` library modules, compatibility may be compromised.

In practice, no imports are required to transmit the ReadableStream from a Response to the WritableStream of an opened file in Deno:

Below is a specific example that demonstrates downloading the Deno v1.25.0 zip archive for Windows.

./example.ts:

// Temporary file path creation and usage demonstrated here, 
// alternative paths can also be specified as needed:
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) {
  // Handling when there is no body in the response
  throw new Error("Response has no body");
}

const file = await Deno.open(tmpFilePath, { write: true });
await response.body.pipeTo(file.writable);
// File closure will occur automatically once the stream ends

console.log("File written");

Execution 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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to interact with and modify the relationships of "many-to-many" fields in my database table?

As someone who is new to nestjs, I am working with two entities: @Entity({ name: "Books" }) @ObjectType() export class Book { @PrimaryGeneratedColumn() @Field() id: number; @Column() @Field() title: string; @ManyToMany(() => Auth ...

TypeScript Definitions for Material-UI version 15

Is there a Material-UI v15 TypeScript definition file in the works? I need it for a project I'm working on and as a TypeScript newbie, I want to make sure the custom file I've begun is accurate. ...

Leveraging non-ionic-native Cordova plugin

I'm currently working on integrating the [This]https://github.com/louisbl/cordova-plugin-locationservices plugin into my Ionic 4 app. Since it's a non-Ionic-native plugin, what is the proper way to call its functions within my TypeScript code? ...

Understanding the significance of emitDecoratorMetadata in transpiled code

I have a question regarding the significance of the emitDecoratorMetadata option when transpiling TypeScript to JavaScript in an Angular 2 environment. If this option is set to false, and metadata is not included in the final code, what impact will it ha ...

What is the method to retrieve the value of a textbox located inside a div with TypeScript?

Here is my HTML structure: <div id="series1"> <div class="col-md-6"> <label for="Mark">Mark</label> <input type="text" id="mark" class="shortTextbox" (blur)="signalSelected('mark')" /> </d ...

Having trouble with the Angular Language Service extension in VS Code for Angular-16?

Upon transitioning to Angular 16, I encountered errors while attempting to edit the components HTML due to the malfunctioning of the Angular Language Service extension. [Info - 09:41:11] Angular language server process ID: 18032 [Info - 09:41:11] Using t ...

What is the process for adding connected entities in MikroORM?

I am encountering difficulties in inserting related elements into each other. I believe I may be approaching it incorrectly. Here is an example of how I am attempting to do so. Mikro does not appear to set the foreign key in the dec_declinaison table. /* ...

typescript import module from tsd

Generated by swagger-codegen, the file index.ts contains: export * from './api/api'; export * from './model/models'; The file tsd.d.ts includes: ... /// <reference path="path/to/index.ts" /> TypeScript version 2.2.1. Why do I ...

Ways to verify if the current date exists within a TypeScript date array

I am trying to find a way in typescript to check if the current date is included in a given array of dates. However, even after using the code below, it still returns false even when the current date should be present within the array. Can anyone please pr ...

What is the most appropriate form to use, and what factors should be considered in determining

Incorporating generics in typescript allows me to create a generic function in the following manner: Choice 1 declare function foo1<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } Alternatively, I have the option to omit the seco ...

How to send multiple queries in one request with graphql-request while using getStaticProps?

I am currently utilizing graphCMS in combination with NextJS and have successfully implemented fetching data. However, I am facing an issue where I need to execute 2 queries on the homepage of my website - one for all posts and another for recent posts. q ...

Steps for setting up a Node.js Express application that serves a Vue.js single page application

Currently, I am in the process of setting up a Node.js project that incorporates Express to create backend APIs and deliver a Single Page Application (SPA) designed with Vue.js. Upon initializing a project using the Vue cli, components such as the main fi ...

Typescript: defining index signatures with numerical types in the range of 1 to 3

type X = {[K in '1' | '2']: string} // valid type Y = {[K in 1 | 2]: string} // invalid https://i.sstatic.net/8iBoK.png Could there be a legitimate explanation for this inconsistency? I couldn't find any related problem on github ...

Is there a way to prevent the user from proceeding to the next step if they haven't finished the initial one?

After successfully creating a multi-step form using shadcn, react-hook form, and zod, I encountered an issue. Even if I haven't completed the input fields, I can still proceed to the next step by clicking the next button. Appointment.ts export const ...

Application fails to launch after disabling unsafe-eval in the restricted Content Security Policy settings

Description My application is facing issues due to having a restricted CSP policy that does not allow unsafe-eval for scripts. When I add a Content-Security-Policy header without unsafe-eval, my application fails to load. Minimal Reproduction The restric ...

Issues with loading NextJS/Ant-design styles and JS bundles are causing delays in the staging environment

Hey there lovely folks, I'm in need of assistance with my NextJS and Ant-design application. The current issue is only occurring on the stagging & production environment, I am unable to replicate it locally, even by running: npm run dev or npm r ...

"Silently update the value of an Rxjs Observable without triggering notifications to subscribers

I'm currently working on updating an observable without alerting the subscribers to the next value change. In my project, I am utilizing Angular Reactive Forms and subscribing to the form control's value changes Observable in the following manner ...

Transmitting Events to JavaScript from Your Custom Module in React Native?

I've been attempting to transfer an event from my module to React Native. I followed the steps outlined here, but it didn't behave as anticipated. I'm not getting any errors, but it simply isn't functioning. Below is my code in Android ...

Angular6 implementation of a 3D card carousel

Looking for a library to create a 3D Card Slider similar to the image below using Angular6? Check it out here ...

What is the best way to ensure that GCM push notifications are still received even when the app is closed or the

Currently, I'm in the process of developing an application using Ionic 2 that requires push notifications to be received. In certain scenarios, such as when the app is force-stopped on Android or when the device is turned off, push notifications are ...