@lighthouse/common 4.36.0 → 4.37.0-canary-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/helpers/build-fetch-url/index.js +13 -0
- package/dist/helpers/fetch-image/index.js +14 -0
- package/dist/helpers/get-audit-items-data/index.js +4 -1
- package/dist/helpers/get-cloudfront-cookies/index.js +118 -0
- package/dist/helpers/get-secret-value/index.js +49 -0
- package/dist/pdf/audit/index.js +23 -29
- package/dist/pdf/helpers/fields/index.js +3 -1
- package/dist/pdf/issue/index.js +20 -26
- package/dist/pdf/task/index.js +20 -26
- package/lib/helpers/build-fetch-url/index.js +16 -0
- package/lib/helpers/build-fetch-url/index.js.map +1 -1
- package/lib/helpers/fetch-image/index.js +24 -4
- package/lib/helpers/fetch-image/index.js.map +1 -1
- package/lib/helpers/get-audit-items-data/index.js +3 -0
- package/lib/helpers/get-audit-items-data/index.js.map +1 -1
- package/lib/helpers/get-cloudfront-cookies/index.js +176 -0
- package/lib/helpers/get-cloudfront-cookies/index.js.map +1 -0
- package/lib/helpers/get-secret-value/index.js +38 -0
- package/lib/helpers/get-secret-value/index.js.map +1 -0
- package/lib/pdf/audit/index.js +23 -31
- package/lib/pdf/audit/index.js.map +1 -1
- package/lib/pdf/helpers/fields/index.js +3 -1
- package/lib/pdf/helpers/fields/index.js.map +1 -1
- package/lib/pdf/issue/index.js +20 -28
- package/lib/pdf/issue/index.js.map +1 -1
- package/lib/pdf/task/index.js +20 -28
- package/lib/pdf/task/index.js.map +1 -1
- package/mise.toml +2 -0
- package/package.json +1 -1
|
@@ -8,12 +8,25 @@ exports.buildFetchUrl = buildFetchUrl;
|
|
|
8
8
|
function buildFetchUrl(url, options) {
|
|
9
9
|
const {
|
|
10
10
|
awsS3BaseUrl,
|
|
11
|
+
cloudfrontBaseUrl = '',
|
|
11
12
|
cloudinaryBaseUrl,
|
|
12
13
|
fit,
|
|
13
14
|
height,
|
|
14
15
|
width,
|
|
15
16
|
quality
|
|
16
17
|
} = options;
|
|
18
|
+
|
|
19
|
+
if (cloudfrontBaseUrl) {
|
|
20
|
+
const transformations = [];
|
|
21
|
+
let transformationsString = '';
|
|
22
|
+
if (width) transformations.push(`w_${width.toString()}`);
|
|
23
|
+
if (height) transformations.push(`h_${height.toString()}`);
|
|
24
|
+
if (quality) transformations.push(`q_${quality.toString()}`);
|
|
25
|
+
if (fit) transformations.push('c_fit');
|
|
26
|
+
const fetchUrl = `${cloudfrontBaseUrl}/${transformationsString}${awsS3BaseUrl}/${url}`;
|
|
27
|
+
return fetchUrl;
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
const transformations = [];
|
|
18
31
|
let transformationsString = '';
|
|
19
32
|
if (width) transformations.push(`w_${width.toString()}`);
|
|
@@ -20,6 +20,8 @@ var _constants = require("../../constants");
|
|
|
20
20
|
|
|
21
21
|
var _images = require("../../images");
|
|
22
22
|
|
|
23
|
+
var _getCloudfrontCookies = require("../get-cloudfront-cookies");
|
|
24
|
+
|
|
23
25
|
// NOTE use the native fetch if it's available in the browser, because the
|
|
24
26
|
// ponyfill (which actually uses the github polyfill) does not support all the
|
|
25
27
|
// same options as native fetch
|
|
@@ -46,6 +48,18 @@ function fetchImage(url, options = {}) {
|
|
|
46
48
|
const fetchOptions = { ...defaultOptions,
|
|
47
49
|
...options
|
|
48
50
|
};
|
|
51
|
+
const applicationId = options.applicationId;
|
|
52
|
+
|
|
53
|
+
if (url.contains('.cloudfront.net')) {
|
|
54
|
+
fetchOptions.credentials = 'include'; // Ensure cookies are sent with the request
|
|
55
|
+
|
|
56
|
+
const signedCookies = (0, _getCloudfrontCookies.generateCloudFrontCookies)({
|
|
57
|
+
userId: 'anonymous',
|
|
58
|
+
applicationId
|
|
59
|
+
});
|
|
60
|
+
fetchOptions.headers.Cookie = Object.entries(signedCookies).map(([key, value]) => `${key}=${value}`).join('; ');
|
|
61
|
+
}
|
|
62
|
+
|
|
49
63
|
const {
|
|
50
64
|
isHeader = false
|
|
51
65
|
} = options;
|
|
@@ -13,7 +13,8 @@ function getAuditItemsData(items, data) {
|
|
|
13
13
|
const {
|
|
14
14
|
settings: {
|
|
15
15
|
awsS3BaseUrl,
|
|
16
|
-
cloudinaryBaseUrl
|
|
16
|
+
cloudinaryBaseUrl,
|
|
17
|
+
cloudfrontBaseUrl
|
|
17
18
|
} = {},
|
|
18
19
|
entity: {
|
|
19
20
|
groupScores
|
|
@@ -56,6 +57,7 @@ function getAuditItemsData(items, data) {
|
|
|
56
57
|
const assets = rawAssets.map((asset, assetIndex) => {
|
|
57
58
|
const assetUrl = (0, _.buildFetchUrl)(asset, {
|
|
58
59
|
awsS3BaseUrl,
|
|
60
|
+
cloudfrontBaseUrl,
|
|
59
61
|
cloudinaryBaseUrl,
|
|
60
62
|
fit: true,
|
|
61
63
|
height: 400,
|
|
@@ -65,6 +67,7 @@ function getAuditItemsData(items, data) {
|
|
|
65
67
|
const link = `${awsS3BaseUrl}/${asset}`;
|
|
66
68
|
const thumbnailUrl = (0, _.buildFetchUrl)(asset, {
|
|
67
69
|
awsS3BaseUrl,
|
|
70
|
+
cloudfrontBaseUrl,
|
|
68
71
|
cloudinaryBaseUrl,
|
|
69
72
|
width: 100,
|
|
70
73
|
quality: 50
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.generateCloudFrontCookies = generateCloudFrontCookies;
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
getSignedCookies
|
|
10
|
+
} = require('@aws-sdk/cloudfront-signer');
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
getSecretValue
|
|
14
|
+
} = require('../../aws/secrets-manager');
|
|
15
|
+
|
|
16
|
+
const logger = require('../../logger');
|
|
17
|
+
/**
|
|
18
|
+
* Generate CloudFront signed cookies for authenticated users
|
|
19
|
+
* @param {Object} options - Configuration options
|
|
20
|
+
* @param {string} options.userId - User ID for logging purposes
|
|
21
|
+
* @param {string} options.applicationId - Application ID for resource scoping
|
|
22
|
+
* Return Type: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-cloudfront-signer/Interface/CloudfrontSignedCookiesOutput/
|
|
23
|
+
* @returns {Promise<{
|
|
24
|
+
* "CloudFront-Key-Pair-Id": string,
|
|
25
|
+
* "CloudFront-Signature": string,
|
|
26
|
+
* "CloudFront-Expires"?: number,
|
|
27
|
+
* "CloudFront-Policy"?: string
|
|
28
|
+
* } | null>} Signed cookies object or null if disabled/failed
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
async function generateCloudFrontCookies({
|
|
33
|
+
userId,
|
|
34
|
+
applicationId
|
|
35
|
+
}) {
|
|
36
|
+
// Early return if CloudFront is not configured
|
|
37
|
+
if (!process.env.CLOUDFRONT_DOMAIN || !process.env.CLOUDFRONT_KEY_PAIR_ID) {
|
|
38
|
+
logger.debug('CloudFront cookie generation skipped - not configured');
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// Get private key from AWS Secrets Manager
|
|
44
|
+
const privateKey = await getPrivateKey();
|
|
45
|
+
|
|
46
|
+
if (!privateKey) {
|
|
47
|
+
logger.warn('CloudFront private key not available');
|
|
48
|
+
return null;
|
|
49
|
+
} // Set expiration time (14 days from now)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
const expiration = new Date();
|
|
53
|
+
expiration.setTime(expiration.getTime() + 14 * 24 * 60 * 60 * 1000); // Generate resource URL pattern for wildcard access
|
|
54
|
+
|
|
55
|
+
const distributionDomain = `https://${process.env.CLOUDFRONT_DOMAIN}`;
|
|
56
|
+
const resourcePath = applicationId ? `${applicationId}/*` : '*';
|
|
57
|
+
const urlPattern = `${distributionDomain}/${resourcePath}`; // Generate signed cookies
|
|
58
|
+
|
|
59
|
+
const signedCookies = getSignedCookies({
|
|
60
|
+
url: urlPattern,
|
|
61
|
+
keyPairId: process.env.CLOUDFRONT_KEY_PAIR_ID,
|
|
62
|
+
privateKey: privateKey,
|
|
63
|
+
dateLessThan: expiration.getTime() // Use epoch timestamp in milliseconds
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
logger.info('CloudFront cookies generated successfully', {
|
|
67
|
+
userId,
|
|
68
|
+
applicationId,
|
|
69
|
+
resourcePath,
|
|
70
|
+
expiration: expiration.toISOString()
|
|
71
|
+
});
|
|
72
|
+
return signedCookies;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
logger.error('Failed to generate CloudFront cookies', {
|
|
75
|
+
userId,
|
|
76
|
+
applicationId,
|
|
77
|
+
error: {
|
|
78
|
+
message: error.message,
|
|
79
|
+
code: error.code
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let privateKeyCache = null;
|
|
87
|
+
let cacheExpiry = null;
|
|
88
|
+
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
|
|
89
|
+
|
|
90
|
+
async function getPrivateKey() {
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
|
|
93
|
+
if (privateKeyCache && cacheExpiry && now < cacheExpiry) {
|
|
94
|
+
return privateKeyCache;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID) {
|
|
98
|
+
logger.warn('CLOUDFRONT_PRIVATE_KEY_SECRET_ID not configured');
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const privateKey = await getSecretValue(process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID, 'CLOUDFRONT_PRIVATE_KEY');
|
|
104
|
+
privateKeyCache = privateKey;
|
|
105
|
+
cacheExpiry = now + CACHE_TTL;
|
|
106
|
+
logger.debug('CloudFront private key retrieved and cached');
|
|
107
|
+
return privateKey;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
logger.error('Failed to retrieve CloudFront private key', {
|
|
110
|
+
secretId: process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID,
|
|
111
|
+
error: {
|
|
112
|
+
message: error.message,
|
|
113
|
+
code: error.code
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = getSecretValue;
|
|
9
|
+
|
|
10
|
+
var _commonErrors = require("@lighthouse/common-errors");
|
|
11
|
+
|
|
12
|
+
var _awsSdk = _interopRequireDefault(require("aws-sdk"));
|
|
13
|
+
|
|
14
|
+
var _lodash = require("lodash");
|
|
15
|
+
|
|
16
|
+
function getSecretValue(secretId, secretKey) {
|
|
17
|
+
if (!secretId) {
|
|
18
|
+
return Promise.reject(new _commonErrors.errors.ValidationError(`Missing required param: secretId:${secretId}`));
|
|
19
|
+
} // TODO: update these credentials to specific values for service
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const secretsClient = new _awsSdk.default.SecretsManager({
|
|
23
|
+
accessKeyId: process.env.AWS_KEY,
|
|
24
|
+
region: process.env.AWS_SECRET_MANAGER_REGION,
|
|
25
|
+
secretAccessKey: process.env.AWS_SECRET
|
|
26
|
+
});
|
|
27
|
+
return secretsClient.getSecretValue({
|
|
28
|
+
SecretId: secretId
|
|
29
|
+
}).promise().then(payload => {
|
|
30
|
+
const secret = parseSecretString(payload); // Return early if secretKey isn't defined (we want the full set of key/values)
|
|
31
|
+
|
|
32
|
+
if (!secretKey) return secret;
|
|
33
|
+
const secretValue = secret[secretKey];
|
|
34
|
+
|
|
35
|
+
if (!secretValue) {
|
|
36
|
+
throw new _commonErrors.errors.NotFoundError('Secret value could not be found');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return secretValue;
|
|
40
|
+
}).catch(err => {
|
|
41
|
+
throw new _commonErrors.errors.UnknownError(`AWSSecretFetchError: ${err.code}, ${err.message}`);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseSecretString(payload) {
|
|
46
|
+
const secretString = payload.SecretString || '';
|
|
47
|
+
const parsed = (0, _lodash.attempt)(JSON.parse, secretString);
|
|
48
|
+
return (0, _lodash.isError)(parsed) ? {} : parsed;
|
|
49
|
+
}
|
package/dist/pdf/audit/index.js
CHANGED
|
@@ -155,37 +155,31 @@ function generateContent(data) {
|
|
|
155
155
|
style: 'totalScore'
|
|
156
156
|
});
|
|
157
157
|
const body = renderThirdRow ? [firstRow, secondRow, thirdRow] : [firstRow, secondRow];
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const submittedAt = (0, _helpers.text)(`Submitted: ${submittedAtInTimezone}`, {
|
|
169
|
-
colSpan: 2,
|
|
170
|
-
style: 'small'
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
const momentDuration = _momentTimezone.default.duration(Math.abs(entity.formDuration));
|
|
158
|
+
const startedAtInTimezone = entity.startedAt ? (0, _momentTimezone.default)(entity.startedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
159
|
+
const startedAt = (0, _helpers.text)(`Started: ${startedAtInTimezone}`, {
|
|
160
|
+
colSpan: 2,
|
|
161
|
+
style: 'small'
|
|
162
|
+
});
|
|
163
|
+
const submittedAtInTimezone = entity.submittedAt ? (0, _momentTimezone.default)(entity.submittedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
164
|
+
const submittedAt = (0, _helpers.text)(`Submitted: ${submittedAtInTimezone}`, {
|
|
165
|
+
colSpan: 2,
|
|
166
|
+
style: 'small'
|
|
167
|
+
});
|
|
174
168
|
|
|
175
|
-
|
|
176
|
-
const hours = Math.floor(momentDuration.hours()).toString().padStart(2, '0');
|
|
177
|
-
const minutes = momentDuration.minutes().toString().padStart(2, '0');
|
|
178
|
-
const seconds = momentDuration.seconds().toString().padStart(2, '0');
|
|
179
|
-
const formattedFormDuration = entity.formDuration ? `${entity.formDuration < 0 ? '-' : ''}${days}:${hours}:${minutes}:${seconds}` : 'Not recorded';
|
|
180
|
-
const formDuration = (0, _helpers.text)(`Form Duration (DD:HH:MM:SS): ${formattedFormDuration}`, {
|
|
181
|
-
colSpan: 2,
|
|
182
|
-
style: 'small'
|
|
183
|
-
});
|
|
184
|
-
body.push([startedAt, dummyColumn]);
|
|
185
|
-
body.push([submittedAt, dummyColumn]);
|
|
186
|
-
body.push([formDuration, dummyColumn]);
|
|
187
|
-
}
|
|
169
|
+
const momentDuration = _momentTimezone.default.duration(Math.abs(entity.formDuration));
|
|
188
170
|
|
|
171
|
+
const days = Math.floor(momentDuration.asDays()).toString().padStart(2, '0');
|
|
172
|
+
const hours = Math.floor(momentDuration.hours()).toString().padStart(2, '0');
|
|
173
|
+
const minutes = momentDuration.minutes().toString().padStart(2, '0');
|
|
174
|
+
const seconds = momentDuration.seconds().toString().padStart(2, '0');
|
|
175
|
+
const formattedFormDuration = entity.formDuration ? `${entity.formDuration < 0 ? '-' : ''}${days}:${hours}:${minutes}:${seconds}` : 'Not recorded';
|
|
176
|
+
const formDuration = (0, _helpers.text)(`Form Duration (DD:HH:MM:SS): ${formattedFormDuration}`, {
|
|
177
|
+
colSpan: 2,
|
|
178
|
+
style: 'small'
|
|
179
|
+
});
|
|
180
|
+
body.push([startedAt, dummyColumn]);
|
|
181
|
+
body.push([submittedAt, dummyColumn]);
|
|
182
|
+
body.push([formDuration, dummyColumn]);
|
|
189
183
|
const titleTable = (0, _helpers.twoColumnTable)({
|
|
190
184
|
body,
|
|
191
185
|
layout: 'noBorders',
|
|
@@ -50,7 +50,8 @@ function buildImage(options) {
|
|
|
50
50
|
} = options;
|
|
51
51
|
const {
|
|
52
52
|
awsS3BaseUrl,
|
|
53
|
-
cloudinaryBaseUrl
|
|
53
|
+
cloudinaryBaseUrl,
|
|
54
|
+
cloudfrontBaseUrl
|
|
54
55
|
} = settings;
|
|
55
56
|
const isVideoType = new RegExp('.mp4$').test(filepath);
|
|
56
57
|
const link = `${awsS3BaseUrl}/${filepath}`;
|
|
@@ -66,6 +67,7 @@ function buildImage(options) {
|
|
|
66
67
|
|
|
67
68
|
const url = (0, _helpers.buildFetchUrl)(filepath, {
|
|
68
69
|
awsS3BaseUrl,
|
|
70
|
+
cloudfrontBaseUrl,
|
|
69
71
|
cloudinaryBaseUrl,
|
|
70
72
|
fit: true,
|
|
71
73
|
height: 400,
|
package/dist/pdf/issue/index.js
CHANGED
|
@@ -92,34 +92,28 @@ function generateContent(data) {
|
|
|
92
92
|
style: 'small'
|
|
93
93
|
});
|
|
94
94
|
const body = !(0, _lodash.isEmpty)(reverseGeocoded) ? [[headerTitle], [headerSubTitle], [headerAddress]] : [[headerTitle], [headerSubTitle]];
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const submittedAtInTimezone = entity.submittedAt ? (0, _momentTimezone.default)(entity.submittedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
104
|
-
const submittedAt = (0, _helpers.text)(`Submitted: ${submittedAtInTimezone}`, {
|
|
105
|
-
style: 'small'
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
const momentDuration = _momentTimezone.default.duration(Math.abs(entity.formDuration));
|
|
95
|
+
const startedAtInTimezone = entity.startedAt ? (0, _momentTimezone.default)(entity.startedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
96
|
+
const startedAt = (0, _helpers.text)(`Started: ${startedAtInTimezone}`, {
|
|
97
|
+
style: 'small'
|
|
98
|
+
});
|
|
99
|
+
const submittedAtInTimezone = entity.submittedAt ? (0, _momentTimezone.default)(entity.submittedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
100
|
+
const submittedAt = (0, _helpers.text)(`Submitted: ${submittedAtInTimezone}`, {
|
|
101
|
+
style: 'small'
|
|
102
|
+
});
|
|
109
103
|
|
|
110
|
-
|
|
111
|
-
const hours = Math.floor(momentDuration.hours()).toString().padStart(2, '0');
|
|
112
|
-
const minutes = momentDuration.minutes().toString().padStart(2, '0');
|
|
113
|
-
const seconds = momentDuration.seconds().toString().padStart(2, '0');
|
|
114
|
-
const formattedFormDuration = entity.formDuration ? `${entity.formDuration < 0 ? '-' : ''}${days}:${hours}:${minutes}:${seconds}` : 'Not recorded';
|
|
115
|
-
const formDuration = (0, _helpers.text)(`Form Duration (DD:HH:MM:SS): ${formattedFormDuration}`, {
|
|
116
|
-
style: 'small'
|
|
117
|
-
});
|
|
118
|
-
body.push([startedAt]);
|
|
119
|
-
body.push([submittedAt]);
|
|
120
|
-
body.push([formDuration]);
|
|
121
|
-
}
|
|
104
|
+
const momentDuration = _momentTimezone.default.duration(Math.abs(entity.formDuration));
|
|
122
105
|
|
|
106
|
+
const days = Math.floor(momentDuration.asDays()).toString().padStart(2, '0');
|
|
107
|
+
const hours = Math.floor(momentDuration.hours()).toString().padStart(2, '0');
|
|
108
|
+
const minutes = momentDuration.minutes().toString().padStart(2, '0');
|
|
109
|
+
const seconds = momentDuration.seconds().toString().padStart(2, '0');
|
|
110
|
+
const formattedFormDuration = entity.formDuration ? `${entity.formDuration < 0 ? '-' : ''}${days}:${hours}:${minutes}:${seconds}` : 'Not recorded';
|
|
111
|
+
const formDuration = (0, _helpers.text)(`Form Duration (DD:HH:MM:SS): ${formattedFormDuration}`, {
|
|
112
|
+
style: 'small'
|
|
113
|
+
});
|
|
114
|
+
body.push([startedAt]);
|
|
115
|
+
body.push([submittedAt]);
|
|
116
|
+
body.push([formDuration]);
|
|
123
117
|
const titleTable = (0, _helpers.table)({
|
|
124
118
|
body,
|
|
125
119
|
layout: 'noBorders',
|
package/dist/pdf/task/index.js
CHANGED
|
@@ -94,34 +94,28 @@ function generateContent(data) {
|
|
|
94
94
|
body.push([headerAddress]);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const submittedAtInTimezone = entity.submittedAt ? (0, _momentTimezone.default)(entity.submittedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
106
|
-
const submittedAt = (0, _helpers.text)(`Submitted: ${submittedAtInTimezone}`, {
|
|
107
|
-
style: 'small'
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const momentDuration = _momentTimezone.default.duration(Math.abs(entity.formDuration));
|
|
97
|
+
const startedAtInTimezone = entity.startedAt ? (0, _momentTimezone.default)(entity.startedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
98
|
+
const startedAt = (0, _helpers.text)(`Started: ${startedAtInTimezone}`, {
|
|
99
|
+
style: 'small'
|
|
100
|
+
});
|
|
101
|
+
const submittedAtInTimezone = entity.submittedAt ? (0, _momentTimezone.default)(entity.submittedAt).tz(timezone).format('YYYY-MM-DD HH:mm:ss z') : 'Not recorded';
|
|
102
|
+
const submittedAt = (0, _helpers.text)(`Submitted: ${submittedAtInTimezone}`, {
|
|
103
|
+
style: 'small'
|
|
104
|
+
});
|
|
111
105
|
|
|
112
|
-
|
|
113
|
-
const hours = Math.floor(momentDuration.hours()).toString().padStart(2, '0');
|
|
114
|
-
const minutes = momentDuration.minutes().toString().padStart(2, '0');
|
|
115
|
-
const seconds = momentDuration.seconds().toString().padStart(2, '0');
|
|
116
|
-
const formattedFormDuration = entity.formDuration ? `${entity.formDuration < 0 ? '-' : ''}${days}:${hours}:${minutes}:${seconds}` : 'Not recorded';
|
|
117
|
-
const formDuration = (0, _helpers.text)(`Form Duration (DD:HH:MM:SS): ${formattedFormDuration}`, {
|
|
118
|
-
style: 'small'
|
|
119
|
-
});
|
|
120
|
-
body.push([startedAt]);
|
|
121
|
-
body.push([submittedAt]);
|
|
122
|
-
body.push([formDuration]);
|
|
123
|
-
}
|
|
106
|
+
const momentDuration = _momentTimezone.default.duration(Math.abs(entity.formDuration));
|
|
124
107
|
|
|
108
|
+
const days = Math.floor(momentDuration.asDays()).toString().padStart(2, '0');
|
|
109
|
+
const hours = Math.floor(momentDuration.hours()).toString().padStart(2, '0');
|
|
110
|
+
const minutes = momentDuration.minutes().toString().padStart(2, '0');
|
|
111
|
+
const seconds = momentDuration.seconds().toString().padStart(2, '0');
|
|
112
|
+
const formattedFormDuration = entity.formDuration ? `${entity.formDuration < 0 ? '-' : ''}${days}:${hours}:${minutes}:${seconds}` : 'Not recorded';
|
|
113
|
+
const formDuration = (0, _helpers.text)(`Form Duration (DD:HH:MM:SS): ${formattedFormDuration}`, {
|
|
114
|
+
style: 'small'
|
|
115
|
+
});
|
|
116
|
+
body.push([startedAt]);
|
|
117
|
+
body.push([submittedAt]);
|
|
118
|
+
body.push([formDuration]);
|
|
125
119
|
const titleTable = (0, _helpers.table)({
|
|
126
120
|
body,
|
|
127
121
|
layout: 'noBorders',
|
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
export function buildFetchUrl(url, options) {
|
|
2
2
|
var awsS3BaseUrl = options.awsS3BaseUrl,
|
|
3
|
+
_options$cloudfrontBa = options.cloudfrontBaseUrl,
|
|
4
|
+
cloudfrontBaseUrl = _options$cloudfrontBa === void 0 ? '' : _options$cloudfrontBa,
|
|
3
5
|
cloudinaryBaseUrl = options.cloudinaryBaseUrl,
|
|
4
6
|
fit = options.fit,
|
|
5
7
|
height = options.height,
|
|
6
8
|
width = options.width,
|
|
7
9
|
quality = options.quality;
|
|
10
|
+
|
|
11
|
+
if (cloudfrontBaseUrl) {
|
|
12
|
+
var _transformations = [];
|
|
13
|
+
var _transformationsString = '';
|
|
14
|
+
if (width) _transformations.push("w_".concat(width.toString()));
|
|
15
|
+
if (height) _transformations.push("h_".concat(height.toString()));
|
|
16
|
+
if (quality) _transformations.push("q_".concat(quality.toString()));
|
|
17
|
+
if (fit) _transformations.push('c_fit');
|
|
18
|
+
|
|
19
|
+
var _fetchUrl = "".concat(cloudfrontBaseUrl, "/").concat(_transformationsString).concat(awsS3BaseUrl, "/").concat(url);
|
|
20
|
+
|
|
21
|
+
return _fetchUrl;
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
var transformations = [];
|
|
9
25
|
var transformationsString = '';
|
|
10
26
|
if (width) transformations.push("w_".concat(width.toString()));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/helpers/build-fetch-url/index.js"],"names":["buildFetchUrl","url","options","awsS3BaseUrl","cloudinaryBaseUrl","fit","height","width","quality","transformations","transformationsString","push","toString","
|
|
1
|
+
{"version":3,"sources":["../../../src/helpers/build-fetch-url/index.js"],"names":["buildFetchUrl","url","options","awsS3BaseUrl","cloudfrontBaseUrl","cloudinaryBaseUrl","fit","height","width","quality","transformations","transformationsString","push","toString","fetchUrl","join"],"mappings":"AAAA,OAAO,SAASA,aAAT,CAAuBC,GAAvB,EAA4BC,OAA5B,EAAqC;AAAA,MAExCC,YAFwC,GAStCD,OATsC,CAExCC,YAFwC;AAAA,8BAStCD,OATsC,CAGxCE,iBAHwC;AAAA,MAGxCA,iBAHwC,sCAGpB,EAHoB;AAAA,MAIxCC,iBAJwC,GAStCH,OATsC,CAIxCG,iBAJwC;AAAA,MAKxCC,GALwC,GAStCJ,OATsC,CAKxCI,GALwC;AAAA,MAMxCC,MANwC,GAStCL,OATsC,CAMxCK,MANwC;AAAA,MAOxCC,KAPwC,G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l,\n fit,\n height,\n width,\n quality,\n } = options\n\n if (cloudfrontBaseUrl) {\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 const fetchUrl = `${cloudfrontBaseUrl}/${transformationsString}${awsS3BaseUrl}/${url}`\n return fetchUrl\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"],"file":"index.js"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
1
2
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
3
|
import _typeof from "@babel/runtime/helpers/typeof";
|
|
3
4
|
|
|
@@ -9,7 +10,8 @@ import { atob, btoa } from '@lighthouse/abab';
|
|
|
9
10
|
import fetchPonyfill from 'fetch-ponyfill';
|
|
10
11
|
import Promise from 'bluebird';
|
|
11
12
|
import { LIGHTHOUSE_LOGO_URL } from '../../constants';
|
|
12
|
-
import { imageNotFound } from '../../images';
|
|
13
|
+
import { imageNotFound } from '../../images';
|
|
14
|
+
import { generateCloudFrontCookies } from '../get-cloudfront-cookies'; // NOTE use the native fetch if it's available in the browser, because the
|
|
13
15
|
// ponyfill (which actually uses the github polyfill) does not support all the
|
|
14
16
|
// same options as native fetch
|
|
15
17
|
|
|
@@ -36,6 +38,24 @@ export function fetchImage(url) {
|
|
|
36
38
|
|
|
37
39
|
var fetchOptions = _objectSpread(_objectSpread({}, defaultOptions), options);
|
|
38
40
|
|
|
41
|
+
var applicationId = options.applicationId;
|
|
42
|
+
|
|
43
|
+
if (url.contains('.cloudfront.net')) {
|
|
44
|
+
fetchOptions.credentials = 'include'; // Ensure cookies are sent with the request
|
|
45
|
+
|
|
46
|
+
var signedCookies = generateCloudFrontCookies({
|
|
47
|
+
userId: 'anonymous',
|
|
48
|
+
applicationId: applicationId
|
|
49
|
+
});
|
|
50
|
+
fetchOptions.headers.Cookie = Object.entries(signedCookies).map(function (_ref) {
|
|
51
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
52
|
+
key = _ref2[0],
|
|
53
|
+
value = _ref2[1];
|
|
54
|
+
|
|
55
|
+
return "".concat(key, "=").concat(value);
|
|
56
|
+
}).join('; ');
|
|
57
|
+
}
|
|
58
|
+
|
|
39
59
|
var _options$isHeader = options.isHeader,
|
|
40
60
|
isHeader = _options$isHeader === void 0 ? false : _options$isHeader;
|
|
41
61
|
return fetch(encodedUrl, fetchOptions).then(function (response) {
|
|
@@ -58,9 +78,9 @@ export function fetchImage(url) {
|
|
|
58
78
|
imageType: imageType
|
|
59
79
|
};
|
|
60
80
|
});
|
|
61
|
-
}).then(function (
|
|
62
|
-
var buffer =
|
|
63
|
-
imageType =
|
|
81
|
+
}).then(function (_ref3) {
|
|
82
|
+
var buffer = _ref3.buffer,
|
|
83
|
+
imageType = _ref3.imageType;
|
|
64
84
|
var base64Flag = "data:image/".concat(imageType, ";base64,");
|
|
65
85
|
var imageStr = arrayBufferToBase64(buffer);
|
|
66
86
|
var base64 = "".concat(base64Flag).concat(imageStr);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/helpers/fetch-image/index.js"],"names":["atob","btoa","fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","fetch","self","contentTypes","defaultOptions","cache","fetchImage","url","options","encodedUrl","encodeURI","fetchOptions","isHeader","then","response","contentHeader","headers","get","contentType","reject","Error","ok","imageType","arrayBuffer","buffer","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","length","sequence","i"],"mappings":";;;;;;;AAAA,SAASA,IAAT,EAAeC,IAAf,QAA2B,kBAA3B;AACA,OAAOC,aAAP,MAA0B,gBAA1B;AACA,OAAOC,OAAP,MAAoB,UAApB;AACA,SAASC,mBAAT,QAAoC,iBAApC;AACA,SAASC,aAAT,QAA8B,cAA9B,C,CAEA;AACA;AACA;;AACA,IAAMC,KAAK,GACR,QAAOC,IAAP,yCAAOA,IAAP,OAAgB,QAAhB,IAA4BA,IAAI,CAACD,KAAlC,IAA4CJ,aAAa,CAAC;AAAEC,EAAAA,OAAO,EAAPA;AAAF,CAAD,CAAb,CAA2BG,KADzE;AAGA,IAAME,YAAY,GAAG;AACnB,eAAa,KADM;AAEnB,gBAAc;AAFK,CAArB;AAKA,IAAMC,cAAc,GAAG;AACrB;AACA;AACA;AACA;AACA;AACA;AACA;AACAC,EAAAA,KAAK,EAAE;AARc,CAAvB;AAWA,OAAO,SAASC,UAAT,CAAoBC,GAApB,EAAuC;AAAA,MAAdC,OAAc,uEAAJ,EAAI;AAC5C,MAAMC,UAAU,GAAGC,SAAS,CAACH,GAAD,CAA5B;;AACA,MAAMI,YAAY,mCACbP,cADa,GAEbI,OAFa,CAAlB;;AAF4C,0BAMfA,OANe,CAMpCI,QANoC;AAAA,MAMpCA,QANoC,kCAMzB,KANyB;AAQ5C,SAAOX,KAAK,CAACQ,UAAD,EAAaE,YAAb,CAAL,CACJE,IADI,CACC,UAAAC,QAAQ,EAAI;AAChB,QAAMC,aAAa,GAAGD,QAAQ,CAACE,OAAT,CAAiBC,GAAjB,CAAqB,gBAArB,CAAtB;AACA,QAAMC,WAAW,GAAGJ,QAAQ,CAACE,OAAT,CAAiBC,GAAjB,CAAqB,cAArB,CAApB,CAFgB,CAIhB;AACA;;AACA,QAAIF,aAAa,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AAG,GAAGD,YAAZ;AACA,MAAME,SAAS,GAAGb,UAAU,CAACc,IAAX,CAChBzD,IAAI,CAACuD,GAAG,CAACG,OAAJ,CAAY,wBAAZ,EAAsC,EAAtC,CAAD,CADY,EAEhB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,UAAF,CAAa,CAAb,CAAJ;AAAA,GAFe,CAAlB;AAIA,MAAMG,QAAQ,GAAG,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,EAAa,EAAb,EAAiB,EAAjB,EAAqB,EAArB,EAAyB,EAAzB,EAA6B,GAA7B,EAAkC,EAAlC,EAAsC,EAAtC,EAA0C,GAA1C,CAAjB,CAN6C,CAMmB;AAEhE;;AACA,OAAK,IAAIC,CAAC,GAAG,EAAb,EAAiBA,CAAC,GAAG,CAArB,EAAwBA,CAAC,EAAzB,EAA6B;AAC3B,QAAIR,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmBE,CAApB,CAAT,KAAoCD,QAAQ,CAAC,KAAKC,CAAN,CAAhD,EAA0D;AACxD,aAAO,KAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD","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'\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 encodedUrl = encodeURI(url)\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\nfunction 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] // in hex:\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"],"file":"index.js"}
|
|
1
|
+
{"version":3,"sources":["../../../src/helpers/fetch-image/index.js"],"names":["atob","btoa","fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","generateCloudFrontCookies","fetch","self","contentTypes","defaultOptions","cache","fetchImage","url","options","encodedUrl","encodeURI","fetchOptions","applicationId","contains","credentials","signedCookies","userId","headers","Cookie","Object","entries","map","key","value","join","isHeader","then","response","contentHeader","get","contentType","reject","Error","ok","imageType","arrayBuffer","buffer","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","length","sequence","i"],"mappings":";;;;;;;;AAAA,SAASA,IAAT,EAAeC,IAAf,QAA2B,kBAA3B;AACA,OAAOC,aAAP,MAA0B,gBAA1B;AACA,OAAOC,OAAP,MAAoB,UAApB;AACA,SAASC,mBAAT,QAAoC,iBAApC;AACA,SAASC,aAAT,QAA8B,cAA9B;AACA,SAASC,yBAAT,QAA0C,2BAA1C,C,CACA;AACA;AACA;;AACA,IAAMC,KAAK,GACR,QAAOC,IAAP,yCAAOA,IAAP,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,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmB,CAApB,CAAT,KAAoC,GAFtC;AAIA,SAAOD,cAAP;AACD,C,CAED;AACA;;AACA,OAAO,SAASR,gBAAT,CAA0BC,YAA1B,EAAwC;AAC7C,MAAMC,GAAG,GAAGD,YAAZ;AACA,MAAME,SAAS,GAAGb,UAAU,CAACc,IAAX,CAChBtE,IAAI,CAACoE,GAAG,CAACG,OAAJ,CAAY,wBAAZ,EAAsC,EAAtC,CAAD,CADY,EAEhB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,UAAF,CAAa,CAAb,CAAJ;AAAA,GAFe,CAAlB;AAIA,MAAMG,QAAQ,GAAG,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,EAAa,EAAb,EAAiB,EAAjB,EAAqB,EAArB,EAAyB,EAAzB,EAA6B,GAA7B,EAAkC,EAAlC,EAAsC,EAAtC,EAA0C,GAA1C,CAAjB,CAN6C,CAMmB;AAEhE;;AACA,OAAK,IAAIC,CAAC,GAAG,EAAb,EAAiBA,CAAC,GAAG,CAArB,EAAwBA,CAAC,EAAzB,EAA6B;AAC3B,QAAIR,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmBE,CAApB,CAAT,KAAoCD,QAAQ,CAAC,KAAKC,CAAN,CAAhD,EAA0D;AACxD,aAAO,KAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD","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 { generateCloudFrontCookies } from '../get-cloudfront-cookies'\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 encodedUrl = encodeURI(url)\n const fetchOptions = {\n ...defaultOptions,\n ...options,\n }\n\n const applicationId = options.applicationId\n\n if (url.contains('.cloudfront.net')) {\n fetchOptions.credentials = 'include' // Ensure cookies are sent with the request\n\n const signedCookies = generateCloudFrontCookies({\n userId: 'anonymous',\n applicationId,\n })\n\n fetchOptions.headers.Cookie = Object.entries(signedCookies)\n .map(([key, value]) => `${key}=${value}`)\n .join('; ')\n }\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\nfunction 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] // in hex:\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"],"file":"index.js"}
|
|
@@ -5,6 +5,7 @@ export function getAuditItemsData(items, data) {
|
|
|
5
5
|
_data$settings = _data$settings === void 0 ? {} : _data$settings;
|
|
6
6
|
var awsS3BaseUrl = _data$settings.awsS3BaseUrl,
|
|
7
7
|
cloudinaryBaseUrl = _data$settings.cloudinaryBaseUrl,
|
|
8
|
+
cloudfrontBaseUrl = _data$settings.cloudfrontBaseUrl,
|
|
8
9
|
_data$entity = data.entity;
|
|
9
10
|
_data$entity = _data$entity === void 0 ? {} : _data$entity;
|
|
10
11
|
var groupScores = _data$entity.groupScores;
|
|
@@ -45,6 +46,7 @@ export function getAuditItemsData(items, data) {
|
|
|
45
46
|
var assets = rawAssets.map(function (asset, assetIndex) {
|
|
46
47
|
var assetUrl = buildFetchUrl(asset, {
|
|
47
48
|
awsS3BaseUrl: awsS3BaseUrl,
|
|
49
|
+
cloudfrontBaseUrl: cloudfrontBaseUrl,
|
|
48
50
|
cloudinaryBaseUrl: cloudinaryBaseUrl,
|
|
49
51
|
fit: true,
|
|
50
52
|
height: 400,
|
|
@@ -54,6 +56,7 @@ export function getAuditItemsData(items, data) {
|
|
|
54
56
|
var link = "".concat(awsS3BaseUrl, "/").concat(asset);
|
|
55
57
|
var thumbnailUrl = buildFetchUrl(asset, {
|
|
56
58
|
awsS3BaseUrl: awsS3BaseUrl,
|
|
59
|
+
cloudfrontBaseUrl: cloudfrontBaseUrl,
|
|
57
60
|
cloudinaryBaseUrl: cloudinaryBaseUrl,
|
|
58
61
|
width: 100,
|
|
59
62
|
quality: 50
|