@atlaskit/media-client 18.1.0 → 19.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 (118) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/cjs/client/collection-fetcher.js +8 -8
  3. package/dist/cjs/client/file-fetcher/index.js +40 -36
  4. package/dist/cjs/client/media-client.js +2 -2
  5. package/dist/cjs/client/media-store/index.js +69 -52
  6. package/dist/cjs/models/file-state.js +4 -2
  7. package/dist/cjs/uploader/index.js +40 -70
  8. package/dist/cjs/utils/createFileDataLoader.js +32 -15
  9. package/dist/cjs/utils/request/helpers.js +39 -22
  10. package/dist/cjs/utils/request/index.js +5 -3
  11. package/dist/cjs/version.json +1 -1
  12. package/dist/es2019/client/collection-fetcher.js +6 -6
  13. package/dist/es2019/client/file-fetcher/index.js +16 -14
  14. package/dist/es2019/client/media-client.js +2 -2
  15. package/dist/es2019/client/media-store/index.js +50 -41
  16. package/dist/es2019/models/file-state.js +4 -2
  17. package/dist/es2019/uploader/index.js +17 -21
  18. package/dist/es2019/utils/createFileDataLoader.js +13 -3
  19. package/dist/es2019/utils/request/helpers.js +26 -8
  20. package/dist/es2019/utils/request/index.js +6 -4
  21. package/dist/es2019/version.json +1 -1
  22. package/dist/esm/client/collection-fetcher.js +8 -8
  23. package/dist/esm/client/file-fetcher/index.js +40 -36
  24. package/dist/esm/client/media-client.js +2 -2
  25. package/dist/esm/client/media-store/index.js +68 -50
  26. package/dist/esm/models/file-state.js +4 -2
  27. package/dist/esm/uploader/index.js +40 -69
  28. package/dist/esm/utils/createFileDataLoader.js +31 -13
  29. package/dist/esm/utils/request/helpers.js +36 -20
  30. package/dist/esm/utils/request/index.js +6 -4
  31. package/dist/esm/version.json +1 -1
  32. package/dist/types/client/collection-fetcher.d.ts +4 -3
  33. package/dist/types/client/file-fetcher/index.d.ts +9 -8
  34. package/dist/types/client/media-client.d.ts +2 -2
  35. package/dist/types/client/media-store/index.d.ts +16 -21
  36. package/dist/types/models/file-state.d.ts +9 -6
  37. package/dist/types/models/media.d.ts +3 -1
  38. package/dist/types/uploader/index.d.ts +2 -1
  39. package/dist/types/utils/request/helpers.d.ts +10 -2
  40. package/dist/types/utils/request/types.d.ts +3 -0
  41. package/package.json +7 -15
  42. package/report.api.md +116 -75
  43. package/dist/types-ts4.0/client/__mocks__/collection-fetcher.d.ts +0 -8
  44. package/dist/types-ts4.0/client/__mocks__/file-fetcher.d.ts +0 -10
  45. package/dist/types-ts4.0/client/__mocks__/media-client.d.ts +0 -13
  46. package/dist/types-ts4.0/client/collection-fetcher.d.ts +0 -35
  47. package/dist/types-ts4.0/client/events.d.ts +0 -14
  48. package/dist/types-ts4.0/client/file-fetcher/error.d.ts +0 -29
  49. package/dist/types-ts4.0/client/file-fetcher/index.d.ts +0 -56
  50. package/dist/types-ts4.0/client/media-client.d.ts +0 -28
  51. package/dist/types-ts4.0/client/media-store/error.d.ts +0 -16
  52. package/dist/types-ts4.0/client/media-store/index.d.ts +0 -174
  53. package/dist/types-ts4.0/client/media-store/resolveAuth.d.ts +0 -4
  54. package/dist/types-ts4.0/client/mobile-upload.d.ts +0 -11
  55. package/dist/types-ts4.0/client/stargate-client.d.ts +0 -15
  56. package/dist/types-ts4.0/constants.d.ts +0 -6
  57. package/dist/types-ts4.0/file-streams-cache.d.ts +0 -15
  58. package/dist/types-ts4.0/globalMediaEventEmitter.d.ts +0 -6
  59. package/dist/types-ts4.0/identifier.d.ts +0 -15
  60. package/dist/types-ts4.0/index.d.ts +0 -89
  61. package/dist/types-ts4.0/models/artifacts.d.ts +0 -12
  62. package/dist/types-ts4.0/models/auth-headers.d.ts +0 -11
  63. package/dist/types-ts4.0/models/auth-query-parameters.d.ts +0 -11
  64. package/dist/types-ts4.0/models/errors/helpers.d.ts +0 -6
  65. package/dist/types-ts4.0/models/errors/index.d.ts +0 -11
  66. package/dist/types-ts4.0/models/errors/types.d.ts +0 -15
  67. package/dist/types-ts4.0/models/file-state.d.ts +0 -91
  68. package/dist/types-ts4.0/models/item.d.ts +0 -25
  69. package/dist/types-ts4.0/models/media-subscribable.d.ts +0 -3
  70. package/dist/types-ts4.0/models/media.d.ts +0 -66
  71. package/dist/types-ts4.0/models/mobile-upload.d.ts +0 -28
  72. package/dist/types-ts4.0/upload-controller.d.ts +0 -7
  73. package/dist/types-ts4.0/uploader/calculateChunkSize.d.ts +0 -11
  74. package/dist/types-ts4.0/uploader/error.d.ts +0 -29
  75. package/dist/types-ts4.0/uploader/index.d.ts +0 -21
  76. package/dist/types-ts4.0/utils/checkWebpSupport.d.ts +0 -1
  77. package/dist/types-ts4.0/utils/convertBase64ToBlob.d.ts +0 -1
  78. package/dist/types-ts4.0/utils/createFileDataLoader.d.ts +0 -30
  79. package/dist/types-ts4.0/utils/createMediaSubject.d.ts +0 -3
  80. package/dist/types-ts4.0/utils/detectEmptyFile.d.ts +0 -16
  81. package/dist/types-ts4.0/utils/getDimensionsFromBlob.d.ts +0 -6
  82. package/dist/types-ts4.0/utils/getImageDimensionsFromBlob.d.ts +0 -5
  83. package/dist/types-ts4.0/utils/getMediaTypeFromUploadableFile.d.ts +0 -3
  84. package/dist/types-ts4.0/utils/getVideoDimensionsFromBlob.d.ts +0 -5
  85. package/dist/types-ts4.0/utils/hashing/hasher.d.ts +0 -3
  86. package/dist/types-ts4.0/utils/hashing/hasherCreator.d.ts +0 -3
  87. package/dist/types-ts4.0/utils/hashing/simpleHasher.d.ts +0 -4
  88. package/dist/types-ts4.0/utils/hashing/workerHasher.d.ts +0 -16
  89. package/dist/types-ts4.0/utils/imageResizeModeToFileImageMode.d.ts +0 -3
  90. package/dist/types-ts4.0/utils/isImageRemote.d.ts +0 -1
  91. package/dist/types-ts4.0/utils/mediaSubscribable/fromObservable.d.ts +0 -5
  92. package/dist/types-ts4.0/utils/mediaSubscribable/index.d.ts +0 -3
  93. package/dist/types-ts4.0/utils/mediaSubscribable/toPromise.d.ts +0 -10
  94. package/dist/types-ts4.0/utils/mediaSubscribable/types.d.ts +0 -16
  95. package/dist/types-ts4.0/utils/mobileUpload/error.d.ts +0 -29
  96. package/dist/types-ts4.0/utils/mobileUpload/helpers.d.ts +0 -8
  97. package/dist/types-ts4.0/utils/mobileUpload/index.d.ts +0 -5
  98. package/dist/types-ts4.0/utils/mobileUpload/servicesCache.d.ts +0 -4
  99. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/index.d.ts +0 -7
  100. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/states/error.d.ts +0 -3
  101. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/states/processed.d.ts +0 -3
  102. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/states/processing.d.ts +0 -3
  103. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/states/processingFailed.d.ts +0 -3
  104. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/states/uploading.d.ts +0 -3
  105. package/dist/types-ts4.0/utils/mobileUpload/stateMachine/types.d.ts +0 -69
  106. package/dist/types-ts4.0/utils/overrideMediaTypeIfUnknown.d.ts +0 -5
  107. package/dist/types-ts4.0/utils/polling/errors.d.ts +0 -12
  108. package/dist/types-ts4.0/utils/polling/index.d.ts +0 -33
  109. package/dist/types-ts4.0/utils/polling/types.d.ts +0 -7
  110. package/dist/types-ts4.0/utils/request/errors.d.ts +0 -20
  111. package/dist/types-ts4.0/utils/request/helpers.d.ts +0 -38
  112. package/dist/types-ts4.0/utils/request/index.d.ts +0 -5
  113. package/dist/types-ts4.0/utils/request/types.d.ts +0 -43
  114. package/dist/types-ts4.0/utils/safeUnsubscribe.d.ts +0 -2
  115. package/dist/types-ts4.0/utils/setTimeoutPromise.d.ts +0 -2
  116. package/dist/types-ts4.0/utils/shouldFetchRemoteFileStates.d.ts +0 -16
  117. package/dist/types-ts4.0/utils/url.d.ts +0 -17
  118. package/dist/types-ts4.0/utils/with-media-client-hoc.d.ts +0 -16
