-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtypes.ts
More file actions
659 lines (587 loc) · 14.6 KB
/
types.ts
File metadata and controls
659 lines (587 loc) · 14.6 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
// ---------------------------------------------------------
// Consolidated TypeScript Types
// ---------------------------------------------------------
import type { Context } from 'hono'
import type { D1Database } from '@cloudflare/workers-types'
// ---------------------------------------------------------
// Request Types
// ---------------------------------------------------------
export interface _LoginRequestBody {
name?: string
password?: string
email?: string
[key: string]: unknown
}
export interface _AuditRequestBody {
requires?: Record<string, string>
[key: string]: unknown
}
export interface _UpstreamPackageData {
'dist-tags'?: Record<string, string>
versions?: Record<string, unknown>
time?: Record<string, string>
[key: string]: unknown
}
export interface _DashboardData {
[key: string]: unknown
}
export interface RequestQueueMessage {
body: {
type: 'package_refresh' | 'version_refresh'
packageName?: string
spec?: string
upstream: string
options: Record<string, unknown>
}
ack(): void
retry(): void
}
export interface QueueBatch {
messages: RequestQueueMessage[]
}
// ---------------------------------------------------------
// Database Types
// ---------------------------------------------------------
export interface Package {
name: string
tags: string // JSON string containing Record<string, string>
lastUpdated?: string
origin: 'local' | 'upstream'
upstream?: string
cachedAt?: string
}
export interface ParsedPackage {
name: string
tags: Record<string, string>
lastUpdated: string | null
origin: string | null
upstream: string | null
cachedAt: string | null
}
export interface Version {
spec: string
manifest: string // JSON string containing PackageManifest
publishedAt?: string
origin: 'local' | 'upstream'
upstream?: string
cachedAt?: string
}
export interface ParsedVersion {
spec: string
version: string
manifest: Record<string, any>
published_at: string | null
origin: string | null
upstream: string | null
cachedAt: string | null
}
// ---------------------------------------------------------
// Authentication & Authorization Types
// ---------------------------------------------------------
export interface TokenScope {
values: string[]
types: {
pkg?: { read: boolean; write: boolean }
user?: { read: boolean; write: boolean }
}
}
export interface TokenAccess {
anyUser: boolean
specificUser: boolean
anyPackage: boolean
specificPackage: boolean
readAccess: boolean
writeAccess: boolean
methods: string[]
}
export interface AuthUser {
uuid: string | null
scope: TokenScope[] | null
token: string
}
// ---------------------------------------------------------
// Package & Manifest Types
// ---------------------------------------------------------
export interface PackageManifest {
name: string
version: string
description?: string
main?: string
module?: string
types?: string
bin?: Record<string, string> | string
scripts?: Record<string, string>
dependencies?: Record<string, string>
devDependencies?: Record<string, string>
peerDependencies?: Record<string, string>
optionalDependencies?: Record<string, string>
peerDependenciesMeta?: Record<string, { optional?: boolean }>
engines?: Record<string, string>
os?: string[]
cpu?: string[]
keywords?: string[]
author?: string | { name: string; email?: string; url?: string }
contributors?: (
| string
| { name: string; email?: string; url?: string }
)[]
license?: string
repository?:
| string
| { type: string; url: string; directory?: string }
bugs?: string | { url: string; email?: string }
homepage?: string
files?: string[]
publishConfig?: Record<string, any>
dist?: {
tarball: string
shasum?: string
integrity?: string
fileCount?: number
unpackedSize?: number
}
_id?: string
_rev?: string
_attachments?: Record<string, any>
[key: string]: any // Allow additional properties
}
export interface SlimmedManifest {
name: string
version: string
dependencies?: Record<string, string>
peerDependencies?: Record<string, string>
optionalDependencies?: Record<string, string>
peerDependenciesMeta?: Record<string, { optional?: boolean }>
bin?: Record<string, string> | string
engines?: Record<string, string>
dist: {
tarball: string
}
}
export interface Packument {
name: string
'dist-tags': Record<string, string>
versions: Record<string, SlimmedManifest>
time: Record<string, string> & {
modified: string
}
}
export interface PackageSpec {
name?: string
pkg?: string
scope?: string
}
// ---------------------------------------------------------
// Upstream & Configuration Types
// ---------------------------------------------------------
export interface UpstreamConfig {
type: 'local' | 'npm' | 'vsr' | 'jsr'
url: string
allowPublish?: boolean
}
export interface OriginConfig {
default: string
upstreams: Record<string, UpstreamConfig>
}
// ---------------------------------------------------------
// Cache Types
// ---------------------------------------------------------
export interface CacheOptions {
packumentTtlMinutes?: number
manifestTtlMinutes?: number
staleWhileRevalidateMinutes?: number
forceRefresh?: boolean
upstream?: string
}
export interface CacheResult<T> {
data?: T
package?: T // For package cache results
version?: T // For version cache results
fromCache: boolean
stale?: boolean
}
export interface CacheValidation {
valid: boolean
stale: boolean
data: any
}
export interface QueueMessage {
type: 'package_refresh' | 'version_refresh'
packageName?: string
spec?: string
upstream: string
timestamp: number
options: {
packumentTtlMinutes?: number
manifestTtlMinutes?: number
upstream?: string
}
}
// ---------------------------------------------------------
// Request Context Types
// ---------------------------------------------------------
export interface RequestContext {
protocol?: string
host?: string
upstream?: string
}
export interface DatabaseOperations {
// Package operations
getPackage(name: string): Promise<ParsedPackage | null>
upsertPackage(
name: string,
tags: Record<string, string>,
lastUpdated?: string,
): Promise<any>
upsertCachedPackage(
name: string,
tags: Record<string, string>,
upstream: string,
lastUpdated?: string,
): Promise<any>
getCachedPackage(name: string): Promise<ParsedPackage | null>
isPackageCacheValid(
name: string,
ttlMinutes?: number,
): Promise<boolean>
// API-key scope operations (Better Auth api-key plugin + companion
// `token_scopes` table). Implementations live in src/db/client.ts.
getScopesForKey(apiKeyId: string): Promise<TokenScope[]>
replaceScopesForKey(
apiKeyId: string,
scopes: TokenScope[],
): Promise<void>
addScopeForKey(
apiKeyId: string,
scope: {
target: 'pkg' | 'user'
value: string
read?: boolean
write?: boolean
},
): Promise<void>
removeScopeRows(
apiKeyId: string | null,
target: 'pkg' | 'user',
value: string,
): Promise<void>
getKeysForUser(userId: string): Promise<
Array<{
id: string
name: string | null
start: string | null
prefix: string | null
referenceId: string
enabled: boolean | null
createdAt: Date
updatedAt: Date
expiresAt: Date | null
}>
>
getUserIdByName(name: string): Promise<string | null>
listAllScopes(): Promise<
Array<{
apiKeyId: string
target: 'pkg' | 'user'
value: string
read: boolean
write: boolean
referenceId: string
}>
>
// Version operations
getVersion(spec: string): Promise<ParsedVersion | null>
upsertVersion(
spec: string,
manifest: PackageManifest,
publishedAt: string,
): Promise<any>
upsertCachedVersion(
spec: string,
manifest: PackageManifest,
upstream: string,
publishedAt: string,
): Promise<any>
getCachedVersion(spec: string): Promise<ParsedVersion | null>
isVersionCacheValid(
spec: string,
ttlMinutes?: number,
): Promise<boolean>
// Search operations
searchPackages(
query: string,
scope?: string,
): Promise<SearchResult[]>
getVersionsByPackage(packageName: string): Promise<ParsedVersion[]>
// npm per-version download snapshots (api.npmjs.org/versions/.../last-week)
getVersionDownloadsFetchedAt(
packageName: string,
period: string,
): Promise<string | null>
getVersionDownloadsSnapshot(
packageName: string,
period: string,
): Promise<{
fetchedAt: string
byVersion: Record<string, number>
} | null>
replaceVersionDownloads(
packageName: string,
period: string,
fetchedAt: string,
byVersion: Record<string, number>,
): Promise<void>
// Cached README markdown extracted from package tarballs
getPackageReadme(spec: string): Promise<{
spec: string
readme: string
filename: string
extractedAt: string
} | null>
upsertPackageReadme(
spec: string,
readme: string,
filename: string,
extractedAt: string,
): Promise<void>
}
export type HonoContext = Context<{
Bindings: Environment
Variables: {
db: DatabaseOperations
upstream?: string
}
}> & {
waitUntil?: (promise: Promise<any>) => void
executionCtx?: {
waitUntil: (promise: Promise<any>) => void
}
}
// ---------------------------------------------------------
// API Response Types
// ---------------------------------------------------------
export interface ApiError {
error: string
message?: string
details?: any
}
export interface TokenCreateRequest {
uuid?: string
scope: TokenScope[]
}
export interface TokenCreateResponse {
token: string
uuid: string
scope: TokenScope[]
}
export interface AccessRequest {
username: string
permission: 'read-only' | 'read-write'
}
export interface AccessResponse {
name: string
collaborators: Record<string, 'read-only' | 'read-write'>
}
// ---------------------------------------------------------
// Utility Types
// ---------------------------------------------------------
export type HttpMethod =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'PATCH'
| 'HEAD'
| 'OPTIONS'
export interface ValidationResult {
valid: boolean
errors?: string[]
}
export interface FileInfo {
path: string
content: Uint8Array | string
size: number
}
// ---------------------------------------------------------
// Constants Types
// ---------------------------------------------------------
export interface CookieOptions {
path: string
httpOnly: boolean
secure: boolean
sameSite: 'strict' | 'lax' | 'none'
}
export interface ApiDocsConfig {
metaData: {
title: string
}
hideModels: boolean
hideDownloadButton: boolean
darkMode: boolean
favicon: string
defaultHttpClient: {
targetKey: string
clientKey: string
}
authentication: {
http: {
bearer: { token: string }
basic: { username: string; password: string }
}
}
hiddenClients: Record<string, boolean | string[]>
spec: {
content: any
}
customCss: string
}
// ---------------------------------------------------------
// Authentication Provider Types (WorkOS)
// ---------------------------------------------------------
export interface WorkOSUser {
id: string
email: string
firstName?: string
lastName?: string
profilePictureUrl?: string
createdAt: string
updatedAt: string
}
export interface WorkOSAuthResponse {
authenticated: boolean
sessionId?: string
organizationId?: string
role?: string
permissions?: string[]
user?: WorkOSUser
reason?: string
}
export interface WorkOSAuthResult {
user: WorkOSUser
sealedSession: string
}
// ---------------------------------------------------------
// Search Types
// ---------------------------------------------------------
export interface SearchResult {
name: string
tags: Record<string, string>
origin?: string | null
upstream?: string | null
version?: string
description?: string
keywords?: string[]
lastUpdated?: string
homepage?: string
repository?: string
bugs?: string
author?: string
publisher?: string
maintainers?: string[]
}
export interface SearchResponse {
objects: {
package: {
name: string
scope: string
version: string
description: string
keywords: string[]
date: string
links: {
npm: string
homepage?: string
repository?: string
bugs?: string
}
author?: string
publisher?: string
maintainers: string[]
}
score: {
final: number
detail: {
quality: number
popularity: number
maintenance: number
}
}
searchScore: number
}[]
total: number
time: string
}
// ---------------------------------------------------------
// Environment Types
// ---------------------------------------------------------
export interface Environment {
// Database
D1_DATABASE?: D1Database
DB?: D1Database
// Queue and Storage
CACHE_REFRESH_QUEUE?: any
BUCKET?: any
ASSETS?: any
// Authentication — Better Auth
// (set via `wrangler secret put` in production)
BETTER_AUTH_SECRET?: string
BETTER_AUTH_URL?: string
GITHUB_CLIENT_ID?: string
GITHUB_CLIENT_SECRET?: string
GOOGLE_CLIENT_ID?: string
GOOGLE_CLIENT_SECRET?: string
/** Label shown by authenticator apps when enrolling TOTP. */
AUTH_2FA_ISSUER?: string
// Origin(s) for the companion web UI. Comma-separated for multi-env
// deployments (e.g. "https://registry.example.com,https://staging.example.com").
// Used to populate CORS allowlist + Better Auth's `trustedOrigins`.
WEB_URL?: string
// Admin seeding (consumed by scripts/seed-admin.js, not the worker)
ADMIN_EMAIL?: string
ADMIN_PASSWORD?: string
ADMIN_NAME?: string
// Configuration flags (enriched by configMiddleware)
TELEMETRY_ENABLED?: boolean
API_DOCS_ENABLED?: boolean
DEBUG_ENABLED?: boolean
// Telemetry
SENTRY?: {
dsn: string
environment?: string
}
SENTRY_CONFIG?: {
dsn: string
environment?: string
sendDefaultPii?: boolean
sampleRate?: number
tracesSampleRate?: number
}
// Cloudflare specific
CF?: {
connecting_ip?: string
}
// Other config values
PORT?: number
VERSION?: string
URL?: string
[key: string]: any
}
export interface Args {
telemetry: boolean
debug: boolean
help: boolean
port: number
host: string
config?: string
env?: string
'db-name'?: string
'bucket-name'?: string
'queue-name'?: string
'dry-run'?: boolean
'web-port'?: number
'no-web'?: boolean
}
export interface MinArgsResult {
args: Args
positionals: string[]
}