@atlaskit/media-client 32.0.2 → 33.1.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.
Files changed (30) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/cjs/client/file-fetcher/index.js +143 -114
  3. package/dist/cjs/client/media-client.js +1 -1
  4. package/dist/cjs/client/media-store/MediaStore.js +227 -138
  5. package/dist/cjs/utils/hashing/hasherCreator.js +20 -32
  6. package/dist/cjs/utils/request/helpers.js +1 -1
  7. package/dist/es2019/client/file-fetcher/index.js +15 -0
  8. package/dist/es2019/client/media-client.js +1 -1
  9. package/dist/es2019/client/media-store/MediaStore.js +63 -7
  10. package/dist/es2019/utils/hashing/hasherCreator.js +19 -23
  11. package/dist/es2019/utils/request/helpers.js +1 -1
  12. package/dist/esm/client/file-fetcher/index.js +143 -114
  13. package/dist/esm/client/media-client.js +1 -1
  14. package/dist/esm/client/media-store/MediaStore.js +227 -138
  15. package/dist/esm/utils/hashing/hasherCreator.js +19 -29
  16. package/dist/esm/utils/request/helpers.js +1 -1
  17. package/dist/types/client/file-fetcher/index.d.ts +4 -1
  18. package/dist/types/client/media-store/MediaStore.d.ts +3 -1
  19. package/dist/types/client/media-store/types.d.ts +13 -0
  20. package/dist/types/utils/hashing/hasherCreator.d.ts +1 -1
  21. package/dist/types-ts4.5/client/file-fetcher/index.d.ts +4 -1
  22. package/dist/types-ts4.5/client/media-store/MediaStore.d.ts +3 -1
  23. package/dist/types-ts4.5/client/media-store/types.d.ts +13 -0
  24. package/dist/types-ts4.5/utils/hashing/hasherCreator.d.ts +1 -1
  25. package/package.json +5 -5
  26. package/dist/cjs/utils/hashing/workerHasher.js +0 -113
  27. package/dist/es2019/utils/hashing/workerHasher.js +0 -89
  28. package/dist/esm/utils/hashing/workerHasher.js +0 -106
  29. package/dist/types/utils/hashing/workerHasher.d.ts +0 -16
  30. package/dist/types-ts4.5/utils/hashing/workerHasher.d.ts +0 -16
@@ -5,7 +5,7 @@ var _typeof = require("@babel/runtime/helpers/typeof");
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.destroyHasher = exports.createHasher = void 0;
8
+ exports.destroyHashers = exports.createHasher = void 0;
9
9
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
10
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
11
  var _mediaCore = require("@atlaskit/media-core");
@@ -13,18 +13,19 @@ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return
13
13
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
14
14
  var hasher = null;
15
15
  var sha256Hasher = null;
16
- var destroyHasher = exports.destroyHasher = function destroyHasher() {
17
- return hasher = null;
16
+ var destroyHashers = exports.destroyHashers = function destroyHashers() {
17
+ hasher = null;
18
+ sha256Hasher = null;
18
19
  };
19
20
  var createHasher = exports.createHasher = /*#__PURE__*/function () {
20
21
  var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(algorithm) {
21
- var numWorkers, _yield$import, SimpleHasher, _yield$import2, WorkerHasher, _yield$import3, _SimpleHasher;
22
+ var _yield$import, _SimpleHasher, _yield$import2, SimpleHasher;
22
23
  return _regenerator.default.wrap(function _callee$(_context) {
23
24
  while (1) switch (_context.prev = _context.next) {
24
25
  case 0:
25
- numWorkers = 3;
26
+ _context.prev = 0;
26
27
  if (!(algorithm === _mediaCore.ChunkHashAlgorithm.Sha256)) {
27
- _context.next = 11;
28
+ _context.next = 9;
28
29
  break;
29
30
  }
30
31
  if (sha256Hasher) {
@@ -37,44 +38,31 @@ var createHasher = exports.createHasher = /*#__PURE__*/function () {
37
38
  });
38
39
  case 5:
39
40
  _yield$import = _context.sent;
40
- SimpleHasher = _yield$import.SimpleHasher;
41
- sha256Hasher = new SimpleHasher();
41
+ _SimpleHasher = _yield$import.SimpleHasher;
42
+ sha256Hasher = new _SimpleHasher();
42
43
  case 8:
43
44
  return _context.abrupt("return", sha256Hasher);
45
+ case 9:
46
+ _context.next = 13;
47
+ break;
44
48
  case 11:
45
- if (hasher) {
46
- _context.next = 27;
47
- break;
48
- }
49
- _context.prev = 12;
49
+ _context.prev = 11;
50
+ _context.t0 = _context["catch"](0);
51
+ case 13:
50
52
  _context.next = 15;
51
53
  return Promise.resolve().then(function () {
52
- return _interopRequireWildcard(require('./workerHasher'));
54
+ return _interopRequireWildcard(require('./simpleHasher'));
53
55
  });
54
56
  case 15:
55
57
  _yield$import2 = _context.sent;
56
- WorkerHasher = _yield$import2.WorkerHasher;
57
- hasher = new WorkerHasher(numWorkers);
58
- _context.next = 27;
59
- break;
60
- case 20:
61
- _context.prev = 20;
62
- _context.t0 = _context["catch"](12);
63
- _context.next = 24;
64
- return Promise.resolve().then(function () {
65
- return _interopRequireWildcard(require('./simpleHasher'));
66
- });
67
- case 24:
68
- _yield$import3 = _context.sent;
69
- _SimpleHasher = _yield$import3.SimpleHasher;
70
- hasher = new _SimpleHasher();
71
- case 27:
58
+ SimpleHasher = _yield$import2.SimpleHasher;
59
+ hasher = new SimpleHasher();
72
60
  return _context.abrupt("return", hasher);
73
- case 28:
61
+ case 19:
74
62
  case "end":
75
63
  return _context.stop();
76
64
  }
77
- }, _callee, null, [[12, 20]]);
65
+ }, _callee, null, [[0, 11]]);
78
66
  }));
