@flowfuse/nr-file-nodes 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/50-file-test-file.txt +0 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +178 -0
- package/README.md +21 -0
- package/file.html +412 -0
- package/file.js +402 -0
- package/locales/en-US/file.html +69 -0
- package/locales/en-US/file.json +65 -0
- package/package.json +54 -0
- package/vfs.js +186 -0
package/vfs.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
**/
|
|
16
|
+
|
|
17
|
+
const Stream = require('stream')
|
|
18
|
+
const got = require('got')
|
|
19
|
+
|
|
20
|
+
module.exports = function (RED, _teamID, _projectID, _token) {
|
|
21
|
+
'use strict'
|
|
22
|
+
const teamID = _teamID || (process.env.FF_FS_TEST_CONFIG ? process.env.FLOWFORGE_TEAM_ID : null) || RED.settings.flowforge?.teamID
|
|
23
|
+
const projectID = _projectID || (process.env.FF_FS_TEST_CONFIG ? process.env.FLOWFORGE_PROJECT_ID : null) || RED.settings.flowforge?.projectID
|
|
24
|
+
const projectToken = _token || (process.env.FF_FS_TEST_CONFIG ? process.env.FLOWFORGE_PROJECT_TOKEN : null) || RED.settings.flowforge?.fileStore?.token
|
|
25
|
+
const fileStoreURL = RED.settings.flowforge?.fileStore?.url || 'http://127.0.0.1:3001'
|
|
26
|
+
|
|
27
|
+
const client = got.extend({
|
|
28
|
+
// prefixUrl: `${app.config.base_url}/account/check/project`,
|
|
29
|
+
prefixUrl: `${fileStoreURL}/v1/files/${teamID}/${projectID}`,
|
|
30
|
+
headers: {
|
|
31
|
+
'user-agent': 'FlowForge Node-RED File Nodes for Storage Server',
|
|
32
|
+
authorization: 'Bearer ' + projectToken
|
|
33
|
+
},
|
|
34
|
+
timeout: {
|
|
35
|
+
request: 3000
|
|
36
|
+
},
|
|
37
|
+
retry: {
|
|
38
|
+
limit: 0
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const normaliseError = (err, filename) => {
|
|
43
|
+
const niceError = new Error('Unknown Error')
|
|
44
|
+
let statusCode = null
|
|
45
|
+
let childErr = {}
|
|
46
|
+
if (typeof err === 'string') {
|
|
47
|
+
err = new Error(err)
|
|
48
|
+
} else if (err?._normalised) {
|
|
49
|
+
return err // already normalised
|
|
50
|
+
}
|
|
51
|
+
err = err || {}
|
|
52
|
+
if (err?.response) {
|
|
53
|
+
statusCode = err.response.statusCode
|
|
54
|
+
if (err.response.body) {
|
|
55
|
+
try {
|
|
56
|
+
if (err.response.body && typeof err.response.body === 'object') {
|
|
57
|
+
childErr = err.response.body
|
|
58
|
+
} else {
|
|
59
|
+
childErr = { ...JSON.parse(err.response.body.toString()) }
|
|
60
|
+
}
|
|
61
|
+
} catch (_error) { /* do nothing */ }
|
|
62
|
+
if (!childErr || typeof childErr !== 'object') {
|
|
63
|
+
childErr = {}
|
|
64
|
+
}
|
|
65
|
+
Object.assign(niceError, childErr)
|
|
66
|
+
niceError.message = childErr.error || childErr.message || niceError.message
|
|
67
|
+
niceError.code = childErr.code || niceError.code
|
|
68
|
+
niceError.stack = childErr.stack || niceError.stack
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (err?.code === 'ETIMEDOUT') {
|
|
72
|
+
niceError.code = err.code
|
|
73
|
+
niceError.message = err.message
|
|
74
|
+
niceError.stack = err.stack
|
|
75
|
+
}
|
|
76
|
+
if (/route.*not found/gi.test(niceError.message) && statusCode === 404) {
|
|
77
|
+
niceError.message = 'ENOENT: no such file or directory' + (filename ? `, '${filename}'` : '')
|
|
78
|
+
niceError.code = 'ENOENT'
|
|
79
|
+
} else if (statusCode === 413) {
|
|
80
|
+
niceError.message = 'Quota exceeded.'
|
|
81
|
+
if (childErr && childErr.limit) {
|
|
82
|
+
niceError.message += ` The current limit is ${childErr.limit} bytes.`
|
|
83
|
+
}
|
|
84
|
+
niceError.code = 'quota_exceeded'
|
|
85
|
+
}
|
|
86
|
+
niceError.stack = niceError.stack || err.stack
|
|
87
|
+
niceError.code = niceError.code || err.code || 'unexpected_error'
|
|
88
|
+
niceError._normalised = true // prevent double processing
|
|
89
|
+
return niceError
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
unlink (filename, callback) {
|
|
94
|
+
client.delete(filename)
|
|
95
|
+
.then(() => {
|
|
96
|
+
callback()
|
|
97
|
+
})
|
|
98
|
+
.catch(err => {
|
|
99
|
+
callback(normaliseError(err, filename))
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
ensureDir (dirName, callback) {
|
|
103
|
+
const options = {
|
|
104
|
+
headers: {
|
|
105
|
+
FF_MODE: 'ensureDir'
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
client.post(dirName, options)
|
|
109
|
+
.then((body) => {
|
|
110
|
+
callback(null, (body && body.rawBody) || null)
|
|
111
|
+
})
|
|
112
|
+
.catch(_err => {
|
|
113
|
+
callback(normaliseError('operation not permitted'))
|
|
114
|
+
})
|
|
115
|
+
},
|
|
116
|
+
writeFile (filename, buffer, callback) {
|
|
117
|
+
const options = {
|
|
118
|
+
headers: {
|
|
119
|
+
'Content-Type': 'application/octet-stream'
|
|
120
|
+
},
|
|
121
|
+
body: buffer
|
|
122
|
+
}
|
|
123
|
+
client.post(filename, options)
|
|
124
|
+
.then((body) => {
|
|
125
|
+
callback(null, (body && body.rawBody) || null)
|
|
126
|
+
})
|
|
127
|
+
.catch(err => {
|
|
128
|
+
callback(normaliseError(err))
|
|
129
|
+
})
|
|
130
|
+
},
|
|
131
|
+
appendFile (filename, buffer, callback) {
|
|
132
|
+
const options = {
|
|
133
|
+
headers: {
|
|
134
|
+
'Content-Type': 'application/octet-stream',
|
|
135
|
+
FF_MODE: 'append'
|
|
136
|
+
},
|
|
137
|
+
body: buffer
|
|
138
|
+
}
|
|
139
|
+
client.post(filename, options)
|
|
140
|
+
.then((body) => {
|
|
141
|
+
callback(null, (body && body.rawBody) || null)
|
|
142
|
+
})
|
|
143
|
+
.catch(err => {
|
|
144
|
+
callback(normaliseError(err, filename))
|
|
145
|
+
})
|
|
146
|
+
},
|
|
147
|
+
readFile (filename, callback) {
|
|
148
|
+
const options = {
|
|
149
|
+
headers: {
|
|
150
|
+
'Content-Type': 'application/octet-stream'
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
client.get(filename, options)
|
|
154
|
+
.then((body) => {
|
|
155
|
+
callback(null, (body && body.rawBody) || null)
|
|
156
|
+
})
|
|
157
|
+
.catch(err => {
|
|
158
|
+
if (err?.request?.statusCode === 404) {
|
|
159
|
+
callback(normaliseError({ code: 'ENOENT', message: 'ENOENT: no such file or directory' }, filename))
|
|
160
|
+
} else {
|
|
161
|
+
callback(normaliseError(err, filename))
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
},
|
|
165
|
+
createReadStream (filename) {
|
|
166
|
+
const readableStream = new Stream.Readable({
|
|
167
|
+
highWaterMark: 64000,
|
|
168
|
+
read () { }
|
|
169
|
+
})
|
|
170
|
+
this.readFile(filename, (err, buf) => {
|
|
171
|
+
if (err) {
|
|
172
|
+
readableStream.emit('error', err)
|
|
173
|
+
readableStream.destroy()
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
readableStream.push(buf)
|
|
177
|
+
setImmediate(() => {
|
|
178
|
+
if (readableStream && readableStream.readable) {
|
|
179
|
+
readableStream.push(null) // end of file
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
return readableStream
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|