pups-client 0.0.0-stage → 0.2.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.
Files changed (113) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +163 -2
  3. package/dist/cli/bin.d.ts +3 -0
  4. package/dist/cli/bin.d.ts.map +1 -0
  5. package/dist/cli/bin.js +4 -0
  6. package/dist/cli/bin.js.map +1 -0
  7. package/dist/cli/file-store.d.ts +21 -0
  8. package/dist/cli/file-store.d.ts.map +1 -0
  9. package/dist/cli/file-store.js +59 -0
  10. package/dist/cli/file-store.js.map +1 -0
  11. package/dist/cli/loopback.d.ts +28 -0
  12. package/dist/cli/loopback.d.ts.map +1 -0
  13. package/dist/cli/loopback.js +82 -0
  14. package/dist/cli/loopback.js.map +1 -0
  15. package/dist/cli/main.d.ts +16 -0
  16. package/dist/cli/main.d.ts.map +1 -0
  17. package/dist/cli/main.js +337 -0
  18. package/dist/cli/main.js.map +1 -0
  19. package/dist/client.d.ts +151 -0
  20. package/dist/client.d.ts.map +1 -0
  21. package/dist/client.js +427 -0
  22. package/dist/client.js.map +1 -0
  23. package/dist/discovery.d.ts +58 -0
  24. package/dist/discovery.d.ts.map +1 -0
  25. package/dist/discovery.js +138 -0
  26. package/dist/discovery.js.map +1 -0
  27. package/dist/errors.d.ts +77 -0
  28. package/dist/errors.d.ts.map +1 -0
  29. package/dist/errors.js +59 -0
  30. package/dist/errors.js.map +1 -0
  31. package/dist/feed.d.ts +314 -0
  32. package/dist/feed.d.ts.map +1 -0
  33. package/dist/feed.js +805 -0
  34. package/dist/feed.js.map +1 -0
  35. package/dist/file-types.d.ts +30 -0
  36. package/dist/file-types.d.ts.map +1 -0
  37. package/dist/file-types.js +67 -0
  38. package/dist/file-types.js.map +1 -0
  39. package/dist/host-meta.d.ts +71 -0
  40. package/dist/host-meta.d.ts.map +1 -0
  41. package/dist/host-meta.js +122 -0
  42. package/dist/host-meta.js.map +1 -0
  43. package/dist/index.d.ts +25 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +16 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/oauth.d.ts +54 -0
  48. package/dist/oauth.d.ts.map +1 -0
  49. package/dist/oauth.js +124 -0
  50. package/dist/oauth.js.map +1 -0
  51. package/dist/paths.d.ts +32 -0
  52. package/dist/paths.d.ts.map +1 -0
  53. package/dist/paths.js +91 -0
  54. package/dist/paths.js.map +1 -0
  55. package/dist/scopes.d.ts +28 -0
  56. package/dist/scopes.d.ts.map +1 -0
  57. package/dist/scopes.js +56 -0
  58. package/dist/scopes.js.map +1 -0
  59. package/dist/session.d.ts +194 -0
  60. package/dist/session.d.ts.map +1 -0
  61. package/dist/session.js +317 -0
  62. package/dist/session.js.map +1 -0
  63. package/dist/store/indexeddb-store.d.ts +23 -0
  64. package/dist/store/indexeddb-store.d.ts.map +1 -0
  65. package/dist/store/indexeddb-store.js +116 -0
  66. package/dist/store/indexeddb-store.js.map +1 -0
  67. package/dist/store/memory-store.d.ts +13 -0
  68. package/dist/store/memory-store.d.ts.map +1 -0
  69. package/dist/store/memory-store.js +22 -0
  70. package/dist/store/memory-store.js.map +1 -0
  71. package/dist/store/token-store.d.ts +35 -0
  72. package/dist/store/token-store.d.ts.map +1 -0
  73. package/dist/store/token-store.js +2 -0
  74. package/dist/store/token-store.js.map +1 -0
  75. package/dist/tabs.d.ts +29 -0
  76. package/dist/tabs.d.ts.map +1 -0
  77. package/dist/tabs.js +51 -0
  78. package/dist/tabs.js.map +1 -0
  79. package/dist/tokens.d.ts +59 -0
  80. package/dist/tokens.d.ts.map +1 -0
  81. package/dist/tokens.js +200 -0
  82. package/dist/tokens.js.map +1 -0
  83. package/dist/util.d.ts +23 -0
  84. package/dist/util.d.ts.map +1 -0
  85. package/dist/util.js +109 -0
  86. package/dist/util.js.map +1 -0
  87. package/dist/version.d.ts +2 -0
  88. package/dist/version.d.ts.map +1 -0
  89. package/dist/version.js +2 -0
  90. package/dist/version.js.map +1 -0
  91. package/package.json +55 -4
  92. package/src/cli/bin.ts +4 -0
  93. package/src/cli/file-store.ts +66 -0
  94. package/src/cli/loopback.ts +103 -0
  95. package/src/cli/main.ts +329 -0
  96. package/src/client.ts +496 -0
  97. package/src/discovery.ts +178 -0
  98. package/src/errors.ts +136 -0
  99. package/src/feed.ts +1007 -0
  100. package/src/file-types.ts +80 -0
  101. package/src/host-meta.ts +148 -0
  102. package/src/index.ts +49 -0
  103. package/src/oauth.ts +163 -0
  104. package/src/paths.ts +92 -0
  105. package/src/scopes.ts +68 -0
  106. package/src/session.ts +396 -0
  107. package/src/store/indexeddb-store.ts +140 -0
  108. package/src/store/memory-store.ts +28 -0
  109. package/src/store/token-store.ts +36 -0
  110. package/src/tabs.ts +61 -0
  111. package/src/tokens.ts +208 -0
  112. package/src/util.ts +118 -0
  113. package/src/version.ts +1 -0
