@corelicense/node 0.5.0 → 0.5.5

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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -162
  3. package/dist/client/http-error-utils.d.ts +10 -0
  4. package/dist/client/http-error-utils.d.ts.map +1 -0
  5. package/dist/client/http-error-utils.js +38 -0
  6. package/dist/client/http-error-utils.js.map +1 -0
  7. package/dist/client/license-api-client.d.ts.map +1 -1
  8. package/dist/client/license-api-client.js +26 -6
  9. package/dist/client/license-api-client.js.map +1 -1
  10. package/dist/client/map-client-error.d.ts.map +1 -1
  11. package/dist/client/map-client-error.js +38 -12
  12. package/dist/client/map-client-error.js.map +1 -1
  13. package/dist/client/public-keys-client.d.ts.map +1 -1
  14. package/dist/client/public-keys-client.js +48 -4
  15. package/dist/client/public-keys-client.js.map +1 -1
  16. package/dist/core-license.js +1 -1
  17. package/dist/crypto/key-loader.d.ts +1 -1
  18. package/dist/crypto/key-loader.d.ts.map +1 -1
  19. package/dist/crypto/key-loader.js +34 -9
  20. package/dist/crypto/key-loader.js.map +1 -1
  21. package/dist/errors/api-error.d.ts +1 -0
  22. package/dist/errors/api-error.d.ts.map +1 -1
  23. package/dist/errors/api-error.js +2 -2
  24. package/dist/errors/api-error.js.map +1 -1
  25. package/dist/errors/codes.d.ts +5 -0
  26. package/dist/errors/codes.d.ts.map +1 -1
  27. package/dist/errors/codes.js +5 -0
  28. package/dist/errors/codes.js.map +1 -1
  29. package/dist/errors/license-error.d.ts +2 -0
  30. package/dist/errors/license-error.d.ts.map +1 -1
  31. package/dist/errors/license-error.js +13 -2
  32. package/dist/errors/license-error.js.map +1 -1
  33. package/dist/errors/throw-for-status.d.ts.map +1 -1
  34. package/dist/errors/throw-for-status.js +3 -1
  35. package/dist/errors/throw-for-status.js.map +1 -1
  36. package/dist/index.d.ts +0 -2
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +0 -2
  39. package/dist/index.js.map +1 -1
  40. package/dist/types/status.d.ts +1 -1
  41. package/dist/types/status.d.ts.map +1 -1
  42. package/docs/api-reference.md +261 -0
  43. package/docs/installation.md +188 -0
  44. package/docs/integration.md +789 -0
  45. package/package.json +6 -4
