@atproto/bsync 0.0.42 → 0.1.0

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/config.d.ts +9 -0
  3. package/dist/config.d.ts.map +1 -1
  4. package/dist/config.js +13 -0
  5. package/dist/config.js.map +1 -1
  6. package/dist/context.d.ts +3 -0
  7. package/dist/context.d.ts.map +1 -1
  8. package/dist/context.js +14 -1
  9. package/dist/context.js.map +1 -1
  10. package/dist/dataplane.d.ts +9 -0
  11. package/dist/dataplane.d.ts.map +1 -0
  12. package/dist/dataplane.js +30 -0
  13. package/dist/dataplane.js.map +1 -0
  14. package/dist/proto/bsync_connect.d.ts +10 -10
  15. package/dist/proto/bsync_connect.d.ts.map +1 -1
  16. package/dist/proto/bsync_connect.js +11 -11
  17. package/dist/proto/bsync_connect.js.map +1 -1
  18. package/dist/proto/bsync_pb.d.ts +35 -39
  19. package/dist/proto/bsync_pb.d.ts.map +1 -1
  20. package/dist/proto/bsync_pb.js +56 -58
  21. package/dist/proto/bsync_pb.js.map +1 -1
  22. package/dist/proto/dataplane_connect.d.ts +20 -0
  23. package/dist/proto/dataplane_connect.d.ts.map +1 -0
  24. package/dist/proto/dataplane_connect.js +24 -0
  25. package/dist/proto/dataplane_connect.js.map +1 -0
  26. package/dist/proto/dataplane_pb.d.ts +37 -0
  27. package/dist/proto/dataplane_pb.d.ts.map +1 -0
  28. package/dist/proto/dataplane_pb.js +61 -0
  29. package/dist/proto/dataplane_pb.js.map +1 -0
  30. package/dist/routes/{add-notif-operation.d.ts → fanout-notification-seen.d.ts} +1 -1
  31. package/dist/routes/fanout-notification-seen.d.ts.map +1 -0
  32. package/dist/routes/fanout-notification-seen.js +26 -0
  33. package/dist/routes/fanout-notification-seen.js.map +1 -0
  34. package/dist/routes/index.d.ts.map +1 -1
  35. package/dist/routes/index.js +12 -4
  36. package/dist/routes/index.js.map +1 -1
  37. package/dist/routes/scan-mute-operations.js +108 -45
  38. package/dist/routes/scan-mute-operations.js.map +1 -1
  39. package/dist/routes/scan-notif-operations.js +105 -42
  40. package/dist/routes/scan-notif-operations.js.map +1 -1
  41. package/dist/routes/scan-operations.js +108 -45
  42. package/dist/routes/scan-operations.js.map +1 -1
  43. package/dist/routes/util.d.ts +2 -1
  44. package/dist/routes/util.d.ts.map +1 -1
  45. package/dist/routes/util.js +35 -12
  46. package/dist/routes/util.js.map +1 -1
  47. package/dist/telemetry/attributes.d.ts +14 -0
  48. package/dist/telemetry/attributes.d.ts.map +1 -0
  49. package/dist/telemetry/attributes.js +14 -0
  50. package/dist/telemetry/attributes.js.map +1 -0
  51. package/dist/telemetry/rpc.d.ts +30 -0
  52. package/dist/telemetry/rpc.d.ts.map +1 -0
  53. package/dist/telemetry/rpc.js +113 -0
  54. package/dist/telemetry/rpc.js.map +1 -0
  55. package/dist/telemetry/telemetry.d.ts +2 -0
  56. package/dist/telemetry/telemetry.d.ts.map +1 -0
  57. package/dist/telemetry/telemetry.js +7 -0
  58. package/dist/telemetry/telemetry.js.map +1 -0
  59. package/package.json +16 -7
  60. package/dist/routes/add-notif-operation.d.ts.map +0 -1
  61. package/dist/routes/add-notif-operation.js +0 -61
  62. package/dist/routes/add-notif-operation.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"scan-operations.js","sourceRoot":"","sources":["../../src/routes/scan-operations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAGlC,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAA;AAElE,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvD,eAAe,CAAC,GAAe,EAAwC,EAAE,CAAC,CAAC;IACzE,KAAK,CAAC,cA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E,CAAA;YAClC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;gBAChB,OAAO,IAAI,sBAAsB,CAAC;oBAChC,UAAU,EAAE,EAAE;oBACd,MAAM,EAAE,GAAG,CAAC,MAAM;iBACnB,CAAC,CAAA;YACJ,CAAC;QACH,CAAC;QAED,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;QAElC,OAAO,IAAI,sBAAsB,CAAC;YAChC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;gBAC3B,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE;gBACpB,QAAQ,EAAE,EAAE,CAAC,QAAQ;gBACrB,SAAS,EAAE,EAAE,CAAC,SAAS;gBACvB,GAAG,EAAE,EAAE,CAAC,GAAG;gBACX,MAAM,EAAE,EAAE,CAAC,MAAM;gBACjB,OAAO,EAAE,EAAE,CAAC,OAAO;aACpB,CAAC,CAAC;YACH,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE;SAC7B,CAAC,CAAA;IACJ,CAAC;CACF,CAAC,CAAA","sourcesContent":["import { once } from 'node:events'\nimport type { ServiceImpl } from '@connectrpc/connect'\nimport type { AppContext } from '../context.js'\nimport { createOperationChannel } from '../db/schema/operation.js'\nimport type { Service } from '../proto/bsync_connect.js'\nimport { ScanOperationsResponse } from '../proto/bsync_pb.js'\nimport { authWithApiKey } from './auth.js'\nimport { combineSignals, validCursor } from './util.js'\n\nexport default (ctx: AppContext): Partial<ServiceImpl<typeof Service>> => ({\n async scanOperations(req, handlerCtx) {\n authWithApiKey(ctx, handlerCtx)\n const { db, events } = ctx\n const limit = req.limit || 1000\n const cursor = validCursor(req.cursor)\n const nextOpPromise = once(events, createOperationChannel, {\n signal: combineSignals(\n ctx.shutdown,\n AbortSignal.timeout(ctx.cfg.service.longPollTimeoutMs),\n ),\n })\n nextOpPromise.catch(() => null) // ensure timeout is always handled\n\n const nextOpPageQb = db.db\n .selectFrom('operation')\n .selectAll()\n .where('id', '>', cursor ?? -1)\n .orderBy('id', 'asc')\n .limit(limit)\n\n let ops = await nextOpPageQb.execute()\n\n if (!ops.length) {\n // if there were no ops on the page, wait for an event then try again.\n try {\n await nextOpPromise\n } catch (err) {\n ctx.shutdown.throwIfAborted()\n return new ScanOperationsResponse({\n operations: [],\n cursor: req.cursor,\n })\n }\n ops = await nextOpPageQb.execute()\n if (!ops.length) {\n return new ScanOperationsResponse({\n operations: [],\n cursor: req.cursor,\n })\n }\n }\n\n const lastOp = ops[ops.length - 1]\n\n return new ScanOperationsResponse({\n operations: ops.map((op) => ({\n id: op.id.toString(),\n actorDid: op.actorDid,\n namespace: op.namespace,\n key: op.key,\n method: op.method,\n payload: op.payload,\n })),\n cursor: lastOp.id.toString(),\n })\n },\n})\n"]}
