@introspection-ai/recipes 0.53.0 → 0.55.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/connector-tools.d.ts +1 -1
- package/dist/connector-tools.d.ts.map +1 -1
- package/dist/connector-tools.js +16 -0
- package/dist/connector-tools.js.map +1 -1
- package/dist/documents/arguments.d.ts +17 -0
- package/dist/documents/arguments.d.ts.map +1 -1
- package/dist/documents/arguments.generated.d.ts +2 -1
- package/dist/documents/arguments.generated.d.ts.map +1 -1
- package/dist/documents/arguments.generated.js +22 -0
- package/dist/documents/arguments.generated.js.map +1 -1
- package/dist/documents/index.d.ts +1 -1
- package/dist/documents/index.d.ts.map +1 -1
- package/dist/documents/module.d.ts.map +1 -1
- package/dist/documents/module.js +2 -0
- package/dist/documents/module.js.map +1 -1
- package/dist/documents/tools.d.ts +1 -1
- package/dist/documents/tools.d.ts.map +1 -1
- package/dist/documents/tools.js +71 -6
- package/dist/documents/tools.js.map +1 -1
- package/dist/documents/types.d.ts +96 -0
- package/dist/documents/types.d.ts.map +1 -1
- package/dist/interactions.d.ts +6 -0
- package/dist/interactions.d.ts.map +1 -1
- package/dist/interactions.js +9 -0
- package/dist/interactions.js.map +1 -1
- package/dist/introspection-open.d.ts +2 -2
- package/dist/introspection-open.d.ts.map +1 -1
- package/dist/introspection-open.js +27 -9
- package/dist/introspection-open.js.map +1 -1
- package/dist/issues/index.d.ts +1 -1
- package/dist/issues/index.d.ts.map +1 -1
- package/dist/issues/index.js +1 -1
- package/dist/issues/index.js.map +1 -1
- package/dist/issues/module.d.ts +3 -0
- package/dist/issues/module.d.ts.map +1 -1
- package/dist/issues/module.js +4 -0
- package/dist/issues/module.js.map +1 -1
- package/dist/mcp/cli/command-context.d.ts +13 -0
- package/dist/mcp/cli/command-context.d.ts.map +1 -1
- package/dist/mcp/cli/command-context.js +56 -16
- package/dist/mcp/cli/command-context.js.map +1 -1
- package/dist/mcp/cli/core.d.ts +1 -1
- package/dist/mcp/cli/core.d.ts.map +1 -1
- package/dist/mcp/cli/core.js +25 -16
- package/dist/mcp/cli/core.js.map +1 -1
- package/dist/mcp/client-entry.js +1 -1
- package/dist/mcp/client-entry.js.map +1 -1
- package/dist/mcp/daemon/client.d.ts +10 -1
- package/dist/mcp/daemon/client.d.ts.map +1 -1
- package/dist/mcp/daemon/client.js +41 -17
- package/dist/mcp/daemon/client.js.map +1 -1
- package/dist/mcp/daemon/hosted.d.ts +22 -0
- package/dist/mcp/daemon/hosted.d.ts.map +1 -0
- package/dist/mcp/daemon/hosted.js +98 -0
- package/dist/mcp/daemon/hosted.js.map +1 -0
- package/dist/mcp/daemon/index.js +18 -423
- package/dist/mcp/daemon/index.js.map +1 -1
- package/dist/mcp/daemon/protocol.d.ts +4 -0
- package/dist/mcp/daemon/protocol.d.ts.map +1 -1
- package/dist/mcp/daemon/protocol.js +10 -0
- package/dist/mcp/daemon/protocol.js.map +1 -1
- package/dist/mcp/daemon/server.d.ts +41 -0
- package/dist/mcp/daemon/server.d.ts.map +1 -0
- package/dist/mcp/daemon/server.js +547 -0
- package/dist/mcp/daemon/server.js.map +1 -0
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +5 -3
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/pi-runtime.d.ts +4 -2
- package/dist/mcp/pi-runtime.d.ts.map +1 -1
- package/dist/mcp/pi-runtime.js +12 -6
- package/dist/mcp/pi-runtime.js.map +1 -1
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +16 -5
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp-chunks/{chunk-QUDOU7MQ.js → chunk-757ZF552.js} +1 -1
- package/dist/mcp-chunks/chunk-AJDK5Z7O.js +546 -0
- package/dist/mcp-chunks/{chunk-T5BPTC3T.js → chunk-KZGUH7E2.js} +90 -45
- package/dist/mcp-chunks/{chunk-IMG2JMBS.js → chunk-MYAMG6VL.js} +1 -1
- package/dist/mcp-chunks/{chunk-LQKMNWDE.js → chunk-OIK5AH2S.js} +5 -5
- package/dist/mcp-chunks/{chunk-3UVHZECT.js → chunk-OWGTNJNR.js} +9 -7
- package/dist/mcp-chunks/chunk-TUPFPO4L.js +400 -0
- package/dist/mcp-chunks/{cli-BM23ZXPJ.js → cli-UQ5QXFXU.js} +31 -31
- package/dist/mcp-chunks/{client-RKTAKDAY.js → client-SBELHRAE.js} +3 -3
- package/dist/mcp-chunks/{daemon-command-VU6WTFCJ.js → daemon-command-MQS6AP2W.js} +5 -5
- package/dist/mcp-chunks/dist-SDPPH7XQ.js +65 -0
- package/dist/mcp-chunks/{generate-cli-runner-JAAPSWGX.js → generate-cli-runner-LQBBQ7F5.js} +3 -3
- package/dist/mcp-chunks/{generated-daemon-runtime-LX4L2X7Z.js → generated-daemon-runtime-TUCZ3JEP.js} +4 -4
- package/dist/mcp-chunks/{pi-mcp-runtime-GHDEPXUL.js → pi-mcp-runtime-OLO25D7A.js} +1 -29
- package/dist/mcp-chunks/{runtime-T63YI7TC.js → runtime-5SOGEUTQ.js} +2 -2
- package/dist/mcp-chunks/{serve-command-75P4VBJ6.js → serve-command-YKSIXG7K.js} +3 -3
- package/dist/mcp-chunks/server-HQBFWAJR.js +41 -0
- package/dist/mcp-daemon.js +29 -432
- package/dist/mcp-run-worker.js +9 -8
- package/dist/open.d.ts +9 -2
- package/dist/open.d.ts.map +1 -1
- package/dist/open.js +31 -3
- package/dist/open.js.map +1 -1
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +1 -1
- package/dist/pi-extension.js.map +1 -1
- package/dist/recipe/companions.d.ts +6 -0
- package/dist/recipe/companions.d.ts.map +1 -1
- package/dist/recipe/companions.js +22 -4
- package/dist/recipe/companions.js.map +1 -1
- package/dist/recipe/manifest.generated.d.ts +2 -1
- package/dist/recipe/manifest.generated.d.ts.map +1 -1
- package/dist/recipe/manifest.generated.js +3 -2
- package/dist/recipe/manifest.generated.js.map +1 -1
- package/dist/recipe/package.d.ts +12 -0
- package/dist/recipe/package.d.ts.map +1 -1
- package/dist/recipe/package.js +32 -0
- package/dist/recipe/package.js.map +1 -1
- package/dist/recipe/suggestions.d.ts +15 -7
- package/dist/recipe/suggestions.d.ts.map +1 -1
- package/dist/recipe/suggestions.js +33 -11
- package/dist/recipe/suggestions.js.map +1 -1
- package/dist/signature/arguments.d.ts +78 -0
- package/dist/signature/arguments.d.ts.map +1 -0
- package/dist/signature/arguments.generated.d.ts +9 -0
- package/dist/signature/arguments.generated.d.ts.map +1 -0
- package/dist/signature/arguments.generated.js +129 -0
- package/dist/signature/arguments.generated.js.map +1 -0
- package/dist/signature/arguments.js +8 -0
- package/dist/signature/arguments.js.map +1 -0
- package/dist/signature/docx.d.ts +41 -0
- package/dist/signature/docx.d.ts.map +1 -0
- package/dist/signature/docx.js +449 -0
- package/dist/signature/docx.js.map +1 -0
- package/dist/signature/index.d.ts +9 -0
- package/dist/signature/index.d.ts.map +1 -0
- package/dist/signature/index.js +5 -0
- package/dist/signature/index.js.map +1 -0
- package/dist/signature/module.d.ts +39 -0
- package/dist/signature/module.d.ts.map +1 -0
- package/dist/signature/module.js +111 -0
- package/dist/signature/module.js.map +1 -0
- package/dist/signature/tool.d.ts +137 -0
- package/dist/signature/tool.d.ts.map +1 -0
- package/dist/signature/tool.js +677 -0
- package/dist/signature/tool.js.map +1 -0
- package/dist/signature/types.d.ts +259 -0
- package/dist/signature/types.d.ts.map +1 -0
- package/dist/signature/types.js +69 -0
- package/dist/signature/types.js.map +1 -0
- package/dist/suggestion-picker.d.ts +2 -2
- package/dist/suggestion-picker.d.ts.map +1 -1
- package/dist/suggestion-picker.js +3 -3
- package/dist/suggestion-picker.js.map +1 -1
- package/docs/README.md +2 -1
- package/docs/documents/README.md +152 -6
- package/docs/interactions.md +20 -15
- package/docs/issues/README.md +6 -1
- package/docs/mcp-configuration.md +11 -3
- package/docs/providers.md +3 -1
- package/docs/recipe-format.md +30 -13
- package/docs/signature/README.md +311 -0
- package/docs/signature/docusign.md +139 -0
- package/docs/signature/dropbox-sign.md +76 -0
- package/docs/signature/pandadoc.md +76 -0
- package/package.json +10 -5
- package/skills/documents/SKILL.md +17 -1
- package/skills/issues/SKILL.md +108 -0
- package/skills/signature/SKILL.md +97 -0
- package/dist/mcp-chunks/{auth-command-UYFOGGQR.js → auth-command-PZLSOYAT.js} +6 -6
- package/dist/mcp-chunks/{call-command-6U5BNDR7.js → call-command-VILJIFYG.js} +6 -6
- package/dist/mcp-chunks/{chunk-36D4FILJ.js → chunk-ORJND4AI.js} +3 -3
- package/dist/mcp-chunks/{resource-command-DUY4SVE5.js → resource-command-SRFQQROJ.js} +4 -4
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
export const SIGNATURE_ARGUMENTS = {
|
|
2
|
+
prepare: {
|
|
3
|
+
type: "object",
|
|
4
|
+
properties: {
|
|
5
|
+
path: {
|
|
6
|
+
type: "string",
|
|
7
|
+
description: "The final .docx under outputs/, with no tracked changes or comments",
|
|
8
|
+
minLength: 1,
|
|
9
|
+
},
|
|
10
|
+
subject: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "The email subject signers see",
|
|
13
|
+
minLength: 1,
|
|
14
|
+
maxLength: 100,
|
|
15
|
+
},
|
|
16
|
+
message: {
|
|
17
|
+
type: "string",
|
|
18
|
+
description: "A short note in the email",
|
|
19
|
+
maxLength: 2000,
|
|
20
|
+
},
|
|
21
|
+
signers: {
|
|
22
|
+
type: "array",
|
|
23
|
+
items: {
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
name: {
|
|
27
|
+
type: "string",
|
|
28
|
+
description: "Their full name, exactly as on their Name: line for a signer",
|
|
29
|
+
minLength: 1,
|
|
30
|
+
},
|
|
31
|
+
email: {
|
|
32
|
+
type: "string",
|
|
33
|
+
description: "Their email address",
|
|
34
|
+
minLength: 3,
|
|
35
|
+
},
|
|
36
|
+
order: {
|
|
37
|
+
type: "integer",
|
|
38
|
+
description: "1 goes first; equal numbers sign at the same time",
|
|
39
|
+
minimum: 1,
|
|
40
|
+
maximum: 99,
|
|
41
|
+
default: 1,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
required: ["name", "email"],
|
|
45
|
+
additionalProperties: false,
|
|
46
|
+
description: "Someone who signs or receives a copy",
|
|
47
|
+
},
|
|
48
|
+
description: "Who signs, each found by their Name: line in the signature block",
|
|
49
|
+
minItems: 1,
|
|
50
|
+
maxItems: 10,
|
|
51
|
+
},
|
|
52
|
+
cc: {
|
|
53
|
+
type: "array",
|
|
54
|
+
items: {
|
|
55
|
+
type: "object",
|
|
56
|
+
properties: {
|
|
57
|
+
name: {
|
|
58
|
+
type: "string",
|
|
59
|
+
description: "Their full name, exactly as on their Name: line for a signer",
|
|
60
|
+
minLength: 1,
|
|
61
|
+
},
|
|
62
|
+
email: {
|
|
63
|
+
type: "string",
|
|
64
|
+
description: "Their email address",
|
|
65
|
+
minLength: 3,
|
|
66
|
+
},
|
|
67
|
+
order: {
|
|
68
|
+
type: "integer",
|
|
69
|
+
description: "1 goes first; equal numbers sign at the same time",
|
|
70
|
+
minimum: 1,
|
|
71
|
+
maximum: 99,
|
|
72
|
+
default: 1,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
required: ["name", "email"],
|
|
76
|
+
additionalProperties: false,
|
|
77
|
+
description: "Someone who signs or receives a copy",
|
|
78
|
+
},
|
|
79
|
+
description: "Who receives a copy once the signers before them have signed",
|
|
80
|
+
maxItems: 10,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
required: ["path", "subject", "signers"],
|
|
84
|
+
additionalProperties: false,
|
|
85
|
+
},
|
|
86
|
+
send: {
|
|
87
|
+
type: "object",
|
|
88
|
+
properties: {
|
|
89
|
+
request: {
|
|
90
|
+
type: "string",
|
|
91
|
+
description: "The request's handle, sig_…, as prepare returned it",
|
|
92
|
+
minLength: 1,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
required: ["request"],
|
|
96
|
+
additionalProperties: false,
|
|
97
|
+
},
|
|
98
|
+
status: {
|
|
99
|
+
type: "object",
|
|
100
|
+
properties: {
|
|
101
|
+
request: {
|
|
102
|
+
type: "string",
|
|
103
|
+
description: "The request's handle, sig_…, as prepare returned it",
|
|
104
|
+
minLength: 1,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
required: ["request"],
|
|
108
|
+
additionalProperties: false,
|
|
109
|
+
},
|
|
110
|
+
void: {
|
|
111
|
+
type: "object",
|
|
112
|
+
properties: {
|
|
113
|
+
request: {
|
|
114
|
+
type: "string",
|
|
115
|
+
description: "The request's handle, sig_…, as prepare returned it",
|
|
116
|
+
minLength: 1,
|
|
117
|
+
},
|
|
118
|
+
reason: {
|
|
119
|
+
type: "string",
|
|
120
|
+
description: "Why it is cancelled; the signers see it",
|
|
121
|
+
minLength: 1,
|
|
122
|
+
maxLength: 200,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
required: ["request", "reason"],
|
|
126
|
+
additionalProperties: false,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=arguments.generated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arguments.generated.js","sourceRoot":"","sources":["../../src/signature/arguments.generated.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arguments each `signature` command takes, as the model sees them.
|
|
3
|
+
* `scripts/generate-tool-schemas.mjs` turns each `Signature<Command>Arguments`
|
|
4
|
+
* type into the JSON Schema in `arguments.generated.ts`: a field's doc comment
|
|
5
|
+
* is its description and its tags are its limits.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=arguments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arguments.js","sourceRoot":"","sources":["../../src/signature/arguments.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks and preparation `signature create` runs on a `.docx` before
|
|
3
|
+
* anything leaves the sandbox (`docs/signature/README.md`): no pending
|
|
4
|
+
* tracked changes, no comments, and each signer's own `By:` and `Date:` lines
|
|
5
|
+
* found and given an anchor of their own in the copy that is sent.
|
|
6
|
+
*
|
|
7
|
+
* A `.docx` is a zip of XML parts. Only `word/document.xml` is rewritten; every
|
|
8
|
+
* other entry is copied byte for byte, compressed as it was.
|
|
9
|
+
*/
|
|
10
|
+
/** Build a `.docx` (or any zip) from its parts, for tests and fixtures. */
|
|
11
|
+
export declare function zipParts(parts: Readonly<Record<string, string>>): Uint8Array;
|
|
12
|
+
export interface DocxReadiness {
|
|
13
|
+
/** Tracked changes still pending, across every content part. */
|
|
14
|
+
trackedChanges: number;
|
|
15
|
+
/** Comments in the file, open or resolved. */
|
|
16
|
+
comments: number;
|
|
17
|
+
}
|
|
18
|
+
/** What `create` would refuse in this file: its tracked changes and comments. */
|
|
19
|
+
export declare function docxReadiness(bytes: Uint8Array): DocxReadiness;
|
|
20
|
+
export interface DocxSigner {
|
|
21
|
+
name: string;
|
|
22
|
+
}
|
|
23
|
+
export interface PreparedDocx {
|
|
24
|
+
/** The copy to send: the file with each signer's anchors added. */
|
|
25
|
+
bytes: Uint8Array;
|
|
26
|
+
/** Per signer, in the order given. */
|
|
27
|
+
placements: Array<{
|
|
28
|
+
sign: string;
|
|
29
|
+
date?: string;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Check a final `.docx` and prepare the copy to send: refuses one with
|
|
34
|
+
* pending tracked changes or comments, finds each signer's `By:` and `Date:`
|
|
35
|
+
* lines from their `Name:` line, and appends an anchor that occurs once
|
|
36
|
+
* (`\s1\`, `\d1\`, …) to each.
|
|
37
|
+
*/
|
|
38
|
+
export declare function prepareDocx(bytes: Uint8Array, signers: readonly DocxSigner[]): PreparedDocx;
|
|
39
|
+
/** The text of every paragraph in the main document, for tests and diagnostics. */
|
|
40
|
+
export declare function docxParagraphs(bytes: Uint8Array): string[];
|
|
41
|
+
//# sourceMappingURL=docx.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docx.d.ts","sourceRoot":"","sources":["../../src/signature/docx.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAwJH,2EAA2E;AAC3E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,UAAU,CAmB5E;AAiGD,MAAM,WAAW,aAAa;IAC5B,gEAAgE;IAChE,cAAc,EAAE,MAAM,CAAC;IACvB,8CAA8C;IAC9C,QAAQ,EAAE,MAAM,CAAC;CAClB;AA2DD,iFAAiF;AACjF,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa,CAE9D;AAwDD,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;CACd;AAuID,MAAM,WAAW,YAAY;IAC3B,mEAAmE;IACnE,KAAK,EAAE,UAAU,CAAC;IAClB,sCAAsC;IACtC,UAAU,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACpD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,SAAS,UAAU,EAAE,GAC7B,YAAY,CAoDd;AAED,mFAAmF;AACnF,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,EAAE,CAG1D"}
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks and preparation `signature create` runs on a `.docx` before
|
|
3
|
+
* anything leaves the sandbox (`docs/signature/README.md`): no pending
|
|
4
|
+
* tracked changes, no comments, and each signer's own `By:` and `Date:` lines
|
|
5
|
+
* found and given an anchor of their own in the copy that is sent.
|
|
6
|
+
*
|
|
7
|
+
* A `.docx` is a zip of XML parts. Only `word/document.xml` is rewritten; every
|
|
8
|
+
* other entry is copied byte for byte, compressed as it was.
|
|
9
|
+
*/
|
|
10
|
+
import { crc32, deflateRawSync, inflateRawSync } from "node:zlib";
|
|
11
|
+
const EOCD = 0x06054b50;
|
|
12
|
+
const CENTRAL = 0x02014b50;
|
|
13
|
+
const LOCAL = 0x04034b50;
|
|
14
|
+
const STORED = 0;
|
|
15
|
+
const DEFLATED = 8;
|
|
16
|
+
/** The archive's entries, in central-directory order; undefined when it is not a zip this reader takes. */
|
|
17
|
+
function readZip(bytes) {
|
|
18
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
19
|
+
let eocd = -1;
|
|
20
|
+
for (let at = bytes.length - 22; at >= Math.max(0, bytes.length - 22 - 0xffff); at -= 1) {
|
|
21
|
+
if (view.getUint32(at, true) === EOCD) {
|
|
22
|
+
eocd = at;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (eocd < 0)
|
|
27
|
+
return undefined;
|
|
28
|
+
const count = view.getUint16(eocd + 10, true);
|
|
29
|
+
let at = view.getUint32(eocd + 16, true);
|
|
30
|
+
if (at === 0xffffffff)
|
|
31
|
+
return undefined;
|
|
32
|
+
const entries = [];
|
|
33
|
+
for (let index = 0; index < count; index += 1) {
|
|
34
|
+
if (at + 46 > bytes.length || view.getUint32(at, true) !== CENTRAL)
|
|
35
|
+
return undefined;
|
|
36
|
+
const flags = view.getUint16(at + 8, true);
|
|
37
|
+
const method = view.getUint16(at + 10, true);
|
|
38
|
+
const time = view.getUint16(at + 12, true);
|
|
39
|
+
const date = view.getUint16(at + 14, true);
|
|
40
|
+
const crc = view.getUint32(at + 16, true);
|
|
41
|
+
const compressedSize = view.getUint32(at + 20, true);
|
|
42
|
+
const size = view.getUint32(at + 24, true);
|
|
43
|
+
const nameLength = view.getUint16(at + 28, true);
|
|
44
|
+
const extraLength = view.getUint16(at + 30, true);
|
|
45
|
+
const commentLength = view.getUint16(at + 32, true);
|
|
46
|
+
const local = view.getUint32(at + 42, true);
|
|
47
|
+
if (compressedSize === 0xffffffff || local === 0xffffffff)
|
|
48
|
+
return undefined;
|
|
49
|
+
const name = new TextDecoder().decode(bytes.subarray(at + 46, at + 46 + nameLength));
|
|
50
|
+
if (view.getUint32(local, true) !== LOCAL)
|
|
51
|
+
return undefined;
|
|
52
|
+
const start = local +
|
|
53
|
+
30 +
|
|
54
|
+
view.getUint16(local + 26, true) +
|
|
55
|
+
view.getUint16(local + 28, true);
|
|
56
|
+
entries.push({
|
|
57
|
+
name,
|
|
58
|
+
method,
|
|
59
|
+
flags,
|
|
60
|
+
crc,
|
|
61
|
+
compressedSize,
|
|
62
|
+
size,
|
|
63
|
+
time,
|
|
64
|
+
date,
|
|
65
|
+
data: bytes.subarray(start, start + compressedSize),
|
|
66
|
+
});
|
|
67
|
+
at += 46 + nameLength + extraLength + commentLength;
|
|
68
|
+
}
|
|
69
|
+
return entries;
|
|
70
|
+
}
|
|
71
|
+
function entryBytes(entry) {
|
|
72
|
+
if (entry.method === STORED)
|
|
73
|
+
return entry.data;
|
|
74
|
+
if (entry.method === DEFLATED)
|
|
75
|
+
return inflateRawSync(entry.data);
|
|
76
|
+
throw new Error(`${entry.name} uses zip compression method ${entry.method}, which is not supported`);
|
|
77
|
+
}
|
|
78
|
+
function deflatedEntry(base, content) {
|
|
79
|
+
return {
|
|
80
|
+
...base,
|
|
81
|
+
method: DEFLATED,
|
|
82
|
+
// Bit 3 says sizes follow the data; this writer puts them in the header.
|
|
83
|
+
flags: base.flags & ~0x0008,
|
|
84
|
+
crc: crc32(content),
|
|
85
|
+
size: content.length,
|
|
86
|
+
data: deflateRawSync(content),
|
|
87
|
+
compressedSize: 0,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function writeZip(entries) {
|
|
91
|
+
const chunks = [];
|
|
92
|
+
const central = [];
|
|
93
|
+
let offset = 0;
|
|
94
|
+
for (const entry of entries) {
|
|
95
|
+
const name = new TextEncoder().encode(entry.name);
|
|
96
|
+
const size = entry.data.length;
|
|
97
|
+
const local = new DataView(new ArrayBuffer(30));
|
|
98
|
+
local.setUint32(0, LOCAL, true);
|
|
99
|
+
local.setUint16(4, 20, true);
|
|
100
|
+
local.setUint16(6, entry.flags & ~0x0008, true);
|
|
101
|
+
local.setUint16(8, entry.method, true);
|
|
102
|
+
local.setUint16(10, entry.time, true);
|
|
103
|
+
local.setUint16(12, entry.date, true);
|
|
104
|
+
local.setUint32(14, entry.crc, true);
|
|
105
|
+
local.setUint32(18, size, true);
|
|
106
|
+
local.setUint32(22, entry.size, true);
|
|
107
|
+
local.setUint16(26, name.length, true);
|
|
108
|
+
local.setUint16(28, 0, true);
|
|
109
|
+
chunks.push(new Uint8Array(local.buffer), name, entry.data);
|
|
110
|
+
const record = new DataView(new ArrayBuffer(46));
|
|
111
|
+
record.setUint32(0, CENTRAL, true);
|
|
112
|
+
record.setUint16(4, 20, true);
|
|
113
|
+
record.setUint16(6, 20, true);
|
|
114
|
+
record.setUint16(8, entry.flags & ~0x0008, true);
|
|
115
|
+
record.setUint16(10, entry.method, true);
|
|
116
|
+
record.setUint16(12, entry.time, true);
|
|
117
|
+
record.setUint16(14, entry.date, true);
|
|
118
|
+
record.setUint32(16, entry.crc, true);
|
|
119
|
+
record.setUint32(20, size, true);
|
|
120
|
+
record.setUint32(24, entry.size, true);
|
|
121
|
+
record.setUint16(28, name.length, true);
|
|
122
|
+
record.setUint32(42, offset, true);
|
|
123
|
+
central.push(new Uint8Array(record.buffer), name);
|
|
124
|
+
offset += 30 + name.length + size;
|
|
125
|
+
}
|
|
126
|
+
const centralSize = central.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
127
|
+
const end = new DataView(new ArrayBuffer(22));
|
|
128
|
+
end.setUint32(0, EOCD, true);
|
|
129
|
+
end.setUint16(8, entries.length, true);
|
|
130
|
+
end.setUint16(10, entries.length, true);
|
|
131
|
+
end.setUint32(12, centralSize, true);
|
|
132
|
+
end.setUint32(16, offset, true);
|
|
133
|
+
return Buffer.concat([...chunks, ...central, new Uint8Array(end.buffer)]);
|
|
134
|
+
}
|
|
135
|
+
/** Build a `.docx` (or any zip) from its parts, for tests and fixtures. */
|
|
136
|
+
export function zipParts(parts) {
|
|
137
|
+
return writeZip(Object.entries(parts).map(([name, text]) => deflatedEntry({
|
|
138
|
+
name,
|
|
139
|
+
method: DEFLATED,
|
|
140
|
+
flags: 0x0800,
|
|
141
|
+
crc: 0,
|
|
142
|
+
compressedSize: 0,
|
|
143
|
+
size: 0,
|
|
144
|
+
time: 0,
|
|
145
|
+
date: 0x21,
|
|
146
|
+
data: new Uint8Array(),
|
|
147
|
+
}, new TextEncoder().encode(text))));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The outermost `<name>` elements in `xml[from, to)`, by tag depth. A tag
|
|
151
|
+
* matches only on its whole name, so `w:p` never matches `w:pPr`.
|
|
152
|
+
*/
|
|
153
|
+
function elements(xml, name, from = 0, to = xml.length) {
|
|
154
|
+
const pattern = new RegExp(`<(/?)${name}(?=[\\s>/])[^>]*?(/?)>`, "g");
|
|
155
|
+
pattern.lastIndex = from;
|
|
156
|
+
const spans = [];
|
|
157
|
+
let depth = 0;
|
|
158
|
+
let start = -1;
|
|
159
|
+
for (let match = pattern.exec(xml); match && match.index < to; match = pattern.exec(xml)) {
|
|
160
|
+
const closing = match[1] === "/";
|
|
161
|
+
const selfClosing = match[2] === "/";
|
|
162
|
+
if (closing) {
|
|
163
|
+
depth -= 1;
|
|
164
|
+
if (depth === 0 && start >= 0) {
|
|
165
|
+
spans.push({ start, end: match.index + match[0].length });
|
|
166
|
+
start = -1;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else if (selfClosing) {
|
|
170
|
+
if (depth === 0)
|
|
171
|
+
spans.push({ start: match.index, end: match.index + match[0].length });
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
if (depth === 0)
|
|
175
|
+
start = match.index;
|
|
176
|
+
depth += 1;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return spans;
|
|
180
|
+
}
|
|
181
|
+
const ENTITIES = {
|
|
182
|
+
amp: "&",
|
|
183
|
+
lt: "<",
|
|
184
|
+
gt: ">",
|
|
185
|
+
quot: '"',
|
|
186
|
+
apos: "'",
|
|
187
|
+
};
|
|
188
|
+
function decode(text) {
|
|
189
|
+
return text.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (whole, entity) => {
|
|
190
|
+
if (entity.startsWith("#x") || entity.startsWith("#X"))
|
|
191
|
+
return String.fromCodePoint(Number.parseInt(entity.slice(2), 16));
|
|
192
|
+
if (entity.startsWith("#"))
|
|
193
|
+
return String.fromCodePoint(Number.parseInt(entity.slice(1), 10));
|
|
194
|
+
return ENTITIES[entity] ?? whole;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/** The text a paragraph shows: its `w:t` runs and tabs, without deleted text. */
|
|
198
|
+
function textOf(xml, span) {
|
|
199
|
+
const part = xml.slice(span.start, span.end);
|
|
200
|
+
let text = "";
|
|
201
|
+
for (const match of part.matchAll(/<w:t(?:\s[^>]*)?>([^<]*)<\/w:t>|<w:tab\s*\/>/g))
|
|
202
|
+
text += match[1] === undefined ? "\t" : decode(match[1]);
|
|
203
|
+
return text;
|
|
204
|
+
}
|
|
205
|
+
/** Folded for matching: whitespace collapsed, non-breaking spaces as spaces, lower case. */
|
|
206
|
+
function fold(text) {
|
|
207
|
+
return text
|
|
208
|
+
.replace(/[\s\u00a0\u2007\u202f]+/g, " ")
|
|
209
|
+
.trim()
|
|
210
|
+
.toLowerCase();
|
|
211
|
+
}
|
|
212
|
+
// ---------------------------------------------------------------------------
|
|
213
|
+
// Readiness
|
|
214
|
+
/** The parts that can carry tracked changes. */
|
|
215
|
+
const CONTENT_PART = /^word\/(document|header\d*|footer\d*|footnotes|endnotes)\.xml$/;
|
|
216
|
+
/** Revision marks: insertions, deletions, moves, and property and table changes. */
|
|
217
|
+
const REVISION = /<w:(ins|del|moveFrom|moveTo|cellIns|cellDel|cellMerge|rPrChange|pPrChange|sectPrChange|tblPrChange|tblGridChange|trPrChange|tcPrChange|tblPrExChange|numberingChange)(?=[\s>/])/g;
|
|
218
|
+
/** A document `create` refuses; it carries the family's error, as a provider's does. */
|
|
219
|
+
class DocxError extends Error {
|
|
220
|
+
code;
|
|
221
|
+
signature;
|
|
222
|
+
constructor(code, message) {
|
|
223
|
+
super(message);
|
|
224
|
+
this.code = code;
|
|
225
|
+
this.name = "DocxError";
|
|
226
|
+
this.signature = { code, message };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function openDocx(bytes) {
|
|
230
|
+
const entries = readZip(bytes);
|
|
231
|
+
if (!entries || !entries.some((entry) => entry.name === "word/document.xml"))
|
|
232
|
+
throw new DocxError("not_ready", "The file is not a Word document (.docx): it has no word/document.xml.");
|
|
233
|
+
const cache = new Map();
|
|
234
|
+
return {
|
|
235
|
+
entries,
|
|
236
|
+
part(name) {
|
|
237
|
+
if (!cache.has(name)) {
|
|
238
|
+
const entry = entries.find((candidate) => candidate.name === name);
|
|
239
|
+
if (!entry)
|
|
240
|
+
return undefined;
|
|
241
|
+
cache.set(name, new TextDecoder().decode(entryBytes(entry)));
|
|
242
|
+
}
|
|
243
|
+
return cache.get(name);
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
function readiness(docx) {
|
|
248
|
+
let trackedChanges = 0;
|
|
249
|
+
for (const entry of docx.entries) {
|
|
250
|
+
if (!CONTENT_PART.test(entry.name))
|
|
251
|
+
continue;
|
|
252
|
+
trackedChanges += docx.part(entry.name).match(REVISION)?.length ?? 0;
|
|
253
|
+
}
|
|
254
|
+
const commentsPart = docx.part("word/comments.xml") ?? "";
|
|
255
|
+
const comments = Math.max(commentsPart.match(/<w:comment(?=[\s>/])/g)?.length ?? 0, docx.part("word/document.xml").match(/<w:commentReference(?=[\s>/])/g)
|
|
256
|
+
?.length ?? 0);
|
|
257
|
+
return { trackedChanges, comments };
|
|
258
|
+
}
|
|
259
|
+
/** What `create` would refuse in this file: its tracked changes and comments. */
|
|
260
|
+
export function docxReadiness(bytes) {
|
|
261
|
+
return readiness(openDocx(bytes));
|
|
262
|
+
}
|
|
263
|
+
function tablesIn(xml, from, to) {
|
|
264
|
+
const tables = [];
|
|
265
|
+
for (const table of elements(xml, "w:tbl", from, to)) {
|
|
266
|
+
const rows = [];
|
|
267
|
+
for (const row of elements(xml, "w:tr", table.start, table.end)) {
|
|
268
|
+
const cells = [];
|
|
269
|
+
let column = 0;
|
|
270
|
+
for (const cell of elements(xml, "w:tc", row.start, row.end)) {
|
|
271
|
+
const nested = elements(xml, "w:tbl", cell.start, cell.end);
|
|
272
|
+
const paragraphs = elements(xml, "w:p", cell.start, cell.end).filter((paragraph) => !nested.some((inner) => paragraph.start >= inner.start && paragraph.end <= inner.end));
|
|
273
|
+
cells.push({ column, paragraphs });
|
|
274
|
+
const properties = xml.slice(cell.start, cell.end);
|
|
275
|
+
const span = /<w:gridSpan\s+w:val="(\d+)"/.exec(properties);
|
|
276
|
+
column += span ? Number(span[1]) : 1;
|
|
277
|
+
tables.push(...tablesIn(xml, cell.start + 1, cell.end));
|
|
278
|
+
}
|
|
279
|
+
rows.push(cells);
|
|
280
|
+
}
|
|
281
|
+
tables.push({ rows });
|
|
282
|
+
}
|
|
283
|
+
return tables;
|
|
284
|
+
}
|
|
285
|
+
const labelled = (label) => (text) => new RegExp(`^${label}\\s*:`).test(fold(text));
|
|
286
|
+
const isBy = labelled("by");
|
|
287
|
+
const isDate = labelled("date");
|
|
288
|
+
/** The signer's name on a `Name:` line, folded; undefined for any other line. */
|
|
289
|
+
function nameLine(text) {
|
|
290
|
+
const match = /^name\s*:\s*(.+)$/.exec(fold(text));
|
|
291
|
+
return match?.[1]?.trim();
|
|
292
|
+
}
|
|
293
|
+
function locate(xml, tables, signer) {
|
|
294
|
+
const wanted = fold(signer.name);
|
|
295
|
+
const found = [];
|
|
296
|
+
for (const table of tables)
|
|
297
|
+
table.rows.forEach((cells, row) => {
|
|
298
|
+
for (const cell of cells)
|
|
299
|
+
if (cell.paragraphs.some((paragraph) => nameLine(textOf(xml, paragraph)) === wanted))
|
|
300
|
+
found.push({ table, row, cell });
|
|
301
|
+
});
|
|
302
|
+
if (found.length === 0)
|
|
303
|
+
throw new DocxError("anchor_not_found", `No "Name: ${signer.name}" line in a table of the document. Fill in the signer's name on their side of the signature block, spelled as given here.`);
|
|
304
|
+
if (found.length > 1)
|
|
305
|
+
throw new DocxError("ambiguous", `"Name: ${signer.name}" is on ${found.length} lines of the signature block, so their side cannot be told apart.`);
|
|
306
|
+
const { table, row, cell } = found[0];
|
|
307
|
+
const inCell = (test, of) => of.paragraphs.find((paragraph) => test(textOf(xml, paragraph)));
|
|
308
|
+
// One cell holding every line, or one line per row in the signer's column.
|
|
309
|
+
const nearest = (test, prefer) => {
|
|
310
|
+
const own = inCell(test, cell);
|
|
311
|
+
if (own)
|
|
312
|
+
return own;
|
|
313
|
+
const order = table.rows
|
|
314
|
+
.map((cells, index) => ({ cells, index }))
|
|
315
|
+
.filter(({ index }) => index !== row)
|
|
316
|
+
.sort((a, b) => Math.abs(a.index - row) - Math.abs(b.index - row) ||
|
|
317
|
+
(Math.sign(a.index - row) === prefer ? -1 : 1));
|
|
318
|
+
for (const { cells } of order) {
|
|
319
|
+
const same = cells.find((candidate) => candidate.column === cell.column);
|
|
320
|
+
const paragraph = same && inCell(test, same);
|
|
321
|
+
if (paragraph)
|
|
322
|
+
return paragraph;
|
|
323
|
+
}
|
|
324
|
+
return undefined;
|
|
325
|
+
};
|
|
326
|
+
const sign = nearest(isBy, -1);
|
|
327
|
+
if (!sign)
|
|
328
|
+
throw new DocxError("anchor_not_found", `No "By:" line on ${signer.name}'s side of the signature block.`);
|
|
329
|
+
const date = nearest(isDate, 1);
|
|
330
|
+
return { sign, ...(date ? { date } : {}) };
|
|
331
|
+
}
|
|
332
|
+
/** An invisible run: white, 1-point text DocuSign finds and a reader does not see. */
|
|
333
|
+
function anchorRun(anchor) {
|
|
334
|
+
return `<w:r><w:rPr><w:color w:val="FFFFFF"/><w:sz w:val="2"/><w:szCs w:val="2"/></w:rPr><w:t xml:space="preserve">${anchor}</w:t></w:r>`;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* The edit that puts a line's anchor right after its label's colon. A run
|
|
338
|
+
* holding the colon and the signature line (`By: ____`) is split in two at
|
|
339
|
+
* the colon, so the field starts where the line does; a run with more than
|
|
340
|
+
* one text node keeps its text and takes the anchor after it.
|
|
341
|
+
*/
|
|
342
|
+
function anchorEdit(xml, paragraph, anchor) {
|
|
343
|
+
let text = "";
|
|
344
|
+
for (const run of elements(xml, "w:r", paragraph.start, paragraph.end)) {
|
|
345
|
+
const own = textOf(xml, run);
|
|
346
|
+
text += own;
|
|
347
|
+
if (!text.includes(":"))
|
|
348
|
+
continue;
|
|
349
|
+
const nodes = elements(xml, "w:t", run.start, run.end);
|
|
350
|
+
const node = nodes[0];
|
|
351
|
+
const nodeXml = node ? xml.slice(node.start, node.end) : "";
|
|
352
|
+
const inner = nodeXml.slice(nodeXml.indexOf(">") + 1, nodeXml.lastIndexOf("</w:t>"));
|
|
353
|
+
const colon = own.indexOf(":");
|
|
354
|
+
if (nodes.length === 1 &&
|
|
355
|
+
node &&
|
|
356
|
+
decode(inner) === own &&
|
|
357
|
+
colon >= 0 &&
|
|
358
|
+
colon < own.length - 1) {
|
|
359
|
+
const open = xml.slice(run.start, node.start);
|
|
360
|
+
const close = xml.slice(node.end, run.end);
|
|
361
|
+
const cut = rawOffset(inner, colon + 1);
|
|
362
|
+
const keep = (body) => `<w:t xml:space="preserve">${body}</w:t>`;
|
|
363
|
+
return {
|
|
364
|
+
start: run.start,
|
|
365
|
+
end: run.end,
|
|
366
|
+
text: open +
|
|
367
|
+
keep(inner.slice(0, cut)) +
|
|
368
|
+
close +
|
|
369
|
+
anchorRun(anchor) +
|
|
370
|
+
open +
|
|
371
|
+
keep(inner.slice(cut)) +
|
|
372
|
+
close,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
return { start: run.end, end: run.end, text: anchorRun(anchor) };
|
|
376
|
+
}
|
|
377
|
+
const end = xml.lastIndexOf("</w:p>", paragraph.end);
|
|
378
|
+
return { start: end, end, text: anchorRun(anchor) };
|
|
379
|
+
}
|
|
380
|
+
/** The offset in escaped XML text that follows the first `chars` characters of its decoded text. */
|
|
381
|
+
function rawOffset(raw, chars) {
|
|
382
|
+
let seen = 0;
|
|
383
|
+
let index = 0;
|
|
384
|
+
while (index < raw.length && seen < chars) {
|
|
385
|
+
if (raw[index] === "&") {
|
|
386
|
+
const semi = raw.indexOf(";", index);
|
|
387
|
+
index = semi >= 0 ? semi + 1 : index + 1;
|
|
388
|
+
}
|
|
389
|
+
else
|
|
390
|
+
index += 1;
|
|
391
|
+
seen += 1;
|
|
392
|
+
}
|
|
393
|
+
return index;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Check a final `.docx` and prepare the copy to send: refuses one with
|
|
397
|
+
* pending tracked changes or comments, finds each signer's `By:` and `Date:`
|
|
398
|
+
* lines from their `Name:` line, and appends an anchor that occurs once
|
|
399
|
+
* (`\s1\`, `\d1\`, …) to each.
|
|
400
|
+
*/
|
|
401
|
+
export function prepareDocx(bytes, signers) {
|
|
402
|
+
const docx = openDocx(bytes);
|
|
403
|
+
const { trackedChanges, comments } = readiness(docx);
|
|
404
|
+
if (trackedChanges > 0 || comments > 0) {
|
|
405
|
+
const left = [
|
|
406
|
+
...(trackedChanges > 0
|
|
407
|
+
? [`${trackedChanges} tracked change${trackedChanges === 1 ? "" : "s"}`]
|
|
408
|
+
: []),
|
|
409
|
+
...(comments > 0
|
|
410
|
+
? [`${comments} comment${comments === 1 ? "" : "s"}`]
|
|
411
|
+
: []),
|
|
412
|
+
];
|
|
413
|
+
throw new DocxError("not_ready", `The document still has ${left.join(" and ")}. Resolve every tracked change (documents respond) and remove the comments before sending it for signature.`);
|
|
414
|
+
}
|
|
415
|
+
const xml = docx.part("word/document.xml");
|
|
416
|
+
const body = elements(xml, "w:body")[0] ?? { start: 0, end: xml.length };
|
|
417
|
+
const tables = tablesIn(xml, body.start, body.end);
|
|
418
|
+
const edits = [];
|
|
419
|
+
const placements = [];
|
|
420
|
+
const used = new Set();
|
|
421
|
+
signers.forEach((signer, index) => {
|
|
422
|
+
const located = locate(xml, tables, signer);
|
|
423
|
+
if (used.has(located.sign.start))
|
|
424
|
+
throw new DocxError("ambiguous", `${signer.name}'s "By:" line is another signer's too.`);
|
|
425
|
+
used.add(located.sign.start);
|
|
426
|
+
const sign = `\\s${index + 1}\\`;
|
|
427
|
+
edits.push(anchorEdit(xml, located.sign, sign));
|
|
428
|
+
let date;
|
|
429
|
+
if (located.date && !used.has(located.date.start)) {
|
|
430
|
+
used.add(located.date.start);
|
|
431
|
+
date = `\\d${index + 1}\\`;
|
|
432
|
+
edits.push(anchorEdit(xml, located.date, date));
|
|
433
|
+
}
|
|
434
|
+
placements.push({ sign, ...(date ? { date } : {}) });
|
|
435
|
+
});
|
|
436
|
+
let prepared = xml;
|
|
437
|
+
for (const { start, end, text } of [...edits].sort((a, b) => b.start - a.start))
|
|
438
|
+
prepared = prepared.slice(0, start) + text + prepared.slice(end);
|
|
439
|
+
const entries = docx.entries.map((entry) => entry.name === "word/document.xml"
|
|
440
|
+
? deflatedEntry(entry, new TextEncoder().encode(prepared))
|
|
441
|
+
: entry);
|
|
442
|
+
return { bytes: writeZip(entries), placements };
|
|
443
|
+
}
|
|
444
|
+
/** The text of every paragraph in the main document, for tests and diagnostics. */
|
|
445
|
+
export function docxParagraphs(bytes) {
|
|
446
|
+
const xml = openDocx(bytes).part("word/document.xml");
|
|
447
|
+
return elements(xml, "w:p").map((paragraph) => textOf(xml, paragraph));
|
|
448
|
+
}
|
|
449
|
+
//# sourceMappingURL=docx.js.map
|