@deepseek-ai/libreoffice-kit 0.0.1 → 0.0.2-rc5
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/NOTICE +240 -0
- package/README.md +43 -47
- package/README.zh.md +35 -50
- package/assets/font-subset.json +70 -0
- package/assets/font-subset.wasm +0 -0
- package/lib/browser/assets.json +10 -0
- package/lib/browser/index.js +442 -0
- package/lib/browser/types/connection.d.ts +20 -0
- package/lib/browser/types/engine-rendering.d.ts +3 -0
- package/lib/browser/types/engine-types.d.ts +17 -0
- package/lib/browser/types/font-channel.d.ts +20 -0
- package/lib/browser/types/index.d.ts +13 -0
- package/lib/browser/types/office-engine.d.ts +46 -0
- package/lib/browser/types/office-types.d.ts +107 -0
- package/lib/browser/types/office.d.ts +4 -0
- package/lib/browser/types/protocol.d.ts +109 -0
- package/lib/browser/types/rendering.d.ts +4 -0
- package/lib/browser/types/types.d.ts +92 -0
- package/lib/browser/types/worker.d.ts +2 -0
- package/lib/browser/worker.js +3498 -0
- package/lib/browser-assets.js +119 -0
- package/lib/cli.js +1336 -0
- package/lib/document.js +178 -0
- package/lib/engine-rendering.js +120 -0
- package/lib/font-config.js +46 -0
- package/lib/font-source.js +377 -0
- package/lib/font-worker.js +1383 -0
- package/lib/index.js +408 -165
- package/lib/rendering.js +99 -0
- package/lib/sheet-geometry.js +111 -0
- package/lib/types/browser-assets.d.ts +15 -0
- package/lib/types/cli.d.ts +3 -0
- package/lib/types/default-fonts.d.ts +3 -0
- package/lib/types/document.d.ts +1 -1
- package/lib/types/engine-rendering.d.ts +15 -0
- package/lib/types/engine.d.ts +5 -5
- package/lib/types/font-config.d.ts +24 -0
- package/lib/types/font-full.d.ts +13 -0
- package/lib/types/font-loader.d.ts +1 -7
- package/lib/types/font-preferences-data.d.ts +16 -0
- package/lib/types/font-preferences.d.ts +29 -0
- package/lib/types/font-request.d.ts +12 -0
- package/lib/types/font-source-types.d.ts +84 -0
- package/lib/types/font-source.d.ts +10 -0
- package/lib/types/font-subsets.d.ts +35 -0
- package/lib/types/font-worker.d.ts +2 -0
- package/lib/types/fonts.d.ts +20 -37
- package/lib/types/harfbuzz-subset.d.ts +11 -0
- package/lib/types/image-operations.d.ts +58 -0
- package/lib/types/image-renderer.d.ts +6 -0
- package/lib/types/index.d.ts +25 -2
- package/lib/types/native.d.ts +4 -2
- package/lib/types/operations.d.ts +41 -0
- package/lib/types/options.d.ts +1 -1
- package/lib/types/png.d.ts +2 -0
- package/lib/types/profile.d.ts +8 -0
- package/lib/types/rendering.d.ts +41 -0
- package/lib/types/runtime.d.ts +14 -0
- package/lib/types/sheet-geometry.d.ts +38 -0
- package/lib/types/unicode-scripts.d.ts +18 -0
- package/lib/types/wasm.d.ts +76 -1
- package/lib/types/worker.d.ts +3 -0
- package/lib/worker.js +1716 -271
- package/licenses/font-subset/harfbuzz-COPYING +42 -0
- package/licenses/font-subset/harfbuzzjs-LICENSE +19 -0
- package/licenses/javascript/fflate-LICENSE +21 -0
- package/licenses/javascript/saxes-LICENSE +64 -0
- package/licenses/javascript/xmlchars-LICENSE +18 -0
- package/package.json +59 -12
- package/sources/browser/packages/browser/package.json +22 -0
- package/sources/browser/packages/browser/src/connection.ts +166 -0
- package/sources/browser/packages/browser/src/engine-rendering.ts +2 -0
- package/sources/browser/packages/browser/src/engine-types.ts +14 -0
- package/sources/browser/packages/browser/src/font-channel.ts +99 -0
- package/sources/browser/packages/browser/src/index.ts +39 -0
- package/sources/browser/packages/browser/src/office-engine.ts +299 -0
- package/sources/browser/packages/browser/src/office-types.ts +77 -0
- package/sources/browser/packages/browser/src/office.ts +62 -0
- package/sources/browser/packages/browser/src/protocol.ts +39 -0
- package/sources/browser/packages/browser/src/rendering.ts +3 -0
- package/sources/browser/packages/browser/src/types.ts +94 -0
- package/sources/browser/packages/browser/src/worker.ts +166 -0
- package/sources/browser/packages/browser/tsconfig.json +5 -0
- package/sources/browser/packages/browser/tsdown.config.ts +7 -0
- package/sources/browser/scripts/build-browser.mjs +82 -0
- package/sources/font-subset/engine/font-subset/README.md +23 -0
- package/sources/font-subset/engine/font-subset/README.zh.md +23 -0
- package/sources/font-subset/engine/font-subset/source.json +16 -0
- package/sources/font-subset/harfbuzzjs/config-override-subset.h +7 -0
- package/sources/font-subset/harfbuzzjs/harfbuzz-subset.symbols +34 -0
- package/sources/font-subset/scripts/build-font-subset.mjs +64 -0
- package/sources/font-subset/scripts/pack-utils.mjs +69 -0
- package/sources/font-subset/scripts/platform-matrix.mjs +100 -0
- package/sources/font-subset/scripts/verify-artifacts.mjs +260 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/** Validate staged or installed engine packages without compiling or downloading. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { closeSync, lstatSync, openSync, readdirSync, readFileSync, readSync, statSync } from 'node:fs';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
import { enginePrefix, isMain, nodeRange, readJson, targets } from './platform-matrix.mjs';
|
|
6
|
+
|
|
7
|
+
const hashPattern = /^[a-f0-9]{64}$/;
|
|
8
|
+
const payloadRoots = ['assets', 'bin', 'program', 'sources', 'licenses'];
|
|
9
|
+
|
|
10
|
+
export function assert(condition, message) {
|
|
11
|
+
if (!condition) throw new Error(message);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function sha256(file) {
|
|
15
|
+
const hash = createHash('sha256');
|
|
16
|
+
const fd = openSync(file, 'r');
|
|
17
|
+
const chunk = Buffer.alloc(1024 * 1024);
|
|
18
|
+
try {
|
|
19
|
+
for (let count; (count = readSync(fd, chunk)) !== 0;) hash.update(chunk.subarray(0, count));
|
|
20
|
+
} finally { closeSync(fd); }
|
|
21
|
+
return hash.digest('hex');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Manifest paths are POSIX relative paths on every host, including Windows. */
|
|
25
|
+
export function safePath(value) {
|
|
26
|
+
assert(typeof value === 'string' && value.length > 0 && !/[\\:\x00-\x1f]/.test(value)
|
|
27
|
+
&& !value.split('/').some((part) => part === '' || part === '.' || part === '..'), `Unsafe package path: ${value}`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function regularFile(dir, relative) {
|
|
32
|
+
safePath(relative);
|
|
33
|
+
const parts = relative.split('/');
|
|
34
|
+
for (let index = 1; index <= parts.length; index++) {
|
|
35
|
+
const file = join(dir, ...parts.slice(0, index));
|
|
36
|
+
let stat;
|
|
37
|
+
try { stat = lstatSync(file); }
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error.code !== 'ENOENT') throw error;
|
|
40
|
+
throw new Error(`Missing package artifact: ${relative}`, { cause: error });
|
|
41
|
+
}
|
|
42
|
+
assert(index === parts.length ? stat.isFile() : stat.isDirectory(), `Not a regular package file/directory: ${relative}`);
|
|
43
|
+
}
|
|
44
|
+
return join(dir, ...parts);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function inventory(dir, prefix) {
|
|
48
|
+
let entries;
|
|
49
|
+
try { entries = readdirSync(join(dir, prefix), { withFileTypes: true }); }
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error.code === 'ENOENT') return [];
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
assert(lstatSync(join(dir, prefix)).isDirectory(), `Not a regular payload directory: ${prefix}`);
|
|
55
|
+
return entries.flatMap((entry) => {
|
|
56
|
+
const name = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
57
|
+
assert(entry.isFile() || entry.isDirectory(), `Symlink or special payload file: ${name}`);
|
|
58
|
+
return entry.isDirectory() ? inventory(dir, name) : [name];
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function verifyNoInstallHooks(manifest, allowWorkspace = false) {
|
|
63
|
+
for (const hook of ['preinstall', 'install', 'postinstall', 'prepare']) {
|
|
64
|
+
assert(!Object.hasOwn(manifest.scripts ?? {}, hook), `${manifest.name}: forbidden install lifecycle ${hook}`);
|
|
65
|
+
}
|
|
66
|
+
assert(manifest.gypfile !== true && manifest.bin === undefined, `${manifest.name}: engine packages must not register install builds or CLI bins`);
|
|
67
|
+
for (const field of ['dependencies', 'optionalDependencies', 'peerDependencies']) {
|
|
68
|
+
for (const [name, version] of Object.entries(manifest[field] ?? {})) {
|
|
69
|
+
assert(/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(version) || (allowWorkspace && name.startsWith(`${enginePrefix}-`) && version === 'workspace:*'), `${manifest.name}: ${field} must pin a published version: ${name}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Validate metadata independently of whether the target has been built. */
|
|
75
|
+
export function verifyEngineMetadata(manifest, prebuild) {
|
|
76
|
+
verifyNoInstallHooks(manifest);
|
|
77
|
+
assert(manifest.type === 'module' && manifest.engines?.node === nodeRange, 'Engine package requires ESM and the declared Node baseline');
|
|
78
|
+
assert(manifest.exports?.['./prebuilds.json'] === './prebuilds.json'
|
|
79
|
+
&& manifest.exports?.['./package.json'] === './package.json', 'Engine package must export prebuilds.json and package.json');
|
|
80
|
+
assert(prebuild.schemaVersion === 1 && ['unbuilt', 'built'].includes(prebuild.status), 'Unsupported engine schema/status');
|
|
81
|
+
assert(prebuild.version === manifest.version && manifest.name === `${enginePrefix}-${prebuild.platform}`, 'Engine package identity/version mismatch');
|
|
82
|
+
const target = targets[prebuild.platform];
|
|
83
|
+
const engine = prebuild.engine;
|
|
84
|
+
if (engine?.glibcMinimum !== undefined) {
|
|
85
|
+
assert(target?.libc === 'glibc' && typeof engine.glibcMinimum === 'string'
|
|
86
|
+
&& /^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?$/.test(engine.glibcMinimum)
|
|
87
|
+
&& engine.glibcMinimum.split('.').every(part => Number.isSafeInteger(Number(part))), 'Invalid native glibcMinimum');
|
|
88
|
+
}
|
|
89
|
+
const exact = (actual, expected) => JSON.stringify(actual) === JSON.stringify(expected);
|
|
90
|
+
if (prebuild.platform === 'wasm') {
|
|
91
|
+
assert(JSON.stringify(manifest.os) === '["linux"]' && manifest.cpu === undefined && manifest.libc === undefined, 'WASM package must install only on Linux, for every CPU and libc');
|
|
92
|
+
assert(engine?.kind === 'wasm' && engine.loader === 'assets/soffice.cjs' && engine.wasm === 'assets/soffice.wasm'
|
|
93
|
+
&& engine.data === 'assets/soffice.data' && engine.metadata === 'assets/soffice.data.js.metadata'
|
|
94
|
+
&& engine.programDirectory === '/instdir/program', 'Unsupported WASM engine paths');
|
|
95
|
+
} else {
|
|
96
|
+
assert(target && exact(manifest.os, [target.os]) && exact(manifest.cpu, [target.cpu])
|
|
97
|
+
&& (target.libc ? exact(manifest.libc, [target.libc]) : manifest.libc === undefined), 'Native platform disagrees with package os/cpu/libc');
|
|
98
|
+
assert(engine?.kind === 'native' && engine.executable === `bin/libreoffice-kit${target.os === 'win32' ? '.exe' : ''}`
|
|
99
|
+
&& typeof engine.programDirectory === 'string' && (engine.programDirectory === 'program' || engine.programDirectory.startsWith('program/')), 'Unsupported native engine paths');
|
|
100
|
+
safePath(engine.programDirectory);
|
|
101
|
+
}
|
|
102
|
+
assert(prebuild.files && typeof prebuild.files === 'object' && !Array.isArray(prebuild.files), 'Engine files must be a hash inventory');
|
|
103
|
+
for (const [file, hash] of Object.entries(prebuild.files)) {
|
|
104
|
+
safePath(file);
|
|
105
|
+
assert(payloadRoots.includes(file.split('/')[0]) && hashPattern.test(hash), `Invalid file inventory entry: ${file}`);
|
|
106
|
+
}
|
|
107
|
+
assert(Array.isArray(prebuild.licenses), 'Engine licenses must be an array');
|
|
108
|
+
if (prebuild.status === 'unbuilt') {
|
|
109
|
+
assert(Object.keys(prebuild.files).length === 0 && prebuild.source === null && prebuild.licenses.length === 0,
|
|
110
|
+
'Unbuilt target must not claim built files or source/license receipts');
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const source = prebuild.source;
|
|
114
|
+
assert(source && /^https:\/\//.test(source.repository) && /^[a-f0-9]{40}$/.test(source.revision)
|
|
115
|
+
&& typeof source.version === 'string' && source.version.length > 0 && Array.isArray(source.files) && source.files.length > 0,
|
|
116
|
+
'Built engine requires source repository, full revision, version, and packaged build inputs');
|
|
117
|
+
assert(new Set(source.files).size === source.files.length, 'Duplicate source manifest paths');
|
|
118
|
+
for (const file of source.files) assert(file.startsWith('sources/') && Object.hasOwn(prebuild.files, file), `Missing hashed source: ${file}`);
|
|
119
|
+
assert(prebuild.licenses.some((license) => license.component === 'LibreOffice' && license.spdx === 'MPL-2.0'), 'Missing LibreOffice MPL-2.0 license');
|
|
120
|
+
for (const license of prebuild.licenses) {
|
|
121
|
+
assert(typeof license.component === 'string' && license.component.length > 0 && typeof license.spdx === 'string' && license.spdx.length > 0
|
|
122
|
+
&& typeof license.path === 'string' && license.path.startsWith('licenses/') && Object.hasOwn(prebuild.files, license.path), 'Missing hashed redistribution license');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Header-only checks accept minimal test fixtures; they are not release validation. */
|
|
127
|
+
export function verifyNativeHeader(bytes, platform, shared = false) {
|
|
128
|
+
const target = targets[platform];
|
|
129
|
+
assert(target, `Unknown native target: ${platform}`);
|
|
130
|
+
if (target.os === 'linux') {
|
|
131
|
+
assert(bytes.length >= 64 && bytes.readUInt32LE(0) === 0x464c457f && bytes[4] === 2 && bytes[5] === 1, 'Expected little-endian ELF64 image');
|
|
132
|
+
assert(bytes.readUInt16LE(18) === (target.cpu === 'x64' ? 62 : 183), 'Wrong ELF architecture');
|
|
133
|
+
assert((shared ? [3] : [2, 3]).includes(bytes.readUInt16LE(16)), 'Wrong ELF executable/library type');
|
|
134
|
+
} else if (target.os === 'darwin') {
|
|
135
|
+
assert(bytes.length >= 32 && bytes.readUInt32LE(0) === 0xfeedfacf, 'Expected thin Mach-O 64-bit image');
|
|
136
|
+
assert(bytes.readUInt32LE(4) === (target.cpu === 'x64' ? 0x01000007 : 0x0100000c), 'Wrong Mach-O architecture');
|
|
137
|
+
assert((shared ? [6, 8] : [2]).includes(bytes.readUInt32LE(12)), 'Wrong Mach-O executable/library type');
|
|
138
|
+
} else {
|
|
139
|
+
assert(bytes.length >= 64 && bytes.readUInt16LE(0) === 0x5a4d, 'Expected PE executable');
|
|
140
|
+
const pe = bytes.readUInt32LE(60);
|
|
141
|
+
assert(pe >= 64 && pe + 26 <= bytes.length && bytes.readUInt32LE(pe) === 0x4550, 'Truncated or invalid PE header');
|
|
142
|
+
assert(bytes.readUInt16LE(pe + 4) === (target.cpu === 'x64' ? 0x8664 : 0xaa64), 'Wrong PE architecture');
|
|
143
|
+
assert(bytes.readUInt16LE(pe + 24) === 0x20b && (bytes.readUInt16LE(pe + 22) & 0x2002) === (shared ? 0x2002 : 2), 'Wrong PE32+ executable/DLL type');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Require executable code records in addition to the format/architecture header. */
|
|
148
|
+
export function verifyNativeImage(file, platform, shared = false) {
|
|
149
|
+
const bytes = readFileSync(file);
|
|
150
|
+
verifyNativeHeader(bytes, platform, shared);
|
|
151
|
+
const target = targets[platform];
|
|
152
|
+
if (process.platform !== 'win32' && target.os !== 'win32') assert((statSync(file).mode & 0o111) !== 0, 'Native engine is not executable');
|
|
153
|
+
if (target.os === 'linux') {
|
|
154
|
+
const offset = Number(bytes.readBigUInt64LE(32));
|
|
155
|
+
const width = bytes.readUInt16LE(54);
|
|
156
|
+
const count = bytes.readUInt16LE(56);
|
|
157
|
+
assert((shared || bytes.readBigUInt64LE(24) !== 0n) && width >= 56 && count > 0 && offset >= 64 && offset + width * count <= bytes.length, 'ELF image has no executable program table');
|
|
158
|
+
let code = false;
|
|
159
|
+
let interpreter;
|
|
160
|
+
for (let index = 0; index < count; index++) {
|
|
161
|
+
const record = offset + index * width;
|
|
162
|
+
const kind = bytes.readUInt32LE(record);
|
|
163
|
+
const start = Number(bytes.readBigUInt64LE(record + 8));
|
|
164
|
+
const size = Number(bytes.readBigUInt64LE(record + 32));
|
|
165
|
+
assert(start >= 0 && size >= 0 && start + size <= bytes.length, 'ELF segment outside image');
|
|
166
|
+
if (kind === 1 && (bytes.readUInt32LE(record + 4) & 1) && size > 0) code = true;
|
|
167
|
+
if (kind === 3) interpreter = bytes.subarray(start, start + size).toString('utf8').replace(/\0.*$/, '');
|
|
168
|
+
}
|
|
169
|
+
assert(code, 'ELF image has no executable segment');
|
|
170
|
+
if (interpreter) assert(/ld-linux/.test(interpreter), 'ELF interpreter disagrees with declared libc');
|
|
171
|
+
} else if (target.os === 'darwin') {
|
|
172
|
+
const count = bytes.readUInt32LE(16);
|
|
173
|
+
const size = bytes.readUInt32LE(20);
|
|
174
|
+
assert(count > 0 && size >= count * 8 && 32 + size <= bytes.length, 'Mach-O image has no load commands');
|
|
175
|
+
let offset = 32;
|
|
176
|
+
let code = false;
|
|
177
|
+
for (let index = 0; index < count; index++) {
|
|
178
|
+
assert(offset + 8 <= 32 + size, 'Truncated Mach-O load command');
|
|
179
|
+
const command = bytes.readUInt32LE(offset);
|
|
180
|
+
const length = bytes.readUInt32LE(offset + 4);
|
|
181
|
+
assert(length >= 8 && offset + length <= 32 + size, 'Invalid Mach-O load command');
|
|
182
|
+
if (command === 0x19 && length >= 72) {
|
|
183
|
+
const start = Number(bytes.readBigUInt64LE(offset + 40));
|
|
184
|
+
const lengthOnDisk = Number(bytes.readBigUInt64LE(offset + 48));
|
|
185
|
+
assert(start + lengthOnDisk <= bytes.length, 'Mach-O segment outside image');
|
|
186
|
+
if ((bytes.readUInt32LE(offset + 60) & 4) && lengthOnDisk > 0) code = true;
|
|
187
|
+
}
|
|
188
|
+
offset += length;
|
|
189
|
+
}
|
|
190
|
+
assert(code && offset === 32 + size, 'Mach-O image has no complete executable segment');
|
|
191
|
+
} else {
|
|
192
|
+
const pe = bytes.readUInt32LE(60);
|
|
193
|
+
const count = bytes.readUInt16LE(pe + 6);
|
|
194
|
+
const optionalSize = bytes.readUInt16LE(pe + 20);
|
|
195
|
+
const sections = pe + 24 + optionalSize;
|
|
196
|
+
assert(optionalSize >= 112 && count > 0 && sections + count * 40 <= bytes.length && (shared || bytes.readUInt32LE(pe + 40) !== 0), 'PE image has no executable sections');
|
|
197
|
+
let code = false;
|
|
198
|
+
for (let index = 0; index < count; index++) {
|
|
199
|
+
const record = sections + index * 40;
|
|
200
|
+
const size = bytes.readUInt32LE(record + 16);
|
|
201
|
+
const start = bytes.readUInt32LE(record + 20);
|
|
202
|
+
assert(start + size <= bytes.length, 'PE section outside image');
|
|
203
|
+
if ((bytes.readUInt32LE(record + 36) & 0x20000000) && size > 0) code = true;
|
|
204
|
+
}
|
|
205
|
+
assert(code, 'PE image has no executable code');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function verifyPayload(dir, prebuild, actual) {
|
|
210
|
+
const declared = Object.keys(prebuild.files).sort();
|
|
211
|
+
actual.sort();
|
|
212
|
+
assert(JSON.stringify(declared) === JSON.stringify(actual), 'Payload inventory contains missing or undeclared files');
|
|
213
|
+
for (const file of declared) assert(sha256(regularFile(dir, file)) === prebuild.files[file], `Artifact checksum mismatch: ${file}`);
|
|
214
|
+
for (const license of prebuild.licenses) assert(readFileSync(regularFile(dir, license.path), 'utf8').trim().length > 0, `Empty license: ${license.path}`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Full payload gate: checksums, source/license closure, resources, and real engine exports. */
|
|
218
|
+
export function verifyEnginePackage(dir) {
|
|
219
|
+
const manifest = readJson(regularFile(dir, 'package.json'));
|
|
220
|
+
const prebuild = readJson(regularFile(dir, 'prebuilds.json'));
|
|
221
|
+
verifyEngineMetadata(manifest, prebuild);
|
|
222
|
+
assert(prebuild.status === 'built', `${prebuild.platform}: unbuilt target cannot be packed or released`);
|
|
223
|
+
verifyPayload(dir, prebuild, payloadRoots.flatMap((prefix) => inventory(dir, prefix)));
|
|
224
|
+
const declared = Object.keys(prebuild.files);
|
|
225
|
+
const engine = prebuild.engine;
|
|
226
|
+
if (engine.kind === 'native') {
|
|
227
|
+
verifyNativeImage(regularFile(dir, engine.executable), prebuild.platform);
|
|
228
|
+
assert(declared.some((file) => file.startsWith(`${engine.programDirectory}/`)), 'Native engine has no program resources');
|
|
229
|
+
} else {
|
|
230
|
+
for (const key of ['loader', 'wasm', 'data', 'metadata']) regularFile(dir, engine[key]);
|
|
231
|
+
const module = new WebAssembly.Module(readFileSync(join(dir, engine.wasm)));
|
|
232
|
+
const exports = new Set(WebAssembly.Module.exports(module).filter((entry) => entry.kind === 'function').map((entry) => entry.name.replace(/^_/, '')));
|
|
233
|
+
for (const name of ['dsh_lok_initialize', 'dsh_lok_document_load', 'dsh_lok_document_save_pdf', 'dsh_lok_document_destroy', 'dsh_lok_destroy', 'dsh_lok_error', 'malloc', 'free']) {
|
|
234
|
+
assert(exports.has(name), `WASM engine missing conversion export: ${name}`);
|
|
235
|
+
}
|
|
236
|
+
const dataSize = statSync(join(dir, engine.data)).size;
|
|
237
|
+
const metadata = readJson(join(dir, engine.metadata));
|
|
238
|
+
assert(dataSize > 0 && Array.isArray(metadata.files) && metadata.files.length > 0, 'WASM data has no packaged resources');
|
|
239
|
+
const names = new Set();
|
|
240
|
+
const ranges = [];
|
|
241
|
+
for (const file of metadata.files) {
|
|
242
|
+
assert(typeof file.filename === 'string' && (file.filename.startsWith('/instdir/')
|
|
243
|
+
|| ['/android/default-document/example.odt', '/android/default-document/example_test.ods'].includes(file.filename))
|
|
244
|
+
&& !names.has(file.filename), 'Invalid or duplicate WASM resource path');
|
|
245
|
+
safePath(file.filename.slice(1));
|
|
246
|
+
names.add(file.filename);
|
|
247
|
+
assert(Number.isSafeInteger(file.start) && Number.isSafeInteger(file.end) && file.start >= 0 && file.end >= file.start && file.end <= dataSize, 'WASM resource outside data file');
|
|
248
|
+
ranges.push([file.start, file.end]);
|
|
249
|
+
}
|
|
250
|
+
ranges.sort((a, b) => a[0] - b[0]);
|
|
251
|
+
assert(ranges.every((range, index) => index === 0 || ranges[index - 1][1] <= range[0]), 'Overlapping WASM data ranges');
|
|
252
|
+
assert([...names].some((name) => name.startsWith(`${engine.programDirectory}/`)), 'WASM data lacks program resources');
|
|
253
|
+
assert(/\bmodule\[?[^;]*exports|\bmodule\.exports/.test(readFileSync(join(dir, engine.loader), 'utf8')), 'WASM loader must provide a Node CommonJS factory');
|
|
254
|
+
}
|
|
255
|
+
return { name: manifest.name, platform: prebuild.platform, files: declared.length };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (isMain(import.meta.url)) {
|
|
259
|
+
console.log(JSON.stringify(verifyEnginePackage(resolve(process.argv[2] ?? '.'))));
|
|
260
|
+
}
|