@atproto/bsync 0.0.44 → 0.1.1
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/CHANGELOG.md +18 -0
- package/dist/proto/bsync_connect.d.ts +1 -10
- package/dist/proto/bsync_connect.d.ts.map +1 -1
- package/dist/proto/bsync_connect.js +1 -10
- package/dist/proto/bsync_connect.js.map +1 -1
- package/dist/proto/bsync_pb.d.ts +0 -38
- package/dist/proto/bsync_pb.d.ts.map +1 -1
- package/dist/proto/bsync_pb.js +0 -57
- package/dist/proto/bsync_pb.js.map +1 -1
- package/dist/routes/index.d.ts.map +1 -1
- package/dist/routes/index.js +0 -2
- package/dist/routes/index.js.map +1 -1
- package/dist/routes/scan-mute-operations.js +108 -45
- package/dist/routes/scan-mute-operations.js.map +1 -1
- package/dist/routes/scan-notif-operations.js +105 -42
- package/dist/routes/scan-notif-operations.js.map +1 -1
- package/dist/routes/scan-operations.js +108 -45
- package/dist/routes/scan-operations.js.map +1 -1
- package/dist/routes/util.d.ts +2 -1
- package/dist/routes/util.d.ts.map +1 -1
- package/dist/routes/util.js +35 -12
- package/dist/routes/util.js.map +1 -1
- package/package.json +5 -6
- package/dist/routes/add-notif-operation.d.ts +0 -6
- package/dist/routes/add-notif-operation.d.ts.map +0 -1
- package/dist/routes/add-notif-operation.js +0 -61
- package/dist/routes/add-notif-operation.js.map +0 -1
package/dist/routes/util.js
CHANGED
|
@@ -9,20 +9,43 @@ export const validCursor = (cursor) => {
|
|
|
9
9
|
}
|
|
10
10
|
return int;
|
|
11
11
|
};
|
|
12
|
-
export
|
|
13
|
-
const controller = new
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return signal;
|
|
18
|
-
}
|
|
19
|
-
signal.addEventListener('abort', () => controller.abort(signal.reason), {
|
|
20
|
-
// @ts-ignore https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68625
|
|
21
|
-
signal: controller.signal,
|
|
12
|
+
export function combineSignals(...signals) {
|
|
13
|
+
const controller = new DisposableAbortController();
|
|
14
|
+
const onAbort = function (_event) {
|
|
15
|
+
const reason = new Error('This operation was aborted', {
|
|
16
|
+
cause: this.reason,
|
|
22
17
|
});
|
|
18
|
+
controller.abort(reason);
|
|
19
|
+
};
|
|
20
|
+
try {
|
|
21
|
+
for (const sig of signals) {
|
|
22
|
+
if (sig) {
|
|
23
|
+
sig.throwIfAborted();
|
|
24
|
+
sig.addEventListener('abort', onAbort, { signal: controller.signal });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return controller;
|
|
23
28
|
}
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
catch (err) {
|
|
30
|
+
controller.abort(err);
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function combinedSignals(...signals) {
|
|
35
|
+
const controller = combineSignals(...signals);
|
|
36
|
+
return Object.defineProperty(controller.signal, Symbol.dispose, {
|
|
37
|
+
value: controller[Symbol.dispose].bind(controller),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Allows using {@link AbortController} with the `using` keyword, in order to
|
|
42
|
+
* automatically abort them once the execution block ends.
|
|
43
|
+
*/
|
|
44
|
+
class DisposableAbortController extends AbortController {
|
|
45
|
+
[Symbol.dispose]() {
|
|
46
|
+
this.abort(new Error('AbortController was disposed'));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
26
49
|
export const isValidDid = (did) => {
|
|
27
50
|
try {
|
|
28
51
|
ensureValidDid(did);
|
package/dist/routes/util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/routes/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACxD,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,eAAe,GAChB,MAAM,iBAAiB,CAAA;AAExB,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;QACD,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAW,EAAE,EAAE;IAC1C,IAAI,CAAC;QACH,gBAAgB,CAAC,GAAG,CAAC,CAAA;QACrB,OAAO,IAAI,CAAA;IACb,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,SAAiB,EAAQ,EAAE;IAC3D,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAElC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAA;IACtE,CAAC;IAED,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,KAAK,CAAA;IAE9B,eAAe,CAAC,IAAI,CAAC,CAAA;IACrB,IAAI,QAAQ,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAA;IAClE,CAAC;AACH,CAAC,CAAA","sourcesContent":["import { Code, ConnectError } from '@connectrpc/connect'\nimport {\n InvalidDidError,\n ensureValidAtUri,\n ensureValidDid,\n ensureValidNsid,\n} from '@atproto/syntax'\n\nexport const validCursor = (cursor: string): number | null => {\n if (cursor === '') return null\n const int = parseInt(cursor, 10)\n if (isNaN(int) || int < 0) {\n throw new ConnectError('invalid cursor', Code.InvalidArgument)\n }\n return int\n}\n\nexport function combineSignals(\n ...signals: readonly (AbortSignal | undefined)[]\n): AbortController & Disposable {\n const controller = new DisposableAbortController()\n\n const onAbort = function (this: AbortSignal, _event: Event) {\n const reason = new Error('This operation was aborted', {\n cause: this.reason,\n })\n\n controller.abort(reason)\n }\n\n try {\n for (const sig of signals) {\n if (sig) {\n sig.throwIfAborted()\n sig.addEventListener('abort', onAbort, { signal: controller.signal })\n }\n }\n\n return controller\n } catch (err) {\n controller.abort(err)\n throw err\n }\n}\n\nexport function combinedSignals(\n ...signals: readonly (AbortSignal | undefined)[]\n): AbortSignal & Disposable {\n const controller = combineSignals(...signals)\n return Object.defineProperty(controller.signal, Symbol.dispose, {\n value: controller[Symbol.dispose].bind(controller),\n }) as AbortSignal & Disposable\n}\n\n/**\n * Allows using {@link AbortController} with the `using` keyword, in order to\n * automatically abort them once the execution block ends.\n */\nclass DisposableAbortController extends AbortController implements Disposable {\n [Symbol.dispose]() {\n this.abort(new Error('AbortController was disposed'))\n }\n}\n\nexport const isValidDid = (did: string) => {\n try {\n ensureValidDid(did)\n return true\n } catch (err) {\n if (err instanceof InvalidDidError) {\n return false\n }\n throw err\n }\n}\n\nexport const isValidAtUri = (uri: string) => {\n try {\n ensureValidAtUri(uri)\n return true\n } catch {\n return false\n }\n}\n\nexport const validateNamespace = (namespace: string): void => {\n const parts = namespace.split('#')\n\n if (parts.length !== 1 && parts.length !== 2) {\n throw new Error('namespace must be in the format \"nsid[#fragment]\"')\n }\n\n const [nsid, fragment] = parts\n\n ensureValidNsid(nsid)\n if (fragment && !/^[a-zA-Z][a-zA-Z0-9]*$/.test(fragment)) {\n throw new Error('namespace fragment must be a valid identifier')\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/bsync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Sychronizing service for app.bsky App View (Bluesky API)",
|
|
6
6
|
"keywords": [
|
|
@@ -45,17 +45,17 @@
|
|
|
45
45
|
"kysely": "^0.29.2",
|
|
46
46
|
"pg": "^8.10.0",
|
|
47
47
|
"pino-http": "^11.0.0",
|
|
48
|
-
"@atproto/
|
|
48
|
+
"@atproto-labs/opentelemetry-node": "^0.2.1",
|
|
49
49
|
"@atproto/syntax": "^0.7.6",
|
|
50
|
-
"@atproto
|
|
50
|
+
"@atproto/common": "^0.8.4"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@bufbuild/buf": "^1.
|
|
53
|
+
"@bufbuild/buf": "^1.73.0",
|
|
54
54
|
"@bufbuild/protoc-gen-es": "^1.5.0",
|
|
55
55
|
"@connectrpc/protoc-gen-connect-es": "^1.7.0",
|
|
56
56
|
"@types/pg": "^8.15.5",
|
|
57
57
|
"get-port": "^5.1.1",
|
|
58
|
-
"jest": "^30.5.
|
|
58
|
+
"jest": "^30.5.2"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"codegen:buf": "buf generate proto",
|
|
@@ -63,7 +63,6 @@
|
|
|
63
63
|
"build": "tsc --build tsconfig.build.json",
|
|
64
64
|
"start": "node --enable-source-maps dist/bin.js",
|
|
65
65
|
"test": "NODE_OPTIONS=--experimental-vm-modules ../dev-infra/with-test-db.sh jest",
|
|
66
|
-
"test:log": "tail -50 test.log | pino-pretty",
|
|
67
66
|
"test:updateSnapshot": "jest --updateSnapshot",
|
|
68
67
|
"migration:create": "node ./bin/migration-create.ts",
|
|
69
68
|
"buf:gen": ">&2 echo 'DEPRECATED: run `pnpm run codegen:buf` instead ' && pnpm run codegen:buf"
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { type ServiceImpl } from '@connectrpc/connect';
|
|
2
|
-
import type { AppContext } from '../context.js';
|
|
3
|
-
import type { Service } from '../proto/bsync_connect.js';
|
|
4
|
-
export default _default;
|
|
5
|
-
declare function _default(ctx: AppContext): Partial<ServiceImpl<typeof Service>>;
|
|
6
|
-
//# sourceMappingURL=add-notif-operation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"add-notif-operation.d.ts","sourceRoot":"","sources":["../../src/routes/add-notif-operation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAE1E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;;0BAKxC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,OAAO,CAAC,CAAC"}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { Code, ConnectError } from '@connectrpc/connect';
|
|
2
|
-
import { sql } from 'kysely';
|
|
3
|
-
import { createNotifOpChannel } from '../db/schema/notif_op.js';
|
|
4
|
-
import { AddNotifOperationResponse } from '../proto/bsync_pb.js';
|
|
5
|
-
import { authWithApiKey } from './auth.js';
|
|
6
|
-
import { isValidDid } from './util.js';
|
|
7
|
-
export default (ctx) => ({
|
|
8
|
-
async addNotifOperation(req, handlerCtx) {
|
|
9
|
-
authWithApiKey(ctx, handlerCtx);
|
|
10
|
-
const { db } = ctx;
|
|
11
|
-
const { actorDid, priority } = req;
|
|
12
|
-
if (!isValidDid(actorDid)) {
|
|
13
|
-
throw new ConnectError('actor_did must be a valid did', Code.InvalidArgument);
|
|
14
|
-
}
|
|
15
|
-
const id = await db.transaction(async (txn) => {
|
|
16
|
-
// create notif op
|
|
17
|
-
const id = await createNotifOp(txn, actorDid, priority);
|
|
18
|
-
// update notif state
|
|
19
|
-
if (priority !== undefined) {
|
|
20
|
-
await updateNotifItem(txn, id, actorDid, priority);
|
|
21
|
-
}
|
|
22
|
-
return id;
|
|
23
|
-
});
|
|
24
|
-
return new AddNotifOperationResponse({
|
|
25
|
-
operation: {
|
|
26
|
-
id: String(id),
|
|
27
|
-
actorDid,
|
|
28
|
-
priority,
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
const createNotifOp = async (db, actorDid, priority) => {
|
|
34
|
-
const { ref } = db.db.dynamic;
|
|
35
|
-
const { id } = await db.db
|
|
36
|
-
.insertInto('notif_op')
|
|
37
|
-
.values({
|
|
38
|
-
actorDid,
|
|
39
|
-
priority,
|
|
40
|
-
})
|
|
41
|
-
.returning('id')
|
|
42
|
-
.executeTakeFirstOrThrow();
|
|
43
|
-
await sql `notify ${ref(createNotifOpChannel)}`.execute(db.db); // emitted transactionally
|
|
44
|
-
return id;
|
|
45
|
-
};
|
|
46
|
-
const updateNotifItem = async (db, fromId, actorDid, priority) => {
|
|
47
|
-
const { ref } = db.db.dynamic;
|
|
48
|
-
await db.db
|
|
49
|
-
.insertInto('notif_item')
|
|
50
|
-
.values({
|
|
51
|
-
actorDid,
|
|
52
|
-
priority,
|
|
53
|
-
fromId,
|
|
54
|
-
})
|
|
55
|
-
.onConflict((oc) => oc.column('actorDid').doUpdateSet({
|
|
56
|
-
priority: sql `${ref('excluded.priority')}`,
|
|
57
|
-
fromId: sql `${ref('excluded.fromId')}`,
|
|
58
|
-
}))
|
|
59
|
-
.execute();
|
|
60
|
-
};
|
|
61
|
-
//# sourceMappingURL=add-notif-operation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"add-notif-operation.js","sourceRoot":"","sources":["../../src/routes/add-notif-operation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,YAAY,EAAoB,MAAM,qBAAqB,CAAA;AAC1E,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAA;AAG5B,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAA;AAE/D,OAAO,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC,eAAe,CAAC,GAAe,EAAwC,EAAE,CAAC,CAAC;IACzE,KAAK,CAAC,iBAAiB,CAAC,GAAG,EAAE,UAAU;QACrC,cAAc,CAAC,GAAG,EAAE,UAAU,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E,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA,CAAC,0BAA0B;IACxF,OAAO,EAAE,CAAA;AACX,CAAC,CAAA;AAED,MAAM,eAAe,GAAG,KAAK,EAC3B,EAAY,EACZ,MAAc,EACd,QAAgB,EAChB,QAAiB,EACjB,EAAE;IACF,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,OAAO,CAAA;IAC7B,MAAM,EAAE,CAAC,EAAE;SACR,UAAU,CAAC,YAAY,CAAC;SACxB,MAAM,CAAC;QACN,QAAQ;QACR,QAAQ;QACR,MAAM;KACP,CAAC;SACD,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,CACjB,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,WAAW,CAAC;QAChC,QAAQ,EAAE,GAAG,CAAA,GAAG,GAAG,CAAC,mBAAmB,CAAC,EAAE;QAC1C,MAAM,EAAE,GAAG,CAAA,GAAG,GAAG,CAAC,iBAAiB,CAAC,EAAE;KACvC,CAAC,CACH;SACA,OAAO,EAAE,CAAA;AACd,CAAC,CAAA","sourcesContent":["import { Code, ConnectError, type ServiceImpl } from '@connectrpc/connect'\nimport { sql } from 'kysely'\nimport type { AppContext } from '../context.js'\nimport type { Database } from '../db/index.js'\nimport { createNotifOpChannel } from '../db/schema/notif_op.js'\nimport type { Service } from '../proto/bsync_connect.js'\nimport { AddNotifOperationResponse } from '../proto/bsync_pb.js'\nimport { authWithApiKey } from './auth.js'\nimport { isValidDid } from './util.js'\n\nexport default (ctx: AppContext): Partial<ServiceImpl<typeof Service>> => ({\n async addNotifOperation(req, handlerCtx) {\n authWithApiKey(ctx, handlerCtx)\n const { db } = ctx\n const { actorDid, priority } = req\n if (!isValidDid(actorDid)) {\n throw new ConnectError(\n 'actor_did must be a valid did',\n Code.InvalidArgument,\n )\n }\n const id = await db.transaction(async (txn) => {\n // create notif op\n const id = await createNotifOp(txn, actorDid, priority)\n // update notif state\n if (priority !== undefined) {\n await updateNotifItem(txn, id, actorDid, priority)\n }\n return id\n })\n return new AddNotifOperationResponse({\n operation: {\n id: String(id),\n actorDid,\n priority,\n },\n })\n },\n})\n\nconst createNotifOp = async (\n db: Database,\n actorDid: string,\n priority: boolean | undefined,\n) => {\n const { ref } = db.db.dynamic\n const { id } = await db.db\n .insertInto('notif_op')\n .values({\n actorDid,\n priority,\n })\n .returning('id')\n .executeTakeFirstOrThrow()\n await sql`notify ${ref(createNotifOpChannel)}`.execute(db.db) // emitted transactionally\n return id\n}\n\nconst updateNotifItem = async (\n db: Database,\n fromId: number,\n actorDid: string,\n priority: boolean,\n) => {\n const { ref } = db.db.dynamic\n await db.db\n .insertInto('notif_item')\n .values({\n actorDid,\n priority,\n fromId,\n })\n .onConflict((oc) =>\n oc.column('actorDid').doUpdateSet({\n priority: sql`${ref('excluded.priority')}`,\n fromId: sql`${ref('excluded.fromId')}`,\n }),\n )\n .execute()\n}\n"]}
|