@salesforce/nimbus-plugin-lds 1.302.0 → 1.304.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -447,4 +447,107 @@ class MockNimbusNetworkAdapter {
447
447
  }
448
448
  }
449
449
 
450
- export { JsSqliteStorePlugin, JsSqliteStoreWebWorkerPlugin, MockNimbusNetworkAdapter, isUpsertOperation, sortedSchemaMigrations };
450
+ function mockNimbusBinaryStorePlugin(plugin) {
451
+ var _a;
452
+ globalThis.__nimbus = {
453
+ ...(globalThis.__nimbus || {}),
454
+ plugins: {
455
+ // eslint-disable-next-line @salesforce/lds/no-optional-chaining
456
+ ...(((_a = globalThis.__nimbus) === null || _a === void 0 ? void 0 : _a.plugins) || {}),
457
+ LdsBinaryStorePlugin: plugin,
458
+ },
459
+ };
460
+ }
461
+ // simple in-memory BinaryStorePlugin implementation
462
+ class MockNimbusBinaryStorePlugin {
463
+ constructor() {
464
+ this.counter = 0;
465
+ this.store = new Map();
466
+ this.streamsInProgress = new Map();
467
+ this.streamTypes = new Map();
468
+ }
469
+ storeBinary(data, type, size, onSuccess, _onError) {
470
+ const uri = this.counter.toString();
471
+ this.counter++;
472
+ this.store.set(uri, {
473
+ data,
474
+ type,
475
+ size,
476
+ canonicalUrl: null,
477
+ ttlSeconds: null,
478
+ expiration: null,
479
+ });
480
+ onSuccess(uri);
481
+ }
482
+ cacheBinary(url, ttlSeconds, onSuccess, _onError) {
483
+ // not gonna actually download a file in this mock impl
484
+ const mockData = new Uint8Array([1, 2, 3, 4, 5]);
485
+ const mockType = 'image/png';
486
+ const uri = this.counter.toString();
487
+ this.counter++;
488
+ this.store.set(uri, {
489
+ data: mockData,
490
+ type: mockType,
491
+ size: mockData.length,
492
+ canonicalUrl: url,
493
+ ttlSeconds,
494
+ expiration: Date.now() + ttlSeconds * 1000,
495
+ });
496
+ onSuccess(uri);
497
+ }
498
+ removeBinary(uri, onSuccess, _onError) {
499
+ onSuccess(this.store.delete(uri));
500
+ }
501
+ setCanonicalUrl(uri, canonicalUrl, ttlSeconds, onSuccess, onError) {
502
+ const fileInfo = this.store.get(uri);
503
+ // if the local file doesn't exist treat this same as calling "cacheBinary"
504
+ if (fileInfo === undefined) {
505
+ this.cacheBinary(canonicalUrl, ttlSeconds, onSuccess, onError);
506
+ return;
507
+ }
508
+ // else update the canonicalUrl and expiration info
509
+ this.store.set(uri, {
510
+ ...fileInfo,
511
+ canonicalUrl,
512
+ ttlSeconds,
513
+ // immediately expire
514
+ expiration: Date.now() - 1,
515
+ });
516
+ onSuccess(uri);
517
+ }
518
+ getBinary(uri) {
519
+ return this.store.get(uri);
520
+ }
521
+ createStream(type, onSuccess, _onError) {
522
+ const uri = this.counter.toString();
523
+ this.streamsInProgress.set(uri, new Uint8Array());
524
+ this.streamTypes.set(uri, type);
525
+ onSuccess(uri);
526
+ }
527
+ writeToStream(uri, chunk, onSuccess, _onError) {
528
+ const existingBytes = this.streamsInProgress.get(uri);
529
+ const newBytes = Buffer.from(chunk, 'base64');
530
+ let combinedBytes = new Uint8Array([...existingBytes, ...newBytes]);
531
+ this.streamsInProgress.set(uri, combinedBytes);
532
+ onSuccess(uri);
533
+ }
534
+ closeStream(uri, onSuccess, _onError) {
535
+ var _a, _b;
536
+ const data = (_a = this.streamsInProgress.get(uri)) !== null && _a !== void 0 ? _a : new Uint8Array();
537
+ const type = (_b = this.streamTypes.get(uri)) !== null && _b !== void 0 ? _b : 'undefined';
538
+ this.streamsInProgress.delete(uri);
539
+ this.streamTypes.delete(uri);
540
+ // also add it to the store for testing
541
+ this.store.set(uri, {
542
+ data: data,
543
+ type: type,
544
+ size: data.length,
545
+ canonicalUrl: null,
546
+ ttlSeconds: null,
547
+ expiration: null,
548
+ });
549
+ onSuccess(uri);
550
+ }
551
+ }
552
+
553
+ export { JsSqliteStorePlugin, JsSqliteStoreWebWorkerPlugin, MockNimbusBinaryStorePlugin, MockNimbusNetworkAdapter, isUpsertOperation, mockNimbusBinaryStorePlugin, sortedSchemaMigrations };
@@ -79,4 +79,32 @@ export interface BinaryStorePlugin {
79
79
  * @param onError Callback to call if there was an error.
80
80
  */
81
81
  setCanonicalUrl(uri: string, canonicalUrl: string, ttlSeconds: number, onSuccess: (uri: string) => void, onError: (errorMessage: string) => void): void;
82
+ /**
83
+ * Creates a stream that can be used to write buffered chunks of base64-encoded data.
84
+ * This function should be used in conjunction with writeToStream() and closeStream().
85
+ *
86
+ * @param type The MIME type of the binary file. Ex: "image/png".
87
+ * @returns A Promise of a string representing the URI of the binary file. NOTE: the URI
88
+ * to a binary file should be treated as opaque (only use it to pass into methods
89
+ * on this interface).
90
+ */
91
+ createStream(type: string, onSuccess: (uri: string) => void, onError: (errorMessage: string) => void): void;
92
+ /**
93
+ * Writes a base64-encoded chunk of data to the stream identified by the stream uri provided
94
+ * by the call from createStream().
95
+ *
96
+ * @param uri The URI of the file.
97
+ * @returns A Promise of a string representing the URI of the binary file that was succesfully
98
+ * written to.
99
+ */
100
+ writeToStream(uri: string, chunk: string, onSuccess: (uri: string) => void, onError: (errorMessage: string) => void): void;
101
+ /**
102
+ * Closes the stream represented by the given uri. Once a stream is closed, it cannot be
103
+ * written to and will cause an error to be returned.
104
+ *
105
+ * @param uri The URI of the file.
106
+ * @returns A Promise of a string representing the URI of the binary file that was succesfully
107
+ * closed.
108
+ */
109
+ closeStream(uri: string, onSuccess: (uri: string) => void, onError: (errorMessage: string) => void): void;
82
110
  }
