Socket
Book a DemoInstallSign in
Socket

upstreamjs

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

upstreamjs

JavaScript请求服务负载均衡,仿nginx upstream

1.0.0
unpublished
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

upstream

JavaScript请求服务负载均衡,仿nginx upstream

说明

  • 根据配置的源发起fetch 返回ok=true表示可用
  • 源须支持跨域请求
  • 3 秒超时, 10 秒请求缓存,缓存全局对象upstreamCache

作用

  • 无缝切换可用的源
  • 优先选择最快的源

使用

  • https://ss.netnr.com/weather
  • https://ss.netnr.com/wallpaper
  • https://www.netnr.com/run/code/4890439129232505252
var hosts = ["lib.baomitu.com", "unpkg.com"];

//所有源请求完或默认超时
upstream(hosts, function (fast, ok) {
    //fast:最快的源
    //ok:可用的源 数组
    console.log(fast, ok)
});

//最快的一个源或默认超时
upstream(hosts, function (fast, ok) {
    console.log(fast, ok)
}, 1);

//所有源请求完或自定义超时
upstream(hosts, function (fast, ok) {
    console.log(fast, ok)
}, 280);


//主机头跳转或404等,须请求具体路径的情况
upstream([
    "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js",
    "https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js",
    "https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"
], function (fast, ok) {
    console.log(fast, ok);
    //根据ok变量提取可用的源
});


/*
 实战:使用跨域代理解决不能直接请求API的情况,且转换HTTP为HTTPS协议
 https://ss.netnr.com/weather
 一个天气接口,直接发起请求存在跨域的问题,而且是HTTP协议
 配置多个跨域服务的源,调用跨域服务得到API数据
*/
upstream(["https://proxy.zme.ink/", "https://bird.ioliu.cn/v2?url="], function (fast) {
    var api = "http://wthrcdn.etouch.cn/weather_mini?citykey=101040100";
    var url = fast + api;
    fetch(url).then(res => res.json()).then(data => console.log(data));
}, 1);

Keywords

upstream

FAQs

Package last updated on 09 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.