@lighthouse/common 6.2.0-canary.32 → 6.2.0-canary.35
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/helpers/build-fetch-url/index.js +19 -9
- package/dist/helpers/fetch-image/index.js +0 -7
- package/dist/helpers/fetch-image-for-pdf-generator-service/index.js +42 -69
- package/dist/helpers/fetch-image-for-web/index.js +4 -18
- package/dist/helpers/get-audit-items-data/index.js +15 -4
- package/dist/pdf/audit/index.js +5 -5
- package/dist/pdf/helpers/build-audit-content/index.js +0 -9
- package/lib/helpers/build-fetch-url/index.js +30 -9
- package/lib/helpers/build-fetch-url/index.js.map +1 -1
- package/lib/helpers/fetch-image/index.js +0 -7
- package/lib/helpers/fetch-image/index.js.map +1 -1
- package/lib/helpers/fetch-image-for-pdf-generator-service/index.js +43 -70
- package/lib/helpers/fetch-image-for-pdf-generator-service/index.js.map +1 -1
- package/lib/helpers/fetch-image-for-web/index.js +10 -29
- package/lib/helpers/fetch-image-for-web/index.js.map +1 -1
- package/lib/helpers/get-audit-items-data/index.js +14 -6
- package/lib/helpers/get-audit-items-data/index.js.map +1 -1
- package/lib/pdf/audit/index.js +4 -5
- package/lib/pdf/audit/index.js.map +1 -1
- package/lib/pdf/helpers/build-audit-content/index.js +0 -9
- package/lib/pdf/helpers/build-audit-content/index.js.map +1 -1
- package/package.json +15 -8
|
@@ -13,17 +13,27 @@ function buildFetchUrl(url, options) {
|
|
|
13
13
|
height,
|
|
14
14
|
width,
|
|
15
15
|
quality,
|
|
16
|
-
shouldUseCloudfront
|
|
16
|
+
shouldUseCloudfront,
|
|
17
|
+
Policy,
|
|
18
|
+
KeyPairId,
|
|
19
|
+
Signature
|
|
17
20
|
} = options;
|
|
18
21
|
if (shouldUseCloudfront) {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
const isWebContext = shouldUseCloudfront && typeof window === 'object';
|
|
23
|
+
const paramMap = {
|
|
24
|
+
width,
|
|
25
|
+
height,
|
|
26
|
+
quality,
|
|
27
|
+
fit: 'contain'
|
|
28
|
+
};
|
|
29
|
+
if (isWebContext) {
|
|
30
|
+
paramMap.Policy = Policy;
|
|
31
|
+
paramMap['Key-Pair-Id'] = KeyPairId;
|
|
32
|
+
paramMap.Signature = Signature;
|
|
33
|
+
}
|
|
34
|
+
const params = Object.entries(paramMap).filter(([, value]) => value != null).map(([key, value]) => `${key}=${String(value)}`);
|
|
35
|
+
const paramsString = params.join('&');
|
|
36
|
+
return `${cloudfrontBaseUrl}/${url}?${paramsString}`;
|
|
27
37
|
}
|
|
28
38
|
const transformations = [];
|
|
29
39
|
let transformationsString = '';
|
|
@@ -40,9 +40,6 @@ function fetchImage(url, options = {}) {
|
|
|
40
40
|
const {
|
|
41
41
|
shouldUseCloudfront
|
|
42
42
|
} = options;
|
|
43
|
-
console.log({
|
|
44
|
-
options
|
|
45
|
-
});
|
|
46
43
|
if (shouldUseCloudfront) {
|
|
47
44
|
const isWebContext = typeof window === 'object';
|
|
48
45
|
return isWebContext ?
|
|
@@ -57,10 +54,6 @@ function fetchImage(url, options = {}) {
|
|
|
57
54
|
const {
|
|
58
55
|
isHeader = false
|
|
59
56
|
} = options;
|
|
60
|
-
console.log('FETCHING SOME IMAGE,:', {
|
|
61
|
-
url,
|
|
62
|
-
options
|
|
63
|
-
});
|
|
64
57
|
return fetch(encodedUrl, fetchOptions).then(response => {
|
|
65
58
|
const contentHeader = response.headers.get('content-length');
|
|
66
59
|
const contentType = response.headers.get('content-type');
|
|
@@ -29,24 +29,21 @@ const fetchImageForPdfGeneratorService = async function (url) {
|
|
|
29
29
|
}
|
|
30
30
|
const {
|
|
31
31
|
path,
|
|
32
|
-
|
|
32
|
+
queryString
|
|
33
33
|
} = parseUrlString(url);
|
|
34
|
-
const
|
|
35
|
-
console.log('FETCHING IMAGE FROM S3');
|
|
34
|
+
const transformedBucketImagePath = path + (queryString ? `/${queryString}` : '');
|
|
36
35
|
const alreadyTransformedImage = await fetchResourceFromS3({
|
|
37
36
|
bucketName: `${process.env.S3_BUCKET_IMAGE_TRANSFORMER_CACHE}`,
|
|
38
|
-
key:
|
|
37
|
+
key: transformedBucketImagePath
|
|
39
38
|
});
|
|
40
39
|
if (alreadyTransformedImage?.body) {
|
|
41
|
-
console.log('IMAGE FOUND TO BE ALREADY TRANSFORMED');
|
|
42
40
|
const fullDataUrl = formatBase64Image({
|
|
43
41
|
base64String: alreadyTransformedImage.body.toString('base64'),
|
|
44
|
-
key:
|
|
42
|
+
key: transformedBucketImagePath
|
|
45
43
|
});
|
|
46
|
-
console.log('FULL DATA URL? ', JSON.stringify(fullDataUrl).substring(0, 50));
|
|
47
44
|
return fullDataUrl;
|
|
48
45
|
}
|
|
49
|
-
const transformerResponse = await requestImageTransformation(
|
|
46
|
+
const transformerResponse = await requestImageTransformation(url);
|
|
50
47
|
const statusCode = transformerResponse.statusCode;
|
|
51
48
|
switch (statusCode) {
|
|
52
49
|
case 200:
|
|
@@ -61,20 +58,20 @@ const fetchImageForPdfGeneratorService = async function (url) {
|
|
|
61
58
|
case 302:
|
|
62
59
|
{
|
|
63
60
|
console.debug('Image transformation successful but image is too big for lambda delivery, fetching directly from S3');
|
|
64
|
-
const redirectLocation = transformerResponse.headers
|
|
65
|
-
if (redirectLocation) {
|
|
66
|
-
|
|
67
|
-
bucketName: `${process.env.S3_BUCKET_IMAGE_TRANSFORMER_CACHE}`,
|
|
68
|
-
key: transformedBucketPath
|
|
69
|
-
});
|
|
70
|
-
console.debug('Image successfully fetched from S3 at:', redirectLocation);
|
|
71
|
-
const fullDataUrl = formatBase64Image({
|
|
72
|
-
base64String: newlyTransformedImage.body.toString('base64'),
|
|
73
|
-
key: transformedBucketPath
|
|
74
|
-
});
|
|
75
|
-
return fullDataUrl;
|
|
61
|
+
const redirectLocation = transformerResponse.headers.Location;
|
|
62
|
+
if (!redirectLocation) {
|
|
63
|
+
throw new Error('Redirect response received but no location provided');
|
|
76
64
|
}
|
|
77
|
-
|
|
65
|
+
const newlyTransformedImage = await fetchResourceFromS3({
|
|
66
|
+
bucketName: `${process.env.S3_BUCKET_IMAGE_TRANSFORMER_CACHE}`,
|
|
67
|
+
key: transformedBucketImagePath
|
|
68
|
+
});
|
|
69
|
+
console.debug('Image successfully fetched from S3 at:', redirectLocation);
|
|
70
|
+
const fullDataUrl = formatBase64Image({
|
|
71
|
+
base64String: newlyTransformedImage.body.toString('base64'),
|
|
72
|
+
key: transformedBucketImagePath
|
|
73
|
+
});
|
|
74
|
+
return fullDataUrl;
|
|
78
75
|
}
|
|
79
76
|
case 400:
|
|
80
77
|
throw new Error(`Bad request to image transformer: ${transformerResponse.body}`);
|
|
@@ -89,30 +86,16 @@ const fetchImageForPdfGeneratorService = async function (url) {
|
|
|
89
86
|
}
|
|
90
87
|
};
|
|
91
88
|
exports.fetchImageForPdfGeneratorService = fetchImageForPdfGeneratorService;
|
|
92
|
-
async function requestImageTransformation(
|
|
93
|
-
if (!
|
|
94
|
-
throw new Error('Image
|
|
95
|
-
}
|
|
96
|
-
console.debug('ImageTransformation: Invoking image transformer lambda for URL:', url);
|
|
97
|
-
const {
|
|
98
|
-
path,
|
|
99
|
-
queryParams
|
|
100
|
-
} = parseUrlString(url);
|
|
101
|
-
const queryParamsObject = {};
|
|
102
|
-
if (queryParams) {
|
|
103
|
-
queryParams.split(',').forEach(param => {
|
|
104
|
-
const [key, value] = param.split('=');
|
|
105
|
-
if (key && value) {
|
|
106
|
-
queryParamsObject[key] = value;
|
|
107
|
-
}
|
|
108
|
-
});
|
|
89
|
+
async function requestImageTransformation(key) {
|
|
90
|
+
if (!key) {
|
|
91
|
+
throw new Error('Image Path is required for image transformation');
|
|
109
92
|
}
|
|
93
|
+
console.debug('ImageTransformation: Invoking image transformer lambda for path:', key);
|
|
110
94
|
const lambdaEvent = {
|
|
111
|
-
queryStringParameters: Object.keys(queryParamsObject).length > 0 ? queryParamsObject : null,
|
|
112
95
|
requestContext: {
|
|
113
96
|
http: {
|
|
114
97
|
method: 'GET',
|
|
115
|
-
path:
|
|
98
|
+
path: key
|
|
116
99
|
}
|
|
117
100
|
}
|
|
118
101
|
};
|
|
@@ -148,9 +131,6 @@ async function fetchResourceFromS3({
|
|
|
148
131
|
};
|
|
149
132
|
try {
|
|
150
133
|
const result = await s3.getObject(params).promise();
|
|
151
|
-
console.debug(JSON.stringify({
|
|
152
|
-
result
|
|
153
|
-
}, null, 2));
|
|
154
134
|
return {
|
|
155
135
|
body: result.Body,
|
|
156
136
|
contentType: result.ContentType,
|
|
@@ -187,42 +167,35 @@ function formatBase64Image({
|
|
|
187
167
|
|
|
188
168
|
/**
|
|
189
169
|
* Parses a URL-like string into path and query parameters
|
|
190
|
-
* @param {string} urlString - String like "
|
|
191
|
-
* @returns {Object} - Object with { path: string,
|
|
170
|
+
* @param {string} urlString - String like "https://example.cloudfront.net/<applicationId>/<path>/filename.jpeg?width=100&height=100"
|
|
171
|
+
* @returns {Object} - Object with { path: string, queryString: string }
|
|
192
172
|
* @example
|
|
193
|
-
* parseUrlString("abc123/folder/image.jpeg?width=100&height=100")
|
|
194
|
-
* // Returns: { path: "abc123/folder/image.jpeg",
|
|
173
|
+
* parseUrlString("https://example.cloudfront.net/abc123/folder/image.jpeg?width=100&height=100")
|
|
174
|
+
* // Returns: { path: "abc123/folder/image.jpeg", queryString: "width=100&t=456" }
|
|
195
175
|
*/
|
|
196
176
|
function parseUrlString(urlString) {
|
|
197
177
|
if (!urlString || typeof urlString !== 'string') {
|
|
198
178
|
throw new Error('URL string is required and must be a string');
|
|
199
179
|
}
|
|
200
180
|
|
|
201
|
-
//
|
|
202
|
-
const
|
|
203
|
-
const
|
|
181
|
+
// Extract the path after the domain
|
|
182
|
+
const url = new URL(urlString);
|
|
183
|
+
const pathname = url.pathname;
|
|
184
|
+
// Remove leading slash if present
|
|
185
|
+
const pathWithoutLeadingSlash = pathname.startsWith('/') ? pathname.slice(1) : pathname;
|
|
186
|
+
|
|
187
|
+
// Get everything after the domain (path + query)
|
|
188
|
+
const fullPath = pathWithoutLeadingSlash + url.search;
|
|
189
|
+
let [path, queryString] = fullPath.split('?');
|
|
190
|
+
|
|
191
|
+
// change query string to ve seperated by commas instead of &
|
|
192
|
+
|
|
193
|
+
queryString = queryString ? queryString.replace(/&/g, ',') : '';
|
|
204
194
|
if (!path) {
|
|
205
195
|
throw new Error('Invalid URL string: missing path component');
|
|
206
196
|
}
|
|
207
|
-
|
|
208
|
-
// If there are no query parameters, return path with empty queryParams
|
|
209
|
-
if (!queryString) {
|
|
210
|
-
return {
|
|
211
|
-
path: path,
|
|
212
|
-
queryParams: ''
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Parse query parameters and convert to comma-separated format
|
|
217
|
-
const queryParams = queryString.split('&').map(param => {
|
|
218
|
-
// Handle parameters that might not have '=' (though uncommon)
|
|
219
|
-
if (!param.includes('=')) {
|
|
220
|
-
return param;
|
|
221
|
-
}
|
|
222
|
-
return param;
|
|
223
|
-
}).join(',');
|
|
224
197
|
return {
|
|
225
|
-
path
|
|
226
|
-
|
|
198
|
+
path,
|
|
199
|
+
queryString
|
|
227
200
|
};
|
|
228
201
|
}
|
|
@@ -6,12 +6,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.fetchImageForWeb = void 0;
|
|
7
7
|
var _images = require("../../images");
|
|
8
8
|
var _fetchLighthouseLogo = require("../fetch-lighthouse-logo");
|
|
9
|
-
var _fetchImage = require("../fetch-image");
|
|
10
|
-
var _imageValidators = require("../image-validators");
|
|
11
|
-
const contentTypes = {
|
|
12
|
-
'image/png': 'png',
|
|
13
|
-
'image/jpeg': 'jpeg'
|
|
14
|
-
};
|
|
15
9
|
const fetchImageForWeb = async function (url, options) {
|
|
16
10
|
const {
|
|
17
11
|
isHeader = false,
|
|
@@ -21,26 +15,18 @@ const fetchImageForWeb = async function (url, options) {
|
|
|
21
15
|
} = options;
|
|
22
16
|
try {
|
|
23
17
|
const firstParamConnector = url.indexOf('?') > -1 ? '&' : '?';
|
|
24
|
-
const
|
|
25
|
-
const
|
|
18
|
+
const hasSignatureParams = url.includes('Signature=') && url.includes('Policy=') && url.includes('Key-Pair-Id=');
|
|
19
|
+
const constructedUrl = !hasSignatureParams ? `${url}${firstParamConnector}Signature=${Signature}&Policy=${Policy}&Key-Pair-Id=${KeyPairId}` : url;
|
|
26
20
|
console.debug('Fetching image via CloudFront For Web');
|
|
27
|
-
const imageResponse = await fetch(
|
|
21
|
+
const imageResponse = await fetch(constructedUrl);
|
|
28
22
|
const contentLengthHeader = imageResponse.headers.get('content-length');
|
|
29
|
-
const contentType = imageResponse.headers.get('content-type');
|
|
30
23
|
if (contentLengthHeader === '0') {
|
|
31
24
|
return Promise.reject(new Error(`Failed to fetch image as no content length: ${encodedUrl}`));
|
|
32
25
|
}
|
|
33
26
|
if (!imageResponse.ok) {
|
|
34
27
|
return Promise.reject(new Error(`Failed to fetch image: ${encodedUrl}`));
|
|
35
28
|
}
|
|
36
|
-
|
|
37
|
-
const logoArrayBuffer = await imageResponse.arrayBuffer();
|
|
38
|
-
const base64Flag = `data:image/${imageType};base64,`;
|
|
39
|
-
const imageStr = (0, _fetchImage.arrayBufferToBase64)(logoArrayBuffer);
|
|
40
|
-
const base64 = `${base64Flag}${imageStr}`;
|
|
41
|
-
const isValid = (0, _imageValidators.validateBase64Image)(base64);
|
|
42
|
-
if (!isValid) throw new Error('InvalidImageError');
|
|
43
|
-
return base64;
|
|
29
|
+
return await imageResponse.arrayBuffer();
|
|
44
30
|
} catch (error) {
|
|
45
31
|
if (isHeader) {
|
|
46
32
|
// NOTE: Replace failed headers with LH logo
|
|
@@ -12,6 +12,9 @@ function getAuditItemsData(items, data) {
|
|
|
12
12
|
awsS3BaseUrl,
|
|
13
13
|
cloudinaryBaseUrl,
|
|
14
14
|
cloudfrontBaseUrl,
|
|
15
|
+
Policy,
|
|
16
|
+
KeyPairId,
|
|
17
|
+
Signature,
|
|
15
18
|
shouldUseCloudfront
|
|
16
19
|
} = {},
|
|
17
20
|
entity: {
|
|
@@ -55,11 +58,14 @@ function getAuditItemsData(items, data) {
|
|
|
55
58
|
awsS3BaseUrl,
|
|
56
59
|
cloudfrontBaseUrl,
|
|
57
60
|
cloudinaryBaseUrl,
|
|
58
|
-
shouldUseCloudfront,
|
|
59
|
-
fit: true,
|
|
60
61
|
height: 400,
|
|
61
62
|
width: 600,
|
|
62
|
-
quality: 50
|
|
63
|
+
quality: 50,
|
|
64
|
+
fit: true,
|
|
65
|
+
shouldUseCloudfront,
|
|
66
|
+
Policy,
|
|
67
|
+
KeyPairId,
|
|
68
|
+
Signature
|
|
63
69
|
});
|
|
64
70
|
const link = `${awsS3BaseUrl}/${asset}`;
|
|
65
71
|
const thumbnailUrl = (0, _.buildFetchUrl)(asset, {
|
|
@@ -68,7 +74,12 @@ function getAuditItemsData(items, data) {
|
|
|
68
74
|
cloudinaryBaseUrl,
|
|
69
75
|
shouldUseCloudfront,
|
|
70
76
|
width: 100,
|
|
71
|
-
quality: 50
|
|
77
|
+
quality: 50,
|
|
78
|
+
fit: true,
|
|
79
|
+
shouldUseCloudfront,
|
|
80
|
+
Policy,
|
|
81
|
+
KeyPairId,
|
|
82
|
+
Signature
|
|
72
83
|
});
|
|
73
84
|
const key = `${groupIndex}-item-asset-${assetIndex}`;
|
|
74
85
|
return {
|
package/dist/pdf/audit/index.js
CHANGED
|
@@ -39,11 +39,14 @@ function buildAuditPdf(pdfOptions, data) {
|
|
|
39
39
|
entity,
|
|
40
40
|
timezone
|
|
41
41
|
} = data;
|
|
42
|
+
const {
|
|
43
|
+
flags = {}
|
|
44
|
+
} = pdfOptions;
|
|
42
45
|
const sequenceId = entity.sequenceId;
|
|
43
46
|
const timestamp = entity.createdAt;
|
|
44
47
|
const title = entity.title || 'Unknown';
|
|
45
48
|
const fileTitle = `Audit Report - ${title}`;
|
|
46
|
-
return generateContent(data).then(content => (0, _helpers.generateDefinition)({
|
|
49
|
+
return generateContent(data, flags).then(content => (0, _helpers.generateDefinition)({
|
|
47
50
|
content,
|
|
48
51
|
fileTitle,
|
|
49
52
|
sequenceId,
|
|
@@ -59,9 +62,6 @@ function generateContent(data) {
|
|
|
59
62
|
const {
|
|
60
63
|
entity
|
|
61
64
|
} = data;
|
|
62
|
-
console.log('GENERATE AUDIT CONTENT:', JSON.stringify({
|
|
63
|
-
data
|
|
64
|
-
}, null, 2));
|
|
65
65
|
const {
|
|
66
66
|
followUps = [],
|
|
67
67
|
footerFields = {},
|
|
@@ -196,7 +196,7 @@ function generateContent(data) {
|
|
|
196
196
|
timezone
|
|
197
197
|
});
|
|
198
198
|
const promises = {
|
|
199
|
-
entry: (0, _helpers.buildAuditContent)(groupedData.items
|
|
199
|
+
entry: (0, _helpers.buildAuditContent)(groupedData.items),
|
|
200
200
|
footerTemplate: (0, _helpers.buildTemplateContent)(footerFields.formGroups, data),
|
|
201
201
|
headerTemplate: (0, _helpers.buildTemplateContent)(headerFields.formGroups, data)
|
|
202
202
|
};
|
|
@@ -11,21 +11,12 @@ var _helpers = require("../../../helpers");
|
|
|
11
11
|
var _ = require("../");
|
|
12
12
|
var _table = require("../table");
|
|
13
13
|
const buildAuditContent = exports.buildAuditContent = _bluebird.default.method((items, settings = {}) => {
|
|
14
|
-
console.log(JSON.stringify({
|
|
15
|
-
items,
|
|
16
|
-
settings
|
|
17
|
-
}, null, 2));
|
|
18
14
|
return _bluebird.default.map(items, group => {
|
|
19
15
|
return _bluebird.default.map(group.items, (item, index) => {
|
|
20
16
|
return _bluebird.default.map(item.assets, ({
|
|
21
17
|
assetUrl,
|
|
22
18
|
link
|
|
23
19
|
}) => {
|
|
24
|
-
console.log(JSON.stringify({
|
|
25
|
-
assetUrl,
|
|
26
|
-
link,
|
|
27
|
-
items
|
|
28
|
-
}, null, 2));
|
|
29
20
|
// NOTE: Signature, Policy and KeyPairId are the only used values from settings in this context
|
|
30
21
|
return (0, _helpers.fetchImage)(assetUrl, settings).then(base64String => ({
|
|
31
22
|
alignment: 'center',
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import _typeof from "@babel/runtime/helpers/typeof";
|
|
1
3
|
export function buildFetchUrl(url, options) {
|
|
2
4
|
var awsS3BaseUrl = options.awsS3BaseUrl,
|
|
3
5
|
_options$cloudfrontBa = options.cloudfrontBaseUrl,
|
|
@@ -7,16 +9,35 @@ export function buildFetchUrl(url, options) {
|
|
|
7
9
|
height = options.height,
|
|
8
10
|
width = options.width,
|
|
9
11
|
quality = options.quality,
|
|
10
|
-
shouldUseCloudfront = options.shouldUseCloudfront
|
|
12
|
+
shouldUseCloudfront = options.shouldUseCloudfront,
|
|
13
|
+
Policy = options.Policy,
|
|
14
|
+
KeyPairId = options.KeyPairId,
|
|
15
|
+
Signature = options.Signature;
|
|
11
16
|
if (shouldUseCloudfront) {
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
var isWebContext = shouldUseCloudfront && (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object';
|
|
18
|
+
var paramMap = {
|
|
19
|
+
width: width,
|
|
20
|
+
height: height,
|
|
21
|
+
quality: quality,
|
|
22
|
+
fit: 'contain'
|
|
23
|
+
};
|
|
24
|
+
if (isWebContext) {
|
|
25
|
+
paramMap.Policy = Policy;
|
|
26
|
+
paramMap['Key-Pair-Id'] = KeyPairId;
|
|
27
|
+
paramMap.Signature = Signature;
|
|
28
|
+
}
|
|
29
|
+
var params = Object.entries(paramMap).filter(function (_ref) {
|
|
30
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
31
|
+
value = _ref2[1];
|
|
32
|
+
return value != null;
|
|
33
|
+
}).map(function (_ref3) {
|
|
34
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
35
|
+
key = _ref4[0],
|
|
36
|
+
value = _ref4[1];
|
|
37
|
+
return "".concat(key, "=").concat(String(value));
|
|
38
|
+
});
|
|
39
|
+
var paramsString = params.join('&');
|
|
40
|
+
return "".concat(cloudfrontBaseUrl, "/").concat(url, "?").concat(paramsString);
|
|
20
41
|
}
|
|
21
42
|
var transformations = [];
|
|
22
43
|
var transformationsString = '';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["buildFetchUrl","url","options","awsS3BaseUrl","_options$cloudfrontBa","cloudfrontBaseUrl","cloudinaryBaseUrl","fit","height","width","quality","shouldUseCloudfront","
|
|
1
|
+
{"version":3,"file":"index.js","names":["buildFetchUrl","url","options","awsS3BaseUrl","_options$cloudfrontBa","cloudfrontBaseUrl","cloudinaryBaseUrl","fit","height","width","quality","shouldUseCloudfront","Policy","KeyPairId","Signature","isWebContext","window","_typeof","paramMap","params","Object","entries","filter","_ref","_ref2","_slicedToArray","value","map","_ref3","_ref4","key","concat","String","paramsString","join","transformations","transformationsString","push","toString","fetchUrl"],"sources":["../../../src/helpers/build-fetch-url/index.js"],"sourcesContent":["export function buildFetchUrl(url, options) {\n const {\n awsS3BaseUrl,\n cloudfrontBaseUrl = '',\n cloudinaryBaseUrl,\n fit,\n height,\n width,\n quality,\n shouldUseCloudfront,\n Policy,\n KeyPairId,\n Signature,\n } = options\n\n if (shouldUseCloudfront) {\n const isWebContext = shouldUseCloudfront && typeof window === 'object'\n\n const paramMap = {\n width,\n height,\n quality,\n fit: 'contain',\n }\n\n if (isWebContext) {\n paramMap.Policy = Policy\n paramMap['Key-Pair-Id'] = KeyPairId\n paramMap.Signature = Signature\n }\n const params = Object.entries(paramMap)\n .filter(([, value]) => value != null)\n .map(([key, value]) => `${key}=${String(value)}`)\n\n const paramsString = params.join('&')\n return `${cloudfrontBaseUrl}/${url}?${paramsString}`\n }\n\n const transformations = []\n let transformationsString = ''\n\n if (width) transformations.push(`w_${width.toString()}`)\n if (height) transformations.push(`h_${height.toString()}`)\n if (quality) transformations.push(`q_${quality.toString()}`)\n if (fit) transformations.push('c_fit')\n\n transformationsString = `${transformations.join(',')}/`\n\n const fetchUrl = `${cloudinaryBaseUrl}/${transformationsString}${awsS3BaseUrl}/${url}`\n\n return fetchUrl\n}\n"],"mappings":";;AAAA,OAAO,SAASA,aAAaA,CAACC,GAAG,EAAEC,OAAO,EAAE;EAC1C,IACEC,YAAY,GAWVD,OAAO,CAXTC,YAAY;IAAAC,qBAAA,GAWVF,OAAO,CAVTG,iBAAiB;IAAjBA,iBAAiB,GAAAD,qBAAA,cAAG,EAAE,GAAAA,qBAAA;IACtBE,iBAAiB,GASfJ,OAAO,CATTI,iBAAiB;IACjBC,GAAG,GAQDL,OAAO,CARTK,GAAG;IACHC,MAAM,GAOJN,OAAO,CAPTM,MAAM;IACNC,KAAK,GAMHP,OAAO,CANTO,KAAK;IACLC,OAAO,GAKLR,OAAO,CALTQ,OAAO;IACPC,mBAAmB,GAIjBT,OAAO,CAJTS,mBAAmB;IACnBC,MAAM,GAGJV,OAAO,CAHTU,MAAM;IACNC,SAAS,GAEPX,OAAO,CAFTW,SAAS;IACTC,SAAS,GACPZ,OAAO,CADTY,SAAS;EAGX,IAAIH,mBAAmB,EAAE;IACvB,IAAMI,YAAY,GAAGJ,mBAAmB,IAAI,QAAOK,MAAM,iCAAAC,OAAA,CAAND,MAAM,OAAK,QAAQ;IAEtE,IAAME,QAAQ,GAAG;MACfT,KAAK,EAALA,KAAK;MACLD,MAAM,EAANA,MAAM;MACNE,OAAO,EAAPA,OAAO;MACPH,GAAG,EAAE;IACP,CAAC;IAED,IAAIQ,YAAY,EAAE;MAChBG,QAAQ,CAACN,MAAM,GAAGA,MAAM;MACxBM,QAAQ,CAAC,aAAa,CAAC,GAAGL,SAAS;MACnCK,QAAQ,CAACJ,SAAS,GAAGA,SAAS;IAChC;IACA,IAAMK,MAAM,GAAGC,MAAM,CAACC,OAAO,CAACH,QAAQ,CAAC,CACpCI,MAAM,CAAC,UAA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Q;AACjB","ignoreList":[]}
|
|
@@ -33,9 +33,6 @@ var defaultOptions = {
|
|
|
33
33
|
export function fetchImage(url) {
|
|
34
34
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
35
35
|
var shouldUseCloudfront = options.shouldUseCloudfront;
|
|
36
|
-
console.log({
|
|
37
|
-
options: options
|
|
38
|
-
});
|
|
39
36
|
if (shouldUseCloudfront) {
|
|
40
37
|
var isWebContext = (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object';
|
|
41
38
|
return isWebContext ?
|
|
@@ -46,10 +43,6 @@ export function fetchImage(url) {
|
|
|
46
43
|
var fetchOptions = _objectSpread(_objectSpread({}, defaultOptions), options);
|
|
47
44
|
var _options$isHeader = options.isHeader,
|
|
48
45
|
isHeader = _options$isHeader === void 0 ? false : _options$isHeader;
|
|
49
|
-
console.log('FETCHING SOME IMAGE,:', {
|
|
50
|
-
url: url,
|
|
51
|
-
options: options
|
|
52
|
-
});
|
|
53
46
|
return fetch(encodedUrl, fetchOptions).then(function (response) {
|
|
54
47
|
var contentHeader = response.headers.get('content-length');
|
|
55
48
|
var contentType = response.headers.get('content-type');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["atob","btoa","fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","fetchImageForPdfGeneratorService","fetchImageForWeb","fetch","self","_typeof","contentTypes","defaultOptions","cache","fetchImage","url","options","arguments","length","undefined","shouldUseCloudfront","console","log","isWebContext","window","encodedUrl","encodeURI","fetchOptions","_objectSpread","_options$isHeader","isHeader","then","response","contentHeader","headers","get","contentType","reject","Error","concat","ok","imageType","arrayBuffer","buffer","_ref","base64Flag","imageStr","arrayBufferToBase64","base64","isValid","validateBase64Image","catch","error","binary","bytes","slice","call","Uint8Array","forEach","b","String","fromCharCode","base64String","isJpeg","startsWith","validateJpegImage","isPng","validatePngImage","base64string","src","imageData","from","replace","c","charCodeAt","imageCorrupted","sequence","i"],"sources":["../../../src/helpers/fetch-image/index.js"],"sourcesContent":["import { atob, btoa } from '@lighthouse/abab'\nimport fetchPonyfill from 'fetch-ponyfill'\nimport Promise from 'bluebird'\nimport { LIGHTHOUSE_LOGO_URL } from '../../constants'\nimport { imageNotFound } from '../../images'\nimport { fetchImageForPdfGeneratorService } from '../fetch-image-for-pdf-generator-service'\nimport { fetchImageForWeb } from '../fetch-image-for-web'\n\n// NOTE use the native fetch if it's available in the browser, because the\n// ponyfill (which actually uses the github polyfill) does not support all the\n// same options as native fetch\nconst fetch =\n (typeof self === 'object' && self.fetch) || fetchPonyfill({ Promise }).fetch\n\nconst contentTypes = {\n 'image/png': 'png',\n 'image/jpeg': 'jpeg',\n}\n\nconst defaultOptions = {\n // NOTE The cache: no-cache option is important to avoid an issue with CORS\n // and caching on Chrome. Here's a good explanation of the issue:\n // https://stackoverflow.com/a/37455118\n // In our case, when loading the web version of a form, the signature image is\n // cached without the correct CORS headers. If the pdf is then generated,\n // there's a mismatch between the cached image headers and the CORS headers\n // sent from the fetch request, causing an error\n cache: 'no-cache',\n}\n\nexport function fetchImage(url, options = {}) {\n const { shouldUseCloudfront } = options\n console.log({ options })\n if (shouldUseCloudfront) {\n const isWebContext = typeof window === 'object'\n\n return isWebContext\n ? // Values used from options: isHeader, Signature, Policy, KeyPairId\n fetchImageForWeb(url, options)\n : fetchImageForPdfGeneratorService(url)\n }\n\n const encodedUrl = encodeURI(url)\n\n const fetchOptions = {\n ...defaultOptions,\n ...options,\n }\n const { isHeader = false } = options\n console.log('FETCHING SOME IMAGE,:', { url, options })\n return fetch(encodedUrl, fetchOptions)\n .then((response) => {\n const contentHeader = response.headers.get('content-length')\n const contentType = response.headers.get('content-type')\n\n // NOTE: the response will be ok but we won't be able to render any\n // image meaning pdfmake will error. Raise error here and return early.\n if (contentHeader === '0') {\n return Promise.reject(\n new Error(`Failed to fetch image as no content length: ${encodedUrl}`)\n )\n }\n\n if (!response.ok) {\n return Promise.reject(new Error(`Failed to fetch image: ${encodedUrl}`))\n }\n\n const imageType = contentTypes[contentType]\n\n return response.arrayBuffer().then((buffer) => ({\n buffer,\n imageType,\n }))\n })\n .then(({ buffer, imageType }) => {\n const base64Flag = `data:image/${imageType};base64,`\n const imageStr = arrayBufferToBase64(buffer)\n\n const base64 = `${base64Flag}${imageStr}`\n const isValid = validateBase64Image(base64)\n\n if (!isValid) {\n return Promise.reject(new Error('InvalidImageError'))\n }\n\n return base64\n })\n .catch((error) => {\n if (isHeader) {\n // NOTE: Replace failed headers with LH logo\n console.error('FetchImageHeaderError', error)\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\n }\n\n console.error(error)\n return imageNotFound\n })\n}\n\nexport function arrayBufferToBase64(buffer) {\n let binary = ''\n const bytes = [].slice.call(new Uint8Array(buffer))\n\n bytes.forEach((b) => (binary += String.fromCharCode(b)))\n\n return btoa(binary)\n}\n\nexport function validateBase64Image(base64String) {\n const isJpeg = base64String.startsWith('data:image/jpeg;base64,')\n\n if (isJpeg) return validateJpegImage(base64String)\n\n const isPng = base64String.startsWith('data:image/png;base64,')\n\n if (isPng) return validatePngImage(base64String)\n\n return false\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validateJpegImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/jpeg;base64,', '')),\n (c) => c.charCodeAt(0)\n )\n const imageCorrupted =\n imageData[imageData.length - 1] === 217 &&\n imageData[imageData.length - 2] === 255\n\n return imageCorrupted\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validatePngImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/png;base64,', '')),\n (c) => c.charCodeAt(0)\n )\n const sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]\n\n //check last 12 elements of array so they contains needed values\n for (let i = 12; i > 0;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AAE,GAAGC,CAAC,CAAC,EAAE;MACxD,OAAO,KAAK;IACd;EACF;EAEA,OAAO,IAAI;AACb","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["atob","btoa","fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","fetchImageForPdfGeneratorService","fetchImageForWeb","fetch","self","_typeof","contentTypes","defaultOptions","cache","fetchImage","url","options","arguments","length","undefined","shouldUseCloudfront","isWebContext","window","encodedUrl","encodeURI","fetchOptions","_objectSpread","_options$isHeader","isHeader","then","response","contentHeader","headers","get","contentType","reject","Error","concat","ok","imageType","arrayBuffer","buffer","_ref","base64Flag","imageStr","arrayBufferToBase64","base64","isValid","validateBase64Image","catch","error","console","binary","bytes","slice","call","Uint8Array","forEach","b","String","fromCharCode","base64String","isJpeg","startsWith","validateJpegImage","isPng","validatePngImage","base64string","src","imageData","from","replace","c","charCodeAt","imageCorrupted","sequence","i"],"sources":["../../../src/helpers/fetch-image/index.js"],"sourcesContent":["import { atob, btoa } from '@lighthouse/abab'\nimport fetchPonyfill from 'fetch-ponyfill'\nimport Promise from 'bluebird'\nimport { LIGHTHOUSE_LOGO_URL } from '../../constants'\nimport { imageNotFound } from '../../images'\nimport { fetchImageForPdfGeneratorService } from '../fetch-image-for-pdf-generator-service'\nimport { fetchImageForWeb } from '../fetch-image-for-web'\n\n// NOTE use the native fetch if it's available in the browser, because the\n// ponyfill (which actually uses the github polyfill) does not support all the\n// same options as native fetch\nconst fetch =\n (typeof self === 'object' && self.fetch) || fetchPonyfill({ Promise }).fetch\n\nconst contentTypes = {\n 'image/png': 'png',\n 'image/jpeg': 'jpeg',\n}\n\nconst defaultOptions = {\n // NOTE The cache: no-cache option is important to avoid an issue with CORS\n // and caching on Chrome. Here's a good explanation of the issue:\n // https://stackoverflow.com/a/37455118\n // In our case, when loading the web version of a form, the signature image is\n // cached without the correct CORS headers. If the pdf is then generated,\n // there's a mismatch between the cached image headers and the CORS headers\n // sent from the fetch request, causing an error\n cache: 'no-cache',\n}\n\nexport function fetchImage(url, options = {}) {\n const { shouldUseCloudfront } = options\n\n if (shouldUseCloudfront) {\n const isWebContext = typeof window === 'object'\n\n return isWebContext\n ? // Values used from options: isHeader, Signature, Policy, KeyPairId\n fetchImageForWeb(url, options)\n : fetchImageForPdfGeneratorService(url)\n }\n\n const encodedUrl = encodeURI(url)\n\n const fetchOptions = {\n ...defaultOptions,\n ...options,\n }\n const { isHeader = false } = options\n\n return fetch(encodedUrl, fetchOptions)\n .then(response => {\n const contentHeader = response.headers.get('content-length')\n const contentType = response.headers.get('content-type')\n\n // NOTE: the response will be ok but we won't be able to render any\n // image meaning pdfmake will error. Raise error here and return early.\n if (contentHeader === '0') {\n return Promise.reject(\n new Error(`Failed to fetch image as no content length: ${encodedUrl}`)\n )\n }\n\n if (!response.ok) {\n return Promise.reject(new Error(`Failed to fetch image: ${encodedUrl}`))\n }\n\n const imageType = contentTypes[contentType]\n\n return response.arrayBuffer().then(buffer => ({\n buffer,\n imageType,\n }))\n })\n .then(({ buffer, imageType }) => {\n const base64Flag = `data:image/${imageType};base64,`\n const imageStr = arrayBufferToBase64(buffer)\n\n const base64 = `${base64Flag}${imageStr}`\n const isValid = validateBase64Image(base64)\n\n if (!isValid) {\n return Promise.reject(new Error('InvalidImageError'))\n }\n\n return base64\n })\n .catch(error => {\n if (isHeader) {\n // NOTE: Replace failed headers with LH logo\n console.error('FetchImageHeaderError', error)\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\n }\n\n console.error(error)\n return imageNotFound\n })\n}\n\nexport function arrayBufferToBase64(buffer) {\n let binary = ''\n const bytes = [].slice.call(new Uint8Array(buffer))\n\n bytes.forEach(b => (binary += String.fromCharCode(b)))\n\n return btoa(binary)\n}\n\nexport function validateBase64Image(base64String) {\n const isJpeg = base64String.startsWith('data:image/jpeg;base64,')\n\n if (isJpeg) return validateJpegImage(base64String)\n\n const isPng = base64String.startsWith('data:image/png;base64,')\n\n if (isPng) return validatePngImage(base64String)\n\n return false\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validateJpegImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/jpeg;base64,', '')),\n c => c.charCodeAt(0)\n )\n const imageCorrupted =\n imageData[imageData.length - 1] === 217 &&\n imageData[imageData.length - 2] === 255\n\n return imageCorrupted\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validatePngImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/png;base64,', '')),\n c => c.charCodeAt(0)\n )\n const sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]\n\n //check last 12 elements of array so they contains needed values\n for (let i = 12; i > 0; i--) {\n if (imageData[imageData.length - i] !== sequence[12 - i]) {\n return false\n }\n }\n\n return true\n}\n"],"mappings":";;;;AAAA,SAASA,IAAI,EAAEC,IAAI,QAAQ,kBAAkB;AAC7C,OAAOC,aAAa,MAAM,gBAAgB;AAC1C,OAAOC,OAAO,MAAM,UAAU;AAC9B,SAASC,mBAAmB,QAAQ,iBAAiB;AACrD,SAASC,aAAa,QAAQ,cAAc;AAC5C,SAASC,gCAAgC,QAAQ,0CAA0C;AAC3F,SAASC,gBAAgB,QAAQ,wBAAwB;;AAEzD;AACA;AACA;AACA,IAAMC,KAAK,GACR,QAAOC,IAAI,iCAAAC,OAAA,CAAJD,IAAI,OAAK,QAAQ,IAAIA,IAAI,CAACD,KAAK,IAAKN,aAAa,CAAC;EAAEC,OAAO,EAAPA;AAAQ,CAAC,CAAC,CAACK,KAAK;AAE9E,IAAMG,YAAY,GAAG;EACnB,WAAW,EAAE,KAAK;EAClB,YAAY,EAAE;AAChB,CAAC;AAED,IAAMC,cAAc,GAAG;EACrB;EACA;EACA;EACA;EACA;EACA;EACA;EACAC,KAAK,EAAE;AACT,CAAC;AAED,O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