From da9586c67292bf9aadbc6d7433eebbe8402eb332 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Wed, 27 Sep 2023 17:42:56 +0800 Subject: [PATCH] dev(pkg::core): add a memory access model --- packages/typst.ts/examples/compiler.html | 6 ++- packages/typst.ts/src/fs/index.mts | 2 + packages/typst.ts/src/fs/memory.mts | 54 ++++++++++++++++++++++++ packages/typst.ts/src/main.mts | 3 +- packages/typst.ts/src/options.init.mts | 1 + 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 packages/typst.ts/src/fs/memory.mts diff --git a/packages/typst.ts/examples/compiler.html b/packages/typst.ts/examples/compiler.html index e976bb3f..61569ebd 100644 --- a/packages/typst.ts/examples/compiler.html +++ b/packages/typst.ts/examples/compiler.html @@ -30,8 +30,9 @@ const begin = performance.now(); compilerPlugin.reset(); - const mainFilePath = 'corpus/skyzh-cv/main.typ'; - // const mainFilePath = 'corpus/package/example.typ'; + const mainFilePath = '/corpus/skyzh-cv/main.typ'; + // const mainFilePath = '/corpus/package/example.typ'; + // compilerPlugin.addSource(mainFilePath, `#import "@preview/example:0.1.0": add`); if (fmt === 'ast') { const ast = await compilerPlugin.getAst(mainFilePath); @@ -73,6 +74,7 @@ let compilerPlugin = window.TypstCompileModule.createTypstCompiler(); + // const fetchBackend = new window.TypstCompileModule.MemoryAccessModel(); const fetchBackend = new window.TypstCompileModule.FetchAccessModel( 'http://localhost:20810', ); diff --git a/packages/typst.ts/src/fs/index.mts b/packages/typst.ts/src/fs/index.mts index a83698f0..326a4541 100644 --- a/packages/typst.ts/src/fs/index.mts +++ b/packages/typst.ts/src/fs/index.mts @@ -3,6 +3,8 @@ import { FsAccessModel } from '../internal.types.mjs'; export { FetchAccessModel } from './fetch.mjs'; export type { FetchAccessOptions } from './fetch.mjs'; +export { MemoryAccessModel } from './memory.mjs'; + export interface WritableAccessModel extends FsAccessModel { insertFile(path: string, data: Uint8Array, mtime: Date): void; removeFile(path: string): void; diff --git a/packages/typst.ts/src/fs/memory.mts b/packages/typst.ts/src/fs/memory.mts new file mode 100644 index 00000000..efbe10c1 --- /dev/null +++ b/packages/typst.ts/src/fs/memory.mts @@ -0,0 +1,54 @@ +import { FsAccessModel } from '../internal.types.mjs'; +import { WritableAccessModel } from './index.mjs'; + +export class MemoryAccessModel implements FsAccessModel, WritableAccessModel { + mTimes: Map = new Map(); + mData: Map = new Map(); + constructor() {} + + reset() { + this.mTimes.clear(); + this.mData.clear(); + } + + insertFile(path: string, data: Uint8Array, mtime: Date) { + this.mTimes.set(path, mtime); + this.mData.set(path, data); + } + + removeFile(path: string) { + this.mTimes.delete(path); + this.mData.delete(path); + } + + getMTime(path: string): Date | undefined { + if (!path.startsWith('/@memory/')) { + return undefined; + } + + if (this.mTimes.has(path)) { + return this.mTimes.get(path); + } + return undefined; + } + + isFile(): boolean | undefined { + return true; + } + + getRealPath(path: string): string | undefined { + return path; + } + + readAll(path: string): Uint8Array | undefined { + if (!path.startsWith('/@memory/')) { + return undefined; + } + + if (this.mData.has(path)) { + return this.mData.get(path); + } + + return undefined; + } +} diff --git a/packages/typst.ts/src/main.mts b/packages/typst.ts/src/main.mts index 1a953162..5327ecb3 100644 --- a/packages/typst.ts/src/main.mts +++ b/packages/typst.ts/src/main.mts @@ -12,7 +12,7 @@ export type { RenderSession, TypstRenderer } from './renderer.mjs'; export { rendererBuildInfo, createTypstRenderer, createTypstSvgRenderer } from './renderer.mjs'; import { RenderView, renderTextLayer } from './render/canvas/view.mjs'; import * as compiler from './compiler.mjs'; -import { FetchAccessModel } from './fs/index.mjs'; +import { FetchAccessModel, MemoryAccessModel } from './fs/index.mjs'; import { FetchPackageRegistry } from './fs/package.mjs'; export { FetchAccessModel } from './fs/index.mjs'; export { FetchPackageRegistry } from './fs/package.mjs'; @@ -37,6 +37,7 @@ if (window) { preloadSystemFonts: initOptions.preloadSystemFonts, FetchAccessModel, + MemoryAccessModel, FetchPackageRegistry, withAccessModel: initOptions.withAccessModel, diff --git a/packages/typst.ts/src/options.init.mts b/packages/typst.ts/src/options.init.mts index 1077bcb0..d8629785 100644 --- a/packages/typst.ts/src/options.init.mts +++ b/packages/typst.ts/src/options.init.mts @@ -29,6 +29,7 @@ export type BeforeBuildMark = typeof BeforeBuildSymbol; * - preloadRemoteFonts * - preloadSystemFonts * - withAccessModel + * - withPackageRegistry */ export type BeforeBuildFn = StagedOptFn;