@decdn/client 0.0.0-stage → 0.1.1
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-APACHE +201 -0
- package/LICENSE-MIT +19 -0
- package/README.md +308 -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 +99 -3
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or exemplary damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2026 deCDN Contributors
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,309 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- markdownlint-disable-file MD033 MD041 -->
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://decdn.org">
|
|
4
|
+
<picture>
|
|
5
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/decdn/decdn/main/.github/assets/wordmark-dark.svg">
|
|
6
|
+
<img alt="deCDN" src="https://raw.githubusercontent.com/decdn/decdn/main/.github/assets/wordmark-light.svg" width="150">
|
|
7
|
+
</picture>
|
|
8
|
+
</a>
|
|
9
|
+
</p>
|
|
2
10
|
|
|
3
|
-
|
|
11
|
+
<h1 align="center">@decdn/client</h1>
|
|
12
|
+
|
|
13
|
+
<h3 align="center">TypeScript client for deCDN: verified fetches, bundles, loopback capabilities and namespace pointers.</h3>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="https://github.com/decdn/sdk-typescript/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://img.shields.io/github/actions/workflow/status/decdn/sdk-typescript/ci.yml?branch=main&label=CI&logo=github" alt="CI"></a>
|
|
17
|
+
<a href="https://github.com/decdn/sdk-typescript/actions/workflows/codeql.yml?query=branch%3Amain"><img src="https://img.shields.io/github/actions/workflow/status/decdn/sdk-typescript/codeql.yml?branch=main&label=CodeQL&logo=github" alt="CodeQL"></a>
|
|
18
|
+
<a href="https://github.com/decdn/sdk-typescript/actions/workflows/gateway-releases.yml"><img src="https://img.shields.io/github/actions/workflow/status/decdn/sdk-typescript/gateway-releases.yml?branch=main&label=gateway%20releases&logo=github" alt="Contract suite against released gateways"></a>
|
|
19
|
+
<a href="https://www.npmjs.com/package/@decdn/client"><img src="https://img.shields.io/npm/v/@decdn/client?logo=npm" alt="npm"></a>
|
|
20
|
+
<a href="https://bundlejs.com/?q=%40decdn%2Fclient"><img src="https://img.shields.io/bundlejs/size/@decdn/client?label=minzip" alt="Bundle size"></a>
|
|
21
|
+
<a href="https://www.npmjs.com/package/@decdn/client"><img src="https://img.shields.io/npm/types/@decdn/client?logo=typescript&logoColor=white" alt="Types: TypeScript"></a>
|
|
22
|
+
<a href="#compatibility"><img src="https://img.shields.io/node/v/@decdn/client?logo=node.js&logoColor=white" alt="Node.js"></a>
|
|
23
|
+
<a href="#compatibility"><img src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Fdecdn%2Fsdk-typescript%2Fmain%2Fpackage.json&query=%24.decdn.gatewayFloor&label=decdn%20gateway&prefix=%E2%89%A5%20v" alt="decdn gateway floor"></a>
|
|
24
|
+
<a href="#license"><img src="https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue" alt="License: MIT OR Apache-2.0"></a>
|
|
25
|
+
<a href="https://www.conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow" alt="Conventional Commits"></a>
|
|
26
|
+
<a href="https://discord.gg/vVbNswKVGX"><img src="https://img.shields.io/badge/chat-Discord-5865F2?logo=discord&logoColor=white" alt="Discord"></a>
|
|
27
|
+
</p>
|
|
28
|
+
|
|
29
|
+
<h3 align="center">
|
|
30
|
+
<a href="https://decdn.org">Website</a> ·
|
|
31
|
+
<a href="https://docs.decdn.org">Docs</a> ·
|
|
32
|
+
<a href="https://github.com/decdn/decdn/blob/main/adr/appendix-gateway.md">Gateway spec</a> ·
|
|
33
|
+
<a href="https://github.com/decdn/decdn">deCDN</a> ·
|
|
34
|
+
<a href="https://discord.gg/vVbNswKVGX">Discord</a>
|
|
35
|
+
</h3>
|
|
36
|
+
|
|
37
|
+
TypeScript client for [deCDN](https://github.com/decdn/decdn), for Node.js
|
|
38
|
+
20.19+ and evergreen browsers. It speaks HTTP to a `decdn gateway`, which pays
|
|
39
|
+
for every pull and verifies it, and it hashes the bytes again with BLAKE3
|
|
40
|
+
wherever it holds a whole blob named by its hash. No native code, no iroh, no
|
|
41
|
+
wallet library.
|
|
42
|
+
|
|
43
|
+
- **Verified streaming.** A blob, or a whole bundle entry, is hashed with
|
|
44
|
+
BLAKE3 as it streams; a body that does not hash to its name ends with
|
|
45
|
+
`HASH_MISMATCH` instead of `done`. Ranges and rendered pages are
|
|
46
|
+
`"skipped"` ([Verification](#verification)).
|
|
47
|
+
- **Bundles.** Entries are checked against the bundle manifest, which is
|
|
48
|
+
itself verified and cached per client.
|
|
49
|
+
- **Byte ranges** of sized entries, for large files such as model weights. A
|
|
50
|
+
window does not hash to anything, so its `verified` is `"skipped"`.
|
|
51
|
+
- **Pages on a site origin.** `DecdnClient.open()` with no URL works from a
|
|
52
|
+
page on a bundle's site origin (`<hash32>.<host>`); a namespace host needs
|
|
53
|
+
`gatewayUrl`.
|
|
54
|
+
- **Loopback gateways.** Mint delegate keys and register capabilities on a
|
|
55
|
+
local `decdn gateway`.
|
|
56
|
+
- **Namespace pointers.** Point a `PublisherRegistry` namespace at a bundle on
|
|
57
|
+
a public gateway, signing EIP-712 typed data with a private key or a
|
|
58
|
+
viem-style `signTypedData(typedData)` signer.
|
|
59
|
+
- **Small and typed.** ESM, CJS and declarations; runtime dependencies
|
|
60
|
+
`@noble/hashes` and `@noble/curves` only; namespaces, seq, caps and expiries
|
|
61
|
+
are `bigint`.
|
|
62
|
+
|
|
63
|
+
## Contents
|
|
64
|
+
|
|
65
|
+
- [Install](#install)
|
|
66
|
+
- [Gateways](#gateways)
|
|
67
|
+
- [Compatibility](#compatibility)
|
|
68
|
+
- [Public gateway](#public-gateway)
|
|
69
|
+
- [Loopback gateway (Node)](#loopback-gateway-node)
|
|
70
|
+
- [Pages on a site origin](#pages-on-a-site-origin)
|
|
71
|
+
- [Verification](#verification)
|
|
72
|
+
- [Errors](#errors)
|
|
73
|
+
- [Namespace pointers](#namespace-pointers)
|
|
74
|
+
- [API at a glance](#api-at-a-glance)
|
|
75
|
+
- [Development](#development)
|
|
76
|
+
- [License](#license)
|
|
77
|
+
|
|
78
|
+
## Install
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
pnpm add @decdn/client
|
|
82
|
+
# or
|
|
83
|
+
npm install @decdn/client
|
|
84
|
+
# or
|
|
85
|
+
yarn add @decdn/client
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Gateways
|
|
89
|
+
|
|
90
|
+
The client talks to one of two kinds of gateway:
|
|
91
|
+
|
|
92
|
+
| Gateway | Where it runs | Who pays |
|
|
93
|
+
| -------------------------------------------------------- | ----------------------------------------------------------------- | ------------------------------------------------- |
|
|
94
|
+
| Public (`decdn gateway --public-url https://decdn.link`) | Node or any web page | The operator's pool, within a daily allowance |
|
|
95
|
+
| 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 |
|
|
96
|
+
|
|
97
|
+
The gateway's HTTP contract is
|
|
98
|
+
[appendix-gateway](https://github.com/decdn/decdn/blob/main/adr/appendix-gateway.md);
|
|
99
|
+
bundles and their manifests are
|
|
100
|
+
[appendix-bundles](https://github.com/decdn/decdn/blob/main/adr/appendix-bundles.md).
|
|
101
|
+
|
|
102
|
+
## Compatibility
|
|
103
|
+
|
|
104
|
+
Node.js 20.19 or later, and evergreen browsers.
|
|
105
|
+
|
|
106
|
+
| `@decdn/client` | `decdn gateway` |
|
|
107
|
+
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
108
|
+
| 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 |
|
|
109
|
+
|
|
110
|
+
CI runs the contract suite against the floor and the latest decdn release, with
|
|
111
|
+
each release's signed binaries ([CONTRIBUTING.md](CONTRIBUTING.md#release-gateways)).
|
|
112
|
+
|
|
113
|
+
## Public gateway
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { DecdnClient } from "@decdn/client";
|
|
117
|
+
|
|
118
|
+
const client = await DecdnClient.open({ gatewayUrl: "https://decdn.link" });
|
|
119
|
+
|
|
120
|
+
// A blob, streamed and hashed as it arrives.
|
|
121
|
+
const blob = await client.get("decdn:<64 hex>");
|
|
122
|
+
blob.verified; // true
|
|
123
|
+
const bytes = await blob.bytes(); // rejects with HASH_MISMATCH on bad bytes
|
|
124
|
+
|
|
125
|
+
// One file of a bundle, checked against the bundle's manifest.
|
|
126
|
+
const page = await client.get("decdn:<manifest hash>/docs/index.html");
|
|
127
|
+
const text = await page.text();
|
|
128
|
+
|
|
129
|
+
// A window of a sized entry: 206, not verifiable.
|
|
130
|
+
const part = await client.get("decdn:<manifest hash>/model.bin", {
|
|
131
|
+
range: { offset: 0, length: 1 << 20 },
|
|
132
|
+
});
|
|
133
|
+
part.contentRange; // { offset, length, size }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The same code runs in a browser on any origin: a public gateway answers every
|
|
137
|
+
origin, `Range` preflights included.
|
|
138
|
+
|
|
139
|
+
## Loopback gateway (Node)
|
|
140
|
+
|
|
141
|
+
A loopback gateway pays with a capability a pool owner issued to one of its
|
|
142
|
+
delegate keys. Mint the delegate, have the owner assign it, and register it
|
|
143
|
+
for the host your requests name:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { DecdnClient } from "@decdn/client";
|
|
147
|
+
import { readLoopbackToken } from "@decdn/client/node";
|
|
148
|
+
|
|
149
|
+
const client = await DecdnClient.open({
|
|
150
|
+
gatewayUrl: "http://127.0.0.1:8484",
|
|
151
|
+
token: await readLoopbackToken(), // ~/.decdn/gateway/token
|
|
152
|
+
host: "example.com", // sent as x-decdn-host
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const { address } = await client.loopback.delegate();
|
|
156
|
+
// The pool owner: decdn pool assign --pool <id> --signer <address> ...
|
|
157
|
+
await client.loopback.registerCapability({
|
|
158
|
+
capability: "dcap1:...",
|
|
159
|
+
scope: "example.com",
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const r = await client.get("decdn:<64 hex>");
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`client.loopback.capabilities()` lists what the gateway holds. Without a
|
|
166
|
+
covering capability a request fails with `PAYMENT_REQUIRED`. A `dcap1:` token
|
|
167
|
+
decodes offline with `parseCapability`.
|
|
168
|
+
|
|
169
|
+
The SDK does not start the gateway. Run it yourself (`decdn gateway`, or the
|
|
170
|
+
`decdn/decdn` Docker image). `open()` treats a `localhost`, `127.0.0.1` or
|
|
171
|
+
`::1` URL as a loopback gateway; a public gateway you run locally needs
|
|
172
|
+
another host name, such as `decdn.localhost`.
|
|
173
|
+
|
|
174
|
+
## Pages on a site origin
|
|
175
|
+
|
|
176
|
+
A bundle opened as a website lives on its own origin,
|
|
177
|
+
`https://<hash32>.decdn.link/` or `http://<hash32>.localhost:8484/`. A page
|
|
178
|
+
there opens the client with no URL:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
const client = await DecdnClient.open();
|
|
182
|
+
client.context; // "site"
|
|
183
|
+
const style = await client.get("/css/site.css"); // its own bundle, verified
|
|
184
|
+
const other = await client.get("decdn:<64 hex>"); // through /_decdn/
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
On a namespace host (`https://<ns>.decdn.link/`), pass the gateway URL so
|
|
188
|
+
the host is recognised: `DecdnClient.open({ gatewayUrl: "https://decdn.link" })`.
|
|
189
|
+
The bundle there is whatever the pointer names, so the page's own paths are
|
|
190
|
+
not verified and `manifest()` needs a hash.
|
|
191
|
+
|
|
192
|
+
A page on a loopback site origin reaches the gateway's delegate and capability
|
|
193
|
+
API with a second client on the gateway itself:
|
|
194
|
+
`DecdnClient.open({ gatewayUrl: "http://127.0.0.1:8484" })`. A loopback
|
|
195
|
+
`gatewayUrl` opens a `loopback` client from any page; without the token, which
|
|
196
|
+
a page cannot send, it reaches that API alone.
|
|
197
|
+
|
|
198
|
+
## Verification
|
|
199
|
+
|
|
200
|
+
`get` takes `verify: "auto" | true | false` (default `"auto"`, or the
|
|
201
|
+
client's own default from `open`). `response.verified` says what happened:
|
|
202
|
+
|
|
203
|
+
| Request | `verified` | How |
|
|
204
|
+
| --------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------ |
|
|
205
|
+
| A blob by hash | `true` | The whole body hashes to the URI's hash. |
|
|
206
|
+
| A bundle entry | `true` | The client reads the bundle manifest (cached per client, itself verified) and the body hashes to the entry's hash. |
|
|
207
|
+
| 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. |
|
|
208
|
+
| 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`. |
|
|
209
|
+
| A bundle's index or listing | `"skipped"` | The gateway renders it. |
|
|
210
|
+
| Any, with `verify: false` | `false` | Nothing is hashed and no manifest is read. |
|
|
211
|
+
|
|
212
|
+
`verify: true` refuses a request that cannot be verified with `CONFIG`.
|
|
213
|
+
|
|
214
|
+
A verified body is hashed as it streams: `response.body` ends with an error
|
|
215
|
+
(`HASH_MISMATCH`, or `TRUNCATED` when the gateway cuts it) instead of `done`,
|
|
216
|
+
and `bytes()`, `text()` and `json()` reject. Bytes read before the end are not
|
|
217
|
+
yet verified; act on them only once the stream is done.
|
|
218
|
+
|
|
219
|
+
## Errors
|
|
220
|
+
|
|
221
|
+
Every failure is a `DecdnError` with a stable `code`:
|
|
222
|
+
|
|
223
|
+
| Code | When | Extra fields |
|
|
224
|
+
| ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------- |
|
|
225
|
+
| `PAYMENT_REQUIRED` | 402: the allowance is spent, the entry does not fit what is left, or no loopback capability covers the host | `retryAfterSecs`, `fallbackUri` |
|
|
226
|
+
| `POOL_UNFUNDED` | 503: the gateway's pool cannot pay | `retryAfterSecs`, `fallbackUri` |
|
|
227
|
+
| `RANGE_NOT_SATISFIABLE` | 416 | `size` |
|
|
228
|
+
| `CONFLICT` | 409: a pointer `seq` that is not newer, a capability already registered | `seq` |
|
|
229
|
+
| `NOT_FOUND`, `UNAUTHORIZED`, `FORBIDDEN`, `RATE_LIMITED`, `UPSTREAM`, `PAYLOAD_TOO_LARGE`, `UNPROCESSABLE` | 404, 401, 403, 429, 502, 413, 422 | `status`, `body` |
|
|
230
|
+
| `BAD_URI` | A `decdn:` URI that does not parse, or 400 | |
|
|
231
|
+
| `HASH_MISMATCH`, `TRUNCATED` | The body does not hash to its hash, or is shorter or longer than declared | |
|
|
232
|
+
| `MALFORMED_MANIFEST`, `BAD_CAPABILITY` | A manifest or `dcap1:` token that does not decode | |
|
|
233
|
+
| `NETWORK`, `CONFIG`, `HTTP` | The gateway is unreachable; the client cannot do what was asked; another status | |
|
|
234
|
+
|
|
235
|
+
`fallbackUri` is the canonical `decdn:` URI: fetch it with a paying client of
|
|
236
|
+
your own (`decdn get <uri>`), or wait `retryAfterSecs`. The SDK retries
|
|
237
|
+
nothing on its own; `get(uri, { retry: true })` waits out one `Retry-After`
|
|
238
|
+
and tries once more.
|
|
239
|
+
|
|
240
|
+
## Namespace pointers
|
|
241
|
+
|
|
242
|
+
On a public gateway, the owner of a namespace on the `PublisherRegistry`
|
|
243
|
+
points it at a bundle, and `https://<ns>.<host>/` serves that bundle:
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
const client = await DecdnClient.open({
|
|
247
|
+
gatewayUrl: "https://decdn.link",
|
|
248
|
+
pointer: { chainId: 421614, publisherRegistry: "0x..." },
|
|
249
|
+
});
|
|
250
|
+
const set = await client.pointer.set({
|
|
251
|
+
namespace: 42n,
|
|
252
|
+
root: "decdn:<manifest hash>/",
|
|
253
|
+
signer: { privateKey: "0x..." }, // or a viem account: anything with signTypedData
|
|
254
|
+
});
|
|
255
|
+
set.url; // https://42.decdn.link/
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
The pointer is EIP-712 typed data (`pointerTypedData` gives it to a wallet),
|
|
259
|
+
`seq` defaults to the current Unix second, and the gateway takes only a `seq`
|
|
260
|
+
above the last one it holds from the same signer. The chain id and registry
|
|
261
|
+
are the deployment the gateway verifies against.
|
|
262
|
+
|
|
263
|
+
## API at a glance
|
|
264
|
+
|
|
265
|
+
| Export | What it does |
|
|
266
|
+
| ------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
267
|
+
| `DecdnClient.open(opts?)` | Opens a client on a public or loopback gateway, or on the site origin a page is served from |
|
|
268
|
+
| `client.get(uri, opts?)` | Fetches a blob, a bundle, a bundle entry or a range (or, on a site page, a path of its own bundle) as a `DecdnResponse` (`verified`, `body`, `bytes`, `text`, `json`) |
|
|
269
|
+
| `client.head(uri)` | The size, hash and content type of what a URI names, without its bytes |
|
|
270
|
+
| `client.resolve(uri)` | A URI's kind, hash, bare and site URLs, and its manifest entry (reads the manifest for a bundle or entry) |
|
|
271
|
+
| `client.manifest(hashOrUri?, signal?)` | A bundle's verified manifest, cached per client; on a site page, the page's own bundle when omitted |
|
|
272
|
+
| `client.siteUrl(uri)`, `client.namespaceUrl(ns)` | The site URL of a bundle or entry; the URL a namespace's pointer serves at (`https://<ns>.<host>/`). Neither makes a request |
|
|
273
|
+
| `client.loopback.delegate()` | Mints a delegate key on a loopback gateway, held for 15 minutes unless a capability claims it |
|
|
274
|
+
| `client.loopback.registerCapability({ capability, scope })`, `.capabilities()` | Registers a `dcap1:` capability for a scope (`example.com`, `*.example.com` or `*`); lists the capabilities the gateway holds (needs the token) |
|
|
275
|
+
| `client.pointer.set(args)` | Signs and publishes a namespace pointer on a public gateway |
|
|
276
|
+
| `pointerTypedData`, `pointerSigningHash`, `signPointer`, `recoverPointerSigner` | The pointer's EIP-712 typed data and signing hash, its signature, and the signer a signature recovers to |
|
|
277
|
+
| `parseUri`, `formatUri` | Parse and canonically format a `decdn:` URI, following the Rust client's grammar exactly (`BAD_URI` on anything it refuses) |
|
|
278
|
+
| `hashToBase32`, `base32ToHash` | A hash to and from its site label |
|
|
279
|
+
| `blake3Hex`, `createBlake3` | BLAKE3, one-shot and incremental |
|
|
280
|
+
| `parseManifest`, `parseCapability` | Decodes a v1 bundle manifest, checking its hash when given one; decodes a `dcap1:` token without checking its signature |
|
|
281
|
+
| `DecdnError` | Every failure, with a stable `code` ([Errors](#errors)) |
|
|
282
|
+
| `readLoopbackToken`, `defaultDataDir` | From `@decdn/client/node`: the loopback gateway's token and data dir (`~/.decdn/gateway`) |
|
|
283
|
+
|
|
284
|
+
Namespaces, sequence numbers, caps and expiries are `bigint`.
|
|
285
|
+
|
|
286
|
+
## Development
|
|
287
|
+
|
|
288
|
+
```sh
|
|
289
|
+
pnpm install
|
|
290
|
+
pnpm lint # eslint, prettier, tsc
|
|
291
|
+
pnpm test # unit tests and the contract suite against the fake gateway
|
|
292
|
+
pnpm test:browser # Chromium smoke run (pnpm exec playwright install chromium-headless-shell)
|
|
293
|
+
pnpm build # dist/: ESM, CJS and types
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
`test/fake-gateway.ts` reproduces the gateway's HTTP contract. The contract
|
|
297
|
+
suite in `test/contract` runs against it by default, and against a real
|
|
298
|
+
gateway in decdn's CI
|
|
299
|
+
([`gateway_sdk.rs`](https://github.com/decdn/decdn/blob/main/crates/e2e/tests/gateway_sdk.rs)),
|
|
300
|
+
so the two cannot drift apart. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
301
|
+
|
|
302
|
+
Releases are cut from signed `v*` tags ([RELEASING.md](RELEASING.md)); the
|
|
303
|
+
keys that sign them are in [`KEYS`](KEYS) (`gpg --import KEYS`, then
|
|
304
|
+
`git verify-tag vX.Y.Z`).
|
|
305
|
+
|
|
306
|
+
## License
|
|
307
|
+
|
|
308
|
+
Dual-licensed under either [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE),
|
|
309
|
+
at your option.
|