-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (94 loc) · 3.61 KB
/
index.js
File metadata and controls
108 lines (94 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { relative, join } from 'node:path';
import fs from 'node:fs';
import { BlobServiceClient } from '@azure/storage-blob';
import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import readdir from 'recursive-readdir';
import pLimit from 'p-limit';
import mime from 'mime-types';
import intel from 'intel';
import { throwIfMissing } from '../../support/util.js';
const log = intel.getLogger('sitespeedio.plugin.azure');
async function upload(dir, azureOptions, prefix) {
const blobServiceClient = BlobServiceClient.fromConnectionString(
azureOptions.connectionString
);
const containerClient = blobServiceClient.getContainerClient(
azureOptions.containerName
);
await containerClient.createIfNotExists();
const files = await readdir(dir);
const limit = pLimit(azureOptions.maxAsyncAzure || 20);
const promises = [];
for (let file of files) {
promises.push(
limit(() => uploadFile(file, containerClient, azureOptions, prefix, dir))
);
}
return Promise.all(promises);
}
async function uploadFile(
file,
containerClient,
azureOptions,
prefix,
baseDir
) {
const subPath = relative(baseDir, file);
const blobName = join(azureOptions.path || prefix, subPath);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const data = fs.readFileSync(file);
const contentType = mime.lookup(file) || 'application/octet-stream';
const options = { blobHTTPHeaders: { blobContentType: contentType } };
// add custom parameters if any
Object.assign(options, azureOptions.params);
await blockBlobClient.upload(data, data.length, options);
log.info(`Uploaded file to Azure Blob Storage: ${blobName}`);
}
export default class AzurePlugin extends SitespeedioPlugin {
constructor(options, context, queue) {
super({ name: 'azure', options, context, queue });
}
open(context, options) {
this.azureOptions = options.azure;
this.options = options;
this.make = context.messageMaker('azure').make;
throwIfMissing(
this.azureOptions,
['containerName', 'connectionString'],
'azure'
);
this.storageManager = context.storageManager;
}
async processMessage(message, queue) {
if (message.type === 'sitespeedio.setup') {
queue.postMessage(this.make('azure.setup'));
} else if (message.type === 'html.finished') {
const make = this.make;
const azureOptions = this.azureOptions;
const baseDir = this.storageManager.getBaseDir();
log.info(
`Uploading ${baseDir} to Azure Blob Storage container ${azureOptions.containerName}, this can take a while ...`
);
try {
await upload(
baseDir,
azureOptions,
this.storageManager.getStoragePrefix()
);
log.info('Finished upload to Azure Blob Storage');
if (azureOptions.removeLocalResult) {
fs.rmSync(baseDir, { recursive: true });
log.debug(`Removed local files and directory ${baseDir}`);
} else {
log.debug(
`Local result files and directories are stored in ${baseDir}`
);
}
} catch (error) {
queue.postMessage(make('error', error));
log.error('Could not upload to Azure Blob Storage', error);
}
queue.postMessage(make('azure.finished'));
}
}
}