【问题标题】:Is it possible to import a module in deno from a github release?是否可以从 github 版本中导入 deno 中的模块?
【发布时间】:2021-05-05 06:15:16
【问题描述】:

我想在 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 跟踪它。

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


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

【问题讨论】:

  • 它是因为,它的 application/xml 不是 JavaScript ,还要注意 MediaType: Unknown 时,我请求它给了我 http 403 错误。
  • server: AmazonS3请求,错误:<Error> <Code>AccessDenied</Code> <Message>Request has expired</Message> <X-Amz-Expires>300</X-Amz-Expires> <Expires>2021-04-05T16:23:38Z</Expires> <ServerTime>2021-04-05T21:03:05Z</ServerTime> <RequestId>CJBY8NSXG35XJHJD</RequestId> <HostId>XT38SvXhRD6ZKG7Vx0tt1GikPA6AyVaqeCQFS4aUpqYZbIEf0k3ZbmO0wzh2Q4RB5E9Gemwpy+g=</HostId> </Error>
  • @Nur 您可能试图从重定向的 url 下载,这似乎是一个自动生成的 url,仅在有限的时间内可用。如果您使用https://github.com/zingi/random-lon-lat-generator/releases/download/v0.1.0/random_lon_lat_generator.js,您应该不会收到 403 错误。
  • @zingi 似乎没有其他方法可以从 GitHub 获取内容类型为 text/plainapplication/javascript 以获取发布资产。但是有一个解决方法,您可以在 Glitch 上创建一个小型服务器,它会下载资产并使用正确/所需的内容类型返回响应。

标签: javascript github deno


【解决方案1】:

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

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

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 模块的入口点。例如,在标准库的hash module 中就是这样做的。

【讨论】:

  • 您还可以从 GitHub Releases 下载单个文件。请参阅我引用的 repo:一个文件:https://github.com/zingi/random-lon-lat-generator/releases/download/v0.1.0/random_lon_lat_generator.js
  • 一个文件和 zip 文件似乎都是 302。但是 zip url 似乎重定向到一个更永久的 url:https://github.com/zingi/random-lon-lat-generator/archive/refs/tags/v0.1.0.zip => https://codeload.github.com/zingi/random-lon-lat-generator/zip/refs/tags/v0.1.0
  • 关于raw.githubusercontent.com:我已经提到了3次,模块代码不是(还)是git代码的一部分,因此不能这样使用。跨度>
  • 我刚刚查了一下,与某个版本相关的 zip 存档似乎只包含了当时 git 跟踪的代码的快照。所以它不包含模块代码,它只在发布资产中可用。
  • @zingi 是的,我重新编写了第一段以使其更清晰,还包括一些可能对您有所帮助的选项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多