tinyzoo 0.2.0 → 0.2.2

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": "tinyzoo",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "description": "Code-first, distributable CMS for Nuxt 4 (Layer + Module).",
6
6
  "license": "UNLICENSED",
@@ -50,6 +50,7 @@
50
50
  "!starter/.output"
51
51
  ],
52
52
  "dependencies": {
53
+ "@fastify/busboy": "^3.2.6",
53
54
  "@hocuspocus/extension-redis": "^3.4.4",
54
55
  "@hocuspocus/provider": "^3.4.4",
55
56
  "@hocuspocus/server": "^3.4.4",
@@ -63,19 +64,17 @@
63
64
  "@tiptap/starter-kit": "^3.34.0",
64
65
  "@tiptap/vue-3": "^3.34.0",
65
66
  "@tiptap/y-tiptap": "^3.3.0",
66
- "@types/busboy": "^1.5.5",
67
67
  "@types/node": ">=20",
68
68
  "@types/nodemailer": "^8.0.2",
69
69
  "@types/pg": "^8.11.0",
70
70
  "aws4fetch": "^1.0.20",
71
- "busboy": "^1.6.0",
72
71
  "drizzle-orm": "^0.44.0",
73
72
  "image-size": "^2.0.2",
74
73
  "ioredis": "^5.11.1",
75
74
  "jiti": "^2.6.0",
76
75
  "nodemailer": "^7.0.13",
77
76
  "pg": "^8.16.3",
78
- "sharp": "^0.34.5",
77
+ "sharp": "^0.35.5",
79
78
  "tsx": "^4.19.0",
80
79
  "yjs": "^13.6.31",
81
80
  "zod": "^4.0.0"
@@ -360,7 +360,7 @@ async function startCollabServer(): Promise<Hocuspocus> {
360
360
  })
361
361
  instance = server
362
362
 
363
- heartbeat ??= setInterval(() => { void beat() }, HEARTBEAT_MS)
363
+ heartbeat ??= setInterval(() => { void beat() }, cmsTimerMs('CMS_TEST_COLLAB_HEARTBEAT_MS', HEARTBEAT_MS))
364
364
  ;(heartbeat as any).unref?.()
365
365
  return server
366
366
  }
@@ -55,6 +55,7 @@ interface LocalPeer {
55
55
  }
56
56
 
57
57
  const LIVE_CHANNEL = 'live'
58
+ /** The test build shortens it (CMS_TEST_LIVE_HEARTBEAT_MS); the other limits below stay, so a quiet test tab is not dropped. */
58
59
  const HEARTBEAT_MS = 20_000
59
60
  const REMOTE_TTL_MS = 50_000
60
61
  /** A socket that has not answered for this long is gone (sleeping laptop, dead network). */
@@ -82,7 +83,7 @@ let stopChanges: (() => void) | null = null
82
83
  */
83
84
  function start(): void {
84
85
  if (started) return
85
- timer = setInterval(() => { void heartbeat() }, HEARTBEAT_MS)
86
+ timer = setInterval(() => { void heartbeat() }, cmsTimerMs('CMS_TEST_LIVE_HEARTBEAT_MS', HEARTBEAT_MS))
86
87
  ;(timer as any).unref?.()
87
88
  stopChanges = onCmsChange((change) => { void deliverChange(change).catch((err) => console.error('[cms] realtime delivery failed', scrubError(err))) })
88
89
  started = (async () => {
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Timer intervals the test build shortens. The defaults are the production values;
3
+ * a deploy has no reason to set these, and a value that is not a plain number of
4
+ * milliseconds (at least 50) is ignored. Read when the timer starts, not at build time,
5
+ * so one build serves both: the API suites start the same server with a short
6
+ * heartbeat (CMS_TEST_LIVE_HEARTBEAT_MS, CMS_TEST_COLLAB_HEARTBEAT_MS) and webhook
7
+ * retries (CMS_TEST_WEBHOOK_RETRY_MS) and wait for seconds instead of half a minute.
8
+ */
9
+ export function cmsTimerMs(envName: string, defaultMs: number): number {
10
+ const raw = typeof process !== 'undefined' ? process.env?.[envName] : undefined
11
+ if (!raw || !/^\d+$/.test(raw.trim())) return defaultMs
12
+ const ms = Number(raw)
13
+ return ms >= 50 ? ms : defaultMs
14
+ }
@@ -1,4 +1,5 @@
1
- import busboy from 'busboy'
1
+ // The Fastify team's maintained fork of busboy (also what undici, Node's fetch, uses); types built in.
2
+ import Busboy, { type BusboyInstance } from '@fastify/busboy'
2
3
  import { createHash, randomUUID } from 'node:crypto'
3
4
  import { createWriteStream } from 'node:fs'
4
5
  import { mkdir, readdir, stat, unlink } from 'node:fs/promises'
@@ -59,6 +60,29 @@ function sweepOldTemp(dir: string) {
59
60
  }))).catch(() => {})
