@paywalls-net/filter 1.2.2 → 1.2.3

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 +1 -1
  2. package/src/index.js +30 -1
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Client SDK for integrating paywalls.net bot filtering and authorization services into your server or CDN.",
4
4
  "author": "paywalls.net",
5
5
  "license": "MIT",
6
- "version": "1.2.2",
6
+ "version": "1.2.3",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
package/src/index.js CHANGED
@@ -2,11 +2,38 @@
2
2
  * Example publisher-hosted client code for a Cloudflare Worker that
3
3
  * filters bot-like requests by using paywalls.net authorization services.
4
4
  */
5
-
5
+ const sdk_version = "1.2.x";
6
6
  import { classifyUserAgent, loadAgentPatterns } from './user-agent-classification.js';
7
7
 
8
8
  const PAYWALLS_CLOUD_API_HOST = "https://cloud-api.paywalls.net";
9
9
 
10
+ function detectRuntime() {
11
+ if (typeof process !== "undefined" && process.versions && process.versions.node) {
12
+ return `Node.js/${process.versions.node}`;
13
+ } else if (typeof navigator !== "undefined" && navigator.userAgent) {
14
+ return `Browser/${navigator.userAgent}`;
15
+ } else if (typeof globalThis !== "undefined" && globalThis.Deno && Deno.version) {
16
+ return `Deno/${Deno.version.deno}`;
17
+ } else if (typeof globalThis !== "undefined" && globalThis.Bun && Bun.version) {
18
+ return `Bun/${Bun.version}`;
19
+ }
20
+ return "unknown";
21
+ }
22
+
23
+ function detectFetchVersion() {
24
+ if (typeof fetch !== "undefined" && fetch.name) {
25
+ return fetch.name;
26
+ } else if (typeof globalThis !== "undefined" && globalThis.fetch) {
27
+ return "native";
28
+ } else {
29
+ return "unavailable";
30
+ }
31
+ }
32
+
33
+ let runtime = detectRuntime();
34
+ let fetchVersion = detectFetchVersion();
35
+ const sdkUserAgent = `pw-filter-sdk/${sdk_version} (${runtime}; fetch/${fetchVersion})`;
36
+
10
37
  async function logAccess(cfg, request, access) {
11
38
  // Separate html from the status in the access object.
12
39
  const { response, ...status } = access;
@@ -40,6 +67,7 @@ async function logAccess(cfg, request, access) {
40
67
  method: "POST",
41
68
  headers: {
42
69
  "Content-Type": "application/json",
70
+ "User-Agent": sdkUserAgent,
43
71
  Authorization: `Bearer ${cfg.paywallsAPIKey}`
44
72
  },
45
73
  body: JSON.stringify(body)
@@ -93,6 +121,7 @@ async function checkAgentStatus(cfg, request) {
93
121
  method: "POST",
94
122
  headers: {
95
123
  "Content-Type": "application/json",
124
+ "User-Agent": sdkUserAgent,
96
125
  Authorization: `Bearer ${cfg.paywallsAPIKey}`
97
126
  },
98
127
  body: body