@bieberkit/plugin-privacy 0.0.1 → 0.6.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/CHANGELOG.md ADDED
@@ -0,0 +1,48 @@
1
+ <!--
2
+ BieberKit
3
+
4
+ @file plugins/privacy/CHANGELOG.md
5
+ @description Release history generated from changesets for @bieberkit/plugin-privacy.
6
+ @author Bieber Kieu <kieutrongthien@gmail.com>
7
+ @generated pnpm changeset version (license header normalized)
8
+ @copyright Copyright (c) 2026 Bieber Kieu
9
+ @license Apache-2.0
10
+
11
+ Licensed under the Apache License, Version 2.0 (the "License");
12
+ you may not use this file except in compliance with the License.
13
+ You may obtain a copy of the License at
14
+
15
+ http://www.apache.org/licenses/LICENSE-2.0
16
+
17
+ Unless required by applicable law or agreed to in writing, software
18
+ distributed under the License is distributed on an "AS IS" BASIS,
19
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ See the License for the specific language governing permissions and
21
+ limitations under the License.
22
+
23
+ SPDX-License-Identifier: Apache-2.0
24
+ -->
25
+
26
+ # @bieberkit/plugin-privacy
27
+
28
+ ## 0.6.0
29
+
30
+ ### Minor Changes
31
+
32
+ - 302267c: Add optional-script consent to generated public sites and store decisions in the privacy plugin. Analytics and marketing choices are independent and can be withdrawn. Configure script URLs in `apps/site/src/optional-scripts.ts`.
33
+
34
+ ### Patch Changes
35
+
36
+ - Updated dependencies [b27ba2d]
37
+ - Updated dependencies [d69343e]
38
+ - Updated dependencies [4958f26]
39
+ - Updated dependencies [6bf1a53]
40
+ - Updated dependencies [594f5db]
41
+ - Updated dependencies [ab624de]
42
+ - Updated dependencies [f1811d7]
43
+ - Updated dependencies [a9ecbb2]
44
+ - Updated dependencies [9ee9a18]
45
+ - Updated dependencies [8b309a4]
46
+ - @bieberkit/http@0.6.0
47
+ - @bieberkit/core@0.6.0
48
+ - @bieberkit/data@0.6.0
package/README.md CHANGED
@@ -1,3 +1,31 @@
1
+ <!--
2
+ BieberKit
3
+
4
+ @file plugins/privacy/README.md
5
+ @description Describes the visitor consent API of the privacy plugin.
6
+ @author Bieber Kieu <kieutrongthien@gmail.com>
7
+ @copyright Copyright (c) 2026 Bieber Kieu
8
+ @license Apache-2.0
9
+
10
+ Licensed under the Apache License, Version 2.0 (the "License");
11
+ you may not use this file except in compliance with the License.
12
+ You may obtain a copy of the License at
13
+
14
+ http://www.apache.org/licenses/LICENSE-2.0
15
+
16
+ Unless required by applicable law or agreed to in writing, software
17
+ distributed under the License is distributed on an "AS IS" BASIS,
18
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ See the License for the specific language governing permissions and
20
+ limitations under the License.
21
+
22
+ SPDX-License-Identifier: Apache-2.0
23
+ -->
24
+
1
25
  # @bieberkit/plugin-privacy
2
26
 
