Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit c0c14f5

Browse files
committed
Delegate function scanning to zip-n-ship
1 parent 37d9be6 commit c0c14f5

File tree

2 files changed

+19
-48
lines changed

2 files changed

+19
-48
lines changed

src/deploy/hash-fns.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const promisifyAll = require('util.promisify-all')
21
const promisify = require('util.promisify')
32
const pump = promisify(require('pump'))
4-
const fs = promisifyAll(require('fs'))
53
const fromArray = require('from2-array')
4+
const zipIt = require('@netlify/zip-it-and-ship-it')
5+
const path = require('path')
66

7-
const { hasherCtor, manifestCollectorCtor, fnStatCtor } = require('./hasher-segments')
7+
const { hasherCtor, manifestCollectorCtor } = require('./hasher-segments')
88

99
module.exports = hashFns
1010
async function hashFns(dir, opts) {
@@ -20,21 +20,32 @@ async function hashFns(dir, opts) {
2020
)
2121
// early out if the functions dir is omitted
2222
if (!dir) return { functions: {}, shaMap: {} }
23-
if (!opts.filter) throw new Error('Missing required filter function')
2423
if (!opts.tmpDir) throw new Error('Missing tmpDir directory for zipping files')
2524

26-
const fileList = await fs.readdir(dir).then(files => files.filter(opts.filter))
27-
const fileStream = fromArray.obj(fileList)
25+
const functionZips = await zipIt.zipFunctions(dir, opts.tmpDir)
26+
27+
const fileObjs = functionZips.map(({ path: functionPath, runtime }) => ({
28+
filepath: functionPath,
29+
root: opts.tmpDir,
30+
relname: path.relative(opts.tmpDir, functionPath),
31+
basename: path.basename(functionPath),
32+
extname: path.extname(functionPath),
33+
type: 'file',
34+
assetType: 'function',
35+
normalizedPath: path.basename(functionPath, path.extname(functionPath)),
36+
runtime
37+
}))
38+
39+
const functionStream = fromArray.obj(fileObjs)
2840

29-
const fnStat = fnStatCtor({ root: dir, concurrentStat: opts.concurrentHash, tmpDir: opts.tmpDir })
3041
const hasher = hasherCtor(opts)
3142

3243
// Written to by manifestCollector
3344
const functions = {} // normalizedPath: hash (wanted by deploy API)
3445
const fnShaMap = {} //hash: [fileObj, fileObj, fileObj]
3546
const manifestCollector = manifestCollectorCtor(functions, fnShaMap, opts)
3647

37-
await pump(fileStream, fnStat, hasher, manifestCollector)
48+
await pump(functionStream, hasher, manifestCollector)
3849

3950
return { functions, fnShaMap }
4051
}

src/deploy/hasher-segments.js

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ const objWriter = require('flush-write-stream').obj
33
const { normalizePath } = require('./util')
44
const transform = require('parallel-transform')
55
const hasha = require('hasha')
6-
const path = require('path')
7-
const fs = require('fs')
86
const map = require('through2-map').obj
9-
const zipIt = require('@netlify/zip-it-and-ship-it')
107

118
// a parallel transform stream segment ctor that hashes fileObj's created by folder-walker
129
exports.hasherCtor = ({ concurrentHash, hashAlgorithm = 'sha1' }) => {
@@ -61,40 +58,3 @@ exports.fnFilterCtor = objFilterCtor(fileObj => {
6158
// filter additional files out of our fn pipeline
6259
return fileObj && fileObj.type === 'file' && !!fileObj.runtime
6360
})
64-
65-
// parallel stream ctor similar to folder-walker but specialized for netlify functions
66-
// Stream in names of files that may be functions, and this will stat the file and return a fileObj
67-
exports.fnStatCtor = ({ root, concurrentStat, tmpDir }) => {
68-
if (!concurrentStat || !root || !tmpDir) throw new Error('Missing required opts')
69-
return transform(concurrentStat, { objectMode: true }, (name, cb) => {
70-
const filepath = path.resolve(path.join(root, name))
71-
fs.stat(filepath, (err, stat) => {
72-
if (err) return cb(err)
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-
})
98-
})
99-
})
100-
}

0 commit comments

Comments
 (0)