@@ -8,14 +8,7 @@ import events from 'node:events';
8
8
import path from 'node:path' ;
9
9
import https from 'node:https' ;
10
10
import stream from 'node:stream/promises' ;
11
- import url from 'node:url' ;
12
-
13
- const __dirname = path . dirname ( url . fileURLToPath ( import . meta. url ) ) ;
14
-
15
- /** Resolves to the root of this repository */
16
- function resolveRoot ( ...paths ) {
17
- return path . resolve ( __dirname , '..' , '..' , ...paths ) ;
18
- }
11
+ import { buildLibmongocryptDownloadUrl , getLibmongocryptPrebuildName , resolveRoot , run } from './utils.mjs' ;
19
12
20
13
async function parseArguments ( ) {
21
14
const pkg = JSON . parse ( await fs . readFile ( resolveRoot ( 'package.json' ) , 'utf8' ) ) ;
@@ -26,7 +19,6 @@ async function parseArguments() {
26
19
clean : { short : 'c' , type : 'boolean' , default : false } ,
27
20
build : { short : 'b' , type : 'boolean' , default : false } ,
28
21
dynamic : { type : 'boolean' , default : false } ,
29
- fastDownload : { type : 'boolean' , default : false } , // Potentially incorrect download, only for the brave and impatient
30
22
'skip-bindings' : { type : 'boolean' , default : false } ,
31
23
help : { short : 'h' , type : 'boolean' , default : false }
32
24
} ;
@@ -46,7 +38,6 @@ async function parseArguments() {
46
38
return {
47
39
url : args . values . gitURL ,
48
40
ref : args . values . libVersion ,
49
- fastDownload : args . values . fastDownload ,
50
41
clean : args . values . clean ,
51
42
build : args . values . build ,
52
43
dynamic : args . values . dynamic ,
@@ -55,26 +46,6 @@ async function parseArguments() {
55
46
} ;
56
47
}
57
48
58
- /** `xtrace` style command runner, uses spawn so that stdio is inherited */
59
- async function run ( command , args = [ ] , options = { } ) {
60
- const commandDetails = `+ ${ command } ${ args . join ( ' ' ) } ${ options . cwd ? ` (in: ${ options . cwd } )` : '' } ` ;
61
- console . error ( commandDetails ) ;
62
- const proc = child_process . spawn ( command , args , {
63
- shell : process . platform === 'win32' ,
64
- stdio : 'inherit' ,
65
- cwd : resolveRoot ( '.' ) ,
66
- ...options
67
- } ) ;
68
- await events . once ( proc , 'exit' ) ;
69
-
70
- if ( proc . exitCode != 0 ) throw new Error ( `CRASH(${ proc . exitCode } ): ${ commandDetails } ` ) ;
71
- }
72
-
73
- /** CLI flag maker: `toFlags({a: 1, b: 2})` yields `['-a=1', '-b=2']` */
74
- function toFlags ( object ) {
75
- return Array . from ( Object . entries ( object ) ) . map ( ( [ k , v ] ) => `-${ k } =${ v } ` ) ;
76
- }
77
-
78
49
export async function cloneLibMongoCrypt ( libmongocryptRoot , { url, ref } ) {
79
50
console . error ( 'fetching libmongocrypt...' , { url, ref } ) ;
80
51
await fs . rm ( libmongocryptRoot , { recursive : true , force : true } ) ;
@@ -87,14 +58,19 @@ export async function cloneLibMongoCrypt(libmongocryptRoot, { url, ref }) {
87
58
}
88
59
89
60
export async function buildLibMongoCrypt ( libmongocryptRoot , nodeDepsRoot , options ) {
61
+ /** CLI flag maker: `toFlags({a: 1, b: 2})` yields `['-a=1', '-b=2']` */
62
+ function toCLIFlags ( object ) {
63
+ return Array . from ( Object . entries ( object ) ) . map ( ( [ k , v ] ) => `-${ k } =${ v } ` ) ;
64
+ }
65
+
90
66
console . error ( 'building libmongocrypt...' ) ;
91
67
92
68
const nodeBuildRoot = resolveRoot ( nodeDepsRoot , 'tmp' , 'libmongocrypt-build' ) ;
93
69
94
70
await fs . rm ( nodeBuildRoot , { recursive : true , force : true } ) ;
95
71
await fs . mkdir ( nodeBuildRoot , { recursive : true } ) ;
96
72
97
- const CMAKE_FLAGS = toFlags ( {
73
+ const CMAKE_FLAGS = toCLIFlags ( {
98
74
/**
99
75
* We provide crypto hooks from Node.js binding to openssl (so disable system crypto)
100
76
* TODO: NODE-5455
@@ -127,12 +103,12 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, option
127
103
128
104
const WINDOWS_CMAKE_FLAGS =
129
105
process . platform === 'win32' // Windows is still called "win32" when it is 64-bit
130
- ? toFlags ( { Thost : 'x64' , A : 'x64' , DENABLE_WINDOWS_STATIC_RUNTIME : 'ON' } )
106
+ ? toCLIFlags ( { Thost : 'x64' , A : 'x64' , DENABLE_WINDOWS_STATIC_RUNTIME : 'ON' } )
131
107
: [ ] ;
132
108
133
109
const DARWIN_CMAKE_FLAGS =
134
110
process . platform === 'darwin' // The minimum darwin target version we want for
135
- ? toFlags ( { DCMAKE_OSX_DEPLOYMENT_TARGET : '10.12' } )
111
+ ? toCLIFlags ( { DCMAKE_OSX_DEPLOYMENT_TARGET : '10.12' } )
136
112
: [ ] ;
137
113
138
114
const cmakeProgram = process . platform === 'win32' ? 'cmake.exe' : 'cmake' ;
@@ -149,35 +125,18 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, option
149
125
} ) ;
150
126
}
151
127
152
- export async function downloadLibMongoCrypt ( nodeDepsRoot , { ref, fastDownload } ) {
153
- const downloadURL =
154
- ref === 'latest'
155
- ? 'https://mciuploads.s3.amazonaws.com/libmongocrypt/all/master/latest/libmongocrypt-all.tar.gz'
156
- : `https://mciuploads.s3.amazonaws.com/libmongocrypt/all/${ ref } /libmongocrypt-all.tar.gz` ;
128
+ export async function downloadLibMongoCrypt ( nodeDepsRoot , { ref } ) {
129
+ const prebuild = getLibmongocryptPrebuildName ( ) ;
130
+
131
+ const downloadURL = buildLibmongocryptDownloadUrl ( ref , prebuild ) ;
157
132
158
133
console . error ( 'downloading libmongocrypt...' , downloadURL ) ;
159
134
const destination = resolveRoot ( `_libmongocrypt-${ ref } ` ) ;
160
135
161
136
await fs . rm ( destination , { recursive : true , force : true } ) ;
162
137
await fs . mkdir ( destination ) ;
163
138
164
- const platformMatrix = {
165
- [ 'darwin-arm64' ] : 'macos' ,
166
- [ 'darwin-x64' ] : 'macos' ,
167
- [ 'linux-ppc64' ] : 'rhel-71-ppc64el' ,
168
- [ 'linux-s390x' ] : 'rhel72-zseries-test' ,
169
- [ 'linux-arm64' ] : 'ubuntu1804-arm64' ,
170
- [ 'linux-x64' ] : 'rhel-70-64-bit' ,
171
- [ 'win32-x64' ] : 'windows-test'
172
- } ;
173
-
174
- const detectedPlatform = `${ process . platform } -${ process . arch } ` ;
175
- const prebuild = platformMatrix [ detectedPlatform ] ;
176
- if ( prebuild == null ) throw new Error ( `Unsupported: ${ detectedPlatform } ` ) ;
177
-
178
- console . error ( `Platform: ${ detectedPlatform } Prebuild: ${ prebuild } ` ) ;
179
-
180
- const downloadDestination = `${ prebuild } /nocrypto` ;
139
+ const downloadDestination = `nocrypto` ;
181
140
const unzipArgs = [ '-xzv' , '-C' , `_libmongocrypt-${ ref } ` , downloadDestination ] ;
182
141
console . error ( `+ tar ${ unzipArgs . join ( ' ' ) } ` ) ;
183
142
const unzip = child_process . spawn ( 'tar' , unzipArgs , {
@@ -190,35 +149,8 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref, fastDownload })
190
149
191
150
const start = performance . now ( ) ;
192
151
193
- let signal ;
194
- if ( fastDownload ) {
195
- /**
196
- * Tar will print out each file it finds inside MEMBER (ex. macos/nocrypto)
197
- * For each file it prints, we give it a deadline of 3 seconds to print the next one.
198
- * If nothing prints after 3 seconds we exit early.
199
- * This depends on the tar file being in order and un-tar-able in under 3sec.
200
- */
201
- const controller = new AbortController ( ) ;
202
- signal = controller . signal ;
203
- let firstMemberSeen = true ;
204
- let timeout ;
205
- unzip . stderr . on ( 'data' , chunk => {
206
- process . stderr . write ( chunk , ( ) => {
207
- if ( firstMemberSeen ) {
208
- firstMemberSeen = false ;
209
- timeout = setTimeout ( ( ) => {
210
- clearTimeout ( timeout ) ;
211
- unzip . stderr . removeAllListeners ( 'data' ) ;
212
- controller . abort ( ) ;
213
- } , 3_000 ) ;
214
- }
215
- timeout ?. refresh ( ) ;
216
- } ) ;
217
- } ) ;
218
- }
219
-
220
152
try {
221
- await stream . pipeline ( response , unzip . stdin , { signal } ) ;
153
+ await stream . pipeline ( response , unzip . stdin ) ;
222
154
} catch {
223
155
await fs . access ( path . join ( `_libmongocrypt-${ ref } ` , downloadDestination ) ) ;
224
156
}
@@ -228,7 +160,7 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref, fastDownload })
228
160
console . error ( `downloaded libmongocrypt in ${ ( end - start ) / 1000 } secs...` ) ;
229
161
230
162
await fs . rm ( nodeDepsRoot , { recursive : true , force : true } ) ;
231
- await fs . cp ( resolveRoot ( destination , prebuild , 'nocrypto' ) , nodeDepsRoot , { recursive : true } ) ;
163
+ await fs . cp ( resolveRoot ( destination , 'nocrypto' ) , nodeDepsRoot , { recursive : true } ) ;
232
164
const potentialLib64Path = path . join ( nodeDepsRoot , 'lib64' ) ;
233
165
try {
234
166
await fs . rename ( potentialLib64Path , path . join ( nodeDepsRoot , 'lib' ) ) ;
0 commit comments