npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

sorl2pac

v0.1.0

Published

Convert SwitchyOmega rule lists to PAC scripts.

Readme

English | 中文

sorl2pac

将 SwitchyOmega 规则列表(*.sorl)转换为 PAC 脚本。

使用方法

import { sorl2pac } from "sorl2pac";

const pacSource = sorl2pac(sorlText, {
	matched: "DIRECT",
	unmatched: "PROXY 127.0.0.1:7890",
});

pacSource 是一份完整的 PAC 脚本。它包含一个内部的 match(host) 辅助函数,以及标准的 FindProxyForURL(url, host) 函数。

function FindProxyForURL(url, host) {
	return match(host) ? "DIRECT" : "PROXY 127.0.0.1:7890";
}

API

function sorl2pac(
	sorl: string,
	results: {
		matched: string;
		unmatched: string;
	},
): string;
  • sorl 必须是一个字符串,内容为受支持的 SwitchyOmega 规则列表。
  • match(host)true 时,FindProxyForURL() 会返回 results.matched
  • match(host)false 时,会返回 results.unmatched
  • match(host) 会生成在 PAC 脚本内部,它不是额外导出的包接口。

如果 API 参数无效,sorl2pac() 会抛出 TypeError;如果规则列表语法不受支持,则会抛出 SyntaxError

支持的语法

这个包有意只支持一小部分 SwitchyOmega 新格式语法:

  • [SwitchyOmega Conditions]
  • 空行
  • ; 注释行
  • @note
  • 裸写的 HostWildcardCondition 规则,例如 *.example.com
  • host 通配符前缀:HostWildcard:HostWildcardCondition:Host:H:W:HW:
  • ! 排除规则

不受支持的语法会直接失败,而不是被忽略;这包括旧版 #BEGIN 规则列表、@with result+profile、URL 条件、IP 条件、时间条件以及 AutoProxy 语法。

匹配行为

  • 规则会从上到下依次检查。
  • 普通规则命中时返回 true
  • 排除规则命中时返回 false
  • 没有规则命中时返回 false
  • 空规则列表是有效的,并且始终返回 false
  • *.abc.com 同时匹配 abc.comsub.abc.com
  • **.abc.com 匹配 sub.abc.com,但不匹配 abc.com

由于 results 可配置,调用方可以用同一份规则实现白名单或黑名单行为。

const whitelistPac = sorl2pac(sorlText, {
	matched: "DIRECT",
	unmatched: "PROXY 127.0.0.1:7890",
});

const blacklistPac = sorl2pac(sorlText, {
	matched: "PROXY 127.0.0.1:7890",
	unmatched: "DIRECT",
});

License

Any code contributed to this project is considered authorized for commercial use by the project authors and their affiliated companies and distributed under this project's license.

任何贡献到本项目的代码,均视为授权本项目作者及其关联公司用于商业用途,并可按本项目协议进行分发。

MIT