|
| 1 | +/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with the Apache |
| 6 | + * License, Version 2.0 (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + *****************************************************************************/ |
| 19 | + |
| 20 | +'use strict'; |
| 21 | + |
| 22 | +const fs = require('fs'); |
| 23 | +const Readable = require('stream').Readable; |
| 24 | +const zlib = require('zlib'); |
| 25 | +const path = require('path'); |
| 26 | +const util = require('util'); |
| 27 | +const packageUtil = require('./util.js'); |
| 28 | + |
| 29 | +packageUtil.initDynamicProps(); |
| 30 | + |
| 31 | +// writeFileToReadable is used to buffer the contents of files to a readable |
| 32 | +// stream that can be written out later. |
| 33 | +function writeFileToReadable(path, readable) { |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + packageUtil.trace('In writeFileToReadable', path, util.inspect(readable, {depth: 0})); |
| 36 | + |
| 37 | + const rs = fs.createReadStream(path); |
| 38 | + |
| 39 | + rs.on('data', chunk => { |
| 40 | + readable.push(chunk); |
| 41 | + }); |
| 42 | + |
| 43 | + rs.on('error', err => { |
| 44 | + reject(err); |
| 45 | + }); |
| 46 | + |
| 47 | + rs.on('close', () => { |
| 48 | + resolve(); |
| 49 | + }); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +// createPackage is used to create a custom file that combines the node-oracledb |
| 54 | +// binary with the license. This function is meant to be used at the command line. |
| 55 | +function createPackage() { |
| 56 | + packageUtil.trace('In createPackage'); |
| 57 | + |
| 58 | + let binaryPath = packageUtil.BINARY_PATH_LOCAL; |
| 59 | + |
| 60 | + for (let x = 2; x < process.argv.length; x += 1) { |
| 61 | + let argParts = process.argv[x].split('='); |
| 62 | + let argName = argParts[0]; |
| 63 | + let argVal = argParts[1]; |
| 64 | + |
| 65 | + if (argName === 'path') { |
| 66 | + binaryPath = argVal; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (!binaryPath.endsWith(packageUtil.BINARY_FILE_NAME)) { |
| 71 | + throw new Error('path should resolve to a file named ' + packageUtil.BINARY_FILE_NAME); |
| 72 | + } |
| 73 | + |
| 74 | + let binaryStats = fs.statSync(binaryPath); |
| 75 | + |
| 76 | + if (!binaryStats.isFile()) { |
| 77 | + throw new Error('path did not resolve to a file'); |
| 78 | + } |
| 79 | + |
| 80 | + // Requiring in the binary ensures that it matches the version of Node.js currently |
| 81 | + // running. This is important as the process variable is used to file naming. |
| 82 | + require(path.normalize(binaryPath + '/../../../')); |
| 83 | + |
| 84 | + let licensePath; |
| 85 | + |
| 86 | + if (binaryPath != packageUtil.BINARY_PATH_LOCAL) { |
| 87 | + licensePath = path.normalize(binaryPath + '/../../../' + packageUtil.LICENSE_FILE_NAME); |
| 88 | + } else { |
| 89 | + licensePath = packageUtil.LICENSE_PATH_LOCAL; |
| 90 | + } |
| 91 | + |
| 92 | + class TempReadable extends Readable { |
| 93 | + constructor(options) { |
| 94 | + super(options); |
| 95 | + } |
| 96 | + |
| 97 | + _read(size) {} // Must be implemented but not used |
| 98 | + } |
| 99 | + |
| 100 | + const tempReadable = new TempReadable({ |
| 101 | + highWaterMark: 1048576 // 1 MB |
| 102 | + }); |
| 103 | + |
| 104 | + packageUtil.getSha(binaryPath) |
| 105 | + .then(binarySha => { |
| 106 | + const newShaLine = binarySha + ' ' + packageUtil.dynamicProps.BINARY_BUILD_NAME; |
| 107 | + let shaFileContents; |
| 108 | + |
| 109 | + try { |
| 110 | + shaFileContents = fs.readFileSync(packageUtil.SHA_FILE_NAME, {encoding: 'utf8'}); |
| 111 | + shaFileContents = shaFileContents.split('\n'); |
| 112 | + |
| 113 | + let updatedLine = false; |
| 114 | + |
| 115 | + for (let x = 0; x < shaFileContents.length; x += 1) { |
| 116 | + const line = shaFileContents[x]; |
| 117 | + |
| 118 | + if (line.split(' ')[1] === packageUtil.dynamicProps.BINARY_BUILD_NAME) { |
| 119 | + shaFileContents[x] = newShaLine; |
| 120 | + updatedLine = true; |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (!updatedLine) { |
| 126 | + shaFileContents.splice(shaFileContents.length - 1, 0, newShaLine); |
| 127 | + } |
| 128 | + |
| 129 | + shaFileContents = shaFileContents.join('\n'); |
| 130 | + } catch (err) { |
| 131 | + shaFileContents = newShaLine + '\n'; |
| 132 | + } |
| 133 | + |
| 134 | + fs.writeFileSync(packageUtil.SHA_FILE_NAME, shaFileContents); |
| 135 | + |
| 136 | + let stats = fs.statSync(licensePath); |
| 137 | + let licenseSize = stats.size.toString(); |
| 138 | + |
| 139 | + let zerosToAppend = packageUtil.LICENSE_HEADER_BYTES - licenseSize.length; |
| 140 | + let paddedZeros = ''; |
| 141 | + |
| 142 | + for (let x = 0; x < zerosToAppend; x += 1) { |
| 143 | + paddedZeros += '0'; |
| 144 | + } |
| 145 | + |
| 146 | + licenseSize = paddedZeros + licenseSize; |
| 147 | + |
| 148 | + // The following line generates an error on Node.js 4.0, but not 4.8.5. Not sure |
| 149 | + // when the correct API was added. |
| 150 | + const licenseSizeBuf = Buffer.from(licenseSize, 'ascii'); |
| 151 | + |
| 152 | + tempReadable.push(licenseSizeBuf); |
| 153 | + }) |
| 154 | + .then(() => { |
| 155 | + return writeFileToReadable(licensePath, tempReadable); |
| 156 | + }) |
| 157 | + .then(() => { |
| 158 | + return writeFileToReadable(binaryPath, tempReadable); |
| 159 | + }) |
| 160 | + .then(() => { |
| 161 | + const ws = fs.createWriteStream(packageUtil.dynamicProps.PACKAGE_FILE_NAME); |
| 162 | + const gzip = zlib.createGzip(); |
| 163 | + const filestream = tempReadable.pipe(gzip).pipe(ws); |
| 164 | + |
| 165 | + tempReadable.push(null); // Signal the end of data in |
| 166 | + |
| 167 | + filestream.on('close', () => { |
| 168 | + console.log('Package created: ' + packageUtil.dynamicProps.PACKAGE_FILE_NAME); |
| 169 | + }); |
| 170 | + }) |
| 171 | + .catch(err => { |
| 172 | + console.log('Error creating package', err); |
| 173 | + }) |
| 174 | +} |
| 175 | + |
| 176 | +createPackage(); |
0 commit comments