feat: add local OneNote and ONEPKG reader

This commit is contained in:
2026-07-22 17:06:03 +02:00
commit f581cbdced
93 changed files with 14949 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { parseToolboxApp } from '@add-ideas/toolbox-contract';
import { format } from 'prettier';
const projectRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'..'
);
const packageJsonPath = path.join(projectRoot, 'package.json');
const definitionPath = path.join(
projectRoot,
'src',
'toolbox',
'manifest.definition.json'
);
const versionSourcePath = path.join(projectRoot, 'src', 'version.ts');
const manifestPath = path.join(projectRoot, 'public', 'toolbox-app.json');
const [packageJson, definition, versionSource] = await Promise.all([
readJson(packageJsonPath),
readJson(definitionPath),
readFile(versionSourcePath, 'utf8'),
]);
const sourceVersion = versionSource.match(
/APP_VERSION\s*=\s*['"]([^'"]+)['"]/
)?.[1];
if (!sourceVersion) {
throw new Error('Could not read APP_VERSION from src/version.ts.');
}
if (sourceVersion !== packageJson.version) {
throw new Error(
`Version mismatch: package.json is ${packageJson.version}, while src/version.ts is ${sourceVersion}.`
);
}
const manifest = {
$schema:
'https://git.add-ideas.de/zemion/toolbox-sdk/raw/branch/main/schemas/toolbox-app.v1.schema.json',
schemaVersion: 1,
...definition,
version: packageJson.version,
};
parseToolboxApp(manifest);
const output = await format(JSON.stringify(manifest), { parser: 'json' });
if (process.argv.includes('--check')) {
const existing = await readFile(manifestPath, 'utf8').catch(() => '');
if (existing !== output) {
throw new Error(
'public/toolbox-app.json is out of date. Run npm run manifest:generate.'
);
}
} else {
await writeFile(manifestPath, output);
}
async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf8'));
}

126
scripts/package-release.mjs Normal file
View File

@@ -0,0 +1,126 @@
import { createHash } from 'node:crypto';
import { access, mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
collectDirectoryFiles,
createDeterministicZip,
createThirdPartyLicenseBundle,
} from './release-archive.mjs';
const modulePath = fileURLToPath(import.meta.url);
const defaultProjectRoot = path.resolve(path.dirname(modulePath), '..');
export async function collectReleaseMetadataFiles(projectRoot) {
const files = {};
for (const fileName of [
'CHANGELOG.md',
'LICENSE',
'README.md',
'SOURCE.md',
'THIRD_PARTY_NOTICES.md',
]) {
files[fileName] = await readFile(path.join(projectRoot, fileName));
}
for (const licenseName of [
'AGPL-3.0.txt',
'MPL-2.0.txt',
'lzxd-MIT.txt',
'rust-cab-MIT.txt',
]) {
files[`LICENSES/${licenseName}`] = await readFile(
path.join(projectRoot, 'LICENSES', licenseName)
);
}
files['LICENSES/THIRD-PARTY-LICENSES.txt'] =
await createThirdPartyLicenseBundle(projectRoot, [
'@add-ideas/toolbox-contract',
'@add-ideas/toolbox-shell-react',
'react',
'react-dom',
'scheduler',
]);
for (const documentName of [
'FORMAT_SUPPORT.md',
'PORTING.md',
'REFERENCE_IMPLEMENTATIONS.md',
'SECURITY.md',
]) {
files[`docs/${documentName}`] = await readFile(
path.join(projectRoot, 'docs', documentName)
);
}
files['tests/fixtures/README.md'] = await readFile(
path.join(projectRoot, 'tests', 'fixtures', 'README.md')
);
return files;
}
async function packageRelease(projectRoot, arguments_) {
const packageJson = JSON.parse(
await readFile(path.join(projectRoot, 'package.json'), 'utf8')
);
const releaseDirectory = path.join(projectRoot, 'release');
const archiveName = `onenote-tools-${packageJson.version}.zip`;
const archivePath = path.join(releaseDirectory, archiveName);
const checksumPath = path.join(
releaseDirectory,
`onenote-tools-${packageJson.version}.sha256`
);
const force = arguments_.includes('--force');
const unknownArguments = arguments_.filter(
(argument) => argument !== '--force'
);
if (unknownArguments.length > 0) {
throw new Error(`Unknown release argument: ${unknownArguments.join(', ')}`);
}
await mkdir(releaseDirectory, { recursive: true });
if (!force) {
await assertDoesNotExist(archivePath, projectRoot);
await assertDoesNotExist(checksumPath, projectRoot);
}
const files = await collectDirectoryFiles(path.join(projectRoot, 'dist'));
if (Object.keys(files).some((fileName) => fileName.endsWith('.wasm'))) {
throw new Error('Release input contains a forbidden WebAssembly file.');
}
Object.assign(files, await collectReleaseMetadataFiles(projectRoot));
const archive = createDeterministicZip(files);
const checksum = createHash('sha256').update(archive).digest('hex');
const writeFlag = force ? 'w' : 'wx';
await writeFile(archivePath, archive, { flag: writeFlag });
await writeFile(checksumPath, `${checksum} ${archiveName}\n`, {
encoding: 'utf8',
flag: writeFlag,
});
console.log(`Created release/${archiveName}`);
console.log(`SHA-256 ${checksum}`);
}
async function assertDoesNotExist(outputPath, projectRoot) {
try {
await access(outputPath);
} catch (error) {
if (error && typeof error === 'object' && error.code === 'ENOENT') return;
throw error;
}
throw new Error(
`${path.relative(projectRoot, outputPath)} already exists; pass --force to replace this exact version.`
);
}
if (
process.argv[1] !== undefined &&
path.resolve(process.argv[1]) === modulePath
) {
await packageRelease(defaultProjectRoot, process.argv.slice(2));
}