1
+ {"version":3,"file":"scan-operations.js","sourceRoot":"","sources":["../../src/routes/scan-operations.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAGlC,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAA;AAElE,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAExD,eAAe,CAAC,GAAe,EAAwC,EAAE,CAAC,CAAC;IACzE,KAAK,CAAC,cAAc,CAAC,GAAG,EAAE,UAAU;;;YAClC,cAAc,CAAC,GAAG,EAAE,UAAU,CAAC,CAAA;YAC/B,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,GAAG,CAAA;YAC1B,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,IAAI,IAAI,CAAA;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AAM,EAAE,GAAG,CAAC,MAAM;qBACnB,CAAC,CAAA;gBACJ,CAAC;YACH,CAAC;YAED,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;YAElC,OAAO,IAAI,sBAAsB,CAAC;gBAChC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;oBAC3B,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE;oBACpB,QAAQ,EAAE,EAAE,CAAC,QAAQ;oBACrB,SAAS,EAAE,EAAE,CAAC,SAAS;oBACvB,GAAG,EAAE,EAAE,CAAC,GAAG;oBACX,MAAM,EAAE,EAAE,CAAC,MAAM;oBACjB,OAAO,EAAE,EAAE,CAAC,OAAO;iBACpB,CAAC,CAAC;gBACH,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE;aAC7B,CAAC,CAAA;;;;;;;;;IACJ,CAAC;CACF,CAAC,CAAA","sourcesContent":["import { once } from 'node:events'\nimport type { ServiceImpl } from '@connectrpc/connect'\nimport type { AppContext } from '../context.js'\nimport { createOperationChannel } from '../db/schema/operation.js'\nimport type { Service } from '../proto/bsync_connect.js'\nimport { ScanOperationsResponse } from '../proto/bsync_pb.js'\nimport { authWithApiKey } from './auth.js'\nimport { combinedSignals, validCursor } from './util.js'\n\nexport default (ctx: AppContext): Partial<ServiceImpl<typeof Service>> => ({\n async scanOperations(req, handlerCtx) {\n authWithApiKey(ctx, handlerCtx)\n const { db, events } = ctx\n const limit = req.limit || 1000\n const cursor = validCursor(req.cursor)\n\n using signal = combinedSignals(\n ctx.shutdown,\n AbortSignal.timeout(ctx.cfg.service.longPollTimeoutMs),\n )\n\n const nextOpPromise = once(events, createOperationChannel, { signal })\n\n // awaited later\n void nextOpPromise.catch(() => null)\n\n const nextOpPageQb = db.db\n .selectFrom('operation')\n .selectAll()\n .where('id', '>', cursor ?? -1)\n .orderBy('id', 'asc')\n .limit(limit)\n\n let ops = await nextOpPageQb.execute()\n\n if (!ops.length) {\n // if there were no ops on the page, wait for an event then try again.\n try {\n await nextOpPromise\n } catch (err) {\n if (ctx.shutdown.aborted) throw err\n\n return new ScanOperationsResponse({\n operations: [],\n cursor: req.cursor,\n })\n }\n ops = await nextOpPageQb.execute()\n if (!ops.length) {\n return new ScanOperationsResponse({\n operations: [],\n cursor: req.cursor,\n })\n }\n }\n\n const lastOp = ops[ops.length - 1]\n\n return new ScanOperationsResponse({\n operations: ops.map((op) => ({\n id: op.id.toString(),\n actorDid: op.actorDid,\n namespace: op.namespace,\n key: op.key,\n method: op.method,\n payload: op.payload,\n })),\n cursor: lastOp.id.toString(),\n })\n },\n})\n"]}
@@ -1,5 +1,6 @@
1
1
  export declare const validCursor: (cursor: string) => number | null;
2
- export declare const combineSignals: (a: AbortSignal, b: AbortSignal) => AbortSignal;
2
+ export declare function combineSignals(...signals: readonly (AbortSignal | undefined)[]): AbortController & Disposable;
3
+ export declare function combinedSignals(...signals: readonly (AbortSignal | undefined)[]): AbortSignal & Disposable;
3
4
  export declare const isValidDid: (did: string) => boolean;
4
5
  export declare const isValidAtUri: (uri: string) => boolean;
5
6
  export declare const validateNamespace: (namespace: string) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/routes/util.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,WAAW,WAAY,MAAM,KAAG,MAAM,GAAG,IAOrD,CAAA;AAED,eAAO,MAAM,cAAc,MAAO,WAAW,KAAK,WAAW,KAAG,WAa/D,CAAA;AAED,eAAO,MAAM,UAAU,QAAS,MAAM,YAUrC,CAAA;AAED,eAAO,MAAM,YAAY,QAAS,MAAM,YAOvC,CAAA;AAED,eAAO,MAAM,iBAAiB,cAAe,MAAM,KAAG,IAarD,CAAA"}
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/routes/util.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,WAAW,WAAY,MAAM,KAAG,MAAM,GAAG,IAOrD,CAAA;AAED,wBAAgB,cAAc,CAC5B,GAAG,OAAO,EAAE,SAAS,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,GAC/C,eAAe,GAAG,UAAU,CAwB9B;AAED,wBAAgB,eAAe,CAC7B,GAAG,OAAO,EAAE,SAAS,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,GAC/C,WAAW,GAAG,UAAU,CAK1B;AAYD,eAAO,MAAM,UAAU,QAAS,MAAM,YAUrC,CAAA;AAED,eAAO,MAAM,YAAY,QAAS,MAAM,YAOvC,CAAA;AAED,eAAO,MAAM,iBAAiB,cAAe,MAAM,KAAG,IAarD,CAAA"}
@@ -9,20 +9,43 @@ export const validCursor = (cursor) => {
9
9
  }
10
10
  return int;
11
11
  };