3
- Reserved for the first BieberKit release of this package. The release will be available under `beta`.
27
+ Records analytics and marketing consent by purpose, source and time. The browser can read or withdraw its choices through the same-origin API. Generated sites configure their optional scripts in `apps/site/src/optional-scripts.ts`. See ADR 0051.
28
+
29
+ ## License
30
+
31
+ Apache License 2.0. Copyright 2026 Bieber Kieu.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * BieberKit
3
+ *
4
+ * @file plugins/privacy/src/index.ts
5
+ * @description Declares the privacy plugin and its consent endpoints.
6
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
7
+ * @copyright Copyright (c) 2026 Bieber Kieu
8
+ * @license Apache-2.0
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ *
22
+ * SPDX-License-Identifier: Apache-2.0
23
+ */
24
+ import { PluginDef } from "@bieberkit/core";
25
+ declare const privacy: PluginDef;
26
+ export { privacy as default };
package/dist/index.js ADDED
@@ -0,0 +1,250 @@
1
+ import { definePlugin } from "@bieberkit/core";
2
+ import { defineEntity, f } from "@bieberkit/data";
3
+ import { HttpError, defineRoute } from "@bieberkit/http";
4
+ import { createHash, randomBytes } from "node:crypto";
5
+ import { runInTransaction, runInUnitOfWork } from "@bieberkit/data/orm";
6
+ // package.json
7
+ var version = "0.6.0";
8
+ // src/entities/consent.ts
9
+ /**
10
+ * BieberKit
11
+ *
12
+ * @file plugins/privacy/src/entities/consent.ts
13
+ * @description Defines the ledger of visitor consent decisions.
14
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
15
+ * @copyright Copyright (c) 2026 Bieber Kieu
16
+ * @license Apache-2.0
17
+ *
18
+ * Licensed under the Apache License, Version 2.0 (the "License");
19
+ * you may not use this file except in compliance with the License.
20
+ * You may obtain a copy of the License at
21
+ *
22
+ * http://www.apache.org/licenses/LICENSE-2.0
23
+ *
24
+ * Unless required by applicable law or agreed to in writing, software
25
+ * distributed under the License is distributed on an "AS IS" BASIS,
26
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
+ * See the License for the specific language governing permissions and
28
+ * limitations under the License.
29
+ *
30
+ * SPDX-License-Identifier: Apache-2.0
31
+ */
32
+ const Consent = defineEntity({
33
+ name: "privacy.Consent",
34
+ table: "bk_privacy_consents",
35
+ fields: {
36
+ id: f.id(),
37
+ visitor: f.string({
38
+ min: 64,
39
+ max: 64
40
+ }).index().pii(),
41
+ analytics: f.boolean(),
42
+ marketing: f.boolean(),
43
+ source: f.oneOf(["banner", "preferences"]),
44
+ createdAt: f.createdAt()
45
+ }
46
+ });
47
+ // src/services/consent.ts
48
+ /**
49
+ * BieberKit
50
+ *
51
+ * @file plugins/privacy/src/services/consent.ts
52
+ * @description Reads and stores consent decisions by the digest of a visitor token.
53
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
54
+ * @copyright Copyright (c) 2026 Bieber Kieu
55
+ * @license Apache-2.0
56
+ *
57
+ * Licensed under the Apache License, Version 2.0 (the "License");
58
+ * you may not use this file except in compliance with the License.
59
+ * You may obtain a copy of the License at
60
+ *
61
+ * http://www.apache.org/licenses/LICENSE-2.0
62
+ *
63
+ * Unless required by applicable law or agreed to in writing, software
64
+ * distributed under the License is distributed on an "AS IS" BASIS,
65
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
66
+ * See the License for the specific language governing permissions and
67
+ * limitations under the License.
68
+ *
69
+ * SPDX-License-Identifier: Apache-2.0
70
+ */
71
+ const COOKIE = "__Host-bk_consent=";
72
+ const TOKEN = /^[A-Za-z0-9_-]{43}$/;
73
+ const unanswered = {
74
+ answered: false,
75
+ analytics: false,
76
+ marketing: false
77
+ };
78
+ function tokenOf(cookie) {
79
+ if (typeof cookie !== "string") return void 0;
80
+ const value = cookie.split(";").map((part) => part.trim()).find((part) => part.startsWith(COOKIE))?.slice(18);
81
+ return value !== void 0 && TOKEN.test(value) ? value : void 0;
82
+ }
83
+ const visitorOf = (token) => createHash("sha256").update(token).digest("hex");
84
+ async function readConsent(db, cookie) {
85
+ const token = tokenOf(cookie);
86
+ if (token === void 0) return unanswered;
87
+ return runInUnitOfWork(db, async (em) => {
88
+ const entity = em.getMetadata().getByClassName(Consent.name).class;
89
+ const row = await em.findOne(entity, { visitor: visitorOf(token) }, { orderBy: {
90
+ createdAt: "desc",
91
+ id: "desc"
92
+ } });
93
+ if (typeof row !== "object" || row === null) return unanswered;
94
+ return {
95
+ answered: true,
96
+ analytics: Reflect.get(row, "analytics") === true,
97
+ marketing: Reflect.get(row, "marketing") === true
98
+ };
99
+ });
100
+ }
101
+ async function storeConsent(db, cookie, change) {
102
+ const existing = tokenOf(cookie);
103
+ const token = existing ?? randomBytes(32).toString("base64url");
104
+ await runInTransaction(db, async (em) => {
105
+ const entity = em.getMetadata().getByClassName(Consent.name).class;
106
+ em.create(entity, {
107
+ visitor: visitorOf(token),
108
+ ...change
109
+ });
110
+ await em.flush();
111
+ });
112
+ return {
113
+ state: {
114
+ answered: true,
115
+ analytics: change.analytics,
116
+ marketing: change.marketing
117
+ },
118
+ setCookie: existing === void 0 ? COOKIE + token + "; Path=/; Max-Age=31536000; HttpOnly; Secure; SameSite=Lax" : void 0
119
+ };
120
+ }
121
+ // src/routes/index.ts
122
+ /**
123
+ * BieberKit
124
+ *
125
+ * @file plugins/privacy/src/routes/index.ts
126
+ * @description Serves private consent reads and updates through the site API.
127
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
128
+ * @copyright Copyright (c) 2026 Bieber Kieu
129
+ * @license Apache-2.0
130
+ *
131
+ * Licensed under the Apache License, Version 2.0 (the "License");
132
+ * you may not use this file except in compliance with the License.
133
+ * You may obtain a copy of the License at
134
+ *
135
+ * http://www.apache.org/licenses/LICENSE-2.0
136
+ *
137
+ * Unless required by applicable law or agreed to in writing, software
138
+ * distributed under the License is distributed on an "AS IS" BASIS,
139
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
140
+ * See the License for the specific language governing permissions and
141
+ * limitations under the License.
142
+ *
143
+ * SPDX-License-Identifier: Apache-2.0
144
+ */
145
+ const consent = "/api/v1/privacy/consent";
146
+ const response = { 200: {
147
+ type: "object",
148
+ properties: { data: {
149
+ type: "object",
150
+ properties: {
151
+ answered: { type: "boolean" },
152
+ analytics: { type: "boolean" },
153
+ marketing: { type: "boolean" }
154
+ },
155
+ required: [
156
+ "answered",
157
+ "analytics",
158
+ "marketing"
159
+ ]
160
+ } },
161
+ required: ["data"]
162
+ } };
163
+ function choiceOf(input) {
164
+ if (typeof input === "object" && input !== null && "analytics" in input && typeof input.analytics === "boolean" && "marketing" in input && typeof input.marketing === "boolean" && "source" in input && (input.source === "banner" || input.source === "preferences")) return {
165
+ analytics: input.analytics,
166
+ marketing: input.marketing,
167
+ source: input.source
168
+ };
169
+ throw new HttpError("CONSENT_INVALID", 400, "The consent choices are invalid.", "Send the choices for both purposes.");
170
+ }
171
+ const routes = [defineRoute({
172
+ method: "GET",
173
+ url: consent,
174
+ public: true,
175
+ schema: { response },
176
+ handler: async (ctx) => {
177
+ ctx.header("cache-control", "private, no-store");
178
+ return { data: await readConsent(ctx.services.database, ctx.headers["cookie"]) };
179
+ }
180
+ }), defineRoute({
181
+ method: "POST",
182
+ url: consent,
183
+ public: true,
184
+ schema: {
185
+ body: {
186
+ type: "object",
187
+ additionalProperties: false,
188
+ properties: {
189
+ analytics: { type: "boolean" },
190
+ marketing: { type: "boolean" },
191
+ source: {
192
+ type: "string",
193
+ enum: ["banner", "preferences"]
194
+ }
195
+ },
196
+ required: [
197
+ "analytics",
198
+ "marketing",
199
+ "source"
200
+ ]
201
+ },
202
+ response
203
+ },
204
+ handler: async (ctx) => {
205
+ const from = ctx.headers["sec-fetch-site"];
206
+ if (from !== void 0 && from !== "same-origin") throw new HttpError("CONSENT_ORIGIN", 403, "Consent must be changed from this site.", "Open the site and try again.");
207
+ const result = await storeConsent(ctx.services.database, ctx.headers["cookie"], choiceOf(ctx.body));
208
+ ctx.header("cache-control", "private, no-store");
209
+ if (result.setCookie !== void 0) ctx.header("set-cookie", result.setCookie);
210
+ return { data: result.state };
211
+ }
212
+ })];
213
+ // src/index.ts
214
+ /**
215
+ * BieberKit
216
+ *
217
+ * @file plugins/privacy/src/index.ts
218
+ * @description Declares the privacy plugin and its consent endpoints.
219
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
220
+ * @copyright Copyright (c) 2026 Bieber Kieu
221
+ * @license Apache-2.0
222
+ *
223
+ * Licensed under the Apache License, Version 2.0 (the "License");
224
+ * you may not use this file except in compliance with the License.
225
+ * You may obtain a copy of the License at
226
+ *
227
+ * http://www.apache.org/licenses/LICENSE-2.0
228
+ *
229
+ * Unless required by applicable law or agreed to in writing, software
230
+ * distributed under the License is distributed on an "AS IS" BASIS,
231
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
232
+ * See the License for the specific language governing permissions and
233
+ * limitations under the License.
234
+ *
235
+ * SPDX-License-Identifier: Apache-2.0
236
+ */
237
+ const privacy = definePlugin({
238
+ name: "@bieberkit/plugin-privacy",
239
+ version,
240
+ core: "*",
241
+ dialects: [
242
+ "postgres",
243
+ "mysql",
244
+ "sqlite"
245
+ ],
246
+ permissions: [],
247
+ entities: [Consent],
248
+ routes
249
+ });
250
+ export { privacy as default };
package/package.json CHANGED
@@ -1,19 +1,72 @@
1
1
  {
2
2
  "name": "@bieberkit/plugin-privacy",
3
- "version": "0.0.1",
4
- "description": "Reserved for the first BieberKit release of this package.",
3
+ "version": "0.6.0",
4
+ "description": "Records and withdraws consent for optional public site scripts.",
5
+ "keywords": [
6
+ "bieberkit",
7
+ "bieberkit-plugin"
8
+ ],
9
+ "homepage": "https://github.com/kieutrongthien/bieberkit#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/kieutrongthien/bieberkit/issues"
12
+ },
5
13
  "license": "Apache-2.0",
