@aws-amplify/datastore 5.0.1-api-v6-models.c1977f8.0 → 5.0.1-api-v6-models.891fe0d.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 (70) hide show
  1. package/lib/authModeStrategies/defaultAuthStrategy.js +1 -1
  2. package/lib/authModeStrategies/multiAuthStrategy.js +29 -105
  3. package/lib/datastore/datastore.js +887 -1370
  4. package/lib/index.js +1 -1
  5. package/lib/predicates/index.js +53 -102
  6. package/lib/predicates/next.js +310 -557
  7. package/lib/predicates/sort.js +24 -27
  8. package/lib/ssr/index.js +1 -1
  9. package/lib/storage/adapter/AsyncStorageAdapter.js +115 -449
  10. package/lib/storage/adapter/AsyncStorageDatabase.js +215 -480
  11. package/lib/storage/adapter/InMemoryStore.js +27 -101
  12. package/lib/storage/adapter/InMemoryStore.native.js +1 -1
  13. package/lib/storage/adapter/IndexedDBAdapter.js +441 -1002
  14. package/lib/storage/adapter/StorageAdapterBase.js +177 -396
  15. package/lib/storage/adapter/getDefaultAdapter/index.js +5 -6
  16. package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -2
  17. package/lib/storage/relationship.js +174 -260
  18. package/lib/storage/storage.js +244 -507
  19. package/lib/sync/datastoreConnectivity.js +29 -84
  20. package/lib/sync/datastoreReachability/index.js +1 -1
  21. package/lib/sync/datastoreReachability/index.native.js +2 -2
  22. package/lib/sync/index.js +512 -885
  23. package/lib/sync/merger.js +30 -133
  24. package/lib/sync/outbox.js +147 -302
  25. package/lib/sync/processors/errorMaps.js +30 -80
  26. package/lib/sync/processors/mutation.js +331 -579
  27. package/lib/sync/processors/subscription.js +268 -428
  28. package/lib/sync/processors/sync.js +276 -464
  29. package/lib/sync/utils.js +248 -393
  30. package/lib/tsconfig.tsbuildinfo +1 -1
  31. package/lib/types.js +16 -58
  32. package/lib/util.js +335 -575
  33. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -1
  34. package/lib-esm/authModeStrategies/multiAuthStrategy.js +27 -103
  35. package/lib-esm/datastore/datastore.js +874 -1359
  36. package/lib-esm/index.js +6 -6
  37. package/lib-esm/predicates/index.js +54 -104
  38. package/lib-esm/predicates/next.js +306 -555
  39. package/lib-esm/predicates/sort.js +24 -27
  40. package/lib-esm/ssr/index.js +1 -1
  41. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -446
  42. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -477
  43. package/lib-esm/storage/adapter/InMemoryStore.js +27 -102
  44. package/lib-esm/storage/adapter/IndexedDBAdapter.js +436 -997
  45. package/lib-esm/storage/adapter/StorageAdapterBase.js +172 -392
  46. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -3
  47. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -1
  48. package/lib-esm/storage/relationship.js +173 -260
  49. package/lib-esm/storage/storage.js +236 -499
  50. package/lib-esm/sync/datastoreConnectivity.js +26 -82
  51. package/lib-esm/sync/datastoreReachability/index.js +1 -1
  52. package/lib-esm/sync/datastoreReachability/index.native.js +1 -1
  53. package/lib-esm/sync/index.js +498 -872
  54. package/lib-esm/sync/merger.js +28 -131
  55. package/lib-esm/sync/outbox.js +143 -298
  56. package/lib-esm/sync/processors/errorMaps.js +32 -82
  57. package/lib-esm/sync/processors/mutation.js +324 -572
  58. package/lib-esm/sync/processors/subscription.js +258 -418
  59. package/lib-esm/sync/processors/sync.js +269 -457
  60. package/lib-esm/sync/utils.js +245 -390
  61. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  62. package/lib-esm/types.js +16 -59
  63. package/lib-esm/util.js +335 -577
  64. package/package.json +12 -12
  65. package/src/datastore/datastore.ts +4 -3
  66. package/src/storage/adapter/getDefaultAdapter/index.ts +1 -3
  67. package/src/sync/processors/mutation.ts +1 -1
  68. package/src/sync/processors/sync.ts +1 -1
  69. package/src/sync/utils.ts +1 -1
  70. package/src/util.ts +44 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/datastore",
3
- "version": "5.0.1-api-v6-models.c1977f8.0+c1977f8",
3
+ "version": "5.0.1-api-v6-models.891fe0d.0+891fe0d",
4
4
  "description": "AppSyncLocal support for aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -47,8 +47,8 @@
47
47
  "ssr"
48
48
  ],
