@szdatatalk/sqlward 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +57 -2
- package/index.d.ts +84 -0
- package/index.js +341 -0
- package/package.json +45 -4
- package/wasm/sqlward_wasm.wasm +0 -0
- package/wasm-worker.js +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright notices, patent notices,
|
|
102
|
+
trademark notices, and attribution notices from the Source
|
|
103
|
+
form of the Work, excluding those notices that do not pertain
|
|
104
|
+
to any part of the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "Notice" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such Notice file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a Notice text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the Notice file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the Derivative Works from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright notice to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the Notice file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
applicable law and appropriateness of using or redistributing the
|
|
151
|
+
Work and assume any risks associated with Your exercise of permissions
|
|
152
|
+
under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall Contributor be liable
|
|
158
|
+
for damages, including any direct, indirect, special, incidental, or
|
|
159
|
+
consequential damages of any kind, arising out of this License or out
|
|
160
|
+
of the use or inability to use the Work (including but not limited to
|
|
161
|
+
loss of data or data being rendered inaccurate or losses sustained by
|
|
162
|
+
You or third parties or a failure of the Work or any other software),
|
|
163
|
+
even if such Contributor has been advised of the possibility of such
|
|
164
|
+
damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, or indemnity
|
|
169
|
+
terms for Your component or your distribution. However, in accepting
|
|
170
|
+
such obligations, You may act only on Your own behalf and on Your
|
|
171
|
+
sole responsibility, and not on behalf of any other Contributor, and
|
|
172
|
+
only if You agree to indemnify, defend, and hold each Contributor
|
|
173
|
+
harmless for any liability incurred by, or claims asserted against,
|
|
174
|
+
such Contributor by reason of your accepting any such warranty or
|
|
175
|
+
additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate loss-free notice to the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or filename be included in the same "printed page" as the
|
|
187
|
+
copyright notice for easier identification within third-party
|
|
188
|
+
notices.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 SqlWard Contributors
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@szdatatalk/sqlward`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-Python, offline WebAssembly runtime for **SqlWard** (`v0.3.0`).
|
|
4
|
+
|
|
5
|
+
Deterministic static SQL analysis with zero runtime dependencies and zero SQL execution:
|
|
6
|
+
|
|
7
|
+
- **8 SQL dialects**: Postgres, MySQL, T-SQL, Oracle, Hive, Spark, Doris, StarRocks.
|
|
8
|
+
- **Column-level lineage & impact**: `parseColumn`, `graphDigestFromEdges`, `impactDownstream`, `impactUpstream`.
|
|
9
|
+
- **Security policy & replayable evidence**: `evaluatePolicy`, `verifyReceipt`, `verifyWorkflowEvidence`.
|
|
10
|
+
- **Pure-Rust domain adapters**: `bind`, `analyzeDbt`, `profileOmop`, `analyzeMappingImpact`, `queryRobotImpact`, `verifyInterop`.
|
|
11
|
+
- **Bounded worker execution**: `runWasmWorker` runs analysis in a terminable `node:worker_threads` worker with `AbortSignal`, timeout, and a 1 MiB payload cap.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @szdatatalk/sqlward
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires Node.js >= 20.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { instantiateWasm, runWasmWorker } from "@szdatatalk/sqlward";
|
|
25
|
+
|
|
26
|
+
const wasm = await instantiateWasm();
|
|
27
|
+
|
|
28
|
+
const lineage = wasm.parseColumn({
|
|
29
|
+
dialect: "postgres",
|
|
30
|
+
sql: "INSERT INTO dws.order_summary (user_id, total_amt) SELECT o.user_id, SUM(o.amount) FROM ods.orders o GROUP BY o.user_id",
|
|
31
|
+
catalogSchema: { "ods.orders": ["user_id", "amount"] },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
console.log(lineage.status, lineage.graphDigest, lineage.statements);
|
|
35
|
+
|
|
36
|
+
// Or run inside a bounded worker thread with cancellation support
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
const workerResult = await runWasmWorker(
|
|
39
|
+
"evaluatePolicy",
|
|
40
|
+
{
|
|
41
|
+
dialect: "postgres",
|
|
42
|
+
sql: "DELETE FROM ods.orders",
|
|
43
|
+
snapshot: {
|
|
44
|
+
policyId: "pol-1",
|
|
45
|
+
policyVersion: "1.0.0",
|
|
46
|
+
profile: "read-only",
|
|
47
|
+
relations: { "ods.orders": { kind: "table", sensitivityLabels: [] } },
|
|
48
|
+
},
|
|
49
|
+
principal: { principalId: "u1", tenantId: "t1", roles: ["analyst"] },
|
|
50
|
+
},
|
|
51
|
+
{ timeoutMs: 5000, signal: controller.signal },
|
|
52
|
+
);
|
|
53
|
+
console.log(workerResult.decision); // "DENY"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
Apache-2.0
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export const PROMOTED: true;
|
|
2
|
+
export const CONTRACT_VERSION: "0.3.0";
|
|
3
|
+
export const MAX_WASM_INPUT_BYTES: 1048576;
|
|
4
|
+
|
|
5
|
+
export interface LineageEdge {
|
|
6
|
+
sourceTable: string;
|
|
7
|
+
sourceColumn: string;
|
|
8
|
+
targetTable: string;
|
|
9
|
+
targetColumn: string;
|
|
10
|
+
dependencyType: string;
|
|
11
|
+
relation: string;
|
|
12
|
+
transformExpr?: string | null;
|
|
13
|
+
maskingApplied?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface EdgeComparisonReport {
|
|
17
|
+
matchCount: number;
|
|
18
|
+
missingInActual: string[];
|
|
19
|
+
unexpectedInActual: string[];
|
|
20
|
+
isMatch: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface WasmVersionInfo {
|
|
24
|
+
version: string;
|
|
25
|
+
promoted: boolean;
|
|
26
|
+
sqlParseInWasm: boolean;
|
|
27
|
+
pythonRequired?: boolean;
|
|
28
|
+
dialects?: string[];
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SqlWardWasmInstance {
|
|
33
|
+
promoted: true;
|
|
34
|
+
maxInputBytes: number;
|
|
35
|
+
versionInfo(): WasmVersionInfo;
|
|
36
|
+
parseColumn(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
37
|
+
evaluatePolicy(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
38
|
+
verifyReceipt(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
39
|
+
verifyWorkflowEvidence(bundle: Record<string, unknown> | string): Record<string, unknown>;
|
|
40
|
+
bind(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
41
|
+
analyzeDbt(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
42
|
+
profileOmop(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
43
|
+
analyzeMappingImpact(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
44
|
+
queryRobotImpact(query: Record<string, unknown> | string): Record<string, unknown>;
|
|
45
|
+
verifyInterop(request: Record<string, unknown> | string): Record<string, unknown>;
|
|
46
|
+
graphDigestFromEdges(edges: LineageEdge[] | string): string;
|
|
47
|
+
impactDownstream(edges: LineageEdge[] | string, originTable: string, originColumn: string): unknown;
|
|
48
|
+
impactUpstream(edges: LineageEdge[] | string, originTable: string, originColumn: string): unknown;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface WasmWorkerOptions {
|
|
52
|
+
timeoutMs?: number;
|
|
53
|
+
signal?: AbortSignal;
|
|
54
|
+
wasmPath?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type WasmWorkerOperation =
|
|
58
|
+
| "graphDigest"
|
|
59
|
+
| "impactDownstream"
|
|
60
|
+
| "impactUpstream"
|
|
61
|
+
| "parseColumn"
|
|
62
|
+
| "evaluatePolicy"
|
|
63
|
+
| "verifyReceipt"
|
|
64
|
+
| "verifyWorkflowEvidence"
|
|
65
|
+
| "bind"
|
|
66
|
+
| "analyzeDbt"
|
|
67
|
+
| "profileOmop"
|
|
68
|
+
| "analyzeMappingImpact"
|
|
69
|
+
| "queryRobotImpact"
|
|
70
|
+
| "verifyInterop";
|
|
71
|
+
|
|
72
|
+
export function defaultWasmPath(): string;
|
|
73
|
+
export function edgeKey(edge: LineageEdge): string;
|
|
74
|
+
export function compareEdges(
|
|
75
|
+
actual: LineageEdge[],
|
|
76
|
+
expectedDirect: Omit<LineageEdge, "dependencyType">[],
|
|
77
|
+
expectedIndirect: Omit<LineageEdge, "dependencyType">[],
|
|
78
|
+
): EdgeComparisonReport;
|
|
79
|
+
export function instantiateWasm(wasmSource?: string | Uint8Array | ArrayBuffer): Promise<SqlWardWasmInstance>;
|
|
80
|
+
export function runWasmWorker(
|
|
81
|
+
operation: WasmWorkerOperation,
|
|
82
|
+
payload: unknown,
|
|
83
|
+
options?: WasmWorkerOptions,
|
|
84
|
+
): Promise<unknown>;
|
package/index.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Offline Node & WASM runtime SDK (`@szdatatalk/sqlward`, v0.3.0).
|
|
3
|
+
*
|
|
4
|
+
* Zero Python dependency, zero compiler required at runtime, zero network access.
|
|
5
|
+
* Executes `sqlward_wasm.wasm` directly in-process or inside a terminable
|
|
6
|
+
* `node:worker_threads` Worker with 1 MiB byte bounds for 8-dialect SQL parsing,
|
|
7
|
+
* column lineage, graph digest/impact, M6 policy/receipt/workflow verification,
|
|
8
|
+
* and pure-Rust domain adapters (bind, dbt, OMOP, robot, interop). Never executes SQL.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { Worker } from "node:worker_threads";
|
|
15
|
+
|
|
16
|
+
export const PROMOTED = true;
|
|
17
|
+
export const CONTRACT_VERSION = "0.3.0";
|
|
18
|
+
export const MAX_WASM_INPUT_BYTES = 1_048_576;
|
|
19
|
+
|
|
20
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
21
|
+
|
|
22
|
+
export function defaultWasmPath() {
|
|
23
|
+
if (process.env.SQLWARD_WASM_PATH) {
|
|
24
|
+
return process.env.SQLWARD_WASM_PATH;
|
|
25
|
+
}
|
|
26
|
+
if (process.env.LINEAGEKIT_WASM_PATH) {
|
|
27
|
+
return process.env.LINEAGEKIT_WASM_PATH;
|
|
28
|
+
}
|
|
29
|
+
const candidates = [
|
|
30
|
+
path.resolve(__dirname, "wasm/sqlward_wasm.wasm"),
|
|
31
|
+
path.resolve(__dirname, "sqlward_wasm.wasm"),
|
|
32
|
+
path.resolve(__dirname, "../../evidence/phase2/artifacts/wasm/sqlward_wasm.wasm"),
|
|
33
|
+
path.resolve(__dirname, "../../native/target/wasm32-unknown-unknown/release/sqlward_wasm.wasm"),
|
|
34
|
+
path.resolve(__dirname, "../../native/target/wasm32-unknown-unknown/debug/sqlward_wasm.wasm"),
|
|
35
|
+
path.resolve(__dirname, "../../evidence/phase2/artifacts/wasm/lineagekit_wasm.wasm"),
|
|
36
|
+
path.resolve(__dirname, "../../native/target/wasm32-unknown-unknown/release/lineagekit_wasm.wasm"),
|
|
37
|
+
path.resolve(__dirname, "../../native/target/wasm32-unknown-unknown/debug/lineagekit_wasm.wasm"),
|
|
38
|
+
];
|
|
39
|
+
for (const p of candidates) {
|
|
40
|
+
if (fs.existsSync(p)) return p;
|
|
41
|
+
}
|
|
42
|
+
return candidates[0];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function edgeKey(edge) {
|
|
46
|
+
return [
|
|
47
|
+
edge.sourceTable,
|
|
48
|
+
edge.sourceColumn,
|
|
49
|
+
edge.targetTable,
|
|
50
|
+
edge.targetColumn,
|
|
51
|
+
edge.dependencyType,
|
|
52
|
+
edge.relation,
|
|
53
|
+
edge.maskingApplied === true ? "true" : "false",
|
|
54
|
+
].join("|");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function compareEdges(actual, expectedDirect, expectedIndirect) {
|
|
58
|
+
const expected = new Set([
|
|
59
|
+
...expectedDirect.map((e) =>
|
|
60
|
+
edgeKey({ ...e, dependencyType: "Direct", maskingApplied: !!e.maskingApplied }),
|
|
61
|
+
),
|
|
62
|
+
...expectedIndirect.map((e) =>
|
|
63
|
+
edgeKey({ ...e, dependencyType: "Indirect", maskingApplied: !!e.maskingApplied }),
|
|
64
|
+
),
|
|
65
|
+
]);
|
|
66
|
+
const actualKeys = new Set(actual.map(edgeKey));
|
|
67
|
+
const missing = [...expected].filter((k) => !actualKeys.has(k)).sort();
|
|
68
|
+
const unexpected = [...actualKeys].filter((k) => !expected.has(k)).sort();
|
|
69
|
+
return {
|
|
70
|
+
matchCount: [...expected].filter((k) => actualKeys.has(k)).length,
|
|
71
|
+
missingInActual: missing,
|
|
72
|
+
unexpectedInActual: unexpected,
|
|
73
|
+
isMatch: missing.length === 0 && unexpected.length === 0,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function makeStubImports() {
|
|
78
|
+
const noop = () => 0;
|
|
79
|
+
const nsProxy = new Proxy(
|
|
80
|
+
{},
|
|
81
|
+
{
|
|
82
|
+
get: () => noop,
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
return new Proxy(
|
|
86
|
+
{},
|
|
87
|
+
{
|
|
88
|
+
get: () => nsProxy,
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Instantiate `sqlward_wasm.wasm` from bytes or file path with zero external glue.
|
|
95
|
+
*/
|
|
96
|
+
export async function instantiateWasm(wasmSource = defaultWasmPath()) {
|
|
97
|
+
const bytes =
|
|
98
|
+
wasmSource instanceof Uint8Array || wasmSource instanceof ArrayBuffer
|
|
99
|
+
? wasmSource
|
|
100
|
+
: fs.readFileSync(wasmSource);
|
|
101
|
+
const { instance } = await WebAssembly.instantiate(bytes, makeStubImports());
|
|
102
|
+
const ex = instance.exports;
|
|
103
|
+
const wasmAlloc = ex.sw_wasm_alloc || ex.lk_wasm_alloc;
|
|
104
|
+
const wasmDealloc = ex.sw_wasm_dealloc || ex.lk_wasm_dealloc;
|
|
105
|
+
const wasmLastPtr = ex.sw_wasm_last_ptr || ex.lk_wasm_last_ptr;
|
|
106
|
+
const wasmLastLen = ex.sw_wasm_last_len || ex.lk_wasm_last_len;
|
|
107
|
+
const wasmMaxInputBytes = ex.sw_wasm_max_input_bytes || ex.lk_wasm_max_input_bytes;
|
|
108
|
+
const wasmGraphDigest = ex.sw_wasm_graph_digest || ex.lk_wasm_graph_digest;
|
|
109
|
+
const wasmImpactDownstream = ex.sw_wasm_impact_downstream || ex.lk_wasm_impact_downstream;
|
|
110
|
+
const wasmImpactUpstream = ex.sw_wasm_impact_upstream || ex.lk_wasm_impact_upstream;
|
|
111
|
+
const encoder = new TextEncoder();
|
|
112
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
113
|
+
|
|
114
|
+
function writeUtf8(str) {
|
|
115
|
+
const encoded = encoder.encode(str);
|
|
116
|
+
if (encoded.byteLength > MAX_WASM_INPUT_BYTES) {
|
|
117
|
+
throw new RangeError("WASM_INPUT_TOO_LARGE: payload exceeds 1 MiB limit");
|
|
118
|
+
}
|
|
119
|
+
if (encoded.byteLength === 0) {
|
|
120
|
+
return { ptr: 0, len: 0 };
|
|
121
|
+
}
|
|
122
|
+
const ptr = wasmAlloc(encoded.byteLength);
|
|
123
|
+
if (!ptr) {
|
|
124
|
+
throw new RangeError("WASM_ALLOC_FAILED: allocation refused by linear-memory cap");
|
|
125
|
+
}
|
|
126
|
+
new Uint8Array(ex.memory.buffer, ptr, encoded.byteLength).set(encoded);
|
|
127
|
+
return { ptr, len: encoded.byteLength };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function freeBuf({ ptr, len }) {
|
|
131
|
+
if (ptr && len > 0) {
|
|
132
|
+
wasmDealloc(ptr, len);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function readLastResult() {
|
|
137
|
+
const ptr = wasmLastPtr();
|
|
138
|
+
const len = wasmLastLen();
|
|
139
|
+
return decoder.decode(new Uint8Array(ex.memory.buffer, ptr, len));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function normalizeEdgesPayload(edges) {
|
|
143
|
+
if (typeof edges === "string") return edges;
|
|
144
|
+
if (!Array.isArray(edges)) return JSON.stringify(edges);
|
|
145
|
+
return JSON.stringify(
|
|
146
|
+
edges.map((e) => ({
|
|
147
|
+
...e,
|
|
148
|
+
dependencyType:
|
|
149
|
+
typeof e?.dependencyType === "string" ? e.dependencyType.toUpperCase() : e?.dependencyType,
|
|
150
|
+
})),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function callSingleJson(fn, payload) {
|
|
155
|
+
if (typeof fn !== "function") {
|
|
156
|
+
throw new Error("WASM_EXPORT_MISSING: rebuild sqlward_wasm.wasm >= 0.3.0");
|
|
157
|
+
}
|
|
158
|
+
const json = typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
159
|
+
const buf = writeUtf8(json);
|
|
160
|
+
try {
|
|
161
|
+
const rc = fn(buf.ptr, buf.len);
|
|
162
|
+
const text = readLastResult();
|
|
163
|
+
if (rc !== 0) throw new Error(text);
|
|
164
|
+
return JSON.parse(text);
|
|
165
|
+
} finally {
|
|
166
|
+
freeBuf(buf);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
promoted: true,
|
|
172
|
+
maxInputBytes: Number(wasmMaxInputBytes()),
|
|
173
|
+
versionInfo() {
|
|
174
|
+
if (typeof ex.sw_wasm_version_info !== "function") {
|
|
175
|
+
return { version: CONTRACT_VERSION, promoted: true, sqlParseInWasm: false };
|
|
176
|
+
}
|
|
177
|
+
const rc = ex.sw_wasm_version_info();
|
|
178
|
+
const text = readLastResult();
|
|
179
|
+
if (rc !== 0) throw new Error(text);
|
|
180
|
+
return JSON.parse(text);
|
|
181
|
+
},
|
|
182
|
+
parseColumn(request) {
|
|
183
|
+
return callSingleJson(ex.sw_wasm_parse_column, request);
|
|
184
|
+
},
|
|
185
|
+
evaluatePolicy(request) {
|
|
186
|
+
return callSingleJson(ex.sw_wasm_policy_eval, request);
|
|
187
|
+
},
|
|
188
|
+
verifyReceipt(request) {
|
|
189
|
+
return callSingleJson(ex.sw_wasm_receipt_verify, request);
|
|
190
|
+
},
|
|
191
|
+
verifyWorkflowEvidence(bundle) {
|
|
192
|
+
return callSingleJson(ex.sw_wasm_workflow_verify, bundle);
|
|
193
|
+
},
|
|
194
|
+
bind(request) {
|
|
195
|
+
return callSingleJson(ex.sw_wasm_bind, request);
|
|
196
|
+
},
|
|
197
|
+
analyzeDbt(request) {
|
|
198
|
+
return callSingleJson(ex.sw_wasm_dbt_analyze, request);
|
|
199
|
+
},
|
|
200
|
+
profileOmop(request) {
|
|
201
|
+
return callSingleJson(ex.sw_wasm_omop_profile, request);
|
|
202
|
+
},
|
|
203
|
+
analyzeMappingImpact(request) {
|
|
204
|
+
return callSingleJson(ex.sw_wasm_mapping_impact, request);
|
|
205
|
+
},
|
|
206
|
+
queryRobotImpact(query) {
|
|
207
|
+
return callSingleJson(ex.sw_wasm_robot_impact, query);
|
|
208
|
+
},
|
|
209
|
+
verifyInterop(request) {
|
|
210
|
+
return callSingleJson(ex.sw_wasm_interop_verify, request);
|
|
211
|
+
},
|
|
212
|
+
graphDigestFromEdges(edges) {
|
|
213
|
+
const json = normalizeEdgesPayload(edges);
|
|
214
|
+
const buf = writeUtf8(json);
|
|
215
|
+
try {
|
|
216
|
+
const rc = wasmGraphDigest(buf.ptr, buf.len);
|
|
217
|
+
const text = readLastResult();
|
|
218
|
+
if (rc !== 0) throw new Error(text);
|
|
219
|
+
return text;
|
|
220
|
+
} finally {
|
|
221
|
+
freeBuf(buf);
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
impactDownstream(edges, originTable, originColumn) {
|
|
225
|
+
const json = normalizeEdgesPayload(edges);
|
|
226
|
+
const bEdges = writeUtf8(json);
|
|
227
|
+
const bTbl = writeUtf8(originTable);
|
|
228
|
+
const bCol = writeUtf8(originColumn);
|
|
229
|
+
try {
|
|
230
|
+
const rc = wasmImpactDownstream(
|
|
231
|
+
bEdges.ptr,
|
|
232
|
+
bEdges.len,
|
|
233
|
+
bTbl.ptr,
|
|
234
|
+
bTbl.len,
|
|
235
|
+
bCol.ptr,
|
|
236
|
+
bCol.len,
|
|
237
|
+
);
|
|
238
|
+
const text = readLastResult();
|
|
239
|
+
if (rc !== 0) throw new Error(text);
|
|
240
|
+
return JSON.parse(text);
|
|
241
|
+
} finally {
|
|
242
|
+
freeBuf(bEdges);
|
|
243
|
+
freeBuf(bTbl);
|
|
244
|
+
freeBuf(bCol);
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
impactUpstream(edges, originTable, originColumn) {
|
|
248
|
+
const json = normalizeEdgesPayload(edges);
|
|
249
|
+
const bEdges = writeUtf8(json);
|
|
250
|
+
const bTbl = writeUtf8(originTable);
|
|
251
|
+
const bCol = writeUtf8(originColumn);
|
|
252
|
+
try {
|
|
253
|
+
const rc = wasmImpactUpstream(
|
|
254
|
+
bEdges.ptr,
|
|
255
|
+
bEdges.len,
|
|
256
|
+
bTbl.ptr,
|
|
257
|
+
bTbl.len,
|
|
258
|
+
bCol.ptr,
|
|
259
|
+
bCol.len,
|
|
260
|
+
);
|
|
261
|
+
const text = readLastResult();
|
|
262
|
+
if (rc !== 0) throw new Error(text);
|
|
263
|
+
return JSON.parse(text);
|
|
264
|
+
} finally {
|
|
265
|
+
freeBuf(bEdges);
|
|
266
|
+
freeBuf(bTbl);
|
|
267
|
+
freeBuf(bCol);
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Execute WASM analysis inside a terminable worker thread (`AbortSignal` + `timeoutMs`).
|
|
275
|
+
*/
|
|
276
|
+
export function runWasmWorker(operation, payload, options = {}) {
|
|
277
|
+
const { timeoutMs = 5000, signal, wasmPath = defaultWasmPath() } = options;
|
|
278
|
+
const workerScript = path.join(__dirname, "wasm-worker.js");
|
|
279
|
+
|
|
280
|
+
return new Promise((resolve, reject) => {
|
|
281
|
+
if (signal?.aborted) {
|
|
282
|
+
reject(new Error("WASM_CANCELLED"));
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const rawJson =
|
|
286
|
+
typeof payload === "string"
|
|
287
|
+
? payload
|
|
288
|
+
: typeof payload?.edges === "string"
|
|
289
|
+
? payload.edges
|
|
290
|
+
: JSON.stringify(payload ?? {});
|
|
291
|
+
if (Buffer.byteLength(rawJson, "utf8") > MAX_WASM_INPUT_BYTES) {
|
|
292
|
+
reject(new RangeError("WASM_INPUT_TOO_LARGE: payload exceeds 1 MiB limit"));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const worker = new Worker(workerScript, {
|
|
297
|
+
workerData: { operation, payload, wasmPath },
|
|
298
|
+
});
|
|
299
|
+
let settled = false;
|
|
300
|
+
let cancelReason = null;
|
|
301
|
+
|
|
302
|
+
const finish = (err, val) => {
|
|
303
|
+
if (settled) return;
|
|
304
|
+
settled = true;
|
|
305
|
+
clearTimeout(timer);
|
|
306
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
307
|
+
if (err) reject(err);
|
|
308
|
+
else resolve(val);
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const onAbort = () => {
|
|
312
|
+
cancelReason = "WASM_CANCELLED";
|
|
313
|
+
worker.terminate().finally(() => finish(new Error("WASM_CANCELLED")));
|
|
314
|
+
};
|
|
315
|
+
if (signal) signal.addEventListener("abort", onAbort, { once: true });
|
|
316
|
+
|
|
317
|
+
const timer = setTimeout(() => {
|
|
318
|
+
cancelReason = "WASM_TIMEOUT";
|
|
319
|
+
worker.terminate().finally(() => finish(new Error("WASM_TIMEOUT")));
|
|
320
|
+
}, timeoutMs);
|
|
321
|
+
|
|
322
|
+
worker.once("message", (msg) => {
|
|
323
|
+
if (cancelReason) {
|
|
324
|
+
finish(new Error(cancelReason));
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (msg?.ok) finish(null, msg.result);
|
|
328
|
+
else finish(new Error(msg?.error || "WASM_WORKER_ERROR"));
|
|
329
|
+
});
|
|
330
|
+
worker.once("error", (err) => {
|
|
331
|
+
if (cancelReason) finish(new Error(cancelReason));
|
|
332
|
+
else finish(err);
|
|
333
|
+
});
|
|
334
|
+
worker.once("exit", (code) => {
|
|
335
|
+
if (!settled) {
|
|
336
|
+
if (cancelReason) finish(new Error(cancelReason));
|
|
337
|
+
else if (code !== 0) finish(new Error(`WASM_WORKER_EXIT_${code}`));
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@szdatatalk/sqlward",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Zero-Python WASM runtime for SqlWard: 8-dialect SQL lineage, M6 security policy, receipts, and dbt/OMOP/Robot/M5 adapters",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"wasm-worker.js",
|
|
19
|
+
"wasm/sqlward_wasm.wasm",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"szdatatalk",
|
|
25
|
+
"datatalk",
|
|
26
|
+
"sqlward",
|
|
27
|
+
"sql",
|
|
28
|
+
"lineage",
|
|
29
|
+
"wasm",
|
|
30
|
+
"data-governance",
|
|
31
|
+
"security",
|
|
32
|
+
"policy",
|
|
33
|
+
"dbt",
|
|
34
|
+
"omop"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"stage-wasm": "node -e \"const fs=require('fs'),p=require('path');const dst=p.resolve('wasm/sqlward_wasm.wasm');const srcs=[p.resolve('../../native/target/wasm32-unknown-unknown/release/sqlward_wasm.wasm'),p.resolve('../../evidence/phase2/artifacts/wasm/sqlward_wasm.wasm')];const src=srcs.find((s)=>fs.existsSync(s));if(src){fs.mkdirSync(p.dirname(dst),{recursive:true});fs.copyFileSync(src,dst);}if(!fs.existsSync(dst)){throw new Error('Missing wasm/sqlward_wasm.wasm; build sqlward-wasm first');}const lic=p.resolve('../../LICENSE');if(fs.existsSync(lic))fs.copyFileSync(lic,p.resolve('LICENSE'));\"",
|
|
41
|
+
"prepack": "npm run stage-wasm",
|
|
42
|
+
"test": "node --test test/contract.test.js"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=20"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
Binary file
|
package/wasm-worker.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
+
import { instantiateWasm } from "./index.js";
|
|
3
|
+
|
|
4
|
+
async function main() {
|
|
5
|
+
const { operation, payload, wasmPath } = workerData;
|
|
6
|
+
if (operation === "__spin_for_cancel_test__") {
|
|
7
|
+
// Deliberate busy loop to prove host worker termination works cleanly
|
|
8
|
+
while (true) {
|
|
9
|
+
Math.imul(1234567, 890123);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const wasm = await instantiateWasm(wasmPath);
|
|
13
|
+
let result;
|
|
14
|
+
if (operation === "graphDigest") {
|
|
15
|
+
result = wasm.graphDigestFromEdges(payload.edges);
|
|
16
|
+
} else if (operation === "impactDownstream") {
|
|
17
|
+
result = wasm.impactDownstream(payload.edges, payload.originTable, payload.originColumn);
|
|
18
|
+
} else if (operation === "impactUpstream") {
|
|
19
|
+
result = wasm.impactUpstream(payload.edges, payload.originTable, payload.originColumn);
|
|
20
|
+
} else if (operation === "parseColumn") {
|
|
21
|
+
result = wasm.parseColumn(payload);
|
|
22
|
+
} else if (operation === "evaluatePolicy") {
|
|
23
|
+
result = wasm.evaluatePolicy(payload);
|
|
24
|
+
} else if (operation === "verifyReceipt") {
|
|
25
|
+
result = wasm.verifyReceipt(payload);
|
|
26
|
+
} else if (operation === "verifyWorkflowEvidence") {
|
|
27
|
+
result = wasm.verifyWorkflowEvidence(payload);
|
|
28
|
+
} else if (operation === "bind") {
|
|
29
|
+
result = wasm.bind(payload);
|
|
30
|
+
} else if (operation === "analyzeDbt") {
|
|
31
|
+
result = wasm.analyzeDbt(payload);
|
|
32
|
+
} else if (operation === "profileOmop") {
|
|
33
|
+
result = wasm.profileOmop(payload);
|
|
34
|
+
} else if (operation === "analyzeMappingImpact") {
|
|
35
|
+
result = wasm.analyzeMappingImpact(payload);
|
|
36
|
+
} else if (operation === "queryRobotImpact") {
|
|
37
|
+
result = wasm.queryRobotImpact(payload);
|
|
38
|
+
} else if (operation === "verifyInterop") {
|
|
39
|
+
result = wasm.verifyInterop(payload);
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error(`unsupported WASM worker operation: ${operation}`);
|
|
42
|
+
}
|
|
43
|
+
parentPort.postMessage({ ok: true, result });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
main().catch((err) => {
|
|
47
|
+
parentPort.postMessage({ ok: false, error: String(err?.message || err) });
|
|
48
|
+
});
|