• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

share / sharedb / 6549902929

17 Oct 2023 04:09PM UTC coverage: 97.51%. Remained the same
6549902929

push

github

alecgibson
4.1.1

1567 of 1778 branches covered (0.0%)

3368 of 3454 relevant lines covered (97.51%)

867.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/lib/read-snapshots-request.js
1
var ShareDBError = require('./error');
3✔
2

3
module.exports = ReadSnapshotsRequest;
3✔
4

5
/**
6
 * Context object passed to "readSnapshots" middleware functions
7
 *
8
 * @param {string} collection
9
 * @param {Snapshot[]} snapshots - snapshots being read
10
 * @param {keyof Backend.prototype.SNAPSHOT_TYPES} snapshotType - the type of snapshot read being
11
 *   performed
12
 */
13
function ReadSnapshotsRequest(collection, snapshots, snapshotType) {
14
  this.collection = collection;
1,470✔
15
  this.snapshots = snapshots;
1,470✔
16
  this.snapshotType = snapshotType;
1,470✔
17

18
  // Added by Backend#trigger
19
  this.action = null;
1,470✔
20
  this.agent = null;
1,470✔
21
  this.backend = null;
1,470✔
22

23
  /**
24
   * Map of doc id to error: `{[docId: string]: string | Error}`
25
   */
26
  this._idToError = null;
1,470✔
27
}
28

29
/**
30
 * Rejects the read of a specific snapshot. A rejected snapshot read will not have that snapshot's
31
 * data sent down to the client.
32
 *
33
 * If the error has a `code` property of `"ERR_SNAPSHOT_READ_SILENT_REJECTION"`, then the Share
34
 * client will not pass the error to user code, but will still do things like cancel subscriptions.
35
 * The `#rejectSnapshotReadSilent(snapshot, errorMessage)` method can also be used for convenience.
36
 *
37
 * @param {Snapshot} snapshot
38
 * @param {string | Error} error
39
 *
40
 * @see #rejectSnapshotReadSilent
41
 * @see ShareDBError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION
42
 * @see ShareDBError.CODES.ERR_SNAPSHOT_READS_REJECTED
43
 */
44
ReadSnapshotsRequest.prototype.rejectSnapshotRead = function(snapshot, error) {
3✔
45
  if (!this._idToError) {
36!
46
    this._idToError = {};
36✔
47
  }
48
  this._idToError[snapshot.id] = error;
36✔
49
};
50

51
/**
52
 * Rejects the read of a specific snapshot. A rejected snapshot read will not have that snapshot's
53
 * data sent down to the client.
54
 *
55
 * This method will set a special error code that causes the Share client to not pass the error to
56
 * user code, though it will still do things like cancel subscriptions.
57
 *
58
 * @param {Snapshot} snapshot
59
 * @param {string} errorMessage
60
 */
61
ReadSnapshotsRequest.prototype.rejectSnapshotReadSilent = function(snapshot, errorMessage) {
3✔
62
  this.rejectSnapshotRead(snapshot, this.silentRejectionError(errorMessage));
15✔
63
};
64

65
ReadSnapshotsRequest.prototype.silentRejectionError = function(errorMessage) {
3✔
66
  return new ShareDBError(ShareDBError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION, errorMessage);
15✔
67
};
68

69
/**
70
 * Returns whether this trigger of "readSnapshots" has had a snapshot read rejected.
71
 */
72
ReadSnapshotsRequest.prototype.hasSnapshotRejection = function() {
3✔
73
  return this._idToError != null;
1,422✔
74
};
75

76
/**
77
 * Returns an overall error from "readSnapshots" based on the snapshot-specific errors.
78
 *
79
 * - If there's exactly one snapshot and it has an error, then that error is returned.
80
 * - If there's more than one snapshot and at least one has an error, then an overall
81
 *   "ERR_SNAPSHOT_READS_REJECTED" is returned, with an `idToError` property.
82
 */
83
ReadSnapshotsRequest.prototype.getReadSnapshotsError = function() {
3✔
84
  var snapshots = this.snapshots;
36✔
85
  var idToError = this._idToError;
36✔
86
  // If there are 0 snapshots, there can't be any snapshot-specific errors.
87
  if (snapshots.length === 0) {
36!
88
    return;
×
89
  }
90

91
  // Single snapshot with error is treated as a full error.
92
  if (snapshots.length === 1) {
36✔
93
    var snapshotError = idToError[snapshots[0].id];
18✔
94
    if (snapshotError) {
18!
95
      return snapshotError;
18✔
96
    } else {
97
      return;
×
98
    }
99
  }
100

101
  // Errors in specific snapshots result in an overall ERR_SNAPSHOT_READS_REJECTED.
102
  //
103
  // fetchBulk and subscribeBulk know how to handle that special error by sending a doc-by-doc
104
  // success/failure to the client. Other methods that don't or can't handle partial failures
105
  // will treat it as a full rejection.
106
  var err = new ShareDBError(ShareDBError.CODES.ERR_SNAPSHOT_READS_REJECTED);
18✔
107
  err.idToError = idToError;
18✔
108
  return err;
18✔
109
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc