Skip to content

Commit

Permalink
dev(pkg::core): add a memory access model
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Sep 27, 2023
1 parent ec05968 commit da9586c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/typst.ts/examples/compiler.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -73,6 +74,7 @@

let compilerPlugin = window.TypstCompileModule.createTypstCompiler();

// const fetchBackend = new window.TypstCompileModule.MemoryAccessModel();
const fetchBackend = new window.TypstCompileModule.FetchAccessModel(
'http://localhost:20810',
);
Expand Down
2 changes: 2 additions & 0 deletions packages/typst.ts/src/fs/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions packages/typst.ts/src/fs/memory.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { FsAccessModel } from '../internal.types.mjs';
import { WritableAccessModel } from './index.mjs';

export class MemoryAccessModel implements FsAccessModel, WritableAccessModel {
mTimes: Map<string, Date | undefined> = new Map();
mData: Map<string, Uint8Array | undefined> = 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;
}
}
3 changes: 2 additions & 1 deletion packages/typst.ts/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -37,6 +37,7 @@ if (window) {
preloadSystemFonts: initOptions.preloadSystemFonts,

FetchAccessModel,
MemoryAccessModel,
FetchPackageRegistry,

withAccessModel: initOptions.withAccessModel,
Expand Down
1 change: 1 addition & 0 deletions packages/typst.ts/src/options.init.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type BeforeBuildMark = typeof BeforeBuildSymbol;
* - preloadRemoteFonts
* - preloadSystemFonts
* - withAccessModel
* - withPackageRegistry
*/
export type BeforeBuildFn = StagedOptFn<BeforeBuildMark>;

Expand Down

0 comments on commit da9586c

Please sign in to comment.