@coda-console/resource-manager 0.9.4 → 0.9.5

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/lib/media.mjs CHANGED
@@ -8,6 +8,7 @@ import { ErrorCode, servedType } from '../view/protocol.mjs';
8
8
  import { fromFsError, resolveInRoot } from './paths.mjs';
9
9
 
10
10
  const NOFOLLOW = constants.O_NOFOLLOW ?? 0;
11
+ const NONBLOCK = constants.O_NONBLOCK ?? 0;
11
12
 
12
13
  const CORS = { 'access-control-allow-origin': '*', 'access-control-expose-headers': 'content-length, content-range, etag' };
13
14
 
@@ -50,7 +51,8 @@ export function createMedia() {
50
51
  if (!type) return end(415);
51
52
  let handle;
52
53
  try {
53
- handle = await open(await resolveInRoot(root, path), constants.O_RDONLY | NOFOLLOW);
54
+ // 加 O_NONBLOCK:避免指向命名管道(FIFO)时在 open 阶段永久阻塞,随后由 info.isFile() 及时拒绝。
55
+ handle = await open(await resolveInRoot(root, path), constants.O_RDONLY | NOFOLLOW | NONBLOCK);
54
56
  const info = await handle.stat();
55
57
  if (!info.isFile()) return end(404);
56
58
  const { size } = info;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "@coda-console/resource-manager",
4
4
  "displayName": "资源管理器",
5
5
  "description": "会话旁的只读文件浏览器:浏览会话工作目录,预览文本、图片、音频、视频和 3D 模型,把路径交给对话。",
6
- "version": "0.9.4",
6
+ "version": "0.9.5",
7
7
  "categories": ["工作流程"],
8
8
  "license": "MIT",
9
9
  "repository": {
package/view/app.mjs CHANGED
@@ -14,7 +14,7 @@ import { Model, STATE_LIMIT, View, noticeText, parseState, putSession, trimState
14
14
  import { showNotice } from './notice.mjs';
15
15
  import { mountPreview } from './preview.mjs';
16
16
  import { mountPress, onMotionChange, reducedMotion } from './press.mjs';
17
- import { EntryKind, RootState, SortKey, Type } from './protocol.mjs';
17
+ import { EntryKind, RootState, SortKey, Type, externalPreviewOf } from './protocol.mjs';
18
18
  import { createRestore, createTrail } from './restore.mjs';
19
19
  import { createSearch } from './search.mjs';
20
20
  import { mountSearchbar } from './searchbar.mjs';
@@ -169,12 +169,60 @@ function openInView(path) {
169
169
  }
170
170
  // 两端都在页内打开:model.openFile 设 selected 和 opened,经 onChange 切到预览。iPhone 触屏没有键盘焦点,不交焦点;
171
171
  // 快速两次轻点:第一次已切到预览,目录树 .off 不接指针事件,第二次落不到条目上。
172
- // 打开文件的统一入口(目录树、图标视图和搜索结果):焦点交接和过渡意图在这里记,不随调用方复制。select 为假(搜索结果)时只设预览,不改 model.selected。
173
- function openFile(path, { select = true } = {}) {
172
+ // 独立自增序号(不复用 epoch)与单在途串行队列:
173
+ // 同一页面同时只允许一个 coda.open 在途,在途期间的新打开意图合并到最新一次;
174
+ // 换会话、根目录变化、切目录、打开属性页或改开页内文件时递增序号并清空待执行队列,防止旧操作晚完成或晚失败覆盖新状态。
175
+ let openSeq = 0;
176
+ let openingExternal = false;
177
+ let queuedExternal = null;
178
+ const cancelExternal = () => { openSeq++; queuedExternal = null; };
179
+ const encodePath = path => path.split('/').map(encodeURIComponent).join('/');
180
+ function openInline(path, { select = true } = {}) {
174
181
  openPressed = !isIOS; // 用户在 Mac 上打开的:render 把焦点交给预览面板
175
182
  note({ kind: 'push', from: mode(), to: 'preview' });
176
183
  model.openFile(path, { select });
177
184
  }
185
+ async function drainExternal(first) {
186
+ openingExternal = true;
187
+ let current = first;
188
+ try {
189
+ while (current) {
190
+ const { path, select, seq } = current;
191
+ current = null;
192
+ if (seq === openSeq) {
193
+ try {
194
+ const granted = await conn.link.call(Type.MEDIA, { path });
195
+ if (seq === openSeq) {
196
+ if (!granted?.token) throw new Error('no media service');
197
+ await coda.open(`coda-view://service/media/${granted.token}/${encodePath(path)}`);
198
+ }
199
+ } catch {
200
+ if (seq === openSeq && model.ready) openInline(path, { select });
201
+ }
202
+ }
203
+ if (queuedExternal) {
204
+ current = queuedExternal;
205
+ queuedExternal = null;
206
+ }
207
+ }
208
+ } finally {
209
+ openingExternal = false;
210
+ }
211
+ }
212
+ // 打开文件的统一入口(目录树、图标视图和搜索结果):PDF/Office/iWork 与 Markdown 优先经本机流式服务走 coda.open,失败静默退回页内预览;其余保持页内预览。
213
+ // 走 coda.open 成功时不设 model.opened(因此不记入 setState 的 opened)。select 为假(搜索结果)时回退页内预览只设预览,不改 model.selected。
214
+ function openFile(path, { select = true } = {}) {
215
+ cancelExternal();
216
+ const seq = openSeq;
217
+ const via = typeof coda.open === 'function' && model.ready && model.root?.path ? externalPreviewOf(path) : null;
218
+ if (via) {
219
+ const task = { path, select, seq };
220
+ if (openingExternal) queuedExternal = task;
221
+ else drainExternal(task);
222
+ return;
223
+ }
224
+ openInline(path, { select });
225
+ }
178
226
 
179
227
  // ---- 渲染:同一批变化合并成一次 ----
180
228
  let queued = false;
@@ -614,6 +662,7 @@ $('preview').addEventListener('keydown', event => {
614
662
  let shownCwd = '';
615
663
  const enterDir = taken(path => enterInto(path));
616
664
  function enterInto(path) {
665
+ cancelExternal();
617
666
  trail.remember(model.cwd, grid.scroll());
618
667
  selectEntry(null); // 进入新的一层:选中清空
619
668
  model.enter(path);
@@ -627,6 +676,7 @@ function enterInto(path) {
627
676
  function leaveDir(levels = 1, record = true) {
628
677
  takeover++; // 用户经按钮、键盘、返回退出图标视图的一层
629
678
  if (model.cwd === '') return false;
679
+ cancelExternal();
630
680
  const from = model.cwd.split('/');
631
681
  model.leave(levels);
632
682
  if (record) note({ kind: 'fade', to: model.cwd });
@@ -690,6 +740,7 @@ if (nativeBack) quiet(() => coda.on('back', event => {
690
740
 
691
741
  // ---- 属性页:所有关闭的路径(原生返回、横屏的返回、换会话、根目录变化)都重新声明回上层 ----
692
742
  function openInfo(row) {
743
+ cancelExternal();
693
744
  note({ kind: 'push', from: mode(), to: 'info' });
694
745
  infoOpen = true;
695
746
  infoStopped = false;
@@ -723,6 +774,7 @@ function onSession(data) {
723
774
  locator.cancel();
724
775
  clearSearch(); // 换会话:搜索词、结果、结果的选中、定位都清掉
725
776
  epoch++;
777
+ cancelExternal();
726
778
  hint.hide();
727
779
  const saved = persisted.sessions[data.session] ?? null;
728
780
  pending = saved?.opened ? { preview: { top: saved.scroll.top, left: saved.scroll.left }, path: saved.opened } : { preview: null, path: null };
@@ -739,7 +791,7 @@ function onSession(data) {
739
791
  function onRoot(data) {
740
792
  const before = model.root;
741
793
  if (data.state !== before?.state || data.path !== before?.path) { menu.hide(); intent = null; endRunning(); } // 开着的菜单里的路径是旧根目录下的;换根目录不播过渡
742
- if (data.state !== RootState.READY || before?.path !== data.path) closeInfo(false, true); // 属性页看的是旧根目录下的路径
794
+ if (data.state !== RootState.READY || before?.path !== data.path) { cancelExternal(); closeInfo(false, true); } // 属性页看的是旧根目录下的路径
743
795
  const scope = model.scope;
744
796
  if (data.state !== RootState.READY) { search.suspend(); locator.cancel(); } // 根目录暂不可用:取消在途搜索,丢掉结果,搜索词留着;定位作废(再就绪也不接着做)
745
797
  const done = model.receive(data);
package/view/protocol.mjs CHANGED
@@ -78,25 +78,84 @@ const MEDIA_TYPES = {
78
78
  };
79
79
  // 模型的伴随文件:服务要供应(gltf 的 .bin、obj 的 .mtl),但页面不把它们当媒体预览(.mtl 仍是文本,.bin 仍是二进制)。
80
80
  const COMPANION_TYPES = { bin: 'application/octet-stream', mtl: 'text/plain; charset=utf-8' };
81
+ // 经 Coda 的 coda.open 预览的文档类型:统一经插件本机流式服务供应,复用 resolveInRoot 与 O_NOFOLLOW 校验,防止越出工作目录。
82
+ const DOCUMENT_TYPES = {
83
+ md: 'text/markdown; charset=utf-8',
84
+ markdown: 'text/markdown; charset=utf-8',
85
+ pdf: 'application/pdf',
86
+ doc: 'application/msword',
87
+ docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
88
+ xls: 'application/vnd.ms-excel',
89
+ xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
90
+ ppt: 'application/vnd.ms-powerpoint',
91
+ pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
92
+ rtf: 'application/rtf',
93
+ pages: 'application/vnd.apple.pages',
94
+ numbers: 'application/vnd.apple.numbers',
95
+ key: 'application/vnd.apple.keynote',
96
+ };
97
+ // Markdown 相对链接可能指向的常见目录内文本/代码文件:只供流式服务响应相对链接,直接点击时仍走页内预览。
98
+ const LINK_TARGET_TYPES = {
99
+ txt: 'text/plain; charset=utf-8',
100
+ csv: 'text/csv; charset=utf-8',
101
+ tsv: 'text/tab-separated-values; charset=utf-8',
102
+ json: 'application/json; charset=utf-8',
103
+ yaml: 'text/yaml; charset=utf-8',
104
+ yml: 'text/yaml; charset=utf-8',
105
+ xml: 'application/xml; charset=utf-8',
106
+ html: 'text/html; charset=utf-8',
107
+ htm: 'text/html; charset=utf-8',
108
+ css: 'text/css; charset=utf-8',
109
+ js: 'text/javascript; charset=utf-8',
110
+ mjs: 'text/javascript; charset=utf-8',
111
+ ts: 'text/plain; charset=utf-8',
112
+ tsx: 'text/plain; charset=utf-8',
113
+ jsx: 'text/plain; charset=utf-8',
114
+ py: 'text/plain; charset=utf-8',
115
+ go: 'text/plain; charset=utf-8',
116
+ rs: 'text/plain; charset=utf-8',
117
+ sh: 'text/plain; charset=utf-8',
118
+ sql: 'text/plain; charset=utf-8',
119
+ toml: 'text/plain; charset=utf-8',
120
+ ini: 'text/plain; charset=utf-8',
121
+ log: 'text/plain; charset=utf-8',
122
+ };
81
123
  // 查证过 Coda 播放器不支持的视频:按扩展名直接不预览,不读字节(mkv 容器不支持,avi 的 canPlayType 说 maybe 但实际报错)。
82
124
  const NO_PREVIEW = new Set(['mkv', 'avi']);
83
125
 
126
+ function extensionOf(name) {
127
+ const str = String(name);
128
+ const base = str.slice(str.lastIndexOf('/') + 1);
129
+ const dot = base.lastIndexOf('.');
130
+ return dot <= 0 ? null : base.slice(dot + 1).toLowerCase();
131
+ }
132
+
84
133
  // 按扩展名给出 { kind: 'audio' | 'video' | 'image' | 'model', type, preview };不是媒体文件返回 null。preview 为 false 的没有 type,不经服务。
85
134
  export function mediaOf(name) {
86
- const dot = String(name).lastIndexOf('.');
87
- if (dot < 0) return null;
88
- const ext = name.slice(dot + 1).toLowerCase();
135
+ const ext = extensionOf(name);
136
+ if (!ext) return null;
89
137
  if (NO_PREVIEW.has(ext)) return { kind: 'video', type: null, preview: false };
90
- const entry = MEDIA_TYPES[ext];
138
+ const entry = Object.hasOwn(MEDIA_TYPES, ext) ? MEDIA_TYPES[ext] : null;
91
139
  return entry ? { kind: entry[0], type: entry[1], preview: true } : null;
92
140
  }
93
141
 
94
- // 流式服务肯供应的文件的 MIME 类型:能预览的媒体加上模型的伴随文件;其余返回 null(服务回 415)。
142
+ // 按扩展名判断是否优先经本机流式服务走 coda.open(PDF / Office / iWork / Markdown 返回 'media',其余返回 null 保持页内预览)。
143
+ export function externalPreviewOf(name) {
144
+ const ext = extensionOf(name);
145
+ if (!ext) return null;
146
+ return Object.hasOwn(DOCUMENT_TYPES, ext) ? 'media' : null;
147
+ }
148
+
149
+ // 流式服务肯供应的文件的 MIME 类型:能预览的媒体、模型的伴随文件、经 coda.open 预览的文档和 Markdown 相对链接目标;其余返回 null(服务回 415)。
95
150
  export function servedType(name) {
96
151
  const media = mediaOf(name);
97
152
  if (media) return media.preview ? media.type : null;
98
- const dot = String(name).lastIndexOf('.');
99
- return dot < 0 ? null : COMPANION_TYPES[name.slice(dot + 1).toLowerCase()] ?? null;
153
+ const ext = extensionOf(name);
154
+ if (!ext) return null;
155
+ if (Object.hasOwn(COMPANION_TYPES, ext)) return COMPANION_TYPES[ext];
156
+ if (Object.hasOwn(DOCUMENT_TYPES, ext)) return DOCUMENT_TYPES[ext];
157
+ if (Object.hasOwn(LINK_TARGET_TYPES, ext)) return LINK_TARGET_TYPES[ext];
158
+ return null;
100
159
  }
101
160
 
102
161
  // 相对路径的规则只在这一处:模块(lib/paths.mjs)校验请求,页面恢复保存的 cwd 时用同一个函数。