60
61
  }
61
62
 
63
+ let tempDirWarned = false
64
+
65
+ /**
66
+ * Creates the folder uploads are staged in. A container with a read-only filesystem and
67
+ * no writable /tmp (some hosts run every app that way) fails here on the first upload;
68
+ * the log then says what to set instead of a bare ENOENT/EROFS, once per process.
69
+ */
70
+ async function prepareTempDir(tempDir: string): Promise<void> {
71
+ try {
72
+ await mkdir(tempDir, { recursive: true })
73
+ }
74
+ catch (err: any) {
75
+ if (!tempDirWarned) {
76
+ tempDirWarned = true
77
+ console.error(`[cms] uploads are staged on disk, but ${tempDir} cannot be created (${err?.code ?? err}). `
78
+ + 'The filesystem is probably read-only: mount a writable volume and point TMPDIR at it '
79
+ + '(or at /dev/shm, which counts against the memory limit, for small files only). '
80
+ + 'See https://github.com/tinyzookeeper/tinyzoo/blob/main/docs/deploying.md#media')
81
+ }
82
+ throw createError({ statusCode: 500, statusMessage: 'The server has no writable folder for uploads' })
83
+ }
84
+ }
85
+
62
86
  /**
63
87
  * Read the multipart upload of `event`: the first part named `file` with a filename is
64
88
  * written to `tempDir`, the small text fields are returned. Throws 413 when more than
@@ -68,14 +92,15 @@ function sweepOldTemp(dir: string) {
68
92
  export async function receiveUpload(event: any, opts: { maxBytes: number; tempDir: string }): Promise<ReceivedUpload> {
69
93
  const req = event.node.req
70
94
  const { maxBytes, tempDir } = opts
71
- await mkdir(tempDir, { recursive: true })
95
+ await prepareTempDir(tempDir)
72
96
  sweepOldTemp(tempDir)
73
97
 
74
- let parser: busboy.Busboy
98
+ let parser: BusboyInstance
75
99
  try {
76
- parser = busboy({
100
+ parser = Busboy({
77
101
  headers: req.headers,
78
- defParamCharset: 'utf8',
102
+ // Field values and file names without a charset of their own are UTF-8.
103
+ defCharset: 'utf8',
79
104
  // One file, a handful of short fields; a byte over the cap shows as the file's 'limit'.
80
105
  limits: { files: 1, fields: 12, fieldSize: 4096, parts: 16, fileSize: maxBytes + 1 },
81
106
  })
@@ -113,13 +138,13 @@ export async function receiveUpload(event: any, opts: { maxBytes: number; tempDi
113
138
  if (total > cap) stop(tooLarge(maxBytes))
114
139
  }
115
140
 
116
- parser.on('field', (name, value, info) => {
117
- if (info.valueTruncated || info.nameTruncated) return stop(createError({ statusCode: 400, statusMessage: 'A form field is too long' }))
141
+ parser.on('field', (name: string, value: string, nameTruncated: boolean, valueTruncated: boolean) => {
142
+ if (valueTruncated || nameTruncated) return stop(createError({ statusCode: 400, statusMessage: 'A form field is too long' }))
118
143
  fields[name] = value
119
144
  })
120
145
 
121
- parser.on('file', (name, stream, info) => {
122
- if (name !== 'file' || !info.filename || received || writing) {
146
+ parser.on('file', (name: string, stream: NodeJS.ReadableStream & { resume(): void }, filename: string, _encoding: string, mimeType: string) => {
147
+ if (name !== 'file' || !filename || received || writing) {
123
148
  stream.resume() // a second or unexpected file: read past it
124
149
  return
125
150
  }
@@ -131,6 +156,11 @@ export async function receiveUpload(event: any, opts: { maxBytes: number; tempDi
131
156
  const meter = new Transform({
132
157
  transform(chunk: Buffer, _enc, cb) {
133
158
  size += chunk.length
159
+ // The cap is ours, not the parser's: refuse the moment the file passes it.
160
+ if (size > maxBytes) {
161
+ stop(tooLarge(maxBytes))
162
+ return cb(tooLarge(maxBytes))
163
+ }
134
164
  hash.update(chunk)
135
165
  if (headBytes < UPLOAD_HEAD_BYTES) {
136
166
  const part = chunk.subarray(0, UPLOAD_HEAD_BYTES - headBytes)
@@ -142,7 +172,7 @@ export async function receiveUpload(event: any, opts: { maxBytes: number; tempDi
142
172
  })
143
173
  stream.on('limit', () => stop(tooLarge(maxBytes)))
144
174
  writing = pipeline(stream, meter, createWriteStream(tempPath)).then(() => {
145
- received = { filename: info.filename ?? '', type: String(info.mimeType || '').toLowerCase(), path: tempPath, size, sha256: hash.digest('hex'), head: Buffer.concat(head) }
175
+ received = { filename: filename ?? '', type: String(mimeType || '').toLowerCase(), path: tempPath, size, sha256: hash.digest('hex'), head: Buffer.concat(head) }
146
176
  })
147
177
  writing.catch((err) => stop(err))
148
178
  })
@@ -151,7 +181,8 @@ export async function receiveUpload(event: any, opts: { maxBytes: number; tempDi
151
181
  parser.on('partsLimit', () => stop(createError({ statusCode: 400, statusMessage: 'Too many parts in the upload' })))
152
182
  parser.on('fieldsLimit', () => stop(createError({ statusCode: 400, statusMessage: 'Too many fields in the upload' })))
153
183
 
154
- parser.on('close', async () => {
184
+ // 'finish': every part has been read (the file part's stream may still be writing).
185
+ parser.on('finish', async () => {
155
186
  if (finished) return
156
187
  try { await writing } catch { return } // stop() has already answered
157
188
  if (finished) return
@@ -20,7 +20,9 @@ import type { CmsChange } from 'tinyzoo'
20
20
  * the 2nd); a 4xx answer other than 408/429 is final. Runs after the response and
21
21
  * only ever logs failures. In production nothing is sent without a secret.
22
22
  */
