首页 > 解决方案 > 是否可以从 github 版本中导入 deno 中的模块?

问题描述

我想在 deno 中从 github 导入一个模块,该模块仅作为 github 版本提供,而不是存储库中代码的一部分。

我想导入:https://github.com/zingi/random-lon-lat-generator/releases/download/v0.1.0/random_lon_lat_generator.js

我试过了:

import * as wasm from 'https://github.com/zingi/random-lon-lat-generator/releases/download/v0.1.0/random_lon_lat_generator.js'

这给出了这个错误:

Download https://github.com/zingi/random-lon-lat-generator/releases/download/v0.1.0/random_lon_lat_generator.js
Download https://github-releases.githubusercontent.com/352299341/6ca4b280-9638-11eb-9f4a-c7b6b890c5e9?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210405%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210405T161838Z&X-Amz-Expires=300&X-Amz-Signature=82fbc720c3a05232836678385da43cecd2a9d29ca959f736e5e8a47ce62b23bf&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=352299341&response-content-disposition=attachment%3B%20filename%3Drandom_lon_lat_generator.js&response-content-type=application%2Foctet-stream
error: An unsupported media type was attempted to be imported as a module.
  Specifier: https://github-releases.githubusercontent.com/352299341/6ca4b280-9638-11eb-9f4a-c7b6b890c5e9?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210405%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210405T161838Z&X-Amz-Expires=300&X-Amz-Signature=82fbc720c3a05232836678385da43cecd2a9d29ca959f736e5e8a47ce62b23bf&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=352299341&response-content-disposition=attachment%3B%20filename%3Drandom_lon_lat_generator.js&response-content-type=application%2Foctet-stream
  MediaType: Unknown

我知道如果它是 git 中跟踪文件的一部分,我可以轻松导入一个模块,使用raw.githubusercontent.com

但是因为该模块还包含已编译的 WebAssembly,所以我不想用 git 跟踪它。

如果不可能,您对如何使这项工作有任何其他建议吗?


编辑:错误信息似乎与这篇文章中的相同。但是问题来源不同,无法通过那里接受的答案来解决。github 版本中的资产似乎没有永久链接,例如 git 中的跟踪文件与raw.githubusercontent.com. github 发布页面上提供的文件链接似乎转发 ( 302) 到生成的、限时可用的 url,例如:github-releases.githubusercontent.com/.... 因此,如果有任何可能获得指向 github 资产的永久“原始”链接,将会很有趣。

标签: javascriptgithubdeno

解决方案


GitHub 发布下载 URL 实际上重定向到 S3 预签名 URL,无论内容是什么,该URL本身都会发回带有application/octet-streamContent-Type 标头的响应。由于 deno 不知道该内容类型,这就是您看到该错误消息的原因。

raw.githubusercontent.com但是,您可以通过CDN直接从与发布对应的 git 标签导入。例如,您可以使用以下命令从 std 库导入:

import { serve } from "https://raw.githubusercontent.com/denoland/deno_std/0.92.0/http/server.ts";

如果它是无法通过 GitHub 的原始用户内容 CDN 访问的构建工件,您可以推送到 GCS 或 S3 等外部数据存储。它们都可以通过 URL 公开,然后您可以从那里导入。

一些模块注册表还允许您进行构建步骤,例如nest.land提供了该选项。然后,您可以将其挂接到与发布工作流相同的 CI 工作流中。


不过,对于您的具体情况,我可以从 repo 中看到您正在构建一个 wasm 模块;您还可以包含一个 TypeScript 或 JavaScript 文件,作为您的 wasm 模块的入口点。例如,这是在标准库的哈希模块中完成的。


推荐阅读