@arcjet/redact-wasm 1.0.0-alpha.23

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.
@@ -0,0 +1,330 @@
1
+ function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.instantiate) {
2
+
3
+ let dv = new DataView(new ArrayBuffer());
4
+ const dataView = mem => dv.buffer === mem.buffer ? dv : dv = new DataView(mem.buffer);
5
+
6
+ function toUint32(val) {
7
+ return val >>> 0;
8
+ }
9
+
10
+ const utf8Decoder = new TextDecoder();
11
+
12
+ const utf8Encoder = new TextEncoder();
13
+
14
+ let utf8EncodedLen = 0;
15
+ function utf8Encode(s, realloc, memory) {
16
+ if (typeof s !== 'string') throw new TypeError('expected a string');
17
+ if (s.length === 0) {
18
+ utf8EncodedLen = 0;
19
+ return 1;
20
+ }
21
+ let buf = utf8Encoder.encode(s);
22
+ let ptr = realloc(0, 0, 1, buf.length);
23
+ new Uint8Array(memory.buffer).set(buf, ptr);
24
+ utf8EncodedLen = buf.length;
25
+ return ptr;
26
+ }
27
+
28
+
29
+ const module0 = getCoreModule('arcjet_analyze_bindings_redact.component.core.wasm');
30
+ const module1 = getCoreModule('arcjet_analyze_bindings_redact.component.core2.wasm');
31
+ const module2 = getCoreModule('arcjet_analyze_bindings_redact.component.core3.wasm');
32
+
33
+ const { detectSensitiveInfo, redactSensitiveInfo } = imports['arcjet:redact/custom-redact'];
34
+ let gen = (function* init () {
35
+ let exports0;
36
+ let exports1;
37
+ let memory0;
38
+ let realloc0;
39
+
40
+ function trampoline0(arg0, arg1, arg2) {
41
+ var len1 = arg1;
42
+ var base1 = arg0;
43
+ var result1 = [];
44
+ for (let i = 0; i < len1; i++) {
45
+ const base = base1 + i * 8;
46
+ var ptr0 = dataView(memory0).getInt32(base + 0, true);
47
+ var len0 = dataView(memory0).getInt32(base + 4, true);
48
+ var result0 = utf8Decoder.decode(new Uint8Array(memory0.buffer, ptr0, len0));
49
+ result1.push(result0);
50
+ }
51
+ const ret = detectSensitiveInfo(result1);
52
+ var vec5 = ret;
53
+ var len5 = vec5.length;
54
+ var result5 = realloc0(0, 0, 4, len5 * 16);
55
+ for (let i = 0; i < vec5.length; i++) {
56
+ const e = vec5[i];
57
+ const base = result5 + i * 16;var variant4 = e;
58
+ if (variant4 === null || variant4=== undefined) {
59
+ dataView(memory0).setInt8(base + 0, 0, true);
60
+ } else {
61
+ const e = variant4;
62
+ dataView(memory0).setInt8(base + 0, 1, true);
63
+ var variant3 = e;
64
+ switch (variant3.tag) {
65
+ case 'email': {
66
+ dataView(memory0).setInt8(base + 4, 0, true);
67
+ break;
68
+ }
69
+ case 'phone-number': {
70
+ dataView(memory0).setInt8(base + 4, 1, true);
71
+ break;
72
+ }
73
+ case 'ip-address': {
74
+ dataView(memory0).setInt8(base + 4, 2, true);
75
+ break;
76
+ }
77
+ case 'credit-card-number': {
78
+ dataView(memory0).setInt8(base + 4, 3, true);
79
+ break;
80
+ }
81
+ case 'custom': {
82
+ const e = variant3.val;
83
+ dataView(memory0).setInt8(base + 4, 4, true);
84
+ var ptr2 = utf8Encode(e, realloc0, memory0);
85
+ var len2 = utf8EncodedLen;
86
+ dataView(memory0).setInt32(base + 12, len2, true);
87
+ dataView(memory0).setInt32(base + 8, ptr2, true);
88
+ break;
89
+ }
90
+ default: {
91
+ throw new TypeError(`invalid variant tag value \`${JSON.stringify(variant3.tag)}\` (received \`${variant3}\`) specified for \`SensitiveInfoEntity\``);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ dataView(memory0).setInt32(arg2 + 4, len5, true);
97
+ dataView(memory0).setInt32(arg2 + 0, result5, true);
98
+ }
99
+
100
+ function trampoline1(arg0, arg1, arg2, arg3) {
101
+ let variant1;
102
+ switch (arg0) {
103
+ case 0: {
104
+ variant1= {
105
+ tag: 'email',
106
+ };
107
+ break;
108
+ }
109
+ case 1: {
110
+ variant1= {
111
+ tag: 'phone-number',
112
+ };
113
+ break;
114
+ }
115
+ case 2: {
116
+ variant1= {
117
+ tag: 'ip-address',
118
+ };
119
+ break;
120
+ }
121
+ case 3: {
122
+ variant1= {
123
+ tag: 'credit-card-number',
124
+ };
125
+ break;
126
+ }
127
+ case 4: {
128
+ var ptr0 = arg1;
129
+ var len0 = arg2;
130
+ var result0 = utf8Decoder.decode(new Uint8Array(memory0.buffer, ptr0, len0));
131
+ variant1= {
132
+ tag: 'custom',
133
+ val: result0
134
+ };
135
+ break;
136
+ }
137
+ default: {
138
+ throw new TypeError('invalid variant discriminant for SensitiveInfoEntity');
139
+ }
140
+ }
141
+ const ret = redactSensitiveInfo(variant1);
142
+ var variant3 = ret;
143
+ if (variant3 === null || variant3=== undefined) {
144
+ dataView(memory0).setInt8(arg3 + 0, 0, true);
145
+ } else {
146
+ const e = variant3;
147
+ dataView(memory0).setInt8(arg3 + 0, 1, true);
148
+ var ptr2 = utf8Encode(e, realloc0, memory0);
149
+ var len2 = utf8EncodedLen;
150
+ dataView(memory0).setInt32(arg3 + 8, len2, true);
151
+ dataView(memory0).setInt32(arg3 + 4, ptr2, true);
152
+ }
153
+ }
154
+ let postReturn0;
155
+ Promise.all([module0, module1, module2]).catch(() => {});
156
+ ({ exports: exports0 } = yield instantiateCore(yield module1));
157
+ ({ exports: exports1 } = yield instantiateCore(yield module0, {
158
+ 'arcjet:redact/custom-redact': {
159
+ 'detect-sensitive-info': exports0['0'],
160
+ 'redact-sensitive-info': exports0['1'],
161
+ },
162
+ }));
163
+ memory0 = exports1.memory;
164
+ realloc0 = exports1.cabi_realloc;
165
+ (yield instantiateCore(yield module2, {
166
+ '': {
167
+ $imports: exports0.$imports,
168
+ '0': trampoline0,
169
+ '1': trampoline1,
170
+ },
171
+ }));
172
+ postReturn0 = exports1.cabi_post_redact;
173
+
174
+ function redact(arg0, arg1) {
175
+ var ptr0 = utf8Encode(arg0, realloc0, memory0);
176
+ var len0 = utf8EncodedLen;
177
+ var {entities: v1_0, contextWindowSize: v1_1, skipCustomDetect: v1_2, skipCustomRedact: v1_3 } = arg1;
178
+ var variant5 = v1_0;
179
+ let variant5_0;
180
+ let variant5_1;
181
+ let variant5_2;
182
+ if (variant5 === null || variant5=== undefined) {
183
+ variant5_0 = 0;
184
+ variant5_1 = 0;
185
+ variant5_2 = 0;
186
+ } else {
187
+ const e = variant5;
188
+ var vec4 = e;
189
+ var len4 = vec4.length;
190
+ var result4 = realloc0(0, 0, 4, len4 * 12);
191
+ for (let i = 0; i < vec4.length; i++) {
192
+ const e = vec4[i];
193
+ const base = result4 + i * 12;var variant3 = e;
194
+ switch (variant3.tag) {
195
+ case 'email': {
196
+ dataView(memory0).setInt8(base + 0, 0, true);
197
+ break;
198
+ }
199
+ case 'phone-number': {
200
+ dataView(memory0).setInt8(base + 0, 1, true);
201
+ break;
202
+ }
203
+ case 'ip-address': {
204
+ dataView(memory0).setInt8(base + 0, 2, true);
205
+ break;
206
+ }
207
+ case 'credit-card-number': {
208
+ dataView(memory0).setInt8(base + 0, 3, true);
209
+ break;
210
+ }
211
+ case 'custom': {
212
+ const e = variant3.val;
213
+ dataView(memory0).setInt8(base + 0, 4, true);
214
+ var ptr2 = utf8Encode(e, realloc0, memory0);
215
+ var len2 = utf8EncodedLen;
216
+ dataView(memory0).setInt32(base + 8, len2, true);
217
+ dataView(memory0).setInt32(base + 4, ptr2, true);
218
+ break;
219
+ }
220
+ default: {
221
+ throw new TypeError(`invalid variant tag value \`${JSON.stringify(variant3.tag)}\` (received \`${variant3}\`) specified for \`SensitiveInfoEntity\``);
222
+ }
223
+ }
224
+ }
225
+ variant5_0 = 1;
226
+ variant5_1 = result4;
227
+ variant5_2 = len4;
228
+ }
229
+ var variant6 = v1_1;
230
+ let variant6_0;
231
+ let variant6_1;
232
+ if (variant6 === null || variant6=== undefined) {
233
+ variant6_0 = 0;
234
+ variant6_1 = 0;
235
+ } else {
236
+ const e = variant6;
237
+ variant6_0 = 1;
238
+ variant6_1 = toUint32(e);
239
+ }
240
+ const ret = exports1.redact(ptr0, len0, variant5_0, variant5_1, variant5_2, variant6_0, variant6_1, v1_2 ? 1 : 0, v1_3 ? 1 : 0);
241
+ var len11 = dataView(memory0).getInt32(ret + 4, true);
242
+ var base11 = dataView(memory0).getInt32(ret + 0, true);
243
+ var result11 = [];
244
+ for (let i = 0; i < len11; i++) {
245
+ const base = base11 + i * 36;
246
+ var ptr7 = dataView(memory0).getInt32(base + 0, true);
247
+ var len7 = dataView(memory0).getInt32(base + 4, true);
248
+ var result7 = utf8Decoder.decode(new Uint8Array(memory0.buffer, ptr7, len7));
249
+ var ptr8 = dataView(memory0).getInt32(base + 8, true);
250
+ var len8 = dataView(memory0).getInt32(base + 12, true);
251
+ var result8 = utf8Decoder.decode(new Uint8Array(memory0.buffer, ptr8, len8));
252
+ let variant10;
253
+ switch (dataView(memory0).getUint8(base + 24, true)) {
254
+ case 0: {
255
+ variant10= {
256
+ tag: 'email',
257
+ };
258
+ break;
259
+ }
260
+ case 1: {
261
+ variant10= {
262
+ tag: 'phone-number',
263
+ };
264
+ break;
265
+ }
266
+ case 2: {
267
+ variant10= {
268
+ tag: 'ip-address',
269
+ };
270
+ break;
271
+ }
272
+ case 3: {
273
+ variant10= {
274
+ tag: 'credit-card-number',
275
+ };
276
+ break;
277
+ }
278
+ case 4: {
279
+ var ptr9 = dataView(memory0).getInt32(base + 28, true);
280
+ var len9 = dataView(memory0).getInt32(base + 32, true);
281
+ var result9 = utf8Decoder.decode(new Uint8Array(memory0.buffer, ptr9, len9));
282
+ variant10= {
283
+ tag: 'custom',
284
+ val: result9
285
+ };
286
+ break;
287
+ }
288
+ default: {
289
+ throw new TypeError('invalid variant discriminant for SensitiveInfoEntity');
290
+ }
291
+ }
292
+ result11.push({
293
+ original: result7,
294
+ redacted: result8,
295
+ start: dataView(memory0).getInt32(base + 16, true) >>> 0,
296
+ end: dataView(memory0).getInt32(base + 20, true) >>> 0,
297
+ identifiedType: variant10,
298
+ });
299
+ }
300
+ const retVal = result11;
301
+ postReturn0(ret);
302
+ return retVal;
303
+ }
304
+
305
+ return { redact, };
306
+ })();
307
+ let promise, resolve, reject;
308
+ function runNext (value) {
309
+ try {
310
+ let done;
311
+ do {
312
+ ({ value, done } = gen.next(value));
313
+ } while (!(value instanceof Promise) && !done);
314
+ if (done) {
315
+ if (resolve) resolve(value);
316
+ else return value;
317
+ }
318
+ if (!promise) promise = new Promise((_resolve, _reject) => (resolve = _resolve, reject = _reject));
319
+ value.then(nextVal => done ? resolve() : runNext(nextVal), reject);
320
+ }
321
+ catch (e) {
322
+ if (reject) reject(e);
323
+ else throw e;
324
+ }
325
+ }
326
+ const maybeSyncReturn = runNext(null);
327
+ return promise || maybeSyncReturn;
328
+ }
329
+
330
+ export { instantiate };
@@ -0,0 +1,21 @@
1
+ export namespace ArcjetRedactCustomRedact {
2
+ export function detectSensitiveInfo(tokens: Array<string>): Array<SensitiveInfoEntity | undefined>;
3
+ export function redactSensitiveInfo(entityType: SensitiveInfoEntity): string | undefined;
4
+ }
5
+ export type SensitiveInfoEntity = SensitiveInfoEntityEmail | SensitiveInfoEntityPhoneNumber | SensitiveInfoEntityIpAddress | SensitiveInfoEntityCreditCardNumber | SensitiveInfoEntityCustom;
6
+ export interface SensitiveInfoEntityEmail {
7
+ tag: 'email',
8
+ }
9
+ export interface SensitiveInfoEntityPhoneNumber {
10
+ tag: 'phone-number',
11
+ }
12
+ export interface SensitiveInfoEntityIpAddress {
13
+ tag: 'ip-address',
14
+ }
15
+ export interface SensitiveInfoEntityCreditCardNumber {
16
+ tag: 'credit-card-number',
17
+ }
18
+ export interface SensitiveInfoEntityCustom {
19
+ tag: 'custom',
20
+ val: string,
21
+ }
package/wasm.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Vercel uses the `.wasm?module` suffix to make WebAssembly available in their
3
+ * Vercel Functions product.
4
+ *
5
+ * https://vercel.com/docs/functions/wasm#using-a-webassembly-file
6
+ */
7
+ declare module "*.wasm?module" {
8
+ export default WebAssembly.Module;
9
+ }
10
+
11
+ /**
12
+ * The Cloudflare docs say they support the `.wasm?module` suffix, but that
13
+ * seems to no longer be the case with Wrangler 2 so we need to have separate
14
+ * imports for just the `.wasm` files.
15
+ *
16
+ * https://developers.cloudflare.com/workers/runtime-apis/webassembly/javascript/#bundling
17
+ */
18
+ declare module "*.wasm" {
19
+ export default WebAssembly.Module;
20
+ }
21
+
22
+ /**
23
+ * Our Rollup build turns `.wasm?js` imports into JS imports that provide the
24
+ * `wasm()` function which decodes a base64 Data URL into a WebAssembly Module
25
+ */
26
+ declare module "*.wasm?js" {
27
+ export function wasm(): Promise<WebAssembly.Module>;
28
+ }
package/workerd.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { RedactedSensitiveInfoEntity, RedactSensitiveInfoConfig, SensitiveInfoEntity } from "./wasm/arcjet_analyze_bindings_redact.component.js";
2
+ import type { ArcjetRedactCustomRedact } from "./wasm/interfaces/arcjet-redact-custom-redact.js";
3
+ export declare function initializeWasm(detect: CustomDetect, replace: CustomRedact): Promise<import("./wasm/arcjet_analyze_bindings_redact.component.js").Root | undefined>;
4
+ type CustomDetect = typeof ArcjetRedactCustomRedact.detectSensitiveInfo;
5
+ type CustomRedact = typeof ArcjetRedactCustomRedact.redactSensitiveInfo;
6
+ export { type CustomDetect, type CustomRedact, type RedactedSensitiveInfoEntity, type RedactSensitiveInfoConfig, type SensitiveInfoEntity, };
package/workerd.js ADDED
@@ -0,0 +1,34 @@
1
+ import { instantiate } from './wasm/arcjet_analyze_bindings_redact.component.js';
2
+ import componentCoreWasm from './wasm/arcjet_analyze_bindings_redact.component.core.wasm';
3
+ import componentCore2Wasm from './wasm/arcjet_analyze_bindings_redact.component.core2.wasm';
4
+ import componentCore3Wasm from './wasm/arcjet_analyze_bindings_redact.component.core3.wasm';
5
+
6
+ async function moduleFromPath(path) {
7
+ if (path === "arcjet_analyze_bindings_redact.component.core.wasm") {
8
+ return componentCoreWasm;
9
+ }
10
+ if (path === "arcjet_analyze_bindings_redact.component.core2.wasm") {
11
+ return componentCore2Wasm;
12
+ }
13
+ if (path === "arcjet_analyze_bindings_redact.component.core3.wasm") {
14
+ return componentCore3Wasm;
15
+ }
16
+ throw new Error(`Unknown path: ${path}`);
17
+ }
18
+ async function initializeWasm(detect, replace) {
19
+ const coreImports = {
20
+ "arcjet:redact/custom-redact": {
21
+ detectSensitiveInfo: detect,
22
+ redactSensitiveInfo: replace,
23
+ },
24
+ };
25
+ try {
26
+ // Await the instantiation to catch the failure
27
+ return await instantiate(moduleFromPath, coreImports);
28
+ }
29
+ catch {
30
+ console.debug("WebAssembly is not supported in this runtime");
31
+ }
32
+ }
33
+
34
+ export { initializeWasm };
package/workerd.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { instantiate } from "./wasm/arcjet_analyze_bindings_redact.component.js";
2
+ import type {
3
+ ImportObject,
4
+ RedactedSensitiveInfoEntity,
5
+ RedactSensitiveInfoConfig,
6
+ SensitiveInfoEntity,
7
+ } from "./wasm/arcjet_analyze_bindings_redact.component.js";
8
+ import type { ArcjetRedactCustomRedact } from "./wasm/interfaces/arcjet-redact-custom-redact.js";
9
+
10
+ import componentCoreWasm from "./wasm/arcjet_analyze_bindings_redact.component.core.wasm";
11
+ import componentCore2Wasm from "./wasm/arcjet_analyze_bindings_redact.component.core2.wasm";
12
+ import componentCore3Wasm from "./wasm/arcjet_analyze_bindings_redact.component.core3.wasm";
13
+
14
+ async function moduleFromPath(path: string): Promise<WebAssembly.Module> {
15
+ if (path === "arcjet_analyze_bindings_redact.component.core.wasm") {
16
+ return componentCoreWasm;
17
+ }
18
+ if (path === "arcjet_analyze_bindings_redact.component.core2.wasm") {
19
+ return componentCore2Wasm;
20
+ }
21
+ if (path === "arcjet_analyze_bindings_redact.component.core3.wasm") {
22
+ return componentCore3Wasm;
23
+ }
24
+
25
+ throw new Error(`Unknown path: ${path}`);
26
+ }
27
+
28
+ export async function initializeWasm(
29
+ detect: CustomDetect,
30
+ replace: CustomRedact,
31
+ ) {
32
+ const coreImports: ImportObject = {
33
+ "arcjet:redact/custom-redact": {
34
+ detectSensitiveInfo: detect,
35
+ redactSensitiveInfo: replace,
36
+ },
37
+ };
38
+
39
+ try {
40
+ // Await the instantiation to catch the failure
41
+ return await instantiate(moduleFromPath, coreImports);
42
+ } catch {
43
+ console.debug("WebAssembly is not supported in this runtime");
44
+ }
45
+ }
46
+
47
+ type CustomDetect = typeof ArcjetRedactCustomRedact.detectSensitiveInfo;
48
+ type CustomRedact = typeof ArcjetRedactCustomRedact.redactSensitiveInfo;
49
+
50
+ export {
51
+ type CustomDetect,
52
+ type CustomRedact,
53
+ type RedactedSensitiveInfoEntity,
54
+ type RedactSensitiveInfoConfig,
55
+ type SensitiveInfoEntity,
56
+ };