@vedivad/typst-web-service 0.2.0 → 0.3.0

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/dist/worker.js CHANGED
@@ -2016,6 +2016,50 @@ function getDiagnosticsArg(diagnostics) {
2016
2016
  }
2017
2017
  }
2018
2018
 
2019
+ // ../../node_modules/.bun/@myriaddreamin+typst.ts@0.7.0-rc2+5e675a3c06b42b50/node_modules/@myriaddreamin/typst.ts/dist/esm/fs/memory.mjs
2020
+ var MemoryAccessModel = class {
2021
+ constructor() {
2022
+ __publicField(this, "mTimes", /* @__PURE__ */ new Map());
2023
+ __publicField(this, "mData", /* @__PURE__ */ new Map());
2024
+ }
2025
+ reset() {
2026
+ this.mTimes.clear();
2027
+ this.mData.clear();
2028
+ }
2029
+ insertFile(path, data, mtime) {
2030
+ this.mTimes.set(path, mtime);
2031
+ this.mData.set(path, data);
2032
+ }
2033
+ removeFile(path) {
2034
+ this.mTimes.delete(path);
2035
+ this.mData.delete(path);
2036
+ }
2037
+ getMTime(path) {
2038
+ if (!path.startsWith("/@memory/")) {
2039
+ return void 0;
2040
+ }
2041
+ if (this.mTimes.has(path)) {
2042
+ return this.mTimes.get(path);
2043
+ }
2044
+ return void 0;
2045
+ }
2046
+ isFile() {
2047
+ return true;
2048
+ }
2049
+ getRealPath(path) {
2050
+ return path;
2051
+ }
2052
+ readAll(path) {
2053
+ if (!path.startsWith("/@memory/")) {
2054
+ return void 0;
2055
+ }
2056
+ if (this.mData.has(path)) {
2057
+ return this.mData.get(path);
2058
+ }
2059
+ return void 0;
2060
+ }
2061
+ };
2062
+
2019
2063
  // ../../node_modules/.bun/@myriaddreamin+typst.ts@0.7.0-rc2+5e675a3c06b42b50/node_modules/@myriaddreamin/typst.ts/dist/esm/fs/package.mjs
