This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
238 lines (216 loc) · 7.74 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const crypto = require("crypto");
var azure = require('azure-storage');
var path = require('path');
var mkdirp = require('mkdirp');
var { createFileNode } = require('gatsby-source-filesystem/create-file-node');
const getValueWithDefault = (valueItem, defaultValue) => { return ((valueItem || { _: defaultValue })._ || defaultValue) }
const getValue = valueItem => getValueWithDefault(valueItem, null)
function makeTableNode(createNode, createNodeId, tableName, tableType) {
const item = {
name: tableName,
type: tableType
}
const nodeId = createNodeId('azureTable/' + tableName)
const nodeContent = JSON.stringify(item)
const nodeContentDigest = crypto
.createHash('md5')
.update(nodeContent)
.digest('hex')
const nodeData = Object.assign(item, {
id: nodeId,
parent: null,
children: [],
internal: {
type: 'azureTable',
content: nodeContent,
contentDigest: nodeContentDigest,
},
})
createNode(nodeData)
}
function makeContainerNode(createNode, createNodeId, containerName, localFolder) {
const item = {
name: containerName,
localFolder: localFolder
}
const nodeId = createNodeId('azureContainer/' + containerName)
const nodeContent = JSON.stringify(item)
const nodeContentDigest = crypto
.createHash('md5')
.update(nodeContent)
.digest('hex')
const nodeData = Object.assign(item, {
id: nodeId,
parent: null,
children: [],
internal: {
type: 'azureContainer',
content: nodeContent,
contentDigest: nodeContentDigest,
},
})
createNode(nodeData)
}
function downloadBlobFile(createNode, createNodeId, blobService, { container, name, localPath }) {
mkdirp.sync(path.dirname(localPath, createNodeId));
return new Promise(function (resolve, reject) {
try {
blobService.getBlobToLocalFile(container, name, localPath, function (error, result, response) {
if (!error) {
createFileNode(localPath, createNodeId, pluginOptions = {
name: "gatsby-source-azure-storage"
}).then(function (node) {
let publicUrl = blobService.getUrl(container, name)
let nodeWithUrl = Object.assign({ url: publicUrl}, node)
createNode(nodeWithUrl)
resolve()
}, function (failure) {
console.error(` Failed creating node from blob "${name}" from container "${container}"`)
reject(failure)
})
} else {
console.error(` Failed downloading blob "${name}" from container "${container}"`)
reject(error)
}
})
} catch (err) {
console.error(` Failed to download blob "${name}" from container "${container}"`)
reject(err)
}
})
}
function makeNodesFromQuery(createNode, createNodeId, tableService, tableName, typeName) {
return new Promise(function (resolve, reject) {
try {
const query = new azure.TableQuery()
function queryWithToken(token) {
tableService.queryEntities(tableName, query, token, function (error, result, response) {
if (!error) {
result.entries.forEach(value => {
const item = Object.entries(value).reduce((o, prop) => ({ ...o, [prop[0]]: getValue(prop[1]) }), {})
const nodeId = createNodeId(`${item.PartitionKey}/${item.RowKey}`)
const nodeContent = JSON.stringify(item)
const nodeContentDigest = crypto
.createHash('md5')
.update(nodeContent)
.digest('hex')
const nodeData = Object.assign(item, {
id: nodeId,
parent: null,
children: [],
internal: {
type: typeName,
content: nodeContent,
contentDigest: nodeContentDigest,
},
})
createNode(nodeData)
})
if (result.continuationToken == null) {
resolve()
} else {
queryWithToken(result.continuationToken)
}
} else {
console.error(` Unable to query table "${tableName}"`)
reject(error)
}
})
}
queryWithToken(null)
} catch (err) {
console.error(` Error on table "${tableName}"`)
reject(err)
}
})
}
function makeNodesFromContainer(createNode, createNodeId, blobService, containerName, downloadFolder) {
return new Promise(function (resolve, reject) {
try {
function queryWithToken(token = null, nodes = []) {
blobService.listBlobsSegmented(containerName, token, function (error, result, response) {
if (!error) {
result.entries.forEach(value => {
const item = {
name: value.name,
container: value.container || containerName,
contentMD5: value.contentSettings.contentMD5,
creationTime: value.creationTime,
lastModified: value.lastModified,
blobType: value.blobType,
serverEncrypted: value.serverEncrypted,
localPath: (downloadFolder == null ? null : path.join(process.cwd(), downloadFolder, value.name))
}
const nodeId = createNodeId(`${value.name}/${value.contentSettings.contentMD5}`)
const nodeContent = JSON.stringify(item)
const nodeContentDigest = crypto
.createHash('md5')
.update(nodeContent)
.digest('hex')
const nodeData = Object.assign(item, {
id: nodeId,
parent: null,
children: [],
internal: {
type: 'azureBlob',
content: nodeContent,
contentDigest: nodeContentDigest,
},
})
createNode(nodeData)
nodes.push(nodeData)
})
if (result.continuationToken == null) {
resolve(nodes)
} else {
queryWithToken(result.continuationToken, nodes)
}
} else {
console.error(` Unable to query container "${tableName}"`)
reject(error)
}
})
}
queryWithToken()
} catch (err) {
console.error(` Error on container "${containerName}"`)
reject(err)
}
})
}
exports.sourceNodes = (
{ actions, createNodeId },
configOptions
) => {
const { createNode } = actions
// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins
let tableService = azure.createTableService()
let blobService = azure.createBlobService()
let hasTables = configOptions.tables != null && configOptions.tables.length > 0
let tablePromises = hasTables
? configOptions.tables.map(x => {
let typeName = (x.type || x.name)
makeTableNode(createNode, createNodeId, x.name, typeName)
return makeNodesFromQuery(createNode, createNodeId, tableService, x.name, typeName)
})
: []
let blobPromises = (configOptions.containers != null && configOptions.containers.length > 0)
? configOptions.containers.map(x => {
let localFolder = (x.localFolder || configOptions.containerLocalFolder)
makeContainerNode(createNode, createNodeId, x.name, localFolder)
let promiseNode = makeNodesFromContainer(createNode, createNodeId, blobService, x.name, localFolder)
if (localFolder == null) {
return promiseNode
} else {
return promiseNode
.then(values => {
return Promise.all(values.map(node => {
return downloadBlobFile(createNode, createNodeId, blobService, node)
}))
})
}
})
: []
return Promise.all(tablePromises.concat(blobPromises))
}