@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.
@@ -0,0 +1,260 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanitizeConfig = exports.CLI_RULES = exports.CONFIG_RULES = void 0;
4
+ const loader_1 = require("./loader");
5
+ const schema_1 = require("./schema");
6
+ const MAX_DEPTH = 8;
7
+ const FORMATTER_PACKAGE = /^@graphql-markdown\/[a-z0-9-]+(\/[a-z0-9-]+)*$/;
8
+ const scalar = { kind: "scalar" };
9
+ const string = { kind: "string" };
10
+ const path = { kind: "path" };
11
+ const url = { kind: "url" };
12
+ const any = { kind: "any" };
13
+ const formatter = { kind: "formatter" };
14
+ const pointer = { kind: "pointer" };
15
+ const enumOf = (values) => {
16
+ return { kind: "enum", values };
17
+ };
18
+ const deprecated = enumOf(schema_1.DEPRECATED_OPTIONS);
19
+ exports.CONFIG_RULES = {
20
+ baseURL: url,
21
+ customDirective: { kind: "map", value: any },
22
+ decorators: { kind: "map", value: any },
23
+ diffMethod: enumOf(schema_1.DIFF_METHODS),
24
+ docOptions: {
25
+ kind: "object",
26
+ fields: {
27
+ frontMatter: { kind: "map", value: any },
28
+ index: scalar,
29
+ generatorFrameworkName: string,
30
+ generatorFrameworkVersion: string,
31
+ categorySort: { kind: "enum", values: ["natural"] },
32
+ sectionHeaderId: scalar,
33
+ },
34
+ },
35
+ force: scalar,
36
+ groupByDirective: {
37
+ kind: "object",
38
+ fields: { directive: string, field: string, fallback: string },
39
+ },
40
+ homepage: path,
41
+ id: string,
42
+ linkRoot: path,
43
+ loaders: { kind: "loaders" },
44
+ formatter,
45
+ mdxParser: formatter,
46
+ metatags: { kind: "map", value: string },
47
+ onlyDocDirective: string,
48
+ outputAdapter: { kind: "opaque" },
49
+ pretty: scalar,
50
+ printTypeOptions: {
51
+ kind: "object",
52
+ fields: {
53
+ deprecated,
54
+ exampleSection: { kind: "map", value: string },
55
+ hierarchy: enumOf(schema_1.HIERARCHIES),
56
+ parentTypePrefix: scalar,
57
+ typeBadges: scalar,
58
+ },
59
+ },
60
+ rootPath: path,
61
+ schema: pointer,
62
+ skipDocDirective: string,
63
+ telemetry: scalar,
64
+ tmpDir: path,
65
+ };
66
+ exports.CLI_RULES = {
67
+ base: url,
68
+ config: scalar,
69
+ deprecated,
70
+ diff: enumOf(schema_1.DIFF_METHODS),
71
+ force: scalar,
72
+ groupByDirective: string,
73
+ homepage: path,
74
+ index: scalar,
75
+ link: path,
76
+ hierarchy: enumOf(schema_1.HIERARCHIES),
77
+ formatter,
78
+ mdxParser: formatter,
79
+ noParentType: scalar,
80
+ noTypeBadges: scalar,
81
+ noSectionId: scalar,
82
+ pretty: scalar,
83
+ root: path,
84
+ schema: pointer,
85
+ skip: string,
86
+ only: string,
87
+ tmp: path,
88
+ telemetry: scalar,
89
+ };
90
+ /** Masks a string by its shape: url, path or generic string. */
91
+ const maskString = (value) => {
92
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value)) {
93
+ return "<url>";
94
+ }
95
+ if (/[/\\]/.test(value) || value.startsWith(".")) {
96
+ return "<path>";
97
+ }
98
+ return "<string>";
99
+ };
100
+ const isPlainObject = (value) => {
101
+ const proto = Object.getPrototypeOf(value);
102
+ return proto === Object.prototype || proto === null;
103
+ };
104
+ const sanitizeFormatter = (value) => {
105
+ if (schema_1.FORMATTER_PRESETS.includes(value) ||
106
+ FORMATTER_PACKAGE.test(value)) {
107
+ return value;
108
+ }
109
+ return "<custom-formatter>";
110
+ };
111
+ const sanitizeLoaderName = (value) => {
112
+ return schema_1.KNOWN_LOADERS.includes(value)
113
+ ? value
114
+ : "<custom-loader>";
115
+ };
116
+ /* eslint-disable @typescript-eslint/no-use-before-define -- mutual recursion */
117
+ const sanitizeLoaderEntry = (item, depth) => {
118
+ if (typeof item === "object" && item !== null && isPlainObject(item)) {
119
+ const cfg = item;
120
+ return {
121
+ ...(cfg.module !== undefined && {
122
+ module: sanitizeLoaders(cfg.module, depth + 2),
123
+ }),
124
+ ...(cfg.options !== undefined && {
125
+ options: sanitizeValue(cfg.options, { kind: "map", value: any }, depth + 2),
126
+ }),
127
+ };
128
+ }
129
+ return sanitizeLoaders(item, depth + 1);
130
+ };
131
+ const sanitizeLoaders = (value, depth) => {
132
+ if (typeof value === "string") {
133
+ return sanitizeLoaderName(value);
134
+ }
135
+ if (Array.isArray(value)) {
136
+ return value.map((item) => {
137
+ return sanitizeLoaders(item, depth + 1);
138
+ });
139
+ }
140
+ if (typeof value === "object" && value !== null && isPlainObject(value)) {
141
+ const out = {};
142
+ for (const [key, item] of Object.entries(value)) {
143
+ out[sanitizeLoaderName(key)] = sanitizeLoaderEntry(item, depth);
144
+ }
145
+ return out;
146
+ }
147
+ return sanitizeValue(value, any, depth);
148
+ };
149
+ const sanitizeStringByRule = (value, rule) => {
150
+ switch (rule.kind) {
151
+ case "enum":
152
+ return rule.values.includes(value) ? value : maskString(value);
153
+ case "url":
154
+ return "<url>";
155
+ case "path":
156
+ return "<path>";
157
+ case "formatter":
158
+ return sanitizeFormatter(value);
159
+ default:
160
+ return rule.kind === "string" ? "<string>" : maskString(value);
161
+ }
162
+ };
163
+ const sanitizeObjectByRule = (value, rule, depth) => {
164
+ if (!isPlainObject(value)) {
165
+ return "<object>";
166
+ }
167
+ const entries = Object.entries(value);
168
+ const out = {};
169
+ let n = 0;
170
+ for (const [key, item] of entries) {
171
+ if (rule.kind === "object" && Object.hasOwn(rule.fields, key)) {
172
+ const next = sanitizeValue(item, rule.fields[key], depth + 1);
173
+ if (next !== undefined) {
174
+ out[key] = next;
175
+ }
176
+ continue;
177
+ }
178
+ const itemRule = rule.kind === "map" ? rule.value : any;
179
+ const next = sanitizeValue(item, itemRule, depth + 1);
180
+ if (next !== undefined) {
181
+ out[`<key:${n}>`] = next;
182
+ n += 1;
183
+ }
184
+ }
185
+ return out;
186
+ };
187
+ const sanitizePrimitive = (value) => {
188
+ if (value === null || typeof value === "boolean") {
189
+ return { handled: true, value };
190
+ }
191
+ if (typeof value === "number") {
192
+ return { handled: true, value: Number.isFinite(value) ? value : null };
193
+ }
194
+ if (typeof value === "function") {
195
+ return { handled: true, value: "<function>" };
196
+ }
197
+ if (value === undefined) {
198
+ return { handled: true, value: undefined };
199
+ }
200
+ if (typeof value === "symbol" || typeof value === "bigint") {
201
+ return { handled: true, value: "<object>" };
202
+ }
203
+ return { handled: false };
204
+ };
205
+ const sanitizeValue = (value, rule, depth) => {
206
+ const primitive = sanitizePrimitive(value);
207
+ if (primitive.handled) {
208
+ return primitive.value;
209
+ }
210
+ if (depth > MAX_DEPTH || rule.kind === "opaque") {
211
+ return typeof value === "string" ? "<string>" : "<object>";
212
+ }
213
+ return sanitizeByRule(value, rule, depth);
214
+ };
215
+ const sanitizeByRule = (value, rule, depth) => {
216
+ if (rule.kind === "pointer") {
217
+ return (0, loader_1.maskPointer)(value);
218
+ }
219
+ if (rule.kind === "loaders") {
220
+ return sanitizeLoaders(value, depth);
221
+ }
222
+ if (typeof value === "string") {
223
+ return sanitizeStringByRule(value, rule);
224
+ }
225
+ return sanitizeCollection(value, rule, depth);
226
+ };
227
+ const sanitizeCollection = (value, rule, depth) => {
228
+ if (Array.isArray(value)) {
229
+ return value.map((item) => {
230
+ return sanitizeValue(item, rule, depth + 1);
231
+ });
232
+ }
233
+ if (typeof value === "object" && value !== null) {
234
+ return sanitizeObjectByRule(value, rule, depth);
235
+ }
236
+ return "<object>";
237
+ };
238
+ /* eslint-enable @typescript-eslint/no-use-before-define */
239
+ /**
240
+ * Sanitizes a config source. Top-level keys without a rule are dropped and
241
+ * counted in `unknownKeys`.
242
+ */
243
+ const sanitizeConfig = (value, rules) => {
244
+ const result = { value: {}, unknownKeys: 0 };
245
+ if (typeof value !== "object" || value === null) {
246
+ return result;
247
+ }
248
+ for (const [key, item] of Object.entries(value)) {
249
+ if (!Object.hasOwn(rules, key)) {
250
+ result.unknownKeys += 1;
251
+ continue;
252
+ }
253
+ const next = sanitizeValue(item, rules[key], 0);
254
+ if (next !== undefined) {
255
+ result.value[key] = next;
256
+ }
257
+ }
258
+ return result;
259
+ };
260
+ exports.sanitizeConfig = sanitizeConfig;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Allowlist of every string a payload may carry, as plain data: enums plus
3
+ * mask tokens. Shared with the Worker, which rejects anything else.
4
+ */
5
+ export declare const EVENTS: readonly ["config", "create_scaffold", "create_wire"];
6
+ export declare const TRIGGERS: readonly ["cli", "create", "docusaurus", "nuxt", "programmatic"];
7
+ export declare const CI_PROVIDERS: readonly ["github-actions", "gitlab", "circleci", "azure-pipelines", "jenkins", "bitbucket", "buildkite", "travis", "vercel", "netlify", "cloudflare-pages", "aws-codebuild", "teamcity", "appveyor", "drone", "render", "other"];
8
+ export declare const PACKAGE_MANAGERS: readonly ["npm", "pnpm", "yarn", "bun", "unknown"];
9
+ export declare const LOADER_KINDS: readonly ["url", "file-sdl", "file-json", "git", "github", "glob", "code-file", "inline-sdl", "unknown"];
10
+ export declare const MODES: readonly ["scaffold", "wire"];
11
+ export declare const DIFF_METHODS: readonly ["FORCE", "NONE"];
12
+ export declare const DEPRECATED_OPTIONS: readonly ["default", "group", "skip"];
13
+ export declare const HIERARCHIES: readonly ["api", "entity", "flat"];
14
+ export declare const FORMATTER_PRESETS: readonly ["docfx", "docusaurus", "fumadocs", "honkit", "hugo", "mdbook", "mdx", "mkdocs", "starlight", "vocs"];
15
+ export declare const KNOWN_LOADERS: readonly ["UrlLoader", "JsonFileLoader", "GraphQLFileLoader", "CodeFileLoader", "GitLoader", "GithubLoader", "ApolloEngineLoader", "PrismaLoader", "@graphql-tools/url-loader", "@graphql-tools/json-file-loader", "@graphql-tools/graphql-file-loader", "@graphql-tools/code-file-loader", "@graphql-tools/git-loader", "@graphql-tools/github-loader", "@graphql-tools/apollo-engine-loader", "@graphql-tools/prisma-loader"];
16
+ export declare const MASK_TOKENS: readonly ["<url>", "<path>", "<string>", "<function>", "<object>", "<custom-loader>", "<custom-formatter>", "<git>", "<github>", "<file:sdl>", "<file:json>", "<file:code>", "<glob>", "<inline-sdl>", "<unknown>"];
17
+ /** Pattern of masked object keys, e.g. `<key:0>`. */
18
+ export declare const KEY_TOKEN_PATTERN: string;
19
+ export declare const ALLOWLIST: {
20
+ readonly events: readonly ["config", "create_scaffold", "create_wire"];
21
+ readonly triggers: readonly ["cli", "create", "docusaurus", "nuxt", "programmatic"];
22
+ readonly ciProviders: readonly ["github-actions", "gitlab", "circleci", "azure-pipelines", "jenkins", "bitbucket", "buildkite", "travis", "vercel", "netlify", "cloudflare-pages", "aws-codebuild", "teamcity", "appveyor", "drone", "render", "other"];
23
+ readonly packageManagers: readonly ["npm", "pnpm", "yarn", "bun", "unknown"];
24
+ readonly loaderKinds: readonly ["url", "file-sdl", "file-json", "git", "github", "glob", "code-file", "inline-sdl", "unknown"];
25
+ readonly modes: readonly ["scaffold", "wire"];
26
+ readonly diffMethods: readonly ["FORCE", "NONE"];
27
+ readonly deprecatedOptions: readonly ["default", "group", "skip"];
28
+ readonly hierarchies: readonly ["api", "entity", "flat"];
29
+ readonly formatterPresets: readonly ["docfx", "docusaurus", "fumadocs", "honkit", "hugo", "mdbook", "mdx", "mkdocs", "starlight", "vocs"];
30
+ readonly formatterPackagePattern: "^@graphql-markdown/[a-z0-9-]+(/[a-z0-9-]+)*$";
31
+ readonly knownLoaders: readonly ["UrlLoader", "JsonFileLoader", "GraphQLFileLoader", "CodeFileLoader", "GitLoader", "GithubLoader", "ApolloEngineLoader", "PrismaLoader", "@graphql-tools/url-loader", "@graphql-tools/json-file-loader", "@graphql-tools/graphql-file-loader", "@graphql-tools/code-file-loader", "@graphql-tools/git-loader", "@graphql-tools/github-loader", "@graphql-tools/apollo-engine-loader", "@graphql-tools/prisma-loader"];
32
+ readonly maskTokens: readonly ["<url>", "<path>", "<string>", "<function>", "<object>", "<custom-loader>", "<custom-formatter>", "<git>", "<github>", "<file:sdl>", "<file:json>", "<file:code>", "<glob>", "<inline-sdl>", "<unknown>"];
33
+ readonly keyTokenPattern: string;
34
+ readonly graphqlMajorUnknown: "unknown";
35
+ };
36
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,MAAM,YAAI,QAAQ,EAAE,iBAAiB,EAAE,aAAa,CAAU,CAAC;AAC5E,eAAO,MAAM,QAAQ,YACnB,KAAK,EACL,QAAQ,EACR,YAAY,EACZ,MAAM,EACN,cAAc,CACN,CAAC;AACX,eAAO,MAAM,YAAY,YACvB,gBAAgB,EAChB,QAAQ,EACR,UAAU,EACV,iBAAiB,EACjB,SAAS,EACT,WAAW,EACX,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,kBAAkB,EAClB,eAAe,EACf,UAAU,EACV,UAAU,EACV,OAAO,EACP,QAAQ,EACR,OAAO,CACC,CAAC;AACX,eAAO,MAAM,gBAAgB,YAC3B,KAAK,EACL,MAAM,EACN,MAAM,EACN,KAAK,EACL,SAAS,CACD,CAAC;AACX,eAAO,MAAM,YAAY,YACvB,KAAK,EACL,UAAU,EACV,WAAW,EACX,KAAK,EACL,QAAQ,EACR,MAAM,EACN,WAAW,EACX,YAAY,EACZ,SAAS,CACD,CAAC;AACX,eAAO,MAAM,KAAK,YAAI,UAAU,EAAE,MAAM,CAAU,CAAC;AACnD,eAAO,MAAM,YAAY,YAAI,OAAO,EAAE,MAAM,CAAU,CAAC;AACvD,eAAO,MAAM,kBAAkB,YAAI,SAAS,EAAE,OAAO,EAAE,MAAM,CAAU,CAAC;AACxE,eAAO,MAAM,WAAW,YAAI,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAU,CAAC;AAC9D,eAAO,MAAM,iBAAiB,YAC5B,OAAO,EACP,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,WAAW,EACX,MAAM,CACE,CAAC;AACX,eAAO,MAAM,aAAa,YACxB,WAAW,EACX,gBAAgB,EAChB,mBAAmB,EACnB,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,oBAAoB,EACpB,cAAc,EACd,2BAA2B,EAC3B,iCAAiC,EACjC,oCAAoC,EACpC,iCAAiC,EACjC,2BAA2B,EAC3B,8BAA8B,EAC9B,qCAAqC,EACrC,8BAA8B,CACtB,CAAC;AACX,eAAO,MAAM,WAAW,YACtB,OAAO,EACP,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,UAAU,EACV,iBAAiB,EACjB,oBAAoB,EACpB,OAAO,EACP,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,QAAQ,EACR,cAAc,EACd,WAAW,CACH,CAAC;AACX,qDAAqD;AACrD,eAAO,MAAM,iBAAiB,QAA0B,CAAC;AAEzD,eAAO,MAAM,SAAS;aACpB,MAAM;aACN,QAAQ;aACR,WAAW;aACX,eAAe;aACf,WAAW;aACX,KAAK;aACL,WAAW;aACX,iBAAiB;aACjB,WAAW;aACX,gBAAgB;aAChB,uBAAuB,EAAE,8CAA8C;aACvE,YAAY;aACZ,UAAU;aACV,eAAe;aACf,mBAAmB,EAAE,SAAS;CACtB,CAAC"}
package/dist/schema.js ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ALLOWLIST = exports.KEY_TOKEN_PATTERN = exports.MASK_TOKENS = exports.KNOWN_LOADERS = exports.FORMATTER_PRESETS = exports.HIERARCHIES = exports.DEPRECATED_OPTIONS = exports.DIFF_METHODS = exports.MODES = exports.LOADER_KINDS = exports.PACKAGE_MANAGERS = exports.CI_PROVIDERS = exports.TRIGGERS = exports.EVENTS = void 0;
4
+ /**
5
+ * Allowlist of every string a payload may carry, as plain data: enums plus
6
+ * mask tokens. Shared with the Worker, which rejects anything else.
7
+ */
8
+ exports.EVENTS = ["config", "create_scaffold", "create_wire"];
9
+ exports.TRIGGERS = [
10
+ "cli",
11
+ "create",
12
+ "docusaurus",
13
+ "nuxt",
14
+ "programmatic",
15
+ ];
16
+ exports.CI_PROVIDERS = [
17
+ "github-actions",
18
+ "gitlab",
19
+ "circleci",
20
+ "azure-pipelines",
21
+ "jenkins",
22
+ "bitbucket",
23
+ "buildkite",
24
+ "travis",
25
+ "vercel",
26
+ "netlify",
27
+ "cloudflare-pages",
28
+ "aws-codebuild",
29
+ "teamcity",
30
+ "appveyor",
31
+ "drone",
32
+ "render",
33
+ "other",
34
+ ];
35
+ exports.PACKAGE_MANAGERS = [
36
+ "npm",
37
+ "pnpm",
38
+ "yarn",
39
+ "bun",
40
+ "unknown",
41
+ ];
42
+ exports.LOADER_KINDS = [
43
+ "url",
44
+ "file-sdl",
45
+ "file-json",
46
+ "git",
47
+ "github",
48
+ "glob",
49
+ "code-file",
50
+ "inline-sdl",
51
+ "unknown",
52
+ ];
53
+ exports.MODES = ["scaffold", "wire"];
54
+ exports.DIFF_METHODS = ["FORCE", "NONE"];
55
+ exports.DEPRECATED_OPTIONS = ["default", "group", "skip"];
56
+ exports.HIERARCHIES = ["api", "entity", "flat"];
57
+ exports.FORMATTER_PRESETS = [
58
+ "docfx",
59
+ "docusaurus",
60
+ "fumadocs",
61
+ "honkit",
62
+ "hugo",
63
+ "mdbook",
64
+ "mdx",
65
+ "mkdocs",
66
+ "starlight",
67
+ "vocs",
68
+ ];
69
+ exports.KNOWN_LOADERS = [
70
+ "UrlLoader",
71
+ "JsonFileLoader",
72
+ "GraphQLFileLoader",
73
+ "CodeFileLoader",
74
+ "GitLoader",
75
+ "GithubLoader",
76
+ "ApolloEngineLoader",
77
+ "PrismaLoader",
78
+ "@graphql-tools/url-loader",
79
+ "@graphql-tools/json-file-loader",
80
+ "@graphql-tools/graphql-file-loader",
81
+ "@graphql-tools/code-file-loader",
82
+ "@graphql-tools/git-loader",
83
+ "@graphql-tools/github-loader",
84
+ "@graphql-tools/apollo-engine-loader",
85
+ "@graphql-tools/prisma-loader",
86
+ ];
87
+ exports.MASK_TOKENS = [
88
+ "<url>",
89
+ "<path>",
90
+ "<string>",
91
+ "<function>",
92
+ "<object>",
93
+ "<custom-loader>",
94
+ "<custom-formatter>",
95
+ "<git>",
96
+ "<github>",
97
+ "<file:sdl>",
98
+ "<file:json>",
99
+ "<file:code>",
100
+ "<glob>",
101
+ "<inline-sdl>",
102
+ "<unknown>",
103
+ ];
104
+ /** Pattern of masked object keys, e.g. `<key:0>`. */
105
+ exports.KEY_TOKEN_PATTERN = String.raw `^<key:\d+>$`;
106
+ exports.ALLOWLIST = {
107
+ events: exports.EVENTS,
108
+ triggers: exports.TRIGGERS,
109
+ ciProviders: exports.CI_PROVIDERS,
110
+ packageManagers: exports.PACKAGE_MANAGERS,
111
+ loaderKinds: exports.LOADER_KINDS,
112
+ modes: exports.MODES,
113
+ diffMethods: exports.DIFF_METHODS,
114
+ deprecatedOptions: exports.DEPRECATED_OPTIONS,
115
+ hierarchies: exports.HIERARCHIES,
116
+ formatterPresets: exports.FORMATTER_PRESETS,
117
+ formatterPackagePattern: "^@graphql-markdown/[a-z0-9-]+(/[a-z0-9-]+)*$",
118
+ knownLoaders: exports.KNOWN_LOADERS,
119
+ maskTokens: exports.MASK_TOKENS,
120
+ keyTokenPattern: exports.KEY_TOKEN_PATTERN,
121
+ graphqlMajorUnknown: "unknown",
122
+ };
@@ -0,0 +1,22 @@
1
+ export interface TelemetryState {
2
+ enabled: boolean;
3
+ noticeVersion: number;
4
+ lastSentConfigs: string[];
5
+ reason?: string;
6
+ }
7
+ export declare const getStatePath: (dir?: string) => string;
8
+ /**
9
+ * Reads the state file.
10
+ *
11
+ * @returns the state, `undefined` when the file does not exist, or `null` when
12
+ * it is unreadable or corrupt (telemetry is then disabled for this run).
13
+ */
14
+ export declare const readState: (path?: string) => TelemetryState | null | undefined;
15
+ /**
16
+ * Atomically writes the state (temp file + rename). Re-reads the current file
17
+ * and merges `lastSentConfigs` first so parallel runs do not lose entries.
18
+ *
19
+ * @returns the state that was written.
20
+ */
21
+ export declare const writeState: (next: Partial<TelemetryState>, path?: string) => TelemetryState;
22
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAaA,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,YAAY,SAAS,MAAM,KAAwB,MAE/D,CAAC;AAkBF;;;;;GAKG;AACH,eAAO,MAAM,SAAS,UACd,MAAM,KACX,cAAc,GAAG,IAAI,GAAG,SAU1B,CAAC;AAIF;;;;;GAKG;AACH,eAAO,MAAM,UAAU,SACf,OAAO,CAAC,cAAc,CAAC,SACvB,MAAM,KACX,cAwBF,CAAC"}
package/dist/state.js ADDED
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeState = exports.readState = exports.getStatePath = void 0;
4
+ const node_fs_1 = require("node:fs");
5
+ const node_path_1 = require("node:path");
6
+ const config_dir_1 = require("./config-dir");
7
+ const constants_1 = require("./constants");
8
+ const getStatePath = (dir = (0, config_dir_1.resolveConfigDir)()) => {
9
+ return (0, node_path_1.join)(dir, "telemetry.json");
10
+ };
11
+ exports.getStatePath = getStatePath;
12
+ const isState = (value) => {
13
+ if (typeof value !== "object" || value === null) {
14
+ return false;
15
+ }
16
+ const v = value;
17
+ return (typeof v.enabled === "boolean" &&
18
+ typeof v.noticeVersion === "number" &&
19
+ Array.isArray(v.lastSentConfigs) &&
20
+ v.lastSentConfigs.every((s) => {
21
+ return typeof s === "string";
22
+ }) &&
23
+ (v.reason === undefined || typeof v.reason === "string"));
24
+ };
25
+ /**
26
+ * Reads the state file.
27
+ *
28
+ * @returns the state, `undefined` when the file does not exist, or `null` when
29
+ * it is unreadable or corrupt (telemetry is then disabled for this run).
30
+ */
31
+ const readState = (path = (0, exports.getStatePath)()) => {
32
+ if (!(0, node_fs_1.existsSync)(path)) {
33
+ return undefined;
34
+ }
35
+ try {
36
+ const parsed = JSON.parse((0, node_fs_1.readFileSync)(path, "utf8"));
37
+ return isState(parsed) ? parsed : null;
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ };
43
+ exports.readState = readState;
44
+ let counter = 0;
45
+ /**
46
+ * Atomically writes the state (temp file + rename). Re-reads the current file
47
+ * and merges `lastSentConfigs` first so parallel runs do not lose entries.
48
+ *
49
+ * @returns the state that was written.
50
+ */
51
+ const writeState = (next, path = (0, exports.getStatePath)()) => {
52
+ const current = (0, exports.readState)(path);
53
+ const base = current ?? {
54
+ enabled: true,
55
+ noticeVersion: 0,
56
+ lastSentConfigs: [],
57
+ };
58
+ const merged = [
59
+ ...new Set([...base.lastSentConfigs, ...(next.lastSentConfigs ?? [])]),
60
+ ].slice(-constants_1.MAX_SENT_CONFIGS);
61
+ const state = { ...base, ...next, lastSentConfigs: merged };
62
+ if (state.reason === undefined) {
63
+ delete state.reason;
64
+ }
65
+ (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(path), { recursive: true });
66
+ const tmp = `${path}.${process.pid}.${counter++}.tmp`;
67
+ try {
68
+ (0, node_fs_1.writeFileSync)(tmp, JSON.stringify(state, null, 2) + "\n");
69
+ (0, node_fs_1.renameSync)(tmp, path);
70
+ }
71
+ catch (error) {
72
+ (0, node_fs_1.rmSync)(tmp, { force: true });
73
+ throw error;
74
+ }
75
+ return state;
76
+ };
77
+ exports.writeState = writeState;
@@ -0,0 +1,19 @@
1
+ import type { PayloadInput } from "./payload";
2
+ export interface TrackInput extends Omit<PayloadInput, "noStateBefore"> {
3
+ /** Value of the `telemetry` option. */
4
+ option?: boolean;
5
+ /** Override of the `telemetry.json` location (tests). */
6
+ statePath?: string;
7
+ /** Receives the full notice instead of stderr (e.g. a prompt UI). */
8
+ onFullNotice?: (notice: string) => void;
9
+ }
10
+ /** Builds the request body, gzipped above the size threshold. */
11
+ export declare const encodeBody: (json: string) => {
12
+ body: BodyInit;
13
+ headers: Record<string, string>;
14
+ };
15
+ /**
16
+ * Sends one anonymous config event. Never throws and never rejects.
17
+ */
18
+ export declare const track: (input: TrackInput) => Promise<void>;
19
+ //# sourceMappingURL=track.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"track.d.ts","sourceRoot":"","sources":["../src/track.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAG9C,MAAM,WAAW,UAAW,SAAQ,IAAI,CAAC,YAAY,EAAE,eAAe,CAAC;IACrE,uCAAuC;IACvC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAED,iEAAiE;AACjE,eAAO,MAAM,UAAU,SACf,MAAM,KACX;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CASnD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,KAAK,UAAiB,UAAU,KAAG,OAAO,CAAC,IAAI,CA2D3D,CAAC"}
package/dist/track.js ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.track = exports.encodeBody = void 0;
4
+ const node_zlib_1 = require("node:zlib");
5
+ const ci_1 = require("./ci");
6
+ const constants_1 = require("./constants");
7
+ const disclaimer_1 = require("./disclaimer");
8
+ const enabled_1 = require("./enabled");
9
+ const payload_1 = require("./payload");
10
+ const state_1 = require("./state");
11
+ /** Builds the request body, gzipped above the size threshold. */
12
+ const encodeBody = (json) => {
13
+ const headers = {
14
+ "Content-Type": "application/json",
15
+ };
16
+ if (Buffer.byteLength(json) > constants_1.GZIP_THRESHOLD) {
17
+ headers["Content-Encoding"] = "gzip";
18
+ return { body: (0, node_zlib_1.gzipSync)(json), headers };
19
+ }
20
+ return { body: json, headers };
21
+ };
22
+ exports.encodeBody = encodeBody;
23
+ /**
24
+ * Sends one anonymous config event. Never throws and never rejects.
25
+ */
26
+ const track = async (input) => {
27
+ try {
28
+ const env = input.env ?? process.env;
29
+ const statePath = input.statePath ?? (0, state_1.getStatePath)();
30
+ const state = (0, state_1.readState)(statePath);
31
+ const decision = (0, enabled_1.isEnabled)({
32
+ trigger: input.trigger,
33
+ option: input.option,
34
+ state,
35
+ env,
36
+ });
37
+ if (!decision.enabled) {
38
+ return;
39
+ }
40
+ const ci = (0, ci_1.detectCi)(env);
41
+ if ((0, disclaimer_1.printDisclaimer)(state, ci, input.onFullNotice)) {
42
+ // Nothing is sent when the notice cannot be recorded.
43
+ (0, state_1.writeState)({ enabled: true, noticeVersion: constants_1.NOTICE_VERSION }, statePath);
44
+ }
45
+ const payload = (0, payload_1.buildPayload)({
46
+ ...input,
47
+ env,
48
+ trigger: decision.trigger,
49
+ noStateBefore: state === undefined,
50
+ });
51
+ const json = JSON.stringify(payload);
52
+ const debug = env.GRAPHQL_MARKDOWN_TELEMETRY_DEBUG;
53
+ if (debug && debug !== "0" && debug !== "false") {
54
+ process.stderr.write(`${JSON.stringify(payload, null, 2)}\n`);
55
+ return;
56
+ }
57
+ // Generate events carry a duration, so their fingerprint never repeats.
58
+ const dedupe = payload.event !== "generate";
59
+ const fp = (0, payload_1.fingerprint)(payload);
60
+ if (dedupe && state?.lastSentConfigs.includes(fp)) {
61
+ return;
62
+ }
63
+ const { body, headers } = (0, exports.encodeBody)(json);
64
+ const response = await fetch((0, enabled_1.getEndpoint)(env), {
65
+ method: "POST",
66
+ headers,
67
+ body,
68
+ signal: AbortSignal.timeout(constants_1.TIMEOUT_MS),
69
+ });
70
+ if (response.status === 202) {
71
+ if (dedupe) {
72
+ (0, state_1.writeState)({ lastSentConfigs: [fp] }, statePath);
73
+ }
74
+ }
75
+ else if (response.status === 410) {
76
+ (0, state_1.writeState)({ enabled: false, reason: "sunset" }, statePath);
77
+ }
78
+ }
79
+ catch {
80
+ // telemetry must never affect a run
81
+ }
82
+ };
83
+ exports.track = track;