0

As my title suggests, I would like to download an asset from the latest release in a private github repo. I'm using Octokit/core to get the latest release assets, but I have some issues getting the file to download.

Using tauri-plugin-upload it works fine with public repos, but fails to download from a private repo.

I've replaced repo owner and name for privacy. These values are actually in my final code.

My tauri config:

{
  "build": {
    "beforeDevCommand": "npm run dev",
    "beforeBuildCommand": "npm run build",
    "devPath": "http://localhost:1420",
    "distDir": "../dist",
    "withGlobalTauri": false
  },
  "package": {
    "productName": "My App",
    "version": ".0.0"
  },
  "tauri": {
    "allowlist": {
      "all": false,
      "shell": {
        "all": false,
        "open": true
      },
      "fs": {
        "all": true,
        "readFile": true,
        "writeFile": true,
        "readDir": true,
        "copyFile": true,
        "createDir": true,
        "removeDir": true,
        "removeFile": true,
        "renameFile": true,
        "exists": true
      },
      "http": {
        "all": true,
        "request": true
      }
    },
    "bundle": {
      "active": true,
      "targets": "all",
      "identifier": "com.my-app.app",
      "icon": [
        "icons/icon.ico"
      ]
    },
    "security": {
      "csp": null
    },
    "windows": [
      {
        "fullscreen": false,
        "resizable": true,
        "title": "My App",
        "width": 450,
        "height": 140
      }
    ]
  }
}

main.rs

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
    tauri::Builder::default()
    .plugin(tauri_plugin_upload::init())
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

App.svelte

import { download } from "tauri-plugin-upload-api";
import { onMount } from "svelte";
import { Octokit } from "@octokit/core";

const octokit = new Octokit(
    {
        auth: "<TOKEN_HERE>",
        userAgent: "my-app/v1.0"
    }
);

let filename, fileSize, dUrl

onMount(async () => {
    const {data: { assets }} = await octokit.request("GET /repos/{owner}/{repo}/releases/latest");
    dUrl = assets[0].browser_download_url;
    filename = assets[0].name;
    fileSize = assets[0].size;

    download(
        dUrl,
        filename,
        (progress, total) => console.log(`Uploaded ${progress} of ${total} bytes`),
        { "User-Agent": "my-app/v1.0", "Authorization": "Bearer <TOKEN_HERE>" }
    )
})

Another issue I have is with tauri build (npm run tauri build) when using Octokit. I have followed this answer and I got my app to run in dev, but build fails

My vite config

import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  plugins: [svelte()],
  resolve: {
    alias: {
      'node-fetch': 'isomorphic-fetch',
    },
  },
  define: {
    "global": {},
  },

  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  // prevent vite from obscuring rust errors
  clearScreen: false,
  // tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
  },
  // to make use of `TAURI_DEBUG` and other env variables
  // https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
  envPrefix: ["VITE_", "TAURI_"],
  build: {
    // Tauri supports es2021
    target: process.env.TAURI_PLATFORM == "windows" ? "chrome105" : "safari13",
    // don't minify for debug builds
    minify: !process.env.TAURI_DEBUG ? "esbuild" : false,
    // produce sourcemaps for debug builds
    sourcemap: !!process.env.TAURI_DEBUG,
  },
}));

Build fail error message

file: C:/Projects/my-app/node_modules/isomorphic-fetch/fetch-npm-browserify.js:601:4
599: 
600: if (!global.fetch) {
601:   global.fetch = fetch
         ^
602:   global.Headers = Headers
603:   global.Request = Request
error during build:
SyntaxError: Unexpected token (601:4) in C:/Projects/my-app/node_modules/whatwg-fetch/fetch.js
    at pp$4.raise (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:21186:13)
    at pp$9.unexpected (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:18487:8)
    at pp$5.parseExprAtom (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20570:10)
    at pp$5.parseExprSubscripts (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20362:19)
    at pp$5.parseMaybeUnary (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20328:17)
    at pp$5.parseExprOps (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20255:19)
    at pp$5.parseMaybeConditional (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20238:19)
    at pp$5.parseMaybeAssign (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20205:19)
    at pp$5.parseExpression (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:20168:19)
    at pp$8.parseStatement (file:///C:/Projects/my-app/node_modules/rollup/dist/es/shared/node-entry.js:18677:45)
       Error beforeBuildCommand `npm run build` failed with exit code 1

I've also tried js-file-downloader.

App.svelte

import JsFileDownloader from 'js-file-downloader';
import { onMount } from "svelte";
import { Octokit } from "@octokit/core";

const octokit = new Octokit(
    {
        auth: "<TOKEN_HERE>",
        userAgent: "my-app/v1.0"
    }
);

let filename, dUrl;

onMount(async () => {
    const {data: { assets, tag_name }} = await octokit.request("GET /repos/{owner}/{repo}/releases/latest");
    dUrl = assets[0].browser_download_url;
    filename = assets[0].name;

    new JsFileDownloader(
        { 
            url: dUrl,
            filename: filename,
            headers: [
                { name: "Authorization", value: "Bearer <TOKEN_HERE>" },
                { name: "Access-Control-Allow-Origin", value: "*"},
                { name: "Accept", value: "application/octet-stream" }
            ]
        }
    );
})

But with js-file-downloader I get an error in console that

http://localhost:1420 has been blocked by CORS Policy. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Even though I give it the Access-Control-Allow-Origin header.

jthulhu
  • 7,223
  • 2
  • 16
  • 33
DogFoxX
  • 111
  • 10
  • `Access-Control-Allow-Origin` is a _response_ header, not a request header. You need to have it set on the server in responses. – cdhowie May 27 '23 at 08:32
  • So where would I have to put this header? From what I can see js-file-downloader has no options for response headers. And I still have the issue with tauri-plugin-upload not downloading the file from private repo, and the app won't build. – DogFoxX May 27 '23 at 08:46
  • The header would need to be set on GitHub's end. – cdhowie May 27 '23 at 08:49

0 Answers0