@clawchatsai/connector 0.2.6 → 0.2.7

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/dist/index.js CHANGED
@@ -16,7 +16,7 @@ import * as os from 'node:os';
16
16
  import * as path from 'node:path';
17
17
  import { createRequire } from 'node:module';
18
18
  import { SignalingClient } from './signaling-client.js';
19
- import { dispatchRpc } from './shim.js';
19
+ import { dispatchRpc, rpcResponseBody } from './shim.js';
20
20
  import { attachBrowserChannel, revokeStaleChannels, loadAuthState } from './auth-handler.js';
21
21
  import { readConfig, writeConfig, updateConfig, readAuthConfig, removeBackupCode, writeFileAtomic, addDevice, touchDevice } from './config-store.js';
22
22
  import { listDevices, revokeDevice, showFingerprint } from './devices-cli.js';
@@ -788,24 +788,7 @@ async function handleRpcMessage(dc, msg, ctx) {
788
788
  };
789
789
  try {
790
790
  const response = await dispatchRpc(rpcReq, app.handleRequest);
791
- // For binary content types (images, audio, etc.), wrap as _binary envelope
792
- // so the browser transport can reconstruct a proper Blob with correct MIME type
793
- const contentType = response.headers['content-type'] || '';
794
- const isBinaryResponse = /^(image|audio|video|application\/octet-stream|application\/pdf)/.test(contentType);
795
- let responseBody;
796
- if (isBinaryResponse && response.rawBody) {
797
- // Encode raw bytes as base64 in a _binary envelope.
798
- // The transport layer on the browser side already handles this format,
799
- // reconstructing a proper Blob with the correct MIME type.
800
- responseBody = {
801
- _binary: true,
802
- contentType,
803
- data: response.rawBody.toString('base64'),
804
- };
805
- }
806
- else {
807
- responseBody = response.body;
808
- }
791
+ const responseBody = rpcResponseBody(response);
809
792
  const responseMsg = {
810
793
  type: 'rpc-res',
811
794
  id: response.id,
package/dist/shim.d.ts CHANGED
@@ -55,6 +55,13 @@ declare class FakeRes extends Writable {
55
55
  writeHead(statusCode: number, headers?: Record<string, string | number>): this;
56
56
  end(chunk?: unknown, encoding?: unknown, _cb?: unknown): this;
57
57
  }
58
+ export declare function isTextContentType(contentType: string): boolean;
59
+ /**
60
+ * The rpc-res body for a response: the text as-is, or a `{ _binary, contentType, data }` envelope
61
+ * (base64) that the browser transport turns back into a Blob. Text goes as text only when its bytes
62
+ * are valid UTF-8, so a Latin-1 CSV still arrives byte-exact. No body or no content type: text.
63
+ */
64
+ export declare function rpcResponseBody(response: RpcResponse): unknown;
58
65
  /**
59
66
  * Dispatch an RPC request through handleRequest and return the response.
60
67
  */
package/dist/shim.js CHANGED
@@ -140,6 +140,33 @@ function tryJsonParse(buf) {
140
140
  return buf.toString('base64');
141
141
  }
142
142
  }
143
+ // Content types sent over the DataChannel as a UTF-8 string; everything else is base64-wrapped.
144
+ // An allowlist, not a binary list: an unknown type (zip, docx, kmz, …) decoded as UTF-8 turns every
145
+ // byte >= 0x80 into U+FFFD. image/svg+xml stays binary: the browser builds typed Blobs from it.
146
+ const TEXT_CONTENT_TYPE = /^(text\/|application\/(json|xml|javascript|x-www-form-urlencoded|yaml)$|application\/[^/]+\+(json|xml)$)/;
147
+ const strictUtf8 = new TextDecoder('utf-8', { fatal: true });
148
+ export function isTextContentType(contentType) {
149
+ return TEXT_CONTENT_TYPE.test(contentType.split(';')[0].trim().toLowerCase());
150
+ }
151
+ /**
152
+ * The rpc-res body for a response: the text as-is, or a `{ _binary, contentType, data }` envelope
153
+ * (base64) that the browser transport turns back into a Blob. Text goes as text only when its bytes
154
+ * are valid UTF-8, so a Latin-1 CSV still arrives byte-exact. No body or no content type: text.
155
+ */
156
+ export function rpcResponseBody(response) {
157
+ const contentType = response.headers['content-type'] || '';
158
+ const raw = response.rawBody;
159
+ if (!contentType || !raw?.length)
160
+ return response.body;
161
+ if (isTextContentType(contentType)) {
162
+ try {
163
+ strictUtf8.decode(raw);
164
+ return response.body;
165
+ }
166
+ catch { /* not UTF-8: send the bytes */ }
167
+ }
168
+ return { _binary: true, contentType, data: raw.toString('base64') };
169
+ }
143
170
  /**
144
171
  * Dispatch an RPC request through handleRequest and return the response.
145
172
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",