@blue-soda/dsh-remote 0.4.28
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/LICENSE +29 -0
- package/README.md +81 -0
- package/bin/ds-harness-remote.js +11 -0
- package/cordis.patch.yml +32 -0
- package/dist/acp.d.ts +75 -0
- package/dist/acp.d.ts.map +1 -0
- package/dist/acp.js +61 -0
- package/dist/acp.js.map +1 -0
- package/dist/api-proxy-switch.d.ts +33 -0
- package/dist/api-proxy-switch.d.ts.map +1 -0
- package/dist/api-proxy-switch.js +109 -0
- package/dist/api-proxy-switch.js.map +1 -0
- package/dist/bin/doctor.d.ts +3 -0
- package/dist/bin/doctor.d.ts.map +1 -0
- package/dist/bin/doctor.js +13 -0
- package/dist/bin/doctor.js.map +1 -0
- package/dist/cli.d.ts +32 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +289 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-runtime.d.ts +239 -0
- package/dist/client-runtime.d.ts.map +1 -0
- package/dist/client-runtime.js +1391 -0
- package/dist/client-runtime.js.map +1 -0
- package/dist/client-secure-transport.d.ts +23 -0
- package/dist/client-secure-transport.d.ts.map +1 -0
- package/dist/client-secure-transport.js +143 -0
- package/dist/client-secure-transport.js.map +1 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.github.js +4231 -0
- package/dist/client.js +4231 -0
- package/dist/codex/app-server.d.ts +71 -0
- package/dist/codex/app-server.d.ts.map +1 -0
- package/dist/codex/app-server.js +268 -0
- package/dist/codex/app-server.js.map +1 -0
- package/dist/codex/codex-image-codec.d.ts +7 -0
- package/dist/codex/codex-image-codec.d.ts.map +1 -0
- package/dist/codex/codex-image-codec.js +288 -0
- package/dist/codex/codex-image-codec.js.map +1 -0
- package/dist/codex/domain.d.ts +93 -0
- package/dist/codex/domain.d.ts.map +1 -0
- package/dist/codex/domain.js +1282 -0
- package/dist/codex/domain.js.map +1 -0
- package/dist/codex/method-policy.d.ts +338 -0
- package/dist/codex/method-policy.d.ts.map +1 -0
- package/dist/codex/method-policy.js +141 -0
- package/dist/codex/method-policy.js.map +1 -0
- package/dist/codex/peer-bridge.d.ts +57 -0
- package/dist/codex/peer-bridge.d.ts.map +1 -0
- package/dist/codex/peer-bridge.js +299 -0
- package/dist/codex/peer-bridge.js.map +1 -0
- package/dist/codex/permissions.d.ts +4 -0
- package/dist/codex/permissions.d.ts.map +1 -0
- package/dist/codex/permissions.js +21 -0
- package/dist/codex/permissions.js.map +1 -0
- package/dist/codex/session-id.d.ts +10 -0
- package/dist/codex/session-id.d.ts.map +1 -0
- package/dist/codex/session-id.js +15 -0
- package/dist/codex/session-id.js.map +1 -0
- package/dist/codex/virtual-harness.d.ts +209 -0
- package/dist/codex/virtual-harness.d.ts.map +1 -0
- package/dist/codex/virtual-harness.js +2616 -0
- package/dist/codex/virtual-harness.js.map +1 -0
- package/dist/codex-workspace-bridge.d.ts +108 -0
- package/dist/codex-workspace-bridge.d.ts.map +1 -0
- package/dist/codex-workspace-bridge.js +502 -0
- package/dist/codex-workspace-bridge.js.map +1 -0
- package/dist/config.d.ts +99 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +127 -0
- package/dist/config.js.map +1 -0
- package/dist/connection-controller.d.ts +44 -0
- package/dist/connection-controller.d.ts.map +1 -0
- package/dist/connection-controller.js +185 -0
- package/dist/connection-controller.js.map +1 -0
- package/dist/control-route.d.ts +34 -0
- package/dist/control-route.d.ts.map +1 -0
- package/dist/control-route.js +136 -0
- package/dist/control-route.js.map +1 -0
- package/dist/control-runtime.d.ts +138 -0
- package/dist/control-runtime.d.ts.map +1 -0
- package/dist/control-runtime.js +543 -0
- package/dist/control-runtime.js.map +1 -0
- package/dist/control-stream.d.ts +67 -0
- package/dist/control-stream.d.ts.map +1 -0
- package/dist/control-stream.js +179 -0
- package/dist/control-stream.js.map +1 -0
- package/dist/defaults.d.ts +9 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +9 -0
- package/dist/defaults.js.map +1 -0
- package/dist/file-viewer-bridge.d.ts +30 -0
- package/dist/file-viewer-bridge.d.ts.map +1 -0
- package/dist/file-viewer-bridge.js +128 -0
- package/dist/file-viewer-bridge.js.map +1 -0
- package/dist/file-viewer-contract.d.ts +5 -0
- package/dist/file-viewer-contract.d.ts.map +1 -0
- package/dist/file-viewer-contract.js +4 -0
- package/dist/file-viewer-contract.js.map +1 -0
- package/dist/harness-api-bridge.d.ts +79 -0
- package/dist/harness-api-bridge.d.ts.map +1 -0
- package/dist/harness-api-bridge.js +852 -0
- package/dist/harness-api-bridge.js.map +1 -0
- package/dist/harness-api-history.d.ts +3 -0
- package/dist/harness-api-history.d.ts.map +1 -0
- package/dist/harness-api-history.js +59 -0
- package/dist/harness-api-history.js.map +1 -0
- package/dist/harness-remote-bridge.d.ts +52 -0
- package/dist/harness-remote-bridge.d.ts.map +1 -0
- package/dist/harness-remote-bridge.js +529 -0
- package/dist/harness-remote-bridge.js.map +1 -0
- package/dist/harness-version.d.ts +23 -0
- package/dist/harness-version.d.ts.map +1 -0
- package/dist/harness-version.js +95 -0
- package/dist/harness-version.js.map +1 -0
- package/dist/identity-store.d.ts +45 -0
- package/dist/identity-store.d.ts.map +1 -0
- package/dist/identity-store.js +183 -0
- package/dist/identity-store.js.map +1 -0
- package/dist/ids.d.ts +2 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +17 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28870 -0
- package/dist/index.js.map +7 -0
- package/dist/logging.d.ts +18 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +30 -0
- package/dist/logging.js.map +1 -0
- package/dist/loopback-host.d.ts +20 -0
- package/dist/loopback-host.d.ts.map +1 -0
- package/dist/loopback-host.js +245 -0
- package/dist/loopback-host.js.map +1 -0
- package/dist/loopback-preview.d.ts +19 -0
- package/dist/loopback-preview.d.ts.map +1 -0
- package/dist/loopback-preview.js +232 -0
- package/dist/loopback-preview.js.map +1 -0
- package/dist/native-rtc-helper.d.ts +14 -0
- package/dist/native-rtc-helper.d.ts.map +1 -0
- package/dist/native-rtc-helper.js +575 -0
- package/dist/native-rtc-helper.js.map +1 -0
- package/dist/remote-api-proxy.d.ts +17 -0
- package/dist/remote-api-proxy.d.ts.map +1 -0
- package/dist/remote-api-proxy.js +400 -0
- package/dist/remote-api-proxy.js.map +1 -0
- package/dist/remote-directory-browser.d.ts +16 -0
- package/dist/remote-directory-browser.d.ts.map +1 -0
- package/dist/remote-directory-browser.js +48 -0
- package/dist/remote-directory-browser.js.map +1 -0
- package/dist/remote-file-content-provider.d.ts +48 -0
- package/dist/remote-file-content-provider.d.ts.map +1 -0
- package/dist/remote-file-content-provider.js +93 -0
- package/dist/remote-file-content-provider.js.map +1 -0
- package/dist/remote-typert-gateway.d.ts +20 -0
- package/dist/remote-typert-gateway.d.ts.map +1 -0
- package/dist/remote-typert-gateway.js +332 -0
- package/dist/remote-typert-gateway.js.map +1 -0
- package/dist/rpc-router.d.ts +31 -0
- package/dist/rpc-router.d.ts.map +1 -0
- package/dist/rpc-router.js +201 -0
- package/dist/rpc-router.js.map +1 -0
- package/dist/safe-error.d.ts +8 -0
- package/dist/safe-error.d.ts.map +1 -0
- package/dist/safe-error.js +20 -0
- package/dist/safe-error.js.map +1 -0
- package/dist/server-api.d.ts +109 -0
- package/dist/server-api.d.ts.map +1 -0
- package/dist/server-api.js +520 -0
- package/dist/server-api.js.map +1 -0
- package/dist/server-connection.d.ts +79 -0
- package/dist/server-connection.d.ts.map +1 -0
- package/dist/server-connection.js +1023 -0
- package/dist/server-connection.js.map +1 -0
- package/dist/server-credentials.d.ts +55 -0
- package/dist/server-credentials.d.ts.map +1 -0
- package/dist/server-credentials.js +105 -0
- package/dist/server-credentials.js.map +1 -0
- package/dist/service.d.ts +141 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +432 -0
- package/dist/service.js.map +1 -0
- package/dist/session-format-compat.d.ts +3 -0
- package/dist/session-format-compat.d.ts.map +1 -0
- package/dist/session-format-compat.js +183 -0
- package/dist/session-format-compat.js.map +1 -0
- package/dist/status-stream.d.ts +48 -0
- package/dist/status-stream.d.ts.map +1 -0
- package/dist/status-stream.js +133 -0
- package/dist/status-stream.js.map +1 -0
- package/dist/terminal-policy.d.ts +21 -0
- package/dist/terminal-policy.d.ts.map +1 -0
- package/dist/terminal-policy.js +68 -0
- package/dist/terminal-policy.js.map +1 -0
- package/dist/tui-command.d.ts +13 -0
- package/dist/tui-command.d.ts.map +1 -0
- package/dist/tui-command.js +390 -0
- package/dist/tui-command.js.map +1 -0
- package/dist/typert-gateway-contract.d.ts +59 -0
- package/dist/typert-gateway-contract.d.ts.map +1 -0
- package/dist/typert-gateway-contract.js +2 -0
- package/dist/typert-gateway-contract.js.map +1 -0
- package/dist/typert-gateway-switch.d.ts +85 -0
- package/dist/typert-gateway-switch.d.ts.map +1 -0
- package/dist/typert-gateway-switch.js +315 -0
- package/dist/typert-gateway-switch.js.map +1 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +12 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +12 -0
- package/dist/version.js.map +1 -0
- package/dist/werift-rtc.d.ts +121 -0
- package/dist/werift-rtc.d.ts.map +1 -0
- package/dist/werift-rtc.js +490 -0
- package/dist/werift-rtc.js.map +1 -0
- package/dsh-plugin.json +41 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +165 -0
- package/public.d.ts +26 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
2
|
+
import type { SafeLogger } from '../logging.js';
|
|
3
|
+
export interface CodexAppServerNotification {
|
|
4
|
+
kind: 'notification';
|
|
5
|
+
method: string;
|
|
6
|
+
params: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface CodexAppServerRequest {
|
|
9
|
+
kind: 'request';
|
|
10
|
+
id: string | number;
|
|
11
|
+
method: string;
|
|
12
|
+
params: unknown;
|
|
13
|
+
}
|
|
14
|
+
export type CodexAppServerInbound = CodexAppServerNotification | CodexAppServerRequest;
|
|
15
|
+
export type CodexAppServerInboundHandler = (message: CodexAppServerInbound) => void;
|
|
16
|
+
export type CodexAppServerUnavailableHandler = (code: string) => void;
|
|
17
|
+
export type SpawnCodexAppServer = (binary: string) => ChildProcessWithoutNullStreams;
|
|
18
|
+
export interface CodexAppServerLike {
|
|
19
|
+
start(): Promise<void>;
|
|
20
|
+
isReady(): boolean;
|
|
21
|
+
call(method: string, params: unknown, timeoutMs?: number): Promise<unknown>;
|
|
22
|
+
respond(id: string | number, result: unknown): Promise<void>;
|
|
23
|
+
respondError(id: string | number, code: number, message: string): Promise<void>;
|
|
24
|
+
onInbound(handler: CodexAppServerInboundHandler): () => void;
|
|
25
|
+
onUnavailable(handler: CodexAppServerUnavailableHandler): () => void;
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export declare class CodexAppServerError extends Error {
|
|
29
|
+
readonly code: string;
|
|
30
|
+
constructor(code: string, message: string, options?: ErrorOptions);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Host-local stdio client for one Codex App Server process. This class knows
|
|
34
|
+
* JSON-RPC correlation only; Remote authorization and method policy live in
|
|
35
|
+
* the surrounding Codex domain.
|
|
36
|
+
*/
|
|
37
|
+
export declare class CodexAppServerClient implements CodexAppServerLike {
|
|
38
|
+
private readonly binary;
|
|
39
|
+
private readonly logger?;
|
|
40
|
+
private readonly spawnAppServer;
|
|
41
|
+
private process?;
|
|
42
|
+
private nextId;
|
|
43
|
+
private readonly pending;
|
|
44
|
+
private readonly inboundHandlers;
|
|
45
|
+
private readonly unavailableHandlers;
|
|
46
|
+
private stdoutBuffer;
|
|
47
|
+
private stderrBytes;
|
|
48
|
+
private ready;
|
|
49
|
+
private closed;
|
|
50
|
+
private failureNotified;
|
|
51
|
+
private startPromise?;
|
|
52
|
+
constructor(binary: string, logger?: SafeLogger | undefined, spawnAppServer?: SpawnCodexAppServer);
|
|
53
|
+
start(): Promise<void>;
|
|
54
|
+
isReady(): boolean;
|
|
55
|
+
call(method: string, params: unknown, timeoutMs?: number): Promise<unknown>;
|
|
56
|
+
respond(id: string | number, result: unknown): Promise<void>;
|
|
57
|
+
respondError(id: string | number, code: number, message: string): Promise<void>;
|
|
58
|
+
onInbound(handler: CodexAppServerInboundHandler): () => void;
|
|
59
|
+
onUnavailable(handler: CodexAppServerUnavailableHandler): () => void;
|
|
60
|
+
close(): Promise<void>;
|
|
61
|
+
private startOnce;
|
|
62
|
+
private request;
|
|
63
|
+
private write;
|
|
64
|
+
private consumeStdout;
|
|
65
|
+
private handleLine;
|
|
66
|
+
private handleProcessFailure;
|
|
67
|
+
private notifyUnavailable;
|
|
68
|
+
private takePending;
|
|
69
|
+
private failPending;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=app-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-server.d.ts","sourceRoot":"","sources":["../../src/codex/app-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,8BAA8B,EAAE,MAAM,oBAAoB,CAAA;AAE/E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAc/C,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,cAAc,CAAA;IACpB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,OAAO,CAAA;CAChB;AAED,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,SAAS,CAAA;IACf,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,OAAO,CAAA;CAChB;AAED,MAAM,MAAM,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OAAO,CAAC,iBAAiB;IAMzB,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,WAAW;CAGpB"}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { Buffer } from 'node:buffer';
|
|
3
|
+
import { PLUGIN_VERSION } from '../version.js';
|
|
4
|
+
const APP_SERVER_REQUEST_TIMEOUT_MS = 60_000;
|
|
5
|
+
const APP_SERVER_START_TIMEOUT_MS = 15_000;
|
|
6
|
+
const MAX_APP_SERVER_LINE_BYTES = 288 * 1024 * 1024;
|
|
7
|
+
const MAX_STDERR_CAPTURE_BYTES = 4 * 1024;
|
|
8
|
+
export class CodexAppServerError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(code, message, options) {
|
|
11
|
+
super(message, options);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.name = 'CodexAppServerError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Host-local stdio client for one Codex App Server process. This class knows
|
|
18
|
+
* JSON-RPC correlation only; Remote authorization and method policy live in
|
|
19
|
+
* the surrounding Codex domain.
|
|
20
|
+
*/
|
|
21
|
+
export class CodexAppServerClient {
|
|
22
|
+
binary;
|
|
23
|
+
logger;
|
|
24
|
+
spawnAppServer;
|
|
25
|
+
process;
|
|
26
|
+
nextId = 1;
|
|
27
|
+
pending = new Map();
|
|
28
|
+
inboundHandlers = new Set();
|
|
29
|
+
unavailableHandlers = new Set();
|
|
30
|
+
stdoutBuffer = Buffer.alloc(0);
|
|
31
|
+
stderrBytes = 0;
|
|
32
|
+
ready = false;
|
|
33
|
+
closed = false;
|
|
34
|
+
failureNotified = false;
|
|
35
|
+
startPromise;
|
|
36
|
+
constructor(binary, logger, spawnAppServer = binary => spawn(binary, ['app-server'], {
|
|
37
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
38
|
+
windowsHide: true,
|
|
39
|
+
})) {
|
|
40
|
+
this.binary = binary;
|
|
41
|
+
this.logger = logger;
|
|
42
|
+
this.spawnAppServer = spawnAppServer;
|
|
43
|
+
}
|
|
44
|
+
start() {
|
|
45
|
+
if (this.closed)
|
|
46
|
+
return Promise.reject(new CodexAppServerError('CODEX_CLOSED', 'The Codex domain is closed.'));
|
|
47
|
+
if (this.ready)
|
|
48
|
+
return Promise.resolve();
|
|
49
|
+
this.startPromise ??= this.startOnce().finally(() => { this.startPromise = undefined; });
|
|
50
|
+
return this.startPromise;
|
|
51
|
+
}
|
|
52
|
+
isReady() { return this.ready; }
|
|
53
|
+
async call(method, params, timeoutMs = APP_SERVER_REQUEST_TIMEOUT_MS) {
|
|
54
|
+
if (!this.ready)
|
|
55
|
+
throw new CodexAppServerError('CODEX_UNAVAILABLE', 'Codex App Server is not ready.');
|
|
56
|
+
return this.request(method, params, timeoutMs);
|
|
57
|
+
}
|
|
58
|
+
async respond(id, result) {
|
|
59
|
+
this.write({ id, result });
|
|
60
|
+
}
|
|
61
|
+
async respondError(id, code, message) {
|
|
62
|
+
this.write({ id, error: { code, message } });
|
|
63
|
+
}
|
|
64
|
+
onInbound(handler) {
|
|
65
|
+
this.inboundHandlers.add(handler);
|
|
66
|
+
return () => this.inboundHandlers.delete(handler);
|
|
67
|
+
}
|
|
68
|
+
onUnavailable(handler) {
|
|
69
|
+
this.unavailableHandlers.add(handler);
|
|
70
|
+
return () => this.unavailableHandlers.delete(handler);
|
|
71
|
+
}
|
|
72
|
+
async close() {
|
|
73
|
+
if (this.closed)
|
|
74
|
+
return;
|
|
75
|
+
this.closed = true;
|
|
76
|
+
this.ready = false;
|
|
77
|
+
this.failPending(new CodexAppServerError('CODEX_CLOSED', 'Codex App Server was closed.'));
|
|
78
|
+
const child = this.process;
|
|
79
|
+
this.process = undefined;
|
|
80
|
+
if (child === undefined || child.exitCode !== null || child.killed)
|
|
81
|
+
return;
|
|
82
|
+
await new Promise(resolve => {
|
|
83
|
+
const timer = setTimeout(() => {
|
|
84
|
+
child.kill('SIGKILL');
|
|
85
|
+
resolve();
|
|
86
|
+
}, 2_000);
|
|
87
|
+
timer.unref?.();
|
|
88
|
+
child.once('exit', () => {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
resolve();
|
|
91
|
+
});
|
|
92
|
+
child.kill('SIGTERM');
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async startOnce() {
|
|
96
|
+
if (this.process !== undefined) {
|
|
97
|
+
throw new CodexAppServerError('CODEX_STARTING', 'Codex App Server is already starting.');
|
|
98
|
+
}
|
|
99
|
+
const child = this.spawnAppServer(this.binary);
|
|
100
|
+
this.process = child;
|
|
101
|
+
this.failureNotified = false;
|
|
102
|
+
this.stdoutBuffer = Buffer.alloc(0);
|
|
103
|
+
this.stderrBytes = 0;
|
|
104
|
+
child.stdout.on('data', chunk => this.consumeStdout(Buffer.from(chunk)));
|
|
105
|
+
child.stderr.on('data', chunk => {
|
|
106
|
+
// Always drain stderr, but never log Codex payloads or paths.
|
|
107
|
+
this.stderrBytes = Math.min(MAX_STDERR_CAPTURE_BYTES, this.stderrBytes + Buffer.byteLength(chunk));
|
|
108
|
+
});
|
|
109
|
+
child.on('error', error => this.handleProcessFailure('CODEX_BINARY_UNAVAILABLE', error));
|
|
110
|
+
child.on('exit', (code, signal) => {
|
|
111
|
+
if (this.process !== child)
|
|
112
|
+
return;
|
|
113
|
+
this.process = undefined;
|
|
114
|
+
this.ready = false;
|
|
115
|
+
this.failPending(new CodexAppServerError('CODEX_APP_SERVER_EXITED', 'Codex App Server exited unexpectedly.'));
|
|
116
|
+
if (!this.closed) {
|
|
117
|
+
this.logger?.warn('Codex App Server exited', {
|
|
118
|
+
code: code ?? 'none',
|
|
119
|
+
signal: signal ?? 'none',
|
|
120
|
+
stderrBytes: this.stderrBytes,
|
|
121
|
+
});
|
|
122
|
+
this.notifyUnavailable('CODEX_APP_SERVER_EXITED');
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
try {
|
|
126
|
+
await this.request('initialize', {
|
|
127
|
+
clientInfo: {
|
|
128
|
+
name: 'deepseek_harness_remote',
|
|
129
|
+
title: 'DeepSeek Harness Remote',
|
|
130
|
+
version: PLUGIN_VERSION,
|
|
131
|
+
},
|
|
132
|
+
capabilities: {
|
|
133
|
+
experimentalApi: true,
|
|
134
|
+
mcpServerOpenaiFormElicitation: false,
|
|
135
|
+
},
|
|
136
|
+
}, APP_SERVER_START_TIMEOUT_MS);
|
|
137
|
+
this.write({ method: 'initialized', params: {} });
|
|
138
|
+
this.ready = true;
|
|
139
|
+
this.logger?.info('Codex App Server ready');
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
child.kill('SIGTERM');
|
|
143
|
+
if (error instanceof CodexAppServerError)
|
|
144
|
+
throw error;
|
|
145
|
+
throw new CodexAppServerError('CODEX_INITIALIZE_FAILED', 'Codex App Server initialization failed.', { cause: error });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
request(method, params, timeoutMs) {
|
|
149
|
+
const id = this.nextId++;
|
|
150
|
+
const result = new Promise((resolve, reject) => {
|
|
151
|
+
const timer = setTimeout(() => {
|
|
152
|
+
this.pending.delete(id);
|
|
153
|
+
reject(new CodexAppServerError('CODEX_REQUEST_TIMEOUT', 'Codex App Server request timed out.'));
|
|
154
|
+
}, timeoutMs);
|
|
155
|
+
timer.unref?.();
|
|
156
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
157
|
+
});
|
|
158
|
+
try {
|
|
159
|
+
this.write({ id, method, params });
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
const pending = this.takePending(id);
|
|
163
|
+
pending?.reject(error instanceof Error ? error : new Error('Codex App Server write failed.'));
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
write(message) {
|
|
168
|
+
const child = this.process;
|
|
169
|
+
if (child === undefined || child.stdin.destroyed || !child.stdin.writable) {
|
|
170
|
+
throw new CodexAppServerError('CODEX_UNAVAILABLE', 'Codex App Server is not available.');
|
|
171
|
+
}
|
|
172
|
+
child.stdin.write(`${JSON.stringify(message)}\n`);
|
|
173
|
+
}
|
|
174
|
+
consumeStdout(chunk) {
|
|
175
|
+
this.stdoutBuffer = this.stdoutBuffer.length === 0 ? chunk : Buffer.concat([this.stdoutBuffer, chunk]);
|
|
176
|
+
if (this.stdoutBuffer.length > MAX_APP_SERVER_LINE_BYTES) {
|
|
177
|
+
this.handleProcessFailure('CODEX_RESPONSE_TOO_LARGE', new Error('Codex App Server emitted an oversized JSONL message.'));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
let newline = this.stdoutBuffer.indexOf(0x0a);
|
|
181
|
+
while (newline >= 0) {
|
|
182
|
+
const line = this.stdoutBuffer.subarray(0, newline);
|
|
183
|
+
this.stdoutBuffer = this.stdoutBuffer.subarray(newline + 1);
|
|
184
|
+
if (line.length > 0)
|
|
185
|
+
this.handleLine(line);
|
|
186
|
+
newline = this.stdoutBuffer.indexOf(0x0a);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
handleLine(line) {
|
|
190
|
+
let value;
|
|
191
|
+
try {
|
|
192
|
+
value = JSON.parse(line.toString('utf8'));
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
this.handleProcessFailure('CODEX_INVALID_RESPONSE', new Error('Codex App Server emitted invalid JSON.'));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!isRecord(value)) {
|
|
199
|
+
this.handleProcessFailure('CODEX_INVALID_RESPONSE', new Error('Codex App Server emitted an invalid message.'));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if ((typeof value.id === 'number' || typeof value.id === 'string') && ('result' in value || 'error' in value)) {
|
|
203
|
+
const pending = this.takePending(value.id);
|
|
204
|
+
if (pending === undefined)
|
|
205
|
+
return;
|
|
206
|
+
if ('error' in value && value.error !== undefined) {
|
|
207
|
+
pending.reject(new CodexAppServerError('CODEX_UPSTREAM_ERROR', safeUpstreamError(value.error)));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
pending.resolve(value.result);
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (typeof value.method !== 'string' || value.method.length === 0 || value.method.length > 160)
|
|
215
|
+
return;
|
|
216
|
+
const params = value.params ?? {};
|
|
217
|
+
const inbound = typeof value.id === 'string' || typeof value.id === 'number'
|
|
218
|
+
? { kind: 'request', id: value.id, method: value.method, params }
|
|
219
|
+
: { kind: 'notification', method: value.method, params };
|
|
220
|
+
for (const handler of this.inboundHandlers)
|
|
221
|
+
handler(inbound);
|
|
222
|
+
}
|
|
223
|
+
handleProcessFailure(code, cause) {
|
|
224
|
+
this.ready = false;
|
|
225
|
+
this.failPending(new CodexAppServerError(code, 'Codex App Server communication failed.', { cause }));
|
|
226
|
+
const child = this.process;
|
|
227
|
+
this.process = undefined;
|
|
228
|
+
child?.kill('SIGTERM');
|
|
229
|
+
this.logger?.warn('Codex App Server communication failed', { code });
|
|
230
|
+
if (!this.closed)
|
|
231
|
+
this.notifyUnavailable(code);
|
|
232
|
+
}
|
|
233
|
+
notifyUnavailable(code) {
|
|
234
|
+
if (this.failureNotified)
|
|
235
|
+
return;
|
|
236
|
+
this.failureNotified = true;
|
|
237
|
+
for (const handler of this.unavailableHandlers)
|
|
238
|
+
handler(code);
|
|
239
|
+
}
|
|
240
|
+
takePending(id) {
|
|
241
|
+
const pending = this.pending.get(id);
|
|
242
|
+
if (pending === undefined)
|
|
243
|
+
return undefined;
|
|
244
|
+
this.pending.delete(id);
|
|
245
|
+
clearTimeout(pending.timer);
|
|
246
|
+
return pending;
|
|
247
|
+
}
|
|
248
|
+
failPending(error) {
|
|
249
|
+
for (const id of [...this.pending.keys()])
|
|
250
|
+
this.takePending(id)?.reject(error);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function safeUpstreamError(value) {
|
|
254
|
+
if (!isRecord(value) || typeof value.message !== 'string')
|
|
255
|
+
return 'Codex App Server rejected the request.';
|
|
256
|
+
// Upstream messages may contain paths or prompt fragments. Only retain a
|
|
257
|
+
// short generic category for the Remote boundary.
|
|
258
|
+
const message = value.message.toLowerCase();
|
|
259
|
+
if (message.includes('active writer'))
|
|
260
|
+
return 'Codex thread already has an active writer.';
|
|
261
|
+
return message.includes('not initialized')
|
|
262
|
+
? 'Codex App Server is not initialized.'
|
|
263
|
+
: 'Codex App Server rejected the request.';
|
|
264
|
+
}
|
|
265
|
+
function isRecord(value) {
|
|
266
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=app-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-server.js","sourceRoot":"","sources":["../../src/codex/app-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAuC,MAAM,oBAAoB,CAAA;AAC/E,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAE9C,MAAM,6BAA6B,GAAG,MAAM,CAAA;AAC5C,MAAM,2BAA2B,GAAG,MAAM,CAAA;AAC1C,MAAM,yBAAyB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAA;AACnD,MAAM,wBAAwB,GAAG,CAAC,GAAG,IAAI,CAAA;AAqCzC,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACvB;IAArB,YAAqB,IAAY,EAAE,OAAe,EAAE,OAAsB;QACxE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QADJ,SAAI,GAAJ,IAAI,CAAQ;QAE/B,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAA;IACnC,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,oBAAoB;IAcZ;IACA;IACA;IAfX,OAAO,CAAiC;IACxC,MAAM,GAAG,CAAC,CAAA;IACD,OAAO,GAAG,IAAI,GAAG,EAAmC,CAAA;IACpD,eA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AAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC7E,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type JsonRecord = Record<string, unknown>;
|
|
2
|
+
export declare function toolOutputImageBlocks(item: JsonRecord): JsonRecord[];
|
|
3
|
+
export declare function contentBlocks(value: unknown, attachmentSeed: string): JsonRecord[];
|
|
4
|
+
export declare function collectImageBlocks(value: unknown, output?: JsonRecord[]): JsonRecord[];
|
|
5
|
+
export declare function isCanonicalBase64(value: string): boolean;
|
|
6
|
+
export {};
|
|
7
|
+
//# sourceMappingURL=codex-image-codec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codex-image-codec.d.ts","sourceRoot":"","sources":["../../src/codex/codex-image-codec.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAmBzC,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,EAAE,CAgBpE;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,GAAG,UAAU,EAAE,CAqBlF;AAuDD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,GAAE,UAAU,EAAO,GAAG,UAAU,EAAE,CAc1F;AAmJD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAGxD"}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
const CODEX_IMAGE_MEDIA_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/gif']);
|
|
2
|
+
const CODEX_IMAGE_ATTACHMENT_PREFIX = 'codex-image:';
|
|
3
|
+
const MAX_CODEX_IMAGE_BASE64 = 288 * 1024 * 1024;
|
|
4
|
+
const DATA_IMAGE_URL = /^data:(image\/(?:png|jpeg|webp|gif));base64,([A-Za-z0-9+/]+={0,2})$/u;
|
|
5
|
+
function string(value) {
|
|
6
|
+
return typeof value === 'string' ? value : undefined;
|
|
7
|
+
}
|
|
8
|
+
function isRecord(value) {
|
|
9
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
function record(value) {
|
|
12
|
+
return isRecord(value) ? value : {};
|
|
13
|
+
}
|
|
14
|
+
export function toolOutputImageBlocks(item) {
|
|
15
|
+
const candidates = [
|
|
16
|
+
record(item.result).content,
|
|
17
|
+
record(item.output).content,
|
|
18
|
+
item.contentItems,
|
|
19
|
+
Array.isArray(item.output) ? item.output : undefined,
|
|
20
|
+
];
|
|
21
|
+
for (const candidate of candidates) {
|
|
22
|
+
if (!Array.isArray(candidate))
|
|
23
|
+
continue;
|
|
24
|
+
const images = candidate.flatMap(value => {
|
|
25
|
+
const block = record(value);
|
|
26
|
+
return isImageContent(block) && parseImageBlock(block) !== undefined ? [block] : [];
|
|
27
|
+
});
|
|
28
|
+
if (images.length > 0)
|
|
29
|
+
return images;
|
|
30
|
+
}
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
export function contentBlocks(value, attachmentSeed) {
|
|
34
|
+
if (typeof value === 'string')
|
|
35
|
+
return [{ type: 'text', text: value }];
|
|
36
|
+
if (!Array.isArray(value))
|
|
37
|
+
return [];
|
|
38
|
+
const blocks = [];
|
|
39
|
+
let imageIndex = 0;
|
|
40
|
+
for (const raw of value) {
|
|
41
|
+
if (typeof raw === 'string') {
|
|
42
|
+
blocks.push({ type: 'text', text: raw });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const block = record(raw);
|
|
46
|
+
const image = imageContentBlock(block, attachmentSeed, imageIndex);
|
|
47
|
+
if (image !== undefined) {
|
|
48
|
+
imageIndex += 1;
|
|
49
|
+
blocks.push(image);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const text = string(block.text) ?? string(block.content);
|
|
53
|
+
if (text !== undefined)
|
|
54
|
+
blocks.push({ type: block.type === 'reasoning' ? 'reasoning' : 'text', text });
|
|
55
|
+
}
|
|
56
|
+
return blocks;
|
|
57
|
+
}
|
|
58
|
+
function imageContentBlock(block, attachmentSeed, index) {
|
|
59
|
+
if (!isImageContent(block))
|
|
60
|
+
return undefined;
|
|
61
|
+
const image = parseImageBlock(block);
|
|
62
|
+
if (image === undefined)
|
|
63
|
+
return undefined;
|
|
64
|
+
const dimensions = imageDimensions(image.mediaType, image.data);
|
|
65
|
+
const name = string(block.name);
|
|
66
|
+
const attachment = {
|
|
67
|
+
attachmentId: `${CODEX_IMAGE_ATTACHMENT_PREFIX}${hashString(`${attachmentSeed}:${index}`)}:${index}`,
|
|
68
|
+
mediaType: image.mediaType,
|
|
69
|
+
bytes: base64ByteLength(image.data),
|
|
70
|
+
width: dimensions.width,
|
|
71
|
+
height: dimensions.height,
|
|
72
|
+
...(name === undefined ? {} : { name }),
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
type: 'image',
|
|
76
|
+
attachment,
|
|
77
|
+
mediaType: image.mediaType,
|
|
78
|
+
data: image.data,
|
|
79
|
+
url: image.url,
|
|
80
|
+
...(name === undefined ? {} : { name }),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function isImageContent(block) {
|
|
84
|
+
return block.type === 'image' || block.type === 'input_image';
|
|
85
|
+
}
|
|
86
|
+
function parseImageBlock(block) {
|
|
87
|
+
const url = string(block.url)
|
|
88
|
+
?? string(block.image_url)
|
|
89
|
+
?? string(record(block.image_url).url);
|
|
90
|
+
const parsed = url === undefined ? undefined : parseDataImageUrl(url);
|
|
91
|
+
if (parsed !== undefined)
|
|
92
|
+
return parsed;
|
|
93
|
+
const data = string(block.data);
|
|
94
|
+
if (data === undefined || !isCanonicalBase64(data))
|
|
95
|
+
return undefined;
|
|
96
|
+
if (data.length > MAX_CODEX_IMAGE_BASE64)
|
|
97
|
+
return undefined;
|
|
98
|
+
const mediaType = string(block.mediaType) ?? string(block.mimeType) ?? sniffImageMediaType(data);
|
|
99
|
+
return mediaType !== undefined && CODEX_IMAGE_MEDIA_TYPES.has(mediaType)
|
|
100
|
+
? { mediaType, data, url: `data:${mediaType};base64,${data}` }
|
|
101
|
+
: undefined;
|
|
102
|
+
}
|
|
103
|
+
function parseDataImageUrl(value) {
|
|
104
|
+
const match = DATA_IMAGE_URL.exec(value);
|
|
105
|
+
if (match === null)
|
|
106
|
+
return undefined;
|
|
107
|
+
const mediaType = match[1];
|
|
108
|
+
const data = match[2];
|
|
109
|
+
if (!CODEX_IMAGE_MEDIA_TYPES.has(mediaType) || !isCanonicalBase64(data))
|
|
110
|
+
return undefined;
|
|
111
|
+
return { mediaType, data, url: `data:${mediaType};base64,${data}` };
|
|
112
|
+
}
|
|
113
|
+
export function collectImageBlocks(value, output = []) {
|
|
114
|
+
if (Array.isArray(value)) {
|
|
115
|
+
for (const item of value)
|
|
116
|
+
collectImageBlocks(item, output);
|
|
117
|
+
return output;
|
|
118
|
+
}
|
|
119
|
+
if (!isRecord(value))
|
|
120
|
+
return output;
|
|
121
|
+
if (value.type === 'image')
|
|
122
|
+
output.push(value);
|
|
123
|
+
for (const [key, child] of Object.entries(value)) {
|
|
124
|
+
if (key === 'attachment')
|
|
125
|
+
continue;
|
|
126
|
+
if (key === 'content' || key === 'message' || key === 'inserted' || key === 'chunk' || key === 'block') {
|
|
127
|
+
collectImageBlocks(child, output);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return output;
|
|
131
|
+
}
|
|
132
|
+
function base64ByteLength(value) {
|
|
133
|
+
const padding = value.endsWith('==') ? 2 : value.endsWith('=') ? 1 : 0;
|
|
134
|
+
return Math.max(1, Math.floor(value.length * 3 / 4) - padding);
|
|
135
|
+
}
|
|
136
|
+
function imageDimensions(mediaType, data) {
|
|
137
|
+
const bytes = base64PrefixBytes(data, 256 * 1024);
|
|
138
|
+
const parsed = mediaType === 'image/png'
|
|
139
|
+
? pngDimensions(bytes)
|
|
140
|
+
: mediaType === 'image/jpeg'
|
|
141
|
+
? jpegDimensions(bytes)
|
|
142
|
+
: mediaType === 'image/gif'
|
|
143
|
+
? gifDimensions(bytes)
|
|
144
|
+
: mediaType === 'image/webp'
|
|
145
|
+
? webpDimensions(bytes)
|
|
146
|
+
: undefined;
|
|
147
|
+
return parsed ?? { width: 1, height: 1 };
|
|
148
|
+
}
|
|
149
|
+
function pngDimensions(bytes) {
|
|
150
|
+
if (bytes.length < 24
|
|
151
|
+
|| bytes[0] !== 0x89
|
|
152
|
+
|| bytes[1] !== 0x50
|
|
153
|
+
|| bytes[2] !== 0x4e
|
|
154
|
+
|| bytes[3] !== 0x47
|
|
155
|
+
|| bytes[4] !== 0x0d
|
|
156
|
+
|| bytes[5] !== 0x0a
|
|
157
|
+
|| bytes[6] !== 0x1a
|
|
158
|
+
|| bytes[7] !== 0x0a)
|
|
159
|
+
return undefined;
|
|
160
|
+
return positiveDimensions(readUint32BE(bytes, 16), readUint32BE(bytes, 20));
|
|
161
|
+
}
|
|
162
|
+
function jpegDimensions(bytes) {
|
|
163
|
+
if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8)
|
|
164
|
+
return undefined;
|
|
165
|
+
let offset = 2;
|
|
166
|
+
while (offset + 9 < bytes.length) {
|
|
167
|
+
if (bytes[offset] !== 0xff) {
|
|
168
|
+
offset += 1;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const marker = bytes[offset + 1];
|
|
172
|
+
offset += 2;
|
|
173
|
+
if (marker === 0xd8 || marker === 0xd9)
|
|
174
|
+
continue;
|
|
175
|
+
if (offset + 2 > bytes.length)
|
|
176
|
+
return undefined;
|
|
177
|
+
const length = readUint16BE(bytes, offset);
|
|
178
|
+
if (length < 2 || offset + length > bytes.length)
|
|
179
|
+
return undefined;
|
|
180
|
+
if (isJpegSof(marker) && length >= 7) {
|
|
181
|
+
return positiveDimensions(readUint16BE(bytes, offset + 5), readUint16BE(bytes, offset + 3));
|
|
182
|
+
}
|
|
183
|
+
offset += length;
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
function gifDimensions(bytes) {
|
|
188
|
+
if (bytes.length < 10
|
|
189
|
+
|| bytes[0] !== 0x47
|
|
190
|
+
|| bytes[1] !== 0x49
|
|
191
|
+
|| bytes[2] !== 0x46
|
|
192
|
+
|| bytes[3] !== 0x38
|
|
193
|
+
|| (bytes[4] !== 0x37 && bytes[4] !== 0x39)
|
|
194
|
+
|| bytes[5] !== 0x61)
|
|
195
|
+
return undefined;
|
|
196
|
+
return positiveDimensions(readUint16LE(bytes, 6), readUint16LE(bytes, 8));
|
|
197
|
+
}
|
|
198
|
+
function webpDimensions(bytes) {
|
|
199
|
+
if (bytes.length < 30
|
|
200
|
+
|| stringFromBytes(bytes, 0, 4) !== 'RIFF'
|
|
201
|
+
|| stringFromBytes(bytes, 8, 12) !== 'WEBP')
|
|
202
|
+
return undefined;
|
|
203
|
+
const chunk = stringFromBytes(bytes, 12, 16);
|
|
204
|
+
if (chunk === 'VP8X') {
|
|
205
|
+
return positiveDimensions(1 + readUint24LE(bytes, 24), 1 + readUint24LE(bytes, 27));
|
|
206
|
+
}
|
|
207
|
+
if (chunk === 'VP8L' && bytes[20] === 0x2f) {
|
|
208
|
+
const b0 = bytes[21];
|
|
209
|
+
const b1 = bytes[22];
|
|
210
|
+
const b2 = bytes[23];
|
|
211
|
+
const b3 = bytes[24];
|
|
212
|
+
const width = 1 + b0 + ((b1 & 0x3f) << 8);
|
|
213
|
+
const height = 1 + ((b1 & 0xc0) >> 6) + (b2 << 2) + ((b3 & 0x0f) << 10);
|
|
214
|
+
return positiveDimensions(width, height);
|
|
215
|
+
}
|
|
216
|
+
if (chunk === 'VP8 ' && bytes[23] === 0x9d && bytes[24] === 0x01 && bytes[25] === 0x2a) {
|
|
217
|
+
return positiveDimensions(readUint16LE(bytes, 26) & 0x3fff, readUint16LE(bytes, 28) & 0x3fff);
|
|
218
|
+
}
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
function sniffImageMediaType(data) {
|
|
222
|
+
const bytes = base64PrefixBytes(data, 32);
|
|
223
|
+
if (pngDimensions(bytes) !== undefined)
|
|
224
|
+
return 'image/png';
|
|
225
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)
|
|
226
|
+
return 'image/jpeg';
|
|
227
|
+
if (gifDimensions(bytes) !== undefined)
|
|
228
|
+
return 'image/gif';
|
|
229
|
+
if (bytes.length >= 12 && stringFromBytes(bytes, 0, 4) === 'RIFF' && stringFromBytes(bytes, 8, 12) === 'WEBP')
|
|
230
|
+
return 'image/webp';
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
function base64PrefixBytes(value, maxBytes) {
|
|
234
|
+
const chars = value.slice(0, Math.ceil(maxBytes / 3) * 4);
|
|
235
|
+
let binary;
|
|
236
|
+
try {
|
|
237
|
+
binary = atob(chars);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return new Uint8Array();
|
|
241
|
+
}
|
|
242
|
+
const bytes = new Uint8Array(Math.min(binary.length, maxBytes));
|
|
243
|
+
for (let index = 0; index < bytes.length; index += 1)
|
|
244
|
+
bytes[index] = binary.charCodeAt(index);
|
|
245
|
+
return bytes;
|
|
246
|
+
}
|
|
247
|
+
function readUint16BE(bytes, offset) {
|
|
248
|
+
return (bytes[offset] << 8) + bytes[offset + 1];
|
|
249
|
+
}
|
|
250
|
+
function readUint16LE(bytes, offset) {
|
|
251
|
+
return bytes[offset] + (bytes[offset + 1] << 8);
|
|
252
|
+
}
|
|
253
|
+
function readUint24LE(bytes, offset) {
|
|
254
|
+
return bytes[offset] + (bytes[offset + 1] << 8) + (bytes[offset + 2] << 16);
|
|
255
|
+
}
|
|
256
|
+
function readUint32BE(bytes, offset) {
|
|
257
|
+
return (bytes[offset] * 0x1000000) + (bytes[offset + 1] * 0x10000) + (bytes[offset + 2] * 0x100) + bytes[offset + 3];
|
|
258
|
+
}
|
|
259
|
+
function positiveDimensions(width, height) {
|
|
260
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
261
|
+
}
|
|
262
|
+
function isJpegSof(marker) {
|
|
263
|
+
return (marker >= 0xc0 && marker <= 0xc3)
|
|
264
|
+
|| (marker >= 0xc5 && marker <= 0xc7)
|
|
265
|
+
|| (marker >= 0xc9 && marker <= 0xcb)
|
|
266
|
+
|| (marker >= 0xcd && marker <= 0xcf);
|
|
267
|
+
}
|
|
268
|
+
function stringFromBytes(bytes, start, end) {
|
|
269
|
+
if (bytes.length < end)
|
|
270
|
+
return '';
|
|
271
|
+
let output = '';
|
|
272
|
+
for (let index = start; index < end; index += 1)
|
|
273
|
+
output += String.fromCharCode(bytes[index]);
|
|
274
|
+
return output;
|
|
275
|
+
}
|
|
276
|
+
export function isCanonicalBase64(value) {
|
|
277
|
+
return value.length >= 4 && value.length % 4 === 0
|
|
278
|
+
&& /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u.test(value);
|
|
279
|
+
}
|
|
280
|
+
function hashString(value) {
|
|
281
|
+
let hash = 2166136261;
|
|
282
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
283
|
+
hash ^= value.charCodeAt(index);
|
|
284
|
+
hash = Math.imul(hash, 16777619);
|
|
285
|
+
}
|
|
286
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
287
|
+
}
|
|
288
|
+
//# sourceMappingURL=codex-image-codec.js.map
|