MCPcopy
hub / github.com/jvilk/BrowserFS / addNewNode

Method addNewNode

src/generic/key_value_filesystem.ts:1125–1145  ·  view source on GitHub ↗

* Adds a new node under a random ID. Retries 5 times before giving up in * the exceedingly unlikely chance that we try to reuse a random GUID. * @param cb Passed an error or the GUID that the data was stored under.

(tx: AsyncKeyValueRWTransaction, data: Buffer, cb: BFSCallback<string>)

Source from the content-addressed store, hash-verified

1123 * @param cb Passed an error or the GUID that the data was stored under.
1124 */
1125 private addNewNode(tx: AsyncKeyValueRWTransaction, data: Buffer, cb: BFSCallback<string>): void {
1126 let retries = 0, currId: string;
1127 const reroll = () => {
1128 if (++retries === 5) {
1129 // Max retries hit. Return with an error.
1130 cb(new ApiError(ErrorCode.EIO, 'Unable to commit data to key-value store.'));
1131 } else {
1132 // Try again.
1133 currId = GenerateRandomID();
1134 tx.put(currId, data, false, (e: ApiError, committed?: boolean) => {
1135 if (e || !committed) {
1136 reroll();
1137 } else {
1138 // Successfully stored under 'currId'.
1139 cb(null, currId);
1140 }
1141 });
1142 }
1143 };
1144 reroll();
1145 }
1146
1147 /**
1148 * Commits a new file (well, a FILE or a DIRECTORY) to the file system with

Callers 1

commitNewFileMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected