braid-blob 0.0.67 → 0.0.68

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.
Files changed (2) hide show
  1. package/README.md +82 -63
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -178,97 +178,95 @@ var braid_blob = require('braid-blob')
178
178
  braid_blob.db_folder = './braid-blobs' // Default: ./braid-blobs
179
179
  ```
180
180
 
181
- ### Serve blobs to HTTP Requests (GET, PUT, and DELETE)
181
+ ### Examples
182
182
 
183
- Your app becomes a blob server with:
183
+ #### Get a blob
184
184
 
185
185
  ```javascript
186
- braid_blob.serve(req, res, params)
186
+ // Get a local blob:
187
+ var {body, version, content_type} = await braid_blob.get('foo')
188
+
189
+ // Get a remote blob:
190
+ var {body, version, content_type} = await braid_blob.get(new URL('https://foo.bar/baz'))
191
+
192
+ // Get a specific version of a remote blob:
193
+ var {body} = await braid_blob.get(
194
+ new URL('https://foo.bar/baz'),
195
+ {version: ['5zb2sjdstmk-1768093765048']}
196
+ )
197
+
198
+ // To subscribe to a remote blob, without storing updates locally:
199
+ await braid_blob.get(new URL('https://foo.bar/baz'), {
200
+ subscribe: (update) => {
201
+ console.log('Got update:', update.version, update.content_type)
202
+ // update.body contains the new blob data
203
+ }
204
+ })
205
+
206
+ // To mirror a remote blob to local storage (bidirectional sync):
207
+ var ac = new AbortController()
208
+ braid_blob.sync('local-key', new URL('https://foo.bar/baz'), {signal: ac.signal})
209
+ // Later, stop syncing:
210
+ ac.abort()
187
211
  ```
188
212
 
189
- This will synchronize the client issuing the given request and response with its blob on disk.
213
+ Note: `.get()` with `subscribe` receives updates but does not store them locally. `.sync()` performs two subscriptions (local↔remote) plus auto-forwards updates in both directions.
190
214
 
191
- Parameters:
192
- - `req` - HTTP request object
193
- - `res` - HTTP response object
194
- - `params` - Optional configuration object
195
- - `key` - The blob on disk to sync with (default: `req.url`)
196
-
197
- ### Sync a remotely served blob to disk
198
-
199
- Your app becomes a blob client with:
215
+ #### Put a blob
200
216
 
201
217
  ```javascript
202
- braid_blob.sync(key, url, params)
218
+ // Write to a local blob:
219
+ await braid_blob.put('foo', Buffer.from('hello'), {content_type: 'text/plain'})
220
+
221
+ // Write to a remote blob:
222
+ await braid_blob.put(
223
+ new URL('https://foo.bar/baz'),
224
+ Buffer.from('hello'),
225
+ {content_type: 'text/plain'}
226
+ )
203
227
  ```
204
228
 
205
- Synchronizes a remote URL to its blob on disk.
206
-
207
- Parameters:
208
- - `key` - The blob on disk (string)
209
- - `url` - Remote URL (URL object)
210
- - `params` - Optional configuration object
211
- - `signal` - AbortSignal for cancellation (use to stop sync)
212
- - `content_type` - Content type for requests
213
-
214
- ### Read, Write or Delete a blob
215
-
216
- #### Read a local or remote blob
229
+ #### Delete a blob
217
230
 
218
231
  ```javascript
219
- braid_blob.get(key, params)
232
+ // Delete a local blob:
233
+ await braid_blob.delete('foo')
234
+
235
+ // Delete a remote blob:
236
+ await braid_blob.delete(new URL('https://foo.bar/baz'))
220
237
  ```
221
238
 
222
- Retrieves a blob from local storage or a remote URL.
239
+ ### API Reference
223
240
 
224
- Examples:
225
- ```javascript
226
- // Get the current contents of a local blob:
227
- braid_blob.get('foo').body
241
+ #### braid_blob.get(key, params)
228
242
 
