@autonomys/file-server 1.6.0 → 1.6.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/dist/http/headers.d.ts +1 -1
- package/dist/http/headers.d.ts.map +1 -1
- package/dist/http/headers.js +64 -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;AA8C3E,eAAO,MAAM,6BAA6B,GACxC,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,UAAU,gBAAgB,EAC1B,wBAA4C,eAAe,SAoC5D,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,73 @@
|
|
|
1
1
|
import { CompressionAlgorithm, EncryptionAlgorithm } from '@autonomys/auto-dag-data';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
// Check if this is actually a document navigation (based on headers only)
|
|
3
|
+
// Used to determine if browser will auto-decompress Content-Encoding
|
|
4
|
+
const isDocumentNavigation = (req) => {
|
|
5
|
+
const destHeader = req.headers['sec-fetch-dest'];
|
|
6
|
+
const dest = (Array.isArray(destHeader) ? destHeader[0] : (destHeader !== null && destHeader !== void 0 ? destHeader : '')).toLowerCase();
|
|
7
|
+
if (dest && dest !== 'document')
|
|
8
|
+
return false; // e.g. <img>, fetch(), etc.
|
|
9
|
+
const modeHeader = req.headers['sec-fetch-mode'];
|
|
10
|
+
const mode = (Array.isArray(modeHeader) ? modeHeader[0] : (modeHeader !== null && modeHeader !== void 0 ? modeHeader : '')).toLowerCase();
|
|
11
|
+
if (mode && mode !== 'navigate')
|
|
12
|
+
return false; // programmatic fetch
|
|
13
|
+
return true;
|
|
5
14
|
};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
const isInlineDocument = (req) => {
|
|
16
|
+
// Check explicit query parameters - treat presence as boolean flag
|
|
17
|
+
// ?download or ?download=true triggers attachment, ?download=false is ignored
|
|
18
|
+
if (req.query.download === 'true' || req.query.download === '')
|
|
19
|
+
return false;
|
|
20
|
+
// ?inline or ?inline=true triggers inline, ?inline=false is ignored
|
|
21
|
+
if (req.query.inline === 'true' || req.query.inline === '')
|
|
22
|
+
return true;
|
|
23
|
+
// Fall back to header-based detection
|
|
24
|
+
return isDocumentNavigation(req);
|
|
25
|
+
};
|
|
26
|
+
// Helper to create an ASCII-safe fallback for filename parameter (RFC 2183/6266)
|
|
27
|
+
const toAsciiFallback = (name) => name
|
|
28
|
+
.replace(/[^\x20-\x7E]+/g, '_') // replace non-ASCII with underscore
|
|
29
|
+
.replace(/["\\]/g, '\\$&'); // escape quotes and backslashes
|
|
30
|
+
// RFC 5987 encoding for filename* parameter
|
|
31
|
+
const rfc5987Encode = (str) => encodeURIComponent(str)
|
|
32
|
+
.replace(/['()]/g, (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase())
|
|
33
|
+
.replace(/\*/g, '%2A');
|
|
34
|
+
const buildDisposition = (req, filename) => {
|
|
35
|
+
const fallbackName = toAsciiFallback(filename || 'download');
|
|
36
|
+
const encoded = rfc5987Encode(filename || 'download');
|
|
37
|
+
const type = isInlineDocument(req) ? 'inline' : 'attachment';
|
|
38
|
+
return `${type}; filename="${fallbackName}"; filename*=UTF-8''${encoded}`;
|
|
19
39
|
};
|
|
20
|
-
const
|
|
40
|
+
export const handleDownloadResponseHeaders = (req, res, metadata, { byteRange = undefined, rawMode = false }) => {
|
|
21
41
|
var _a;
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
42
|
+
const baseName = metadata.name || 'download';
|
|
43
|
+
const fileName = metadata.type === 'file' ? baseName : `${baseName}.zip`;
|
|
44
|
+
if (metadata.type === 'file') {
|
|
45
|
+
const contentType = (!metadata.isEncrypted && !rawMode && metadata.mimeType) || 'application/octet-stream';
|
|
46
|
+
res.set('Content-Type', contentType);
|
|
47
|
+
const compressedButNoEncrypted = metadata.isCompressed && !metadata.isEncrypted;
|
|
48
|
+
// Only set Content-Encoding for document navigations where browsers auto-decompress
|
|
49
|
+
// Don't set it for <img>, fetch(), etc. as browsers won't auto-decompress those
|
|
50
|
+
const shouldHandleEncoding = req.query.ignoreEncoding
|
|
51
|
+
? req.query.ignoreEncoding !== 'true'
|
|
52
|
+
: isDocumentNavigation(req);
|
|
53
|
+
if (compressedButNoEncrypted && shouldHandleEncoding && !rawMode && !byteRange) {
|
|
54
|
+
res.set('Content-Encoding', 'deflate');
|
|
55
|
+
}
|
|
56
|
+
if (byteRange) {
|
|
57
|
+
res.status(206);
|
|
58
|
+
res.set('Content-Range', `bytes ${byteRange[0]}-${byteRange[1]}/${metadata.size}`);
|
|
59
|
+
const upperBound = (_a = byteRange[1]) !== null && _a !== void 0 ? _a : Number(metadata.size) - 1;
|
|
60
|
+
res.set('Content-Length', (upperBound - byteRange[0] + 1).toString());
|
|
61
|
+
}
|
|
62
|
+
else if (metadata.size) {
|
|
63
|
+
res.set('Content-Length', metadata.size.toString());
|
|
64
|
+
}
|
|
34
65
|
}
|
|
35
|
-
else
|
|
36
|
-
|
|
66
|
+
else {
|
|
67
|
+
const contentType = metadata.isEncrypted ? 'application/octet-stream' : 'application/zip';
|
|
68
|
+
res.set('Content-Type', contentType);
|
|
37
69
|
}
|
|
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"`);
|
|
70
|
+
res.set('Content-Disposition', buildDisposition(req, fileName));
|
|
43
71
|
};
|
|
44
72
|
export const handleS3DownloadResponseHeaders = (req, res, metadata) => {
|
|
45
73
|
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.
|
|
4
|
+
"version": "1.6.2",
|
|
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.
|
|
51
|
-
"@autonomys/auto-dag-data": "^1.6.
|
|
52
|
-
"@autonomys/auto-utils": "^1.6.
|
|
51
|
+
"@autonomys/asynchronous": "^1.6.2",
|
|
52
|
+
"@autonomys/auto-dag-data": "^1.6.2",
|
|
53
|
+
"@autonomys/auto-utils": "^1.6.2",
|
|
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": "910aff6c92f736b87f7986780d667035b9c72217"
|
|
65
66
|
}
|