@lighthouse/common 6.10.0 → 6.10.1-canary.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.
- package/.mise.toml +6 -6
- package/CHANGELOG.md +758 -758
- package/README.md +1 -1
- package/dist/errors/FetchImageError.js +3 -3
- package/dist/helpers/fetch-image-for-pdf-generator-service/index.js +7 -7
- package/dist/helpers/floatify/index.js +6 -6
- package/dist/helpers/get-audit-items-data/index.js +47 -36
- package/dist/helpers/validate-url/index.js +4 -4
- package/dist/pdf/activity-report/index.js +32 -32
- package/dist/pdf/audit/index.js +23 -23
- package/dist/pdf/helpers/fields/index.js +47 -27
- package/dist/pdf/issue/index.js +23 -23
- package/dist/pdf/job/index.js +22 -22
- package/dist/pdf/shift-report/index.js +28 -28
- package/dist/pdf/task/index.js +23 -23
- package/dist/scheduling/generators/occurrenceIntervalsGenerator.js +2 -2
- package/dist/scheduling/generators/scheduleIntervalsGenerator.js +2 -2
- package/dist/scheduling/generators/serviceIntervalsGenerator.js +2 -2
- package/dist/scheduling/helpers/convertToTimezone.js +6 -6
- package/dist/scheduling/helpers/generateNonRepeatingSchedule.js +2 -2
- package/dist/scheduling/helpers/generateRepeatingSchedule.js +2 -2
- package/dist/scheduling/helpers/generateScheduleEnd.js +2 -2
- package/dist/scheduling/helpers/hasValidStartAndEnd.js +5 -5
- package/dist/scheduling/helpers/intervalCovers.js +6 -6
- package/dist/scheduling/helpers/intervalIntersectsEnd.js +6 -6
- package/dist/scheduling/helpers/intervalIntersectsStart.js +6 -6
- package/dist/scheduling/helpers/intervalOnlyIntersectsEnd.js +6 -6
- package/dist/scheduling/helpers/intervalOnlyIntersectsStart.js +6 -6
- package/dist/scheduling/helpers/intervalOutside.js +6 -6
- package/dist/scheduling/helpers/intervalWithin.js +6 -6
- package/dist/scheduling/helpers/mergeIntervals.js +6 -6
- package/dist/scheduling/helpers/splitIntervals.js +6 -6
- package/dist/scheduling/strategies/getNext.js +2 -2
- package/dist/scheduling/strategies/getNextExactDateOfMonth.js +2 -2
- package/dist/scheduling/strategies/getNextLastDayOfMonth.js +2 -2
- package/dist/scheduling/strategies/getNextLastWeekdayOfMonth.js +2 -2
- package/dist/scheduling/strategies/getNextNoRepeat.js +2 -2
- package/dist/scheduling/strategies/getNextWeekday.js +2 -2
- package/dist/scheduling/strategies/getNextXDayOfXWeekOfMonth.js +2 -2
- package/dist/service-hours/__helpers__/calculate-hours.js +4 -4
- package/dist/service-hours/__helpers__/isOpen.js +2 -2
- package/dist/service-hours/schema.js +4 -4
- package/lib/constants.js +12 -6
- package/lib/constants.js.map +1 -1
- package/lib/errors/DomainError.js +14 -21
- package/lib/errors/DomainError.js.map +1 -1
- package/lib/errors/FetchImageError.js +17 -24
- package/lib/errors/FetchImageError.js.map +1 -1
- package/lib/errors/ValidationError.js +30 -36
- package/lib/errors/ValidationError.js.map +1 -1
- package/lib/errors/index.js +23 -5
- package/lib/errors/index.js.map +1 -1
- package/lib/helpers/build-fetch-url/index.js +43 -47
- package/lib/helpers/build-fetch-url/index.js.map +1 -1
- package/lib/helpers/fetch-image/index.js +83 -82
- package/lib/helpers/fetch-image/index.js.map +1 -1
- package/lib/helpers/fetch-image-for-pdf-generator-service/index.js +168 -243
- package/lib/helpers/fetch-image-for-pdf-generator-service/index.js.map +1 -1
- package/lib/helpers/fetch-image-for-web/index.js +71 -94
- package/lib/helpers/fetch-image-for-web/index.js.map +1 -1
- package/lib/helpers/fetch-lighthouse-logo/index.js +36 -61
- package/lib/helpers/fetch-lighthouse-logo/index.js.map +1 -1
- package/lib/helpers/floatify/index.js +13 -7
- package/lib/helpers/floatify/index.js.map +1 -1
- package/lib/helpers/get-assignees-full-names/index.js +11 -7
- package/lib/helpers/get-assignees-full-names/index.js.map +1 -1
- package/lib/helpers/get-audit-entry-details/index.js +42 -32
- package/lib/helpers/get-audit-entry-details/index.js.map +1 -1
- package/lib/helpers/get-audit-items-data/index.js +104 -83
- package/lib/helpers/get-audit-items-data/index.js.map +1 -1
- package/lib/helpers/get-geometry-geocoded-string/index.js +10 -5
- package/lib/helpers/get-geometry-geocoded-string/index.js.map +1 -1
- package/lib/helpers/get-issue-details/index.js +53 -43
- package/lib/helpers/get-issue-details/index.js.map +1 -1
- package/lib/helpers/get-job-details/index.js +49 -41
- package/lib/helpers/get-job-details/index.js.map +1 -1
- package/lib/helpers/get-location-reference/index.js +32 -34
- package/lib/helpers/get-location-reference/index.js.map +1 -1
- package/lib/helpers/get-pretty-duration/index.js +16 -10
- package/lib/helpers/get-pretty-duration/index.js.map +1 -1
- package/lib/helpers/get-reference-details/index.js +20 -10
- package/lib/helpers/get-reference-details/index.js.map +1 -1
- package/lib/helpers/get-status-details/index.js +8 -2
- package/lib/helpers/get-status-details/index.js.map +1 -1
- package/lib/helpers/get-task-entry-details/index.js +32 -23
- package/lib/helpers/get-task-entry-details/index.js.map +1 -1
- package/lib/helpers/get-timezone-abbr/index.js +11 -4
- package/lib/helpers/get-timezone-abbr/index.js.map +1 -1
- package/lib/helpers/get-timezone-datetime/index.js +20 -13
- package/lib/helpers/get-timezone-datetime/index.js.map +1 -1
- package/lib/helpers/get-user-full-name/index.js +9 -3
- package/lib/helpers/get-user-full-name/index.js.map +1 -1
- package/lib/helpers/get-value-as-percentage/index.js +9 -4
- package/lib/helpers/get-value-as-percentage/index.js.map +1 -1
- package/lib/helpers/image-validators/index.js +18 -12
- package/lib/helpers/image-validators/index.js.map +1 -1
- package/lib/helpers/index.js +130 -18
- package/lib/helpers/index.js.map +1 -1
- package/lib/helpers/validate-url/index.js +11 -5
- package/lib/helpers/validate-url/index.js.map +1 -1
- package/lib/images/index.js +10 -4
- package/lib/images/index.js.map +1 -1
- package/lib/index.js +66 -10
- package/lib/index.js.map +1 -1
- package/lib/pdf/activity-report/index.js +407 -537
- package/lib/pdf/activity-report/index.js.map +1 -1
- package/lib/pdf/audit/index.js +137 -134
- package/lib/pdf/audit/index.js.map +1 -1
- package/lib/pdf/fonts/index.js +4 -2
- package/lib/pdf/fonts/index.js.map +1 -1
- package/lib/pdf/helpers/build-audit-content/index.js +57 -51
- package/lib/pdf/helpers/build-audit-content/index.js.map +1 -1
- package/lib/pdf/helpers/build-audit-follow-ups/index.js +52 -50
- package/lib/pdf/helpers/build-audit-follow-ups/index.js.map +1 -1
- package/lib/pdf/helpers/build-location-scans-content/index.js +22 -16
- package/lib/pdf/helpers/build-location-scans-content/index.js.map +1 -1
- package/lib/pdf/helpers/build-template-content/index.js +52 -51
- package/lib/pdf/helpers/build-template-content/index.js.map +1 -1
- package/lib/pdf/helpers/default-footer/index.js +38 -35
- package/lib/pdf/helpers/default-footer/index.js.map +1 -1
- package/lib/pdf/helpers/default-header/index.js +40 -35
- package/lib/pdf/helpers/default-header/index.js.map +1 -1
- package/lib/pdf/helpers/default-styles/index.js +7 -1
- package/lib/pdf/helpers/default-styles/index.js.map +1 -1
- package/lib/pdf/helpers/fields/index.js +225 -214
- package/lib/pdf/helpers/fields/index.js.map +1 -1
- package/lib/pdf/helpers/format-location-address/index.js +14 -8
- package/lib/pdf/helpers/format-location-address/index.js.map +1 -1
- package/lib/pdf/helpers/generate-definition/index.js +60 -65
- package/lib/pdf/helpers/generate-definition/index.js.map +1 -1
- package/lib/pdf/helpers/horizontal-line/index.js +14 -14
- package/lib/pdf/helpers/horizontal-line/index.js.map +1 -1
- package/lib/pdf/helpers/html-transformer/index.js +45 -35
- package/lib/pdf/helpers/html-transformer/index.js.map +1 -1
- package/lib/pdf/helpers/index.js +194 -15
- package/lib/pdf/helpers/index.js.map +1 -1
- package/lib/pdf/helpers/parse-value/index.js +10 -4
- package/lib/pdf/helpers/parse-value/index.js.map +1 -1
- package/lib/pdf/helpers/table/index.js +136 -205
- package/lib/pdf/helpers/table/index.js.map +1 -1
- package/lib/pdf/helpers/text/index.js +11 -8
- package/lib/pdf/helpers/text/index.js.map +1 -1
- package/lib/pdf/icons/index.js +8 -2
- package/lib/pdf/icons/index.js.map +1 -1
- package/lib/pdf/index.js +47 -6
- package/lib/pdf/index.js.map +1 -1
- package/lib/pdf/issue/index.js +141 -135
- package/lib/pdf/issue/index.js.map +1 -1
- package/lib/pdf/job/index.js +95 -81
- package/lib/pdf/job/index.js.map +1 -1
- package/lib/pdf/shift-report/emptyFixtures.js +8 -2
- package/lib/pdf/shift-report/emptyFixtures.js.map +1 -1
- package/lib/pdf/shift-report/index.js +399 -521
- package/lib/pdf/shift-report/index.js.map +1 -1
- package/lib/pdf/task/index.js +89 -83
- package/lib/pdf/task/index.js.map +1 -1
- package/lib/scheduling/generators/index.js +38 -3
- package/lib/scheduling/generators/index.js.map +1 -1
- package/lib/scheduling/generators/occurrenceIntervalsGenerator.js +44 -37
- package/lib/scheduling/generators/occurrenceIntervalsGenerator.js.map +1 -1
- package/lib/scheduling/generators/scheduleIntervalsGenerator.js +20 -28
- package/lib/scheduling/generators/scheduleIntervalsGenerator.js.map +1 -1
- package/lib/scheduling/generators/serviceIntervalsGenerator.js +120 -150
- package/lib/scheduling/generators/serviceIntervalsGenerator.js.map +1 -1
- package/lib/scheduling/helpers/convertToTimezone.js +18 -11
- package/lib/scheduling/helpers/convertToTimezone.js.map +1 -1
- package/lib/scheduling/helpers/generateNonRepeatingSchedule.js +57 -95
- package/lib/scheduling/helpers/generateNonRepeatingSchedule.js.map +1 -1
- package/lib/scheduling/helpers/generateRepeatingSchedule.js +80 -119
- package/lib/scheduling/helpers/generateRepeatingSchedule.js.map +1 -1
- package/lib/scheduling/helpers/generateScheduleEnd.js +24 -14
- package/lib/scheduling/helpers/generateScheduleEnd.js.map +1 -1
- package/lib/scheduling/helpers/hasValidStartAndEnd.js +19 -11
- package/lib/scheduling/helpers/hasValidStartAndEnd.js.map +1 -1
- package/lib/scheduling/helpers/index.js +159 -14
- package/lib/scheduling/helpers/index.js.map +1 -1
- package/lib/scheduling/helpers/intervalCovers.js +13 -7
- package/lib/scheduling/helpers/intervalCovers.js.map +1 -1
- package/lib/scheduling/helpers/intervalIntersectsEnd.js +13 -7
- package/lib/scheduling/helpers/intervalIntersectsEnd.js.map +1 -1
- package/lib/scheduling/helpers/intervalIntersectsStart.js +13 -7
- package/lib/scheduling/helpers/intervalIntersectsStart.js.map +1 -1
- package/lib/scheduling/helpers/intervalOnlyIntersectsEnd.js +15 -10
- package/lib/scheduling/helpers/intervalOnlyIntersectsEnd.js.map +1 -1
- package/lib/scheduling/helpers/intervalOnlyIntersectsStart.js +15 -10
- package/lib/scheduling/helpers/intervalOnlyIntersectsStart.js.map +1 -1
- package/lib/scheduling/helpers/intervalOutside.js +15 -9
- package/lib/scheduling/helpers/intervalOutside.js.map +1 -1
- package/lib/scheduling/helpers/intervalWithin.js +13 -7
- package/lib/scheduling/helpers/intervalWithin.js.map +1 -1
- package/lib/scheduling/helpers/mergeIntervals.js +21 -17
- package/lib/scheduling/helpers/mergeIntervals.js.map +1 -1
- package/lib/scheduling/helpers/splitIntervals.js +24 -23
- package/lib/scheduling/helpers/splitIntervals.js.map +1 -1
- package/lib/scheduling/index.js +38 -3
- package/lib/scheduling/index.js.map +1 -1
- package/lib/scheduling/scheduling.types.js +9 -3
- package/lib/scheduling/scheduling.types.js.map +1 -1
- package/lib/scheduling/strategies/getNext.js +57 -63
- package/lib/scheduling/strategies/getNext.js.map +1 -1
- package/lib/scheduling/strategies/getNextExactDateOfMonth.js +47 -56
- package/lib/scheduling/strategies/getNextExactDateOfMonth.js.map +1 -1
- package/lib/scheduling/strategies/getNextLastDayOfMonth.js +43 -51
- package/lib/scheduling/strategies/getNextLastDayOfMonth.js.map +1 -1
- package/lib/scheduling/strategies/getNextLastWeekdayOfMonth.js +49 -56
- package/lib/scheduling/strategies/getNextLastWeekdayOfMonth.js.map +1 -1
- package/lib/scheduling/strategies/getNextNoRepeat.js +25 -36
- package/lib/scheduling/strategies/getNextNoRepeat.js.map +1 -1
- package/lib/scheduling/strategies/getNextWeekday.js +47 -79
- package/lib/scheduling/strategies/getNextWeekday.js.map +1 -1
- package/lib/scheduling/strategies/getNextXDayOfXWeekOfMonth.js +52 -58
- package/lib/scheduling/strategies/getNextXDayOfXWeekOfMonth.js.map +1 -1
- package/lib/scheduling/strategies/index.js +82 -7
- package/lib/scheduling/strategies/index.js.map +1 -1
- package/lib/schema/index.js +16 -1
- package/lib/schema/index.js.map +1 -1
- package/lib/schema/schema.js +28 -34
- package/lib/schema/schema.js.map +1 -1
- package/lib/service-hours/__helpers__/calculate-hours.js +24 -22
- package/lib/service-hours/__helpers__/calculate-hours.js.map +1 -1
- package/lib/service-hours/__helpers__/isOpen.js +25 -24
- package/lib/service-hours/__helpers__/isOpen.js.map +1 -1
- package/lib/service-hours/index.js +27 -2
- package/lib/service-hours/index.js.map +1 -1
- package/lib/service-hours/schema.js +68 -55
- package/lib/service-hours/schema.js.map +1 -1
- package/package.json +86 -86
- package/vitest.config.ts +23 -23
- package/vitest.setup.js +7 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationError.js","names":["
|
|
1
|
+
{"version":3,"file":"ValidationError.js","names":["_yup","require","_DomainError","_interopRequireDefault","DEFAULT_MESSAGE","ValidationError","DomainError","constructor","data","message","SchemaValidationError","validationMessage","name","other","status","exports","default"],"sources":["../../src/errors/ValidationError.ts"],"sourcesContent":["import { ValidationError as SchemaValidationError } from 'yup'\r\nimport DomainError from './DomainError'\r\n\r\ninterface Options {\r\n data?: any\r\n message?: string\r\n}\r\n\r\nconst DEFAULT_MESSAGE =\r\n 'The data you submitted was invalid. Please try again with valid data.'\r\n\r\nexport default class ValidationError extends DomainError {\r\n public data: object\r\n public message: string\r\n public status: number\r\n constructor({ data, message = DEFAULT_MESSAGE }: Options) {\r\n // When the data object is a schema validation error, take its data\r\n // attributes and apply to our wrapped error\r\n if (data instanceof SchemaValidationError) {\r\n const { message: validationMessage, name, ...other } = data\r\n\r\n super(validationMessage)\r\n this.data = other\r\n this.status = 400\r\n return\r\n }\r\n\r\n super(message)\r\n this.data = data\r\n this.status = 400\r\n }\r\n}\r\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAC,sBAAA,CAAAF,OAAA;AAOA,MAAMG,eAAe,GACnB,uEAAuE;AAE1D,MAAMC,eAAe,SAASC,oBAAW,CAAC;EAIvDC,WAAWA,CAAC;IAAEC,IAAI;IAAEC,OAAO,GAAGL;EAAyB,CAAC,EAAE;IACxD;IACA;IACA,IAAII,IAAI,YAAYE,oBAAqB,EAAE;MACzC,MAAM;QAAED,OAAO,EAAEE,iBAAiB;QAAEC,IAAI;QAAE,GAAGC;MAAM,CAAC,GAAGL,IAAI;MAE3D,KAAK,CAACG,iBAAiB,CAAC;MACxB,IAAI,CAACH,IAAI,GAAGK,KAAK;MACjB,IAAI,CAACC,MAAM,GAAG,GAAG;MACjB;IACF;IAEA,KAAK,CAACL,OAAO,CAAC;IACd,IAAI,CAACD,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACM,MAAM,GAAG,GAAG;EACnB;AACF;AAACC,OAAA,CAAAC,OAAA,GAAAX,eAAA","ignoreList":[]}
|
package/lib/errors/index.js
CHANGED
|
@@ -1,7 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "DomainError", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _DomainError.default;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "ValidationError", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _ValidationError.default;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
exports.isKnownError = isKnownError;
|
|
20
|
+
var _DomainError = _interopRequireDefault(require("./DomainError"));
|
|
21
|
+
var _ValidationError = _interopRequireDefault(require("./ValidationError"));
|
|
22
|
+
function isKnownError(err) {
|
|
23
|
+
return err instanceof _DomainError.default;
|
|
6
24
|
}
|
|
7
25
|
//# sourceMappingURL=index.js.map
|
package/lib/errors/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["
|
|
1
|
+
{"version":3,"file":"index.js","names":["_DomainError","_interopRequireDefault","require","_ValidationError","isKnownError","err","DomainError"],"sources":["../../src/errors/index.ts"],"sourcesContent":["import DomainError from './DomainError'\r\n\r\nexport { DomainError }\r\nexport { default as ValidationError } from './ValidationError'\r\n\r\nexport function isKnownError(err: Error): boolean {\r\n return err instanceof DomainError\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,sBAAA,CAAAC,OAAA;AAGA,IAAAC,gBAAA,GAAAF,sBAAA,CAAAC,OAAA;AAEO,SAASE,YAAYA,CAACC,GAAU,EAAW;EAChD,OAAOA,GAAG,YAAYC,oBAAW;AACnC","ignoreList":[]}
|
|
@@ -1,33 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.buildFetchUrl = buildFetchUrl;
|
|
7
|
+
var _index = require("../validate-url/index.js");
|
|
8
|
+
function buildFetchUrl(url, options) {
|
|
5
9
|
// Validate url parameter
|
|
6
10
|
if (!url || typeof url !== 'string') {
|
|
7
|
-
throw new Error(
|
|
11
|
+
throw new Error(`buildFetchUrl: Invalid url parameter. url=${JSON.stringify(url)}`);
|
|
8
12
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
cloudfrontBaseUrl =
|
|
12
|
-
cloudinaryBaseUrl
|
|
13
|
-
fit
|
|
14
|
-
height
|
|
15
|
-
width
|
|
16
|
-
quality
|
|
17
|
-
shouldUseCloudfront
|
|
18
|
-
Policy
|
|
19
|
-
KeyPairId
|
|
20
|
-
Signature
|
|
13
|
+
const {
|
|
14
|
+
awsS3BaseUrl,
|
|
15
|
+
cloudfrontBaseUrl = '',
|
|
16
|
+
cloudinaryBaseUrl,
|
|
17
|
+
fit,
|
|
18
|
+
height,
|
|
19
|
+
width,
|
|
20
|
+
quality,
|
|
21
|
+
shouldUseCloudfront,
|
|
22
|
+
Policy,
|
|
23
|
+
KeyPairId,
|
|
24
|
+
Signature
|
|
25
|
+
} = options;
|
|
21
26
|
if (shouldUseCloudfront) {
|
|
22
27
|
// Validate cloudfrontBaseUrl when using CloudFront
|
|
23
28
|
if (!cloudfrontBaseUrl) {
|
|
24
|
-
throw new Error(
|
|
29
|
+
throw new Error(`buildFetchUrl: cloudfrontBaseUrl is required when shouldUseCloudfront=true. cloudfrontBaseUrl=${JSON.stringify(cloudfrontBaseUrl)}`);
|
|
25
30
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
width
|
|
29
|
-
height
|
|
30
|
-
quality
|
|
31
|
+
const isWebContext = shouldUseCloudfront && typeof window === 'object';
|
|
32
|
+
const paramMap = {
|
|
33
|
+
width,
|
|
34
|
+
height,
|
|
35
|
+
quality,
|
|
31
36
|
fit: 'contain'
|
|
32
37
|
};
|
|
33
38
|
if (isWebContext) {
|
|
@@ -35,42 +40,33 @@ export function buildFetchUrl(url, options) {
|
|
|
35
40
|
paramMap['Key-Pair-Id'] = KeyPairId;
|
|
36
41
|
paramMap.Signature = Signature;
|
|
37
42
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return value != null;
|
|
42
|
-
}).map(function (_ref3) {
|
|
43
|
-
var _ref4 = _slicedToArray(_ref3, 2),
|
|
44
|
-
key = _ref4[0],
|
|
45
|
-
value = _ref4[1];
|
|
46
|
-
return "".concat(key, "=").concat(String(value));
|
|
47
|
-
});
|
|
48
|
-
var paramsString = params.join('&');
|
|
49
|
-
var result = "".concat(cloudfrontBaseUrl, "/").concat(url, "?").concat(paramsString);
|
|
43
|
+
const params = Object.entries(paramMap).filter(([, value]) => value != null).map(([key, value]) => `${key}=${String(value)}`);
|
|
44
|
+
const paramsString = params.join('&');
|
|
45
|
+
const result = `${cloudfrontBaseUrl}/${url}?${paramsString}`;
|
|
50
46
|
|
|
51
47
|
// Validate output is absolute URL
|
|
52
|
-
if (!isAbsoluteUrl(result)) {
|
|
53
|
-
throw new Error(
|
|
48
|
+
if (!(0, _index.isAbsoluteUrl)(result)) {
|
|
49
|
+
throw new Error(`buildFetchUrl: Constructed URL is not absolute. cloudfrontBaseUrl=${JSON.stringify(cloudfrontBaseUrl)}, url=${JSON.stringify(url)}, result=${JSON.stringify(result)}`);
|
|
54
50
|
}
|
|
55
51
|
return result;
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
// Validate required base URLs for Cloudinary path
|
|
59
55
|
if (!cloudinaryBaseUrl || !awsS3BaseUrl) {
|
|
60
|
-
throw new Error(
|
|
56
|
+
throw new Error(`buildFetchUrl: cloudinaryBaseUrl and awsS3BaseUrl are required. cloudinaryBaseUrl=${JSON.stringify(cloudinaryBaseUrl)}, awsS3BaseUrl=${JSON.stringify(awsS3BaseUrl)}`);
|
|
61
57
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (width) transformations.push(
|
|
65
|
-
if (height) transformations.push(
|
|
66
|
-
if (quality) transformations.push(
|
|
58
|
+
const transformations = [];
|
|
59
|
+
let transformationsString = '';
|
|
60
|
+
if (width) transformations.push(`w_${width.toString()}`);
|
|
61
|
+
if (height) transformations.push(`h_${height.toString()}`);
|
|
62
|
+
if (quality) transformations.push(`q_${quality.toString()}`);
|
|
67
63
|
if (fit) transformations.push('c_fit');
|
|
68
|
-
transformationsString =
|
|
69
|
-
|
|
64
|
+
transformationsString = `${transformations.join(',')}/`;
|
|
65
|
+
const fetchUrl = `${cloudinaryBaseUrl}/${transformationsString}${awsS3BaseUrl}/${url}`;
|
|
70
66
|
|
|
71
67
|
// Validate output is absolute URL
|
|
72
|
-
if (!isAbsoluteUrl(fetchUrl)) {
|
|
73
|
-
throw new Error(
|
|
68
|
+
if (!(0, _index.isAbsoluteUrl)(fetchUrl)) {
|
|
69
|
+
throw new Error(`buildFetchUrl: Constructed URL is not absolute. cloudinaryBaseUrl=${JSON.stringify(cloudinaryBaseUrl)}, awsS3BaseUrl=${JSON.stringify(awsS3BaseUrl)}, url=${JSON.stringify(url)}, result=${JSON.stringify(fetchUrl)}`);
|
|
74
70
|
}
|
|
75
71
|
return fetchUrl;
|
|
76
72
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["
|
|
1
|
+
{"version":3,"file":"index.js","names":["_index","require","buildFetchUrl","url","options","Error","JSON","stringify","awsS3BaseUrl","cloudfrontBaseUrl","cloudinaryBaseUrl","fit","height","width","quality","shouldUseCloudfront","Policy","KeyPairId","Signature","isWebContext","window","paramMap","params","Object","entries","filter","value","map","key","String","paramsString","join","result","isAbsoluteUrl","transformations","transformationsString","push","toString","fetchUrl"],"sources":["../../../src/helpers/build-fetch-url/index.js"],"sourcesContent":["import { isAbsoluteUrl } from '../validate-url/index.js'\r\n\r\nexport function buildFetchUrl(url, options) {\r\n // Validate url parameter\r\n if (!url || typeof url !== 'string') {\r\n throw new Error(\r\n `buildFetchUrl: Invalid url parameter. url=${JSON.stringify(url)}`\r\n )\r\n }\r\n\r\n const {\r\n awsS3BaseUrl,\r\n cloudfrontBaseUrl = '',\r\n cloudinaryBaseUrl,\r\n fit,\r\n height,\r\n width,\r\n quality,\r\n shouldUseCloudfront,\r\n Policy,\r\n KeyPairId,\r\n Signature,\r\n } = options\r\n\r\n if (shouldUseCloudfront) {\r\n // Validate cloudfrontBaseUrl when using CloudFront\r\n if (!cloudfrontBaseUrl) {\r\n throw new Error(\r\n `buildFetchUrl: cloudfrontBaseUrl is required when shouldUseCloudfront=true. cloudfrontBaseUrl=${JSON.stringify(cloudfrontBaseUrl)}`\r\n )\r\n }\r\n const isWebContext = shouldUseCloudfront && typeof window === 'object'\r\n\r\n const paramMap = {\r\n width,\r\n height,\r\n quality,\r\n fit: 'contain',\r\n }\r\n\r\n if (isWebContext) {\r\n paramMap.Policy = Policy\r\n paramMap['Key-Pair-Id'] = KeyPairId\r\n paramMap.Signature = Signature\r\n }\r\n const params = Object.entries(paramMap)\r\n .filter(([, value]) => value != null)\r\n .map(([key, value]) => `${key}=${String(value)}`)\r\n\r\n const paramsString = params.join('&')\r\n const result = `${cloudfrontBaseUrl}/${url}?${paramsString}`\r\n\r\n // Validate output is absolute URL\r\n if (!isAbsoluteUrl(result)) {\r\n throw new Error(\r\n `buildFetchUrl: Constructed URL is not absolute. cloudfrontBaseUrl=${JSON.stringify(cloudfrontBaseUrl)}, url=${JSON.stringify(url)}, result=${JSON.stringify(result)}`\r\n )\r\n }\r\n\r\n return result\r\n }\r\n\r\n // Validate required base URLs for Cloudinary path\r\n if (!cloudinaryBaseUrl || !awsS3BaseUrl) {\r\n throw new Error(\r\n `buildFetchUrl: cloudinaryBaseUrl and awsS3BaseUrl are required. cloudinaryBaseUrl=${JSON.stringify(cloudinaryBaseUrl)}, awsS3BaseUrl=${JSON.stringify(awsS3BaseUrl)}`\r\n )\r\n }\r\n\r\n const transformations = []\r\n let transformationsString = ''\r\n\r\n if (width) transformations.push(`w_${width.toString()}`)\r\n if (height) transformations.push(`h_${height.toString()}`)\r\n if (quality) transformations.push(`q_${quality.toString()}`)\r\n if (fit) transformations.push('c_fit')\r\n\r\n transformationsString = `${transformations.join(',')}/`\r\n\r\n const fetchUrl = `${cloudinaryBaseUrl}/${transformationsString}${awsS3BaseUrl}/${url}`\r\n\r\n // Validate output is absolute URL\r\n if (!isAbsoluteUrl(fetchUrl)) {\r\n throw new Error(\r\n `buildFetchUrl: Constructed URL is not absolute. cloudinaryBaseUrl=${JSON.stringify(cloudinaryBaseUrl)}, awsS3BaseUrl=${JSON.st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B,IAAIyB,qBAAqB,GAAG3B,YAAY,IAAIL,GAAG,EAAE;;EAEtF;EACA,IAAI,CAAC,IAAA8B,oBAAa,EAACK,QAAQ,CAAC,EAAE;IAC5B,MAAM,IAAIjC,KAAK,CACb,qEAAqEC,IAAI,CAACC,SAAS,CAACG,iBAAiB,CAAC,kBAAkBJ,IAAI,CAACC,SAAS,CAACC,YAAY,CAAC,SAASF,IAAI,CAACC,SAAS,CAACJ,GAAG,CAAC,YAAYG,IAAI,CAACC,SAAS,CAAC+B,QAAQ,CAAC,EACtN,CAAC;EACH;EAEA,OAAOA,QAAQ;AACjB","ignoreList":[]}
|
|
@@ -1,26 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import _typeof from "@babel/runtime/helpers/typeof";
|
|
3
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
4
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
5
|
-
import fetchPonyfill from 'fetch-ponyfill';
|
|
6
|
-
import Promise from 'bluebird';
|
|
7
|
-
import { LIGHTHOUSE_LOGO_URL } from '../../constants';
|
|
8
|
-
import { imageNotFound } from '../../images';
|
|
9
|
-
import { fetchImageForPdfGeneratorService } from '../fetch-image-for-pdf-generator-service';
|
|
10
|
-
import { fetchImageForWeb } from '../fetch-image-for-web';
|
|
11
|
-
import { isAbsoluteUrl } from '../validate-url/index.js';
|
|
1
|
+
"use strict";
|
|
12
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.arrayBufferToBase64 = arrayBufferToBase64;
|
|
8
|
+
exports.fetchImage = fetchImage;
|
|
9
|
+
exports.validateBase64Image = validateBase64Image;
|
|
10
|
+
exports.validateJpegImage = validateJpegImage;
|
|
11
|
+
exports.validatePngImage = validatePngImage;
|
|
12
|
+
var _fetchPonyfill = _interopRequireDefault(require("fetch-ponyfill"));
|
|
13
|
+
var _bluebird = _interopRequireDefault(require("bluebird"));
|
|
14
|
+
var _constants = require("../../constants");
|
|
15
|
+
var _images = require("../../images");
|
|
16
|
+
var _fetchImageForPdfGeneratorService = require("../fetch-image-for-pdf-generator-service");
|
|
17
|
+
var _fetchImageForWeb = require("../fetch-image-for-web");
|
|
18
|
+
var _index = require("../validate-url/index.js");
|
|
13
19
|
// NOTE use the native fetch if it's available in the browser, because the
|
|
14
20
|
// ponyfill (which actually uses the github polyfill) does not support all the
|
|
15
21
|
// same options as native fetch
|
|
16
|
-
|
|
17
|
-
Promise:
|
|
22
|
+
const fetch = typeof self === 'object' && self.fetch || (0, _fetchPonyfill.default)({
|
|
23
|
+
Promise: _bluebird.default
|
|
18
24
|
}).fetch;
|
|
19
|
-
|
|
25
|
+
const contentTypes = {
|
|
20
26
|
'image/png': 'png',
|
|
21
27
|
'image/jpeg': 'jpeg'
|
|
22
28
|
};
|
|
23
|
-
|
|
29
|
+
const defaultOptions = {
|
|
24
30
|
// NOTE The cache: no-cache option is important to avoid an issue with CORS
|
|
25
31
|
// and caching on Chrome. Here's a good explanation of the issue:
|
|
26
32
|
// https://stackoverflow.com/a/37455118
|
|
@@ -30,128 +36,123 @@ var defaultOptions = {
|
|
|
30
36
|
// sent from the fetch request, causing an error
|
|
31
37
|
cache: 'no-cache'
|
|
32
38
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
context = _options$context === void 0 ? {} : _options$context;
|
|
39
|
+
function fetchImage(url, options = {}) {
|
|
40
|
+
const {
|
|
41
|
+
shouldUseCloudfront,
|
|
42
|
+
isHeader = false,
|
|
43
|
+
context = {}
|
|
44
|
+
} = options;
|
|
40
45
|
|
|
41
46
|
// Validate url parameter
|
|
42
47
|
if (!url || typeof url !== 'string') {
|
|
43
|
-
|
|
48
|
+
const error = new Error(`fetchImage: Invalid url parameter. url=${JSON.stringify(url)}`);
|
|
44
49
|
if (isHeader) {
|
|
45
50
|
console.error('FetchImageHeaderError', {
|
|
46
51
|
message: error.message,
|
|
47
|
-
url
|
|
48
|
-
context
|
|
52
|
+
url,
|
|
53
|
+
context
|
|
49
54
|
});
|
|
50
|
-
return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions);
|
|
55
|
+
return fetchImage(_constants.LIGHTHOUSE_LOGO_URL, defaultOptions);
|
|
51
56
|
}
|
|
52
57
|
console.error('FetchImageError', {
|
|
53
58
|
message: error.message,
|
|
54
|
-
url
|
|
55
|
-
context
|
|
59
|
+
url,
|
|
60
|
+
context
|
|
56
61
|
});
|
|
57
|
-
return
|
|
62
|
+
return _bluebird.default.resolve(_images.imageNotFound);
|
|
58
63
|
}
|
|
59
64
|
if (shouldUseCloudfront) {
|
|
60
|
-
|
|
65
|
+
const isWebContext = typeof window === 'object';
|
|
61
66
|
return isWebContext ?
|
|
62
67
|
// Values used from options: isHeader, Signature, Policy, KeyPairId
|
|
63
|
-
fetchImageForWeb(url, options) : fetchImageForPdfGeneratorService(url);
|
|
68
|
+
(0, _fetchImageForWeb.fetchImageForWeb)(url, options) : (0, _fetchImageForPdfGeneratorService.fetchImageForPdfGeneratorService)(url);
|
|
64
69
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
const encodedUrl = encodeURI(url);
|
|
71
|
+
const fetchOptions = {
|
|
72
|
+
...defaultOptions,
|
|
73
|
+
...options
|
|
74
|
+
};
|
|
75
|
+
return fetch(encodedUrl, fetchOptions).then(response => {
|
|
76
|
+
const contentHeader = response.headers.get('content-length');
|
|
77
|
+
const contentType = response.headers.get('content-type');
|
|
70
78
|
|
|
71
79
|
// NOTE: the response will be ok but we won't be able to render any
|
|
72
80
|
// image meaning pdfmake will error. Raise error here and return early.
|
|
73
81
|
if (contentHeader === '0') {
|
|
74
|
-
return
|
|
82
|
+
return _bluebird.default.reject(new Error(`Failed to fetch image as no content length: ${url}`));
|
|
75
83
|
}
|
|
76
84
|
if (!response.ok) {
|
|
77
|
-
return
|
|
85
|
+
return _bluebird.default.reject(new Error(`Failed to fetch image: ${url}`));
|
|
78
86
|
}
|
|
79
|
-
|
|
80
|
-
return response.arrayBuffer().then(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
var isValid = validateBase64Image(base64);
|
|
87
|
+
const imageType = contentTypes[contentType];
|
|
88
|
+
return response.arrayBuffer().then(buffer => ({
|
|
89
|
+
buffer,
|
|
90
|
+
imageType
|
|
91
|
+
}));
|
|
92
|
+
}).then(({
|
|
93
|
+
buffer,
|
|
94
|
+
imageType
|
|
95
|
+
}) => {
|
|
96
|
+
const base64Flag = `data:image/${imageType};base64,`;
|
|
97
|
+
const imageStr = arrayBufferToBase64(buffer);
|
|
98
|
+
const base64 = `${base64Flag}${imageStr}`;
|
|
99
|
+
const isValid = validateBase64Image(base64);
|
|
93
100
|
if (!isValid) {
|
|
94
|
-
return
|
|
101
|
+
return _bluebird.default.reject(new Error('InvalidImageError'));
|
|
95
102
|
}
|
|
96
103
|
return base64;
|
|
97
|
-
}).catch(
|
|
104
|
+
}).catch(error => {
|
|
98
105
|
if (isHeader) {
|
|
99
106
|
// NOTE: Replace failed headers with LH logo
|
|
100
107
|
console.error('FetchImageHeaderError', {
|
|
101
|
-
url
|
|
108
|
+
url,
|
|
102
109
|
message: error.message,
|
|
103
|
-
context
|
|
110
|
+
context,
|
|
104
111
|
stack: error.stack
|
|
105
112
|
});
|
|
106
|
-
return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions);
|
|
113
|
+
return fetchImage(_constants.LIGHTHOUSE_LOGO_URL, defaultOptions);
|
|
107
114
|
}
|
|
108
115
|
console.error('FetchImageError', {
|
|
109
|
-
url
|
|
116
|
+
url,
|
|
110
117
|
message: error.message,
|
|
111
|
-
context
|
|
118
|
+
context,
|
|
112
119
|
stack: error.stack
|
|
113
120
|
});
|
|
114
|
-
return imageNotFound;
|
|
121
|
+
return _images.imageNotFound;
|
|
115
122
|
});
|
|
116
123
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
bytes.forEach(
|
|
121
|
-
return binary += String.fromCharCode(b);
|
|
122
|
-
});
|
|
124
|
+
function arrayBufferToBase64(buffer) {
|
|
125
|
+
let binary = '';
|
|
126
|
+
const bytes = [].slice.call(new Uint8Array(buffer));
|
|
127
|
+
bytes.forEach(b => binary += String.fromCharCode(b));
|
|
123
128
|
return btoa(binary);
|
|
124
129
|
}
|
|
125
|
-
|
|
126
|
-
|
|
130
|
+
function validateBase64Image(base64String) {
|
|
131
|
+
const isJpeg = base64String.startsWith('data:image/jpeg;base64,');
|
|
127
132
|
if (isJpeg) return validateJpegImage(base64String);
|
|
128
|
-
|
|
133
|
+
const isPng = base64String.startsWith('data:image/png;base64,');
|
|
129
134
|
if (isPng) return validatePngImage(base64String);
|
|
130
135
|
return false;
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
// See SO for more info: https://stackoverflow.com/a/41635312
|
|
134
139
|
// Fiddle: https://jsfiddle.net/Lnyxuchw/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
});
|
|
140
|
-
var imageCorrupted = imageData[imageData.length - 1] === 217 && imageData[imageData.length - 2] === 255;
|
|
140
|
+
function validateJpegImage(base64string) {
|
|
141
|
+
const src = base64string;
|
|
142
|
+
const imageData = Uint8Array.from(atob(src.replace('data:image/jpeg;base64,', '')), c => c.charCodeAt(0));
|
|
143
|
+
const imageCorrupted = imageData[imageData.length - 1] === 217 && imageData[imageData.length - 2] === 255;
|
|
141
144
|
return imageCorrupted;
|
|
142
145
|
}
|
|
143
146
|
|
|
144
147
|
// See SO for more info: https://stackoverflow.com/a/41635312
|
|
145
148
|
// Fiddle: https://jsfiddle.net/Lnyxuchw/
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
});
|
|
151
|
-
var sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130];
|
|
149
|
+
function validatePngImage(base64string) {
|
|
150
|
+
const src = base64string;
|
|
151
|
+
const imageData = Uint8Array.from(atob(src.replace('data:image/png;base64,', '')), c => c.charCodeAt(0));
|
|
152
|
+
const sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130];
|
|
152
153
|
|
|
153
154
|
//check last 12 elements of array so they contains needed values
|
|
154
|
-
for (
|
|
155
|
+
for (let i = 12; i > 0; i--) {
|
|
155
156
|
if (imageData[imageData.length - i] !== sequence[12 - i]) {
|
|
156
157
|
return false;
|
|
157
158
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","fetchImageForPdfGeneratorService","fetchImageForWeb","isAbsoluteUrl","fetch","self","_typeof","contentTypes","defaultOptions","cache","fetchImage","url","options","arguments","length","undefined","shouldUseCloudfront","_options$isHeader","isHeader","_options$context","context","error","Error","concat","JSON","stringify","console","message","resolve","isWebContext","window","encodedUrl","encodeURI","fetchOptions","_objectSpread","then","response","contentHeader","headers","get","contentType","reject","ok","imageType","arrayBuffer","buffer","_ref","base64Flag","imageStr","arrayBufferToBase64","base64","isValid","validateBase64Image","catch","stack","binary","bytes","slice","call","Uint8Array","forEach","b","String","fromCharCode","btoa","base64String","isJpeg","startsWith","validateJpegImage","isPng","validatePngImage","base64string","src","imageData","from","atob","replace","c","charCodeAt","imageCorrupted","sequence","i"],"sources":["../../../src/helpers/fetch-image/index.js"],"sourcesContent":["import 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'\nimport { isAbsoluteUrl } from '../validate-url/index.js'\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, isHeader = false, context = {} } = options\n\n // Validate url parameter\n if (!url || typeof url !== 'string') {\n const error = new Error(\n `fetchImage: Invalid url parameter. url=${JSON.stringify(url)}`\n )\n if (isHeader) {\n console.error('FetchImageHeaderError', {\n message: error.message,\n url,\n context,\n })\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\n }\n console.error('FetchImageError', {\n message: error.message,\n url,\n context,\n })\n return Promise.resolve(imageNotFound)\n }\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\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: ${url}`)\n )\n }\n\n if (!response.ok) {\n return Promise.reject(new Error(`Failed to fetch image: ${url}`))\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', {\n url,\n message: error.message,\n context,\n stack: error.stack,\n })\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\n }\n\n console.error('FetchImageError', {\n url,\n message: error.message,\n context,\n stack: error.stack,\n })\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,OAAOA,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;AACzD,SAASC,aAAa,QAAQ,0BAA0B;;AAExD;AACA;AACA;AACA,IAAMC,KAAK,GACR,QAAOC,IAAI,iCAAAC,OAAA,CAAJD,IAAI,OAAK,QAAQ,IAAIA,IAAI,CAACD,KAAK,IAAKP,aAAa,CAAC;EAAEC,OAAO,EAAPA;AAAQ,CAAC,CAAC,CAACM,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,OAAO,SAASC,UAAUA,CAACC,GAAG,EAAgB;EAAA,IAAdC,OAAO,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE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QAAQ,CAAC,EAAE,GAAGC,CAAC,CAAC,EAAE;MACxD,OAAO,KAAK;IACd;EACF;EAEA,OAAO,IAAI;AACb","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["_fetchPonyfill","_interopRequireDefault","require","_bluebird","_constants","_images","_fetchImageForPdfGeneratorService","_fetchImageForWeb","_index","fetch","self","fetchPonyfill","Promise","contentTypes","defaultOptions","cache","fetchImage","url","options","shouldUseCloudfront","isHeader","context","error","Error","JSON","stringify","console","message","LIGHTHOUSE_LOGO_URL","resolve","imageNotFound","isWebContext","window","fetchImageForWeb","fetchImageForPdfGeneratorService","encodedUrl","encodeURI","fetchOptions","then","response","contentHeader","headers","get","contentType","reject","ok","imageType","arrayBuffer","buffer","base64Flag","imageStr","arrayBufferToBase64","base64","isValid","validateBase64Image","catch","stack","binary","bytes","slice","call","Uint8Array","forEach","b","String","fromCharCode","btoa","base64String","isJpeg","startsWith","validateJpegImage","isPng","validatePngImage","base64string","src","imageData","from","atob","replace","c","charCodeAt","imageCorrupted","length","sequence","i"],"sources":["../../../src/helpers/fetch-image/index.js"],"sourcesContent":["import fetchPonyfill from 'fetch-ponyfill'\r\nimport Promise from 'bluebird'\r\nimport { LIGHTHOUSE_LOGO_URL } from '../../constants'\r\nimport { imageNotFound } from '../../images'\r\nimport { fetchImageForPdfGeneratorService } from '../fetch-image-for-pdf-generator-service'\r\nimport { fetchImageForWeb } from '../fetch-image-for-web'\r\nimport { isAbsoluteUrl } from '../validate-url/index.js'\r\n\r\n// NOTE use the native fetch if it's available in the browser, because the\r\n// ponyfill (which actually uses the github polyfill) does not support all the\r\n// same options as native fetch\r\nconst fetch =\r\n (typeof self === 'object' && self.fetch) || fetchPonyfill({ Promise }).fetch\r\n\r\nconst contentTypes = {\r\n 'image/png': 'png',\r\n 'image/jpeg': 'jpeg',\r\n}\r\n\r\nconst defaultOptions = {\r\n // NOTE The cache: no-cache option is important to avoid an issue with CORS\r\n // and caching on Chrome. Here's a good explanation of the issue:\r\n // https://stackoverflow.com/a/37455118\r\n // In our case, when loading the web version of a form, the signature image is\r\n // cached without the correct CORS headers. If the pdf is then generated,\r\n // there's a mismatch between the cached image headers and the CORS headers\r\n // sent from the fetch request, causing an error\r\n cache: 'no-cache',\r\n}\r\n\r\nexport function fetchImage(url, options = {}) {\r\n const { shouldUseCloudfront, isHeader = false, context = {} } = options\r\n\r\n // Validate url parameter\r\n if (!url || typeof url !== 'string') {\r\n const error = new Error(\r\n `fetchImage: Invalid url parameter. url=${JSON.stringify(url)}`\r\n )\r\n if (isHeader) {\r\n console.error('FetchImageHeaderError', {\r\n message: error.message,\r\n url,\r\n context,\r\n })\r\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\r\n }\r\n console.error('FetchImageError', {\r\n message: error.message,\r\n url,\r\n context,\r\n })\r\n return Promise.resolve(imageNotFound)\r\n }\r\n\r\n if (shouldUseCloudfront) {\r\n const isWebContext = typeof window === 'object'\r\n\r\n return isWebContext\r\n ? // Values used from options: isHeader, Signature, Policy, KeyPairId\r\n fetchImageForWeb(url, options)\r\n : fetchImageForPdfGeneratorService(url)\r\n }\r\n\r\n const encodedUrl = encodeURI(url)\r\n\r\n const fetchOptions = {\r\n ...defaultOptions,\r\n ...options,\r\n }\r\n\r\n return fetch(encodedUrl, fetchOptions)\r\n .then((response) => {\r\n const contentHeader = response.headers.get('content-length')\r\n const contentType = response.headers.get('content-type')\r\n\r\n // NOTE: the response will be ok but we won't be able to render any\r\n // image meaning pdfmake will error. Raise error here and return early.\r\n if (contentHeader === '0') {\r\n return Promise.reject(\r\n new Error(`Failed to fetch image as no content length: ${url}`)\r\n )\r\n }\r\n\r\n if (!response.ok) {\r\n return Promise.reject(new Error(`Failed to fetch image: ${url}`))\r\n }\r\n\r\n const imageType = contentTypes[contentType]\r\n\r\n return response.arrayBuffer().then((buffer) => ({\r\n buffer,\r\n imageType,\r\n }))\r\n })\r\n .then(({ buffer, imageType }) => {\r\n const base64Flag = `data:image/${imageType};base64,`\r\n const imageStr = arrayBufferToBase64(buffer)\r\n\r\n const base64 = `${base64Flag}${imageStr}`\r\n const isValid = validateBase64Image(base64)\r\n\r\n if (!isValid) {\r\n return Promise.reject(new Error('InvalidImageError'))\r\n }\r\n\r\n return base64\r\n })\r\n .catch((error) => {\r\n if (isHeader) {\r\n // NOTE: Replace failed headers with LH logo\r\n console.error('FetchImageHeaderError', {\r\n url,\r\n message: error.message,\r\n context,\r\n stack: error.stack,\r\n })\r\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\r\n }\r\n\r\n console.error('FetchImageError', {\r\n url,\r\n message: error.message,\r\n context,\r\n stack: error.stack,\r\n })\r\n return imageNotFound\r\n })\r\n}\r\n\r\nexport function arrayBufferToBase64(buffer) {\r\n let binary = ''\r\n const bytes = [].slice.call(new Uint8Array(buffer))\r\n\r\n bytes.forEach((b) => (binary += String.fromCharCode(b)))\r\n\r\n return btoa(binary)\r\n}\r\n\r\nexport function validateBase64Image(base64String) {\r\n const isJpeg = base64String.startsWith('data:image/jpeg;base64,')\r\n\r\n if (isJpeg) return validateJpegImage(base64String)\r\n\r\n const isPng = base64String.startsWith('data:image/png;base64,')\r\n\r\n if (isPng) return validatePngImage(base64String)\r\n\r\n return false\r\n}\r\n\r\n// See SO for more info: https://stackoverflow.com/a/41635312\r\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\r\nexport function validateJpegImage(base64string) {\r\n const src = base64string\r\n const imageData = Uint8Array.from(\r\n atob(src.replace('data:image/jpeg;base64,', '')),\r\n (c) => c.charCodeAt(0)\r\n )\r\n const imageCorrupted =\r\n imageData[imageData.length - 1] === 217 &&\r\n imageData[imageData.length - 2] === 255\r\n\r\n return imageCorrupted\r\n}\r\n\r\n// See SO for more info: https://stackoverflow.com/a/41635312\r\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\r\nexport function validatePngImage(base64string) {\r\n const src = base64string\r\n const imageData = Uint8Array.from(\r\n atob(src.replace('data:image/png;base64,', '')),\r\n (c) => c.charCodeAt(0)\r\n )\r\n const sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]\r\n\r\n //check last 12 elements of array so they contains needed values\r\n for (let i = 12; i > 0; i--) {\r\n if (imageData[imageData.length - i] !== sequence[12 - i]) {\r\n return false\r\n }\r\n }\r\n\r\n return true\r\n}\r\n"],"mappings":";;;;;;;;;;;AAAA,IAAAA,cAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,SAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,UAAA,GAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AACA,IAAAI,iCAAA,GAAAJ,OAAA;AACA,IAAAK,iBAAA,GAAAL,OAAA;AACA,IAAAM,MAAA,GAAAN,OAAA;AAEA;AACA;AACA;AACA,MAAMO,KAAK,GACR,OAAOC,IAAI,KAAK,QAAQ,IAAIA,IAAI,CAACD,KAAK,IAAK,IAAAE,sBAAa,EAAC;EAAEC,OAAO,EAAPA;AAAQ,CAAC,CAAC,CAACH,KAAK;AAE9E,MAAMI,YAAY,GAAG;EACnB,WAAW,EAAE,KAAK;EAClB,YAAY,EAAE;AAChB,CAAC;AAED,MAAMC,cAAc,GAAG;EACrB;EACA;EACA;EACA;EACA;EACA;EACA;EACAC,KAAK,EAAE;AACT,CAAC;AAEM,SAASC,UAAUA,CAACC,GAAG,EAAEC,OAAO,GAAG,CAAC,CAAC,EAAE;EAC5C,MAAM;IAAEC,mBAAmB;IAAEC,QAAQ,GAAG,KAAK;IAAEC,OAAO,GAAG,CAAC;EAAE,CAAC,GAAGH,OAAO;;EAEvE;EACA,IAAI,CAACD,GAAG,IAAI,OAAOA,GAAG,KAAK,QAAQ,EAAE;IACnC,MAAMK,KAAK,GAAG,IAAIC,KAAK,CACrB,0CAA0CC,IAAI,CAACC,SAAS,CAACR,GAAG,CAAC,EAC/D,CAAC;IACD,IAAIG,QAAQ,EAAE;MACZM,OAAO,CAACJ,KAAK,CAAC,u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