dsh-ai-subtitle 0.3.0 → 0.3.2
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.en.md +37 -1
- package/README.ja.md +23 -1
- package/README.ko.md +23 -1
- package/README.md +22 -0
- package/client.js +47 -1
- package/index.js +14 -1
- package/package.json +1 -1
- package/src/admission.js +57 -0
- package/src/api.js +13 -1
package/README.en.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://www.npmjs.com/package/dsh-ai-subtitle)
|
|
8
8
|
[](https://nodejs.org/)
|
|
9
9
|
|
|
10
|
-
<!-- translated-from: README.md sha256:
|
|
10
|
+
<!-- translated-from: README.md sha256:f3562069ca01 -->
|
|
11
11
|
|
|
12
12
|
A subtitle plugin for DeepSeek Harness (DSH) that turns Japanese video into bilingual
|
|
13
13
|
Japanese/Chinese SRT files. The plugin installs every runtime component it needs
|
|
@@ -48,6 +48,42 @@ video/audio ──ffmpeg──▶ 16 kHz wav ──energy VAD + Whisper──▶
|
|
|
48
48
|
|
|
49
49
|
Not implemented yet: ASS export with styling.
|
|
50
50
|
|
|
51
|
+
## Getting started
|
|
52
|
+
|
|
53
|
+
The panel has a **Usage** button at the bottom that opens the same walkthrough; for a
|
|
54
|
+
first run:
|
|
55
|
+
|
|
56
|
+
1. **Install the runtime** (once): ffmpeg, Python and PyTorch, about 3 GB. CUDA is used
|
|
57
|
+
automatically when an NVIDIA GPU is present, otherwise CPU. Optionally download the
|
|
58
|
+
Whisper model at the same time (about 1.6 GB).
|
|
59
|
+
2. **Transcribe**: pick a video or audio file and start. The task card shows progress and
|
|
60
|
+
can be cancelled; the project appears in the list when done.
|
|
61
|
+
3. **Translate**: open the project, pick a model, then **Translate this batch** or
|
|
62
|
+
**Translate all remaining**. Tick **Also proofread the Japanese** to correct ASR
|
|
63
|
+
errors first.
|
|
64
|
+
4. **Review**: **Edit cues** changes Japanese or Chinese text, splits and merges cues, nudges
|
|
65
|
+
timing by 0.1 s and deletes lines you do not want; notes attached to a cue or a batch
|
|
66
|
+
are re-applied with **Save notes and retranslate**.
|
|
67
|
+
5. **Export**: **Export subtitles** writes a `.srt` named after the video.
|
|
68
|
+
|
|
69
|
+
**Background and glossary** in the same dialog carries tags and a note into every
|
|
70
|
+
translation prompt — names, works and tone belong there. The glossary imports pasted CSV
|
|
71
|
+
and exports the same format.
|
|
72
|
+
|
|
73
|
+
### Notes
|
|
74
|
+
|
|
75
|
+
- Windows only, DSH 0.2.1 or newer;
|
|
76
|
+
- transcription runs locally; translation sends only the selected subtitle text to the
|
|
77
|
+
model service you chose in DSH;
|
|
78
|
+
- the plugin stores no API keys — DSH owns the credentials;
|
|
79
|
+
- projects default to `Documents\\DshSubtitle\\projects` and can be changed or opened from
|
|
80
|
+
the panel;
|
|
81
|
+
- hour-long videos take a while to transcribe; transcribe first, then translate in batches;
|
|
82
|
+
- the panel API accepts only same-origin requests from the DSH page; another program calling
|
|
83
|
+
it directly is refused;
|
|
84
|
+
- deleting a project offers "remove from list" or "delete the local files"; the second
|
|
85
|
+
cannot be undone.
|
|
86
|
+
|
|
51
87
|
## Installation
|
|
52
88
|
|
|
53
89
|
```sh
|
package/README.ja.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://www.npmjs.com/package/dsh-ai-subtitle)
|
|
8
8
|
[](https://nodejs.org/)
|
|
9
9
|
|
|
10
|
-
<!-- translated-from: README.md sha256:
|
|
10
|
+
<!-- translated-from: README.md sha256:f3562069ca01 -->
|
|
11
11
|
|
|
12
12
|
DeepSeek Harness(DSH)の字幕プラグインです。日本語動画を日中バイリンガルの SRT に変換します。必要なランタイム(ffmpeg、CPython、PyTorch、transformers)は指定ディレクトリにすべて導入されるため、Python・ffmpeg・モデルを事前に用意する必要はありません。
|
|
13
13
|
|
|
@@ -35,6 +35,28 @@ DeepSeek Harness(DSH)の字幕プラグインです。日本語動画を日
|
|
|
35
35
|
|
|
36
36
|
未実装:ASS 出力(スタイル付き字幕)。
|
|
37
37
|
|
|
38
|
+
## 使い方
|
|
39
|
+
|
|
40
|
+
パネル下部の「使用説明」から同じ流れを開けます。初回は次の順番で:
|
|
41
|
+
|
|
42
|
+
1. **ランタイムをインストール**(一度だけ):ffmpeg、Python、PyTorch を約 3 GB ダウンロード。NVIDIA GPU があれば CUDA、なければ CPU。Whisper モデル(約 1.6 GB)を同時に落とすこともできます。
|
|
43
|
+
2. **文字起こし**:動画または音声を選んで開始。タスクカードに進捗が出て、キャンセルも可能。完了するとプロジェクトが一覧に追加されます。
|
|
44
|
+
3. **翻訳**:プロジェクトを開き、モデルを選んで「このバッチを翻訳」または「残りをすべて翻訳」。認識ミスも直すなら「日本語の認識も校正」にチェック。
|
|
45
|
+
4. **校正・編集**:「行ごとに編集」で日本語・中国語の修正、分割・結合、0.1 秒単位の時間調整、不要な行の削除ができます。各行やバッチへの意見は「意見を保存して再翻訳」で反映されます。
|
|
46
|
+
5. **書き出し**:「字幕を書き出し」で動画と同名の `.srt` ができます。
|
|
47
|
+
|
|
48
|
+
「背景と用語」のタグと説明は毎回の翻訳プロンプトに含まれます。人名・作品名・番組の雰囲気はここに書いてください。用語集は CSV の貼り付け取り込みと書き出しに対応しています。
|
|
49
|
+
|
|
50
|
+
### 注意
|
|
51
|
+
|
|
52
|
+
- Windows のみ、DSH 0.2.1 以上;
|
|
53
|
+
- 文字起こしはローカルで実行されます。翻訳は選んだ字幕テキストだけを DSH で設定したモデルサービスへ送ります;
|
|
54
|
+
- API key は保存しません(認証情報は DSH が管理);
|
|
55
|
+
- プロジェクトは既定で「ドキュメント\\DshSubtitle\\projects」。パネルから変更・オープンできます;
|
|
56
|
+
- 数時間の動画は時間がかかるため、先に文字起こしを済ませてから分割して翻訳するのがおすすめです;
|
|
57
|
+
- パネルの API は DSH ページからの同一オリジン要求のみ受け付けます。他のプログラムからの直接呼び出しは拒否されます。
|
|
58
|
+
- プロジェクト削除は「一覧から外す」と「ローカルファイルも削除」を選べます。後者は元に戻せません。
|
|
59
|
+
|
|
38
60
|
## 導入
|
|
39
61
|
|
|
40
62
|
```sh
|
package/README.ko.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://www.npmjs.com/package/dsh-ai-subtitle)
|
|
8
8
|
[](https://nodejs.org/)
|
|
9
9
|
|
|
10
|
-
<!-- translated-from: README.md sha256:
|
|
10
|
+
<!-- translated-from: README.md sha256:f3562069ca01 -->
|
|
11
11
|
|
|
12
12
|
DeepSeek Harness(DSH)용 자막 플러그인으로, 일본어 영상을 일중 이중 언어 SRT로 변환합니다. 필요한 런타임(ffmpeg, CPython, PyTorch, transformers)은 지정한 디렉터리에 모두 설치되므로 Python, ffmpeg, 모델을 미리 준비할 필요가 없습니다.
|
|
13
13
|
|
|
@@ -35,6 +35,28 @@ DeepSeek Harness(DSH)용 자막 플러그인으로, 일본어 영상을 일중
|
|
|
35
35
|
|
|
36
36
|
미구현: ASS 내보내기(스타일 자막).
|
|
37
37
|
|
|
38
|
+
## 사용 방법
|
|
39
|
+
|
|
40
|
+
패널 하단의 「사용 설명」에서 같은 안내를 열 수 있습니다. 처음 사용하는 순서:
|
|
41
|
+
|
|
42
|
+
1. **런타임 설치**(한 번만): ffmpeg, Python, PyTorch 약 3 GB를 내려받습니다. NVIDIA GPU가 있으면 CUDA, 없으면 CPU. Whisper 모델(약 1.6 GB)을 함께 받을 수도 있습니다.
|
|
43
|
+
2. **전사**: 영상이나 음성을 선택해 시작합니다. 작업 카드에 진행률이 보이고 취소할 수 있으며, 끝나면 프로젝트가 목록에 추가됩니다.
|
|
44
|
+
3. **번역**: 프로젝트를 열고 모델을 고른 뒤 「이 배치 번역」 또는 「남은 배치 모두 번역」. 인식 오류도 고치려면 「일본어 인식도 교정」을 선택합니다.
|
|
45
|
+
4. **교정/수정**: 「줄별 수정」에서 일본어·중국어 수정, 분할·병합, 0.1초 단위 시간 조정, 불필요한 줄 삭제가 가능합니다. 줄이나 배치에 의견을 남기고 「의견 저장 후 재번역」을 누르면 반영됩니다.
|
|
46
|
+
5. **내보내기**: 「자막 내보내기」로 영상과 같은 이름의 `.srt`를 만듭니다.
|
|
47
|
+
|
|
48
|
+
「배경과 용어」의 태그와 설명은 매번 번역 프롬프트에 포함됩니다. 인명·작품명·프로그램 분위기는 여기에 적으세요. 용어집은 CSV 붙여넣기 가져오기와 내보내기를 지원합니다.
|
|
49
|
+
|
|
50
|
+
### 주의
|
|
51
|
+
|
|
52
|
+
- Windows 전용, DSH 0.2.1 이상;
|
|
53
|
+
- 전사는 로컬에서 실행됩니다. 번역은 선택한 자막 텍스트만 DSH에 설정된 모델 서비스로 보냅니다;
|
|
54
|
+
- API key를 저장하지 않습니다(자격 증명은 DSH가 관리);
|
|
55
|
+
- 프로젝트 기본 위치는 「문서\\DshSubtitle\\projects」이며 패널에서 변경하거나 열 수 있습니다;
|
|
56
|
+
- 몇 시간짜리 영상은 전사에 시간이 걸리므로 먼저 전사한 뒤 여러 배치로 나눠 번역하는 편이 좋습니다;
|
|
57
|
+
- 패널 API는 DSH 페이지의 동일 출처 요청만 허용합니다. 다른 프로그램이 직접 호출하면 거부됩니다.
|
|
58
|
+
- 프로젝트 삭제 시 「목록에서 제거」와 「로컬 파일까지 삭제」를 고를 수 있으며 후자는 되돌릴 수 없습니다.
|
|
59
|
+
|
|
38
60
|
## 설치
|
|
39
61
|
|
|
40
62
|
```sh
|
package/README.md
CHANGED
|
@@ -33,6 +33,28 @@ DeepSeek Harness(DSH)的字幕插件,把日语视频转成中日双语 SRT
|
|
|
33
33
|
|
|
34
34
|
尚未实现:ASS 导出(带样式的字幕)。
|
|
35
35
|
|
|
36
|
+
## 使用流程
|
|
37
|
+
|
|
38
|
+
面板底部有「使用说明」,点开是同样的一段流程;这里给第一次使用的人:
|
|
39
|
+
|
|
40
|
+
1. **安装运行时**(只做一次):点「安装运行时」,下载 ffmpeg、Python、PyTorch(约 3 GB)。有 NVIDIA 显卡自动用 CUDA,没有则用 CPU;可以勾选同时下载 Whisper 模型(约 1.6 GB)。
|
|
41
|
+
2. **转写**:选择视频或音频,点开始。任务卡显示进度、可取消;完成后项目进入列表。
|
|
42
|
+
3. **翻译**:打开项目 → 选模型 → 「自动翻译本批」或「自动翻译全部剩余」。想同时修正识别错误,勾选「同时校正日文识别」。
|
|
43
|
+
4. **校对与修改**:「逐条修改」可改日文/中文、拆分合并、按 0.1 秒微调时间、删除不需要的行;给某条或整批写意见后「保存意见并重译本批」。
|
|
44
|
+
5. **导出**:点「导出字幕」,得到与视频同名的 `.srt`。
|
|
45
|
+
|
|
46
|
+
「背景与术语」里的标签和说明会随每次翻译交给模型,适合写人名、作品名和节目风格;术语表支持粘贴 CSV 导入,也可以导出。
|
|
47
|
+
|
|
48
|
+
### 注意事项
|
|
49
|
+
|
|
50
|
+
- 仅支持 Windows,需要 DSH 0.2.1 及以上;
|
|
51
|
+
- 转写在本机完成;翻译只把你选中的字幕文本发给你在 DSH 里选择的模型服务;
|
|
52
|
+
- 插件不保存 API key,模型凭证由 DSH 管理;
|
|
53
|
+
- 项目默认在「文档\\DshSubtitle\\projects」,可在面板里更改或直接打开;
|
|
54
|
+
- 数小时的视频转写较慢,建议先转写完再分几批翻译;
|
|
55
|
+
- 面板接口只接受来自 DSH 页面的同源请求,其他程序直接调用会被拒绝;
|
|
56
|
+
- 删除项目时可选「仅从列表移除」或「连本地文件一起删除」,后者不可撤销。
|
|
57
|
+
|
|
36
58
|
## 安装
|
|
37
59
|
|
|
38
60
|
```sh
|
package/client.js
CHANGED
|
@@ -10,7 +10,7 @@ window.__ModuleLoader__.load({
|
|
|
10
10
|
const PANEL_ID = 'subtitle-studio-panel'
|
|
11
11
|
const API = '/dsh-ai-subtitle/api'
|
|
12
12
|
// Bump whenever the bundle needs to be distinguishable in the running page.
|
|
13
|
-
const BUILD = 'ui-2026-12-
|
|
13
|
+
const BUILD = 'ui-2026-12-05b'
|
|
14
14
|
function trace(message) {
|
|
15
15
|
try {
|
|
16
16
|
const payload = JSON.stringify({ msg: String(message).slice(0, 300) })
|
|
@@ -470,6 +470,7 @@ window.__ModuleLoader__.load({
|
|
|
470
470
|
const [pickerTarget, setPickerTarget] = useState('install')
|
|
471
471
|
const [projectsInfo, setProjectsInfo] = useState(null)
|
|
472
472
|
const [projectsDialog, setProjectsDialog] = useState(false)
|
|
473
|
+
const [helpDialog, setHelpDialog] = useState(false)
|
|
473
474
|
const [projectsDraft, setProjectsDraft] = useState('')
|
|
474
475
|
const [projectsMove, setProjectsMove] = useState(true)
|
|
475
476
|
const [places, setPlaces] = useState([])
|
|
@@ -1503,6 +1504,13 @@ window.__ModuleLoader__.load({
|
|
|
1503
1504
|
),
|
|
1504
1505
|
),
|
|
1505
1506
|
|
|
1507
|
+
h(
|
|
1508
|
+
'div',
|
|
1509
|
+
{ key: 'footer', style: { display: 'flex', gap: 8, alignItems: 'center', marginTop: 14, flexWrap: 'wrap' } },
|
|
1510
|
+
h(B, { style: medium, onClick: () => setHelpDialog(true) }, '使用说明'),
|
|
1511
|
+
h('span', { style: { ...code, fontSize: 11 } }, '首次使用先安装运行时;翻译用 DSH 已配置的模型'),
|
|
1512
|
+
),
|
|
1513
|
+
|
|
1506
1514
|
detail
|
|
1507
1515
|
? h(Modal, {
|
|
1508
1516
|
title: detail.name,
|
|
@@ -1952,6 +1960,44 @@ window.__ModuleLoader__.load({
|
|
|
1952
1960
|
})
|
|
1953
1961
|
: null,
|
|
1954
1962
|
|
|
1963
|
+
helpDialog
|
|
1964
|
+
? h(Modal, {
|
|
1965
|
+
title: '使用说明',
|
|
1966
|
+
subtitle: '从安装运行时到导出字幕的完整流程',
|
|
1967
|
+
width: 'min(760px, 94vw)',
|
|
1968
|
+
height: '84vh',
|
|
1969
|
+
onClose: () => setHelpDialog(false),
|
|
1970
|
+
children: [
|
|
1971
|
+
h(
|
|
1972
|
+
'div',
|
|
1973
|
+
{ key: 'flow', style: { display: 'flex', flexDirection: 'column', gap: 12, fontSize: 12, lineHeight: 1.7 } },
|
|
1974
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '① 安装运行时(只做一次)'),
|
|
1975
|
+
h('div', { style: code }, '首次使用点「安装运行时」:下载 ffmpeg、Python、PyTorch(约 3 GB)。有 NVIDIA 显卡自动用 CUDA,没有则用 CPU;可勾选同时下载 Whisper 模型(约 1.6 GB)。')),
|
|
1976
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '② 转写'),
|
|
1977
|
+
h('div', { style: code }, '「转写」里选择视频或音频文件,点开始。任务卡显示进度,可随时取消;完成后项目出现在下面的列表里。')),
|
|
1978
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '③ 翻译'),
|
|
1979
|
+
h('div', { style: code }, '打开项目 → 选模型 →「自动翻译本批」或「自动翻译全部剩余」。勾选「同时校正日文识别」会先修正同音字、漏字和断句。')),
|
|
1980
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '④ 校对与修改'),
|
|
1981
|
+
h('div', { style: code }, '「逐条修改」可改日文和中文、拆分合并、微调时间、删除不需要的行(删除只是不在字幕里显示,不影响导出校验)。也可以给某一条或整批写意见,再「保存意见并重译本批」。')),
|
|
1982
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '⑤ 导出'),
|
|
1983
|
+
h('div', { style: code }, '点「导出字幕」,生成与视频同名的 .srt,放在项目目录下。')),
|
|
1984
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '背景与术语'),
|
|
1985
|
+
h('div', { style: code }, '「背景与术语」里的标签和说明会随每次翻译交给模型,适合写人名、作品名和节目风格;术语表支持粘贴 CSV 导入和导出。')),
|
|
1986
|
+
h('div', null, h('div', { style: { fontWeight: 600, marginBottom: 4 } }, '注意事项'),
|
|
1987
|
+
h('div', { style: { ...code, display: 'flex', flexDirection: 'column', gap: 2 } },
|
|
1988
|
+
h('span', null, '· 仅支持 Windows;需要 DSH 0.2.1 及以上。'),
|
|
1989
|
+
h('span', null, '· 转写在本机完成;翻译只把你选中的字幕文本发给你在 DSH 里选择的模型服务。'),
|
|
1990
|
+
h('span', null, '· 插件不保存任何 API key,模型凭证由 DSH 管理。'),
|
|
1991
|
+
h('span', null, '· 项目默认在「文档\\DshSubtitle\\projects」,可在面板里更改或直接打开。'),
|
|
1992
|
+
h('span', null, '· 数小时的视频转写较慢,建议先转写完再分几批翻译。'),
|
|
1993
|
+
h('span', null, '· 删除项目时可选「仅从列表移除」或「连本地文件一起删除」,后者不可撤销。'),
|
|
1994
|
+
)),
|
|
1995
|
+
),
|
|
1996
|
+
],
|
|
1997
|
+
actions: [h(B, { key: 'close', style: quietButton, onClick: () => setHelpDialog(false) }, '知道了')],
|
|
1998
|
+
})
|
|
1999
|
+
: null,
|
|
2000
|
+
|
|
1955
2001
|
projectsDialog
|
|
1956
2002
|
? h(Modal, {
|
|
1957
2003
|
title: '项目目录',
|
package/index.js
CHANGED
|
@@ -66,10 +66,23 @@ export function apply(ctx, config = {}) {
|
|
|
66
66
|
return llmCtx
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
// The panel routes must not be reachable from another origin, so the API
|
|
70
|
+
// defers to the Host's connection admission when the composition provides it.
|
|
71
|
+
let connectionCtx = null
|
|
72
|
+
ctx.inject(['connection'], (scoped) => {
|
|
73
|
+
scoped.effect(() => {
|
|
74
|
+
connectionCtx = scoped
|
|
75
|
+
return () => {
|
|
76
|
+
connectionCtx = null
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
const connectionService = () => (connectionCtx ? connectionCtx.connection : undefined)
|
|
81
|
+
|
|
69
82
|
// The sidebar panel talks to the Host over these local JSON routes. The
|
|
70
83
|
// plugin still works without a web server (headless profiles just skip it).
|
|
71
84
|
ctx.inject(['webServer'], (scoped) => {
|
|
72
|
-
scoped.effect(() => registerApi(scoped, runtimeFor, modelContext))
|
|
85
|
+
scoped.effect(() => registerApi(scoped, runtimeFor, modelContext, connectionService))
|
|
73
86
|
})
|
|
74
87
|
|
|
75
88
|
ctx.tools.register(defineTool({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-ai-subtitle",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Japanese video to bilingual JP/zh subtitles inside DeepSeek Harness: a self-installing ffmpeg, Python and Whisper runtime, cue editing and SRT export.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
package/src/admission.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Admission guard for the panel's HTTP routes.
|
|
3
|
+
*
|
|
4
|
+
* The routes live on the same origin as the DSH web GUI, so the browser sends
|
|
5
|
+
* the host's auth cookie with every panel request. `ctx.connection` owns that
|
|
6
|
+
* check (Host/Origin trust plus browser authentication); when a host exposes
|
|
7
|
+
* it, the plugin defers to it. Older or headless compositions without the
|
|
8
|
+
* service fall back to the request-header checks below, which stop a page on
|
|
9
|
+
* another origin from driving the routes through the browser.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Fallback rejection for hosts without `connection.requestRejection`.
|
|
14
|
+
* @param headers - request headers (node:http lower-cased record or Headers).
|
|
15
|
+
* @returns 403 for a cross-site or foreign-origin request, undefined otherwise.
|
|
16
|
+
*/
|
|
17
|
+
export function fallbackRejection(headers) {
|
|
18
|
+
const read = (name) => {
|
|
19
|
+
if (!headers) return undefined
|
|
20
|
+
if (typeof headers.get === 'function') return headers.get(name)
|
|
21
|
+
const value = headers[name]
|
|
22
|
+
return Array.isArray(value) ? value[0] : value
|
|
23
|
+
}
|
|
24
|
+
const site = String(read('sec-fetch-site') || '').toLowerCase()
|
|
25
|
+
if (site === 'cross-site') return 403
|
|
26
|
+
const origin = read('origin')
|
|
27
|
+
if (origin) {
|
|
28
|
+
const host = read('host')
|
|
29
|
+
if (!host) return 403
|
|
30
|
+
try {
|
|
31
|
+
if (new URL(origin).host !== host) return 403
|
|
32
|
+
} catch {
|
|
33
|
+
return 403
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Decide whether one request may reach the panel API.
|
|
41
|
+
* @param connection - `ctx.connection`, when the composition provides it.
|
|
42
|
+
* @param request - node:http request carrying headers.
|
|
43
|
+
* @returns rejection status (401/403) or null when the request may proceed.
|
|
44
|
+
*/
|
|
45
|
+
export function rejectRequest(connection, request) {
|
|
46
|
+
if (connection && typeof connection.requestRejection === 'function') {
|
|
47
|
+
try {
|
|
48
|
+
const status = connection.requestRejection(request)
|
|
49
|
+
return status === undefined ? null : status
|
|
50
|
+
} catch {
|
|
51
|
+
// A failing trust check must never fail open.
|
|
52
|
+
return 403
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const status = fallbackRejection(request && request.headers ? request.headers : undefined)
|
|
56
|
+
return status === undefined ? null : status
|
|
57
|
+
}
|
package/src/api.js
CHANGED
|
@@ -7,6 +7,7 @@ import { DEFAULT_MODEL } from './installer.js'
|
|
|
7
7
|
import { buildRows, cueStats, deriveCueTimes, describeCoverageErrors, toSrt, validateCues } from './cues.js'
|
|
8
8
|
import { forgetProject, loadRegistry, patchProject } from './registry.js'
|
|
9
9
|
import { defaultProjectsDir } from './paths.js'
|
|
10
|
+
import { rejectRequest } from './admission.js'
|
|
10
11
|
import { recentMedia, rememberMedia } from './recent.js'
|
|
11
12
|
import { chooseSources, sourcesFromState } from './sources.js'
|
|
12
13
|
import { applyBatch, batchState, loadProject, nextBatch, planBatches, renderBatch, reviewStatus } from './review.js'
|
|
@@ -357,8 +358,19 @@ function projectDetail(dir, limit = 300) {
|
|
|
357
358
|
* Local JSON API for the studio panel. Registered as a handler route because the
|
|
358
359
|
* panel needs POST for install, transcribe, import, save and export.
|
|
359
360
|
*/
|
|
360
|
-
export function registerApi(ctx, runtimeFor, modelContext) {
|
|
361
|
+
export function registerApi(ctx, runtimeFor, modelContext, connectionFor) {
|
|
361
362
|
const handler = async (request, response) => {
|
|
363
|
+
// Same-origin panel requests carry the host's auth cookie; anything else is
|
|
364
|
+
// refused before it can reach a route that changes files.
|
|
365
|
+
const connection = typeof connectionFor === 'function'
|
|
366
|
+
? connectionFor()
|
|
367
|
+
: typeof ctx.get === 'function' ? ctx.get('connection') : undefined
|
|
368
|
+
const rejection = rejectRequest(connection, request)
|
|
369
|
+
if (rejection !== null) {
|
|
370
|
+
response.writeHead(rejection, { 'content-type': 'text/plain; charset=utf-8' })
|
|
371
|
+
response.end(rejection === 401 ? 'unauthorized' : 'forbidden')
|
|
372
|
+
return
|
|
373
|
+
}
|
|
362
374
|
const url = new URL(request.url || '/', 'http://local')
|
|
363
375
|
const path = url.pathname.slice(PREFIX.length) || '/'
|
|
364
376
|
const runtime = runtimeFor(url.searchParams.get('installDir') || undefined)
|