@estiva-app/protocol 0.16.0 → 0.18.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/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/media.d.ts +145 -0
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +151 -0
- package/dist/media.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +11 -1
- package/src/media.ts +248 -0
package/dist/index.d.ts
CHANGED
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
/** Bumped by hand with the version in package.json — `test/version.test.ts`
|
|
47
47
|
* pins the two together. It exists so "the upgrade reached the app" can be
|
|
48
48
|
* checked by grepping a built bundle rather than by trusting a lockfile. */
|
|
49
|
-
export declare const PROTOCOL_VERSION = "0.
|
|
49
|
+
export declare const PROTOCOL_VERSION = "0.18.0";
|
|
50
50
|
export { type NostrTag, type UnsignedEvent, type SignedEvent, type Profile, type ThreadRef, type ChannelVisibility, type ChannelKind, type MemberRole, type Label, type ResolutionAction, KIND, MAX_EMOJI_CHARS, MAX_MESSAGE_BYTES, MAX_MENTIONS, MAX_RATIONALE_BYTES, RELAY_AUTH_TOLERANCE_SECS, ASSERTION_SUBTYPE, computeEventId, toNostrSeconds, canonicalChannelName, threadTags, addr, relayAuthUrl, buildProfile, parseProfile, buildCreateChannel, buildDeleteChannel, buildEditChannelMetadata, buildAddMember, buildReaction, buildDeletion, buildEdit, buildMessage, buildResolution, buildFile, buildComponent, buildHighlight, buildUnsignedRelayAuthEvent, } from './events.js';
|
|
51
51
|
export { type AddressPointer, type EventPointer, encodeNevent, decodeNevent, convertBits, bech32Encode, bech32Decode, encodeNaddr, decodeNaddr, pointerToAddress, addressToPointer, addrToNaddr, naddrToAddr, referenceToPointer, NADDR_RE, findNaddrs, stripNaddrs, encodeNpub, decodeNpub, NOSTR_URI_RE, findNostrUris, } from './nip19.js';
|
|
52
52
|
export { type AuthEventArgs, TIMESTAMP_TOLERANCE_SECS, normalizeUrl, buildUnsignedAuthEvent, base64, authorizationHeaderFor, authorizationHeader, } from './nip98.js';
|
|
@@ -59,5 +59,5 @@ export { type Block, type BlockDocument, type KnownBlockType, KNOWN_BLOCK_TYPES,
|
|
|
59
59
|
export { type RenderInline, type RenderBlock, type RenderFormat, type ContentFormat, CONTENT_FORMAT_TAG, BLOCK_DOCUMENT_FORMAT, contentFormatOf, toRenderTree, renderTreeText, standaloneReference, referencesIn, } from './render.js';
|
|
60
60
|
export { markerTextToBlockDocument, blockDocumentToMarkerText, } from './bridge-content.js';
|
|
61
61
|
export { type BlockAnchor, BLOCK_ANCHOR_TAG, blockAnchorOf, resolveBlockAnchor, } from './anchors.js';
|
|
62
|
-
export { type BlossomVerb, type Imeta, BLOSSOM_AUTH_TTL_SECS, buildBlossomAuth, blossomAuthHeader, imetaTag, imetaOf, } from './media.js';
|
|
62
|
+
export { type BlossomVerb, type Imeta, BLOSSOM_AUTH_TTL_SECS, buildBlossomAuth, blossomAuthHeader, imetaTag, imetaOf, type BlobFetchLike, type BlobResponseLike, type FetchedBlob, sha256FromMediaUrl, fetchBlob, sha256Of, uploadBlob, imetaFor, type UploadedBlob, type BlobUploadFetchLike, } from './media.js';
|
|
63
63
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;4EAE4E;AAC5E,eAAO,MAAM,gBAAgB,WAAW,CAAA;AAExC,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,OAAO,EACZ,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,UAAU,EAC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EACpB,KAAK,gBAAgB,EACrB,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,cAAc,EACd,UAAU,EACV,eAAe,GAChB,MAAM,WAAW,CAAA;AAElB,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,0BAA0B,GAC3B,MAAM,oBAAoB,CAAA;AAE3B,OAAO,EACL,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,qBAAqB,EACrB,oBAAoB,EACpB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,KAAK,KAAK,EACV,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,UAAU,EACV,qBAAqB,EACrB,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,YAAY,GACb,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,kBAAkB,EAClB,qBAAqB,EACrB,eAAe,EACf,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,YAAY,GACb,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EACL,KAAK,WAAW,EAChB,gBAAgB,EAChB,aAAa,EACb,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,QAAQ,EACR,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;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,EACL,KAAK,WAAW,EAChB,gBAAgB,EAChB,aAAa,EACb,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,QAAQ,EACR,OAAO,EACP,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,kBAAkB,EAClB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,KAAK,YAAY,EACjB,KAAK,mBAAmB,GACzB,MAAM,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
/** Bumped by hand with the version in package.json — `test/version.test.ts`
|
|
47
47
|
* pins the two together. It exists so "the upgrade reached the app" can be
|
|
48
48
|
* checked by grepping a built bundle rather than by trusting a lockfile. */
|
|
49
|
-
export const PROTOCOL_VERSION = '0.
|
|
49
|
+
export const PROTOCOL_VERSION = '0.18.0';
|
|
50
50
|
export {
|
|
51
51
|
// constants
|
|
52
52
|
KIND, MAX_EMOJI_CHARS, MAX_MESSAGE_BYTES, MAX_MENTIONS, MAX_RATIONALE_BYTES, RELAY_AUTH_TOLERANCE_SECS, ASSERTION_SUBTYPE,
|
|
@@ -65,5 +65,5 @@ export { KNOWN_BLOCK_TYPES, BlockDocumentError, validateBlockDocument, parseBloc
|
|
|
65
65
|
export { CONTENT_FORMAT_TAG, BLOCK_DOCUMENT_FORMAT, contentFormatOf, toRenderTree, renderTreeText, standaloneReference, referencesIn, } from './render.js';
|
|
66
66
|
export { markerTextToBlockDocument, blockDocumentToMarkerText, } from './bridge-content.js';
|
|
67
67
|
export { BLOCK_ANCHOR_TAG, blockAnchorOf, resolveBlockAnchor, } from './anchors.js';
|
|
68
|
-
export { BLOSSOM_AUTH_TTL_SECS, buildBlossomAuth, blossomAuthHeader, imetaTag, imetaOf, } from './media.js';
|
|
68
|
+
export { BLOSSOM_AUTH_TTL_SECS, buildBlossomAuth, blossomAuthHeader, imetaTag, imetaOf, sha256FromMediaUrl, fetchBlob, sha256Of, uploadBlob, imetaFor, } from './media.js';
|
|
69
69
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;4EAE4E;AAC5E,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAQ,CAAA;AAExC,OAAO;AAYL,YAAY;AACZ,IAAI,EACJ,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,yBAAyB,EACzB,iBAAiB;AACjB,8CAA8C;AAC9C,cAAc,EACd,cAAc,EACd,oBAAoB,EACpB,UAAU,EACV,IAAI,EACJ,YAAY;AACZ,WAAW;AACX,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,wBAAwB,EACxB,cAAc,EACd,aAAa,EACb,aAAa,EACb,SAAS,EACT,YAAY,EACZ,eAAe,EACf,SAAS,EACT,cAAc,EACd,cAAc,EACd,2BAA2B,GAC5B,MAAM,aAAa,CAAA;AAEpB,OAAO,EAGL,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,WAAW,EACX,WAAW,EACX,kBAAkB,EAClB,QAAQ,EACR,UAAU,EACV,WAAW,EACX,UAAU,EACV,UAAU,EACV,YAAY,EACZ,aAAa,GACd,MAAM,YAAY,CAAA;AAEnB,OAAO,EAEL,wBAAwB,EACxB,YAAY,EACZ,sBAAsB,EACtB,MAAM,EACN,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,YAAY,CAAA;AAEnB,OAAO,EAGL,mBAAmB,EACnB,SAAS,EACT,eAAe,GAChB,M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ACtB,UAAU,EACV,qBAAqB,EACrB,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,YAAY,GACb,MAAM,aAAa,CAAA;AAEpB,OAAO,EAKL,kBAAkB,EAClB,qBAAqB,EACrB,eAAe,EACf,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,YAAY,GACb,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EAEL,gBAAgB,EAChB,aAAa,EACb,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EAGL,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,QAAQ,EACR,OAAO,EAIP,kBAAkB,EAClB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,QAAQ,GAGT,MAAM,YAAY,CAAA"}
|
package/dist/media.d.ts
CHANGED
|
@@ -108,4 +108,149 @@ export declare function imetaTag(meta: Imeta): NostrTag;
|
|
|
108
108
|
export declare function imetaOf(event: {
|
|
109
109
|
tags: NostrTag[];
|
|
110
110
|
}): Imeta[];
|
|
111
|
+
/**
|
|
112
|
+
* The sha256 a media path names.
|
|
113
|
+
*
|
|
114
|
+
* `/media/<sha256>.<ext>` and `/media/<sha256>.thumb.jpg` both answer the same
|
|
115
|
+
* hash — the thumbnail is a derivative of its parent, and an authorization is
|
|
116
|
+
* issued against the parent. Returns undefined for anything that is not
|
|
117
|
+
* media-shaped, so a caller cannot accidentally sign for a path it misread.
|
|
118
|
+
*/
|
|
119
|
+
export declare function sha256FromMediaUrl(url: string): string | undefined;
|
|
120
|
+
/** A response carrying bytes. Narrower than `Response`, so no DOM lib is needed. */
|
|
121
|
+
export interface BlobResponseLike {
|
|
122
|
+
status: number;
|
|
123
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
124
|
+
text(): Promise<string>;
|
|
125
|
+
headers: {
|
|
126
|
+
get(name: string): string | null;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A transport that can return bytes.
|
|
131
|
+
*
|
|
132
|
+
* Separate from {@link FetchLike}, which `Relay` uses: that one sends a string
|
|
133
|
+
* body and reads a string back, which is right for `POST /query` and cannot
|
|
134
|
+
* express a binary GET. Widening it would change `Relay`'s contract for every
|
|
135
|
+
* caller to serve this one.
|
|
136
|
+
*/
|
|
137
|
+
export type BlobFetchLike = (url: string, init: {
|
|
138
|
+
method: string;
|
|
139
|
+
headers: Record<string, string>;
|
|
140
|
+
}) => Promise<BlobResponseLike>;
|
|
141
|
+
/** What came back, and what it is. */
|
|
142
|
+
export interface FetchedBlob {
|
|
143
|
+
bytes: ArrayBuffer;
|
|
144
|
+
/** The relay's stored MIME, which is authoritative over anything a tag said. */
|
|
145
|
+
contentType: string;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Fetch a blob the relay is holding — CON-12's read half.
|
|
149
|
+
*
|
|
150
|
+
* **An `<img src>` cannot do this**, which is the whole reason this exists.
|
|
151
|
+
* Media GET is unconditionally authenticated on the deployed relay: the
|
|
152
|
+
* `BUZZ_REQUIRE_MEDIA_GET_AUTH` flag reads like an opt-in and is inert, and a
|
|
153
|
+
* GET with no header answers 401 rather than 404 (probed 2026-09-08). So every
|
|
154
|
+
* reader — Peek showing its own attachment, Ship showing Peek's — has to sign a
|
|
155
|
+
* `get` authorization, fetch, and turn the bytes into something renderable.
|
|
156
|
+
*
|
|
157
|
+
* Shared because both apps need exactly this and neither should re-derive it:
|
|
158
|
+
* the hash the authorization must name is inside the path, the verb has to be
|
|
159
|
+
* `get` rather than `upload`, and getting either wrong fails as a 401 that
|
|
160
|
+
* looks like a session problem.
|
|
161
|
+
*
|
|
162
|
+
* Returns the bytes rather than an object URL: `URL.createObjectURL` is a DOM
|
|
163
|
+
* API and its lifetime belongs to whoever will revoke it.
|
|
164
|
+
*/
|
|
165
|
+
export declare function fetchBlob(args: {
|
|
166
|
+
/** The relay origin, used when `url` is a bare `/media/…` path. */
|
|
167
|
+
relayUrl: string;
|
|
168
|
+
/** `/media/<sha256>.<ext>`, or the absolute form of the same. */
|
|
169
|
+
url: string;
|
|
170
|
+
sign: (unsigned: UnsignedEvent) => Promise<SignedEvent>;
|
|
171
|
+
pubkey: string;
|
|
172
|
+
transport?: BlobFetchLike;
|
|
173
|
+
}): Promise<FetchedBlob>;
|
|
174
|
+
/**
|
|
175
|
+
* A blob's identity: its sha256, lowercase hex.
|
|
176
|
+
*
|
|
177
|
+
* `@noble/hashes` rather than `crypto.subtle`, which is async, absent from
|
|
178
|
+
* older Node without a flag, and unavailable on an insecure origin. This
|
|
179
|
+
* package already hashes every event id with it.
|
|
180
|
+
*/
|
|
181
|
+
export declare function sha256Of(bytes: ArrayBuffer | Uint8Array): string;
|
|
182
|
+
/** What the relay hands back once it holds the bytes — BUD-02 descriptor. */
|
|
183
|
+
export interface UploadedBlob {
|
|
184
|
+
/** Where the bytes are. Already rewritten for the tenant, so it goes straight into `imeta`. */
|
|
185
|
+
url: string;
|
|
186
|
+
sha256: string;
|
|
187
|
+
size: number;
|
|
188
|
+
/** MIME **as the relay stored it**. `imeta`'s `m` must equal this exactly. */
|
|
189
|
+
type: string;
|
|
190
|
+
/** `<width>x<height>`, computed by the relay rather than by the client. */
|
|
191
|
+
dim?: string;
|
|
192
|
+
/** The relay's generated thumbnail, when it made one. */
|
|
193
|
+
thumb?: string;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* A transport that can send bytes.
|
|
197
|
+
*
|
|
198
|
+
* A third shape, and for the third reason: {@link FetchLike} sends a string
|
|
199
|
+
* body, {@link BlobFetchLike} sends none and reads bytes, this one sends bytes
|
|
200
|
+
* and reads a string. One type that did all three would be loose enough to let
|
|
201
|
+
* any of them be called wrongly.
|
|
202
|
+
*/
|
|
203
|
+
export type BlobUploadFetchLike = (url: string, init: {
|
|
204
|
+
method: string;
|
|
205
|
+
headers: Record<string, string>;
|
|
206
|
+
body: ArrayBuffer;
|
|
207
|
+
}) => Promise<{
|
|
208
|
+
status: number;
|
|
209
|
+
text(): Promise<string>;
|
|
210
|
+
}>;
|
|
211
|
+
/**
|
|
212
|
+
* Put a file on the relay — CON-12's write half, BUD-01/BUD-11.
|
|
213
|
+
*
|
|
214
|
+
* **The upload must land before the message that names it.** Ingest runs
|
|
215
|
+
* `verify_imeta_blobs` and refuses a message whose blob it does not already
|
|
216
|
+
* hold, so this is not an optimisation to reorder — it is the only order that
|
|
217
|
+
* works. A dangling attachment reference cannot be published at all, which is
|
|
218
|
+
* a constraint worth being glad about.
|
|
219
|
+
*
|
|
220
|
+
* The hash is computed here rather than read off the response, because it is
|
|
221
|
+
* what the authorization has to name: BUD-11 requires the `x` tag to match, so
|
|
222
|
+
* the client must know the digest *before* it may ask permission to send the
|
|
223
|
+
* bytes. `x-sha-256` carries it again on the PUT, where the relay checks the
|
|
224
|
+
* bytes against it rather than trusting either side's arithmetic.
|
|
225
|
+
*
|
|
226
|
+
* Takes bytes rather than a `File`: `File` is a DOM type, this package builds
|
|
227
|
+
* without `lib.dom`, and a caller that has one passes `await file.arrayBuffer()`.
|
|
228
|
+
*
|
|
229
|
+
* Throws with the relay's own words. What that means is the caller's to decide.
|
|
230
|
+
*/
|
|
231
|
+
export declare function uploadBlob(args: {
|
|
232
|
+
/** The relay origin. `/upload` is appended. */
|
|
233
|
+
relayUrl: string;
|
|
234
|
+
bytes: ArrayBuffer;
|
|
235
|
+
/** The MIME to send. The relay stores its own answer, which wins — see {@link UploadedBlob.type}. */
|
|
236
|
+
contentType?: string;
|
|
237
|
+
/** Display name. Goes in the authorization's human-readable reason, nowhere else. */
|
|
238
|
+
filename?: string;
|
|
239
|
+
sign: (unsigned: UnsignedEvent) => Promise<SignedEvent>;
|
|
240
|
+
pubkey: string;
|
|
241
|
+
transport?: BlobUploadFetchLike;
|
|
242
|
+
}): Promise<UploadedBlob>;
|
|
243
|
+
/**
|
|
244
|
+
* An uploaded blob as the `imeta` that references it.
|
|
245
|
+
*
|
|
246
|
+
* Shared because the mapping has one trap in it: `m` and `x` must be the
|
|
247
|
+
* **relay's** answers, not the client's. `validate_imeta_tags` compares them
|
|
248
|
+
* against what it stored, and a browser that re-encoded a file on the way in —
|
|
249
|
+
* or simply guessed a MIME from an extension — publishes a message the relay
|
|
250
|
+
* refuses, with nothing to say which field was wrong.
|
|
251
|
+
*/
|
|
252
|
+
export declare function imetaFor(blob: UploadedBlob, extra?: {
|
|
253
|
+
alt?: string;
|
|
254
|
+
filename?: string;
|
|
255
|
+
}): Imeta;
|
|
111
256
|
//# sourceMappingURL=media.d.ts.map
|
package/dist/media.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAMvE,gEAAgE;AAChE,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,CAAA;AAE9D,8EAA8E;AAC9E,eAAO,MAAM,qBAAqB,MAAM,CAAA;AAExC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE;IACJ,IAAI,EAAE,WAAW,CAAA;IACjB,+BAA+B;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,8DAA8D;I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AAM,EAAE,MAAM,CAAC;IAAC,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;CAAE,CAAC,CAAA;AAEzD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE;IACrC,+CAA+C;IAC/C,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,WAAW,CAAA;IAClB,qGAAqG;IACrG,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,qFAAqF;IACrF,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,OAAO,CAAC,WAAW,CAAC,CAAA;IACvD,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,mBAAmB,CAAA;CAChC,GAAG,OAAO,CAAC,YAAY,CAAC,CA6CxB;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,YAAY,EAAE,KAAK,CAAC,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,KAAK,CAW/F"}
|
package/dist/media.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { KIND, toNostrSeconds } from './events.js';
|
|
2
2
|
import { authorizationHeaderFor } from './nip98.js';
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
4
|
+
import { bytesToHex } from '@noble/hashes/utils';
|
|
3
5
|
/** How long an authorization stays valid unless the caller says otherwise. */
|
|
4
6
|
export const BLOSSOM_AUTH_TTL_SECS = 300;
|
|
5
7
|
/**
|
|
@@ -127,4 +129,153 @@ export function imetaOf(event) {
|
|
|
127
129
|
}
|
|
128
130
|
return out;
|
|
129
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* The sha256 a media path names.
|
|
134
|
+
*
|
|
135
|
+
* `/media/<sha256>.<ext>` and `/media/<sha256>.thumb.jpg` both answer the same
|
|
136
|
+
* hash — the thumbnail is a derivative of its parent, and an authorization is
|
|
137
|
+
* issued against the parent. Returns undefined for anything that is not
|
|
138
|
+
* media-shaped, so a caller cannot accidentally sign for a path it misread.
|
|
139
|
+
*/
|
|
140
|
+
export function sha256FromMediaUrl(url) {
|
|
141
|
+
const afterMedia = url.split('/media/')[1];
|
|
142
|
+
if (afterMedia === undefined)
|
|
143
|
+
return undefined;
|
|
144
|
+
const hash = afterMedia.split('.')[0];
|
|
145
|
+
return /^[0-9a-f]{64}$/.test(hash) ? hash : undefined;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Fetch a blob the relay is holding — CON-12's read half.
|
|
149
|
+
*
|
|
150
|
+
* **An `<img src>` cannot do this**, which is the whole reason this exists.
|
|
151
|
+
* Media GET is unconditionally authenticated on the deployed relay: the
|
|
152
|
+
* `BUZZ_REQUIRE_MEDIA_GET_AUTH` flag reads like an opt-in and is inert, and a
|
|
153
|
+
* GET with no header answers 401 rather than 404 (probed 2026-09-08). So every
|
|
154
|
+
* reader — Peek showing its own attachment, Ship showing Peek's — has to sign a
|
|
155
|
+
* `get` authorization, fetch, and turn the bytes into something renderable.
|
|
156
|
+
*
|
|
157
|
+
* Shared because both apps need exactly this and neither should re-derive it:
|
|
158
|
+
* the hash the authorization must name is inside the path, the verb has to be
|
|
159
|
+
* `get` rather than `upload`, and getting either wrong fails as a 401 that
|
|
160
|
+
* looks like a session problem.
|
|
161
|
+
*
|
|
162
|
+
* Returns the bytes rather than an object URL: `URL.createObjectURL` is a DOM
|
|
163
|
+
* API and its lifetime belongs to whoever will revoke it.
|
|
164
|
+
*/
|
|
165
|
+
export async function fetchBlob(args) {
|
|
166
|
+
const sha256 = sha256FromMediaUrl(args.url);
|
|
167
|
+
if (!sha256)
|
|
168
|
+
throw new Error(`not a media url: ${args.url}`);
|
|
169
|
+
const auth = await args.sign(buildBlossomAuth(args.pubkey, Date.now(), { verb: 'get', sha256, reason: 'Read attachment' }));
|
|
170
|
+
const absolute = args.url.startsWith('http')
|
|
171
|
+
? args.url
|
|
172
|
+
: `${args.relayUrl.replace(/\/+$/, '')}${args.url.startsWith('/') ? '' : '/'}${args.url}`;
|
|
173
|
+
const send = args.transport ?? fetch;
|
|
174
|
+
const response = await send(absolute, {
|
|
175
|
+
method: 'GET',
|
|
176
|
+
headers: { authorization: blossomAuthHeader(auth) },
|
|
177
|
+
});
|
|
178
|
+
if (response.status < 200 || response.status >= 300) {
|
|
179
|
+
const detail = await response.text().catch(() => '');
|
|
180
|
+
throw new Error(`blob read failed: ${response.status} ${detail.slice(0, 200)}`);
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
bytes: await response.arrayBuffer(),
|
|
184
|
+
contentType: response.headers.get('content-type') ?? 'application/octet-stream',
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* A blob's identity: its sha256, lowercase hex.
|
|
189
|
+
*
|
|
190
|
+
* `@noble/hashes` rather than `crypto.subtle`, which is async, absent from
|
|
191
|
+
* older Node without a flag, and unavailable on an insecure origin. This
|
|
192
|
+
* package already hashes every event id with it.
|
|
193
|
+
*/
|
|
194
|
+
export function sha256Of(bytes) {
|
|
195
|
+
return bytesToHex(sha256(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Put a file on the relay — CON-12's write half, BUD-01/BUD-11.
|
|
199
|
+
*
|
|
200
|
+
* **The upload must land before the message that names it.** Ingest runs
|
|
201
|
+
* `verify_imeta_blobs` and refuses a message whose blob it does not already
|
|
202
|
+
* hold, so this is not an optimisation to reorder — it is the only order that
|
|
203
|
+
* works. A dangling attachment reference cannot be published at all, which is
|
|
204
|
+
* a constraint worth being glad about.
|
|
205
|
+
*
|
|
206
|
+
* The hash is computed here rather than read off the response, because it is
|
|
207
|
+
* what the authorization has to name: BUD-11 requires the `x` tag to match, so
|
|
208
|
+
* the client must know the digest *before* it may ask permission to send the
|
|
209
|
+
* bytes. `x-sha-256` carries it again on the PUT, where the relay checks the
|
|
210
|
+
* bytes against it rather than trusting either side's arithmetic.
|
|
211
|
+
*
|
|
212
|
+
* Takes bytes rather than a `File`: `File` is a DOM type, this package builds
|
|
213
|
+
* without `lib.dom`, and a caller that has one passes `await file.arrayBuffer()`.
|
|
214
|
+
*
|
|
215
|
+
* Throws with the relay's own words. What that means is the caller's to decide.
|
|
216
|
+
*/
|
|
217
|
+
export async function uploadBlob(args) {
|
|
218
|
+
const digest = sha256Of(args.bytes);
|
|
219
|
+
const auth = await args.sign(buildBlossomAuth(args.pubkey, Date.now(), {
|
|
220
|
+
verb: 'upload',
|
|
221
|
+
sha256: digest,
|
|
222
|
+
reason: args.filename ? `Upload ${args.filename}` : 'Upload attachment',
|
|
223
|
+
}));
|
|
224
|
+
const send = args.transport ?? fetch;
|
|
225
|
+
const response = await send(`${args.relayUrl.replace(/\/+$/, '')}/upload`, {
|
|
226
|
+
method: 'PUT',
|
|
227
|
+
headers: {
|
|
228
|
+
authorization: blossomAuthHeader(auth),
|
|
229
|
+
// BUD-11 makes this mandatory on PUT /upload.
|
|
230
|
+
'x-sha-256': digest,
|
|
231
|
+
'content-type': args.contentType || 'application/octet-stream',
|
|
232
|
+
},
|
|
233
|
+
body: args.bytes,
|
|
234
|
+
});
|
|
235
|
+
const body = await response.text();
|
|
236
|
+
if (response.status < 200 || response.status >= 300) {
|
|
237
|
+
throw new Error(`upload failed: ${response.status} ${body.slice(0, 200)}`);
|
|
238
|
+
}
|
|
239
|
+
let descriptor;
|
|
240
|
+
try {
|
|
241
|
+
descriptor = JSON.parse(body);
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
throw new Error('upload returned no blob descriptor');
|
|
245
|
+
}
|
|
246
|
+
// `url` is the relay's own, already rewritten for the tenant — assembling a
|
|
247
|
+
// host here would produce something `validate_imeta_tags` rejects as
|
|
248
|
+
// non-local on any deployment whose tenant host is not the configured one.
|
|
249
|
+
if (!descriptor.url || !descriptor.sha256)
|
|
250
|
+
throw new Error('upload returned no blob descriptor');
|
|
251
|
+
return {
|
|
252
|
+
url: descriptor.url,
|
|
253
|
+
sha256: descriptor.sha256,
|
|
254
|
+
size: descriptor.size ?? args.bytes.byteLength,
|
|
255
|
+
type: descriptor.type ?? args.contentType ?? 'application/octet-stream',
|
|
256
|
+
dim: descriptor.dim,
|
|
257
|
+
thumb: descriptor.thumb,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* An uploaded blob as the `imeta` that references it.
|
|
262
|
+
*
|
|
263
|
+
* Shared because the mapping has one trap in it: `m` and `x` must be the
|
|
264
|
+
* **relay's** answers, not the client's. `validate_imeta_tags` compares them
|
|
265
|
+
* against what it stored, and a browser that re-encoded a file on the way in —
|
|
266
|
+
* or simply guessed a MIME from an extension — publishes a message the relay
|
|
267
|
+
* refuses, with nothing to say which field was wrong.
|
|
268
|
+
*/
|
|
269
|
+
export function imetaFor(blob, extra) {
|
|
270
|
+
return {
|
|
271
|
+
url: blob.url,
|
|
272
|
+
m: blob.type,
|
|
273
|
+
x: blob.sha256,
|
|
274
|
+
size: blob.size,
|
|
275
|
+
dim: blob.dim,
|
|
276
|
+
thumb: blob.thumb,
|
|
277
|
+
alt: extra?.alt,
|
|
278
|
+
filename: extra?.filename,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
130
281
|
//# sourceMappingURL=media.js.map
|
package/dist/media.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.js","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"media.js","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;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E,IAAI,CAAC,IAAI;QACZ,CAAC,EAAE,IAAI,CAAC,MAAM;QACd,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,KAAK,EAAE,IAAI,CAAC,KAAK;QACjB,GAAG,EAAE,KAAK,EAAE,GAAG;QACf,QAAQ,EAAE,KAAK,EAAE,QAAQ;KAC1B,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@estiva-app/protocol",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "The Estiva wire format: Buzz-shaped Nostr event builders, NIP-01 ids, NIP-19 naddr, NIP-98 HTTP auth, and the relay clients. Speaks the protocol; interprets nothing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/src/index.ts
CHANGED
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
/** Bumped by hand with the version in package.json — `test/version.test.ts`
|
|
48
48
|
* pins the two together. It exists so "the upgrade reached the app" can be
|
|
49
49
|
* checked by grepping a built bundle rather than by trusting a lockfile. */
|
|
50
|
-
export const PROTOCOL_VERSION = '0.
|
|
50
|
+
export const PROTOCOL_VERSION = '0.18.0'
|
|
51
51
|
|
|
52
52
|
export {
|
|
53
53
|
// types
|
|
@@ -235,4 +235,14 @@ export {
|
|
|
235
235
|
blossomAuthHeader,
|
|
236
236
|
imetaTag,
|
|
237
237
|
imetaOf,
|
|
238
|
+
type BlobFetchLike,
|
|
239
|
+
type BlobResponseLike,
|
|
240
|
+
type FetchedBlob,
|
|
241
|
+
sha256FromMediaUrl,
|
|
242
|
+
fetchBlob,
|
|
243
|
+
sha256Of,
|
|
244
|
+
uploadBlob,
|
|
245
|
+
imetaFor,
|
|
246
|
+
type UploadedBlob,
|
|
247
|
+
type BlobUploadFetchLike,
|
|
238
248
|
} from './media.js'
|
package/src/media.ts
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
import type { NostrTag, SignedEvent, UnsignedEvent } from './events.js'
|
|
16
16
|
import { KIND, toNostrSeconds } from './events.js'
|
|
17
17
|
import { authorizationHeaderFor } from './nip98.js'
|
|
18
|
+
import { sha256 } from '@noble/hashes/sha256'
|
|
19
|
+
import { bytesToHex } from '@noble/hashes/utils'
|
|
18
20
|
|
|
19
21
|
/** What a Blossom authorization permits. One verb per event. */
|
|
20
22
|
export type BlossomVerb = 'upload' | 'get' | 'list' | 'delete'
|
|
@@ -175,3 +177,249 @@ export function imetaOf(event: { tags: NostrTag[] }): Imeta[] {
|
|
|
175
177
|
}
|
|
176
178
|
return out
|
|
177
179
|
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The sha256 a media path names.
|
|
183
|
+
*
|
|
184
|
+
* `/media/<sha256>.<ext>` and `/media/<sha256>.thumb.jpg` both answer the same
|
|
185
|
+
* hash — the thumbnail is a derivative of its parent, and an authorization is
|
|
186
|
+
* issued against the parent. Returns undefined for anything that is not
|
|
187
|
+
* media-shaped, so a caller cannot accidentally sign for a path it misread.
|
|
188
|
+
*/
|
|
189
|
+
export function sha256FromMediaUrl(url: string): string | undefined {
|
|
190
|
+
const afterMedia = url.split('/media/')[1]
|
|
191
|
+
if (afterMedia === undefined) return undefined
|
|
192
|
+
const hash = afterMedia.split('.')[0]
|
|
193
|
+
return /^[0-9a-f]{64}$/.test(hash) ? hash : undefined
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** A response carrying bytes. Narrower than `Response`, so no DOM lib is needed. */
|
|
197
|
+
export interface BlobResponseLike {
|
|
198
|
+
status: number
|
|
199
|
+
arrayBuffer(): Promise<ArrayBuffer>
|
|
200
|
+
text(): Promise<string>
|
|
201
|
+
headers: { get(name: string): string | null }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* A transport that can return bytes.
|
|
206
|
+
*
|
|
207
|
+
* Separate from {@link FetchLike}, which `Relay` uses: that one sends a string
|
|
208
|
+
* body and reads a string back, which is right for `POST /query` and cannot
|
|
209
|
+
* express a binary GET. Widening it would change `Relay`'s contract for every
|
|
210
|
+
* caller to serve this one.
|
|
211
|
+
*/
|
|
212
|
+
export type BlobFetchLike = (
|
|
213
|
+
url: string,
|
|
214
|
+
init: { method: string; headers: Record<string, string> },
|
|
215
|
+
) => Promise<BlobResponseLike>
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The platform `fetch`, in the two shapes this file asks of it.
|
|
219
|
+
*
|
|
220
|
+
* An intersection rather than two declarations, because there is one global and
|
|
221
|
+
* TypeScript will not let a module name it twice. It really does accept both
|
|
222
|
+
* calls — a GET with no body and a PUT with bytes — so an overloaded type is
|
|
223
|
+
* the accurate description rather than a convenience.
|
|
224
|
+
*
|
|
225
|
+
* Read inside a function, so importing this module touches no global.
|
|
226
|
+
*/
|
|
227
|
+
declare const fetch: BlobFetchLike & BlobUploadFetchLike
|
|
228
|
+
|
|
229
|
+
/** What came back, and what it is. */
|
|
230
|
+
export interface FetchedBlob {
|
|
231
|
+
bytes: ArrayBuffer
|
|
232
|
+
/** The relay's stored MIME, which is authoritative over anything a tag said. */
|
|
233
|
+
contentType: string
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Fetch a blob the relay is holding — CON-12's read half.
|
|
238
|
+
*
|
|
239
|
+
* **An `<img src>` cannot do this**, which is the whole reason this exists.
|
|
240
|
+
* Media GET is unconditionally authenticated on the deployed relay: the
|
|
241
|
+
* `BUZZ_REQUIRE_MEDIA_GET_AUTH` flag reads like an opt-in and is inert, and a
|
|
242
|
+
* GET with no header answers 401 rather than 404 (probed 2026-09-08). So every
|
|
243
|
+
* reader — Peek showing its own attachment, Ship showing Peek's — has to sign a
|
|
244
|
+
* `get` authorization, fetch, and turn the bytes into something renderable.
|
|
245
|
+
*
|
|
246
|
+
* Shared because both apps need exactly this and neither should re-derive it:
|
|
247
|
+
* the hash the authorization must name is inside the path, the verb has to be
|
|
248
|
+
* `get` rather than `upload`, and getting either wrong fails as a 401 that
|
|
249
|
+
* looks like a session problem.
|
|
250
|
+
*
|
|
251
|
+
* Returns the bytes rather than an object URL: `URL.createObjectURL` is a DOM
|
|
252
|
+
* API and its lifetime belongs to whoever will revoke it.
|
|
253
|
+
*/
|
|
254
|
+
export async function fetchBlob(args: {
|
|
255
|
+
/** The relay origin, used when `url` is a bare `/media/…` path. */
|
|
256
|
+
relayUrl: string
|
|
257
|
+
/** `/media/<sha256>.<ext>`, or the absolute form of the same. */
|
|
258
|
+
url: string
|
|
259
|
+
sign: (unsigned: UnsignedEvent) => Promise<SignedEvent>
|
|
260
|
+
pubkey: string
|
|
261
|
+
transport?: BlobFetchLike
|
|
262
|
+
}): Promise<FetchedBlob> {
|
|
263
|
+
const sha256 = sha256FromMediaUrl(args.url)
|
|
264
|
+
if (!sha256) throw new Error(`not a media url: ${args.url}`)
|
|
265
|
+
|
|
266
|
+
const auth = await args.sign(
|
|
267
|
+
buildBlossomAuth(args.pubkey, Date.now(), { verb: 'get', sha256, reason: 'Read attachment' }),
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
const absolute = args.url.startsWith('http')
|
|
271
|
+
? args.url
|
|
272
|
+
: `${args.relayUrl.replace(/\/+$/, '')}${args.url.startsWith('/') ? '' : '/'}${args.url}`
|
|
273
|
+
|
|
274
|
+
const send = args.transport ?? fetch
|
|
275
|
+
const response = await send(absolute, {
|
|
276
|
+
method: 'GET',
|
|
277
|
+
headers: { authorization: blossomAuthHeader(auth) },
|
|
278
|
+
})
|
|
279
|
+
if (response.status < 200 || response.status >= 300) {
|
|
280
|
+
const detail = await response.text().catch(() => '')
|
|
281
|
+
throw new Error(`blob read failed: ${response.status} ${detail.slice(0, 200)}`)
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
bytes: await response.arrayBuffer(),
|
|
285
|
+
contentType: response.headers.get('content-type') ?? 'application/octet-stream',
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* A blob's identity: its sha256, lowercase hex.
|
|
291
|
+
*
|
|
292
|
+
* `@noble/hashes` rather than `crypto.subtle`, which is async, absent from
|
|
293
|
+
* older Node without a flag, and unavailable on an insecure origin. This
|
|
294
|
+
* package already hashes every event id with it.
|
|
295
|
+
*/
|
|
296
|
+
export function sha256Of(bytes: ArrayBuffer | Uint8Array): string {
|
|
297
|
+
return bytesToHex(sha256(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)))
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** What the relay hands back once it holds the bytes — BUD-02 descriptor. */
|
|
301
|
+
export interface UploadedBlob {
|
|
302
|
+
/** Where the bytes are. Already rewritten for the tenant, so it goes straight into `imeta`. */
|
|
303
|
+
url: string
|
|
304
|
+
sha256: string
|
|
305
|
+
size: number
|
|
306
|
+
/** MIME **as the relay stored it**. `imeta`'s `m` must equal this exactly. */
|
|
307
|
+
type: string
|
|
308
|
+
/** `<width>x<height>`, computed by the relay rather than by the client. */
|
|
309
|
+
dim?: string
|
|
310
|
+
/** The relay's generated thumbnail, when it made one. */
|
|
311
|
+
thumb?: string
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* A transport that can send bytes.
|
|
316
|
+
*
|
|
317
|
+
* A third shape, and for the third reason: {@link FetchLike} sends a string
|
|
318
|
+
* body, {@link BlobFetchLike} sends none and reads bytes, this one sends bytes
|
|
319
|
+
* and reads a string. One type that did all three would be loose enough to let
|
|
320
|
+
* any of them be called wrongly.
|
|
321
|
+
*/
|
|
322
|
+
export type BlobUploadFetchLike = (
|
|
323
|
+
url: string,
|
|
324
|
+
init: { method: string; headers: Record<string, string>; body: ArrayBuffer },
|
|
325
|
+
) => Promise<{ status: number; text(): Promise<string> }>
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Put a file on the relay — CON-12's write half, BUD-01/BUD-11.
|
|
329
|
+
*
|
|
330
|
+
* **The upload must land before the message that names it.** Ingest runs
|
|
331
|
+
* `verify_imeta_blobs` and refuses a message whose blob it does not already
|
|
332
|
+
* hold, so this is not an optimisation to reorder — it is the only order that
|
|
333
|
+
* works. A dangling attachment reference cannot be published at all, which is
|
|
334
|
+
* a constraint worth being glad about.
|
|
335
|
+
*
|
|
336
|
+
* The hash is computed here rather than read off the response, because it is
|
|
337
|
+
* what the authorization has to name: BUD-11 requires the `x` tag to match, so
|
|
338
|
+
* the client must know the digest *before* it may ask permission to send the
|
|
339
|
+
* bytes. `x-sha-256` carries it again on the PUT, where the relay checks the
|
|
340
|
+
* bytes against it rather than trusting either side's arithmetic.
|
|
341
|
+
*
|
|
342
|
+
* Takes bytes rather than a `File`: `File` is a DOM type, this package builds
|
|
343
|
+
* without `lib.dom`, and a caller that has one passes `await file.arrayBuffer()`.
|
|
344
|
+
*
|
|
345
|
+
* Throws with the relay's own words. What that means is the caller's to decide.
|
|
346
|
+
*/
|
|
347
|
+
export async function uploadBlob(args: {
|
|
348
|
+
/** The relay origin. `/upload` is appended. */
|
|
349
|
+
relayUrl: string
|
|
350
|
+
bytes: ArrayBuffer
|
|
351
|
+
/** The MIME to send. The relay stores its own answer, which wins — see {@link UploadedBlob.type}. */
|
|
352
|
+
contentType?: string
|
|
353
|
+
/** Display name. Goes in the authorization's human-readable reason, nowhere else. */
|
|
354
|
+
filename?: string
|
|
355
|
+
sign: (unsigned: UnsignedEvent) => Promise<SignedEvent>
|
|
356
|
+
pubkey: string
|
|
357
|
+
transport?: BlobUploadFetchLike
|
|
358
|
+
}): Promise<UploadedBlob> {
|
|
359
|
+
const digest = sha256Of(args.bytes)
|
|
360
|
+
const auth = await args.sign(
|
|
361
|
+
buildBlossomAuth(args.pubkey, Date.now(), {
|
|
362
|
+
verb: 'upload',
|
|
363
|
+
sha256: digest,
|
|
364
|
+
reason: args.filename ? `Upload ${args.filename}` : 'Upload attachment',
|
|
365
|
+
}),
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
const send = args.transport ?? fetch
|
|
369
|
+
const response = await send(`${args.relayUrl.replace(/\/+$/, '')}/upload`, {
|
|
370
|
+
method: 'PUT',
|
|
371
|
+
headers: {
|
|
372
|
+
authorization: blossomAuthHeader(auth),
|
|
373
|
+
// BUD-11 makes this mandatory on PUT /upload.
|
|
374
|
+
'x-sha-256': digest,
|
|
375
|
+
'content-type': args.contentType || 'application/octet-stream',
|
|
376
|
+
},
|
|
377
|
+
body: args.bytes,
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
const body = await response.text()
|
|
381
|
+
if (response.status < 200 || response.status >= 300) {
|
|
382
|
+
throw new Error(`upload failed: ${response.status} ${body.slice(0, 200)}`)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let descriptor: Partial<UploadedBlob>
|
|
386
|
+
try {
|
|
387
|
+
descriptor = JSON.parse(body) as Partial<UploadedBlob>
|
|
388
|
+
} catch {
|
|
389
|
+
throw new Error('upload returned no blob descriptor')
|
|
390
|
+
}
|
|
391
|
+
// `url` is the relay's own, already rewritten for the tenant — assembling a
|
|
392
|
+
// host here would produce something `validate_imeta_tags` rejects as
|
|
393
|
+
// non-local on any deployment whose tenant host is not the configured one.
|
|
394
|
+
if (!descriptor.url || !descriptor.sha256) throw new Error('upload returned no blob descriptor')
|
|
395
|
+
return {
|
|
396
|
+
url: descriptor.url,
|
|
397
|
+
sha256: descriptor.sha256,
|
|
398
|
+
size: descriptor.size ?? args.bytes.byteLength,
|
|
399
|
+
type: descriptor.type ?? args.contentType ?? 'application/octet-stream',
|
|
400
|
+
dim: descriptor.dim,
|
|
401
|
+
thumb: descriptor.thumb,
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* An uploaded blob as the `imeta` that references it.
|
|
407
|
+
*
|
|
408
|
+
* Shared because the mapping has one trap in it: `m` and `x` must be the
|
|
409
|
+
* **relay's** answers, not the client's. `validate_imeta_tags` compares them
|
|
410
|
+
* against what it stored, and a browser that re-encoded a file on the way in —
|
|
411
|
+
* or simply guessed a MIME from an extension — publishes a message the relay
|
|
412
|
+
* refuses, with nothing to say which field was wrong.
|
|
413
|
+
*/
|
|
414
|
+
export function imetaFor(blob: UploadedBlob, extra?: { alt?: string; filename?: string }): Imeta {
|
|
415
|
+
return {
|
|
416
|
+
url: blob.url,
|
|
417
|
+
m: blob.type,
|
|
418
|
+
x: blob.sha256,
|
|
419
|
+
size: blob.size,
|
|
420
|
+
dim: blob.dim,
|
|
421
|
+
thumb: blob.thumb,
|
|
422
|
+
alt: extra?.alt,
|
|
423
|
+
filename: extra?.filename,
|
|
424
|
+
}
|
|
425
|
+
}
|