229
- // Get the contents of a remote blob:
230
- braid_blob.get(new URL('https://foo.bar/baz')).body
231
-
232
- // Get an old version of a remote blob:
233
- braid_blob.get(
234
- new URL('https://foo.bar/baz'),
235
- {version: ["5zb2sjdstmk-1768093765048"]}
236
- ).body
237
- ```
243
+ Retrieves a blob from local storage or a remote URL.
238
244
 
239
245
  Parameters:
240
246
  - `key` - The local blob (if string) or remote URL (if [URL object](https://nodejs.org/api/url.html#class-url)) to read from
241
247
  - `params` - Optional configuration object
242
- - `version` - Version ID to check existence (use with `head: true`)
243
- - `parent` - Version ID; when subscribing, only receive updates newer than this
244
- - `subscribe` - Callback function for real-time updates
245
- - `head` - If true, returns only metadata (version, content_type) without body
246
- - `content_type` - Content type for the request
247
- - `signal` - AbortSignal for cancellation
248
+ - `version` - Retrieve a specific version instead of the latest (e.g., `['abc-123']`)
249
+ - `parents` - When subscribing, only receive updates newer than this version (e.g., `['abc-123']`)
250
+ - `subscribe` - Callback `(update) => {}` called with each update; `update` has `{body, version, content_type}`
251
+ - `head` - If `true`, returns only metadata (`{version, content_type}`) without the body—useful for checking if a blob exists or getting its current version
252
+ - `content_type` - Expected content type (sent as Accept header for remote URLs)
253
+ - `signal` - AbortSignal to cancel the request or stop a subscription
248
254
 
249
- Returns: `{version, body, content_type}` object, or `null` if not found.
255
+ Returns: `{version, body, content_type}` object, or `null` if the blob doesn't exist. When subscribing to a remote URL, returns the fetch response object; updates are delivered via the callback.
250
256
 
251
- #### Write a local or remote blob
257
+ #### braid_blob.put(key, body, params)
252
258
 
253
- ```javascript
254
- braid_blob.put(key, body, params)
255
- ```
256
-
257
- Writes a blob to local storage or a remote URL. Any other peers synchronizing with this blob (via `.serve()`, `.sync()`, or `.get(.., {subscribe: ..}`) will be updated.
259
+ Writes a blob to local storage or a remote URL. Any other peers synchronizing with this blob (via `.serve()`, `.sync()`, or `.get(.., {subscribe: ..})`) will be updated.
258
260
 
259
261
  Parameters:
260
262
  - `key` - The local blob (if string) or remote URL (if [URL object](https://nodejs.org/api/url.html#class-url)) to write to
261
- - `body` - Buffer or data to store
263
+ - `body` - The data to store (Buffer, ArrayBuffer, or Uint8Array)
262
264
  - `params` - Optional configuration object
263
- - `version` - Version identifier
264
- - `content_type` - Content type of the blob
265
- - `signal` - AbortSignal for cancellation
265
+ - `version` - Specify a version ID for this write (e.g., `['my-version-1']`); if omitted, one is generated automatically
266
+ - `content_type` - MIME type of the blob (e.g., `'image/png'`, `'application/json'`)
267
+ - `signal` - AbortSignal to cancel the request
266
268
 
267
- #### Delete a local or remote blob
268
-
269
- ```javascript
270
- braid_blob.delete(key, params)
271
- ```
269
+ #### braid_blob.delete(key, params)
272
270
 
273
271
  Deletes a blob from local storage or a remote URL.
274
272
 
@@ -277,6 +275,27 @@ Parameters:
277
275
  - `params` - Optional configuration object
278
276
  - `signal` - AbortSignal for cancellation
279
277
 
278
+ #### braid_blob.sync(key, url, params)
279
+
280
+ Synchronizes a remote URL bidirectionally with a local blob on disk. This performs two subscriptions (one to the remote, one to the local blob) and auto-forwards updates in both directions.
281
+
282
+ Parameters:
283
+ - `key` - The local blob on disk (string)
284
+ - `url` - Remote URL (URL object)
285
+ - `params` - Optional configuration object
286
+ - `signal` - AbortSignal for cancellation (use to stop sync)
287
+ - `content_type` - Content type for requests
288
+
289
+ #### braid_blob.serve(req, res, params)
290
+
291
+ Serves blob requests over HTTP. Synchronizes the client issuing the given request with its blob on disk.
292
+
293
+ Parameters:
294
+ - `req` - HTTP request object
295
+ - `res` - HTTP response object
296
+ - `params` - Optional configuration object
297
+ - `key` - The blob on disk to sync with (default: the path from `req.url`)
298
+
280
299
  ## Browser Client API
281
300
 
282
301
  A simple browser client is included for subscribing to blob updates.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.67",
3
+ "version": "0.0.68",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",