dredless 0.5.2 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dredless",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "A fully-featured and from-scratch headless drednot.io client implementation written in pure js.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/certainhuman/dredless#readme",
package/src/index.d.ts CHANGED
@@ -23,6 +23,18 @@ export interface ShipList {
23
23
  ships: Ship[];
24
24
  }
25
25
 
26
+ export interface InviteMetadata {
27
+ shipId: string | null;
28
+ name: string | null;
29
+ color: string | null;
30
+ previewImageUrl: string | null;
31
+ hostname: string;
32
+ inviteCode: string;
33
+ }
34
+ export interface InviteFetchOptions { fetchImpl?: typeof fetch; }
35
+ export function fetchInviteMetadata(invite: string | URL, options?: InviteFetchOptions): Promise<InviteMetadata>;
36
+ export function parseInviteMetadata(html: string): InviteMetadata;
37
+
26
38
  export type ShipSpec =
27
39
  | { type: "join_or_load"; id: number | string }
28
40
  | { type: "new"; name?: string | null; color?: string | null }
package/src/index.js CHANGED
@@ -77,12 +77,14 @@ export {
77
77
  } from "./protocol/outbound/ui-config.js";
78
78
  export {decryptPayload} from "./crypto/chacha.js";
79
79
  export {decompressLz4Frame} from "./compression/lz4.js";
80
+ export {fetchInviteMetadata, parseInviteMetadata} from "./network/invites.js";
80
81
 
81
82
  import {AnonSession, createAnonSession, createAnonToken, createSession, Session} from "./network/session.js";
82
83
  import {Connection} from "./network/connection.js";
83
84
  import {DredlessClient} from "./client/index.js";
84
85
  import {fetchGameVersion, fetchNoticeVersion, fetchServerStatus, fetchServerStatuses, fetchServers} from "./network/servers.js";
85
86
  import {fetchShipList, fetchShips} from "./network/ships.js";
87
+ import {fetchInviteMetadata} from "./network/invites.js";
86
88
 
87
89
  async function sessionOrAnon(session) {
88
90
  return session || createAnonSession();
@@ -119,6 +121,7 @@ export const Dredless = {
119
121
  fetchServers,
120
122
  fetchShips,
121
123
  fetchShipList,
124
+ fetchInviteMetadata,
122
125
  joinShip,
123
126
  startShip,
124
127
  startNewShip,
@@ -2,3 +2,4 @@ export * from "./connection.js";
2
2
  export * from "./servers.js";
3
3
  export * from "./ships.js";
4
4
  export * from "./session.js";
5
+ export * from "./invites.js";
@@ -2,3 +2,4 @@ export {Connection} from "./connection.js";
2
2
  export {fetchGameVersion, fetchNoticeVersion, fetchServerStatus, fetchServerStatuses, fetchServers, resolveServer} from "./servers.js";
3
3
  export {fetchShips, fetchShipList, createShipSpec, createInviteShipSpec, shipRef} from "./ships.js";
4
4
  export {Session, AnonSession, createSession, createAnonSession, createAnonToken} from "./session.js";
5
+ export {fetchInviteMetadata, parseInviteMetadata} from "./invites.js";
@@ -0,0 +1,11 @@
1
+ export interface InviteMetadata {
2
+ shipId: string | null;
3
+ name: string | null;
4
+ color: string | null;
5
+ previewImageUrl: string | null;
6
+ hostname: string;
7
+ inviteCode: string;
8
+ }
9
+ export interface InviteFetchOptions { fetchImpl?: typeof fetch; }
10
+ export function fetchInviteMetadata(invite: string | URL, options?: InviteFetchOptions): Promise<InviteMetadata>;
11
+ export function parseInviteMetadata(html: string): InviteMetadata;
@@ -0,0 +1,60 @@
1
+ import {getFetch} from "../runtime.js";
2
+
3
+ export async function fetchInviteMetadata(invite, {fetchImpl = null} = {}) {
4
+ const url = normalizeInviteUrl(invite);
5
+ const response = await (fetchImpl || getFetch())(url.href, {headers: {accept: "text/html"}});
6
+ if (!response.ok) throw new Error(`Invite metadata request failed: ${response.status} ${response.statusText}`);
7
+ return parseInviteMetadata(await response.text());
8
+ }
9
+
10
+ export function parseInviteMetadata(html) {
11
+ const embeddedUrl = metaContent(html, "og:url");
12
+ const url = normalizeInviteUrl(embeddedUrl);
13
+ const parsedTitle = parseInviteTitle(metaContent(html, "title") || "");
14
+ const preview = metaContent(html, "og:image") || metaContent(html, "twitter:image");
15
+ return Object.freeze({
16
+ shipId: parsedTitle.shipId,
17
+ name: parsedTitle.name,
18
+ color: metaContent(html, "theme-color") || metaContent(html, "color") || null,
19
+ previewImageUrl: preview ? new URL(preview, url).href : null,
20
+ hostname: url.hostname,
21
+ inviteCode: url.pathname.split("/").filter(Boolean).at(-1)
22
+ });
23
+ }
24
+
25
+ function normalizeInviteUrl(invite) {
26
+ const url = new URL(String(invite));
27
+ if (!/^\/invite\/[^/]+\/?$/i.test(url.pathname)) throw new TypeError("Expected a Drednot invite URL");
28
+ return url;
29
+ }
30
+
31
+ function parseInviteTitle(title) {
32
+ const match = title.match(/^Invite:\s*(.*?)\s*\{([^{}]+)}(?:\s*-\s*drednot\.io)?\s*$/i);
33
+ return match ? {name: decodeHtml(match[1]), shipId: decodeHtml(match[2])} : {name: null, shipId: null};
34
+ }
35
+
36
+ function metaContent(html, key) {
37
+ if (key === "title") {
38
+ const match = html.match(/<title\b[^>]*>([\s\S]*?)<\/title\s*>/i);
39
+ return match ? decodeHtml(match[1].trim()) : null;
40
+ }
41
+ for (const tag of html.matchAll(/<meta\b[^>]*>/gi)) {
42
+ const property = attribute(tag[0], "property") || attribute(tag[0], "name");
43
+ if (property?.toLowerCase() === key.toLowerCase()) return attribute(tag[0], "content");
44
+ }
45
+ return null;
46
+ }
47
+
48
+ function attribute(tag, name) {
49
+ const match = tag.match(new RegExp(`\\b${name}\\s*=\\s*(["'])([\\s\\S]*?)\\1`, "i"));
50
+ return match ? decodeHtml(match[2]) : null;
51
+ }
52
+
53
+ function decodeHtml(value) {
54
+ return value.replace(/&(#x?[0-9a-f]+|amp|lt|gt|quot|apos|#39);/gi, (_, entity) => {
55
+ const named = {amp: "&", lt: "<", gt: ">", quot: '"', apos: "'"};
56
+ if (named[entity.toLowerCase()]) return named[entity.toLowerCase()];
57
+ const number = entity[1].toLowerCase() === "x" ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10);
58
+ return Number.isFinite(number) ? String.fromCodePoint(number) : _;
59
+ });
60
+ }