@xrkseek/xrk-phone-bridge 0.7.26 → 0.8.9
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/lib/catalog.mjs +186 -186
- package/lib/http.mjs +50 -50
- package/lib/runtime.mjs +855 -855
- package/materialize.mjs +391 -307
- package/package.json +24 -24
- package/skills/xrk-phone/SKILL.md +66 -66
package/lib/catalog.mjs
CHANGED
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remote phone_* schemas — mirror PhoneToolCatalog (Android).
|
|
3
|
-
* Keep names/descriptions stable; drift breaks model KV cache.
|
|
4
|
-
*/
|
|
5
|
-
const str = (description) => ({ type: "string", description });
|
|
6
|
-
const bool = (description) => ({ type: "boolean", description });
|
|
7
|
-
const int = (description) => ({ type: "integer", description });
|
|
8
|
-
const num = (description) => ({ type: "number", description });
|
|
9
|
-
const en = (values, description) => ({ type: "string", enum: values, description });
|
|
10
|
-
|
|
11
|
-
const SCREEN_ID = str("屏幕号,如 s7(主屏)或 b3(后台屏),取自最近一次返回的屏幕");
|
|
12
|
-
const INTENT_KINDS = [
|
|
13
|
-
"alarm",
|
|
14
|
-
"timer",
|
|
15
|
-
"calendar",
|
|
16
|
-
"dial",
|
|
17
|
-
"sms_draft",
|
|
18
|
-
"share",
|
|
19
|
-
"navigate",
|
|
20
|
-
"settings",
|
|
21
|
-
];
|
|
22
|
-
const SETTINGS_PAGES = [
|
|
23
|
-
"home",
|
|
24
|
-
"wifi",
|
|
25
|
-
"bluetooth",
|
|
26
|
-
"mobile_network",
|
|
27
|
-
"airplane",
|
|
28
|
-
"display",
|
|
29
|
-
"sound",
|
|
30
|
-
"battery",
|
|
31
|
-
"location",
|
|
32
|
-
"date",
|
|
33
|
-
"language",
|
|
34
|
-
"storage",
|
|
35
|
-
"nfc",
|
|
36
|
-
"app_notifications",
|
|
37
|
-
];
|
|
38
|
-
const KEYS = ["back", "home", "recents", "notifications"];
|
|
39
|
-
const DIRECTIONS = ["up", "down", "left", "right"];
|
|
40
|
-
const NAV_MODES = ["drive", "transit", "walk", "ride"];
|
|
41
|
-
|
|
42
|
-
function tool(name, description, properties, required = []) {
|
|
43
|
-
const inputSchema = {
|
|
44
|
-
type: "object",
|
|
45
|
-
properties,
|
|
46
|
-
additionalProperties: false,
|
|
47
|
-
};
|
|
48
|
-
if (required.length) inputSchema.required = required;
|
|
49
|
-
return { name, description, inputSchema };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** @type {readonly { name: string, description: string, inputSchema: object }[]} */
|
|
53
|
-
export const PHONE_TOOL_CATALOG = [
|
|
54
|
-
tool(
|
|
55
|
-
"apps",
|
|
56
|
-
"列出这台手机上能打开的应用(名称 · 包名)。query 可用中文名或包名;先 apps 再 open,拿返回的包名最稳。",
|
|
57
|
-
{ query: str("按名称(含中文)或包名筛选,可省略") },
|
|
58
|
-
),
|
|
59
|
-
tool(
|
|
60
|
-
"open",
|
|
61
|
-
"尝试打开应用或链接(app 或 url 二选一)。app 优先用包名(如 com.taobao.taobao);中文应用名也可,找不到就先 phone_apps。background:true 仅用于在后台屏打开应用(需用户开通不打扰模式);可读取时返回新屏幕。启动回话不表示目标业务已经完成。",
|
|
62
|
-
{
|
|
63
|
-
app: str("包名优先;也可用中文/英文应用名"),
|
|
64
|
-
url: str("网址或深链"),
|
|
65
|
-
background: bool("在后台屏打开"),
|
|
66
|
-
},
|
|
67
|
-
),
|
|
68
|
-
tool(
|
|
69
|
-
"intent",
|
|
70
|
-
"用系统意图把事务交给处理应用,能办就别点界面;后续界面和确认由该应用决定,启动回话不证明事务完成。字段按 kind:" +
|
|
71
|
-
"alarm(hour,minute,days?,message?)、timer(seconds,message?)、calendar(title,begin,end?,all_day?,location?,description?)、" +
|
|
72
|
-
"dial(number)、sms_draft(number?,text)、share(text,app?)、navigate(destination|lat+lng,mode?,map?)、settings(page,app?)。",
|
|
73
|
-
{
|
|
74
|
-
kind: en(INTENT_KINDS, "事务类型"),
|
|
75
|
-
hour: int("时 0–23"),
|
|
76
|
-
minute: int("分 0–59"),
|
|
77
|
-
days: {
|
|
78
|
-
type: "array",
|
|
79
|
-
items: { type: "integer" },
|
|
80
|
-
description: "重复的星期,1=周一 … 7=周日;省略只响一次",
|
|
81
|
-
},
|
|
82
|
-
seconds: int("秒 1–86400"),
|
|
83
|
-
message: str("闹钟 / 计时器标签"),
|
|
84
|
-
title: str("日程标题"),
|
|
85
|
-
begin: str("开始,如 2026-10-24T19:00(手机时区);全天写 2026-10-24"),
|
|
86
|
-
end: str("结束,省略 = 开始后 1 小时"),
|
|
87
|
-
all_day: bool("全天"),
|
|
88
|
-
location: str("地点"),
|
|
89
|
-
description: str("备注"),
|
|
90
|
-
number: str("电话号码"),
|
|
91
|
-
text: str("文字内容"),
|
|
92
|
-
app: str("分享目标应用(省略弹分享面板);settings:app_notifications 用哪个应用"),
|
|
93
|
-
destination: str("目的地名称或地址"),
|
|
94
|
-
lat: num("纬度 GCJ-02"),
|
|
95
|
-
lng: num("经度 GCJ-02"),
|
|
96
|
-
mode: en(NAV_MODES, "出行方式,默认 drive"),
|
|
97
|
-
map: en(["amap", "baidu"], "地图,默认先高德再百度"),
|
|
98
|
-
page: en(SETTINGS_PAGES, "设置页;app_notifications 还需 app"),
|
|
99
|
-
},
|
|
100
|
-
["kind"],
|
|
101
|
-
),
|
|
102
|
-
tool(
|
|
103
|
-
"notify",
|
|
104
|
-
"给用户发一条手机通知。",
|
|
105
|
-
{ title: str("标题"), text: str("正文") },
|
|
106
|
-
["title", "text"],
|
|
107
|
-
),
|
|
108
|
-
tool(
|
|
109
|
-
"clipboard",
|
|
110
|
-
"读写剪贴板:set 写入文字;get:true 读取(系统只允许前台应用读,XRK 不在前台时读不到)。",
|
|
111
|
-
{ set: str("要写入的文字"), get: bool("读取剪贴板") },
|
|
112
|
-
),
|
|
113
|
-
tool(
|
|
114
|
-
"screen",
|
|
115
|
-
"读当前屏幕:每行一个元素,[编号] 只在本屏有效。detail:true 附坐标,结果可能截断;image:true 请求截图,安全或能力限制时可能不附图(主屏可带编号,后台图不带编号);background:true 读后台屏。元素少或为空不表示页面安全;本机文字识别不可用时按回话提示处理,无法核对目标就 handoff。",
|
|
116
|
-
{
|
|
117
|
-
detail: bool("附上已返回元素的坐标,不保证完整页面"),
|
|
118
|
-
image: bool("请求附带截图;可能因安全或能力限制省略"),
|
|
119
|
-
background: bool("读后台屏(不打扰模式)"),
|
|
120
|
-
},
|
|
121
|
-
),
|
|
122
|
-
tool(
|
|
123
|
-
"collect",
|
|
124
|
-
"只读:自动滚动列表收集全部条目(按文字去重),读长列表只用这一次。",
|
|
125
|
-
{
|
|
126
|
-
screen: SCREEN_ID,
|
|
127
|
-
list: int("列表的编号"),
|
|
128
|
-
max_pages: int("最多翻几页(1–30),默认 10"),
|
|
129
|
-
until: str("出现这段文字就停"),
|
|
130
|
-
},
|
|
131
|
-
["screen", "list"],
|
|
132
|
-
),
|
|
133
|
-
tool(
|
|
134
|
-
"tap",
|
|
135
|
-
"点一个元素,返回新屏幕。敏感按钮或首次操作该 App 会先请用户确认。",
|
|
136
|
-
{ screen: SCREEN_ID, id: int("元素编号"), long: bool("长按") },
|
|
137
|
-
["screen", "id"],
|
|
138
|
-
),
|
|
139
|
-
tool(
|
|
140
|
-
"type",
|
|
141
|
-
"在输入框输入文字(替换原内容),返回回读和新屏幕。submit:true 后按回车 / 发送,要确认。密码、验证码、支付框默认拒绝,被拒绝就 handoff。",
|
|
142
|
-
{
|
|
143
|
-
screen: SCREEN_ID,
|
|
144
|
-
id: int("输入框编号"),
|
|
145
|
-
text: str("要输入的文字"),
|
|
146
|
-
submit: bool("输入后提交 / 发送"),
|
|
147
|
-
},
|
|
148
|
-
["screen", "id", "text"],
|
|
149
|
-
),
|
|
150
|
-
tool(
|
|
151
|
-
"scroll",
|
|
152
|
-
"滚动,返回新屏幕。id 是要滚动的列表,省略时滚主列表。",
|
|
153
|
-
{
|
|
154
|
-
screen: SCREEN_ID,
|
|
155
|
-
id: int("列表编号,可省略"),
|
|
156
|
-
direction: en(DIRECTIONS, "方向"),
|
|
157
|
-
},
|
|
158
|
-
["screen", "direction"],
|
|
159
|
-
),
|
|
160
|
-
tool(
|
|
161
|
-
"key",
|
|
162
|
-
"按系统键(back / home / recents / notifications),返回新屏幕。",
|
|
163
|
-
{ key: en(KEYS, "按键"), screen: str("屏幕号;省略时作用于主屏") },
|
|
164
|
-
["key"],
|
|
165
|
-
),
|
|
166
|
-
tool(
|
|
167
|
-
"tap_xy",
|
|
168
|
-
"按本任务最近一次实际返回截图的像素坐标操作,只在元素里找不到目标时用。action 默认为 tap;long 长按;swipe 从 (x,y) 滑到 (x2,y2)。主屏用 s 号、后台屏用 b 号;截图超过 30 秒或上下文核对失败要重读。每张图只供一次坐标手势;需通过安全检查与所需用户确认,长按和滑动仍逐次确认。结果可报告画面变化,但不表示目标业务成功;结果未知或未变化先读屏,不自动重试。",
|
|
169
|
-
{
|
|
170
|
-
observation: str("本任务最近一次实际返回截图的屏幕号(主屏 s 号或后台屏 b 号)"),
|
|
171
|
-
x: int("横坐标(像素,从 0 开始)"),
|
|
172
|
-
y: int("纵坐标(像素,从 0 开始)"),
|
|
173
|
-
action: en(["tap", "long", "swipe"], "操作类型,省略为 tap;long 为长按,swipe 为滑动"),
|
|
174
|
-
x2: int("仅 swipe:终点横坐标(原截图像素,非负整数)"),
|
|
175
|
-
y2: int("仅 swipe:终点纵坐标(原截图像素,非负整数)"),
|
|
176
|
-
duration_ms: int("仅 swipe:时长 100–2000 毫秒,省略为 400"),
|
|
177
|
-
},
|
|
178
|
-
["observation", "x", "y"],
|
|
179
|
-
),
|
|
180
|
-
tool(
|
|
181
|
-
"handoff",
|
|
182
|
-
"把手机交给用户完成登录、验证码、支付或没把握的步骤;等用户点「继续」或「停止」,最长 10 分钟。",
|
|
183
|
-
{ reason: str("要用户做什么,一句话") },
|
|
184
|
-
["reason"],
|
|
185
|
-
),
|
|
186
|
-
];
|
|
1
|
+
/**
|
|
2
|
+
* Remote phone_* schemas — mirror PhoneToolCatalog (Android).
|
|
3
|
+
* Keep names/descriptions stable; drift breaks model KV cache.
|
|
4
|
+
*/
|
|
5
|
+
const str = (description) => ({ type: "string", description });
|
|
6
|
+
const bool = (description) => ({ type: "boolean", description });
|
|
7
|
+
const int = (description) => ({ type: "integer", description });
|
|
8
|
+
const num = (description) => ({ type: "number", description });
|
|
9
|
+
const en = (values, description) => ({ type: "string", enum: values, description });
|
|
10
|
+
|
|
11
|
+
const SCREEN_ID = str("屏幕号,如 s7(主屏)或 b3(后台屏),取自最近一次返回的屏幕");
|
|
12
|
+
const INTENT_KINDS = [
|
|
13
|
+
"alarm",
|
|
14
|
+
"timer",
|
|
15
|
+
"calendar",
|
|
16
|
+
"dial",
|
|
17
|
+
"sms_draft",
|
|
18
|
+
"share",
|
|
19
|
+
"navigate",
|
|
20
|
+
"settings",
|
|
21
|
+
];
|
|
22
|
+
const SETTINGS_PAGES = [
|
|
23
|
+
"home",
|
|
24
|
+
"wifi",
|
|
25
|
+
"bluetooth",
|
|
26
|
+
"mobile_network",
|
|
27
|
+
"airplane",
|
|
28
|
+
"display",
|
|
29
|
+
"sound",
|
|
30
|
+
"battery",
|
|
31
|
+
"location",
|
|
32
|
+
"date",
|
|
33
|
+
"language",
|
|
34
|
+
"storage",
|
|
35
|
+
"nfc",
|
|
36
|
+
"app_notifications",
|
|
37
|
+
];
|
|
38
|
+
const KEYS = ["back", "home", "recents", "notifications"];
|
|
39
|
+
const DIRECTIONS = ["up", "down", "left", "right"];
|
|
40
|
+
const NAV_MODES = ["drive", "transit", "walk", "ride"];
|
|
41
|
+
|
|
42
|
+
function tool(name, description, properties, required = []) {
|
|
43
|
+
const inputSchema = {
|
|
44
|
+
type: "object",
|
|
45
|
+
properties,
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
};
|
|
48
|
+
if (required.length) inputSchema.required = required;
|
|
49
|
+
return { name, description, inputSchema };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** @type {readonly { name: string, description: string, inputSchema: object }[]} */
|
|
53
|
+
export const PHONE_TOOL_CATALOG = [
|
|
54
|
+
tool(
|
|
55
|
+
"apps",
|
|
56
|
+
"列出这台手机上能打开的应用(名称 · 包名)。query 可用中文名或包名;先 apps 再 open,拿返回的包名最稳。",
|
|
57
|
+
{ query: str("按名称(含中文)或包名筛选,可省略") },
|
|
58
|
+
),
|
|
59
|
+
tool(
|
|
60
|
+
"open",
|
|
61
|
+
"尝试打开应用或链接(app 或 url 二选一)。app 优先用包名(如 com.taobao.taobao);中文应用名也可,找不到就先 phone_apps。background:true 仅用于在后台屏打开应用(需用户开通不打扰模式);可读取时返回新屏幕。启动回话不表示目标业务已经完成。",
|
|
62
|
+
{
|
|
63
|
+
app: str("包名优先;也可用中文/英文应用名"),
|
|
64
|
+
url: str("网址或深链"),
|
|
65
|
+
background: bool("在后台屏打开"),
|
|
66
|
+
},
|
|
67
|
+
),
|
|
68
|
+
tool(
|
|
69
|
+
"intent",
|
|
70
|
+
"用系统意图把事务交给处理应用,能办就别点界面;后续界面和确认由该应用决定,启动回话不证明事务完成。字段按 kind:" +
|
|
71
|
+
"alarm(hour,minute,days?,message?)、timer(seconds,message?)、calendar(title,begin,end?,all_day?,location?,description?)、" +
|
|
72
|
+
"dial(number)、sms_draft(number?,text)、share(text,app?)、navigate(destination|lat+lng,mode?,map?)、settings(page,app?)。",
|
|
73
|
+
{
|
|
74
|
+
kind: en(INTENT_KINDS, "事务类型"),
|
|
75
|
+
hour: int("时 0–23"),
|
|
76
|
+
minute: int("分 0–59"),
|
|
77
|
+
days: {
|
|
78
|
+
type: "array",
|
|
79
|
+
items: { type: "integer" },
|
|
80
|
+
description: "重复的星期,1=周一 … 7=周日;省略只响一次",
|
|
81
|
+
},
|
|
82
|
+
seconds: int("秒 1–86400"),
|
|
83
|
+
message: str("闹钟 / 计时器标签"),
|
|
84
|
+
title: str("日程标题"),
|
|
85
|
+
begin: str("开始,如 2026-10-24T19:00(手机时区);全天写 2026-10-24"),
|
|
86
|
+
end: str("结束,省略 = 开始后 1 小时"),
|
|
87
|
+
all_day: bool("全天"),
|
|
88
|
+
location: str("地点"),
|
|
89
|
+
description: str("备注"),
|
|
90
|
+
number: str("电话号码"),
|
|
91
|
+
text: str("文字内容"),
|
|
92
|
+
app: str("分享目标应用(省略弹分享面板);settings:app_notifications 用哪个应用"),
|
|
93
|
+
destination: str("目的地名称或地址"),
|
|
94
|
+
lat: num("纬度 GCJ-02"),
|
|
95
|
+
lng: num("经度 GCJ-02"),
|
|
96
|
+
mode: en(NAV_MODES, "出行方式,默认 drive"),
|
|
97
|
+
map: en(["amap", "baidu"], "地图,默认先高德再百度"),
|
|
98
|
+
page: en(SETTINGS_PAGES, "设置页;app_notifications 还需 app"),
|
|
99
|
+
},
|
|
100
|
+
["kind"],
|
|
101
|
+
),
|
|
102
|
+
tool(
|
|
103
|
+
"notify",
|
|
104
|
+
"给用户发一条手机通知。",
|
|
105
|
+
{ title: str("标题"), text: str("正文") },
|
|
106
|
+
["title", "text"],
|
|
107
|
+
),
|
|
108
|
+
tool(
|
|
109
|
+
"clipboard",
|
|
110
|
+
"读写剪贴板:set 写入文字;get:true 读取(系统只允许前台应用读,XRK 不在前台时读不到)。",
|
|
111
|
+
{ set: str("要写入的文字"), get: bool("读取剪贴板") },
|
|
112
|
+
),
|
|
113
|
+
tool(
|
|
114
|
+
"screen",
|
|
115
|
+
"读当前屏幕:每行一个元素,[编号] 只在本屏有效。detail:true 附坐标,结果可能截断;image:true 请求截图,安全或能力限制时可能不附图(主屏可带编号,后台图不带编号);background:true 读后台屏。元素少或为空不表示页面安全;本机文字识别不可用时按回话提示处理,无法核对目标就 handoff。",
|
|
116
|
+
{
|
|
117
|
+
detail: bool("附上已返回元素的坐标,不保证完整页面"),
|
|
118
|
+
image: bool("请求附带截图;可能因安全或能力限制省略"),
|
|
119
|
+
background: bool("读后台屏(不打扰模式)"),
|
|
120
|
+
},
|
|
121
|
+
),
|
|
122
|
+
tool(
|
|
123
|
+
"collect",
|
|
124
|
+
"只读:自动滚动列表收集全部条目(按文字去重),读长列表只用这一次。",
|
|
125
|
+
{
|
|
126
|
+
screen: SCREEN_ID,
|
|
127
|
+
list: int("列表的编号"),
|
|
128
|
+
max_pages: int("最多翻几页(1–30),默认 10"),
|
|
129
|
+
until: str("出现这段文字就停"),
|
|
130
|
+
},
|
|
131
|
+
["screen", "list"],
|
|
132
|
+
),
|
|
133
|
+
tool(
|
|
134
|
+
"tap",
|
|
135
|
+
"点一个元素,返回新屏幕。敏感按钮或首次操作该 App 会先请用户确认。",
|
|
136
|
+
{ screen: SCREEN_ID, id: int("元素编号"), long: bool("长按") },
|
|
137
|
+
["screen", "id"],
|
|
138
|
+
),
|
|
139
|
+
tool(
|
|
140
|
+
"type",
|
|
141
|
+
"在输入框输入文字(替换原内容),返回回读和新屏幕。submit:true 后按回车 / 发送,要确认。密码、验证码、支付框默认拒绝,被拒绝就 handoff。",
|
|
142
|
+
{
|
|
143
|
+
screen: SCREEN_ID,
|
|
144
|
+
id: int("输入框编号"),
|
|
145
|
+
text: str("要输入的文字"),
|
|
146
|
+
submit: bool("输入后提交 / 发送"),
|
|
147
|
+
},
|
|
148
|
+
["screen", "id", "text"],
|
|
149
|
+
),
|
|
150
|
+
tool(
|
|
151
|
+
"scroll",
|
|
152
|
+
"滚动,返回新屏幕。id 是要滚动的列表,省略时滚主列表。",
|
|
153
|
+
{
|
|
154
|
+
screen: SCREEN_ID,
|
|
155
|
+
id: int("列表编号,可省略"),
|
|
156
|
+
direction: en(DIRECTIONS, "方向"),
|
|
157
|
+
},
|
|
158
|
+
["screen", "direction"],
|
|
159
|
+
),
|
|
160
|
+
tool(
|
|
161
|
+
"key",
|
|
162
|
+
"按系统键(back / home / recents / notifications),返回新屏幕。",
|
|
163
|
+
{ key: en(KEYS, "按键"), screen: str("屏幕号;省略时作用于主屏") },
|
|
164
|
+
["key"],
|
|
165
|
+
),
|
|
166
|
+
tool(
|
|
167
|
+
"tap_xy",
|
|
168
|
+
"按本任务最近一次实际返回截图的像素坐标操作,只在元素里找不到目标时用。action 默认为 tap;long 长按;swipe 从 (x,y) 滑到 (x2,y2)。主屏用 s 号、后台屏用 b 号;截图超过 30 秒或上下文核对失败要重读。每张图只供一次坐标手势;需通过安全检查与所需用户确认,长按和滑动仍逐次确认。结果可报告画面变化,但不表示目标业务成功;结果未知或未变化先读屏,不自动重试。",
|
|
169
|
+
{
|
|
170
|
+
observation: str("本任务最近一次实际返回截图的屏幕号(主屏 s 号或后台屏 b 号)"),
|
|
171
|
+
x: int("横坐标(像素,从 0 开始)"),
|
|
172
|
+
y: int("纵坐标(像素,从 0 开始)"),
|
|
173
|
+
action: en(["tap", "long", "swipe"], "操作类型,省略为 tap;long 为长按,swipe 为滑动"),
|
|
174
|
+
x2: int("仅 swipe:终点横坐标(原截图像素,非负整数)"),
|
|
175
|
+
y2: int("仅 swipe:终点纵坐标(原截图像素,非负整数)"),
|
|
176
|
+
duration_ms: int("仅 swipe:时长 100–2000 毫秒,省略为 400"),
|
|
177
|
+
},
|
|
178
|
+
["observation", "x", "y"],
|
|
179
|
+
),
|
|
180
|
+
tool(
|
|
181
|
+
"handoff",
|
|
182
|
+
"把手机交给用户完成登录、验证码、支付或没把握的步骤;等用户点「继续」或「停止」,最长 10 分钟。",
|
|
183
|
+
{ reason: str("要用户做什么,一句话") },
|
|
184
|
+
["reason"],
|
|
185
|
+
),
|
|
186
|
+
];
|
package/lib/http.mjs
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
/** Reject oversized JSON bodies (default 1 MiB) so a bad client cannot pin RAM. */
|
|
2
|
-
export const DEFAULT_BODY_LIMIT = 1_048_576;
|
|
3
|
-
/**
|
|
4
|
-
* `/result` may carry a phone screenshot as base64 JSON (~4/3× binary).
|
|
5
|
-
* Cap at 12 MiB so a full-screen JPEG/PNG can land without opening the door
|
|
6
|
-
* to unbounded RAM growth on other routes.
|
|
7
|
-
*/
|
|
8
|
-
export const RESULT_BODY_LIMIT = 12 * 1024 * 1024;
|
|
9
|
-
|
|
10
|
-
export function readBody(req, { limit = DEFAULT_BODY_LIMIT } = {}) {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
const chunks = [];
|
|
13
|
-
let size = 0;
|
|
14
|
-
let rejected = false;
|
|
15
|
-
req.on("data", (c) => {
|
|
16
|
-
if (rejected) return;
|
|
17
|
-
size += c.length;
|
|
18
|
-
if (size > limit) {
|
|
19
|
-
rejected = true;
|
|
20
|
-
reject(Object.assign(new Error("body-too-large"), { code: "body-too-large" }));
|
|
21
|
-
req.destroy();
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
chunks.push(c);
|
|
25
|
-
});
|
|
26
|
-
req.on("end", () => {
|
|
27
|
-
if (rejected) return;
|
|
28
|
-
const raw = Buffer.concat(chunks).toString("utf8");
|
|
29
|
-
if (!raw) return resolve(undefined);
|
|
30
|
-
try {
|
|
31
|
-
resolve(JSON.parse(raw));
|
|
32
|
-
} catch (e) {
|
|
33
|
-
reject(e);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
req.on("error", reject);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function sendJson(res, status, payload) {
|
|
41
|
-
const body = JSON.stringify(payload ?? {});
|
|
42
|
-
res.writeHead(status, {
|
|
43
|
-
"content-type": "application/json; charset=utf-8",
|
|
44
|
-
"cache-control": "no-store",
|
|
45
|
-
});
|
|
46
|
-
res.end(body);
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const PREFIX = "/xrk-phone-bridge";
|
|
1
|
+
/** Reject oversized JSON bodies (default 1 MiB) so a bad client cannot pin RAM. */
|
|
2
|
+
export const DEFAULT_BODY_LIMIT = 1_048_576;
|
|
3
|
+
/**
|
|
4
|
+
* `/result` may carry a phone screenshot as base64 JSON (~4/3× binary).
|
|
5
|
+
* Cap at 12 MiB so a full-screen JPEG/PNG can land without opening the door
|
|
6
|
+
* to unbounded RAM growth on other routes.
|
|
7
|
+
*/
|
|
8
|
+
export const RESULT_BODY_LIMIT = 12 * 1024 * 1024;
|
|
9
|
+
|
|
10
|
+
export function readBody(req, { limit = DEFAULT_BODY_LIMIT } = {}) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const chunks = [];
|
|
13
|
+
let size = 0;
|
|
14
|
+
let rejected = false;
|
|
15
|
+
req.on("data", (c) => {
|
|
16
|
+
if (rejected) return;
|
|
17
|
+
size += c.length;
|
|
18
|
+
if (size > limit) {
|
|
19
|
+
rejected = true;
|
|
20
|
+
reject(Object.assign(new Error("body-too-large"), { code: "body-too-large" }));
|
|
21
|
+
req.destroy();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
chunks.push(c);
|
|
25
|
+
});
|
|
26
|
+
req.on("end", () => {
|
|
27
|
+
if (rejected) return;
|
|
28
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
29
|
+
if (!raw) return resolve(undefined);
|
|
30
|
+
try {
|
|
31
|
+
resolve(JSON.parse(raw));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
req.on("error", reject);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function sendJson(res, status, payload) {
|
|
41
|
+
const body = JSON.stringify(payload ?? {});
|
|
42
|
+
res.writeHead(status, {
|
|
43
|
+
"content-type": "application/json; charset=utf-8",
|
|
44
|
+
"cache-control": "no-store",
|
|
45
|
+
});
|
|
46
|
+
res.end(body);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const PREFIX = "/xrk-phone-bridge";
|