12
- export const combineSignals = (a, b) => {
13
- const controller = new AbortController();
14
- for (const signal of [a, b]) {
15
- if (signal.aborted) {
16
- controller.abort();
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
- return controller.signal;
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);
@@ -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,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAc,EAAiB,EAAE;IAC3D,IAAI,MAAM,KAAK,EAAE;QAAE,OAAO,IAAI,CAAA;IAC9B,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;IAChC,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,YAAY,CAAC,gBAAgB,EAAE,IAAI,CAAC,eAAe,CAAC,CAAA;IAChE,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAc,EAAE,CAAc,EAAe,EAAE;IAC5E,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;IACxC,KAAK,MAAM,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;QAC5B,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,UAAU,CAAC,KAAK,EAAE,CAAA;YAClB,OAAO,MAAM,CAAA;QACf,CAAC;QACD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;YACtE,2EAA2E;YAC3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validDidError,\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 const combineSignals = (a: AbortSignal, b: AbortSignal): AbortSignal => {\n const controller = new AbortController()\n for (const signal of [a, b]) {\n if (signal.aborted) {\n controller.abort()\n return signal\n }\n signal.addEventListener('abort', () => controller.abort(signal.reason), {\n // @ts-ignore https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68625\n signal: controller.signal,\n })\n }\n return controller.signal\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"]}
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,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAc,EAAiB,EAAE;IAC3D,IAAI,MAAM,KAAK,EAAE;QAAE,OAAO,IAAI,CAAA;IAC9B,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM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,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"]}
@@ -0,0 +1,14 @@
1
+ import type { Attributes } from '@opentelemetry/api';
2
+ import { Method } from '../proto/bsync_pb.js';
3
+ /**
4
+ * Attributes describing a stash operation.
5
+ *
6
+ *
7
+ * Both values are low-cardinality: the namespace is an NSID drawn from a fixed
8
+ * set of record types, and the method is one of create/update/delete.
9
+ */
10
+ export declare const operationAttributes: (op: {
11
+ namespace: string;
12
+ method: Method;
13
+ }) => Attributes;
14
+ //# sourceMappingURL=attributes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributes.d.ts","sourceRoot":"","sources":["../../src/telemetry/attributes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAKpD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,OAAQ;IACtC,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;CACf,KAAG,UAGF,CAAA"}
@@ -0,0 +1,14 @@
1
+ import { ATTR_BSYNC_NAMESPACE, ATTR_BSYNC_OPERATION, } from '@atproto-labs/opentelemetry-node/conventions';
2
+ import { Method } from '../proto/bsync_pb.js';
3
+ /**
4
+ * Attributes describing a stash operation.
5
+ *
6
+ *
7
+ * Both values are low-cardinality: the namespace is an NSID drawn from a fixed
8
+ * set of record types, and the method is one of create/update/delete.
9
+ */
10
+ export const operationAttributes = (op) => ({
11
+ [ATTR_BSYNC_NAMESPACE]: op.namespace,
12
+ [ATTR_BSYNC_OPERATION]: Method[op.method]?.toLowerCase() ?? 'unknown',
13
+ });
14
+ //# sourceMappingURL=attributes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributes.js","sourceRoot":"","sources":["../../src/telemetry/attributes.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,8CAA8C,CAAA;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,EAGnC,EAAc,EAAE,CAAC,CAAC;IACjB,CAAC,oBAAoB,CAAC,EAAE,EAAE,CAAC,SAAS;IACpC,CAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,IAAI,SAAS;CACtE,CAAC,CAAA","sourcesContent":["import type { Attributes } from '@opentelemetry/api'\nimport {\n ATTR_BSYNC_NAMESPACE,\n ATTR_BSYNC_OPERATION,\n} from '@atproto-labs/opentelemetry-node/conventions'\nimport { Method } from '../proto/bsync_pb.js'\n\n/**\n * Attributes describing a stash operation.\n *\n *\n * Both values are low-cardinality: the namespace is an NSID drawn from a fixed\n * set of record types, and the method is one of create/update/delete.\n */\nexport const operationAttributes = (op: {\n namespace: string\n method: Method\n}): Attributes => ({\n [ATTR_BSYNC_NAMESPACE]: op.namespace,\n [ATTR_BSYNC_OPERATION]: Method[op.method]?.toLowerCase() ?? 'unknown',\n})\n"]}
@@ -0,0 +1,30 @@
1
+ import { type Interceptor, type ServiceImpl, type StreamRequest, type UnaryRequest } from '@connectrpc/connect';
2
+ import { type Attributes } from '@opentelemetry/api';
3
+ import type { Service } from '../proto/bsync_connect.js';
4
+ type ServiceMethods = Partial<ServiceImpl<typeof Service>>;
5
+ type RpcRequest = UnaryRequest | StreamRequest;
6
+ type GetClientAttributes = (request: RpcRequest) => Attributes;
7
+ /**
8
+ * Derives extra attributes from a request. The returned attributes land on both
9
+ * the duration histogram and the request span, so they must stay
10
+ * low-cardinality: no DIDs, keys or URIs, which would explode the metric series.
11
+ */
12
+ export type GetAttributes<M extends keyof ServiceMethods> = (request: Parameters<NonNullable<ServiceMethods[M]>>[0]) => Attributes;
13
+ export declare const createRpcClientInterceptor: (getAttributes?: GetClientAttributes) => Interceptor;
14
+ /**
15
+ * Wraps every handler of a service implementation with server-side RPC
16
+ * telemetry: an `rpc.server.call.duration` histogram, plus RPC attributes and
17
+ * error status on the span the HTTP instrumentation already opened for the
18
+ * request.
19
+ *
20
+ * The client half of the same call is recorded by the AppView, in
21
+ * `@atproto/bsky`'s `createRpcClientInterceptor`. Both sides use the same
22
+ * attribute keys, so queue time (client duration minus server duration) is a
23
+ * subtraction rather than a join across differently-shaped series.
24
+ *
25
+ * @note Connect has no server-side counterpart to a client `Interceptor`, so
26
+ * this wraps the implementations rather than plugging into the router.
27
+ */
28
+ export declare const withRpcServerTelemetry: <T extends ServiceMethods>(methods: T, getAttributes?: { [M in keyof T]?: GetAttributes<M & keyof ServiceMethods>; }) => T;
29
+ export {};
30
+ //# sourceMappingURL=rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/telemetry/rpc.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EACL,KAAK,UAAU,EAShB,MAAM,oBAAoB,CAAA;AAY3B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAExD,KAAK,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,OAAO,CAAC,CAAC,CAAA;AAC1D,KAAK,UAAU,GAAG,YAAY,GAAG,aAAa,CAAA;AAC9C,KAAK,mBAAmB,GAAG,CAAC,OAAO,EAAE,UAAU,KAAK,UAAU,CAAA;AAE9D;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,MAAM,cAAc,IAAI,CAC1D,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KACnD,UAAU,CAAA;AA6Bf,eAAO,MAAM,0BAA0B,mBACrB,mBAAmB,KAClC,WAwCF,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,cAAc,WACpD,CAAC,kBACM,GAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,GAAG,MAAM,cAAc,CAAC,GAAE,KAC3E,CAMK,CAAA"}
@@ -0,0 +1,113 @@
1
+ import { performance } from 'node:perf_hooks';
2
+ import { Code, ConnectError, } from '@connectrpc/connect';
3
+ import { SpanKind, SpanStatusCode, ValueType, context, metrics, propagation, trace, } from '@opentelemetry/api';
4
+ import { ATTR_ERROR_TYPE } from '@opentelemetry/semantic-conventions';
5
+ import { ATTR_RPC_METHOD, ATTR_RPC_RESPONSE_STATUS_CODE, ATTR_RPC_SYSTEM_NAME, METRIC_RPC_CLIENT_CALL_DURATION, METRIC_RPC_SERVER_CALL_DURATION, RPC_SYSTEM_NAME_VALUE_CONNECTRPC, } from '@opentelemetry/semantic-conventions/incubating';
6
+ import { RPC_CALL_DURATION_BUCKETS } from '@atproto-labs/opentelemetry-node/conventions';
7
+ import { statusCodeToString } from '@atproto-labs/opentelemetry-node/util';
8
+ const meter = metrics.getMeter('@atproto/bsync');
9
+ const tracer = trace.getTracer('@atproto/bsync');
10
+ const rpcClientDuration = meter.createHistogram(METRIC_RPC_CLIENT_CALL_DURATION, {
11
+ description: 'Measures the duration of an outgoing Remote Procedure Call',
12
+ unit: 's',
13
+ valueType: ValueType.DOUBLE,
14
+ advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },
15
+ });
16
+ const rpcServerDuration = meter.createHistogram(METRIC_RPC_SERVER_CALL_DURATION, {
17
+ description: 'Measures the duration of an incoming Remote Procedure Call',
18
+ unit: 's',
19
+ valueType: ValueType.DOUBLE,
20
+ advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },
21
+ });
22
+ const headersSetter = {
23
+ set(carrier, key, value) {
24
+ carrier.set(key, value);
25
+ },
26
+ };
27
+ export const createRpcClientInterceptor = (getAttributes) => {
28
+ return (next) => async (req) => {
29
+ const method = `${req.service.typeName}/${req.method.name}`;
30
+ const attributes = {
31
+ [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,
32
+ [ATTR_RPC_METHOD]: method,
33
+ ...getAttributes?.(req),
34
+ };
35
+ const start = performance.now();
36
+ return tracer.startActiveSpan(method, { kind: SpanKind.CLIENT, attributes }, async (span) => {
37
+ propagation.inject(context.active(), req.header, headersSetter);
38
+ let code;
39
+ try {
40
+ return await next(req);
41
+ }
42
+ catch (err) {
43
+ code = err instanceof ConnectError ? err.code : Code.Unknown;
44
+ span.setStatus({ code: SpanStatusCode.ERROR });
45
+ span.recordException(err instanceof Error ? err : String(err));
46
+ throw err;
47
+ }
48
+ finally {
49
+ const statusCode = statusCodeToString(code);
50
+ const completedAttributes = {
51
+ ...attributes,
52
+ [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,
53
+ ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),
54
+ };
55
+ span.setAttributes(completedAttributes);
56
+ rpcClientDuration.record((performance.now() - start) / 1000, completedAttributes);
57
+ span.end();
58
+ }
59
+ });
60
+ };
61
+ };
62
+ /**
63
+ * Wraps every handler of a service implementation with server-side RPC
64
+ * telemetry: an `rpc.server.call.duration` histogram, plus RPC attributes and
65
+ * error status on the span the HTTP instrumentation already opened for the
66
+ * request.
67
+ *
68
+ * The client half of the same call is recorded by the AppView, in
69
+ * `@atproto/bsky`'s `createRpcClientInterceptor`. Both sides use the same
70
+ * attribute keys, so queue time (client duration minus server duration) is a
71
+ * subtraction rather than a join across differently-shaped series.
72
+ *
73
+ * @note Connect has no server-side counterpart to a client `Interceptor`, so
74
+ * this wraps the implementations rather than plugging into the router.
75
+ */
76
+ export const withRpcServerTelemetry = (methods, getAttributes) => Object.fromEntries(Object.entries(methods).map(([name, impl]) => [
77
+ name,
78
+ instrument(impl, getAttributes?.[name]),
79
+ ]));
80
+ const instrument = (impl, getAttributes) => async function (request, ctx) {
81
+ // @NOTE The RPC convention requires `rpc.method` to be the fully-qualified
82
+ // method name, so the service name is part of it rather than an attribute
83
+ // of its own.
84
+ const attributes = {
85
+ [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,
86
+ [ATTR_RPC_METHOD]: `${ctx.service.typeName}/${ctx.method.name}`,
87
+ ...getAttributes?.(request),
88
+ };
89
+ const span = trace.getActiveSpan();
90
+ span?.setAttributes(attributes);
91
+ const start = performance.now();
92
+ let code;
93
+ try {
94
+ return await impl.call(this, request, ctx);
95
+ }
96
+ catch (err) {
97
+ code = err instanceof ConnectError ? err.code : Code.Unknown;
98
+ span?.setStatus({ code: SpanStatusCode.ERROR });
99
+ span?.recordException(err instanceof Error ? err : String(err));
100
+ throw err;
101
+ }
102
+ finally {
103
+ const statusCode = statusCodeToString(code);
104
+ rpcServerDuration.record((performance.now() - start) / 1000, {
105
+ ...attributes,
106
+ [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,
107
+ // @NOTE The convention requires `error.type` to be unset on success,
108
+ // and to carry the status code on failure.
109
+ ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),
110
+ });
111
+ }
112
+ };
113
+ //# sourceMappingURL=rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../src/telemetry/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EACL,IAAI,EACJ,YAAY,GAMb,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAEL,QAAQ,EACR,cAAc,EAEd,SAAS,EACT,OAAO,EACP,OAAO,EACP,WAAW,EACX,KAAK,GACN,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,eAAe,EAAE,MAA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AAE,GAAG,CAAC,CAAA;IAC5C,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,GAAG,YAAY,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAA;QAC5D,IAAI,EAAE,SAAS,CAAC,EAAE,IAAI,EAAE,cAAc,CAAC,KAAK,EAAE,CAAC,CAAA;QAC/C,IAAI,EAAE,eAAe,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;QAC/D,MAAM,GAAG,CAAA;IACX,CAAC;YAAS,CAAC;QACT,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAA;QAC3C,iBAAiB,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,IAAI,EAAE;YAC3D,GAAG,UAAU;YACb,CAAC,6BAA6B,CAAC,EAAE,UAAU;YAC3C,qEAAqE;YACrE,2CAA2C;YAC3C,GAAG,CAAC,IAAI,KAAK,SAAS,IAAI,EAAE,CAAC,eAAe,CAAC,EAAE,UAAU,EAAE,CAAC;SAC7D,CAAC,CAAA;IACJ,CAAC;AACH,CAAC,CAAA","sourcesContent":["import { performance } from 'node:perf_hooks'\nimport {\n Code,\n ConnectError,\n type HandlerContext,\n type Interceptor,\n type ServiceImpl,\n type StreamRequest,\n type UnaryRequest,\n} from '@connectrpc/connect'\nimport {\n type Attributes,\n SpanKind,\n SpanStatusCode,\n type TextMapSetter,\n ValueType,\n context,\n metrics,\n propagation,\n trace,\n} from '@opentelemetry/api'\nimport { ATTR_ERROR_TYPE } from '@opentelemetry/semantic-conventions'\nimport {\n ATTR_RPC_METHOD,\n ATTR_RPC_RESPONSE_STATUS_CODE,\n ATTR_RPC_SYSTEM_NAME,\n METRIC_RPC_CLIENT_CALL_DURATION,\n METRIC_RPC_SERVER_CALL_DURATION,\n RPC_SYSTEM_NAME_VALUE_CONNECTRPC,\n} from '@opentelemetry/semantic-conventions/incubating'\nimport { RPC_CALL_DURATION_BUCKETS } from '@atproto-labs/opentelemetry-node/conventions'\nimport { statusCodeToString } from '@atproto-labs/opentelemetry-node/util'\nimport type { Service } from '../proto/bsync_connect.js'\n\ntype ServiceMethods = Partial<ServiceImpl<typeof Service>>\ntype RpcRequest = UnaryRequest | StreamRequest\ntype GetClientAttributes = (request: RpcRequest) => Attributes\n\n/**\n * Derives extra attributes from a request. The returned attributes land on both\n * the duration histogram and the request span, so they must stay\n * low-cardinality: no DIDs, keys or URIs, which would explode the metric series.\n */\nexport type GetAttributes<M extends keyof ServiceMethods> = (\n request: Parameters<NonNullable<ServiceMethods[M]>>[0],\n) => Attributes\n\nconst meter = metrics.getMeter('@atproto/bsync')\nconst tracer = trace.getTracer('@atproto/bsync')\nconst rpcClientDuration = meter.createHistogram(\n METRIC_RPC_CLIENT_CALL_DURATION,\n {\n description: 'Measures the duration of an outgoing Remote Procedure Call',\n unit: 's',\n valueType: ValueType.DOUBLE,\n advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },\n },\n)\nconst rpcServerDuration = meter.createHistogram(\n METRIC_RPC_SERVER_CALL_DURATION,\n {\n description: 'Measures the duration of an incoming Remote Procedure Call',\n unit: 's',\n valueType: ValueType.DOUBLE,\n advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },\n },\n)\n\nconst headersSetter: TextMapSetter<Headers> = {\n set(carrier, key, value) {\n carrier.set(key, value)\n },\n}\n\nexport const createRpcClientInterceptor = (\n getAttributes?: GetClientAttributes,\n): Interceptor => {\n return (next) => async (req: RpcRequest) => {\n const method = `${req.service.typeName}/${req.method.name}`\n const attributes: Attributes = {\n [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,\n [ATTR_RPC_METHOD]: method,\n ...getAttributes?.(req),\n }\n const start = performance.now()\n\n return tracer.startActiveSpan(\n method,\n { kind: SpanKind.CLIENT, attributes },\n async (span) => {\n propagation.inject(context.active(), req.header, headersSetter)\n let code: Code | undefined\n try {\n return await next(req)\n } catch (err) {\n code = err instanceof ConnectError ? err.code : Code.Unknown\n span.setStatus({ code: SpanStatusCode.ERROR })\n span.recordException(err instanceof Error ? err : String(err))\n throw err\n } finally {\n const statusCode = statusCodeToString(code)\n const completedAttributes: Attributes = {\n ...attributes,\n [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,\n ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),\n }\n span.setAttributes(completedAttributes)\n rpcClientDuration.record(\n (performance.now() - start) / 1000,\n completedAttributes,\n )\n span.end()\n }\n },\n )\n }\n}\n\n/**\n * Wraps every handler of a service implementation with server-side RPC\n * telemetry: an `rpc.server.call.duration` histogram, plus RPC attributes and\n * error status on the span the HTTP instrumentation already opened for the\n * request.\n *\n * The client half of the same call is recorded by the AppView, in\n * `@atproto/bsky`'s `createRpcClientInterceptor`. Both sides use the same\n * attribute keys, so queue time (client duration minus server duration) is a\n * subtraction rather than a join across differently-shaped series.\n *\n * @note Connect has no server-side counterpart to a client `Interceptor`, so\n * this wraps the implementations rather than plugging into the router.\n */\nexport const withRpcServerTelemetry = <T extends ServiceMethods>(\n methods: T,\n getAttributes?: { [M in keyof T]?: GetAttributes<M & keyof ServiceMethods> },\n): T =>\n Object.fromEntries(\n Object.entries(methods).map(([name, impl]) => [\n name,\n instrument(impl as Handler, getAttributes?.[name as keyof T]),\n ]),\n ) as T\n\ntype Handler = (request: never, ctx: HandlerContext) => Promise<unknown>\n\nconst instrument = (\n impl: Handler,\n getAttributes?: (request: never) => Attributes,\n): Handler =>\n async function (this: unknown, request, ctx) {\n // @NOTE The RPC convention requires `rpc.method` to be the fully-qualified\n // method name, so the service name is part of it rather than an attribute\n // of its own.\n const attributes: Attributes = {\n [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,\n [ATTR_RPC_METHOD]: `${ctx.service.typeName}/${ctx.method.name}`,\n ...getAttributes?.(request),\n }\n\n const span = trace.getActiveSpan()\n span?.setAttributes(attributes)\n\n const start = performance.now()\n let code: Code | undefined\n try {\n return await impl.call(this, request, ctx)\n } catch (err) {\n code = err instanceof ConnectError ? err.code : Code.Unknown\n span?.setStatus({ code: SpanStatusCode.ERROR })\n span?.recordException(err instanceof Error ? err : String(err))\n throw err\n } finally {\n const statusCode = statusCodeToString(code)\n rpcServerDuration.record((performance.now() - start) / 1000, {\n ...attributes,\n [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,\n // @NOTE The convention requires `error.type` to be unset on success,\n // and to carry the status code on failure.\n ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),\n })\n }\n }\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=telemetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/telemetry/telemetry.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import pkg from '@atproto/bsync/package.json' with { type: 'json' };
2
+ import { setup } from '@atproto-labs/opentelemetry-node';
3
+ await setup(() => ({
4
+ name: pkg.name,
5
+ version: pkg.version,
6
+ }));
7
+ //# sourceMappingURL=telemetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../../src/telemetry/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,6BAA6B,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAA;AAExD,MAAM,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;IACjB,IAAI,EAAE,GAAG,CAAC,IAAI;IACd,OAAO,EAAE,GAAG,CAAC,OAAO;CACrB,CAAC,CAAC,CAAA","sourcesContent":["import pkg from '@atproto/bsync/package.json' with { type: 'json' }\nimport { setup } from '@atproto-labs/opentelemetry-node'\n\nawait setup(() => ({\n name: pkg.name,\n version: pkg.version,\n}))\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/bsync",
3
- "version": "0.0.42",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "description": "Sychronizing service for app.bsky App View (Bluesky API)",
6
6
  "keywords": [
@@ -23,6 +23,13 @@
23
23
  ".": {
24
24
  "types": "./dist/index.d.ts",
25
25
  "default": "./dist/index.js"
26
+ },
27
+ "./telemetry": {
28
+ "types": "./dist/telemetry/telemetry.d.ts",
29
+ "default": "./dist/telemetry/telemetry.js"
30
+ },
31
+ "./package.json": {
32
+ "default": "./package.json"
26
33
  }
27
34
  },
28
35
  "engines": {
@@ -32,20 +39,23 @@
32
39
  "@bufbuild/protobuf": "^1.5.0",
33
40
  "@connectrpc/connect": "^1.1.4",
34
41
  "@connectrpc/connect-node": "^1.1.4",
42
+ "@opentelemetry/api": "^1.9.1",
43
+ "@opentelemetry/semantic-conventions": "^1.41.1",
35
44
  "http-terminator": "^3.2.0",
36
45
  "kysely": "^0.29.2",
37
46
  "pg": "^8.10.0",
38
47
  "pino-http": "^11.0.0",
39
- "@atproto/common": "^0.8.1",
40
- "@atproto/syntax": "^0.7.5"
48
+ "@atproto-labs/opentelemetry-node": "^0.2.1",
49
+ "@atproto/common": "^0.8.3",
50
+ "@atproto/syntax": "^0.7.6"
41
51
  },
42
52
  "devDependencies": {
43
- "@bufbuild/buf": "^1.28.1",
53
+ "@bufbuild/buf": "^1.72.0",
44
54
  "@bufbuild/protoc-gen-es": "^1.5.0",
45
- "@connectrpc/protoc-gen-connect-es": "^1.1.4",
55
+ "@connectrpc/protoc-gen-connect-es": "^1.7.0",
46
56
  "@types/pg": "^8.15.5",
47
57
  "get-port": "^5.1.1",
48
- "jest": "^30.0.0"
58
+ "jest": "^30.5.1"
49
59
  },
50
60
  "scripts": {
51
61
  "codegen:buf": "buf generate proto",
@@ -53,7 +63,6 @@
53
63
  "build": "tsc --build tsconfig.build.json",
54
64
  "start": "node --enable-source-maps dist/bin.js",
55
65
  "test": "NODE_OPTIONS=--experimental-vm-modules ../dev-infra/with-test-db.sh jest",
56
- "test:log": "tail -50 test.log | pino-pretty",
57
66
  "test:updateSnapshot": "jest --updateSnapshot",
58
67
  "migration:create": "node ./bin/migration-create.ts",
59
68
  "buf:gen": ">&2 echo 'DEPRECATED: run `pnpm run codegen:buf` instead ' && pnpm run codegen:buf"
@@ -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,CAAC,CAAA;QAC/B,MAAM,EAAE,EAAE,EAAE,GAAG,GAAG,CAAA;QAClB,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,G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AM,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"]}