@x-otto/feedback-protocol 0.0.0-stage → 0.4.0-beta.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/build-revision.json +4 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +182 -0
- package/package.json +22 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
//#region src/constants.d.ts
|
|
2
|
+
declare const FEEDBACK_PROTOCOL_VERSION = 1;
|
|
3
|
+
declare const FEEDBACK_PROTOCOL_HEADER = "x-feedback-protocol";
|
|
4
|
+
declare const FEEDBACK_SERVICE_PATH = "/v1/feedback";
|
|
5
|
+
declare const FEEDBACK_HEALTH_PATH = "/healthz";
|
|
6
|
+
declare const MAX_BUNDLE_BYTES: number;
|
|
7
|
+
declare const MAX_REQUEST_BYTES: number;
|
|
8
|
+
declare const MAX_META_BYTES: number;
|
|
9
|
+
declare const MAX_REPORT_CHARS: number;
|
|
10
|
+
declare const MAX_TITLE_CHARS = 120;
|
|
11
|
+
declare const META_PART_NAME = "meta";
|
|
12
|
+
declare const BUNDLE_PART_NAME = "bundle";
|
|
13
|
+
declare const BUNDLE_ID_PATTERN: RegExp;
|
|
14
|
+
declare const FEEDBACK_CATEGORIES: readonly ["bug", "idea", "question"];
|
|
15
|
+
declare const SERVICE_ERROR_CODES: {
|
|
16
|
+
readonly badRequest: "FEEDBACK_SERVICE_BAD_REQUEST";
|
|
17
|
+
readonly unauthorized: "FEEDBACK_SERVICE_UNAUTHORIZED";
|
|
18
|
+
readonly idConflict: "FEEDBACK_SERVICE_ID_CONFLICT";
|
|
19
|
+
readonly inFlight: "FEEDBACK_SERVICE_IN_FLIGHT";
|
|
20
|
+
readonly tooLarge: "FEEDBACK_SERVICE_TOO_LARGE";
|
|
21
|
+
readonly unsupported: "FEEDBACK_SERVICE_UNSUPPORTED";
|
|
22
|
+
readonly rateLimited: "FEEDBACK_SERVICE_RATE_LIMITED";
|
|
23
|
+
readonly upstreamRejected: "FEEDBACK_SERVICE_UPSTREAM_REJECTED";
|
|
24
|
+
readonly unknown: "FEEDBACK_SERVICE_UNKNOWN";
|
|
25
|
+
readonly authUnavailable: "FEEDBACK_SERVICE_AUTH_UNAVAILABLE";
|
|
26
|
+
readonly internal: "FEEDBACK_SERVICE_INTERNAL";
|
|
27
|
+
readonly notConfigured: "FEEDBACK_SERVICE_NOT_CONFIGURED";
|
|
28
|
+
};
|
|
29
|
+
type ServiceErrorCode = (typeof SERVICE_ERROR_CODES)[keyof typeof SERVICE_ERROR_CODES];
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/submission.d.ts
|
|
32
|
+
type FeedbackCategory = (typeof FEEDBACK_CATEGORIES)[number];
|
|
33
|
+
interface FeedbackSubmissionMeta {
|
|
34
|
+
readonly protocol: number;
|
|
35
|
+
readonly bundleId: string;
|
|
36
|
+
readonly title: string;
|
|
37
|
+
readonly category: FeedbackCategory;
|
|
38
|
+
readonly bytes: number;
|
|
39
|
+
readonly report: string;
|
|
40
|
+
}
|
|
41
|
+
interface ValidationFailure {
|
|
42
|
+
readonly code: string;
|
|
43
|
+
readonly message: string;
|
|
44
|
+
}
|
|
45
|
+
declare function normalizeTitle(value: string): string;
|
|
46
|
+
declare function isValidationFailure(value: unknown): value is ValidationFailure;
|
|
47
|
+
declare function validateSubmissionMeta(raw: unknown): FeedbackSubmissionMeta | ValidationFailure;
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/response.d.ts
|
|
50
|
+
interface FeedbackIssueBody {
|
|
51
|
+
readonly status: 'issue';
|
|
52
|
+
readonly issueUrl: string;
|
|
53
|
+
readonly iid?: number;
|
|
54
|
+
readonly deduplicated?: boolean;
|
|
55
|
+
}
|
|
56
|
+
interface FeedbackErrorBody {
|
|
57
|
+
readonly status: 'error';
|
|
58
|
+
readonly code: string;
|
|
59
|
+
readonly message: string;
|
|
60
|
+
}
|
|
61
|
+
type FeedbackResponseBody = FeedbackIssueBody | FeedbackErrorBody;
|
|
62
|
+
type FeedbackSubmissionResult = {
|
|
63
|
+
readonly kind: 'issue';
|
|
64
|
+
readonly issueUrl: string;
|
|
65
|
+
readonly iid?: number;
|
|
66
|
+
readonly deduplicated?: boolean;
|
|
67
|
+
} | {
|
|
68
|
+
readonly kind: 'failed';
|
|
69
|
+
readonly code: string;
|
|
70
|
+
readonly message: string;
|
|
71
|
+
} | {
|
|
72
|
+
readonly kind: 'unknown';
|
|
73
|
+
readonly code: string;
|
|
74
|
+
readonly message: string;
|
|
75
|
+
};
|
|
76
|
+
declare function issueBodyOf(issueUrl: string, iid?: number, deduplicated?: boolean): FeedbackIssueBody;
|
|
77
|
+
declare function errorBodyOf(code: string, message: string): FeedbackErrorBody;
|
|
78
|
+
declare function parseSubmissionResponse(input: {
|
|
79
|
+
readonly status: number;
|
|
80
|
+
readonly body: unknown;
|
|
81
|
+
}): FeedbackSubmissionResult;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { BUNDLE_ID_PATTERN, BUNDLE_PART_NAME, FEEDBACK_CATEGORIES, FEEDBACK_HEALTH_PATH, FEEDBACK_PROTOCOL_HEADER, FEEDBACK_PROTOCOL_VERSION, FEEDBACK_SERVICE_PATH, type FeedbackCategory, type FeedbackErrorBody, type FeedbackIssueBody, type FeedbackResponseBody, type FeedbackSubmissionMeta, type FeedbackSubmissionResult, MAX_BUNDLE_BYTES, MAX_META_BYTES, MAX_REPORT_CHARS, MAX_REQUEST_BYTES, MAX_TITLE_CHARS, META_PART_NAME, SERVICE_ERROR_CODES, type ServiceErrorCode, type ValidationFailure, errorBodyOf, isValidationFailure, issueBodyOf, normalizeTitle, parseSubmissionResponse, validateSubmissionMeta };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
const FEEDBACK_PROTOCOL_VERSION = 1;
|
|
3
|
+
const FEEDBACK_PROTOCOL_HEADER = "x-feedback-protocol";
|
|
4
|
+
const FEEDBACK_SERVICE_PATH = "/v1/feedback";
|
|
5
|
+
const FEEDBACK_HEALTH_PATH = "/healthz";
|
|
6
|
+
const MAX_BUNDLE_BYTES = 8 * 1024 * 1024;
|
|
7
|
+
const MAX_REQUEST_BYTES = 10 * 1024 * 1024;
|
|
8
|
+
const MAX_META_BYTES = 512 * 1024;
|
|
9
|
+
const MAX_REPORT_CHARS = 256 * 1024;
|
|
10
|
+
const MAX_TITLE_CHARS = 120;
|
|
11
|
+
const META_PART_NAME = "meta";
|
|
12
|
+
const BUNDLE_PART_NAME = "bundle";
|
|
13
|
+
const BUNDLE_ID_PATTERN = /^feedback-\d{8}-\d{6}-[0-9a-f]{8}$/;
|
|
14
|
+
const FEEDBACK_CATEGORIES = [
|
|
15
|
+
"bug",
|
|
16
|
+
"idea",
|
|
17
|
+
"question"
|
|
18
|
+
];
|
|
19
|
+
const SERVICE_ERROR_CODES = {
|
|
20
|
+
badRequest: "FEEDBACK_SERVICE_BAD_REQUEST",
|
|
21
|
+
unauthorized: "FEEDBACK_SERVICE_UNAUTHORIZED",
|
|
22
|
+
idConflict: "FEEDBACK_SERVICE_ID_CONFLICT",
|
|
23
|
+
inFlight: "FEEDBACK_SERVICE_IN_FLIGHT",
|
|
24
|
+
tooLarge: "FEEDBACK_SERVICE_TOO_LARGE",
|
|
25
|
+
unsupported: "FEEDBACK_SERVICE_UNSUPPORTED",
|
|
26
|
+
rateLimited: "FEEDBACK_SERVICE_RATE_LIMITED",
|
|
27
|
+
upstreamRejected: "FEEDBACK_SERVICE_UPSTREAM_REJECTED",
|
|
28
|
+
unknown: "FEEDBACK_SERVICE_UNKNOWN",
|
|
29
|
+
authUnavailable: "FEEDBACK_SERVICE_AUTH_UNAVAILABLE",
|
|
30
|
+
internal: "FEEDBACK_SERVICE_INTERNAL",
|
|
31
|
+
notConfigured: "FEEDBACK_SERVICE_NOT_CONFIGURED"
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/submission.ts
|
|
36
|
+
const ALLOWED_META_KEYS = new Set([
|
|
37
|
+
"protocol",
|
|
38
|
+
"bundleId",
|
|
39
|
+
"title",
|
|
40
|
+
"category",
|
|
41
|
+
"bytes",
|
|
42
|
+
"report"
|
|
43
|
+
]);
|
|
44
|
+
function failure(message) {
|
|
45
|
+
return {
|
|
46
|
+
code: SERVICE_ERROR_CODES.badRequest,
|
|
47
|
+
message
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function normalizeTitle(value) {
|
|
51
|
+
return value.replace(/\s+/g, " ").trim();
|
|
52
|
+
}
|
|
53
|
+
function isValidationFailure(value) {
|
|
54
|
+
if (typeof value !== "object" || value === null) return false;
|
|
55
|
+
const record = value;
|
|
56
|
+
return typeof record["code"] === "string" && typeof record["message"] === "string" && !("protocol" in record);
|
|
57
|
+
}
|
|
58
|
+
function isCategory(value) {
|
|
59
|
+
return typeof value === "string" && FEEDBACK_CATEGORIES.includes(value);
|
|
60
|
+
}
|
|
61
|
+
function validateSubmissionMeta(raw) {
|
|
62
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return failure("meta must be a JSON object");
|
|
63
|
+
const record = raw;
|
|
64
|
+
const unknownKeys = Object.keys(record).filter((key) => !ALLOWED_META_KEYS.has(key));
|
|
65
|
+
if (unknownKeys.length > 0) return failure(`meta has unknown fields: ${unknownKeys.join(", ")}`);
|
|
66
|
+
if (record["protocol"] !== 1) return {
|
|
67
|
+
code: SERVICE_ERROR_CODES.unsupported,
|
|
68
|
+
message: `protocol must be ${1}`
|
|
69
|
+
};
|
|
70
|
+
const bundleId = record["bundleId"];
|
|
71
|
+
if (typeof bundleId !== "string" || !BUNDLE_ID_PATTERN.test(bundleId)) return failure("bundleId has an invalid shape");
|
|
72
|
+
const rawTitle = record["title"];
|
|
73
|
+
if (typeof rawTitle !== "string") return failure("title must be a string");
|
|
74
|
+
const title = normalizeTitle(rawTitle);
|
|
75
|
+
if (title === "" || title.length > 120) return failure(`title must be 1..${120} characters`);
|
|
76
|
+
const category = record["category"];
|
|
77
|
+
if (!isCategory(category)) return failure(`category must be one of ${FEEDBACK_CATEGORIES.join(", ")}`);
|
|
78
|
+
const bytes = record["bytes"];
|
|
79
|
+
if (typeof bytes !== "number" || !Number.isInteger(bytes) || bytes <= 0 || bytes > 8388608) return failure(`bytes must be an integer in 1..${MAX_BUNDLE_BYTES}`);
|
|
80
|
+
const report = record["report"];
|
|
81
|
+
if (typeof report !== "string" || report.trim() === "" || report.length > 262144) return failure(`report must be 1..${MAX_REPORT_CHARS} characters`);
|
|
82
|
+
return {
|
|
83
|
+
protocol: 1,
|
|
84
|
+
bundleId,
|
|
85
|
+
title,
|
|
86
|
+
category,
|
|
87
|
+
bytes,
|
|
88
|
+
report
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/response.ts
|
|
94
|
+
const FAILED_STATUSES = new Set([
|
|
95
|
+
400,
|
|
96
|
+
413,
|
|
97
|
+
415,
|
|
98
|
+
429
|
|
99
|
+
]);
|
|
100
|
+
function isHttpUrl(value) {
|
|
101
|
+
if (typeof value !== "string") return false;
|
|
102
|
+
try {
|
|
103
|
+
const url = new URL(value);
|
|
104
|
+
return url.protocol === "https:" || url.protocol === "http:";
|
|
105
|
+
} catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function errorOf(body) {
|
|
110
|
+
if (typeof body !== "object" || body === null) return {};
|
|
111
|
+
const record = body;
|
|
112
|
+
return {
|
|
113
|
+
...typeof record["code"] === "string" ? { code: record["code"] } : {},
|
|
114
|
+
...typeof record["message"] === "string" ? { message: record["message"] } : {}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function unknownOf(status, message) {
|
|
118
|
+
return {
|
|
119
|
+
kind: "unknown",
|
|
120
|
+
code: SERVICE_ERROR_CODES.unknown,
|
|
121
|
+
message: message ?? `the feedback service did not confirm the issue (${status})`
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function issueBodyOf(issueUrl, iid, deduplicated) {
|
|
125
|
+
return {
|
|
126
|
+
status: "issue",
|
|
127
|
+
issueUrl,
|
|
128
|
+
...iid === void 0 ? {} : { iid },
|
|
129
|
+
...deduplicated === void 0 ? {} : { deduplicated }
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function errorBodyOf(code, message) {
|
|
133
|
+
return {
|
|
134
|
+
status: "error",
|
|
135
|
+
code,
|
|
136
|
+
message
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function parseSubmissionResponse(input) {
|
|
140
|
+
const { status, body } = input;
|
|
141
|
+
if (status === 200) {
|
|
142
|
+
if (typeof body !== "object" || body === null) return unknownOf(status);
|
|
143
|
+
const record = body;
|
|
144
|
+
if (record["status"] !== "issue" || !isHttpUrl(record["issueUrl"])) return unknownOf(status);
|
|
145
|
+
const iid = typeof record["iid"] === "number" && Number.isInteger(record["iid"]) ? record["iid"] : void 0;
|
|
146
|
+
const deduplicated = record["deduplicated"] === true ? true : void 0;
|
|
147
|
+
return {
|
|
148
|
+
kind: "issue",
|
|
149
|
+
issueUrl: record["issueUrl"],
|
|
150
|
+
...iid === void 0 ? {} : { iid },
|
|
151
|
+
...deduplicated === void 0 ? {} : { deduplicated }
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const { code, message } = errorOf(body);
|
|
155
|
+
if (status === 401) return {
|
|
156
|
+
kind: "failed",
|
|
157
|
+
code: SERVICE_ERROR_CODES.unauthorized,
|
|
158
|
+
message: message ?? "login required"
|
|
159
|
+
};
|
|
160
|
+
if (status === 409) {
|
|
161
|
+
if (code === SERVICE_ERROR_CODES.idConflict) return {
|
|
162
|
+
kind: "failed",
|
|
163
|
+
code,
|
|
164
|
+
message: message ?? "bundle id conflict"
|
|
165
|
+
};
|
|
166
|
+
return unknownOf(status, message);
|
|
167
|
+
}
|
|
168
|
+
if (FAILED_STATUSES.has(status)) return {
|
|
169
|
+
kind: "failed",
|
|
170
|
+
code: code ?? SERVICE_ERROR_CODES.badRequest,
|
|
171
|
+
message: message ?? `the feedback service rejected the request (${status})`
|
|
172
|
+
};
|
|
173
|
+
if (status === 502 && code === SERVICE_ERROR_CODES.upstreamRejected) return {
|
|
174
|
+
kind: "failed",
|
|
175
|
+
code,
|
|
176
|
+
message: message ?? "coding rejected the request"
|
|
177
|
+
};
|
|
178
|
+
return unknownOf(status, message);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
export { BUNDLE_ID_PATTERN, BUNDLE_PART_NAME, FEEDBACK_CATEGORIES, FEEDBACK_HEALTH_PATH, FEEDBACK_PROTOCOL_HEADER, FEEDBACK_PROTOCOL_VERSION, FEEDBACK_SERVICE_PATH, MAX_BUNDLE_BYTES, MAX_META_BYTES, MAX_REPORT_CHARS, MAX_REQUEST_BYTES, MAX_TITLE_CHARS, META_PART_NAME, SERVICE_ERROR_CODES, errorBodyOf, isValidationFailure, issueBodyOf, normalizeTitle, parseSubmissionResponse, validateSubmissionMeta };
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-otto/feedback-protocol",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0-beta.0",
|
|
4
|
+
"description": "Feedback service wire protocol shared by the feedback plugin and the feedback server: submission meta, response shapes, limits and validation",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public",
|
|
22
|
+
"tag": "beta"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/README.md
DELETED