6
14
  "author": "Bieber Kieu <kieutrongthien@gmail.com>",
7
- "homepage": "https://github.com/kieutrongthien/bieberkit#readme",
8
15
  "repository": {
9
16
  "type": "git",
10
17
  "url": "git+https://github.com/kieutrongthien/bieberkit.git",
11
18
  "directory": "plugins/privacy"
12
19
  },
13
20
  "files": [
14
- "NOTICE"
21
+ "dist",
22
+ "NOTICE",
23
+ "CHANGELOG.md"
15
24
  ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ },
32
+ "./package.json": "./package.json"
33
+ },
16
34
  "publishConfig": {
17
- "registry": "https://registry.npmjs.org/"
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "@bieberkit/core": "0.6.0",
39
+ "@bieberkit/data": "0.6.0",
40
+ "@bieberkit/http": "0.6.0"
41
+ },
42
+ "devDependencies": {
43
+ "@bieberkit/testing": "0.6.0"
44
+ },
45
+ "engines": {
46
+ "node": ">=24.12"
47
+ },
48
+ "bieberkit": {
49
+ "core": "*",
50
+ "dialects": [
51
+ "mysql",
52
+ "postgres",
53
+ "sqlite"
54
+ ],
55
+ "requires": {},
56
+ "conflicts": [],
57
+ "env": [],
58
+ "tables": [
59
+ "bk_privacy_consents"
60
+ ],
61
+ "routes": [
62
+ "GET /api/v1/privacy/consent",
63
+ "POST /api/v1/privacy/consent"
64
+ ],
65
+ "permissions": [],
66
+ "admin": []
67
+ },
68
+ "scripts": {
69
+ "build": "tsdown && node --conditions=@bieberkit/source ../../tools/plugin-manifests.ts --write --plugin .",
70
+ "typecheck": "tsc --noEmit -p tsconfig.json"
18
71
  }
19
72
  }