2020
2064
  var FetchPackageRegistry = class {
2021
2065
  constructor(am) {
@@ -2064,52 +2108,8 @@ var FetchPackageRegistry = class {
2064
2108
  }
2065
2109
  };
2066
2110
 
2067
- // ../../node_modules/.bun/@myriaddreamin+typst.ts@0.7.0-rc2+5e675a3c06b42b50/node_modules/@myriaddreamin/typst.ts/dist/esm/fs/memory.mjs
2068
- var MemoryAccessModel = class {
2069
- constructor() {
2070
- __publicField(this, "mTimes", /* @__PURE__ */ new Map());
2071
- __publicField(this, "mData", /* @__PURE__ */ new Map());
2072
- }
2073
- reset() {
2074
- this.mTimes.clear();
2075
- this.mData.clear();
2076
- }
2077
- insertFile(path, data, mtime) {
2078
- this.mTimes.set(path, mtime);
2079
- this.mData.set(path, data);
2080
- }
2081
- removeFile(path) {
2082
- this.mTimes.delete(path);
2083
- this.mData.delete(path);
2084
- }
2085
- getMTime(path) {
2086
- if (!path.startsWith("/@memory/")) {
2087
- return void 0;
2088
- }
2089
- if (this.mTimes.has(path)) {
2090
- return this.mTimes.get(path);
2091
- }
2092
- return void 0;
2093
- }
2094
- isFile() {
2095
- return true;
2096
- }
2097
- getRealPath(path) {
2098
- return path;
2099
- }
2100
- readAll(path) {
2101
- if (!path.startsWith("/@memory/")) {
2102
- return void 0;
2103
- }
2104
- if (this.mData.has(path)) {
2105
- return this.mData.get(path);
2106
- }
2107
- return void 0;
2108
- }
2109
- };
2110
-
2111
2111
  // src/worker.ts
2112
- var PDF_FORMAT = 1;
2112
+ var MAIN_FILE = "/main.typ";
2113
2113
  var accessModel = new MemoryAccessModel();
2114
2114
  var packageRegistry = new FetchPackageRegistry(accessModel);
2115
2115
  var compiler = null;
@@ -2126,20 +2126,31 @@ async function initCompiler(wasmUrl, fontUrls, packages) {
2126
2126
  function parseRange(range) {
2127
2127
  const m = range.match(/(\d+):(\d+)-(\d+):(\d+)/);
2128
2128
  if (!m) {
2129
- console.warn(`[typst-web-service] Skipping diagnostic with unrecognized range format: ${JSON.stringify(range)}`);
2129
+ console.warn(
2130
+ `[typst-web-service] Skipping diagnostic with unrecognized range format: ${JSON.stringify(range)}`
2131
+ );
2130
2132
  return null;
2131
2133
  }
2132
2134
  return { startLine: +m[1], startCol: +m[2], endLine: +m[3], endCol: +m[4] };
2133
2135
  }
2134
- async function compile(source) {
2136
+ async function compile(files) {
2135
2137
  if (!compiler) throw new Error("Compiler not initialized");
2136
- compiler.addSource("/main.typ", source);
2137
- const result = await compiler.compile({ mainFilePath: "/main.typ", diagnostics: "full" });
2138
- const diagnostics = (result.diagnostics ?? []).flatMap((d) => {
2139
- const range = parseRange(d.range);
2140
- if (!range) return [];
2141
- return [{ ...d, severity: d.severity, range }];
2138
+ for (const [path, source] of Object.entries(files)) {
2139
+ compiler.addSource(path, source);
2140
+ }
2141
+ const result = await compiler.compile({
2142
+ mainFilePath: MAIN_FILE,
2143
+ diagnostics: "full"
2142
2144
  });
2145
+ const diagnostics = (result.diagnostics ?? []).flatMap(
2146
+ (d) => {
2147
+ const range = parseRange(d.range);
2148
+ if (!range) return [];
2149
+ return [
2150
+ { ...d, severity: d.severity, range }
2151
+ ];
2152
+ }
2153
+ );
2143
2154
  return { diagnostics, vector: result.result ?? void 0 };
2144
2155
  }
2145
2156
  function postError(id, err) {
@@ -2150,61 +2161,73 @@ function postError(id, err) {
2150
2161
  });
2151
2162
  }
2152
2163
  function transferBuffer(data) {
2153
- return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
2164
+ return data.buffer.slice(
2165
+ data.byteOffset,
2166
+ data.byteOffset + data.byteLength
2167
+ );
2154
2168
  }
2155
2169
  var yieldToEventLoop = () => new Promise((r) => setTimeout(r, 0));
2156
- var pendingCompile = null;
2157
- var processingCompile = false;
2158
- var pendingRender = null;
2159
- var processingRender = false;
2160
- async function drainCompileQueue() {
2161
- processingCompile = true;
2162
- while (pendingCompile) {
2163
- const req = pendingCompile;
2164
- pendingCompile = null;
2165
- await yieldToEventLoop();
2166
- if (pendingCompile) {
2167
- self.postMessage({ type: "cancelled", id: req.id });
2168
- continue;
2169
- }
2170
- try {
2171
- const { diagnostics, vector: vectorData } = await compile(req.source);
2172
- const vector = vectorData ? transferBuffer(vectorData) : void 0;
2173
- const msg = { type: "result", id: req.id, diagnostics, vector };
2174
- self.postMessage(msg, vector ? [vector] : []);
2175
- } catch (err) {
2176
- postError(req.id, err);
2170
+ function makeQueue(handle) {
2171
+ let pending = null;
2172
+ let processing = false;
2173
+ async function drain() {
2174
+ processing = true;
2175
+ while (pending) {
2176
+ const req = pending;
2177
+ pending = null;
2178
+ await yieldToEventLoop();
2179
+ if (pending) {
2180
+ self.postMessage({
2181
+ type: "cancelled",
2182
+ id: req.id
2183
+ });
2184
+ continue;
2185
+ }
2186
+ await handle(req);
2177
2187
  }
2188
+ processing = false;
2178
2189
  }
2179
- processingCompile = false;
2190
+ return (req) => {
2191
+ pending = req;
2192
+ if (!processing) drain();
2193
+ };
2180
2194
  }
2181
- async function drainRenderQueue() {
2182
- processingRender = true;
2183
- while (pendingRender) {
2184
- const req = pendingRender;
2185
- pendingRender = null;
2186
- await yieldToEventLoop();
2187
- if (pendingRender) {
2188
- self.postMessage({ type: "cancelled", id: req.id });
2189
- continue;
2190
- }
2191
- try {
2192
- if (!compiler) throw new Error("Compiler not initialized");
2193
- compiler.addSource("/main.typ", req.source);
2194
- const result = await compiler.compile({
2195
- mainFilePath: "/main.typ",
2196
- format: PDF_FORMAT,
2197
- diagnostics: "none"
2198
- });
2199
- if (!result.result) throw new Error("Compilation produced no output");
2200
- const data = transferBuffer(result.result);
2201
- self.postMessage({ type: "pdf", id: req.id, data }, [data]);
2202
- } catch (err) {
2203
- postError(req.id, err);
2195
+ var enqueueCompile = makeQueue(async (req) => {
2196
+ try {
2197
+ const { diagnostics, vector: vectorData } = await compile(req.files);
2198
+ const vector = vectorData ? transferBuffer(vectorData) : void 0;
2199
+ const msg = {
2200
+ type: "result",
2201
+ id: req.id,
2202
+ diagnostics,
2203
+ vector
2204
+ };
2205
+ self.postMessage(msg, vector ? [vector] : []);
2206
+ } catch (err) {
2207
+ postError(req.id, err);
2208
+ }
2209
+ });
2210
+ var enqueueRender = makeQueue(async (req) => {
2211
+ try {
2212
+ if (!compiler) throw new Error("Compiler not initialized");
2213
+ for (const [path, source] of Object.entries(req.files)) {
2214
+ compiler.addSource(path, source);
2204
2215
  }
2216
+ const result = await compiler.compile({
2217
+ mainFilePath: MAIN_FILE,
2218
+ format: CompileFormatEnum.pdf,
2219
+ diagnostics: "none"
2220
+ });
2221
+ if (!result.result) throw new Error("Compilation produced no output");
2222
+ const data = transferBuffer(result.result);
2223
+ self.postMessage(
2224
+ { type: "pdf", id: req.id, data },
2225
+ [data]
2226
+ );
2227
+ } catch (err) {
2228
+ postError(req.id, err);
2205
2229
  }
2206
- processingRender = false;
2207
- }
2230
+ });
2208
2231
  self.onmessage = async (e) => {
2209
2232
  const req = e.data;
2210
2233
  if (req.type === "init") {
@@ -2217,17 +2240,18 @@ self.onmessage = async (e) => {
2217
2240
  return;
2218
2241
  }
2219
2242
  if (req.type === "compile") {
2220
- pendingCompile = req;
2221
- if (!processingCompile) drainCompileQueue();
2243
+ enqueueCompile(req);
2222
2244
  return;
2223
2245
  }
2224
2246
  if (req.type === "render") {
2225
- pendingRender = req;
2226
- if (!processingRender) drainRenderQueue();
2247
+ enqueueRender(req);
2227
2248
  return;
2228
2249
  }
2229
2250
  if (req.type === "destroy") {
2230
- self.postMessage({ type: "destroyed", id: req.id });
2251
+ self.postMessage({
2252
+ type: "destroyed",
2253
+ id: req.id
2254
+ });
2231
2255
  }
2232
2256
  };
2233
2257
  //# sourceMappingURL=worker.js.map