@docxly/core-rs 0.1.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/README.md +50 -0
- package/dist/browser.js +21 -0
- package/dist/generated/core_rs.d.ts +39 -0
- package/dist/generated/core_rs.js +234 -0
- package/dist/generated/core_rs_bg.wasm +0 -0
- package/dist/generated/core_rs_bg.wasm.d.ts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/node.js +26 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @docxly/core-rs
|
|
2
|
+
|
|
3
|
+
`@docxly/core-rs` is the npm package for the `docxly` Rust core. It exposes DOCX generation through a WASM wrapper for Node and browser-based runtimes.
|
|
4
|
+
|
|
5
|
+
## Status
|
|
6
|
+
|
|
7
|
+
- DOCX generation: supported
|
|
8
|
+
- HWPX generation: not exposed in npm v0.x
|
|
9
|
+
- API style: async only
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @docxly/core-rs
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Node
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { writeFile } from "node:fs/promises";
|
|
23
|
+
import { generateDocx } from "@docxly/core-rs";
|
|
24
|
+
|
|
25
|
+
const bytes = await generateDocx("# Hello\n\nThis is **docxly**.");
|
|
26
|
+
await writeFile("output.docx", bytes);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Browser
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { generateDocx } from "@docxly/core-rs";
|
|
33
|
+
|
|
34
|
+
const bytes = await generateDocx("# Hello\n\nThis is **docxly**.");
|
|
35
|
+
const blob = new Blob([bytes], {
|
|
36
|
+
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
export interface DocxOptions {
|
|
44
|
+
title?: string;
|
|
45
|
+
author?: string;
|
|
46
|
+
strictMode?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function generateDocx(markdown: string, options?: DocxOptions): Promise<Uint8Array>;
|
|
50
|
+
```
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import init, { generateDocxBytes } from "./generated/core_rs.js";
|
|
2
|
+
|
|
3
|
+
let initPromise;
|
|
4
|
+
|
|
5
|
+
async function ensureInit() {
|
|
6
|
+
if (!initPromise) {
|
|
7
|
+
initPromise = init({ module_or_path: new URL("./generated/core_rs_bg.wasm", import.meta.url) });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
await initPromise;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function generateDocx(markdown, options = {}) {
|
|
14
|
+
await ensureInit();
|
|
15
|
+
return generateDocxBytes(
|
|
16
|
+
markdown,
|
|
17
|
+
options.title ?? null,
|
|
18
|
+
options.author ?? null,
|
|
19
|
+
options.strictMode ?? true,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export function generateDocxBytes(markdown: string, title: string | null | undefined, author: string | null | undefined, strict_mode: boolean): Uint8Array;
|
|
5
|
+
|
|
6
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
7
|
+
|
|
8
|
+
export interface InitOutput {
|
|
9
|
+
readonly memory: WebAssembly.Memory;
|
|
10
|
+
readonly generateDocxBytes: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
|
|
11
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
12
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
13
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
14
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
15
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
16
|
+
readonly __wbindgen_start: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
23
|
+
* a precompiled `WebAssembly.Module`.
|
|
24
|
+
*
|
|
25
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
26
|
+
*
|
|
27
|
+
* @returns {InitOutput}
|
|
28
|
+
*/
|
|
29
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
33
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
34
|
+
*
|
|
35
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
36
|
+
*
|
|
37
|
+
* @returns {Promise<InitOutput>}
|
|
38
|
+
*/
|
|
39
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/* @ts-self-types="./core_rs.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} markdown
|
|
5
|
+
* @param {string | null | undefined} title
|
|
6
|
+
* @param {string | null | undefined} author
|
|
7
|
+
* @param {boolean} strict_mode
|
|
8
|
+
* @returns {Uint8Array}
|
|
9
|
+
*/
|
|
10
|
+
export function generateDocxBytes(markdown, title, author, strict_mode) {
|
|
11
|
+
const ptr0 = passStringToWasm0(markdown, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
12
|
+
const len0 = WASM_VECTOR_LEN;
|
|
13
|
+
var ptr1 = isLikeNone(title) ? 0 : passStringToWasm0(title, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
14
|
+
var len1 = WASM_VECTOR_LEN;
|
|
15
|
+
var ptr2 = isLikeNone(author) ? 0 : passStringToWasm0(author, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
16
|
+
var len2 = WASM_VECTOR_LEN;
|
|
17
|
+
const ret = wasm.generateDocxBytes(ptr0, len0, ptr1, len1, ptr2, len2, strict_mode);
|
|
18
|
+
if (ret[3]) {
|
|
19
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
20
|
+
}
|
|
21
|
+
var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
22
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
23
|
+
return v4;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function __wbg_get_imports() {
|
|
27
|
+
const import0 = {
|
|
28
|
+
__proto__: null,
|
|
29
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
30
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
31
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
32
|
+
return ret;
|
|
33
|
+
},
|
|
34
|
+
__wbindgen_init_externref_table: function() {
|
|
35
|
+
const table = wasm.__wbindgen_externrefs;
|
|
36
|
+
const offset = table.grow(4);
|
|
37
|
+
table.set(0, undefined);
|
|
38
|
+
table.set(offset + 0, undefined);
|
|
39
|
+
table.set(offset + 1, null);
|
|
40
|
+
table.set(offset + 2, true);
|
|
41
|
+
table.set(offset + 3, false);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
__proto__: null,
|
|
46
|
+
"./core_rs_bg.js": import0,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
51
|
+
ptr = ptr >>> 0;
|
|
52
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getStringFromWasm0(ptr, len) {
|
|
56
|
+
ptr = ptr >>> 0;
|
|
57
|
+
return decodeText(ptr, len);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let cachedUint8ArrayMemory0 = null;
|
|
61
|
+
function getUint8ArrayMemory0() {
|
|
62
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
63
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
64
|
+
}
|
|
65
|
+
return cachedUint8ArrayMemory0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isLikeNone(x) {
|
|
69
|
+
return x === undefined || x === null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
73
|
+
if (realloc === undefined) {
|
|
74
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
75
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
76
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
77
|
+
WASM_VECTOR_LEN = buf.length;
|
|
78
|
+
return ptr;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let len = arg.length;
|
|
82
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
83
|
+
|
|
84
|
+
const mem = getUint8ArrayMemory0();
|
|
85
|
+
|
|
86
|
+
let offset = 0;
|
|
87
|
+
|
|
88
|
+
for (; offset < len; offset++) {
|
|
89
|
+
const code = arg.charCodeAt(offset);
|
|
90
|
+
if (code > 0x7F) break;
|
|
91
|
+
mem[ptr + offset] = code;
|
|
92
|
+
}
|
|
93
|
+
if (offset !== len) {
|
|
94
|
+
if (offset !== 0) {
|
|
95
|
+
arg = arg.slice(offset);
|
|
96
|
+
}
|
|
97
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
98
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
99
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
100
|
+
|
|
101
|
+
offset += ret.written;
|
|
102
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
WASM_VECTOR_LEN = offset;
|
|
106
|
+
return ptr;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function takeFromExternrefTable0(idx) {
|
|
110
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
111
|
+
wasm.__externref_table_dealloc(idx);
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
116
|
+
cachedTextDecoder.decode();
|
|
117
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
118
|
+
let numBytesDecoded = 0;
|
|
119
|
+
function decodeText(ptr, len) {
|
|
120
|
+
numBytesDecoded += len;
|
|
121
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
122
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
123
|
+
cachedTextDecoder.decode();
|
|
124
|
+
numBytesDecoded = len;
|
|
125
|
+
}
|
|
126
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const cachedTextEncoder = new TextEncoder();
|
|
130
|
+
|
|
131
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
132
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
133
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
134
|
+
view.set(buf);
|
|
135
|
+
return {
|
|
136
|
+
read: arg.length,
|
|
137
|
+
written: buf.length
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let WASM_VECTOR_LEN = 0;
|
|
143
|
+
|
|
144
|
+
let wasmModule, wasm;
|
|
145
|
+
function __wbg_finalize_init(instance, module) {
|
|
146
|
+
wasm = instance.exports;
|
|
147
|
+
wasmModule = module;
|
|
148
|
+
cachedUint8ArrayMemory0 = null;
|
|
149
|
+
wasm.__wbindgen_start();
|
|
150
|
+
return wasm;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function __wbg_load(module, imports) {
|
|
154
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
155
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
156
|
+
try {
|
|
157
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
160
|
+
|
|
161
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
162
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
163
|
+
|
|
164
|
+
} else { throw e; }
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const bytes = await module.arrayBuffer();
|
|
169
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
170
|
+
} else {
|
|
171
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
172
|
+
|
|
173
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
174
|
+
return { instance, module };
|
|
175
|
+
} else {
|
|
176
|
+
return instance;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function expectedResponseType(type) {
|
|
181
|
+
switch (type) {
|
|
182
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function initSync(module) {
|
|
189
|
+
if (wasm !== undefined) return wasm;
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
if (module !== undefined) {
|
|
193
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
194
|
+
({module} = module)
|
|
195
|
+
} else {
|
|
196
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const imports = __wbg_get_imports();
|
|
201
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
202
|
+
module = new WebAssembly.Module(module);
|
|
203
|
+
}
|
|
204
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
205
|
+
return __wbg_finalize_init(instance, module);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function __wbg_init(module_or_path) {
|
|
209
|
+
if (wasm !== undefined) return wasm;
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
if (module_or_path !== undefined) {
|
|
213
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
214
|
+
({module_or_path} = module_or_path)
|
|
215
|
+
} else {
|
|
216
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (module_or_path === undefined) {
|
|
221
|
+
module_or_path = new URL('core_rs_bg.wasm', import.meta.url);
|
|
222
|
+
}
|
|
223
|
+
const imports = __wbg_get_imports();
|
|
224
|
+
|
|
225
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
226
|
+
module_or_path = fetch(module_or_path);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
230
|
+
|
|
231
|
+
return __wbg_finalize_init(instance, module);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const generateDocxBytes: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
|
|
5
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
6
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
7
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
8
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
9
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
10
|
+
export const __wbindgen_start: () => void;
|
package/dist/index.d.ts
ADDED
package/dist/node.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
import init, { generateDocxBytes } from "./generated/core_rs.js";
|
|
4
|
+
|
|
5
|
+
let initPromise;
|
|
6
|
+
|
|
7
|
+
async function ensureInit() {
|
|
8
|
+
if (!initPromise) {
|
|
9
|
+
initPromise = (async () => {
|
|
10
|
+
const wasmBytes = await readFile(new URL("./generated/core_rs_bg.wasm", import.meta.url));
|
|
11
|
+
await init({ module_or_path: wasmBytes });
|
|
12
|
+
})();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
await initPromise;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function generateDocx(markdown, options = {}) {
|
|
19
|
+
await ensureInit();
|
|
20
|
+
return generateDocxBytes(
|
|
21
|
+
markdown,
|
|
22
|
+
options.title ?? null,
|
|
23
|
+
options.author ?? null,
|
|
24
|
+
options.strictMode ?? true,
|
|
25
|
+
);
|
|
26
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@docxly/core-rs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "WASM wrapper for docxly core-rs DOCX generation",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/docxly/core-rs.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/docxly/core-rs",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/docxly/core-rs/issues"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"main": "./dist/node.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"browser": "./dist/browser.js",
|
|
25
|
+
"default": "./dist/node.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"clean": "rm -rf dist",
|
|
37
|
+
"check:version": "node ./scripts/check-version.mjs",
|
|
38
|
+
"build": "node ./scripts/build.mjs",
|
|
39
|
+
"test:node": "node ./scripts/node-smoke.mjs",
|
|
40
|
+
"test:browser": "node ./scripts/browser-smoke.mjs",
|
|
41
|
+
"prepack": "npm run check:version && npm run build"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"esbuild": "^0.25.1"
|
|
45
|
+
}
|
|
46
|
+
}
|