@@ -9,3 +9,4 @@ export { sortedSchemaMigrations, isUpsertOperation } from './SqliteStorePlugin';
9
9
  export { JsSqliteStorePlugin } from './JsSqliteStorePlugin/JsSqliteStorePlugin';
10
10
  export { JsSqliteStoreWebWorkerPlugin } from './JsSqliteStorePlugin/JsWorkerSqliteStorePlugin';
11
11
  export { MockNimbusNetworkAdapter } from './mocks/MockNimbusNetworkAdapter';
12
+ export { MockNimbusBinaryStorePlugin, mockNimbusBinaryStorePlugin, } from './mocks/MockNimbusBinaryStorePlugin';
@@ -0,0 +1,25 @@
1
+ import type { BinaryStorePlugin } from '../BinaryStorePlugin';
2
+ export declare function mockNimbusBinaryStorePlugin(plugin: BinaryStorePlugin): void;
3
+ interface FileInfo {
4
+ data: Uint8Array;
5
+ type: string;
6
+ size: number;
7
+ canonicalUrl: string | null;
8
+ ttlSeconds: number | null;
9
+ expiration: number | null;
10
+ }
11
+ export declare class MockNimbusBinaryStorePlugin implements BinaryStorePlugin {
12
+ private counter;
13
+ private store;
14
+ private streamsInProgress;
15
+ private streamTypes;
16
+ storeBinary(data: Uint8Array, type: string, size: number, onSuccess: (uri: string) => void, _onError: (errorMessage: string) => void): void;
17
+ cacheBinary(url: string, ttlSeconds: number, onSuccess: (uri: string) => void, _onError: (errorMessage: string) => void): void;
18
+ removeBinary(uri: string, onSuccess: (wasFound: boolean) => void, _onError: (errorMessage: string) => void): void;
19
+ setCanonicalUrl(uri: string, canonicalUrl: string, ttlSeconds: number, onSuccess: (uri: string) => void, onError: (errorMessage: string) => void): void;
20
+ getBinary(uri: string): FileInfo | undefined;
21
+ createStream(type: string, onSuccess: (uri: string) => void, _onError: (errorMessage: string) => void): void;
22
+ writeToStream(uri: string, chunk: string, onSuccess: (uri: string) => void, _onError: (errorMessage: string) => void): void;
23
+ closeStream(uri: string, onSuccess: (uri: string) => void, _onError: (errorMessage: string) => void): void;
24
+ }
25
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/nimbus-plugin-lds",
3
- "version": "1.302.0",
3
+ "version": "1.304.0",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "description": "Nimbus plugins for LDS on Mobile native integrations: durable store, networking, and draft queue.",
6
6
  "types": "dist/types/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rollup/plugin-wasm": "^5.2.0",
28
- "@salesforce/lds-store-sql": "^1.302.0",
28
+ "@salesforce/lds-store-sql": "^1.304.0",
29
29
  "@types/sql.js": "1.4.4",
30
30
  "nimbus-types": "^2.0.0-alpha1",
31
31
  "rollup-plugin-string": "^3.0.0",