@filumo/core 0.1.0 → 0.1.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 +3 -2
- package/README.zh-CN.md +3 -2
- package/dist/build-manifest.json +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +464 -28
- package/dist/sourceStream.d.ts +10 -0
- package/dist/styles.css +1 -1
- package/dist/types.d.ts +9 -0
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Headless document sessions, file sources and plugin protocols for Filumo, with D
|
|
|
10
10
|
|
|
11
11
|
The headless foundation for Filumo: document sources, format detection, plugin contracts, cancellation and session cleanup. Shared text utilities are available on demand from a separate subpath.
|
|
12
12
|
|
|
13
|
-
This documentation covers **0.1.
|
|
13
|
+
This documentation covers **0.1.1**. The commands below describe consumption after publication to npm. When using this source workspace, build the packages first as described in the integration guide.
|
|
14
14
|
|
|
15
15
|
## Features
|
|
16
16
|
|
|
@@ -116,7 +116,7 @@ The current package is bold. Each package is published independently. Package na
|
|
|
116
116
|
| ------------------------------------------------------------------ | ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
|
|
117
117
|
| **[@filumo/core](https://www.npmjs.com/package/@filumo/core)** | Headless sources, plugin contracts, sessions and shared text utilities | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/core/README.md) |
|
|
118
118
|
| [@filumo/dom](https://www.npmjs.com/package/@filumo/dom) | Native DOM viewer, toolbars, source views, outlines and shared UI | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/dom/README.md) |
|
|
119
|
-
| [@filumo/viewer](https://www.npmjs.com/package/@filumo/viewer) | Standard preset and factories for
|
|
119
|
+
| [@filumo/viewer](https://www.npmjs.com/package/@filumo/viewer) | Standard preset and factories for ten format plugins | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/viewer/README.md) |
|
|
120
120
|
| [@filumo/office](https://www.npmjs.com/package/@filumo/office) | Word, Excel, PowerPoint and supported legacy formats | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/office/README.md) |
|
|
121
121
|
| [@filumo/pdf](https://www.npmjs.com/package/@filumo/pdf) | Continuous PDF reading, outlines, thumbnails and search | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/pdf/README.md) |
|
|
122
122
|
| [@filumo/image](https://www.npmjs.com/package/@filumo/image) | Image preview, zoom, rotation, panning and shared overlays | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/image/README.md) |
|
|
@@ -129,6 +129,7 @@ The current package is bold. Each package is published independently. Package na
|
|
|
129
129
|
| [@filumo/react](https://www.npmjs.com/package/@filumo/react) | React 18/19 adapter | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/react/README.md) |
|
|
130
130
|
| [@filumo/vue](https://www.npmjs.com/package/@filumo/vue) | Vue 3 (>=3.3 <4) adapter | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/vue/README.md) |
|
|
131
131
|
| [@filumo/vue2](https://www.npmjs.com/package/@filumo/vue2) | Vue 2 (^2.6.14 or ^2.7.16) adapter | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/vue2/README.md) |
|
|
132
|
+
| [@filumo/archive](https://www.npmjs.com/package/@filumo/archive) | Archive directories, on-demand extraction and child previews | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/archive/README.md) |
|
|
132
133
|
|
|
133
134
|
<!-- filumo-packages:end -->
|
|
134
135
|
|
package/README.zh-CN.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
Filumo 的无界面基础层:统一文件来源、格式探测、插件协议、取消与会话释放;公共文本能力从独立子入口按需导入。
|
|
10
10
|
|
|
11
|
-
本文档对应 **0.1.
|
|
11
|
+
本文档对应 **0.1.1**。以下命令展示 npm 发布后的消费方式;从当前源码工作区使用时,请先按接入指南构建各包。
|
|
12
12
|
|
|
13
13
|
## 主要能力
|
|
14
14
|
|
|
@@ -107,7 +107,7 @@ Core 没有样式文件、界面或格式引擎。上面的例子只读取会话
|
|
|
107
107
|
| ------------------------------------------------------------------ | -------------------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
108
108
|
| **[@filumo/core](https://www.npmjs.com/package/@filumo/core)** | 无 UI 的来源、插件协议、会话和公共文本处理 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/core/README.zh-CN.md) |
|
|
109
109
|
| [@filumo/dom](https://www.npmjs.com/package/@filumo/dom) | 原生 DOM 查看器、工具栏、原文、大纲和共享 UI | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/dom/README.zh-CN.md) |
|
|
110
|
-
| [@filumo/viewer](https://www.npmjs.com/package/@filumo/viewer) |
|
|
110
|
+
| [@filumo/viewer](https://www.npmjs.com/package/@filumo/viewer) | 十类格式插件的标准预设与工厂聚合 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/viewer/README.zh-CN.md) |
|
|
111
111
|
| [@filumo/office](https://www.npmjs.com/package/@filumo/office) | Word、Excel、PowerPoint 与受支持的旧格式 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/office/README.zh-CN.md) |
|
|
112
112
|
| [@filumo/pdf](https://www.npmjs.com/package/@filumo/pdf) | PDF 连续阅读、大纲、缩略图和查找 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/pdf/README.zh-CN.md) |
|
|
113
113
|
| [@filumo/image](https://www.npmjs.com/package/@filumo/image) | 图片预览、缩放、旋转、拖动及共享弹层 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/image/README.zh-CN.md) |
|
|
@@ -120,6 +120,7 @@ Core 没有样式文件、界面或格式引擎。上面的例子只读取会话
|
|
|
120
120
|
| [@filumo/react](https://www.npmjs.com/package/@filumo/react) | React 18/19 适配组件 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/react/README.zh-CN.md) |
|
|
121
121
|
| [@filumo/vue](https://www.npmjs.com/package/@filumo/vue) | Vue 3(>=3.3 <4)适配组件 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/vue/README.zh-CN.md) |
|
|
122
122
|
| [@filumo/vue2](https://www.npmjs.com/package/@filumo/vue2) | Vue 2(^2.6.14 或 ^2.7.16)适配组件 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/vue2/README.zh-CN.md) |
|
|
123
|
+
| [@filumo/archive](https://www.npmjs.com/package/@filumo/archive) | 压缩包目录、按需提取与包内预览 | [README](https://github.com/gyxing/filumo-viewer/blob/master/packages/archive/README.zh-CN.md) |
|
|
123
124
|
|
|
124
125
|
<!-- filumo-packages:end -->
|
|
125
126
|
|
package/dist/build-manifest.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 2,
|
|
3
3
|
"package": "@filumo/core",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"outputs": [
|
|
6
6
|
{
|
|
7
7
|
"file": "index.js",
|
|
8
8
|
"entryPoint": "src/index.ts",
|
|
9
9
|
"inputs": [
|
|
10
|
+
"src/sourceStream.ts",
|
|
10
11
|
"src/guardSource.ts",
|
|
11
12
|
"src/range/BlobRangeReader.ts",
|
|
12
13
|
"src/range/RangeCache.ts",
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -5,6 +5,188 @@ import {
|
|
|
5
5
|
throwIfAborted
|
|
6
6
|
} from "./chunks/chunk-AUUMY3MH.js";
|
|
7
7
|
|
|
8
|
+
// packages/core/src/sourceStream.ts
|
|
9
|
+
async function readStreamPrefix(stream, length, signal) {
|
|
10
|
+
const reader = stream.getReader();
|
|
11
|
+
const chunks = [];
|
|
12
|
+
let written = 0;
|
|
13
|
+
let eof = false;
|
|
14
|
+
try {
|
|
15
|
+
while (written < length) {
|
|
16
|
+
throwIfAborted(signal);
|
|
17
|
+
const item = await waitForSourceRead(reader.read(), signal);
|
|
18
|
+
throwIfAborted(signal);
|
|
19
|
+
if (item.done) {
|
|
20
|
+
eof = true;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
const count = Math.min(item.value.length, length - written);
|
|
24
|
+
chunks.push(item.value.slice(0, count));
|
|
25
|
+
written += count;
|
|
26
|
+
}
|
|
27
|
+
const bytes = new Uint8Array(written);
|
|
28
|
+
let offset = 0;
|
|
29
|
+
for (const chunk of chunks) {
|
|
30
|
+
bytes.set(chunk, offset);
|
|
31
|
+
offset += chunk.length;
|
|
32
|
+
}
|
|
33
|
+
return { bytes, eof };
|
|
34
|
+
} finally {
|
|
35
|
+
try {
|
|
36
|
+
if (!eof)
|
|
37
|
+
await reader.cancel().catch(() => void 0);
|
|
38
|
+
} finally {
|
|
39
|
+
reader.releaseLock();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function waitForSourceRead(promise, signal) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const abort = () => reject(signal.reason ?? abortError());
|
|
46
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
47
|
+
if (signal.aborted)
|
|
48
|
+
abort();
|
|
49
|
+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function guardByteStream(stream, signal, lifetime, maxBytes, finish) {
|
|
53
|
+
const reader = stream.getReader();
|
|
54
|
+
let closed = false, length = 0;
|
|
55
|
+
let controller;
|
|
56
|
+
let abort;
|
|
57
|
+
const cleanup = () => {
|
|
58
|
+
signal.removeEventListener("abort", abort);
|
|
59
|
+
lifetime.removeEventListener("abort", abort);
|
|
60
|
+
try {
|
|
61
|
+
reader.releaseLock();
|
|
62
|
+
} catch {
|
|
63
|
+
}
|
|
64
|
+
finish?.();
|
|
65
|
+
};
|
|
66
|
+
const fail = (error) => {
|
|
67
|
+
if (closed)
|
|
68
|
+
return;
|
|
69
|
+
closed = true;
|
|
70
|
+
controller.error(error);
|
|
71
|
+
void reader.cancel(error).catch(() => void 0).finally(cleanup);
|
|
72
|
+
};
|
|
73
|
+
abort = () => fail(signal.reason ?? lifetime.reason ?? abortError());
|
|
74
|
+
return new ReadableStream(
|
|
75
|
+
{
|
|
76
|
+
start(value) {
|
|
77
|
+
controller = value;
|
|
78
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
79
|
+
lifetime.addEventListener("abort", abort, { once: true });
|
|
80
|
+
if (signal.aborted || lifetime.aborted)
|
|
81
|
+
abort();
|
|
82
|
+
},
|
|
83
|
+
async pull() {
|
|
84
|
+
if (closed)
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
const item = await reader.read();
|
|
88
|
+
if (closed)
|
|
89
|
+
return;
|
|
90
|
+
if (item.done) {
|
|
91
|
+
closed = true;
|
|
92
|
+
controller.close();
|
|
93
|
+
cleanup();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (!(item.value instanceof Uint8Array))
|
|
97
|
+
throw new FilumoError("INVALID_SOURCE", "\u6765\u6E90\u6D41\u6CA1\u6709\u8FD4\u56DE\u5B57\u8282");
|
|
98
|
+
length += item.value.byteLength;
|
|
99
|
+
if (maxBytes !== void 0 && length > maxBytes)
|
|
100
|
+
throw new FilumoError(
|
|
101
|
+
"FILE_SIZE_LIMIT_EXCEEDED",
|
|
102
|
+
"\u6587\u4EF6\u8D85\u8FC7\u5BBF\u4E3B\u914D\u7F6E\u7684\u5927\u5C0F\u9650\u5236"
|
|
103
|
+
);
|
|
104
|
+
controller.enqueue(item.value);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
fail(error);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
async cancel(reason) {
|
|
110
|
+
if (closed)
|
|
111
|
+
return;
|
|
112
|
+
closed = true;
|
|
113
|
+
try {
|
|
114
|
+
await reader.cancel(reason);
|
|
115
|
+
} finally {
|
|
116
|
+
cleanup();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
{ highWaterMark: 0 }
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
async function readSourceBytes(source, limit, signal) {
|
|
124
|
+
throwIfAborted(signal);
|
|
125
|
+
if (!Number.isSafeInteger(limit) || limit <= 0)
|
|
126
|
+
throw new Error("\u8BFB\u53D6\u4E0A\u9650\u5FC5\u987B\u662F\u6B63\u6574\u6570");
|
|
127
|
+
const check = (size) => {
|
|
128
|
+
if (!Number.isSafeInteger(size) || size < 0 || size > limit)
|
|
129
|
+
throw new FilumoError("FILE_SIZE_LIMIT_EXCEEDED", "\u6587\u4EF6\u8D85\u8FC7\u5B8C\u6574\u8BFB\u53D6\u4E0A\u9650");
|
|
130
|
+
};
|
|
131
|
+
if (source.size !== void 0)
|
|
132
|
+
check(source.size);
|
|
133
|
+
const range = await source.getRangeReader?.(signal);
|
|
134
|
+
if (range) {
|
|
135
|
+
check(range.length);
|
|
136
|
+
const result = new Uint8Array(range.length);
|
|
137
|
+
for (let offset2 = 0; offset2 < range.length; offset2 += 256 * 1024) {
|
|
138
|
+
throwIfAborted(signal);
|
|
139
|
+
const length2 = Math.min(256 * 1024, range.length - offset2);
|
|
140
|
+
const bytes2 = await range.read(offset2, length2, signal);
|
|
141
|
+
throwIfAborted(signal);
|
|
142
|
+
if (!(bytes2 instanceof Uint8Array) || bytes2.length !== length2)
|
|
143
|
+
throw new FilumoError("INVALID_RANGE", "\u6765\u6E90\u533A\u95F4\u6570\u636E\u4E0D\u5B8C\u6574");
|
|
144
|
+
result.set(bytes2, offset2);
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
if (!source.openStream) {
|
|
149
|
+
if (source.size === void 0)
|
|
150
|
+
throw new FilumoError(
|
|
151
|
+
"UNBOUNDED_SOURCE",
|
|
152
|
+
"\u6765\u6E90\u9700\u8981\u63D0\u4F9B\u6D41\u5F0F\u8BFB\u53D6\u6216\u53EF\u9760\u957F\u5EA6"
|
|
153
|
+
);
|
|
154
|
+
const bytes2 = await source.readAll(signal);
|
|
155
|
+
throwIfAborted(signal);
|
|
156
|
+
check(bytes2.byteLength);
|
|
157
|
+
return bytes2.slice();
|
|
158
|
+
}
|
|
159
|
+
const stream = guardByteStream(
|
|
160
|
+
await source.openStream(signal),
|
|
161
|
+
signal,
|
|
162
|
+
signal,
|
|
163
|
+
limit
|
|
164
|
+
);
|
|
165
|
+
const reader = stream.getReader(), chunks = [];
|
|
166
|
+
let length = 0;
|
|
167
|
+
try {
|
|
168
|
+
for (; ; ) {
|
|
169
|
+
const part = await reader.read();
|
|
170
|
+
if (part.done)
|
|
171
|
+
break;
|
|
172
|
+
chunks.push(part.value);
|
|
173
|
+
length += part.value.length;
|
|
174
|
+
}
|
|
175
|
+
} catch (error) {
|
|
176
|
+
await reader.cancel(error).catch(() => void 0);
|
|
177
|
+
throw error;
|
|
178
|
+
} finally {
|
|
179
|
+
reader.releaseLock();
|
|
180
|
+
}
|
|
181
|
+
const bytes = new Uint8Array(length);
|
|
182
|
+
let offset = 0;
|
|
183
|
+
for (const chunk of chunks) {
|
|
184
|
+
bytes.set(chunk, offset);
|
|
185
|
+
offset += chunk.length;
|
|
186
|
+
}
|
|
187
|
+
return bytes;
|
|
188
|
+
}
|
|
189
|
+
|
|
8
190
|
// packages/core/src/guardSource.ts
|
|
9
191
|
var owners = /* @__PURE__ */ new WeakMap();
|
|
10
192
|
function waitForSource(promise, signal) {
|
|
@@ -74,7 +256,10 @@ function guardSource(raw, policy) {
|
|
|
74
256
|
const bytes2 = await raw.readRange(offset, length, readingSignal);
|
|
75
257
|
check(bytes2.byteLength);
|
|
76
258
|
if (bytes2.byteLength > length)
|
|
77
|
-
throw new FilumoError(
|
|
259
|
+
throw new FilumoError(
|
|
260
|
+
"INVALID_RANGE",
|
|
261
|
+
"\u6765\u6E90\u8FD4\u56DE\u4E86\u8D85\u51FA\u8BF7\u6C42\u8303\u56F4\u7684\u6570\u636E"
|
|
262
|
+
);
|
|
78
263
|
return bytes2;
|
|
79
264
|
}
|
|
80
265
|
const bytes = await raw.readAll(readingSignal);
|
|
@@ -97,6 +282,21 @@ function guardSource(raw, policy) {
|
|
|
97
282
|
return raw.url;
|
|
98
283
|
},
|
|
99
284
|
readAll: (signal) => read(signal),
|
|
285
|
+
openStream: raw.openStream ? async (signal) => {
|
|
286
|
+
if (closed)
|
|
287
|
+
throw new FilumoError("SOURCE_DISPOSED", "\u6587\u4EF6\u6765\u6E90\u5DF2\u91CA\u653E");
|
|
288
|
+
check(raw.size);
|
|
289
|
+
const stream = await run(
|
|
290
|
+
signal,
|
|
291
|
+
(current) => raw.openStream(current)
|
|
292
|
+
);
|
|
293
|
+
return guardByteStream(
|
|
294
|
+
stream,
|
|
295
|
+
signal,
|
|
296
|
+
lifetime.signal,
|
|
297
|
+
policy?.maxFileBytes
|
|
298
|
+
);
|
|
299
|
+
} : void 0,
|
|
100
300
|
getRangeReader: raw.getRangeReader ? (signal) => run(signal, async (readingSignal) => {
|
|
101
301
|
const reader = await raw.getRangeReader(readingSignal);
|
|
102
302
|
if (!reader)
|
|
@@ -118,7 +318,10 @@ function guardSource(raw, policy) {
|
|
|
118
318
|
const bytes = await reader.read(offset, count, rangeSignal);
|
|
119
319
|
check(bytes.length);
|
|
120
320
|
if (bytes.length !== count)
|
|
121
|
-
throw new FilumoError(
|
|
321
|
+
throw new FilumoError(
|
|
322
|
+
"INVALID_RANGE",
|
|
323
|
+
"\u6765\u6E90\u533A\u95F4\u6570\u636E\u4E0D\u5B8C\u6574"
|
|
324
|
+
);
|
|
122
325
|
return bytes;
|
|
123
326
|
})
|
|
124
327
|
};
|
|
@@ -441,6 +644,8 @@ async function normalizeSource(input, options = {}) {
|
|
|
441
644
|
let rangeSetup;
|
|
442
645
|
let fallbackResponse;
|
|
443
646
|
let finishRange;
|
|
647
|
+
let prefix;
|
|
648
|
+
let prefixReading;
|
|
444
649
|
const localAbort = new AbortController();
|
|
445
650
|
if (typeof Blob !== "undefined" && input instanceof Blob) {
|
|
446
651
|
blob = input;
|
|
@@ -480,6 +685,31 @@ async function normalizeSource(input, options = {}) {
|
|
|
480
685
|
"\u6587\u4EF6\u8D85\u8FC7\u5BBF\u4E3B\u914D\u7F6E\u7684\u5927\u5C0F\u9650\u5236"
|
|
481
686
|
);
|
|
482
687
|
};
|
|
688
|
+
const setupRange = (signal) => {
|
|
689
|
+
if (!rangeSetup)
|
|
690
|
+
rangeSetup = createHttpRangeReader(
|
|
691
|
+
url,
|
|
692
|
+
request,
|
|
693
|
+
options.resourcePolicy,
|
|
694
|
+
localAbort.signal
|
|
695
|
+
).then((setup) => {
|
|
696
|
+
if (disposed) {
|
|
697
|
+
void setup.fallbackResponse?.body?.cancel().catch(() => void 0);
|
|
698
|
+
setup.finish?.();
|
|
699
|
+
throw abortError();
|
|
700
|
+
}
|
|
701
|
+
fallbackResponse = setup.fallbackResponse;
|
|
702
|
+
finishRange = setup.finish;
|
|
703
|
+
mimeType || (mimeType = setup.mimeType);
|
|
704
|
+
if (setup.reader)
|
|
705
|
+
size = setup.reader.length;
|
|
706
|
+
return setup;
|
|
707
|
+
}).catch((error) => {
|
|
708
|
+
rangeSetup = void 0;
|
|
709
|
+
throw error;
|
|
710
|
+
});
|
|
711
|
+
return waitForSource(rangeSetup, signal);
|
|
712
|
+
};
|
|
483
713
|
const read = async (signal) => {
|
|
484
714
|
if (disposed)
|
|
485
715
|
throw new FilumoError("SOURCE_DISPOSED", "\u6587\u4EF6\u6765\u6E90\u5DF2\u91CA\u653E");
|
|
@@ -489,11 +719,16 @@ async function normalizeSource(input, options = {}) {
|
|
|
489
719
|
checkSize(size);
|
|
490
720
|
if (bytes)
|
|
491
721
|
return bytes;
|
|
492
|
-
if (!reading)
|
|
722
|
+
if (!reading) {
|
|
723
|
+
const pendingPrefix = prefixReading;
|
|
493
724
|
reading = (async () => {
|
|
494
725
|
const abort = () => localAbort.abort(signal.reason ?? abortError());
|
|
495
726
|
signal.addEventListener("abort", abort, { once: true });
|
|
496
727
|
try {
|
|
728
|
+
if (pendingPrefix)
|
|
729
|
+
await waitForSource(pendingPrefix, signal);
|
|
730
|
+
if (bytes)
|
|
731
|
+
return bytes;
|
|
497
732
|
if (blob) {
|
|
498
733
|
bytes = new Uint8Array(await blob.arrayBuffer());
|
|
499
734
|
} else {
|
|
@@ -561,8 +796,119 @@ async function normalizeSource(input, options = {}) {
|
|
|
561
796
|
reading = void 0;
|
|
562
797
|
throw error;
|
|
563
798
|
});
|
|
799
|
+
}
|
|
564
800
|
return reading;
|
|
565
801
|
};
|
|
802
|
+
const readPrefix = async (length, signal) => {
|
|
803
|
+
if (bytes)
|
|
804
|
+
return bytes.slice(0, length);
|
|
805
|
+
if (reading)
|
|
806
|
+
return (await waitForSource(reading, signal)).slice(0, length);
|
|
807
|
+
if (prefix && prefix.length >= length)
|
|
808
|
+
return prefix.slice(0, length);
|
|
809
|
+
if (prefixReading) {
|
|
810
|
+
const current = await waitForSource(prefixReading, signal);
|
|
811
|
+
if (current.length >= length)
|
|
812
|
+
return current.slice(0, length);
|
|
813
|
+
return readPrefix(length, signal);
|
|
814
|
+
}
|
|
815
|
+
if (size !== void 0 && length >= size)
|
|
816
|
+
return (await read(signal)).slice(0, length);
|
|
817
|
+
const operation = Promise.resolve().then(async () => {
|
|
818
|
+
const cancelSource = () => localAbort.abort(signal.reason ?? abortError());
|
|
819
|
+
signal.addEventListener("abort", cancelSource, { once: true });
|
|
820
|
+
try {
|
|
821
|
+
throwIfAborted(signal);
|
|
822
|
+
const { reader } = await setupRange(signal);
|
|
823
|
+
if (reader)
|
|
824
|
+
return reader.read(0, Math.min(length, reader.length), signal);
|
|
825
|
+
const abort = new AbortController();
|
|
826
|
+
const cancel = () => abort.abort(
|
|
827
|
+
signal.reason ?? localAbort.signal.reason ?? abortError()
|
|
828
|
+
);
|
|
829
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
830
|
+
localAbort.signal.addEventListener("abort", cancel, { once: true });
|
|
831
|
+
const rangeFinish = finishRange;
|
|
832
|
+
finishRange = void 0;
|
|
833
|
+
let response;
|
|
834
|
+
const finish = () => {
|
|
835
|
+
signal.removeEventListener("abort", cancel);
|
|
836
|
+
localAbort.signal.removeEventListener("abort", cancel);
|
|
837
|
+
rangeFinish?.();
|
|
838
|
+
};
|
|
839
|
+
try {
|
|
840
|
+
throwIfAborted(signal);
|
|
841
|
+
throwIfAborted(localAbort.signal);
|
|
842
|
+
response = fallbackResponse ?? await fetch(url, { ...request, signal: abort.signal });
|
|
843
|
+
fallbackResponse = void 0;
|
|
844
|
+
throwIfAborted(signal);
|
|
845
|
+
throwIfAborted(localAbort.signal);
|
|
846
|
+
if (!response.ok)
|
|
847
|
+
throw new FilumoError(
|
|
848
|
+
"DOWNLOAD_FAILED",
|
|
849
|
+
"\u6587\u4EF6\u4E0B\u8F7D\u5931\u8D25\uFF1A" + response.status
|
|
850
|
+
);
|
|
851
|
+
mimeType || (mimeType = response.headers.get("content-type") ?? void 0);
|
|
852
|
+
const advertised = response.headers.get("content-length");
|
|
853
|
+
if (advertised && /^\d+$/.test(advertised)) {
|
|
854
|
+
checkSize(Number(advertised));
|
|
855
|
+
}
|
|
856
|
+
if (!response.body) {
|
|
857
|
+
const result2 = new Uint8Array(await response.arrayBuffer());
|
|
858
|
+
throwIfAborted(signal);
|
|
859
|
+
throwIfAborted(localAbort.signal);
|
|
860
|
+
checkSize(result2.length);
|
|
861
|
+
bytes = result2;
|
|
862
|
+
size = result2.length;
|
|
863
|
+
return result2;
|
|
864
|
+
}
|
|
865
|
+
const window = Math.max(
|
|
866
|
+
length,
|
|
867
|
+
Math.min(
|
|
868
|
+
64 * 1024,
|
|
869
|
+
size ?? Infinity,
|
|
870
|
+
options.resourcePolicy?.maxFileBytes ?? Infinity
|
|
871
|
+
)
|
|
872
|
+
);
|
|
873
|
+
const result = await readStreamPrefix(
|
|
874
|
+
guardByteStream(
|
|
875
|
+
response.body,
|
|
876
|
+
signal,
|
|
877
|
+
localAbort.signal,
|
|
878
|
+
options.resourcePolicy?.maxFileBytes
|
|
879
|
+
),
|
|
880
|
+
window,
|
|
881
|
+
signal
|
|
882
|
+
);
|
|
883
|
+
throwIfAborted(signal);
|
|
884
|
+
throwIfAborted(localAbort.signal);
|
|
885
|
+
if (result.eof) {
|
|
886
|
+
bytes = result.bytes;
|
|
887
|
+
size = bytes.length;
|
|
888
|
+
} else
|
|
889
|
+
prefix = result.bytes.slice(0, 64 * 1024);
|
|
890
|
+
return result.bytes;
|
|
891
|
+
} catch (error) {
|
|
892
|
+
if (response?.body && !response.body.locked)
|
|
893
|
+
await response.body.cancel().catch(() => void 0);
|
|
894
|
+
throw error;
|
|
895
|
+
} finally {
|
|
896
|
+
finish();
|
|
897
|
+
}
|
|
898
|
+
} finally {
|
|
899
|
+
signal.removeEventListener("abort", cancelSource);
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
prefixReading = operation;
|
|
903
|
+
try {
|
|
904
|
+
return (await waitForSource(operation, signal)).slice(0, length);
|
|
905
|
+
} finally {
|
|
906
|
+
void operation.finally(() => {
|
|
907
|
+
if (prefixReading === operation)
|
|
908
|
+
prefixReading = void 0;
|
|
909
|
+
}).catch(() => void 0);
|
|
910
|
+
}
|
|
911
|
+
};
|
|
566
912
|
return {
|
|
567
913
|
get name() {
|
|
568
914
|
return name;
|
|
@@ -577,6 +923,103 @@ async function normalizeSource(input, options = {}) {
|
|
|
577
923
|
return url;
|
|
578
924
|
},
|
|
579
925
|
readAll: read,
|
|
926
|
+
async openStream(signal) {
|
|
927
|
+
if (disposed)
|
|
928
|
+
throw new FilumoError("SOURCE_DISPOSED", "\u6587\u4EF6\u6765\u6E90\u5DF2\u91CA\u653E");
|
|
929
|
+
throwIfAborted(signal);
|
|
930
|
+
throwIfAborted(localAbort.signal);
|
|
931
|
+
if (size !== void 0)
|
|
932
|
+
checkSize(size);
|
|
933
|
+
if (blob)
|
|
934
|
+
return guardByteStream(
|
|
935
|
+
blob.stream(),
|
|
936
|
+
signal,
|
|
937
|
+
localAbort.signal,
|
|
938
|
+
options.resourcePolicy?.maxFileBytes
|
|
939
|
+
);
|
|
940
|
+
if (bytes) {
|
|
941
|
+
const current = bytes;
|
|
942
|
+
return guardByteStream(
|
|
943
|
+
new ReadableStream({
|
|
944
|
+
start(c) {
|
|
945
|
+
c.enqueue(current);
|
|
946
|
+
c.close();
|
|
947
|
+
}
|
|
948
|
+
}),
|
|
949
|
+
signal,
|
|
950
|
+
localAbort.signal,
|
|
951
|
+
options.resourcePolicy?.maxFileBytes
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
if (prefixReading)
|
|
955
|
+
await waitForSource(prefixReading, signal);
|
|
956
|
+
if (bytes) {
|
|
957
|
+
const current = bytes;
|
|
958
|
+
return guardByteStream(
|
|
959
|
+
new ReadableStream({
|
|
960
|
+
start(c) {
|
|
961
|
+
c.enqueue(current);
|
|
962
|
+
c.close();
|
|
963
|
+
}
|
|
964
|
+
}),
|
|
965
|
+
signal,
|
|
966
|
+
localAbort.signal,
|
|
967
|
+
options.resourcePolicy?.maxFileBytes
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
if (rangeSetup)
|
|
971
|
+
await waitForSource(rangeSetup, signal);
|
|
972
|
+
const abort = new AbortController();
|
|
973
|
+
const cancel = () => abort.abort(signal.reason ?? localAbort.signal.reason ?? abortError());
|
|
974
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
975
|
+
localAbort.signal.addEventListener("abort", cancel, { once: true });
|
|
976
|
+
const finish = () => {
|
|
977
|
+
signal.removeEventListener("abort", cancel);
|
|
978
|
+
localAbort.signal.removeEventListener("abort", cancel);
|
|
979
|
+
};
|
|
980
|
+
try {
|
|
981
|
+
throwIfAborted(signal);
|
|
982
|
+
throwIfAborted(localAbort.signal);
|
|
983
|
+
const response = fallbackResponse ?? await fetch(url, { ...request, signal: abort.signal });
|
|
984
|
+
fallbackResponse = void 0;
|
|
985
|
+
const rangeFinish = finishRange;
|
|
986
|
+
finishRange = void 0;
|
|
987
|
+
if (!response.ok) {
|
|
988
|
+
await response.body?.cancel();
|
|
989
|
+
throw new FilumoError(
|
|
990
|
+
"DOWNLOAD_FAILED",
|
|
991
|
+
"\u6587\u4EF6\u4E0B\u8F7D\u5931\u8D25\uFF1A" + response.status
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
mimeType || (mimeType = response.headers.get("content-type") ?? void 0);
|
|
995
|
+
const advertised = response.headers.get("content-length");
|
|
996
|
+
if (advertised && /^\d+$/.test(advertised)) {
|
|
997
|
+
size = Number(advertised);
|
|
998
|
+
try {
|
|
999
|
+
checkSize(size);
|
|
1000
|
+
} catch (error) {
|
|
1001
|
+
await response.body?.cancel();
|
|
1002
|
+
rangeFinish?.();
|
|
1003
|
+
throw error;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
if (!response.body)
|
|
1007
|
+
throw new FilumoError("INVALID_SOURCE", "\u6765\u6E90\u672A\u63D0\u4F9B\u53EF\u8BFB\u53D6\u7684\u6570\u636E\u6D41");
|
|
1008
|
+
return guardByteStream(
|
|
1009
|
+
response.body,
|
|
1010
|
+
signal,
|
|
1011
|
+
localAbort.signal,
|
|
1012
|
+
options.resourcePolicy?.maxFileBytes,
|
|
1013
|
+
() => {
|
|
1014
|
+
finish();
|
|
1015
|
+
rangeFinish?.();
|
|
1016
|
+
}
|
|
1017
|
+
);
|
|
1018
|
+
} catch (error) {
|
|
1019
|
+
finish();
|
|
1020
|
+
throw error;
|
|
1021
|
+
}
|
|
1022
|
+
},
|
|
580
1023
|
async getRangeReader(signal) {
|
|
581
1024
|
if (disposed)
|
|
582
1025
|
throw new FilumoError("SOURCE_DISPOSED", "\u6587\u4EF6\u6765\u6E90\u5DF2\u91CA\u653E");
|
|
@@ -599,29 +1042,7 @@ async function normalizeSource(input, options = {}) {
|
|
|
599
1042
|
return bytes.slice(offset, offset + length);
|
|
600
1043
|
}
|
|
601
1044
|
};
|
|
602
|
-
|
|
603
|
-
rangeSetup = createHttpRangeReader(
|
|
604
|
-
url,
|
|
605
|
-
request,
|
|
606
|
-
options.resourcePolicy,
|
|
607
|
-
localAbort.signal
|
|
608
|
-
).then((setup) => {
|
|
609
|
-
if (disposed) {
|
|
610
|
-
void setup.fallbackResponse?.body?.cancel().catch(() => void 0);
|
|
611
|
-
setup.finish?.();
|
|
612
|
-
throw abortError();
|
|
613
|
-
}
|
|
614
|
-
fallbackResponse = setup.fallbackResponse;
|
|
615
|
-
finishRange = setup.finish;
|
|
616
|
-
mimeType || (mimeType = setup.mimeType);
|
|
617
|
-
if (setup.reader)
|
|
618
|
-
size = setup.reader.length;
|
|
619
|
-
return setup;
|
|
620
|
-
}).catch((error) => {
|
|
621
|
-
rangeSetup = void 0;
|
|
622
|
-
throw error;
|
|
623
|
-
});
|
|
624
|
-
return (await waitForSource(rangeSetup, signal)).reader;
|
|
1045
|
+
return (await setupRange(signal)).reader;
|
|
625
1046
|
},
|
|
626
1047
|
async readBlob(signal) {
|
|
627
1048
|
if (disposed)
|
|
@@ -641,6 +1062,10 @@ async function normalizeSource(input, options = {}) {
|
|
|
641
1062
|
throw new FilumoError("INVALID_RANGE", "\u6765\u6E90\u8BFB\u53D6\u8303\u56F4\u65E0\u6548");
|
|
642
1063
|
if (size !== void 0)
|
|
643
1064
|
checkSize(size);
|
|
1065
|
+
if (!length)
|
|
1066
|
+
return new Uint8Array();
|
|
1067
|
+
if (!blob && offset === 0 && url?.startsWith("http") && (request?.method ?? "GET").toUpperCase() === "GET" && !request?.body && !new Headers(request?.headers).has("Range"))
|
|
1068
|
+
return readPrefix(length, signal);
|
|
644
1069
|
if (!blob)
|
|
645
1070
|
return (await read(signal)).slice(offset, offset + length);
|
|
646
1071
|
const abort = new AbortController();
|
|
@@ -672,6 +1097,8 @@ async function normalizeSource(input, options = {}) {
|
|
|
672
1097
|
blob = void 0;
|
|
673
1098
|
bytes = void 0;
|
|
674
1099
|
reading = void 0;
|
|
1100
|
+
prefix = void 0;
|
|
1101
|
+
prefixReading = void 0;
|
|
675
1102
|
}
|
|
676
1103
|
};
|
|
677
1104
|
}
|
|
@@ -751,7 +1178,9 @@ function createDocumentController(options) {
|
|
|
751
1178
|
detectedMetadata = metadata();
|
|
752
1179
|
const extension = currentSource.name?.toLowerCase().match(/\.[^.]+$/)?.[0];
|
|
753
1180
|
const candidates = plugins.filter(
|
|
754
|
-
(plugin) => extension && plugin.extensions.includes(extension) || currentSource.mimeType && plugin.mimeTypes.includes(
|
|
1181
|
+
(plugin) => extension && plugin.extensions.includes(extension) || currentSource.mimeType && plugin.mimeTypes.includes(
|
|
1182
|
+
currentSource.mimeType.split(";")[0]
|
|
1183
|
+
)
|
|
755
1184
|
);
|
|
756
1185
|
for (const candidate of [
|
|
757
1186
|
...candidates,
|
|
@@ -782,7 +1211,11 @@ function createDocumentController(options) {
|
|
|
782
1211
|
if (!candidate.detectFallback)
|
|
783
1212
|
continue;
|
|
784
1213
|
const matched = await waitForSource(
|
|
785
|
-
candidate.detectFallback(
|
|
1214
|
+
candidate.detectFallback(
|
|
1215
|
+
source,
|
|
1216
|
+
abort.signal,
|
|
1217
|
+
options.resourcePolicy
|
|
1218
|
+
),
|
|
786
1219
|
abort.signal
|
|
787
1220
|
);
|
|
788
1221
|
throwIfAborted(abort.signal);
|
|
@@ -810,6 +1243,7 @@ function createDocumentController(options) {
|
|
|
810
1243
|
sessionId: "filumo-" + Date.now() + "-" + task,
|
|
811
1244
|
signal: abort.signal,
|
|
812
1245
|
resourcePolicy: options.resourcePolicy,
|
|
1246
|
+
resources: selection.resources,
|
|
813
1247
|
reportProgress(progress) {
|
|
814
1248
|
latestProgress = progress;
|
|
815
1249
|
if (generation === task && !disposed)
|
|
@@ -957,8 +1391,10 @@ export {
|
|
|
957
1391
|
createDocumentController,
|
|
958
1392
|
createSession,
|
|
959
1393
|
exportOriginal,
|
|
1394
|
+
guardByteStream,
|
|
960
1395
|
normalizeSource,
|
|
961
1396
|
notify,
|
|
1397
|
+
readSourceBytes,
|
|
962
1398
|
throwIfAborted,
|
|
963
1399
|
validatePolicy
|
|
964
1400
|
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DocumentSource } from './types';
|
|
2
|
+
/** 只保留请求前缀;真实 EOF 才能证明结果是全文,正常截断不取消来源租约。 */
|
|
3
|
+
export declare function readStreamPrefix(stream: ReadableStream<Uint8Array>, length: number, signal: AbortSignal): Promise<{
|
|
4
|
+
bytes: Uint8Array;
|
|
5
|
+
eof: boolean;
|
|
6
|
+
}>;
|
|
7
|
+
/** 流在消费期间持续持有取消监听,不能在交付 reader 时就释放来源租约。 */
|
|
8
|
+
export declare function guardByteStream(stream: ReadableStream<Uint8Array>, signal: AbortSignal, lifetime: AbortSignal, maxBytes?: number, finish?: () => void): ReadableStream<Uint8Array>;
|
|
9
|
+
/** 在分配完整结果前执行大小限制,未知长度且没有流的第三方来源不能安全物化。 */
|
|
10
|
+
export declare function readSourceBytes(source: DocumentSource, limit: number, signal: AbortSignal): Promise<Uint8Array>;
|
package/dist/styles.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
:root { --filumo-style-core: 0.1.
|
|
1
|
+
:root { --filumo-style-core: 0.1.1; }
|
package/dist/types.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export interface DocumentSource {
|
|
|
10
10
|
readonly size?: number;
|
|
11
11
|
readonly url?: string;
|
|
12
12
|
readAll(signal: AbortSignal): Promise<Uint8Array>;
|
|
13
|
+
/** 每次从头交付独立消费流;消费者负责消费或取消,不能隐式物化全文。 */
|
|
14
|
+
openStream?(signal: AbortSignal): Promise<ReadableStream<Uint8Array>>;
|
|
13
15
|
/** 可直接交付不可变的本地 Blob;不具备此能力时返回 undefined,调用方回退到 readAll。 */
|
|
14
16
|
readBlob?(signal: AbortSignal): Promise<Blob | undefined>;
|
|
15
17
|
/** 明确提供高效区间能力;未实现时调用方继续使用原有读取路径。 */
|
|
@@ -26,6 +28,12 @@ export type DocumentInput = File | Blob | Uint8Array | string | {
|
|
|
26
28
|
/** 单次打开可显式选择已经注册的插件,不隐式安装其他格式。 */
|
|
27
29
|
export interface DocumentOpenOptions {
|
|
28
30
|
pluginId?: string;
|
|
31
|
+
resources?: DocumentResourceContext;
|
|
32
|
+
}
|
|
33
|
+
/** 单次打开的相邻资源解析;返回 undefined 明确阻止,不回退外部请求。 */
|
|
34
|
+
export interface DocumentResourceContext {
|
|
35
|
+
baseUrl?: string;
|
|
36
|
+
resolveResource?: (reference: string, kind: 'image' | 'link', signal: AbortSignal) => string | undefined | Promise<string | undefined>;
|
|
29
37
|
}
|
|
30
38
|
export type DocumentStatus = 'idle' | 'detecting' | 'loading' | 'ready' | 'error' | 'cancelled' | 'disposed';
|
|
31
39
|
export interface DocumentSnapshot {
|
|
@@ -58,6 +66,7 @@ export interface OpenContext {
|
|
|
58
66
|
sessionId: string;
|
|
59
67
|
signal: AbortSignal;
|
|
60
68
|
resourcePolicy?: ResourcePolicy;
|
|
69
|
+
resources?: DocumentResourceContext;
|
|
61
70
|
reportProgress(progress: DocumentSnapshot['progress']): void;
|
|
62
71
|
}
|
|
63
72
|
export interface DocumentDriver<T = unknown> {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@filumo/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Headless document sessions, file sources and plugin protocols for Filumo, with DOM-free text utilities.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"filumo",
|
|
@@ -20,22 +20,19 @@
|
|
|
20
20
|
"文件读取"
|
|
21
21
|
],
|
|
22
22
|
"homepage": "https://github.com/gyxing/filumo-viewer/tree/master/packages/core#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/gyxing/filumo-viewer/issues"
|
|
25
|
+
},
|
|
23
26
|
"repository": {
|
|
24
27
|
"type": "git",
|
|
25
28
|
"url": "git+https://github.com/gyxing/filumo-viewer.git",
|
|
26
29
|
"directory": "packages/core"
|
|
27
30
|
},
|
|
28
|
-
"
|
|
29
|
-
"url": "https://github.com/gyxing/filumo-viewer/issues"
|
|
30
|
-
},
|
|
31
|
+
"license": "MIT",
|
|
31
32
|
"author": {
|
|
32
33
|
"name": "gyxing",
|
|
33
34
|
"url": "https://github.com/gyxing"
|
|
34
35
|
},
|
|
35
|
-
"license": "MIT",
|
|
36
|
-
"publishConfig": {
|
|
37
|
-
"access": "public"
|
|
38
|
-
},
|
|
39
36
|
"sideEffects": [
|
|
40
37
|
"**/styles/**",
|
|
41
38
|
"**/*.css"
|
|
@@ -56,16 +53,19 @@
|
|
|
56
53
|
},
|
|
57
54
|
"module": "./dist/index.js",
|
|
58
55
|
"types": "./dist/index.d.ts",
|
|
59
|
-
"files": [
|
|
60
|
-
"dist",
|
|
61
|
-
"README.md",
|
|
62
|
-
"README.zh-CN.md"
|
|
63
|
-
],
|
|
64
56
|
"typesVersions": {
|
|
65
57
|
"*": {
|
|
66
58
|
"text": [
|
|
67
59
|
"dist/text.d.ts"
|
|
68
60
|
]
|
|
69
61
|
}
|
|
62
|
+
},
|
|
63
|
+
"files": [
|
|
64
|
+
"dist",
|
|
65
|
+
"README.md",
|
|
66
|
+
"README.zh-CN.md"
|
|
67
|
+
],
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
70
|
}
|
|
71
71
|
}
|