Giải mã stack trace bằng source map API
Đổi stack trace JavaScript đã minify thành file, dòng, cột, tên hàm gốc và đoạn code xung quanh bằng source map bạn gửi lên hoặc tìm từ URL.
Dán stack trace từ Chrome / Edge / Node (at fn (file:line:col)), Firefox / Safari (fn@file:line:col) hoặc React Native / Hermes, kèm source map: map (JSON hoặc file JS có sourceMappingURL dạng data:), maps (đối tượng {"app.js.map": …} khi có nhiều bundle), map_url, hoặc gửi file .map / .zip chứa nhiều map. Với fetch=1, dịch vụ tải từng URL script trong stack trace, theo comment //# sourceMappingURL hoặc header SourceMap rồi tải map (chỉ URL công khai, có giới hạn kích thước và redirect).
Mỗi frame có file gốc, dòng, cột, tên hàm gốc (lấy từ mapping tại vị trí gọi ở frame kế tiếp, giống các công cụ theo dõi lỗi), các dòng context từ sourcesContent và cờ library cho mã trong node_modules / ignoreList; resolved_stack là cả stack trace đã viết lại. Hỗ trợ index map (sections) và map của Vite, webpack, esbuild, Rollup, Terser. action=lookup tra một dòng / cột (hoặc chiều ngược lại với source=); action=validate kiểm tra map: version, sources / sourcesContent khớp nhau, mappings giải mã được và không vượt quá file nguồn, ignoreList, debugId, và cảnh báo khi map làm lộ mã nguồn.
Map và mã nguồn chỉ được xử lý trong bộ nhớ, không lưu và không ghi log. Dùng source-map-js (BSD-3-Clause).
Markdown · OpenAPI · Postman · llms.txt
Tham số
| Tên | Vị trí | Kiểu | Bắt buộc | Mặc định | |
|---|---|---|---|---|---|
stack | body | string | không | Stack trace (bắt buộc với action=resolve; tối đa 500 frame). | |
map | body | object | không | Source map (đối tượng hoặc chuỗi JSON, hoặc JS có sourceMappingURL dạng data:). | |
maps | body | object | không | Nhiều map, khoá là tên / URL script hoặc map. | |
file | body | file | không | File .map hoặc .zip chứa các file .map. | |
map_url | body|query | string | không | URL source map cần tải. | |
fetch | body|query | boolean | không | 1: tự tìm map từ URL script trong stack trace (tối đa 6 script). | |
context | body|query | number | không | 3 | Số dòng code quanh mỗi frame (0–10). |
action | body|query | string | không | resolve (mặc định khi có stack), lookup (line, column[, source]) hoặc validate (tuỳ chọn js = code đã build). | |
line | body|query | number | không | lookup: dòng trong code đã build (từ 1), hoặc dòng gốc khi có source. | |
column | body|query | number | không | lookup: cột (từ 1 như stack trace; column_base=0 nếu đếm từ 0). |
Ví dụ
curl -X POST "https://apibox.io.vn/v1/source-map" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"stack":"Error: boom at 3\n at n (https://example.com/assets/out.js:1:47)\n at t (https://example.com/assets/out.js:1:122)\n at u (https://example.com/assets/out.js:1:156)","map":{"version":3,"sources":["lib.ts","app.ts"],"sourcesContent":["export function helper(x: number): number {\n if (x > 2) {\n throw new Error('\''boom at '\'' + x);\n }\n return x * 2;\n}\n","import { helper } from '\''./lib'\'';\n\nfunction computeTotal(items: number[]): number {\n let sum = 0;\n for (const i of items) sum += helper(i);\n return sum;\n}\n\nexport function main() {\n return computeTotal([1, 2, 3]);\n}\nmain();\n"],"mappings":"mBAAO,SAASA,EAAOC,EAAmB,CACxC,GAAIA,EAAI,EACN,MAAM,IAAI,MAAM,WAAaA,CAAC,EAEhC,OAAOA,EAAI,CACb,CCHA,SAASC,EAAaC,EAAyB,CAC7C,IAAIC,EAAM,EACV,QAAWC,KAAKF,EAAOC,GAAOE,EAAOD,CAAC,EACtC,OAAOD,CACT,CAEO,SAASG,GAAO,CACrB,OAAOL,EAAa,CAAC,EAAG,EAAG,CAAC,CAAC,CAC/B,CACAK,EAAK","names":["helper","x","computeTotal","items","sum","i","helper","main"]},"context":1}'const res = await fetch('https://apibox.io.vn/v1/source-map', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"stack": "Error: boom at 3\n at n (https://example.com/assets/out.js:1:47)\n at t (https://example.com/assets/out.js:1:122)\n at u (https://example.com/assets/out.js:1:156)",
"map": {
"version": 3,
"sources": [
"lib.ts",
"app.ts"
],
"sourcesContent": [
"export function helper(x: number): number {\n if (x > 2) {\n throw new Error('boom at ' + x);\n }\n return x * 2;\n}\n",
"import { helper } from './lib';\n\nfunction computeTotal(items: number[]): number {\n let sum = 0;\n for (const i of items) sum += helper(i);\n return sum;\n}\n\nexport function main() {\n return computeTotal([1, 2, 3]);\n}\nmain();\n"
],
"mappings": "mBAAO,SAASA,EAAOC,EAAmB,CACxC,GAAIA,EAAI,EACN,MAAM,IAAI,MAAM,WAAaA,CAAC,EAEhC,OAAOA,EAAI,CACb,CCHA,SAASC,EAAaC,EAAyB,CAC7C,IAAIC,EAAM,EACV,QAAWC,KAAKF,EAAOC,GAAOE,EAAOD,CAAC,EACtC,OAAOD,CACT,CAEO,SAASG,GAAO,CACrB,OAAOL,EAAa,CAAC,EAAG,EAAG,CAAC,CAAC,CAC/B,CACAK,EAAK",
"names": [
"helper",
"x",
"computeTotal",
"items",
"sum",
"i",
"helper",
"main"
]
},
"context": 1
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/source-map",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"stack": "Error: boom at 3\n at n (https://example.com/assets/out.js:1:47)\n at t (https://example.com/assets/out.js:1:122)\n at u (https://example.com/assets/out.js:1:156)", "map": {"version": 3, "sources": ["lib.ts", "app.ts"], "sourcesContent": ["export function helper(x: number): number {\n if (x > 2) {\n throw new Error('boom at ' + x);\n }\n return x * 2;\n}\n", "import { helper } from './lib';\n\nfunction computeTotal(items: number[]): number {\n let sum = 0;\n for (const i of items) sum += helper(i);\n return sum;\n}\n\nexport function main() {\n return computeTotal([1, 2, 3]);\n}\nmain();\n"], "mappings": "mBAAO,SAASA,EAAOC,EAAmB,CACxC,GAAIA,EAAI,EACN,MAAM,IAAI,MAAM,WAAaA,CAAC,EAEhC,OAAOA,EAAI,CACb,CCHA,SAASC,EAAaC,EAAyB,CAC7C,IAAIC,EAAM,EACV,QAAWC,KAAKF,EAAOC,GAAOE,EAAOD,CAAC,EACtC,OAAOD,CACT,CAEO,SAASG,GAAO,CACrB,OAAOL,EAAa,CAAC,EAAG,EAAG,CAAC,CAAC,CAC/B,CACAK,EAAK", "names": ["helper", "x", "computeTotal", "items", "sum", "i", "helper", "main"]}, "context": 1},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/source-map');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"stack":"Error: boom at 3\\n at n (https://example.com/assets/out.js:1:47)\\n at t (https://example.com/assets/out.js:1:122)\\n at u (https://example.com/assets/out.js:1:156)","map":{"version":3,"sources":["lib.ts","app.ts"],"sourcesContent":["export function helper(x: number): number {\\n if (x > 2) {\\n throw new Error(\'boom at \' + x);\\n }\\n return x * 2;\\n}\\n","import { helper } from \'./lib\';\\n\\nfunction computeTotal(items: number[]): number {\\n let sum = 0;\\n for (const i of items) sum += helper(i);\\n return sum;\\n}\\n\\nexport function main() {\\n return computeTotal([1, 2, 3]);\\n}\\nmain();\\n"],"mappings":"mBAAO,SAASA,EAAOC,EAAmB,CACxC,GAAIA,EAAI,EACN,MAAM,IAAI,MAAM,WAAaA,CAAC,EAEhC,OAAOA,EAAI,CACb,CCHA,SAASC,EAAaC,EAAyB,CAC7C,IAAIC,EAAM,EACV,QAAWC,KAAKF,EAAOC,GAAOE,EAAOD,CAAC,EACtC,OAAOD,CACT,CAEO,SAASG,GAAO,CACrB,OAAOL,EAAa,CAAC,EAAG,EAAG,CAAC,CAAC,CAC/B,CACAK,EAAK","names":["helper","x","computeTotal","items","sum","i","helper","main"]},"context":1}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Kết quả trả về
{
"message": "Error: boom at 3",
"frames_total": 3,
"frames_resolved": 3,
"frames": [
{
"raw": "at n (https://example.com/assets/out.js:1:47)",
"function": "n",
"file": "https://example.com/assets/out.js",
"line": 1,
"column": 47,
"original": {
"source": "lib.ts",
"line": 3,
"column": 11,
"function": "helper",
"name": null
},
"library": false,
"context": {
"start_line": 2,
"lines": [
" if (x > 2) {",
" throw new Error('boom at ' + x);",
" }"
],
"highlight": 3
},
"map": "map"
},
{
"raw": "at t (https://example.com/assets/out.js:1:122)",
"function": "t",
"file": "https://example.com/assets/out.js",
"line": 1,
"column": 122,
"original": {
"source": "app.ts",
"line": 5,
"column": 33,
"function": "computeTotal",
"name": "helper"
},
"library": false,
"context": {
"start_line": 4,
"lines": [
" let sum = 0;",
" for (const i of items) sum += helper(i);",
" return sum;"
],
"highlight": 5
},
"map": "map"
},
"…"
],
"resolved_stack": "Error: boom at 3\n at helper (lib.ts:3:11)\n at computeTotal (app.ts:5:33)\n at u (app.ts:10:10)"
}
Ứng dụng
- Đọc lỗi JavaScript production mà không cần trả phí nền tảng theo dõi lỗi
- Cho bot hỗ trợ hoặc luồng n8n đổi log lỗi của khách thành file / dòng thật
- Kiểm tra trong CI rằng source map khớp với bundle và không làm lộ mã nguồn
- Ánh xạ crash Hermes / React Native về code gốc
Hỏi đáp
Source map của tôi có bị lưu không?
Không. Map, script và stack trace chỉ được xử lý trong bộ nhớ cho đúng yêu cầu đó, không lưu và không ghi log.
Vì sao tên hàm lấy từ frame kế tiếp?
Frame đã minify mang tên rút gọn; tên gốc là định danh được map tại vị trí gọi trong frame gọi nó, giống cách Sentry và Chrome DevTools làm.
Nếu map không có sourcesContent thì sao?
Frame vẫn ra được file / dòng / cột gốc nhưng không có đoạn code xung quanh; action=validate sẽ báo điều này.
API liên quan
Tạo UUID
Sinh tối đa 1.000 UUID v4, UUID v7 theo thời gian, ULID, Nano ID hoặc mã hex ngẫu nhiên mỗi request. Ngẫu nhiên an toàn, hỗ trợ chữ in hoa.
⏱Phân tích biểu thức cron
Kiểm tra biểu thức cron và xem các lần chạy tiếp theo theo múi giờ bất kỳ, kèm mô tả dễ hiểu. Hỗ trợ cú pháp cron 5, 6 và 7 trường.
🕒Giờ & múi giờ
Lấy giờ hiện tại ở mọi múi giờ, đổi giữa các múi giờ và Unix timestamp (giây hoặc mili giây), kèm độ lệch UTC và thứ trong tuần cho tối đa 30 múi giờ.