@celljs/ta-host 4.5.1 → 4.6.1
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 +31 -8
- package/cell.schema.json +18 -0
- package/cell.yml +4 -0
- package/esm/node/auth-server-impl.d.ts +61 -0
- package/esm/node/auth-server-impl.js +247 -0
- package/esm/node/auth-server-impl.js.map +1 -0
- package/esm/node/client-tool-broker.d.ts +35 -0
- package/esm/node/client-tool-broker.js +82 -0
- package/esm/node/client-tool-broker.js.map +1 -0
- package/esm/node/exec-environment-factory.d.ts +17 -0
- package/esm/node/exec-environment-factory.js +58 -0
- package/esm/node/exec-environment-factory.js.map +1 -0
- package/esm/node/extension-data-notifier.d.ts +41 -0
- package/esm/node/extension-data-notifier.js +119 -0
- package/esm/node/extension-data-notifier.js.map +1 -0
- package/esm/node/extension-data.d.ts +45 -0
- package/esm/node/extension-data.js +181 -0
- package/esm/node/extension-data.js.map +1 -0
- package/esm/node/host-protocol.d.ts +29 -1
- package/esm/node/host-protocol.js +2 -0
- package/esm/node/host-protocol.js.map +1 -1
- package/esm/node/index.d.ts +24 -1
- package/esm/node/index.js +24 -1
- package/esm/node/index.js.map +1 -1
- package/esm/node/package-host-extension.d.ts +9 -0
- package/esm/node/package-host-extension.js +18 -0
- package/esm/node/package-host-extension.js.map +1 -0
- package/esm/node/package-hosts.d.ts +44 -0
- package/esm/node/package-hosts.js +167 -0
- package/esm/node/package-hosts.js.map +1 -0
- package/esm/node/package-server-impl.d.ts +25 -0
- package/esm/node/package-server-impl.js +94 -0
- package/esm/node/package-server-impl.js.map +1 -0
- package/esm/node/package-switch.d.ts +29 -0
- package/esm/node/package-switch.js +68 -0
- package/esm/node/package-switch.js.map +1 -0
- package/esm/node/relay-bridge.d.ts +34 -0
- package/esm/node/relay-bridge.js +105 -0
- package/esm/node/relay-bridge.js.map +1 -0
- package/esm/node/session-info-util.d.ts +16 -0
- package/esm/node/session-info-util.js +49 -0
- package/esm/node/session-info-util.js.map +1 -0
- package/esm/node/session-storage/file-session-storage.d.ts +27 -0
- package/esm/node/session-storage/file-session-storage.js +139 -0
- package/esm/node/session-storage/file-session-storage.js.map +1 -0
- package/esm/node/session-storage/index.d.ts +3 -0
- package/esm/node/session-storage/index.js +4 -0
- package/esm/node/session-storage/index.js.map +1 -0
- package/esm/node/session-storage/session-storage-protocol.d.ts +57 -0
- package/esm/node/session-storage/session-storage-protocol.js +40 -0
- package/esm/node/session-storage/session-storage-protocol.js.map +1 -0
- package/esm/node/session-storage/stored-session.d.ts +46 -0
- package/esm/node/session-storage/stored-session.js +116 -0
- package/esm/node/session-storage/stored-session.js.map +1 -0
- package/esm/node/settings-server-impl.d.ts +11 -0
- package/esm/node/settings-server-impl.js +45 -0
- package/esm/node/settings-server-impl.js.map +1 -0
- package/esm/node/settings-store.d.ts +56 -0
- package/esm/node/settings-store.js +306 -0
- package/esm/node/settings-store.js.map +1 -0
- package/esm/node/ta-access.d.ts +19 -2
- package/esm/node/ta-access.js +64 -7
- package/esm/node/ta-access.js.map +1 -1
- package/esm/node/ta-errors.d.ts +3 -1
- package/esm/node/ta-errors.js +17 -1
- package/esm/node/ta-errors.js.map +1 -1
- package/esm/node/ta-server-impl.d.ts +17 -5
- package/esm/node/ta-server-impl.js +70 -13
- package/esm/node/ta-server-impl.js.map +1 -1
- package/esm/node/ta-session-factory.d.ts +2 -0
- package/esm/node/ta-session-factory.js +25 -11
- package/esm/node/ta-session-factory.js.map +1 -1
- package/esm/node/ta-session.d.ts +9 -1
- package/esm/node/ta-session.js +26 -2
- package/esm/node/ta-session.js.map +1 -1
- package/esm/node/terminal-protocol.d.ts +18 -0
- package/esm/node/terminal-protocol.js +2 -0
- package/esm/node/terminal-protocol.js.map +1 -0
- package/esm/node/terminal-service.d.ts +59 -0
- package/esm/node/terminal-service.js +240 -0
- package/esm/node/terminal-service.js.map +1 -0
- package/esm/node/terminal-util.d.ts +62 -0
- package/esm/node/terminal-util.js +153 -0
- package/esm/node/terminal-util.js.map +1 -0
- package/esm/node/web-extension-catalog.d.ts +38 -0
- package/esm/node/web-extension-catalog.js +160 -0
- package/esm/node/web-extension-catalog.js.map +1 -0
- package/esm/node/web-extension-util.d.ts +21 -0
- package/esm/node/web-extension-util.js +123 -0
- package/esm/node/web-extension-util.js.map +1 -0
- package/esm/node/working-directory.d.ts +13 -0
- package/esm/node/working-directory.js +40 -0
- package/esm/node/working-directory.js.map +1 -0
- package/esm/node/workspace-files.d.ts +47 -0
- package/esm/node/workspace-files.js +265 -0
- package/esm/node/workspace-files.js.map +1 -0
- package/esm/node/workspace-protocol.d.ts +20 -0
- package/esm/node/workspace-protocol.js +3 -0
- package/esm/node/workspace-protocol.js.map +1 -0
- package/esm/node/workspace-server-impl.d.ts +27 -0
- package/esm/node/workspace-server-impl.js +88 -0
- package/esm/node/workspace-server-impl.js.map +1 -0
- package/esm/node/workspace-service.d.ts +31 -0
- package/esm/node/workspace-service.js +209 -0
- package/esm/node/workspace-service.js.map +1 -0
- package/esm/node/workspace-util.d.ts +29 -0
- package/esm/node/workspace-util.js +104 -0
- package/esm/node/workspace-util.js.map +1 -0
- package/lib/node/auth-server-impl.d.ts +62 -0
- package/lib/node/auth-server-impl.d.ts.map +1 -0
- package/lib/node/auth-server-impl.js +250 -0
- package/lib/node/auth-server-impl.js.map +1 -0
- package/lib/node/auth-server-impl.spec.d.ts +2 -0
- package/lib/node/auth-server-impl.spec.d.ts.map +1 -0
- package/lib/node/auth-server-impl.spec.js +47 -0
- package/lib/node/auth-server-impl.spec.js.map +1 -0
- package/lib/node/client-tool-broker.d.ts +36 -0
- package/lib/node/client-tool-broker.d.ts.map +1 -0
- package/lib/node/client-tool-broker.js +86 -0
- package/lib/node/client-tool-broker.js.map +1 -0
- package/lib/node/client-tool-broker.spec.d.ts +2 -0
- package/lib/node/client-tool-broker.spec.d.ts.map +1 -0
- package/lib/node/client-tool-broker.spec.js +49 -0
- package/lib/node/client-tool-broker.spec.js.map +1 -0
- package/lib/node/exec-environment-factory.d.ts +18 -0
- package/lib/node/exec-environment-factory.d.ts.map +1 -0
- package/lib/node/exec-environment-factory.js +61 -0
- package/lib/node/exec-environment-factory.js.map +1 -0
- package/lib/node/extension-data-notifier.d.ts +42 -0
- package/lib/node/extension-data-notifier.d.ts.map +1 -0
- package/lib/node/extension-data-notifier.js +123 -0
- package/lib/node/extension-data-notifier.js.map +1 -0
- package/lib/node/extension-data-notifier.spec.d.ts +2 -0
- package/lib/node/extension-data-notifier.spec.d.ts.map +1 -0
- package/lib/node/extension-data-notifier.spec.js +35 -0
- package/lib/node/extension-data-notifier.spec.js.map +1 -0
- package/lib/node/extension-data.d.ts +46 -0
- package/lib/node/extension-data.d.ts.map +1 -0
- package/lib/node/extension-data.js +184 -0
- package/lib/node/extension-data.js.map +1 -0
- package/lib/node/extension-data.spec.d.ts +2 -0
- package/lib/node/extension-data.spec.d.ts.map +1 -0
- package/lib/node/extension-data.spec.js +99 -0
- package/lib/node/extension-data.spec.js.map +1 -0
- package/lib/node/host-protocol.d.ts +29 -1
- package/lib/node/host-protocol.d.ts.map +1 -1
- package/lib/node/host-protocol.js +3 -1
- package/lib/node/host-protocol.js.map +1 -1
- package/lib/node/index.d.ts +24 -1
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +24 -1
- package/lib/node/index.js.map +1 -1
- package/lib/node/package-host-extension.d.ts +10 -0
- package/lib/node/package-host-extension.d.ts.map +1 -0
- package/lib/node/package-host-extension.js +22 -0
- package/lib/node/package-host-extension.js.map +1 -0
- package/lib/node/package-hosts.d.ts +45 -0
- package/lib/node/package-hosts.d.ts.map +1 -0
- package/lib/node/package-hosts.js +170 -0
- package/lib/node/package-hosts.js.map +1 -0
- package/lib/node/package-hosts.spec.d.ts +2 -0
- package/lib/node/package-hosts.spec.d.ts.map +1 -0
- package/lib/node/package-hosts.spec.js +53 -0
- package/lib/node/package-hosts.spec.js.map +1 -0
- package/lib/node/package-server-impl.d.ts +26 -0
- package/lib/node/package-server-impl.d.ts.map +1 -0
- package/lib/node/package-server-impl.js +97 -0
- package/lib/node/package-server-impl.js.map +1 -0
- package/lib/node/package-switch.d.ts +30 -0
- package/lib/node/package-switch.d.ts.map +1 -0
- package/lib/node/package-switch.js +71 -0
- package/lib/node/package-switch.js.map +1 -0
- package/lib/node/relay-bridge.d.ts +35 -0
- package/lib/node/relay-bridge.d.ts.map +1 -0
- package/lib/node/relay-bridge.js +108 -0
- package/lib/node/relay-bridge.js.map +1 -0
- package/lib/node/session-info-util.d.ts +17 -0
- package/lib/node/session-info-util.d.ts.map +1 -0
- package/lib/node/session-info-util.js +52 -0
- package/lib/node/session-info-util.js.map +1 -0
- package/lib/node/session-info-util.spec.d.ts +2 -0
- package/lib/node/session-info-util.spec.d.ts.map +1 -0
- package/lib/node/session-info-util.spec.js +22 -0
- package/lib/node/session-info-util.spec.js.map +1 -0
- package/lib/node/session-storage/file-session-storage.d.ts +28 -0
- package/lib/node/session-storage/file-session-storage.d.ts.map +1 -0
- package/lib/node/session-storage/file-session-storage.js +143 -0
- package/lib/node/session-storage/file-session-storage.js.map +1 -0
- package/lib/node/session-storage/file-session-storage.spec.d.ts +2 -0
- package/lib/node/session-storage/file-session-storage.spec.d.ts.map +1 -0
- package/lib/node/session-storage/file-session-storage.spec.js +70 -0
- package/lib/node/session-storage/file-session-storage.spec.js.map +1 -0
- package/lib/node/session-storage/index.d.ts +4 -0
- package/lib/node/session-storage/index.d.ts.map +1 -0
- package/lib/node/session-storage/index.js +20 -0
- package/lib/node/session-storage/index.js.map +1 -0
- package/lib/node/session-storage/session-storage-protocol.d.ts +58 -0
- package/lib/node/session-storage/session-storage-protocol.d.ts.map +1 -0
- package/lib/node/session-storage/session-storage-protocol.js +45 -0
- package/lib/node/session-storage/session-storage-protocol.js.map +1 -0
- package/lib/node/session-storage/stored-session.d.ts +47 -0
- package/lib/node/session-storage/stored-session.d.ts.map +1 -0
- package/lib/node/session-storage/stored-session.js +120 -0
- package/lib/node/session-storage/stored-session.js.map +1 -0
- package/lib/node/session-storage/stored-session.spec.d.ts +2 -0
- package/lib/node/session-storage/stored-session.spec.d.ts.map +1 -0
- package/lib/node/session-storage/stored-session.spec.js +73 -0
- package/lib/node/session-storage/stored-session.spec.js.map +1 -0
- package/lib/node/settings-server-impl.d.ts +12 -0
- package/lib/node/settings-server-impl.d.ts.map +1 -0
- package/lib/node/settings-server-impl.js +48 -0
- package/lib/node/settings-server-impl.js.map +1 -0
- package/lib/node/settings-store.d.ts +57 -0
- package/lib/node/settings-store.d.ts.map +1 -0
- package/lib/node/settings-store.js +309 -0
- package/lib/node/settings-store.js.map +1 -0
- package/lib/node/settings-store.spec.d.ts +2 -0
- package/lib/node/settings-store.spec.d.ts.map +1 -0
- package/lib/node/settings-store.spec.js +145 -0
- package/lib/node/settings-store.spec.js.map +1 -0
- package/lib/node/ta-access.d.ts +19 -2
- package/lib/node/ta-access.d.ts.map +1 -1
- package/lib/node/ta-access.js +66 -9
- package/lib/node/ta-access.js.map +1 -1
- package/lib/node/ta-access.spec.js +8 -0
- package/lib/node/ta-access.spec.js.map +1 -1
- package/lib/node/ta-errors.d.ts +3 -1
- package/lib/node/ta-errors.d.ts.map +1 -1
- package/lib/node/ta-errors.js +17 -1
- package/lib/node/ta-errors.js.map +1 -1
- package/lib/node/ta-errors.spec.d.ts +2 -0
- package/lib/node/ta-errors.spec.d.ts.map +1 -0
- package/lib/node/ta-errors.spec.js +25 -0
- package/lib/node/ta-errors.spec.js.map +1 -0
- package/lib/node/ta-server-impl.d.ts +17 -5
- package/lib/node/ta-server-impl.d.ts.map +1 -1
- package/lib/node/ta-server-impl.js +70 -13
- package/lib/node/ta-server-impl.js.map +1 -1
- package/lib/node/ta-session-factory.d.ts +2 -0
- package/lib/node/ta-session-factory.d.ts.map +1 -1
- package/lib/node/ta-session-factory.js +24 -10
- package/lib/node/ta-session-factory.js.map +1 -1
- package/lib/node/ta-session.d.ts +9 -1
- package/lib/node/ta-session.d.ts.map +1 -1
- package/lib/node/ta-session.js +26 -2
- package/lib/node/ta-session.js.map +1 -1
- package/lib/node/terminal-protocol.d.ts +19 -0
- package/lib/node/terminal-protocol.d.ts.map +1 -0
- package/lib/node/terminal-protocol.js +5 -0
- package/lib/node/terminal-protocol.js.map +1 -0
- package/lib/node/terminal-service.d.ts +60 -0
- package/lib/node/terminal-service.d.ts.map +1 -0
- package/lib/node/terminal-service.js +241 -0
- package/lib/node/terminal-service.js.map +1 -0
- package/lib/node/terminal-service.spec.d.ts +2 -0
- package/lib/node/terminal-service.spec.d.ts.map +1 -0
- package/lib/node/terminal-service.spec.js +79 -0
- package/lib/node/terminal-service.spec.js.map +1 -0
- package/lib/node/terminal-util.d.ts +63 -0
- package/lib/node/terminal-util.d.ts.map +1 -0
- package/lib/node/terminal-util.js +158 -0
- package/lib/node/terminal-util.js.map +1 -0
- package/lib/node/terminal-util.spec.d.ts +2 -0
- package/lib/node/terminal-util.spec.d.ts.map +1 -0
- package/lib/node/terminal-util.spec.js +71 -0
- package/lib/node/terminal-util.spec.js.map +1 -0
- package/lib/node/web-extension-catalog.d.ts +39 -0
- package/lib/node/web-extension-catalog.d.ts.map +1 -0
- package/lib/node/web-extension-catalog.js +163 -0
- package/lib/node/web-extension-catalog.js.map +1 -0
- package/lib/node/web-extension-catalog.spec.d.ts +2 -0
- package/lib/node/web-extension-catalog.spec.d.ts.map +1 -0
- package/lib/node/web-extension-catalog.spec.js +157 -0
- package/lib/node/web-extension-catalog.spec.js.map +1 -0
- package/lib/node/web-extension-util.d.ts +22 -0
- package/lib/node/web-extension-util.d.ts.map +1 -0
- package/lib/node/web-extension-util.js +126 -0
- package/lib/node/web-extension-util.js.map +1 -0
- package/lib/node/working-directory.d.ts +14 -0
- package/lib/node/working-directory.d.ts.map +1 -0
- package/lib/node/working-directory.js +43 -0
- package/lib/node/working-directory.js.map +1 -0
- package/lib/node/workspace-files.d.ts +48 -0
- package/lib/node/workspace-files.d.ts.map +1 -0
- package/lib/node/workspace-files.js +268 -0
- package/lib/node/workspace-files.js.map +1 -0
- package/lib/node/workspace-files.spec.d.ts +2 -0
- package/lib/node/workspace-files.spec.d.ts.map +1 -0
- package/lib/node/workspace-files.spec.js +133 -0
- package/lib/node/workspace-files.spec.js.map +1 -0
- package/lib/node/workspace-protocol.d.ts +21 -0
- package/lib/node/workspace-protocol.d.ts.map +1 -0
- package/lib/node/workspace-protocol.js +6 -0
- package/lib/node/workspace-protocol.js.map +1 -0
- package/lib/node/workspace-server-impl.d.ts +28 -0
- package/lib/node/workspace-server-impl.d.ts.map +1 -0
- package/lib/node/workspace-server-impl.js +91 -0
- package/lib/node/workspace-server-impl.js.map +1 -0
- package/lib/node/workspace-service.d.ts +32 -0
- package/lib/node/workspace-service.d.ts.map +1 -0
- package/lib/node/workspace-service.js +212 -0
- package/lib/node/workspace-service.js.map +1 -0
- package/lib/node/workspace-service.spec.d.ts +2 -0
- package/lib/node/workspace-service.spec.d.ts.map +1 -0
- package/lib/node/workspace-service.spec.js +104 -0
- package/lib/node/workspace-service.spec.js.map +1 -0
- package/lib/node/workspace-util.d.ts +30 -0
- package/lib/node/workspace-util.d.ts.map +1 -0
- package/lib/node/workspace-util.js +107 -0
- package/lib/node/workspace-util.js.map +1 -0
- package/lib/node/workspace-util.spec.d.ts +2 -0
- package/lib/node/workspace-util.spec.d.ts.map +1 -0
- package/lib/node/workspace-util.spec.js +44 -0
- package/lib/node/workspace-util.spec.js.map +1 -0
- package/package.json +14 -9
- package/src/node/auth-server-impl.spec.ts +49 -0
- package/src/node/auth-server-impl.ts +252 -0
- package/src/node/client-tool-broker.spec.ts +53 -0
- package/src/node/client-tool-broker.ts +106 -0
- package/src/node/exec-environment-factory.ts +46 -0
- package/src/node/extension-data-notifier.spec.ts +34 -0
- package/src/node/extension-data-notifier.ts +124 -0
- package/src/node/extension-data.spec.ts +105 -0
- package/src/node/extension-data.ts +180 -0
- package/src/node/host-protocol.ts +31 -1
- package/src/node/index.ts +24 -1
- package/src/node/package-host-extension.ts +20 -0
- package/src/node/package-hosts.spec.ts +54 -0
- package/src/node/package-hosts.ts +174 -0
- package/src/node/package-server-impl.ts +87 -0
- package/src/node/package-switch.ts +80 -0
- package/src/node/relay-bridge.ts +94 -0
- package/src/node/session-info-util.spec.ts +23 -0
- package/src/node/session-info-util.ts +55 -0
- package/src/node/session-storage/file-session-storage.spec.ts +72 -0
- package/src/node/session-storage/file-session-storage.ts +145 -0
- package/src/node/session-storage/index.ts +3 -0
- package/src/node/session-storage/session-storage-protocol.ts +81 -0
- package/src/node/session-storage/stored-session.spec.ts +80 -0
- package/src/node/session-storage/stored-session.ts +149 -0
- package/src/node/settings-server-impl.ts +33 -0
- package/src/node/settings-store.spec.ts +155 -0
- package/src/node/settings-store.ts +308 -0
- package/src/node/ta-access.spec.ts +9 -0
- package/src/node/ta-access.ts +65 -7
- package/src/node/ta-errors.spec.ts +25 -0
- package/src/node/ta-errors.ts +21 -1
- package/src/node/ta-server-impl.ts +75 -12
- package/src/node/ta-session-factory.ts +21 -7
- package/src/node/ta-session.ts +43 -3
- package/src/node/terminal-protocol.ts +18 -0
- package/src/node/terminal-service.spec.ts +84 -0
- package/src/node/terminal-service.ts +259 -0
- package/src/node/terminal-util.spec.ts +78 -0
- package/src/node/terminal-util.ts +166 -0
- package/src/node/web-extension-catalog.spec.ts +172 -0
- package/src/node/web-extension-catalog.ts +172 -0
- package/src/node/web-extension-util.ts +144 -0
- package/src/node/working-directory.ts +38 -0
- package/src/node/workspace-files.spec.ts +145 -0
- package/src/node/workspace-files.ts +256 -0
- package/src/node/workspace-protocol.ts +22 -0
- package/src/node/workspace-server-impl.ts +90 -0
- package/src/node/workspace-service.spec.ts +113 -0
- package/src/node/workspace-service.ts +223 -0
- package/src/node/workspace-util.spec.ts +47 -0
- package/src/node/workspace-util.ts +103 -0
- package/esm/node/local-exec-environment-factory.d.ts +0 -4
- package/esm/node/local-exec-environment-factory.js +0 -18
- package/esm/node/local-exec-environment-factory.js.map +0 -1
- package/lib/node/local-exec-environment-factory.d.ts +0 -5
- package/lib/node/local-exec-environment-factory.d.ts.map +0 -1
- package/lib/node/local-exec-environment-factory.js +0 -21
- package/lib/node/local-exec-environment-factory.js.map +0 -1
- package/src/node/local-exec-environment-factory.ts +0 -9
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { Component } from '@celljs/core';
|
|
4
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
5
|
+
import { TaPackages } from '@celljs/ta-harness/lib/node/ta-packages';
|
|
6
|
+
|
|
7
|
+
export const TaWorkingDirectory = Symbol('TaWorkingDirectory');
|
|
8
|
+
|
|
9
|
+
/** The working directory the user chose for ta serve (TaServer.setWorkingDirectory), kept across restarts. */
|
|
10
|
+
export interface TaWorkingDirectory {
|
|
11
|
+
get(): string | undefined;
|
|
12
|
+
set(cwd: string): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** In ta.json of the agent directory, beside Ta's other settings of its own; the file keeps its other keys. */
|
|
16
|
+
@Component(TaWorkingDirectory)
|
|
17
|
+
export class TaWorkingDirectoryImpl implements TaWorkingDirectory {
|
|
18
|
+
protected readonly file = join(TaBrand.agentDir(), TaPackages.SETTINGS_FILE);
|
|
19
|
+
|
|
20
|
+
get(): string | undefined {
|
|
21
|
+
const { workingDirectory } = this.read();
|
|
22
|
+
return typeof workingDirectory === 'string' && workingDirectory ? workingDirectory : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
set(cwd: string): void {
|
|
26
|
+
writeFileSync(`${this.file}.tmp`, `${JSON.stringify({ ...this.read(), workingDirectory: cwd }, null, 2)}\n`);
|
|
27
|
+
renameSync(`${this.file}.tmp`, this.file);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected read(): Record<string, unknown> {
|
|
31
|
+
try {
|
|
32
|
+
const data = JSON.parse(readFileSync(this.file, 'utf8'));
|
|
33
|
+
return data && typeof data === 'object' && !Array.isArray(data) ? data : {};
|
|
34
|
+
} catch {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { createServer, type Server } from 'node:http';
|
|
4
|
+
import type { AddressInfo } from 'node:net';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { type ExecEnvironment, LocalExecEnvironment } from '@celljs/ta-exec';
|
|
8
|
+
import type { Context } from '@celljs/web/lib/node';
|
|
9
|
+
import { DefaultWorkspaceFiles, WorkspaceFilesUtil } from './workspace-files';
|
|
10
|
+
|
|
11
|
+
function write(file: string, content: string | Uint8Array) {
|
|
12
|
+
mkdirSync(join(file, '..'), { recursive: true });
|
|
13
|
+
writeFileSync(file, content);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The middleware in a server of its own; what it passes on answers 418. */
|
|
17
|
+
function serve(files: DefaultWorkspaceFiles): Promise<Server> {
|
|
18
|
+
const server = createServer((request, response) => {
|
|
19
|
+
void files.handle({ request, response } as unknown as Context, async () => {
|
|
20
|
+
response.statusCode = 418;
|
|
21
|
+
response.end();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
return new Promise(resolve => server.listen(0, '127.0.0.1', () => resolve(server)));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function files(remote = false): DefaultWorkspaceFiles {
|
|
28
|
+
const created = new DefaultWorkspaceFiles();
|
|
29
|
+
// Another machine: the same files, read through the environment instead of node:fs.
|
|
30
|
+
const create = (cwd: string): ExecEnvironment => {
|
|
31
|
+
const local = new LocalExecEnvironment(cwd);
|
|
32
|
+
return remote ? Object.assign(Object.create(local), { local: false }) : local;
|
|
33
|
+
};
|
|
34
|
+
Object.assign(created, { environments: { create, defaultCwd: async () => process.cwd() }, host: '127.0.0.1' });
|
|
35
|
+
return created;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('WorkspaceFilesUtil', () => {
|
|
39
|
+
it('reads the id and the path of a URL', () => {
|
|
40
|
+
expect(WorkspaceFilesUtil.parse('/api/files/abc/docs/a%20b.md?v=2')).toEqual({ id: 'abc', path: 'docs/a b.md' });
|
|
41
|
+
expect(WorkspaceFilesUtil.parse('/api/files/abc')).toEqual({ id: 'abc', path: '' });
|
|
42
|
+
expect(WorkspaceFilesUtil.parse('/api/files/abc/%E0%A4%A')).toBeUndefined();
|
|
43
|
+
expect(WorkspaceFilesUtil.parse('/api/ws')).toBeUndefined();
|
|
44
|
+
expect(WorkspaceFilesUtil.url('abc', 'docs/a b#1.md')).toBe('/api/files/abc/docs/a%20b%231.md');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('serves one range', () => {
|
|
48
|
+
expect(WorkspaceFilesUtil.range('bytes=0-9', 100)).toEqual({ start: 0, end: 9 });
|
|
49
|
+
expect(WorkspaceFilesUtil.range('bytes=90-', 100)).toEqual({ start: 90, end: 99 });
|
|
50
|
+
expect(WorkspaceFilesUtil.range('bytes=-10', 100)).toEqual({ start: 90, end: 99 });
|
|
51
|
+
expect(WorkspaceFilesUtil.range('bytes=50-500', 100)).toEqual({ start: 50, end: 99 });
|
|
52
|
+
expect(WorkspaceFilesUtil.range('bytes=100-', 100)).toBe(false);
|
|
53
|
+
expect(WorkspaceFilesUtil.range('bytes=0-1,5-6', 100)).toBeUndefined();
|
|
54
|
+
expect(WorkspaceFilesUtil.range(undefined, 100)).toBeUndefined();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('names the type of a file', () => {
|
|
58
|
+
expect(WorkspaceFilesUtil.contentType('/a/index.HTML')).toBe('text/html; charset=utf-8');
|
|
59
|
+
expect(WorkspaceFilesUtil.contentType('clip.mp4')).toBe('video/mp4');
|
|
60
|
+
expect(WorkspaceFilesUtil.contentType('Makefile')).toBe('application/octet-stream');
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('DefaultWorkspaceFiles', () => {
|
|
65
|
+
let root: string;
|
|
66
|
+
let workspace: string;
|
|
67
|
+
const servers: Server[] = [];
|
|
68
|
+
|
|
69
|
+
beforeAll(() => {
|
|
70
|
+
root = realpathSync(mkdtempSync(join(tmpdir(), 'ta-files-')));
|
|
71
|
+
workspace = join(root, 'workspace');
|
|
72
|
+
write(join(workspace, 'site/index.html'), '<h1>Hi</h1>');
|
|
73
|
+
write(join(workspace, 'site/app.js'), 'alert(1)');
|
|
74
|
+
write(join(workspace, 'clip.mp4'), new Uint8Array(Array.from({ length: 256 }, (_, i) => i)));
|
|
75
|
+
write(join(workspace, 'doc.pdf'), '%PDF-1.4');
|
|
76
|
+
write(join(root, 'secret.txt'), 'secret');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
afterAll(() => {
|
|
80
|
+
for (const server of servers) {
|
|
81
|
+
server.close();
|
|
82
|
+
}
|
|
83
|
+
rmSync(root, { recursive: true, force: true });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
async function base(remote = false): Promise<{ origin: string; files: DefaultWorkspaceFiles }> {
|
|
87
|
+
const created = files(remote);
|
|
88
|
+
const server = await serve(created);
|
|
89
|
+
servers.push(server);
|
|
90
|
+
return { origin: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, files: created };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
it('gives each directory one URL that stays inside it', async () => {
|
|
94
|
+
const { files } = await base();
|
|
95
|
+
const url = files.url(workspace, 'site/index.html');
|
|
96
|
+
expect(url).toMatch(/^\/api\/files\/[\w-]{22}\/site\/index\.html$/);
|
|
97
|
+
expect(files.url(workspace, 'clip.mp4').split('/')[3]).toBe(url.split('/')[3]);
|
|
98
|
+
expect(files.url(join(workspace, 'site'), 'app.js').split('/')[3]).not.toBe(url.split('/')[3]);
|
|
99
|
+
expect(() => files.url(workspace, '../secret.txt')).toThrow('outside');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('answers a page sandboxed, and the files beside it', async () => {
|
|
103
|
+
const { origin, files } = await base();
|
|
104
|
+
const page = await fetch(origin + files.url(workspace, 'site/index.html'));
|
|
105
|
+
expect(page.status).toBe(200);
|
|
106
|
+
expect(await page.text()).toBe('<h1>Hi</h1>');
|
|
107
|
+
expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
|
|
108
|
+
expect(page.headers.get('content-security-policy')).toContain('sandbox allow-scripts');
|
|
109
|
+
expect(page.headers.get('referrer-policy')).toBe('no-referrer');
|
|
110
|
+
const script = await fetch(new URL('app.js', origin + files.url(workspace, 'site/index.html')));
|
|
111
|
+
expect(await script.text()).toBe('alert(1)');
|
|
112
|
+
// A directory answers its index.html.
|
|
113
|
+
expect(await (await fetch(origin + files.url(workspace, 'site'))).text()).toBe('<h1>Hi</h1>');
|
|
114
|
+
// Browsers show no PDF sandboxed.
|
|
115
|
+
expect((await fetch(origin + files.url(workspace, 'doc.pdf'))).headers.get('content-security-policy')).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('serves a range, and asks again only when the file changed', async () => {
|
|
119
|
+
for (const remote of [false, true]) {
|
|
120
|
+
const { origin, files } = await base(remote);
|
|
121
|
+
const url = origin + files.url(workspace, 'clip.mp4');
|
|
122
|
+
const part = await fetch(url, { headers: { range: 'bytes=10-13' } });
|
|
123
|
+
expect(part.status).toBe(206);
|
|
124
|
+
expect(part.headers.get('content-range')).toBe('bytes 10-13/256');
|
|
125
|
+
expect([...new Uint8Array(await part.arrayBuffer())]).toEqual([10, 11, 12, 13]);
|
|
126
|
+
const whole = await fetch(url);
|
|
127
|
+
expect((await whole.arrayBuffer()).byteLength).toBe(256);
|
|
128
|
+
expect((await fetch(url, { headers: { 'if-none-match': whole.headers.get('etag') ?? '' } })).status).toBe(304);
|
|
129
|
+
expect((await fetch(url, { headers: { range: 'bytes=300-' } })).status).toBe(416);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('answers nothing outside the directory, for an unknown id, or by another name', async () => {
|
|
134
|
+
const { origin, files } = await base();
|
|
135
|
+
const url = files.url(workspace, 'clip.mp4');
|
|
136
|
+
const id = url.split('/')[3];
|
|
137
|
+
expect((await fetch(`${origin}/api/files/${id}/..%2Fsecret.txt`)).status).toBe(404);
|
|
138
|
+
expect((await fetch(`${origin}/api/files/${id}/missing.txt`)).status).toBe(404);
|
|
139
|
+
expect((await fetch(`${origin}/api/files/unknown/clip.mp4`)).status).toBe(404);
|
|
140
|
+
expect((await fetch(origin + url, { method: 'POST' })).status).toBe(405);
|
|
141
|
+
expect((await fetch(origin + url, { headers: { host: 'evil.example' } })).status).toBe(404);
|
|
142
|
+
// Other requests go on to the rest of the server.
|
|
143
|
+
expect((await fetch(`${origin}/api/ws`)).status).toBe(418);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { Autowired, Component, Value } from '@celljs/core';
|
|
5
|
+
import { type ExecEnvironment, ExecEnvironmentFactory, type FileStat } from '@celljs/ta-exec';
|
|
6
|
+
import { TA_FILES_PATH } from '@celljs/ta-protocol';
|
|
7
|
+
import { type Context, Middleware } from '@celljs/web/lib/node';
|
|
8
|
+
import { ACCESS_MIDDLEWARE_PRIORITY, TaAccessUtil } from './ta-access';
|
|
9
|
+
import { WorkspaceFiles } from './workspace-protocol';
|
|
10
|
+
import { WorkspaceUtil } from './workspace-util';
|
|
11
|
+
|
|
12
|
+
/** Types of the files the page shows as they are; anything else is `application/octet-stream`. */
|
|
13
|
+
const TYPES: Record<string, string> = {
|
|
14
|
+
html: 'text/html',
|
|
15
|
+
htm: 'text/html',
|
|
16
|
+
css: 'text/css',
|
|
17
|
+
txt: 'text/plain',
|
|
18
|
+
md: 'text/markdown',
|
|
19
|
+
csv: 'text/csv',
|
|
20
|
+
js: 'text/javascript',
|
|
21
|
+
mjs: 'text/javascript',
|
|
22
|
+
cjs: 'text/javascript',
|
|
23
|
+
json: 'application/json',
|
|
24
|
+
map: 'application/json',
|
|
25
|
+
xml: 'application/xml',
|
|
26
|
+
wasm: 'application/wasm',
|
|
27
|
+
pdf: 'application/pdf',
|
|
28
|
+
svg: 'image/svg+xml',
|
|
29
|
+
png: 'image/png',
|
|
30
|
+
jpg: 'image/jpeg',
|
|
31
|
+
jpeg: 'image/jpeg',
|
|
32
|
+
gif: 'image/gif',
|
|
33
|
+
webp: 'image/webp',
|
|
34
|
+
avif: 'image/avif',
|
|
35
|
+
bmp: 'image/bmp',
|
|
36
|
+
ico: 'image/x-icon',
|
|
37
|
+
woff: 'font/woff',
|
|
38
|
+
woff2: 'font/woff2',
|
|
39
|
+
ttf: 'font/ttf',
|
|
40
|
+
otf: 'font/otf',
|
|
41
|
+
mp4: 'video/mp4',
|
|
42
|
+
m4v: 'video/mp4',
|
|
43
|
+
webm: 'video/webm',
|
|
44
|
+
ogv: 'video/ogg',
|
|
45
|
+
mov: 'video/quicktime',
|
|
46
|
+
mp3: 'audio/mpeg',
|
|
47
|
+
m4a: 'audio/mp4',
|
|
48
|
+
aac: 'audio/aac',
|
|
49
|
+
wav: 'audio/wav',
|
|
50
|
+
ogg: 'audio/ogg',
|
|
51
|
+
oga: 'audio/ogg',
|
|
52
|
+
flac: 'audio/flac',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What a page of the workspace may do: run its scripts, in an origin of its own, so that it cannot
|
|
57
|
+
* reach the page of ta or call ta serve. A PDF is left out: browsers do not show one sandboxed.
|
|
58
|
+
*/
|
|
59
|
+
const SANDBOX = 'sandbox allow-scripts allow-forms allow-popups allow-modals allow-downloads';
|
|
60
|
+
|
|
61
|
+
const ID_BYTES = 16;
|
|
62
|
+
/** Read whole from another machine: a part of a larger file is read with `tail` and `head`. */
|
|
63
|
+
const REMOTE_WHOLE_BYTES = 8 * 1024 * 1024;
|
|
64
|
+
|
|
65
|
+
export namespace WorkspaceFilesUtil {
|
|
66
|
+
export function contentType(path: string): string {
|
|
67
|
+
const type = TYPES[path.replace(/^.*[./]/, '').toLowerCase()] ?? 'application/octet-stream';
|
|
68
|
+
return /^text\/|^application\/(json|xml)$/.test(type) ? `${type}; charset=utf-8` : type;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** `/api/files/<id>/<path>`; undefined for other URLs or a path that does not decode. */
|
|
72
|
+
export function parse(url: string | undefined): { id: string; path: string } | undefined {
|
|
73
|
+
const pathname = (url ?? '').split(/[?#]/)[0];
|
|
74
|
+
if (!pathname.startsWith(`${TA_FILES_PATH}/`)) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const rest = pathname.slice(TA_FILES_PATH.length + 1);
|
|
78
|
+
const slash = rest.indexOf('/');
|
|
79
|
+
try {
|
|
80
|
+
return slash < 0 ? { id: rest, path: '' } : { id: rest.slice(0, slash), path: decodeURIComponent(rest.slice(slash + 1)) };
|
|
81
|
+
} catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function url(id: string, path: string): string {
|
|
87
|
+
return `${TA_FILES_PATH}/${id}/${path.split('/').map(encodeURIComponent).join('/')}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The one range of `bytes=a-b`, `bytes=a-` or `bytes=-n` within `size` bytes; undefined for none or
|
|
92
|
+
* several (the whole file is answered), `false` when it lies outside the file.
|
|
93
|
+
*/
|
|
94
|
+
export function range(header: string | undefined, size: number): { start: number; end: number } | false | undefined {
|
|
95
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(header?.trim() ?? '');
|
|
96
|
+
if (!match || (!match[1] && !match[2])) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
const start = match[1] ? Number(match[1]) : Math.max(0, size - Number(match[2]));
|
|
100
|
+
const end = match[1] && match[2] ? Math.min(Number(match[2]), size - 1) : size - 1;
|
|
101
|
+
return start <= end && start < size ? { start, end } : false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function etag(stat: FileStat): string {
|
|
105
|
+
return `"${stat.size.toString(36)}-${Math.floor(stat.mtimeMs).toString(36)}"`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Answers `GET /api/files/<id>/<path>` with the bytes of a file, without the token of ta serve: `<id>`
|
|
111
|
+
* stands for one directory, and only who has the token learns it (TaWorkspaceServer.fileUrl). Paths stay inside
|
|
112
|
+
* that directory; a directory answers its `index.html`. One range is served, so that videos seek.
|
|
113
|
+
*/
|
|
114
|
+
@Component([WorkspaceFiles, Middleware])
|
|
115
|
+
export class DefaultWorkspaceFiles implements WorkspaceFiles, Middleware {
|
|
116
|
+
/** Before TaAccess, which turns away `/api` requests without the token. */
|
|
117
|
+
readonly priority = ACCESS_MIDDLEWARE_PRIORITY + 1;
|
|
118
|
+
|
|
119
|
+
@Autowired(ExecEnvironmentFactory)
|
|
120
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
121
|
+
|
|
122
|
+
@Value('server.host')
|
|
123
|
+
protected readonly host?: string;
|
|
124
|
+
|
|
125
|
+
protected readonly directories = new Map<string, string>();
|
|
126
|
+
protected readonly ids = new Map<string, string>();
|
|
127
|
+
|
|
128
|
+
url(cwd: string, path: string): string {
|
|
129
|
+
const { relative } = WorkspaceUtil.inside(cwd, path);
|
|
130
|
+
let id = this.ids.get(cwd);
|
|
131
|
+
if (!id) {
|
|
132
|
+
id = randomBytes(ID_BYTES).toString('base64url');
|
|
133
|
+
this.ids.set(cwd, id);
|
|
134
|
+
this.directories.set(id, cwd);
|
|
135
|
+
}
|
|
136
|
+
return WorkspaceFilesUtil.url(id, relative);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
|
|
140
|
+
const request = ctx.request as unknown as IncomingMessage;
|
|
141
|
+
const target = WorkspaceFilesUtil.parse(request.url);
|
|
142
|
+
if (!target) {
|
|
143
|
+
await next();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const response = ctx.response as unknown as ServerResponse;
|
|
147
|
+
const cwd = this.directories.get(target.id);
|
|
148
|
+
if (!cwd || !this.acceptsHost(request)) {
|
|
149
|
+
this.fail(response, 404);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
153
|
+
response.setHeader('Allow', 'GET, HEAD');
|
|
154
|
+
this.fail(response, 405);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
await this.send(request, response, cwd, target.path);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
protected async send(request: IncomingMessage, response: ServerResponse, cwd: string, path: string): Promise<void> {
|
|
161
|
+
const env = this.environments.create(cwd);
|
|
162
|
+
const file = await this.find(env, cwd, path);
|
|
163
|
+
if (!file) {
|
|
164
|
+
this.fail(response, 404);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const { absolute, stat } = file;
|
|
168
|
+
const type = WorkspaceFilesUtil.contentType(absolute);
|
|
169
|
+
const etag = WorkspaceFilesUtil.etag(stat);
|
|
170
|
+
response.setHeader('Content-Type', type);
|
|
171
|
+
response.setHeader('ETag', etag);
|
|
172
|
+
response.setHeader('Cache-Control', 'no-cache');
|
|
173
|
+
response.setHeader('Accept-Ranges', 'bytes');
|
|
174
|
+
response.setHeader('X-Content-Type-Options', 'nosniff');
|
|
175
|
+
response.setHeader('Referrer-Policy', 'no-referrer');
|
|
176
|
+
// Pages run in an origin of their own: their module scripts and fetches come from `null`.
|
|
177
|
+
response.setHeader('Access-Control-Allow-Origin', '*');
|
|
178
|
+
if (!type.startsWith('application/pdf')) {
|
|
179
|
+
response.setHeader('Content-Security-Policy', SANDBOX);
|
|
180
|
+
}
|
|
181
|
+
if (request.headers['if-none-match'] === etag) {
|
|
182
|
+
response.statusCode = 304;
|
|
183
|
+
response.end();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const range = WorkspaceFilesUtil.range(request.headers.range, stat.size);
|
|
187
|
+
if (range === false) {
|
|
188
|
+
response.setHeader('Content-Range', `bytes */${stat.size}`);
|
|
189
|
+
this.fail(response, 416);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const { start, end } = range ?? { start: 0, end: stat.size - 1 };
|
|
193
|
+
response.statusCode = range ? 206 : 200;
|
|
194
|
+
if (range) {
|
|
195
|
+
response.setHeader('Content-Range', `bytes ${start}-${end}/${stat.size}`);
|
|
196
|
+
}
|
|
197
|
+
response.setHeader('Content-Length', Math.max(0, end - start + 1));
|
|
198
|
+
if (request.method === 'HEAD' || stat.size === 0) {
|
|
199
|
+
response.end();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (env.local) {
|
|
203
|
+
await this.stream(response, absolute, start, end);
|
|
204
|
+
} else {
|
|
205
|
+
response.end(await this.read(env, absolute, stat.size, start, end));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The file at `path`, or the `index.html` of the directory there. */
|
|
210
|
+
protected async find(env: ExecEnvironment, cwd: string, path: string): Promise<{ absolute: string; stat: FileStat } | undefined> {
|
|
211
|
+
let absolute: string;
|
|
212
|
+
try {
|
|
213
|
+
absolute = WorkspaceUtil.inside(cwd, path).absolute;
|
|
214
|
+
} catch {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
let stat = await env.stat(absolute);
|
|
218
|
+
if (stat?.isDirectory) {
|
|
219
|
+
absolute = WorkspaceUtil.join(absolute, 'index.html');
|
|
220
|
+
stat = await env.stat(absolute);
|
|
221
|
+
}
|
|
222
|
+
return stat && !stat.isDirectory ? { absolute, stat } : undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
protected stream(response: ServerResponse, absolute: string, start: number, end: number): Promise<void> {
|
|
226
|
+
return new Promise(resolve => {
|
|
227
|
+
const stream = createReadStream(absolute, { start, end });
|
|
228
|
+
stream.on('error', () => response.destroy());
|
|
229
|
+
response.once('close', () => {
|
|
230
|
+
stream.destroy();
|
|
231
|
+
resolve();
|
|
232
|
+
});
|
|
233
|
+
stream.pipe(response);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
protected async read(env: ExecEnvironment, absolute: string, size: number, start: number, end: number): Promise<Uint8Array> {
|
|
238
|
+
if (size <= REMOTE_WHOLE_BYTES) {
|
|
239
|
+
return (await env.readFile(absolute)).subarray(start, end + 1);
|
|
240
|
+
}
|
|
241
|
+
const chunks: Uint8Array[] = [];
|
|
242
|
+
const child = env.spawn('/bin/sh', ['-c', 'tail -c +"$1" -- "$0" | head -c "$2"', absolute, String(start + 1), String(end - start + 1)]);
|
|
243
|
+
child.onStdout(data => chunks.push(data));
|
|
244
|
+
await new Promise(resolve => child.onExit(resolve));
|
|
245
|
+
return Buffer.concat(chunks);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
protected acceptsHost(request: IncomingMessage): boolean {
|
|
249
|
+
return !TaAccessUtil.isLoopbackHost(this.host) || TaAccessUtil.isLoopbackHost(request.headers.host);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
protected fail(response: ServerResponse, status: number): void {
|
|
253
|
+
response.statusCode = status;
|
|
254
|
+
response.end();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TaCommandOutput, TaDirEntry, TaFileContent, TaRunCommandOptions } from '@celljs/ta-protocol';
|
|
2
|
+
|
|
3
|
+
export const TaWorkspace = Symbol('TaWorkspace');
|
|
4
|
+
export const WorkspaceFiles = Symbol('WorkspaceFiles');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The files of a directory for the web client, and programs run there: the methods of TaServer of
|
|
8
|
+
* the same names, with `cwd` resolved. Paths are relative to `cwd` and may not leave it; the work
|
|
9
|
+
* runs in the execution environment of `cwd`, as the tools of a session do.
|
|
10
|
+
*/
|
|
11
|
+
export interface TaWorkspace {
|
|
12
|
+
listDirectory(cwd: string, path: string): Promise<TaDirEntry[]>;
|
|
13
|
+
readFile(cwd: string, path: string): Promise<TaFileContent>;
|
|
14
|
+
writeFile(cwd: string, path: string, text: string): Promise<void>;
|
|
15
|
+
findFiles(cwd: string, query: string, limit?: number): Promise<string[]>;
|
|
16
|
+
runCommand(cwd: string, command: string, args: string[], options?: TaRunCommandOptions): Promise<TaCommandOutput>;
|
|
17
|
+
}
|
|
18
|
+
/** The bytes of workspace files over plain HTTP, for the page to show them as they are (TaWorkspaceServer.fileUrl). */
|
|
19
|
+
export interface WorkspaceFiles {
|
|
20
|
+
/** The URL of `path` of `cwd`; throws when it leaves `cwd`. */
|
|
21
|
+
url(cwd: string, path: string): string;
|
|
22
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Autowired } from '@celljs/core';
|
|
2
|
+
import { Rpc } from '@celljs/rpc';
|
|
3
|
+
import { RpcContext } from '@celljs/rpc/lib/node';
|
|
4
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
5
|
+
import {
|
|
6
|
+
type TaCommandOutput,
|
|
7
|
+
type TaDirEntry,
|
|
8
|
+
type TaFileContent,
|
|
9
|
+
type TaOpenTerminalOptions,
|
|
10
|
+
type TaRunCommandOptions,
|
|
11
|
+
type TaTerminalInfo,
|
|
12
|
+
TaTerminalServer,
|
|
13
|
+
TaWorkspaceServer,
|
|
14
|
+
} from '@celljs/ta-protocol';
|
|
15
|
+
import { TaErrors } from './ta-errors';
|
|
16
|
+
import { TaTerminals } from './terminal-protocol';
|
|
17
|
+
import { TaWorkspace, WorkspaceFiles } from './workspace-protocol';
|
|
18
|
+
|
|
19
|
+
/** The files and programs of a directory, for the web client; `cwd` defaults to the server's directory. */
|
|
20
|
+
@Rpc(TaWorkspaceServer)
|
|
21
|
+
export class TaWorkspaceServerImpl implements TaWorkspaceServer {
|
|
22
|
+
@Autowired(ExecEnvironmentFactory)
|
|
23
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
24
|
+
|
|
25
|
+
@Autowired(TaWorkspace)
|
|
26
|
+
protected readonly workspace: TaWorkspace;
|
|
27
|
+
|
|
28
|
+
@Autowired(WorkspaceFiles)
|
|
29
|
+
protected readonly files: WorkspaceFiles;
|
|
30
|
+
|
|
31
|
+
listDirectory(path: string, cwd?: string): Promise<TaDirEntry[]> {
|
|
32
|
+
return TaErrors.guard(async () => this.workspace.listDirectory(await this.cwd(cwd), path));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
readFile(path: string, cwd?: string): Promise<TaFileContent> {
|
|
36
|
+
return TaErrors.guard(async () => this.workspace.readFile(await this.cwd(cwd), path));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
writeFile(path: string, text: string, cwd?: string): Promise<void> {
|
|
40
|
+
return TaErrors.guard(async () => this.workspace.writeFile(await this.cwd(cwd), path, text));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fileUrl(path: string, cwd?: string): Promise<string> {
|
|
44
|
+
return TaErrors.guard(async () => this.files.url(await this.cwd(cwd), path));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
findFiles(query: string, cwd?: string, limit?: number): Promise<string[]> {
|
|
48
|
+
return TaErrors.guard(async () => this.workspace.findFiles(await this.cwd(cwd), query, limit));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
runCommand(command: string, args: string[], options: TaRunCommandOptions = {}): Promise<TaCommandOutput> {
|
|
52
|
+
return TaErrors.guard(async () => this.workspace.runCommand(await this.cwd(options.cwd), command, args, options));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async cwd(cwd?: string): Promise<string> {
|
|
56
|
+
return cwd || (await this.environments.defaultCwd());
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The shells of the connection that calls. */
|
|
61
|
+
@Rpc(TaTerminalServer)
|
|
62
|
+
export class TaTerminalServerImpl implements TaTerminalServer {
|
|
63
|
+
@Autowired(ExecEnvironmentFactory)
|
|
64
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
65
|
+
|
|
66
|
+
@Autowired(TaTerminals)
|
|
67
|
+
protected readonly terminals: TaTerminals;
|
|
68
|
+
|
|
69
|
+
openTerminal(options: TaOpenTerminalOptions): Promise<TaTerminalInfo> {
|
|
70
|
+
return TaErrors.guard(async () =>
|
|
71
|
+
this.terminals.open(RpcContext.requireConnection(), { ...options, cwd: options.cwd || (await this.environments.defaultCwd()) }),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
writeTerminal(terminalId: string, data: string): Promise<void> {
|
|
76
|
+
return TaErrors.guard(() => this.terminals.write(RpcContext.requireConnection(), terminalId, data));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
resizeTerminal(terminalId: string, cols: number, rows: number): Promise<void> {
|
|
80
|
+
return TaErrors.guard(() => this.terminals.resize(RpcContext.requireConnection(), terminalId, cols, rows));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
closeTerminal(terminalId: string): Promise<void> {
|
|
84
|
+
return TaErrors.guard(() => this.terminals.close(RpcContext.requireConnection(), terminalId));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
listTerminals(): Promise<TaTerminalInfo[]> {
|
|
88
|
+
return TaErrors.guard(() => this.terminals.list(RpcContext.requireConnection()));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { execSync } from 'node:child_process';
|
|
3
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { LocalExecEnvironment } from '@celljs/ta-exec';
|
|
7
|
+
import { DefaultTaWorkspace } from './workspace-service';
|
|
8
|
+
|
|
9
|
+
function write(file: string, content: string | Uint8Array) {
|
|
10
|
+
mkdirSync(join(file, '..'), { recursive: true });
|
|
11
|
+
writeFileSync(file, content);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function workspace(): DefaultTaWorkspace {
|
|
15
|
+
const created = new DefaultTaWorkspace();
|
|
16
|
+
Object.assign(created, { environments: { create: (cwd: string) => new LocalExecEnvironment(cwd), defaultCwd: async () => process.cwd() } });
|
|
17
|
+
return created;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('DefaultTaWorkspace', () => {
|
|
21
|
+
let root: string;
|
|
22
|
+
let repo: string;
|
|
23
|
+
const git = (command: string) => execSync(`git -c user.email=t@t -c user.name=t ${command}`, { cwd: repo, stdio: 'pipe' });
|
|
24
|
+
|
|
25
|
+
beforeAll(() => {
|
|
26
|
+
root = realpathSync(mkdtempSync(join(tmpdir(), 'ta-workspace-')));
|
|
27
|
+
repo = join(root, 'repo');
|
|
28
|
+
write(join(repo, '.gitignore'), 'ignored/\n');
|
|
29
|
+
write(join(repo, 'src/app.ts'), 'export const needle = 1;\n');
|
|
30
|
+
write(join(repo, 'src/view10.ts'), 'view\n');
|
|
31
|
+
write(join(repo, 'src/view2.ts'), 'view\n');
|
|
32
|
+
write(join(repo, 'README.md'), '# Needle\n');
|
|
33
|
+
write(join(repo, 'ignored/needle.ts'), 'needle\n');
|
|
34
|
+
write(join(repo, 'logo.png'), new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0, 0, 0]));
|
|
35
|
+
git('init -q -b main');
|
|
36
|
+
git('add -A');
|
|
37
|
+
git('commit -qm init');
|
|
38
|
+
write(join(repo, 'src/app.ts'), 'export const needle = 2;\n');
|
|
39
|
+
write(join(repo, 'src/new.ts'), 'new\n');
|
|
40
|
+
write(join(repo, 'README.md'), '# Needle!\n');
|
|
41
|
+
git('add README.md');
|
|
42
|
+
write(join(root, 'secret.txt'), 'secret');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterAll(() => rmSync(root, { recursive: true, force: true }));
|
|
46
|
+
|
|
47
|
+
it('lists a directory without .git, directories first', async () => {
|
|
48
|
+
const entries = await workspace().listDirectory(repo, '');
|
|
49
|
+
expect(entries.map(entry => entry.name)).toEqual(['ignored', 'src', '.gitignore', 'logo.png', 'README.md']);
|
|
50
|
+
expect(entries.find(entry => entry.name === 'README.md')).toMatchObject({ path: 'README.md', kind: 'file', size: 10 });
|
|
51
|
+
expect((await workspace().listDirectory(repo, 'src')).map(entry => entry.path)).toEqual(['src/app.ts', 'src/new.ts', 'src/view2.ts', 'src/view10.ts']);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('does not leave the directory', async () => {
|
|
55
|
+
await expect(workspace().listDirectory(repo, '..')).rejects.toThrow('outside');
|
|
56
|
+
await expect(workspace().readFile(repo, '../secret.txt')).rejects.toThrow('outside');
|
|
57
|
+
await expect(workspace().readFile(repo, '/etc/hosts')).rejects.toThrow('outside');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('reads text, and only says so of binary files', async () => {
|
|
61
|
+
expect(await workspace().readFile(repo, 'src/app.ts')).toEqual({
|
|
62
|
+
path: 'src/app.ts',
|
|
63
|
+
size: 25,
|
|
64
|
+
binary: false,
|
|
65
|
+
truncated: false,
|
|
66
|
+
text: 'export const needle = 2;\n',
|
|
67
|
+
});
|
|
68
|
+
expect(await workspace().readFile(repo, 'logo.png')).toEqual({ path: 'logo.png', size: 7, binary: true, truncated: false });
|
|
69
|
+
await expect(workspace().readFile(repo, 'src')).rejects.toThrow('not a file');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('writes a file of an existing directory', async () => {
|
|
73
|
+
await workspace().writeFile(root, 'written.txt', 'written\n');
|
|
74
|
+
expect((await workspace().readFile(root, 'written.txt')).text).toBe('written\n');
|
|
75
|
+
await expect(workspace().writeFile(repo, 'missing/a.txt', 'a')).rejects.toThrow('cannot be written');
|
|
76
|
+
await expect(workspace().writeFile(repo, 'src', 'a')).rejects.toThrow('cannot be written');
|
|
77
|
+
await expect(workspace().writeFile(repo, '../secret.txt', 'a')).rejects.toThrow('outside');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('reads the start of a large file', async () => {
|
|
81
|
+
write(join(root, 'big/large.txt'), 'x'.repeat(1024 * 1024 + 10));
|
|
82
|
+
const content = await workspace().readFile(join(root, 'big'), 'large.txt');
|
|
83
|
+
expect(content).toMatchObject({ truncated: true, size: 1024 * 1024 + 10 });
|
|
84
|
+
expect(content.text).toHaveLength(1024 * 1024);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('finds files by name, without the ignored ones', async () => {
|
|
88
|
+
expect(await workspace().findFiles(repo, 'app')).toEqual(['src/app.ts']);
|
|
89
|
+
const all = await workspace().findFiles(repo, '', 100);
|
|
90
|
+
expect(all).toContain('src/new.ts');
|
|
91
|
+
expect(all).not.toContain('ignored/needle.ts');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('runs a program without a shell, and keeps to its limits', async () => {
|
|
95
|
+
const status = await workspace().runCommand(repo, 'git', ['status', '--porcelain']);
|
|
96
|
+
expect(status).toMatchObject({ exitCode: 0, truncated: false, timedOut: false });
|
|
97
|
+
expect(status.stdout).toContain(' M src/app.ts');
|
|
98
|
+
const quoted = await workspace().runCommand(repo, 'printf', ['%s|', '$HOME', 'a b', "it's"]);
|
|
99
|
+
expect(quoted.stdout).toBe("$HOME|a b|it's|");
|
|
100
|
+
expect((await workspace().runCommand(repo, 'cat', [], { input: 'fed' })).stdout).toBe('fed');
|
|
101
|
+
expect((await workspace().runCommand(repo, 'cat', [])).stdout).toBe('');
|
|
102
|
+
const failed = await workspace().runCommand(repo, 'git', ['show', 'nope']);
|
|
103
|
+
expect(failed.exitCode).not.toBe(0);
|
|
104
|
+
expect(failed.stderr).toContain('nope');
|
|
105
|
+
expect(await workspace().runCommand(repo, 'head', ['-c', '100', '/dev/zero'], { maxBytes: 10 })).toMatchObject({
|
|
106
|
+
truncated: true,
|
|
107
|
+
stdout: '\0'.repeat(10),
|
|
108
|
+
});
|
|
109
|
+
expect(await workspace().runCommand(repo, 'sleep', ['5'], { timeoutMs: 100 })).toMatchObject({ timedOut: true, exitCode: null });
|
|
110
|
+
expect((await workspace().runCommand(repo, 'no-such-program-of-ta', [])).exitCode).not.toBe(0);
|
|
111
|
+
await expect(workspace().runCommand(repo, '', [])).rejects.toThrow('No program');
|
|
112
|
+
});
|
|
113
|
+
});
|