-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
155 lines (142 loc) · 3.75 KB
/
cli.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
// @ts-check
const sade = require("sade");
const { join, resolve } = require("path");
const { readFileSync, existsSync } = require("fs");
const { getSubResources } = require("./index.js");
const { version } = JSON.parse(
readFileSync(join(__dirname, "package.json"), "utf-8"),
);
sade("subres <urlOrFile>", true)
.version(version)
.option("--timeout", "Timeout (in seconds) for navigation", 20)
.option("--wait-until", "Puppeteer waitUntil event", "load")
.option(
"--format",
"Possible output format values: json, json-short, type-url, or url-only",
"type-url",
)
.option(
"--ignore",
"Ignore if same-origin, different-origin or field has a value",
)
.action(async (urlOrFile, opts) => {
try {
await main(urlOrFile, opts);
} catch (error) {
console.error(error);
process.exit(1);
}
})
.parse(process.argv);
async function main(urlOrFile, opts) {
const inputURL = normalizeURL(urlOrFile);
const options = {
waitUntil: opts["wait-until"],
timeout: opts.timeout * 1000,
};
const formatOutput = getFormatter(opts.format);
const ignoreHandlers = getIgnoreHandlers(inputURL, opts.ignore);
for await (const sr of getSubResources(inputURL, options)) {
if (!ignoreHandlers.some(shouldIgnore => shouldIgnore(sr))) {
console.log(formatOutput(sr));
}
}
}
/** @param {string} urlOrFile */
function normalizeURL(urlOrFile) {
try {
return new URL(urlOrFile);
} catch {
if (!existsSync(urlOrFile)) {
throw new Error(`ENOENT (No such file): ${urlOrFile}`);
}
urlOrFile = resolve(urlOrFile).replace(/\\/g, "/");
if (urlOrFile[0] !== "/") {
urlOrFile = "/" + urlOrFile;
}
return new URL(encodeURI("file://" + urlOrFile));
}
}
/**
* @param {"json" | "json-short" | "type-url" | "url-only"} format
* @typedef {{ type: string, url: URL }} SubResource
* @returns {(subresource: SubResource) => string}
*/
function getFormatter(format) {
switch (format) {
case "json":
return sr => JSON.stringify({ type: sr.type, url: urlToPlain(sr.url) });
case "json-short":
return sr => JSON.stringify(sr);
case "type-url":
return sr => `${sr.type}\t${sr.url}`;
case "url-only":
return sr => sr.url.href;
default:
throw new Error(`Invalid value for --format: ${JSON.stringify(format)}`);
}
/** @param {URL} url */
function urlToPlain(url) {
return {
href: url.href,
protocol: url.protocol,
host: url.host,
port: url.port,
origin: url.origin,
pathname: url.pathname,
// @ts-expect-error
searchParams: Object.fromEntries([...url.searchParams.entries()]),
hash: url.hash,
};
}
}
/**
* @param {URL} url entrypoint URL
* @param {string|string[]} options
* @typedef {(sr: SubResource) => boolean} IgnoreHandler
*/
function getIgnoreHandlers(url, options) {
if (!options) {
return [];
}
if (typeof options === "string") {
options = [options];
}
/**
* @param {string} op
* @returns {IgnoreHandler}
*/
function getHandler(op) {
if (op === "same-origin") {
return sr => sr.url.origin === url.origin;
}
if (op === "different-origin") {
return sr => sr.url.origin !== url.origin;
}
const [prop, value] = op.split(":", 2);
const regex = new RegExp(value || ".");
switch (prop) {
case "type":
return sr => regex.test(sr.type);
case "host":
case "pathname":
case "protocol":
case "origin":
case "port":
case "search":
return sr => regex.test(sr.url[prop]);
case "hash":
return sr => regex.test(sr.url.hash.slice(1));
case "param":
case "query": {
const [paramName, paramValue] = value.split("=", 2);
const regex = new RegExp(paramValue || ".");
return sr => regex.test(sr.url.searchParams.get(paramName) || "");
}
default:
throw new Error(`Invalid --ignore filter: ${op}`);
}
}
return options.map(getHandler);
}