@@ -20,8 +20,8 @@ export class MediaClient {
20
20
  this.stargate = new StargateClient(mediaClientConfig.stargateBaseUrl);
21
21
  }
22
22
 
23
- getImage(id, params, controller, fetchMaxRes, traceId) {
24
- return this.mediaStore.getImage(id, params, controller, fetchMaxRes, traceId);
23
+ getImage(id, params, controller, fetchMaxRes, traceContext) {
24
+ return this.mediaStore.getImage(id, params, controller, fetchMaxRes, traceContext);
25
25
  }
26
26
 
27
27
  getImageUrl(id, params) {
@@ -32,13 +32,6 @@ const jsonHeaders = {
32
32
  Accept: 'application/json',
33
33
  'Content-Type': 'application/json'
34
34
  };
35
- export const ZipkinHeaderKeys = {
36
- traceId: 'x-b3-traceid',
37
- spanId: 'x-b3-spanid',
38
- parentSpanId: 'x-b3-parentspanid',
39
- sampled: 'x-b3-sampled',
40
- flags: 'x-b3-flags'
41
- };
42
35
  export class MediaStore {
43
36
  constructor(config, featureFlags) {
44
37
  _defineProperty(this, "resolveAuth", authContext => resolveAuth(this.config.authProvider, authContext));
@@ -49,7 +42,7 @@ export class MediaStore {
49
42
  this.featureFlags = featureFlags;
50
43
  }
51
44
 
52
- async getCollectionItems(collectionName, params) {
45
+ async getCollectionItems(collectionName, params, traceContext) {
53
46
  const metadata = {
54
47
  method: 'GET',
55
48
  endpoint: '/collection/{collectionName}/items'
@@ -63,7 +56,8 @@ export class MediaStore {
63
56
  },
64
57
  headers: {
65
58
  Accept: 'application/json'
66
- }
59
+ },
60
+ traceContext
67
61
  };
68
62
  const response = await this.request(`/collection/${collectionName}/items`, options);
69
63
  const {
@@ -83,7 +77,7 @@ export class MediaStore {
83
77
  };
84
78
  }
85
79
 
86
- async removeCollectionFile(id, collectionName, occurrenceKey) {
80
+ async removeCollectionFile(id, collectionName, occurrenceKey, traceContext) {
87
81
  const metadata = {
88
82
  method: 'PUT',
89
83
  endpoint: '/collection/{collectionName}'
@@ -106,12 +100,13 @@ export class MediaStore {
106
100
  Accept: 'application/json',
107
101
  'Content-Type': 'application/json'
108
102
  },
109
- body: JSON.stringify(body)
103
+ body: JSON.stringify(body),
104
+ traceContext
110
105
  };
111
106
  await this.request(`/collection/${collectionName}`, options);
112
107
  }
113
108
 
114
- createUpload(createUpTo = 1, collectionName) {
109
+ createUpload(createUpTo = 1, collectionName, traceContext) {
115
110
  const metadata = {
116
111
  method: 'POST',
117
112
  endpoint: '/upload'
@@ -125,7 +120,8 @@ export class MediaStore {
125
120
  },
126
121
  headers: {
127
122
  Accept: 'application/json'
128
- }
123
+ },
124
+ traceContext
129
125
  };
130
126
  return this.request(`/upload`, options).then(createMapResponseToJson(metadata));
131
127
  }
@@ -134,7 +130,7 @@ export class MediaStore {
134
130
  collectionName,
135
131
  uploadId,
136
132
  partNumber
137
- } = {}) {
133
+ } = {}, traceContext) {
138
134
  const metadata = {
139
135
  method: 'PUT',
140
136
  endpoint: '/chunk/{etag}'
@@ -147,7 +143,8 @@ export class MediaStore {
147
143
  authContext: {
148
144
  collectionName
149
145
  },
150
- body: blob
146
+ body: blob,
147
+ traceContext
151
148
  };
152
149
  await this.request(`/chunk/${etag}`, options);
153
150
  }
@@ -155,7 +152,7 @@ export class MediaStore {
155
152
  probeChunks(chunks, {
156
153
  collectionName,
157
154
  uploadId
158
- } = {}) {
155
+ } = {}, traceContext) {
159
156
  const metadata = {
160
157
  method: 'POST',
161
158
  endpoint: '/chunk/probe'
@@ -170,12 +167,13 @@ export class MediaStore {
170
167
  headers: jsonHeaders,
171
168
  body: JSON.stringify({
172
169
  chunks
173
- })
170
+ }),
171
+ traceContext
174
172
  };
175
173
  return this.request(`/chunk/probe`, options).then(createMapResponseToJson(metadata));
176
174
  }
177
175
 
178
- createFileFromUpload(body, params = {}) {
176
+ createFileFromUpload(body, params = {}, traceContext) {
179
177
  const metadata = {
180
178
  method: 'POST',
181
179
  endpoint: '/file/upload'
@@ -186,12 +184,13 @@ export class MediaStore {
186
184
  },
187
185
  params,
188
186
  headers: jsonHeaders,
189
- body: JSON.stringify(body)
187
+ body: JSON.stringify(body),
188
+ traceContext
190
189
  };
191
190
  return this.request('/file/upload', options).then(createMapResponseToJson(metadata));
192
191
  }
193
192
 
194
- touchFiles(body, params = {}) {
193
+ touchFiles(body, params = {}, traceContext) {
195
194
  const metadata = {
196
195
  method: 'POST',
197
196
  endpoint: '/upload/createWithFiles'
@@ -201,12 +200,13 @@ export class MediaStore {
201
200
  collectionName: params.collection
202
201
  },
203
202
  headers: jsonHeaders,
204
- body: JSON.stringify(body)
203
+ body: JSON.stringify(body),
204
+ traceContext
205
205
  };
206
206
  return this.request('/upload/createWithFiles', options).then(createMapResponseToJson(metadata));
207
207
  }
208
208
 
209
- getFile(fileId, params = {}) {
209
+ getFile(fileId, params = {}, traceContext) {
210
210
  const metadata = {
211
211
  method: 'GET',
212
212
  endpoint: '/file/{fileId}'
@@ -215,7 +215,8 @@ export class MediaStore {
215
215
  authContext: {
216
216
  collectionName: params.collection
217
217
  },
218
- params
218
+ params,
219
+ traceContext
219
220
  };
220
221
  return this.request(`/file/${fileId}`, options).then(createMapResponseToJson(metadata));
221
222
  }
@@ -228,7 +229,8 @@ export class MediaStore {
228
229
  collectionName
229
230
  });
230
231
  return this.createFileImageURL(id, auth, params);
231
- }
232
+ } // TODO Create ticket in case Trace Id can be supported through query params
233
+
232
234
 
233
235
  getFileImageURLSync(id, params) {
234
236
  const auth = this.resolveInitialAuth();
@@ -278,13 +280,10 @@ export class MediaStore {
278
280
  return createUrl(`${auth.baseUrl}${artifactUrl}`, options);
279
281
  }
280
282
 
281
- async getImage(id, params, controller, fetchMaxRes, traceId) {
283
+ async getImage(id, params, controller, fetchMaxRes, traceContext) {
282
284
  // TODO add checkWebpSupport() back https://product-fabric.atlassian.net/browse/MPT-584
283
285
  const isWebpSupported = false;
284
- let headers = !!traceId ? {
285
- [ZipkinHeaderKeys.traceId]: traceId,
286
- [ZipkinHeaderKeys.spanId]: getRandomHex(16)
287
- } : {};
286
+ const headers = {};
288
287
 
289
288
  if (isWebpSupported) {
290
289
  headers.accept = 'image/webp,image/*,*/*;q=0.8';
@@ -299,12 +298,13 @@ export class MediaStore {
299
298
  collectionName: params && params.collection
300
299
  },
301
300
  params: extendImageParams(params, fetchMaxRes),
302
- headers
301
+ headers,
302
+ traceContext
303
303
  };
304
304
  return this.request(`/file/${id}/image`, options, controller).then(createMapResponseToBlob(metadata));
305
305
  }
306
306
 
307
- async getItems(ids, collectionName) {
307
+ async getItems(ids, collectionName, traceContext) {
308
308
  const descriptors = ids.map(id => ({
309
309
  type: 'file',
310
310
  id,
@@ -321,12 +321,13 @@ export class MediaStore {
321
321
  headers: jsonHeaders,
322
322
  body: JSON.stringify({
323
323
  descriptors
324
- })
324
+ }),
325
+ traceContext
325
326
  };
326
327
  return this.request('/items', options).then(createMapResponseToJson(metadata));
327
328
  }
328
329
 
329
- async getImageMetadata(id, params) {
330
+ async getImageMetadata(id, params, traceContext) {
330
331
  const metadata = {
331
332
  method: 'GET',
332
333
  endpoint: '/file/{fileId}/image/metadata'
@@ -335,12 +336,13 @@ export class MediaStore {
335
336
  authContext: {
336
337
  collectionName: params && params.collection
337
338
  },
338
- params
339
+ params,
340
+ traceContext
339
341
  };
340
342
  return this.request(`/file/${id}/image/metadata`, options).then(createMapResponseToJson(metadata));
341
343
  }
342
344
 
343
- async appendChunksToUpload(uploadId, body, collectionName) {
345
+ async appendChunksToUpload(uploadId, body, collectionName, traceContext) {
344
346
  const metadata = {
345
347
  method: 'PUT',
346
348
  endpoint: '/upload/{uploadId}/chunks'
@@ -350,12 +352,13 @@ export class MediaStore {
350
352
  collectionName
351
353
  },
352
354
  headers: jsonHeaders,
353
- body: JSON.stringify(body)
355
+ body: JSON.stringify(body),
356
+ traceContext
354
357
  };
355
358
  await this.request(`/upload/${uploadId}/chunks`, options);
356
359
  }
357
360
 
358
- copyFileWithToken(body, params) {
361
+ copyFileWithToken(body, params, traceContext) {
359
362
  const metadata = {
360
363
  method: 'POST',
361
364
  endpoint: '/file/copy/withToken'
@@ -368,8 +371,9 @@ export class MediaStore {
368
371
  params,
369
372
  // Contains collection name to write to
370
373
  headers: jsonHeaders,
371
- body: JSON.stringify(body) // Contains collection name to read from
372
-
374
+ body: JSON.stringify(body),
375
+ // Contains collection name to read from
376
+ traceContext
373
377
  };
374
378
  return this.request('/file/copy/withToken', options).then(createMapResponseToJson(metadata));
375
379
  }
@@ -386,9 +390,13 @@ export class MediaStore {
386
390
  params,
387
391
  headers,
388
392
  body,
389
- clientOptions
393
+ clientOptions,
394
+ traceContext
390
395
  } = options;
391
396
  const auth = await this.resolveAuth(authContext);
397
+ const extendedTraceContext = traceContext ? { ...traceContext,
398
+ spanId: (traceContext === null || traceContext === void 0 ? void 0 : traceContext.spanId) || getRandomHex(8)
399
+ } : undefined;
392
400
  const response = await request(`${auth.baseUrl}${path}`, {
393
401
  method,
394
402
  endpoint,
@@ -396,7 +404,8 @@ export class MediaStore {
396
404
  params,
397
405
  headers,
398
406
  body,
399
- clientOptions
407
+ clientOptions,
408
+ traceContext: extendedTraceContext
400
409
  }, controller);
401
410
  setKeyValueInSessionStorage(MEDIA_API_REGION, response.headers.get('x-media-region'));
402
411
  setKeyValueInSessionStorage(MEDIA_API_ENVIRONMENT, response.headers.get('x-media-env'));
@@ -25,7 +25,8 @@ export const mapMediaFileToFileState = mediaFile => {
25
25
  mediaType,
26
26
  mimeType,
27
27
  representations,
28
- createdAt
28
+ createdAt,
29
+ metadataTraceContext
29
30
  } = mediaFile.data;
30
31
  const baseState = {
31
32
  id,
@@ -35,7 +36,8 @@ export const mapMediaFileToFileState = mediaFile => {
35
36
  mimeType,
36
37
  artifacts,
37
38
  representations,
38
- createdAt
39
+ createdAt,
40
+ metadataTraceContext
39
41
  };
40
42
 
41
43
  switch (processingStatus) {
@@ -5,45 +5,41 @@ import { createHasher } from '../utils/hashing/hasherCreator';
5
5
  import { UploaderError } from './error';
6
6
  import { CHUNK_SIZE, PROCESSING_BATCH_SIZE } from '../constants';
7
7
  import { calculateChunkSize, fileSizeError } from './calculateChunkSize';
8
- import { getMediaFeatureFlag } from '@atlaskit/media-common'; // TODO: Allow to pass multiple files
9
8
 
10
9
  const hashingFunction = async blob => {
11
10
  const hasher = await createHasher();
12
11
  return hasher.hash(blob);
13
12
  };
14
13
 
15
- const createProbingFunction = (store, deferredUploadId, collectionName) => async chunks => {
14
+ const createProbingFunction = (store, deferredUploadId, collectionName, traceContext) => async chunks => {
16
15
  const response = await store.probeChunks(hashedChunks(chunks), {
17
16
  collectionName,
18
- uploadId: getMediaFeatureFlag('mediaUploadApiV2', store.featureFlags) ? await deferredUploadId : undefined
19
- });
17
+ uploadId: await deferredUploadId
18
+ }, traceContext);
20
19
  const results = response.data.results;
21
20
  return Object.values(results).map(result => result.exists);
22
21
  };
23
22
 
24
- const createUploadingFunction = (store, deferredUploadId, collectionName) => async chunk => {
25
- const options = getMediaFeatureFlag('mediaUploadApiV2', store.featureFlags) ? {
26
- partNumber: chunk.partNumber,
27
- uploadId: await deferredUploadId
28
- } : {};
23
+ const createUploadingFunction = (store, deferredUploadId, collectionName, traceContext) => async chunk => {
29
24
  return await store.uploadChunk(chunk.hash, chunk.blob, {
30
25
  collectionName,
31
- ...options
32
- });
26
+ partNumber: chunk.partNumber,
27
+ uploadId: await deferredUploadId
28
+ }, traceContext);
33
29
  };
34
30
 
35
- const createProcessingFunction = (store, deferredUploadId, collection) => {
31
+ const createProcessingFunction = (store, deferredUploadId, collection, traceContext) => {
36
32
  let offset = 0;
37
33
  return async chunks => {
38
34
  await store.appendChunksToUpload(await deferredUploadId, {
39
35
  chunks: hashedChunks(chunks),
40
36
  offset
41
- }, collection);
37
+ }, collection, traceContext);
42
38
  offset += chunks.length;
43
39
  };
44
40
  };
45
41
 
46
- const createFileFromUpload = async (file, store, uploadableFileUpfrontIds, uploadId) => {
42
+ const createFileFromUpload = async (file, store, uploadableFileUpfrontIds, uploadId, traceContext) => {
47
43
  const {
48
44
  collection,
49
45
  name,
@@ -61,10 +57,10 @@ const createFileFromUpload = async (file, store, uploadableFileUpfrontIds, uploa
61
57
  occurrenceKey,
62
58
  collection,
63
59
  replaceFileId: id
64
- });
60
+ }, traceContext);
65
61
  };
66
62
 
67
- export const uploadFile = (file, store, uploadableFileUpfrontIds, callbacks) => {
63
+ export const uploadFile = (file, store, uploadableFileUpfrontIds, callbacks, traceContext) => {
68
64
  const {
69
65
  content,
70
66
  collection
@@ -77,7 +73,7 @@ export const uploadFile = (file, store, uploadableFileUpfrontIds, callbacks) =>
77
73
  let chunkSize = CHUNK_SIZE;
78
74
 
79
75
  try {
80
- if (content instanceof Blob && getMediaFeatureFlag('mediaUploadApiV2', store.featureFlags)) {
76
+ if (content instanceof Blob) {
81
77
  chunkSize = calculateChunkSize(content.size);
82
78
  }
83
79
  } catch (err) {
@@ -101,10 +97,10 @@ export const uploadFile = (file, store, uploadableFileUpfrontIds, callbacks) =>
101
97
  probingBatchSize: 100,
102
98
  chunkSize,
103
99
  uploadingConcurrency: 3,
104
- uploadingFunction: createUploadingFunction(store, deferredUploadId, collection),
105
- probingFunction: createProbingFunction(store, deferredUploadId, collection),
100
+ uploadingFunction: createUploadingFunction(store, deferredUploadId, collection, traceContext),
101
+ probingFunction: createProbingFunction(store, deferredUploadId, collection, traceContext),
106
102
  processingBatchSize: PROCESSING_BATCH_SIZE,
107
- processingFunction: createProcessingFunction(store, deferredUploadId, collection)
103
+ processingFunction: createProcessingFunction(store, deferredUploadId, collection, traceContext)
108
104
  }, {
109
105
  onProgress(progress) {
110
106
  if (callbacks) {
@@ -116,7 +112,7 @@ export const uploadFile = (file, store, uploadableFileUpfrontIds, callbacks) =>
116
112
 
117
113
  const onUploadFinish = callbacks && callbacks.onUploadFinish || (() => {});
118
114
 
119
- const subscription = from(deferredUploadId).pipe(concatMap(uploadId => chunkinatorObservable.pipe(concatMap(() => from(createFileFromUpload(file, store, uploadableFileUpfrontIds, uploadId)))))).subscribe({
115
+ const subscription = from(deferredUploadId).pipe(concatMap(uploadId => chunkinatorObservable.pipe(concatMap(() => from(createFileFromUpload(file, store, uploadableFileUpfrontIds, uploadId, traceContext)))))).subscribe({
120
116
  error: err => onUploadFinish(err),
121
117
  complete: () => onUploadFinish()
122
118
  });
@@ -2,6 +2,7 @@
2
2
  // @see https://github.com/graphql/dataloader/issues/249
3
3
  import 'setimmediate';
4
4
  import Dataloader from 'dataloader';
5
+ import { getRandomHex } from '@atlaskit/media-common';
5
6
  export const MAX_BATCH_SIZE = 100;
6
7
 
7
8
  const isBatchLoadingErrorResult = result => {
@@ -17,7 +18,9 @@ export const getItemsFromKeys = (dataloaderKeys, fileItems) => {
17
18
  collection
18
19
  } = fileItem;
19
20
  const key = makeCacheKey(id, collection);
20
- prev[key] = isBatchLoadingErrorResult(fileItem) ? fileItem.error : fileItem.details;
21
+ prev[key] = isBatchLoadingErrorResult(fileItem) ? fileItem.error : { ...fileItem.details,
22
+ metadataTraceContext: fileItem.metadataTraceContext
23
+ };
21
24
  return prev;
22
25
  }, {});
23
26
  return dataloaderKeys.map(dataloaderKey => {
@@ -55,12 +58,19 @@ export function createBatchLoadingFunc(mediaStore) {
55
58
  }, {});
56
59
  const items = [];
57
60
  await Promise.all(Object.keys(fileIdsByCollection).map(async collectionNameKey => {
61
+ const metadataTraceContext = {
62
+ traceId: getRandomHex(8),
63
+ spanId: getRandomHex(8)
64
+ };
58
65
  const fileIds = fileIdsByCollection[collectionNameKey];
59
66
  const collectionName = collectionNameKey === nonCollectionName ? undefined : collectionNameKey;
60
67
 
61
68
  try {
62
- const response = await mediaStore.getItems(fileIds, collectionName);
63
- items.push(...response.data.items);
69
+ const response = await mediaStore.getItems(fileIds, collectionName, metadataTraceContext);
70
+ const itemsWithMetadataTraceContext = response.data.items.map(item => ({ ...item,
71
+ metadataTraceContext
72
+ }));
73
+ items.push(...itemsWithMetadataTraceContext);
64
74
  } catch (error) {
65
75
  fileIds.forEach(fileId => {
66
76
  items.push({
@@ -19,7 +19,26 @@ export function isFetchNetworkError(err) {
19
19
  export function isRateLimitedError(error) {
20
20
  return !!error && isRequestError(error) && error.attributes.statusCode === 429 || !!error && !!error.message && error.message.includes('429');
21
21
  }
22
+ export const ZipkinHeaderKeys = {
23
+ traceId: 'x-b3-traceid',
24
+ spanId: 'x-b3-spanid',
25
+ parentSpanId: 'x-b3-parentspanid',
26
+ sampled: 'x-b3-sampled',
27
+ flags: 'x-b3-flags'
28
+ };
29
+
30
+ const mapTraceIdToRequestHeaders = traceContext => {
31
+ return traceContext ? {
32
+ [ZipkinHeaderKeys.traceId]: traceContext.traceId,
33
+ [ZipkinHeaderKeys.spanId]: traceContext.spanId
34
+ } : {};
35
+ };
36
+
22
37
  export function mapAuthToRequestHeaders(auth) {
38
+ if (!auth) {
39
+ return {};
40
+ }
41
+
23
42
  if (isClientBasedAuth(auth)) {
24
43
  return {
25
44
  'X-Client-Id': auth.clientId,
@@ -47,15 +66,14 @@ export function createUrl(url, {
47
66
  parsedUrl.searchParams.sort();
48
67
  return parsedUrl.toString();
49
68
  }
50
- export function withAuth(auth) {
51
- return headers => {
52
- if (auth) {
53
- return { ...(headers || {}),
54
- ...mapAuthToRequestHeaders(auth)
55
- };
56
- }
69
+ export function extendHeaders(headers, auth, traceContext) {
70
+ if (!auth && !traceContext && !headers) {
71
+ return undefined;
72
+ }
57
73
 
58
- return headers;
74
+ return { ...(headers !== null && headers !== void 0 ? headers : {}),
75
+ ...mapAuthToRequestHeaders(auth),
76
+ ...mapTraceIdToRequestHeaders(traceContext)
59
77
  };
60
78
  }
61
79
  /**
@@ -1,6 +1,6 @@
1
1
  export { RequestError, isRequestError } from './errors';
2
2
  export { isRateLimitedError } from './helpers';
3
- import { createUrl, fetchRetry, createProcessFetchResponse, withAuth } from './helpers';
3
+ import { createUrl, fetchRetry, createProcessFetchResponse, extendHeaders } from './helpers';
4
4
  export async function request(url, options = {}, controller) {
5
5
  const {
6
6
  method = 'GET',
@@ -9,14 +9,16 @@ export async function request(url, options = {}, controller) {
9
9
  params,
10
10
  headers,
11
11
  body,
12
- clientOptions = {}
12
+ clientOptions = {},
13
+ traceContext
13
14
  } = options;
14
15
  const {
15
16
  retryOptions
16
17
  } = clientOptions;
17
18
  const metadata = {
18
19
  method,
19
- endpoint
20
+ endpoint,
21
+ traceContext
20
22
  }; // TODO BMPT-918: add client timeout feature behing a FF (using clientOptions.clientTimeout + Promise.race)
21
23
 
22
24
  const doFetch = () => fetch(createUrl(url, {
@@ -24,7 +26,7 @@ export async function request(url, options = {}, controller) {
24
26
  }), {
25
27
  method,
26
28
  body,
27
- headers: withAuth(auth)(headers),
29
+ headers: extendHeaders(headers, auth, traceContext),
28
30
  signal: controller && controller.signal
29
31
  }).then(createProcessFetchResponse(metadata));
30
32
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/media-client",
3
- "version": "18.1.0",
3
+ "version": "19.1.0",
4
4
  "sideEffects": false
5
5
  }
@@ -72,7 +72,7 @@ export var CollectionFetcher = /*#__PURE__*/function () {
72
72
  }
73
73
  }, {
74
74
  key: "getItems",
75
- value: function getItems(collectionName, params) {
75
+ value: function getItems(collectionName, params, traceContext) {
76
76
  var _this2 = this;
77
77
 
78
78
  if (!collectionCache[collectionName]) {
@@ -83,7 +83,7 @@ export var CollectionFetcher = /*#__PURE__*/function () {
83
83
  var subject = collection.subject;
84
84
  this.mediaStore.getCollectionItems(collectionName, _objectSpread(_objectSpread({}, params), {}, {
85
85
  details: 'full'
86
- })).then(function (items) {
86
+ }), traceContext).then(function (items) {
87
87
  var _items$data = items.data,
88
88
  contents = _items$data.contents,
89
89
  nextInclusiveStartKey = _items$data.nextInclusiveStartKey;
@@ -103,14 +103,14 @@ export var CollectionFetcher = /*#__PURE__*/function () {
103
103
  }, {
104
104
  key: "removeFile",
105
105
  value: function () {
106
- var _removeFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(id, collectionName, occurrenceKey) {
106
+ var _removeFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(id, collectionName, occurrenceKey, traceContext) {
107
107
  var collection;
108
108
  return _regeneratorRuntime.wrap(function _callee$(_context) {
109
109
  while (1) {
110
110
  switch (_context.prev = _context.next) {
111
111
  case 0:
112
112
  _context.next = 2;
113
- return this.mediaStore.removeCollectionFile(id, collectionName, occurrenceKey);
113
+ return this.mediaStore.removeCollectionFile(id, collectionName, occurrenceKey, traceContext);
114
114
 
115
115
  case 2:
116
116
  this.removeFromCache(id, collectionName);
@@ -125,7 +125,7 @@ export var CollectionFetcher = /*#__PURE__*/function () {
125
125
  }, _callee, this);
126
126
  }));
127
127
 
128
- function removeFile(_x, _x2, _x3) {
128
+ function removeFile(_x, _x2, _x3, _x4) {
129
129
  return _removeFile.apply(this, arguments);
130
130
  }
131
131
 
@@ -134,7 +134,7 @@ export var CollectionFetcher = /*#__PURE__*/function () {
134
134
  }, {
135
135
  key: "loadNextPage",
136
136
  value: function () {
137
- var _loadNextPage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(collectionName, params) {
137
+ var _loadNextPage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(collectionName, params, traceContext) {
138
138
  var collection, isLoading, _collectionCache$coll, inclusiveStartKey, currentItems, subject, response, _response$data, contents, nextInclusiveStartKey, newItems, items;
139
139
 
140
140
  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
@@ -158,7 +158,7 @@ export var CollectionFetcher = /*#__PURE__*/function () {
158
158
  return this.mediaStore.getCollectionItems(collectionName, _objectSpread(_objectSpread({}, params), {}, {
159
159
  inclusiveStartKey: inclusiveStartKey,
160
160
  details: 'full'
161
- }));
161
+ }), traceContext);
162
162
 
163
163
  case 8:
164
164
  response = _context2.sent;
@@ -182,7 +182,7 @@ export var CollectionFetcher = /*#__PURE__*/function () {
182
182
  }, _callee2, this);
183
183
  }));
184
184
 
185
- function loadNextPage(_x4, _x5) {
185
+ function loadNextPage(_x5, _x6, _x7) {
186
186
  return _loadNextPage.apply(this, arguments);
187
187
  }
188
188