|
3 | 3 | const common = require('../common');
|
4 | 4 | const fixtures = require('../common/fixtures');
|
5 | 5 | const tmpdir = require('../common/tmpdir');
|
| 6 | +const { inspect } = require('util'); |
6 | 7 |
|
7 |
| -const { readFileSync } = require('fs'); |
| 8 | +const { readFileSync, copyFileSync } = require('fs'); |
8 | 9 | const {
|
9 | 10 | spawnSyncAndExitWithoutError,
|
10 | 11 | } = require('../common/child_process');
|
@@ -54,47 +55,75 @@ function skipIfSingleExecutableIsNotSupported() {
|
54 | 55 | }
|
55 | 56 | }
|
56 | 57 |
|
57 |
| -function injectAndCodeSign(targetExecutable, resource) { |
| 58 | +function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow = false) { |
| 59 | + try { |
| 60 | + copyFileSync(sourceExecutable, targetExecutable); |
| 61 | + } catch (e) { |
| 62 | + const message = `Cannot copy ${sourceExecutable} to ${targetExecutable}: ${inspect(e)}`; |
| 63 | + if (verifyWorkflow) { |
| 64 | + throw new Error(message); |
| 65 | + } |
| 66 | + common.skip(message); |
| 67 | + } |
| 68 | + console.log(`Copied ${sourceExecutable} to ${targetExecutable}`); |
| 69 | + |
58 | 70 | const postjectFile = fixtures.path('postject-copy', 'node_modules', 'postject', 'dist', 'cli.js');
|
59 |
| - spawnSyncAndExitWithoutError(process.execPath, [ |
60 |
| - postjectFile, |
61 |
| - targetExecutable, |
62 |
| - 'NODE_SEA_BLOB', |
63 |
| - resource, |
64 |
| - '--sentinel-fuse', 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2', |
65 |
| - ...process.platform === 'darwin' ? [ '--macho-segment-name', 'NODE_SEA' ] : [], |
66 |
| - ], {}); |
| 71 | + try { |
| 72 | + spawnSyncAndExitWithoutError(process.execPath, [ |
| 73 | + postjectFile, |
| 74 | + targetExecutable, |
| 75 | + 'NODE_SEA_BLOB', |
| 76 | + seaBlob, |
| 77 | + '--sentinel-fuse', 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2', |
| 78 | + ...process.platform === 'darwin' ? [ '--macho-segment-name', 'NODE_SEA' ] : [], |
| 79 | + ]); |
| 80 | + } catch (e) { |
| 81 | + const message = `Cannot inject ${seaBlob} into ${targetExecutable}: ${inspect(e)}`; |
| 82 | + if (verifyWorkflow) { |
| 83 | + throw new Error(message); |
| 84 | + } |
| 85 | + common.skip(message); |
| 86 | + } |
| 87 | + console.log(`Injected ${seaBlob} into ${targetExecutable}`); |
67 | 88 |
|
68 | 89 | if (process.platform === 'darwin') {
|
69 |
| - spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ], {}); |
70 |
| - spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ], {}); |
| 90 | + try { |
| 91 | + spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ], {}); |
| 92 | + spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ], {}); |
| 93 | + } catch (e) { |
| 94 | + const message = `Cannot sign ${targetExecutable}: ${inspect(e)}`; |
| 95 | + if (verifyWorkflow) { |
| 96 | + throw new Error(message); |
| 97 | + } |
| 98 | + common.skip(message); |
| 99 | + } |
| 100 | + console.log(`Signed ${targetExecutable}`); |
71 | 101 | } else if (process.platform === 'win32') {
|
72 |
| - let signtoolFound = false; |
73 | 102 | try {
|
74 | 103 | spawnSyncAndExitWithoutError('where', [ 'signtool' ], {});
|
75 |
| - signtoolFound = true; |
76 |
| - } catch (err) { |
77 |
| - console.log(err.message); |
78 |
| - } |
79 |
| - if (signtoolFound) { |
80 |
| - let certificatesFound = false; |
81 |
| - let stderr; |
82 |
| - try { |
83 |
| - ({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ], {})); |
84 |
| - certificatesFound = true; |
85 |
| - } catch (err) { |
86 |
| - if (!/SignTool Error: No certificates were found that met all the given criteria/.test(stderr)) { |
87 |
| - throw err; |
88 |
| - } |
| 104 | + } catch (e) { |
| 105 | + const message = `Cannot find signtool: ${inspect(e)}`; |
| 106 | + if (verifyWorkflow) { |
| 107 | + throw new Error(message); |
89 | 108 | }
|
90 |
| - if (certificatesFound) { |
91 |
| - spawnSyncAndExitWithoutError('signtool', 'verify', '/pa', 'SHA256', targetExecutable, {}); |
| 109 | + common.skip(message); |
| 110 | + } |
| 111 | + let stderr; |
| 112 | + try { |
| 113 | + ({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ], {})); |
| 114 | + spawnSyncAndExitWithoutError('signtool', 'verify', '/pa', 'SHA256', targetExecutable, {}); |
| 115 | + } catch (e) { |
| 116 | + const message = `Cannot sign ${targetExecutable}: ${inspect(e)}\n${stderr}`; |
| 117 | + if (verifyWorkflow) { |
| 118 | + throw new Error(message); |
92 | 119 | }
|
| 120 | + common.skip(message); |
93 | 121 | }
|
| 122 | + console.log(`Signed ${targetExecutable}`); |
94 | 123 | }
|
95 | 124 | }
|
96 | 125 |
|
97 | 126 | module.exports = {
|
98 | 127 | skipIfSingleExecutableIsNotSupported,
|
99 |
| - injectAndCodeSign, |
| 128 | + generateSEA, |
100 | 129 | };
|
0 commit comments