@capgo/capacitor-ssl-pinning 8.0.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.
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { cp, mkdir, readdir, rm } from 'node:fs/promises';
4
+ import { basename, join, resolve } from 'node:path';
5
+
6
+ const rootDir = process.env.CAPACITOR_ROOT_DIR;
7
+ const webDir = process.env.CAPACITOR_WEB_DIR;
8
+ const configJson = process.env.CAPACITOR_CONFIG;
9
+
10
+ const log = (message) => console.log(`[ssl-pinning] ${message}`);
11
+ const fail = (message) => {
12
+ throw new Error(`[ssl-pinning] ${message}`);
13
+ };
14
+
15
+ if (!rootDir || !webDir || !configJson) {
16
+ log('Capacitor hook environment not detected, skipping certificate preparation.');
17
+ process.exit(0);
18
+ }
19
+
20
+ const config = JSON.parse(configJson);
21
+ const certs = config?.plugins?.SSLPinning?.certs;
22
+
23
+ if (!Array.isArray(certs) || certs.length === 0) {
24
+ log('No plugins.SSLPinning.certs entries found, skipping certificate preparation.');
25
+ process.exit(0);
26
+ }
27
+
28
+ const sourceFiles = certs.map((relativePath) => {
29
+ if (typeof relativePath !== 'string' || !relativePath.trim()) {
30
+ fail('All plugins.SSLPinning.certs entries must be non-empty strings.');
31
+ }
32
+
33
+ return {
34
+ source: resolve(rootDir, relativePath),
35
+ fileName: basename(relativePath),
36
+ };
37
+ });
38
+
39
+ const targetDir = join(webDir, 'certs');
40
+ await mkdir(targetDir, { recursive: true });
41
+
42
+ for (const existingEntry of await readdir(targetDir, { withFileTypes: true })) {
43
+ if (existingEntry.isFile()) {
44
+ await rm(join(targetDir, existingEntry.name), { force: true });
45
+ }
46
+ }
47
+
48
+ for (const { source, fileName } of sourceFiles) {
49
+ await cp(source, join(targetDir, fileName), { force: true });
50
+ log(`Copied ${fileName} into ${targetDir}`);
51
+ }