@atlaskit/media-viewer 54.11.28 → 54.12.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/AGENTS.md +21 -1
- package/CHANGELOG.md +50 -0
- package/dist/cjs/ItemViewerBase.js +3 -5
- package/dist/cjs/analytics/events/operational/preload.js +66 -0
- package/dist/cjs/analytics/ufoExperiences.js +1 -1
- package/dist/cjs/chunkLoaders.js +28 -0
- package/dist/cjs/components/media-viewer-loader.js +4 -5
- package/dist/cjs/preload.js +200 -0
- package/dist/cjs/viewers/image/imageRequestParams.js +26 -0
- package/dist/cjs/viewers/image/index.js +87 -27
- package/dist/cjs/warmedImages.js +177 -0
- package/dist/es2019/ItemViewerBase.js +3 -3
- package/dist/es2019/analytics/events/operational/preload.js +61 -0
- package/dist/es2019/analytics/ufoExperiences.js +1 -1
- package/dist/es2019/chunkLoaders.js +11 -0
- package/dist/es2019/components/media-viewer-loader.js +2 -1
- package/dist/es2019/preload.js +185 -0
- package/dist/es2019/viewers/image/imageRequestParams.js +18 -0
- package/dist/es2019/viewers/image/index.js +59 -7
- package/dist/es2019/warmedImages.js +157 -0
- package/dist/esm/ItemViewerBase.js +3 -3
- package/dist/esm/analytics/events/operational/preload.js +61 -0
- package/dist/esm/analytics/ufoExperiences.js +1 -1
- package/dist/esm/chunkLoaders.js +15 -0
- package/dist/esm/components/media-viewer-loader.js +2 -1
- package/dist/esm/preload.js +193 -0
- package/dist/esm/viewers/image/imageRequestParams.js +20 -0
- package/dist/esm/viewers/image/index.js +87 -27
- package/dist/esm/warmedImages.js +170 -0
- package/dist/types/MediaViewerError.d.ts +1 -1
- package/dist/types/analytics/events/operational/_mediaFile.d.ts +1 -1
- package/dist/types/analytics/events/operational/preload.d.ts +36 -0
- package/dist/types/chunkLoaders.d.ts +10 -0
- package/dist/types/preload.d.ts +58 -0
- package/dist/types/viewers/image/imageRequestParams.d.ts +15 -0
- package/dist/types/viewers/image/index.d.ts +2 -0
- package/dist/types/warmedImages.d.ts +60 -0
- package/package.json +6 -6
- package/preload/package.json +10 -0
|
@@ -21,7 +21,9 @@ var _fg = require("@atlaskit/platform-feature-flags/fg");
|
|
|
21
21
|
var _buildImgErrorDiagnostics = require("../../buildImgErrorDiagnostics");
|
|
22
22
|
var _outcome = require("../../domain/outcome");
|
|
23
23
|
var _MediaViewerError = require("../../MediaViewerError");
|
|
24
|
+
var _warmedImages = require("../../warmedImages");
|
|
24
25
|
var _baseViewer = require("../base-viewer");
|
|
26
|
+
var _imageRequestParams = require("./imageRequestParams");
|
|
25
27
|
var _interactiveImg = require("./interactive-img");
|
|
26
28
|
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
|
|
27
29
|
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
@@ -41,6 +43,8 @@ var ImageViewer = exports.ImageViewer = /*#__PURE__*/function (_BaseViewer) {
|
|
|
41
43
|
args[_key] = arguments[_key];
|
|
42
44
|
}
|
|
43
45
|
_this = _callSuper(this, ImageViewer, [].concat(args));
|
|
46
|
+
/** Counts `init` calls, so an `init` that a reset has replaced can tell it is stale. */
|
|
47
|
+
(0, _defineProperty2.default)(_this, "initRuns", 0);
|
|
44
48
|
(0, _defineProperty2.default)(_this, "onLoad", function () {
|
|
45
49
|
_this.props.onLoad();
|
|
46
50
|
_this.onMediaDisplayed();
|
|
@@ -68,10 +72,11 @@ var ImageViewer = exports.ImageViewer = /*#__PURE__*/function (_BaseViewer) {
|
|
|
68
72
|
key: "init",
|
|
69
73
|
value: function () {
|
|
70
74
|
var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
|
|
71
|
-
var _this$props, fileState, mediaClient, collectionName, traceContext, controller, orientation, objectUrl, originalBinaryImageUrl, isLocalFileReference, clientId, renderedBlobMimeType, preview, _yield$preview, value, origin,
|
|
75
|
+
var initRun, _this$props, fileState, mediaClient, collectionName, traceContext, controller, orientation, objectUrl, originalBinaryImageUrl, isLocalFileReference, clientId, renderedBlobMimeType, preview, _yield$preview, value, origin, warmed, watermarkVersion, blob, item, response, effectiveMimeType, unsupportedError, imgError, _t, _t2, _t3, _t4;
|
|
72
76
|
return _regenerator.default.wrap(function (_context) {
|
|
73
77
|
while (1) switch (_context.prev = _context.next) {
|
|
74
78
|
case 0:
|
|
79
|
+
initRun = this.initRuns += 1;
|
|
75
80
|
_this$props = this.props, fileState = _this$props.item, mediaClient = _this$props.mediaClient, collectionName = _this$props.collectionName, traceContext = _this$props.traceContext;
|
|
76
81
|
if (!(0, _mediaClient.isErrorFileState)(fileState)) {
|
|
77
82
|
_context.next = 1;
|
|
@@ -121,56 +126,111 @@ var ImageViewer = exports.ImageViewer = /*#__PURE__*/function (_BaseViewer) {
|
|
|
121
126
|
case 9:
|
|
122
127
|
objectUrl = value;
|
|
123
128
|
case 10:
|
|
124
|
-
_context.next =
|
|
129
|
+
_context.next = 25;
|
|
125
130
|
break;
|
|
126
131
|
case 11:
|
|
127
132
|
if (!(0, _mediaClient.isImageRepresentationReady)(fileState)) {
|
|
128
|
-
_context.next =
|
|
133
|
+
_context.next = 24;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
if (!(0, _warmedImages.hasWarmedImageForFile)({
|
|
137
|
+
id: fileState.id,
|
|
138
|
+
collectionName: collectionName
|
|
139
|
+
})) {
|
|
140
|
+
_context.next = 16;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
_context.prev = 12;
|
|
144
|
+
_context.next = 13;
|
|
145
|
+
return mediaClient.getImageWatermarkVersion(collectionName);
|
|
146
|
+
case 13:
|
|
147
|
+
watermarkVersion = _context.sent;
|
|
148
|
+
if (!(!this.mounted || initRun !== this.initRuns)) {
|
|
149
|
+
_context.next = 14;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
return _context.abrupt("return");
|
|
153
|
+
case 14:
|
|
154
|
+
warmed = (0, _warmedImages.takeWarmedImage)((0, _warmedImages.getWarmedImageKey)({
|
|
155
|
+
id: fileState.id,
|
|
156
|
+
collectionName: collectionName,
|
|
157
|
+
watermarkVersion: watermarkVersion
|
|
158
|
+
}));
|
|
159
|
+
_context.next = 16;
|
|
160
|
+
break;
|
|
161
|
+
case 15:
|
|
162
|
+
_context.prev = 15;
|
|
163
|
+
_t2 = _context["catch"](12);
|
|
164
|
+
case 16:
|
|
165
|
+
if (!warmed) {
|
|
166
|
+
_context.next = 21;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
this.cancelImageFetch = function () {
|
|
170
|
+
return warmed.controller.abort();
|
|
171
|
+
};
|
|
172
|
+
_context.prev = 17;
|
|
173
|
+
_context.next = 18;
|
|
174
|
+
return warmed.image;
|
|
175
|
+
case 18:
|
|
176
|
+
blob = _context.sent;
|
|
177
|
+
_context.next = 20;
|
|
178
|
+
break;
|
|
179
|
+
case 19:
|
|
180
|
+
_context.prev = 19;
|
|
181
|
+
_t3 = _context["catch"](17);
|
|
182
|
+
case 20:
|
|
183
|
+
if (!warmed.controller.signal.aborted) {
|
|
184
|
+
_context.next = 21;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
return _context.abrupt("return");
|
|
188
|
+
case 21:
|
|
189
|
+
if (blob) {
|
|
190
|
+
_context.next = 23;
|
|
129
191
|
break;
|
|
130
192
|
}
|
|
131
193
|
item = processedFileStateToMediaItem(fileState);
|
|
132
|
-
response = mediaClient.getImage(item.details.id,
|
|
133
|
-
collection: collectionName,
|
|
134
|
-
mode: 'fit'
|
|
135
|
-
}, controller, true, traceContext);
|
|
194
|
+
response = mediaClient.getImage(item.details.id, (0, _imageRequestParams.getImageViewerRequestParams)(collectionName), controller, _imageRequestParams.IMAGE_VIEWER_FETCH_MAX_RES, traceContext);
|
|
136
195
|
this.cancelImageFetch = function () {
|
|
137
196
|
return controller === null || controller === void 0 ? void 0 : controller.abort();
|
|
138
197
|
};
|
|
139
|
-
_context.next =
|
|
198
|
+
_context.next = 22;
|
|
140
199
|
return response;
|
|
141
|
-
case
|
|
200
|
+
case 22:
|
|
142
201
|
blob = _context.sent;
|
|
202
|
+
case 23:
|
|
143
203
|
renderedBlobMimeType = blob.type || undefined;
|
|
144
204
|
objectUrl = URL.createObjectURL(blob);
|
|
145
|
-
_context.next =
|
|
205
|
+
_context.next = 25;
|
|
146
206
|
break;
|
|
147
|
-
case
|
|
207
|
+
case 24:
|
|
148
208
|
this.setState({
|
|
149
209
|
content: _outcome.Outcome.pending()
|
|
150
210
|
});
|
|
151
211
|
return _context.abrupt("return");
|
|
152
|
-
case
|
|
212
|
+
case 25:
|
|
153
213
|
if (!(!isLocalFileReference &&
|
|
154
214
|
// objectUrl at this point is binary file already
|
|
155
215
|
!(0, _mediaClient.isErrorFileState)(fileState) && fileState.status !== 'uploading' && fileState.mediaType === 'image')) {
|
|
156
|
-
_context.next =
|
|
216
|
+
_context.next = 28;
|
|
157
217
|
break;
|
|
158
218
|
}
|
|
159
219
|
// Prefer the MIME type of the actual fetched bytes over the declared one.
|
|
160
220
|
effectiveMimeType = (0, _fg.fg)('platform_media_unsupported_mime_routing') ? renderedBlobMimeType !== null && renderedBlobMimeType !== void 0 ? renderedBlobMimeType : fileState.mimeType : fileState.mimeType;
|
|
161
221
|
if (!(0, _isMimeTypeSupportedByBrowser.isImageMimeTypeSupportedByBrowser)(effectiveMimeType)) {
|
|
162
|
-
_context.next =
|
|
222
|
+
_context.next = 27;
|
|
163
223
|
break;
|
|
164
224
|
}
|
|
165
|
-
_context.next =
|
|
225
|
+
_context.next = 26;
|
|
166
226
|
return mediaClient.file.getFileBinaryURL(fileState.id, collectionName);
|
|
167
|
-
case
|
|
227
|
+
case 26:
|
|
168
228
|
originalBinaryImageUrl = _context.sent;
|
|
169
|
-
_context.next =
|
|
229
|
+
_context.next = 28;
|
|
170
230
|
break;
|
|
171
|
-
case
|
|
231
|
+
case 27:
|
|
172
232
|
if (!(0, _fg.fg)('platform_media_unsupported_mime_routing')) {
|
|
173
|
-
_context.next =
|
|
233
|
+
_context.next = 28;
|
|
174
234
|
break;
|
|
175
235
|
}
|
|
176
236
|
// Route unsupported MIME types browser can't natively decode straight to the
|
|
@@ -184,7 +244,7 @@ var ImageViewer = exports.ImageViewer = /*#__PURE__*/function (_BaseViewer) {
|
|
|
184
244
|
});
|
|
185
245
|
this.props.onError(unsupportedError);
|
|
186
246
|
return _context.abrupt("return");
|
|
187
|
-
case
|
|
247
|
+
case 28:
|
|
188
248
|
this.setState({
|
|
189
249
|
content: _outcome.Outcome.successful({
|
|
190
250
|
objectUrl: objectUrl,
|
|
@@ -193,24 +253,24 @@ var ImageViewer = exports.ImageViewer = /*#__PURE__*/function (_BaseViewer) {
|
|
|
193
253
|
clientId: clientId
|
|
194
254
|
})
|
|
195
255
|
});
|
|
196
|
-
_context.next =
|
|
256
|
+
_context.next = 30;
|
|
197
257
|
break;
|
|
198
|
-
case
|
|
199
|
-
_context.prev =
|
|
200
|
-
|
|
258
|
+
case 29:
|
|
259
|
+
_context.prev = 29;
|
|
260
|
+
_t4 = _context["catch"](2);
|
|
201
261
|
// TODO : properly handle aborted requests (MS-2843)
|
|
202
262
|
if (!(controller !== null && controller !== void 0 && controller.signal.aborted)) {
|
|
203
|
-
imgError = new _MediaViewerError.MediaViewerError('imageviewer-fetch-url',
|
|
263
|
+
imgError = new _MediaViewerError.MediaViewerError('imageviewer-fetch-url', _t4 instanceof Error ? _t4 : undefined);
|
|
204
264
|
this.setState({
|
|
205
265
|
content: _outcome.Outcome.failed(imgError)
|
|
206
266
|
});
|
|
207
267
|
this.props.onError(imgError);
|
|
208
268
|
}
|
|
209
|
-
case
|
|
269
|
+
case 30:
|
|
210
270
|
case "end":
|
|
211
271
|
return _context.stop();
|
|
212
272
|
}
|
|
213
|
-
}, _callee, this, [[2,
|
|
273
|
+
}, _callee, this, [[2, 29], [3, 5], [12, 15], [17, 19]]);
|
|
214
274
|
}));
|
|
215
275
|
function init() {
|
|
216
276
|
return _init.apply(this, arguments);
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.takeWarmedImage = exports.setWarmedImage = exports.hasWarmedImageForFile = exports.hasWarmedImage = exports.getWarmedImageKey = exports.clearWarmedImages = exports.WARMED_IMAGE_TTL_MS = void 0;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
+
/**
|
|
10
|
+
* Full resolution image fetches started before the viewer opens.
|
|
11
|
+
*
|
|
12
|
+
* `ImageViewer` takes the entry for the file it opens and awaits the same request, so
|
|
13
|
+
* the image downloads once even when the click lands while the warm-up is still
|
|
14
|
+
* downloading.
|
|
15
|
+
*
|
|
16
|
+
* Taking an entry hands over its `AbortController` too. The viewer then owns the
|
|
17
|
+
* download, and closing the viewer aborts it as it aborts the viewer's own request.
|
|
18
|
+
* Entries nobody takes are aborted when they are evicted or expire.
|
|
19
|
+
*
|
|
20
|
+
* Whoever takes the blob creates and revokes its own object URL.
|
|
21
|
+
*
|
|
22
|
+
* Entries are keyed by file, collection and watermark version. The watermark is
|
|
23
|
+
* applied server-side, so a warm-up made before a watermark config change carries
|
|
24
|
+
* the old watermark, and a viewer opened after the change must not take it.
|
|
25
|
+
*
|
|
26
|
+
* The store lives on `globalThis`, not in this module. A bundler can copy a small
|
|
27
|
+
* module into several bundles, each with its own module state, and the warm-up and
|
|
28
|
+
* `ImageViewer` load from different bundles. The shared key gives every copy of this
|
|
29
|
+
* file the same store.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* How many untaken warm-ups to hold at once. These are full resolution images and can
|
|
34
|
+
* be several megabytes each, so the ceiling is deliberately low: the point is the
|
|
35
|
+
* image the user is about to open, plus a little slack for hovering past one image on
|
|
36
|
+
* the way to another.
|
|
37
|
+
*/
|
|
38
|
+
var MAX_WARMED_IMAGES = 2;
|
|
39
|
+
|
|
40
|
+
/** How long an untaken warm-up is held. Covers a hover followed by a click. */
|
|
41
|
+
var WARMED_IMAGE_TTL_MS = exports.WARMED_IMAGE_TTL_MS = 30000;
|
|
42
|
+
|
|
43
|
+
/** How a warm-up left the store. */
|
|
44
|
+
|
|
45
|
+
// Versioned, so a later change to `Entry` never reads a store another version wrote.
|
|
46
|
+
var STORE_KEY = Symbol.for('@atlaskit/media-viewer/warmed-images/v1');
|
|
47
|
+
var getStore = function getStore() {
|
|
48
|
+
var scope = globalThis;
|
|
49
|
+
var store = scope[STORE_KEY];
|
|
50
|
+
if (!store) {
|
|
51
|
+
store = new Map();
|
|
52
|
+
scope[STORE_KEY] = store;
|
|
53
|
+
}
|
|
54
|
+
return store;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Listeners report analytics. A throw from one must not break a take, which runs
|
|
59
|
+
* inside `ImageViewer`, or an eviction, which runs inside the next warm-up.
|
|
60
|
+
*/
|
|
61
|
+
var notify = function notify(call) {
|
|
62
|
+
try {
|
|
63
|
+
call();
|
|
64
|
+
} catch (_unused) {
|
|
65
|
+
// Ignored, see above.
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var getWarmedImageKey = exports.getWarmedImageKey = function getWarmedImageKey(_ref) {
|
|
69
|
+
var id = _ref.id,
|
|
70
|
+
collectionName = _ref.collectionName,
|
|
71
|
+
watermarkVersion = _ref.watermarkVersion;
|
|
72
|
+
return (
|
|
73
|
+
// Joined as JSON, because a watermark version can contain any separator character.
|
|
74
|
+
JSON.stringify([id, collectionName !== null && collectionName !== void 0 ? collectionName : '', watermarkVersion !== null && watermarkVersion !== void 0 ? watermarkVersion : ''])
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
var hasWarmedImage = exports.hasWarmedImage = function hasWarmedImage(key) {
|
|
78
|
+
return getStore().has(key);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Whether any warm-up is held for the file, whatever its watermark version. Lets
|
|
83
|
+
* `ImageViewer` skip resolving the version when there is nothing it could take.
|
|
84
|
+
*/
|
|
85
|
+
var hasWarmedImageForFile = exports.hasWarmedImageForFile = function hasWarmedImageForFile(_ref2) {
|
|
86
|
+
var id = _ref2.id,
|
|
87
|
+
collectionName = _ref2.collectionName;
|
|
88
|
+
// The file's keys all start with the JSON of its first two parts, minus the closing
|
|
89
|
+
// bracket. JSON quotes each part, so no other file's key can share the prefix.
|
|
90
|
+
var prefix = "".concat(JSON.stringify([id, collectionName !== null && collectionName !== void 0 ? collectionName : '']).slice(0, -1), ",");
|
|
91
|
+
return (0, _toConsumableArray2.default)(getStore().keys()).some(function (key) {
|
|
92
|
+
return key.startsWith(prefix);
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
var removeWarmedImage = function removeWarmedImage(key, outcome) {
|
|
96
|
+
var store = getStore();
|
|
97
|
+
var entry = store.get(key);
|
|
98
|
+
if (entry) {
|
|
99
|
+
var _entry$listener;
|
|
100
|
+
clearTimeout(entry.expiry);
|
|
101
|
+
store.delete(key);
|
|
102
|
+
var onEnd = (_entry$listener = entry.listener) === null || _entry$listener === void 0 ? void 0 : _entry$listener.onEnd;
|
|
103
|
+
if (outcome && onEnd) {
|
|
104
|
+
notify(function () {
|
|
105
|
+
return onEnd(outcome, entry.blob);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return entry;
|
|
110
|
+
};
|
|
111
|
+
var discardWarmedImage = function discardWarmedImage(key, outcome) {
|
|
112
|
+
var _removeWarmedImage;
|
|
113
|
+
(_removeWarmedImage = removeWarmedImage(key, outcome)) === null || _removeWarmedImage === void 0 || _removeWarmedImage.controller.abort();
|
|
114
|
+
};
|
|
115
|
+
var setWarmedImage = exports.setWarmedImage = function setWarmedImage(key, _ref3, listener) {
|
|
116
|
+
var image = _ref3.image,
|
|
117
|
+
controller = _ref3.controller;
|
|
118
|
+
var store = getStore();
|
|
119
|
+
discardWarmedImage(key, 'replaced');
|
|
120
|
+
|
|
121
|
+
// `Map` iterates in insertion order, so the first key is the oldest.
|
|
122
|
+
if (store.size >= MAX_WARMED_IMAGES) {
|
|
123
|
+
var oldest = store.keys().next().value;
|
|
124
|
+
if (oldest !== undefined) {
|
|
125
|
+
discardWarmedImage(oldest, 'evicted');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
var entry = {
|
|
129
|
+
image: image,
|
|
130
|
+
controller: controller,
|
|
131
|
+
listener: listener,
|
|
132
|
+
expiry: setTimeout(function () {
|
|
133
|
+
return discardWarmedImage(key, 'expired');
|
|
134
|
+
}, WARMED_IMAGE_TTL_MS)
|
|
135
|
+
};
|
|
136
|
+
store.set(key, entry);
|
|
137
|
+
image.then(function (blob) {
|
|
138
|
+
entry.blob = blob;
|
|
139
|
+
}, function (error) {
|
|
140
|
+
// Every removal except a take aborts the request, and so does closing the
|
|
141
|
+
// viewer after a take. None of those are failures.
|
|
142
|
+
if (controller.signal.aborted) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// Drop a failed warm-up so a later hover can try again. This handler also marks
|
|
146
|
+
// the rejection as handled when nobody takes the warm-up.
|
|
147
|
+
var wasTaken = getStore().get(key) !== entry;
|
|
148
|
+
if (!wasTaken) {
|
|
149
|
+
removeWarmedImage(key, 'failed');
|
|
150
|
+
}
|
|
151
|
+
var onError = listener === null || listener === void 0 ? void 0 : listener.onError;
|
|
152
|
+
if (onError) {
|
|
153
|
+
notify(function () {
|
|
154
|
+
return onError(error, wasTaken);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Removes the warm-up for `key` and hands it over. The caller owns the request from
|
|
162
|
+
* here, including aborting it.
|
|
163
|
+
*/
|
|
164
|
+
var takeWarmedImage = exports.takeWarmedImage = function takeWarmedImage(key) {
|
|
165
|
+
var entry = removeWarmedImage(key, 'taken');
|
|
166
|
+
return entry && {
|
|
167
|
+
image: entry.image,
|
|
168
|
+
controller: entry.controller
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** Aborts and drops every warm-up without reporting an outcome. For tests. */
|
|
173
|
+
var clearWarmedImages = exports.clearWarmedImages = function clearWarmedImages() {
|
|
174
|
+
(0, _toConsumableArray2.default)(getStore().keys()).forEach(function (key) {
|
|
175
|
+
return discardWarmedImage(key);
|
|
176
|
+
});
|
|
177
|
+
};
|
|
@@ -15,6 +15,7 @@ import { createLoadSucceededEvent } from './analytics/events/operational/loadSuc
|
|
|
15
15
|
import { fireAnalytics } from './analytics/fireAnalytics';
|
|
16
16
|
import { getFileAttributes } from './analytics/getFileAttributes';
|
|
17
17
|
import { startMediaFileUfoExperience, succeedMediaFileUfoExperience } from './analytics/ufoExperiences';
|
|
18
|
+
import { loadImageViewerChunk } from './chunkLoaders';
|
|
18
19
|
import { Outcome } from './domain/outcome';
|
|
19
20
|
import ErrorMessage from './errorMessageWithAnalytics';
|
|
20
21
|
import { ErrorViewDownloadButton } from './ErrorViewDownloadButton';
|
|
@@ -25,9 +26,8 @@ import { MediaViewerError } from './MediaViewerError';
|
|
|
25
26
|
import ArchiveViewerLoader from './viewers/archiveSidebar/archiveViewerLoader';
|
|
26
27
|
import { CustomViewer } from './viewers/customViewer/customViewer';
|
|
27
28
|
import { InteractiveImg } from './viewers/image/interactive-img';
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
29
|
+
// Shared with `./preload` so the warm-up and the render path load the same chunk.
|
|
30
|
+
const loadImageViewer = loadImageViewerChunk;
|
|
31
31
|
const LoadableImageViewer = Loadable({
|
|
32
32
|
loader: () => loadImageViewer().then(mod => mod.ImageViewer),
|
|
33
33
|
loading: () => /*#__PURE__*/React.createElement(Spinner, null)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { getErrorDetail } from '../../../getErrorDetail';
|
|
2
|
+
import { getPrimaryErrorReason } from '../../../getPrimaryErrorReason';
|
|
3
|
+
import { getRequestMetadata } from '../../../getRequestMetadata';
|
|
4
|
+
import { getSecondaryErrorReason } from '../../../getSecondaryErrorReason';
|
|
5
|
+
import { MediaViewerError } from '../../../MediaViewerError';
|
|
6
|
+
|
|
7
|
+
/** Fired when a warm-up request fails for a reason other than an abort. Never sampled. */
|
|
8
|
+
|
|
9
|
+
/** Fired once per sampled warm-up, when it leaves the warmed image store. */
|
|
10
|
+
|
|
11
|
+
export const createPreloadFailedEventPayload = ({
|
|
12
|
+
error,
|
|
13
|
+
wasTaken,
|
|
14
|
+
durationMs,
|
|
15
|
+
fileAttributes,
|
|
16
|
+
traceContext
|
|
17
|
+
}) => {
|
|
18
|
+
const viewerError = new MediaViewerError('preload-image-fetch', error instanceof Error ? error : undefined);
|
|
19
|
+
const requestMetadata = getRequestMetadata(viewerError);
|
|
20
|
+
return {
|
|
21
|
+
eventType: 'operational',
|
|
22
|
+
actionSubject: 'mediaFile',
|
|
23
|
+
action: 'preloadFailed',
|
|
24
|
+
attributes: {
|
|
25
|
+
status: 'fail',
|
|
26
|
+
failReason: getPrimaryErrorReason(viewerError),
|
|
27
|
+
error: getSecondaryErrorReason(viewerError),
|
|
28
|
+
errorDetail: getErrorDetail(viewerError),
|
|
29
|
+
statusCode: requestMetadata === null || requestMetadata === void 0 ? void 0 : requestMetadata.statusCode,
|
|
30
|
+
request: requestMetadata,
|
|
31
|
+
fileMimetype: fileAttributes.fileMimetype,
|
|
32
|
+
fileMediatype: fileAttributes.fileMediatype,
|
|
33
|
+
fileAttributes,
|
|
34
|
+
traceContext,
|
|
35
|
+
wasTaken,
|
|
36
|
+
durationMs
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export const createPreloadEndedEventPayload = ({
|
|
41
|
+
outcome,
|
|
42
|
+
blob,
|
|
43
|
+
durationMs,
|
|
44
|
+
fileAttributes,
|
|
45
|
+
traceContext,
|
|
46
|
+
samplingRate
|
|
47
|
+
}) => ({
|
|
48
|
+
eventType: 'operational',
|
|
49
|
+
actionSubject: 'mediaFile',
|
|
50
|
+
action: 'preloadEnded',
|
|
51
|
+
attributes: {
|
|
52
|
+
outcome,
|
|
53
|
+
resolved: !!blob,
|
|
54
|
+
sizeBytes: blob === null || blob === void 0 ? void 0 : blob.size,
|
|
55
|
+
durationMs,
|
|
56
|
+
fileAttributes,
|
|
57
|
+
traceContext,
|
|
58
|
+
isSamplingEnabled: samplingRate < 1,
|
|
59
|
+
samplingRate
|
|
60
|
+
}
|
|
61
|
+
});
|
|
@@ -4,7 +4,7 @@ import { fg } from '@atlaskit/platform-feature-flags/fg';
|
|
|
4
4
|
import { UFOExperience } from '@atlaskit/ufo/experience';
|
|
5
5
|
import { ExperiencePerformanceTypes, ExperienceTypes } from '@atlaskit/ufo/experience-types';
|
|
6
6
|
const packageName = "@atlaskit/media-viewer";
|
|
7
|
-
const packageVersion = "54.
|
|
7
|
+
const packageVersion = "54.12.0";
|
|
8
8
|
let ufoExperience;
|
|
9
9
|
const getExperience = () => {
|
|
10
10
|
if (!ufoExperience) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single call site for each lazily loaded chunk that `./preload` warms.
|
|
3
|
+
*
|
|
4
|
+
* Both the render path and the warm-up path must go through these. Repeating an
|
|
5
|
+
* `import()` with the same `webpackChunkName` in two files relies on the two
|
|
6
|
+
* comments staying identical forever; if they drift, the warm-up fetches a
|
|
7
|
+
* different chunk than the one the viewer loads and silently does nothing.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const loadMediaViewerChunk = () => import( /* webpackChunkName: "@atlaskit-internal_media-viewer" */'./components/media-viewer-error-boundary');
|
|
11
|
+
export const loadImageViewerChunk = () => import( /* webpackChunkName: "@atlaskit-internal_imageViewer" */'./viewers/image');
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import React, { lazy, Suspense } from 'react';
|
|
2
|
+
import { loadMediaViewerChunk } from '../chunkLoaders';
|
|
2
3
|
import ModalSpinner from '../viewers/modalSpinner';
|
|
3
4
|
import { MediaViewerPortal } from './portal';
|
|
4
|
-
const MediaViewer = /*#__PURE__*/lazy(() =>
|
|
5
|
+
const MediaViewer = /*#__PURE__*/lazy(() => loadMediaViewerChunk().then(({
|
|
5
6
|
MediaViewerWithErrorBoundary
|
|
6
7
|
}) => ({
|
|
7
8
|
default: MediaViewerWithErrorBoundary
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { isErrorFileState } from '@atlaskit/media-client/is-error-file-state';
|
|
2
|
+
import { isFileIdentifier } from '@atlaskit/media-client/is-file-identifier';
|
|
3
|
+
import { isImageRepresentationReady } from '@atlaskit/media-client/is-image-representation-ready';
|
|
4
|
+
import { getMediaTypeFromMimeType } from '@atlaskit/media-common/mediaTypeUtils';
|
|
5
|
+
import { sanitiseAnalyticsPayload } from '@atlaskit/media-common/sanitisePayload';
|
|
6
|
+
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
|
|
7
|
+
import { createPreloadEndedEventPayload, createPreloadFailedEventPayload } from './analytics/events/operational/preload';
|
|
8
|
+
import { loadImageViewerChunk, loadMediaViewerChunk } from './chunkLoaders';
|
|
9
|
+
import { getImageViewerRequestParams, IMAGE_VIEWER_FETCH_MAX_RES } from './viewers/image/imageRequestParams';
|
|
10
|
+
import { getWarmedImageKey, hasWarmedImage, setWarmedImage } from './warmedImages';
|
|
11
|
+
/**
|
|
12
|
+
* Share of warm-ups that report `preloadEnded`, which measures how many warm-ups the
|
|
13
|
+
* viewer takes. `preloadFailed` is never sampled.
|
|
14
|
+
*/
|
|
15
|
+
const PRELOAD_ENDED_SAMPLING_RATE = 0.1;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A warm-up is speculative, so it doesn't retry a failure with backoff. Retrying a
|
|
19
|
+
* 5xx would add load when the image endpoint is already struggling, and the viewer
|
|
20
|
+
* makes its own request on open anyway. media-client's Edge network-error fallback
|
|
21
|
+
* still applies. `fetchRetry` waits `startTimeoutInMs` even
|
|
22
|
+
* after the last attempt, so it is zero: a failed warm-up a viewer has taken then
|
|
23
|
+
* hands over to the viewer's own request without a pause.
|
|
24
|
+
*/
|
|
25
|
+
const WARM_UP_CLIENT_OPTIONS = {
|
|
26
|
+
retryOptions: {
|
|
27
|
+
maxAttempts: 1,
|
|
28
|
+
startTimeoutInMs: 0
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** SVGs are rendered by `SvgViewer`, which does not go through the image endpoint. */
|
|
33
|
+
const SVG_MIME_TYPE = 'image/svg+xml';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Effective connection types where speculatively downloading a full resolution
|
|
37
|
+
* image costs the user more than the latency it saves.
|
|
38
|
+
*/
|
|
39
|
+
const METERED_EFFECTIVE_TYPES = ['slow-2g', '2g', '3g'];
|
|
40
|
+
const isMeteredConnection = () => {
|
|
41
|
+
if (typeof navigator === 'undefined') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const {
|
|
45
|
+
connection
|
|
46
|
+
} = navigator;
|
|
47
|
+
if (!connection) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return !!connection.saveData || !!connection.effectiveType && METERED_EFFECTIVE_TYPES.includes(connection.effectiveType);
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Speculatively warms what the media viewer needs to display `identifier`, so
|
|
54
|
+
* that opening it is closer to instant. Intended to be called from a hover or
|
|
55
|
+
* focus handler on whatever opens the viewer.
|
|
56
|
+
*
|
|
57
|
+
* Two things get warmed:
|
|
58
|
+
* - the lazily loaded viewer chunks, and
|
|
59
|
+
* - the full resolution image binary, whose in-flight request is kept in
|
|
60
|
+
* `./warmedImages` for `ImageViewer` to take instead of issuing its own.
|
|
61
|
+
*
|
|
62
|
+
* The image request starts once media-client has resolved the watermark version
|
|
63
|
+
* it carries, because the version is part of the warmed image's key.
|
|
64
|
+
*
|
|
65
|
+
* The image binary is only warmed when the file is known to be a non-SVG image,
|
|
66
|
+
* because `ImageViewer` is the only viewer that makes this request. Callers that
|
|
67
|
+
* know nothing about the file still get the chunk warm-up.
|
|
68
|
+
*
|
|
69
|
+
* Safe to call repeatedly and safe to call for files the user never opens: an
|
|
70
|
+
* image already being warmed is not requested again, a failed image request is not
|
|
71
|
+
* retried and its rejection is handled, and no image is fetched on metered or slow
|
|
72
|
+
* connections. Failures and outcomes go to `onAnalyticsEvent`.
|
|
73
|
+
*
|
|
74
|
+
* This is the exposure point for the `platform_media_viewer_preload_on_hover`
|
|
75
|
+
* experiment. Callers attach their handlers in both arms, and the exposure fires
|
|
76
|
+
* here before the arms differ, so both arms are exposed on the same hover or focus.
|
|
77
|
+
*/
|
|
78
|
+
export const preloadMediaViewer = ({
|
|
79
|
+
getMediaClient,
|
|
80
|
+
identifier,
|
|
81
|
+
mimeType,
|
|
82
|
+
mediaType,
|
|
83
|
+
fileState,
|
|
84
|
+
traceContext,
|
|
85
|
+
onAnalyticsEvent
|
|
86
|
+
}) => {
|
|
87
|
+
// External images need no warm-up: the viewer renders the same `dataURI` the
|
|
88
|
+
// card is already displaying. Checked before the exposure, so neither arm is
|
|
89
|
+
// exposed for them.
|
|
90
|
+
if (!isFileIdentifier(identifier)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!isExperimentEnabled('platform_media_viewer_preload_on_hover')) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
void loadMediaViewerChunk().catch(() => {});
|
|
97
|
+
const resolvedMediaType = mediaType !== null && mediaType !== void 0 ? mediaType : mimeType ? getMediaTypeFromMimeType(mimeType) : undefined;
|
|
98
|
+
if (resolvedMediaType !== 'image' || mimeType === SVG_MIME_TYPE) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
void loadImageViewerChunk().catch(() => {});
|
|
102
|
+
if (fileState) {
|
|
103
|
+
// `ImageViewer` renders `fileState.preview` directly when one exists
|
|
104
|
+
// (typically a local upload preview) without touching the image endpoint.
|
|
105
|
+
if (!isErrorFileState(fileState) && fileState.preview) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Otherwise it waits for the backend image representation before fetching.
|
|
110
|
+
if (!isImageRepresentationReady(fileState)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (isMeteredConnection()) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const mediaClient = getMediaClient();
|
|
118
|
+
const {
|
|
119
|
+
id,
|
|
120
|
+
collectionName
|
|
121
|
+
} = identifier;
|
|
122
|
+
const warmImage = watermarkVersion => {
|
|
123
|
+
const key = getWarmedImageKey({
|
|
124
|
+
id,
|
|
125
|
+
collectionName,
|
|
126
|
+
watermarkVersion
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// An existing entry is either still downloading or already resolved. Either way
|
|
130
|
+
// the viewer can take it, so there is nothing to do. Checked after the version
|
|
131
|
+
// resolves, so repeated hovers still make one request.
|
|
132
|
+
if (hasWarmedImage(key)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const controller = new AbortController();
|
|
136
|
+
const startedAt = Date.now();
|
|
137
|
+
const fileSize = fileState && !isErrorFileState(fileState) ? fileState.size : undefined;
|
|
138
|
+
const fileAttributes = {
|
|
139
|
+
fileId: id,
|
|
140
|
+
fileMediatype: resolvedMediaType,
|
|
141
|
+
fileMimetype: mimeType,
|
|
142
|
+
...(fileSize !== undefined && {
|
|
143
|
+
fileSize
|
|
144
|
+
})
|
|
145
|
+
};
|
|
146
|
+
const report = createPayload => {
|
|
147
|
+
try {
|
|
148
|
+
// Sanitised here, as every media channel event is, so each caller sends the
|
|
149
|
+
// same masked payload.
|
|
150
|
+
onAnalyticsEvent === null || onAnalyticsEvent === void 0 ? void 0 : onAnalyticsEvent(sanitiseAnalyticsPayload(createPayload()));
|
|
151
|
+
} catch {
|
|
152
|
+
// Analytics must never break the warm-up.
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const isSampled = !!onAnalyticsEvent && Math.random() < PRELOAD_ENDED_SAMPLING_RATE;
|
|
156
|
+
setWarmedImage(key, {
|
|
157
|
+
image: mediaClient.getImage(id, getImageViewerRequestParams(collectionName), controller, IMAGE_VIEWER_FETCH_MAX_RES, traceContext, WARM_UP_CLIENT_OPTIONS),
|
|
158
|
+
controller
|
|
159
|
+
}, onAnalyticsEvent && {
|
|
160
|
+
onEnd: isSampled ? (outcome, blob) => report(() => createPreloadEndedEventPayload({
|
|
161
|
+
outcome,
|
|
162
|
+
blob,
|
|
163
|
+
durationMs: Date.now() - startedAt,
|
|
164
|
+
fileAttributes,
|
|
165
|
+
traceContext,
|
|
166
|
+
samplingRate: PRELOAD_ENDED_SAMPLING_RATE
|
|
167
|
+
})) : undefined,
|
|
168
|
+
onError: (error, wasTaken) => report(() => createPreloadFailedEventPayload({
|
|
169
|
+
error,
|
|
170
|
+
wasTaken,
|
|
171
|
+
durationMs: Date.now() - startedAt,
|
|
172
|
+
fileAttributes,
|
|
173
|
+
traceContext
|
|
174
|
+
}))
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// The key carries the watermark version the image request is made with, so a
|
|
179
|
+
// warm-up made before a watermark config change never reaches a viewer opened after
|
|
180
|
+
// it. If auth fails, the image request would fail too, so only the chunks are warmed
|
|
181
|
+
// and the viewer's own request reports the failure. Called inside a promise, so a
|
|
182
|
+
// throw (say, from an older media-client without the method) can't reach the hover
|
|
183
|
+
// handler that called this.
|
|
184
|
+
void Promise.resolve().then(() => mediaClient.getImageWatermarkVersion(collectionName)).then(warmImage, () => {});
|
|
185
|
+
};
|