-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourcemap
More file actions
executable file
·68 lines (52 loc) · 1.92 KB
/
sourcemap
File metadata and controls
executable file
·68 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import os
import errno
import sys
import requests
import json
from urllib.parse import urlparse
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: ./map2src.py http://url/to/bundle.js.map output_dir")
sys.exit()
url = sys.argv[1]
output_dir = sys.argv[2]
if not os.path.exists(output_dir):
os.mkdir(output_dir)
if not os.path.isdir(output_dir):
print("[-] Error: File `{}` exists, but not a directory".format(output_dir))
sys.exit()
if url.startswith('http://') or url.startswith('https://'):
json_str = requests.get(url, verify=False).text
else:
json_str = open(url).read()
print("[*] Source map loaded.")
map_file = json.loads(json_str)
if len(map_file['sourcesContent']) != len(map_file['sources']):
print("[*] Warning: Length of `sources` and `sourcesContent` doesn't match")
for i, orig_src_path in enumerate(map_file['sources']):
orig_src_path = os.path.abspath(urlparse(orig_src_path).path).lstrip("/")
src_path = os.path.join(output_dir, orig_src_path)
if os.path.exists(src_path):
print(f"[*] {src_path} exists, skipped.")
continue
try:
os.makedirs(os.path.dirname(src_path))
except OSError as err:
if err.errno == errno.EEXIST and os.path.isdir(os.path.dirname(src_path)):
pass
else:
raise err
print("[+] Saveing", src_path)
try:
src = open(src_path, 'w')
src.write(map_file['sourcesContent'][i])
src.close()
except:
print("[-] Failed:", src_path)
map_filename = os.path.join(output_dir, os.path.basename(url))
print("[+]", map_filename)
json.dump(map_file, open(map_filename, "w"))
print("Done.")