@vltpkg/user-agent 1.1.1

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,15 @@
1
+ Copyright (c) vlt technology, Inc.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ Subject to the terms and conditions of this license, each copyright holder and contributor hereby grants to those receiving rights under this license a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except for failure to satisfy the conditions of this license) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer this software, where such license applies only to those patent claims, already acquired or hereafter acquired, licensable by such copyright holder or contributor that are necessarily infringed by:
8
+
9
+ (a) their Contribution(s) (the licensed copyrights of copyright holders and non-copyrightable additions of contributors, in source or binary form) alone; or
10
+ (b) combination of their Contribution(s) with the work of authorship to which such Contribution(s) was added by such copyright holder or contributor, if, at the time the Contribution is added, such addition causes such combination to be necessarily infringed. The patent license shall not apply to any other combinations which include the Contribution.
11
+ Except as expressly stated above, no rights or licenses from any copyright holder or contributor is granted under this license, whether expressly, by implication, estoppel or otherwise.
12
+
13
+ DISCLAIMER
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @vltpkg/user-agent
2
+
3
+ The `User-Agent` string that vlt sends with every outbound request.
4
+
5
+ **[Usage](#usage)**
6
+
7
+ ## Overview
8
+
9
+ Every place in the client that talks to a registry or an API sends the
10
+ same `User-Agent`, and lifecycle scripts see the same value in
11
+ `npm_config_user_agent`. This package is the single source of that
12
+ string so the various clients cannot drift apart.
13
+
14
+ The value is the vlt version followed by the runtime, which is taken
15
+ from `navigator.userAgent` when one is available, and otherwise
16
+ detected from `process.versions`.
17
+
18
+ This package has no dependencies and touches no node builtins, so it
19
+ is safe to use from browser-safe packages such as `@vltpkg/query`.
20
+
21
+ ## Usage
22
+
23
+ ```js
24
+ import { userAgent, userAgentHeaders } from '@vltpkg/user-agent'
25
+
26
+ console.log(userAgent) // vlt/1.0.10 Node.js/22.22.0
27
+
28
+ // `userAgentHeaders` is `{ 'User-Agent': userAgent }` in Node.js, Bun and
29
+ // Deno, and `{}` in a browser, where the header cannot be set by scripts
30
+ // and would otherwise force a CORS preflight on cross-origin requests.
31
+ await fetch(url, {
32
+ headers: { Accept: 'application/json', ...userAgentHeaders },
33
+ })
34
+ ```
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The `User-Agent` header sent with every request vlt makes, and the value
3
+ * of `npm_config_user_agent` in lifecycle script environments.
4
+ *
5
+ * Looks like `vlt/1.2.3 Node.js/22.22.0`, with the trailing portion coming
6
+ * from `navigator.userAgent` when the runtime provides one.
7
+ */
8
+ export declare const userAgent: string;
9
+ /**
10
+ * Headers to spread into the `headers` of a `fetch()` call so that the
11
+ * request carries {@link userAgent}.
12
+ *
13
+ * Empty in browsers: the browser sets its own `User-Agent` and does not let
14
+ * scripts override it, and since `User-Agent` is not a CORS-safelisted
15
+ * request header, setting it would force a preflight on cross-origin
16
+ * registry requests that not every registry accepts.
17
+ */
18
+ export declare const userAgentHeaders: Readonly<Record<string, string>>;
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import pkg from '@vltpkg/user-agent/package.json' with { type: 'json' };
2
+ // Read the runtime off of globalThis rather than importing `node:process`
3
+ // so that this stays usable from browser-safe packages.
4
+ const { navigator: nav, process: proc } = globalThis;
5
+ /* c8 ignore next - there is no `process` in a browser */
6
+ const versions = proc?.versions ?? {};
7
+ const isObj = (v) => typeof v === 'object' && !!v;
8
+ const { Deno, Bun } = globalThis;
9
+ const isDeno = isObj(Deno);
10
+ const isBun = !isDeno && isObj(Bun);
11
+ // bun and deno also report 'node' in process.versions so its only
12
+ // node if it is not bun or deno
13
+ const isNode = !isDeno && !isBun && 'node' in versions;
14
+ // All the runtimes put their versions into process.versions
15
+ const bun = isBun ? versions.bun : undefined;
16
+ const deno = isDeno ? versions.deno : undefined;
17
+ const node = isNode ? versions.node : undefined;
18
+ // Anything that is not one of the server-side runtimes we know about
19
+ // is treated as a browser (or browser-like) environment.
20
+ const isBrowser = !isDeno && !isBun && !isNode;
21
+ const runtime = nav?.userAgent ??
22
+ (bun ? `Bun/${bun}`
23
+ : deno ? `Deno/${deno}`
24
+ : node ? `Node.js/${node}`
25
+ : '(unknown platform)');
26
+ /**
27
+ * The `User-Agent` header sent with every request vlt makes, and the value
28
+ * of `npm_config_user_agent` in lifecycle script environments.
29
+ *
30
+ * Looks like `vlt/1.2.3 Node.js/22.22.0`, with the trailing portion coming
31
+ * from `navigator.userAgent` when the runtime provides one.
32
+ */
33
+ export const userAgent = `vlt/${pkg.version} ${runtime}`;
34
+ /**
35
+ * Headers to spread into the `headers` of a `fetch()` call so that the
36
+ * request carries {@link userAgent}.
37
+ *
38
+ * Empty in browsers: the browser sets its own `User-Agent` and does not let
39
+ * scripts override it, and since `User-Agent` is not a CORS-safelisted
40
+ * request header, setting it would force a preflight on cross-origin
41
+ * registry requests that not every registry accepts.
42
+ */
43
+ export const userAgentHeaders = isBrowser ? {} : { 'User-Agent': userAgent };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@vltpkg/user-agent",
3
+ "description": "The User-Agent string that vlt sends with every outbound request",
4
+ "version": "1.1.1",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/vltpkg/vltpkg.git",
8
+ "directory": "src/user-agent"
9
+ },
10
+ "author": {
11
+ "name": "vlt technology inc.",
12
+ "email": "support@vlt.sh",
13
+ "url": "http://vlt.sh"
14
+ },
15
+ "devDependencies": {
16
+ "@eslint/js": "^9.39.1",
17
+ "@types/node": "^22.19.2",
18
+ "eslint": "^9.39.1",
19
+ "prettier": "^3.7.4",
20
+ "tap": "^21.5.0",
21
+ "typedoc": "~0.27.9",
22
+ "typescript": "5.7.3",
23
+ "typescript-eslint": "^8.49.0"
24
+ },
25
+ "license": "BSD-2-Clause-Patent",
26
+ "engines": {
27
+ "node": ">=22.22.0"
28
+ },
29
+ "scripts": {
30
+ "format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
31
+ "format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
32
+ "lint": "eslint . --fix",
33
+ "lint:check": "eslint .",
34
+ "prepack": "tsc -p tsconfig.publish.json && ../../scripts/update-dist-exports.ts",
35
+ "snap": "tap",
36
+ "test": "tap",
37
+ "posttest": "tsc --noEmit",
38
+ "typecheck": "tsc --noEmit"
39
+ },
40
+ "tap": {
41
+ "extends": "../../tap-config.yaml"
42
+ },
43
+ "prettier": "../../.prettierrc.js",
44
+ "module": "./dist/index.js",
45
+ "type": "module",
46
+ "exports": {
47
+ "./package.json": "./package.json",
48
+ ".": {
49
+ "import": {
50
+ "default": "./dist/index.js"
51
+ }
52
+ }
53
+ },
54
+ "files": [
55
+ "dist"
56
+ ],
57
+ "bugs": [
58
+ {
59
+ "type": "link",
60
+ "url": "https://github.com/vltpkg/vltpkg/issues"
61
+ }
62
+ ],
63
+ "homepage": "https://github.com/vltpkg/vltpkg/tree/main/src/user-agent#readme"
64
+ }