49
49
  "dependencies": {
50
- "@aws-amplify/api": "6.0.1-api-v6-models.c1977f8.0+c1977f8",
51
- "@aws-amplify/auth": "6.0.1-api-v6-models.c1977f8.0+c1977f8",
50
+ "@aws-amplify/api": "6.0.1-api-v6-models.891fe0d.0+891fe0d",
51
+ "@aws-amplify/auth": "6.0.1-api-v6-models.891fe0d.0+891fe0d",
52
52
  "buffer": "4.9.2",
53
53
  "idb": "5.0.6",
54
54
  "immer": "9.0.6",
@@ -56,11 +56,11 @@
56
56
  "ulid": "^2.3.0"
57
57
  },
58
58
  "peerDependencies": {
59
- "@aws-amplify/core": "6.0.1-api-v6-models.c1977f8.0+c1977f8"
59
+ "@aws-amplify/core": "6.0.1-api-v6-models.891fe0d.0+891fe0d"
60
60
  },
61
61
  "devDependencies": {
62
- "@aws-amplify/core": "6.0.1-api-v6-models.c1977f8.0+c1977f8",
63
- "@aws-amplify/react-native": "1.0.1-api-v6-models.c1977f8.0+c1977f8",
62
+ "@aws-amplify/core": "6.0.1-api-v6-models.891fe0d.0+891fe0d",
63
+ "@aws-amplify/react-native": "1.0.1-api-v6-models.891fe0d.0+891fe0d",
64
64
  "@types/uuid-validate": "^0.0.1",
65
65
  "dexie": "3.2.2",
66
66
  "dexie-export-import": "1.0.3",
@@ -73,7 +73,7 @@
73
73
  "name": "DataStore (top-level class)",
74
74
  "path": "./lib-esm/index.js",
75
75
  "import": "{ Amplify, DataStore }",
76
- "limit": "89.0 kB"
76
+ "limit": "65.25 kB"
77
77
  }
78
78
  ],
79
79
  "jest": {
@@ -106,10 +106,7 @@
106
106
  "__tests__/model.ts",
107
107
  "__tests__/schema.ts",
108
108
  "__tests__/helpers/",
109
- "__tests__/commonAdapterTests.ts",
110
- "__tests__/DataStore.ts",
111
- "__tests__/connectivityHandling.test.ts",
112
- "__tests__/mutation.test.ts"
109
+ "__tests__/commonAdapterTests.ts"
113
110
  ],
114
111
  "moduleFileExtensions": [
115
112
  "ts",
@@ -133,7 +130,10 @@
133
130
  "dist",
134
131
  "lib",
135
132
  "lib-esm"
133
+ ],
134
+ "setupFilesAfterEnv": [
135
+ "<rootDir>/jest.setup.js"
136
136
  ]
137
137
  },
138
- "gitHead": "c1977f8efaae41b693495a2d6c53c503d0038e95"
138
+ "gitHead": "891fe0dc64e168de42b3b8d42def3eb9aed89efa"
139
139
  }
@@ -11,7 +11,7 @@ import {
11
11
  enablePatches,
12
12
  Patch,
13
13
  } from 'immer';
14
- import { amplifyUuid } from '@aws-amplify/core/internals/utils';
14
+ import { amplifyUuid, isBrowser } from '@aws-amplify/core/internals/utils';
15
15
  import { Observable, SubscriptionLike, filter } from 'rxjs';
16
16
  import { defaultAuthStrategy, multiAuthStrategy } from '../authModeStrategies';
