@pkcprotocol/pkc-js 0.0.153 → 0.0.154
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/README.md +21 -1
- package/dist/browser/errors.d.ts +4 -2
- package/dist/browser/errors.js +4 -2
- package/dist/browser/errors.js.map +1 -1
- package/dist/browser/generated-version.d.ts +1 -1
- package/dist/browser/generated-version.js +1 -1
- package/dist/browser/pkc-error.d.ts +1 -1
- package/dist/browser/publications/comment/comment.d.ts +1 -0
- package/dist/browser/publications/comment/comment.js +57 -12
- package/dist/browser/publications/comment/comment.js.map +1 -1
- package/dist/browser/publications/publication.d.ts +8 -1
- package/dist/browser/publications/publication.js +393 -146
- package/dist/browser/publications/publication.js.map +1 -1
- package/dist/browser/publications/types.d.ts +1 -0
- package/dist/browser/rpc/src/index.js +12 -5
- package/dist/browser/rpc/src/index.js.map +1 -1
- package/dist/browser/runtime/node/blockstore.d.ts +2 -2
- package/dist/browser/runtime/node/blockstore.js +2 -2
- package/dist/browser/runtime/node/community/db-handler.d.ts +5 -7
- package/dist/browser/runtime/node/community/db-handler.js +79 -82
- package/dist/browser/runtime/node/community/db-handler.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/cleanup.js +62 -12
- package/dist/browser/runtime/node/community/local-community/cleanup.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/db-state.d.ts +1 -1
- package/dist/browser/runtime/node/community/local-community/db-state.js +31 -39
- package/dist/browser/runtime/node/community/local-community/db-state.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/editing.js +71 -22
- package/dist/browser/runtime/node/community/local-community/editing.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/ipns-publishing.js +1 -1
- package/dist/browser/runtime/node/community/local-community/ipns-publishing.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/lifecycle.d.ts +0 -2
- package/dist/browser/runtime/node/community/local-community/lifecycle.js +50 -72
- package/dist/browser/runtime/node/community/local-community/lifecycle.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community.d.ts +4 -2
- package/dist/browser/runtime/node/community/local-community.js +11 -7
- package/dist/browser/runtime/node/community/local-community.js.map +1 -1
- package/dist/browser/runtime/node/community/start-lock.d.ts +14 -5
- package/dist/browser/runtime/node/community/start-lock.js +153 -10
- package/dist/browser/runtime/node/community/start-lock.js.map +1 -1
- package/dist/browser/runtime/node/util.js +18 -10
- package/dist/browser/runtime/node/util.js.map +1 -1
- package/dist/bundled/bundle-manifest.json +35 -44
- package/dist/bundled/challenges.js +1 -1
- package/dist/bundled/chunks/{helia-for-pkc-COJ3xMF9.js → helia-for-pkc-CG9YohPs.js} +4 -4
- package/dist/bundled/chunks/{local-community-BTx2HxIm.js → local-community-CaQmhX8D.js} +1 -1
- package/dist/bundled/chunks/{local-community-CflGu_ET.js → local-community-V8_VyEhO.js} +217 -162
- package/dist/bundled/chunks/{node-B0b6lAIX.js → node-c4vpD2VN.js} +340 -172
- package/dist/bundled/chunks/{page-sort-scoring-DAMnUmDY.js → page-sort-scoring-d1frahfd.js} +1 -1
- package/dist/bundled/chunks/{rpc-local-community-Cy03QajY.js → rpc-local-community-DDAibWpE.js} +144 -1431
- package/dist/bundled/chunks/{util-C4y8KHmp.js → util-B_dKP7Te.js} +1 -1
- package/dist/bundled/chunks/{util-D7AtfLYH.js → util-BwEIge-b.js} +5 -3
- package/dist/bundled/chunks/{validate-package-factories-EH9ydbPR.js → validate-package-factories-DfpNf4oq.js} +2 -2
- package/dist/bundled/index.js +1 -1
- package/dist/bundled/page-sorts.js +1 -1
- package/dist/bundled/rpc/src/index.js +14 -9
- package/dist/node/errors.d.ts +4 -2
- package/dist/node/errors.js +4 -2
- package/dist/node/errors.js.map +1 -1
- package/dist/node/generated-version.d.ts +1 -1
- package/dist/node/generated-version.js +1 -1
- package/dist/node/pkc-error.d.ts +1 -1
- package/dist/node/publications/comment/comment.d.ts +1 -0
- package/dist/node/publications/comment/comment.js +57 -12
- package/dist/node/publications/comment/comment.js.map +1 -1
- package/dist/node/publications/publication.d.ts +8 -1
- package/dist/node/publications/publication.js +393 -146
- package/dist/node/publications/publication.js.map +1 -1
- package/dist/node/publications/types.d.ts +1 -0
- package/dist/node/rpc/src/index.js +12 -5
- package/dist/node/rpc/src/index.js.map +1 -1
- package/dist/node/runtime/node/blockstore.d.ts +2 -2
- package/dist/node/runtime/node/blockstore.js +2 -2
- package/dist/node/runtime/node/community/db-handler.d.ts +5 -7
- package/dist/node/runtime/node/community/db-handler.js +79 -82
- package/dist/node/runtime/node/community/db-handler.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/cleanup.js +62 -12
- package/dist/node/runtime/node/community/local-community/cleanup.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/db-state.d.ts +1 -1
- package/dist/node/runtime/node/community/local-community/db-state.js +31 -39
- package/dist/node/runtime/node/community/local-community/db-state.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/editing.js +71 -22
- package/dist/node/runtime/node/community/local-community/editing.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/ipns-publishing.js +1 -1
- package/dist/node/runtime/node/community/local-community/ipns-publishing.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/lifecycle.d.ts +0 -2
- package/dist/node/runtime/node/community/local-community/lifecycle.js +50 -72
- package/dist/node/runtime/node/community/local-community/lifecycle.js.map +1 -1
- package/dist/node/runtime/node/community/local-community.d.ts +4 -2
- package/dist/node/runtime/node/community/local-community.js +11 -7
- package/dist/node/runtime/node/community/local-community.js.map +1 -1
- package/dist/node/runtime/node/community/start-lock.d.ts +14 -5
- package/dist/node/runtime/node/community/start-lock.js +153 -10
- package/dist/node/runtime/node/community/start-lock.js.map +1 -1
- package/dist/node/runtime/node/util.js +18 -10
- package/dist/node/runtime/node/util.js.map +1 -1
- package/package.json +1 -2
|
@@ -9,8 +9,7 @@ import { deleteOldCommunityInWindows, deriveCommentIpfsFromCommentTableRow, forg
|
|
|
9
9
|
import env from "../../../version.js";
|
|
10
10
|
import Database from "better-sqlite3";
|
|
11
11
|
import { sha256 } from "js-sha256";
|
|
12
|
-
import
|
|
13
|
-
import { isCommunityStartLocked } from "./start-lock.js";
|
|
12
|
+
import { getStartLockPath, isCommunityStartLocked, releaseStartLock, takeStartLock } from "./start-lock.js";
|
|
14
13
|
import { isDefaultChallengeStructure } from "./local-community/defaults.js";
|
|
15
14
|
import { addAllCidsUnderPurgedCommentToBeRemoved } from "./local-community/cleanup.js";
|
|
16
15
|
import { updateDbInternalState } from "./local-community/db-state.js";
|
|
@@ -57,8 +56,8 @@ export class DbHandler {
|
|
|
57
56
|
// Advanced by every write to the publication tables (comments, votes, edits, moderations, aliases, purges). The
|
|
58
57
|
// update cycle's author memo (CommunityAuthorMemo) is only reused while it stays the same.
|
|
59
58
|
this._publicationWriteGeneration = 0;
|
|
60
|
-
//
|
|
61
|
-
this.
|
|
59
|
+
// The address whose start lock this handler's connection holds, from lockCommunityStart until unlockCommunityStart
|
|
60
|
+
this._startLockAddress = undefined;
|
|
62
61
|
this._community = community;
|
|
63
62
|
this._transactionDepth = 0;
|
|
64
63
|
this._createdTables = false;
|
|
@@ -143,7 +142,10 @@ export class DbHandler {
|
|
|
143
142
|
catch (e) {
|
|
144
143
|
await this.initDbIfNeeded();
|
|
145
144
|
log.error(`Community (${this._community.address}) failed to create/migrate tables. Current db version (${this.getDbVersion()}), latest db version (${env.DB_VERSION}). Error`, e);
|
|
146
|
-
|
|
145
|
+
// A start() that fails here holds the start lock on this connection, and its stop() releases the lock and
|
|
146
|
+
// closes the connection
|
|
147
|
+
if (this._startLockAddress === undefined)
|
|
148
|
+
this.destoryConnection();
|
|
147
149
|
throw e;
|
|
148
150
|
}
|
|
149
151
|
hideClassPrivateProps(this);
|
|
@@ -172,6 +174,18 @@ export class DbHandler {
|
|
|
172
174
|
}
|
|
173
175
|
destoryConnection() {
|
|
174
176
|
const log = Logger("pkc-js:local-community:dbHandler:destroyConnection");
|
|
177
|
+
// The start lock lives on this connection (see start-lock.ts), so closing it releases the lock: another process
|
|
178
|
+
// could then start the community next to this one. stop() releases the lock before it gets here, so anything
|
|
179
|
+
// else that does is a caller that took a starting or started community for a stopped one. The connection stays
|
|
180
|
+
// open and the lock held. Under vitest it throws, so a test that reaches such a path fails.
|
|
181
|
+
if (this.holdsStartLock()) {
|
|
182
|
+
const error = new Error(`Refused to close the DB connection of community (${this._startLockAddress}) while it holds the community's start lock (issue #561)`);
|
|
183
|
+
log.error(error);
|
|
184
|
+
if (process.env["VITEST"])
|
|
185
|
+
throw error;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
this._startLockAddress = undefined;
|
|
175
189
|
this._preparedStatements.clear(); // statements belong to the connection
|
|
176
190
|
if (this._db && this._db.open) {
|
|
177
191
|
try {
|
|
@@ -482,16 +496,13 @@ export class DbHandler {
|
|
|
482
496
|
const dbExistsAlready = fs.existsSync(dbPath);
|
|
483
497
|
if (needToMigrate) {
|
|
484
498
|
if (dbExistsAlready && currentDbVersion > 0) {
|
|
485
|
-
this.destoryConnection();
|
|
486
499
|
backupDbPath = path.join(path.dirname(dbPath), ".backup_before_migration", `${path.basename(dbPath)}.${currentDbVersion}.${timestamp()}`);
|
|
487
500
|
log(`Copying db ${path.basename(dbPath)} to ${backupDbPath} before migration`);
|
|
488
501
|
if (!fs.existsSync(path.dirname(backupDbPath)))
|
|
489
502
|
await fs.promises.mkdir(path.dirname(backupDbPath), { recursive: true });
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
this._db = new Database(dbPath);
|
|
494
|
-
this._db.pragma("journal_mode = WAL");
|
|
503
|
+
// Through the open connection, which is not closed: start() migrates while holding the start lock, and
|
|
504
|
+
// the lock lives on this connection (see start-lock.ts)
|
|
505
|
+
await this._db.backup(backupDbPath);
|
|
495
506
|
}
|
|
496
507
|
this._db.exec("PRAGMA foreign_keys = OFF");
|
|
497
508
|
const tablesToDrop = ["challengeRequests", "challenges", "challengeAnswers", "challengeVerifications", "signers"];
|
|
@@ -3008,6 +3019,10 @@ export class DbHandler {
|
|
|
3008
3019
|
await deleteOldCommunityInWindows(oldPathString, this._community._pkc);
|
|
3009
3020
|
else
|
|
3010
3021
|
await fs.promises.rm(oldPathString, { force: true });
|
|
3022
|
+
// No community lives at the old address any more, so its start lock sidecar goes too. Not while the old
|
|
3023
|
+
// file is still there, which it is on Windows when it could not be removed yet (see start-lock.ts)
|
|
3024
|
+
if (!fs.existsSync(oldPathString))
|
|
3025
|
+
await fs.promises.rm(getStartLockPath({ dataPath, communityAddress: oldDbName }), { force: true });
|
|
3011
3026
|
}
|
|
3012
3027
|
}
|
|
3013
3028
|
catch (error) {
|
|
@@ -3017,93 +3032,75 @@ export class DbHandler {
|
|
|
3017
3032
|
this._dbConfig = { ...this._dbConfig, filename: newPathString };
|
|
3018
3033
|
log(`Changed db path from (${oldPathString}) to (${newPathString})`);
|
|
3019
3034
|
}
|
|
3020
|
-
//
|
|
3021
|
-
//
|
|
3022
|
-
//
|
|
3023
|
-
|
|
3024
|
-
// no longer holds the lock from that point on, so it must stop running the community.
|
|
3025
|
-
async lockCommunityStart({ onCompromised }) {
|
|
3035
|
+
// Takes the community's start lock on this handler's connection (see start-lock.ts), which holds it until
|
|
3036
|
+
// unlockCommunityStart(). Closing the connection would release it too, which destoryConnection() refuses to do
|
|
3037
|
+
// silently. A process that dies releases it without any of this running.
|
|
3038
|
+
async lockCommunityStart() {
|
|
3026
3039
|
const log = Logger("pkc-js:local-community:db-handler:lock:start");
|
|
3027
3040
|
const communityAddress = this._community.address;
|
|
3028
|
-
const
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
});
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
}
|
|
3054
|
-
}
|
|
3055
|
-
async unlockCommunityStart(communityAddress = this._community.address) {
|
|
3041
|
+
const dataPath = this._community._pkc.dataPath;
|
|
3042
|
+
await this.initDbIfNeeded();
|
|
3043
|
+
// The lock directory of the mtime lock this one replaced, left by a process that was killed before an upgrade.
|
|
3044
|
+
// Removed before the lock is taken: nothing may fail or wait between taking the lock and returning, since a
|
|
3045
|
+
// start() that fails here has not entered the started state, whose teardown is what releases the lock
|
|
3046
|
+
await fs.promises.rm(path.join(dataPath, "communities", `${communityAddress}.start.lock`), { recursive: true, force: true });
|
|
3047
|
+
if (!takeStartLock({ db: this._db, dataPath, communityAddress }))
|
|
3048
|
+
throw new PKCError("ERR_COMMUNITY_ALREADY_STARTED", { communityAddress });
|
|
3049
|
+
// The connection was opened some awaits ago. Another process that deleted or renamed the community since has
|
|
3050
|
+
// moved its DB away and removed its sidecar, and the lock just taken is on a new sidecar, for a community with
|
|
3051
|
+
// no DB at its path: it would run, publish and write to a file nothing reads again. Given back at once (no
|
|
3052
|
+
// await since the lock was taken), and the sidecar with it, as nothing is left for it to guard.
|
|
3053
|
+
if (!fs.existsSync(path.join(dataPath, "communities", communityAddress))) {
|
|
3054
|
+
releaseStartLock({ db: this._db, dataPath, communityAddress });
|
|
3055
|
+
fs.rmSync(getStartLockPath({ dataPath, communityAddress }), { force: true });
|
|
3056
|
+
throw new PKCError("CAN_NOT_LOAD_LOCAL_COMMUNITY_IF_DB_DOES_NOT_EXIST", { communityAddress, dataPath });
|
|
3057
|
+
}
|
|
3058
|
+
this._startLockAddress = communityAddress;
|
|
3059
|
+
log(`Locked the start of community (${communityAddress}) successfully`);
|
|
3060
|
+
}
|
|
3061
|
+
// Whether this handler's connection holds the community's start lock, i.e. this instance is starting or started
|
|
3062
|
+
holdsStartLock() {
|
|
3063
|
+
return this._startLockAddress !== undefined && this._db?.open === true;
|
|
3064
|
+
}
|
|
3065
|
+
async unlockCommunityStart() {
|
|
3056
3066
|
const log = Logger("pkc-js:local-community:db-handler:unlock:start");
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
const lockfilePath = path.join(this._community._pkc.dataPath, "communities", `${communityAddress}.start.lock`);
|
|
3060
|
-
const communityDbPath = path.join(this._community._pkc.dataPath, "communities", communityAddress);
|
|
3061
|
-
if (!fs.existsSync(lockfilePath) || !fs.existsSync(communityDbPath))
|
|
3067
|
+
const communityAddress = this._startLockAddress;
|
|
3068
|
+
if (communityAddress === undefined)
|
|
3062
3069
|
return;
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
}
|
|
3067
|
-
|
|
3068
|
-
log(`Error while trying to unlock start of community (${communityAddress}): ${e}`);
|
|
3069
|
-
throw e;
|
|
3070
|
-
}
|
|
3070
|
+
this._startLockAddress = undefined;
|
|
3071
|
+
if (!this._db?.open)
|
|
3072
|
+
return; // the lock went with the connection
|
|
3073
|
+
releaseStartLock({ db: this._db, dataPath: this._community._pkc.dataPath, communityAddress });
|
|
3074
|
+
log(`Unlocked start of community (${communityAddress})`);
|
|
3071
3075
|
}
|
|
3072
3076
|
async isCommunityStartLocked(communityAddress = this._community.address) {
|
|
3073
3077
|
return isCommunityStartLocked({ dataPath: this._community._pkc.dataPath, communityAddress });
|
|
3074
3078
|
}
|
|
3075
|
-
//
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3079
|
+
// Runs `write` as one write transaction on the community DB: SQLite lets one connection write at a time, across
|
|
3080
|
+
// processes too, and the lock is the kernel's, so a killed writer leaves nothing behind. `write` must be synchronous
|
|
3081
|
+
// (issue #417). A transaction that is already open on this connection is joined. Used for a read-modify-write of
|
|
3082
|
+
// the community's internal state, which a lock file guarded before (issue #561).
|
|
3083
|
+
runInWriteTransaction(write) {
|
|
3084
|
+
if (this._db.inTransaction)
|
|
3085
|
+
return write();
|
|
3080
3086
|
try {
|
|
3081
|
-
|
|
3082
|
-
lockfilePath,
|
|
3083
|
-
retries: 5,
|
|
3084
|
-
onCompromised: () => { }
|
|
3085
|
-
});
|
|
3087
|
+
this._db.exec("BEGIN IMMEDIATE");
|
|
3086
3088
|
}
|
|
3087
3089
|
catch (e) {
|
|
3088
|
-
|
|
3089
|
-
if (
|
|
3090
|
+
// another connection kept writing for longer than this connection's busy timeout
|
|
3091
|
+
if (typeof e.code === "string" && e.code.startsWith("SQLITE_BUSY"))
|
|
3090
3092
|
throw new PKCError("ERR_COMMUNITY_STATE_LOCKED", { communityAddress: this._community.address, error: e });
|
|
3091
|
-
|
|
3093
|
+
throw e;
|
|
3092
3094
|
}
|
|
3093
|
-
}
|
|
3094
|
-
async unlockCommunityState() {
|
|
3095
|
-
const log = Logger("pkc-js:local-community:db-handler:lock:unlockCommunityState");
|
|
3096
|
-
const lockfilePath = path.join(this._community._pkc.dataPath, "communities", `${this._community.address}.state.lock`);
|
|
3097
|
-
const communityDbPath = path.join(this._community._pkc.dataPath, "communities", this._community.address);
|
|
3098
|
-
if (!fs.existsSync(lockfilePath))
|
|
3099
|
-
return;
|
|
3100
3095
|
try {
|
|
3101
|
-
|
|
3096
|
+
const result = write();
|
|
3097
|
+
this._db.exec("COMMIT");
|
|
3098
|
+
return result;
|
|
3102
3099
|
}
|
|
3103
3100
|
catch (e) {
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3101
|
+
if (this._db.inTransaction)
|
|
3102
|
+
this._db.exec("ROLLBACK");
|
|
3103
|
+
throw e;
|
|
3107
3104
|
}
|
|
3108
3105
|
}
|
|
3109
3106
|
communityDbExists() {
|