@airnauts/comments-adapter-memory 0.1.0 → 0.4.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.
- package/README.md +20 -2
- package/dist/in-memory.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
# @airnauts/comments-adapter-memory
|
|
2
2
|
|
|
3
|
-
In-memory `Repository` adapter for the
|
|
4
|
-
|
|
3
|
+
In-memory `Repository` adapter for the
|
|
4
|
+
[Airnauts commenting tool](https://github.com/Airnauts/commenting-tool) server —
|
|
5
|
+
ephemeral, process-local storage for local development and tests. State is lost when
|
|
6
|
+
the process exits; pair with a real persistence adapter (e.g.
|
|
7
|
+
`@airnauts/comments-adapter-mongo`) for production.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @airnauts/comments-adapter-memory
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
5
16
|
|
|
6
17
|
```ts
|
|
7
18
|
import { memoryRepository } from '@airnauts/comments-adapter-memory'
|
|
8
19
|
|
|
9
20
|
const repository = memoryRepository()
|
|
10
21
|
```
|
|
22
|
+
|
|
23
|
+
Pass `repository` to `createCommentsServer` from `@airnauts/comments-server`. The
|
|
24
|
+
underlying `InMemoryRepository` class is also exported.
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
MIT © Airnauts
|
package/dist/in-memory.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.d.ts","sourceRoot":"","sources":["../src/in-memory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,cAAc,EACd,YAAY,EACb,MAAM,yBAAyB,CAAA;AAChC,OAAO,EACL,KAA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AAa,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlE,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAS9E,KAAK,IAAI,IAAI;CAId"}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ function clone(value) {
|
|
|
10
10
|
return structuredClone(value);
|
|
11
11
|
}
|
|
12
12
|
function toListItem(t) {
|
|
13
|
+
const root = t.comments[0];
|
|
13
14
|
const {
|
|
14
15
|
comments: _c,
|
|
15
16
|
captureContext: _cc,
|
|
@@ -18,7 +19,10 @@ function toListItem(t) {
|
|
|
18
19
|
env: _env,
|
|
19
20
|
...rest
|
|
20
21
|
} = t;
|
|
21
|
-
return
|
|
22
|
+
return {
|
|
23
|
+
...rest,
|
|
24
|
+
rootComment: root ? { text: root.text, createdAt: root.createdAt } : null
|
|
25
|
+
};
|
|
22
26
|
}
|
|
23
27
|
function toThread(t) {
|
|
24
28
|
const { projectId: _pid, env: _env, ...wire } = t;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/in-memory.ts","../src/index.ts"],"sourcesContent":["import type {\n Attachment,\n AttachmentId,\n Thread,\n ThreadId,\n ThreadListItem,\n ThreadStatus,\n} from '@airnauts/comments-core'\nimport {\n type AnchorPatch,\n decodeCursor,\n encodeCursor,\n type ListQuery,\n type ListResult,\n type NewComment,\n type NewThread,\n type Repository,\n type Scope,\n} from '@airnauts/comments-server'\n\ntype StoredThread = Thread & { projectId: string; env?: string }\n\n// Attachments are keyed by scope + id; the separators keep the three parts unambiguous.\nfunction attachmentKey(scope: Scope, id: AttachmentId): string {\n return `${scope.projectId}\u0000${scope.env ?? ''}\u0000${id}`\n}\n\nfunction clone<T>(value: T): T {\n return structuredClone(value)\n}\n\nfunction toListItem(t: StoredThread): ThreadListItem {\n // Strip server-only scope + thread-only payload (comments/captureContext/provenance).\n const {\n comments: _c,\n captureContext: _cc,\n provenance: _p,\n projectId: _pid,\n env: _env,\n ...rest\n } = t as StoredThread & Record<string, unknown>\n return rest as ThreadListItem\n}\n\nfunction toThread(t: StoredThread): Thread {\n const { projectId: _pid, env: _env, ...wire } = t as StoredThread & Record<string, unknown>\n return wire as Thread\n}\n\nfunction matchesScope(t: StoredThread, scope: Scope): boolean {\n if (t.projectId !== scope.projectId) return false\n return (t.env ?? undefined) === (scope.env ?? undefined)\n}\n\nfunction unresolvedCountOf(thread: StoredThread): number {\n return thread.status === 'open' ? 1 : 0\n}\n\nfunction recomputeCounts(thread: StoredThread): StoredThread {\n return {\n ...thread,\n commentCount: thread.comments.length,\n unresolvedCount: unresolvedCountOf(thread),\n }\n}\n\nexport class InMemoryRepository implements Repository {\n private threads = new Map<string, StoredThread>()\n private attachments = new Map<string, Attachment>()\n\n async createThread(input: NewThread): Promise<Thread> {\n const stored: StoredThread = recomputeCounts({\n projectId: input.projectId,\n env: input.env,\n id: input.id,\n scope: input.scope,\n pageKey: input.pageKey,\n pageUrl: input.pageUrl,\n pageTitle: input.pageTitle,\n anchor: clone(input.anchor),\n status: input.status,\n anchorState: input.anchorState,\n selectionLost: input.selectionLost,\n captureContext: clone(input.captureContext),\n provenance: input.provenance ? clone(input.provenance) : undefined,\n createdBy: clone(input.createdBy),\n createdAt: input.createdAt,\n updatedAt: input.updatedAt,\n lastActivityAt: input.lastActivityAt,\n schemaVersion: input.schemaVersion,\n commentCount: 0, // placeholder, overwritten by recomputeCounts\n unresolvedCount: 0, // placeholder, overwritten by recomputeCounts\n comments: [clone(input.firstComment)],\n })\n this.threads.set(input.id, stored)\n return toThread(clone(stored))\n }\n\n async getThread(scope: Scope, id: ThreadId): Promise<Thread | null> {\n const t = this.threads.get(id)\n if (!t || !matchesScope(t, scope)) return null\n return toThread(clone(t))\n }\n\n async listThreads(query: ListQuery): Promise<ListResult> {\n const limit = Math.max(1, Math.min(query.limit, 200))\n const filtered: StoredThread[] = []\n for (const t of this.threads.values()) {\n if (!matchesScope(t, query)) continue\n if (query.pageKey !== undefined && t.pageKey !== query.pageKey) continue\n if (query.status !== undefined && t.status !== query.status) continue\n filtered.push(t)\n }\n filtered.sort((a, b) => {\n if (a.updatedAt !== b.updatedAt) return a.updatedAt < b.updatedAt ? 1 : -1\n return a.id < b.id ? 1 : a.id > b.id ? -1 : 0\n })\n\n let start = 0\n if (query.cursor) {\n const decoded = decodeCursor(query.cursor)\n if (decoded) {\n start = filtered.findIndex(\n (t) =>\n t.updatedAt < decoded.updatedAt ||\n (t.updatedAt === decoded.updatedAt && t.id < decoded.id),\n )\n if (start === -1) start = filtered.length\n }\n }\n const page = filtered.slice(start, start + limit)\n const last = page[page.length - 1]\n const more = start + limit < filtered.length\n const nextCursor =\n more && last ? encodeCursor({ updatedAt: last.updatedAt, id: last.id }) : null\n return {\n threads: page.map((t) => toListItem(clone(t))),\n nextCursor,\n }\n }\n\n async addComment(scope: Scope, threadId: ThreadId, comment: NewComment): Promise<NewComment> {\n const t = this.threads.get(threadId)\n if (!t || !matchesScope(t, scope)) throw new Error('thread not found')\n const next: StoredThread = recomputeCounts({\n ...t,\n comments: [...t.comments, clone(comment)],\n updatedAt: comment.createdAt,\n lastActivityAt: comment.createdAt,\n })\n this.threads.set(threadId, next)\n return clone(comment)\n }\n\n async setStatus(\n scope: Scope,\n threadId: ThreadId,\n status: ThreadStatus,\n now: string,\n ): Promise<Thread> {\n const t = this.threads.get(threadId)\n if (!t || !matchesScope(t, scope)) throw new Error('thread not found')\n const next: StoredThread = recomputeCounts({\n ...t,\n status,\n updatedAt: now,\n lastActivityAt: now,\n })\n this.threads.set(threadId, next)\n return toThread(clone(next))\n }\n\n async updateAnchor(\n scope: Scope,\n threadId: ThreadId,\n patch: AnchorPatch,\n now: string,\n ): Promise<ThreadListItem> {\n const t = this.threads.get(threadId)\n if (!t || !matchesScope(t, scope)) throw new Error('thread not found')\n const nextAnchor = {\n ...t.anchor,\n selectors: patch.selectors ?? t.anchor.selectors,\n signals: patch.signals ?? t.anchor.signals,\n }\n const next: StoredThread = recomputeCounts({\n ...t,\n anchor: nextAnchor,\n anchorState: patch.anchorState,\n selectionLost: patch.selectionLost ?? t.selectionLost,\n updatedAt: now,\n lastActivityAt: now,\n })\n this.threads.set(threadId, next)\n return toListItem(clone(next))\n }\n\n async putAttachment(scope: Scope, attachment: Attachment): Promise<void> {\n this.attachments.set(attachmentKey(scope, attachment.id), clone(attachment))\n }\n\n async getAttachments(scope: Scope, ids: AttachmentId[]): Promise<Attachment[]> {\n const out: Attachment[] = []\n for (const id of ids) {\n const found = this.attachments.get(attachmentKey(scope, id))\n if (found) out.push(clone(found))\n }\n return out\n }\n\n reset(): void {\n this.threads.clear()\n this.attachments.clear()\n }\n}\n","import type { Repository } from '@airnauts/comments-server'\nimport { InMemoryRepository } from './in-memory'\n\nexport { InMemoryRepository }\n\n/** Fresh, process-local in-memory `Repository`. No connection, no config. */\nexport function memoryRepository(): Repository {\n return new InMemoryRepository()\n}\n"],"mappings":";AAQA;AAAA,EAEE;AAAA,EACA;AAAA,OAOK;AAKP,SAAS,cAAc,OAAc,IAA0B;AAC7D,SAAO,GAAG,MAAM,SAAS,KAAI,MAAM,OAAO,EAAE,KAAI,EAAE;AACpD;AAEA,SAAS,MAAS,OAAa;AAC7B,SAAO,gBAAgB,KAAK;AAC9B;AAEA,SAAS,WAAW,GAAiC;AAEnD,QAAM;AAAA,IACJ,UAAU;AAAA,IACV,gBAAgB;AAAA,IAChB,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,KAAK;AAAA,IACL,GAAG;AAAA,EACL,IAAI;AACJ,SAAO;AACT;AAEA,SAAS,SAAS,GAAyB;AACzC,QAAM,EAAE,WAAW,MAAM,KAAK,MAAM,GAAG,KAAK,IAAI;AAChD,SAAO;AACT;AAEA,SAAS,aAAa,GAAiB,OAAuB;AAC5D,MAAI,EAAE,cAAc,MAAM,UAAW,QA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e,MAAM,iBAAiB,EAAE;AAAA,MACxC,WAAW;AAAA,MACX,gBAAgB;AAAA,IAClB,CAAC;AACD,SAAK,QAAQ,IAAI,UAAU,IAAI;AAC/B,WAAO,WAAW,MAAM,IAAI,CAAC;AAAA,EAC/B;AAAA,EAEA,MAAM,cAAc,OAAc,YAAuC;AACvE,SAAK,YAAY,IAAI,cAAc,OAAO,WAAW,EAAE,GAAG,MAAM,UAAU,CAAC;AAAA,EAC7E;AAAA,EAEA,MAAM,eAAe,OAAc,KAA4C;AAC7E,UAAM,MAAoB,CAAC;AAC3B,eAAW,MAAM,KAAK;AACpB,YAAM,QAAQ,KAAK,YAAY,IAAI,cAAc,OAAO,EAAE,CAAC;AAC3D,UAAI,MAAO,KAAI,KAAK,MAAM,KAAK,CAAC;AAAA,IAClC;AACA,WAAO;AAAA,EACT;AAAA,EAEA,QAAc;AACZ,SAAK,QAAQ,MAAM;AACnB,SAAK,YAAY,MAAM;AAAA,EACzB;AACF;;;AChNO,SAAS,mBAA+B;AAC7C,SAAO,IAAI,mBAAmB;AAChC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/in-memory.ts","../src/index.ts"],"sourcesContent":["import type {\n Attachment,\n AttachmentId,\n Thread,\n ThreadId,\n ThreadListItem,\n ThreadStatus,\n} from '@airnauts/comments-core'\nimport {\n type AnchorPatch,\n decodeCursor,\n encodeCursor,\n type ListQuery,\n type ListResult,\n type NewComment,\n type NewThread,\n type Repository,\n type Scope,\n} from '@airnauts/comments-server'\n\ntype StoredThread = Thread & { projectId: string; env?: string }\n\n// Attachments are keyed by scope + id; the separators keep the three parts unambiguous.\nfunction attachmentKey(scope: Scope, id: AttachmentId): string {\n return `${scope.projectId}\u0000${scope.env ?? ''}\u0000${id}`\n}\n\nfunction clone<T>(value: T): T {\n return structuredClone(value)\n}\n\nfunction toListItem(t: StoredThread): ThreadListItem {\n // Strip server-only scope + thread-only payload (comments/captureContext/provenance),\n // but first project the root (earliest) comment into the preview field.\n const root = t.comments[0]\n const {\n comments: _c,\n captureContext: _cc,\n provenance: _p,\n projectId: _pid,\n env: _env,\n ...rest\n } = t as StoredThread & Record<string, unknown>\n return {\n ...(rest as ThreadListItem),\n rootComment: root ? { text: root.text, createdAt: root.createdAt } : null,\n }\n}\n\nfunction toThread(t: StoredThread): Thread {\n const { projectId: _pid, env: _env, ...wire } = t as StoredThread & Record<string, unknown>\n return wire as Thread\n}\n\nfunction matchesScope(t: StoredThread, scope: Scope): boolean {\n if (t.projectId !== scope.projectId) return false\n return (t.env ?? undefined) === (scope.env ?? undefined)\n}\n\nfunction unresolvedCountOf(thread: StoredThread): number {\n return thread.status === 'open' ? 1 : 0\n}\n\nfunction recomputeCounts(thread: StoredThread): StoredThread {\n return {\n ...thread,\n commentCount: thread.comments.length,\n unresolvedCount: unresolvedCountOf(thread),\n }\n}\n\nexport class InMemoryRepository implements Repository {\n private threads = new Map<string, StoredThread>()\n private attachments = new Map<string, Attachment>()\n\n async createThread(input: NewThread): Promise<Thread> {\n const stored: StoredThread = recomputeCounts({\n projectId: input.projectId,\n env: input.env,\n id: input.id,\n scope: input.scope,\n pageKey: input.pageKey,\n pageUrl: input.pageUrl,\n pageTitle: input.pageTitle,\n anchor: clone(input.anchor),\n status: input.status,\n anchorState: input.anchorState,\n selectionLost: input.selectionLost,\n captureContext: clone(input.captureContext),\n provenance: input.provenance ? clone(input.provenance) : undefined,\n createdBy: clone(input.createdBy),\n createdAt: input.createdAt,\n updatedAt: input.updatedAt,\n lastActivityAt: input.lastActivityAt,\n schemaVersion: input.schemaVersion,\n commentCount: 0, // placeholder, overwritten by recomputeCounts\n unresolvedCount: 0, // placeholder, overwritten by recomputeCounts\n comments: [clone(input.firstComment)],\n })\n this.threads.set(input.id, stored)\n return toThread(clone(stored))\n }\n\n async getThread(scope: Scope, id: ThreadId): Promise<Thread | null> {\n const t = this.threads.get(id)\n if (!t || !matchesScope(t, scope)) return null\n return toThread(clone(t))\n }\n\n async listThreads(query: ListQuery): Promise<ListResult> {\n const limit = Math.max(1, Math.min(query.limit, 200))\n const filtered: StoredThread[] = []\n for (const t of this.threads.values()) {\n if (!matchesScope(t, query)) continue\n if (query.pageKey !== undefined && t.pageKey !== query.pageKey) continue\n if (query.status !== undefined && t.status !== query.status) continue\n filtered.push(t)\n }\n filtered.sort((a, b) => {\n if (a.updatedAt !== b.updatedAt) return a.updatedAt < b.updatedAt ? 1 : -1\n return a.id < b.id ? 1 : a.id > b.id ? -1 : 0\n })\n\n let start = 0\n if (query.cursor) {\n const decoded = decodeCursor(query.cursor)\n if (decoded) {\n start = filtered.findIndex(\n (t) =>\n t.updatedAt < decoded.updatedAt ||\n (t.updatedAt === decoded.updatedAt && t.id < decoded.id),\n )\n if (start === -1) start = filtered.length\n }\n }\n const page = filtered.slice(start, start + limit)\n const last = page[page.length - 1]\n const more = start + limit < filtered.length\n const nextCursor =\n more && last ? encodeCursor({ updatedAt: last.updatedAt, id: last.id }) : null\n return {\n threads: page.map((t) => toListItem(clone(t))),\n nextCursor,\n }\n }\n\n async addComment(scope: Scope, threadId: ThreadId, comment: NewComment): Promise<NewComment> {\n const t = this.threads.get(threadId)\n if (!t || !matchesScope(t, scope)) throw new Error('thread not found')\n const next: StoredThread = recomputeCounts({\n ...t,\n comments: [...t.comments, clone(comment)],\n updatedAt: comment.createdAt,\n lastActivityAt: comment.createdAt,\n })\n this.threads.set(threadId, next)\n return clone(comment)\n }\n\n async setStatus(\n scope: Scope,\n threadId: ThreadId,\n status: ThreadStatus,\n now: string,\n ): Promise<Thread> {\n const t = this.threads.get(threadId)\n if (!t || !matchesScope(t, scope)) throw new Error('thread not found')\n const next: StoredThread = recomputeCounts({\n ...t,\n status,\n updatedAt: now,\n lastActivityAt: now,\n })\n this.threads.set(threadId, next)\n return toThread(clone(next))\n }\n\n async updateAnchor(\n scope: Scope,\n threadId: ThreadId,\n patch: AnchorPatch,\n now: string,\n ): Promise<ThreadListItem> {\n const t = this.threads.get(threadId)\n if (!t || !matchesScope(t, scope)) throw new Error('thread not found')\n const nextAnchor = {\n ...t.anchor,\n selectors: patch.selectors ?? t.anchor.selectors,\n signals: patch.signals ?? t.anchor.signals,\n }\n const next: StoredThread = recomputeCounts({\n ...t,\n anchor: nextAnchor,\n anchorState: patch.anchorState,\n selectionLost: patch.selectionLost ?? t.selectionLost,\n updatedAt: now,\n lastActivityAt: now,\n })\n this.threads.set(threadId, next)\n return toListItem(clone(next))\n }\n\n async putAttachment(scope: Scope, attachment: Attachment): Promise<void> {\n this.attachments.set(attachmentKey(scope, attachment.id), clone(attachment))\n }\n\n async getAttachments(scope: Scope, ids: AttachmentId[]): Promise<Attachment[]> {\n const out: Attachment[] = []\n for (const id of ids) {\n const found = this.attachments.get(attachmentKey(scope, id))\n if (found) out.push(clone(found))\n }\n return out\n }\n\n reset(): void {\n this.threads.clear()\n this.attachments.clear()\n }\n}\n","import type { Repository } from '@airnauts/comments-server'\nimport { InMemoryRepository } from './in-memory'\n\nexport { InMemoryRepository }\n\n/** Fresh, process-local in-memory `Repository`. No connection, no config. */\nexport function memoryRepository(): Repository {\n return new InMemoryRepository()\n}\n"],"mappings":";AAQA;AAAA,EAEE;AAAA,EACA;AAAA,OAOK;AAKP,SAAS,cAAc,OAAc,IAA0B;AAC7D,SAAO,GAAG,MAAM,SAAS,KAAI,MAAM,OAAO,EAAE,KAAI,EAAE;AACpD;AAEA,SAAS,MAAS,OAAa;AAC7B,SAAO,gBAAgB,KAAK;AAC9B;AAEA,SAAS,WAAW,GAAiC;AAGnD,QAAM,OAAO,EAAE,SAAS,CAAC;AACzB,QAAM;AAAA,IACJ,UAAU;AAAA,IACV,gBAAgB;AAAA,IAChB,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,KAAK;AAAA,IACL,GAAG;AAAA,EACL,IAAI;AACJ,SAAO;AAAA,IACL,GAAI;AAAA,IACJ,aAAa,OAAO,EAAE,MAAM,KAAK,MAAM,WAAW,KAAK,UAAU,IAAI;AAAA,EACvE;AACF;AAEA,SAAS,SAAS,GAA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AA,MACvC,SAAS,MAAM,WAAW,EAAE,OAAO;AAAA,IACrC;AACA,UAAM,OAAqB,gBAAgB;AAAA,MACzC,GAAG;AAAA,MACH,QAAQ;AAAA,MACR,aAAa,MAAM;AAAA,MACnB,eAAe,MAAM,iBAAiB,EAAE;AAAA,MACxC,WAAW;AAAA,MACX,gBAAgB;AAAA,IAClB,CAAC;AACD,SAAK,QAAQ,IAAI,UAAU,IAAI;AAC/B,WAAO,WAAW,MAAM,IAAI,CAAC;AAAA,EAC/B;AAAA,EAEA,MAAM,cAAc,OAAc,YAAuC;AACvE,SAAK,YAAY,IAAI,cAAc,OAAO,WAAW,EAAE,GAAG,MAAM,UAAU,CAAC;AAAA,EAC7E;AAAA,EAEA,MAAM,eAAe,OAAc,KAA4C;AAC7E,UAAM,MAAoB,CAAC;AAC3B,eAAW,MAAM,KAAK;AACpB,YAAM,QAAQ,KAAK,YAAY,IAAI,cAAc,OAAO,EAAE,CAAC;AAC3D,UAAI,MAAO,KAAI,KAAK,MAAM,KAAK,CAAC;AAAA,IAClC;AACA,WAAO;AAAA,EACT;AAAA,EAEA,QAAc;AACZ,SAAK,QAAQ,MAAM;AACnB,SAAK,YAAY,MAAM;AAAA,EACzB;AACF;;;ACrNO,SAAS,mBAA+B;AAC7C,SAAO,IAAI,mBAAmB;AAChC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@airnauts/comments-adapter-memory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "In-memory repository adapter for the Airnauts commenting tool server.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"comments",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@airnauts/comments-core": "^0.
|
|
43
|
-
"@airnauts/comments-server": "^0.
|
|
42
|
+
"@airnauts/comments-core": "^0.4.0",
|
|
43
|
+
"@airnauts/comments-server": "^0.4.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@airnauts/comments-test-support": "0.0.0"
|