@payloadcms/storage-azure 3.78.0-internal.ab11ffa → 3.78.0
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"staticHandler.d.ts","sourceRoot":"","sources":["../src/staticHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"staticHandler.d.ts","sourceRoot":"","sources":["../src/staticHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAA;AAC3E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAO/C,UAAU,IAAI;IACZ,UAAU,EAAE,gBAAgB,CAAA;IAC5B,gBAAgB,EAAE,MAAM,eAAe,CAAA;CACxC;AAED,eAAO,MAAM,UAAU,qCAAsC,IAAI,KAAG,aAuGnE,CAAA"}
|
package/dist/staticHandler.js
CHANGED
|
@@ -2,31 +2,8 @@ import { RestError } from '@azure/storage-blob';
|
|
|
2
2
|
import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { getRangeRequestInfo } from 'payload/internal';
|
|
5
|
-
const isNodeReadableStream = (body)=>{
|
|
6
|
-
return typeof body === 'object' && body !== null && 'pipe' in body && typeof body.pipe === 'function' && 'destroy' in body && typeof body.destroy === 'function';
|
|
7
|
-
};
|
|
8
|
-
const abortRequestAndDestroyStream = ({ abortController, blob })=>{
|
|
9
|
-
try {
|
|
10
|
-
abortController.abort();
|
|
11
|
-
} catch {
|
|
12
|
-
/* noop */ }
|
|
13
|
-
if (blob?.readableStreamBody && isNodeReadableStream(blob.readableStreamBody)) {
|
|
14
|
-
blob.readableStreamBody.destroy();
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
5
|
export const getHandler = ({ collection, getStorageClient })=>{
|
|
18
6
|
return async (req, { headers: incomingHeaders, params: { clientUploadContext, filename } })=>{
|
|
19
|
-
let blob = undefined;
|
|
20
|
-
let streamed = false;
|
|
21
|
-
const abortController = new AbortController();
|
|
22
|
-
if (req.signal) {
|
|
23
|
-
req.signal.addEventListener('abort', ()=>{
|
|
24
|
-
abortRequestAndDestroyStream({
|
|
25
|
-
abortController,
|
|
26
|
-
blob
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
7
|
try {
|
|
31
8
|
const prefix = await getFilePrefix({
|
|
32
9
|
clientUploadContext,
|
|
@@ -56,11 +33,7 @@ export const getHandler = ({ collection, getStorageClient })=>{
|
|
|
56
33
|
});
|
|
57
34
|
}
|
|
58
35
|
// Download with range if partial
|
|
59
|
-
blob = rangeResult.type === 'partial' ? await blockBlobClient.download(rangeResult.rangeStart, rangeResult.rangeEnd - rangeResult.rangeStart + 1
|
|
60
|
-
abortSignal: abortController.signal
|
|
61
|
-
}) : await blockBlobClient.download(0, undefined, {
|
|
62
|
-
abortSignal: abortController.signal
|
|
63
|
-
});
|
|
36
|
+
const blob = rangeResult.type === 'partial' ? await blockBlobClient.download(rangeResult.rangeStart, rangeResult.rangeEnd - rangeResult.rangeStart + 1) : await blockBlobClient.download();
|
|
64
37
|
let headers = new Headers(incomingHeaders);
|
|
65
38
|
// Add range-related headers from the result
|
|
66
39
|
for (const [key, value] of Object.entries(rangeResult.headers)){
|
|
@@ -87,24 +60,25 @@ export const getHandler = ({ collection, getStorageClient })=>{
|
|
|
87
60
|
status: 304
|
|
88
61
|
});
|
|
89
62
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
63
|
+
// Manually create a ReadableStream for the web from a Node.js stream.
|
|
64
|
+
const readableStream = new ReadableStream({
|
|
65
|
+
start (controller) {
|
|
66
|
+
const nodeStream = blob.readableStreamBody;
|
|
67
|
+
if (!nodeStream) {
|
|
68
|
+
throw new Error('No readable stream body');
|
|
69
|
+
}
|
|
70
|
+
nodeStream.on('data', (chunk)=>{
|
|
71
|
+
controller.enqueue(new Uint8Array(chunk));
|
|
72
|
+
});
|
|
73
|
+
nodeStream.on('end', ()=>{
|
|
74
|
+
controller.close();
|
|
75
|
+
});
|
|
76
|
+
nodeStream.on('error', (err)=>{
|
|
77
|
+
controller.error(err);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
105
80
|
});
|
|
106
|
-
|
|
107
|
-
return new Response(stream, {
|
|
81
|
+
return new Response(readableStream, {
|
|
108
82
|
headers,
|
|
109
83
|
status: rangeResult.status
|
|
110
84
|
});
|
|
@@ -119,13 +93,6 @@ export const getHandler = ({ collection, getStorageClient })=>{
|
|
|
119
93
|
return new Response('Internal Server Error', {
|
|
120
94
|
status: 500
|
|
121
95
|
});
|
|
122
|
-
} finally{
|
|
123
|
-
if (!streamed) {
|
|
124
|
-
abortRequestAndDestroyStream({
|
|
125
|
-
abortController,
|
|
126
|
-
blob
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
96
|
}
|
|
130
97
|
};
|
|
131
98
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/staticHandler.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/staticHandler.ts"],"sourcesContent":["import type { ContainerClient } from '@azure/storage-blob'\nimport type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types'\nimport type { CollectionConfig } from 'payload'\n\nimport { RestError } from '@azure/storage-blob'\nimport { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities'\nimport path from 'path'\nimport { getRangeRequestInfo } from 'payload/internal'\n\ninterface Args {\n collection: CollectionConfig\n getStorageClient: () => ContainerClient\n}\n\nexport const getHandler = ({ collection, getStorageClient }: Args): StaticHandler => {\n return async (req, { headers: incomingHeaders, params: { clientUploadContext, filename } }) => {\n try {\n const prefix = await getFilePrefix({ clientUploadContext, collection, filename, req })\n const blockBlobClient = getStorageClient().getBlockBlobClient(\n path.posix.join(prefix, filename),\n )\n\n // Get file size for range validation\n const properties = await blockBlobClient.getProperties()\n const fileSize = properties.contentLength\n\n if (!fileSize) {\n return new Response('Internal Server Error', { status: 500 })\n }\n\n // Handle range request\n const rangeHeader = req.headers.get('range')\n const rangeResult = getRangeRequestInfo({ fileSize, rangeHeader })\n\n if (rangeResult.type === 'invalid') {\n return new Response(null, {\n headers: new Headers(rangeResult.headers),\n status: rangeResult.status,\n })\n }\n\n // Download with range if partial\n const blob =\n rangeResult.type === 'partial'\n ? await blockBlobClient.download(\n rangeResult.rangeStart,\n rangeResult.rangeEnd - rangeResult.rangeStart + 1,\n )\n : await blockBlobClient.download()\n\n let headers = new Headers(incomingHeaders)\n\n // Add range-related headers from the result\n for (const [key, value] of Object.entries(rangeResult.headers)) {\n headers.append(key, value)\n }\n\n // Add Azure-specific headers\n headers.append('Content-Type', String(properties.contentType))\n if (properties.etag) {\n headers.append('ETag', String(properties.etag))\n }\n\n // Add Content-Security-Policy header for SVG files to prevent executable code\n if (properties.contentType === 'image/svg+xml') {\n headers.append('Content-Security-Policy', \"script-src 'none'\")\n }\n\n const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match')\n\n if (\n collection.upload &&\n typeof collection.upload === 'object' &&\n typeof collection.upload.modifyResponseHeaders === 'function'\n ) {\n headers = collection.upload.modifyResponseHeaders({ headers }) || headers\n }\n\n if (etagFromHeaders && etagFromHeaders === properties.etag) {\n return new Response(null, {\n headers,\n status: 304,\n })\n }\n\n // Manually create a ReadableStream for the web from a Node.js stream.\n const readableStream = new ReadableStream({\n start(controller) {\n const nodeStream = blob.readableStreamBody\n if (!nodeStream) {\n throw new Error('No readable stream body')\n }\n\n nodeStream.on('data', (chunk) => {\n controller.enqueue(new Uint8Array(chunk))\n })\n nodeStream.on('end', () => {\n controller.close()\n })\n nodeStream.on('error', (err) => {\n controller.error(err)\n })\n },\n })\n\n return new Response(readableStream, {\n headers,\n status: rangeResult.status,\n })\n } catch (err: unknown) {\n if (err instanceof RestError && err.statusCode === 404) {\n return new Response(null, { status: 404, statusText: 'Not Found' })\n }\n req.payload.logger.error(err)\n return new Response('Internal Server Error', { status: 500 })\n }\n }\n}\n"],"names":["RestError","getFilePrefix","path","getRangeRequestInfo","getHandler","collection","getStorageClient","req","headers","incomingHeaders","params","clientUploadContext","filename","prefix","blockBlobClient","getBlockBlobClient","posix","join","properties","getProperties","fileSize","contentLength","Response","status","rangeHeader","get","rangeResult","type","Headers","blob","download","rangeStart","rangeEnd","key","value","Object","entries","append","String","contentType","etag","etagFromHeaders","upload","modifyResponseHeaders","readableStream","ReadableStream","start","controller","nodeStream","readableStreamBody","Error","on","chunk","enqueue","Uint8Array","close","err","error","statusCode","statusText","payload","logger"],"mappings":"AAIA,SAASA,SAAS,QAAQ,sBAAqB;AAC/C,SAASC,aAAa,QAAQ,6CAA4C;AAC1E,OAAOC,UAAU,OAAM;AACvB,SAASC,mBAAmB,QAAQ,mBAAkB;AAOtD,OAAO,MAAMC,aAAa,CAAC,EAAEC,UAAU,EAAEC,gBAAgB,EAAQ;IAC/D,OAAO,OAAOC,KAAK,EAAEC,SAASC,eAAe,EAAEC,QAAQ,EAAEC,mBAAmB,EAAEC,QAAQ,EAAE,EAAE;QACxF,IAAI;YACF,MAAMC,SAAS,MAAMZ,cAAc;gBAAEU;gBAAqBN;gBAAYO;gBAAUL;YAAI;YACpF,MAAMO,kBAAkBR,mBAAmBS,kBAAkB,CAC3Db,KAAKc,KAAK,CAACC,IAAI,CAACJ,QAAQD;YAG1B,qCAAqC;YACrC,MAAMM,aAAa,MAAMJ,gBAAgBK,aAAa;YACtD,MAAMC,WAAWF,WAAWG,aAAa;YAEzC,IAAI,CAACD,UAAU;gBACb,OAAO,IAAIE,SAAS,y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C,qBAAqB,CAAC;oBAAEnC;gBAAQ,MAAMA;YACpE;YAEA,IAAIiC,mBAAmBA,oBAAoBvB,WAAWsB,IAAI,EAAE;gBAC1D,OAAO,IAAIlB,SAAS,MAAM;oBACxBd;oBACAe,QAAQ;gBACV;YACF;YAEA,sEAAsE;YACtE,MAAMqB,iBAAiB,IAAIC,eAAe;gBACxCC,OAAMC,UAAU;oBACd,MAAMC,aAAanB,KAAKoB,kBAAkB;oBAC1C,IAAI,CAACD,YAAY;wBACf,MAAM,IAAIE,MAAM;oBAClB;oBAEAF,WAAWG,EAAE,CAAC,QAAQ,CAACC;wBACrBL,WAAWM,OAAO,CAAC,IAAIC,WAAWF;oBACpC;oBACAJ,WAAWG,EAAE,CAAC,OAAO;wBACnBJ,WAAWQ,KAAK;oBAClB;oBACAP,WAAWG,EAAE,CAAC,SAAS,CAACK;wBACtBT,WAAWU,KAAK,CAACD;oBACnB;gBACF;YACF;YAEA,OAAO,IAAIlC,SAASsB,gBAAgB;gBAClCpC;gBACAe,QAAQG,YAAYH,MAAM;YAC5B;QACF,EAAE,OAAOiC,KAAc;YACrB,IAAIA,eAAexD,aAAawD,IAAIE,UAAU,KAAK,KAAK;gBACtD,OAAO,IAAIpC,SAAS,MAAM;oBAAEC,QAAQ;oBAAKoC,YAAY;gBAAY;YACnE;YACApD,IAAIqD,OAAO,CAACC,MAAM,CAACJ,KAAK,CAACD;YACzB,OAAO,IAAIlC,SAAS,yBAAyB;gBAAEC,QAAQ;YAAI;QAC7D;IACF;AACF,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/storage-azure",
|
|
3
|
-
"version": "3.78.0
|
|
3
|
+
"version": "3.78.0",
|
|
4
4
|
"description": "Payload storage adapter for Azure Blob Storage",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@azure/abort-controller": "^1.1.0",
|
|
41
41
|
"@azure/storage-blob": "^12.11.0",
|
|
42
|
-
"@payloadcms/plugin-cloud-storage": "3.78.0
|
|
42
|
+
"@payloadcms/plugin-cloud-storage": "3.78.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"payload": "3.78.0
|
|
45
|
+
"payload": "3.78.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"payload": "3.78.0
|
|
48
|
+
"payload": "3.78.0"
|
|
49
49
|
},
|
|
50
50
|
"engines": {
|
|
51
51
|
"node": "^18.20.2 || >=20.9.0"
|