-
Notifications
You must be signed in to change notification settings - Fork 35
/
generate-wordlist.ts
176 lines (140 loc) · 4.12 KB
/
generate-wordlist.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { Categories, WordCount } from "./types.ts";
const decoder = new TextDecoder("utf-8");
class OccurenceCounter {
counts: Map<string, WordCount>;
constructor() {
this.counts = new Map<string, WordCount>();
}
inc(key: string, type: keyof typeof Categories) {
if (this.counts.has(key)) {
const WordCount = this.counts.get(key)!;
this.counts.set(key, { ...WordCount, [type]: WordCount[type] + 1 });
} else {
this.counts.set(key, {
overall: 0,
operationType: 0,
operationField: 0,
queryField: 0,
mutationField: 0,
subscriptionField: 0,
queryType: 0,
mutationType: 0,
subscriptionType: 0,
argument: 0,
type: 0,
field: 0,
[type]: 1,
});
}
}
toCsv() {
const lines = [];
for (const [word, count] of this.counts.entries()) {
lines.push([
word,
count.overall,
count.operationType,
count.operationField,
count.queryType,
count.queryField,
count.mutationType,
count.mutationField,
count.subscriptionType,
count.subscriptionField,
count.argument,
count.type,
count.field,
]);
}
const sortBy = 1;
lines.sort((a, b) => (b[sortBy] as number) - (a[sortBy] as number));
const table = [Object.keys(Categories), ...lines];
return table.map((line) => line.join(",")).join("\n");
}
}
const count = new OccurenceCounter();
const parseIntrospection = (introspection: any) => {
const queryTypeName = introspection.data.__schema.queryType?.name;
const mutationTypeName = introspection.data.__schema.mutationType?.name;
const subscriptionTypeName =
introspection.data.__schema?.subscriptionType?.name;
const queryType = introspection.data.__schema.types.find(
(type: any) => type.name === queryTypeName
);
parseOperationType(queryType, "query");
const mutationType = introspection.data.__schema.types.find(
(type: any) => type.name === mutationTypeName
);
parseOperationType(mutationType, "mutation");
const subscriptionType = introspection.data.__schema.types.find(
(type: any) => type.name === subscriptionTypeName
);
parseOperationType(subscriptionType, "subscription");
for (const type of introspection.data.__schema.types) {
if (
type.name === queryTypeName ||
type.name === mutationTypeName ||
type.name === subscriptionTypeName
) {
parseGenericType(type);
}
}
};
const parseOperationType = (
operationType: any,
type: "query" | "mutation" | "subscription"
) => {
if (!operationType) return;
const operationFields = operationType.fields;
const operationTypeName = operationType.name;
count.inc(operationTypeName, "overall");
count.inc(operationTypeName, `${type}Type`);
count.inc(operationTypeName, "operationType");
for (const field of operationFields) {
const fieldName = field.name;
count.inc(fieldName, "overall");
count.inc(fieldName, `${type}Field`);
count.inc(fieldName, "operationField");
parseField(field);
}
};
const parseField = (field: any) => {
const fieldName = field.name;
count.inc(fieldName, "overall");
count.inc(fieldName, "field");
const args = field.args;
for (const arg of args) {
parseArg(arg);
}
};
const parseArg = (args: any) => {
const argName = args.name;
count.inc(argName, "overall");
count.inc(argName, "argument");
};
const parseGenericType = (type: any) => {
const typeName = type.name;
count.inc(typeName, "overall");
count.inc(typeName, "type");
const fields = type.fields;
for (const field of fields) {
parseField(field);
}
};
for await (const wlk of walk("./introspections", {
includeDirs: false,
exts: ["json"],
})) {
console.log(wlk.path);
const raw = Deno.readFileSync(wlk.path);
try {
const introspection = JSON.parse(decoder.decode(raw));
parseIntrospection(introspection);
} catch (e) {
console.error(e);
continue;
}
}
const csv = count.toCsv();
Deno.writeTextFileSync("./wordlists/wordlist.csv", csv);