-
Notifications
You must be signed in to change notification settings - Fork 114
/
generate-upload-data.js
108 lines (91 loc) · 3.25 KB
/
generate-upload-data.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
/**
* This script will generate many uploads for a given user token using the web3.storage API endpoints.
* Handy when working with and testing features that require a pristine mass set of data, such as pagination.
* Arguments can be passed with a arg=value pattern, ie token='<YOUR TOKEN HERE>'. Passing a token is the only required argument.
* Not passing a value will default to a truthy value. ie appending `pin` argument is the same as `pin=true`.
* This script requires a users token to be passed or assigned to an environment variable API_TOKEN.
* Passable arguments are:
* - token: the token of the user these uploads will be authenticated with.
* - uploads: The amount of uploads that need to be added, defaulting to 25.
* - url: The URL of the API, defaulting to localhost.
* - psa_pin_requests: If true, on top of uploading the file, a PSA pin request is created for the given CID.
* Example usage.
* From the API folder, run the following command using a generated token.
* - node scripts/generate-upload-data.js token='yourApiToken'
* This will generate 25 uploads for the given user. Append the pin argument if these uploads should also be pinned.
*/
import fetch, { Blob } from '@web-std/fetch'
const requiredArgs = [
'token'
]
const optionalArgs = {
uploads: '25',
psa_pin_requests: false,
url: 'http://127.0.0.1:8787'
}
const argv = process.argv.slice(2).map((arg) => [
arg.includes('=')
? arg.split('=')[0]
: arg,
!arg.includes('=') || arg.split('=')[1]
])
const parsedArgs = Object.fromEntries(argv)
if (!parsedArgs.token && process.env.API_TOKEN) {
parsedArgs.token = process.env.API_TOKEN
}
if (!parsedArgs.token) {
throw Error('Token required, set a token by appending `token=123` to this script')
}
Object.keys(parsedArgs).forEach(arg => {
if (![...requiredArgs, ...Object.keys(optionalArgs)].includes(arg)) {
throw Error(`Argument ${arg} is not recognized, accepted arguments are [${[...requiredArgs, ...Object.keys(optionalArgs)]}]`)
}
})
const config = {
...optionalArgs,
...parsedArgs
}
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.token}`
}
}
async function generateData () {
let i = 0
while (i < config.uploads) {
const file = new Blob([`Automated file upload number ${i} id: ${Date.now()}`])
const response = await fetch(
new URL('/upload', `${config.url}`).toString(),
{
...options,
body: file
}
)
const responseData = await response.json()
const cid = responseData.cid
if (!response.ok) {
console.error(`HTTP error! Status: ${response.status}. Body: ${JSON.stringify(responseData)}`)
}
if (config.psa_pin_requests) {
const body = {
cid
}
const pinResponse = await fetch(
new URL('/pins', `${config.url}`).toString(),
{
...options,
body: JSON.stringify(body)
}
)
const pinResponseData = await pinResponse.json()
if (!pinResponse.ok) {
console.error(`HTTP error! Status: ${pinResponse.status}. Body: ${JSON.stringify(pinResponseData)}`)
}
}
i++
}
console.log(`Done, uploaded ${i} files ${config.psa_pin_requests ? 'pinning each of them' : ''}.`)
}
generateData()