@pawlogic/dl 0.4.1 → 0.5.0

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.
package/dl-npm-update.sh CHANGED
@@ -29,10 +29,13 @@ TTL=300
29
29
  PACKAGE="@pawlogic/dl"
30
30
 
31
31
  # ── 观测上报: fire-and-forget JSON POST,失败 swallow ────────────────────────
32
- # args: outcome duration_ms
32
+ # args: outcome duration_ms [error_tail_file]
33
+ # error_tail_file 仅 npm_fail 分支传入,读末尾 ~200 字节带上去 —— 之前 npm stderr
34
+ # 全 swallow,根本看不到为什么失败,fleet 升级率 0.24% 都查不到根因。
33
35
  report_event() {
34
36
  _outcome="$1"
35
37
  _dur="${2:-null}"
38
+ _errfile="${3:-}"
36
39
  [ -z "${CALLBACK_SERVER_URL:-}" ] && return 0
37
40
  command -v curl >/dev/null 2>&1 || return 0
38
41
  _tag="${DL_NPM_DIST_TAG:-unknown}"
@@ -45,11 +48,20 @@ report_event() {
45
48
  if [ -f "$_verfile" ] && command -v node >/dev/null 2>&1; then
46
49
  _ver=$(node -e 'try{console.log(require("'"$_verfile"'").version||"")}catch(e){}' 2>/dev/null || echo "")
47
50
  fi
51
+ # error_tail: 末尾 200 字节, JSON 不安全字符 (\ " 控制符 / 换行 / tab) 一律压成
52
+ # 单空格; 长度上限挡住 LB 滥用,字符替换让 JSON 不需要复杂 escape。
53
+ _err_json="null"
54
+ if [ -n "$_errfile" ] && [ -f "$_errfile" ]; then
55
+ _err_raw=$(tail -c 200 "$_errfile" 2>/dev/null | tr '\n\r\t\\"' ' ' | tr -d '\000-\037')
56
+ if [ -n "$_err_raw" ]; then
57
+ _err_json="\"$_err_raw\""
58
+ fi
59
+ fi
48
60
  # 后台 curl, -m 2 上限 2s; 不解析响应; stderr/stdout 全丢。
49
61
  (
50
62
  curl -fsS -m 2 -X POST "${CALLBACK_SERVER_URL%/}/internal/dl-selfupdate-event" \
51
63
  -H 'content-type: application/json' \
52
- -d "{\"trigger\":\"wrapper\",\"dist_tag\":\"$_tag\",\"outcome\":\"$_outcome\",\"duration_ms\":$_dur,\"installed_version\":\"$_ver\",\"build_short_sha\":null}" \
64
+ -d "{\"trigger\":\"wrapper\",\"dist_tag\":\"$_tag\",\"outcome\":\"$_outcome\",\"duration_ms\":$_dur,\"installed_version\":\"$_ver\",\"build_short_sha\":null,\"error_tail\":$_err_json}" \
53
65
  >/dev/null 2>&1 || true
54
66
  ) &
55
67
  }
@@ -89,7 +101,16 @@ fi
89
101
  # flock 不一定有(busybox sh) → mkdir 兜底原子锁。
90
102
  run_install() {
91
103
  _t0=$(date +%s 2>/dev/null || echo 0)
92
- if npm install -g --silent --no-audit --no-fund --no-update-notifier "$PACKAGE@$DL_NPM_DIST_TAG" >/dev/null 2>&1; then
104
+ # stderr 写到独立文件 npm_fail 分支带末尾给观测端;stdout 仍丢掉。
105
+ _errlog=$(mktemp 2>/dev/null || echo "/tmp/dl-npm-err.$$")
106
+ # 必须带 --prefix=/home/user/.local 跟 HOME=/home/user,跟 sandbox-template
107
+ # 首次安装那一段对齐 (packages/sandbox-template/scripts/publish-e2b-template.ts
108
+ # 里 wrapper 写法)。沙箱进程 owner 是 user,无 /usr/lib/node_modules 写权限;
109
+ # 不带 prefix → npm 走默认全局路径 → EACCES 1 秒内挂掉, stderr 又被吞 → fleet
110
+ # 自更新 100% 失败但完全黑盒。这是 PR #729 漏掉 prefix 留下的 bug。
111
+ if HOME=/home/user npm install -g --prefix=/home/user/.local \
112
+ --no-audit --no-fund --no-update-notifier \
113
+ "$PACKAGE@$DL_NPM_DIST_TAG" >/dev/null 2>"$_errlog"; then
93
114
  _outcome=ok
94
115
  else
95
116
  _outcome=npm_fail
@@ -114,7 +135,12 @@ run_install() {
114
135
  chmod +x /home/user/.local/lib/dl-npm-update.sh 2>/dev/null || true
115
136
  fi
116
137
  echo "$now" > "$STAMP"
117
- report_event "$_outcome" "$_dur_ms"
138
+ if [ "$_outcome" = "npm_fail" ]; then
139
+ report_event "$_outcome" "$_dur_ms" "$_errlog"
140
+ else
141
+ report_event "$_outcome" "$_dur_ms"
142
+ fi
143
+ rm -f "$_errlog" 2>/dev/null || true
118
144
  }
119
145
 
120
146
  if command -v flock >/dev/null 2>&1; then