@posissue/core 0.0.0-stage → 1.3.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/LICENSE +21 -0
- package/README.md +14 -2
- package/dist/index.d.ts +76 -0
- package/dist/index.js +138 -0
- package/package.json +40 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ninjin0802
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @posissue/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared types, validation and issue rendering for [PosIssue](https://posissue.2nj.lol) — a feedback widget that lets anyone send GitHub issues without a GitHub account.
|
|
4
|
+
|
|
5
|
+
You usually don’t need this package directly. Use [`posissue`](https://www.npmjs.com/package/posissue) for the widget, or [`@posissue/server`](https://www.npmjs.com/package/@posissue/server) to self-host.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { DEFAULT_FORMS, parseReportRequest, buildIssue, type FormTemplate } from "@posissue/core";
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- `FormTemplate` / `FormField` — the typed form definition (one type → UI, validation and issue body)
|
|
12
|
+
- `parseReportRequest(input, forms)` — validate an untrusted report against the form type
|
|
13
|
+
- `buildIssue(report, forms, labels)` — render a safe issue (mentions and links neutralized)
|
|
14
|
+
|
|
15
|
+
MIT · https://github.com/ninjin0802/posissue
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export type ReportKind = "bug" | "feature" | "other";
|
|
2
|
+
export declare const REPORT_KINDS: readonly ReportKind[];
|
|
3
|
+
/** 表示用の文字列。文字列なら全言語共通、オブジェクトなら言語別(en は必須)。 */
|
|
4
|
+
export type LocalText = string | {
|
|
5
|
+
en: string;
|
|
6
|
+
ja?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const text: (t: LocalText, lang?: "en" | "ja") => string;
|
|
9
|
+
export type FieldType = "text" | "textarea" | "select" | "checkbox";
|
|
10
|
+
/** フォームの1項目。Issue 本文では `### label` の見出し付きで並ぶ。 */
|
|
11
|
+
export interface FormField {
|
|
12
|
+
id: string;
|
|
13
|
+
type: FieldType;
|
|
14
|
+
label: LocalText;
|
|
15
|
+
placeholder?: LocalText;
|
|
16
|
+
help?: LocalText;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
options?: {
|
|
19
|
+
value: string;
|
|
20
|
+
label: LocalText;
|
|
21
|
+
}[];
|
|
22
|
+
maxLength?: number;
|
|
23
|
+
}
|
|
24
|
+
/** 種別ごとのフォームの型。これを渡すだけでフォームと Issue 本文の形が決まる。 */
|
|
25
|
+
export interface FormTemplate {
|
|
26
|
+
kind: ReportKind;
|
|
27
|
+
label: LocalText;
|
|
28
|
+
description?: LocalText;
|
|
29
|
+
titlePlaceholder?: LocalText;
|
|
30
|
+
fields: FormField[];
|
|
31
|
+
}
|
|
32
|
+
export declare const DEFAULT_FORMS: FormTemplate[];
|
|
33
|
+
export interface ReportInput {
|
|
34
|
+
kind: ReportKind;
|
|
35
|
+
title: string;
|
|
36
|
+
fields: Record<string, string>;
|
|
37
|
+
meta?: Record<string, string>;
|
|
38
|
+
}
|
|
39
|
+
export interface ReportRequest extends ReportInput {
|
|
40
|
+
projectKey: string;
|
|
41
|
+
turnstileToken: string;
|
|
42
|
+
hp?: string;
|
|
43
|
+
elapsedMs: number;
|
|
44
|
+
}
|
|
45
|
+
export type ErrorCode = "invalid_input" | "unknown_project" | "forbidden_origin" | "captcha_failed" | "rate_limited" | "duplicate" | "spam" | "upstream_error";
|
|
46
|
+
export type ReportResponse = {
|
|
47
|
+
ok: true;
|
|
48
|
+
issueNumber: number;
|
|
49
|
+
issueUrl: string;
|
|
50
|
+
} | {
|
|
51
|
+
ok: true;
|
|
52
|
+
pending: true;
|
|
53
|
+
} | {
|
|
54
|
+
ok: false;
|
|
55
|
+
error: ErrorCode;
|
|
56
|
+
};
|
|
57
|
+
export declare const LIMITS: {
|
|
58
|
+
readonly title: 100;
|
|
59
|
+
readonly body: 5000;
|
|
60
|
+
readonly metaKeys: 20;
|
|
61
|
+
readonly metaValue: 200;
|
|
62
|
+
};
|
|
63
|
+
/** 開発者が渡したフォームの型が正しいか確かめる。間違いは起動時に例外で知らせる。 */
|
|
64
|
+
export declare function assertForms(forms: FormTemplate[]): void;
|
|
65
|
+
/** 信頼できない入力を、フォームの型に沿って検証する。不正なら null。 */
|
|
66
|
+
export declare function parseReportRequest(v: unknown, forms?: FormTemplate[]): ReportRequest | null;
|
|
67
|
+
/** レポート全体の URL 数(スパム判定 7.1 #4)。 */
|
|
68
|
+
export declare const reportUrls: (r: ReportInput) => number;
|
|
69
|
+
export declare const countUrls: (s: string) => number;
|
|
70
|
+
/** @メンションと #番号 の通知を無効化する(7.2)。 */
|
|
71
|
+
export declare const neutralize: (s: string) => string;
|
|
72
|
+
export declare function buildIssue(r: ReportInput, forms?: FormTemplate[], extraLabels?: string[]): {
|
|
73
|
+
title: string;
|
|
74
|
+
body: string;
|
|
75
|
+
labels: string[];
|
|
76
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export const REPORT_KINDS = ["bug", "feature", "other"];
|
|
2
|
+
export const text = (t, lang = "en") => (typeof t === "string" ? t : (lang === "ja" && t.ja) || t.en);
|
|
3
|
+
export const DEFAULT_FORMS = [
|
|
4
|
+
{
|
|
5
|
+
kind: "bug", label: { en: "Bug", ja: "バグ報告" },
|
|
6
|
+
description: { en: "Something is broken or not working as expected.", ja: "動かない・おかしな動きをする" },
|
|
7
|
+
titlePlaceholder: { en: "e.g. Save button does nothing", ja: "例: 保存ボタンを押しても反応しない" },
|
|
8
|
+
fields: [
|
|
9
|
+
{ id: "steps", type: "textarea", required: true, label: { en: "Steps to reproduce", ja: "再現手順" },
|
|
10
|
+
placeholder: { en: "1. Open ...\n2. Click ...", ja: "1. 〇〇を開く\n2. 〇〇を押す" } },
|
|
11
|
+
{ id: "expected", type: "textarea", label: { en: "Expected behavior", ja: "期待した動き" } },
|
|
12
|
+
{ id: "actual", type: "textarea", required: true, label: { en: "What happened", ja: "実際に起きたこと" } },
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
kind: "feature", label: { en: "Feature request", ja: "機能の要望" },
|
|
17
|
+
description: { en: "An idea to make it better.", ja: "こうなったら嬉しい" },
|
|
18
|
+
titlePlaceholder: { en: "e.g. Export as CSV", ja: "例: CSV で書き出したい" },
|
|
19
|
+
fields: [
|
|
20
|
+
{ id: "problem", type: "textarea", required: true, label: { en: "What problem does it solve?", ja: "困っていること" } },
|
|
21
|
+
{ id: "proposal", type: "textarea", label: { en: "Your idea", ja: "こうしてほしい" } },
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
kind: "other", label: { en: "Other", ja: "その他" },
|
|
26
|
+
description: { en: "Questions, thanks, anything else.", ja: "質問・感想など" },
|
|
27
|
+
fields: [{ id: "message", type: "textarea", required: true, label: { en: "Message", ja: "内容" } }],
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
export const LIMITS = { title: 100, body: 5000, metaKeys: 20, metaValue: 200 };
|
|
31
|
+
const META_KEY_RE = /^[A-Za-z0-9_-]{1,50}$/; // meta のキーは記号なし(Issue の表にそのまま出すため)
|
|
32
|
+
const KEY_RE = /^pk_[A-Za-z0-9]{24}$/;
|
|
33
|
+
const str = (v, max) => typeof v === "string" && v.trim().length > 0 && v.length <= max;
|
|
34
|
+
const ID_RE = /^[A-Za-z0-9_-]{1,32}$/;
|
|
35
|
+
const maxLen = (f) => f.maxLength ?? (f.type === "textarea" ? LIMITS.body : LIMITS.title);
|
|
36
|
+
/** 開発者が渡したフォームの型が正しいか確かめる。間違いは起動時に例外で知らせる。 */
|
|
37
|
+
export function assertForms(forms) {
|
|
38
|
+
const kinds = new Set();
|
|
39
|
+
for (const t of forms) {
|
|
40
|
+
if (!REPORT_KINDS.includes(t.kind) || kinds.has(t.kind))
|
|
41
|
+
throw new Error(`invalid or duplicate kind: ${t.kind}`);
|
|
42
|
+
kinds.add(t.kind);
|
|
43
|
+
const ids = new Set();
|
|
44
|
+
for (const f of t.fields) {
|
|
45
|
+
if (!ID_RE.test(f.id) || ids.has(f.id))
|
|
46
|
+
throw new Error(`invalid or duplicate field id: ${t.kind}.${f.id}`);
|
|
47
|
+
ids.add(f.id);
|
|
48
|
+
if (f.type === "select" && !f.options?.length)
|
|
49
|
+
throw new Error(`select needs options: ${t.kind}.${f.id}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** 信頼できない入力を、フォームの型に沿って検証する。不正なら null。 */
|
|
54
|
+
export function parseReportRequest(v, forms = DEFAULT_FORMS) {
|
|
55
|
+
if (!v || typeof v !== "object")
|
|
56
|
+
return null;
|
|
57
|
+
const o = v;
|
|
58
|
+
const tpl = forms.find((t) => t.kind === o.kind);
|
|
59
|
+
if (!tpl || !str(o.title, LIMITS.title))
|
|
60
|
+
return null;
|
|
61
|
+
if (!o.fields || typeof o.fields !== "object" || Array.isArray(o.fields))
|
|
62
|
+
return null;
|
|
63
|
+
const raw = o.fields;
|
|
64
|
+
const fields = {};
|
|
65
|
+
let total = 0;
|
|
66
|
+
for (const f of tpl.fields) {
|
|
67
|
+
const val = raw[f.id] ?? "";
|
|
68
|
+
if (typeof val !== "string" || val.length > maxLen(f))
|
|
69
|
+
return null;
|
|
70
|
+
if (f.required && !val.trim())
|
|
71
|
+
return null;
|
|
72
|
+
if (f.type === "select" && val && !f.options.some((x) => x.value === val))
|
|
73
|
+
return null;
|
|
74
|
+
if (f.type === "checkbox" && val && val !== "true" && val !== "false")
|
|
75
|
+
return null;
|
|
76
|
+
total += val.length;
|
|
77
|
+
fields[f.id] = val;
|
|
78
|
+
}
|
|
79
|
+
if (Object.keys(raw).some((k) => !(k in fields)) || total > LIMITS.body)
|
|
80
|
+
return null;
|
|
81
|
+
if (typeof o.projectKey !== "string" || !KEY_RE.test(o.projectKey))
|
|
82
|
+
return null;
|
|
83
|
+
if (typeof o.turnstileToken !== "string" || typeof o.elapsedMs !== "number")
|
|
84
|
+
return null;
|
|
85
|
+
if (o.hp !== undefined && typeof o.hp !== "string")
|
|
86
|
+
return null;
|
|
87
|
+
let meta;
|
|
88
|
+
if (o.meta !== undefined) {
|
|
89
|
+
if (!o.meta || typeof o.meta !== "object" || Array.isArray(o.meta))
|
|
90
|
+
return null;
|
|
91
|
+
const entries = Object.entries(o.meta);
|
|
92
|
+
if (entries.length > LIMITS.metaKeys)
|
|
93
|
+
return null;
|
|
94
|
+
for (const [k, val] of entries) {
|
|
95
|
+
if (!META_KEY_RE.test(k) || typeof val !== "string" || val.length > LIMITS.metaValue)
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
meta = Object.fromEntries(entries);
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
kind: tpl.kind, title: o.title.trim(), fields, meta,
|
|
102
|
+
projectKey: o.projectKey, turnstileToken: o.turnstileToken, hp: o.hp,
|
|
103
|
+
elapsedMs: o.elapsedMs,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** レポート全体の URL 数(スパム判定 7.1 #4)。 */
|
|
107
|
+
export const reportUrls = (r) => countUrls(r.title) + Object.values(r.fields).reduce((n, v) => n + countUrls(v), 0);
|
|
108
|
+
// スキーム付き・www.・Markdown のリンクや画像 "](" ・プロトコル相対 "//host" を数える
|
|
109
|
+
export const countUrls = (s) => (s.match(/https?:\/\/|www\.|\]\(|(?:^|[\s("'=<])\/\/[^\s/]/gim) ?? []).length;
|
|
110
|
+
const ZWSP = "";
|
|
111
|
+
/** @メンションと #番号 の通知を無効化する(7.2)。 */
|
|
112
|
+
export const neutralize = (s) => s.replace(/@(?=[A-Za-z0-9-])/g, "@" + ZWSP).replace(/#(?=\d)/g, "#" + ZWSP);
|
|
113
|
+
const cell = (s) => neutralize(s).replace(/[|`\\]/g, (c) => "\\" + c).replace(/\r?\n/g, " ");
|
|
114
|
+
// meta は報告者が自由に入れられる。Markdown/HTML の記号を全部文字参照にして、リンクにならないようにする(URL 数の制限の抜け道を塞ぐ)
|
|
115
|
+
const code = (s) => `<code>${neutralize(s).replace(/[&<>|[\]()!*_`\\:~.]/g, (c) => `&#${c.charCodeAt(0)};`).replace(/\r?\n/g, " ")}</code>`;
|
|
116
|
+
const PREFIX = { bug: "[Bug]", feature: "[Feature]", other: "[Feedback]" };
|
|
117
|
+
const KIND_LABEL = { bug: "bug", feature: "enhancement", other: "feedback" };
|
|
118
|
+
export function buildIssue(r, forms = DEFAULT_FORMS, extraLabels = []) {
|
|
119
|
+
const tpl = forms.find((t) => t.kind === r.kind);
|
|
120
|
+
const sections = tpl.fields
|
|
121
|
+
.filter((f) => r.fields[f.id]?.trim())
|
|
122
|
+
.map((f) => {
|
|
123
|
+
const v = r.fields[f.id];
|
|
124
|
+
const shown = f.type === "checkbox" ? (v === "true" ? "Yes" : "No")
|
|
125
|
+
: f.type === "select" ? text(f.options.find((o) => o.value === v).label) : v;
|
|
126
|
+
return `### ${neutralize(text(f.label))}\n\n${neutralize(shown)}`;
|
|
127
|
+
}).join("\n\n");
|
|
128
|
+
const meta = Object.entries(r.meta ?? {});
|
|
129
|
+
const table = meta.length
|
|
130
|
+
? `\n\n---\n<details><summary>Environment</summary>\n\n| key | value |\n|---|---|\n${meta
|
|
131
|
+
.map(([k, v]) => `| ${cell(k)} | ${code(v)} |`).join("\n")}\n</details>`
|
|
132
|
+
: "\n\n---";
|
|
133
|
+
return {
|
|
134
|
+
title: `${PREFIX[r.kind]} ${neutralize(r.title)}`,
|
|
135
|
+
body: `${sections}${table}\n\n_PosIssue ウィジェットから送信されました_`,
|
|
136
|
+
labels: [...new Set(["via-widget", KIND_LABEL[r.kind], ...extraLabels])],
|
|
137
|
+
};
|
|
138
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posissue/core",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"description": "Types, validation and issue rendering shared by PosIssue packages.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"posissue",
|
|
6
|
+
"github",
|
|
7
|
+
"issues",
|
|
8
|
+
"feedback"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/ninjin0802/posissue.git",
|
|
13
|
+
"directory": "packages/core"
|
|
14
|
+
},
|
|
15
|
+
"version": "1.3.0",
|
|
16
|
+
"homepage": "https://posissue.2nj.lol",
|
|
17
|
+
"bugs": "https://github.com/ninjin0802/posissue/issues",
|
|
18
|
+
"author": "ninjin0802",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp ../../LICENSE .",
|
|
40
|
+
"prepublishOnly": "npm run build"
|
|
41
|
+
}
|
|
42
|
+
}
|