@papyrus-sdk/engine-native 0.2.17 → 0.2.18
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 +10 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +53 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.kt +45 -0
- package/dist/index.d.mts +23 -2
- package/dist/index.d.ts +23 -2
- package/dist/index.js +197 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +197 -40
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusNativeEngine.m +38 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Papyrus Engine Native
|
|
2
2
|
|
|
3
3
|
React Native native engine for PDF rendering (PDFKit on iOS, PDFium on Android),
|
|
4
|
-
plus a WebView runtime for EPUB/TXT via `MobileDocumentEngine`.
|
|
4
|
+
plus a WebView runtime for EPUB/TXT/CBZ via `MobileDocumentEngine`.
|
|
5
5
|
|
|
6
6
|
## Install
|
|
7
7
|
|
|
@@ -11,12 +11,20 @@ npm install @papyrus-sdk/engine-native @papyrus-sdk/core @papyrus-sdk/types
|
|
|
11
11
|
|
|
12
12
|
`@papyrus-sdk/core` and `@papyrus-sdk/types` are required peer dependencies.
|
|
13
13
|
|
|
14
|
-
For EPUB/TXT on mobile, also install:
|
|
14
|
+
For EPUB/TXT/CBZ on mobile, also install:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
17
|
npm install react-native-webview
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
+
CBZ files use the same WebView dependency and are loaded with
|
|
21
|
+
`{ type: 'comic', source: { uri: 'file:///.../book.cbz' } }`. CBR is not part
|
|
22
|
+
of the base mobile runtime because its libarchive worker/WASM payload is
|
|
23
|
+
optional and substantially larger.
|
|
24
|
+
|
|
25
|
+
For CBR, install `@papyrus-sdk/engine-cbr-mobile` and pass its three runtime
|
|
26
|
+
asset URLs through `webViewRuntimeConfig` as shown in that package's README.
|
|
27
|
+
|
|
20
28
|
## Usage
|
|
21
29
|
|
|
22
30
|
```ts
|
|
@@ -4,6 +4,7 @@ import android.content.ContentResolver;
|
|
|
4
4
|
import android.content.Context;
|
|
5
5
|
import android.net.Uri;
|
|
6
6
|
import android.os.ParcelFileDescriptor;
|
|
7
|
+
import android.util.Base64;
|
|
7
8
|
import android.view.View;
|
|
8
9
|
|
|
9
10
|
import com.facebook.react.bridge.Arguments;
|
|
@@ -22,6 +23,7 @@ import com.facebook.react.uimanager.UIManagerModule;
|
|
|
22
23
|
import com.shockwave.pdfium.PdfDocument;
|
|
23
24
|
|
|
24
25
|
import java.io.File;
|
|
26
|
+
import java.io.FileInputStream;
|
|
25
27
|
import java.io.FileOutputStream;
|
|
26
28
|
import java.io.IOException;
|
|
27
29
|
import java.io.InputStream;
|
|
@@ -54,6 +56,57 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
|
|
|
54
56
|
PapyrusEngineStore.destroyEngine(engineId);
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
@ReactMethod
|
|
60
|
+
public void readFileChunk(String uriValue, double offsetValue, int length, Promise promise) {
|
|
61
|
+
executor.execute(() -> {
|
|
62
|
+
try {
|
|
63
|
+
if (length <= 0 || length > 4 * 1024 * 1024) {
|
|
64
|
+
throw new IOException("Invalid file chunk length");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
Uri uri = Uri.parse(uriValue);
|
|
68
|
+
InputStream inputStream;
|
|
69
|
+
if ("file".equalsIgnoreCase(uri.getScheme())) {
|
|
70
|
+
inputStream = new FileInputStream(new File(uri.getPath()));
|
|
71
|
+
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
|
|
72
|
+
ContentResolver resolver = reactContext.getContentResolver();
|
|
73
|
+
inputStream = resolver.openInputStream(uri);
|
|
74
|
+
if (inputStream == null) throw new IOException("Unable to read content URI");
|
|
75
|
+
} else {
|
|
76
|
+
throw new IOException("Unsupported local file URI");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try (InputStream stream = inputStream) {
|
|
80
|
+
long remaining = Math.max(0L, (long) offsetValue);
|
|
81
|
+
while (remaining > 0) {
|
|
82
|
+
long skipped = stream.skip(remaining);
|
|
83
|
+
if (skipped <= 0) {
|
|
84
|
+
if (stream.read() < 0) break;
|
|
85
|
+
skipped = 1;
|
|
86
|
+
}
|
|
87
|
+
remaining -= skipped;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
byte[] buffer = new byte[length];
|
|
91
|
+
int read = 0;
|
|
92
|
+
while (read < length) {
|
|
93
|
+
int count = stream.read(buffer, read, length - read);
|
|
94
|
+
if (count < 0) break;
|
|
95
|
+
if (count == 0) continue;
|
|
96
|
+
read += count;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
WritableMap result = Arguments.createMap();
|
|
100
|
+
result.putString("data", Base64.encodeToString(buffer, 0, read, Base64.NO_WRAP));
|
|
101
|
+
result.putBoolean("done", read < length);
|
|
102
|
+
promise.resolve(result);
|
|
103
|
+
}
|
|
104
|
+
} catch (Throwable error) {
|
|
105
|
+
promise.reject("papyrus_file_chunk_failed", error);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
57
110
|
@ReactMethod
|
|
58
111
|
public void load(final String engineId, final ReadableMap source, final Promise promise) {
|
|
59
112
|
executor.execute(() -> {
|
|
@@ -11,7 +11,9 @@ import expo.modules.kotlin.Promise
|
|
|
11
11
|
import expo.modules.kotlin.modules.Module
|
|
12
12
|
import expo.modules.kotlin.modules.ModuleDefinition
|
|
13
13
|
import expo.modules.kotlin.typedarray.Uint8Array
|
|
14
|
+
import android.util.Base64
|
|
14
15
|
import java.io.File
|
|
16
|
+
import java.io.FileInputStream
|
|
15
17
|
import java.io.FileOutputStream
|
|
16
18
|
import java.io.IOException
|
|
17
19
|
import java.io.InputStream
|
|
@@ -36,6 +38,49 @@ class PapyrusNativeEngineModule : Module() {
|
|
|
36
38
|
PapyrusEngineStore.destroyEngine(engineId)
|
|
37
39
|
}
|
|
38
40
|
|
|
41
|
+
AsyncFunction("readFileChunk") { uriValue: String, offsetValue: Double, length: Int ->
|
|
42
|
+
if (length <= 0 || length > 4 * 1024 * 1024) {
|
|
43
|
+
throw IOException("Invalid file chunk length")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
val uri = Uri.parse(uriValue)
|
|
47
|
+
val inputStream: InputStream = when (uri.scheme?.lowercase()) {
|
|
48
|
+
"file" -> FileInputStream(File(uri.path ?: throw IOException("Invalid file URI")))
|
|
49
|
+
"content" -> {
|
|
50
|
+
val context = appContext.reactContext ?: throw IOException("React context missing")
|
|
51
|
+
context.contentResolver.openInputStream(uri)
|
|
52
|
+
?: throw IOException("Unable to read content URI")
|
|
53
|
+
}
|
|
54
|
+
else -> throw IOException("Unsupported local file URI")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
inputStream.use { stream ->
|
|
58
|
+
var remaining = maxOf(0L, offsetValue.toLong())
|
|
59
|
+
while (remaining > 0) {
|
|
60
|
+
val skipped = stream.skip(remaining)
|
|
61
|
+
if (skipped <= 0L) {
|
|
62
|
+
if (stream.read() < 0) break
|
|
63
|
+
remaining -= 1
|
|
64
|
+
} else {
|
|
65
|
+
remaining -= skipped
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
val buffer = ByteArray(length)
|
|
70
|
+
var read = 0
|
|
71
|
+
while (read < length) {
|
|
72
|
+
val count = stream.read(buffer, read, length - read)
|
|
73
|
+
if (count < 0) break
|
|
74
|
+
if (count > 0) read += count
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
mapOf(
|
|
78
|
+
"data" to Base64.encodeToString(buffer, 0, read, Base64.NO_WRAP),
|
|
79
|
+
"done" to (read < length),
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
39
84
|
AsyncFunction("load") { engineId: String, source: Map<String, Any?>, promise: Promise ->
|
|
40
85
|
executor.execute {
|
|
41
86
|
try {
|
package/dist/index.d.mts
CHANGED
|
@@ -130,7 +130,18 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
|
|
|
130
130
|
type WebViewBridge = {
|
|
131
131
|
postMessage: (message: string) => void;
|
|
132
132
|
};
|
|
133
|
+
type WebViewRuntimeSource = string | number | {
|
|
134
|
+
uri: string;
|
|
135
|
+
};
|
|
136
|
+
type WebViewRuntimeConfig = Record<string, string>;
|
|
137
|
+
type MobileDocumentEngineOptions = {
|
|
138
|
+
/** Optional asset URI/require result for a runtime extension such as CBR. */
|
|
139
|
+
webViewRuntimeSource?: WebViewRuntimeSource;
|
|
140
|
+
webViewRuntimeConfig?: WebViewRuntimeConfig;
|
|
141
|
+
};
|
|
133
142
|
declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
143
|
+
private readonly webViewRuntimeSource?;
|
|
144
|
+
private readonly webViewRuntimeConfig?;
|
|
134
145
|
private bridge;
|
|
135
146
|
private ready;
|
|
136
147
|
private requestId;
|
|
@@ -142,9 +153,14 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
142
153
|
private zoom;
|
|
143
154
|
private rotation;
|
|
144
155
|
private outline;
|
|
156
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
157
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
158
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
145
159
|
getRenderTargetType(): "webview";
|
|
146
160
|
attachBridge(bridge: WebViewBridge): void;
|
|
147
161
|
handleMessage(raw: string): void;
|
|
162
|
+
private handleAssetRequest;
|
|
163
|
+
private handleFileChunkRequest;
|
|
148
164
|
load(input: DocumentLoadInput): Promise<void>;
|
|
149
165
|
getPageCount(): number;
|
|
150
166
|
getCurrentPage(): number;
|
|
@@ -160,6 +176,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
160
176
|
width: number;
|
|
161
177
|
height: number;
|
|
162
178
|
}>;
|
|
179
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
163
180
|
searchText(query: string): Promise<SearchResult[]>;
|
|
164
181
|
selectText(pageIndex: number, rect: {
|
|
165
182
|
x: number;
|
|
@@ -175,6 +192,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
175
192
|
private request;
|
|
176
193
|
private normalizeRuntimeSource;
|
|
177
194
|
private fetchRemoteSource;
|
|
195
|
+
private fetchLocalSource;
|
|
178
196
|
private readResponseBuffer;
|
|
179
197
|
private isUriSource;
|
|
180
198
|
private isDataSource;
|
|
@@ -184,7 +202,9 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
184
202
|
private pdfEngine;
|
|
185
203
|
private webEngine;
|
|
186
204
|
private activeEngine;
|
|
187
|
-
constructor();
|
|
205
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
206
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
207
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
188
208
|
getRenderTargetType(): RenderTargetType;
|
|
189
209
|
getNativeEngineId(): string | null;
|
|
190
210
|
attachWebView(bridge: WebViewBridge): void;
|
|
@@ -204,6 +224,7 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
204
224
|
width: number;
|
|
205
225
|
height: number;
|
|
206
226
|
}>;
|
|
227
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
207
228
|
searchText(query: string): Promise<SearchResult[]>;
|
|
208
229
|
selectText(pageIndex: number, rect: {
|
|
209
230
|
x: number;
|
|
@@ -216,4 +237,4 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
216
237
|
destroy(): void;
|
|
217
238
|
}
|
|
218
239
|
|
|
219
|
-
export { MobileDocumentEngine, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine };
|
|
240
|
+
export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource };
|
package/dist/index.d.ts
CHANGED
|
@@ -130,7 +130,18 @@ declare class NativeDocumentEngine extends BaseDocumentEngine {
|
|
|
130
130
|
type WebViewBridge = {
|
|
131
131
|
postMessage: (message: string) => void;
|
|
132
132
|
};
|
|
133
|
+
type WebViewRuntimeSource = string | number | {
|
|
134
|
+
uri: string;
|
|
135
|
+
};
|
|
136
|
+
type WebViewRuntimeConfig = Record<string, string>;
|
|
137
|
+
type MobileDocumentEngineOptions = {
|
|
138
|
+
/** Optional asset URI/require result for a runtime extension such as CBR. */
|
|
139
|
+
webViewRuntimeSource?: WebViewRuntimeSource;
|
|
140
|
+
webViewRuntimeConfig?: WebViewRuntimeConfig;
|
|
141
|
+
};
|
|
133
142
|
declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
143
|
+
private readonly webViewRuntimeSource?;
|
|
144
|
+
private readonly webViewRuntimeConfig?;
|
|
134
145
|
private bridge;
|
|
135
146
|
private ready;
|
|
136
147
|
private requestId;
|
|
@@ -142,9 +153,14 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
142
153
|
private zoom;
|
|
143
154
|
private rotation;
|
|
144
155
|
private outline;
|
|
156
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
157
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
158
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
145
159
|
getRenderTargetType(): "webview";
|
|
146
160
|
attachBridge(bridge: WebViewBridge): void;
|
|
147
161
|
handleMessage(raw: string): void;
|
|
162
|
+
private handleAssetRequest;
|
|
163
|
+
private handleFileChunkRequest;
|
|
148
164
|
load(input: DocumentLoadInput): Promise<void>;
|
|
149
165
|
getPageCount(): number;
|
|
150
166
|
getCurrentPage(): number;
|
|
@@ -160,6 +176,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
160
176
|
width: number;
|
|
161
177
|
height: number;
|
|
162
178
|
}>;
|
|
179
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
163
180
|
searchText(query: string): Promise<SearchResult[]>;
|
|
164
181
|
selectText(pageIndex: number, rect: {
|
|
165
182
|
x: number;
|
|
@@ -175,6 +192,7 @@ declare class WebViewDocumentEngine extends BaseDocumentEngine {
|
|
|
175
192
|
private request;
|
|
176
193
|
private normalizeRuntimeSource;
|
|
177
194
|
private fetchRemoteSource;
|
|
195
|
+
private fetchLocalSource;
|
|
178
196
|
private readResponseBuffer;
|
|
179
197
|
private isUriSource;
|
|
180
198
|
private isDataSource;
|
|
@@ -184,7 +202,9 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
184
202
|
private pdfEngine;
|
|
185
203
|
private webEngine;
|
|
186
204
|
private activeEngine;
|
|
187
|
-
constructor();
|
|
205
|
+
constructor(options?: MobileDocumentEngineOptions);
|
|
206
|
+
getWebViewRuntimeSource(): WebViewRuntimeSource | undefined;
|
|
207
|
+
getWebViewRuntimeConfig(): WebViewRuntimeConfig | undefined;
|
|
188
208
|
getRenderTargetType(): RenderTargetType;
|
|
189
209
|
getNativeEngineId(): string | null;
|
|
190
210
|
attachWebView(bridge: WebViewBridge): void;
|
|
@@ -204,6 +224,7 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
204
224
|
width: number;
|
|
205
225
|
height: number;
|
|
206
226
|
}>;
|
|
227
|
+
getPagePreview(pageIndex: number): Promise<string | null>;
|
|
207
228
|
searchText(query: string): Promise<SearchResult[]>;
|
|
208
229
|
selectText(pageIndex: number, rect: {
|
|
209
230
|
x: number;
|
|
@@ -216,4 +237,4 @@ declare class MobileDocumentEngine extends BaseDocumentEngine {
|
|
|
216
237
|
destroy(): void;
|
|
217
238
|
}
|
|
218
239
|
|
|
219
|
-
export { MobileDocumentEngine, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine };
|
|
240
|
+
export { MobileDocumentEngine, type MobileDocumentEngineOptions, NativeDocumentEngine, PapyrusPageView, type PapyrusPageViewProps, PapyrusPdfDocumentView, PapyrusPdfViewerView, type PapyrusPdfViewerViewProps, WebViewDocumentEngine, type WebViewRuntimeConfig, type WebViewRuntimeSource };
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,57 @@ var import_NativeViewManagerAdapter = require("expo-modules-core/src/NativeViewM
|
|
|
32
32
|
var import_requireNativeModule = require("expo-modules-core/src/requireNativeModule");
|
|
33
33
|
var import_core = require("@papyrus-sdk/core");
|
|
34
34
|
var import_types = require("@papyrus-sdk/types");
|
|
35
|
+
|
|
36
|
+
// documentType.ts
|
|
37
|
+
var parseDataUri = (value) => {
|
|
38
|
+
const match = /^data:([^;,]+)?(;base64)?,(.*)$/.exec(value);
|
|
39
|
+
if (!match) return null;
|
|
40
|
+
return {
|
|
41
|
+
mime: match[1] ?? "",
|
|
42
|
+
isBase64: Boolean(match[2]),
|
|
43
|
+
data: match[3] ?? ""
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
var typeFromExtension = (value) => {
|
|
47
|
+
const clean = value.split("?")[0].split("#")[0].toLowerCase();
|
|
48
|
+
const extension = clean.includes(".") ? clean.split(".").pop() : void 0;
|
|
49
|
+
if (extension === "epub") return "epub";
|
|
50
|
+
if (extension === "txt") return "text";
|
|
51
|
+
if (extension === "pdf") return "pdf";
|
|
52
|
+
if (extension === "cbz" || extension === "cbr") return "comic";
|
|
53
|
+
return null;
|
|
54
|
+
};
|
|
55
|
+
var inferComicFormat = (source) => {
|
|
56
|
+
const candidate = typeof source === "string" ? source : typeof source === "object" && source !== null && "uri" in source ? source.uri : "";
|
|
57
|
+
const dataUri = parseDataUri(candidate);
|
|
58
|
+
const mime = dataUri?.mime.toLowerCase() ?? "";
|
|
59
|
+
if (mime.includes("rar") || /\.cbr(?:$|[?#])/i.test(candidate)) {
|
|
60
|
+
return "cbr";
|
|
61
|
+
}
|
|
62
|
+
return "cbz";
|
|
63
|
+
};
|
|
64
|
+
var resolveComicFormat = (source, explicitFormat) => explicitFormat ?? inferComicFormat(source);
|
|
65
|
+
var inferDocumentType = (source) => {
|
|
66
|
+
if (typeof source === "string") {
|
|
67
|
+
const dataUri = parseDataUri(source);
|
|
68
|
+
if (dataUri?.mime) {
|
|
69
|
+
const mime = dataUri.mime.toLowerCase();
|
|
70
|
+
if (mime.includes("epub")) return "epub";
|
|
71
|
+
if (mime.includes("comicbook") || mime.includes("zip") || mime.includes("rar")) {
|
|
72
|
+
return "comic";
|
|
73
|
+
}
|
|
74
|
+
if (mime.includes("text")) return "text";
|
|
75
|
+
if (mime.includes("pdf")) return "pdf";
|
|
76
|
+
}
|
|
77
|
+
return typeFromExtension(source) ?? "pdf";
|
|
78
|
+
}
|
|
79
|
+
if (typeof source === "object" && source !== null && "uri" in source) {
|
|
80
|
+
return typeFromExtension(source.uri) ?? "pdf";
|
|
81
|
+
}
|
|
82
|
+
return "pdf";
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// index.ts
|
|
35
86
|
var MODULE_NAME = "PapyrusNativeEngine";
|
|
36
87
|
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
37
88
|
var BASE64_LOOKUP = (() => {
|
|
@@ -42,7 +93,7 @@ var BASE64_LOOKUP = (() => {
|
|
|
42
93
|
}
|
|
43
94
|
return table;
|
|
44
95
|
})();
|
|
45
|
-
var
|
|
96
|
+
var parseDataUri2 = (value) => {
|
|
46
97
|
const match = /^data:([^;,]+)?(;base64)?,(.*)$/.exec(value);
|
|
47
98
|
if (!match) return null;
|
|
48
99
|
return {
|
|
@@ -51,7 +102,7 @@ var parseDataUri = (value) => {
|
|
|
51
102
|
data: match[3] ?? ""
|
|
52
103
|
};
|
|
53
104
|
};
|
|
54
|
-
var looksLikeUri = (value) => value.startsWith("http://") || value.startsWith("https://") || value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("file://");
|
|
105
|
+
var looksLikeUri = (value) => value.startsWith("http://") || value.startsWith("https://") || value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("file://") || value.startsWith("content://");
|
|
55
106
|
var isLikelyBase64 = (value) => {
|
|
56
107
|
if (looksLikeUri(value)) return false;
|
|
57
108
|
if (value.includes(".")) return false;
|
|
@@ -59,6 +110,7 @@ var isLikelyBase64 = (value) => {
|
|
|
59
110
|
return /^[A-Za-z0-9+/=]+$/.test(value);
|
|
60
111
|
};
|
|
61
112
|
var isHttpUri = (value) => value.startsWith("http://") || value.startsWith("https://");
|
|
113
|
+
var isLocalUri = (value) => value.startsWith("content://") || value.startsWith("file://");
|
|
62
114
|
var decodeBase64 = (value) => {
|
|
63
115
|
const clean = value.replace(/[^A-Za-z0-9+/=]/g, "");
|
|
64
116
|
const padding = clean.endsWith("==") ? 2 : clean.endsWith("=") ? 1 : 0;
|
|
@@ -102,33 +154,7 @@ var encodeBase64 = (bytes) => {
|
|
|
102
154
|
return output;
|
|
103
155
|
};
|
|
104
156
|
var isLoadRequest = (input) => typeof input === "object" && input !== null && "source" in input && "type" in input;
|
|
105
|
-
var normalizeLoadInput = (input) => isLoadRequest(input) ? { source: input.source, type: input.type } : { source: input };
|
|
106
|
-
var inferDocumentType = (source) => {
|
|
107
|
-
if (typeof source === "string") {
|
|
108
|
-
const dataUri = parseDataUri(source);
|
|
109
|
-
if (dataUri?.mime) {
|
|
110
|
-
const mime = dataUri.mime.toLowerCase();
|
|
111
|
-
if (mime.includes("epub")) return "epub";
|
|
112
|
-
if (mime.includes("text")) return "text";
|
|
113
|
-
if (mime.includes("pdf")) return "pdf";
|
|
114
|
-
}
|
|
115
|
-
const clean = source.split("?")[0].split("#")[0];
|
|
116
|
-
const ext = clean.includes(".") ? clean.split(".").pop()?.toLowerCase() : void 0;
|
|
117
|
-
if (ext === "epub") return "epub";
|
|
118
|
-
if (ext === "txt") return "text";
|
|
119
|
-
if (ext === "pdf") return "pdf";
|
|
120
|
-
return "pdf";
|
|
121
|
-
}
|
|
122
|
-
if (typeof source === "object" && source !== null && "uri" in source) {
|
|
123
|
-
const uri = source.uri;
|
|
124
|
-
const clean = uri.split("?")[0].split("#")[0];
|
|
125
|
-
const ext = clean.includes(".") ? clean.split(".").pop()?.toLowerCase() : void 0;
|
|
126
|
-
if (ext === "epub") return "epub";
|
|
127
|
-
if (ext === "txt") return "text";
|
|
128
|
-
if (ext === "pdf") return "pdf";
|
|
129
|
-
}
|
|
130
|
-
return "pdf";
|
|
131
|
-
};
|
|
157
|
+
var normalizeLoadInput = (input) => isLoadRequest(input) ? { source: input.source, type: input.type, format: input.format } : { source: input };
|
|
132
158
|
var normalizeNativeDestination = (dest) => {
|
|
133
159
|
if (!dest) return null;
|
|
134
160
|
if (typeof dest === "string") {
|
|
@@ -333,7 +359,7 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
333
359
|
}
|
|
334
360
|
async normalizeSource(source) {
|
|
335
361
|
if (typeof source === "string") {
|
|
336
|
-
const dataUri =
|
|
362
|
+
const dataUri = parseDataUri2(source);
|
|
337
363
|
if (dataUri?.isBase64) {
|
|
338
364
|
return { data: decodeBase64(dataUri.data) };
|
|
339
365
|
}
|
|
@@ -371,8 +397,8 @@ var NativeDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
371
397
|
}
|
|
372
398
|
};
|
|
373
399
|
var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
374
|
-
constructor() {
|
|
375
|
-
super(
|
|
400
|
+
constructor(options = {}) {
|
|
401
|
+
super();
|
|
376
402
|
this.bridge = null;
|
|
377
403
|
this.ready = false;
|
|
378
404
|
this.requestId = 0;
|
|
@@ -384,6 +410,14 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
384
410
|
this.zoom = 1;
|
|
385
411
|
this.rotation = 0;
|
|
386
412
|
this.outline = [];
|
|
413
|
+
this.webViewRuntimeSource = options.webViewRuntimeSource;
|
|
414
|
+
this.webViewRuntimeConfig = options.webViewRuntimeConfig;
|
|
415
|
+
}
|
|
416
|
+
getWebViewRuntimeSource() {
|
|
417
|
+
return this.webViewRuntimeSource;
|
|
418
|
+
}
|
|
419
|
+
getWebViewRuntimeConfig() {
|
|
420
|
+
return this.webViewRuntimeConfig;
|
|
387
421
|
}
|
|
388
422
|
getRenderTargetType() {
|
|
389
423
|
return "webview";
|
|
@@ -401,6 +435,14 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
401
435
|
return;
|
|
402
436
|
}
|
|
403
437
|
if (!message) return;
|
|
438
|
+
if (message.type === "asset-request") {
|
|
439
|
+
void this.handleAssetRequest(message);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (message.type === "file-chunk-request") {
|
|
443
|
+
void this.handleFileChunkRequest(message);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
404
446
|
if (message.type === "ready") {
|
|
405
447
|
this.ready = true;
|
|
406
448
|
this.readyResolvers.forEach((resolve) => resolve());
|
|
@@ -466,8 +508,73 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
466
508
|
console.warn("[Papyrus WebView] Unknown message", message);
|
|
467
509
|
}
|
|
468
510
|
}
|
|
511
|
+
async handleAssetRequest(message) {
|
|
512
|
+
const bridge = this.bridge;
|
|
513
|
+
if (!bridge) return;
|
|
514
|
+
try {
|
|
515
|
+
const response = await fetch(message.url);
|
|
516
|
+
if (!response.ok) {
|
|
517
|
+
throw new Error(`status ${response.status}`);
|
|
518
|
+
}
|
|
519
|
+
const data = message.encoding === "text" ? await response.text() : `data:application/wasm;base64,${encodeBase64(
|
|
520
|
+
new Uint8Array(await response.arrayBuffer())
|
|
521
|
+
)}`;
|
|
522
|
+
bridge.postMessage(
|
|
523
|
+
JSON.stringify({
|
|
524
|
+
type: "asset-response",
|
|
525
|
+
id: message.id,
|
|
526
|
+
ok: true,
|
|
527
|
+
data
|
|
528
|
+
})
|
|
529
|
+
);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
532
|
+
bridge.postMessage(
|
|
533
|
+
JSON.stringify({
|
|
534
|
+
type: "asset-response",
|
|
535
|
+
id: message.id,
|
|
536
|
+
ok: false,
|
|
537
|
+
error: reason
|
|
538
|
+
})
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
async handleFileChunkRequest(message) {
|
|
543
|
+
const bridge = this.bridge;
|
|
544
|
+
if (!bridge) return;
|
|
545
|
+
try {
|
|
546
|
+
const nativeModule = resolveNativeModule();
|
|
547
|
+
if (!nativeModule?.readFileChunk) {
|
|
548
|
+
throw new Error("Leitura nativa de arquivos n\xE3o est\xE1 dispon\xEDvel");
|
|
549
|
+
}
|
|
550
|
+
const result = await nativeModule.readFileChunk(
|
|
551
|
+
message.uri,
|
|
552
|
+
message.offset,
|
|
553
|
+
message.length
|
|
554
|
+
);
|
|
555
|
+
bridge.postMessage(
|
|
556
|
+
JSON.stringify({
|
|
557
|
+
type: "file-chunk-response",
|
|
558
|
+
id: message.id,
|
|
559
|
+
ok: true,
|
|
560
|
+
data: result.data,
|
|
561
|
+
done: result.done
|
|
562
|
+
})
|
|
563
|
+
);
|
|
564
|
+
} catch (error) {
|
|
565
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
566
|
+
bridge.postMessage(
|
|
567
|
+
JSON.stringify({
|
|
568
|
+
type: "file-chunk-response",
|
|
569
|
+
id: message.id,
|
|
570
|
+
ok: false,
|
|
571
|
+
error: reason
|
|
572
|
+
})
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
469
576
|
async load(input) {
|
|
470
|
-
const { source, type } = normalizeLoadInput(input);
|
|
577
|
+
const { source, type, format } = normalizeLoadInput(input);
|
|
471
578
|
const resolvedType = type ?? inferDocumentType(source);
|
|
472
579
|
if (resolvedType === "pdf") {
|
|
473
580
|
throw new Error(
|
|
@@ -480,7 +587,8 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
480
587
|
);
|
|
481
588
|
const response = await this.request("load", {
|
|
482
589
|
type: resolvedType,
|
|
483
|
-
source: payloadSource
|
|
590
|
+
source: payloadSource,
|
|
591
|
+
...resolvedType === "comic" ? { format: resolveComicFormat(source, format) } : {}
|
|
484
592
|
});
|
|
485
593
|
if (typeof response?.pageCount === "number")
|
|
486
594
|
this.pageCount = response.pageCount;
|
|
@@ -530,6 +638,11 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
530
638
|
{ pageIndex }
|
|
531
639
|
);
|
|
532
640
|
}
|
|
641
|
+
async getPagePreview(pageIndex) {
|
|
642
|
+
return await this.request("get-page-preview", {
|
|
643
|
+
pageIndex
|
|
644
|
+
});
|
|
645
|
+
}
|
|
533
646
|
async searchText(query) {
|
|
534
647
|
return await this.request("search-text", { query });
|
|
535
648
|
}
|
|
@@ -582,7 +695,7 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
582
695
|
await this.ensureReady();
|
|
583
696
|
const id = `${Date.now()}-${this.requestId++}`;
|
|
584
697
|
return new Promise((resolve, reject) => {
|
|
585
|
-
const timeoutMs = kind === "load" ? 18e4 : 8e3;
|
|
698
|
+
const timeoutMs = kind === "load" ? 18e4 : kind === "get-page-preview" ? 3e4 : 8e3;
|
|
586
699
|
const timeoutId = setTimeout(() => {
|
|
587
700
|
this.pending.delete(id);
|
|
588
701
|
reject(new Error(`[Papyrus] WebView response timeout: ${kind}`));
|
|
@@ -602,7 +715,7 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
602
715
|
}
|
|
603
716
|
async normalizeRuntimeSource(type, source) {
|
|
604
717
|
if (typeof source === "string") {
|
|
605
|
-
const dataUri =
|
|
718
|
+
const dataUri = parseDataUri2(source);
|
|
606
719
|
if (dataUri) {
|
|
607
720
|
if (dataUri.isBase64) {
|
|
608
721
|
return {
|
|
@@ -619,6 +732,9 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
619
732
|
const fetched = await this.fetchRemoteSource(type, source);
|
|
620
733
|
if (fetched) return fetched;
|
|
621
734
|
}
|
|
735
|
+
if (isLocalUri(source)) {
|
|
736
|
+
return await this.fetchLocalSource(type, source);
|
|
737
|
+
}
|
|
622
738
|
return { kind: "uri", uri: source };
|
|
623
739
|
}
|
|
624
740
|
if (isLikelyBase64(source)) {
|
|
@@ -635,6 +751,9 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
635
751
|
const fetched = await this.fetchRemoteSource(type, uri);
|
|
636
752
|
if (fetched) return fetched;
|
|
637
753
|
}
|
|
754
|
+
if (isLocalUri(uri)) {
|
|
755
|
+
return await this.fetchLocalSource(type, uri);
|
|
756
|
+
}
|
|
638
757
|
return { kind: "uri", uri };
|
|
639
758
|
}
|
|
640
759
|
if (this.isDataSource(source)) {
|
|
@@ -666,11 +785,35 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
666
785
|
const buffer = await this.readResponseBuffer(response);
|
|
667
786
|
return { kind: "base64", data: encodeBase64(new Uint8Array(buffer)) };
|
|
668
787
|
}
|
|
788
|
+
if (type === "comic") {
|
|
789
|
+
const buffer = await this.readResponseBuffer(response);
|
|
790
|
+
return { kind: "base64", data: encodeBase64(new Uint8Array(buffer)) };
|
|
791
|
+
}
|
|
669
792
|
return null;
|
|
670
793
|
} catch {
|
|
671
794
|
return null;
|
|
672
795
|
}
|
|
673
796
|
}
|
|
797
|
+
async fetchLocalSource(type, uri) {
|
|
798
|
+
if (type === "epub" || type === "comic") {
|
|
799
|
+
return { kind: "uri", uri };
|
|
800
|
+
}
|
|
801
|
+
try {
|
|
802
|
+
const response = await fetch(uri);
|
|
803
|
+
if (!response.ok && response.status !== 0) {
|
|
804
|
+
throw new Error(`status ${response.status}`);
|
|
805
|
+
}
|
|
806
|
+
if (type === "text") {
|
|
807
|
+
return { kind: "text", text: await response.text() };
|
|
808
|
+
}
|
|
809
|
+
throw new Error(`tipo n\xE3o suportado: ${type}`);
|
|
810
|
+
} catch (error) {
|
|
811
|
+
const reason = error instanceof Error ? `: ${error.message}` : "";
|
|
812
|
+
throw new Error(
|
|
813
|
+
`[WebViewDocumentEngine] N\xE3o foi poss\xEDvel ler o arquivo local ${uri}${reason}`
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
674
817
|
async readResponseBuffer(response) {
|
|
675
818
|
try {
|
|
676
819
|
return await response.arrayBuffer();
|
|
@@ -695,12 +838,18 @@ var WebViewDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
695
838
|
}
|
|
696
839
|
};
|
|
697
840
|
var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
698
|
-
constructor() {
|
|
841
|
+
constructor(options = {}) {
|
|
699
842
|
super();
|
|
700
843
|
this.pdfEngine = new NativeDocumentEngine();
|
|
701
|
-
this.webEngine = new WebViewDocumentEngine();
|
|
844
|
+
this.webEngine = new WebViewDocumentEngine(options);
|
|
702
845
|
this.activeEngine = this.pdfEngine;
|
|
703
846
|
}
|
|
847
|
+
getWebViewRuntimeSource() {
|
|
848
|
+
return this.webEngine.getWebViewRuntimeSource();
|
|
849
|
+
}
|
|
850
|
+
getWebViewRuntimeConfig() {
|
|
851
|
+
return this.webEngine.getWebViewRuntimeConfig();
|
|
852
|
+
}
|
|
704
853
|
getRenderTargetType() {
|
|
705
854
|
return this.activeEngine.getRenderTargetType?.() ?? "canvas";
|
|
706
855
|
}
|
|
@@ -714,10 +863,14 @@ var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
714
863
|
this.webEngine.handleMessage(data);
|
|
715
864
|
}
|
|
716
865
|
async load(input) {
|
|
717
|
-
const { source, type } = normalizeLoadInput(input);
|
|
866
|
+
const { source, type, format } = normalizeLoadInput(input);
|
|
718
867
|
const resolvedType = type ?? inferDocumentType(source);
|
|
719
868
|
this.activeEngine = resolvedType === "pdf" ? this.pdfEngine : this.webEngine;
|
|
720
|
-
await this.activeEngine.load({
|
|
869
|
+
await this.activeEngine.load({
|
|
870
|
+
type: resolvedType,
|
|
871
|
+
source,
|
|
872
|
+
...resolvedType === "comic" && format ? { format } : {}
|
|
873
|
+
});
|
|
721
874
|
}
|
|
722
875
|
getPageCount() {
|
|
723
876
|
return this.activeEngine.getPageCount();
|
|
@@ -752,6 +905,10 @@ var MobileDocumentEngine = class extends import_core.BaseDocumentEngine {
|
|
|
752
905
|
async getPageDimensions(pageIndex) {
|
|
753
906
|
return await this.activeEngine.getPageDimensions(pageIndex);
|
|
754
907
|
}
|
|
908
|
+
async getPagePreview(pageIndex) {
|
|
909
|
+
const engine = this.activeEngine;
|
|
910
|
+
return await engine.getPagePreview?.(pageIndex) ?? null;
|
|
911
|
+
}
|
|
755
912
|
async searchText(query) {
|
|
756
913
|
if (typeof this.activeEngine.searchText === "function") {
|
|
757
914
|
return await this.activeEngine.searchText(query);
|