@typerighter/rpc-client 0.39.0 → 0.40.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/.gitignore ADDED
@@ -0,0 +1 @@
1
+ dist/
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@typerighter/rpc-client",
4
- "version": "0.39.0",
4
+ "version": "0.40.0",
5
5
  "description": "JSON-RPC client for Typedown over TCP/stdio.",
6
6
  "repository": {
7
7
  "type": "git",
package/src/index.ts CHANGED
@@ -99,6 +99,8 @@ export interface TdSchemaInfo {
99
99
  export interface TdSiteConfig {
100
100
  version: string;
101
101
  basePath: string;
102
+ origin: string | undefined;
103
+ lang: string;
102
104
  rootDir: string;
103
105
  siteTitle: string;
104
106
  siteDescription: string;
@@ -109,6 +111,12 @@ export interface TdSiteConfig {
109
111
  nav: TdNavItem[];
110
112
  }
111
113
 
114
+ export interface TdPageMeta {
115
+ title?: string;
116
+ description?: string;
117
+ image?: string;
118
+ }
119
+
112
120
  export interface TdBuiltResource {
113
121
  schema?: string;
114
122
  schemaLabel?: string;
@@ -119,6 +127,7 @@ export interface TdBuiltResource {
119
127
  headings: TdHeading[];
120
128
  title?: string;
121
129
  metadata: TdFileMetadata;
130
+ meta?: TdPageMeta;
122
131
  }
123
132
 
124
133
  // Server-reserved JSON-RPC error code for cancelled queries (-32000 to -32099)
@@ -1,54 +0,0 @@
1
- /**
2
- * JSON-RPC 2.0 client with Content-Length framing
3
- * Simplified from vscode-jsonrpc (MIT, Microsoft Corporation)
4
- * Ref: https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L506-L591
5
- */
6
- import type { Readable, Writable } from 'node:stream';
7
- interface Disposable {
8
- dispose(): void;
9
- }
10
- /**
11
- * Simplified MessageConnection interface
12
- * https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L506-L591
13
- */
14
- export interface MessageConnection extends Disposable {
15
- /** Send a JSON-RPC request and wait for the response */
16
- sendRequest<R>(method: string, params?: any[] | object): Promise<R>;
17
- /** Send a one-way JSON-RPC notification (no response expected) */
18
- sendNotification(method: string, params?: any[] | object): void;
19
- /** Register a handler for server-sent notifications, returns an unsubscribe handle */
20
- onNotification(method: string, handler: (params: any) => void): Disposable;
21
- /** Register a listener for connection close, returns an unsubscribe handle */
22
- onClose(listener: () => void): Disposable;
23
- /** Reject all pending requests and stop listening */
24
- dispose(): void;
25
- }
26
- export declare class JsonRpcClient implements MessageConnection {
27
- private reader;
28
- private writer;
29
- private sequenceNumber;
30
- private responsePromises;
31
- private notificationHandlers;
32
- private chunks;
33
- private chunksLength;
34
- private contentLength;
35
- private disposed;
36
- private closeHandlers;
37
- constructor(reader: Readable, writer: Writable);
38
- static connectTcp(host: string, port: number): Promise<JsonRpcClient>;
39
- static connectStdio(stdin: Readable, stdout: Writable): JsonRpcClient;
40
- dispose(): void;
41
- onClose(listener: () => void): Disposable;
42
- sendRequest<R>(method: string, params?: any[] | object): Promise<R>;
43
- sendNotification(method: string, params?: any[] | object): void;
44
- onNotification(method: string, handler: (params: any) => void): Disposable;
45
- private onData;
46
- private handleResponse;
47
- private handleNotification;
48
- private rejectAllPending;
49
- private handleStreamClose;
50
- private writeMessage;
51
- private compact;
52
- private consume;
53
- }
54
- export {};
@@ -1,209 +0,0 @@
1
- /**
2
- * JSON-RPC 2.0 client with Content-Length framing
3
- * Simplified from vscode-jsonrpc (MIT, Microsoft Corporation)
4
- * Ref: https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L506-L591
5
- */
6
- import { createConnection } from 'node:net';
7
- import { Message as MessageGuards, ErrorCodes, ResponseError } from './types.js';
8
- const HEADER_SEPARATOR = Buffer.from('\r\n\r\n', 'ascii');
9
- const HEADER_SEPARATOR_LENGTH = 4; // \r\n\r\n
10
- export class JsonRpcClient {
11
- // Transport
12
- reader;
13
- writer;
14
- // Request/response matching
15
- sequenceNumber = 1;
16
- responsePromises = new Map();
17
- notificationHandlers = new Map();
18
- // Inbound buffer, raw chunks to avoid corrupting multi-byte UTF-8
19
- chunks = [];
20
- chunksLength = 0;
21
- contentLength = -1; // -1 = waiting for header
22
- // Lifecycle
23
- disposed = false;
24
- closeHandlers = [];
25
- constructor(reader, writer) {
26
- this.reader = reader;
27
- this.writer = writer;
28
- this.reader.on('data', (chunk) => this.onData(chunk));
29
- this.reader.on('end', () => this.handleStreamClose());
30
- this.reader.on('close', () => this.handleStreamClose());
31
- }
32
- static connectTcp(host, port) {
33
- return new Promise((resolve, reject) => {
34
- const socket = createConnection({ host, port }, () => {
35
- resolve(new JsonRpcClient(socket, socket));
36
- });
37
- socket.on('error', reject);
38
- });
39
- }
40
- static connectStdio(stdin, stdout) {
41
- return new JsonRpcClient(stdin, stdout);
42
- }
43
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L1563-L1590
44
- dispose() {
45
- if (this.disposed)
46
- return;
47
- this.disposed = true;
48
- this.rejectAllPending('Connection disposed');
49
- // Close the streams so the server detects the disconnect
50
- this.reader.destroy();
51
- this.writer.destroy();
52
- }
53
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L565
54
- onClose(listener) {
55
- this.closeHandlers.push(listener);
56
- return {
57
- dispose: () => {
58
- const index = this.closeHandlers.indexOf(listener);
59
- if (index >= 0)
60
- this.closeHandlers.splice(index, 1);
61
- },
62
- };
63
- }
64
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L537
65
- sendRequest(method, params) {
66
- const id = this.sequenceNumber++;
67
- const message = { jsonrpc: '2.0', id, method, params };
68
- return new Promise((resolve, reject) => {
69
- this.responsePromises.set(id, { resolve: resolve, reject });
70
- this.writeMessage(message);
71
- });
72
- }
73
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L549
74
- sendNotification(method, params) {
75
- const message = { jsonrpc: '2.0', method, params };
76
- this.writeMessage(message);
77
- }
78
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/connection.ts#L557
79
- onNotification(method, handler) {
80
- const handlers = this.notificationHandlers.get(method) ?? [];
81
- handlers.push(handler);
82
- this.notificationHandlers.set(method, handlers);
83
- return {
84
- dispose: () => {
85
- const list = this.notificationHandlers.get(method);
86
- if (list) {
87
- const idx = list.indexOf(handler);
88
- if (idx !== -1)
89
- list.splice(idx, 1);
90
- }
91
- },
92
- };
93
- }
94
- // Accumulate chunk and parse as many complete messages as possible
95
- // A single chunk can contain multiple messages
96
- // Ref: vscode-jsonrpc ReadableStreamMessageReader.nextMessage
97
- onData(chunk) {
98
- this.chunks.push(chunk);
99
- this.chunksLength += chunk.length;
100
- while (true) {
101
- if (this.contentLength < 0) {
102
- const buffer = this.compact();
103
- const separatorIndex = buffer.indexOf(HEADER_SEPARATOR);
104
- if (separatorIndex < 0)
105
- return;
106
- const header = buffer.subarray(0, separatorIndex).toString('ascii');
107
- const match = header.match(/Content-Length:\s*(\d+)/i);
108
- if (!match) {
109
- this.consume(separatorIndex + HEADER_SEPARATOR_LENGTH);
110
- continue;
111
- }
112
- this.contentLength = Number(match[1]);
113
- this.consume(separatorIndex + HEADER_SEPARATOR_LENGTH);
114
- }
115
- if (this.chunksLength < this.contentLength)
116
- return;
117
- const buffer = this.compact();
118
- const body = buffer.subarray(0, this.contentLength).toString('utf-8');
119
- this.consume(this.contentLength);
120
- this.contentLength = -1;
121
- try {
122
- const message = JSON.parse(body);
123
- if (MessageGuards.isResponse(message)) {
124
- this.handleResponse(message);
125
- }
126
- else if (MessageGuards.isNotification(message)) {
127
- this.handleNotification(message);
128
- }
129
- }
130
- catch (error) {
131
- console.error('[jsonrpc] Failed to parse message:', error);
132
- }
133
- }
134
- }
135
- // Ref: vscode-jsonrpc connection.ts
136
- handleResponse(response) {
137
- if (typeof response.id !== 'number')
138
- return;
139
- const pending = this.responsePromises.get(response.id);
140
- if (!pending)
141
- return;
142
- this.responsePromises.delete(response.id);
143
- if (response.error) {
144
- pending.reject(new ResponseError(response.error.code, response.error.message, response.error.data));
145
- }
146
- else if (response.result !== undefined) {
147
- pending.resolve(response.result);
148
- }
149
- }
150
- handleNotification(notification) {
151
- const handlers = this.notificationHandlers.get(notification.method);
152
- if (!handlers?.length)
153
- return;
154
- for (const handler of handlers) {
155
- try {
156
- handler(notification.params);
157
- }
158
- catch (error) {
159
- console.error(`[jsonrpc] Notification handler for '${notification.method}' threw:`, error);
160
- }
161
- }
162
- }
163
- // Cleanup
164
- rejectAllPending(reason) {
165
- for (const [, pending] of this.responsePromises) {
166
- pending.reject(new ResponseError(ErrorCodes.PendingResponseRejected, reason));
167
- }
168
- this.responsePromises.clear();
169
- }
170
- // Fires once on stream end/close
171
- handleStreamClose() {
172
- if (this.disposed)
173
- return;
174
- this.disposed = true;
175
- this.rejectAllPending('Server disconnected');
176
- for (const handler of this.closeHandlers) {
177
- handler();
178
- }
179
- }
180
- /* Helpers */
181
- // Single buffer write to prevent interleaving under concurrent requests
182
- writeMessage(message) {
183
- const body = Buffer.from(JSON.stringify(message), 'utf-8');
184
- const header = Buffer.from(`Content-Length: ${body.length}\r\n\r\n`, 'ascii');
185
- this.writer.write(Buffer.concat([header, body]));
186
- }
187
- // Concat all chunks into 1 buffer
188
- compact() {
189
- if (this.chunks.length === 1)
190
- return this.chunks[0];
191
- const buffer = Buffer.concat(this.chunks);
192
- this.chunks = [buffer];
193
- this.chunksLength = buffer.length;
194
- return buffer;
195
- }
196
- // Consume n bytes from buffer
197
- consume(bytes) {
198
- const buffer = this.compact();
199
- const remaining = buffer.subarray(bytes);
200
- if (remaining.length > 0) {
201
- this.chunks = [remaining];
202
- this.chunksLength = remaining.length;
203
- }
204
- else {
205
- this.chunks = [];
206
- this.chunksLength = 0;
207
- }
208
- }
209
- }
@@ -1,2 +0,0 @@
1
- export { JsonRpcClient, type MessageConnection } from './client.js';
2
- export { type RequestMessage, type ResponseMessage, type NotificationMessage, type ResponseErrorLiteral, Message, ErrorCodes, ResponseError, } from './types.js';
@@ -1,2 +0,0 @@
1
- export { JsonRpcClient } from './client.js';
2
- export { Message, ErrorCodes, ResponseError, } from './types.js';
@@ -1,53 +0,0 @@
1
- /**
2
- * JSON-RPC 2.0 message types and error codes
3
- * Copied from vscode-jsonrpc (MIT, Microsoft Corporation)
4
- * Ref: https://github.com/microsoft/vscode-languageserver-node/tree/main/jsonrpc/src/common/messages
5
- */
6
- export interface Message {
7
- jsonrpc: string;
8
- }
9
- export interface RequestMessage extends Message {
10
- id: number | string | null;
11
- method: string;
12
- params?: any[] | object;
13
- }
14
- export interface ResponseMessage extends Message {
15
- id: number | string | null;
16
- result?: string | number | boolean | object | any[] | null;
17
- error?: ResponseErrorLiteral<any>;
18
- }
19
- export interface NotificationMessage extends Message {
20
- method: string;
21
- params?: any[] | object;
22
- }
23
- export interface ResponseErrorLiteral<D = void> {
24
- code: number;
25
- message: string;
26
- data?: D;
27
- }
28
- export declare namespace Message {
29
- function isRequest(message: Message | undefined): message is RequestMessage;
30
- function isNotification(message: Message | undefined): message is NotificationMessage;
31
- function isResponse(message: Message | undefined): message is ResponseMessage;
32
- }
33
- export declare namespace ErrorCodes {
34
- const ParseError: -32700;
35
- const InvalidRequest: -32600;
36
- const MethodNotFound: -32601;
37
- const InvalidParams: -32602;
38
- const InternalError: -32603;
39
- const jsonrpcReservedErrorRangeStart: -32099;
40
- const MessageWriteError: -32099;
41
- const MessageReadError: -32098;
42
- const PendingResponseRejected: -32097;
43
- const ConnectionInactive: -32096;
44
- const ServerNotInitialized: -32002;
45
- const UnknownErrorCode: -32001;
46
- const jsonrpcReservedErrorRangeEnd: -32000;
47
- }
48
- export declare class ResponseError<D = void> extends Error {
49
- readonly code: number;
50
- readonly data: D | undefined;
51
- constructor(code: number, message: string, data?: D);
52
- toJson(): ResponseErrorLiteral<D>;
53
- }
@@ -1,67 +0,0 @@
1
- /**
2
- * JSON-RPC 2.0 message types and error codes
3
- * Copied from vscode-jsonrpc (MIT, Microsoft Corporation)
4
- * Ref: https://github.com/microsoft/vscode-languageserver-node/tree/main/jsonrpc/src/common/messages
5
- */
6
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/messages.ts#L298-L323
7
- export var Message;
8
- (function (Message) {
9
- function isRequest(message) {
10
- const candidate = message;
11
- return candidate !== undefined && typeof candidate.method === 'string'
12
- && (typeof candidate.id === 'string' || typeof candidate.id === 'number');
13
- }
14
- Message.isRequest = isRequest;
15
- function isNotification(message) {
16
- const candidate = message;
17
- return candidate !== undefined && typeof candidate.method === 'string'
18
- && candidate.id === undefined;
19
- }
20
- Message.isNotification = isNotification;
21
- function isResponse(message) {
22
- const candidate = message;
23
- return candidate !== undefined
24
- && (candidate.result !== undefined || !!candidate.error)
25
- && (typeof candidate.id === 'string' || typeof candidate.id === 'number' || candidate.id === null);
26
- }
27
- Message.isResponse = isResponse;
28
- })(Message || (Message = {}));
29
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/messages.ts#L32-L83
30
- export var ErrorCodes;
31
- (function (ErrorCodes) {
32
- ErrorCodes.ParseError = -32700;
33
- ErrorCodes.InvalidRequest = -32600;
34
- ErrorCodes.MethodNotFound = -32601;
35
- ErrorCodes.InvalidParams = -32602;
36
- ErrorCodes.InternalError = -32603;
37
- // JSON-RPC reserved error range (-32099 to -32000)
38
- ErrorCodes.jsonrpcReservedErrorRangeStart = -32099;
39
- ErrorCodes.MessageWriteError = -32099;
40
- ErrorCodes.MessageReadError = -32098;
41
- ErrorCodes.PendingResponseRejected = -32097;
42
- ErrorCodes.ConnectionInactive = -32096;
43
- ErrorCodes.ServerNotInitialized = -32002;
44
- ErrorCodes.UnknownErrorCode = -32001;
45
- ErrorCodes.jsonrpcReservedErrorRangeEnd = -32000;
46
- })(ErrorCodes || (ErrorCodes = {}));
47
- // https://github.com/microsoft/vscode-languageserver-node/blob/5010cdf9822e1038a30ee7eb6ee5d7aaa79acc4a/jsonrpc/src/common/messages.ts#L128-L148
48
- export class ResponseError extends Error {
49
- code;
50
- data;
51
- constructor(code, message, data) {
52
- super(message);
53
- this.code = typeof code === 'number' ? code : ErrorCodes.UnknownErrorCode;
54
- this.data = data;
55
- Object.setPrototypeOf(this, ResponseError.prototype);
56
- }
57
- toJson() {
58
- const result = {
59
- code: this.code,
60
- message: this.message,
61
- };
62
- if (this.data !== undefined) {
63
- result.data = this.data;
64
- }
65
- return result;
66
- }
67
- }
package/dist/index.d.ts DELETED
@@ -1,137 +0,0 @@
1
- /**
2
- * Typedown RPC client
3
- *
4
- * Thin wrapper over JsonRpcClient that provides typed methods
5
- * matching the Rust RPC server's contract (rpc/contract.rs)
6
- *
7
- * The server (typedown-rpc binary) sends notifications for FS changes;
8
- * register handlers via onContentChanged/onSchemaChanged/onConfigChanged
9
- */
10
- import type { Readable, Writable } from 'node:stream';
11
- export { JsonRpcClient, Message, ResponseError, ErrorCodes } from './core/index.js';
12
- export type { RequestMessage, ResponseMessage, NotificationMessage, ResponseErrorLiteral } from './core/index.js';
13
- export interface TdDiagnosticItem {
14
- filepath: string;
15
- line: number;
16
- column: number;
17
- severity: string;
18
- code: string;
19
- message: string;
20
- }
21
- export interface TdContentNotification {
22
- filepath: string;
23
- affectedFiles?: string[];
24
- }
25
- export interface TdFileMetadata {
26
- mtime: number;
27
- ctime: number;
28
- }
29
- export interface TdHeading {
30
- level: number;
31
- title: string;
32
- titleHtml: string;
33
- slug: string;
34
- }
35
- export interface TdSidebarItem {
36
- filepath: string;
37
- schema?: string;
38
- schemaLabel?: string;
39
- label?: string;
40
- icon?: TdIcon;
41
- excerpt?: string;
42
- metadata: TdFileMetadata;
43
- }
44
- export interface TdContentSummary {
45
- filepath: string;
46
- schema?: string;
47
- schemaLabel?: string;
48
- label?: string;
49
- icon?: TdIcon;
50
- header: Record<string, any>;
51
- excerpt?: string;
52
- metadata: TdFileMetadata;
53
- }
54
- export interface TdNavItem {
55
- title: string;
56
- link: string;
57
- icon?: string;
58
- }
59
- export interface TdIcon {
60
- name: string;
61
- }
62
- export interface TdDiagnosticReport {
63
- diagnostics: TdDiagnosticItem[];
64
- fileCount: number;
65
- errorCount: number;
66
- warningCount: number;
67
- }
68
- export interface TdFormatResult {
69
- content: string;
70
- changed: boolean;
71
- }
72
- export interface TdSchemaNotification {
73
- schema: string;
74
- }
75
- export interface TdSchemaInfo {
76
- schema: string;
77
- label: string;
78
- properties: Record<string, any>;
79
- }
80
- export interface TdSiteConfig {
81
- version: string;
82
- basePath: string;
83
- rootDir: string;
84
- siteTitle: string;
85
- siteDescription: string;
86
- repo: string | undefined;
87
- author: string | undefined;
88
- license: string | undefined;
89
- publicDir: string;
90
- nav: TdNavItem[];
91
- }
92
- export interface TdBuiltResource {
93
- schema?: string;
94
- schemaLabel?: string;
95
- label?: string;
96
- icon?: TdIcon;
97
- header: Record<string, any>;
98
- content: string;
99
- headings: TdHeading[];
100
- title?: string;
101
- metadata: TdFileMetadata;
102
- }
103
- export declare const RPC_CANCELLED_CODE = -32002;
104
- /**
105
- * Typed RPC client for the Typedown build server
106
- *
107
- * Wraps a generic JsonRpcClient with typed request methods
108
- * and notification handlers matching the Rust RPC contract
109
- */
110
- export declare class RpcClient {
111
- private rpc;
112
- private constructor();
113
- static connectTcp(host: string, port: number): Promise<RpcClient>;
114
- static connectStdio(stdin: Readable, stdout: Writable): RpcClient;
115
- /** Send exit notification and close the connection
116
- * The detached server will detect the disconnect and save its cache */
117
- dispose(): void;
118
- onClose(callback: () => void): {
119
- dispose: () => void;
120
- };
121
- requestFile(path: string): Promise<TdBuiltResource>;
122
- requestFiles(paths: string[]): Promise<TdBuiltResource[]>;
123
- listVault(): Promise<string[]>;
124
- listFilesGroupedBySchema(): Promise<Record<string, TdContentSummary[]>>;
125
- listSidebar(): Promise<TdSidebarItem[]>;
126
- listSchemas(): Promise<string[]>;
127
- getSchema(schema: string): Promise<TdSchemaInfo>;
128
- getVersion(): Promise<string>;
129
- getConfig(): Promise<TdSiteConfig>;
130
- checkVault(): Promise<TdDiagnosticReport>;
131
- formatFile(path: string): Promise<TdFormatResult>;
132
- onContentUpdated(callback: (notification: TdContentNotification) => void): void;
133
- onContentDeleted(callback: (notification: TdContentNotification) => void): void;
134
- onSchemaUpdated(callback: (notification: TdSchemaNotification) => void): void;
135
- onSchemaDeleted(callback: (notification: TdSchemaNotification) => void): void;
136
- onConfigChanged(callback: (config: TdSiteConfig) => void): void;
137
- }