23
- const RETRY_DELAYS_MS = [1_000, 5_000]
23
+ const RETRY_DELAY_MS = 1_000
24
+ /** The 2nd attempt after the first delay, the 3rd after five times as long (the test build shortens it: CMS_TEST_WEBHOOK_RETRY_MS). */
25
+ const retryDelays = (): number[] => { const first = cmsTimerMs('CMS_TEST_WEBHOOK_RETRY_MS', RETRY_DELAY_MS); return [first, first * 5] }
24
26
  const TIMEOUT_MS = 5_000
25
27
 
26
28
  export function webhookConfig(): { urls: string[]; secret: string } {
@@ -71,8 +73,9 @@ export async function sendWebhooks(change: CmsChange): Promise<void> {
71
73
 
72
74
  async function deliver(url: string, secret: string, type: string, delivery: string, body: string): Promise<void> {
73
75
  let lastError = ''
74
- for (let attempt = 0; attempt <= RETRY_DELAYS_MS.length; attempt++) {
75
- if (attempt) await new Promise((r) => setTimeout(r, RETRY_DELAYS_MS[attempt - 1]))
76
+ const delays = retryDelays()
77
+ for (let attempt = 0; attempt <= delays.length; attempt++) {
78
+ if (attempt) await new Promise((r) => setTimeout(r, delays[attempt - 1]))
76
79
  try {
77
80
  const t = Math.floor(Date.now() / 1000)
78
81
  const headers: Record<string, string> = {