UserAgent

import { UserAgent } from "jsr:@ende/http";

class

class UserAgent

A representation of user agent string, which can be used to determine environmental information represented by the string. All properties are determined lazily.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.browser.name}
    on ${userAgent.os.name} ${userAgent.os.version}!`);
});

Constructors

constructor

new UserAgent(ua: string | null)

Constructs a new instance.

ua: string | null

The user agent string to construct this instance with.

Methods

browser

get browser(): Browser

The name and version of the browser extracted from the user agent string.

Returns

An object with information about the user agent's browser.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.browser.name}!`);
});

cpu

get cpu(): Cpu

The architecture of the CPU extracted from the user agent string.

Returns

An object with information about the user agent's CPU.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.cpu.architecture}!`);
});

device

get device(): Device

The model, type, and vendor of a device if present in a user agent string.

Returns

An object with information about the user agent's device.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.device.model}!`);
});

engine

get engine(): Engine

The name and version of the browser engine in a user agent string.

Returns

An object with information about the user agent's browser engine.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.engine.name}!`);
});

os

get os(): Os

The name and version of the operating system in a user agent string.

Returns

An object with information about the user agent's OS.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.os.name}!`);
});

ua

get ua(): string

A read only version of the user agent string related to the instance.

Returns

The user agent string.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.ua}!`);
});

toJSON

toJSON(): { browser: Browser; cpu: Cpu; device: Device; engine: Engine; os: Os; ua: string }

Converts the current instance to a JSON representation.

Returns

A JSON representation on this user agent instance.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${JSON.stringify(userAgent.toJSON())}!`);
});

toString

toString(): string

Converts the current instance to a string.

Returns

The user agent string.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  return new Response(`Hello, ${userAgent.toString()}!`);
});

[Symbol.for("Deno.customInspect")]

[Symbol.for("Deno.customInspect")](inspect: (value: unknown, options: unknown) => string, options: unknown): string

Custom output for Ende.inspect.

inspect: (value: unknown, options: unknown) => string

internal inspect function.

options: unknown

inspect options.

Returns

The custom value to inspect.

Example: Usage

import { UserAgent } from "@ende/http/user-agent";

Ende.serve((req) => {
  const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
  Ende.inspect(userAgent);
  return new Response(`Hello, ${userAgent.ua}!`);
});

Source