publishport-opencli 1.0.44 → 1.0.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{execFileSync as f}from"node:child_process";import*as i from"node:fs";import*as d from"node:os";import*as s from"node:path";import{Readable as x}from"node:stream";import{pipeline as y}from"node:stream/promises";import{CommandExecutionError as g}from"../errors.js";import{log as u}from"../logger.js";const p="https://cdn.npmmirror.com/binaries/chrome-for-testing";function h(){const{platform:r,arch:o}=process;if(r==="darwin")return o==="arm64"?"mac-arm64":"mac-x64";if(r==="win32")return o==="ia32"?"win32":"win64";if(r==="linux")return o==="arm64"?"linux-arm64":"linux64";return null}function m(r,o){if(o.startsWith("mac"))return s.join(r,`chrome-${o}`,"Google Chrome for Testing.app","Contents","MacOS","Google Chrome for Testing");if(o.startsWith("win"))return s.join(r,`chrome-${o}`,"chrome.exe");return s.join(r,`chrome-${o}`,"chrome")}export function browsersDir(){const r=process.env.OPENCLI_CONFIG_DIR||s.join(d.homedir(),".ppcli");return s.join(r,"browsers")}function S(r,o){const n=r.split(".").map(Number),e=o.split(".").map(Number);for(let t=0;t<Math.max(n.length,e.length);t++){const c=(n[t]??0)-(e[t]??0);if(c!==0)return c}return 0}export function findDownloadedChrome(){const r=h();if(!r)return null;const o=new RegExp(`^chrome-(\\d+(?:\\.\\d+)+)-${r}$`);let n;try{n=i.readdirSync(browsersDir())}catch(t){if(t.code==="ENOENT")return null;throw t}const e=n.map((t)=>o.exec(t)?.[1]).filter((t)=>!!t).sort((t,c)=>S(c,t));for(const t of e){const c=m(s.join(browsersDir(),`chrome-${t}-${r}`),r);if(i.existsSync(c))return c}return null}async function b(){const r=`${p}/last-known-good-versions.json`,o=await fetch(r,{signal:AbortSignal.timeout(20000)});if(!o.ok)throw Error(`读取浏览器版本清单失败:HTTP ${o.status} ${r}`);const e=(await o.json()).channels?.Stable?.version;if(!e||!/^\d+(\.\d+)+$/.test(e))throw Error(`浏览器版本清单格式异常:${r}`);return e}async function C(r,o){const n=await fetch(r,{signal:AbortSignal.timeout(900000)});if(!n.ok||!n.body)throw Error(`下载浏览器失败:HTTP ${n.status} ${r}`);const e=Number(n.headers.get("content-length")??0);u.info(`正在下载专用浏览器(${e?`${Math.round(e/1024/1024)} MB`:"大小未知"}),首次使用需要一到两分钟……`);await y(x.fromWeb(n.body),i.createWriteStream(o));const t=i.statSync(o).size;if(e&&t!==e)throw Error(`下载浏览器不完整:应为 ${e} 字节,实际 ${t} 字节`)}function E(r,o){if(process.platform==="darwin")f("ditto",["-x","-k",r,o],{stdio:"pipe"});else if(process.platform==="win32"){const n=s.join(process.env.SystemRoot??"C:\\Windows","System32","tar.exe");f(n,["-xf",r,"-C",o],{stdio:"pipe"})}else f("unzip",["-q",r,"-d",o],{stdio:"pipe"})}async function j(){const r=h();if(!r)throw Error(`当前系统(${process.platform}-${process.arch})没有可下载的浏览器版本`);const o=await b(),n=s.join(browsersDir(),`chrome-${o}-${r}`),e=m(n,r);if(i.existsSync(e))return e;i.mkdirSync(browsersDir(),{recursive:!0});const t=i.mkdtempSync(s.join(browsersDir(),`.tmp-${o}-`));try{const c=s.join(t,`chrome-${r}.zip`);await C(`${p}/${o}/${r}/chrome-${r}.zip`,c);const a=s.join(t,"out");i.mkdirSync(a);E(c,a);if(!i.existsSync(m(a,r)))throw Error(`浏览器安装包内未找到可执行文件:${s.relative(a,m(a,r))}`);try{i.renameSync(a,n)}catch(w){if(!i.existsSync(e))throw w}u.info(`专用浏览器已就绪:Chrome ${o}`);return e}finally{i.rmSync(t,{recursive:!0,force:!0})}}let l=null;export async function ensureDownloadedChrome(){const r=findDownloadedChrome();if(r)return r;l??=j().finally(()=>{l=null});try{return await l}catch(o){throw new g("本机没有 Chrome / Edge,自动下载浏览器失败。",`${o instanceof Error?o.message:String(o)}。请检查网络后重试,或安装 Google Chrome。`)}}
1
+ import{execFileSync as f}from"node:child_process";import*as i from"node:fs";import*as d from"node:os";import*as s from"node:path";import{Readable as y}from"node:stream";import{pipeline as x}from"node:stream/promises";import{CommandExecutionError as S}from"../errors.js";import{log as u}from"../logger.js";import{withBrowserSessionLock as g}from"../browser-session-lock.js";const p="https://cdn.npmmirror.com/binaries/chrome-for-testing";function h(){const{platform:r,arch:o}=process;if(r==="darwin")return o==="arm64"?"mac-arm64":"mac-x64";if(r==="win32")return o==="ia32"?"win32":"win64";if(r==="linux")return o==="arm64"?"linux-arm64":"linux64";return null}function m(r,o){if(o.startsWith("mac"))return s.join(r,`chrome-${o}`,"Google Chrome for Testing.app","Contents","MacOS","Google Chrome for Testing");if(o.startsWith("win"))return s.join(r,`chrome-${o}`,"chrome.exe");return s.join(r,`chrome-${o}`,"chrome")}export function browsersDir(){const r=process.env.OPENCLI_CONFIG_DIR||s.join(d.homedir(),".ppcli");return s.join(r,"browsers")}function b(r,o){const n=r.split(".").map(Number),e=o.split(".").map(Number);for(let t=0;t<Math.max(n.length,e.length);t++){const c=(n[t]??0)-(e[t]??0);if(c!==0)return c}return 0}export function findDownloadedChrome(){const r=h();if(!r)return null;const o=new RegExp(`^chrome-(\\d+(?:\\.\\d+)+)-${r}$`);let n;try{n=i.readdirSync(browsersDir())}catch(t){if(t.code==="ENOENT")return null;throw t}const e=n.map((t)=>o.exec(t)?.[1]).filter((t)=>!!t).sort((t,c)=>b(c,t));for(const t of e){const c=m(s.join(browsersDir(),`chrome-${t}-${r}`),r);if(i.existsSync(c))return c}return null}async function C(){const r=`${p}/last-known-good-versions.json`,o=await fetch(r,{signal:AbortSignal.timeout(20000)});if(!o.ok)throw Error(`读取浏览器版本清单失败:HTTP ${o.status} ${r}`);const e=(await o.json()).channels?.Stable?.version;if(!e||!/^\d+(\.\d+)+$/.test(e))throw Error(`浏览器版本清单格式异常:${r}`);return e}async function E(r,o){const n=await fetch(r,{signal:AbortSignal.timeout(900000)});if(!n.ok||!n.body)throw Error(`下载浏览器失败:HTTP ${n.status} ${r}`);const e=Number(n.headers.get("content-length")??0);u.info(`正在下载专用浏览器(${e?`${Math.round(e/1024/1024)} MB`:"大小未知"}),首次使用需要一到两分钟……`);await x(y.fromWeb(n.body),i.createWriteStream(o));const t=i.statSync(o).size;if(e&&t!==e)throw Error(`下载浏览器不完整:应为 ${e} 字节,实际 ${t} 字节`)}function j(r,o){if(process.platform==="darwin")f("ditto",["-x","-k",r,o],{stdio:"pipe"});else if(process.platform==="win32"){const n=s.join(process.env.SystemRoot??"C:\\Windows","System32","tar.exe");f(n,["-xf",r,"-C",o],{stdio:"pipe"})}else f("unzip",["-q",r,"-d",o],{stdio:"pipe"})}async function v(){const r=h();if(!r)throw Error(`当前系统(${process.platform}-${process.arch})没有可下载的浏览器版本`);const o=await C(),n=s.join(browsersDir(),`chrome-${o}-${r}`),e=m(n,r);if(i.existsSync(e))return e;i.mkdirSync(browsersDir(),{recursive:!0});const t=i.mkdtempSync(s.join(browsersDir(),`.tmp-${o}-`));try{const c=s.join(t,`chrome-${r}.zip`);await E(`${p}/${o}/${r}/chrome-${r}.zip`,c);const a=s.join(t,"out");i.mkdirSync(a);j(c,a);if(!i.existsSync(m(a,r)))throw Error(`浏览器安装包内未找到可执行文件:${s.relative(a,m(a,r))}`);try{i.renameSync(a,n)}catch(w){if(!i.existsSync(e))throw w}u.info(`专用浏览器已就绪:Chrome ${o}`);return e}finally{i.rmSync(t,{recursive:!0,force:!0})}}let l=null;export async function ensureDownloadedChrome(){const r=findDownloadedChrome();if(r)return r;l??=g(async()=>findDownloadedChrome()??v(),"chrome-download").finally(()=>{l=null});try{return await l}catch(o){throw new S("本机没有 Chrome / Edge,自动下载浏览器失败。",`${o instanceof Error?o.message:String(o)}。请检查网络后重试,或安装 Google Chrome。`)}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "publishport-opencli",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": false