@dotrino/content 0.6.2 → 0.6.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/content",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Nodo de contenido del ecosistema Dotrino: guarda los bytes del usuario direccionados por su hash (cid), en su propia maquina y, si el dueno quiere, respaldados en su propio bucket (R2, Backblaze, Hetzner, Storj o S3). Lo publico sale por URL directa; lo privado, cifrado y solo por la red Dotrino.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/public.js CHANGED
@@ -45,6 +45,7 @@ import http from 'node:http'
45
45
  import { open as openFile } from 'node:fs/promises'
46
46
  import { pipeline } from 'node:stream/promises'
47
47
  import { isValidCid } from './node.js'
48
+ import { parseRange } from './range.js'
48
49
 
49
50
  export const DEFAULT_PUBLIC_PORT = 3778
50
51
  /** Tope por blob: lo que hace que esto sirva previsualizaciones y no originales. */
@@ -268,7 +269,7 @@ export function createPublicServer (node, opts = {}) {
268
269
 
269
270
  const server = http.createServer(async (req, res) => {
270
271
  try {
271
- if (!limiter.allow(clientIp(req))) return text(res, 429, 'demasiadas peticiones\n', { 'retry-after': '60' })
272
+ if (!limiter.allow(clientIp(req))) return text(res, 429, 'too many requests\n', { 'retry-after': '60' })
272
273
  await route(req, res)
273
274
  } catch (err) {
274
275
  if (res.headersSent) { res.destroy(); return }
@@ -278,7 +279,7 @@ export function createPublicServer (node, opts = {}) {
278
279
  })
279
280
 
280
281
  async function route (req, res) {
281
- if (req.method !== 'GET' && req.method !== 'HEAD') return text(res, 405, 'método no permitido\n')
282
+ if (req.method !== 'GET' && req.method !== 'HEAD') return text(res, 405, 'method not allowed\n')
282
283
  const url = new URL(req.url, 'http://localhost')
283
284
  const [, top, cid] = url.pathname.split('/')
284
285
 
@@ -291,10 +292,10 @@ export function createPublicServer (node, opts = {}) {
291
292
  }
292
293
 
293
294
  if ((top === 'c' || top === 'p') && cid) {
294
- if (!isValidCid(cid)) return text(res, 400, 'cid inválido\n')
295
+ if (!isValidCid(cid)) return text(res, 400, 'invalid cid\n')
295
296
  // 404 y no 403 para lo privado: un 403 confirmaría que ese cid existe aquí.
296
297
  const row = node.publicStat(cid)
297
- if (!row) return text(res, 404, 'no disponible\n')
298
+ if (!row) return text(res, 404, 'not available\n')
298
299
 
299
300
  if (top === 'p') {
300
301
  const body = previewHtml({
@@ -313,10 +314,10 @@ export function createPublicServer (node, opts = {}) {
313
314
  // --- bytes ---
314
315
  // El tipo REAL, sacado del archivo. `row.mime` no decide nada aquí.
315
316
  const kind = await sniffImage(node.store, cid, { public: true })
316
- if (!kind) return text(res, 404, 'no disponible\n')
317
+ if (!kind) return text(res, 404, 'not available\n')
317
318
  if (maxBytes && row.size > maxBytes) {
318
319
  // No es un error del que pide: es la política del node. Se dice cuál es.
319
- return text(res, 413, 'este node solo publica vistas previas; el contenido se abre en la app\n')
320
+ return text(res, 413, 'this node only publishes previews; the content opens in the app\n')
320
321
  }
321
322
  const headers = {
322
323
  'content-type': kind,
@@ -331,14 +332,14 @@ export function createPublicServer (node, opts = {}) {
331
332
  }
332
333
  if (req.headers['if-none-match'] === `"${cid}"`) return res.writeHead(304, headers).end()
333
334
 
334
- const range = parseRangeHeader(req.headers.range, row.size)
335
+ const range = parseRange(req.headers.range, row.size)
335
336
  if (range === false) return res.writeHead(416, { 'content-range': `bytes */${row.size}` }).end()
336
337
  const sending = range ? range.end - range.start + 1 : row.size
337
338
  // El techo se mira contra lo que ESTA respuesta va a mandar, no contra cero:
338
339
  // dejar entrar una petición porque "aún quedan 10 bytes" es rebasarlo igual,
339
340
  // solo que fingiendo que no. Un HEAD no manda cuerpo, así que no gasta.
340
341
  if (req.method === 'GET' && egressLeft() < sending) {
341
- return text(res, 503, 'techo de salida diario alcanzado\n', { 'retry-after': '3600' })
342
+ return text(res, 503, 'daily egress cap reached\n', { 'retry-after': '3600' })
342
343
  }
343
344
  if (range) {
344
345
  headers['content-range'] = `bytes ${range.start}-${range.end}/${row.size}`
@@ -359,7 +360,7 @@ export function createPublicServer (node, opts = {}) {
359
360
  return
360
361
  }
361
362
 
362
- return text(res, 404, 'no disponible\n')
363
+ return text(res, 404, 'not available\n')
363
364
  }
364
365
 
365
366
  /**
@@ -380,21 +381,4 @@ export function createPublicServer (node, opts = {}) {
380
381
  return server
381
382
  }
382
383
 
383
- /** Igual que el del servidor local; duplicado mínimo para no acoplar los dos. */
384
- function parseRangeHeader (header, size) {
385
- if (!header) return null
386
- const m = /^bytes=(\d*)-(\d*)$/.exec(String(header).trim())
387
- if (!m || (m[1] === '' && m[2] === '')) return false
388
- let start, end
389
- if (m[1] === '') {
390
- const n = Number(m[2])
391
- if (n === 0) return false
392
- start = Math.max(0, size - n); end = size - 1
393
- } else {
394
- start = Number(m[1]); end = m[2] === '' ? size - 1 : Math.min(Number(m[2]), size - 1)
395
- }
396
- if (start >= size || start > end) return false
397
- return { start, end }
398
- }
399
-
400
384
  export default { createPublicServer, RateLimiter, previewHtml, utcDay, DEFAULT_PUBLIC_PORT, DEFAULT_MAX_BYTES }
package/src/range.js ADDED
@@ -0,0 +1,18 @@
1
+ /** Parsea `Range: bytes=a-b|a-|-n` contra `size`. null = sin rango; false = inválido. */
2
+ export function parseRange (header, size) {
3
+ if (!header) return null
4
+ const m = /^bytes=(\d*)-(\d*)$/.exec(String(header).trim())
5
+ if (!m || (m[1] === '' && m[2] === '')) return false
6
+ let start, end
7
+ if (m[1] === '') { // sufijo: últimos n bytes
8
+ const n = Number(m[2])
9
+ if (n === 0) return false
10
+ start = Math.max(0, size - n)
11
+ end = size - 1
12
+ } else {
13
+ start = Number(m[1])
14
+ end = m[2] === '' ? size - 1 : Math.min(Number(m[2]), size - 1)
15
+ }
16
+ if (start >= size || start > end) return false
17
+ return { start, end }
18
+ }
package/src/server.js CHANGED
@@ -14,25 +14,9 @@
14
14
  import http from 'node:http'
15
15
  import { pipeline } from 'node:stream/promises'
16
16
  import { isValidCid } from './node.js'
17
+ import { parseRange } from './range.js'
17
18
 
18
- /** Parsea `Range: bytes=a-b|a-|-n` contra `size`. null = sin rango; false = inválido. */
19
- export function parseRange (header, size) {
20
- if (!header) return null
21
- const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim())
22
- if (!m || (m[1] === '' && m[2] === '')) return false
23
- let start, end
24
- if (m[1] === '') { // sufijo: últimos n bytes
25
- const n = Number(m[2])
26
- if (n === 0) return false
27
- start = Math.max(0, size - n)
28
- end = size - 1
29
- } else {
30
- start = Number(m[1])
31
- end = m[2] === '' ? size - 1 : Math.min(Number(m[2]), size - 1)
32
- }
33
- if (start >= size || start > end) return false
34
- return { start, end }
35
- }
19
+ export { parseRange }
36
20
 
37
21
  const json = (res, code, obj) => {
38
22
  const body = JSON.stringify(obj)