@autonomys/file-server 1.6.1 → 1.6.2-beta.1
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/dist/http/headers.d.ts +1 -1
- package/dist/http/headers.d.ts.map +1 -1
- package/dist/http/headers.js +99 -36
- package/package.json +7 -6
package/dist/http/headers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Request, Response } from 'express';
|
|
2
2
|
import { ByteRange, DownloadMetadata, DownloadOptions } from '../models.js';
|
|
3
|
-
export declare const handleDownloadResponseHeaders: (req: Request, res: Response, metadata: DownloadMetadata,
|
|
3
|
+
export declare const handleDownloadResponseHeaders: (req: Request, res: Response, metadata: DownloadMetadata, { byteRange, rawMode }: DownloadOptions) => void;
|
|
4
4
|
export declare const handleS3DownloadResponseHeaders: (req: Request, res: Response, metadata: DownloadMetadata) => void;
|
|
5
5
|
export declare const getByteRange: (req: Request) => ByteRange | undefined;
|
|
6
6
|
//# sourceMappingURL=headers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headers.d.ts","sourceRoot":"","sources":["../../src/http/headers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"headers.d.ts","sourceRoot":"","sources":["../../src/http/headers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAqF3E,eAAO,MAAM,6BAA6B,GACxC,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,UAAU,gBAAgB,EAC1B,wBAA4C,eAAe,SAyC5D,CAAA;AAED,eAAO,MAAM,+BAA+B,GAC1C,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,UAAU,gBAAgB,SAS3B,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,KAAK,OAAO,KAAG,SAAS,GAAG,SAgBvD,CAAA"}
|
package/dist/http/headers.js
CHANGED
|
@@ -1,45 +1,108 @@
|
|
|
1
1
|
import { CompressionAlgorithm, EncryptionAlgorithm } from '@autonomys/auto-dag-data';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (metadata.type === 'file') {
|
|
14
|
-
setFileResponseHeaders(res, metadata, isEncrypted, documentExpected, shouldHandleEncoding, safeName, options);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
setFolderResponseHeaders(res, isEncrypted, documentExpected, safeName);
|
|
2
|
+
import { inferMimeType } from '../utils.js';
|
|
3
|
+
// Generic mimetypes that should trigger extension-based fallback
|
|
4
|
+
const GENERIC_MIME_TYPES = new Set(['application/octet-stream', 'binary/octet-stream']);
|
|
5
|
+
// Get the best mimetype for a file, falling back to extension-based inference
|
|
6
|
+
// when the stored mimetype is missing or generic
|
|
7
|
+
const getMimeType = (metadata) => {
|
|
8
|
+
var _a;
|
|
9
|
+
const storedMime = (_a = metadata.mimeType) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
10
|
+
// If we have a meaningful mimetype, use it
|
|
11
|
+
if (storedMime && !GENERIC_MIME_TYPES.has(storedMime)) {
|
|
12
|
+
return metadata.mimeType;
|
|
18
13
|
}
|
|
14
|
+
// Otherwise, infer from filename extension
|
|
15
|
+
return inferMimeType(metadata.name);
|
|
16
|
+
};
|
|
17
|
+
// Check if this is actually a document navigation (based on headers only)
|
|
18
|
+
// Used to determine if browser will auto-decompress Content-Encoding
|
|
19
|
+
const isDocumentNavigation = (req) => {
|
|
20
|
+
const destHeader = req.headers['sec-fetch-dest'];
|
|
21
|
+
const dest = (Array.isArray(destHeader) ? destHeader[0] : (destHeader !== null && destHeader !== void 0 ? destHeader : '')).toLowerCase();
|
|
22
|
+
if (dest && dest !== 'document')
|
|
23
|
+
return false; // e.g. <img>, <video>, fetch(), etc.
|
|
24
|
+
const modeHeader = req.headers['sec-fetch-mode'];
|
|
25
|
+
const mode = (Array.isArray(modeHeader) ? modeHeader[0] : (modeHeader !== null && modeHeader !== void 0 ? modeHeader : '')).toLowerCase();
|
|
26
|
+
if (mode && mode !== 'navigate')
|
|
27
|
+
return false; // programmatic fetch / subresource
|
|
28
|
+
return true;
|
|
29
|
+
};
|
|
30
|
+
// Decide if this file type is something browsers can usually render inline via URL
|
|
31
|
+
// (mirrors the logic in canDisplayDirectly but on DownloadMetadata)
|
|
32
|
+
const isPreviewableInline = (metadata) => {
|
|
33
|
+
if (metadata.isEncrypted)
|
|
34
|
+
return false;
|
|
35
|
+
// Use getMimeType to get the best available mimetype (with extension fallback)
|
|
36
|
+
const mimeType = getMimeType(metadata).toLowerCase();
|
|
37
|
+
const directDisplayTypes = ['image/', 'video/', 'audio/'];
|
|
38
|
+
return (directDisplayTypes.some((type) => mimeType.startsWith(type)) || mimeType === 'application/pdf');
|
|
19
39
|
};
|
|
20
|
-
const
|
|
40
|
+
const isInlineDisposition = (req, metadata) => {
|
|
41
|
+
// Explicit query overrides - treat presence as boolean flag
|
|
42
|
+
// ?download or ?download=true triggers attachment, ?download=false is ignored
|
|
43
|
+
if (req.query.download === 'true' || req.query.download === '')
|
|
44
|
+
return false;
|
|
45
|
+
// ?inline or ?inline=true triggers inline, ?inline=false is ignored
|
|
46
|
+
if (req.query.inline === 'true' || req.query.inline === '')
|
|
47
|
+
return true;
|
|
48
|
+
// Folders (served as zip) should default to attachment
|
|
49
|
+
if (metadata.type !== 'file')
|
|
50
|
+
return false;
|
|
51
|
+
// For media / PDFs that browsers can render directly, prefer inline even for subresources
|
|
52
|
+
if (isPreviewableInline(metadata))
|
|
53
|
+
return true;
|
|
54
|
+
// Fallback to header-based detection: top-level document navigations are inline
|
|
55
|
+
return isDocumentNavigation(req);
|
|
56
|
+
};
|
|
57
|
+
// Helper to create an ASCII-safe fallback for filename parameter (RFC 2183/6266)
|
|
58
|
+
const toAsciiFallback = (name) => name
|
|
59
|
+
.replace(/[^\x20-\x7E]+/g, '_') // replace non-ASCII with underscore
|
|
60
|
+
.replace(/["\\]/g, '\\$&'); // escape quotes and backslashes
|
|
61
|
+
// RFC 5987 encoding for filename* parameter
|
|
62
|
+
const rfc5987Encode = (str) => encodeURIComponent(str)
|
|
63
|
+
.replace(/['()]/g, (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase())
|
|
64
|
+
.replace(/\*/g, '%2A');
|
|
65
|
+
const buildDisposition = (req, metadata, filename) => {
|
|
66
|
+
const fallbackName = toAsciiFallback(filename || 'download');
|
|
67
|
+
const encoded = rfc5987Encode(filename || 'download');
|
|
68
|
+
const type = isInlineDisposition(req, metadata) ? 'inline' : 'attachment';
|
|
69
|
+
return `${type}; filename="${fallbackName}"; filename*=UTF-8''${encoded}`;
|
|
70
|
+
};
|
|
71
|
+
export const handleDownloadResponseHeaders = (req, res, metadata, { byteRange = undefined, rawMode = false }) => {
|
|
21
72
|
var _a;
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
73
|
+
const baseName = metadata.name || 'download';
|
|
74
|
+
const fileName = metadata.type === 'file' ? baseName : `${baseName}.zip`;
|
|
75
|
+
if (metadata.type === 'file') {
|
|
76
|
+
const contentType = !metadata.isEncrypted && !rawMode ? getMimeType(metadata) : 'application/octet-stream';
|
|
77
|
+
res.set('Content-Type', contentType);
|
|
78
|
+
// Advertise range support for files so browsers like Chrome can seek in media
|
|
79
|
+
if (metadata.size != null) {
|
|
80
|
+
res.set('Accept-Ranges', 'bytes');
|
|
81
|
+
}
|
|
82
|
+
const compressedButNotEncrypted = metadata.isCompressed && !metadata.isEncrypted;
|
|
83
|
+
// Only set Content-Encoding for document navigations where browsers auto-decompress
|
|
84
|
+
// Don't set it for <img>, <video>, fetch(), etc. as browsers won't auto-decompress those
|
|
85
|
+
const shouldHandleEncoding = req.query.ignoreEncoding
|
|
86
|
+
? req.query.ignoreEncoding !== 'true'
|
|
87
|
+
: isDocumentNavigation(req);
|
|
88
|
+
if (compressedButNotEncrypted && shouldHandleEncoding && !rawMode && !byteRange) {
|
|
89
|
+
res.set('Content-Encoding', 'deflate');
|
|
90
|
+
}
|
|
91
|
+
if (byteRange) {
|
|
92
|
+
res.status(206);
|
|
93
|
+
res.set('Content-Range', `bytes ${byteRange[0]}-${byteRange[1]}/${metadata.size}`);
|
|
94
|
+
const upperBound = (_a = byteRange[1]) !== null && _a !== void 0 ? _a : Number(metadata.size) - 1;
|
|
95
|
+
res.set('Content-Length', (upperBound - byteRange[0] + 1).toString());
|
|
96
|
+
}
|
|
97
|
+
else if (metadata.size) {
|
|
98
|
+
res.set('Content-Length', metadata.size.toString());
|
|
99
|
+
}
|
|
34
100
|
}
|
|
35
|
-
else
|
|
36
|
-
|
|
101
|
+
else {
|
|
102
|
+
const contentType = metadata.isEncrypted ? 'application/octet-stream' : 'application/zip';
|
|
103
|
+
res.set('Content-Type', contentType);
|
|
37
104
|
}
|
|
38
|
-
|
|
39
|
-
const setFolderResponseHeaders = (res, isEncrypted, isExpectedDocument, safeName) => {
|
|
40
|
-
const contentType = isEncrypted ? 'application/octet-stream' : 'application/zip';
|
|
41
|
-
res.set('Content-Type', contentType);
|
|
42
|
-
res.set('Content-Disposition', `${isExpectedDocument ? 'inline' : 'attachment'}; filename="${safeName}.zip"`);
|
|
105
|
+
res.set('Content-Disposition', buildDisposition(req, metadata, fileName));
|
|
43
106
|
};
|
|
44
107
|
export const handleS3DownloadResponseHeaders = (req, res, metadata) => {
|
|
45
108
|
if (metadata.isEncrypted) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autonomys/file-server",
|
|
3
3
|
"packageManager": "yarn@4.7.0",
|
|
4
|
-
"version": "1.6.1",
|
|
4
|
+
"version": "1.6.2-beta.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"node": ">=20.8.0"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
|
-
"build": "yarn tsc"
|
|
22
|
+
"build": "yarn tsc",
|
|
23
|
+
"test": "node --experimental-vm-modules ../../../node_modules/jest/bin/jest.js --config ./jest.config.cjs"
|
|
23
24
|
},
|
|
24
25
|
"main": "./dist/index.js",
|
|
25
26
|
"types": "./dist/index.d.ts",
|
|
@@ -47,9 +48,9 @@
|
|
|
47
48
|
"typescript": "^5.8.3"
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"@autonomys/asynchronous": "^1.6.1",
|
|
51
|
-
"@autonomys/auto-dag-data": "^1.6.1",
|
|
52
|
-
"@autonomys/auto-utils": "^1.6.1",
|
|
51
|
+
"@autonomys/asynchronous": "^1.6.2-beta.1",
|
|
52
|
+
"@autonomys/auto-dag-data": "^1.6.2-beta.1",
|
|
53
|
+
"@autonomys/auto-utils": "^1.6.2-beta.1",
|
|
53
54
|
"@keyvhq/sqlite": "^2.1.7",
|
|
54
55
|
"cache-manager": "^6.4.2",
|
|
55
56
|
"express": "^4.19.2",
|
|
@@ -61,5 +62,5 @@
|
|
|
61
62
|
"uuid": "^11.1.0",
|
|
62
63
|
"zod": "^3.24.2"
|
|
63
64
|
},
|
|
64
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "6fc7f796baaaafbae100c7052363186a951ab7bf"
|
|
65
66
|
}
|