@ansonlai/docx-redline-js 0.7.2 → 0.8.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/AGENTS.md CHANGED
@@ -11,8 +11,8 @@ or an installed plugin bundle to infer public behavior.
11
11
  | Edit or review a complete `.docx` | [Agent Fast Start](docs/AGENT_FAST_START.md) and the `docx-redline` CLI |
12
12
  | Build or update an agent skill/tool wrapper | [Skill Authoring Contract](docs/SKILL_AUTHORING.md), then [README wrapper example](README.md#example-agent-session-wrapper-development-only) |
13
13
  | Change paragraph/range reconciliation | `index.js` → `engine/oxml-engine.js` → selected `engine/*-mode.js` |
14
- | Change complete-document operations | `services/standalone-operation-runner.js` → `services/document-operation-*.js` |
15
- | Change DOCX ZIP or CLI behavior | `node/index.js`, `node/docx-document.js`, `node/cli.js` |
14
+ | Change complete-document operations | `document/docx-document.js` → `services/standalone-operation-runner.js` → `services/document-operation-*.js` |
15
+ | Change DOCX ZIP or CLI behavior | `document/zip-archive.js`, `node/index.js`, `node/docx-document.js`, `node/cli.js` |
16
16
  | Choose or add tests | Closest `tests/*.mjs`, then [Testing Guide](docs/TESTING.md) |
17
17
  | Understand ownership/dependencies | [Architecture](ARCHITECTURE.md) |
18
18
 
@@ -59,11 +59,13 @@ examples live in the
59
59
 
60
60
  ## Thin wrappers
61
61
 
62
- Wrap the CLI for shell hosts or `openDocx` from
63
- `@ansonlai/docx-redline-js/node` for byte-oriented Node hosts. A wrapper should
64
- inspect, translate narrow ergonomic inputs into canonical operations, call the
65
- facade once, and return its structured result. Do not reproduce ZIP handling,
66
- targeting, revision allocation, comments, numbering, validation, or rollback.
62
+ Wrap the CLI for shell hosts or `openDocx` from `@ansonlai/docx-redline-js`
63
+ (or `@ansonlai/docx-redline-js/bundle` for zero-dependency sandboxes, or
64
+ `@ansonlai/docx-redline-js/node` for backward-compatible Node hosts) for
65
+ byte-oriented hosts. A wrapper should inspect, translate narrow ergonomic inputs
66
+ into canonical operations, call the facade once, and return its structured result.
67
+ Do not reproduce ZIP handling, targeting, revision allocation, comments, numbering,
68
+ validation, or rollback.
67
69
 
68
70
  The stateful wrapper in `examples/agent-session-wrapper.mjs` is a testable
69
71
  development demonstration only. It is excluded from package files and exports.
@@ -74,13 +76,16 @@ not belong in an ordinary editing prompt merely because the CLI supports them.
74
76
  ## Code map
75
77
 
76
78
  ```text
77
- index.js host-independent public API
78
- core/ OOXML primitives, text views, targeting, validation
79
+ index.js host-independent public API & universal document facade
80
+ adapters/ DOM/XML parser provider with automatic pure-JS fallback
81
+ core/ OOXML primitives, text views, targeting, SHA-256, validation
82
+ document/ universal Uint8Array DOCX facade & fflate ZIP container
79
83
  pipeline/ ingestion, diffing, Markdown, lists, serialization
80
84
  engine/ reconciliation modes and run-level mutation
81
85
  orchestration/ route planning and structural conversion
82
86
  services/ document operations, comments, receipts, artifacts
83
- node/ Node-only ZIP and whole-DOCX facade/CLI
87
+ node/ Node CLI and backward-compatibility re-exports
88
+ dist/ universal ESM and zero-dependency sandbox bundles
84
89
  tests/*.mjs directly runnable suites
85
90
  ```
86
91
 
package/ARCHITECTURE.md CHANGED
@@ -8,11 +8,12 @@ This repository contains only the publishable package surface:
8
8
 
9
9
  - `adapters/`
10
10
  - `core/`
11
+ - `document/` (universal complete-DOCX facade & ZIP archive layer)
11
12
  - `engine/`
12
13
  - `pipeline/`
13
14
  - `services/`
14
15
  - `orchestration/`
15
- - `node/` (separate Node-only package facade)
16
+ - `node/` (Node CLI & backward-compatibility facade)
16
17
  - `index.js`
17
18
  - `index.d.ts`
18
19
  - `dist/`
@@ -22,8 +23,9 @@ No Word add-in entrypoints or host-specific integration layers are part of this
22
23
  ## Goals
23
24
 
24
25
  - Preserve Word-compatible redlines by editing OOXML directly.
25
- - Keep core logic host-independent (no Office.js globals, no Word API calls).
26
- - Reuse the same engine in browser, Node.js, and other JavaScript runtimes.
26
+ - Keep core logic and document-level operations host-independent (no Office.js globals, no Word API calls, no runtime Node built-ins like `node:zlib` or `node:crypto`).
27
+ - Standardize complete-document packages on `Uint8Array` binary payloads, pure-JS SHA-256 revision tokens, and `fflate` ZIP container manipulation.
28
+ - Reuse the same engine across Node.js, browsers, Cloudflare Workers, Deno, Bun, and sandboxed runtimes (e.g. n8n Code nodes).
27
29
  - Keep generated OOXML schema-safe through shared Word element creation and
28
30
  centralized revision metadata helpers.
29
31
 
@@ -37,7 +39,12 @@ No Word add-in entrypoints or host-specific integration layers are part of this
37
39
  │ └── xml-adapter.js
38
40
  ├── core/
39
41
  │ ├── paragraph-text.js
42
+ │ ├── sha256.js
40
43
  │ └── word-xml.js
44
+ ├── document/
45
+ │ ├── docx-document.js
46
+ │ ├── docx-document.d.ts
47
+ │ └── zip-archive.js
41
48
  ├── engine/
42
49
  │ ├── oxml-engine.js
43
50
  │ ├── surgical-mode.js
@@ -63,16 +70,22 @@ No Word add-in entrypoints or host-specific integration layers are part of this
63
70
  │ ├── cli.js
64
71
  │ ├── zip-archive.js
65
72
  │ └── index.js
73
+ ├── dist/
74
+ │ ├── docx-redline-js.esm.js
75
+ │ ├── docx-redline.bundle.js
76
+ │ └── docx-redline.bundle.cjs
66
77
  ├── index.js
67
78
  └── index.d.ts
68
79
  ```
69
80
 
70
81
  ## Entry Points
71
82
 
72
- - `index.js`: primary host-independent exports for OOXML reconciliation.
83
+ - `index.js`: primary host-independent exports for OOXML reconciliation and universal complete-document operations (`openDocx`, `DocxDocument`, `computePackageRevisionToken`).
84
+ - `document/docx-document.js`: universal `Uint8Array`-based document facade and package-level operation runner.
85
+ - `dist/docx-redline.bundle.js` / `dist/docx-redline.bundle.cjs`: zero-dependency standalone sandbox bundles, exported via `@ansonlai/docx-redline-js/bundle`.
73
86
  - `services/standalone-operation-runner.js`: stable complete-document XML
74
87
  operations, exported as `@ansonlai/docx-redline-js/standalone-runner`.
75
- - `node/index.js`: Node-only complete-DOCX buffer facade, exported as
88
+ - `node/index.js`: Node-compatible complete-DOCX facade re-exports, exported as
76
89
  `@ansonlai/docx-redline-js/node`.
77
90
  - `bin/docx-redline.js`: CLI launcher; command implementation lives in
78
91
  `node/cli.js` and shared operation behavior lives in `services/`.
@@ -181,11 +194,18 @@ No Word add-in entrypoints or host-specific integration layers are part of this
181
194
  - `services/document-inspection.js`
182
195
  - Read-only paragraphs/comments inventory with target identity, headings,
183
196
  revision authors, table context, and advisory visible numbering.
184
- - `node/docx-document.js`
185
- - Transactional whole-DOCX editing, artifact wiring, validation, and rollback.
186
- OOXML and package issues are classified as baseline/generated multisets, so
187
- unchanged source defects remain diagnostics while new defects block writes.
188
- This surface is excluded from the browser/root dependency graph.
197
+ - `document/docx-document.js`
198
+ - Universal transactional whole-DOCX editing, artifact wiring, validation, and rollback.
199
+ Standardized on `Uint8Array` binary payloads and pure-JS SHA-256 revision tokens.
200
+ Exposes `openDocx`, `DocxDocument`, and `computePackageRevisionToken` directly across all runtimes.
201
+ - `document/zip-archive.js`
202
+ - Universal, pure-JS ZIP archive reader and writer based on `fflate`. Operates on
203
+ `Uint8Array` byte arrays with zero Node built-ins (`node:zlib`, `Buffer`).
204
+ - `core/sha256.js`
205
+ - Pure-JS synchronous implementation of FIPS 180-4 SHA-256 with zero dependencies,
206
+ enabling deterministic revision tokens in sandboxed runtimes without `node:crypto`.
207
+ - `node/docx-document.js` and `node/zip-archive.js`
208
+ - Node-compatible re-exports maintaining `Buffer` compatibility for `@ansonlai/docx-redline-js/node`.
189
209
  - `node/cli.js` and `bin/docx-redline.js`
190
210
  - Cross-platform, JSON-only agent command boundary. Read commands never
191
211
  mutate; write commands require attribution, use package transactions, and
package/CHANGELOG.md CHANGED
@@ -1,18 +1,73 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## 0.8.0
4
+
5
+ ### Universal Document Facade & Zero-Node Architecture
6
+
7
+ - **Universal `DocxDocument` & `openDocx`:** Implemented complete-DOCX lifecycle operations
8
+ (`openDocx`, `doc.inspect()`, `doc.applyOperations()`, `doc.resolveRevisions()`, `doc.deleteComments()`,
9
+ `doc.toUint8Array()`, `doc.getRevisionToken()`) on standard `Uint8Array` in `document/docx-document.js`,
10
+ re-exported directly from `index.js`. Enables complete-document redline workflows in any JavaScript
11
+ runtime without Node.js dependencies.
12
+ - **Pure-JS ZIP container (`fflate`):** Replaced `node:zlib` with lightweight, zero-dependency `fflate`
13
+ in `document/zip-archive.js`, providing cross-runtime raw DEFLATE / INFLATE compression and decompression
14
+ with full OOXML archive compatibility.
15
+ - **Pure-JS FIPS 180-4 SHA-256:** Created zero-dependency, synchronous SHA-256 implementation in
16
+ `core/sha256.js`, removing `node:crypto` from package revision token calculations while retaining
17
+ exact deterministic hash parity with standard NIST vectors.
18
+ - **Transparent XML DOM fallback:** Added `resolveDomParserConstructor()` and
19
+ `resolveXmlSerializerConstructor()` in `adapters/xml-adapter.js`, automatically utilizing native
20
+ `globalThis.DOMParser`/`globalThis.XMLSerializer` when present and falling back transparently to
21
+ `@xmldom/xmldom` in headless, server, edge, and sandbox environments without requiring manual provider setup.
22
+ - **Zero-dependency sandbox bundles:** Configured `scripts/build.mjs` with `esbuild` (`platform: 'neutral'`)
23
+ to produce single-file, zero-dependency standalone bundles in both ESM (`dist/docx-redline.bundle.js`)
24
+ and CommonJS (`dist/docx-redline.bundle.cjs`), exported as `@ansonlai/docx-redline-js/bundle` for direct
25
+ use in workflow engines (e.g., n8n Code nodes), Cloudflare Workers, AWS Lambda, Deno, and QuickJS sandboxes.
26
+ - **Strict runtime boundary enforcement:** Removed `node:zlib` and `node:crypto` from allowed external
27
+ imports in `tests/no_word_api_index_check.mjs`. Automated sandbox isolation tests
28
+ (`tests/sandbox_bundle_isolation_tests.mjs`) verify zero Node built-in imports and full document
29
+ reconciliation in restricted VM contexts without `process`, `Buffer`, or `require`.
30
+ - **Backward compatibility:** Preserved full API and `Buffer` compatibility for `@ansonlai/docx-redline-js/node`
31
+ consumers and `doc.toBuffer()`.
32
+
33
+ ## 0.7.2
34
+
35
+ ### Agent Surface Simplification
36
+
37
+ - **Extraction-first golden path:** Re-established the single deterministic two-turn
38
+ "extract-then-apply" sequence as the default agent workflow across `AGENTS.md`,
39
+ `docs/AGENT_FAST_START.md`, and `docs/SKILL_AUTHORING.md`. Eliminates the
40
+ upfront "choice tax" and model deliberation overhead.
41
+ - **Localized replacements after inspection:** Decoupled localized patching from
42
+ speculation. Agents inspect the target paragraph first, then supply compact
43
+ `replacements` instructions, achieving 60.6%–90.8% payload reductions without
44
+ speculative ambiguity risk.
45
+ - **Computed success gate:** Consolidated standard success verification onto
46
+ `completion: true` and non-null `outputPath`, removing multi-field verification
47
+ checklists from primary prompts while retaining full `change` audit evidence.
48
+ - **Advanced features relocated:** Speculative 1-turn apply, directional context
49
+ windows (`--context-range`), and anchor diagnostics are relocated to
50
+ `docs/AGENT_KNOWLEDGE_BASE.md` and CLI help as opt-in harness policies.
51
+ - **Observed-state restoration:** Restructured restoration as a clean request-
52
+ or hint-driven branch (`--view rejected` or `selection.hint`) rather than an
53
+ upfront classification decision.
54
+ - **Empirical surface evaluation:** Added synthetic replay benchmark
55
+ (`npm run benchmark:agent-surface`) and validation report documenting 100%
56
+ first-attempt task correctness and payload savings across three workflow policies.
57
+
58
+ ## 0.7.1
59
+
60
+ ### Reliability and Discovery Refinements
4
61
 
5
62
  - Localized replacement matching now tries exact case-sensitive text first,
6
63
  then a narrow ASCII-space/non-breaking-space equivalent match. Ambiguous
7
64
  equivalent matches still fail closed, and unchanged boundary NBSPs are
8
65
  retained to avoid incidental whitespace redlines.
9
-
10
66
  - Added a narrow single-paragraph restore shortcut. A restore with a strong
11
67
  rejected-view target may omit `modified` for verbatim restoration or provide
12
68
  exact `replacements`; the CLI exposes the same path through `apply --restore
13
69
  --target-id ID`. Ranges, captures, weak targets, and inferred companion
14
70
  content remain excluded.
15
-
16
71
  - Fixed rejected-view restore receipts so `resolvedTarget` text, fingerprint,
17
72
  revision view, and match diagnostics describe the view actually used to
18
73
  resolve the target.
@@ -1,64 +1,92 @@
1
- /**
2
- * XML adapter for parser/serializer portability.
3
- *
4
- * Default behavior uses browser-provided DOMParser/XMLSerializer.
5
- * Consumers can override these constructors for non-browser runtimes.
1
+ /**
2
+ * XML adapter for parser/serializer portability.
3
+ *
4
+ * Transparently uses browser-provided DOMParser/XMLSerializer when available in
5
+ * standard web runtimes, automatically falling back to pure-JS @xmldom/xmldom in
6
+ * Node.js and sandbox environments. Consumers can also explicitly override these
7
+ * constructors via configureXmlProvider.
6
8
  */
7
9
 
10
+ import { DOMParser as XmlDomParser, XMLSerializer as XmlDomSerializer } from '@xmldom/xmldom';
8
11
  import { warn as logWarning, error as logError } from './logger.js';
9
12
 
10
- let _DOMParser = globalThis.DOMParser;
11
- let _XMLSerializer = globalThis.XMLSerializer;
12
-
13
- /**
14
- * Configures XML provider constructors.
15
- *
16
- * @param {Object} [options={}] - Provider overrides
17
- * @param {typeof DOMParser} [options.DOMParser] - DOMParser constructor
18
- * @param {typeof XMLSerializer} [options.XMLSerializer] - XMLSerializer constructor
19
- */
20
- export function configureXmlProvider(options = {}) {
21
- if (options.DOMParser) _DOMParser = options.DOMParser;
22
- if (options.XMLSerializer) _XMLSerializer = options.XMLSerializer;
23
- }
24
-
25
- /**
26
- * Creates a parser instance.
27
- *
28
- * @returns {DOMParser}
29
- */
13
+ let _DOMParser = null;
14
+ let _XMLSerializer = null;
15
+
16
+ /**
17
+ * Configures XML provider constructors.
18
+ *
19
+ * @param {Object} [options={}] - Provider overrides
20
+ * @param {typeof DOMParser} [options.DOMParser] - DOMParser constructor
21
+ * @param {typeof XMLSerializer} [options.XMLSerializer] - XMLSerializer constructor
22
+ */
23
+ export function configureXmlProvider(options = {}) {
24
+ if ('DOMParser' in options) _DOMParser = options.DOMParser;
25
+ if ('XMLSerializer' in options) _XMLSerializer = options.XMLSerializer;
26
+ }
27
+
28
+ /**
29
+ * Resolves the active DOMParser constructor.
30
+ *
31
+ * Priority order:
32
+ * 1. Explicitly configured constructor (via configureXmlProvider)
33
+ * 2. Host environment native DOMParser (globalThis.DOMParser)
34
+ * 3. Pure-JS @xmldom/xmldom fallback
35
+ *
36
+ * @returns {typeof DOMParser}
37
+ */
38
+ export function resolveDomParserConstructor() {
39
+ return _DOMParser || globalThis.DOMParser || XmlDomParser;
40
+ }
41
+
42
+ /**
43
+ * Resolves the active XMLSerializer constructor.
44
+ *
45
+ * Priority order:
46
+ * 1. Explicitly configured constructor (via configureXmlProvider)
47
+ * 2. Host environment native XMLSerializer (globalThis.XMLSerializer)
48
+ * 3. Pure-JS @xmldom/xmldom fallback
49
+ *
50
+ * @returns {typeof XMLSerializer}
51
+ */
52
+ export function resolveXmlSerializerConstructor() {
53
+ return _XMLSerializer || globalThis.XMLSerializer || XmlDomSerializer;
54
+ }
55
+
56
+ /**
57
+ * Creates a parser instance.
58
+ *
59
+ * @param {Object} [options={}] - Parser options (e.g. { onError } for xmldom)
60
+ * @returns {DOMParser}
61
+ */
30
62
  export function createParser(options = {}) {
31
- if (!_DOMParser && globalThis.DOMParser) {
32
- _DOMParser = globalThis.DOMParser;
33
- }
34
- if (!_DOMParser) {
35
- throw new Error('DOMParser is not configured. Call configureXmlProvider({ DOMParser, XMLSerializer }) first.');
36
- }
37
- return new _DOMParser(options);
38
- }
39
-
40
- /**
41
- * Creates a serializer instance.
42
- *
43
- * @returns {XMLSerializer}
44
- */
45
- export function createSerializer() {
46
- if (!_XMLSerializer && globalThis.XMLSerializer) {
47
- _XMLSerializer = globalThis.XMLSerializer;
48
- }
49
- if (!_XMLSerializer) {
50
- throw new Error('XMLSerializer is not configured. Call configureXmlProvider({ DOMParser, XMLSerializer }) first.');
51
- }
52
- return new _XMLSerializer();
53
- }
54
-
55
- /**
56
- * Parses XML text into a DOM document.
57
- *
58
- * @param {string} xmlString - XML string
59
- * @param {string} [contentType='text/xml'] - MIME type
60
- * @returns {Document}
61
- */
63
+ const ParserCtor = resolveDomParserConstructor();
64
+ if (!ParserCtor) {
65
+ throw new Error('DOMParser is not configured and no fallback XML parser is available.');
66
+ }
67
+ return new ParserCtor(options);
68
+ }
69
+
70
+ /**
71
+ * Creates a serializer instance.
72
+ *
73
+ * @returns {XMLSerializer}
74
+ */
75
+ export function createSerializer() {
76
+ const SerializerCtor = resolveXmlSerializerConstructor();
77
+ if (!SerializerCtor) {
78
+ throw new Error('XMLSerializer is not configured and no fallback XML serializer is available.');
79
+ }
80
+ return new SerializerCtor();
81
+ }
82
+
83
+ /**
84
+ * Parses XML text into a DOM document.
85
+ *
86
+ * @param {string} xmlString - XML string
87
+ * @param {string} [contentType='text/xml'] - MIME type
88
+ * @returns {Document}
89
+ */
62
90
  export function parseXml(xmlString, contentType = 'text/xml') {
63
91
  const result = parseOoxmlSafe(xmlString, contentType);
64
92
  if (result.error) {
package/core/sha256.js ADDED
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Pure-JS synchronous SHA-256 (FIPS 180-4) implementation.
3
+ *
4
+ * Self-contained, deterministic, and free from Node.js or browser crypto built-ins.
5
+ * Accepts string, Uint8Array, Buffer, or ArrayBuffer and returns a 64-character hex string.
6
+ */
7
+
8
+ const K = new Uint32Array([
9
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
10
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
11
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
12
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
13
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
14
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
15
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
16
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
17
+ ]);
18
+
19
+ const textEncoder = new TextEncoder();
20
+
21
+ /**
22
+ * Computes the SHA-256 digest of binary or text data.
23
+ *
24
+ * @param {Uint8Array|ArrayBuffer|ArrayBufferView|string} input - Input data to hash
25
+ * @returns {string} 64-character lowercase hex string
26
+ */
27
+ export function sha256(input) {
28
+ let bytes;
29
+ if (typeof input === 'string') {
30
+ bytes = textEncoder.encode(input);
31
+ } else if (input instanceof Uint8Array) {
32
+ bytes = input;
33
+ } else if (input instanceof ArrayBuffer) {
34
+ bytes = new Uint8Array(input);
35
+ } else if (ArrayBuffer.isView(input)) {
36
+ bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
37
+ } else {
38
+ throw new TypeError('sha256 input must be a string, Uint8Array, Buffer, or ArrayBuffer.');
39
+ }
40
+
41
+ const byteLen = bytes.length;
42
+ const bitLen = byteLen * 8;
43
+
44
+ // Pad length: input + 1 (0x80) + zeros + 8 bytes (64-bit length) to multiple of 64 bytes
45
+ const paddedLen = Math.ceil((byteLen + 9) / 64) * 64;
46
+ const padded = new Uint8Array(paddedLen);
47
+ padded.set(bytes, 0);
48
+ padded[byteLen] = 0x80;
49
+
50
+ const dataView = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
51
+ // Write 64-bit big-endian length in bits
52
+ const highBits = Math.floor(bitLen / 0x100000000);
53
+ const lowBits = (bitLen >>> 0);
54
+ dataView.setUint32(paddedLen - 8, highBits, false);
55
+ dataView.setUint32(paddedLen - 4, lowBits, false);
56
+
57
+ // Initial hash values
58
+ let h0 = 0x6a09e667;
59
+ let h1 = 0xbb67ae85;
60
+ let h2 = 0x3c6ef372;
61
+ let h3 = 0xa54ff53a;
62
+ let h4 = 0x510e527f;
63
+ let h5 = 0x9b05688c;
64
+ let h6 = 0x1f83d9ab;
65
+ let h7 = 0x5be0cd19;
66
+
67
+ const W = new Uint32Array(64);
68
+
69
+ for (let offset = 0; offset < paddedLen; offset += 64) {
70
+ for (let t = 0; t < 16; t++) {
71
+ W[t] = dataView.getUint32(offset + t * 4, false);
72
+ }
73
+ for (let t = 16; t < 64; t++) {
74
+ const w15 = W[t - 15];
75
+ const s0 = ((w15 >>> 7) | (w15 << 25)) ^ ((w15 >>> 18) | (w15 << 14)) ^ (w15 >>> 3);
76
+ const w2 = W[t - 2];
77
+ const s1 = ((w2 >>> 17) | (w2 << 15)) ^ ((w2 >>> 19) | (w2 << 13)) ^ (w2 >>> 10);
78
+ W[t] = (W[t - 16] + s0 + W[t - 7] + s1) | 0;
79
+ }
80
+
81
+ let a = h0;
82
+ let b = h1;
83
+ let c = h2;
84
+ let d = h3;
85
+ let e = h4;
86
+ let f = h5;
87
+ let g = h6;
88
+ let h = h7;
89
+
90
+ for (let t = 0; t < 64; t++) {
91
+ const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
92
+ const ch = (e & f) ^ (~e & g);
93
+ const temp1 = (h + S1 + ch + K[t] + W[t]) | 0;
94
+ const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
95
+ const maj = (a & b) ^ (a & c) ^ (b & c);
96
+ const temp2 = (S0 + maj) | 0;
97
+
98
+ h = g;
99
+ g = f;
100
+ f = e;
101
+ e = (d + temp1) | 0;
102
+ d = c;
103
+ c = b;
104
+ b = a;
105
+ a = (temp1 + temp2) | 0;
106
+ }
107
+
108
+ h0 = (h0 + a) | 0;
109
+ h1 = (h1 + b) | 0;
110
+ h2 = (h2 + c) | 0;
111
+ h3 = (h3 + d) | 0;
112
+ h4 = (h4 + e) | 0;
113
+ h5 = (h5 + f) | 0;
114
+ h6 = (h6 + g) | 0;
115
+ h7 = (h7 + h) | 0;
116
+ }
117
+
118
+ const hex = n => (n >>> 0).toString(16).padStart(8, '0');
119
+ return hex(h0) + hex(h1) + hex(h2) + hex(h3) + hex(h4) + hex(h5) + hex(h6) + hex(h7);
120
+ }