@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/dist/index.mjs
CHANGED
|
@@ -12,6 +12,57 @@ import { BaseDocumentEngine, papyrusEvents } from "@papyrus-sdk/core";
|
|
|
12
12
|
import {
|
|
13
13
|
PapyrusEventType
|
|
14
14
|
} from "@papyrus-sdk/types";
|
|
15
|
+
|
|
16
|
+
// documentType.ts
|
|
17
|
+
var parseDataUri = (value) => {
|
|
18
|
+
const match = /^data:([^;,]+)?(;base64)?,(.*)$/.exec(value);
|
|
19
|
+
if (!match) return null;
|
|
20
|
+
return {
|
|
21
|
+
mime: match[1] ?? "",
|
|
22
|
+
isBase64: Boolean(match[2]),
|
|
23
|
+
data: match[3] ?? ""
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
var typeFromExtension = (value) => {
|
|
27
|
+
const clean = value.split("?")[0].split("#")[0].toLowerCase();
|
|
28
|
+
const extension = clean.includes(".") ? clean.split(".").pop() : void 0;
|
|
29
|
+
if (extension === "epub") return "epub";
|
|
30
|
+
if (extension === "txt") return "text";
|
|
31
|
+
if (extension === "pdf") return "pdf";
|
|
32
|
+
if (extension === "cbz" || extension === "cbr") return "comic";
|
|
33
|
+
return null;
|
|
34
|
+
};
|
|
35
|
+
var inferComicFormat = (source) => {
|
|
36
|
+
const candidate = typeof source === "string" ? source : typeof source === "object" && source !== null && "uri" in source ? source.uri : "";
|
|
37
|
+
const dataUri = parseDataUri(candidate);
|
|
38
|
+
const mime = dataUri?.mime.toLowerCase() ?? "";
|
|
39
|
+
if (mime.includes("rar") || /\.cbr(?:$|[?#])/i.test(candidate)) {
|
|
40
|
+
return "cbr";
|
|
41
|
+
}
|
|
42
|
+
return "cbz";
|
|
43
|
+
};
|
|
44
|
+
var resolveComicFormat = (source, explicitFormat) => explicitFormat ?? inferComicFormat(source);
|
|
45
|
+
var inferDocumentType = (source) => {
|
|
46
|
+
if (typeof source === "string") {
|
|
47
|
+
const dataUri = parseDataUri(source);
|
|
48
|
+
if (dataUri?.mime) {
|
|
49
|
+
const mime = dataUri.mime.toLowerCase();
|
|
50
|
+
if (mime.includes("epub")) return "epub";
|
|
51
|
+
if (mime.includes("comicbook") || mime.includes("zip") || mime.includes("rar")) {
|
|
52
|
+
return "comic";
|
|
53
|
+
}
|
|
54
|
+
if (mime.includes("text")) return "text";
|
|
55
|
+
if (mime.includes("pdf")) return "pdf";
|
|
56
|
+
}
|
|
57
|
+
return typeFromExtension(source) ?? "pdf";
|
|
58
|
+
}
|
|
59
|
+
if (typeof source === "object" && source !== null && "uri" in source) {
|
|
60
|
+
return typeFromExtension(source.uri) ?? "pdf";
|
|
61
|
+
}
|
|
62
|
+
return "pdf";
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// index.ts
|
|
15
66
|
var MODULE_NAME = "PapyrusNativeEngine";
|
|
16
67
|
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
17
68
|
var BASE64_LOOKUP = (() => {
|
|
@@ -22,7 +73,7 @@ var BASE64_LOOKUP = (() => {
|
|
|
22
73
|
}
|
|
23
74
|
return table;
|
|
24
75
|
})();
|
|
25
|
-
var
|
|
76
|
+
var parseDataUri2 = (value) => {
|
|
26
77
|
const match = /^data:([^;,]+)?(;base64)?,(.*)$/.exec(value);
|
|
27
78
|
if (!match) return null;
|
|
28
79
|
return {
|
|
@@ -31,7 +82,7 @@ var parseDataUri = (value) => {
|
|
|
31
82
|
data: match[3] ?? ""
|
|
32
83
|
};
|
|
33
84
|
};
|
|
34
|
-
var looksLikeUri = (value) => value.startsWith("http://") || value.startsWith("https://") || value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("file://");
|
|
85
|
+
var looksLikeUri = (value) => value.startsWith("http://") || value.startsWith("https://") || value.startsWith("/") || value.startsWith("./") || value.startsWith("../") || value.startsWith("file://") || value.startsWith("content://");
|
|
35
86
|
var isLikelyBase64 = (value) => {
|
|
36
87
|
if (looksLikeUri(value)) return false;
|
|
37
88
|
if (value.includes(".")) return false;
|
|
@@ -39,6 +90,7 @@ var isLikelyBase64 = (value) => {
|
|
|
39
90
|
return /^[A-Za-z0-9+/=]+$/.test(value);
|
|
40
91
|
};
|
|
41
92
|
var isHttpUri = (value) => value.startsWith("http://") || value.startsWith("https://");
|
|
93
|
+
var isLocalUri = (value) => value.startsWith("content://") || value.startsWith("file://");
|
|
42
94
|
var decodeBase64 = (value) => {
|
|
43
95
|
const clean = value.replace(/[^A-Za-z0-9+/=]/g, "");
|
|
44
96
|
const padding = clean.endsWith("==") ? 2 : clean.endsWith("=") ? 1 : 0;
|
|
@@ -82,33 +134,7 @@ var encodeBase64 = (bytes) => {
|
|
|
82
134
|
return output;
|
|
83
135
|
};
|
|
84
136
|
var isLoadRequest = (input) => typeof input === "object" && input !== null && "source" in input && "type" in input;
|
|
85
|
-
var normalizeLoadInput = (input) => isLoadRequest(input) ? { source: input.source, type: input.type } : { source: input };
|
|
86
|
-
var inferDocumentType = (source) => {
|
|
87
|
-
if (typeof source === "string") {
|
|
88
|
-
const dataUri = parseDataUri(source);
|
|
89
|
-
if (dataUri?.mime) {
|
|
90
|
-
const mime = dataUri.mime.toLowerCase();
|
|
91
|
-
if (mime.includes("epub")) return "epub";
|
|
92
|
-
if (mime.includes("text")) return "text";
|
|
93
|
-
if (mime.includes("pdf")) return "pdf";
|
|
94
|
-
}
|
|
95
|
-
const clean = source.split("?")[0].split("#")[0];
|
|
96
|
-
const ext = clean.includes(".") ? clean.split(".").pop()?.toLowerCase() : void 0;
|
|
97
|
-
if (ext === "epub") return "epub";
|
|
98
|
-
if (ext === "txt") return "text";
|
|
99
|
-
if (ext === "pdf") return "pdf";
|
|
100
|
-
return "pdf";
|
|
101
|
-
}
|
|
102
|
-
if (typeof source === "object" && source !== null && "uri" in source) {
|
|
103
|
-
const uri = source.uri;
|
|
104
|
-
const clean = uri.split("?")[0].split("#")[0];
|
|
105
|
-
const ext = clean.includes(".") ? clean.split(".").pop()?.toLowerCase() : void 0;
|
|
106
|
-
if (ext === "epub") return "epub";
|
|
107
|
-
if (ext === "txt") return "text";
|
|
108
|
-
if (ext === "pdf") return "pdf";
|
|
109
|
-
}
|
|
110
|
-
return "pdf";
|
|
111
|
-
};
|
|
137
|
+
var normalizeLoadInput = (input) => isLoadRequest(input) ? { source: input.source, type: input.type, format: input.format } : { source: input };
|
|
112
138
|
var normalizeNativeDestination = (dest) => {
|
|
113
139
|
if (!dest) return null;
|
|
114
140
|
if (typeof dest === "string") {
|
|
@@ -313,7 +339,7 @@ var NativeDocumentEngine = class extends BaseDocumentEngine {
|
|
|
313
339
|
}
|
|
314
340
|
async normalizeSource(source) {
|
|
315
341
|
if (typeof source === "string") {
|
|
316
|
-
const dataUri =
|
|
342
|
+
const dataUri = parseDataUri2(source);
|
|
317
343
|
if (dataUri?.isBase64) {
|
|
318
344
|
return { data: decodeBase64(dataUri.data) };
|
|
319
345
|
}
|
|
@@ -351,8 +377,8 @@ var NativeDocumentEngine = class extends BaseDocumentEngine {
|
|
|
351
377
|
}
|
|
352
378
|
};
|
|
353
379
|
var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
354
|
-
constructor() {
|
|
355
|
-
super(
|
|
380
|
+
constructor(options = {}) {
|
|
381
|
+
super();
|
|
356
382
|
this.bridge = null;
|
|
357
383
|
this.ready = false;
|
|
358
384
|
this.requestId = 0;
|
|
@@ -364,6 +390,14 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
364
390
|
this.zoom = 1;
|
|
365
391
|
this.rotation = 0;
|
|
366
392
|
this.outline = [];
|
|
393
|
+
this.webViewRuntimeSource = options.webViewRuntimeSource;
|
|
394
|
+
this.webViewRuntimeConfig = options.webViewRuntimeConfig;
|
|
395
|
+
}
|
|
396
|
+
getWebViewRuntimeSource() {
|
|
397
|
+
return this.webViewRuntimeSource;
|
|
398
|
+
}
|
|
399
|
+
getWebViewRuntimeConfig() {
|
|
400
|
+
return this.webViewRuntimeConfig;
|
|
367
401
|
}
|
|
368
402
|
getRenderTargetType() {
|
|
369
403
|
return "webview";
|
|
@@ -381,6 +415,14 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
381
415
|
return;
|
|
382
416
|
}
|
|
383
417
|
if (!message) return;
|
|
418
|
+
if (message.type === "asset-request") {
|
|
419
|
+
void this.handleAssetRequest(message);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (message.type === "file-chunk-request") {
|
|
423
|
+
void this.handleFileChunkRequest(message);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
384
426
|
if (message.type === "ready") {
|
|
385
427
|
this.ready = true;
|
|
386
428
|
this.readyResolvers.forEach((resolve) => resolve());
|
|
@@ -446,8 +488,73 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
446
488
|
console.warn("[Papyrus WebView] Unknown message", message);
|
|
447
489
|
}
|
|
448
490
|
}
|
|
491
|
+
async handleAssetRequest(message) {
|
|
492
|
+
const bridge = this.bridge;
|
|
493
|
+
if (!bridge) return;
|
|
494
|
+
try {
|
|
495
|
+
const response = await fetch(message.url);
|
|
496
|
+
if (!response.ok) {
|
|
497
|
+
throw new Error(`status ${response.status}`);
|
|
498
|
+
}
|
|
499
|
+
const data = message.encoding === "text" ? await response.text() : `data:application/wasm;base64,${encodeBase64(
|
|
500
|
+
new Uint8Array(await response.arrayBuffer())
|
|
501
|
+
)}`;
|
|
502
|
+
bridge.postMessage(
|
|
503
|
+
JSON.stringify({
|
|
504
|
+
type: "asset-response",
|
|
505
|
+
id: message.id,
|
|
506
|
+
ok: true,
|
|
507
|
+
data
|
|
508
|
+
})
|
|
509
|
+
);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
512
|
+
bridge.postMessage(
|
|
513
|
+
JSON.stringify({
|
|
514
|
+
type: "asset-response",
|
|
515
|
+
id: message.id,
|
|
516
|
+
ok: false,
|
|
517
|
+
error: reason
|
|
518
|
+
})
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
async handleFileChunkRequest(message) {
|
|
523
|
+
const bridge = this.bridge;
|
|
524
|
+
if (!bridge) return;
|
|
525
|
+
try {
|
|
526
|
+
const nativeModule = resolveNativeModule();
|
|
527
|
+
if (!nativeModule?.readFileChunk) {
|
|
528
|
+
throw new Error("Leitura nativa de arquivos n\xE3o est\xE1 dispon\xEDvel");
|
|
529
|
+
}
|
|
530
|
+
const result = await nativeModule.readFileChunk(
|
|
531
|
+
message.uri,
|
|
532
|
+
message.offset,
|
|
533
|
+
message.length
|
|
534
|
+
);
|
|
535
|
+
bridge.postMessage(
|
|
536
|
+
JSON.stringify({
|
|
537
|
+
type: "file-chunk-response",
|
|
538
|
+
id: message.id,
|
|
539
|
+
ok: true,
|
|
540
|
+
data: result.data,
|
|
541
|
+
done: result.done
|
|
542
|
+
})
|
|
543
|
+
);
|
|
544
|
+
} catch (error) {
|
|
545
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
546
|
+
bridge.postMessage(
|
|
547
|
+
JSON.stringify({
|
|
548
|
+
type: "file-chunk-response",
|
|
549
|
+
id: message.id,
|
|
550
|
+
ok: false,
|
|
551
|
+
error: reason
|
|
552
|
+
})
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
449
556
|
async load(input) {
|
|
450
|
-
const { source, type } = normalizeLoadInput(input);
|
|
557
|
+
const { source, type, format } = normalizeLoadInput(input);
|
|
451
558
|
const resolvedType = type ?? inferDocumentType(source);
|
|
452
559
|
if (resolvedType === "pdf") {
|
|
453
560
|
throw new Error(
|
|
@@ -460,7 +567,8 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
460
567
|
);
|
|
461
568
|
const response = await this.request("load", {
|
|
462
569
|
type: resolvedType,
|
|
463
|
-
source: payloadSource
|
|
570
|
+
source: payloadSource,
|
|
571
|
+
...resolvedType === "comic" ? { format: resolveComicFormat(source, format) } : {}
|
|
464
572
|
});
|
|
465
573
|
if (typeof response?.pageCount === "number")
|
|
466
574
|
this.pageCount = response.pageCount;
|
|
@@ -510,6 +618,11 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
510
618
|
{ pageIndex }
|
|
511
619
|
);
|
|
512
620
|
}
|
|
621
|
+
async getPagePreview(pageIndex) {
|
|
622
|
+
return await this.request("get-page-preview", {
|
|
623
|
+
pageIndex
|
|
624
|
+
});
|
|
625
|
+
}
|
|
513
626
|
async searchText(query) {
|
|
514
627
|
return await this.request("search-text", { query });
|
|
515
628
|
}
|
|
@@ -562,7 +675,7 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
562
675
|
await this.ensureReady();
|
|
563
676
|
const id = `${Date.now()}-${this.requestId++}`;
|
|
564
677
|
return new Promise((resolve, reject) => {
|
|
565
|
-
const timeoutMs = kind === "load" ? 18e4 : 8e3;
|
|
678
|
+
const timeoutMs = kind === "load" ? 18e4 : kind === "get-page-preview" ? 3e4 : 8e3;
|
|
566
679
|
const timeoutId = setTimeout(() => {
|
|
567
680
|
this.pending.delete(id);
|
|
568
681
|
reject(new Error(`[Papyrus] WebView response timeout: ${kind}`));
|
|
@@ -582,7 +695,7 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
582
695
|
}
|
|
583
696
|
async normalizeRuntimeSource(type, source) {
|
|
584
697
|
if (typeof source === "string") {
|
|
585
|
-
const dataUri =
|
|
698
|
+
const dataUri = parseDataUri2(source);
|
|
586
699
|
if (dataUri) {
|
|
587
700
|
if (dataUri.isBase64) {
|
|
588
701
|
return {
|
|
@@ -599,6 +712,9 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
599
712
|
const fetched = await this.fetchRemoteSource(type, source);
|
|
600
713
|
if (fetched) return fetched;
|
|
601
714
|
}
|
|
715
|
+
if (isLocalUri(source)) {
|
|
716
|
+
return await this.fetchLocalSource(type, source);
|
|
717
|
+
}
|
|
602
718
|
return { kind: "uri", uri: source };
|
|
603
719
|
}
|
|
604
720
|
if (isLikelyBase64(source)) {
|
|
@@ -615,6 +731,9 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
615
731
|
const fetched = await this.fetchRemoteSource(type, uri);
|
|
616
732
|
if (fetched) return fetched;
|
|
617
733
|
}
|
|
734
|
+
if (isLocalUri(uri)) {
|
|
735
|
+
return await this.fetchLocalSource(type, uri);
|
|
736
|
+
}
|
|
618
737
|
return { kind: "uri", uri };
|
|
619
738
|
}
|
|
620
739
|
if (this.isDataSource(source)) {
|
|
@@ -646,11 +765,35 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
646
765
|
const buffer = await this.readResponseBuffer(response);
|
|
647
766
|
return { kind: "base64", data: encodeBase64(new Uint8Array(buffer)) };
|
|
648
767
|
}
|
|
768
|
+
if (type === "comic") {
|
|
769
|
+
const buffer = await this.readResponseBuffer(response);
|
|
770
|
+
return { kind: "base64", data: encodeBase64(new Uint8Array(buffer)) };
|
|
771
|
+
}
|
|
649
772
|
return null;
|
|
650
773
|
} catch {
|
|
651
774
|
return null;
|
|
652
775
|
}
|
|
653
776
|
}
|
|
777
|
+
async fetchLocalSource(type, uri) {
|
|
778
|
+
if (type === "epub" || type === "comic") {
|
|
779
|
+
return { kind: "uri", uri };
|
|
780
|
+
}
|
|
781
|
+
try {
|
|
782
|
+
const response = await fetch(uri);
|
|
783
|
+
if (!response.ok && response.status !== 0) {
|
|
784
|
+
throw new Error(`status ${response.status}`);
|
|
785
|
+
}
|
|
786
|
+
if (type === "text") {
|
|
787
|
+
return { kind: "text", text: await response.text() };
|
|
788
|
+
}
|
|
789
|
+
throw new Error(`tipo n\xE3o suportado: ${type}`);
|
|
790
|
+
} catch (error) {
|
|
791
|
+
const reason = error instanceof Error ? `: ${error.message}` : "";
|
|
792
|
+
throw new Error(
|
|
793
|
+
`[WebViewDocumentEngine] N\xE3o foi poss\xEDvel ler o arquivo local ${uri}${reason}`
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
654
797
|
async readResponseBuffer(response) {
|
|
655
798
|
try {
|
|
656
799
|
return await response.arrayBuffer();
|
|
@@ -675,12 +818,18 @@ var WebViewDocumentEngine = class extends BaseDocumentEngine {
|
|
|
675
818
|
}
|
|
676
819
|
};
|
|
677
820
|
var MobileDocumentEngine = class extends BaseDocumentEngine {
|
|
678
|
-
constructor() {
|
|
821
|
+
constructor(options = {}) {
|
|
679
822
|
super();
|
|
680
823
|
this.pdfEngine = new NativeDocumentEngine();
|
|
681
|
-
this.webEngine = new WebViewDocumentEngine();
|
|
824
|
+
this.webEngine = new WebViewDocumentEngine(options);
|
|
682
825
|
this.activeEngine = this.pdfEngine;
|
|
683
826
|
}
|
|
827
|
+
getWebViewRuntimeSource() {
|
|
828
|
+
return this.webEngine.getWebViewRuntimeSource();
|
|
829
|
+
}
|
|
830
|
+
getWebViewRuntimeConfig() {
|
|
831
|
+
return this.webEngine.getWebViewRuntimeConfig();
|
|
832
|
+
}
|
|
684
833
|
getRenderTargetType() {
|
|
685
834
|
return this.activeEngine.getRenderTargetType?.() ?? "canvas";
|
|
686
835
|
}
|
|
@@ -694,10 +843,14 @@ var MobileDocumentEngine = class extends BaseDocumentEngine {
|
|
|
694
843
|
this.webEngine.handleMessage(data);
|
|
695
844
|
}
|
|
696
845
|
async load(input) {
|
|
697
|
-
const { source, type } = normalizeLoadInput(input);
|
|
846
|
+
const { source, type, format } = normalizeLoadInput(input);
|
|
698
847
|
const resolvedType = type ?? inferDocumentType(source);
|
|
699
848
|
this.activeEngine = resolvedType === "pdf" ? this.pdfEngine : this.webEngine;
|
|
700
|
-
await this.activeEngine.load({
|
|
849
|
+
await this.activeEngine.load({
|
|
850
|
+
type: resolvedType,
|
|
851
|
+
source,
|
|
852
|
+
...resolvedType === "comic" && format ? { format } : {}
|
|
853
|
+
});
|
|
701
854
|
}
|
|
702
855
|
getPageCount() {
|
|
703
856
|
return this.activeEngine.getPageCount();
|
|
@@ -732,6 +885,10 @@ var MobileDocumentEngine = class extends BaseDocumentEngine {
|
|
|
732
885
|
async getPageDimensions(pageIndex) {
|
|
733
886
|
return await this.activeEngine.getPageDimensions(pageIndex);
|
|
734
887
|
}
|
|
888
|
+
async getPagePreview(pageIndex) {
|
|
889
|
+
const engine = this.activeEngine;
|
|
890
|
+
return await engine.getPagePreview?.(pageIndex) ?? null;
|
|
891
|
+
}
|
|
735
892
|
async searchText(query) {
|
|
736
893
|
if (typeof this.activeEngine.searchText === "function") {
|
|
737
894
|
return await this.activeEngine.searchText(query);
|