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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sdenv

v0.2.2

Published

补环境框架

Downloads

49

Readme

NPM version

sdenv是一个javascript运行时补环境框架,与github上其它补环境框架存在较大区别,sdenv是站在巨人的肩膀上实现的,依赖于jsdom的强大dom仿真能力,sdenv可以真实模拟浏览器执行环境,作者在固定随机数与添加sdenv-extend的部分插件后可以达到瑞数vmp代码在sdenv运行生成的cookie值与浏览器生成的cookie值一致

依赖

作者开发时使用的是v20.10.0版本node,预期最低要求是18版本,由于未做其它版本可用性测试,因此建议使用sdenv的node版本大于等于v20.10.0

编译node插件用的是node-gyp工具,该工具需要有python环境和c环境(如windows系统需安装Visual Studio,Mac系统需要安装XCode),请根据工具文档进行系统环境搭建。

需要注意windows中安装Visual Studio时需要勾选使用C++的桌面开发选项

安装Visual Studio注意

可能出现的问题

  1. npm安装node-gyp报错:请确保操作系统有c++编译环境与python环境,报错示例(感谢用户风流小混沌提供图片素材): npm安装报错
  2. 安装缓慢及canvas报错:由于canvas安装会优先从github获取现成的包,因此请在安装前先设置代理或者其它国内源,如果安装仍然失败请使用npm官方源+代理方式重新尝试;

解决完报错后记得重新执行下依赖安装!

有其它问题请提issues!

使用

npm包使用

  1. 创建自己的项目
  2. 项目中安装sdenv:npm i sdenv(请确保安装没有报错)
  3. 在项目中导入api并使用(可以参考example目录下的用例):
const { jsdomFromText, jsdomFromUrl, browser } = require('sdenv');

样例代码运行

clone项目仓库后执行依赖安装npm i,确保依赖安装成功后即可运行example目录下的样例文件了。

注意:样例代码仅供参考,作者建议使用npm包方式使用sdenv框架!

  1. 运行本地代码:use-local 样例调用
  2. 运行网站代码:use-remote 样例调用

API

sdenv设计极其简单,它的核心API只有一个,即browser!

browser(window: object, type: string)

传入window对象,和需要拟真的浏览器类型,browser方法会自动将浏览器特性集成到window对象中。

const { browser } = require('sdenv');
...
browser(window, 'chrome')

浏览器类型及支持情况:

类型 | 是否支持 ---- | -------- Chrome | Y Firefox | N Safari | N

jsdomFromText(config: object)

返回回调方法,用于纯文本方式调用jsdom,第一个参数为配置对象,最终会作为第二个参数传入到jsdom中。

const { Script } = require("vm");
const { jsdomFromText } = require('sdenv');
const [jsdomer, cookieJar] = jsdomFromText({
    url: 'https://host/path',
    referrer: 'https://host/path',
    contentType: "text/html",
    runScripts: "outside-only", // 不会执行html文本中的js代码
})
const dom = jsdomer('<html>...</html>');
new Script('javascript代码').runInContext(dom.getInternalVMContext()); // 执行javascript代码
console.log('cookie值:', cookieJar.getCookieStringSync('https://host'));

进一步阅读:

jsdom的JSDOM API

jsdomFromUrl(config?: object, cookieJar?: CookieJar)

返回回调方法用于链接形式调用jsdom,第一个参数为配置对象,与jsdomFromText方法不同,该配置对象用于配置ResourceLoader,建议至少传入ua值,否则请求header中的ua内容会有jsdom标识,需要注意的是,该ua仅在jsdom层使用,cookieJar非必传,当需要延续cookie时需要传入。

const { jsdomFromUrl } = require('sdenv');
const config = { userAgent: 'native browser userAgent' };
const [jsdomer, cookieJar] = jsdomFromUrl(config); // 返回自动生成的cookieJar
const oneDom = await jsdomer('https://host/path');
const twoDom = await jsdomFromUrl(config, cookieJar)[0]('https://host/path'); // 使用已经存在的cookieJar,因为要沿用上一次产生的cookie
console.log('cookie值:', cookieJar.getCookieStringSync('https://host'));

进一步阅读:

jsdom的ResourceLoader API

jsdom的CookieJar API

jsdom的fromURL API

sdenv-extend使用说明

为了模拟浏览器执行环境,需要将node环境与浏览器环境共有代码进行提取,并提供返回环境对象用于sdenv内window与dom内容补充使用。

sdenv-extend具体功能可参考项目内README文档

sdenv-jsdom使用说明

sdenv-jsdom包是sdenv补环境框架能运行瑞数vmp网站并产生正确cookie的核心,该包仓库fork自jsdom仓库,并应对瑞数vmp对jsdom的检测做了代码修改,因此sdenv可以过网站对jsdom的检测!

声明

该项目的开发基于瑞数vmp网站,不能保证在其它反爬虫产品稳定使用,出现问题请及时提issues或者提pull参与共建!

添加作者微信进技术交流群:howduudu_tech(备注sdenv)

订阅号不定时发表版本动态及技术文章:码功