@prunellejs/files 0.7.2 → 0.7.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 +2 -2
- package/src/index.ts +2 -8
- package/src/keys.ts +47 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/files",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Zero-dependency file upload storage (disk, Bun S3, memory), magic-byte MIME sniffing, signed private URLs, and RGPD erasure plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"directory": "packages/plugin-files"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"prunelle": "^0.7.
|
|
24
|
+
"prunelle": "^0.7.3"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { FileInput, FileMetadata, Files, FilesOptions, FileStore, PutFileOp
|
|
|
6
6
|
import { createHash, randomBytes } from 'node:crypto'
|
|
7
7
|
import { definePlugin } from 'prunelle/config'
|
|
8
8
|
import { nodeEnv } from 'prunelle/server'
|
|
9
|
-
import { encodeKeySegments, normalizeBasePath, normalizeFileKey, normalizeFilePrefix, parseTtlSeconds, sanitizeFilenameStem, signFileUrl, verifyFileSignature } from './keys'
|
|
9
|
+
import { encodeKeySegments, formatAttachmentDisposition, normalizeBasePath, normalizeFileKey, normalizeFilePrefix, parseTtlSeconds, sanitizeFilenameStem, signFileUrl, verifyFileSignature } from './keys'
|
|
10
10
|
import { DEFAULT_ALLOWED_MIMES, extensionForMime, mimeMatchesAllowlist, sniffMime } from './sniff'
|
|
11
11
|
import { memoryFiles } from './stores'
|
|
12
12
|
import { FilesError } from './types'
|
|
@@ -70,12 +70,6 @@ function etagMatches(ifNoneMatch: string | null, quotedEtag: string): boolean {
|
|
|
70
70
|
return false
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
function safeDownloadFilename(key: string): string {
|
|
74
|
-
const base = key.split('/').pop() ?? 'download'
|
|
75
|
-
const cleaned = base.replace(/["\\\r\n;]/g, '_').trim()
|
|
76
|
-
return cleaned.length > 0 ? cleaned : 'download'
|
|
77
|
-
}
|
|
78
|
-
|
|
79
73
|
async function readInputBytes(input: FileInput, maxSize: number): Promise<{ bytes: Uint8Array, inferredName: string | undefined }> {
|
|
80
74
|
if (input instanceof Blob) {
|
|
81
75
|
if (input.size > maxSize) {
|
|
@@ -173,7 +167,7 @@ function buildServeResponse(
|
|
|
173
167
|
})
|
|
174
168
|
|
|
175
169
|
if (ACTIVE_CONTENT_TYPES.has(file.contentType.toLowerCase())) {
|
|
176
|
-
headers.set('content-disposition',
|
|
170
|
+
headers.set('content-disposition', formatAttachmentDisposition(key))
|
|
177
171
|
headers.set('content-security-policy', "default-src 'none'; sandbox")
|
|
178
172
|
}
|
|
179
173
|
|
package/src/keys.ts
CHANGED
|
@@ -215,3 +215,50 @@ export function sanitizeFilenameStem(filename: string | undefined): string {
|
|
|
215
215
|
.slice(0, 40)
|
|
216
216
|
return clean
|
|
217
217
|
}
|
|
218
|
+
|
|
219
|
+
// RFC 5987 §3.2.1 `attr-char` byte allowlist.
|
|
220
|
+
const RFC5987_ATTR_CHAR = new Set<number>(
|
|
221
|
+
Array.from("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$&+-.^_`|~", c => c.charCodeAt(0)),
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
function encodeRfc5987(value: string): string {
|
|
225
|
+
const bytes = new TextEncoder().encode(value)
|
|
226
|
+
let out = ''
|
|
227
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
228
|
+
const b = bytes[i]!
|
|
229
|
+
out += RFC5987_ATTR_CHAR.has(b)
|
|
230
|
+
? String.fromCharCode(b)
|
|
231
|
+
: `%${b.toString(16).toUpperCase().padStart(2, '0')}`
|
|
232
|
+
}
|
|
233
|
+
return out
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function asciiFallbackFilename(name: string): string {
|
|
237
|
+
const ascii = name
|
|
238
|
+
.normalize('NFKD')
|
|
239
|
+
.replace(/[\u0300-\u036F]/g, '')
|
|
240
|
+
// eslint-disable-next-line no-control-regex
|
|
241
|
+
.replace(/[^\x20-\x7E]|["\\;]/g, '_')
|
|
242
|
+
.trim()
|
|
243
|
+
if (ascii.length === 0)
|
|
244
|
+
return 'download'
|
|
245
|
+
const dot = ascii.lastIndexOf('.')
|
|
246
|
+
const stem = dot > 0 ? ascii.slice(0, dot) : ascii
|
|
247
|
+
const ext = dot > 0 ? ascii.slice(dot) : ''
|
|
248
|
+
if (/[a-zA-Z0-9]/.test(stem))
|
|
249
|
+
return ascii
|
|
250
|
+
return ext.length > 1 && /^\.[a-zA-Z0-9]+$/.test(ext) ? `download${ext}` : 'download'
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Formats an RFC 6266 / RFC 5987 `Content-Disposition: attachment` header value for a file key. */
|
|
254
|
+
export function formatAttachmentDisposition(key: string): string {
|
|
255
|
+
const base = (key.split('/').pop() ?? '')
|
|
256
|
+
// eslint-disable-next-line no-control-regex
|
|
257
|
+
.replace(/[\u0000-\u001F\u007F]/g, '')
|
|
258
|
+
.trim()
|
|
259
|
+
const raw = base.length > 0 ? base : 'download'
|
|
260
|
+
const fallback = asciiFallbackFilename(raw)
|
|
261
|
+
if (fallback === raw)
|
|
262
|
+
return `attachment; filename="${fallback}"`
|
|
263
|
+
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodeRfc5987(raw)}`
|
|
264
|
+
}
|