@graphql-markdown/telemetry 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/loader.js ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.maskPointer = exports.classifyPointer = void 0;
4
+ const classifyScheme = (p) => {
5
+ if (/^https?:\/\//i.test(p)) {
6
+ return "url";
7
+ }
8
+ if (/^github:/i.test(p)) {
9
+ return "github";
10
+ }
11
+ if (/^git:/i.test(p)) {
12
+ return "git";
13
+ }
14
+ return undefined;
15
+ };
16
+ const isInlineSdl = (p) => {
17
+ return (/\b(type|schema|interface|enum|input|union|scalar|extend|directive)\b[^\n]*[{@]/.test(p) && /\s/.test(p));
18
+ };
19
+ const classifyPath = (p) => {
20
+ if (/[*?{}[\]!]/.test(p)) {
21
+ return "glob";
22
+ }
23
+ if (/\.json$/i.test(p)) {
24
+ return "file-json";
25
+ }
26
+ if (/\.(graphqls?|gql|sdl)$/i.test(p)) {
27
+ return "file-sdl";
28
+ }
29
+ if (/\.(m?[jt]sx?|cjs|cts|vue|svelte|astro)$/i.test(p)) {
30
+ return "code-file";
31
+ }
32
+ return "unknown";
33
+ };
34
+ const classifyOne = (pointer) => {
35
+ if (typeof pointer !== "string") {
36
+ return "unknown";
37
+ }
38
+ const p = pointer.trim();
39
+ return classifyScheme(p) ?? (isInlineSdl(p) ? "inline-sdl" : classifyPath(p));
40
+ };
41
+ const classifyEntry = (pointer) => {
42
+ if (typeof pointer === "object" &&
43
+ pointer !== null &&
44
+ !Array.isArray(pointer)) {
45
+ // Object form `{ "<pointer>": { headers, ... } }`: options are ignored.
46
+ return Object.keys(pointer).map(classifyOne);
47
+ }
48
+ return [classifyOne(pointer)];
49
+ };
50
+ /** Classifies a schema pointer; the pointer itself is never returned. */
51
+ const classifyPointer = (pointer) => {
52
+ if (Array.isArray(pointer)) {
53
+ return pointer.flatMap(classifyEntry);
54
+ }
55
+ const kinds = classifyEntry(pointer);
56
+ return kinds.length === 1 ? kinds[0] : kinds;
57
+ };
58
+ exports.classifyPointer = classifyPointer;
59
+ const MASKS = {
60
+ url: "<url>",
61
+ git: "<git>",
62
+ github: "<github>",
63
+ "file-sdl": "<file:sdl>",
64
+ "file-json": "<file:json>",
65
+ "code-file": "<file:code>",
66
+ glob: "<glob>",
67
+ "inline-sdl": "<inline-sdl>",
68
+ unknown: "<unknown>",
69
+ };
70
+ /** Classifies a schema pointer into mask token(s) for the config slot. */
71
+ const maskPointer = (pointer) => {
72
+ const kinds = (0, exports.classifyPointer)(pointer);
73
+ return Array.isArray(kinds)
74
+ ? kinds.map((k) => {
75
+ return MASKS[k];
76
+ })
77
+ : MASKS[kinds];
78
+ };
79
+ exports.maskPointer = maskPointer;
@@ -0,0 +1,25 @@
1
+ import type { CliOptions, ConfigOptions, TelemetryEvent, TelemetryPayload, TelemetryTrigger } from "@graphql-markdown/types";
2
+ export interface PayloadInput {
3
+ event?: TelemetryEvent;
4
+ trigger: TelemetryTrigger;
5
+ pkg: string;
6
+ pkgVersion: string;
7
+ /** `versionInfo.major` of the loaded `graphql`. */
8
+ graphqlMajor?: unknown;
9
+ config?: ConfigOptions;
10
+ cli?: CliOptions;
11
+ /** Create events only. */
12
+ target?: string;
13
+ mode?: "scaffold" | "wire";
14
+ /** Generate events only. */
15
+ pages?: number;
16
+ durationMs?: number;
17
+ errorType?: string;
18
+ /** True only when no state file existed before this run. */
19
+ noStateBefore?: boolean;
20
+ env?: NodeJS.ProcessEnv;
21
+ }
22
+ export declare const buildPayload: (input: PayloadInput) => TelemetryPayload;
23
+ /** SHA-256 of the canonical sorted-key JSON, excluding `firstRun`. */
24
+ export declare const fingerprint: (payload: TelemetryPayload) => string;
25
+ //# sourceMappingURL=payload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payload.d.ts","sourceRoot":"","sources":["../src/payload.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,UAAU,EACV,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,yBAAyB,CAAC;AAMjC,MAAM,WAAW,YAAY;IAC3B,KAAK,CAAC,EAAE,cAAc,CAAC;IACvB,OAAO,EAAE,gBAAgB,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,mDAAmD;IACnD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,GAAG,CAAC,EAAE,UAAU,CAAC;IACjB,0BAA0B;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,CAAC;IAC3B,4BAA4B;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4DAA4D;IAC5D,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CACzB;AAkED,eAAO,MAAM,YAAY,UAAW,YAAY,KAAG,gBAmDlD,CAAC;AA0BF,sEAAsE;AACtE,eAAO,MAAM,WAAW,YAAa,gBAAgB,KAAG,MAMvD,CAAC"}
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fingerprint = exports.buildPayload = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const node_fs_1 = require("node:fs");
6
+ const ci_1 = require("./ci");
7
+ const loader_1 = require("./loader");
8
+ const sanitize_1 = require("./sanitize");
9
+ const detectPm = (env) => {
10
+ const name = /^(npm|pnpm|yarn|bun)\//.exec(env.npm_config_user_agent ?? "");
11
+ return name?.[1] ?? "unknown";
12
+ };
13
+ const isWsl = (env) => {
14
+ if (env.WSL_DISTRO_NAME) {
15
+ return true;
16
+ }
17
+ try {
18
+ return (0, node_fs_1.readFileSync)("/proc/version", "utf8")
19
+ .toLowerCase()
20
+ .includes("microsoft");
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ };
26
+ const isContainer = (env) => {
27
+ return ((0, node_fs_1.existsSync)("/.dockerenv") ||
28
+ env.CODESPACES !== undefined ||
29
+ env.REMOTE_CONTAINERS !== undefined);
30
+ };
31
+ const applySanitizedSection = (payload, section, source, rules) => {
32
+ const result = (0, sanitize_1.sanitizeConfig)(source, rules);
33
+ if (section === "config") {
34
+ payload.config = result.value;
35
+ }
36
+ else {
37
+ payload.cli = result.value;
38
+ }
39
+ if (payload.loader === undefined && source.schema !== undefined) {
40
+ payload.loader = (0, loader_1.classifyPointer)(source.schema);
41
+ }
42
+ return result.unknownKeys;
43
+ };
44
+ const applyGenerateFields = (payload, input) => {
45
+ const { pages, durationMs, errorType } = input;
46
+ if (typeof pages === "number" && Number.isInteger(pages) && pages >= 0) {
47
+ payload.pages = pages;
48
+ }
49
+ if (typeof durationMs === "number" &&
50
+ Number.isFinite(durationMs) &&
51
+ durationMs >= 0) {
52
+ payload.durationMs = Math.round(durationMs / 100) * 100;
53
+ }
54
+ if (typeof errorType === "string" && errorType !== "") {
55
+ payload.errorType = errorType;
56
+ }
57
+ };
58
+ const buildPayload = (input) => {
59
+ const env = input.env ?? process.env;
60
+ const { ci, ciProvider } = (0, ci_1.detectCi)(env);
61
+ const major = input.graphqlMajor;
62
+ const payload = {
63
+ v: 1,
64
+ event: input.event ?? "config",
65
+ pkg: input.pkg,
66
+ pkgVersion: input.pkgVersion,
67
+ nodeMajor: Number.parseInt(process.versions.node, 10),
68
+ graphqlMajor: typeof major === "number" &&
69
+ Number.isInteger(major) &&
70
+ major >= 0 &&
71
+ major < 100
72
+ ? major
73
+ : "unknown",
74
+ os: process.platform,
75
+ ci,
76
+ pm: detectPm(env),
77
+ firstRun: !ci && input.noStateBefore === true,
78
+ trigger: input.trigger,
79
+ container: isContainer(env),
80
+ wsl: isWsl(env),
81
+ };
82
+ if (ciProvider) {
83
+ payload.ciProvider = ciProvider;
84
+ }
85
+ let unknownKeys = 0;
86
+ if (input.config) {
87
+ unknownKeys += applySanitizedSection(payload, "config", input.config, sanitize_1.CONFIG_RULES);
88
+ }
89
+ if (input.cli) {
90
+ unknownKeys += applySanitizedSection(payload, "cli", input.cli, sanitize_1.CLI_RULES);
91
+ }
92
+ if (unknownKeys > 0) {
93
+ payload.unknownKeys = unknownKeys;
94
+ }
95
+ if (input.target !== undefined) {
96
+ payload.target = input.target;
97
+ }
98
+ if (input.mode !== undefined) {
99
+ payload.mode = input.mode;
100
+ }
101
+ applyGenerateFields(payload, input);
102
+ return payload;
103
+ };
104
+ exports.buildPayload = buildPayload;
105
+ /** Locale-independent code-unit comparison, so fingerprints are deterministic. */
106
+ const compareKeys = (a, b) => {
107
+ if (a === b) {
108
+ return 0;
109
+ }
110
+ return a < b ? -1 : 1;
111
+ };
112
+ const canonical = (value) => {
113
+ if (Array.isArray(value)) {
114
+ return value.map(canonical);
115
+ }
116
+ if (typeof value === "object" && value !== null) {
117
+ return Object.fromEntries(Object.keys(value)
118
+ .sort(compareKeys)
119
+ .map((k) => {
120
+ return [k, canonical(value[k])];
121
+ }));
122
+ }
123
+ return value;
124
+ };
125
+ /** SHA-256 of the canonical sorted-key JSON, excluding `firstRun`. */
126
+ const fingerprint = (payload) => {
127
+ const rest = { ...payload };
128
+ delete rest.firstRun;
129
+ return (0, node_crypto_1.createHash)("sha256")
130
+ .update(JSON.stringify(canonical(rest)))
131
+ .digest("hex");
132
+ };
133
+ exports.fingerprint = fingerprint;
@@ -0,0 +1,229 @@
1
+ export type Rule = {
2
+ kind: "any";
3
+ } | {
4
+ kind: "enum";
5
+ values: readonly string[];
6
+ } | {
7
+ kind: "formatter";
8
+ } | {
9
+ kind: "loaders";
10
+ } | {
11
+ kind: "map";
12
+ value: Rule;
13
+ } | {
14
+ kind: "object";
15
+ fields: Record<string, Rule>;
16
+ } | {
17
+ kind: "opaque";
18
+ } | {
19
+ kind: "path";
20
+ } | {
21
+ kind: "pointer";
22
+ } | {
23
+ kind: "scalar";
24
+ } | {
25
+ kind: "string";
26
+ } | {
27
+ kind: "url";
28
+ };
29
+ export interface SanitizeResult {
30
+ value: Record<string, unknown>;
31
+ unknownKeys: number;
32
+ }
33
+ export declare const CONFIG_RULES: {
34
+ readonly baseURL: {
35
+ kind: "url";
36
+ };
37
+ readonly customDirective: {
38
+ readonly kind: "map";
39
+ readonly value: {
40
+ kind: "any";
41
+ };
42
+ };
43
+ readonly decorators: {
44
+ readonly kind: "map";
45
+ readonly value: {
46
+ kind: "any";
47
+ };
48
+ };
49
+ readonly diffMethod: Rule;
50
+ readonly docOptions: {
51
+ readonly kind: "object";
52
+ readonly fields: {
53
+ readonly frontMatter: {
54
+ readonly kind: "map";
55
+ readonly value: {
56
+ kind: "any";
57
+ };
58
+ };
59
+ readonly index: {
60
+ kind: "scalar";
61
+ };
62
+ readonly generatorFrameworkName: {
63
+ kind: "string";
64
+ };
65
+ readonly generatorFrameworkVersion: {
66
+ kind: "string";
67
+ };
68
+ readonly categorySort: {
69
+ readonly kind: "enum";
70
+ readonly values: readonly ["natural"];
71
+ };
72
+ readonly sectionHeaderId: {
73
+ kind: "scalar";
74
+ };
75
+ };
76
+ };
77
+ readonly force: {
78
+ kind: "scalar";
79
+ };
80
+ readonly groupByDirective: {
81
+ readonly kind: "object";
82
+ readonly fields: {
83
+ readonly directive: {
84
+ kind: "string";
85
+ };
86
+ readonly field: {
87
+ kind: "string";
88
+ };
89
+ readonly fallback: {
90
+ kind: "string";
91
+ };
92
+ };
93
+ };
94
+ readonly homepage: {
95
+ kind: "path";
96
+ };
97
+ readonly id: {
98
+ kind: "string";
99
+ };
100
+ readonly linkRoot: {
101
+ kind: "path";
102
+ };
103
+ readonly loaders: {
104
+ readonly kind: "loaders";
105
+ };
106
+ readonly formatter: {
107
+ kind: "formatter";
108
+ };
109
+ readonly mdxParser: {
110
+ kind: "formatter";
111
+ };
112
+ readonly metatags: {
113
+ readonly kind: "map";
114
+ readonly value: {
115
+ kind: "string";
116
+ };
117
+ };
118
+ readonly onlyDocDirective: {
119
+ kind: "string";
120
+ };
121
+ readonly outputAdapter: {
122
+ readonly kind: "opaque";
123
+ };
124
+ readonly pretty: {
125
+ kind: "scalar";
126
+ };
127
+ readonly printTypeOptions: {
128
+ readonly kind: "object";
129
+ readonly fields: {
130
+ readonly deprecated: Rule;
131
+ readonly exampleSection: {
132
+ readonly kind: "map";
133
+ readonly value: {
134
+ kind: "string";
135
+ };
136
+ };
137
+ readonly hierarchy: Rule;
138
+ readonly parentTypePrefix: {
139
+ kind: "scalar";
140
+ };
141
+ readonly typeBadges: {
142
+ kind: "scalar";
143
+ };
144
+ };
145
+ };
146
+ readonly rootPath: {
147
+ kind: "path";
148
+ };
149
+ readonly schema: {
150
+ kind: "pointer";
151
+ };
152
+ readonly skipDocDirective: {
153
+ kind: "string";
154
+ };
155
+ readonly telemetry: {
156
+ kind: "scalar";
157
+ };
158
+ readonly tmpDir: {
159
+ kind: "path";
160
+ };
161
+ };
162
+ export declare const CLI_RULES: {
163
+ readonly base: {
164
+ kind: "url";
165
+ };
166
+ readonly config: {
167
+ kind: "scalar";
168
+ };
169
+ readonly deprecated: Rule;
170
+ readonly diff: Rule;
171
+ readonly force: {
172
+ kind: "scalar";
173
+ };
174
+ readonly groupByDirective: {
175
+ kind: "string";
176
+ };
177
+ readonly homepage: {
178
+ kind: "path";
179
+ };
180
+ readonly index: {
181
+ kind: "scalar";
182
+ };
183
+ readonly link: {
184
+ kind: "path";
185
+ };
186
+ readonly hierarchy: Rule;
187
+ readonly formatter: {
188
+ kind: "formatter";
189
+ };
190
+ readonly mdxParser: {
191
+ kind: "formatter";
192
+ };
193
+ readonly noParentType: {
194
+ kind: "scalar";
195
+ };
196
+ readonly noTypeBadges: {
197
+ kind: "scalar";
198
+ };
199
+ readonly noSectionId: {
200
+ kind: "scalar";
201
+ };
202
+ readonly pretty: {
203
+ kind: "scalar";
204
+ };
205
+ readonly root: {
206
+ kind: "path";
207
+ };
208
+ readonly schema: {
209
+ kind: "pointer";
210
+ };
211
+ readonly skip: {
212
+ kind: "string";
213
+ };
214
+ readonly only: {
215
+ kind: "string";
216
+ };
217
+ readonly tmp: {
218
+ kind: "path";
219
+ };
220
+ readonly telemetry: {
221
+ kind: "scalar";
222
+ };
223
+ };
224
+ /**
225
+ * Sanitizes a config source. Top-level keys without a rule are dropped and
226
+ * counted in `unknownKeys`.
227
+ */
228
+ export declare const sanitizeConfig: (value: unknown, rules: Record<string, Rule>) => SanitizeResult;
229
+ //# sourceMappingURL=sanitize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":"AAWA,MAAM,MAAM,IAAI,GACZ;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,GACf;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,GAC3C;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,GACrB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,IAAI,CAAA;CAAE,GAC5B;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC;AAEpB,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC,MAAM,E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