diff --git a/.changeset/five-goats-hang.md b/.changeset/five-goats-hang.md new file mode 100644 index 00000000..cf8a846a --- /dev/null +++ b/.changeset/five-goats-hang.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Add `navigator` global with `userAgent` property diff --git a/packages/runtime/src/fetch/fetch.ts b/packages/runtime/src/fetch/fetch.ts index 50c960a3..5f176f77 100644 --- a/packages/runtime/src/fetch/fetch.ts +++ b/packages/runtime/src/fetch/fetch.ts @@ -5,6 +5,7 @@ import { decoder } from '../polyfills/text-decoder'; import { Request, type RequestInit } from './request'; import { Response } from './response'; import { Headers } from './headers'; +import { navigator } from '../navigator'; import type { SwitchClass } from '../switch'; declare const Switch: SwitchClass; @@ -143,7 +144,7 @@ async function fetchHttp(req: Request, url: URL) { req.headers.set('host', url.host); } if (!req.headers.has('user-agent')) { - req.headers.set('user-agent', `nx.js/${Switch.version.nxjs}`); + req.headers.set('user-agent', navigator.userAgent); } if (!req.headers.has('accept')) { req.headers.set('accept', '*/*'); diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index 23507ffa..ba22fe80 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -22,6 +22,7 @@ export type { export type { InspectOptions } from './inspect'; export type * from './types'; export type * from './console'; +export type * from './navigator'; export type { CanvasImageSource, Canvas, @@ -73,6 +74,8 @@ def('setInterval', setInterval); def('clearTimeout', clearTimeout); def('clearInterval', clearInterval); +import './navigator'; + import * as WebAssembly from './wasm'; def('WebAssembly', WebAssembly); diff --git a/packages/runtime/src/navigator.ts b/packages/runtime/src/navigator.ts new file mode 100644 index 00000000..14af1924 --- /dev/null +++ b/packages/runtime/src/navigator.ts @@ -0,0 +1,33 @@ +import { def } from './utils'; +import type { SwitchClass } from './switch'; + +declare const Switch: SwitchClass; + +/** + * The `Navigator` interface represents the state and the identity of the user agent. + * It allows scripts to query it and to register themselves to carry on some activities. + * + * A `Navigator` instance can be retrieved by accessing the global {@link navigator | `navigator`} property. + * + * @see https://developer.mozilla.org/docs/Web/API/Navigator + */ +export class Navigator { + constructor() { + throw new TypeError('Illegal constructor.'); + } + + /** + * Returns the value used for the `User-Agent` HTTP request header for + * HTTP requests initiated with {@link fetch | `fetch()`}. + * + * @example "my-app/0.0.1 (Switch; en-us; rv:14.1.2|AMS 1.5.4|E) nx.js/0.0.18" + * @see https://developer.mozilla.org/docs/Web/API/Navigator/userAgent + */ + get userAgent(): string { + return `nx.js/${Switch.version.nxjs}`; + } +} +def('Navigator', Navigator); + +export const navigator: Navigator = Object.create(Navigator.prototype); +def('navigator', navigator);