{"id":23171701,"url":"https://github.com/smarthypercube/lambda-response-limit","last_synced_at":"2025-04-04T23:42:54.448Z","repository":{"id":242995538,"uuid":"733632138","full_name":"SmartHypercube/lambda-response-limit","owner":"SmartHypercube","description":"AWS Lambda 的 response limit 规则","archived":false,"fork":false,"pushed_at":"2024-06-06T04:30:38.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-10T08:16:25.739Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SmartHypercube.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-19T19:11:39.000Z","updated_at":"2024-06-06T04:30:41.000Z","dependencies_parsed_at":"2024-06-06T06:07:07.779Z","dependency_job_id":null,"html_url":"https://github.com/SmartHypercube/lambda-response-limit","commit_stats":null,"previous_names":["smarthypercube/lambda-response-limit"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmartHypercube%2Flambda-response-limit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmartHypercube%2Flambda-response-limit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmartHypercube%2Flambda-response-limit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SmartHypercube%2Flambda-response-limit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SmartHypercube","download_url":"https://codeload.github.com/SmartHypercube/lambda-response-limit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266479,"owners_count":20910832,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-18T04:19:17.861Z","updated_at":"2025-04-04T23:42:54.431Z","avatar_url":"https://github.com/SmartHypercube.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AWS Lambda 的 response limit 规则\n\nPython:\n\n```python\nimport json\n\nmax_result_length = 6291556\ndef result_length(r):\n    return len(json.dumps(r, ensure_ascii=False).encode())\n```\n\nNode.js:\n\n```js\nconst maxResultLength = 6291556;\nfunction resultLength(r) {\n  return Buffer.byteLength(JSON.stringify(r));\n}\n```\n\n## 验证方法\n\n使用 `serverless deploy` 部署，对于每个函数：\n\n假设得到的 URL 为 `https://example.lambda-url.us-east-1.on.aws/`，则 `curl -X HEAD -v https://example.lambda-url.us-east-1.on.aws/?n=6291556` 预期看到 200 返回码，`curl -X HEAD -v https://example.lambda-url.us-east-1.on.aws/?n=6291557` 预期看到 502 返回码。\n\n## 冷知识\n\n6291556 = 6 * 1024 * 1024 + 100\n\n不仅对于每种语言的 runtime 检查规则不一致，而且这些规则都和真正发回 HTTP response 时编码的规则不一致。在 Python 中返回 `{'s': '/' * 6291567}` 或在 Node.js 中返回 `{s: '/'.repeat(6291568)}` 都能正好用尽最大长度（注意到 Node.js 能多塞进一个 `/` 字符，因为 Node.js 检查长度时，冒号后面没有空格），但收到的 HTTP response 的长度分别是 12583102 和 12583104，远远超过 6MB。真正发回 HTTP response 时，冒号和逗号后面没有空格，但 `/` 字符会不必要地被编码成 `\\/`，这产生了以上结果。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmarthypercube%2Flambda-response-limit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmarthypercube%2Flambda-response-limit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmarthypercube%2Flambda-response-limit/lists"}