|
1 | 1 | const objFilterCtor = require('through2-filter').objCtor |
2 | 2 | const objWriter = require('flush-write-stream').obj |
3 | | -const { isExe, normalizePath } = require('./util') |
| 3 | +const { normalizePath } = require('./util') |
4 | 4 | const transform = require('parallel-transform') |
5 | 5 | const hasha = require('hasha') |
6 | 6 | const path = require('path') |
7 | 7 | const fs = require('fs') |
8 | 8 | const map = require('through2-map').obj |
9 | | -const pump = require('pump') |
10 | | -const archiver = require('archiver') |
| 9 | +const zipIt = require('zip-it-and-ship-it') |
11 | 10 |
|
12 | 11 | // a parallel transform stream segment ctor that hashes fileObj's created by folder-walker |
13 | 12 | exports.hasherCtor = ({ concurrentHash, hashAlgorithm = 'sha1' }) => { |
| 13 | + const hashaOpts = { algorithm: hashAlgorithm } |
14 | 14 | if (!concurrentHash) throw new Error('Missing required opts') |
15 | 15 | return transform(concurrentHash, { objectMode: true }, (fileObj, cb) => { |
16 | 16 | hasha |
17 | | - .fromFile(fileObj.filepath, { algorithm: hashAlgorithm }) |
| 17 | + .fromFile(fileObj.filepath, hashaOpts) |
18 | 18 | // insert hash and asset type to file obj |
19 | 19 | .then(hash => cb(null, Object.assign({}, fileObj, { hash }))) |
20 | 20 | .catch(err => cb(err)) |
@@ -59,64 +59,42 @@ exports.fileFilterCtor = objFilterCtor(fileObj => { |
59 | 59 |
|
60 | 60 | exports.fnFilterCtor = objFilterCtor(fileObj => { |
61 | 61 | // filter additional files out of our fn pipeline |
62 | | - return fileObj.type === 'file' && !!fileObj.runtime |
| 62 | + return fileObj && fileObj.type === 'file' && !!fileObj.runtime |
63 | 63 | }) |
64 | 64 |
|
65 | | -// Zip a file into a temporary directory |
66 | | -function zipFunction(item, tmpDir, cb) { |
67 | | - const zipPath = path.join(tmpDir, item.normalizedPath + '.zip') |
68 | | - const output = fs.createWriteStream(zipPath) |
69 | | - const archive = archiver('zip') |
70 | | - |
71 | | - archive.file(item.filepath, { name: item.basename }) |
72 | | - archive.finalize() |
73 | | - |
74 | | - pump(archive, output, err => { |
75 | | - if (err) return cb(err) |
76 | | - |
77 | | - item.filepath = zipPath |
78 | | - cb(null, item) |
79 | | - }) |
80 | | -} |
81 | | - |
82 | 65 | // parallel stream ctor similar to folder-walker but specialized for netlify functions |
83 | 66 | // Stream in names of files that may be functions, and this will stat the file and return a fileObj |
84 | 67 | exports.fnStatCtor = ({ root, concurrentStat, tmpDir }) => { |
85 | 68 | if (!concurrentStat || !root || !tmpDir) throw new Error('Missing required opts') |
86 | 69 | return transform(concurrentStat, { objectMode: true }, (name, cb) => { |
87 | | - const filepath = path.join(root, name) |
| 70 | + const filepath = path.resolve(path.join(root, name)) |
88 | 71 | fs.stat(filepath, (err, stat) => { |
89 | 72 | if (err) return cb(err) |
90 | | - |
91 | | - const item = { |
92 | | - root, |
93 | | - filepath, |
94 | | - stat, |
95 | | - relname: path.relative(root, filepath), |
96 | | - basename: path.basename(name), |
97 | | - extname: path.extname(name), |
98 | | - type: stat.isFile() ? 'file' : stat.isDirectory() ? 'directory' : null, |
99 | | - assetType: 'function', |
100 | | - normalizedPath: path.basename(name, path.extname(name)) |
101 | | - } |
102 | | - |
103 | | - if (['.zip'].some(ext => item.extname === ext)) { |
104 | | - item.runtime = 'js' |
105 | | - return cb(null, item) |
106 | | - } |
107 | | - |
108 | | - if (['.js'].some(ext => item.extname === ext)) { |
109 | | - item.runtime = 'js' |
110 | | - |
111 | | - return zipFunction(item, tmpDir, cb) |
112 | | - } |
113 | | - |
114 | | - if (isExe(item.stat)) { |
115 | | - item.runtime = 'go' |
116 | | - return zipFunction(item, tmpDir, cb) |
117 | | - } |
118 | | - |
119 | | - return cb(null, item) |
| 73 | + zipIt |
| 74 | + .zipFunction(filepath, tmpDir) |
| 75 | + .then(functionZip => { |
| 76 | + if (functionZip === null) { |
| 77 | + return cb(null, null) |
| 78 | + } |
| 79 | + |
| 80 | + const item = { |
| 81 | + root, |
| 82 | + filepath: functionZip.path, |
| 83 | + stat, |
| 84 | + relname: path.relative(root, filepath), |
| 85 | + basename: path.basename(name), |
| 86 | + extname: path.extname(name), |
| 87 | + type: 'file', |
| 88 | + assetType: 'function', |
| 89 | + normalizedPath: path.basename(name, path.extname(name)), |
| 90 | + runtime: functionZip.runtime |
| 91 | + } |
| 92 | + |
| 93 | + cb(null, item) |
| 94 | + }) |
| 95 | + .catch(err => { |
| 96 | + cb(err, null) |
| 97 | + }) |
120 | 98 | }) |
121 | 99 | }) |
122 | 100 | } |
0 commit comments