package/src/errors.ts ADDED
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Error codes of {@link PupsError}.
3
+ *
4
+ * Codes of the upload API are the `error` values of RFC-6 ("Errors"), so apps can switch on them directly.
5
+ * Problems the library detects before sending a request use the same codes the server would answer with.
6
+ */
7
+ export type PupsErrorCode =
8
+ // Discovery
9
+ /** The domain the user typed can't be turned into `https://<host>/`. */
10
+ | 'invalid_domain'
11
+ /** The domain has no `host-meta.json` with a PUPS link, or it can't be fetched. */
12
+ | 'no_pups_server'
13
+ /** The configuration document is missing required fields or has invalid values. */
14
+ | 'invalid_server_config'
15
+ /** The server implements a `spec_version` this library doesn't support. */
16
+ | 'unsupported_server_version'
17
+ // Authorization
18
+ /** A requested scope is not allowed by the server or for this client. */
19
+ | 'invalid_scope'
20
+ /** The owner denied the request, or dropped every scope. */
21
+ | 'access_denied'
22
+ /** The authorization response doesn't belong to a request started by this app in this browser. */
23
+ | 'state_mismatch'
24
+ /** The `iss` of the authorization response is not the discovered issuer (mix-up defence, RFC 9207). */
25
+ | 'issuer_mismatch'
26
+ /** The `me` of the token response is not the domain the user entered, and doesn't lead to the same issuer. */
27
+ | 'identity_mismatch'
28
+ /** The browser blocked the login popup. Fall back to `login()`. */
29
+ | 'popup_blocked'
30
+ /** The popup flow didn't finish in time. */
31
+ | 'popup_timeout'
32
+ /** Any other failure of the authorization or the code exchange. */
33
+ | 'authorization_failed'
34
+ // Session
35
+ /** The session ended: the grant was revoked, the refresh token expired, or the user logged out. Log in again. */
36
+ | 'session_expired'
37
+ // Upload API (RFC-6 "Errors") and host-meta endpoint
38
+ | 'invalid_path'
39
+ | 'invalid_token'
40
+ | 'insufficient_scope'
41
+ | 'not_found'
42
+ | 'precondition_failed'
43
+ | 'too_large'
44
+ | 'unsupported_type'
45
+ | 'insufficient_storage'
46
+ // Feeds (RFC-7)
47
+ /** A feed file or archive has no `_pups.pupsControlled` marker: it is maintained by other means and never written. */
48
+ | 'not_pups_controlled'
49
+ /** A feed file or archive is not a JSON Feed. */
50
+ | 'invalid_feed'
51
+ /** `409` from the host-meta endpoint: a single-valued rel is taken, or `host-meta.json` is not valid JSON. */
52
+ | 'conflict'
53
+ /** `429`: the server limits the write rate of this grant. See {@link PupsError.retryAfter}. */
54
+ | 'rate_limited'
55
+ /** `400` for a reason other than the path, or an argument the library can't send. */
56
+ | 'invalid_request'
57
+ // Other
58
+ /** The server doesn't offer the feature, e.g. no `host_meta_endpoint`. */
59
+ | 'unsupported'
60
+ /** The request couldn't be sent, or timed out. */
61
+ | 'network'
62
+ /** The caller aborted the operation with an `AbortSignal`. */
63
+ | 'aborted'
64
+ /** The server answered with an unexpected status, usually `5xx`. */
65
+ | 'server_error'
66
+
67
+ const retryableCodes = new Set<PupsErrorCode>(['network', 'rate_limited', 'server_error'])
68
+
69
+ export interface PupsErrorOptions {
70
+ status?: number
71
+ serverMessage?: string
72
+ retryAfter?: number
73
+ cause?: unknown
74
+ }
75
+
76
+ /** The only error type thrown by pups-client. */
77
+ export class PupsError extends Error {
78
+ override readonly name = 'PupsError'
79
+ /** What went wrong, see {@link PupsErrorCode}. */
80
+ readonly code: PupsErrorCode
81
+ /** The HTTP status, if a response caused the error. */
82
+ readonly status?: number
83
+ /** The `message` (or OAuth `error_description`) from the server's error body, if any. Not localized. */
84
+ readonly serverMessage?: string
85
+ /** Seconds to wait before retrying, from a `Retry-After` header. */
86
+ readonly retryAfter?: number
87
+ /** Whether repeating the same call later may succeed: network problems, `429` and `5xx` (except `507`). */
88
+ readonly retryable: boolean
89
+
90
+ constructor(code: PupsErrorCode, message: string, options: PupsErrorOptions = {}) {
91
+ super(message, options.cause === undefined ? undefined : { cause: options.cause })
92
+ this.code = code
93
+ this.status = options.status
94
+ this.serverMessage = options.serverMessage
95
+ this.retryAfter = options.retryAfter
96
+ this.retryable = retryableCodes.has(code)
97
+ }
98
+ }
99
+
100
+ /** Whether `e` is a {@link PupsError}, optionally with one of the given codes. */
101
+ export const isPupsError = (e: unknown, ...codes: PupsErrorCode[]): e is PupsError =>
102
+ e instanceof PupsError && (!codes.length || codes.includes(e.code))
103
+
104
+ const statusCodes: Record<number, PupsErrorCode> = {
105
+ 401: 'invalid_token',
106
+ 403: 'insufficient_scope',
107
+ 404: 'not_found',
108
+ 409: 'conflict',
109
+ 412: 'precondition_failed',
110
+ 413: 'too_large',
111
+ 415: 'unsupported_type',
112
+ 429: 'rate_limited',
113
+ 507: 'insufficient_storage',
114
+ }
115
+
116
+ /** Turns an error response of the upload API or the host-meta endpoint into a {@link PupsError} (RFC-6 "Errors"). */
117
+ export async function errorFromResponse(response: Response, what: string): Promise<PupsError> {
118
+ let body: { error?: unknown; message?: unknown } = {}
119
+ try {
120
+ body = await response.json()
121
+ } catch {
122
+ // Not every error comes from PUPS itself (proxies, WAFs), so a missing JSON body is normal.
123
+ }
124
+ const status = response.status
125
+ const serverError = typeof body.error == 'string' ? body.error : undefined
126
+ const serverMessage = typeof body.message == 'string' ? body.message : undefined
127
+ const code: PupsErrorCode = status == 400
128
+ ? serverError == 'invalid_path' ? 'invalid_path' : 'invalid_request'
129
+ : statusCodes[status] ?? 'server_error'
130
+ const retryAfterHeader = Number(response.headers.get('Retry-After'))
131
+ return new PupsError(code, `${what} failed with HTTP ${status}${serverMessage ? `: ${serverMessage}` : ''}`, {
132
+ status,
133
+ serverMessage,
134
+ retryAfter: retryAfterHeader > 0 ? retryAfterHeader : undefined,
135
+ })
136
+ }