@ciphera-net/pulse-docusaurus 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/LICENSE ADDED
@@ -0,0 +1,203 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
203
+
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Pulse Analytics for Docusaurus
2
+
3
+ Privacy-first analytics for a Docusaurus site, in one plugin. No cookies, no
4
+ personal data, and the script it adds to your pages is under 3 KB.
5
+
6
+ [Pulse Analytics](https://pulse.ciphera.net) is built by [Ciphera](https://ciphera.net), a
7
+ company in Belgium, and hosted in Europe.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @ciphera-net/pulse-docusaurus
13
+ ```
14
+
15
+ ```js
16
+ // docusaurus.config.js
17
+ module.exports = {
18
+ url: 'https://example.com',
19
+ plugins: [
20
+ '@ciphera-net/pulse-docusaurus',
21
+ ],
22
+ }
23
+ ```
24
+
25
+ That is the whole setup. The domain comes from your site's `url` config, which
26
+ Docusaurus already requires you to set, so there is nothing else to configure.
27
+
28
+ To pass options, use the array form:
29
+
30
+ ```js
31
+ plugins: [
32
+ ['@ciphera-net/pulse-docusaurus', { companion: true }],
33
+ ],
34
+ ```
35
+
36
+ You need a Pulse Analytics account with a site registered for the same domain.
37
+ The free plan is enough to start.
38
+
39
+ ## Options
40
+
41
+ | Option | Default | What it does |
42
+ |---|---|---|
43
+ | `domain` | from `url` | The domain your site is registered under in Pulse Analytics. Pass this when `url` is not the domain you track, for example on a preview deployment. |
44
+ | `companion` | `false` | Also record clicks, copies and form submits. A second, separate script — see below. |
45
+ | `api` | — | Route events through your own proxy origin. A bare origin (`https://example.com`), not a full URL: the tracker appends its own path. |
46
+ | `injectInDev` | `false` | Inject during `docusaurus start` too. Off by default — see below. |
47
+
48
+ ## Three things worth knowing
49
+
50
+ **It does not run in `docusaurus start`.** The tracker has no localhost guard,
51
+ so a dev server would send real pageviews at your production dashboard. The
52
+ plugin disables itself outside a production build, exactly as Docusaurus's own
53
+ `@docusaurus/plugin-google-gtag` does. Set `injectInDev: true` if you actually
54
+ want it.
55
+
56
+ **The interaction capture is a second request on purpose.** The core script's
57
+ size is a published claim, and nothing gets folded into it to make a feature
58
+ look free. `companion: true` costs you a second, separate file.
59
+
60
+ **View-source shows the real tag, and there is no inline script.** This is worth
61
+ saying because it is not true of every Pulse integration — the Astro one and the
62
+ Google Tag Manager template both have to build the element at runtime, because
63
+ neither platform's injection API can carry attributes. Docusaurus's
64
+ `injectHtmlTags` serialises attributes directly, so what lands in your built HTML is:
65
+
66
+ ```html
67
+ <script defer="defer" data-domain="example.com" src="https://js.ciphera.net/script.js"></script>
68
+ ```
69
+
70
+ the same tag you would paste by hand. (The plugin emits a bare `defer`;
71
+ Docusaurus's HTML minifier re-serialises every boolean attribute into its
72
+ `name="name"` form, which it also does to Docusaurus's own script tags. The two
73
+ are identical in HTML5, where any value means true. Build with
74
+ `SKIP_HTML_MINIFICATION=true` and you will see the bare form.)
75
+
76
+ **A strict Content-Security-Policy needs no `unsafe-inline` and no nonce for
77
+ this plugin** — only `script-src https://js.ciphera.net` and
78
+ `connect-src https://pulse-api.ciphera.net`.
79
+
80
+ ## A mistyped domain fails the build
81
+
82
+ `domain: "exmaple .com"` throws at build time rather than quietly falling back to
83
+ auto-detection. A typo there is an install that reports no data and looks fine,
84
+ which is the worst outcome an analytics plugin can produce, so it is worth a red
85
+ build instead.
86
+
87
+ If you set no `domain` **and** no `url`, the plugin warns and lets the tracker
88
+ auto-detect the browser's hostname — correct for a single-domain site, wrong for
89
+ a site served on several.
90
+
91
+ ## What Pulse Analytics measures
92
+
93
+ Pageviews, referrers, countries, devices, time on page and scroll depth, plus
94
+ goals, funnels and campaigns. It sets no cookies and stores no personal data.
95
+ Visitors whose browser sends Do Not Track or Global Privacy Control are not
96
+ counted at all — so if you test in Brave or Firefox and see nothing, that is the
97
+ tracker behaving correctly. Safari is the easiest browser to verify an install in.
98
+
99
+ ## Compatibility
100
+
101
+ `peerDependencies` claims what has actually been measured, not what seems
102
+ likely: each release is verified by building a real Docusaurus site and reading
103
+ the tag back out of the emitted HTML (`node scripts/verify-build.mjs 3.10.2 3.0.1`).
104
+ See `RELEASING.md` for the table.
105
+
106
+ ## Licence
107
+
108
+ Copyright 2026 Ciphera BV. Licensed under the Apache License, Version 2.0 —
109
+ the `LICENSE` file is the licence text verbatim, so the copyright line lives
110
+ here rather than inside it.
111
+
112
+ This package has **zero runtime dependencies**, asserted in CI.
113
+
114
+ Docusaurus is a trademark of Meta Platforms, Inc.; this is an independent plugin
115
+ and is not affiliated with or endorsed by Meta or the Docusaurus project.
package/lib/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { LoadContext, Plugin } from "@docusaurus/types";
2
+ import { type PulseOptions } from "./pulse";
3
+ export type { PulseOptions };
4
+ export { SCRIPT_URL, COMPANION_URL, normalizeDomain, isValidDomain, hostnameFromUrl, buildHeadTags, resolveDomain, } from "./pulse";
5
+ /**
6
+ * Pulse Analytics — privacy-first web analytics for Docusaurus.
7
+ *
8
+ * // docusaurus.config.js
9
+ * plugins: [
10
+ * ['@ciphera-net/pulse-docusaurus', { domain: 'example.com' }],
11
+ * ]
12
+ *
13
+ * The domain comes from the site's own `url` config unless you pass one.
14
+ */
15
+ export default function pulseAnalytics(context: LoadContext, options?: PulseOptions): Plugin | null;
package/lib/index.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveDomain = exports.buildHeadTags = exports.hostnameFromUrl = exports.isValidDomain = exports.normalizeDomain = exports.COMPANION_URL = exports.SCRIPT_URL = void 0;
4
+ exports.default = pulseAnalytics;
5
+ const pulse_1 = require("./pulse");
6
+ var pulse_2 = require("./pulse");
7
+ Object.defineProperty(exports, "SCRIPT_URL", { enumerable: true, get: function () { return pulse_2.SCRIPT_URL; } });
8
+ Object.defineProperty(exports, "COMPANION_URL", { enumerable: true, get: function () { return pulse_2.COMPANION_URL; } });
9
+ Object.defineProperty(exports, "normalizeDomain", { enumerable: true, get: function () { return pulse_2.normalizeDomain; } });
10
+ Object.defineProperty(exports, "isValidDomain", { enumerable: true, get: function () { return pulse_2.isValidDomain; } });
11
+ Object.defineProperty(exports, "hostnameFromUrl", { enumerable: true, get: function () { return pulse_2.hostnameFromUrl; } });
12
+ Object.defineProperty(exports, "buildHeadTags", { enumerable: true, get: function () { return pulse_2.buildHeadTags; } });
13
+ Object.defineProperty(exports, "resolveDomain", { enumerable: true, get: function () { return pulse_2.resolveDomain; } });
14
+ /** Compile-time proof that the tag shape `pulse.ts` builds — which is declared
15
+ * there without importing Docusaurus, so the logic stays unit-testable — is
16
+ * still the shape Docusaurus accepts. If their type moves, this reddens the
17
+ * build instead of shipping a tag Docusaurus will reject at site-build time. */
18
+ const _tagShapeMatches = (t) => t;
19
+ void _tagShapeMatches;
20
+ /**
21
+ * Pulse Analytics — privacy-first web analytics for Docusaurus.
22
+ *
23
+ * // docusaurus.config.js
24
+ * plugins: [
25
+ * ['@ciphera-net/pulse-docusaurus', { domain: 'example.com' }],
26
+ * ]
27
+ *
28
+ * The domain comes from the site's own `url` config unless you pass one.
29
+ */
30
+ function pulseAnalytics(context, options = {}) {
31
+ // The tracker has no localhost guard, so `docusaurus start` would send real
32
+ // pageviews at production. Returning null is the sanctioned way for a plugin
33
+ // to self-disable (facebook/docusaurus#10286) and is what
34
+ // `@docusaurus/plugin-google-gtag` does; returning undefined would throw.
35
+ const isProd = process.env.NODE_ENV === "production";
36
+ if (!isProd && !options.injectInDev)
37
+ return null;
38
+ // Throws on an explicitly supplied domain that cannot be a hostname. A typo
39
+ // there is a silent no-data install, which is worth a build error.
40
+ const { domain, source } = (0, pulse_1.resolveDomain)(options, context.siteConfig?.url);
41
+ if (source === "browser") {
42
+ console.warn("[pulse-docusaurus] no `url` in docusaurus.config and no `domain` option — the tag will auto-detect the browser's hostname. Set one of them if this site is served on more than one domain.");
43
+ }
44
+ const headTags = (0, pulse_1.buildHeadTags)(domain, options);
45
+ return {
46
+ name: "pulse-analytics",
47
+ injectHtmlTags() {
48
+ return { headTags };
49
+ },
50
+ };
51
+ }
package/lib/pulse.d.ts ADDED
@@ -0,0 +1,70 @@
1
+ export declare const SCRIPT_URL = "https://js.ciphera.net/script.js";
2
+ export declare const COMPANION_URL = "https://js.ciphera.net/script.interactions.js";
3
+ /** Lower-case, drop a scheme, path, port and trailing dot. Keeps `www.` — the
4
+ * Pulse site may be registered either way and the user can override it. */
5
+ export declare function normalizeDomain(input: string): string;
6
+ export declare function isValidDomain(domain: string): boolean;
7
+ /** The hostname of Docusaurus's `url` config value, or null when it is unset or
8
+ * does not parse as a registrable domain. `url` is mandatory in a Docusaurus
9
+ * config, so this is the near-universal path — the Docusaurus equivalent of
10
+ * Astro's `site` and of the Framer plugin reading `getPublishInfo()`.
11
+ *
12
+ * `http://localhost:3000` deliberately returns null: `localhost` has no dot
13
+ * and no TLD, so it fails `isValidDomain` and the caller falls back to
14
+ * auto-detection instead of registering a site called "localhost". */
15
+ export declare function hostnameFromUrl(url: string | undefined | null): string | null;
16
+ export interface PulseOptions {
17
+ /** The domain the site is registered under in Pulse. Defaults to the
18
+ * hostname of Docusaurus's `url` config. If neither is available the tag is
19
+ * injected without `data-domain` and the tracker falls back to the browser's
20
+ * own hostname — which is correct for a single-domain site. */
21
+ domain?: string;
22
+ /** Also load the companion script, which records clicks, copies and form
23
+ * submits. A SECOND request on purpose: the core script's size is a
24
+ * published claim and nothing may be folded into it. */
25
+ companion?: boolean;
26
+ /** Route events through your own proxy origin. The tracker appends its own
27
+ * path, so this is a bare origin (`https://example.com`), not a full URL. */
28
+ api?: string;
29
+ /** Inject during `docusaurus start` as well. Off by default: the tracker has
30
+ * no localhost guard, so a dev server would send real pageviews at
31
+ * production. */
32
+ injectInDev?: boolean;
33
+ }
34
+ export type DomainSource = "option" | "url" | "browser";
35
+ export interface Resolution {
36
+ domain: string | null;
37
+ source: DomainSource;
38
+ }
39
+ /** Resolve the domain, saying WHERE it came from so the caller can log it.
40
+ * Never throws for a missing domain — a null domain is a working install that
41
+ * auto-detects — but an explicitly supplied domain that cannot be a hostname
42
+ * is a typo, and that does throw rather than silently auto-detect something
43
+ * the user did not ask for. */
44
+ export declare function resolveDomain(options: PulseOptions, siteUrl: string | undefined | null): Resolution;
45
+ /** The shape Docusaurus's `injectHtmlTags` accepts, declared here so this file
46
+ * stays free of Docusaurus imports. `src/index.ts` asserts at compile time
47
+ * that it is assignable to Docusaurus's own `HtmlTagObject`, so a drift in
48
+ * their type reddens the build rather than shipping. */
49
+ export interface HtmlTagObject {
50
+ tagName: string;
51
+ attributes: Record<string, string | boolean>;
52
+ }
53
+ /** Exactly the tag Pulse's install panel emits, as tag objects.
54
+ *
55
+ * 🔑 This is the one surface in the queue that can emit the LITERAL tag.
56
+ * Astro's `injectScript` takes JavaScript source and GTM's sandboxed
57
+ * `injectScript(url, …)` has no attribute parameter, so both have to build the
58
+ * element at runtime or configure through `window.pulseConfig`. Docusaurus
59
+ * serialises `{tagName, attributes}` straight into the HTML, honouring
60
+ * arbitrary `data-*` keys, so view-source here shows the real tag and there is
61
+ * no inline script — which means no CSP nonce and no `unsafe-inline`.
62
+ *
63
+ * Attribute ORDER is the canonical install order (`defer`, `data-domain`,
64
+ * `data-api`, `src`) because Docusaurus serialises `Object.keys` in insertion
65
+ * order, and a tag a user can diff against the docs is worth the care.
66
+ *
67
+ * No `preconnect` hints, deliberately — `@docusaurus/plugin-google-gtag` emits
68
+ * two, but Framer, Astro and GTM emit none, and one surface quietly performing
69
+ * differently from the others is exactly the drift this file exists to stop. */
70
+ export declare function buildHeadTags(domain: string | null, options?: PulseOptions): HtmlTagObject[];
package/lib/pulse.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ // Pure logic for the Pulse Docusaurus plugin: what tags to inject, and what
3
+ // domain to inject them with. No Docusaurus API in this file, so all of it is
4
+ // unit-tested.
5
+ //
6
+ // The decisions here deliberately mirror `pulse-astro/src/pulse.ts` and
7
+ // `pulse-framer/src/pulse.ts`. Two Pulse install surfaces disagreeing about
8
+ // what the tag looks like is a drift bug waiting to happen, so the URLs, the
9
+ // domain grammar and the rule that the companion is a SECOND script are
10
+ // copied, not re-derived.
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.COMPANION_URL = exports.SCRIPT_URL = void 0;
13
+ exports.normalizeDomain = normalizeDomain;
14
+ exports.isValidDomain = isValidDomain;
15
+ exports.hostnameFromUrl = hostnameFromUrl;
16
+ exports.resolveDomain = resolveDomain;
17
+ exports.buildHeadTags = buildHeadTags;
18
+ exports.SCRIPT_URL = "https://js.ciphera.net/script.js";
19
+ exports.COMPANION_URL = "https://js.ciphera.net/script.interactions.js";
20
+ /** A registrable hostname: labels of letters, digits and hyphens, at least one
21
+ * dot, a letter-only TLD. Deliberately strict — the value lands inside an HTML
22
+ * attribute, and this shape cannot carry a quote, a space or a bracket.
23
+ *
24
+ * Docusaurus escapes attribute values itself (`escape-html` in
25
+ * `core/lib/server/htmlTags.js`), so this is the second of two guards rather
26
+ * than the only one. It is kept anyway: a domain that cannot be a hostname is
27
+ * a typo, and a typo is a silent no-data install. */
28
+ const DOMAIN_RE = /^(?=.{1,253}$)(?!-)[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*\.[a-z]{2,63}$/;
29
+ /** Lower-case, drop a scheme, path, port and trailing dot. Keeps `www.` — the
30
+ * Pulse site may be registered either way and the user can override it. */
31
+ function normalizeDomain(input) {
32
+ let s = input.trim().toLowerCase();
33
+ s = s.replace(/^[a-z][a-z0-9+.-]*:\/\//, "");
34
+ s = s.replace(/[/?#].*$/, "");
35
+ s = s.replace(/:.*$/, "");
36
+ s = s.replace(/\.$/, "");
37
+ return s;
38
+ }
39
+ function isValidDomain(domain) {
40
+ return DOMAIN_RE.test(domain);
41
+ }
42
+ /** The hostname of Docusaurus's `url` config value, or null when it is unset or
43
+ * does not parse as a registrable domain. `url` is mandatory in a Docusaurus
44
+ * config, so this is the near-universal path — the Docusaurus equivalent of
45
+ * Astro's `site` and of the Framer plugin reading `getPublishInfo()`.
46
+ *
47
+ * `http://localhost:3000` deliberately returns null: `localhost` has no dot
48
+ * and no TLD, so it fails `isValidDomain` and the caller falls back to
49
+ * auto-detection instead of registering a site called "localhost". */
50
+ function hostnameFromUrl(url) {
51
+ if (!url)
52
+ return null;
53
+ try {
54
+ const host = new URL(String(url)).hostname.toLowerCase().replace(/\.$/, "");
55
+ return isValidDomain(host) ? host : null;
56
+ }
57
+ catch {
58
+ return null;
59
+ }
60
+ }
61
+ /** Resolve the domain, saying WHERE it came from so the caller can log it.
62
+ * Never throws for a missing domain — a null domain is a working install that
63
+ * auto-detects — but an explicitly supplied domain that cannot be a hostname
64
+ * is a typo, and that does throw rather than silently auto-detect something
65
+ * the user did not ask for. */
66
+ function resolveDomain(options, siteUrl) {
67
+ if (options.domain !== undefined) {
68
+ const d = normalizeDomain(options.domain);
69
+ if (!isValidDomain(d)) {
70
+ throw new Error(`[pulse-docusaurus] "${options.domain}" is not a valid domain. Pass the hostname your site is registered under in Pulse, e.g. domain: "example.com".`);
71
+ }
72
+ return { domain: d, source: "option" };
73
+ }
74
+ const fromUrl = hostnameFromUrl(siteUrl);
75
+ if (fromUrl)
76
+ return { domain: fromUrl, source: "url" };
77
+ return { domain: null, source: "browser" };
78
+ }
79
+ /** Exactly the tag Pulse's install panel emits, as tag objects.
80
+ *
81
+ * 🔑 This is the one surface in the queue that can emit the LITERAL tag.
82
+ * Astro's `injectScript` takes JavaScript source and GTM's sandboxed
83
+ * `injectScript(url, …)` has no attribute parameter, so both have to build the
84
+ * element at runtime or configure through `window.pulseConfig`. Docusaurus
85
+ * serialises `{tagName, attributes}` straight into the HTML, honouring
86
+ * arbitrary `data-*` keys, so view-source here shows the real tag and there is
87
+ * no inline script — which means no CSP nonce and no `unsafe-inline`.
88
+ *
89
+ * Attribute ORDER is the canonical install order (`defer`, `data-domain`,
90
+ * `data-api`, `src`) because Docusaurus serialises `Object.keys` in insertion
91
+ * order, and a tag a user can diff against the docs is worth the care.
92
+ *
93
+ * No `preconnect` hints, deliberately — `@docusaurus/plugin-google-gtag` emits
94
+ * two, but Framer, Astro and GTM emit none, and one surface quietly performing
95
+ * differently from the others is exactly the drift this file exists to stop. */
96
+ function buildHeadTags(domain, options = {}) {
97
+ if (domain !== null && !isValidDomain(domain)) {
98
+ throw new Error(`[pulse-docusaurus] refusing to inject an invalid domain: ${domain}`);
99
+ }
100
+ const attributes = { defer: true };
101
+ if (domain !== null)
102
+ attributes["data-domain"] = domain;
103
+ if (options.api)
104
+ attributes["data-api"] = String(options.api).replace(/\/+$/, "");
105
+ attributes["src"] = exports.SCRIPT_URL;
106
+ const tags = [{ tagName: "script", attributes }];
107
+ if (options.companion) {
108
+ // No `data-domain` on the companion: the tracker resolves the domain once,
109
+ // from the core script, and a second copy is a second thing to keep in step.
110
+ tags.push({ tagName: "script", attributes: { defer: true, src: exports.COMPANION_URL } });
111
+ }
112
+ return tags;
113
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@ciphera-net/pulse-docusaurus",
3
+ "version": "1.0.0",
4
+ "description": "Add Pulse Analytics \u2014 privacy-first web analytics \u2014 to a Docusaurus site with one plugin. No cookies, no personal data, under 3 KB.",
5
+ "license": "Apache-2.0",
6
+ "homepage": "https://pulse.ciphera.net",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ciphera-net/pulse-docusaurus.git"
10
+ },
11
+ "bugs": "https://github.com/ciphera-net/pulse-docusaurus/issues",
12
+ "keywords": [
13
+ "docusaurus",
14
+ "docusaurus-plugin",
15
+ "analytics",
16
+ "privacy",
17
+ "cookieless",
18
+ "gdpr",
19
+ "pulse"
20
+ ],
21
+ "main": "lib/index.js",
22
+ "types": "lib/index.d.ts",
23
+ "files": [
24
+ "lib",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.build.json",
30
+ "prepack": "npm run build",
31
+ "typecheck": "tsc --noEmit",
32
+ "test": "vitest run",
33
+ "check:package": "node scripts/check-package.mjs"
34
+ },
35
+ "peerDependencies": {
36
+ "@docusaurus/core": "^3.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@docusaurus/types": "^3.10.2",
40
+ "typescript": "^5.9.2",
41
+ "vitest": "^4.0.18"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "engines": {
47
+ "node": ">=20"
48
+ }
49
+ }