@myfield/sdk 0.9.2 → 0.9.3
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/CHANGELOG.md +7 -0
- package/README.md +30 -9
- package/dist/index.js +467 -366
- package/package.json +15 -8
- package/types/index.d.ts +395 -137
- package/docs/API.md +0 -298
- package/docs/BUCKET-SPEC.md +0 -173
- package/src/auth/authorization.ts +0 -254
- package/src/auth/flow.ts +0 -93
- package/src/auth/identifiers.ts +0 -46
- package/src/auth/index.ts +0 -17
- package/src/auth/instance.ts +0 -395
- package/src/auth/keys.ts +0 -73
- package/src/auth/proof.ts +0 -26
- package/src/auth/session.ts +0 -16
- package/src/auth/store.ts +0 -90
- package/src/auth/types.ts +0 -47
- package/src/bucket/binary.ts +0 -253
- package/src/bucket/client.ts +0 -426
- package/src/bucket/crypto.ts +0 -58
- package/src/bucket/format.ts +0 -324
- package/src/bucket/index.ts +0 -40
- package/src/bucket/keys.ts +0 -104
- package/src/bucket/store.ts +0 -190
- package/src/bucket/transport.ts +0 -249
- package/src/errors.ts +0 -40
- package/src/facade.ts +0 -698
- package/src/index.ts +0 -29
- package/src/internal/encoding.ts +0 -95
- package/src/remote/bucket.ts +0 -180
- package/src/remote/client.ts +0 -38
- package/src/remote/connection.ts +0 -529
- package/src/remote/feed.ts +0 -80
- package/src/remote/http.ts +0 -78
- package/src/remote/session.ts +0 -31
package/src/remote/http.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { ClientError } from "../errors.ts";
|
|
2
|
-
|
|
3
|
-
export type RequestOptions = { signal?: AbortSignal };
|
|
4
|
-
export type RemoteRequest = (path: string, init?: RequestInit) => Promise<Response>;
|
|
5
|
-
|
|
6
|
-
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
7
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function requireString(value: unknown, name: string): asserts value is string {
|
|
11
|
-
if (typeof value !== "string" || !value.trim()) {
|
|
12
|
-
throw new ClientError("INVALID_INPUT", `${name} must be a non-empty string`);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function invalidResponse(response: Response, message = "Invalid protocol response", cause?: unknown): ClientError {
|
|
17
|
-
return new ClientError("INVALID_RESPONSE", message, {
|
|
18
|
-
status: response.status, requestId: response.headers.get("x-request-id") ?? undefined, cause,
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function networkError(cause: unknown, signal?: AbortSignal | null): never {
|
|
23
|
-
signal?.throwIfAborted();
|
|
24
|
-
throw new ClientError("NETWORK_ERROR", "Network request failed", { retryable: true, cause });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function readJson(response: Response, signal?: AbortSignal): Promise<Record<string, unknown>> {
|
|
28
|
-
let value: unknown;
|
|
29
|
-
try { value = await response.json(); }
|
|
30
|
-
catch (cause) {
|
|
31
|
-
signal?.throwIfAborted();
|
|
32
|
-
if (!(cause instanceof SyntaxError)) return networkError(cause, signal);
|
|
33
|
-
throw invalidResponse(response, "Invalid JSON response", cause);
|
|
34
|
-
}
|
|
35
|
-
signal?.throwIfAborted();
|
|
36
|
-
if (!isRecord(value)) throw invalidResponse(response);
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function createHttpRequest(options: { apiBase: string; fetch?: typeof globalThis.fetch }) {
|
|
41
|
-
requireString(options.apiBase, "apiBase");
|
|
42
|
-
let base: URL;
|
|
43
|
-
try { base = new URL(options.apiBase); }
|
|
44
|
-
catch (cause) { throw new ClientError("INVALID_INPUT", "apiBase must be an HTTP(S) origin", { cause }); }
|
|
45
|
-
if (!["http:", "https:"].includes(base.protocol) || base.pathname !== "/" || base.search || base.hash || base.username || base.password) {
|
|
46
|
-
throw new ClientError("INVALID_INPUT", "apiBase must be an HTTP(S) origin without a path, query, fragment or credentials");
|
|
47
|
-
}
|
|
48
|
-
const fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
49
|
-
if (typeof fetch !== "function") throw new ClientError("INVALID_INPUT", "fetch must be a function");
|
|
50
|
-
return async (path: string, init: RequestInit): Promise<Response> => {
|
|
51
|
-
init.signal?.throwIfAborted();
|
|
52
|
-
let response: Response;
|
|
53
|
-
try { response = await fetch(new URL(path, base).href, { ...init, credentials: "omit" }); }
|
|
54
|
-
catch (cause) { return networkError(cause, init.signal); }
|
|
55
|
-
if (response.ok) return response;
|
|
56
|
-
let body: Record<string, unknown> = {};
|
|
57
|
-
try {
|
|
58
|
-
const value: unknown = await response.json();
|
|
59
|
-
if (isRecord(value)) body = value;
|
|
60
|
-
} catch { init.signal?.throwIfAborted(); }
|
|
61
|
-
const details = isRecord(body.details) ? body.details : {};
|
|
62
|
-
const retry = response.headers.get("retry-after");
|
|
63
|
-
const seconds = retry !== null && /^\d+(?:\.\d+)?$/.test(retry.trim()) ? Number(retry) : NaN;
|
|
64
|
-
const date = retry === null ? NaN : Date.parse(retry);
|
|
65
|
-
const retryAfterMs = Number.isFinite(seconds) ? seconds * 1000
|
|
66
|
-
: Number.isFinite(date) ? Math.max(0, date - Date.now()) : undefined;
|
|
67
|
-
throw new ClientError(typeof body.code === "string" && body.code ? body.code : "HTTP_ERROR",
|
|
68
|
-
typeof body.message === "string" && body.message ? body.message
|
|
69
|
-
: typeof body.error === "string" && body.error ? body.error : `HTTP ${response.status}`, {
|
|
70
|
-
status: response.status,
|
|
71
|
-
requestId: typeof body.requestId === "string" ? body.requestId : response.headers.get("x-request-id") ?? undefined,
|
|
72
|
-
retryable: typeof details.retryable === "boolean" ? details.retryable
|
|
73
|
-
: response.status === 408 || response.status === 429 || response.status >= 500,
|
|
74
|
-
retryAfterMs,
|
|
75
|
-
maxBytes: typeof details.maxBytes === "number" ? details.maxBytes : undefined,
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
}
|
package/src/remote/session.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export type InstanceProof = {
|
|
2
|
-
type: "instance.es256";
|
|
3
|
-
instanceId: string;
|
|
4
|
-
alg: "ES256";
|
|
5
|
-
signedAt: string;
|
|
6
|
-
signature: string;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type CloudSessionCredential =
|
|
10
|
-
| { carrier: "bearer"; token: string; expiresAt: string }
|
|
11
|
-
| { carrier: "cookie"; expiresAt: string };
|
|
12
|
-
|
|
13
|
-
type SessionIdentity = { mfid: string; resourceId: string; instanceId: string };
|
|
14
|
-
type Challenge = { challengeId: string; challenge: string };
|
|
15
|
-
type PostJson = <T>(path: string, body: unknown) => Promise<T>;
|
|
16
|
-
|
|
17
|
-
export function createSessionClient(postJson: PostJson) {
|
|
18
|
-
return {
|
|
19
|
-
createChallenge(identity: SessionIdentity): Promise<Challenge> {
|
|
20
|
-
return postJson("/v1/api/auth/instance-challenge", identity);
|
|
21
|
-
},
|
|
22
|
-
async createSession(input: SessionIdentity & { challengeId: string; proof: InstanceProof }): Promise<CloudSessionCredential> {
|
|
23
|
-
const session = await postJson<{ credential: CloudSessionCredential }>("/v1/api/auth/instance-session", {
|
|
24
|
-
...input, credentialCarrier: "bearer",
|
|
25
|
-
});
|
|
26
|
-
return session.credential;
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type SessionClient = ReturnType<typeof createSessionClient>;
|