@dasl/fisl 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.
- package/README.md +292 -2
- package/dist/cli.d.ts +13 -0
- package/dist/cli.js +368 -0
- package/dist/mod.d.ts +21 -0
- package/dist/mod.js +21 -0
- package/dist/src/atproto.d.ts +67 -0
- package/dist/src/atproto.js +179 -0
- package/dist/src/cid-core.d.ts +9 -0
- package/dist/src/cid-core.js +39 -0
- package/dist/src/cid.d.ts +18 -0
- package/dist/src/cid.js +45 -0
- package/dist/src/fetch.d.ts +27 -0
- package/dist/src/fetch.js +389 -0
- package/dist/src/fisl/client.d.ts +17 -0
- package/dist/src/fisl/client.js +132 -0
- package/dist/src/fisl/server.d.ts +19 -0
- package/dist/src/fisl/server.js +69 -0
- package/dist/src/index/ingest.d.ts +14 -0
- package/dist/src/index/ingest.js +31 -0
- package/dist/src/index/jetstream.d.ts +13 -0
- package/dist/src/index/jetstream.js +110 -0
- package/dist/src/index/pds.d.ts +15 -0
- package/dist/src/index/pds.js +69 -0
- package/dist/src/index/sqlite.d.ts +16 -0
- package/dist/src/index/sqlite.js +130 -0
- package/dist/src/index/store.d.ts +39 -0
- package/dist/src/index/store.js +74 -0
- package/dist/src/index/verify.d.ts +19 -0
- package/dist/src/index/verify.js +70 -0
- package/dist/src/publish.d.ts +11 -0
- package/dist/src/publish.js +33 -0
- package/dist/src/rasl/server.d.ts +24 -0
- package/dist/src/rasl/server.js +113 -0
- package/dist/src/record-core.d.ts +8 -0
- package/dist/src/record-core.js +58 -0
- package/dist/src/record.d.ts +35 -0
- package/dist/src/record.js +47 -0
- package/dist/src/serve.d.ts +12 -0
- package/dist/src/serve.js +78 -0
- package/package.json +49 -4
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publish, index, serve and fetch content by DASL CID, with FISL and ATFISL.
|
|
3
|
+
*
|
|
4
|
+
* Import this module as `@dasl/fisl`. The Node.js SQLite store is in
|
|
5
|
+
* `@dasl/fisl/sqlite`, and the Node.js HTTP server is in `@dasl/fisl/serve`.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
export * from "./src/cid.ts";
|
|
10
|
+
export * from "./src/record.ts";
|
|
11
|
+
export * from "./src/atproto.ts";
|
|
12
|
+
export * from "./src/publish.ts";
|
|
13
|
+
export * from "./src/fetch.ts";
|
|
14
|
+
export * from "./src/fisl/server.ts";
|
|
15
|
+
export * from "./src/fisl/client.ts";
|
|
16
|
+
export * from "./src/rasl/server.ts";
|
|
17
|
+
export * from "./src/index/store.ts";
|
|
18
|
+
export * from "./src/index/verify.ts";
|
|
19
|
+
export * from "./src/index/ingest.ts";
|
|
20
|
+
export * from "./src/index/pds.ts";
|
|
21
|
+
export * from "./src/index/jetstream.ts";
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publish, index, serve and fetch content by DASL CID, with FISL and ATFISL.
|
|
3
|
+
*
|
|
4
|
+
* Import this module as `@dasl/fisl`. The Node.js SQLite store is in
|
|
5
|
+
* `@dasl/fisl/sqlite`, and the Node.js HTTP server is in `@dasl/fisl/serve`.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
export * from "./src/cid.js";
|
|
10
|
+
export * from "./src/record.js";
|
|
11
|
+
export * from "./src/atproto.js";
|
|
12
|
+
export * from "./src/publish.js";
|
|
13
|
+
export * from "./src/fetch.js";
|
|
14
|
+
export * from "./src/fisl/server.js";
|
|
15
|
+
export * from "./src/fisl/client.js";
|
|
16
|
+
export * from "./src/rasl/server.js";
|
|
17
|
+
export * from "./src/index/store.js";
|
|
18
|
+
export * from "./src/index/verify.js";
|
|
19
|
+
export * from "./src/index/ingest.js";
|
|
20
|
+
export * from "./src/index/pds.js";
|
|
21
|
+
export * from "./src/index/jetstream.js";
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type AtfislRecord } from "./record.ts";
|
|
2
|
+
export interface Identity {
|
|
3
|
+
did: string;
|
|
4
|
+
pds: string;
|
|
5
|
+
/** The repo signing key as a did:key, if the DID document has one. */
|
|
6
|
+
signingKey?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const PLC_DIRECTORY = "https://plc.directory";
|
|
9
|
+
export declare const APPVIEW = "https://public.api.bsky.app";
|
|
10
|
+
export declare class XrpcError extends Error {
|
|
11
|
+
readonly status: number;
|
|
12
|
+
readonly error: string | undefined;
|
|
13
|
+
constructor(status: number, error: string | undefined, message: string);
|
|
14
|
+
}
|
|
15
|
+
/** Call an XRPC method. Queries use GET with params; procedures POST a JSON body. */
|
|
16
|
+
export declare function xrpc<T = unknown>(service: string, method: string, opts?: {
|
|
17
|
+
params?: Record<string, string | number | undefined>;
|
|
18
|
+
body?: unknown;
|
|
19
|
+
token?: string;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}): Promise<T>;
|
|
22
|
+
/** Resolve a handle to a DID: DNS, then .well-known, then the Bluesky AppView. */
|
|
23
|
+
export declare function resolveHandle(handle: string, signal?: AbortSignal): Promise<string>;
|
|
24
|
+
interface DidDocument {
|
|
25
|
+
id: string;
|
|
26
|
+
service?: {
|
|
27
|
+
id: string;
|
|
28
|
+
type: string;
|
|
29
|
+
serviceEndpoint: string;
|
|
30
|
+
}[];
|
|
31
|
+
verificationMethod?: {
|
|
32
|
+
id: string;
|
|
33
|
+
type: string;
|
|
34
|
+
publicKeyMultibase?: string;
|
|
35
|
+
}[];
|
|
36
|
+
}
|
|
37
|
+
export declare function resolveDid(did: string, signal?: AbortSignal): Promise<DidDocument>;
|
|
38
|
+
/** Resolve a handle or DID to its DID, PDS and signing key. */
|
|
39
|
+
export declare function resolveIdentity(handleOrDid: string, signal?: AbortSignal): Promise<Identity>;
|
|
40
|
+
/** An authenticated session on the user's PDS. */
|
|
41
|
+
export interface Session extends Identity {
|
|
42
|
+
accessJwt: string;
|
|
43
|
+
}
|
|
44
|
+
/** Log in with a handle (or DID) and an app password. */
|
|
45
|
+
export declare function login(identifier: string, password: string): Promise<Session>;
|
|
46
|
+
/** Create or replace the record for a CID. */
|
|
47
|
+
export declare function putRecord(s: Session, cid: string, record: AtfislRecord): Promise<{
|
|
48
|
+
uri: string;
|
|
49
|
+
cid: string;
|
|
50
|
+
}>;
|
|
51
|
+
export declare function deleteRecord(s: Session, cid: string): Promise<unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* Create or replace many records in one commit. At most 200 per call.
|
|
54
|
+
* applyWrites has no upsert, so pass the CIDs that already have a record.
|
|
55
|
+
*/
|
|
56
|
+
export declare function putRecords(s: Session, records: {
|
|
57
|
+
cid: string;
|
|
58
|
+
record: AtfislRecord;
|
|
59
|
+
}[], existing?: Set<string>): Promise<unknown>;
|
|
60
|
+
/** Read one record from a repo. Returns null if there is none. */
|
|
61
|
+
export declare function getRecord(id: Identity, cid: string, signal?: AbortSignal): Promise<AtfislRecord | null>;
|
|
62
|
+
/** List every dev.fisl.at record in a repo. */
|
|
63
|
+
export declare function listRecords(id: Identity, signal?: AbortSignal): AsyncGenerator<{
|
|
64
|
+
cid: string;
|
|
65
|
+
record: AtfislRecord;
|
|
66
|
+
}>;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// A small AT Protocol client: identity resolution, app-password sessions
|
|
2
|
+
// and the repo calls that dev.fisl.at needs. Uses plain fetch.
|
|
3
|
+
import { NSID } from "./record.js";
|
|
4
|
+
export const PLC_DIRECTORY = "https://plc.directory";
|
|
5
|
+
export const APPVIEW = "https://public.api.bsky.app";
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
7
|
+
function withTimeout(signal) {
|
|
8
|
+
const timeout = AbortSignal.timeout(DEFAULT_TIMEOUT_MS);
|
|
9
|
+
return signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
10
|
+
}
|
|
11
|
+
export class XrpcError extends Error {
|
|
12
|
+
status;
|
|
13
|
+
error;
|
|
14
|
+
constructor(status, error, message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.error = error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Call an XRPC method. Queries use GET with params; procedures POST a JSON body. */
|
|
21
|
+
export async function xrpc(service, method, opts = {}) {
|
|
22
|
+
const url = new URL(`/xrpc/${method}`, service);
|
|
23
|
+
for (const [k, v] of Object.entries(opts.params ?? {})) {
|
|
24
|
+
if (v !== undefined)
|
|
25
|
+
url.searchParams.set(k, String(v));
|
|
26
|
+
}
|
|
27
|
+
const headers = {};
|
|
28
|
+
if (opts.token)
|
|
29
|
+
headers.authorization = `Bearer ${opts.token}`;
|
|
30
|
+
if (opts.body !== undefined)
|
|
31
|
+
headers["content-type"] = "application/json";
|
|
32
|
+
const res = await fetch(url, {
|
|
33
|
+
signal: withTimeout(opts.signal),
|
|
34
|
+
method: opts.body === undefined ? "GET" : "POST",
|
|
35
|
+
headers,
|
|
36
|
+
body: opts.body === undefined ? undefined : JSON.stringify(opts.body),
|
|
37
|
+
});
|
|
38
|
+
const type = res.headers.get("content-type") ?? "";
|
|
39
|
+
if (!res.ok) {
|
|
40
|
+
const err = type.includes("json") ? await res.json() : {};
|
|
41
|
+
throw new XrpcError(res.status, err.error, `${method}: ${err.message ?? err.error ?? res.statusText}`);
|
|
42
|
+
}
|
|
43
|
+
if (type.includes("json"))
|
|
44
|
+
return await res.json();
|
|
45
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
46
|
+
}
|
|
47
|
+
/** Resolve a handle to a DID: DNS, then .well-known, then the Bluesky AppView. */
|
|
48
|
+
export async function resolveHandle(handle, signal) {
|
|
49
|
+
handle = handle.replace(/^@/, "").toLowerCase();
|
|
50
|
+
try {
|
|
51
|
+
const { resolveTxt } = await import("node:dns/promises");
|
|
52
|
+
const txt = await resolveTxt(`_atproto.${handle}`);
|
|
53
|
+
// A TXT record can be split into chunks.
|
|
54
|
+
const did = txt.map((t) => t.join(""))
|
|
55
|
+
.find((t) => t.startsWith("did="))?.slice(4);
|
|
56
|
+
if (did)
|
|
57
|
+
return did;
|
|
58
|
+
}
|
|
59
|
+
catch { /* fall through */ }
|
|
60
|
+
try {
|
|
61
|
+
const res = await fetch(`https://${handle}/.well-known/atproto-did`, {
|
|
62
|
+
signal: withTimeout(signal),
|
|
63
|
+
});
|
|
64
|
+
const did = (await res.text()).trim();
|
|
65
|
+
if (res.ok && did.startsWith("did:"))
|
|
66
|
+
return did;
|
|
67
|
+
}
|
|
68
|
+
catch { /* fall through */ }
|
|
69
|
+
const { did } = await xrpc(APPVIEW, "com.atproto.identity.resolveHandle", { params: { handle }, signal });
|
|
70
|
+
return did;
|
|
71
|
+
}
|
|
72
|
+
export async function resolveDid(did, signal) {
|
|
73
|
+
let url;
|
|
74
|
+
if (did.startsWith("did:plc:"))
|
|
75
|
+
url = `${PLC_DIRECTORY}/${did}`;
|
|
76
|
+
else if (did.startsWith("did:web:")) {
|
|
77
|
+
const host = decodeURIComponent(did.slice(8));
|
|
78
|
+
if (!host || /[/?#@\s]/.test(host)) {
|
|
79
|
+
throw new Error(`Invalid did:web: ${did}`);
|
|
80
|
+
}
|
|
81
|
+
const parsed = new URL(`https://${host}/.well-known/did.json`);
|
|
82
|
+
if (parsed.username || parsed.password ||
|
|
83
|
+
parsed.pathname !== "/.well-known/did.json" ||
|
|
84
|
+
parsed.search || parsed.hash) {
|
|
85
|
+
throw new Error(`Invalid did:web: ${did}`);
|
|
86
|
+
}
|
|
87
|
+
url = parsed.href;
|
|
88
|
+
}
|
|
89
|
+
else
|
|
90
|
+
throw new Error(`Unsupported DID method: ${did}`);
|
|
91
|
+
const res = await fetch(url, { signal: withTimeout(signal) });
|
|
92
|
+
if (!res.ok)
|
|
93
|
+
throw new Error(`Could not resolve ${did}: ${res.status}`);
|
|
94
|
+
return await res.json();
|
|
95
|
+
}
|
|
96
|
+
/** Resolve a handle or DID to its DID, PDS and signing key. */
|
|
97
|
+
export async function resolveIdentity(handleOrDid, signal) {
|
|
98
|
+
const did = handleOrDid.startsWith("did:")
|
|
99
|
+
? handleOrDid
|
|
100
|
+
: await resolveHandle(handleOrDid, signal);
|
|
101
|
+
const doc = await resolveDid(did, signal);
|
|
102
|
+
const pds = doc.service?.find((s) => s.id.endsWith("#atproto_pds") && s.type === "AtprotoPersonalDataServer")?.serviceEndpoint;
|
|
103
|
+
if (!pds)
|
|
104
|
+
throw new Error(`${did} has no PDS`);
|
|
105
|
+
const key = doc.verificationMethod?.find((v) => v.id.endsWith("#atproto"))
|
|
106
|
+
?.publicKeyMultibase;
|
|
107
|
+
return { did, pds, signingKey: key ? `did:key:${key}` : undefined };
|
|
108
|
+
}
|
|
109
|
+
/** Log in with a handle (or DID) and an app password. */
|
|
110
|
+
export async function login(identifier, password) {
|
|
111
|
+
const id = await resolveIdentity(identifier);
|
|
112
|
+
const res = await xrpc(id.pds, "com.atproto.server.createSession", { body: { identifier: id.did, password } });
|
|
113
|
+
return { ...id, accessJwt: res.accessJwt };
|
|
114
|
+
}
|
|
115
|
+
/** Create or replace the record for a CID. */
|
|
116
|
+
export function putRecord(s, cid, record) {
|
|
117
|
+
return xrpc(s.pds, "com.atproto.repo.putRecord", {
|
|
118
|
+
token: s.accessJwt,
|
|
119
|
+
// The PDS cannot resolve the dev.fisl.at Lexicon, so it cannot validate.
|
|
120
|
+
body: {
|
|
121
|
+
repo: s.did,
|
|
122
|
+
collection: NSID,
|
|
123
|
+
rkey: cid,
|
|
124
|
+
record: { $type: NSID, ...record },
|
|
125
|
+
validate: false,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
export function deleteRecord(s, cid) {
|
|
130
|
+
return xrpc(s.pds, "com.atproto.repo.deleteRecord", {
|
|
131
|
+
token: s.accessJwt,
|
|
132
|
+
body: { repo: s.did, collection: NSID, rkey: cid },
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Create or replace many records in one commit. At most 200 per call.
|
|
137
|
+
* applyWrites has no upsert, so pass the CIDs that already have a record.
|
|
138
|
+
*/
|
|
139
|
+
export function putRecords(s, records, existing = new Set()) {
|
|
140
|
+
return xrpc(s.pds, "com.atproto.repo.applyWrites", {
|
|
141
|
+
token: s.accessJwt,
|
|
142
|
+
body: {
|
|
143
|
+
repo: s.did,
|
|
144
|
+
validate: false,
|
|
145
|
+
writes: records.map(({ cid, record }) => ({
|
|
146
|
+
$type: `com.atproto.repo.applyWrites#${existing.has(cid) ? "update" : "create"}`,
|
|
147
|
+
collection: NSID,
|
|
148
|
+
rkey: cid,
|
|
149
|
+
value: { $type: NSID, ...record },
|
|
150
|
+
})),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/** Read one record from a repo. Returns null if there is none. */
|
|
155
|
+
export async function getRecord(id, cid, signal) {
|
|
156
|
+
try {
|
|
157
|
+
const res = await xrpc(id.pds, "com.atproto.repo.getRecord", { params: { repo: id.did, collection: NSID, rkey: cid }, signal });
|
|
158
|
+
return res.value;
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
if (e instanceof XrpcError && e.error === "RecordNotFound")
|
|
162
|
+
return null;
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** List every dev.fisl.at record in a repo. */
|
|
167
|
+
export async function* listRecords(id, signal) {
|
|
168
|
+
let cursor;
|
|
169
|
+
do {
|
|
170
|
+
const res = await xrpc(id.pds, "com.atproto.repo.listRecords", {
|
|
171
|
+
params: { repo: id.did, collection: NSID, limit: 100, cursor },
|
|
172
|
+
signal,
|
|
173
|
+
});
|
|
174
|
+
for (const r of res.records) {
|
|
175
|
+
yield { cid: r.uri.split("/").pop(), record: r.value };
|
|
176
|
+
}
|
|
177
|
+
cursor = res.records.length ? res.cursor : undefined;
|
|
178
|
+
} while (cursor);
|
|
179
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Cid } from "./cid.js";
|
|
2
|
+
|
|
3
|
+
export function base32Encode(bytes: Uint8Array): string;
|
|
4
|
+
export function base32Decode(s: string): Uint8Array | null;
|
|
5
|
+
export function parseCid(
|
|
6
|
+
s: unknown,
|
|
7
|
+
options?: { bdasl?: boolean },
|
|
8
|
+
): Cid | null;
|
|
9
|
+
export function isCid(s: unknown): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { base32 } from "multiformats/bases/base32";
|
|
2
|
+
import { CID } from "multiformats/cid";
|
|
3
|
+
|
|
4
|
+
const CODEC_RAW = 0x55,
|
|
5
|
+
CODEC_DRISL = 0x71,
|
|
6
|
+
HASH_SHA256 = 0x12,
|
|
7
|
+
HASH_BLAKE3 = 0x1e;
|
|
8
|
+
|
|
9
|
+
export function base32Encode(bytes) {
|
|
10
|
+
return base32.encode(bytes).slice(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function base32Decode(s) {
|
|
14
|
+
try {
|
|
15
|
+
const bytes = base32.decode("b" + s);
|
|
16
|
+
return base32Encode(bytes) === s ? bytes : null;
|
|
17
|
+
} catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Parse a string CID. Returns null if it is not a DASL (or, with `bdasl`, BDASL) CID. */
|
|
23
|
+
export function parseCid(s, { bdasl = true } = {}) {
|
|
24
|
+
if (typeof s !== "string" || s.length !== 59 || s[0] !== "b") return null;
|
|
25
|
+
try {
|
|
26
|
+
const cid = CID.parse(s, base32);
|
|
27
|
+
const { code: hash, digest, size } = cid.multihash;
|
|
28
|
+
if (cid.version !== 1 || size !== 32) return null;
|
|
29
|
+
if (cid.code !== CODEC_RAW && cid.code !== CODEC_DRISL) return null;
|
|
30
|
+
if (hash !== HASH_SHA256 && !(bdasl && hash === HASH_BLAKE3)) return null;
|
|
31
|
+
return { codec: cid.code, hash, digest, string: s };
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isCid(s) {
|
|
38
|
+
return parseCid(s) !== null;
|
|
39
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const CODEC_RAW = 85;
|
|
2
|
+
export declare const CODEC_DRISL = 113;
|
|
3
|
+
export declare const HASH_SHA256 = 18;
|
|
4
|
+
export declare const HASH_BLAKE3 = 30;
|
|
5
|
+
export interface Cid {
|
|
6
|
+
codec: number;
|
|
7
|
+
hash: number;
|
|
8
|
+
digest: Uint8Array;
|
|
9
|
+
string: string;
|
|
10
|
+
}
|
|
11
|
+
export { base32Decode, base32Encode, isCid, parseCid } from "./cid-core.js";
|
|
12
|
+
export declare function cidFromDigest(digest: Uint8Array, codec?: number, hash?: number): string;
|
|
13
|
+
/** The DASL CID of raw bytes, hashed with SHA-256. */
|
|
14
|
+
export declare function cidForBytes(bytes: Uint8Array): Promise<string>;
|
|
15
|
+
/** The DASL CID of a file. Streams the file, so large files are fine. */
|
|
16
|
+
export declare function cidForFile(path: string): Promise<string>;
|
|
17
|
+
/** Check that bytes match a CID, using the CID's hash function. */
|
|
18
|
+
export declare function verifyBytes(cid: Cid, bytes: Uint8Array): Promise<boolean>;
|
package/dist/src/cid.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// DASL and BDASL CIDs: https://dasl.ing/cid.html, https://dasl.ing/bdasl.html
|
|
2
|
+
//
|
|
3
|
+
// A CID is 36 bytes: version 0x01, codec, hash type, hash length 0x20 and a
|
|
4
|
+
// 32-byte digest. Its only string form is "b" + lowercase base32, no padding.
|
|
5
|
+
import { blake3 } from "@noble/hashes/blake3.js";
|
|
6
|
+
export const CODEC_RAW = 0x55;
|
|
7
|
+
export const CODEC_DRISL = 0x71;
|
|
8
|
+
export const HASH_SHA256 = 0x12;
|
|
9
|
+
export const HASH_BLAKE3 = 0x1e;
|
|
10
|
+
// @ts-types="./cid-core.d.ts"
|
|
11
|
+
export { base32Decode, base32Encode, isCid, parseCid } from "./cid-core.js";
|
|
12
|
+
// @ts-types="./cid-core.d.ts"
|
|
13
|
+
import { base32Encode } from "./cid-core.js";
|
|
14
|
+
export function cidFromDigest(digest, codec = CODEC_RAW, hash = HASH_SHA256) {
|
|
15
|
+
if (digest.length !== 32) {
|
|
16
|
+
throw new RangeError(`Expected a 32-byte digest, got ${digest.length}`);
|
|
17
|
+
}
|
|
18
|
+
const bytes = new Uint8Array(36);
|
|
19
|
+
bytes.set([0x01, codec, hash, 0x20]);
|
|
20
|
+
bytes.set(digest, 4);
|
|
21
|
+
return "b" + base32Encode(bytes);
|
|
22
|
+
}
|
|
23
|
+
/** The DASL CID of raw bytes, hashed with SHA-256. */
|
|
24
|
+
export async function cidForBytes(bytes) {
|
|
25
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
26
|
+
return cidFromDigest(digest);
|
|
27
|
+
}
|
|
28
|
+
/** The DASL CID of a file. Streams the file, so large files are fine. */
|
|
29
|
+
export async function cidForFile(path) {
|
|
30
|
+
const [{ createHash }, { createReadStream }] = await Promise.all([
|
|
31
|
+
import("node:crypto"),
|
|
32
|
+
import("node:fs"),
|
|
33
|
+
]);
|
|
34
|
+
const h = createHash("sha256");
|
|
35
|
+
for await (const chunk of createReadStream(path))
|
|
36
|
+
h.update(chunk);
|
|
37
|
+
return cidFromDigest(new Uint8Array(h.digest()));
|
|
38
|
+
}
|
|
39
|
+
/** Check that bytes match a CID, using the CID's hash function. */
|
|
40
|
+
export async function verifyBytes(cid, bytes) {
|
|
41
|
+
const digest = cid.hash === HASH_BLAKE3 ? blake3(bytes) : new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
42
|
+
if (digest.length !== cid.digest.length)
|
|
43
|
+
return false;
|
|
44
|
+
return digest.every((b, i) => b === cid.digest[i]);
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ResolveOptions } from "./fisl/client.ts";
|
|
2
|
+
export interface FetchOptions extends ResolveOptions {
|
|
3
|
+
/** Allow plain http URLs. Off by default. */
|
|
4
|
+
allowHttp?: boolean;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
/** Deadline per download, including DNS and response body. Default 30 seconds. */
|
|
7
|
+
downloadTimeoutMs?: number;
|
|
8
|
+
/** Maximum simultaneous mirror downloads. Default 3. */
|
|
9
|
+
concurrency?: number;
|
|
10
|
+
/** Allow hosts in private or loopback address space. Off by default. */
|
|
11
|
+
allowPrivate?: boolean;
|
|
12
|
+
/** Maximum bytes per addr. Default 1 GiB in memory, unlimited for files. */
|
|
13
|
+
maxSize?: number;
|
|
14
|
+
/** Failure counts by publisher or origin. Pass the same map across calls. */
|
|
15
|
+
failures?: Map<string, number>;
|
|
16
|
+
log?: (msg: string) => void;
|
|
17
|
+
}
|
|
18
|
+
export declare class FetchError extends Error {
|
|
19
|
+
}
|
|
20
|
+
/** Turn an addr URL into the https/http URLs to request. Empty if unsupported. */
|
|
21
|
+
export declare function requestUrls(url: string, cid: string, allowHttp?: boolean): URL[];
|
|
22
|
+
export declare function isPrivateIp(ip: string): boolean;
|
|
23
|
+
export declare function fetchCid(cidString: string, opts?: FetchOptions): Promise<Uint8Array>;
|
|
24
|
+
/** Stream a verified download into a temporary file, then atomically rename it.
|
|
25
|
+
* The destination is replaced only after verification. No default size limit.
|
|
26
|
+
*/
|
|
27
|
+
export declare function downloadCid(cidString: string, destination: string, opts?: FetchOptions): Promise<void>;
|