pups-client 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +131 -2
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +4 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/file-store.d.ts +21 -0
- package/dist/cli/file-store.d.ts.map +1 -0
- package/dist/cli/file-store.js +59 -0
- package/dist/cli/file-store.js.map +1 -0
- package/dist/cli/loopback.d.ts +28 -0
- package/dist/cli/loopback.d.ts.map +1 -0
- package/dist/cli/loopback.js +82 -0
- package/dist/cli/loopback.js.map +1 -0
- package/dist/cli/main.d.ts +16 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +337 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/client.d.ts +151 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +427 -0
- package/dist/client.js.map +1 -0
- package/dist/discovery.d.ts +58 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +138 -0
- package/dist/discovery.js.map +1 -0
- package/dist/errors.d.ts +73 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +59 -0
- package/dist/errors.js.map +1 -0
- package/dist/file-types.d.ts +30 -0
- package/dist/file-types.d.ts.map +1 -0
- package/dist/file-types.js +67 -0
- package/dist/file-types.js.map +1 -0
- package/dist/host-meta.d.ts +71 -0
- package/dist/host-meta.d.ts.map +1 -0
- package/dist/host-meta.js +122 -0
- package/dist/host-meta.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/oauth.d.ts +54 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +124 -0
- package/dist/oauth.js.map +1 -0
- package/dist/paths.d.ts +32 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +91 -0
- package/dist/paths.js.map +1 -0
- package/dist/scopes.d.ts +28 -0
- package/dist/scopes.d.ts.map +1 -0
- package/dist/scopes.js +56 -0
- package/dist/scopes.js.map +1 -0
- package/dist/session.d.ts +183 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +304 -0
- package/dist/session.js.map +1 -0
- package/dist/store/indexeddb-store.d.ts +23 -0
- package/dist/store/indexeddb-store.d.ts.map +1 -0
- package/dist/store/indexeddb-store.js +116 -0
- package/dist/store/indexeddb-store.js.map +1 -0
- package/dist/store/memory-store.d.ts +13 -0
- package/dist/store/memory-store.d.ts.map +1 -0
- package/dist/store/memory-store.js +22 -0
- package/dist/store/memory-store.js.map +1 -0
- package/dist/store/token-store.d.ts +35 -0
- package/dist/store/token-store.d.ts.map +1 -0
- package/dist/store/token-store.js +2 -0
- package/dist/store/token-store.js.map +1 -0
- package/dist/tabs.d.ts +29 -0
- package/dist/tabs.d.ts.map +1 -0
- package/dist/tabs.js +51 -0
- package/dist/tabs.js.map +1 -0
- package/dist/tokens.d.ts +59 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +200 -0
- package/dist/tokens.js.map +1 -0
- package/dist/util.d.ts +23 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +109 -0
- package/dist/util.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/package.json +55 -4
- package/src/cli/bin.ts +4 -0
- package/src/cli/file-store.ts +66 -0
- package/src/cli/loopback.ts +103 -0
- package/src/cli/main.ts +329 -0
- package/src/client.ts +496 -0
- package/src/discovery.ts +178 -0
- package/src/errors.ts +131 -0
- package/src/file-types.ts +80 -0
- package/src/host-meta.ts +148 -0
- package/src/index.ts +30 -0
- package/src/oauth.ts +163 -0
- package/src/paths.ts +92 -0
- package/src/scopes.ts +68 -0
- package/src/session.ts +381 -0
- package/src/store/indexeddb-store.ts +140 -0
- package/src/store/memory-store.ts +28 -0
- package/src/store/token-store.ts +36 -0
- package/src/tabs.ts +61 -0
- package/src/tokens.ts +208 -0
- package/src/util.ts +118 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { PupsError } from './errors.js';
|
|
2
|
+
import { fetchWithTimeout, parseUrl } from './util.js';
|
|
3
|
+
/** The link relation in `host-meta.json` that points to the PUPS configuration document. A placeholder in RFC-6 0.1. */
|
|
4
|
+
export const PUPS_REL = 'https://bigwide.net/ns/personal-storage';
|
|
5
|
+
/** The `spec_version` values this library implements. */
|
|
6
|
+
export const SUPPORTED_SPEC_VERSIONS = ['0.1'];
|
|
7
|
+
const discoveryTimeoutMs = 10_000;
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes what the user typed (`tim.example.com`, `https://tim.example.com/some/page`) to `https://<host>/`,
|
|
10
|
+
* as RFC-6 "Starting point" requires: HTTPS, lowercase host, IDNs in ASCII form, no port, path, query or fragment.
|
|
11
|
+
* @throws {PupsError} `invalid_domain`
|
|
12
|
+
*/
|
|
13
|
+
export function normalizeDomain(input) {
|
|
14
|
+
const trimmed = input.trim();
|
|
15
|
+
const url = parseUrl(/^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`);
|
|
16
|
+
if (!url || (url.protocol != 'https:' && url.protocol != 'http:') || !url.hostname)
|
|
17
|
+
throw new PupsError('invalid_domain', `"${input}" is not a domain name. Enter something like "example.com".`);
|
|
18
|
+
// The URL parser lowercases the host and converts IDNs to punycode.
|
|
19
|
+
const host = url.hostname.replace(/\.$/, '');
|
|
20
|
+
if (!host.includes('.') && !host.startsWith('['))
|
|
21
|
+
throw new PupsError('invalid_domain', `"${input}" is not a full domain name. Enter something like "example.com".`);
|
|
22
|
+
return `https://${host}/`;
|
|
23
|
+
}
|
|
24
|
+
async function fetchJson(fetchFn, url, signal, notFound) {
|
|
25
|
+
const response = await fetchWithTimeout(fetchFn, url, {
|
|
26
|
+
headers: { Accept: 'application/json' },
|
|
27
|
+
credentials: 'omit',
|
|
28
|
+
redirect: 'follow',
|
|
29
|
+
cache: 'no-cache',
|
|
30
|
+
}, discoveryTimeoutMs, signal);
|
|
31
|
+
// Browsers follow redirects without exposing the chain; at least the final URL must be HTTPS.
|
|
32
|
+
if (response.url && !response.url.startsWith('https://'))
|
|
33
|
+
throw new PupsError('no_pups_server', `${url} redirected to a non-HTTPS URL.`);
|
|
34
|
+
if (!response.ok)
|
|
35
|
+
throw notFound();
|
|
36
|
+
try {
|
|
37
|
+
return { json: await response.json(), finalUrl: response.url || url };
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
throw notFound();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Discovers the PUPS server of a domain (RFC-6 "Discovery"): fetches `/.well-known/host-meta.json`, follows the
|
|
45
|
+
* PUPS link, and validates the configuration document.
|
|
46
|
+
*/
|
|
47
|
+
export async function discover(domain, fetchFn, signal) {
|
|
48
|
+
const normalized = normalizeDomain(domain);
|
|
49
|
+
const hostMetaUrl = `${normalized}.well-known/host-meta.json`;
|
|
50
|
+
const noServer = () => new PupsError('no_pups_server', `${new URL(normalized).host} has no Personal Upload Server: ${hostMetaUrl} is missing, not readable from the browser, or has no PUPS link.`);
|
|
51
|
+
const hostMeta = await fetchJson(fetchFn, hostMetaUrl, signal, noServer);
|
|
52
|
+
const links = hostMeta.json?.links;
|
|
53
|
+
const link = Array.isArray(links)
|
|
54
|
+
? links.find((l) => l && l.rel == PUPS_REL)
|
|
55
|
+
: undefined;
|
|
56
|
+
const configUrl = parseUrl(link?.href);
|
|
57
|
+
if (!configUrl)
|
|
58
|
+
throw noServer();
|
|
59
|
+
if (configUrl.protocol != 'https:')
|
|
60
|
+
throw new PupsError('invalid_server_config', `The PUPS link in ${hostMetaUrl} must be an HTTPS URL.`);
|
|
61
|
+
const config = await fetchJson(fetchFn, configUrl.href, signal, () => new PupsError('invalid_server_config', `The PUPS configuration document ${configUrl.href} can't be read.`));
|
|
62
|
+
const identityHosts = [new URL(normalized).hostname];
|
|
63
|
+
const finalHost = new URL(hostMeta.finalUrl).hostname;
|
|
64
|
+
if (!identityHosts.includes(finalHost))
|
|
65
|
+
identityHosts.push(finalHost);
|
|
66
|
+
return parseConfig(config.json, normalized, identityHosts);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Validates a configuration document (RFC-6 "Configuration document"). Unknown fields are kept in `raw` and
|
|
70
|
+
* otherwise ignored.
|
|
71
|
+
* @throws {PupsError} `invalid_server_config`, `unsupported_server_version`
|
|
72
|
+
*/
|
|
73
|
+
export function parseConfig(json, domain, identityHosts) {
|
|
74
|
+
if (!json || typeof json != 'object' || Array.isArray(json))
|
|
75
|
+
throw new PupsError('invalid_server_config', 'The PUPS configuration document is not a JSON object.');
|
|
76
|
+
const raw = json;
|
|
77
|
+
const fail = (field, problem) => {
|
|
78
|
+
throw new PupsError('invalid_server_config', `PUPS configuration: "${field}" ${problem}.`);
|
|
79
|
+
};
|
|
80
|
+
const https = (field, value = raw[field]) => {
|
|
81
|
+
const url = parseUrl(value);
|
|
82
|
+
if (!url || url.protocol != 'https:' || url.hash)
|
|
83
|
+
return fail(field, 'must be an HTTPS URL without fragment');
|
|
84
|
+
return value;
|
|
85
|
+
};
|
|
86
|
+
const optionalHttps = (field) => raw[field] === undefined ? undefined : https(field);
|
|
87
|
+
const base = (field) => https(field).endsWith('/') ? raw[field] : fail(field, 'must end with "/"');
|
|
88
|
+
const strings = (field, value) => Array.isArray(value) && value.every(v => typeof v == 'string') ? value : fail(field, 'must be an array of strings');
|
|
89
|
+
const specVersion = typeof raw.spec_version == 'string' ? raw.spec_version : fail('spec_version', 'is missing');
|
|
90
|
+
if (!SUPPORTED_SPEC_VERSIONS.includes(specVersion))
|
|
91
|
+
throw new PupsError('unsupported_server_version', `The server implements PUPS ${specVersion}; this library supports ${SUPPORTED_SPEC_VERSIONS.join(', ')}.`);
|
|
92
|
+
const issuer = https('issuer');
|
|
93
|
+
if (new URL(issuer).search)
|
|
94
|
+
fail('issuer', 'must not have a query');
|
|
95
|
+
const maxUploadBytes = raw.max_upload_bytes;
|
|
96
|
+
if (typeof maxUploadBytes != 'number' || !Number.isSafeInteger(maxUploadBytes) || maxUploadBytes <= 0)
|
|
97
|
+
fail('max_upload_bytes', 'must be a positive integer');
|
|
98
|
+
const wellKnownRaw = raw.well_known_paths ?? [];
|
|
99
|
+
if (!Array.isArray(wellKnownRaw))
|
|
100
|
+
fail('well_known_paths', 'must be an array');
|
|
101
|
+
const wellKnownPaths = wellKnownRaw.map((entry, i) => {
|
|
102
|
+
const field = `well_known_paths[${i}]`;
|
|
103
|
+
if (!entry || typeof entry != 'object')
|
|
104
|
+
return fail(field, 'must be an object');
|
|
105
|
+
const e = entry;
|
|
106
|
+
if (typeof e.path != 'string' || !e.path.startsWith('/'))
|
|
107
|
+
return fail(`${field}.path`, 'must start with "/"');
|
|
108
|
+
const url = e.url === undefined ? undefined : https(`${field}.url`, e.url);
|
|
109
|
+
if (url && e.path.endsWith('/') && !url.endsWith('/'))
|
|
110
|
+
fail(`${field}.url`, 'must end with "/" for a directory');
|
|
111
|
+
return {
|
|
112
|
+
path: e.path,
|
|
113
|
+
...url ? { url } : {},
|
|
114
|
+
rels: e.rels === undefined ? [] : strings(`${field}.rels`, e.rels),
|
|
115
|
+
properties: e.properties === undefined ? [] : strings(`${field}.properties`, e.properties),
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
const hostMetaEndpoint = optionalHttps('host_meta_endpoint');
|
|
119
|
+
const postEndpoint = optionalHttps('post_endpoint');
|
|
120
|
+
return {
|
|
121
|
+
domain,
|
|
122
|
+
identityHosts,
|
|
123
|
+
issuer,
|
|
124
|
+
authorizationEndpoint: https('authorization_endpoint'),
|
|
125
|
+
tokenEndpoint: https('token_endpoint'),
|
|
126
|
+
revocationEndpoint: https('revocation_endpoint'),
|
|
127
|
+
storageBase: base('storage_base'),
|
|
128
|
+
publicBase: base('public_base'),
|
|
129
|
+
scopesSupported: strings('scopes_supported', raw.scopes_supported),
|
|
130
|
+
wellKnownPaths,
|
|
131
|
+
...hostMetaEndpoint ? { hostMetaEndpoint } : {},
|
|
132
|
+
...postEndpoint ? { postEndpoint } : {},
|
|
133
|
+
maxUploadBytes: maxUploadBytes,
|
|
134
|
+
specVersion,
|
|
135
|
+
raw,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAEtD,wHAAwH;AACxH,MAAM,CAAC,MAAM,QAAQ,GAAG,yCAAyC,CAAA;AAEjE,yDAAyD;AACzD,MAAM,CAAC,MAAM,uBAAuB,GAAsB,CAAC,KAAK,CAAC,CAAA;AAEjE,MAAM,kBAAkB,GAAG,MAAM,CAAA;AAwCjC;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;IAC5B,MAAM,GAAG,GAAG,QAAQ,CAAC,0BAA0B,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,OAAO,EAAE,CAAC,CAAA;IAC/F,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,IAAI,QAAQ,IAAI,GAAG,CAAC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ;QAChF,MAAM,IAAI,SAAS,CAAC,gBAAgB,EAAE,IAAI,KAAK,6DAA6D,CAAC,CAAA;IAC/G,oEAAoE;IACpE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CAAC,GAAG,KAAK,aAAa,EAAE,CAAC,CAAC,UAAU,CAAC;SAC3F,CAAA;IACH,CAAC,CAAC,CAAA;IAEF,MAAM,gBAAgB,GAAG,aAAa,CAAC,oBAAoB,CAAC,CAAA;IAC5D,MAAM,YAAY,GAAG,aAAa,CAAC,eAAe,CAAC,CAAA;IACnD,OAAO;QACL,MAAM;QACN,aAAa;QACb,MAAM;QACN,qBAAqB,EAAE,KAAK,CAAC,wBAAwB,CAAC;QACtD,aAAa,EAAE,KAAK,CAAC,gBAAgB,CAAC;QACtC,kBAAkB,EAAE,KAAK,CAAC,qBAAqB,CAAC;QAChD,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC;QACjC,UAAU,EAAE,IAAI,CAAC,aAAa,CAAC;QAC/B,eAAe,EAAE,OAAO,CAAC,kBAAkB,EAAE,GAAG,CAAC,gBAAgB,CAAC;QAClE,cAAc;QACd,GAAG,gBAAgB,CAAC,CAAC,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,EAAE;QAC/C,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE;QACvC,cAAc,EAAE,cAAwB;QACxC,WAAW;QACX,GAAG;KACJ,CAAA;AACH,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
/** The domain the user typed can't be turned into `https://<host>/`. */
|
|
9
|
+
'invalid_domain'
|
|
10
|
+
/** The domain has no `host-meta.json` with a PUPS link, or it can't be fetched. */
|
|
11
|
+
| 'no_pups_server'
|
|
12
|
+
/** The configuration document is missing required fields or has invalid values. */
|
|
13
|
+
| 'invalid_server_config'
|
|
14
|
+
/** The server implements a `spec_version` this library doesn't support. */
|
|
15
|
+
| 'unsupported_server_version'
|
|
16
|
+
/** A requested scope is not allowed by the server or for this client. */
|
|
17
|
+
| 'invalid_scope'
|
|
18
|
+
/** The owner denied the request, or dropped every scope. */
|
|
19
|
+
| 'access_denied'
|
|
20
|
+
/** The authorization response doesn't belong to a request started by this app in this browser. */
|
|
21
|
+
| 'state_mismatch'
|
|
22
|
+
/** The `iss` of the authorization response is not the discovered issuer (mix-up defence, RFC 9207). */
|
|
23
|
+
| 'issuer_mismatch'
|
|
24
|
+
/** The `me` of the token response is not the domain the user entered, and doesn't lead to the same issuer. */
|
|
25
|
+
| 'identity_mismatch'
|
|
26
|
+
/** The browser blocked the login popup. Fall back to `login()`. */
|
|
27
|
+
| 'popup_blocked'
|
|
28
|
+
/** The popup flow didn't finish in time. */
|
|
29
|
+
| 'popup_timeout'
|
|
30
|
+
/** Any other failure of the authorization or the code exchange. */
|
|
31
|
+
| 'authorization_failed'
|
|
32
|
+
/** The session ended: the grant was revoked, the refresh token expired, or the user logged out. Log in again. */
|
|
33
|
+
| 'session_expired' | 'invalid_path' | 'invalid_token' | 'insufficient_scope' | 'not_found' | 'precondition_failed' | 'too_large' | 'unsupported_type' | 'insufficient_storage'
|
|
34
|
+
/** `409` from the host-meta endpoint: a single-valued rel is taken, or `host-meta.json` is not valid JSON. */
|
|
35
|
+
| 'conflict'
|
|
36
|
+
/** `429`: the server limits the write rate of this grant. See {@link PupsError.retryAfter}. */
|
|
37
|
+
| 'rate_limited'
|
|
38
|
+
/** `400` for a reason other than the path, or an argument the library can't send. */
|
|
39
|
+
| 'invalid_request'
|
|
40
|
+
/** The server doesn't offer the feature, e.g. no `host_meta_endpoint`. */
|
|
41
|
+
| 'unsupported'
|
|
42
|
+
/** The request couldn't be sent, or timed out. */
|
|
43
|
+
| 'network'
|
|
44
|
+
/** The caller aborted the operation with an `AbortSignal`. */
|
|
45
|
+
| 'aborted'
|
|
46
|
+
/** The server answered with an unexpected status, usually `5xx`. */
|
|
47
|
+
| 'server_error';
|
|
48
|
+
export interface PupsErrorOptions {
|
|
49
|
+
status?: number;
|
|
50
|
+
serverMessage?: string;
|
|
51
|
+
retryAfter?: number;
|
|
52
|
+
cause?: unknown;
|
|
53
|
+
}
|
|
54
|
+
/** The only error type thrown by pups-client. */
|
|
55
|
+
export declare class PupsError extends Error {
|
|
56
|
+
readonly name = "PupsError";
|
|
57
|
+
/** What went wrong, see {@link PupsErrorCode}. */
|
|
58
|
+
readonly code: PupsErrorCode;
|
|
59
|
+
/** The HTTP status, if a response caused the error. */
|
|
60
|
+
readonly status?: number;
|
|
61
|
+
/** The `message` (or OAuth `error_description`) from the server's error body, if any. Not localized. */
|
|
62
|
+
readonly serverMessage?: string;
|
|
63
|
+
/** Seconds to wait before retrying, from a `Retry-After` header. */
|
|
64
|
+
readonly retryAfter?: number;
|
|
65
|
+
/** Whether repeating the same call later may succeed: network problems, `429` and `5xx` (except `507`). */
|
|
66
|
+
readonly retryable: boolean;
|
|
67
|
+
constructor(code: PupsErrorCode, message: string, options?: PupsErrorOptions);
|
|
68
|
+
}
|
|
69
|
+
/** Whether `e` is a {@link PupsError}, optionally with one of the given codes. */
|
|
70
|
+
export declare const isPupsError: (e: unknown, ...codes: PupsErrorCode[]) => e is PupsError;
|
|
71
|
+
/** Turns an error response of the upload API or the host-meta endpoint into a {@link PupsError} (RFC-6 "Errors"). */
|
|
72
|
+
export declare function errorFromResponse(response: Response, what: string): Promise<PupsError>;
|
|
73
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,aAAa;AAEvB,wEAAwE;AACtE,gBAAgB;AAClB,mFAAmF;GACjF,gBAAgB;AAClB,mFAAmF;GACjF,uBAAuB;AACzB,2EAA2E;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AAO,MAAM,WAAW,GAAI,GAAG,OAAO,EAAE,GAAG,OAAO,aAAa,EAAE,KAAG,CAAC,IAAI,SACJ,CAAA;AAcrE,qHAAqH;AACrH,wBAAsB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAmB5F"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const retryableCodes = new Set(['network', 'rate_limited', 'server_error']);
|
|
2
|
+
/** The only error type thrown by pups-client. */
|
|
3
|
+
export class PupsError extends Error {
|
|
4
|
+
name = 'PupsError';
|
|
5
|
+
/** What went wrong, see {@link PupsErrorCode}. */
|
|
6
|
+
code;
|
|
7
|
+
/** The HTTP status, if a response caused the error. */
|
|
8
|
+
status;
|
|
9
|
+
/** The `message` (or OAuth `error_description`) from the server's error body, if any. Not localized. */
|
|
10
|
+
serverMessage;
|
|
11
|
+
/** Seconds to wait before retrying, from a `Retry-After` header. */
|
|
12
|
+
retryAfter;
|
|
13
|
+
/** Whether repeating the same call later may succeed: network problems, `429` and `5xx` (except `507`). */
|
|
14
|
+
retryable;
|
|
15
|
+
constructor(code, message, options = {}) {
|
|
16
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.status = options.status;
|
|
19
|
+
this.serverMessage = options.serverMessage;
|
|
20
|
+
this.retryAfter = options.retryAfter;
|
|
21
|
+
this.retryable = retryableCodes.has(code);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Whether `e` is a {@link PupsError}, optionally with one of the given codes. */
|
|
25
|
+
export const isPupsError = (e, ...codes) => e instanceof PupsError && (!codes.length || codes.includes(e.code));
|
|
26
|
+
const statusCodes = {
|
|
27
|
+
401: 'invalid_token',
|
|
28
|
+
403: 'insufficient_scope',
|
|
29
|
+
404: 'not_found',
|
|
30
|
+
409: 'conflict',
|
|
31
|
+
412: 'precondition_failed',
|
|
32
|
+
413: 'too_large',
|
|
33
|
+
415: 'unsupported_type',
|
|
34
|
+
429: 'rate_limited',
|
|
35
|
+
507: 'insufficient_storage',
|
|
36
|
+
};
|
|
37
|
+
/** Turns an error response of the upload API or the host-meta endpoint into a {@link PupsError} (RFC-6 "Errors"). */
|
|
38
|
+
export async function errorFromResponse(response, what) {
|
|
39
|
+
let body = {};
|
|
40
|
+
try {
|
|
41
|
+
body = await response.json();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Not every error comes from PUPS itself (proxies, WAFs), so a missing JSON body is normal.
|
|
45
|
+
}
|
|
46
|
+
const status = response.status;
|
|
47
|
+
const serverError = typeof body.error == 'string' ? body.error : undefined;
|
|
48
|
+
const serverMessage = typeof body.message == 'string' ? body.message : undefined;
|
|
49
|
+
const code = status == 400
|
|
50
|
+
? serverError == 'invalid_path' ? 'invalid_path' : 'invalid_request'
|
|
51
|
+
: statusCodes[status] ?? 'server_error';
|
|
52
|
+
const retryAfterHeader = Number(response.headers.get('Retry-After'));
|
|
53
|
+
return new PupsError(code, `${what} failed with HTTP ${status}${serverMessage ? `: ${serverMessage}` : ''}`, {
|
|
54
|
+
status,
|
|
55
|
+
serverMessage,
|
|
56
|
+
retryAfter: retryAfterHeader > 0 ? retryAfterHeader : undefined,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AA6DA,MAAM,cAAc,GAAG,IAAI,GAAG,CAAgB,CAAC,SAAS,EAAE,cAAc,EAAE,cAAc,CAAC,CAAC,CAAA;AAS1F,iDAAiD;AACjD,MAAM,OAAO,SAAU,SAAQ,KAAK;IAChB,IAAI,GAAG,WAAW,CAAA;IACpC,kDAAkD;IACzC,IAAI,CAAe;IAC5B,uDAAuD;IAC9C,MAAM,CAAS;IACxB,wGAAwG;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AM,QAAQ,CAAC,IAAI,EAAE,CAAA;IAC9B,CAAC;IAAC,MAAM,CAAC;QACP,4FAA4F;IAC9F,CAAC;IACD,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAA;IAC9B,MAAM,WAAW,GAAG,OAAO,IAAI,CAAC,KAAK,IAAI,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IAC1E,MAAM,aAAa,GAAG,OAAO,IAAI,CAAC,OAAO,IAAI,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAA;IAChF,MAAM,IAAI,GAAkB,MAAM,IAAI,GAAG;QACvC,CAAC,CAAC,WAAW,IAAI,cAAc,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,iBAAiB;QACpE,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,cAAc,CAAA;IACzC,MAAM,gBAAgB,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAA;IACpE,OAAO,IAAI,SAAS,CAAC,IAAI,EAAE,GAAG,IAAI,qBAAqB,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,KAAK,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE;QAC3G,MAAM;QACN,aAAa;QACb,UAAU,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,SAAS;KAChE,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
interface FileType {
|
|
2
|
+
/** The type the server serves the file with. */
|
|
3
|
+
served: string;
|
|
4
|
+
/** The `Content-Type` values accepted on upload. */
|
|
5
|
+
accepted: readonly string[];
|
|
6
|
+
/** Text types are UTF-8 and sent with `; charset=utf-8`. */
|
|
7
|
+
text: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** The extension allowlist of RFC-6 ("File types"). The extension decides the type. */
|
|
10
|
+
export declare const fileTypes: Readonly<Record<string, FileType>>;
|
|
11
|
+
/**
|
|
12
|
+
* Extensions that are never accepted, anywhere in a file name (RFC-6 "Never accepted, anywhere").
|
|
13
|
+
* The allowlist already excludes them; this list gives clear error messages and catches `feed.php.json`.
|
|
14
|
+
*/
|
|
15
|
+
export declare const blockedExtensions: ReadonlySet<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Checks the file name of `path` against the allowlist and returns its file type.
|
|
18
|
+
* @throws {PupsError} `unsupported_type`
|
|
19
|
+
*/
|
|
20
|
+
export declare function fileTypeOf(path: string): FileType & {
|
|
21
|
+
extension: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The `Content-Type` header to upload `path` with: `requested` if given and accepted for the extension,
|
|
25
|
+
* otherwise the served type of the extension. Text types get `; charset=utf-8`.
|
|
26
|
+
* @throws {PupsError} `unsupported_type`
|
|
27
|
+
*/
|
|
28
|
+
export declare function uploadContentType(path: string, requested?: string): string;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=file-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-types.d.ts","sourceRoot":"","sources":["../src/file-types.ts"],"names":[],"mappings":"AAEA,UAAU,QAAQ;IAChB,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAA;IACd,oDAAoD;IACpD,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;IAC3B,4DAA4D;IAC5D,IAAI,EAAE,OAAO,CAAA;CACd;AAKD,uFAAuF;AACvF,eAAO,MAAM,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAqBxD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,WAAW,CAAC,MAAM,CAIhD,CAAA;AAKF;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,CAWzE;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAM1E"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { PupsError } from './errors.js';
|
|
2
|
+
const type = (served, accepted = [served], text = false) => ({ served, accepted, text });
|
|
3
|
+
const text = (served, accepted) => type(served, accepted, true);
|
|
4
|
+
/** The extension allowlist of RFC-6 ("File types"). The extension decides the type. */
|
|
5
|
+
export const fileTypes = {
|
|
6
|
+
json: text('application/json', ['application/json', 'application/feed+json']),
|
|
7
|
+
xml: text('application/xml', ['application/xml', 'text/xml', 'application/rss+xml', 'application/atom+xml']),
|
|
8
|
+
opml: text('text/x-opml', ['text/x-opml', 'application/xml', 'text/xml']),
|
|
9
|
+
md: text('text/markdown', ['text/markdown', 'text/x-markdown']),
|
|
10
|
+
txt: text('text/plain'),
|
|
11
|
+
csv: text('text/csv'),
|
|
12
|
+
vcf: text('text/vcard', ['text/vcard', 'text/x-vcard']),
|
|
13
|
+
svg: text('image/svg+xml'),
|
|
14
|
+
png: type('image/png'),
|
|
15
|
+
jpg: type('image/jpeg'),
|
|
16
|
+
jpeg: type('image/jpeg'),
|
|
17
|
+
gif: type('image/gif'),
|
|
18
|
+
webp: type('image/webp'),
|
|
19
|
+
avif: type('image/avif'),
|
|
20
|
+
mp3: type('audio/mpeg'),
|
|
21
|
+
m4a: type('audio/mp4', ['audio/mp4', 'audio/x-m4a']),
|
|
22
|
+
ogg: type('audio/ogg', ['audio/ogg', 'audio/opus']),
|
|
23
|
+
opus: type('audio/ogg', ['audio/ogg', 'audio/opus']),
|
|
24
|
+
mp4: type('video/mp4', ['video/mp4', 'audio/mp4']),
|
|
25
|
+
webm: type('video/webm', ['video/webm', 'audio/webm']),
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Extensions that are never accepted, anywhere in a file name (RFC-6 "Never accepted, anywhere").
|
|
29
|
+
* The allowlist already excludes them; this list gives clear error messages and catches `feed.php.json`.
|
|
30
|
+
*/
|
|
31
|
+
export const blockedExtensions = new Set([
|
|
32
|
+
'html', 'htm', 'xhtml', 'js', 'mjs', 'css',
|
|
33
|
+
'php', 'phtml', 'php3', 'php4', 'php5', 'php6', 'php7', 'php8', 'phar', 'pht', 'phps', 'inc',
|
|
34
|
+
'jsp', 'jspx', 'asp', 'aspx', 'cgi', 'pl', 'py', 'rb', 'sh', 'shtml', 'htaccess', 'htpasswd',
|
|
35
|
+
]);
|
|
36
|
+
/** The media type without parameters, in lowercase. */
|
|
37
|
+
const essence = (contentType) => contentType.split(';')[0].trim().toLowerCase();
|
|
38
|
+
/**
|
|
39
|
+
* Checks the file name of `path` against the allowlist and returns its file type.
|
|
40
|
+
* @throws {PupsError} `unsupported_type`
|
|
41
|
+
*/
|
|
42
|
+
export function fileTypeOf(path) {
|
|
43
|
+
const name = path.slice(path.lastIndexOf('/') + 1);
|
|
44
|
+
// Apache's mod_mime treats every dot-separated part after the first as an extension.
|
|
45
|
+
const extensions = name.toLowerCase().split('.').slice(1);
|
|
46
|
+
const blocked = extensions.find(e => blockedExtensions.has(e));
|
|
47
|
+
if (blocked)
|
|
48
|
+
throw new PupsError('unsupported_type', `"${name}": files with ".${blocked}" in their name are never accepted (RFC-6, File types).`);
|
|
49
|
+
const extension = extensions.at(-1);
|
|
50
|
+
const fileType = extension ? fileTypes[extension] : undefined;
|
|
51
|
+
if (!extension || !fileType)
|
|
52
|
+
throw new PupsError('unsupported_type', `"${name}": ${extension ? `".${extension}" is not` : 'a file without extension is not'} an allowed file type. Allowed: ${Object.keys(fileTypes).map(e => `.${e}`).join(', ')} (RFC-6, File types).`);
|
|
53
|
+
return { ...fileType, extension };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The `Content-Type` header to upload `path` with: `requested` if given and accepted for the extension,
|
|
57
|
+
* otherwise the served type of the extension. Text types get `; charset=utf-8`.
|
|
58
|
+
* @throws {PupsError} `unsupported_type`
|
|
59
|
+
*/
|
|
60
|
+
export function uploadContentType(path, requested) {
|
|
61
|
+
const fileType = fileTypeOf(path);
|
|
62
|
+
const chosen = requested ? essence(requested) : fileType.served;
|
|
63
|
+
if (!fileType.accepted.includes(chosen))
|
|
64
|
+
throw new PupsError('unsupported_type', `Content-Type "${chosen}" is not accepted for ".${fileType.extension}" files. Accepted: ${fileType.accepted.join(', ')} (RFC-6, File types).`);
|
|
65
|
+
return fileType.text ? `${chosen}; charset=utf-8` : chosen;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=file-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-types.js","sourceRoot":"","sources":["../src/file-types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAWvC,MAAM,IAAI,GAAG,CAAC,MAAc,EAAE,WAAqB,CAAC,MAAM,CAAC,EAAE,IAAI,GAAG,KAAK,EAAY,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;AACpH,MAAM,IAAI,GAAG,CAAC,MAAc,EAAE,QAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAA;AAElF,uFAAuF;AACvF,MAAM,CAAC,MAAM,SAAS,GAAuC;IAC3D,IAAI,EAAE,IAAI,CAAC,kBAAkB,EAAE,CAAC,kBAAkB,EAAE,uBAAuB,CAAC,CAAC;IAC7E,GAAG,EAAE,IAAI,CAAC,iBAAiB,EAAE,CAAC,iBAAiB,EAAE,UAAU,EAAE,qBAAqB,EAAE,sBAAsB,CAAC,CAAC;IAC5G,IAAI,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,aAAa,EAAE,iBAAiB,EAAE,UAAU,CAAC,CAAC;IACzE,EAAE,EAAE,IAAI,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AM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAA;IAC9O,OAAO,EAAE,GAAG,QAAQ,EAAE,SAAS,EAAE,CAAA;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY,EAAE,SAAkB;IAChE,MAAM,QAAQ,GAAG,UAAU,CAAC,IAAI,CAAC,CAAA;IACjC,MAAM,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAA;IAC/D,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC;QACrC,MAAM,IAAI,SAAS,CAAC,kBAAkB,EAAE,iBAAiB,MAAM,2BAA2B,QAAQ,CAAC,SAAS,sBAAsB,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAA;IACxL,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,MAAM,iBAAiB,CAAC,CAAC,CAAC,MAAM,CAAA;AAC5D,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { PupsSession } from './session.js';
|
|
2
|
+
/** A link in `host-meta.json` (RFC 6415, JSON form). */
|
|
3
|
+
export interface HostMetaLink {
|
|
4
|
+
rel: string;
|
|
5
|
+
href?: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
properties?: Record<string, string | null>;
|
|
8
|
+
[field: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
/** `host-meta.json` (RFC 6415, JSON form). */
|
|
11
|
+
export interface HostMetaDocument {
|
|
12
|
+
subject?: string;
|
|
13
|
+
aliases?: string[];
|
|
14
|
+
properties?: Record<string, string | null>;
|
|
15
|
+
links?: HostMetaLink[];
|
|
16
|
+
[field: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Edits `host-meta.json` through the server's `host_meta_endpoint` (RFC-6 "Editing host-meta.json").
|
|
20
|
+
*
|
|
21
|
+
* Apps may only add links to their own files, with the rels their scopes allow, and set the properties declared for
|
|
22
|
+
* well-known paths they hold. Which rels are single-valued and which properties take URLs is known only to the
|
|
23
|
+
* server, so those problems surface as `conflict`, `insufficient_scope` or `invalid_request` from the server.
|
|
24
|
+
*/
|
|
25
|
+
export declare class HostMetaEditor {
|
|
26
|
+
#private;
|
|
27
|
+
/** @internal */
|
|
28
|
+
constructor(session: PupsSession, fetchFn: typeof fetch);
|
|
29
|
+
/** Whether the server can edit `host-meta.json`. If not, the editing methods throw `unsupported`. */
|
|
30
|
+
get available(): boolean;
|
|
31
|
+
/** Reads the public `/.well-known/host-meta.json` of the owner's domain, e.g. to show the current links. */
|
|
32
|
+
get(options?: {
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
}): Promise<HostMetaDocument>;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the granted scopes allow a link with `rel` to the file `href` (a path or a public URL):
|
|
37
|
+
* for a file under a well-known path, the `rels` declared for it; for a file in the app's own directory, URI rels
|
|
38
|
+
* on the client host (with or without `www.`).
|
|
39
|
+
*/
|
|
40
|
+
allowsRel(rel: string, href: string): boolean;
|
|
41
|
+
/** The properties this session may set: those declared for the well-known paths whose scope was granted. */
|
|
42
|
+
allowedProperties(): string[];
|
|
43
|
+
/**
|
|
44
|
+
* Adds a link to one of the app's files. `href` may be a path (relative paths are in the app's own directory) or
|
|
45
|
+
* a public URL; it is sent as the public URL.
|
|
46
|
+
* @throws {PupsError} `insufficient_scope` if the file or rel isn't allowed, `conflict` if a single-valued rel is taken.
|
|
47
|
+
*/
|
|
48
|
+
addLink(link: {
|
|
49
|
+
rel: string;
|
|
50
|
+
href: string;
|
|
51
|
+
type?: string;
|
|
52
|
+
}, options?: {
|
|
53
|
+
signal?: AbortSignal;
|
|
54
|
+
}): Promise<void>;
|
|
55
|
+
/** Removes a link the app added. Links of the owner and of other apps are never touched by the server. */
|
|
56
|
+
removeLink(link: {
|
|
57
|
+
rel: string;
|
|
58
|
+
href: string;
|
|
59
|
+
}, options?: {
|
|
60
|
+
signal?: AbortSignal;
|
|
61
|
+
}): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Sets a shared top-level property. The last write wins; apps can't remove properties, only the owner can.
|
|
64
|
+
* @throws {PupsError} `insufficient_scope` if the property isn't declared for a granted well-known path;
|
|
65
|
+
* `invalid_request` for an invalid value.
|
|
66
|
+
*/
|
|
67
|
+
setProperty(property: string, value: string, options?: {
|
|
68
|
+
signal?: AbortSignal;
|
|
69
|
+
}): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=host-meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host-meta.d.ts","sourceRoot":"","sources":["../src/host-meta.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAG/C,wDAAwD;AACxD,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAAA;IAC1C,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAA;CACzB;AAED,8CAA8C;AAC9C,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAAA;IAC1C,KAAK,CAAC,EAAE,YAAY,EAAE,CAAA;IACtB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAA;CACzB;AAUD;;;;;;GAMG;AACH,qBAAa,cAAc;;IAIzB,gBAAgB;gBACJ,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,KAAK;IAKvD,qGAAqG;IACrG,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,4GAA4G;IACtG,GAAG,CAAC,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAQ5E;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAe7C,4GAA4G;IAC5G,iBAAiB,IAAI,MAAM,EAAE;IAS7B;;;;OAIG;IACG,OAAO,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAOxH,0GAA0G;IACpG,UAAU,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAK5G;;;;OAIG;IACG,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CA8B1G"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { PUPS_REL } from './discovery.js';
|
|
2
|
+
import { errorFromResponse, PupsError } from './errors.js';
|
|
3
|
+
import { pathFromPublicUrl, wellKnownEntryFor } from './paths.js';
|
|
4
|
+
import { writeScopePaths } from './scopes.js';
|
|
5
|
+
import { controlChars, fetchWithTimeout, isSameHostOrWww, parseUrl } from './util.js';
|
|
6
|
+
/** Property URIs reserved for PUPS itself. */
|
|
7
|
+
const PUPS_PROPERTY_PREFIX = 'https://bigwide.net/ns/pups#';
|
|
8
|
+
/** Rels nobody may register through PUPS (RFC-6 "Links"). */
|
|
9
|
+
const reservedRels = new Set([PUPS_REL, 'http://webfinger.net/rel/profile-page', 'me', 'authorization_endpoint', 'openid']);
|
|
10
|
+
const maxPropertyLength = 2048;
|
|
11
|
+
/**
|
|
12
|
+
* Edits `host-meta.json` through the server's `host_meta_endpoint` (RFC-6 "Editing host-meta.json").
|
|
13
|
+
*
|
|
14
|
+
* Apps may only add links to their own files, with the rels their scopes allow, and set the properties declared for
|
|
15
|
+
* well-known paths they hold. Which rels are single-valued and which properties take URLs is known only to the
|
|
16
|
+
* server, so those problems surface as `conflict`, `insufficient_scope` or `invalid_request` from the server.
|
|
17
|
+
*/
|
|
18
|
+
export class HostMetaEditor {
|
|
19
|
+
#session;
|
|
20
|
+
#fetch;
|
|
21
|
+
/** @internal */
|
|
22
|
+
constructor(session, fetchFn) {
|
|
23
|
+
this.#session = session;
|
|
24
|
+
this.#fetch = fetchFn;
|
|
25
|
+
}
|
|
26
|
+
/** Whether the server can edit `host-meta.json`. If not, the editing methods throw `unsupported`. */
|
|
27
|
+
get available() {
|
|
28
|
+
return !!this.#session.server.hostMetaEndpoint;
|
|
29
|
+
}
|
|
30
|
+
/** Reads the public `/.well-known/host-meta.json` of the owner's domain, e.g. to show the current links. */
|
|
31
|
+
async get(options = {}) {
|
|
32
|
+
const url = `${this.#session.server.domain}.well-known/host-meta.json`;
|
|
33
|
+
const response = await fetchWithTimeout(this.#fetch, url, { credentials: 'omit', cache: 'no-store', headers: { Accept: 'application/json' } }, 30_000, options.signal);
|
|
34
|
+
if (response.status == 404)
|
|
35
|
+
return { links: [] };
|
|
36
|
+
if (!response.ok)
|
|
37
|
+
throw await errorFromResponse(response, `GET ${url}`);
|
|
38
|
+
return response.json();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether the granted scopes allow a link with `rel` to the file `href` (a path or a public URL):
|
|
42
|
+
* for a file under a well-known path, the `rels` declared for it; for a file in the app's own directory, URI rels
|
|
43
|
+
* on the client host (with or without `www.`).
|
|
44
|
+
*/
|
|
45
|
+
allowsRel(rel, href) {
|
|
46
|
+
let path;
|
|
47
|
+
try {
|
|
48
|
+
path = this.#target(href).path;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
if (reservedRels.has(rel))
|
|
54
|
+
return false;
|
|
55
|
+
if (path.startsWith(this.#session.appDataPath)) {
|
|
56
|
+
const url = parseUrl(rel);
|
|
57
|
+
return !!url && (url.protocol == 'https:' || url.protocol == 'http:') && isSameHostOrWww(url.hostname, this.#session.clientHost);
|
|
58
|
+
}
|
|
59
|
+
return wellKnownEntryFor(path, this.#session.server.wellKnownPaths)?.rels.includes(rel) ?? false;
|
|
60
|
+
}
|
|
61
|
+
/** The properties this session may set: those declared for the well-known paths whose scope was granted. */
|
|
62
|
+
allowedProperties() {
|
|
63
|
+
const granted = new Set(writeScopePaths(this.#session.scopes));
|
|
64
|
+
const properties = this.#session.server.wellKnownPaths
|
|
65
|
+
.filter(entry => granted.has(entry.path))
|
|
66
|
+
.flatMap(entry => entry.properties)
|
|
67
|
+
.filter(p => !p.startsWith(PUPS_PROPERTY_PREFIX));
|
|
68
|
+
return [...new Set(properties)];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Adds a link to one of the app's files. `href` may be a path (relative paths are in the app's own directory) or
|
|
72
|
+
* a public URL; it is sent as the public URL.
|
|
73
|
+
* @throws {PupsError} `insufficient_scope` if the file or rel isn't allowed, `conflict` if a single-valued rel is taken.
|
|
74
|
+
*/
|
|
75
|
+
async addLink(link, options = {}) {
|
|
76
|
+
const { url } = this.#target(link.href);
|
|
77
|
+
if (!this.allowsRel(link.rel, link.href))
|
|
78
|
+
throw new PupsError('insufficient_scope', `The granted scopes don't allow the rel "${link.rel}" for ${url} (RFC-6, Links).`);
|
|
79
|
+
await this.#post({ action: 'add', rel: link.rel, href: url, ...link.type ? { type: link.type } : {} }, `Adding a ${link.rel} link`, options.signal);
|
|
80
|
+
}
|
|
81
|
+
/** Removes a link the app added. Links of the owner and of other apps are never touched by the server. */
|
|
82
|
+
async removeLink(link, options = {}) {
|
|
83
|
+
const { url } = this.#target(link.href);
|
|
84
|
+
await this.#post({ action: 'remove', rel: link.rel, href: url }, `Removing a ${link.rel} link`, options.signal);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Sets a shared top-level property. The last write wins; apps can't remove properties, only the owner can.
|
|
88
|
+
* @throws {PupsError} `insufficient_scope` if the property isn't declared for a granted well-known path;
|
|
89
|
+
* `invalid_request` for an invalid value.
|
|
90
|
+
*/
|
|
91
|
+
async setProperty(property, value, options = {}) {
|
|
92
|
+
if (!this.allowedProperties().includes(property))
|
|
93
|
+
throw new PupsError('insufficient_scope', `The granted scopes don't allow setting "${property}". Allowed: ${this.allowedProperties().join(', ') || 'none'} (RFC-6, Properties).`);
|
|
94
|
+
if (typeof value != 'string' || !value || value.length > maxPropertyLength || controlChars.test(value))
|
|
95
|
+
throw new PupsError('invalid_request', `A property value must be a non-empty string of at most ${maxPropertyLength} characters without control characters (RFC-6, Properties).`);
|
|
96
|
+
await this.#post({ action: 'set-property', property, value }, `Setting ${property}`, options.signal);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The path and public URL of a link target, which must be a file inside the granted scopes.
|
|
100
|
+
* @throws {PupsError} `insufficient_scope`
|
|
101
|
+
*/
|
|
102
|
+
#target(href) {
|
|
103
|
+
const session = this.#session;
|
|
104
|
+
const path = /^[a-z][a-z0-9+.-]*:/i.test(href) ? pathFromPublicUrl(session.server, href) : session.resolve(href);
|
|
105
|
+
if (!path || !session.canWrite(path))
|
|
106
|
+
throw new PupsError('insufficient_scope', `"${href}" is not a file inside the scopes granted to this app. Links may only point to the app's own files (RFC-6, Links).`);
|
|
107
|
+
return { path, url: session.publicUrl(path) };
|
|
108
|
+
}
|
|
109
|
+
async #post(body, what, signal) {
|
|
110
|
+
const endpoint = this.#session.server.hostMetaEndpoint;
|
|
111
|
+
if (!endpoint)
|
|
112
|
+
throw new PupsError('unsupported', `${new URL(this.#session.server.domain).host}'s server can't edit host-meta.json.`);
|
|
113
|
+
const response = await this.#session._authorized(endpoint, {
|
|
114
|
+
method: 'POST',
|
|
115
|
+
headers: { 'Content-Type': 'application/json' },
|
|
116
|
+
body: JSON.stringify(body),
|
|
117
|
+
}, signal);
|
|
118
|
+
if (!response.ok)
|
|
119
|
+
throw await errorFromResponse(response, what);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=host-meta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host-meta.js","sourceRoot":"","sources":["../src/host-meta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC1D,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE7C,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAoBrF,8CAA8C;AAC9C,MAAM,oBAAoB,GAAG,8BAA8B,CAAA;AAE3D,6DAA6D;AAC7D,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,uCAAuC,EAAE,IAAI,EAAE,wBAAwB,EAAE,QAAQ,CAAC,CAAC,CAAA;AAE3H,MAAM,iBAAiB,GAAG,IAAI,CAAA;AAE9B;;;;;;GAMG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