@spacecorexdev/core 0.0.2 → 0.0.4
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/package.json +19 -3
- package/public/cmaps/78-EUC-H.bcmap +0 -0
- package/public/cmaps/78-EUC-V.bcmap +0 -0
- package/public/cmaps/78-H.bcmap +0 -0
- package/public/cmaps/78-RKSJ-H.bcmap +0 -0
- package/public/cmaps/78-RKSJ-V.bcmap +0 -0
- package/public/cmaps/78-V.bcmap +0 -0
- package/public/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/public/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/public/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/public/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/public/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/public/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/public/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/public/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/public/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/public/cmaps/Add-H.bcmap +0 -0
- package/public/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/public/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/public/cmaps/Add-V.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/public/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/public/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/public/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/public/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/public/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/public/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/public/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/public/cmaps/B5-H.bcmap +0 -0
- package/public/cmaps/B5-V.bcmap +0 -0
- package/public/cmaps/B5pc-H.bcmap +0 -0
- package/public/cmaps/B5pc-V.bcmap +0 -0
- package/public/cmaps/CNS-EUC-H.bcmap +0 -0
- package/public/cmaps/CNS-EUC-V.bcmap +0 -0
- package/public/cmaps/CNS1-H.bcmap +0 -0
- package/public/cmaps/CNS1-V.bcmap +0 -0
- package/public/cmaps/CNS2-H.bcmap +0 -0
- package/public/cmaps/CNS2-V.bcmap +3 -0
- package/public/cmaps/ETHK-B5-H.bcmap +0 -0
- package/public/cmaps/ETHK-B5-V.bcmap +0 -0
- package/public/cmaps/ETen-B5-H.bcmap +0 -0
- package/public/cmaps/ETen-B5-V.bcmap +0 -0
- package/public/cmaps/ETenms-B5-H.bcmap +3 -0
- package/public/cmaps/ETenms-B5-V.bcmap +0 -0
- package/public/cmaps/EUC-H.bcmap +0 -0
- package/public/cmaps/EUC-V.bcmap +0 -0
- package/public/cmaps/Ext-H.bcmap +0 -0
- package/public/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/public/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/public/cmaps/Ext-V.bcmap +0 -0
- package/public/cmaps/GB-EUC-H.bcmap +0 -0
- package/public/cmaps/GB-EUC-V.bcmap +0 -0
- package/public/cmaps/GB-H.bcmap +4 -0
- package/public/cmaps/GB-V.bcmap +0 -0
- package/public/cmaps/GBK-EUC-H.bcmap +0 -0
- package/public/cmaps/GBK-EUC-V.bcmap +0 -0
- package/public/cmaps/GBK2K-H.bcmap +0 -0
- package/public/cmaps/GBK2K-V.bcmap +0 -0
- package/public/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/public/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/public/cmaps/GBT-EUC-H.bcmap +0 -0
- package/public/cmaps/GBT-EUC-V.bcmap +0 -0
- package/public/cmaps/GBT-H.bcmap +0 -0
- package/public/cmaps/GBT-V.bcmap +0 -0
- package/public/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/public/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/public/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/public/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/public/cmaps/H.bcmap +0 -0
- package/public/cmaps/HKdla-B5-H.bcmap +0 -0
- package/public/cmaps/HKdla-B5-V.bcmap +0 -0
- package/public/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/public/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/public/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/public/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/public/cmaps/HKm314-B5-H.bcmap +0 -0
- package/public/cmaps/HKm314-B5-V.bcmap +0 -0
- package/public/cmaps/HKm471-B5-H.bcmap +0 -0
- package/public/cmaps/HKm471-B5-V.bcmap +0 -0
- package/public/cmaps/HKscs-B5-H.bcmap +0 -0
- package/public/cmaps/HKscs-B5-V.bcmap +0 -0
- package/public/cmaps/Hankaku.bcmap +0 -0
- package/public/cmaps/Hiragana.bcmap +0 -0
- package/public/cmaps/KSC-EUC-H.bcmap +0 -0
- package/public/cmaps/KSC-EUC-V.bcmap +0 -0
- package/public/cmaps/KSC-H.bcmap +0 -0
- package/public/cmaps/KSC-Johab-H.bcmap +0 -0
- package/public/cmaps/KSC-Johab-V.bcmap +0 -0
- package/public/cmaps/KSC-V.bcmap +0 -0
- package/public/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/public/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/public/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/public/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/public/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/public/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/public/cmaps/Katakana.bcmap +0 -0
- package/public/cmaps/LICENSE +36 -0
- package/public/cmaps/NWP-H.bcmap +0 -0
- package/public/cmaps/NWP-V.bcmap +0 -0
- package/public/cmaps/RKSJ-H.bcmap +0 -0
- package/public/cmaps/RKSJ-V.bcmap +0 -0
- package/public/cmaps/Roman.bcmap +0 -0
- package/public/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/public/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/public/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/public/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/public/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/public/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/public/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/public/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/public/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/public/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/public/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/public/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/public/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/public/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/public/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/public/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/public/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/public/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/public/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/public/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/public/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/public/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/public/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/public/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/public/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/public/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/public/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/public/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/public/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/public/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/public/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/public/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/public/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/public/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/public/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/public/cmaps/V.bcmap +0 -0
- package/public/cmaps/WP-Symbol.bcmap +0 -0
- package/public/polyfills/polyfills.js +802 -0
- package/public/vendor/docx/docx.worker.js +5 -0
- package/public/vendor/docx/jszip.min.js +13 -0
- package/public/vendor/libarchive/libarchive.wasm +0 -0
- package/public/vendor/libarchive/worker-bundle.js +6 -0
- package/public/vendor/pptx/pptx.worker.js +26 -0
- package/public/wasm/cad/0.8.2/dwfv-render.wasm +0 -0
- package/public/wasm/cad/0.8.2/dwg-worker.js +24 -0
- package/public/wasm/cad/0.8.2/dwg-worker.js.map +1 -0
- package/public/wasm/cad/0.8.2/libredwg-web.js +2 -0
- package/public/wasm/cad/0.8.2/libredwg-web.wasm +0 -0
- package/public/wasm/cad/dwfv-render.wasm +0 -0
- package/public/wasm/cad/dwg-worker.js +24 -0
- package/public/wasm/cad/dwg-worker.js.map +1 -0
- package/public/wasm/cad/libredwg-web.js +2 -0
- package/public/wasm/cad/libredwg-web.wasm +0 -0
- package/public/wasm/data/sql-wasm.wasm +0 -0
- package/public/wasm/hdiff.wasm +0 -0
- package/public/wasm/model/LICENSE.occt-import-js.txt +504 -0
- package/public/wasm/model/LICENSE.occt.txt +502 -0
- package/public/wasm/model/occt-import-js.js +19 -0
- package/public/wasm/model/occt-import-js.wasm +0 -0
- package/public/wasm/model/occt-worker.js +83 -0
- package/public/wasm/typst/fonts/DejaVuSansMono-Bold.ttf +0 -0
- package/public/wasm/typst/fonts/DejaVuSansMono-BoldOblique.ttf +0 -0
- package/public/wasm/typst/fonts/DejaVuSansMono-Oblique.ttf +0 -0
- package/public/wasm/typst/fonts/DejaVuSansMono.ttf +0 -0
- package/public/wasm/typst/fonts/LibertinusSerif-Bold.otf +0 -0
- package/public/wasm/typst/fonts/LibertinusSerif-BoldItalic.otf +0 -0
- package/public/wasm/typst/fonts/LibertinusSerif-Italic.otf +0 -0
- package/public/wasm/typst/fonts/LibertinusSerif-Regular.otf +0 -0
- package/public/wasm/typst/fonts/LibertinusSerif-Semibold.otf +0 -0
- package/public/wasm/typst/fonts/LibertinusSerif-SemiboldItalic.otf +0 -0
- package/public/wasm/typst/fonts/NewCM10-Bold.otf +0 -0
- package/public/wasm/typst/fonts/NewCM10-BoldItalic.otf +0 -0
- package/public/wasm/typst/fonts/NewCM10-Italic.otf +0 -0
- package/public/wasm/typst/fonts/NewCM10-Regular.otf +0 -0
- package/public/wasm/typst/fonts/NewCMMath-Bold.otf +0 -0
- package/public/wasm/typst/fonts/NewCMMath-Book.otf +0 -0
- package/public/wasm/typst/fonts/NewCMMath-Regular.otf +0 -0
- package/public/wasm/typst/fonts/README.md +7 -0
- package/public/wasm/typst/typst_ts_renderer_bg.wasm +0 -0
- package/public/wasm/typst/typst_ts_web_compiler_bg.wasm +0 -0
- package/scripts/patch-docx-grid.cjs +412 -0
|
@@ -0,0 +1,802 @@
|
|
|
1
|
+
/* =========================================================
|
|
2
|
+
* Safari / 旧 WebView 运行时能力兜底
|
|
3
|
+
* 目标执行上下文:主窗口(window) / Web Worker(self) / Service Worker(self)
|
|
4
|
+
* 用法:
|
|
5
|
+
* 1) 主窗口 <script src="/polyfills/polyfills.js"></script>(放在最前)
|
|
6
|
+
* 2) Worker: self.importScripts('/polyfills/polyfills.js');(放在业务脚本最前)
|
|
7
|
+
* 不引入第三方库,完全手工最小实现;只在缺失时赋值,避免与 React dispatcher 冲突。
|
|
8
|
+
* ========================================================= */
|
|
9
|
+
(function installPolyfills(global) {
|
|
10
|
+
if (!global) return;
|
|
11
|
+
|
|
12
|
+
/* -------------------------------------------------------
|
|
13
|
+
* 辅助:仅在缺失时挂载
|
|
14
|
+
* ----------------------------------------------------- */
|
|
15
|
+
function setIfMissing(target, key, value) {
|
|
16
|
+
try {
|
|
17
|
+
if (typeof target[key] === 'undefined') {
|
|
18
|
+
// 优先 defineProperty:可写/可配置但不参与枚举,与原生一致
|
|
19
|
+
Object.defineProperty(target, key, {
|
|
20
|
+
configurable: true,
|
|
21
|
+
enumerable: false,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: value,
|
|
24
|
+
});
|
|
25
|
+
// 部分老 Safari 对内置构造器/全局 defineProperty 不抛错但不生效(静默失败),
|
|
26
|
+
// 因此赋值后立即比对,必要时退化为直接赋值兜底。
|
|
27
|
+
if (target[key] !== value) {
|
|
28
|
+
try { target[key] = value; } catch (_e2) { /* skip */ }
|
|
29
|
+
}
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
} catch (_err) {
|
|
33
|
+
// 严格冻结对象:尝试直接赋值兜底;再失败则静默忽略
|
|
34
|
+
try { target[key] = value; return; } catch (_e) { /* skip */ }
|
|
35
|
+
}
|
|
36
|
+
// 最后一道:再次验证 defineProperty 未报错但实际未写入的场景
|
|
37
|
+
try {
|
|
38
|
+
if (typeof target[key] === 'undefined' && target[key] !== value) target[key] = value;
|
|
39
|
+
} catch (_e) { /* skip */ }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* =======================================================
|
|
43
|
+
* 1) Promise.allSettled
|
|
44
|
+
* ===================================================== */
|
|
45
|
+
(function polyfillPromiseAllSettled() {
|
|
46
|
+
var PromiseCtor = global.Promise;
|
|
47
|
+
if (!PromiseCtor) return;
|
|
48
|
+
if (typeof PromiseCtor.allSettled === 'function') return;
|
|
49
|
+
|
|
50
|
+
var allSettledImpl = function allSettled(values) {
|
|
51
|
+
var items = Array.isArray(values) ? values : (values && typeof values[Symbol.iterator] === 'function' ? Array.from(values) : []);
|
|
52
|
+
if (items.length === 0) return PromiseCtor.resolve([]);
|
|
53
|
+
return new PromiseCtor(function (resolve) {
|
|
54
|
+
var total = items.length;
|
|
55
|
+
var remaining = total;
|
|
56
|
+
var results = new Array(total);
|
|
57
|
+
function settleOne(index, status, payload) {
|
|
58
|
+
if (status === 'fulfilled') {
|
|
59
|
+
results[index] = { status: 'fulfilled', value: payload };
|
|
60
|
+
} else {
|
|
61
|
+
results[index] = { status: 'rejected', reason: payload };
|
|
62
|
+
}
|
|
63
|
+
remaining -= 1;
|
|
64
|
+
if (remaining === 0) resolve(results);
|
|
65
|
+
}
|
|
66
|
+
for (var i = 0; i < total; i++) {
|
|
67
|
+
(function (idx) {
|
|
68
|
+
try {
|
|
69
|
+
var item = items[idx];
|
|
70
|
+
PromiseCtor.resolve(item).then(
|
|
71
|
+
function (v) { settleOne(idx, 'fulfilled', v); },
|
|
72
|
+
function (r) { settleOne(idx, 'rejected', r); }
|
|
73
|
+
);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
settleOne(idx, 'rejected', err);
|
|
76
|
+
}
|
|
77
|
+
})(i);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
setIfMissing(PromiseCtor, 'allSettled', allSettledImpl);
|
|
83
|
+
})();
|
|
84
|
+
|
|
85
|
+
/* =======================================================
|
|
86
|
+
* 2) QueuingStrategy
|
|
87
|
+
* ===================================================== */
|
|
88
|
+
var ByteLengthQS = (function () {
|
|
89
|
+
function ByteLengthQueuingStrategy(opts) {
|
|
90
|
+
this.highWaterMark = opts && opts.highWaterMark != null ? opts.highWaterMark : 0;
|
|
91
|
+
}
|
|
92
|
+
ByteLengthQueuingStrategy.prototype.size = function (chunk) {
|
|
93
|
+
return chunk && typeof chunk.byteLength === 'number' ? chunk.byteLength : 0;
|
|
94
|
+
};
|
|
95
|
+
return ByteLengthQueuingStrategy;
|
|
96
|
+
})();
|
|
97
|
+
|
|
98
|
+
var CountQS = (function () {
|
|
99
|
+
function CountQueuingStrategy(opts) {
|
|
100
|
+
this.highWaterMark = opts && opts.highWaterMark != null ? opts.highWaterMark : 0;
|
|
101
|
+
}
|
|
102
|
+
CountQueuingStrategy.prototype.size = function () { return 1; };
|
|
103
|
+
return CountQueuingStrategy;
|
|
104
|
+
})();
|
|
105
|
+
|
|
106
|
+
setIfMissing(global, 'ByteLengthQueuingStrategy', ByteLengthQS);
|
|
107
|
+
setIfMissing(global, 'CountQueuingStrategy', CountQS);
|
|
108
|
+
|
|
109
|
+
/* =======================================================
|
|
110
|
+
* 3) ReadableStream (+ Default/BYOB Reader + Default/BYOB Controller)
|
|
111
|
+
* ===================================================== */
|
|
112
|
+
var PolyfillReadable = (function () {
|
|
113
|
+
|
|
114
|
+
function installOnReadableStreamInstance(stream) {
|
|
115
|
+
stream._chunks = [];
|
|
116
|
+
stream._waiting = null;
|
|
117
|
+
stream._byobWaiting = null;
|
|
118
|
+
stream._reader = null;
|
|
119
|
+
stream._byobReader = null;
|
|
120
|
+
stream._state = { closed: false, errored: false, error: undefined };
|
|
121
|
+
stream._pulling = false;
|
|
122
|
+
stream._pullScheduled = false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function toUint8(chunk) {
|
|
126
|
+
if (chunk instanceof Uint8Array) return chunk;
|
|
127
|
+
if (chunk && typeof chunk === 'object' && typeof chunk.buffer === 'object' && typeof chunk.byteOffset === 'number' && typeof chunk.byteLength === 'number') {
|
|
128
|
+
return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
129
|
+
}
|
|
130
|
+
if (chunk instanceof ArrayBuffer) return new Uint8Array(chunk);
|
|
131
|
+
if (typeof chunk === 'string') {
|
|
132
|
+
var enc = new TextEncoder();
|
|
133
|
+
return enc.encode(chunk);
|
|
134
|
+
}
|
|
135
|
+
if (Array.isArray(chunk)) return new Uint8Array(chunk);
|
|
136
|
+
return new Uint8Array(0);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function DefaultController(stream) { this._s = stream; }
|
|
140
|
+
DefaultController.prototype.enqueue = function (chunk) { this._s._push(chunk); };
|
|
141
|
+
DefaultController.prototype.close = function () { this._s._close(); };
|
|
142
|
+
DefaultController.prototype.error = function (e) { this._s._error(e != null ? e : new Error('ReadableStream errored')); };
|
|
143
|
+
Object.defineProperty(DefaultController.prototype, 'desiredSize', {
|
|
144
|
+
configurable: true, get: function () { return this._s._chunks ? 1 : 0; }
|
|
145
|
+
});
|
|
146
|
+
Object.defineProperty(DefaultController.prototype, 'byobRequest', {
|
|
147
|
+
configurable: true, get: function () { return null; }
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
function ByteController(stream) { this._s = stream; }
|
|
151
|
+
ByteController.prototype.enqueue = function (chunk) {
|
|
152
|
+
var u8 = toUint8(chunk);
|
|
153
|
+
this._s._push(u8);
|
|
154
|
+
};
|
|
155
|
+
ByteController.prototype.close = function () { this._s._close(); };
|
|
156
|
+
ByteController.prototype.error = function (e) { this._s._error(e != null ? e : new Error('ReadableByteStream errored')); };
|
|
157
|
+
Object.defineProperty(ByteController.prototype, 'desiredSize', {
|
|
158
|
+
configurable: true, get: function () { return this._s._chunks ? 1 : 0; }
|
|
159
|
+
});
|
|
160
|
+
Object.defineProperty(ByteController.prototype, 'byobRequest', {
|
|
161
|
+
configurable: true, get: function () { return null; }
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
function DefaultReader(stream) {
|
|
165
|
+
var self = this;
|
|
166
|
+
this._s = stream;
|
|
167
|
+
stream._reader = this;
|
|
168
|
+
this.closed = new Promise(function (res) { self._res = res; });
|
|
169
|
+
}
|
|
170
|
+
DefaultReader.prototype.read = function () { return this._s._read(); };
|
|
171
|
+
DefaultReader.prototype.releaseLock = function () {
|
|
172
|
+
if (this._s._reader === this) this._s._reader = null;
|
|
173
|
+
};
|
|
174
|
+
DefaultReader.prototype.cancel = function (reason) { return this._s.cancel(reason); };
|
|
175
|
+
|
|
176
|
+
function BYOBReader(stream) {
|
|
177
|
+
var self = this;
|
|
178
|
+
this._s = stream;
|
|
179
|
+
stream._byobReader = this;
|
|
180
|
+
this.closed = new Promise(function (res) { self._res = res; });
|
|
181
|
+
}
|
|
182
|
+
BYOBReader.prototype.read = function (view) { return this._s._readInto(view); };
|
|
183
|
+
BYOBReader.prototype.releaseLock = function () {
|
|
184
|
+
if (this._s._byobReader === this) this._s._byobReader = null;
|
|
185
|
+
};
|
|
186
|
+
BYOBReader.prototype.cancel = function (reason) { return this._s.cancel(reason); };
|
|
187
|
+
|
|
188
|
+
function ReadableStream(underlyingSource, _strategy) {
|
|
189
|
+
var self = this;
|
|
190
|
+
installOnReadableStreamInstance(self);
|
|
191
|
+
self.locked = false;
|
|
192
|
+
var src = underlyingSource || null;
|
|
193
|
+
self._source = src;
|
|
194
|
+
self._cancelCallback = src && typeof src.cancel === 'function' ? src.cancel : null;
|
|
195
|
+
if (src && src.type === 'bytes') {
|
|
196
|
+
self._type = 'bytes';
|
|
197
|
+
self._autoAllocateChunkSize = src.autoAllocateChunkSize;
|
|
198
|
+
self._controller = new ByteController(self);
|
|
199
|
+
} else {
|
|
200
|
+
self._type = undefined;
|
|
201
|
+
self._controller = new DefaultController(self);
|
|
202
|
+
}
|
|
203
|
+
var start = src && typeof src.start === 'function' ? src.start : null;
|
|
204
|
+
if (start) {
|
|
205
|
+
Promise.resolve(start.call(src, self._controller))['catch'](function (e) { self._error(e); });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
ReadableStream.prototype.getReader = function (opts) {
|
|
210
|
+
if (opts && opts.mode === 'byob') {
|
|
211
|
+
if (this._byobReader) throw new TypeError('ReadableStream is locked');
|
|
212
|
+
return new BYOBReader(this);
|
|
213
|
+
}
|
|
214
|
+
if (this._reader) throw new TypeError('ReadableStream is locked');
|
|
215
|
+
return new DefaultReader(this);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
ReadableStream.prototype._schedulePullIfNeeded = function () {
|
|
219
|
+
var self = this;
|
|
220
|
+
if (self._pullScheduled) return;
|
|
221
|
+
var src = self._source;
|
|
222
|
+
var pull = src && typeof src.pull === 'function' ? src.pull : null;
|
|
223
|
+
if (!pull) return;
|
|
224
|
+
if (self._state.closed || self._state.errored) return;
|
|
225
|
+
if (self._chunks.length > 0 || self._waiting || self._byobWaiting) return;
|
|
226
|
+
self._pullScheduled = true;
|
|
227
|
+
var fn = (typeof queueMicrotask === 'function') ? queueMicrotask : function (cb) { Promise.resolve().then(cb); };
|
|
228
|
+
fn(function () {
|
|
229
|
+
self._pullScheduled = false;
|
|
230
|
+
if (self._pulling) return;
|
|
231
|
+
if (self._state.closed || self._state.errored) return;
|
|
232
|
+
self._pulling = true;
|
|
233
|
+
Promise.resolve(pull.call(self._source, self._controller))
|
|
234
|
+
['catch'](function (e) { self._error(e); })
|
|
235
|
+
.then(function () { self._pulling = false; }, function () { self._pulling = false; });
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
ReadableStream.prototype._push = function (chunk) {
|
|
240
|
+
if (this._state.closed || this._state.errored) return;
|
|
241
|
+
if (this._waiting) {
|
|
242
|
+
var resolve = this._waiting; this._waiting = null;
|
|
243
|
+
resolve({ done: false, value: chunk });
|
|
244
|
+
this._schedulePullIfNeeded();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (this._byobWaiting) {
|
|
248
|
+
var resolveByob = this._byobWaiting; this._byobWaiting = null;
|
|
249
|
+
var src = toUint8(chunk);
|
|
250
|
+
var out = new Uint8Array(src.length); out.set(src);
|
|
251
|
+
resolveByob({ done: false, value: out });
|
|
252
|
+
this._schedulePullIfNeeded();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this._chunks.push({ type: 'chunk', value: chunk });
|
|
256
|
+
this._schedulePullIfNeeded();
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
ReadableStream.prototype._close = function () {
|
|
260
|
+
if (this._state.closed || this._state.errored) return;
|
|
261
|
+
this._state.closed = true;
|
|
262
|
+
this._chunks.push({ type: 'close' });
|
|
263
|
+
if (this._waiting) {
|
|
264
|
+
var resolve = this._waiting; this._waiting = null;
|
|
265
|
+
resolve({ done: true, value: undefined });
|
|
266
|
+
}
|
|
267
|
+
this._finalize();
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
ReadableStream.prototype._error = function (error) {
|
|
271
|
+
if (this._state.closed || this._state.errored) return;
|
|
272
|
+
this._state.errored = true;
|
|
273
|
+
this._state.error = error;
|
|
274
|
+
this._chunks.push({ type: 'error', error: error });
|
|
275
|
+
if (this._waiting) {
|
|
276
|
+
var resolve = this._waiting; this._waiting = null;
|
|
277
|
+
resolve(Promise.reject(error));
|
|
278
|
+
}
|
|
279
|
+
this._finalize();
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
ReadableStream.prototype._finalize = function () {
|
|
283
|
+
if (this._reader && this._reader._res) this._reader._res(undefined);
|
|
284
|
+
if (this._byobReader && this._byobReader._res) this._byobReader._res(undefined);
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
ReadableStream.prototype._read = function () {
|
|
288
|
+
var self = this;
|
|
289
|
+
if (self._state.errored) return Promise.reject(self._state.error);
|
|
290
|
+
if (self._state.closed) return Promise.resolve({ done: true, value: undefined });
|
|
291
|
+
while (self._chunks.length) {
|
|
292
|
+
var item = self._chunks.shift();
|
|
293
|
+
if (item.type === 'chunk') {
|
|
294
|
+
self._schedulePullIfNeeded();
|
|
295
|
+
return Promise.resolve({ done: false, value: item.value });
|
|
296
|
+
}
|
|
297
|
+
if (item.type === 'close') return Promise.resolve({ done: true, value: undefined });
|
|
298
|
+
if (item.type === 'error') return Promise.reject(item.error);
|
|
299
|
+
}
|
|
300
|
+
return new Promise(function (resolve, reject) {
|
|
301
|
+
self._waiting = function (valOrPromise) {
|
|
302
|
+
if (valOrPromise && typeof valOrPromise.then === 'function') {
|
|
303
|
+
valOrPromise.then(function (v) { return resolve(v); }, function (e) { return reject(e); });
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (self._state.errored) reject(self._state.error);
|
|
307
|
+
else resolve(valOrPromise);
|
|
308
|
+
};
|
|
309
|
+
self._schedulePullIfNeeded();
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
ReadableStream.prototype._readInto = function (view) {
|
|
314
|
+
var self = this;
|
|
315
|
+
if (self._state.errored) return Promise.reject(self._state.error);
|
|
316
|
+
if (self._state.closed) return Promise.resolve({ done: true, value: view });
|
|
317
|
+
var written = 0;
|
|
318
|
+
var target = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
319
|
+
while (self._chunks.length && written < target.byteLength) {
|
|
320
|
+
var item = self._chunks[0];
|
|
321
|
+
if (item.type === 'chunk') {
|
|
322
|
+
var src = toUint8(item.value);
|
|
323
|
+
var toCopy = Math.min(src.length, target.byteLength - written);
|
|
324
|
+
target.set(src.subarray(0, toCopy), written);
|
|
325
|
+
written += toCopy;
|
|
326
|
+
if (toCopy < src.length) item.value = src.subarray(toCopy);
|
|
327
|
+
else self._chunks.shift();
|
|
328
|
+
} else if (item.type === 'close') {
|
|
329
|
+
self._chunks.shift();
|
|
330
|
+
var out = written === 0 ? view : new DataView(view.buffer, view.byteOffset, written);
|
|
331
|
+
return Promise.resolve({ done: true, value: written ? out : view });
|
|
332
|
+
} else if (item.type === 'error') {
|
|
333
|
+
self._chunks.shift();
|
|
334
|
+
return Promise.reject(item.error);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (written > 0) {
|
|
338
|
+
var newView = new DataView(view.buffer, view.byteOffset, written);
|
|
339
|
+
self._schedulePullIfNeeded();
|
|
340
|
+
return Promise.resolve({ done: false, value: newView });
|
|
341
|
+
}
|
|
342
|
+
return new Promise(function (resolve, reject) {
|
|
343
|
+
self._byobWaiting = function (res) {
|
|
344
|
+
if (self._state.errored) reject(self._state.error);
|
|
345
|
+
else resolve(res);
|
|
346
|
+
};
|
|
347
|
+
self._schedulePullIfNeeded();
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
ReadableStream.prototype.cancel = function (reason) {
|
|
352
|
+
var self = this;
|
|
353
|
+
if (self._state.closed || self._state.errored) return Promise.resolve();
|
|
354
|
+
self._state.closed = true;
|
|
355
|
+
var done = function () { self._finalize(); };
|
|
356
|
+
if (self._cancelCallback) {
|
|
357
|
+
try {
|
|
358
|
+
var r = self._cancelCallback(reason);
|
|
359
|
+
if (r && typeof r.then === 'function') {
|
|
360
|
+
return Promise.resolve(r).then(function () { done(); return undefined; }, function () { done(); return undefined; });
|
|
361
|
+
}
|
|
362
|
+
} catch (e) { /* continue to finalize */ }
|
|
363
|
+
}
|
|
364
|
+
done();
|
|
365
|
+
return Promise.resolve();
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
ReadableStream.prototype.tee = function () {
|
|
369
|
+
var left = branch(this);
|
|
370
|
+
var right = branch(this);
|
|
371
|
+
return [left, right];
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
function branch(src) {
|
|
375
|
+
var rs = new ReadableStream({
|
|
376
|
+
start: function (controller) {
|
|
377
|
+
var reader = src.getReader();
|
|
378
|
+
function pump() {
|
|
379
|
+
return reader.read().then(function (res) {
|
|
380
|
+
if (res.done) return controller.close();
|
|
381
|
+
controller.enqueue(res.value);
|
|
382
|
+
return pump();
|
|
383
|
+
})['catch'](function (e) {
|
|
384
|
+
controller.error(e);
|
|
385
|
+
}).then(function () {
|
|
386
|
+
try { reader.releaseLock(); } catch (_e) { /* skip */ }
|
|
387
|
+
}, function () {
|
|
388
|
+
try { reader.releaseLock(); } catch (_e) { /* skip */ }
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
return pump();
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
return rs;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
ReadableStream.prototype.pipeTo = function (dest, options) {
|
|
398
|
+
var preventClose = !!(options && options.preventClose);
|
|
399
|
+
var preventAbort = !!(options && options.preventAbort);
|
|
400
|
+
var preventCancel = !!(options && options.preventCancel);
|
|
401
|
+
var writer = dest && typeof dest.getWriter === 'function' ? dest.getWriter() : null;
|
|
402
|
+
var self = this;
|
|
403
|
+
return new Promise(function (resolve, reject) {
|
|
404
|
+
var reader = self.getReader();
|
|
405
|
+
function releaseAll() {
|
|
406
|
+
try { reader.releaseLock(); } catch (_e) { /* skip */ }
|
|
407
|
+
if (writer && typeof writer.releaseLock === 'function') {
|
|
408
|
+
try { writer.releaseLock(); } catch (_e) { /* skip */ }
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function closeDest() {
|
|
412
|
+
if (preventClose) return resolve();
|
|
413
|
+
if (writer && typeof writer.close === 'function') {
|
|
414
|
+
Promise.resolve(writer.close()).then(function () { resolve(); }, function (e) { reject(e); });
|
|
415
|
+
} else if (dest && typeof dest.close === 'function') {
|
|
416
|
+
try { dest.close(); } catch (_e) { /* skip */ }
|
|
417
|
+
resolve();
|
|
418
|
+
} else {
|
|
419
|
+
resolve();
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
function abortDest(err) {
|
|
423
|
+
if (preventAbort) return;
|
|
424
|
+
try {
|
|
425
|
+
if (writer && typeof writer.abort === 'function') {
|
|
426
|
+
var pr = writer.abort(err);
|
|
427
|
+
if (pr && typeof pr.then === 'function') pr.then(null, function () { /* already aborting */ });
|
|
428
|
+
} else if (dest && typeof dest.abort === 'function') {
|
|
429
|
+
dest.abort(err);
|
|
430
|
+
}
|
|
431
|
+
} catch (_e) { /* skip */ }
|
|
432
|
+
}
|
|
433
|
+
function cancelSelf(err) {
|
|
434
|
+
if (preventCancel) return;
|
|
435
|
+
try {
|
|
436
|
+
var pr = self.cancel(err);
|
|
437
|
+
if (pr && typeof pr.then === 'function') pr.then(null, function () { /* already canceling */ });
|
|
438
|
+
} catch (_e) { /* skip */ }
|
|
439
|
+
}
|
|
440
|
+
function pump() {
|
|
441
|
+
return reader.read().then(function (res) {
|
|
442
|
+
if (res.done) {
|
|
443
|
+
closeDest();
|
|
444
|
+
return undefined;
|
|
445
|
+
}
|
|
446
|
+
var writePromise;
|
|
447
|
+
if (writer) {
|
|
448
|
+
if (writer.ready && typeof writer.ready.then === 'function') {
|
|
449
|
+
writePromise = writer.ready.then(function () { return writer.write(res.value); });
|
|
450
|
+
} else {
|
|
451
|
+
writePromise = writer.write(res.value);
|
|
452
|
+
}
|
|
453
|
+
} else if (dest && typeof dest.write === 'function') {
|
|
454
|
+
writePromise = Promise.resolve(dest.write(res.value));
|
|
455
|
+
} else {
|
|
456
|
+
writePromise = Promise.resolve();
|
|
457
|
+
}
|
|
458
|
+
return Promise.resolve(writePromise).then(pump, function (writeErr) {
|
|
459
|
+
abortDest(writeErr); cancelSelf(writeErr);
|
|
460
|
+
throw writeErr;
|
|
461
|
+
});
|
|
462
|
+
})['catch'](function (err) {
|
|
463
|
+
releaseAll();
|
|
464
|
+
abortDest(err); cancelSelf(err);
|
|
465
|
+
reject(err);
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
Promise.resolve()
|
|
469
|
+
.then(pump)
|
|
470
|
+
.then(function () { releaseAll(); }, function () { releaseAll(); });
|
|
471
|
+
});
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
ReadableStream.prototype.pipeThrough = function (transform, options) {
|
|
475
|
+
this.pipeTo(transform.writable, options);
|
|
476
|
+
return transform.readable;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
return {
|
|
480
|
+
ReadableStream: ReadableStream,
|
|
481
|
+
DefaultController: DefaultController,
|
|
482
|
+
ByteController: ByteController,
|
|
483
|
+
DefaultReader: DefaultReader,
|
|
484
|
+
BYOBReader: BYOBReader,
|
|
485
|
+
};
|
|
486
|
+
})();
|
|
487
|
+
|
|
488
|
+
setIfMissing(global, 'ReadableStream', PolyfillReadable.ReadableStream);
|
|
489
|
+
setIfMissing(global, 'ReadableByteStreamController', PolyfillReadable.ByteController);
|
|
490
|
+
setIfMissing(global, 'ReadableStreamDefaultController', PolyfillReadable.DefaultController);
|
|
491
|
+
setIfMissing(global, 'ReadableStreamDefaultReader', PolyfillReadable.DefaultReader);
|
|
492
|
+
setIfMissing(global, 'ReadableStreamBYOBReader', PolyfillReadable.BYOBReader);
|
|
493
|
+
|
|
494
|
+
/* =======================================================
|
|
495
|
+
* 4) WritableStream + DefaultWriter
|
|
496
|
+
* ===================================================== */
|
|
497
|
+
var PolyfillWritable = (function () {
|
|
498
|
+
function DefaultWriter(stream) {
|
|
499
|
+
var self = this;
|
|
500
|
+
this._stream = stream;
|
|
501
|
+
stream._writer = this;
|
|
502
|
+
this.closed = new Promise(function (res) { self._resClosed = res; });
|
|
503
|
+
this.ready = Promise.resolve(undefined);
|
|
504
|
+
this.desiredSize = 1;
|
|
505
|
+
}
|
|
506
|
+
DefaultWriter.prototype.abort = function (reason) { return this._stream.abort(reason); };
|
|
507
|
+
DefaultWriter.prototype.close = function () { return this._stream.close(); };
|
|
508
|
+
DefaultWriter.prototype.releaseLock = function () {
|
|
509
|
+
if (this._stream._writer === this) this._stream._writer = null;
|
|
510
|
+
};
|
|
511
|
+
DefaultWriter.prototype.write = function (chunk) { return this._stream.write(chunk); };
|
|
512
|
+
|
|
513
|
+
function makeController(store) {
|
|
514
|
+
return {
|
|
515
|
+
get signal() { return store.signal; },
|
|
516
|
+
error: function (e) { store.onError(e != null ? e : new Error('WritableStream errored')); }
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function WritableStream(sink, _strategy) {
|
|
521
|
+
var self = this;
|
|
522
|
+
self._writer = null;
|
|
523
|
+
self.locked = false;
|
|
524
|
+
self._state = { closed: false, errored: false, error: undefined };
|
|
525
|
+
self._sink = sink || {};
|
|
526
|
+
self._signal = { aborted: false, reason: undefined };
|
|
527
|
+
var ctrlStore = {
|
|
528
|
+
signal: self._signal,
|
|
529
|
+
onError: function (err) { self._error(err); }
|
|
530
|
+
};
|
|
531
|
+
self._controller = makeController(ctrlStore);
|
|
532
|
+
var start = self._sink && typeof self._sink.start === 'function' ? self._sink.start : null;
|
|
533
|
+
if (start) {
|
|
534
|
+
Promise.resolve(start.call(self._sink, self._controller))['catch'](function (e) { self._error(e); });
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
WritableStream.prototype.getWriter = function () {
|
|
539
|
+
if (this._writer) throw new TypeError('WritableStream is locked');
|
|
540
|
+
return new DefaultWriter(this);
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
WritableStream.prototype.write = function (chunk) {
|
|
544
|
+
if (this._state.errored) return Promise.reject(this._state.error);
|
|
545
|
+
if (this._state.closed) return Promise.reject(new TypeError('WritableStream is closed'));
|
|
546
|
+
var fn = this._sink && typeof this._sink.write === 'function' ? this._sink.write : null;
|
|
547
|
+
if (!fn) return Promise.resolve();
|
|
548
|
+
try {
|
|
549
|
+
var r = fn.call(this._sink, chunk, this._controller);
|
|
550
|
+
return Promise.resolve(r);
|
|
551
|
+
} catch (e) { return Promise.reject(e); }
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
WritableStream.prototype.close = function () {
|
|
555
|
+
var self = this;
|
|
556
|
+
if (self._state.closed || self._state.errored) return Promise.resolve();
|
|
557
|
+
self._state.closed = true;
|
|
558
|
+
var fn = self._sink && typeof self._sink.close === 'function' ? self._sink.close : null;
|
|
559
|
+
var after = function () { self._finalize(); };
|
|
560
|
+
if (!fn) { after(); return Promise.resolve(); }
|
|
561
|
+
try {
|
|
562
|
+
var r = fn.call(self._sink);
|
|
563
|
+
return Promise.resolve(r).then(function () { after(); }, function (e) { after(); return Promise.reject(e); });
|
|
564
|
+
} catch (e) { after(); return Promise.reject(e); }
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
WritableStream.prototype.abort = function (reason) {
|
|
568
|
+
var self = this;
|
|
569
|
+
if (self._state.closed || self._state.errored) return Promise.resolve();
|
|
570
|
+
self._signal.aborted = true;
|
|
571
|
+
self._signal.reason = reason;
|
|
572
|
+
self._state.errored = true;
|
|
573
|
+
self._state.error = reason;
|
|
574
|
+
var fn = self._sink && typeof self._sink.abort === 'function' ? self._sink.abort : null;
|
|
575
|
+
var after = function () { self._finalize(); };
|
|
576
|
+
if (!fn) { after(); return Promise.resolve(); }
|
|
577
|
+
try {
|
|
578
|
+
var r = fn.call(self._sink, reason);
|
|
579
|
+
return Promise.resolve(r).then(function () { after(); }, function (e) { after(); return Promise.reject(e); });
|
|
580
|
+
} catch (e) { after(); return Promise.reject(e); }
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
WritableStream.prototype._error = function (err) {
|
|
584
|
+
if (this._state.closed || this._state.errored) return;
|
|
585
|
+
this._state.errored = true;
|
|
586
|
+
this._state.error = err;
|
|
587
|
+
this._finalize();
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
WritableStream.prototype._finalize = function () {
|
|
591
|
+
if (this._writer && this._writer._resClosed) this._writer._resClosed(undefined);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
return { WritableStream: WritableStream, DefaultWriter: DefaultWriter };
|
|
595
|
+
})();
|
|
596
|
+
|
|
597
|
+
setIfMissing(global, 'WritableStream', PolyfillWritable.WritableStream);
|
|
598
|
+
setIfMissing(global, 'WritableStreamDefaultWriter', PolyfillWritable.DefaultWriter);
|
|
599
|
+
|
|
600
|
+
/* =======================================================
|
|
601
|
+
* 5) TransformStream
|
|
602
|
+
* ===================================================== */
|
|
603
|
+
var PolyfillTransform = (function () {
|
|
604
|
+
function TransformStream(transformer, _writableStrategy, _readableStrategy) {
|
|
605
|
+
var readableController;
|
|
606
|
+
var readable = new global.ReadableStream({
|
|
607
|
+
start: function (controller) {
|
|
608
|
+
readableController = controller;
|
|
609
|
+
var startFn = transformer && typeof transformer.start === 'function' ? transformer.start : null;
|
|
610
|
+
if (startFn) {
|
|
611
|
+
Promise.resolve(startFn.call(transformer, readableController))['catch'](function (e) {
|
|
612
|
+
try { readableController.error(e); } catch (_e) { /* skip */ }
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
var writable = new global.WritableStream({
|
|
618
|
+
write: function (chunk, controller) {
|
|
619
|
+
var fn = transformer && typeof transformer.transform === 'function' ? transformer.transform : null;
|
|
620
|
+
if (fn) {
|
|
621
|
+
return Promise.resolve(fn.call(transformer, chunk, readableController));
|
|
622
|
+
}
|
|
623
|
+
readableController.enqueue(chunk);
|
|
624
|
+
return Promise.resolve();
|
|
625
|
+
},
|
|
626
|
+
close: function () {
|
|
627
|
+
var flush = transformer && typeof transformer.flush === 'function' ? transformer.flush : null;
|
|
628
|
+
var p = flush ? Promise.resolve(flush.call(transformer, readableController)) : Promise.resolve();
|
|
629
|
+
return p.then(function () { try { readableController.close(); } catch (_e) { /* skip */ } });
|
|
630
|
+
},
|
|
631
|
+
abort: function (reason) {
|
|
632
|
+
try { readableController.error(reason); } catch (_e) { /* skip */ }
|
|
633
|
+
var abortFn = transformer && typeof transformer.abort === 'function' ? transformer.abort : null;
|
|
634
|
+
return abortFn ? Promise.resolve(abortFn.call(transformer, reason)) : Promise.resolve();
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
this.readable = readable;
|
|
638
|
+
this.writable = writable;
|
|
639
|
+
}
|
|
640
|
+
return TransformStream;
|
|
641
|
+
})();
|
|
642
|
+
|
|
643
|
+
setIfMissing(global, 'TransformStream', PolyfillTransform);
|
|
644
|
+
|
|
645
|
+
/* =======================================================
|
|
646
|
+
* 6) TextDecoderStream / TextEncoderStream
|
|
647
|
+
* ===================================================== */
|
|
648
|
+
if (typeof global.TextDecoderStream === 'undefined' && typeof global.TextDecoder === 'function') {
|
|
649
|
+
var TextDecoderStream = (function () {
|
|
650
|
+
function PolyfillTDS(label, options) {
|
|
651
|
+
var decoder = new global.TextDecoder(label, options);
|
|
652
|
+
this.encoding = decoder.encoding;
|
|
653
|
+
this.fatal = decoder.fatal;
|
|
654
|
+
this.ignoreBOM = decoder.ignoreBOM;
|
|
655
|
+
var ts = new PolyfillTransform({
|
|
656
|
+
transform: function (chunk, controller) {
|
|
657
|
+
controller.enqueue(decoder.decode(chunk, { stream: true }));
|
|
658
|
+
},
|
|
659
|
+
flush: function (controller) {
|
|
660
|
+
var tail = decoder.decode();
|
|
661
|
+
if (tail) controller.enqueue(tail);
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
this.readable = ts.readable;
|
|
665
|
+
this.writable = ts.writable;
|
|
666
|
+
}
|
|
667
|
+
return PolyfillTDS;
|
|
668
|
+
})();
|
|
669
|
+
global.TextDecoderStream = TextDecoderStream;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (typeof global.TextEncoderStream === 'undefined' && typeof global.TextEncoder === 'function') {
|
|
673
|
+
var TextEncoderStream = (function () {
|
|
674
|
+
function PolyfillTES() {
|
|
675
|
+
var encoder = new global.TextEncoder();
|
|
676
|
+
this.encoding = 'utf-8';
|
|
677
|
+
var ts = new PolyfillTransform({
|
|
678
|
+
transform: function (chunk, controller) { controller.enqueue(encoder.encode(chunk)); }
|
|
679
|
+
});
|
|
680
|
+
this.readable = ts.readable;
|
|
681
|
+
this.writable = ts.writable;
|
|
682
|
+
}
|
|
683
|
+
return PolyfillTES;
|
|
684
|
+
})();
|
|
685
|
+
global.TextEncoderStream = TextEncoderStream;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// Safari 半残实现的第二道防线(所有 Polyfill* 都已声明后再执行):
|
|
689
|
+
// 老版本 Safari 中,ReadableStream 已作为函数存在,但 BYOB 控制器缺失,
|
|
690
|
+
// 或 new ReadableStream({type:'bytes'}) 会抛 "ReadableByteStreamController is not implemented"
|
|
691
|
+
// (React 18 renderToReadableStream → createInternalReadableStreamFromUnderlyingSource 路径)。
|
|
692
|
+
// 命中任一迹象就强制整体替换,保证 React SSR 路径拿到一致实现。
|
|
693
|
+
(function detectBrokenStreamsAndOverride() {
|
|
694
|
+
try {
|
|
695
|
+
var RS = global.ReadableStream;
|
|
696
|
+
var RBC = global.ReadableByteStreamController;
|
|
697
|
+
var WS = global.WritableStream;
|
|
698
|
+
var TS = global.TransformStream;
|
|
699
|
+
if (typeof RS !== 'function') return; // 下一节 setIfMissing 或上方安装已覆盖
|
|
700
|
+
|
|
701
|
+
var broken = false;
|
|
702
|
+
if (typeof RBC !== 'function') broken = true;
|
|
703
|
+
|
|
704
|
+
if (!broken) {
|
|
705
|
+
try {
|
|
706
|
+
var src = {
|
|
707
|
+
type: 'bytes',
|
|
708
|
+
start: function (c) { try { c.close(); } catch (_) {} },
|
|
709
|
+
};
|
|
710
|
+
var tmp = new RS(src);
|
|
711
|
+
try {
|
|
712
|
+
var byob = tmp.getReader({ mode: 'byob' });
|
|
713
|
+
if (!byob) broken = true;
|
|
714
|
+
else try { byob.releaseLock(); } catch (_) {}
|
|
715
|
+
} catch (bErr) {
|
|
716
|
+
var bMsg = String(bErr && bErr.message || '');
|
|
717
|
+
if (/not implemented|ReadableByteStreamController|byob|not supported|invalid mode/i.test(bMsg)) broken = true;
|
|
718
|
+
}
|
|
719
|
+
try { tmp.cancel && tmp.cancel(); } catch (_) {}
|
|
720
|
+
} catch (e) {
|
|
721
|
+
var msg = String(e && e.message || '');
|
|
722
|
+
if (/ReadableByteStreamController is not implemented|is not implemented|not supported|type bytes|invalid type/i.test(msg)) broken = true;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (!broken) {
|
|
727
|
+
var proto = RS.prototype;
|
|
728
|
+
if (!proto || typeof proto.pipeTo !== 'function' || typeof proto.pipeThrough !== 'function' || typeof proto.tee !== 'function') {
|
|
729
|
+
if (typeof WS !== 'function') broken = true;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (!broken && typeof TS !== 'undefined' && typeof TS !== 'function') broken = true;
|
|
733
|
+
|
|
734
|
+
if (!broken) return;
|
|
735
|
+
|
|
736
|
+
var PolyRS = PolyfillReadable && PolyfillReadable.ReadableStream;
|
|
737
|
+
var PolyWS = PolyfillWritable && PolyfillWritable.WritableStream;
|
|
738
|
+
var PolyTS = PolyfillTransform;
|
|
739
|
+
|
|
740
|
+
function forceSet(key, value) {
|
|
741
|
+
try {
|
|
742
|
+
Object.defineProperty(global, key, {
|
|
743
|
+
configurable: true, writable: true, enumerable: false, value: value,
|
|
744
|
+
});
|
|
745
|
+
} catch (_) {
|
|
746
|
+
try { global[key] = value; } catch (__) {}
|
|
747
|
+
}
|
|
748
|
+
try { if (global[key] !== value) global[key] = value; } catch (_) {}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
if (PolyRS) forceSet('ReadableStream', PolyRS);
|
|
752
|
+
if (PolyfillReadable) {
|
|
753
|
+
forceSet('ReadableByteStreamController', PolyfillReadable.ByteController);
|
|
754
|
+
forceSet('ReadableStreamDefaultController', PolyfillReadable.DefaultController);
|
|
755
|
+
forceSet('ReadableStreamDefaultReader', PolyfillReadable.DefaultReader);
|
|
756
|
+
forceSet('ReadableStreamBYOBReader', PolyfillReadable.BYOBReader);
|
|
757
|
+
}
|
|
758
|
+
if (PolyWS) {
|
|
759
|
+
forceSet('WritableStream', PolyWS);
|
|
760
|
+
forceSet('WritableStreamDefaultWriter', PolyfillWritable.DefaultWriter);
|
|
761
|
+
}
|
|
762
|
+
if (PolyTS) forceSet('TransformStream', PolyTS);
|
|
763
|
+
} catch (_e) { /* skip */ }
|
|
764
|
+
})();
|
|
765
|
+
|
|
766
|
+
/* =======================================================
|
|
767
|
+
* 7) 针对部分原生 ReadableStream 但缺少 pipeTo 的老版本 Safari 原型补齐
|
|
768
|
+
* ===================================================== */
|
|
769
|
+
(function patchNativeReadableStreamPrototype() {
|
|
770
|
+
var RS = global.ReadableStream;
|
|
771
|
+
if (!RS || typeof RS !== 'function') return;
|
|
772
|
+
try {
|
|
773
|
+
var proto = RS.prototype;
|
|
774
|
+
if (!proto) return;
|
|
775
|
+
var PolyRS = PolyfillReadable.ReadableStream.prototype;
|
|
776
|
+
if (typeof proto.pipeTo !== 'function') {
|
|
777
|
+
Object.defineProperty(proto, 'pipeTo', {
|
|
778
|
+
configurable: true, writable: true, value: PolyRS.pipeTo,
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
if (typeof proto.pipeThrough !== 'function') {
|
|
782
|
+
Object.defineProperty(proto, 'pipeThrough', {
|
|
783
|
+
configurable: true, writable: true, value: PolyRS.pipeThrough,
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
if (typeof proto.tee !== 'function') {
|
|
787
|
+
Object.defineProperty(proto, 'tee', {
|
|
788
|
+
configurable: true, writable: true, value: PolyRS.tee,
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
} catch (_err) { /* skip */ }
|
|
792
|
+
})();
|
|
793
|
+
|
|
794
|
+
/* =======================================================
|
|
795
|
+
* 8) queueMicrotask (少数老 WebView 缺失)
|
|
796
|
+
* ===================================================== */
|
|
797
|
+
if (typeof global.queueMicrotask !== 'function') {
|
|
798
|
+
global.queueMicrotask = function (cb) {
|
|
799
|
+
Promise.resolve().then(function () { try { cb(); } catch (e) { throw e; } });
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
})(typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : typeof globalThis !== 'undefined' ? globalThis : this);
|