Skip to content

Commit 03b8d17

Browse files
Refactor the code for the netlify functions (#296)
* Refactor the code for the netlify functions - Created the zipRes_Uid function to reuse in various places * Removed hash and created Uid from the content of zip file * Using the store.config for generating the unique id * Pass the config in the body to the function * Convert to string * use JSON.stringify() --------- Co-authored-by: vfdev <vfdev.5@gmail.com>
1 parent 78444cd commit 03b8d17

File tree

5 files changed

+41
-65
lines changed

5 files changed

+41
-65
lines changed

functions/code.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
1-
// Below imports are defined in
2-
// `external_node_modules` of [functions] in netlify.toml
3-
// They are required for this function to run
4-
5-
import { v4 as uuidv4 } from 'uuid'
6-
import JSZip from 'jszip'
7-
import { pushToGitHub } from './utils'
8-
9-
const nbUid = uuidv4()
10-
const repoOwner = process.env.VUE_APP_GH_USER
11-
const repo = process.env.VUE_APP_GH_REPO
1+
import { getZip_Uid } from './utils'
122

133
// This function is the one Netlify function runs on
144
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
155
exports.handler = async function (event, _) {
166
// event is a JSON object
177
const data = JSON.parse(event.body)
18-
const zip = new JSZip()
19-
const code = data.code
20-
const template = `ignite-${data.template}`
8+
const { zipRes, nbUid } = await getZip_Uid(data)
219

22-
// As usual from Download component,
23-
// we will zip the files and
24-
// generate a base64 format for pushing to GitHub
25-
// with Octokit.
26-
for (const filename in code) {
27-
zip.file(filename, code[filename])
28-
}
29-
const content = await zip.generateAsync({ type: 'base64' })
30-
const zipRes = await pushToGitHub(
31-
content,
32-
`${template}.zip`,
33-
nbUid,
34-
repoOwner,
35-
repo
36-
)
3710
return {
3811
statusCode: 200,
3912
body: zipRes

functions/colab.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
// Below imports are defined in
2-
// `external_node_modules` of [functions] in netlify.toml
3-
// They are required for this function to run
4-
5-
import { v5 as uuidv5 } from 'uuid'
6-
import JSZip from 'jszip'
7-
import { pushToGitHub } from './utils'
1+
import { pushToGitHub, getZip_Uid } from './utils'
82

93
const repoOwner = process.env.VUE_APP_GH_USER
104
const repo = process.env.VUE_APP_GH_REPO
@@ -14,29 +8,9 @@ const repo = process.env.VUE_APP_GH_REPO
148
exports.handler = async function (event, _) {
159
// event is a JSON object
1610
const data = JSON.parse(event.body)
17-
const zip = new JSZip()
18-
const code = data.code
19-
let hash = ''
2011
const template = `ignite-${data.template}`
2112
const nbName = `${template}.ipynb`
22-
23-
// As usual from Download component,
24-
// we will zip the files and
25-
// generate a base64 format for pushing to GitHub
26-
// with Octokit.
27-
for (const filename in code) {
28-
hash += code[filename]
29-
zip.file(filename, code[filename])
30-
}
31-
const nbUid = uuidv5(hash, uuidv5.URL)
32-
const content = await zip.generateAsync({ type: 'base64' })
33-
const zipRes = await pushToGitHub(
34-
content,
35-
`${template}.zip`,
36-
nbUid,
37-
repoOwner,
38-
repo
39-
)
13+
const { zipRes, nbUid } = await getZip_Uid(data)
4014

4115
const title = template
4216
.replace('ignite-', '')
@@ -101,9 +75,7 @@ exports.handler = async function (event, _) {
10175
await pushToGitHub(
10276
Buffer.from(JSON.stringify(nb)).toString('base64'),
10377
nbName,
104-
nbUid,
105-
repoOwner,
106-
repo
78+
nbUid
10779
)
10880

10981
const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}`

functions/utils.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
// They are required for this function to run
44

55
import { Octokit } from '@octokit/core'
6+
import JSZip from 'jszip'
7+
import { v5 as uuidv5 } from 'uuid'
8+
9+
const repoOwner = process.env.VUE_APP_GH_USER
10+
const repo = process.env.VUE_APP_GH_REPO
611

712
/**
813
* Create a file on GitHub with Octokit.
914
* @param {string} content
1015
* @param {string} filename
1116
* @param {string} nbUid
12-
* @param {string} repoOwner
13-
* @param {string} repo
1417
* @returns download_url
1518
*/
16-
export async function pushToGitHub(content, filename, nbUid, repoOwner, repo) {
19+
export async function pushToGitHub(content, filename, nbUid) {
1720
const octokit = new Octokit({
1821
auth: process.env.VUE_APP_GH_TOKEN
1922
})
@@ -33,3 +36,29 @@ export async function pushToGitHub(content, filename, nbUid, repoOwner, repo) {
3336
console.error(e)
3437
}
3538
}
39+
40+
// This function is the one Netlify function runs on
41+
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
42+
export async function getZip_Uid(data) {
43+
const zip = new JSZip()
44+
const code = data.code
45+
const template = `ignite-${data.template}`
46+
47+
// As usual from Download component,
48+
// we will zip the files and
49+
// generate a base64 format for pushing to GitHub
50+
// with Octokit.
51+
for (const filename in code) {
52+
zip.file(filename, code[filename])
53+
}
54+
// since the generated zip varies every time even with the same code
55+
// it can't be used to generate a UUID
56+
const content = await zip.generateAsync({ type: 'base64' })
57+
// we generate an unique id from the current config for pushing to github
58+
const nbUid = uuidv5(JSON.stringify(data.config), uuidv5.URL)
59+
const zipRes = await pushToGitHub(content, `${template}.zip`, nbUid)
60+
return {
61+
zipRes: zipRes,
62+
nbUid: nbUid
63+
}
64+
}

src/components/NavCode.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ export default {
136136
},
137137
body: JSON.stringify({
138138
code: store.code,
139-
template: store.config.template
139+
template: store.config.template,
140+
config: store.config
140141
})
141142
})
142143
if (res.ok) {

src/components/NavColab.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export default {
9292
},
9393
body: JSON.stringify({
9494
code: store.code,
95-
template: store.config.template
95+
template: store.config.template,
96+
config: store.config
9697
})
9798
})
9899
// response body is plain text

0 commit comments

Comments
 (0)