@fabriktor/fx 0.0.172 → 0.0.174

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.
@@ -0,0 +1,19 @@
1
+ import type { RPFeedLinkPreview } from "@fabriktor/schema";
2
+ export declare const linkPreviewConcurrency = 3;
3
+ export declare const linkPreviewDelayMs = 400;
4
+ export declare const linkPreviewCacheSize = 64;
5
+ export type LinkPart = {
6
+ text: string;
7
+ url: string;
8
+ };
9
+ export declare function publicLinkURL(value: string): string;
10
+ export declare function linkParts(value: string): LinkPart[];
11
+ export declare class LinkPreviews {
12
+ private readonly load;
13
+ private readonly pool;
14
+ private generation;
15
+ private cache;
16
+ constructor(load: (url: string) => Promise<RPFeedLinkPreview | null>);
17
+ clear(): void;
18
+ get(value: string): Promise<RPFeedLinkPreview | null>;
19
+ }
@@ -0,0 +1,87 @@
1
+ // Copyright (C) Fabriktor, Inc. 2025-present.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+ // not use this file except in compliance with the License. You may obtain
5
+ // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ import { MultiCreatePool } from "./multi-create.js";
7
+ export const linkPreviewConcurrency = 3;
8
+ export const linkPreviewDelayMs = 400;
9
+ export const linkPreviewCacheSize = 64;
10
+ export function publicLinkURL(value) {
11
+ try {
12
+ const url = new URL(value);
13
+ return ["https:", "http:"].includes(url.protocol) &&
14
+ !url.username &&
15
+ !url.password
16
+ ? url.href
17
+ : "";
18
+ }
19
+ catch {
20
+ return "";
21
+ }
22
+ }
23
+ export function linkParts(value) {
24
+ const parts = [];
25
+ let cursor = 0;
26
+ for (const match of value.matchAll(/https?:\/\/[^\s<>]+/gi)) {
27
+ let candidate = match[0].replace(/[.,!?;:'"]+$/, "");
28
+ for (const [open, close] of [
29
+ ["(", ")"],
30
+ ["[", "]"],
31
+ ["{", "}"],
32
+ ]) {
33
+ while (candidate.endsWith(close) &&
34
+ candidate.split(close).length > candidate.split(open).length)
35
+ candidate = candidate.slice(0, -1);
36
+ }
37
+ const url = publicLinkURL(candidate);
38
+ if (!url)
39
+ continue;
40
+ if (match.index > cursor)
41
+ parts.push({ text: value.slice(cursor, match.index), url: "" });
42
+ parts.push({ text: candidate, url });
43
+ cursor = match.index + candidate.length;
44
+ }
45
+ if (cursor < value.length)
46
+ parts.push({ text: value.slice(cursor), url: "" });
47
+ return parts;
48
+ }
49
+ // One preview per post; bounded, session-owned cache also coalesces concurrent cards.
50
+ export class LinkPreviews {
51
+ load;
52
+ pool = new MultiCreatePool(linkPreviewConcurrency);
53
+ generation = 0;
54
+ cache = new Map();
55
+ constructor(load) {
56
+ this.load = load;
57
+ }
58
+ clear() {
59
+ this.generation++;
60
+ this.cache.clear();
61
+ }
62
+ get(value) {
63
+ const url = publicLinkURL(value);
64
+ if (!url)
65
+ return Promise.resolve(null);
66
+ const cached = this.cache.get(url);
67
+ if (cached)
68
+ return cached;
69
+ if (this.cache.size >= linkPreviewCacheSize)
70
+ this.cache.delete(this.cache.keys().next().value);
71
+ const generation = this.generation;
72
+ let preview = null;
73
+ const request = this.pool
74
+ .run(async () => {
75
+ if (generation === this.generation)
76
+ preview = await this.load(url);
77
+ })
78
+ .then(() => preview)
79
+ .catch((error) => {
80
+ if (this.cache.get(url) === request)
81
+ this.cache.delete(url);
82
+ throw error;
83
+ });
84
+ this.cache.set(url, request);
85
+ return request;
86
+ }
87
+ }
@@ -14,6 +14,7 @@ export * from "./core/date.js";
14
14
  export * from "./core/err.js";
15
15
  export * from "./core/events.js";
16
16
  export * from "./core/id.js";
17
+ export * from "./core/links.js";
17
18
  export * from "./core/measurement.js";
18
19
  export * from "./core/multi-create.js";
19
20
  export * from "./core/pre-action.js";
package/dist/src/index.js CHANGED
@@ -14,6 +14,7 @@ export * from "./core/date.js";
14
14
  export * from "./core/err.js";
15
15
  export * from "./core/events.js";
16
16
  export * from "./core/id.js";
17
+ export * from "./core/links.js";
17
18
  export * from "./core/measurement.js";
18
19
  export * from "./core/multi-create.js";
19
20
  export * from "./core/pre-action.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.172",
3
+ "version": "0.0.174",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,9 +17,9 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.103",
20
+ "@fabriktor/client": "0.0.107",
21
21
  "@fabriktor/fx": "0.0.94",
22
- "@fabriktor/schema": "0.0.75",
22
+ "@fabriktor/schema": "0.0.78",
23
23
  "convert": "8.0.2"
24
24
  }
25
25
  }