@graphql-markdown/telemetry 0.0.0-stage → 0.1.1

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/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_ts_1 = require("./config-dir.js");
7
+ const constants_ts_1 = require("./constants.js");
8
+ const getStatePath = (dir = (0, config_dir_ts_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_ts_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.ts";
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,cAAc,CAAC;AAGjD,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_ts_1 = require("./ci.js");
6
+ const constants_ts_1 = require("./constants.js");
7
+ const disclaimer_ts_1 = require("./disclaimer.js");
8
+ const enabled_ts_1 = require("./enabled.js");
9
+ const payload_ts_1 = require("./payload.js");
10
+ const state_ts_1 = require("./state.js");
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_ts_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_ts_1.getStatePath)();
30
+ const state = (0, state_ts_1.readState)(statePath);
31
+ const decision = (0, enabled_ts_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_ts_1.detectCi)(env);
41
+ if ((0, disclaimer_ts_1.printDisclaimer)(state, ci, input.onFullNotice)) {
42
+ // Nothing is sent when the notice cannot be recorded.
43
+ (0, state_ts_1.writeState)({ enabled: true, noticeVersion: constants_ts_1.NOTICE_VERSION }, statePath);
44
+ }
45
+ const payload = (0, payload_ts_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_ts_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_ts_1.getEndpoint)(env), {
65
+ method: "POST",
66
+ headers,
67
+ body,
68
+ signal: AbortSignal.timeout(constants_ts_1.TIMEOUT_MS),
69
+ });
70
+ if (response.status === 202) {
71
+ if (dedupe) {
72
+ (0, state_ts_1.writeState)({ lastSentConfigs: [fp] }, statePath);
73
+ }
74
+ }
75
+ else if (response.status === 410) {
76
+ (0, state_ts_1.writeState)({ enabled: false, reason: "sunset" }, statePath);
77
+ }
78
+ }
79
+ catch {
80
+ // telemetry must never affect a run
81
+ }
82
+ };
83
+ exports.track = track;
package/package.json CHANGED
@@ -1,6 +1,89 @@
1
1
  {
2
2
  "name": "@graphql-markdown/telemetry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "homepage": "https://graphql-markdown.dev",
4
+ "description": "Anonymous, opt-out usage telemetry for the GraphQL-Markdown CLI.",
5
+ "bugs": {
6
+ "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
+ },
8
+ "version": "0.1.1",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/graphql-markdown/graphql-markdown.git",
13
+ "directory": "packages/telemetry"
14
+ },
15
+ "author": {
16
+ "name": "Gregory Heitz"
17
+ },
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "require": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "import": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "default": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ }
34
+ },
35
+ "./schema": {
36
+ "require": {
37
+ "types": "./dist/schema.d.ts",
38
+ "default": "./dist/schema.js"
39
+ },
40
+ "import": {
41
+ "types": "./dist/schema.d.ts",
42
+ "default": "./dist/schema.js"
43
+ },
44
+ "default": {
45
+ "types": "./dist/schema.d.ts",
46
+ "default": "./dist/schema.js"
47
+ }
48
+ },
49
+ "./package.json": "./package.json"
50
+ },
51
+ "scripts": {
52
+ "stryker": "stryker run",
53
+ "clean": "tsgo --build --clean && rm -rf ./dist",
54
+ "docs": "typedoc --options typedoc.config.mjs",
55
+ "build": "tsgo --build",
56
+ "lint": "eslint --config ../../packages/tooling-config/eslint/config.ts \"{src,tests}/**/*.{ts,js,json,gql}\"",
57
+ "lint:fix": "eslint --config ../../packages/tooling-config/eslint/config.ts \"{src,tests}/**/*.{ts,js,json,gql}\" --fix",
58
+ "prettier": "prettier \"{src,tests,assets}/**/*.{ts,js,json,md,gql}\" README.md --check",
59
+ "prettier:fix": "prettier \"{src,tests,assets}/**/*.{ts,js,json,md,gql}\" README.md --write",
60
+ "ts:check": "tsgo --noEmit",
61
+ "test": "vitest run --passWithNoTests",
62
+ "test:unit": "vitest run --passWithNoTests tests/unit",
63
+ "test:integration": "vitest run --passWithNoTests tests/integration",
64
+ "test:ci": "vitest run --passWithNoTests --silent --sequence.shuffle",
65
+ "test:watch": "vitest --watch"
66
+ },
67
+ "dependencies": {
68
+ "@graphql-markdown/types": "^1.17.1"
69
+ },
70
+ "devDependencies": {
71
+ "@graphql-markdown/tooling-config": "^1.0.0",
72
+ "@stryker-mutator/api": "10.1.1"
73
+ },
74
+ "directories": {
75
+ "test": "tests"
76
+ },
77
+ "engines": {
78
+ "node": ">=22.18.0"
79
+ },
80
+ "publishConfig": {
81
+ "access": "public"
82
+ },
83
+ "typescript": {
84
+ "definition": "dist/index.d.ts"
85
+ },
86
+ "files": [
87
+ "dist"
88
+ ]
89
+ }