@@ -9,8 +9,9 @@ import { DenoBridge } from '../bridge.js'
99import { Bundle , BundleFormat } from '../bundle.js'
1010import { EdgeFunction } from '../edge_function.js'
1111import { FeatureFlags } from '../feature_flags.js'
12+ import { listRecursively } from '../utils/fs.js'
1213import { ImportMap } from '../import_map.js'
13- import { getDirectoryHash , getStringHash } from '../utils/sha256.js'
14+ import { getFileHash } from '../utils/sha256.js'
1415
1516const TARBALL_EXTENSION = '.tar'
1617
@@ -42,8 +43,8 @@ export const bundle = async ({
4243 importMap,
4344 vendorDirectory,
4445} : BundleTarballOptions ) : Promise < Bundle > => {
45- const sideFilesDir = await tmp . dir ( { unsafeCleanup : true } )
46- const cleanup = [ sideFilesDir . cleanup ]
46+ const bundleDir = await tmp . dir ( { unsafeCleanup : true } )
47+ const cleanup = [ bundleDir . cleanup ]
4748
4849 let denoDir = vendorDirectory ? path . join ( vendorDirectory , 'deno_dir' ) : undefined
4950
@@ -87,71 +88,47 @@ export const bundle = async ({
8788 '--quiet' ,
8889 '--code-splitting' ,
8990 '--outdir' ,
90- distDirectory ,
91+ bundleDir . path ,
9192 ...functions . map ( ( func ) => func . path ) ,
9293 ] ,
9394 {
9495 cwd : basePath ,
9596 } ,
9697 )
9798
98- const manifestPath = path . join ( sideFilesDir . path , 'manifest .json' )
99+ const manifestPath = path . join ( bundleDir . path , '___netlify-edge-functions .json' )
99100 const manifestContents = JSON . stringify ( manifest )
100101 await fs . writeFile ( manifestPath , manifestContents )
101102
102- const denoConfigPath = path . join ( sideFilesDir . path , 'deno.json' )
103+ const denoConfigPath = path . join ( bundleDir . path , 'deno.json' )
103104 const denoConfigContents = JSON . stringify ( importMap . getContentsWithRelativePaths ( ) )
104105 await fs . writeFile ( denoConfigPath , denoConfigContents )
105106
106- const rootLevel = await fs . readdir ( distDirectory )
107- const hash = await getDirectoryHash ( distDirectory )
108107 const tarballPath = path . join ( distDirectory , buildID + TARBALL_EXTENSION )
109-
110108 await fs . mkdir ( path . dirname ( tarballPath ) , { recursive : true } )
111109
112- // Adding all the bundled files.
113110 await tar . create (
114111 {
115- cwd : distDirectory ,
116- file : tarballPath ,
117- onWriteEntry ( entry ) {
118- entry . path = getUnixPath ( `./${ entry . path } ` )
119- } ,
120- } ,
121- rootLevel ,
122- )
123-
124- // Adding `deno.json`.
125- await tar . update (
126- {
127- cwd : distDirectory ,
112+ cwd : bundleDir . path ,
128113 file : tarballPath ,
114+ noDirRecurse : true ,
129115 onWriteEntry ( entry ) {
130- entry . path = './deno.json'
131- } ,
132- } ,
133- [ denoConfigPath ] ,
134- )
116+ const relativePath = path . relative ( bundleDir . path , path . join ( '/' , entry . path ) )
117+ const normalizedPath = getUnixPath ( relativePath )
135118
136- // Adding the manifest file.
137- await tar . update (
138- {
139- cwd : distDirectory ,
140- file : tarballPath ,
141- onWriteEntry ( entry ) {
142- entry . path = './___netlify-edge-functions.json'
119+ entry . path = normalizedPath
143120 } ,
144121 } ,
145- [ manifestPath ] ,
122+ await listRecursively ( bundleDir . path ) ,
146123 )
147124
148- await Promise . all ( cleanup )
125+ const hash = await getFileHash ( tarballPath )
149126
150- const finalHash = [ hash , getStringHash ( manifestContents ) , getStringHash ( denoConfigContents ) ] . join ( '' )
127+ await Promise . allSettled ( cleanup )
151128
152129 return {
153130 extension : TARBALL_EXTENSION ,
154131 format : BundleFormat . TARBALL ,
155- hash : finalHash ,
132+ hash,
156133 }
157134}
0 commit comments