@decdn/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/README.md +205 -2
- package/dist/index.cjs +1409 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1386 -0
- package/dist/index.js.map +7 -0
- package/dist/node.cjs +84 -0
- package/dist/node.cjs.map +7 -0
- package/dist/node.d.cts +1 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +63 -0
- package/dist/node.js.map +7 -0
- package/dist/types/base32.d.ts +8 -0
- package/dist/types/capability.d.ts +15 -0
- package/dist/types/client.d.ts +156 -0
- package/dist/types/errors.d.ts +69 -0
- package/dist/types/hash.d.ts +10 -0
- package/dist/types/hex.d.ts +6 -0
- package/dist/types/http.d.ts +12 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/loopback.d.ts +36 -0
- package/dist/types/manifest.d.ts +30 -0
- package/dist/types/mime.d.ts +4 -0
- package/dist/types/node.d.ts +7 -0
- package/dist/types/pointer.d.ts +82 -0
- package/dist/types/uri.d.ts +20 -0
- package/dist/types/verify.d.ts +14 -0
- package/package.json +97 -3
package/README.md
CHANGED
|
@@ -1,3 +1,206 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @decdn/client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript client for [deCDN](https://github.com/decdn/decdn), for Node.js
|
|
4
|
+
20.19+ and evergreen browsers. It speaks HTTP to a `decdn gateway`, which pays
|
|
5
|
+
for every pull and verifies it, and it hashes the bytes again with BLAKE3
|
|
6
|
+
wherever it holds a whole blob named by its hash. No native code, no iroh, no
|
|
7
|
+
wallet library.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @decdn/client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
| Gateway | Where it runs | Who pays |
|
|
14
|
+
| -------------------------------------------------------- | ----------------------------------------------------------------- | ------------------------------------------------- |
|
|
15
|
+
| Public (`decdn gateway --public-url https://decdn.link`) | Node or any web page | The operator's pool, within a daily allowance |
|
|
16
|
+
| Loopback (`decdn gateway`, `http://127.0.0.1:8484`) | Node with the gateway token, or a page on one of its site origins | The capability registered for the requesting host |
|
|
17
|
+
|
|
18
|
+
The gateway's HTTP contract is
|
|
19
|
+
[appendix-gateway](https://github.com/decdn/decdn/blob/main/adr/appendix-gateway.md);
|
|
20
|
+
bundles and their manifests are
|
|
21
|
+
[appendix-bundles](https://github.com/decdn/decdn/blob/main/adr/appendix-bundles.md).
|
|
22
|
+
|
|
23
|
+
## Compatibility
|
|
24
|
+
|
|
25
|
+
| `@decdn/client` | `decdn gateway` |
|
|
26
|
+
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
27
|
+
| 0.1 | v0.3.0 or later for browser pages and public gateways ([cross-origin reads](https://github.com/decdn/decdn/blob/main/adr/appendix-gateway.md#cross-origin-reads)); any loopback gateway from Node |
|
|
28
|
+
|
|
29
|
+
CI runs the contract suite against the floor and the latest decdn release, with
|
|
30
|
+
each release's signed binaries ([CONTRIBUTING.md](CONTRIBUTING.md#release-gateways)).
|
|
31
|
+
|
|
32
|
+
## Public gateway
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { DecdnClient } from "@decdn/client";
|
|
36
|
+
|
|
37
|
+
const client = await DecdnClient.open({ gatewayUrl: "https://decdn.link" });
|
|
38
|
+
|
|
39
|
+
// A blob, streamed and hashed as it arrives.
|
|
40
|
+
const blob = await client.get("decdn:<64 hex>");
|
|
41
|
+
blob.verified; // true
|
|
42
|
+
const bytes = await blob.bytes(); // rejects with HASH_MISMATCH on bad bytes
|
|
43
|
+
|
|
44
|
+
// One file of a bundle, checked against the bundle's manifest.
|
|
45
|
+
const page = await client.get("decdn:<manifest hash>/docs/index.html");
|
|
46
|
+
const text = await page.text();
|
|
47
|
+
|
|
48
|
+
// A window of a sized entry: 206, not verifiable.
|
|
49
|
+
const part = await client.get("decdn:<manifest hash>/model.bin", {
|
|
50
|
+
range: { offset: 0, length: 1 << 20 },
|
|
51
|
+
});
|
|
52
|
+
part.contentRange; // { offset, length, size }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The same code runs in a browser on any origin: a public gateway answers every
|
|
56
|
+
origin, `Range` preflights included.
|
|
57
|
+
|
|
58
|
+
## Loopback gateway (Node)
|
|
59
|
+
|
|
60
|
+
A loopback gateway pays with a capability a pool owner issued to one of its
|
|
61
|
+
delegate keys. Mint the delegate, have the owner assign it, and register it
|
|
62
|
+
for the host your requests name:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { DecdnClient } from "@decdn/client";
|
|
66
|
+
import { readLoopbackToken } from "@decdn/client/node";
|
|
67
|
+
|
|
68
|
+
const client = await DecdnClient.open({
|
|
69
|
+
gatewayUrl: "http://127.0.0.1:8484",
|
|
70
|
+
token: await readLoopbackToken(), // ~/.decdn/gateway/token
|
|
71
|
+
host: "example.com", // sent as x-decdn-host
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const { address } = await client.loopback.delegate();
|
|
75
|
+
// The pool owner: decdn pool assign --pool <id> --signer <address> ...
|
|
76
|
+
await client.loopback.registerCapability({
|
|
77
|
+
capability: "dcap1:...",
|
|
78
|
+
scope: "example.com",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const r = await client.get("decdn:<64 hex>");
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`client.loopback.capabilities()` lists what the gateway holds. Without a
|
|
85
|
+
covering capability a request fails with `PAYMENT_REQUIRED`. A `dcap1:` token
|
|
86
|
+
decodes offline with `parseCapability`.
|
|
87
|
+
|
|
88
|
+
The SDK does not start the gateway. Run it yourself (`decdn gateway`, or the
|
|
89
|
+
`decdn/decdn` Docker image). `open()` treats a `localhost`, `127.0.0.1` or
|
|
90
|
+
`::1` URL as a loopback gateway; a public gateway you run locally needs
|
|
91
|
+
another host name, such as `decdn.localhost`.
|
|
92
|
+
|
|
93
|
+
## Pages on a site origin
|
|
94
|
+
|
|
95
|
+
A bundle opened as a website lives on its own origin,
|
|
96
|
+
`https://<hash32>.decdn.link/` or `http://<hash32>.localhost:8484/`. A page
|
|
97
|
+
there opens the client with no URL:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const client = await DecdnClient.open();
|
|
101
|
+
client.context; // "site"
|
|
102
|
+
const style = await client.get("/css/site.css"); // its own bundle, verified
|
|
103
|
+
const other = await client.get("decdn:<64 hex>"); // through /_decdn/
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
On a namespace host (`https://<ns>.decdn.link/`), pass the gateway URL so
|
|
107
|
+
the host is recognised: `DecdnClient.open({ gatewayUrl: "https://decdn.link" })`.
|
|
108
|
+
The bundle there is whatever the pointer names, so the page's own paths are
|
|
109
|
+
not verified and `manifest()` needs a hash.
|
|
110
|
+
|
|
111
|
+
A page on a loopback site origin reaches the gateway's delegate and capability
|
|
112
|
+
API with a second client on the gateway itself:
|
|
113
|
+
`DecdnClient.open({ gatewayUrl: "http://127.0.0.1:8484" })`. A loopback
|
|
114
|
+
`gatewayUrl` opens a `loopback` client from any page; without the token, which
|
|
115
|
+
a page cannot send, it reaches that API alone.
|
|
116
|
+
|
|
117
|
+
## Verification
|
|
118
|
+
|
|
119
|
+
`get` takes `verify: "auto" | true | false` (default `"auto"`, or the
|
|
120
|
+
client's own default from `open`). `response.verified` says what happened:
|
|
121
|
+
|
|
122
|
+
| Request | `verified` | How |
|
|
123
|
+
| --------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
124
|
+
| A blob by hash | `true` | The whole body hashes to the URI's hash. |
|
|
125
|
+
| A bundle entry | `true` | The client reads the bundle manifest (once per client, itself verified) and the body hashes to the entry's hash. |
|
|
126
|
+
| A page or stylesheet entry | `true` | A site rewrites HTML and CSS as it serves them, so the client fetches these by their entry hash instead. |
|
|
127
|
+
| A `range` of a bundle entry | `"skipped"` | A window does not hash to anything. A blob has no declared size, so a range on one is refused with `CONFIG`. |
|
|
128
|
+
| A bundle's index or listing | `"skipped"` | The gateway renders it. |
|
|
129
|
+
| Any, with `verify: false` | `false` | Nothing is hashed and no manifest is read. |
|
|
130
|
+
|
|
131
|
+
`verify: true` refuses a request that cannot be verified with `CONFIG`.
|
|
132
|
+
|
|
133
|
+
A verified body is hashed as it streams: `response.body` ends with an error
|
|
134
|
+
(`HASH_MISMATCH`, or `TRUNCATED` when the gateway cuts it) instead of `done`,
|
|
135
|
+
and `bytes()`, `text()` and `json()` reject. Bytes read before the end are not
|
|
136
|
+
yet verified; act on them only once the stream is done.
|
|
137
|
+
|
|
138
|
+
## Errors
|
|
139
|
+
|
|
140
|
+
Every failure is a `DecdnError` with a stable `code`:
|
|
141
|
+
|
|
142
|
+
| Code | When | Extra fields |
|
|
143
|
+
| ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------- |
|
|
144
|
+
| `PAYMENT_REQUIRED` | 402: the allowance is spent, the entry does not fit what is left, or no loopback capability covers the host | `retryAfterSecs`, `fallbackUri` |
|
|
145
|
+
| `POOL_UNFUNDED` | 503: the gateway's pool cannot pay | `retryAfterSecs`, `fallbackUri` |
|
|
146
|
+
| `RANGE_NOT_SATISFIABLE` | 416 | `size` |
|
|
147
|
+
| `CONFLICT` | 409: a pointer `seq` that is not newer, a capability already registered | `seq` |
|
|
148
|
+
| `NOT_FOUND`, `UNAUTHORIZED`, `FORBIDDEN`, `RATE_LIMITED`, `UPSTREAM`, `PAYLOAD_TOO_LARGE`, `UNPROCESSABLE` | 404, 401, 403, 429, 502, 413, 422 | `status`, `body` |
|
|
149
|
+
| `BAD_URI` | A `decdn:` URI that does not parse, or 400 | |
|
|
150
|
+
| `HASH_MISMATCH`, `TRUNCATED` | The body does not hash to its hash, or is shorter or longer than declared | |
|
|
151
|
+
| `MALFORMED_MANIFEST`, `BAD_CAPABILITY` | A manifest or `dcap1:` token that does not decode | |
|
|
152
|
+
| `NETWORK`, `CONFIG`, `HTTP` | The gateway is unreachable; the client cannot do what was asked; another status | |
|
|
153
|
+
|
|
154
|
+
`fallbackUri` is the canonical `decdn:` URI: fetch it with a paying client of
|
|
155
|
+
your own (`decdn get <uri>`), or wait `retryAfterSecs`. The SDK retries
|
|
156
|
+
nothing on its own; `get(uri, { retry: true })` waits out one `Retry-After`
|
|
157
|
+
and tries once more.
|
|
158
|
+
|
|
159
|
+
## Namespace pointers
|
|
160
|
+
|
|
161
|
+
On a public gateway, the owner of a namespace on the `PublisherRegistry`
|
|
162
|
+
points it at a bundle, and `https://<ns>.<host>/` serves that bundle:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
const client = await DecdnClient.open({
|
|
166
|
+
gatewayUrl: "https://decdn.link",
|
|
167
|
+
pointer: { chainId: 421614, publisherRegistry: "0x..." },
|
|
168
|
+
});
|
|
169
|
+
const set = await client.pointer.set({
|
|
170
|
+
namespace: 42n,
|
|
171
|
+
root: "decdn:<manifest hash>/",
|
|
172
|
+
signer: { privateKey: "0x..." }, // or a viem account: anything with signTypedData
|
|
173
|
+
});
|
|
174
|
+
set.url; // https://42.decdn.link/
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The pointer is EIP-712 typed data (`pointerTypedData` gives it to a wallet),
|
|
178
|
+
`seq` defaults to the current Unix second, and the gateway takes only a `seq`
|
|
179
|
+
above the last one it holds from the same signer. The chain id and registry
|
|
180
|
+
are the deployment the gateway verifies against.
|
|
181
|
+
|
|
182
|
+
## Helpers
|
|
183
|
+
|
|
184
|
+
`parseUri` and `formatUri` follow the Rust client's `decdn:` grammar
|
|
185
|
+
exactly. `hashToBase32` and `base32ToHash` convert a hash to its site label.
|
|
186
|
+
`blake3Hex` and `createBlake3` hash. `parseManifest` reads a v1 manifest.
|
|
187
|
+
`client.manifest`, `client.resolve`, `client.head`, `client.siteUrl` and
|
|
188
|
+
`client.namespaceUrl` read and name content without fetching it whole.
|
|
189
|
+
Namespaces, sequence numbers, caps and expiries are `bigint`.
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
```sh
|
|
194
|
+
pnpm install
|
|
195
|
+
pnpm lint # eslint, prettier, tsc
|
|
196
|
+
pnpm test # unit tests and the contract suite against the fake gateway
|
|
197
|
+
pnpm test:browser # Chromium smoke run (pnpm exec playwright install chromium-headless-shell)
|
|
198
|
+
pnpm build # dist/: ESM, CJS and types
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
`test/fake-gateway.ts` reproduces the gateway's HTTP contract. The contract
|
|
202
|
+
suite in `test/contract` runs against it by default, and against a real
|
|
203
|
+
gateway in decdn's CI
|
|
204
|
+
([`gateway_sdk.rs`](https://github.com/decdn/decdn/blob/main/crates/e2e/tests/gateway_sdk.rs)),
|
|
205
|
+
so the two cannot drift apart. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
206
|
+
Releases are cut from signed `v*` tags ([RELEASING.md](RELEASING.md)).
|