79
67
  return function createHasher(_x) {
80
68
  return _ref.apply(this, arguments);
@@ -97,7 +97,7 @@ function createUrl(url, _ref2) {
97
97
  return value != null;
98
98
  }).forEach(function (pair) {
99
99
  var _parsedUrl$searchPara;
100
- (_parsedUrl$searchPara = parsedUrl.searchParams).append.apply(_parsedUrl$searchPara, (0, _toConsumableArray2.default)(pair));
100
+ (_parsedUrl$searchPara = parsedUrl.searchParams).set.apply(_parsedUrl$searchPara, (0, _toConsumableArray2.default)(pair));
101
101
  });
102
102
  parsedUrl.searchParams.sort();
103
103
  return parsedUrl.toString();
@@ -140,6 +140,21 @@ export class FileFetcherImpl {
140
140
  preview
141
141
  };
142
142
  });
143
+ _defineProperty(this, "uploadArtifact", async (id, file, params, collectionName, traceContext) => {
144
+ const {
145
+ data
146
+ } = await this.mediaApi.uploadArtifact(id, file, params, collectionName, traceContext);
147
+
148
+ /* TEMPORARYLY PULLING NEW METADATA till the endpoint is fixed */
149
+ const {
150
+ data: altData
151
+ } = await this.mediaApi.getItems([id], collectionName, traceContext);
152
+ const itemDetails = altData.items[0].details;
153
+ // ------------------------------------------------------------
154
+
155
+ this.setFileState(id, mapMediaItemToFileState(id, itemDetails));
156
+ return data;
157
+ });
143
158
  this.mediaApi = mediaApi;
144
159
  this.store = store;
145
160
  this.dataloader = createFileDataloader(mediaApi);
