@dotrino/content 0.6.2 → 0.6.4
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/bin/cli.js +13 -13
- package/package.json +1 -1
- package/src/public.js +10 -26
- package/src/range.js +18 -0
- package/src/server.js +2 -18
- package/src/storage.js +3 -3
package/bin/cli.js
CHANGED
|
@@ -105,8 +105,8 @@ if (cmd === 'info') {
|
|
|
105
105
|
// aquí: es donde se mira qué es este aparato. Fuera del JSON, que lo lee una máquina.
|
|
106
106
|
if (!values.json) {
|
|
107
107
|
const { updateStatusText } = await import('@dotrino/update/npm')
|
|
108
|
-
const
|
|
109
|
-
if (
|
|
108
|
+
const status = updateStatusText({ dir, current: VERSION, lang: 'es' })
|
|
109
|
+
if (status) console.log('\n' + status)
|
|
110
110
|
}
|
|
111
111
|
process.exit(0)
|
|
112
112
|
}
|
|
@@ -169,7 +169,7 @@ const gcMin = Number(values['gc-min'] || 60)
|
|
|
169
169
|
const { startVaultConfig, isEnrolled } = await import('../src/vaultEnv.js')
|
|
170
170
|
const log = (m) => console.log(m)
|
|
171
171
|
const vaultConfig = startVaultConfig({ log })
|
|
172
|
-
if (!isEnrolled()) console.log('
|
|
172
|
+
if (!isEnrolled()) console.log('no vault: local config (enroll with: dotrino-content enroll <code>)')
|
|
173
173
|
// Se ESPERA a la configuración antes de montar el almacén, porque de ella sale cuál es
|
|
174
174
|
// (§15.14). Con plazo: si la bóveda no contesta, se arranca con el disco y ya se
|
|
175
175
|
// reiniciará cuando llegue — un node no puede quedarse sin servir por eso.
|
|
@@ -180,7 +180,7 @@ const { store, cfg } = await openStore({ dir, log })
|
|
|
180
180
|
|
|
181
181
|
const node = await new ContentNode({ dir, maxBytes, maxBlobBytes, store, log, publicBase: cfg?.pub ? cfg.baseUrl : null }).init()
|
|
182
182
|
// Lo que quedó sin subir en un arranque anterior. Se lanza y no se espera.
|
|
183
|
-
node.backupPending().then(({ pending }) => { if (pending) log(`[
|
|
183
|
+
node.backupPending().then(({ pending }) => { if (pending) log(`[store] ${pending} pending upload(s) to the bucket`) })
|
|
184
184
|
const server = createServer(node)
|
|
185
185
|
|
|
186
186
|
// GC periódico de vencidos (ttl); el GC por cuota corre inline en cada put.
|
|
@@ -192,7 +192,7 @@ node.gc()
|
|
|
192
192
|
// propia ACL, su límite por IP y su techo de salida: no se activa por descuido.
|
|
193
193
|
server.listen(port, '127.0.0.1', () => {
|
|
194
194
|
const s = node.stats()
|
|
195
|
-
console.log(`dotrino-content ${VERSION}
|
|
195
|
+
console.log(`dotrino-content ${VERSION} on http://127.0.0.1:${port} · data: ${dir}`)
|
|
196
196
|
console.log(`blobs: ${s.blobs} · bytes: ${s.bytes}${maxBytes ? ` / ${maxBytes}` : ''}`)
|
|
197
197
|
})
|
|
198
198
|
|
|
@@ -201,15 +201,15 @@ server.listen(port, '127.0.0.1', () => {
|
|
|
201
201
|
// tiene administración remota cuando no la tiene.
|
|
202
202
|
let agent = null
|
|
203
203
|
if (values['no-agent']) {
|
|
204
|
-
console.log('
|
|
204
|
+
console.log('control plane: off (--no-agent)')
|
|
205
205
|
} else if (isLinked()) {
|
|
206
206
|
try {
|
|
207
207
|
agent = await startContentAgent({ node, version: VERSION })
|
|
208
208
|
} catch (e) {
|
|
209
|
-
console.error(`
|
|
209
|
+
console.error(`control plane: did not start (${e.message})`)
|
|
210
210
|
}
|
|
211
211
|
} else {
|
|
212
|
-
console.log('
|
|
212
|
+
console.log('control plane: not linked (run `dotrino-content enroll <code>` to manage it from your apps)')
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
// Modo público (§7.2). Va DESPUÉS del agente a propósito: el `owner` del node lo
|
|
@@ -231,14 +231,14 @@ if (values.public) {
|
|
|
231
231
|
owner: node.owner
|
|
232
232
|
})
|
|
233
233
|
publicServer.listen(publicPort, publicHost, () => {
|
|
234
|
-
console.log(`
|
|
235
|
-
`${maxKb ? `
|
|
236
|
-
`${egressGb ? ` ·
|
|
234
|
+
console.log(`public previews on http://${publicHost}:${publicPort} · images only` +
|
|
235
|
+
`${maxKb ? ` up to ${maxKb} KB` : ' (NO size cap)'}` +
|
|
236
|
+
`${egressGb ? ` · egress cap ${egressGb} GB/day` : ''}`)
|
|
237
237
|
if (!node.owner) {
|
|
238
|
-
console.log('
|
|
238
|
+
console.log(' note: this node is not linked to a vault, so the cards come out without an "Open" link')
|
|
239
239
|
}
|
|
240
240
|
if (!maxKb) {
|
|
241
|
-
console.log('
|
|
241
|
+
console.log(' note: with no size cap this serves originals, not previews — and you pay for the bandwidth')
|
|
242
242
|
}
|
|
243
243
|
})
|
|
244
244
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotrino/content",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
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, '
|
|
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, '
|
|
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
|
|
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, '
|
|
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, '
|
|
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, '
|
|
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 =
|
|
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, '
|
|
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, '
|
|
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
|
-
|
|
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)
|
package/src/storage.js
CHANGED
|
@@ -101,7 +101,7 @@ export async function checkBuckets (cfg, { priv, pub }, f = fetch) {
|
|
|
101
101
|
fatal.push(`the private bucket "${cfg.priv}" answers without credentials (HTTP ${r.status}): it is open to the world`)
|
|
102
102
|
}
|
|
103
103
|
} catch (e) {
|
|
104
|
-
warn.push(`
|
|
104
|
+
warn.push(`could not check whether the private bucket is closed: ${e.message}`)
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
// 3. El dominio del público tiene que servir DE VERDAD, y eso no se deduce mirando
|
|
@@ -122,10 +122,10 @@ export async function checkBuckets (cfg, { priv, pub }, f = fetch) {
|
|
|
122
122
|
const r = await f(`${cfg.baseUrl.replace(/\/+$/, '')}/${cid}`, { method: 'GET' })
|
|
123
123
|
const body = r.ok ? (await r.text()).trim() : ''
|
|
124
124
|
if (body !== PROBE) {
|
|
125
|
-
warn.push(`${cfg.baseUrl}
|
|
125
|
+
warn.push(`${cfg.baseUrl} does not serve what is in "${cfg.pub}" (HTTP ${r.status}): check that the domain is connected TO THE BUCKET in the dashboard`)
|
|
126
126
|
}
|
|
127
127
|
} catch (e) {
|
|
128
|
-
warn.push(`
|
|
128
|
+
warn.push(`could not check the public path: ${e.message}`)
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
|