-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.ts
More file actions
732 lines (664 loc) · 23.9 KB
/
common.ts
File metadata and controls
732 lines (664 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import { OramaClient } from "npm:@oramacloud/client@2.1.4";
import denoJson from "./deno.json" with { type: "json" };
// Orama Configuration
export const ORAMA_INDEX_ID = "jsr-j7uqzz";
export const ORAMA_PUBLIC_KEY = "rdpUADH0pFZIEx9xLyLIkPGTP4ypc9Wq";
export const ORAMA_ENDPOINT = `https://cloud.orama.run/v1/indexes/${ORAMA_INDEX_ID}`;
// JSR API configuration
export const JSR_API_BASE = "https://api.jsr.io";
export const JSR_WEB_BASE = "https://jsr.io";
const TOOL_USER_AGENT = `@orgsoft:jsr/${denoJson.version}; https://github.com/orgsofthq/jsr`;
const fetchWrapper = async (url: string): Promise<ReturnType<typeof fetch>> => {
const response = await fetch(url, {
headers: {
// To respect https://jsr.io/docs/api requirements
"User-Agent": TOOL_USER_AGENT,
"Accept": "application/json, text/plain"
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response;
};
/**
* Represents a single hit from an Orama search result.
*/
export interface OramaHit {
/** The matched document, which is a JSR package */
document: PackageInfo;
/** Relevance score for the search result (0-1) */
score: number;
}
/**
* Enhanced Orama hit with download and version information
*/
export interface EnhancedOramaHit extends OramaHit {
/** Download statistics for this package */
downloads?: DownloadSummary;
}
/**
* Defines the structure of search results returned from Orama.
*/
export interface OramaSearchResult {
/** Number of search hits. */
count: number;
/** Array of search hits. */
hits: OramaHit[];
/** Error message if the search operation failed. */
error?: string;
}
/**
* Enhanced search results with download and version information
*/
export interface EnhancedOramaSearchResult {
/** Number of search hits. */
count: number;
/** Array of enhanced search hits with download data. */
hits: EnhancedOramaHit[];
/** Error message if the search operation failed. */
error?: string;
}
/**
* Options for configuring an Orama search query.
*/
export interface OramaSearchOptions {
limit?: number;
offset?: number;
mode?: "fulltext" | "vector" | "hybrid";
boost?: Record<string, number>;
includeDownloads?: boolean;
// Add other valid search parameters as needed
}
/**
* Result structure returned from Orama search operations
*/
export interface SearchResult {
/** Array of search hits with documents and relevance scores */
hits?: OramaHit[];
/** Total number of results found */
count?: number;
/** Error message if the search operation failed */
error?: string;
}
/**
* Complete package information structure for JSR packages
*/
export interface PackageInfo {
/** Unique package identifier */
id: string;
/** Package name without scope */
name: string;
/** Package scope (e.g., "std", "orgsoft") */
scope: string;
/** Package description */
description: string;
/** Search relevance score when used in search results */
score?: number;
/** Runtime compatibility matrix (deno, node, browser, etc.) */
runtimeCompat?: Record<string, boolean>;
/** Latest published version string */
latestVersion?: string;
/** Total number of published versions */
versionCount?: number;
/** Number of dependencies this package has */
dependencyCount?: number;
/** Number of packages that depend on this one */
dependentCount?: number;
/** GitHub repository information if available */
githubRepository?: {
/** Repository owner/organization */
owner: string;
/** Repository name */
name: string;
};
/** ISO timestamp of last update */
updatedAt?: string;
/** ISO timestamp of package creation */
createdAt?: string;
/** Whether the package is archived */
isArchived?: boolean;
/** README source */
readmeSource?: string;
/** When featured */
whenFeatured?: string;
/** Latest version */
latest?: string;
}
/**
* Metadata for a specific JSR package version.
*/
export interface JsrPackageVersionMeta {
yanked?: boolean;
}
/**
* Top-level metadata for a JSR package from meta.json.
*/
export interface JsrPackageMeta {
latest: string;
versions: Record<string, JsrPackageVersionMeta>;
}
/**
* Represents a single data point in a download timeline.
*/
export interface JsrDownloadPoint {
timeBucket: string;
count: number;
}
/**
* Represents download statistics for a specific package version.
*/
export interface JsrVersionDownloads {
version: string;
downloads: JsrDownloadPoint[];
}
/**
* Structure of the download statistics returned by the JSR API.
*/
export interface JsrDownloads {
total: JsrDownloadPoint[];
recentVersions: JsrVersionDownloads[];
}
/**
* Comprehensive download statistics with rolling window calculations
*/
export interface DownloadSummary {
/** Total downloads across all versions and time periods */
totalDownloads: number;
/** Downloads in the most recent 30 data points */
recentDownloads: number;
/** Version string of the latest published version */
latestVersion: string;
/** Total downloads for the latest version */
latestVersionDownloads: number;
/** Number of data points in the activity timeline */
activityEntries: number;
/** ISO timestamp of the most recent download activity */
lastActivity?: string;
/** Average downloads per day over the last 7 days */
last7DaysAverage: number;
/** Overall weekly average based on total history */
weeklyAverage: number;
/** Overall monthly average based on total history */
monthlyAverage: number;
/** Download sums for each of the last 4 weeks */
weeklySums: number[];
/** Download sums for each of the last 6 months */
monthlySums: number[];
/** Error message if download data could not be retrieved */
error?: string;
}
/**
* Creates and configures an Orama Cloud client for JSR package search
*
* @returns Configured OramaClient instance ready for search operations
* @example
* ```typescript
* const client = createOramaClient();
* const results = await client.search({ term: "web framework" });
* ```
*/
export function createOramaClient(): OramaClient {
return new OramaClient({
endpoint: ORAMA_ENDPOINT,
api_key: ORAMA_PUBLIC_KEY,
});
}
/**
* Performs a direct search query against the Orama Cloud JSR package index
*
* @param query - Search query string (natural language or keywords)
* @param options - Search options including limit, offset, and other parameters
* @param options.limit - Maximum number of results to return (default: 20)
* @param options.offset - Number of results to skip (default: 0)
* @param options.includeDownloads - Whether to fetch download statistics for each package (default: true)
* @returns Promise resolving to search results, optionally enhanced with download data
* @throws Error if the search request fails
* @example
* ```typescript
* const results = await queryOrama("web framework", { limit: 10, includeDownloads: true });
* console.log(results.hits.map(hit => hit.document.name));
* ```
*/
export async function queryOrama(
query: string,
options: OramaSearchOptions = {},
): Promise<OramaSearchResult | EnhancedOramaSearchResult> {
const client = createOramaClient();
const searchParams = {
term: query,
limit: options.limit || 20,
offset: options.offset || 0,
boost: {
id: 3,
scope: 2,
name: 1,
description: 0.5,
},
mode: options.mode || "fulltext",
...options
};
try {
// @ts-ignore boost property works but not in types when mode is non-fulltext
const res = await client.search(searchParams);
if (!res) {
return {
count: 0,
hits: [],
error: "Search failed: received null response"
};
}
const basicResult = res as OramaSearchResult;
// If downloads explicitly disabled, return basic result
if (options.includeDownloads === false) {
return basicResult;
}
// Enhance results with download information (default behavior)
const enhancedHits: EnhancedOramaHit[] = [];
if (basicResult.hits?.length) {
// Fetch download data for all packages in parallel
const downloadPromises = basicResult.hits.map(async (hit): Promise<EnhancedOramaHit> => {
try {
const downloads = await getPackageDownloadSummary(hit.document.scope, hit.document.name);
return {
...hit,
downloads: downloads.error ? undefined : downloads
};
} catch (_error: unknown) {
// If download fetch fails, return hit without download data
return { ...hit };
}
});
const results = await Promise.all(downloadPromises);
enhancedHits.push(...results);
}
return {
count: basicResult.count,
hits: enhancedHits,
error: basicResult.error
} as EnhancedOramaSearchResult;
} catch (error: unknown) {
// Handle API errors gracefully
if (error instanceof Error && "httpResponse" in error) {
const response = (error as { httpResponse: Response }).httpResponse;
let errorBody = "Unknown error";
try {
const text = await response.text();
errorBody = text || `HTTP ${response.status}: ${response.statusText}`;
} catch {
errorBody = `HTTP ${response.status}: ${response.statusText}`;
}
return {
count: 0,
hits: [],
error: `Orama API error: ${errorBody}`
};
}
return {
count: 0,
hits: [],
error: `Search failed: ${getErrorMessage(error)}`
};
}
}
/**
* Performs enhanced fulltext search with optimized field boosting for better relevance
*
* @param query - Search query string
* @param options - Search options including limit and other parameters
* @param options.limit - Maximum number of results to return (default: 10)
* @param options.includeDownloads - Whether to fetch download statistics for each package (default: true)
* @returns Promise resolving to search results with improved relevance scoring, optionally enhanced with download data
* @example
* ```typescript
* const results = await relevanceSearch("http client", { limit: 5 });
* ```
*/
export async function relevanceSearch(
query: string,
options: OramaSearchOptions = {},
): Promise<OramaSearchResult | EnhancedOramaSearchResult> {
return await queryOrama(query, {
...options,
mode: "fulltext",
limit: options.limit || 10,
includeDownloads: options.includeDownloads !== false, // Default to true
boost: {
description: 2.0,
name: 1.5,
scope: 0.8,
id: 0.3
}
});
}
/**
* Finds packages similar to a given package using semantic search with download and version info
*
* @param packageName - Name of the package to find similar packages for (with or without scope)
* @param options - Search options including limit
* @param options.limit - Maximum number of similar packages to return (default: 6)
* @param options.includeDownloads - Whether to fetch download statistics for each package (default: true)
* @returns Promise resolving to array of similar packages with relevance scores, downloads, and version info
* @example
* ```typescript
* const similar = await findSimilarPackages("@std/http", { limit: 5 });
* console.log(similar.hits.map(hit => `${hit.document.name} - ${hit.downloads?.totalDownloads} downloads`));
* ```
*/
export async function findSimilarPackages(
packageName: string,
options: OramaSearchOptions & { includeDownloads?: boolean } = {},
): Promise<EnhancedOramaSearchResult & { originalPackage?: PackageInfo }> {
const includeDownloads = options.includeDownloads !== false; // Default to true
// First get the package details
const packageResult = await queryOrama(packageName, {
limit: 1,
boost: { name: 5, scope: 3, id: 2 }
});
if (!packageResult?.hits?.length) {
return { hits: [], count: 0, error: "Package not found" };
}
const pkg = packageResult.hits[0].document;
const searchQuery = `${pkg.name} ${pkg.description}`.trim();
// Find similar packages using enhanced fulltext search
const similarResults = await queryOrama(searchQuery, {
limit: (options.limit || 10) + 1,
boost: {
description: 2.5,
name: 1.8,
scope: 1.0,
id: 0.2
},
...options
});
// Filter out the original package
const filtered = similarResults?.hits?.filter(
(hit: OramaHit) => hit.document.id !== pkg.id
) || [];
const limitedResults = filtered.slice(0, options.limit || 10);
// Enhance results with download information if requested
const enhancedHits: EnhancedOramaHit[] = [];
if (includeDownloads) {
// Fetch download data for all packages in parallel
const downloadPromises = limitedResults.map(async (hit): Promise<EnhancedOramaHit> => {
try {
const downloads = await getPackageDownloadSummary(hit.document.scope, hit.document.name);
return {
...hit,
downloads: downloads.error ? undefined : downloads
};
} catch (_error: unknown) {
// If download fetch fails, return hit without download data
return { ...hit };
}
});
const results = await Promise.all(downloadPromises);
enhancedHits.push(...results);
} else {
// If downloads not requested, just convert to enhanced format
enhancedHits.push(...limitedResults.map(hit => ({ ...hit })));
}
return {
hits: enhancedHits,
count: enhancedHits.length,
originalPackage: pkg
};
}
/**
* Get error message from an error
*
* @param error - The error to get the message from
* @returns The error message
*/
export function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
/**
* Test search capabilities
*
* @returns Promise resolving to search capabilities
*/
export async function getSearchCapabilities(): Promise<{
query: string;
success: boolean;
count: number;
sample: string | null;
mode: string;
}> {
const testQuery = "http";
const result = await queryOrama(testQuery, { limit: 2 });
return {
query: testQuery,
success: true,
count: result.hits?.length || 0,
sample: result.hits?.[0]?.document?.name || null,
mode: "fulltext"
};
}
/**
* Fetch data from the JSR API
*
* @param endpoint - The API endpoint to fetch from
* @returns Promise resolving to the fetched data
*/
export async function fetchJSRAPI<T>(endpoint: string): Promise<T> {
const response = await fetchWrapper(`${JSR_API_BASE}${endpoint}`);
if (!response.ok) {
throw new Error(`JSR API error: ${response.status} ${response.statusText}`);
}
return response.json() as Promise<T>;
}
/**
* Fetch data from the JSR.io direct API
*
* @param endpoint - The API endpoint to fetch from
* @returns Promise resolving to the fetched data
*/
export async function fetchJSRDirect<T>(endpoint: string): Promise<T> {
const response = await fetch(`${JSR_WEB_BASE}${endpoint}`);
if (!response.ok) {
throw new Error(`JSR direct error: ${response.status} ${response.statusText}`);
}
return response.json() as Promise<T>;
}
/**
* Get exact package info from JSR.io direct API
*
* @param scope - The JSR scope name
* @param packageName - The package name within the scope
* @returns Promise resolving to the package info
*/
export async function getPackageInfoDirect(scope: string, packageName: string): Promise<PackageInfo> {
try {
return await fetchJSRDirect(`/@${scope}/${packageName}/meta.json`);
} catch (_error: unknown) {
// Fallback to old API method
return await getPackageDetails(scope, packageName);
}
}
/**
* Get package file contents
*
* @param scope - The JSR scope name
* @param packageName - The package name within the scope
* @param version - The package version
* @param filePath - The path to the file within the package
* @returns Promise resolving to the file contents as a string
*/
export async function getPackageFile(scope: string, packageName: string, version: string, filePath: string): Promise<string> {
const response = await fetchWrapper(`${JSR_WEB_BASE}/@${scope}/${packageName}/${version}/${filePath}`);
if (!response.ok) {
throw new Error(`File not found: ${response.status} ${response.statusText}`);
}
return response.text();
}
/**
* Check if package exists with exact matching
*
* @param scope - The JSR scope name
* @param packageName - The package name within the scope
* @returns Promise resolving to boolean indicating if the package exists
*/
export async function packageExists(scope: string, packageName: string): Promise<boolean> {
try {
const response = await fetchWrapper(`${JSR_WEB_BASE}/@${scope}/${packageName}/meta.json`);
return response.ok;
} catch {
return false;
}
}
/**
* Retrieves all packages within a JSR scope
*
* @param scope - The JSR scope name (e.g., "std", "orgsoft")
* @returns Promise resolving to array of packages in the scope
* @example
* ```typescript
* const stdPackages = await getScopePackages("std");
* ```
*/
export function getScopePackages(scope: string): Promise<{
items: PackageInfo[];
total: number;
}> {
return fetchJSRAPI(`/scopes/${scope}/packages`);
}
/**
* Retrieves detailed information about a specific JSR package
*
* @param scope - The JSR scope name
* @param packageName - The package name within the scope
* @returns Promise resolving to package details including versions, metadata, etc.
* @example
* ```typescript
* const packageInfo = await getPackageDetails("std", "http");
* ```
*/
export function getPackageDetails(scope: string, packageName: string): Promise<PackageInfo> {
return fetchJSRAPI(`/scopes/${scope}/packages/${packageName}`);
}
/**
* Retrieves raw download statistics for a JSR package
*
* @param scope - The JSR scope name
* @param packageName - The package name within the scope
* @returns Promise resolving to raw download data with timeline and version breakdowns
* @example
* ```typescript
* const downloads = await getPackageDownloads("std", "http");
* ```
*/
export function getPackageDownloads(scope: string, packageName: string): Promise<JsrDownloads> {
return fetchJSRAPI(`/scopes/${scope}/packages/${packageName}/downloads`);
}
/**
* Retrieves documentation for a JSR package using `deno doc`.
*
* @param module - The full JSR module name (e.g., "@std/path").
* @returns A promise that resolves to the documentation text.
* @throws An error if the `deno doc` command fails.
*/
export async function getPackageDocs(module: string): Promise<string> {
const command = new Deno.Command(Deno.execPath(), {
args: ["doc", "jsr:" + module],
env: { NO_COLOR: "1" },
});
const { stdout, stderr, success } = await command.output();
if (!success) {
const errorText = new TextDecoder().decode(stderr);
throw new Error(`Failed to get docs for ${module}: ${errorText}`);
}
return new TextDecoder().decode(stdout);
}
/**
* Calculates comprehensive download statistics with rolling window averages
*
* Processes raw download data to provide meaningful metrics including:
* - Total and recent download counts
* - Rolling averages for different time windows
* - Weekly and monthly trend data
*
* @param scope - The JSR scope name
* @param packageName - The package name within the scope
* @returns Promise resolving to processed download summary with averages and trends
* @example
* ```typescript
* const summary = await getPackageDownloadSummary("std", "http");
* console.log(`Total downloads: ${summary.totalDownloads}`);
* console.log(`Weekly average: ${summary.weeklyAverage}`);
* console.log(`Recent weekly totals: ${summary.weeklySums.join(', ')}`);
* ```
*/
export async function getPackageDownloadSummary(scope: string, packageName: string): Promise<DownloadSummary> {
try {
const downloads = await getPackageDownloads(scope, packageName);
// Calculate total downloads
const totalDownloads = downloads.total?.reduce((sum: number, entry: JsrDownloadPoint) => sum + entry.count, 0) || 0;
// Get recent activity (last 30 entries)
const recentActivity = downloads.total?.slice(-30) || [];
const recentDownloads = recentActivity.reduce((sum: number, entry: JsrDownloadPoint) => sum + entry.count, 0);
// Calculate rolling window averages and sums
const totalData = downloads.total || [];
// Last 7 days average
const last7Days = totalData.slice(-7);
const last7DaysTotal = last7Days.reduce((sum: number, entry: JsrDownloadPoint) => sum + entry.count, 0);
const last7DaysAverage = last7Days.length > 0 ? Math.round(last7DaysTotal / last7Days.length) : 0;
// Overall averages
const totalEntries = totalData.length || 0;
const dailyAverage = totalEntries > 0 ? Math.round(totalDownloads / totalEntries) : 0;
const weeklyAverage = Math.round(dailyAverage * 7);
const monthlyAverage = Math.round(dailyAverage * 30);
// Weekly sums for last 4 weeks (7-day windows)
const weeklySums: number[] = [];
for (let i = 0; i < 4; i++) {
const weekStart = totalData.length - 7 - (i * 7);
const weekEnd = totalData.length - (i * 7);
if (weekStart >= 0) {
const weekData = totalData.slice(Math.max(0, weekStart), weekEnd);
const weekTotal = weekData.reduce((sum: number, entry: JsrDownloadPoint) => sum + entry.count, 0);
weeklySums.unshift(weekTotal);
}
}
// Monthly sums for last 6 months (30-day windows)
const monthlySums: number[] = [];
for (let i = 0; i < 6; i++) {
const monthStart = totalData.length - 30 - (i * 30);
const monthEnd = totalData.length - (i * 30);
if (monthStart >= 0) {
const monthData = totalData.slice(Math.max(0, monthStart), monthEnd);
const monthTotal = monthData.reduce((sum: number, entry: JsrDownloadPoint) => sum + entry.count, 0);
monthlySums.unshift(monthTotal);
}
}
// Get latest version downloads
const latestVersion = downloads.recentVersions?.[0];
const latestVersionDownloads = latestVersion?.downloads?.reduce((sum: number, entry: JsrDownloadPoint) => sum + entry.count, 0) || 0;
return {
totalDownloads,
recentDownloads,
latestVersion: latestVersion?.version || 'Unknown',
latestVersionDownloads,
activityEntries: recentActivity.length,
lastActivity: recentActivity[recentActivity.length - 1]?.timeBucket || undefined,
last7DaysAverage,
weeklyAverage,
monthlyAverage,
weeklySums,
monthlySums
};
} catch (_error: unknown) {
return {
error: getErrorMessage(_error),
totalDownloads: 0,
recentDownloads: 0,
latestVersion: 'Unknown',
latestVersionDownloads: 0,
activityEntries: 0,
last7DaysAverage: 0,
weeklyAverage: 0,
monthlyAverage: 0,
weeklySums: [],
monthlySums: []
};
}
}