@@ -15,7 +15,7 @@ export class MediaClient {
15
15
  this.mediaStore = mediaApi !== null && mediaApi !== void 0 ? mediaApi : new MediaApi({
16
16
  authProvider: mediaClientConfig.authProvider,
17
17
  initialAuth: mediaClientConfig.initialAuth,
18
- chunkHashAlgorithm: mediaClientConfig.useSha256ForUploads ? ChunkHashAlgorithm.Sha256 : ChunkHashAlgorithm.Sha1,
18
+ chunkHashAlgorithm: ChunkHashAlgorithm.Sha256,
19
19
  authProviderTimeout: mediaClientConfig.authProviderTimeoutMs
20
20
  });
21
21
  this.config = mediaClientConfig;
@@ -30,19 +30,66 @@ const jsonHeaders = {
30
30
  };
31
31
  const cdnFeatureFlag = endpoint => {
32
32
  let result = endpoint;
33
- if (fg('platform_media_cdn_delivery')) {
34
- if (isCommercial()) {
35
- result += '/cdn';
36
- }
33
+ if (isCommercial() && fg('platform_media_cdn_delivery')) {
34
+ result += '/cdn';
37
35
  }
38
36
  return result;
39
37
  };
40
38
  export class MediaStore {
41
39
  constructor(config) {
40
+ _defineProperty(this, "getArtifactBinary", async (artifacts, artifactName, {
41
+ collectionName,
42
+ abortController,
43
+ maxAge = FILE_CACHE_MAX_AGE,
44
+ traceContext
45
+ }) => {
46
+ const metadata = {
47
+ method: 'GET',
48
+ endpoint: `artifact-cdn-url`
49
+ };
50
+ const extendedTraceContext = extendTraceContext(traceContext);
51
+ const options = {
52
+ ...metadata,
53
+ traceContext: extendedTraceContext
54
+ };
55
+ const artifactUrl = await this.getArtifactURL(artifacts, artifactName, collectionName, maxAge);
56
+ return request(artifactUrl, options, abortController).then(createMapResponseToBlob(metadata));
57
+ });
58
+ _defineProperty(this, "uploadArtifact", async (id, file, params, collectionName, traceContext) => {
59
+ const metadata = {
60
+ method: 'POST',
61
+ endpoint: '/file/{fileId}/artifact/binary'
62
+ };
63
+ const headers = {
64
+ Accept: 'application/json',
65
+ 'Content-Type': file.type
66
+ };
67
+ const extendedParams = {
68
+ ...params,
69
+ name: file.name
70
+ };
71
+ const authContext = {
72
+ collectionName,
73
+ access: [{
74
+ type: 'file',
75
+ id,
76
+ actions: ['update']
77
+ }]
78
+ };
79
+ const options = {
80
+ ...metadata,
81
+ authContext,
82
+ headers,
83
+ params: extendedParams,
84
+ body: file,
85
+ traceContext
86
+ };
87
+ return this.request(`/file/${id}/artifact/binary`, options).then(createMapResponseToJson(metadata));
88
+ });
42
89
  _defineProperty(this, "resolveAuth", authContext => resolveAuth(this.config.authProvider, authContext, this.config.authProviderTimeout));
43
90
  _defineProperty(this, "resolveInitialAuth", () => resolveInitialAuth(this.config.initialAuth));
44
91
  this.config = config;
45
- this._chunkHashAlgorithm = config.chunkHashAlgorithm || ChunkHashAlgorithm.Sha1;
92
+ this._chunkHashAlgorithm = config.chunkHashAlgorithm || ChunkHashAlgorithm.Sha256;
46
93
  }
47
94
  async removeCollectionFile(id, collectionName, occurrenceKey, traceContext) {
48
95
  const metadata = {
@@ -234,7 +281,16 @@ export class MediaStore {
234
281
  const binaryEndpoint = cdnFeatureFlag('binary');
235
282
  return mapToMediaCdnUrl(createUrl(`${auth.baseUrl}/file/${id}/${binaryEndpoint}`, options), auth.token);
236
283
  }
237
- async getArtifactURL(artifacts, artifactName, collectionName) {
284
+ async getArtifactURL(artifacts, artifactName, collectionName, maxAge = FILE_CACHE_MAX_AGE) {
285
+ // getArtifactURL returns a constructed url PATH, not the full Media/CDN Url.
286
+ // We use the provided cdnUrl from metadata first.
287
+ if (isCommercial() && fg('platform_media_cdn_delivery')) {
288
+ var _artifacts$artifactNa;
289
+ const cdnUrl = (_artifacts$artifactNa = artifacts[artifactName]) === null || _artifacts$artifactNa === void 0 ? void 0 : _artifacts$artifactNa.cdnUrl;
290
+ if (cdnUrl) {
291
+ return cdnUrl;
292
+ }
293
+ }
238
294
  const artifactUrl = getArtifactUrl(artifacts, artifactName);
239
295
  if (!artifactUrl) {
240
296
  throw new Error(`artifact ${artifactName} not found`);
@@ -245,7 +301,7 @@ export class MediaStore {
245
301
  const options = {
246
302
  params: {
247
303
  collection: collectionName,
248
- 'max-age': FILE_CACHE_MAX_AGE
304
+ 'max-age': maxAge
249
305
  },
250
306
  auth
251
307
  };
@@ -1,30 +1,26 @@
1
1
  import { ChunkHashAlgorithm } from '@atlaskit/media-core';
2
2
  let hasher = null;
3
3
  let sha256Hasher = null;
4
- export const destroyHasher = () => hasher = null;
4
+ export const destroyHashers = () => {
5
+ hasher = null;
6
+ sha256Hasher = null;
7
+ };
5
8
  export const createHasher = async algorithm => {
6
- const numWorkers = 3;
7
- if (algorithm === ChunkHashAlgorithm.Sha256) {
8
- if (!sha256Hasher) {
9
- // TODO this needs to support worker sha256 hasher as well
10
- const {
11
- SimpleHasher
12
- } = await import('./sha256SimpleHasher');
13
- sha256Hasher = new SimpleHasher();
9
+ try {
10
+ if (algorithm === ChunkHashAlgorithm.Sha256) {
11
+ if (!sha256Hasher) {
12
+ // TODO this needs to support worker sha256 hasher as well
13
+ const {
14
+ SimpleHasher
15
+ } = await import('./sha256SimpleHasher');
16
+ sha256Hasher = new SimpleHasher();
17
+ }
18
+ return sha256Hasher;
14
19
  }
15
- return sha256Hasher;
16
- } else if (!hasher) {
17
- try {
18
- const {
19
- WorkerHasher
20
- } = await import('./workerHasher');
21
- hasher = new WorkerHasher(numWorkers);
22
- } catch (error) {
23
- const {
24
- SimpleHasher
25
- } = await import('./simpleHasher');
26
- hasher = new SimpleHasher();
27
- }
28
- }
20
+ } catch (error) {}
21
+ const {
22
+ SimpleHasher
23
+ } = await import('./simpleHasher');
24
+ hasher = new SimpleHasher();
29
25
  return hasher;
30
26
  };
@@ -64,7 +64,7 @@ export function createUrl(url, {
64
64
  ...authParams
65
65
  };
66
66
  Object.entries(paramsToAppend).filter(([_, value]) => value != null).forEach(pair => {
67
- parsedUrl.searchParams.append(...pair);
67
+ parsedUrl.searchParams.set(...pair);
68
68
  });
69
69
  parsedUrl.searchParams.sort();
70
70
  return parsedUrl.toString();