Skip to content

Commit

Permalink
Mark NeDB as optional (#488)
Browse files Browse the repository at this point in the history
* Update trivial dependencies

* Drop uuid dep

* Node 20 updates

* Update eslint packages

* Update document on versioning

* Use node 20 everywhere

* changelog

* Fixup test

* Fixup test fails

* Cleanup linting

* Mark nedb as a peer dep.

* changelog

* Add nedb during test runs

* Add nedb  during postgres test runs
  • Loading branch information
Half-Shot committed Jan 5, 2024
1 parent 7ea2f5b commit cfca028
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
with:
node-version: "${{ matrix.node-version }}"
- run: yarn --frozen-lockfile
- run: yarn add nedb --peer
- run: yarn build && yarn test
test-postgres:
runs-on: ubuntu-22.04
Expand All @@ -50,6 +51,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: yarn --frozen-lockfile
- run: yarn add nedb --peer
- run: yarn test
env:
BRIDGE_TEST_PGDB: "bridge_integtest"
Expand Down
1 change: 1 addition & 0 deletions changelog.d/488.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NeDB-based stores are now deprecated. You may still use them by adding "nedb" as a dependency to your project, but no new features or bugfixes are planned.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"js-yaml": "^4.0.0",
"matrix-appservice": "^2.0.0",
"matrix-bot-sdk": "npm:@vector-im/matrix-bot-sdk@^0.6.6-element.1",
"nedb": "^1.8.0",
"nopt": "^5.0.0",
"p-queue": "^6.6.2",
"pkginfo": "^0.4.1",
Expand Down Expand Up @@ -70,5 +69,8 @@
"typedoc": "^0.23.28",
"typescript": "^5.3.3",
"winston-transport": "^4.6.0"
},
"peerDependencies": {
"nedb": "^1.8.0"
}
}
75 changes: 19 additions & 56 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Datastore from "nedb";
import type Datastore from "nedb";
const nedb = import("nedb");
import {promises as fs} from "fs";
import * as util from "util";
import yaml from "js-yaml";
Expand Down Expand Up @@ -1720,61 +1721,23 @@ export class Bridge {

}

function loadDatabase<T extends BridgeStore>(path: string, Cls: new (db: Datastore) => T) {
const defer = deferPromise<T>();
const db = new Datastore({
filename: path,
autoload: true,
onload: function(err) {
if (err) {
defer.reject(err);
}
else {
defer.resolve(new Cls(db));
async function loadDatabase<T extends BridgeStore>(path: string, Cls: new (db: Datastore) => T) {
try {
const datastoreFn = (await nedb).default;
return new Promise<T>((resolve, reject) => {
const dbInstance = new datastoreFn({
filename: path,
autoload: true,
onload: function(err) {
if (err) {
reject(err);
}
else {
resolve(new Cls(dbInstance));
}
}
}
});
return defer.promise;
}

function retryAlgorithm(
event: unknown,
attempts: number,
err: {
httpStatus: number,
cors?: string,
name: string,
// eslint-disable-next-line camelcase
data?: { retry_after_ms: number },
}
) {
if (err.httpStatus === 400 || err.httpStatus === 403 || err.httpStatus === 401) {
// client error; no amount of retrying will save you now.
return -1;
}
// we ship with browser-request which returns { cors: rejected } when trying
// with no connection, so if we match that, give up since they have no conn.
if (err.cors === "rejected") {
return -1;
}

if (err.name === "M_LIMIT_EXCEEDED") {
const waitTime = err.data?.retry_after_ms;
if (waitTime) {
return waitTime;
}
}
if (attempts > 4) {
return -1; // give up
}
return 1000 + (1000 * attempts);
}

function queueAlgorithm(event: {getType: () => string, getRoomId(): string}) {
if (event.getType() === "m.room.message") {
// use a separate queue for each room ID
return "message_" + event.getRoomId();
})});
} catch (ex) {
throw Error('nedb could not be imported. You will need to add this package as a peer dependency.');
}
// allow all other events continue concurrently.
return null;
}
2 changes: 1 addition & 1 deletion src/components/bridge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
*/

import { promisify } from "util";
import Datastore from "nedb";
import type Datastore from "nedb";

type Query = Record<string, unknown>;

Expand Down
2 changes: 1 addition & 1 deletion src/components/event-bridge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import Datastore from "nedb";
import type Datastore from "nedb";
import { BridgeStore } from "./bridge-store";
import { StoredEvent, StoredEventDoc } from "../models/events/event";

Expand Down
2 changes: 1 addition & 1 deletion src/components/room-bridge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ limitations under the License.
*
*/

import Datastore from "nedb";
import type Datastore from "nedb";
import { BridgeStore } from "./bridge-store";
import { MatrixRoom, MatrixRoomData } from "../models/rooms/matrix";
import { RemoteRoom } from "../models/rooms/remote";
Expand Down
2 changes: 1 addition & 1 deletion src/components/user-activity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ limitations under the License.
* }
* }
*/
import Datastore from "nedb";
import type Datastore from "nedb";
import { BridgeStore } from "./bridge-store";
import { UserActivity, UserActivitySet } from "./user-activity";

Expand Down
2 changes: 1 addition & 1 deletion src/components/user-bridge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ limitations under the License.
* matrix_id: "@foo:bar"
* }
*/
import Datastore from "nedb";
import type Datastore from "nedb";
import { BridgeStore } from "./bridge-store";
import { MatrixUser } from "../models/users/matrix";
import { RemoteUser } from "../models/users/remote";
Expand Down

0 comments on commit cfca028

Please sign in to comment.