View File

@@ -0,0 +1,12 @@
export function compareCodePoints(left: string, right: string): number;
export function createDeterministicZip(
files: Record<string, Uint8Array>
): Uint8Array;
export function collectDirectoryFiles(
directory: string,
prefix?: string
): Promise<Record<string, Buffer>>;
export function createThirdPartyLicenseBundle(
projectRoot: string,
packageNames: string[]
): Promise<Buffer>;

118
scripts/release-archive.mjs Normal file
View File

@@ -0,0 +1,118 @@
import { readFile, readdir } from 'node:fs/promises';
import path from 'node:path';
import { zipSync } from 'fflate';
const LICENSE_FILE = /^(?:licen[cs]e|copying|notice)(?:[._-].*)?$/iu;
export function compareCodePoints(left, right) {
return left < right ? -1 : left > right ? 1 : 0;
}
export function createDeterministicZip(files) {
const orderedFiles = Object.fromEntries(
Object.entries(files).sort(([left], [right]) =>
compareCodePoints(left, right)
)
);
return zipSync(orderedFiles, {
level: 9,
mtime: new Date(1980, 0, 1, 0, 0, 0),
});
}
export async function collectDirectoryFiles(directory, prefix = '') {
const result = {};
const entries = await readdir(directory, { withFileTypes: true });
entries.sort((left, right) => compareCodePoints(left.name, right.name));
for (const entry of entries) {
const absolutePath = path.join(directory, entry.name);
const relativePath = path.posix.join(prefix, entry.name);
if (entry.isDirectory()) {
Object.assign(
result,
await collectDirectoryFiles(absolutePath, relativePath)
);
} else if (entry.isFile()) {
result[relativePath] = await readFile(absolutePath);
} else {
throw new Error(
`Unsupported filesystem entry in release input: ${relativePath}`
);
}
}
return result;
}
export async function createThirdPartyLicenseBundle(projectRoot, packageNames) {
const sections = [];
for (const packageName of [...new Set(packageNames)].sort(
compareCodePoints
)) {
const packageDirectory = path.join(
projectRoot,
'node_modules',
...packageName.split('/')
);
const packageJson = JSON.parse(
await readFile(path.join(packageDirectory, 'package.json'), 'utf8')
);
const licenseFiles = await collectLicenseFiles(packageDirectory);
if (licenseFiles.length === 0) {
throw new Error(`No license text found for ${packageName}.`);
}
const contents = [];
for (const licenseFile of licenseFiles) {
const text = await readFile(
path.join(packageDirectory, ...licenseFile.split('/')),
'utf8'
);
contents.push(
`--- ${licenseFile} ---\n${text.replaceAll('\r\n', '\n').trim()}\n`
);
}
sections.push(
[
'================================================================================',
`${packageJson.name ?? packageName}@${packageJson.version ?? 'unknown'}`,
`Declared license: ${String(packageJson.license ?? 'unspecified')}`,
'================================================================================',
...contents,
].join('\n')
);
}
return Buffer.from(
[
'Runtime third-party software license texts bundled with OneNote Tools',
'Generated deterministically from installed production packages.',
'',
...sections,
'',
].join('\n'),
'utf8'
);
}
async function collectLicenseFiles(directory, prefix = '') {
const matches = [];
const entries = await readdir(directory, { withFileTypes: true });
entries.sort((left, right) => compareCodePoints(left.name, right.name));
for (const entry of entries) {
if (entry.name === 'node_modules') continue;
const relativePath = path.posix.join(prefix, entry.name);
const absolutePath = path.join(directory, entry.name);
if (entry.isDirectory()) {
matches.push(...(await collectLicenseFiles(absolutePath, relativePath)));
} else if (entry.isFile() && LICENSE_FILE.test(entry.name)) {
matches.push(relativePath);
} else if (!entry.isFile()) {
throw new Error(
`Unsupported filesystem entry in license input: ${relativePath}`
);
}
}
return matches;
}