【问题标题】:How to run an isolated Node.js environment inside browser?如何在浏览器中运行隔离的 Node.js 环境?
【发布时间】:2019-12-25 05:28:01
【问题描述】:

我正在尝试使用 isomorphic-git 在浏览器端执行 git clone 命令。但由于坚持同源政策,如果没有我可以使用的 cors-proxy,我无法向我们公司的 GitLab 发送跨域请求。出于安全考虑,我们公司的 GitLab 团队不愿意更改 CORS 配置。似乎没有办法从浏览器端克隆代码。同时,isomorphic-git 提供了 node-version 来克隆代码,不再存在跨域问题。但我不愿意再利用一台节点服务器来做到这一点。是否可以在浏览器中启动隔离的 Node.js 环境?

通过 corsProxy 使用 isomorphic-git 从 GitHub 克隆代码:

await git.clone({
  dir,
  corsProxy: 'https://cors.isomorphic-git.org',
  url: 'https://github.com/isomorphic-git/isomorphic-git',
  ref: 'master',
  singleBranch: true,
  depth: 10
});

【问题讨论】:

  • 这没有任何意义。 Node 是独立于浏览器(具有 html/css 渲染功能)的服务器的独立 JS 运行时。为什么一定要做这个浏览器端?!
  • 不,您不能在浏览器内或从浏览器运行节点,除非您有一个浏览器插件,该插件可以以执行此操作所需的提升权限运行,并且本地已经安装了大量代码.如果您对浏览器执行或安全模型有任何了解,那么据我所知,这个请求似乎没有什么意义。
  • 您始终可以开发桌面应用程序而不是网站。你知道,就像以前一样。 Electron 没有 CORS 问题。

标签: javascript node.js git cors isomorphic-git


【解决方案1】:

在我看来答案是否定的,因为即使你以某种方式将 NodeJS(模拟器?环境?东西?)插入 JavaScript 线程,当它想要 git clone 时,它也必须调用浏览器的 API .然后你在同一个地方:JavaScript 仍然受制于同源策略。

如果您可以做到这一点,您应该考虑获得漏洞赏金;听起来像是一个很大的安全漏洞:-)

【讨论】:

    【解决方案2】:

    如果您的项目很小,您可以使用免费的 cors 代理 - https://cors.isomorphic-git.org/ 在浏览器端克隆一个 repo。

    用法:

    git.clone({
        fs,
        http,
        dir,
        corsProxy: 'https://cors.isomorphic-git.org',
        url: 'https://github.com/example.git',
        ref: 'master',
        singleBranch: true,
        depth: 10
    });
    

    参考:https://github.com/isomorphic-git/isomorphic-git#cors-support

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 1970-01-01
      • 2016-06-27
      • 2020-07-28
      • 2020-04-28
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      相关资源
      最近更新 更多