rocx-docx 0.0.0-stage → 0.4.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/LICENSE +504 -0
- package/README.md +106 -2
- package/authoring.js +51 -0
- package/boundary.js +314 -0
- package/browser.js +10 -0
- package/file-io.js +88 -0
- package/generate.js +36 -0
- package/index.d.ts +106 -0
- package/node.js +14 -0
- package/package.json +50 -4
- package/pkg/rocx.js +945 -0
- package/pkg/rocx_bg.wasm +0 -0
- package/rocx-source.tar.gz +0 -0
- package/types/authoring.d.ts +120 -0
- package/types/boundary.d.ts +27 -0
- package/types/drawings.d.ts +202 -0
- package/types/editor.d.ts +316 -0
- package/types/file-io.d.ts +31 -0
- package/types/inspection.d.ts +457 -0
- package/types/resolution.d.ts +73 -0
- package/types/structured.d.ts +228 -0
- package/types/styles.d.ts +175 -0
- package/types/tables.d.ts +132 -0
- package/types/template.d.ts +45 -0
- package/types/text-layout.d.ts +348 -0
package/boundary.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import {
|
|
2
|
+
configure_host, dispatch, EngineHandle, make_shared, Operation, render_document,
|
|
3
|
+
request_dependency, resolve_component, resolve_input, resolve_program, settle_producer,
|
|
4
|
+
} from "./pkg/rocx.js";
|
|
5
|
+
|
|
6
|
+
const tokens = new WeakMap();
|
|
7
|
+
const handles = new WeakMap();
|
|
8
|
+
const wrappers = new WeakMap();
|
|
9
|
+
const templateFiles = new WeakMap();
|
|
10
|
+
const calls = new Map();
|
|
11
|
+
|
|
12
|
+
// Internal to the Node subpath; neither paths nor loaders enter Wasm.
|
|
13
|
+
export function registerTemplateFile(source, load) {
|
|
14
|
+
templateFiles.set(source, load);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function asError(cause) {
|
|
18
|
+
// Keep existing Error identity (including cross-realm errors).
|
|
19
|
+
try {
|
|
20
|
+
if (cause instanceof Error || Error.isError?.(cause)) return cause;
|
|
21
|
+
} catch {
|
|
22
|
+
// A thrown Proxy may trap instanceof; the original value is still a cause.
|
|
23
|
+
}
|
|
24
|
+
return new Error("rocx operation failed", { cause });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function validateTimestamp(timestamp) {
|
|
28
|
+
if (typeof timestamp !== "string" ||
|
|
29
|
+
!/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d{3})?Z$/.test(timestamp)) {
|
|
30
|
+
throw new TypeError("timestamp must be a UTC ISO timestamp");
|
|
31
|
+
}
|
|
32
|
+
const canonical = timestamp.length === 20 ? timestamp.replace("Z", ".000Z") : timestamp;
|
|
33
|
+
const date = new Date(timestamp);
|
|
34
|
+
if (Number.isNaN(date.valueOf()) || date.toISOString() !== canonical || timestamp.startsWith("0000")) {
|
|
35
|
+
throw new TypeError("timestamp is not a valid UTC date and time");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function valueToken(kind, payload, transform) {
|
|
40
|
+
const token = Object.freeze({
|
|
41
|
+
map(callback) {
|
|
42
|
+
return valueToken("map", token, requireFunction(callback));
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
tokens.set(token, { kind, payload, transform, used: false });
|
|
46
|
+
return token;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function requireFunction(callback) {
|
|
50
|
+
if (typeof callback !== "function") throw new TypeError("producer must be a function");
|
|
51
|
+
return callback;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const ready = (value) => valueToken("ready", value);
|
|
55
|
+
export const lazy = (producer) => valueToken("lazy", requireFunction(producer));
|
|
56
|
+
export const future = (producer) => valueToken("future", requireFunction(producer));
|
|
57
|
+
|
|
58
|
+
export function deferred(value) {
|
|
59
|
+
const token = Object.freeze({});
|
|
60
|
+
tokens.set(token, { kind: "deferred", shared: { value } });
|
|
61
|
+
return token;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function property(name, value, options) {
|
|
65
|
+
const { condition } = readOptions(options, ["condition"]);
|
|
66
|
+
if (typeof name !== "string" || !name.isWellFormed()) {
|
|
67
|
+
throw new TypeError("property name must be a well-formed string");
|
|
68
|
+
}
|
|
69
|
+
if (condition !== undefined && typeof condition !== "boolean" && typeof condition !== "function") {
|
|
70
|
+
throw new TypeError("condition must be a boolean or function");
|
|
71
|
+
}
|
|
72
|
+
const shared = { name, value, condition };
|
|
73
|
+
const reference = Object.freeze({ id: Object.freeze({}) });
|
|
74
|
+
const token = Object.freeze({ reference: () => reference });
|
|
75
|
+
tokens.set(reference, { kind: "ref", shared });
|
|
76
|
+
tokens.set(token, { kind: "property", shared });
|
|
77
|
+
return token;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const host = {
|
|
81
|
+
handleId(value) {
|
|
82
|
+
return handles.get(value)?.id ?? 0;
|
|
83
|
+
},
|
|
84
|
+
inspect(value, consume) {
|
|
85
|
+
const token = tokens.get(value);
|
|
86
|
+
if (!token) return [];
|
|
87
|
+
if (token.shared) {
|
|
88
|
+
const shared = token.shared;
|
|
89
|
+
if (!shared.raw) {
|
|
90
|
+
shared.raw = make_shared(shared.value, shared.name, shared.condition);
|
|
91
|
+
shared.value = undefined;
|
|
92
|
+
shared.condition = undefined;
|
|
93
|
+
}
|
|
94
|
+
return [token.kind, shared.raw.id];
|
|
95
|
+
}
|
|
96
|
+
if (!consume) return [token.kind];
|
|
97
|
+
if (token.used) throw new TypeError("Value tokens are single-use");
|
|
98
|
+
token.used = true;
|
|
99
|
+
const { kind, payload, transform } = token;
|
|
100
|
+
token.payload = token.transform = undefined;
|
|
101
|
+
return [kind, payload, transform];
|
|
102
|
+
},
|
|
103
|
+
start(id, producer, input, mode) {
|
|
104
|
+
const controller = new AbortController();
|
|
105
|
+
const errors = new WeakMap();
|
|
106
|
+
const pending = new Set();
|
|
107
|
+
calls.set(id, { controller, pending });
|
|
108
|
+
const context = Object.freeze({
|
|
109
|
+
signal: controller.signal,
|
|
110
|
+
get(target) {
|
|
111
|
+
const promise = new Promise((resolve, reject) => {
|
|
112
|
+
const accepted = request_dependency(id, target, (ok, value, dependency) => {
|
|
113
|
+
pending.delete(reject);
|
|
114
|
+
if (ok) resolve(value);
|
|
115
|
+
else {
|
|
116
|
+
errors.set(value, dependency);
|
|
117
|
+
reject(value);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
if (accepted) pending.add(reject);
|
|
121
|
+
else reject(controller.signal.reason ?? new Error("Producer is no longer active"));
|
|
122
|
+
});
|
|
123
|
+
// An unawaited dependency can be dropped when its producer settles.
|
|
124
|
+
// Awaiters still see the rejection; the orphan cannot hit the host.
|
|
125
|
+
promise.catch(() => {});
|
|
126
|
+
return promise;
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
const reject = (cause) => settle_producer(id, false, cause, errors.get(cause) ?? 0);
|
|
130
|
+
const resolve = (value) => settle_producer(id, true, value, 0);
|
|
131
|
+
try {
|
|
132
|
+
const result = mode === 2 ? producer(input, context) : producer(context);
|
|
133
|
+
if (mode !== 0 && result != null && typeof result.then === "function") {
|
|
134
|
+
// Silence a later rejection from the invalid return then reject now.
|
|
135
|
+
Promise.resolve(result).catch(() => {});
|
|
136
|
+
throw new TypeError("lazy and map callbacks must not return a Promise; use future");
|
|
137
|
+
}
|
|
138
|
+
if (mode !== 0) resolve(result);
|
|
139
|
+
else Promise.resolve(result).then(resolve, reject);
|
|
140
|
+
} catch (cause) {
|
|
141
|
+
reject(cause);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
finish(id, cancelled) {
|
|
145
|
+
const call = calls.get(id);
|
|
146
|
+
calls.delete(id);
|
|
147
|
+
if (!call) return;
|
|
148
|
+
const reason = new Error(cancelled ? "Producer was aborted" : "Producer has settled");
|
|
149
|
+
if (cancelled) call.controller.abort(reason);
|
|
150
|
+
for (const reject of call.pending) reject(reason);
|
|
151
|
+
call.pending.clear();
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Only process private Wasm outputs. Never walk author declarations or
|
|
156
|
+
// callback values, which may be unreachable until a Rust field is forced.
|
|
157
|
+
function expose(value) {
|
|
158
|
+
if (value === null || typeof value !== "object" || value instanceof Uint8Array) return value;
|
|
159
|
+
if (Array.isArray(value)) return Object.freeze(value.map(expose));
|
|
160
|
+
const handle = Object.hasOwn(value, "$rocxHandle") ? value.$rocxHandle : undefined;
|
|
161
|
+
// Custom metadata can use any key, including the private envelope key.
|
|
162
|
+
if (handle instanceof EngineHandle) {
|
|
163
|
+
const existing = wrappers.get(handle);
|
|
164
|
+
if (existing) return existing;
|
|
165
|
+
const wrapper = {};
|
|
166
|
+
if (value.view !== undefined) wrapper.view = expose(value.view);
|
|
167
|
+
if (value.kind === "document") {
|
|
168
|
+
wrapper.toDocx = (options) => render_document(wrapper, options);
|
|
169
|
+
}
|
|
170
|
+
if (value.methods) {
|
|
171
|
+
for (const [method, operation] of Object.entries(value.methods)) {
|
|
172
|
+
wrapper[method] = (...args) => invokeHandle(operation, { target: wrapper, args });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
handles.set(wrapper, handle);
|
|
176
|
+
wrappers.set(handle, wrapper);
|
|
177
|
+
return Object.freeze(wrapper);
|
|
178
|
+
}
|
|
179
|
+
for (const [key, child] of Object.entries(value)) {
|
|
180
|
+
if (child instanceof Uint8Array) {
|
|
181
|
+
Object.defineProperty(value, key, { get: () => child.slice(), enumerable: true });
|
|
182
|
+
} else {
|
|
183
|
+
value[key] = expose(child);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return Object.freeze(value);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function checkSignal(signal) {
|
|
190
|
+
if (signal !== undefined && (
|
|
191
|
+
signal === null || typeof signal.aborted !== "boolean" ||
|
|
192
|
+
typeof signal.addEventListener !== "function" || typeof signal.removeEventListener !== "function"
|
|
193
|
+
)) {
|
|
194
|
+
throw new TypeError("signal must be an AbortSignal");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function readOptions(options, keys) {
|
|
199
|
+
if (options === undefined) return {};
|
|
200
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
201
|
+
throw new TypeError("expected an options record");
|
|
202
|
+
}
|
|
203
|
+
const result = {};
|
|
204
|
+
for (const key of Reflect.ownKeys(options)) {
|
|
205
|
+
if (!keys.includes(key)) throw new TypeError(`unknown option ${String(key)}`);
|
|
206
|
+
const field = Reflect.getOwnPropertyDescriptor(options, key);
|
|
207
|
+
if (!field || !Object.hasOwn(field, "value")) throw new TypeError(`option ${key} must be a data property`);
|
|
208
|
+
result[key] = field.value;
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function aborted(signal) {
|
|
214
|
+
const error = new Error("The operation was aborted", { cause: signal.reason });
|
|
215
|
+
error.name = "AbortError";
|
|
216
|
+
return error;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function waitForInitialization(initialize, signal) {
|
|
220
|
+
if (!signal) return initialize();
|
|
221
|
+
if (signal.aborted) throw aborted(signal);
|
|
222
|
+
let onAbort;
|
|
223
|
+
try {
|
|
224
|
+
await Promise.race([
|
|
225
|
+
initialize(),
|
|
226
|
+
new Promise((_, reject) => {
|
|
227
|
+
onAbort = () => reject(aborted(signal));
|
|
228
|
+
if (signal.aborted) onAbort();
|
|
229
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
230
|
+
}),
|
|
231
|
+
]);
|
|
232
|
+
} finally {
|
|
233
|
+
if (onAbort) signal.removeEventListener("abort", onAbort);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function createBoundary(initialize) {
|
|
238
|
+
let initialized;
|
|
239
|
+
function init() {
|
|
240
|
+
initialized ??= Promise.resolve().then(initialize).then(() => configure_host(host)).catch((error) => {
|
|
241
|
+
initialized = undefined;
|
|
242
|
+
throw asError(error);
|
|
243
|
+
});
|
|
244
|
+
return initialized;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function operation(signal, work) {
|
|
248
|
+
checkSignal(signal);
|
|
249
|
+
await waitForInitialization(init, signal);
|
|
250
|
+
if (signal?.aborted) throw aborted(signal);
|
|
251
|
+
const operation = new Operation();
|
|
252
|
+
const abort = () => operation.abort(signal.reason);
|
|
253
|
+
try {
|
|
254
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
255
|
+
return await work(operation);
|
|
256
|
+
} catch (cause) {
|
|
257
|
+
throw asError(cause);
|
|
258
|
+
} finally {
|
|
259
|
+
signal?.removeEventListener("abort", abort);
|
|
260
|
+
operation.free();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
init,
|
|
266
|
+
async resolveDocument(program, options) {
|
|
267
|
+
const { signal } = readOptions(options, ["signal"]);
|
|
268
|
+
// Read the host clock once, even if a deferred timestamp is authored.
|
|
269
|
+
const clock = new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
270
|
+
return expose(await operation(signal, (op) => resolve_program(program, clock, op)));
|
|
271
|
+
},
|
|
272
|
+
async resolveComponent(declaration, options) {
|
|
273
|
+
const { signal, scope } = readOptions(options, ["signal", "scope"]);
|
|
274
|
+
if (scope !== undefined && scope !== "inline" && scope !== "block") {
|
|
275
|
+
throw new TypeError("scope must be inline or block");
|
|
276
|
+
}
|
|
277
|
+
return expose(await operation(signal, (op) => resolve_component(declaration, scope, op)));
|
|
278
|
+
},
|
|
279
|
+
async resolveValue(value, options) {
|
|
280
|
+
const { signal } = readOptions(options, ["signal"]);
|
|
281
|
+
return operation(signal, (op) => resolve_input(value, op));
|
|
282
|
+
},
|
|
283
|
+
// B1/B2 feature hooks: initialize for root calls; owned handle methods
|
|
284
|
+
// can call dispatch synchronously after initialization.
|
|
285
|
+
async invoke(name, input) {
|
|
286
|
+
await init();
|
|
287
|
+
return invokeHandle(name, input);
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function invokeHandle(name, input) {
|
|
293
|
+
try {
|
|
294
|
+
if (name === "toDocxWithTemplate") {
|
|
295
|
+
const [source, ...remaining] = input.args;
|
|
296
|
+
const load = templateFiles.get(source);
|
|
297
|
+
if (load) {
|
|
298
|
+
return loadTemplateFile(load, input.target, remaining);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return expose(dispatch(name, input));
|
|
302
|
+
} catch (cause) {
|
|
303
|
+
throw asError(cause);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function loadTemplateFile(load, target, remaining) {
|
|
308
|
+
try {
|
|
309
|
+
const source = await load();
|
|
310
|
+
return expose(dispatch("toDocxWithTemplate", { target, args: [source, ...remaining] }));
|
|
311
|
+
} catch (cause) {
|
|
312
|
+
throw asError(cause);
|
|
313
|
+
}
|
|
314
|
+
}
|
package/browser.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import init from "./pkg/rocx.js";
|
|
2
|
+
import { createApi } from "./generate.js";
|
|
3
|
+
|
|
4
|
+
export const {
|
|
5
|
+
generateDocx, resolveDocument, resolveComponent, resolveValue,
|
|
6
|
+
openDocx, compileTemplate, inlineText, createResolvedInline, createResolvedBlockControl,
|
|
7
|
+
validateValue, validateAuthoringId, bookmarkStart, bookmarkEnd,
|
|
8
|
+
} = createApi(() => init());
|
|
9
|
+
export { ready, lazy, future, deferred, property } from "./boundary.js";
|
|
10
|
+
export { declarative } from "./authoring.js";
|
package/file-io.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { open, readFile, rename, unlink } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, isAbsolute, parse, resolve, sep } from "node:path";
|
|
4
|
+
import { future, invokeHandle, registerTemplateFile } from "./boundary.js";
|
|
5
|
+
import { render_document } from "./pkg/rocx.js";
|
|
6
|
+
|
|
7
|
+
export * from "./node.js";
|
|
8
|
+
|
|
9
|
+
function checkPath(path) {
|
|
10
|
+
if (typeof path !== "string" || !path.length || !path.isWellFormed() || path.includes("\0")) {
|
|
11
|
+
throw new TypeError("path must be a nonempty, well-formed filesystem path");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function templateFile(path, edits) {
|
|
16
|
+
checkPath(path);
|
|
17
|
+
const source = Object.freeze({});
|
|
18
|
+
registerTemplateFile(source, async () => ({ bytes: await readFile(path), edits }));
|
|
19
|
+
return source;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function imageFile(path) {
|
|
23
|
+
checkPath(path);
|
|
24
|
+
return future(async ({ signal }) => {
|
|
25
|
+
const bytes = await readFile(path, { signal });
|
|
26
|
+
return Object.freeze({ bytes, sourceName: basename(path) });
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function inDirectory(parent, name) {
|
|
31
|
+
return `${parent}${parent.endsWith(sep) ? "" : sep}${name}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function absoluteDestination(path) {
|
|
35
|
+
const { root } = parse(path);
|
|
36
|
+
// Windows drive-relative and current-drive-rooted paths still depend on
|
|
37
|
+
// process.cwd(); resolve those using Node's platform rules.
|
|
38
|
+
if (sep === "\\" && root && (root.length <= 2)) return resolve(path);
|
|
39
|
+
return isAbsolute(path) ? path : inDirectory(process.cwd(), path);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function replaceFile(path, bytes) {
|
|
43
|
+
const temporary = inDirectory(dirname(path), `.rocx-${randomUUID()}.tmp`);
|
|
44
|
+
// Only clean up a file after acquiring exclusive ownership of its name.
|
|
45
|
+
const file = await open(temporary, "wx");
|
|
46
|
+
try {
|
|
47
|
+
try {
|
|
48
|
+
await file.writeFile(bytes);
|
|
49
|
+
} finally {
|
|
50
|
+
await file.close();
|
|
51
|
+
}
|
|
52
|
+
await rename(temporary, path);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
await unlink(temporary).catch(() => {});
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function saveOptions(input) {
|
|
60
|
+
if (input === undefined) return {};
|
|
61
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
62
|
+
throw new TypeError("expected an options record");
|
|
63
|
+
}
|
|
64
|
+
const output = {};
|
|
65
|
+
for (const key of Reflect.ownKeys(input)) {
|
|
66
|
+
if (!["template", "profile", "timestamp", "readLimits"].includes(key)) {
|
|
67
|
+
throw new TypeError(`unknown option ${String(key)}`);
|
|
68
|
+
}
|
|
69
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(input, key);
|
|
70
|
+
if (!descriptor || !Object.hasOwn(descriptor, "value")) {
|
|
71
|
+
throw new TypeError(`option ${key} must be a data property`);
|
|
72
|
+
}
|
|
73
|
+
output[key] = descriptor.value;
|
|
74
|
+
}
|
|
75
|
+
return output;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function saveDocx(document, path, options) {
|
|
79
|
+
checkPath(path);
|
|
80
|
+
// Keep the rename and its sibling write in the same directory even if
|
|
81
|
+
// process.cwd() changes during asynchronous template reading or saving.
|
|
82
|
+
const destination = absoluteDestination(path);
|
|
83
|
+
const { template, ...renderOptions } = saveOptions(options);
|
|
84
|
+
const bytes = template === undefined
|
|
85
|
+
? render_document(document, renderOptions)
|
|
86
|
+
: await invokeHandle("toDocxWithTemplate", { target: document, args: [template, renderOptions] });
|
|
87
|
+
await replaceFile(destination, bytes);
|
|
88
|
+
}
|
package/generate.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { render_docx } from "./pkg/rocx.js";
|
|
2
|
+
import { asError, createBoundary, validateTimestamp } from "./boundary.js";
|
|
3
|
+
|
|
4
|
+
export function createApi(initialize) {
|
|
5
|
+
const boundary = createBoundary(initialize);
|
|
6
|
+
async function generateDocx(declaration, options) {
|
|
7
|
+
if (declaration != null && Object.hasOwn(declaration, "sections") &&
|
|
8
|
+
!Object.hasOwn(declaration, "blocks")) {
|
|
9
|
+
const document = await boundary.resolveDocument(declaration);
|
|
10
|
+
return document.toDocx(options);
|
|
11
|
+
}
|
|
12
|
+
const { timestamp = new Date().toISOString() } = options === undefined ? {} : options;
|
|
13
|
+
validateTimestamp(timestamp);
|
|
14
|
+
await boundary.init();
|
|
15
|
+
try {
|
|
16
|
+
return await render_docx(declaration, timestamp);
|
|
17
|
+
} catch (cause) {
|
|
18
|
+
throw asError(cause);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// Named root routes are reserved here, not in feature-owned JS entrypoints.
|
|
22
|
+
return {
|
|
23
|
+
...boundary,
|
|
24
|
+
generateDocx,
|
|
25
|
+
openDocx: (bytes, options) => boundary.invoke("openDocx", { bytes, options }),
|
|
26
|
+
compileTemplate: (document, source, options) => boundary.invoke("compileTemplate", { document, source, options }),
|
|
27
|
+
inlineText: (handles) => boundary.invoke("inlineText", { handles }),
|
|
28
|
+
createResolvedInline: (input) => boundary.invoke("createResolvedInline", input),
|
|
29
|
+
createResolvedBlockControl: (properties, children) =>
|
|
30
|
+
boundary.invoke("createResolvedBlockControl", { properties, children }),
|
|
31
|
+
validateValue: (input) => boundary.invoke("validateValue", input),
|
|
32
|
+
validateAuthoringId: (kind, value) => boundary.invoke("validateAuthoringId", { kind, value }),
|
|
33
|
+
bookmarkStart: (bookmarkName) => boundary.invoke("bookmarkStart", { name: bookmarkName }),
|
|
34
|
+
bookmarkEnd: (bookmarkName) => boundary.invoke("bookmarkEnd", { name: bookmarkName }),
|
|
35
|
+
};
|
|
36
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export interface DocumentDeclaration {
|
|
2
|
+
/** Blocks in a single section with default page layout. */
|
|
3
|
+
readonly blocks: readonly Block[];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type Block = Paragraph | Table;
|
|
7
|
+
export type Run = TextRun | ImageRun | Tab | Break;
|
|
8
|
+
export type Inline = Run | Hyperlink;
|
|
9
|
+
|
|
10
|
+
export type Alignment =
|
|
11
|
+
| "left" | "center" | "right" | "justify" | "distribute"
|
|
12
|
+
| "justifyMedium" | "justifyHigh" | "justifyLow" | "thaiJustify";
|
|
13
|
+
|
|
14
|
+
export interface Paragraph {
|
|
15
|
+
readonly kind: "paragraph";
|
|
16
|
+
readonly runs: readonly Inline[];
|
|
17
|
+
readonly alignment?: Alignment;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TextRun {
|
|
21
|
+
readonly kind: "text";
|
|
22
|
+
readonly text: string;
|
|
23
|
+
readonly bold?: boolean;
|
|
24
|
+
readonly italic?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ImageRun {
|
|
28
|
+
readonly kind: "image";
|
|
29
|
+
/** Encoded image bytes (not pixels, a path, or a URL). */
|
|
30
|
+
readonly bytes: Uint8Array;
|
|
31
|
+
readonly altText?: string;
|
|
32
|
+
/** Width in points. If only one dimension is given, preserve the aspect ratio. */
|
|
33
|
+
readonly width?: number;
|
|
34
|
+
/** Height in points. If neither dimension is given, use the intrinsic size. */
|
|
35
|
+
readonly height?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface Tab {
|
|
39
|
+
readonly kind: "tab";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Break {
|
|
43
|
+
readonly kind: "break";
|
|
44
|
+
/** Defaults to a line break. */
|
|
45
|
+
readonly breakType?: "line" | "page" | "column" | "textWrapping";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface Hyperlink {
|
|
49
|
+
readonly kind: "hyperlink";
|
|
50
|
+
/** Absolute external URI/IRI; no internal bookmarks. */
|
|
51
|
+
readonly target: string;
|
|
52
|
+
readonly runs: readonly Run[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface TableCell {
|
|
56
|
+
/** Ordered blocks, including paragraphs and nested tables. */
|
|
57
|
+
readonly blocks: readonly Block[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface Table {
|
|
61
|
+
readonly kind: "table";
|
|
62
|
+
/** Nonempty rectangular grid; a string is shorthand for a plain paragraph. */
|
|
63
|
+
readonly rows: readonly (readonly (string | TableCell)[])[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface GenerateOptions {
|
|
67
|
+
/** UTC ISO timestamp with seconds and optional milliseconds; defaults to now. */
|
|
68
|
+
readonly timestamp?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** An adapter or rendering error, with context when available. */
|
|
72
|
+
export interface GenerateDocxError extends Error {
|
|
73
|
+
/** Location in the JavaScript declaration, when known. */
|
|
74
|
+
readonly path?: string;
|
|
75
|
+
/** Stable diagnostic code from the rocx engine, when available. */
|
|
76
|
+
readonly rule?: string;
|
|
77
|
+
/** Original thrown/rejected host value, when available. */
|
|
78
|
+
readonly cause?: unknown;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Generate a complete DOCX package. Rejects with an Error on failure. */
|
|
82
|
+
export function generateDocx(
|
|
83
|
+
declaration: DocumentDeclaration,
|
|
84
|
+
options?: GenerateOptions,
|
|
85
|
+
): Promise<Uint8Array<ArrayBuffer>>;
|
|
86
|
+
|
|
87
|
+
export { declarative } from "./types/authoring.js";
|
|
88
|
+
export * from "./types/boundary.js";
|
|
89
|
+
export * from "./types/resolution.js";
|
|
90
|
+
export * from "./types/text-layout.js";
|
|
91
|
+
export * from "./types/styles.js";
|
|
92
|
+
export * from "./types/tables.js";
|
|
93
|
+
export * from "./types/drawings.js";
|
|
94
|
+
export * from "./types/structured.js";
|
|
95
|
+
export * from "./types/template.js";
|
|
96
|
+
export * from "./types/inspection.js";
|
|
97
|
+
export * from "./types/editor.js";
|
|
98
|
+
|
|
99
|
+
import type { RenderOptions } from "./types/boundary.js";
|
|
100
|
+
import type { DocumentProgram } from "./types/text-layout.js";
|
|
101
|
+
|
|
102
|
+
/** Resolve once and render with independent output options. */
|
|
103
|
+
export function generateDocx(
|
|
104
|
+
program: DocumentProgram,
|
|
105
|
+
options?: RenderOptions,
|
|
106
|
+
): Promise<Uint8Array<ArrayBuffer>>;
|
package/node.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import init from "./pkg/rocx.js";
|
|
3
|
+
import { createApi } from "./generate.js";
|
|
4
|
+
|
|
5
|
+
export const {
|
|
6
|
+
generateDocx, resolveDocument, resolveComponent, resolveValue,
|
|
7
|
+
openDocx, compileTemplate, inlineText, createResolvedInline, createResolvedBlockControl,
|
|
8
|
+
validateValue, validateAuthoringId, bookmarkStart, bookmarkEnd,
|
|
9
|
+
} = createApi(async () => {
|
|
10
|
+
const bytes = await readFile(new URL("./pkg/rocx_bg.wasm", import.meta.url));
|
|
11
|
+
await init({ module_or_path: bytes });
|
|
12
|
+
});
|
|
13
|
+
export { ready, lazy, future, deferred, property } from "./boundary.js";
|
|
14
|
+
export { declarative } from "./authoring.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rocx-docx",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Author DOCX documents in Node and browsers with the Rust rocx engine",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "^22.12.0 || >=24.0.0"
|
|
8
|
+
},
|
|
9
|
+
"license": "LGPL-2.1-or-later",
|
|
10
|
+
"homepage": "https://reindervosdewael.github.io/rocx/",
|
|
11
|
+
"bugs": "https://github.com/ReinderVosDeWael/rocx/issues",
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public",
|
|
14
|
+
"registry": "https://registry.npmjs.org"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/ReinderVosDeWael/rocx.git",
|
|
19
|
+
"directory": "javascript"
|
|
20
|
+
},
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./index.d.ts",
|
|
25
|
+
"node": "./node.js",
|
|
26
|
+
"default": "./browser.js"
|
|
27
|
+
},
|
|
28
|
+
"./node": {
|
|
29
|
+
"node": {
|
|
30
|
+
"types": "./types/file-io.d.ts",
|
|
31
|
+
"default": "./file-io.js"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"authoring.js",
|
|
37
|
+
"browser.js",
|
|
38
|
+
"boundary.js",
|
|
39
|
+
"file-io.js",
|
|
40
|
+
"generate.js",
|
|
41
|
+
"index.d.ts",
|
|
42
|
+
"node.js",
|
|
43
|
+
"resolution.js",
|
|
44
|
+
"types",
|
|
45
|
+
"pkg/rocx.js",
|
|
46
|
+
"pkg/rocx_bg.wasm",
|
|
47
|
+
"rocx-source.tar.gz"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"prepack": "bash build.sh"
|
|
51
|
+
}
|
|
52
|
+
}
|