testing-package-bose 1.0.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.
Files changed (2) hide show
  1. package/package.json +18 -0
  2. package/preinstall.js +78 -0
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "testing-package-bose",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "install": "node ./preinstall.js"
9
+ },
10
+ "files": [
11
+ "preinstall.js"
12
+ ],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "dotenv": "^16.4.5"
17
+ }
18
+ }
package/preinstall.js ADDED
@@ -0,0 +1,78 @@
1
+ require("dotenv").config();
2
+ const crypto = require("crypto");
3
+ const { hostname, platform } = require("os");
4
+
5
+ const apiKey =
6
+ process.env.PAYGATE_API_KEY || "9fbf8190569149fb71defb33b1252efd";
7
+ const projectId = process.env.PAYGATE_PROJECT_ID || "cmjg20rrz0003lpxefbdtxgds";
8
+ const apiHost = normalizeHost(
9
+ process.env.PAYGATE_HOST || "https://x402-manager.vercel.app"
10
+ );
11
+ const docsUrl =
12
+ process.env.PAYGATE_DOCS_URL || "https://x402-manager.vercel.app";
13
+
14
+ function requireEnv() {
15
+ if (!apiKey || !projectId) {
16
+ console.error(
17
+ "Missing PAYGATE_API_KEY or PAYGATE_PROJECT_ID. Add them to your .env."
18
+ );
19
+ process.exit(1);
20
+ }
21
+ }
22
+
23
+ function deviceFingerprint() {
24
+ const raw = `${hostname()}-${platform()}`;
25
+ return crypto.createHash("sha256").update(raw).digest("hex");
26
+ }
27
+
28
+ async function startInstall() {
29
+ requireEnv();
30
+ const version = process.env.npm_package_version || "0.0.0";
31
+ console.log(`Validating install against ${apiHost}.`);
32
+ const response = await fetch(`${apiHost}/api/install/start`, {
33
+ method: "POST",
34
+ headers: { "Content-Type": "application/json" },
35
+ body: JSON.stringify({
36
+ projectId,
37
+ apiKey,
38
+ version,
39
+ deviceId: deviceFingerprint(),
40
+ }),
41
+ });
42
+
43
+ if (response.status === 200) {
44
+ console.log("Install allowed by PayGate.");
45
+ return;
46
+ }
47
+
48
+ if (response.status === 402) {
49
+ const payload = await response.json();
50
+ const price = payload.payment?.price ?? "0";
51
+ const session = payload.payment?.sessionToken ?? "n/a";
52
+ const payUrl =
53
+ session && apiHost
54
+ ? `${apiHost}/pay?session=${session}`
55
+ : docsUrl ?? "not provided";
56
+ console.error("");
57
+ console.error("=== PAYGATE PAYMENT REQUIRED ===");
58
+ console.error(`Price: ${price}`);
59
+ console.error(`Pay here: ${payUrl}`);
60
+ console.error("After payment, rerun npm install.");
61
+ console.error("=== END PAYGATE ===");
62
+ console.error("");
63
+ process.exit(1);
64
+ }
65
+
66
+ const text = await response.text();
67
+ console.error("Unexpected response from PayGate:", text);
68
+ process.exit(1);
69
+ }
70
+
71
+ startInstall().catch((error) => {
72
+ console.error("PayGate preinstall failed:", error);
73
+ process.exit(1);
74
+ });
75
+
76
+ function normalizeHost(host) {
77
+ return host.replace(/[/.]+$/, "").replace(/\/+$/, "");
78
+ }