17
17
  import {
@@ -1554,7 +1554,9 @@ class DataStore {
1554
1554
  .subscribe({
1555
1555
  next: ({ type, data }) => {
1556
1556
  // In the Browser, we can begin returning data once subscriptions are in place.
1557
- const readyType = ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED;
1557
+ const readyType = isBrowser()
1558
+ ? ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED
1559
+ : ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY;
1558
1560
 
1559
1561
  if (type === readyType) {
1560
1562
  this.initResolve();
@@ -1724,7 +1726,6 @@ class DataStore {
1724
1726
  return this.runningProcesses
1725
1727
  .add(async () => {
1726
1728
  await this.start();
1727
-
1728
1729
  if (!this.storage) {
1729
1730
  throw new Error('No storage to save to');
1730
1731
  }
@@ -3,10 +3,8 @@
3
3
  import { Adapter } from '..';
4
4
  import IndexedDBAdapter from '../IndexedDBAdapter';
5
5
  import AsyncStorageAdapter from '../AsyncStorageAdapter';
6
- import { isWebWorker } from '@aws-amplify/core/internals/utils';
6
+ import { isWebWorker, isBrowser } from '@aws-amplify/core/internals/utils';
7
7
  const getDefaultAdapter: () => Adapter = () => {
8
- const isBrowser = true; // TODO(v6): Update this for SSR
9
-
10
8
  if ((isBrowser && window.indexedDB) || (isWebWorker() && self.indexedDB)) {
11
9
  return IndexedDBAdapter as Adapter;
12
10
  }
@@ -13,7 +13,6 @@ import {
13
13
  GraphQLAuthMode,
14
14
  AmplifyError,
15
15
  } from '@aws-amplify/core/internals/utils';
16
- import { ConsoleLogger } from '@aws-amplify/core';
17
16
 
18
17
  import { Observable, Observer } from 'rxjs';
19
18
  import { MutationEvent } from '../';
@@ -47,6 +46,7 @@ import {
47
46
  getTokenForCustomAuth,
48
47
  } from '../utils';
49
48
  import { getMutationErrorType } from './errorMaps';
49
+ import { ConsoleLogger } from '@aws-amplify/core';
50
50
 
51
51
  const MAX_ATTEMPTS = 10;
52
52
 
@@ -34,7 +34,7 @@ import {
34
34
  AmplifyError,
35
35
  } from '@aws-amplify/core/internals/utils';
36
36
 
37
- import { Amplify, Hub, ConsoleLogger } from '@aws-amplify/core';
37
+ import { Amplify, ConsoleLogger, Hub } from '@aws-amplify/core';
38
38
 
39
39
  import { ModelPredicateCreator } from '../../predicates';
40
40
  import { getSyncErrorType } from './errorMaps';
package/src/sync/utils.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { GraphQLAuthError } from '@aws-amplify/api';
4
- import { ConsoleLogger } from '@aws-amplify/core';
5
4
  import type { GraphQLError } from 'graphql';
6
5
  import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
7
6
  import { ModelInstanceCreator } from '../datastore/datastore';
@@ -40,6 +39,7 @@ import {
40
39
  IDENTIFIER_KEY_SEPARATOR,
41
40
  } from '../util';
42
41
  import { MutationEvent } from './';
42
+ import { ConsoleLogger } from '@aws-amplify/core';
43
43
 
44
44
  const logger = new ConsoleLogger('DataStore');
45
45
 
package/src/util.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { monotonicFactory, ULID } from 'ulid';
4
- import { amplifyUuid, AmplifyUrl } from '@aws-amplify/core/internals/utils';
4
+ import {
5
+ amplifyUuid,
6
+ AmplifyUrl,
7
+ WordArray,
8
+ } from '@aws-amplify/core/internals/utils';
5
9
  import { produce, applyPatches, Patch } from 'immer';
6
10
  import { ModelInstanceCreator } from './datastore/datastore';
7
11
  import {
@@ -373,11 +377,45 @@ export const isSafariCompatabilityMode: () => Promise<boolean> = async () => {
373
377
  return safariCompatabilityModeResult;
374
378
  };
375
379
 
376
- // TODO(v6): refactor this random number logic auth -> core
377
- // const randomBytes = (nBytes: number): Buffer => {
378
- // return Buffer.from(new WordArray().random(nBytes).toString(), 'hex');
379
- // };
380
- const prng = () => 10 / 0xff;
380
+ const HEX_TO_SHORT: Record<string, number> = {};
381
+
382
+ for (let i = 0; i < 256; i++) {
383
+ let encodedByte = i.toString(16).toLowerCase();
384
+ if (encodedByte.length === 1) {
385
+ encodedByte = `0${encodedByte}`;
386
+ }
387
+
388
+ HEX_TO_SHORT[encodedByte] = i;
389
+ }
390
+
391
+ const getBytesFromHex = (encoded: string): Uint8Array => {
392
+ if (encoded.length % 2 !== 0) {
393
+ throw new Error('Hex encoded strings must have an even number length');
394
+ }
395
+
396
+ const out = new Uint8Array(encoded.length / 2);
397
+ for (let i = 0; i < encoded.length; i += 2) {
398
+ const encodedByte = encoded.slice(i, i + 2).toLowerCase();
399
+ if (encodedByte in HEX_TO_SHORT) {
400
+ out[i / 2] = HEX_TO_SHORT[encodedByte];
401
+ } else {
402
+ throw new Error(
403
+ `Cannot decode unrecognized sequence ${encodedByte} as hexadecimal`
404
+ );
405
+ }
406
+ }
407
+
408
+ return out;
409
+ };
410
+
411
+ const randomBytes = (nBytes: number): Uint8Array => {
412
+ const str = new WordArray().random(nBytes).toString();
413
+
414
+ return getBytesFromHex(str);
415
+ };
416
+
417
+ const prng = () => randomBytes(1)[0] / 0xff;
418
+
381
419
  export function monotonicUlidFactory(seed?: number): ULID {
382
420
  const ulid = monotonicFactory(prng);
383
421