eventregistry 10.0.0 → 11.0.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/AGENT-RECIPES.md +86 -0
- package/CHANGELOG.md +47 -0
- package/MIGRATION.md +22 -4
- package/README.md +49 -1
- package/dist/cjs/abort.d.ts +14 -0
- package/dist/cjs/abort.js +71 -0
- package/dist/cjs/abort.js.map +1 -0
- package/dist/cjs/base.js +8 -2
- package/dist/cjs/base.js.map +1 -1
- package/dist/cjs/errors.d.ts +66 -0
- package/dist/cjs/errors.js +75 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/eventRegistry.d.ts +47 -15
- package/dist/cjs/eventRegistry.js +237 -185
- package/dist/cjs/eventRegistry.js.map +1 -1
- package/dist/cjs/fluent/recent.d.ts +7 -2
- package/dist/cjs/fluent/recent.js +7 -3
- package/dist/cjs/fluent/recent.js.map +1 -1
- package/dist/cjs/guards.d.ts +12 -0
- package/dist/cjs/guards.js +75 -0
- package/dist/cjs/guards.js.map +1 -0
- package/dist/cjs/http.d.ts +5 -0
- package/dist/cjs/http.js +64 -11
- package/dist/cjs/http.js.map +1 -1
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.js +17 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/logger.d.ts +2 -0
- package/dist/cjs/logger.js +6 -2
- package/dist/cjs/logger.js.map +1 -1
- package/dist/cjs/mutex.d.ts +2 -1
- package/dist/cjs/mutex.js +19 -2
- package/dist/cjs/mutex.js.map +1 -1
- package/dist/cjs/query.js +4 -4
- package/dist/cjs/query.js.map +1 -1
- package/dist/cjs/queryArticle.js +3 -2
- package/dist/cjs/queryArticle.js.map +1 -1
- package/dist/cjs/queryArticles.d.ts +6 -6
- package/dist/cjs/queryArticles.js +19 -18
- package/dist/cjs/queryArticles.js.map +1 -1
- package/dist/cjs/queryEvent.d.ts +1 -0
- package/dist/cjs/queryEvent.js +11 -5
- package/dist/cjs/queryEvent.js.map +1 -1
- package/dist/cjs/queryEvents.js +8 -7
- package/dist/cjs/queryEvents.js.map +1 -1
- package/dist/cjs/queryIterator.d.ts +2 -0
- package/dist/cjs/queryIterator.js +18 -10
- package/dist/cjs/queryIterator.js.map +1 -1
- package/dist/cjs/queryMentions.d.ts +1 -0
- package/dist/cjs/queryMentions.js +14 -8
- package/dist/cjs/queryMentions.js.map +1 -1
- package/dist/cjs/queryStory.d.ts +9 -6
- package/dist/cjs/queryStory.js +4 -4
- package/dist/cjs/queryStory.js.map +1 -1
- package/dist/cjs/recent.d.ts +18 -6
- package/dist/cjs/recent.js +26 -8
- package/dist/cjs/recent.js.map +1 -1
- package/dist/cjs/recentFeed.d.ts +35 -0
- package/dist/cjs/recentFeed.js +76 -0
- package/dist/cjs/recentFeed.js.map +1 -0
- package/dist/cjs/returnInfo.js +2 -2
- package/dist/cjs/returnInfo.js.map +1 -1
- package/dist/cjs/topicPage.js +7 -3
- package/dist/cjs/topicPage.js.map +1 -1
- package/dist/cjs/types.d.ts +63 -7
- package/dist/cjs/usage.d.ts +39 -0
- package/dist/cjs/usage.js +62 -0
- package/dist/cjs/usage.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/abort.d.ts +14 -0
- package/dist/esm/abort.js +65 -0
- package/dist/esm/abort.js.map +1 -0
- package/dist/esm/analytics.d.ts +2 -2
- package/dist/esm/base.js +8 -2
- package/dist/esm/base.js.map +1 -1
- package/dist/esm/counts.d.ts +2 -2
- package/dist/esm/dailyShares.d.ts +2 -2
- package/dist/esm/errors.d.ts +66 -0
- package/dist/esm/errors.js +67 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/eventForText.d.ts +3 -3
- package/dist/esm/eventRegistry.d.ts +67 -35
- package/dist/esm/eventRegistry.js +238 -186
- package/dist/esm/eventRegistry.js.map +1 -1
- package/dist/esm/fluent/analytics.d.ts +2 -2
- package/dist/esm/fluent/articles.d.ts +5 -5
- package/dist/esm/fluent/counts.d.ts +2 -2
- package/dist/esm/fluent/eventForText.d.ts +1 -1
- package/dist/esm/fluent/events.d.ts +5 -5
- package/dist/esm/fluent/info.d.ts +2 -2
- package/dist/esm/fluent/mentions.d.ts +3 -3
- package/dist/esm/fluent/recent.d.ts +9 -4
- package/dist/esm/fluent/recent.js +7 -3
- package/dist/esm/fluent/recent.js.map +1 -1
- package/dist/esm/fluent/shares.d.ts +2 -2
- package/dist/esm/fluent/stories.d.ts +3 -3
- package/dist/esm/fluent/topicPages.d.ts +3 -3
- package/dist/esm/fluent/trends.d.ts +3 -3
- package/dist/esm/guards.d.ts +12 -0
- package/dist/esm/guards.js +70 -0
- package/dist/esm/guards.js.map +1 -0
- package/dist/esm/helpers/analytics.d.ts +2 -2
- package/dist/esm/helpers/articles.d.ts +4 -4
- package/dist/esm/helpers/counts.d.ts +2 -2
- package/dist/esm/helpers/eventForText.d.ts +1 -1
- package/dist/esm/helpers/events.d.ts +4 -4
- package/dist/esm/helpers/info.d.ts +2 -2
- package/dist/esm/helpers/mentions.d.ts +2 -2
- package/dist/esm/helpers/recent.d.ts +2 -2
- package/dist/esm/helpers/shares.d.ts +2 -2
- package/dist/esm/helpers/stories.d.ts +3 -3
- package/dist/esm/helpers/topicPages.d.ts +3 -3
- package/dist/esm/helpers/trends.d.ts +3 -3
- package/dist/esm/helpers/uri.d.ts +2 -2
- package/dist/esm/http.d.ts +5 -0
- package/dist/esm/http.js +64 -11
- package/dist/esm/http.js.map +1 -1
- package/dist/esm/index.d.ts +54 -47
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/info.d.ts +2 -2
- package/dist/esm/logger.d.ts +2 -0
- package/dist/esm/logger.js +6 -2
- package/dist/esm/logger.js.map +1 -1
- package/dist/esm/mutex.d.ts +2 -1
- package/dist/esm/mutex.js +19 -2
- package/dist/esm/mutex.js.map +1 -1
- package/dist/esm/query.d.ts +2 -2
- package/dist/esm/query.js +4 -4
- package/dist/esm/query.js.map +1 -1
- package/dist/esm/queryArticle.d.ts +3 -3
- package/dist/esm/queryArticle.js +3 -2
- package/dist/esm/queryArticle.js.map +1 -1
- package/dist/esm/queryArticles.d.ts +12 -12
- package/dist/esm/queryArticles.js +19 -18
- package/dist/esm/queryArticles.js.map +1 -1
- package/dist/esm/queryEvent.d.ts +6 -5
- package/dist/esm/queryEvent.js +11 -5
- package/dist/esm/queryEvent.js.map +1 -1
- package/dist/esm/queryEvents.d.ts +6 -6
- package/dist/esm/queryEvents.js +8 -7
- package/dist/esm/queryEvents.js.map +1 -1
- package/dist/esm/queryIterator.d.ts +2 -0
- package/dist/esm/queryIterator.js +18 -10
- package/dist/esm/queryIterator.js.map +1 -1
- package/dist/esm/queryMentions.d.ts +4 -3
- package/dist/esm/queryMentions.js +14 -8
- package/dist/esm/queryMentions.js.map +1 -1
- package/dist/esm/queryStory.d.ts +11 -8
- package/dist/esm/queryStory.js +4 -4
- package/dist/esm/queryStory.js.map +1 -1
- package/dist/esm/recent.d.ts +21 -9
- package/dist/esm/recent.js +26 -8
- package/dist/esm/recent.js.map +1 -1
- package/dist/esm/recentFeed.d.ts +35 -0
- package/dist/esm/recentFeed.js +70 -0
- package/dist/esm/recentFeed.js.map +1 -0
- package/dist/esm/returnInfo.d.ts +1 -1
- package/dist/esm/returnInfo.js +2 -2
- package/dist/esm/returnInfo.js.map +1 -1
- package/dist/esm/topicPage.d.ts +3 -3
- package/dist/esm/topicPage.js +7 -3
- package/dist/esm/topicPage.js.map +1 -1
- package/dist/esm/topicPages.d.ts +1 -1
- package/dist/esm/trends.d.ts +2 -2
- package/dist/esm/types.d.ts +69 -13
- package/dist/esm/usage.d.ts +39 -0
- package/dist/esm/usage.js +58 -0
- package/dist/esm/usage.js.map +1 -0
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +21 -12
- package/src/abort.ts +70 -0
- package/src/base.ts +8 -2
- package/src/errors.ts +105 -0
- package/src/eventRegistry.ts +285 -185
- package/src/fluent/recent.ts +9 -5
- package/src/guards.ts +79 -0
- package/src/http.ts +70 -11
- package/src/index.ts +7 -0
- package/src/logger.ts +7 -2
- package/src/mutex.ts +20 -2
- package/src/query.ts +4 -4
- package/src/queryArticle.ts +3 -2
- package/src/queryArticles.ts +25 -17
- package/src/queryEvent.ts +12 -5
- package/src/queryEvents.ts +9 -8
- package/src/queryIterator.ts +20 -9
- package/src/queryMentions.ts +16 -9
- package/src/queryStory.ts +11 -4
- package/src/recent.ts +35 -10
- package/src/recentFeed.ts +91 -0
- package/src/returnInfo.ts +2 -2
- package/src/topicPage.ts +7 -3
- package/src/types.ts +66 -7
- package/src/usage.ts +79 -0
- package/src/version.ts +1 -1
package/src/fluent/recent.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { EventRegistry } from "../eventRegistry";
|
|
2
2
|
import { GetRecentArticles, GetRecentEvents } from "../recent";
|
|
3
|
-
import {
|
|
3
|
+
import { RecentFeed, RecentFeedOptions, recentArticlesFeed, recentEventsFeed } from "../recentFeed";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Recent activity. Each method returns a `RecentFeed`: `await` it for one poll (as in 10.0.0), or
|
|
7
|
+
* `for await` it to keep polling every `pollMs`.
|
|
8
|
+
*/
|
|
5
9
|
export class RecentFluent {
|
|
6
10
|
constructor(private readonly er: EventRegistry) {}
|
|
7
11
|
|
|
8
|
-
events(args: ConstructorParameters<typeof GetRecentEvents>[1] = {}):
|
|
9
|
-
return
|
|
12
|
+
events(args: ConstructorParameters<typeof GetRecentEvents>[1] & RecentFeedOptions = {}): RecentFeed {
|
|
13
|
+
return recentEventsFeed(this.er, args);
|
|
10
14
|
}
|
|
11
15
|
|
|
12
|
-
articles(args: ConstructorParameters<typeof GetRecentArticles>[1] = {}):
|
|
13
|
-
return
|
|
16
|
+
articles(args: ConstructorParameters<typeof GetRecentArticles>[1] & RecentFeedOptions = {}): RecentFeed {
|
|
17
|
+
return recentArticlesFeed(this.er, args);
|
|
14
18
|
}
|
|
15
19
|
}
|
package/src/guards.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-written shape checks for the top-level blocks of Event Registry responses. They back the opt-in
|
|
3
|
+
* `validate: true` option. Each check only looks at fields the SDK types document; extra fields are fine.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
type Issues = string[];
|
|
7
|
+
type Json = Record<string, unknown>;
|
|
8
|
+
|
|
9
|
+
export const isObject = (value: unknown): value is Json =>
|
|
10
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
|
|
12
|
+
const checkCounters = (block: Json, path: string, issues: Issues) => {
|
|
13
|
+
for (const key of ["count", "page", "pages", "totalResults", "usedResults"]) {
|
|
14
|
+
if (block[key] !== undefined && typeof block[key] !== "number") {
|
|
15
|
+
issues.push(`${path}.${key}: expected a number`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const checkResultsBlock = (value: unknown, path: string, issues: Issues, item: (entry: unknown, entryPath: string) => string | undefined) => {
|
|
21
|
+
if (!isObject(value)) {
|
|
22
|
+
issues.push(`${path}: expected an object`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
checkCounters(value, path, issues);
|
|
26
|
+
if (!Array.isArray(value.results)) {
|
|
27
|
+
issues.push(`${path}.results: expected an array`);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
value.results.forEach((entry, index) => {
|
|
31
|
+
const problem = item(entry, `${path}.results[${index}]`);
|
|
32
|
+
if (problem) {
|
|
33
|
+
issues.push(problem);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const withUri = (entry: unknown, path: string) =>
|
|
39
|
+
isObject(entry) && typeof entry.uri === "string" ? undefined : `${path}.uri: expected an object with a string uri`;
|
|
40
|
+
const anyObject = (entry: unknown, path: string) => isObject(entry) ? undefined : `${path}: expected an object`;
|
|
41
|
+
const aString = (entry: unknown, path: string) => typeof entry === "string" ? undefined : `${path}: expected a string`;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* List every way `data` departs from the documented response shapes. Empty means it is fine.
|
|
45
|
+
* Error responses (`{ error }`) are not validated; the caller already handles those.
|
|
46
|
+
*/
|
|
47
|
+
export function validateResponse(data: unknown): string[] {
|
|
48
|
+
const issues: Issues = [];
|
|
49
|
+
if (!isObject(data)) {
|
|
50
|
+
return ["response: expected an object"];
|
|
51
|
+
}
|
|
52
|
+
if (data.error !== undefined) {
|
|
53
|
+
return issues;
|
|
54
|
+
}
|
|
55
|
+
if (data.articles !== undefined) {
|
|
56
|
+
checkResultsBlock(data.articles, "articles", issues, withUri);
|
|
57
|
+
}
|
|
58
|
+
if (data.events !== undefined) {
|
|
59
|
+
checkResultsBlock(data.events, "events", issues, withUri);
|
|
60
|
+
}
|
|
61
|
+
if (data.mentions !== undefined) {
|
|
62
|
+
checkResultsBlock(data.mentions, "mentions", issues, anyObject);
|
|
63
|
+
}
|
|
64
|
+
if (data.uriWgtList !== undefined) {
|
|
65
|
+
checkResultsBlock(data.uriWgtList, "uriWgtList", issues, aString);
|
|
66
|
+
}
|
|
67
|
+
for (const key of ["keywordAggr", "conceptAggr", "categoryAggr"]) {
|
|
68
|
+
if (data[key] !== undefined) {
|
|
69
|
+
checkResultsBlock(data[key], key, issues, anyObject);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (data.conceptTrends !== undefined) {
|
|
73
|
+
const trends = data.conceptTrends;
|
|
74
|
+
if (!isObject(trends) || !Array.isArray(trends.trends)) {
|
|
75
|
+
issues.push("conceptTrends.trends: expected an array");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return issues;
|
|
79
|
+
}
|
package/src/http.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { abortReason, abortableSleep } from "./abort";
|
|
2
|
+
import { ErApiError, parseRetryAfter } from "./errors";
|
|
3
|
+
|
|
1
4
|
export interface ErHttpResponse<T = unknown> {
|
|
2
5
|
data: T;
|
|
3
6
|
status: number;
|
|
@@ -10,15 +13,58 @@ export interface ErHttpRequest {
|
|
|
10
13
|
method?: string;
|
|
11
14
|
body?: unknown;
|
|
12
15
|
headers?: Record<string, string>;
|
|
16
|
+
/** Per-attempt timeout. For an overall deadline use `signal` (see `composeSignal`). */
|
|
13
17
|
timeoutMs?: number;
|
|
18
|
+
/** Aborts the call: no further attempt starts and a pending backoff wait is cancelled. */
|
|
19
|
+
signal?: AbortSignal;
|
|
14
20
|
retry?: number;
|
|
21
|
+
/** Base of the backoff schedule: attempt n waits about `retryDelayMs * 2^n`, capped at `maxRetryDelayMs`. */
|
|
15
22
|
retryDelayMs?: number;
|
|
23
|
+
maxRetryDelayMs?: number;
|
|
16
24
|
stopStatusCodes?: number[];
|
|
17
25
|
responseType?: "json" | "text";
|
|
18
26
|
}
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
/** Largest delay `setTimeout` accepts; anything bigger overflows and fires after about 1 ms. */
|
|
29
|
+
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
30
|
+
|
|
31
|
+
/** Exponential backoff with equal jitter; the fixed `retryDelayMs` is the base of the schedule. */
|
|
32
|
+
function backoffDelay(attempt: number, baseMs: number, maxMs: number): number {
|
|
33
|
+
const capped = Math.min(maxMs, baseMs * 2 ** Math.min(attempt, 30));
|
|
34
|
+
return Math.round(capped / 2 + Math.random() * (capped / 2));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function scalarText(value: unknown): string | undefined {
|
|
38
|
+
return typeof value === "string" || typeof value === "number" ? String(value) : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function toApiError(response: Response): Promise<ErApiError> {
|
|
42
|
+
let apiError: string | undefined;
|
|
43
|
+
let apiCode: string | undefined;
|
|
44
|
+
try {
|
|
45
|
+
const body = JSON.parse(await response.text()) as Record<string, unknown>;
|
|
46
|
+
const err = body?.error;
|
|
47
|
+
if (typeof err === "string") {
|
|
48
|
+
apiError = err;
|
|
49
|
+
} else if (err && typeof err === "object") {
|
|
50
|
+
const obj = err as Record<string, unknown>;
|
|
51
|
+
apiError = typeof obj.message === "string" ? obj.message : undefined;
|
|
52
|
+
apiCode = scalarText(obj.code);
|
|
53
|
+
}
|
|
54
|
+
apiCode ??= scalarText(body?.errorCode);
|
|
55
|
+
} catch {
|
|
56
|
+
// body is empty or not JSON: status line is all we have
|
|
57
|
+
}
|
|
58
|
+
const tokens = Number(response.headers.get("req-tokens"));
|
|
59
|
+
return new ErApiError({
|
|
60
|
+
status: response.status,
|
|
61
|
+
statusText: response.statusText,
|
|
62
|
+
apiCode,
|
|
63
|
+
apiError,
|
|
64
|
+
requestId: response.headers.get("x-request-id") ?? response.headers.get("req-id") ?? undefined,
|
|
65
|
+
tokens: response.headers.has("req-tokens") && Number.isFinite(tokens) ? tokens : undefined,
|
|
66
|
+
retryAfterMs: parseRetryAfter(response.headers.get("retry-after"))
|
|
67
|
+
});
|
|
22
68
|
}
|
|
23
69
|
|
|
24
70
|
function isFetchNetworkError(error: unknown): boolean {
|
|
@@ -42,13 +88,17 @@ export async function erFetch<T = unknown>(req: ErHttpRequest): Promise<ErHttpRe
|
|
|
42
88
|
const timeoutMs = req.timeoutMs ?? 600_000;
|
|
43
89
|
const retry = req.retry ?? 0;
|
|
44
90
|
const retryDelayMs = req.retryDelayMs ?? 5000;
|
|
91
|
+
const maxRetryDelayMs = req.maxRetryDelayMs ?? 30_000;
|
|
45
92
|
const stopStatusCodes = req.stopStatusCodes ?? [204, 400, 401, 403, 530];
|
|
46
93
|
const serializedBody = req.body !== undefined ? JSON.stringify(req.body) : undefined;
|
|
47
94
|
|
|
48
95
|
let attempt = 0;
|
|
49
96
|
for (;;) {
|
|
97
|
+
req.signal?.throwIfAborted();
|
|
50
98
|
const controller = new AbortController();
|
|
51
99
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
100
|
+
const onAbort = () => controller.abort(req.signal?.reason);
|
|
101
|
+
req.signal?.addEventListener("abort", onAbort, { once: true });
|
|
52
102
|
try {
|
|
53
103
|
const init: RequestInit = {
|
|
54
104
|
method,
|
|
@@ -76,23 +126,27 @@ export async function erFetch<T = unknown>(req: ErHttpRequest): Promise<ErHttpRe
|
|
|
76
126
|
};
|
|
77
127
|
}
|
|
78
128
|
|
|
129
|
+
const apiError = await toApiError(response);
|
|
79
130
|
if (stopStatusCodes.includes(response.status)) {
|
|
80
|
-
throw
|
|
81
|
-
response: { status: response.status, statusText: response.statusText }
|
|
82
|
-
});
|
|
131
|
+
throw apiError;
|
|
83
132
|
}
|
|
84
133
|
|
|
85
134
|
if (retry >= 0 && attempt >= retry) {
|
|
86
|
-
throw Object.assign(
|
|
87
|
-
response: { status: response.status, statusText: response.statusText },
|
|
88
|
-
__retryCount: attempt
|
|
89
|
-
});
|
|
135
|
+
throw Object.assign(apiError, { __retryCount: attempt });
|
|
90
136
|
}
|
|
91
137
|
|
|
138
|
+
const honourRetryAfter = response.status === 429 || response.status === 503;
|
|
139
|
+
const delay = honourRetryAfter && apiError.retryAfterMs !== undefined
|
|
140
|
+
? Math.min(apiError.retryAfterMs, maxRetryDelayMs, MAX_TIMER_MS)
|
|
141
|
+
: backoffDelay(attempt, retryDelayMs, maxRetryDelayMs);
|
|
92
142
|
attempt += 1;
|
|
93
|
-
await
|
|
143
|
+
await abortableSleep(delay, req.signal);
|
|
94
144
|
} catch (error) {
|
|
95
145
|
clearTimeout(timer);
|
|
146
|
+
if (req.signal?.aborted) {
|
|
147
|
+
// the caller cancelled: never retry
|
|
148
|
+
throw abortReason(req.signal);
|
|
149
|
+
}
|
|
96
150
|
const status = (error as { response?: { status?: number } })?.response?.status;
|
|
97
151
|
if (status !== undefined && stopStatusCodes.includes(status)) {
|
|
98
152
|
throw error;
|
|
@@ -101,10 +155,15 @@ export async function erFetch<T = unknown>(req: ErHttpRequest): Promise<ErHttpRe
|
|
|
101
155
|
throw error;
|
|
102
156
|
}
|
|
103
157
|
if (retry >= 0 && attempt >= retry) {
|
|
158
|
+
// Rethrow the original thrown value (it may not be an Error) with the retry count attached.
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
104
160
|
throw Object.assign(error as object, { __retryCount: attempt });
|
|
105
161
|
}
|
|
162
|
+
const delay = backoffDelay(attempt, retryDelayMs, maxRetryDelayMs);
|
|
106
163
|
attempt += 1;
|
|
107
|
-
await
|
|
164
|
+
await abortableSleep(delay, req.signal);
|
|
165
|
+
} finally {
|
|
166
|
+
req.signal?.removeEventListener("abort", onAbort);
|
|
108
167
|
}
|
|
109
168
|
}
|
|
110
169
|
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,10 @@ export {
|
|
|
9
9
|
ArticleMapper
|
|
10
10
|
} from "./eventRegistry";
|
|
11
11
|
export { sleep, mainLangs, allLangs } from "./base";
|
|
12
|
+
export { ErApiError, ErBudgetExceededError, ErTimeoutError, ErValidationError } from "./errors";
|
|
13
|
+
export { validateResponse } from "./guards";
|
|
14
|
+
export { Usage } from "./usage";
|
|
15
|
+
export type { LastCallUsage } from "./usage";
|
|
12
16
|
export { ER } from "./types";
|
|
13
17
|
export {
|
|
14
18
|
GetSourceInfo,
|
|
@@ -103,6 +107,7 @@ export {
|
|
|
103
107
|
GetRecentEvents,
|
|
104
108
|
GetRecentArticles
|
|
105
109
|
} from "./recent";
|
|
110
|
+
export type { RecentCursor } from "./recent";
|
|
106
111
|
export {
|
|
107
112
|
ReturnInfo,
|
|
108
113
|
ArticleInfoFlags,
|
|
@@ -155,6 +160,8 @@ export {
|
|
|
155
160
|
export { getCounts, getCountsEx } from "./helpers/counts";
|
|
156
161
|
export { getTopSharedArticles, getTopSharedEvents } from "./helpers/shares";
|
|
157
162
|
export { getRecentEvents, getRecentArticles } from "./helpers/recent";
|
|
163
|
+
export { RecentFeed, recentArticlesFeed, recentEventsFeed } from "./recentFeed";
|
|
164
|
+
export type { RecentFeedOptions } from "./recentFeed";
|
|
158
165
|
export {
|
|
159
166
|
getSourceInfo,
|
|
160
167
|
getConceptInfo,
|
package/src/logger.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as path from "path";
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
3
|
|
|
4
4
|
export enum LogLevel {
|
|
5
5
|
ERROR = 0,
|
|
@@ -101,6 +101,11 @@ export class Logger {
|
|
|
101
101
|
this.log(LogLevel.WARN, message);
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/** Warn that a constructor received parameters it does not know, e.g. a typo in a filter name. */
|
|
105
|
+
public static warnUnsupported(owner: string, unsupported: object): void {
|
|
106
|
+
this.warn(`${owner}: Unsupported parameters detected: ${JSON.stringify(unsupported)}. Please check the documentation.`);
|
|
107
|
+
}
|
|
108
|
+
|
|
104
109
|
public static debug(message: string): void {
|
|
105
110
|
this.ensureStaticInstance().debug(message);
|
|
106
111
|
}
|
package/src/mutex.ts
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
|
+
import { abortReason } from "./abort";
|
|
2
|
+
|
|
1
3
|
export class Mutex {
|
|
2
4
|
private locked = false;
|
|
3
5
|
private readonly waiters: (() => void)[] = [];
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
/** Resolves when the lock is held. If `signal` aborts while queued, rejects with its reason and holds nothing. */
|
|
8
|
+
public async acquire(signal?: AbortSignal): Promise<void> {
|
|
9
|
+
signal?.throwIfAborted();
|
|
6
10
|
if (!this.locked) {
|
|
7
11
|
this.locked = true;
|
|
8
12
|
return;
|
|
9
13
|
}
|
|
10
|
-
await new Promise<void>((resolve) =>
|
|
14
|
+
await new Promise<void>((resolve, reject) => {
|
|
15
|
+
const waiter = () => {
|
|
16
|
+
signal?.removeEventListener("abort", onAbort);
|
|
17
|
+
resolve();
|
|
18
|
+
};
|
|
19
|
+
const onAbort = () => {
|
|
20
|
+
const index = this.waiters.indexOf(waiter);
|
|
21
|
+
if (index >= 0) {
|
|
22
|
+
this.waiters.splice(index, 1);
|
|
23
|
+
}
|
|
24
|
+
reject(abortReason(signal));
|
|
25
|
+
};
|
|
26
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
27
|
+
this.waiters.push(waiter);
|
|
28
|
+
});
|
|
11
29
|
this.locked = true;
|
|
12
30
|
}
|
|
13
31
|
|
package/src/query.ts
CHANGED
|
@@ -198,10 +198,10 @@ export class ComplexArticleQuery extends QueryCore {
|
|
|
198
198
|
if (args.dataType !== "news") {
|
|
199
199
|
filter["dataType"] = args.dataType;
|
|
200
200
|
}
|
|
201
|
-
if (args.minSentiment) {
|
|
201
|
+
if (args.minSentiment != null) {
|
|
202
202
|
filter["minSentiment"] = args.minSentiment;
|
|
203
203
|
}
|
|
204
|
-
if (args.maxSentiment) {
|
|
204
|
+
if (args.maxSentiment != null) {
|
|
205
205
|
filter["maxSentiment"] = args.maxSentiment;
|
|
206
206
|
}
|
|
207
207
|
if ((args.minSocialScore ?? 0) > 0) {
|
|
@@ -245,10 +245,10 @@ export class ComplexEventQuery extends QueryCore {
|
|
|
245
245
|
}
|
|
246
246
|
this.queryObj["$query"] = query.getQuery();
|
|
247
247
|
const filter: Record<string, unknown> = {};
|
|
248
|
-
if (args?.minSentiment) {
|
|
248
|
+
if (args?.minSentiment != null) {
|
|
249
249
|
filter["minSentiment"] = args.minSentiment;
|
|
250
250
|
}
|
|
251
|
-
if (args?.maxSentiment) {
|
|
251
|
+
if (args?.maxSentiment != null) {
|
|
252
252
|
filter["maxSentiment"] = args.maxSentiment;
|
|
253
253
|
}
|
|
254
254
|
if (Object.keys(filter).length > 0) {
|
package/src/queryArticle.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from "./logger";
|
|
1
2
|
import { Query } from "./base";
|
|
2
3
|
import { ArticleInfoFlags, ReturnInfo } from "./returnInfo";
|
|
3
4
|
import { ER } from "./types";
|
|
@@ -71,7 +72,7 @@ export class RequestArticleSimilarArticles extends RequestArticle {
|
|
|
71
72
|
...unsupported
|
|
72
73
|
} = args;
|
|
73
74
|
if (Object.keys(unsupported).length > 0) {
|
|
74
|
-
|
|
75
|
+
Logger.warnUnsupported("RequestArticleSimilarArticles", unsupported);
|
|
75
76
|
}
|
|
76
77
|
if (page < 1) {
|
|
77
78
|
throw new RangeError("page has to be >= 1");
|
|
@@ -105,7 +106,7 @@ export class RequestArticleDuplicatedArticles extends RequestArticle {
|
|
|
105
106
|
...unsupported
|
|
106
107
|
} = args;
|
|
107
108
|
if (Object.keys(unsupported).length > 0) {
|
|
108
|
-
|
|
109
|
+
Logger.warnUnsupported("RequestArticleDuplicatedArticles", unsupported);
|
|
109
110
|
}
|
|
110
111
|
if (page < 1) {
|
|
111
112
|
throw new RangeError("page has to be >= 1");
|
package/src/queryArticles.ts
CHANGED
|
@@ -52,9 +52,9 @@ export class QueryArticles extends Query<RequestArticles> {
|
|
|
52
52
|
...unsupported
|
|
53
53
|
} = args;
|
|
54
54
|
if (Object.keys(unsupported).length > 0) {
|
|
55
|
-
const unsupportedKeys = ["sortBy", "sortByAsc", "returnInfo", "maxItems"];
|
|
55
|
+
const unsupportedKeys = ["sortBy", "sortByAsc", "returnInfo", "maxItems", "signal", "timeoutMs"];
|
|
56
56
|
if (!Object.keys(unsupported).some(key => unsupportedKeys.includes(key))) {
|
|
57
|
-
|
|
57
|
+
Logger.warnUnsupported("QueryArticles", unsupported);
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
this.setVal("action", "getArticles");
|
|
@@ -170,7 +170,7 @@ export class QueryArticles extends Query<RequestArticles> {
|
|
|
170
170
|
try {
|
|
171
171
|
JSON.parse(complexQuery);
|
|
172
172
|
} catch {
|
|
173
|
-
|
|
173
|
+
Logger.error("Failed to parse the provided string content as a JSON object. Please check the content provided as a parameter to the initWithComplexQuery() method");
|
|
174
174
|
}
|
|
175
175
|
query.setVal("query", complexQuery);
|
|
176
176
|
} else if (typeof complexQuery === "object" && complexQuery !== null) {
|
|
@@ -195,9 +195,10 @@ export class QueryArticlesIter extends QueryArticles implements AsyncIterable<Da
|
|
|
195
195
|
returnInfo = undefined,
|
|
196
196
|
maxItems = -1
|
|
197
197
|
} = args;
|
|
198
|
-
this.er = er;
|
|
198
|
+
this.er = args.signal || args.timeoutMs ? er.withOptions({ signal: args.signal, timeoutMs: args.timeoutMs }) : er;
|
|
199
199
|
this.engine = new QueryIterationEngine<Data.Article>({
|
|
200
200
|
maxItems,
|
|
201
|
+
signal: args.signal,
|
|
201
202
|
entityLabel: "articles",
|
|
202
203
|
onError: (message) => this.er.logger.error(message),
|
|
203
204
|
fetchPage: async (page) => {
|
|
@@ -270,7 +271,7 @@ export class RequestArticlesInfo extends RequestArticles {
|
|
|
270
271
|
...unsupported
|
|
271
272
|
} = parameters;
|
|
272
273
|
if (Object.keys(unsupported).length !== 0) {
|
|
273
|
-
Logger.
|
|
274
|
+
Logger.warnUnsupported("RequestArticlesInfo", unsupported);
|
|
274
275
|
}
|
|
275
276
|
if (page < 1) {
|
|
276
277
|
throw new RangeError("page has to be >= 1");
|
|
@@ -303,7 +304,7 @@ export class RequestArticlesUriWgtList extends RequestArticles {
|
|
|
303
304
|
} = parameters;
|
|
304
305
|
|
|
305
306
|
if (Object.keys(unsupported).length !== 0) {
|
|
306
|
-
Logger.
|
|
307
|
+
Logger.warnUnsupported("RequestArticlesUriWgtList", unsupported);
|
|
307
308
|
}
|
|
308
309
|
if (page < 1) {
|
|
309
310
|
throw new RangeError("page has to be >= 1");
|
|
@@ -340,10 +341,16 @@ export class RequestArticlesConceptAggr extends RequestArticles {
|
|
|
340
341
|
articlesSampleSize = 10000,
|
|
341
342
|
returnInfo = new ReturnInfo(),
|
|
342
343
|
...unsupported
|
|
344
|
+
}: {
|
|
345
|
+
conceptCount?: number;
|
|
346
|
+
conceptCountPerType?: number;
|
|
347
|
+
conceptScoring?: string;
|
|
348
|
+
articlesSampleSize?: number;
|
|
349
|
+
returnInfo?: ReturnInfo;
|
|
343
350
|
} = {}) {
|
|
344
351
|
super();
|
|
345
352
|
if (Object.keys(unsupported).length !== 0) {
|
|
346
|
-
Logger.
|
|
353
|
+
Logger.warnUnsupported("RequestArticlesConceptAggr", unsupported);
|
|
347
354
|
}
|
|
348
355
|
if (conceptCount > 500) {
|
|
349
356
|
throw new RangeError("At most 500 concepts can be returned per call");
|
|
@@ -371,7 +378,7 @@ export class RequestArticlesCategoryAggr extends RequestArticles {
|
|
|
371
378
|
} = {}) {
|
|
372
379
|
super();
|
|
373
380
|
if (Object.keys(unsupported).length !== 0) {
|
|
374
|
-
Logger.
|
|
381
|
+
Logger.warnUnsupported("RequestArticlesCategoryAggr", unsupported);
|
|
375
382
|
}
|
|
376
383
|
if (articlesSampleSize > 50000) {
|
|
377
384
|
throw new RangeError("at most 50000 articles can be used for computation sample");
|
|
@@ -393,7 +400,7 @@ export class RequestArticlesSourceAggr extends RequestArticles {
|
|
|
393
400
|
} = {}) {
|
|
394
401
|
super();
|
|
395
402
|
if (Object.keys(unsupported).length !== 0) {
|
|
396
|
-
Logger.
|
|
403
|
+
Logger.warnUnsupported("RequestArticlesSourceAggr", unsupported);
|
|
397
404
|
}
|
|
398
405
|
this.params = {};
|
|
399
406
|
this.params["sourceAggrSourceCount"] = sourceCount;
|
|
@@ -413,7 +420,7 @@ export class RequestArticlesKeywordAggr extends RequestArticles {
|
|
|
413
420
|
} = {}) {
|
|
414
421
|
super();
|
|
415
422
|
if (Object.keys(unsupported).length !== 0) {
|
|
416
|
-
Logger.
|
|
423
|
+
Logger.warnUnsupported("RequestArticlesKeywordAggr", unsupported);
|
|
417
424
|
}
|
|
418
425
|
if (articlesSampleSize > 20000) {
|
|
419
426
|
throw new RangeError("at most 20000 articles can be used for computation sample");
|
|
@@ -435,7 +442,7 @@ export class RequestArticlesConceptGraph extends RequestArticles {
|
|
|
435
442
|
} = {}) {
|
|
436
443
|
super();
|
|
437
444
|
if (Object.keys(unsupported).length !== 0) {
|
|
438
|
-
Logger.
|
|
445
|
+
Logger.warnUnsupported("RequestArticlesConceptGraph", unsupported);
|
|
439
446
|
}
|
|
440
447
|
if (conceptCount > 1000) {
|
|
441
448
|
throw new RangeError("At most 1000 concepts can be returned per call");
|
|
@@ -466,7 +473,7 @@ export class RequestArticlesConceptMatrix extends RequestArticles {
|
|
|
466
473
|
} = {}) {
|
|
467
474
|
super();
|
|
468
475
|
if (Object.keys(unsupported).length !== 0) {
|
|
469
|
-
Logger.
|
|
476
|
+
Logger.warnUnsupported("RequestArticlesConceptMatrix", unsupported);
|
|
470
477
|
}
|
|
471
478
|
if (conceptCount > 200) {
|
|
472
479
|
throw new RangeError("At most 200 concepts can be returned per call");
|
|
@@ -495,7 +502,7 @@ export class RequestArticlesConceptTrends extends RequestArticles {
|
|
|
495
502
|
...unsupported
|
|
496
503
|
} = parameters;
|
|
497
504
|
if (Object.keys(unsupported).length !== 0) {
|
|
498
|
-
Logger.
|
|
505
|
+
Logger.warnUnsupported("RequestArticlesConceptTrends", unsupported);
|
|
499
506
|
}
|
|
500
507
|
if (conceptCount > 50) {
|
|
501
508
|
throw new RangeError("At most 50 concepts can be returned per call");
|
|
@@ -523,6 +530,7 @@ export class RequestArticlesRecentActivity extends RequestArticles {
|
|
|
523
530
|
constructor({maxArticleCount = 100,
|
|
524
531
|
updatesAfterNewsUri = undefined,
|
|
525
532
|
updatesafterBlogUri = undefined,
|
|
533
|
+
updatesAfterBlogUri = updatesafterBlogUri,
|
|
526
534
|
updatesAfterPrUri = undefined,
|
|
527
535
|
updatesAfterTm = undefined,
|
|
528
536
|
updatesAfterMinsAgo = undefined,
|
|
@@ -534,7 +542,7 @@ export class RequestArticlesRecentActivity extends RequestArticles {
|
|
|
534
542
|
}: ER.RequestArticlesRecentActivityParameters = {}) {
|
|
535
543
|
super();
|
|
536
544
|
if (Object.keys(unsupported).length !== 0) {
|
|
537
|
-
Logger.
|
|
545
|
+
Logger.warnUnsupported("RequestArticlesRecentActivity", unsupported);
|
|
538
546
|
}
|
|
539
547
|
if (maxArticleCount > 2000) {
|
|
540
548
|
throw new RangeError("At most 2000 articles can be returned per call");
|
|
@@ -562,11 +570,11 @@ export class RequestArticlesRecentActivity extends RequestArticles {
|
|
|
562
570
|
if (updatesAfterNewsUri !== undefined) {
|
|
563
571
|
this.params["recentActivityArticlesNewsUpdatesAfterUri"] = updatesAfterNewsUri;
|
|
564
572
|
}
|
|
565
|
-
if (
|
|
566
|
-
this.params["
|
|
573
|
+
if (updatesAfterBlogUri !== undefined) {
|
|
574
|
+
this.params["recentActivityArticlesBlogUpdatesAfterUri"] = updatesAfterBlogUri;
|
|
567
575
|
}
|
|
568
576
|
if (updatesAfterPrUri !== undefined) {
|
|
569
|
-
this.params["
|
|
577
|
+
this.params["recentActivityArticlesPrUpdatesAfterUri"] = updatesAfterPrUri;
|
|
570
578
|
}
|
|
571
579
|
this.params["recentActivityArticlesMandatorySourceLocation"] = mandatorySourceLocation;
|
|
572
580
|
if (returnInfo) {
|
package/src/queryEvent.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isCancellation } from "./abort";
|
|
1
2
|
import { Query, QueryParamsBase } from "./base";
|
|
2
3
|
import { Data } from "./data";
|
|
3
4
|
import { EventRegistry } from "./eventRegistry";
|
|
@@ -39,6 +40,7 @@ export class QueryEvent extends Query<RequestEvent> {
|
|
|
39
40
|
*/
|
|
40
41
|
export class QueryEventArticlesIter extends QueryEvent implements AsyncIterable<Data.Article> {
|
|
41
42
|
private readonly er: EventRegistry;
|
|
43
|
+
private readonly signal?: AbortSignal;
|
|
42
44
|
private readonly sortBy: ER.QueryEvent.SortByOptions;
|
|
43
45
|
private readonly sortByAsc: boolean;
|
|
44
46
|
private readonly returnInfo: ReturnInfo | undefined;
|
|
@@ -85,7 +87,8 @@ export class QueryEventArticlesIter extends QueryEvent implements AsyncIterable<
|
|
|
85
87
|
minSentiment = -1,
|
|
86
88
|
maxSentiment = 1
|
|
87
89
|
} = args;
|
|
88
|
-
this.er = er;
|
|
90
|
+
this.er = args.signal || args.timeoutMs ? er.withOptions({ signal: args.signal, timeoutMs: args.timeoutMs }) : er;
|
|
91
|
+
this.signal = args.signal;
|
|
89
92
|
this.sortBy = sortBy;
|
|
90
93
|
this.sortByAsc = sortByAsc;
|
|
91
94
|
this.returnInfo = returnInfo;
|
|
@@ -153,6 +156,7 @@ export class QueryEventArticlesIter extends QueryEvent implements AsyncIterable<
|
|
|
153
156
|
[Symbol.asyncIterator](): AsyncIterator<Data.Article> {
|
|
154
157
|
return {
|
|
155
158
|
next: async () => {
|
|
159
|
+
this.signal?.throwIfAborted();
|
|
156
160
|
if (this.index >= this.items.length) {
|
|
157
161
|
await this.getNextBatch();
|
|
158
162
|
}
|
|
@@ -179,7 +183,7 @@ export class QueryEventArticlesIter extends QueryEvent implements AsyncIterable<
|
|
|
179
183
|
public execQuery(callback: (item: Data.Article) => void, doneCallback?: (error?: string) => void): void {
|
|
180
184
|
if (callback) { this.callback = callback; }
|
|
181
185
|
if (doneCallback) { this.doneCallback = doneCallback; }
|
|
182
|
-
this.iterate();
|
|
186
|
+
this.iterate().catch((error: unknown) => this.doneCallback(error instanceof Error ? error.message : String(error)));
|
|
183
187
|
}
|
|
184
188
|
|
|
185
189
|
private async iterate(): Promise<void> {
|
|
@@ -236,6 +240,9 @@ export class QueryEventArticlesIter extends QueryEvent implements AsyncIterable<
|
|
|
236
240
|
this.items = [...this.items, ...results];
|
|
237
241
|
return true;
|
|
238
242
|
} catch (error) {
|
|
243
|
+
if (isCancellation(error, this.signal)) {
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
239
246
|
this.er.logger.error(error instanceof Error ? error.message : String(error));
|
|
240
247
|
return false;
|
|
241
248
|
}
|
|
@@ -370,7 +377,7 @@ export class RequestEventArticleUriWgts extends RequestEvent {
|
|
|
370
377
|
...unsupported
|
|
371
378
|
} = args;
|
|
372
379
|
if (Object.keys(unsupported).length > 0) {
|
|
373
|
-
Logger.
|
|
380
|
+
Logger.warnUnsupported("RequestEventArticleUriWgts", unsupported);
|
|
374
381
|
}
|
|
375
382
|
this.params = {};
|
|
376
383
|
if (lang !== undefined) {
|
|
@@ -432,7 +439,7 @@ export class RequestEventArticleTrend extends RequestEvent {
|
|
|
432
439
|
...unsupported
|
|
433
440
|
} = args;
|
|
434
441
|
if (Object.keys(unsupported).length > 0) {
|
|
435
|
-
Logger.
|
|
442
|
+
Logger.warnUnsupported("RequestEventArticleTrend", unsupported);
|
|
436
443
|
}
|
|
437
444
|
if (page < 1) {
|
|
438
445
|
throw new RangeError("Page has to be >= 1");
|
|
@@ -472,7 +479,7 @@ export class RequestEventSimilarEvents extends RequestEvent {
|
|
|
472
479
|
...unsupported
|
|
473
480
|
} = args;
|
|
474
481
|
if (Object.keys(unsupported).length > 0) {
|
|
475
|
-
Logger.
|
|
482
|
+
Logger.warnUnsupported("RequestEventSimilarEvents", unsupported);
|
|
476
483
|
}
|
|
477
484
|
if (count > 50) {
|
|
478
485
|
throw new RangeError("At most 50 similar events can be returned per call");
|