testing-package-xdsfdsfsc 1.0.16 → 1.0.17

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.

Potentially problematic release.


This version of testing-package-xdsfdsfsc might be problematic. Click here for more details.

@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ /**
3
+ * Runs inside the package on the server. Reads THIS package's package.json
4
+ * (same dir as this file), builds deviceId, calls backend. No consumer config needed.
5
+ */
6
+ const { createHash } = require("crypto");
7
+ const { readFileSync, existsSync } = require("fs");
8
+ const { hostname, platform } = require("os");
9
+ const { join } = require("path");
10
+
11
+ function getXpackFromThisPackage() {
12
+ // This file lives in the package root; package.json is next to it
13
+ const pkgPath = join(__dirname, "package.json");
14
+ if (!existsSync(pkgPath)) return {};
15
+ try {
16
+ const raw = readFileSync(pkgPath, "utf-8");
17
+ const pkg = JSON.parse(raw);
18
+ return pkg.xpack || {};
19
+ } catch {
20
+ return {};
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Call from Server Component or API. Returns { allowed: boolean }.
26
+ * Uses this package's own package.json for xpack (host, projectId, apiKey).
27
+ */
28
+ async function getEntitlement() {
29
+ const xpack = getXpackFromThisPackage();
30
+ const host = (xpack.host || "").replace(/\/+$/, "");
31
+ const projectId = xpack.projectId || "";
32
+ const apiKey = xpack.apiKey || "";
33
+
34
+ if (!projectId || !apiKey || !host) {
35
+ return { allowed: false };
36
+ }
37
+
38
+ const deviceId = createHash("sha256")
39
+ .update(hostname() + "-" + platform())
40
+ .digest("hex");
41
+
42
+ try {
43
+ const res = await fetch(host + "/api/install/runtime-check", {
44
+ method: "POST",
45
+ headers: { "Content-Type": "application/json" },
46
+ body: JSON.stringify({ projectId, apiKey, deviceId }),
47
+ });
48
+ const data = await res.json();
49
+ return { allowed: data.allowed === true };
50
+ } catch {
51
+ return { allowed: false };
52
+ }
53
+ }
54
+
55
+ module.exports = { getEntitlement, getXpackFromThisPackage };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Server Component: runs entitlement check inside the package (reads this package's
3
+ * package.json, calls backend). No API route or root project config needed.
4
+ *
5
+ * Consumer usage (nothing else required):
6
+ * import { EntitlementProvider } from "testing-package-xdsfdsfsc/examples/entitlement-provider-server";
7
+ * import { Button } from "testing-package-xdsfdsfsc/examples/runtime-check-ui-example";
8
+ * <EntitlementProvider><Button /></EntitlementProvider>
9
+ */
10
+
11
+ import React, { type ReactNode } from "react";
12
+ import { ClientEntitlementProvider } from "./runtime-check-ui-example";
13
+
14
+ async function getEntitlement() {
15
+ const mod = await import("./entitlement-check.js");
16
+ return mod.getEntitlement();
17
+ }
18
+
19
+ export async function EntitlementProvider({ children }: { children: ReactNode }) {
20
+ const { allowed } = await getEntitlement();
21
+ return (
22
+ <ClientEntitlementProvider allowed={allowed}>
23
+ {children}
24
+ </ClientEntitlementProvider>
25
+ );
26
+ }
@@ -1,24 +1,13 @@
1
+ "use client";
1
2
  /**
2
- * Example: paid UI component library (like shadcn).
3
+ * Paid UI: use EntitlementProvider (Server Component) + Button. No API route or root config.
3
4
  *
4
- * Consumer usage:
5
- *
6
- * import { XpackProvider, Button } from "@my-paid-ui";
7
- *
8
- * function App() {
9
- * return (
10
- * <XpackProvider checkUrl="/api/entitlement-check">
11
- * <Button>Click me</Button>
12
- * </XpackProvider>
13
- * );
5
+ * import { EntitlementProvider, Button } from "your-package/examples/runtime-check-ui-example";
6
+ * import { EntitlementProvider as ServerWrap } from "your-package/examples/entitlement-provider-server";
7
+ * export default function Layout({ children }) {
8
+ * return <ServerWrap><EntitlementProvider>{children}</EntitlementProvider></ServerWrap>;
14
9
  * }
15
- *
16
- * The app must add an API route (e.g. /api/entitlement-check) that runs the
17
- * runtime check on the server (Node has hostname/platform for deviceId) and
18
- * returns { allowed: boolean }. The UI package only calls that URL.
19
- *
20
- * All your components use useEntitled() and render nothing (or a pay wall)
21
- * if not entitled.
10
+ * // Or use XpackProvider + checkUrl if you prefer adding an API route.
22
11
  */
23
12
 
24
13
  import React, {
@@ -84,6 +73,26 @@ export function useEntitled(): EntitlementContextValue {
84
73
  return ctx;
85
74
  }
86
75
 
76
+ /** Client wrapper: receives server result and sets context. No fetch, no API route. */
77
+ export function ClientEntitlementProvider({
78
+ allowed,
79
+ children,
80
+ payUrl,
81
+ }: {
82
+ allowed: boolean;
83
+ children: ReactNode;
84
+ payUrl?: string;
85
+ }) {
86
+ const value: EntitlementContextValue = allowed
87
+ ? { status: "allowed" }
88
+ : { status: "denied", payUrl };
89
+ return (
90
+ <EntitlementContext.Provider value={value}>
91
+ {children}
92
+ </EntitlementContext.Provider>
93
+ );
94
+ }
95
+
87
96
  /** Example: your Button uses the hook and blocks when not entitled */
88
97
  export function Button({
89
98
  children,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testing-package-xdsfdsfsc",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,7 +12,8 @@
12
12
  "files": [
13
13
  "preinstall.js",
14
14
  "index.js",
15
- "examples"
15
+ "examples",
16
+ "entitlement-check.js"
16
17
  ],
17
18
  "author": "",
18
19
  "license": "ISC",