@@ -0,0 +1,188 @@
1
+ # Cài đặt
2
+
3
+ Hướng dẫn cài package, cấu hình môi trường và chạy `boot()` lần đầu.
4
+
5
+ ---
6
+
7
+ ## 1. Cài package
8
+
9
+ ```bash
10
+ npm install @corelicense/node
11
+ ```
12
+
13
+ ### Peer dependencies (tùy framework)
14
+
15
+ Chỉ cài khi dùng integration tương ứng:
16
+
17
+ ```bash
18
+ npm install express # Express middleware
19
+ npm install fastify # Fastify plugin
20
+ npm install @nestjs/common @nestjs/core # NestJS
21
+ ```
22
+
23
+ SDK không bắt buộc peer deps — tree-shake an toàn nếu chỉ dùng `CoreLicense` trực tiếp.
24
+
25
+ ---
26
+
27
+ ## 2. Biến môi trường
28
+
29
+ ### Bắt buộc
30
+
31
+ | Biến | Mô tả |
32
+ |------|--------|
33
+ | `CORELICENSE_KEY` | License key do admin cấp (`CL-...` hoặc `CL1.<base64url-api>.CL-...`) |
34
+ | `CORELICENSE_PRODUCT_ID` | Product ID trên License Server |
35
+
36
+ ### Tùy chọn
37
+
38
+ | Biến | Mô tả | Mặc định |
39
+ |------|--------|----------|
40
+ | `CORELICENSE_API_URL` | Override URL License API | `https://api.corelicense.net` |
41
+ | `CORELICENSE_PUBLIC_KEY` | Pin public key (SPKI PEM) — bỏ qua fetch keys | auto-fetch |
42
+
43
+ **Thứ tự resolve API URL** (không cần set thủ công trên production):
44
+
45
+ 1. `options.apiUrl` / `CORELICENSE_API_URL`
46
+ 2. URL nhúng trong key `CL1.<base64url>.CL-...`
47
+ 3. Cache `sdk.endpoint.json`
48
+ 4. `https://api.corelicense.net`
49
+
50
+ ### Dev local
51
+
52
+ ```env
53
+ CORELICENSE_KEY=CL-AAAAA-BBBBB-CCCCC-DDDDD-EEEEE
54
+ CORELICENSE_PRODUCT_ID=demo
55
+ CORELICENSE_API_URL=http://localhost:8080
56
+ ```
57
+
58
+ ### Production
59
+
60
+ ```env
61
+ CORELICENSE_KEY=CL1.xxxxx.CL-AAAAA-BBBBB-...
62
+ CORELICENSE_PRODUCT_ID=my-product
63
+ # Không cần CORELICENSE_API_URL — SDK dùng api.corelicense.net
64
+ ```
65
+
66
+ Server Go production cần `SDK_PUBLIC_URL=https://api.corelicense.net` khi generate key `CL1.*`.
67
+
68
+ ---
69
+
70
+ ## 3. Khởi tạo tối thiểu
71
+
72
+ ```ts
73
+ import { CoreLicense, loadOptionsFromEnv } from '@corelicense/node';
74
+
75
+ const license = new CoreLicense(
76
+ loadOptionsFromEnv({
77
+ cachePath: './storage/corelicense',
78
+ instance: {
79
+ strategy: 'install-id-domain-hash',
80
+ domain: process.env.APP_DOMAIN, // ví dụ: app.example.com
81
+ },
82
+ audit: {
83
+ enabled: true,
84
+ path: './storage/logs/corelicense.log',
85
+ },
86
+ }),
87
+ );
88
+
89
+ await license.boot();
90
+ ```
91
+
92
+ `loadOptionsFromEnv()` throw `ConfigError` (`CONFIG_MISSING_ENV`) nếu thiếu biến bắt buộc.
93
+
94
+ ---
95
+
96
+ ## 4. `CoreLicenseOptions` (constructor)
97
+
98
+ | Field | Type | Mặc định | Mô tả |
99
+ |-------|------|----------|--------|
100
+ | `productId` | `string` | — | **Bắt buộc** |
101
+ | `licenseKey` | `string` | — | **Bắt buộc** |
102
+ | `apiUrl` | `string` | auto | License Server base URL |
103
+ | `publicKey` | `string` | — | Pin SPKI PEM |
104
+ | `cachePath` | `string` | `./storage/corelicense` | Thư mục cache |
105
+ | `checkIntervalMs` | `number` | `21600000` (6h) | Background refresh; `0` = tắt |
106
+ | `requestTimeoutMs` | `number` | `5000` | HTTP timeout |
107
+ | `offlineGrace` | `boolean` | `true` | Cho phép dùng cache khi server unreachable |
108
+ | `defaultBlockMode` | `'block' \| 'limited'` | `'limited'` | Middleware mode mặc định |
109
+ | `instance.strategy` | `'install-id-domain-hash' \| 'custom'` | `'install-id-domain-hash'` | Cách tính instance ID |
110
+ | `instance.domain` | `string` | — | Domain app (cho hash) |
111
+ | `instance.customId` | `string` | — | Khi `strategy: 'custom'` |
112
+ | `audit.enabled` | `boolean` | `true` | Ghi audit local |
113
+ | `audit.path` | `string` | `./storage/logs/corelicense.log` | File audit |
114
+ | `appVersion` | `string` | — | Gửi kèm SDK info lên server |
115
+
116
+ ---
117
+
118
+ ## 5. Luồng `boot()`
119
+
120
+ 1. Resolve API URL (pin / cache / env / default)
121
+ 2. Tính **instance ID** (install-id + domain hash)
122
+ 3. Fetch hoặc dùng **pinned public key**
123
+ 4. Load policy token từ cache → verify
124
+ 5. Nếu cần: `activate` (lần đầu) hoặc `check` (refresh)
125
+ 6. Bật background checker (nếu `checkIntervalMs > 0`)
126
+ 7. **`requireActive()`** — fail-closed nếu license không active
127
+
128
+ **Lưu ý v0.5:** Offline + grace hết hạn → `boot()` **throw**, không cho app chạy “im lặng”.
129
+
130
+ ---
131
+
132
+ ## 6. Pin public key (enterprise)
133
+
134
+ ```ts
135
+ const license = new CoreLicense(
136
+ loadOptionsFromEnv({
137
+ publicKey: process.env.CORELICENSE_PUBLIC_KEY,
138
+ }),
139
+ );
140
+ ```
141
+
142
+ Khi pin, SDK **không** gọi `GET /v1/public/products/{id}/keys`. Sau key rotation trên server, cập nhật `CORELICENSE_PUBLIC_KEY` và restart app.
143
+
144
+ ---
145
+
146
+ ## 7. File trên disk
147
+
148
+ ```
149
+ storage/corelicense/
150
+ instance.json # instanceId đã resolve
151
+ policy.token # JWT policy (signed)
152
+ usage.json # quota usage theo ngày
153
+ sdk.endpoint.json # apiUrl tin cậy từ server
154
+
155
+ storage/logs/
156
+ corelicense.log # audit events (JSON lines)
157
+ ```
158
+
159
+ Thêm `storage/` vào `.gitignore` của app khách.
160
+
161
+ ---
162
+
163
+ ## 8. Kiểm tra sau cài
164
+
165
+ ```ts
166
+ const status = await license.getStatus();
167
+ console.log(status); // { status: 'active', ... }
168
+
169
+ const policy = await license.getPolicy();
170
+ console.log(policy?.features);
171
+ ```
172
+
173
+ HTTP health route (không qua license guard):
174
+
175
+ ```ts
176
+ app.get('/health', (_req, res) => res.json({ ok: true }));
177
+ app.get('/license/status', async (_req, res) => {
178
+ res.json(await license.getStatus());
179
+ });
180
+ ```
181
+
182
+ ---
183
+
184
+ ## 9. Bước tiếp theo
185
+
186
+ → [Tích hợp chi tiết](./integration.md) — Express, NestJS, module registry, DI, worker, production checklist.
187
+
188
+ → [API Reference](./api-reference.md) — danh sách method và error codes.