|
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 | | -const path = require('path') |
7 | | -const fs = require('fs') |
8 | 6 | const map = require('through2-map').obj |
9 | | -const pump = require('pump') |
10 | | -const archiver = require('archiver') |
11 | 7 |
|
12 | 8 | // a parallel transform stream segment ctor that hashes fileObj's created by folder-walker |
13 | 9 | exports.hasherCtor = ({ concurrentHash, hashAlgorithm = 'sha1' }) => { |
| 10 | + const hashaOpts = { algorithm: hashAlgorithm } |
14 | 11 | if (!concurrentHash) throw new Error('Missing required opts') |
15 | 12 | return transform(concurrentHash, { objectMode: true }, (fileObj, cb) => { |
16 | 13 | hasha |
17 | | - .fromFile(fileObj.filepath, { algorithm: hashAlgorithm }) |
| 14 | + .fromFile(fileObj.filepath, hashaOpts) |
18 | 15 | // insert hash and asset type to file obj |
19 | 16 | .then(hash => cb(null, Object.assign({}, fileObj, { hash }))) |
20 | 17 | .catch(err => cb(err)) |
@@ -56,67 +53,3 @@ exports.manifestCollectorCtor = (filesObj, shaMap, { statusCb, assetType }) => { |
56 | 53 | exports.fileFilterCtor = objFilterCtor(fileObj => { |
57 | 54 | return fileObj.type === 'file' |
58 | 55 | }) |
59 | | - |
60 | | -exports.fnFilterCtor = objFilterCtor(fileObj => { |
61 | | - // filter additional files out of our fn pipeline |
62 | | - return fileObj.type === 'file' && !!fileObj.runtime |
63 | | -}) |
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 | | -// parallel stream ctor similar to folder-walker but specialized for netlify functions |
83 | | -// Stream in names of files that may be functions, and this will stat the file and return a fileObj |
84 | | -exports.fnStatCtor = ({ root, concurrentStat, tmpDir }) => { |
85 | | - if (!concurrentStat || !root || !tmpDir) throw new Error('Missing required opts') |
86 | | - return transform(concurrentStat, { objectMode: true }, (name, cb) => { |
87 | | - const filepath = path.join(root, name) |
88 | | - fs.stat(filepath, (err, stat) => { |
89 | | - 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) |
120 | | - }) |
121 | | - }) |
122 | | -} |
0 commit comments