@searchfe/openclaw-baiduapp 0.1.0-beta.4 → 0.1.0-beta.5

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/dist/index.js CHANGED
@@ -4627,8 +4627,13 @@ function splitMessageByBytes(text, maxBytes = MAX_MESSAGE_BYTES) {
4627
4627
  }
4628
4628
  function jsonOk(res, body) {
4629
4629
  res.statusCode = 200;
4630
- res.setHeader("Content-Type", "text/plain; charset=utf-8");
4631
- res.end(JSON.stringify(body));
4630
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
4631
+ res.end(JSON.stringify({ status: 0, data: body }));
4632
+ }
4633
+ function jsonError(res, message, statusCode = 200) {
4634
+ res.statusCode = statusCode;
4635
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
4636
+ res.end(JSON.stringify({ status: -1, message }));
4632
4637
  }
4633
4638
  async function readRawBody(req, maxBytes) {
4634
4639
  const chunks = [];
@@ -4836,8 +4841,7 @@ async function handleBaiduAppWebhookRequest(req, res) {
4836
4841
  if (req.method === "GET") {
4837
4842
  const echostr = query.get("echostr") ?? "";
4838
4843
  if (!timestamp || !nonce || !signature || !echostr) {
4839
- res.statusCode = 400;
4840
- res.end("missing query params");
4844
+ jsonError(res, "missing query params", 400);
4841
4845
  return true;
4842
4846
  }
4843
4847
  const signatureMatched2 = targets.filter((candidate) => {
@@ -4853,14 +4857,12 @@ async function handleBaiduAppWebhookRequest(req, res) {
4853
4857
  });
4854
4858
  });
4855
4859
  if (signatureMatched2.length === 0) {
4856
- res.statusCode = 401;
4857
- res.end("unauthorized");
4860
+ jsonError(res, "unauthorized");
4858
4861
  return true;
4859
4862
  }
4860
4863
  const decryptable2 = signatureMatched2.filter((candidate) => Boolean(candidate.account.encodingAESKey));
4861
4864
  if (decryptable2.length === 0) {
4862
- res.statusCode = 401;
4863
- res.end("unauthorized");
4865
+ jsonError(res, "unauthorized");
4864
4866
  return true;
4865
4867
  }
4866
4868
  const decryptedCandidates2 = decryptBaiduAppCandidates({
@@ -4868,31 +4870,24 @@ async function handleBaiduAppWebhookRequest(req, res) {
4868
4870
  encrypt: echostr
4869
4871
  });
4870
4872
  if (decryptedCandidates2.length === 0) {
4871
- res.statusCode = 400;
4872
- res.end("decrypt failed");
4873
+ jsonError(res, "decrypt failed");
4873
4874
  return true;
4874
4875
  }
4875
4876
  const selected2 = selectDecryptedTarget({ candidates: decryptedCandidates2, logger });
4876
- res.statusCode = 200;
4877
- res.setHeader("Content-Type", "text/plain; charset=utf-8");
4878
- res.end(selected2.plaintext);
4877
+ jsonOk(res, selected2.plaintext);
4879
4878
  return true;
4880
4879
  }
4881
4880
  if (req.method !== "POST") {
4882
- res.statusCode = 405;
4883
- res.setHeader("Allow", "GET, POST");
4884
- res.end("Method Not Allowed");
4881
+ jsonError(res, "Method Not Allowed", 405);
4885
4882
  return true;
4886
4883
  }
4887
4884
  if (!timestamp || !nonce || !signature) {
4888
- res.statusCode = 400;
4889
- res.end("missing query params");
4885
+ jsonError(res, "missing query params");
4890
4886
  return true;
4891
4887
  }
4892
4888
  const body = await readRawBody(req, 1024 * 1024);
4893
4889
  if (!body.ok || !body.raw) {
4894
- res.statusCode = body.error === "payload too large" ? 413 : 400;
4895
- res.end(body.error ?? "invalid payload");
4890
+ jsonError(res, body.error ?? "invalid payload");
4896
4891
  return true;
4897
4892
  }
4898
4893
  const rawBody = body.raw;
@@ -4914,14 +4909,12 @@ async function handleBaiduAppWebhookRequest(req, res) {
4914
4909
  logger.info(`inbound json parsed: hasEncrypt=${Boolean(encrypt)}`);
4915
4910
  } catch {
4916
4911
  logger.warn(`inbound payload parse failed: not valid xml or json`);
4917
- res.statusCode = 400;
4918
- res.end("invalid payload format");
4912
+ jsonError(res, "invalid payload format");
4919
4913
  return true;
4920
4914
  }
4921
4915
  }
4922
4916
  if (!encrypt) {
4923
- res.statusCode = 400;
4924
- res.end("missing encrypt");
4917
+ jsonError(res, "missing encrypt");
4925
4918
  return true;
4926
4919
  }
4927
4920
  const signatureMatched = targets.filter((candidate) => {
@@ -4938,16 +4931,14 @@ async function handleBaiduAppWebhookRequest(req, res) {
4938
4931
  });
4939
4932
  if (signatureMatched.length === 0) {
4940
4933
  logger.warn(`signature verification failed: checked ${targets.length} account(s), none matched`);
4941
- res.statusCode = 401;
4942
- res.end("unauthorized");
4934
+ jsonError(res, "unauthorized");
4943
4935
  return true;
4944
4936
  }
4945
4937
  logger.debug(`signature verified: ${signatureMatched.length} account(s) matched`);
4946
4938
  const decryptable = signatureMatched.filter((candidate) => Boolean(candidate.account.encodingAESKey));
4947
4939
  if (decryptable.length === 0) {
4948
4940
  logger.warn(`no account has encodingAESKey configured`);
4949
- res.statusCode = 500;
4950
- res.end("baidu-app not configured");
4941
+ jsonError(res, "baidu-app not configured");
4951
4942
  return true;
4952
4943
  }
4953
4944
  const decryptedCandidates = decryptBaiduAppCandidates({
@@ -4956,16 +4947,14 @@ async function handleBaiduAppWebhookRequest(req, res) {
4956
4947
  });
4957
4948
  if (decryptedCandidates.length === 0) {
4958
4949
  logger.warn(`decrypt failed for all ${decryptable.length} candidate account(s)`);
4959
- res.statusCode = 400;
4960
- res.end("decrypt failed");
4950
+ jsonError(res, "decrypt failed");
4961
4951
  return true;
4962
4952
  }
4963
4953
  const selected = selectDecryptedTarget({ candidates: decryptedCandidates, logger });
4964
4954
  const target = selected.target;
4965
4955
  if (!target.account.configured || !target.account.token || !target.account.encodingAESKey) {
4966
4956
  logger.warn(`selected account ${target.account.accountId} not fully configured`);
4967
- res.statusCode = 500;
4968
- res.end("baidu-app not configured");
4957
+ jsonError(res, "baidu-app not configured");
4969
4958
  return true;
4970
4959
  }
4971
4960
  const msg = selected.msg;