@taqueria/plugin-ipfs-pinata 0.25.16-rc → 0.25.19-rc
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/index.cjs +353 -0
- package/index.cjs.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +312 -330
- package/index.js.map +1 -1
- package/package.json +20 -4
package/index.cjs
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
18
|
+
mod
|
|
19
|
+
));
|
|
20
|
+
|
|
21
|
+
// index.ts
|
|
22
|
+
var import_node_sdk2 = require("@taqueria/node-sdk");
|
|
23
|
+
|
|
24
|
+
// src/proxy.ts
|
|
25
|
+
var import_node_sdk = require("@taqueria/node-sdk");
|
|
26
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
27
|
+
|
|
28
|
+
// src/file-processing.ts
|
|
29
|
+
var import_promises = __toESM(require("fs/promises"), 1);
|
|
30
|
+
var import_path = __toESM(require("path"), 1);
|
|
31
|
+
async function* getFiles(fileOrDirPath) {
|
|
32
|
+
const dirInfo = await import_promises.default.stat(fileOrDirPath);
|
|
33
|
+
if (dirInfo.isFile()) {
|
|
34
|
+
yield fileOrDirPath;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const dirents = await import_promises.default.readdir(fileOrDirPath, { withFileTypes: true });
|
|
38
|
+
for (const dirent of dirents) {
|
|
39
|
+
const res = import_path.default.resolve(fileOrDirPath, dirent.name);
|
|
40
|
+
if (dirent.isDirectory()) {
|
|
41
|
+
yield* getFiles(res);
|
|
42
|
+
} else {
|
|
43
|
+
yield res;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
var createFileProvider = async ({
|
|
48
|
+
fileOrDirPath,
|
|
49
|
+
filter,
|
|
50
|
+
shouldEstimateFileCount
|
|
51
|
+
}) => {
|
|
52
|
+
fileOrDirPath = import_path.default.resolve(fileOrDirPath);
|
|
53
|
+
const pathInfo = await import_promises.default.stat(fileOrDirPath);
|
|
54
|
+
if (!pathInfo.isFile() && !pathInfo.isDirectory()) {
|
|
55
|
+
throw new Error(`The path '${fileOrDirPath}' is not a file or directory`);
|
|
56
|
+
}
|
|
57
|
+
let estimateFileCount = void 0;
|
|
58
|
+
if (shouldEstimateFileCount) {
|
|
59
|
+
estimateFileCount = 0;
|
|
60
|
+
for await (const filePath of getFiles(fileOrDirPath)) {
|
|
61
|
+
if (filter && !filter(filePath)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
estimateFileCount++;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const fileGenerator = getFiles(fileOrDirPath);
|
|
68
|
+
const getNextFile = async () => {
|
|
69
|
+
let nextFile = (await fileGenerator.next()).value;
|
|
70
|
+
if (!filter) {
|
|
71
|
+
return nextFile;
|
|
72
|
+
}
|
|
73
|
+
while (nextFile && !filter(nextFile)) {
|
|
74
|
+
nextFile = await getNextFile();
|
|
75
|
+
}
|
|
76
|
+
return nextFile;
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
getNextFile,
|
|
80
|
+
estimateFileCount
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
var processFiles = async ({
|
|
84
|
+
fileOrDirPath,
|
|
85
|
+
processFile,
|
|
86
|
+
filter,
|
|
87
|
+
parallelCount = 10,
|
|
88
|
+
onProgress
|
|
89
|
+
}) => {
|
|
90
|
+
const { getNextFile, estimateFileCount } = await createFileProvider({
|
|
91
|
+
fileOrDirPath,
|
|
92
|
+
filter,
|
|
93
|
+
shouldEstimateFileCount: true
|
|
94
|
+
});
|
|
95
|
+
const successes = [];
|
|
96
|
+
const failures = [];
|
|
97
|
+
onProgress == null ? void 0 : onProgress({
|
|
98
|
+
processedFilesCount: 0,
|
|
99
|
+
estimateFileCount
|
|
100
|
+
});
|
|
101
|
+
await Promise.all([...new Array(parallelCount)].map(async (x) => {
|
|
102
|
+
let fileToProcess = await getNextFile();
|
|
103
|
+
while (fileToProcess) {
|
|
104
|
+
const progressInfo = {
|
|
105
|
+
processedFilesCount: successes.length + failures.length,
|
|
106
|
+
estimateFileCount
|
|
107
|
+
};
|
|
108
|
+
onProgress == null ? void 0 : onProgress(progressInfo);
|
|
109
|
+
try {
|
|
110
|
+
const result = await processFile(fileToProcess, progressInfo);
|
|
111
|
+
successes.push({ filePath: fileToProcess, result });
|
|
112
|
+
} catch (err) {
|
|
113
|
+
failures.push({ filePath: fileToProcess, error: err });
|
|
114
|
+
}
|
|
115
|
+
fileToProcess = await getNextFile();
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
onProgress == null ? void 0 : onProgress({
|
|
119
|
+
processedFilesCount: successes.length + failures.length,
|
|
120
|
+
estimateFileCount
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
successes,
|
|
124
|
+
failures
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/pinata-api.ts
|
|
129
|
+
var import_form_data = __toESM(require("form-data"), 1);
|
|
130
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
131
|
+
var import_ipfs_only_hash = __toESM(require("ipfs-only-hash"), 1);
|
|
132
|
+
var import_node_fetch = __toESM(require("node-fetch"), 1);
|
|
133
|
+
var publishFileToIpfs = async ({
|
|
134
|
+
auth,
|
|
135
|
+
item
|
|
136
|
+
}) => {
|
|
137
|
+
const data = new import_form_data.default();
|
|
138
|
+
data.append("file", import_fs.default.createReadStream(item.filePath));
|
|
139
|
+
data.append(
|
|
140
|
+
"pinataMetadata",
|
|
141
|
+
JSON.stringify({
|
|
142
|
+
name: item.name
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
const response = await (0, import_node_fetch.default)(`https://api.pinata.cloud/pinning/pinFileToIPFS`, {
|
|
146
|
+
headers: {
|
|
147
|
+
Authorization: `Bearer ${auth.pinataJwtToken}`,
|
|
148
|
+
"Content-Type": `multipart/form-data; boundary=${data._boundary}`
|
|
149
|
+
},
|
|
150
|
+
body: data,
|
|
151
|
+
method: "post"
|
|
152
|
+
});
|
|
153
|
+
if (!response.ok) {
|
|
154
|
+
throw new Error(`Failed to upload '${item.name}' to ipfs ${response.statusText}`);
|
|
155
|
+
}
|
|
156
|
+
const uploadResult = await response.json();
|
|
157
|
+
return {
|
|
158
|
+
ipfsHash: uploadResult.IpfsHash
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
var pinHash = async ({
|
|
162
|
+
auth,
|
|
163
|
+
ipfsHash
|
|
164
|
+
}) => {
|
|
165
|
+
const response = await (0, import_node_fetch.default)(`https://api.pinata.cloud/pinning/pinByHash`, {
|
|
166
|
+
headers: {
|
|
167
|
+
Authorization: `Bearer ${auth.pinataJwtToken}`,
|
|
168
|
+
"Content-Type": "application/json"
|
|
169
|
+
},
|
|
170
|
+
method: "post",
|
|
171
|
+
body: JSON.stringify({
|
|
172
|
+
hashToPin: ipfsHash
|
|
173
|
+
})
|
|
174
|
+
});
|
|
175
|
+
if (!response.ok) {
|
|
176
|
+
throw new Error(`Failed to pin '${ipfsHash}' with pinata: ${response.statusText}`);
|
|
177
|
+
}
|
|
178
|
+
return;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// src/utils.ts
|
|
182
|
+
async function delay(timeout) {
|
|
183
|
+
return await new Promise((resolve) => {
|
|
184
|
+
setTimeout(resolve, timeout);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
var createProcessBackoffController = ({
|
|
188
|
+
retryCount = 5,
|
|
189
|
+
targetRequestsPerMinute = 180
|
|
190
|
+
}) => {
|
|
191
|
+
let averageTimePerRequest = 5e3;
|
|
192
|
+
let targetTimePerRequest = 6e4 / targetRequestsPerMinute;
|
|
193
|
+
let lastTime = Date.now();
|
|
194
|
+
const processWithBackoff = async (process2) => {
|
|
195
|
+
let attempt = 0;
|
|
196
|
+
let lastError = void 0;
|
|
197
|
+
while (attempt < retryCount) {
|
|
198
|
+
try {
|
|
199
|
+
let delayTimeMs = Math.max(10, targetTimePerRequest - averageTimePerRequest);
|
|
200
|
+
await delay(Math.floor(delayTimeMs * (1 + 0.5 * Math.random())));
|
|
201
|
+
const result = await process2();
|
|
202
|
+
const timeNow = Date.now();
|
|
203
|
+
const timeElapsed = timeNow - lastTime;
|
|
204
|
+
lastTime = timeNow;
|
|
205
|
+
averageTimePerRequest = averageTimePerRequest * 0.97 + timeElapsed * 0.03;
|
|
206
|
+
return result;
|
|
207
|
+
} catch (err) {
|
|
208
|
+
lastError = err;
|
|
209
|
+
}
|
|
210
|
+
averageTimePerRequest -= (attempt + 1) * 1e3;
|
|
211
|
+
attempt++;
|
|
212
|
+
}
|
|
213
|
+
throw lastError;
|
|
214
|
+
};
|
|
215
|
+
return {
|
|
216
|
+
processWithBackoff
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/proxy.ts
|
|
221
|
+
var import_config = require("dotenv/config");
|
|
222
|
+
var publishToIpfs = async (fileOrDirPath, auth) => {
|
|
223
|
+
if (!fileOrDirPath) {
|
|
224
|
+
throw new Error(`path was not provided`);
|
|
225
|
+
}
|
|
226
|
+
const { processWithBackoff } = createProcessBackoffController({
|
|
227
|
+
retryCount: 5,
|
|
228
|
+
targetRequestsPerMinute: 180
|
|
229
|
+
});
|
|
230
|
+
const result = await processFiles({
|
|
231
|
+
fileOrDirPath,
|
|
232
|
+
parallelCount: 10,
|
|
233
|
+
processFile: async (filePath) => {
|
|
234
|
+
return processWithBackoff(
|
|
235
|
+
() => publishFileToIpfs({
|
|
236
|
+
auth,
|
|
237
|
+
item: { filePath, name: import_path2.default.basename(filePath) }
|
|
238
|
+
})
|
|
239
|
+
);
|
|
240
|
+
},
|
|
241
|
+
onProgress: ({ processedFilesCount, estimateFileCount }) => {
|
|
242
|
+
if (estimateFileCount && processedFilesCount % 10) {
|
|
243
|
+
let ratio = processedFilesCount / estimateFileCount;
|
|
244
|
+
if (ratio > 1)
|
|
245
|
+
ratio = 1;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
return {
|
|
250
|
+
render: "table",
|
|
251
|
+
data: [
|
|
252
|
+
...result.failures.map((x) => {
|
|
253
|
+
var _a;
|
|
254
|
+
return {
|
|
255
|
+
"?": "\u274C",
|
|
256
|
+
filePath: x.filePath,
|
|
257
|
+
ipfsHash: void 0,
|
|
258
|
+
error: ((_a = x.error) == null ? void 0 : _a.message) ?? JSON.stringify(x.error)
|
|
259
|
+
};
|
|
260
|
+
}),
|
|
261
|
+
...result.successes.map((x) => ({
|
|
262
|
+
"?": "\u2714",
|
|
263
|
+
filePath: x.filePath,
|
|
264
|
+
ipfsHash: x.result.ipfsHash,
|
|
265
|
+
error: void 0
|
|
266
|
+
}))
|
|
267
|
+
]
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
var pinToIpfs = async (hash, auth) => {
|
|
271
|
+
if (!hash) {
|
|
272
|
+
throw new Error(`ipfs hash was not provided`);
|
|
273
|
+
}
|
|
274
|
+
await pinHash({ ipfsHash: hash, auth });
|
|
275
|
+
return {
|
|
276
|
+
render: "table",
|
|
277
|
+
data: [{ ipfsHash: hash }]
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
var execute = async (opts) => {
|
|
281
|
+
const {
|
|
282
|
+
task,
|
|
283
|
+
path: path3,
|
|
284
|
+
hash
|
|
285
|
+
} = opts;
|
|
286
|
+
const auth = {
|
|
287
|
+
pinataJwtToken: process.env["pinataJwtToken"]
|
|
288
|
+
};
|
|
289
|
+
if (!auth.pinataJwtToken) {
|
|
290
|
+
throw new Error(`The 'credentials.pinataJwtToken' was not found in config`);
|
|
291
|
+
}
|
|
292
|
+
switch (task) {
|
|
293
|
+
case "publish":
|
|
294
|
+
return publishToIpfs(path3, auth);
|
|
295
|
+
case "pin":
|
|
296
|
+
return pinToIpfs(hash, auth);
|
|
297
|
+
default:
|
|
298
|
+
throw new Error(`${task} is not an understood task by the ipfs-pinata plugin`);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
var proxy_default = async (args) => {
|
|
302
|
+
const opts = args;
|
|
303
|
+
try {
|
|
304
|
+
const resultRaw = await execute(opts);
|
|
305
|
+
const result = "data" in resultRaw ? resultRaw.data : resultRaw;
|
|
306
|
+
return (0, import_node_sdk.sendJsonRes)(result);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
const error = err;
|
|
309
|
+
if (error.message) {
|
|
310
|
+
return (0, import_node_sdk.sendAsyncErr)(error.message);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// index.ts
|
|
316
|
+
import_node_sdk2.Plugin.create(() => ({
|
|
317
|
+
schema: "0.1",
|
|
318
|
+
version: "0.4.0",
|
|
319
|
+
alias: "pinata",
|
|
320
|
+
tasks: [
|
|
321
|
+
import_node_sdk2.Task.create({
|
|
322
|
+
task: "publish",
|
|
323
|
+
command: "publish [path]",
|
|
324
|
+
description: "Upload and pin files using your pinata account.",
|
|
325
|
+
aliases: [],
|
|
326
|
+
handler: "proxy",
|
|
327
|
+
positionals: [
|
|
328
|
+
import_node_sdk2.PositionalArg.create({
|
|
329
|
+
placeholder: "path",
|
|
330
|
+
description: "Directory or file path to publish",
|
|
331
|
+
type: "string"
|
|
332
|
+
})
|
|
333
|
+
],
|
|
334
|
+
encoding: "json"
|
|
335
|
+
}),
|
|
336
|
+
import_node_sdk2.Task.create({
|
|
337
|
+
task: "pin",
|
|
338
|
+
command: "pin [hash]",
|
|
339
|
+
description: "Pin a file already on ipfs with your pinata account.",
|
|
340
|
+
aliases: [],
|
|
341
|
+
handler: "proxy",
|
|
342
|
+
positionals: [
|
|
343
|
+
import_node_sdk2.PositionalArg.create({
|
|
344
|
+
placeholder: "hash",
|
|
345
|
+
description: "Ipfs hash of the file or directory that is already on the ipfs network.",
|
|
346
|
+
type: "string"
|
|
347
|
+
})
|
|
348
|
+
]
|
|
349
|
+
})
|
|
350
|
+
],
|
|
351
|
+
proxy: proxy_default
|
|
352
|
+
}), process.argv);
|
|
353
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts","src/proxy.ts","src/file-processing.ts","src/pinata-api.ts","src/utils.ts"],"sourcesContent":["import { Option, Plugin, PositionalArg, Task } from '@taqueria/node-sdk';\nimport proxy from './src/proxy';\n\nPlugin.create(() => ({\n\tschema: '0.1',\n\tversion: '0.4.0',\n\talias: 'pinata',\n\ttasks: [\n\t\tTask.create({\n\t\t\ttask: 'publish',\n\t\t\tcommand: 'publish [path]',\n\t\t\tdescription: 'Upload and pin files using your pinata account.',\n\t\t\taliases: [],\n\t\t\thandler: 'proxy',\n\t\t\tpositionals: [\n\t\t\t\tPositionalArg.create({\n\t\t\t\t\tplaceholder: 'path',\n\t\t\t\t\tdescription: 'Directory or file path to publish',\n\t\t\t\t\ttype: 'string',\n\t\t\t\t}),\n\t\t\t],\n\t\t\tencoding: 'json',\n\t\t}),\n\t\tTask.create({\n\t\t\ttask: 'pin',\n\t\t\tcommand: 'pin [hash]',\n\t\t\tdescription: 'Pin a file already on ipfs with your pinata account.',\n\t\t\taliases: [],\n\t\t\thandler: 'proxy',\n\t\t\tpositionals: [\n\t\t\t\tPositionalArg.create({\n\t\t\t\t\tplaceholder: 'hash',\n\t\t\t\t\tdescription: 'Ipfs hash of the file or directory that is already on the ipfs network.',\n\t\t\t\t\ttype: 'string',\n\t\t\t\t}),\n\t\t\t],\n\t\t}),\n\t],\n\tproxy,\n}), process.argv);\n","import { sendAsyncErr, sendAsyncRes, sendErr, sendJsonRes } from '@taqueria/node-sdk';\nimport { RequestArgs } from '@taqueria/node-sdk';\nimport { LoadedConfig, SanitizedAbsPath } from '@taqueria/node-sdk/types';\nimport path from 'path';\nimport { processFiles } from './file-processing';\nimport { PinataAuth, pinHash, publishFileToIpfs } from './pinata-api';\nimport { createProcessBackoffController } from './utils';\n\n// Load .env for jwt token\n// TODO: How should this be stored in a secure way?\nimport 'dotenv/config';\n\n// TODO: What should this be, it was removed from the sdk\ntype PluginResponse =\n\t| void\n\t| {\n\t\trender: 'table';\n\t\tdata: unknown[];\n\t};\n\ninterface Opts extends RequestArgs.t {\n\treadonly path?: string;\n\treadonly hash?: string;\n\treadonly task?: string;\n}\n\nconst publishToIpfs = async (fileOrDirPath: undefined | string, auth: PinataAuth): Promise<PluginResponse> => {\n\tif (!fileOrDirPath) {\n\t\tthrow new Error(`path was not provided`);\n\t}\n\n\t// Pinata is limited to 180 requests per minute\n\t// So for the first 180 requests they can go fast\n\n\tconst { processWithBackoff } = createProcessBackoffController({\n\t\tretryCount: 5,\n\t\ttargetRequestsPerMinute: 180,\n\t});\n\n\tconst result = await processFiles({\n\t\tfileOrDirPath,\n\t\tparallelCount: 10,\n\t\tprocessFile: async filePath => {\n\t\t\t// // TEMP: Debug\n\t\t\t// console.log(`publishing: ${filePath}`);\n\n\t\t\treturn processWithBackoff(() =>\n\t\t\t\tpublishFileToIpfs({\n\t\t\t\t\tauth,\n\t\t\t\t\titem: { filePath, name: path.basename(filePath) },\n\t\t\t\t})\n\t\t\t);\n\t\t},\n\t\tonProgress: ({ processedFilesCount, estimateFileCount }) => {\n\t\t\tif (estimateFileCount && processedFilesCount % 10) {\n\t\t\t\tlet ratio = processedFilesCount / estimateFileCount;\n\t\t\t\tif (ratio > 1) ratio = 1;\n\n\t\t\t\t// // TODO: Call task sdk progress\n\t\t\t\t// console.log(`Progress: ${(ratio * 100).toFixed(0)}%`);\n\t\t\t}\n\t\t},\n\t});\n\n\t// // TEMP: DEBUG: Show error\n\t// if (result.failures.length) {\n\t// \tconsole.log('❗ Failures:\\n' + result.failures.map(f => `${f.filePath}: ${f.error}`).join('\\n'));\n\t// }\n\n\treturn {\n\t\trender: 'table',\n\t\tdata: [\n\t\t\t...result.failures.map(x => ({\n\t\t\t\t'?': '❌',\n\t\t\t\tfilePath: x.filePath,\n\t\t\t\tipfsHash: undefined,\n\t\t\t\terror: (x.error as { message?: string })?.message ?? JSON.stringify(x.error),\n\t\t\t})),\n\t\t\t...result.successes.map(x => ({\n\t\t\t\t'?': '✔',\n\t\t\t\tfilePath: x.filePath,\n\t\t\t\tipfsHash: x.result.ipfsHash,\n\t\t\t\terror: undefined,\n\t\t\t})),\n\t\t],\n\t};\n};\n\nconst pinToIpfs = async (hash: undefined | string, auth: PinataAuth): Promise<PluginResponse> => {\n\tif (!hash) {\n\t\tthrow new Error(`ipfs hash was not provided`);\n\t}\n\n\tawait pinHash({ ipfsHash: hash, auth });\n\n\treturn {\n\t\trender: 'table',\n\t\tdata: [{ ipfsHash: hash }],\n\t};\n};\n\nconst execute = async (opts: Opts): Promise<PluginResponse> => {\n\tconst {\n\t\ttask,\n\t\tpath,\n\t\thash,\n\t} = opts;\n\n\tconst auth: PinataAuth = {\n\t\t// TODO: Where should this be stored?\n\t\t// pinataJwtToken: (config as Record<string, any>).credentials.pinataJwtToken,\n\t\tpinataJwtToken: process.env['pinataJwtToken'] as string,\n\t};\n\n\tif (!auth.pinataJwtToken) {\n\t\tthrow new Error(`The 'credentials.pinataJwtToken' was not found in config`);\n\t}\n\n\tswitch (task) {\n\t\tcase 'publish':\n\t\t\treturn publishToIpfs(path, auth);\n\t\tcase 'pin':\n\t\t\treturn pinToIpfs(hash, auth);\n\t\tdefault:\n\t\t\tthrow new Error(`${task} is not an understood task by the ipfs-pinata plugin`);\n\t}\n};\n\nexport default async (args: RequestArgs.t): Promise<PluginResponse> => {\n\tconst opts = args as Opts;\n\n\ttry {\n\t\tconst resultRaw = await execute(opts) as Record<string, unknown>;\n\t\t// TODO: Fix deno parsing\n\t\t// Without this, `data.reduce is not a function`\n\t\tconst result = ('data' in resultRaw) ? resultRaw.data : resultRaw;\n\t\treturn sendJsonRes(result);\n\t} catch (err) {\n\t\tconst error = err as Error;\n\t\tif (error.message) {\n\t\t\treturn sendAsyncErr(error.message);\n\t\t}\n\t}\n};\n","import fs from 'fs/promises';\nimport path from 'path';\n\n// Async generator\n// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search\nasync function* getFiles(fileOrDirPath: string): AsyncGenerator<string, void, unknown> {\n\tconst dirInfo = await fs.stat(fileOrDirPath);\n\tif (dirInfo.isFile()) {\n\t\tyield fileOrDirPath;\n\t\treturn;\n\t}\n\n\tconst dirents = await fs.readdir(fileOrDirPath, { withFileTypes: true });\n\tfor (const dirent of dirents) {\n\t\tconst res = path.resolve(fileOrDirPath, dirent.name);\n\t\tif (dirent.isDirectory()) {\n\t\t\tyield* getFiles(res);\n\t\t} else {\n\t\t\tyield res;\n\t\t}\n\t}\n}\n\nconst createFileProvider = async ({\n\tfileOrDirPath,\n\tfilter,\n\tshouldEstimateFileCount,\n}: {\n\tfileOrDirPath: string;\n\tfilter?: (filePath: string) => boolean;\n\tshouldEstimateFileCount?: boolean;\n}) => {\n\tfileOrDirPath = path.resolve(fileOrDirPath);\n\tconst pathInfo = await fs.stat(fileOrDirPath);\n\tif (\n\t\t!pathInfo.isFile()\n\t\t&& !pathInfo.isDirectory()\n\t) {\n\t\tthrow new Error(`The path '${fileOrDirPath}' is not a file or directory`);\n\t}\n\n\tlet estimateFileCount = undefined as undefined | number;\n\tif (shouldEstimateFileCount) {\n\t\testimateFileCount = 0;\n\t\tfor await (const filePath of getFiles(fileOrDirPath)) {\n\t\t\tif (filter && !filter(filePath)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\testimateFileCount++;\n\t\t}\n\t}\n\n\tconst fileGenerator = getFiles(fileOrDirPath);\n\tconst getNextFile = async () => {\n\t\tlet nextFile = (await fileGenerator.next()).value;\n\t\tif (!filter) {\n\t\t\treturn nextFile;\n\t\t}\n\n\t\twhile (nextFile && !filter(nextFile)) {\n\t\t\tnextFile = await getNextFile();\n\t\t}\n\n\t\treturn nextFile;\n\t};\n\treturn {\n\t\tgetNextFile,\n\t\testimateFileCount,\n\t};\n};\n\ntype ProgressInfo = { processedFilesCount: number; estimateFileCount: undefined | number };\nexport const processFiles = async <TResult>({\n\tfileOrDirPath,\n\tprocessFile,\n\tfilter,\n\tparallelCount = 10,\n\tonProgress,\n}: {\n\tfileOrDirPath: string;\n\tprocessFile: (filePath: string, progress: ProgressInfo) => Promise<TResult>;\n\tfilter?: (filePath: string) => boolean;\n\tparallelCount?: number;\n\tonProgress?: (progress: ProgressInfo) => void;\n}) => {\n\tconst { getNextFile, estimateFileCount } = await createFileProvider({\n\t\tfileOrDirPath,\n\t\tfilter,\n\t\tshouldEstimateFileCount: true,\n\t});\n\n\tconst successes = [] as { filePath: string; result: TResult }[];\n\tconst failures = [] as { filePath: string; error: unknown }[];\n\n\tonProgress?.({\n\t\tprocessedFilesCount: 0,\n\t\testimateFileCount,\n\t});\n\n\tawait Promise.all([...new Array(parallelCount)].map(async x => {\n\t\tlet fileToProcess = await getNextFile();\n\t\twhile (fileToProcess) {\n\t\t\tconst progressInfo = {\n\t\t\t\tprocessedFilesCount: successes.length + failures.length,\n\t\t\t\testimateFileCount,\n\t\t\t};\n\t\t\tonProgress?.(progressInfo);\n\n\t\t\ttry {\n\t\t\t\tconst result = await processFile(fileToProcess, progressInfo);\n\t\t\t\tsuccesses.push({ filePath: fileToProcess, result });\n\t\t\t} catch (err) {\n\t\t\t\tfailures.push({ filePath: fileToProcess, error: err });\n\t\t\t}\n\n\t\t\tfileToProcess = await getNextFile();\n\t\t}\n\t}));\n\n\tonProgress?.({\n\t\tprocessedFilesCount: successes.length + failures.length,\n\t\testimateFileCount,\n\t});\n\n\treturn {\n\t\tsuccesses,\n\t\tfailures,\n\t};\n};\n","import FormData from 'form-data';\nimport fs from 'fs';\nimport Hash from 'ipfs-only-hash';\nimport fetch from 'node-fetch';\n\nexport type PinataAuth = {\n\tpinataJwtToken: string;\n};\n\nexport type PublishFileResult = {\n\tipfsHash: string;\n};\n\nexport const publishFileToIpfs = async ({\n\tauth,\n\titem,\n}: {\n\tauth: PinataAuth;\n\titem: {\n\t\tname: string;\n\t\tfilePath: string;\n\t};\n}): Promise<PublishFileResult> => {\n\t// The data api to check for existing file is limited to 30 requests per minute\n\t// While uploading allows 180 requests per minute\n\t// i.e. it's faster to just upload again\n\n\t// // Skip if already pinned\n\t// const { isPinned, ipfsHash } = await checkIfFileIsPinned({ auth, item });\n\t// if (isPinned) {\n\t// \treturn {\n\t// \t\tipfsHash,\n\t// \t};\n\t// }\n\n\tconst data = new FormData();\n\tdata.append('file', fs.createReadStream(item.filePath));\n\tdata.append(\n\t\t'pinataMetadata',\n\t\tJSON.stringify({\n\t\t\tname: item.name,\n\t\t}),\n\t);\n\n\tconst response = await fetch(`https://api.pinata.cloud/pinning/pinFileToIPFS`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t\t'Content-Type': `multipart/form-data; boundary=${(data as unknown as { _boundary: string })._boundary}`,\n\t\t},\n\t\tbody: data,\n\t\tmethod: 'post',\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to upload '${item.name}' to ipfs ${response.statusText}`);\n\t}\n\n\tconst uploadResult = await response.json() as {\n\t\tIpfsHash: string; // This is the IPFS multi-hash provided back for your content,\n\t\tPinSize: string; // This is how large (in bytes) the content you just pinned is,\n\t\tTimestamp: string; // This is the timestamp for your content pinning (represented in ISO 8601 format)\n\t};\n\n\treturn {\n\t\tipfsHash: uploadResult.IpfsHash,\n\t};\n};\n\nconst checkIfFileIsPinned = async ({\n\tauth,\n\titem,\n}: {\n\tauth: PinataAuth;\n\titem: {\n\t\tname: string;\n\t\tfilePath: string;\n\t};\n}) => {\n\tconst ipfsHash = await Hash.of(fs.createReadStream(item.filePath));\n\n\tconst response = await fetch(`https://api.pinata.cloud/data/pinList?status=pinned&hashContains=${ipfsHash}`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t},\n\t\tmethod: 'get',\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to query '${item.name}' status from pinata ${response.statusText}`);\n\t}\n\n\tconst pinResult = await response.json() as {\n\t\tcount: number;\n\t\trows: {\n\t\t\tid: string;\n\t\t\tipfs_pin_hash: string;\n\t\t\tsize: number;\n\t\t\tuser_id: string;\n\t\t\tdate_pinned: null | string;\n\t\t\tdate_unpinned: null | string;\n\t\t\tmetadata: {\n\t\t\t\tname: string;\n\t\t\t\tkeyvalues: null | string;\n\t\t\t};\n\t\t\tregions: {\n\t\t\t\tregionId: string;\n\t\t\t\tcurrentReplicationCount: number;\n\t\t\t\tdesiredReplicationCount: number;\n\t\t\t}[];\n\t\t}[];\n\t};\n\n\tconst isPinned = pinResult.rows.some(x =>\n\t\tx.ipfs_pin_hash === ipfsHash\n\t\t&& x.date_pinned\n\t\t&& !x.date_unpinned\n\t);\n\n\treturn {\n\t\tisPinned,\n\t\tipfsHash,\n\t};\n};\n\nexport const pinHash = async ({\n\tauth,\n\tipfsHash,\n}: {\n\tauth: PinataAuth;\n\tipfsHash: string;\n}) => {\n\tconst response = await fetch(`https://api.pinata.cloud/pinning/pinByHash`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t\t'Content-Type': 'application/json',\n\t\t},\n\t\tmethod: 'post',\n\t\tbody: JSON.stringify({\n\t\t\thashToPin: ipfsHash,\n\t\t}),\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to pin '${ipfsHash}' with pinata: ${response.statusText}`);\n\t}\n\n\t// Ok is the only response if successful\n\treturn;\n};\n","export async function delay(timeout: number): Promise<void> {\n\treturn await new Promise(resolve => {\n\t\tsetTimeout(resolve, timeout);\n\t});\n}\n\nexport const createProcessBackoffController = ({\n\tretryCount = 5,\n\ttargetRequestsPerMinute = 180,\n}: {\n\tretryCount?: number;\n\ttargetRequestsPerMinute?: number;\n}) => {\n\tlet averageTimePerRequest = 5000;\n\tlet targetTimePerRequest = 60000 / targetRequestsPerMinute;\n\tlet lastTime = Date.now();\n\n\tconst processWithBackoff = async <TResult>(process: () => Promise<TResult>) => {\n\t\tlet attempt = 0;\n\t\tlet lastError = undefined as unknown;\n\t\twhile (attempt < retryCount) {\n\t\t\ttry {\n\t\t\t\tlet delayTimeMs = Math.max(10, targetTimePerRequest - averageTimePerRequest);\n\n\t\t\t\t// Partially randomized delay to ensure parallel requests don't line up\n\t\t\t\tawait delay(Math.floor(delayTimeMs * (1 + 0.5 * Math.random())));\n\n\t\t\t\tconst result = await process();\n\n\t\t\t\tconst timeNow = Date.now();\n\t\t\t\tconst timeElapsed = timeNow - lastTime;\n\t\t\t\tlastTime = timeNow;\n\n\t\t\t\t// Running average\n\t\t\t\taverageTimePerRequest = averageTimePerRequest * 0.97 + timeElapsed * 0.03;\n\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tlastError = err;\n\t\t\t}\n\n\t\t\t// Quickly increase time to wait if failure (allow negatives to wait longer than target)\n\t\t\taverageTimePerRequest -= (attempt + 1) * 1000;\n\t\t\tattemp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Y,MAAM;AAAA,EAC1B,SAAS,KAAP;AACD,UAAM,QAAQ;AACd,QAAI,MAAM,SAAS;AAClB,iBAAO,8BAAa,MAAM,OAAO;AAAA,IAClC;AAAA,EACD;AACD;;;AD5IA,wBAAO,OAAO,OAAO;AAAA,EACpB,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,OAAO;AAAA,EACP,OAAO;AAAA,IACN,sBAAK,OAAO;AAAA,MACX,MAAM;AAAA,MACN,SAAS;AAAA,MACT,aAAa;AAAA,MACb,SAAS,CAAC;AAAA,MACV,SAAS;AAAA,MACT,aAAa;AAAA,QACZ,+BAAc,OAAO;AAAA,UACpB,aAAa;AAAA,UACb,aAAa;AAAA,UACb,MAAM;AAAA,QACP,CAAC;AAAA,MACF;AAAA,MACA,UAAU;AAAA,IACX,CAAC;AAAA,IACD,sBAAK,OAAO;AAAA,MACX,MAAM;AAAA,MACN,SAAS;AAAA,MACT,aAAa;AAAA,MACb,SAAS,CAAC;AAAA,MACV,SAAS;AAAA,MACT,aAAa;AAAA,QACZ,+BAAc,OAAO;AAAA,UACpB,aAAa;AAAA,UACb,aAAa;AAAA,UACb,MAAM;AAAA,QACP,CAAC;AAAA,MACF;AAAA,IACD,CAAC;AAAA,EACF;AAAA,EACA;AACD,IAAI,QAAQ,IAAI;","names":["import_node_sdk","import_path","fs","path","FormData","fs","fetch","fetch","process","path"]}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/index.js
CHANGED
|
@@ -1,351 +1,333 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import "dotenv/config";
|
|
4
|
-
import $khbt3$fspromises from "fs/promises";
|
|
5
|
-
import $khbt3$formdata from "form-data";
|
|
6
|
-
import $khbt3$fs from "fs";
|
|
7
|
-
import $khbt3$ipfsonlyhash from "ipfs-only-hash";
|
|
8
|
-
import $khbt3$nodefetch from "node-fetch";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
// index.ts
|
|
2
|
+
import { Plugin, PositionalArg, Task } from "@taqueria/node-sdk";
|
|
12
3
|
|
|
4
|
+
// src/proxy.ts
|
|
5
|
+
import { sendAsyncErr, sendJsonRes } from "@taqueria/node-sdk";
|
|
6
|
+
import path2 from "path";
|
|
13
7
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
async function*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
8
|
+
// src/file-processing.ts
|
|
9
|
+
import fs from "fs/promises";
|
|
10
|
+
import path from "path";
|
|
11
|
+
async function* getFiles(fileOrDirPath) {
|
|
12
|
+
const dirInfo = await fs.stat(fileOrDirPath);
|
|
13
|
+
if (dirInfo.isFile()) {
|
|
14
|
+
yield fileOrDirPath;
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const dirents = await fs.readdir(fileOrDirPath, { withFileTypes: true });
|
|
18
|
+
for (const dirent of dirents) {
|
|
19
|
+
const res = path.resolve(fileOrDirPath, dirent.name);
|
|
20
|
+
if (dirent.isDirectory()) {
|
|
21
|
+
yield* getFiles(res);
|
|
22
|
+
} else {
|
|
23
|
+
yield res;
|
|
30
24
|
}
|
|
25
|
+
}
|
|
31
26
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
27
|
+
var createFileProvider = async ({
|
|
28
|
+
fileOrDirPath,
|
|
29
|
+
filter,
|
|
30
|
+
shouldEstimateFileCount
|
|
31
|
+
}) => {
|
|
32
|
+
fileOrDirPath = path.resolve(fileOrDirPath);
|
|
33
|
+
const pathInfo = await fs.stat(fileOrDirPath);
|
|
34
|
+
if (!pathInfo.isFile() && !pathInfo.isDirectory()) {
|
|
35
|
+
throw new Error(`The path '${fileOrDirPath}' is not a file or directory`);
|
|
36
|
+
}
|
|
37
|
+
let estimateFileCount = void 0;
|
|
38
|
+
if (shouldEstimateFileCount) {
|
|
39
|
+
estimateFileCount = 0;
|
|
40
|
+
for await (const filePath of getFiles(fileOrDirPath)) {
|
|
41
|
+
if (filter && !filter(filePath)) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
estimateFileCount++;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const fileGenerator = getFiles(fileOrDirPath);
|
|
48
|
+
const getNextFile = async () => {
|
|
49
|
+
let nextFile = (await fileGenerator.next()).value;
|
|
50
|
+
if (!filter) {
|
|
51
|
+
return nextFile;
|
|
43
52
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
estimateFileCount: estimateFileCount
|
|
54
|
-
};
|
|
53
|
+
while (nextFile && !filter(nextFile)) {
|
|
54
|
+
nextFile = await getNextFile();
|
|
55
|
+
}
|
|
56
|
+
return nextFile;
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
getNextFile,
|
|
60
|
+
estimateFileCount
|
|
61
|
+
};
|
|
55
62
|
};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const result = await processFile(fileToProcess, progressInfo);
|
|
80
|
-
successes.push({
|
|
81
|
-
filePath: fileToProcess,
|
|
82
|
-
result: result
|
|
83
|
-
});
|
|
84
|
-
} catch (err) {
|
|
85
|
-
failures.push({
|
|
86
|
-
filePath: fileToProcess,
|
|
87
|
-
error: err
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
fileToProcess = await getNextFile();
|
|
91
|
-
}
|
|
92
|
-
}));
|
|
93
|
-
onProgress?.({
|
|
63
|
+
var processFiles = async ({
|
|
64
|
+
fileOrDirPath,
|
|
65
|
+
processFile,
|
|
66
|
+
filter,
|
|
67
|
+
parallelCount = 10,
|
|
68
|
+
onProgress
|
|
69
|
+
}) => {
|
|
70
|
+
const { getNextFile, estimateFileCount } = await createFileProvider({
|
|
71
|
+
fileOrDirPath,
|
|
72
|
+
filter,
|
|
73
|
+
shouldEstimateFileCount: true
|
|
74
|
+
});
|
|
75
|
+
const successes = [];
|
|
76
|
+
const failures = [];
|
|
77
|
+
onProgress == null ? void 0 : onProgress({
|
|
78
|
+
processedFilesCount: 0,
|
|
79
|
+
estimateFileCount
|
|
80
|
+
});
|
|
81
|
+
await Promise.all([...new Array(parallelCount)].map(async (x) => {
|
|
82
|
+
let fileToProcess = await getNextFile();
|
|
83
|
+
while (fileToProcess) {
|
|
84
|
+
const progressInfo = {
|
|
94
85
|
processedFilesCount: successes.length + failures.length,
|
|
95
|
-
estimateFileCount
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
86
|
+
estimateFileCount
|
|
87
|
+
};
|
|
88
|
+
onProgress == null ? void 0 : onProgress(progressInfo);
|
|
89
|
+
try {
|
|
90
|
+
const result = await processFile(fileToProcess, progressInfo);
|
|
91
|
+
successes.push({ filePath: fileToProcess, result });
|
|
92
|
+
} catch (err) {
|
|
93
|
+
failures.push({ filePath: fileToProcess, error: err });
|
|
94
|
+
}
|
|
95
|
+
fileToProcess = await getNextFile();
|
|
96
|
+
}
|
|
97
|
+
}));
|
|
98
|
+
onProgress == null ? void 0 : onProgress({
|
|
99
|
+
processedFilesCount: successes.length + failures.length,
|
|
100
|
+
estimateFileCount
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
successes,
|
|
104
|
+
failures
|
|
105
|
+
};
|
|
101
106
|
};
|
|
102
107
|
|
|
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
|
-
ipfsHash: uploadResult.IpfsHash
|
|
136
|
-
};
|
|
108
|
+
// src/pinata-api.ts
|
|
109
|
+
import FormData from "form-data";
|
|
110
|
+
import fs2 from "fs";
|
|
111
|
+
import Hash from "ipfs-only-hash";
|
|
112
|
+
import fetch from "node-fetch";
|
|
113
|
+
var publishFileToIpfs = async ({
|
|
114
|
+
auth,
|
|
115
|
+
item
|
|
116
|
+
}) => {
|
|
117
|
+
const data = new FormData();
|
|
118
|
+
data.append("file", fs2.createReadStream(item.filePath));
|
|
119
|
+
data.append(
|
|
120
|
+
"pinataMetadata",
|
|
121
|
+
JSON.stringify({
|
|
122
|
+
name: item.name
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
const response = await fetch(`https://api.pinata.cloud/pinning/pinFileToIPFS`, {
|
|
126
|
+
headers: {
|
|
127
|
+
Authorization: `Bearer ${auth.pinataJwtToken}`,
|
|
128
|
+
"Content-Type": `multipart/form-data; boundary=${data._boundary}`
|
|
129
|
+
},
|
|
130
|
+
body: data,
|
|
131
|
+
method: "post"
|
|
132
|
+
});
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
throw new Error(`Failed to upload '${item.name}' to ipfs ${response.statusText}`);
|
|
135
|
+
}
|
|
136
|
+
const uploadResult = await response.json();
|
|
137
|
+
return {
|
|
138
|
+
ipfsHash: uploadResult.IpfsHash
|
|
139
|
+
};
|
|
137
140
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
headers: {
|
|
157
|
-
Authorization: `Bearer ${auth.pinataJwtToken}`,
|
|
158
|
-
"Content-Type": "application/json"
|
|
159
|
-
},
|
|
160
|
-
method: "post",
|
|
161
|
-
body: JSON.stringify({
|
|
162
|
-
hashToPin: ipfsHash
|
|
163
|
-
})
|
|
164
|
-
});
|
|
165
|
-
if (!response.ok) throw new Error(`Failed to pin '${ipfsHash}' with pinata: ${response.statusText}`);
|
|
166
|
-
// Ok is the only response if successful
|
|
167
|
-
return;
|
|
141
|
+
var pinHash = async ({
|
|
142
|
+
auth,
|
|
143
|
+
ipfsHash
|
|
144
|
+
}) => {
|
|
145
|
+
const response = await fetch(`https://api.pinata.cloud/pinning/pinByHash`, {
|
|
146
|
+
headers: {
|
|
147
|
+
Authorization: `Bearer ${auth.pinataJwtToken}`,
|
|
148
|
+
"Content-Type": "application/json"
|
|
149
|
+
},
|
|
150
|
+
method: "post",
|
|
151
|
+
body: JSON.stringify({
|
|
152
|
+
hashToPin: ipfsHash
|
|
153
|
+
})
|
|
154
|
+
});
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
throw new Error(`Failed to pin '${ipfsHash}' with pinata: ${response.statusText}`);
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
168
159
|
};
|
|
169
160
|
|
|
170
|
-
|
|
171
|
-
async function
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
161
|
+
// src/utils.ts
|
|
162
|
+
async function delay(timeout) {
|
|
163
|
+
return await new Promise((resolve) => {
|
|
164
|
+
setTimeout(resolve, timeout);
|
|
165
|
+
});
|
|
175
166
|
}
|
|
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
|
-
};
|
|
167
|
+
var createProcessBackoffController = ({
|
|
168
|
+
retryCount = 5,
|
|
169
|
+
targetRequestsPerMinute = 180
|
|
170
|
+
}) => {
|
|
171
|
+
let averageTimePerRequest = 5e3;
|
|
172
|
+
let targetTimePerRequest = 6e4 / targetRequestsPerMinute;
|
|
173
|
+
let lastTime = Date.now();
|
|
174
|
+
const processWithBackoff = async (process2) => {
|
|
175
|
+
let attempt = 0;
|
|
176
|
+
let lastError = void 0;
|
|
177
|
+
while (attempt < retryCount) {
|
|
178
|
+
try {
|
|
179
|
+
let delayTimeMs = Math.max(10, targetTimePerRequest - averageTimePerRequest);
|
|
180
|
+
await delay(Math.floor(delayTimeMs * (1 + 0.5 * Math.random())));
|
|
181
|
+
const result = await process2();
|
|
182
|
+
const timeNow = Date.now();
|
|
183
|
+
const timeElapsed = timeNow - lastTime;
|
|
184
|
+
lastTime = timeNow;
|
|
185
|
+
averageTimePerRequest = averageTimePerRequest * 0.97 + timeElapsed * 0.03;
|
|
186
|
+
return result;
|
|
187
|
+
} catch (err) {
|
|
188
|
+
lastError = err;
|
|
189
|
+
}
|
|
190
|
+
averageTimePerRequest -= (attempt + 1) * 1e3;
|
|
191
|
+
attempt++;
|
|
192
|
+
}
|
|
193
|
+
throw lastError;
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
processWithBackoff
|
|
197
|
+
};
|
|
208
198
|
};
|
|
209
199
|
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
ipfsHash: x.result.ipfsHash,
|
|
260
|
-
error: undefined
|
|
261
|
-
}))
|
|
262
|
-
]
|
|
263
|
-
};
|
|
200
|
+
// src/proxy.ts
|
|
201
|
+
import "dotenv/config";
|
|
202
|
+
var publishToIpfs = async (fileOrDirPath, auth) => {
|
|
203
|
+
if (!fileOrDirPath) {
|
|
204
|
+
throw new Error(`path was not provided`);
|
|
205
|
+
}
|
|
206
|
+
const { processWithBackoff } = createProcessBackoffController({
|
|
207
|
+
retryCount: 5,
|
|
208
|
+
targetRequestsPerMinute: 180
|
|
209
|
+
});
|
|
210
|
+
const result = await processFiles({
|
|
211
|
+
fileOrDirPath,
|
|
212
|
+
parallelCount: 10,
|
|
213
|
+
processFile: async (filePath) => {
|
|
214
|
+
return processWithBackoff(
|
|
215
|
+
() => publishFileToIpfs({
|
|
216
|
+
auth,
|
|
217
|
+
item: { filePath, name: path2.basename(filePath) }
|
|
218
|
+
})
|
|
219
|
+
);
|
|
220
|
+
},
|
|
221
|
+
onProgress: ({ processedFilesCount, estimateFileCount }) => {
|
|
222
|
+
if (estimateFileCount && processedFilesCount % 10) {
|
|
223
|
+
let ratio = processedFilesCount / estimateFileCount;
|
|
224
|
+
if (ratio > 1)
|
|
225
|
+
ratio = 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
return {
|
|
230
|
+
render: "table",
|
|
231
|
+
data: [
|
|
232
|
+
...result.failures.map((x) => {
|
|
233
|
+
var _a;
|
|
234
|
+
return {
|
|
235
|
+
"?": "\u274C",
|
|
236
|
+
filePath: x.filePath,
|
|
237
|
+
ipfsHash: void 0,
|
|
238
|
+
error: ((_a = x.error) == null ? void 0 : _a.message) ?? JSON.stringify(x.error)
|
|
239
|
+
};
|
|
240
|
+
}),
|
|
241
|
+
...result.successes.map((x) => ({
|
|
242
|
+
"?": "\u2714",
|
|
243
|
+
filePath: x.filePath,
|
|
244
|
+
ipfsHash: x.result.ipfsHash,
|
|
245
|
+
error: void 0
|
|
246
|
+
}))
|
|
247
|
+
]
|
|
248
|
+
};
|
|
264
249
|
};
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
{
|
|
275
|
-
ipfsHash: hash
|
|
276
|
-
}
|
|
277
|
-
]
|
|
278
|
-
};
|
|
250
|
+
var pinToIpfs = async (hash, auth) => {
|
|
251
|
+
if (!hash) {
|
|
252
|
+
throw new Error(`ipfs hash was not provided`);
|
|
253
|
+
}
|
|
254
|
+
await pinHash({ ipfsHash: hash, auth });
|
|
255
|
+
return {
|
|
256
|
+
render: "table",
|
|
257
|
+
data: [{ ipfsHash: hash }]
|
|
258
|
+
};
|
|
279
259
|
};
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
260
|
+
var execute = async (opts) => {
|
|
261
|
+
const {
|
|
262
|
+
task,
|
|
263
|
+
path: path3,
|
|
264
|
+
hash
|
|
265
|
+
} = opts;
|
|
266
|
+
const auth = {
|
|
267
|
+
pinataJwtToken: process.env["pinataJwtToken"]
|
|
268
|
+
};
|
|
269
|
+
if (!auth.pinataJwtToken) {
|
|
270
|
+
throw new Error(`The 'credentials.pinataJwtToken' was not found in config`);
|
|
271
|
+
}
|
|
272
|
+
switch (task) {
|
|
273
|
+
case "publish":
|
|
274
|
+
return publishToIpfs(path3, auth);
|
|
275
|
+
case "pin":
|
|
276
|
+
return pinToIpfs(hash, auth);
|
|
277
|
+
default:
|
|
278
|
+
throw new Error(`${task} is not an understood task by the ipfs-pinata plugin`);
|
|
279
|
+
}
|
|
296
280
|
};
|
|
297
|
-
var
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if (error.message) return (0, $khbt3$sendAsyncErr)(error.message);
|
|
281
|
+
var proxy_default = async (args) => {
|
|
282
|
+
const opts = args;
|
|
283
|
+
try {
|
|
284
|
+
const resultRaw = await execute(opts);
|
|
285
|
+
const result = "data" in resultRaw ? resultRaw.data : resultRaw;
|
|
286
|
+
return sendJsonRes(result);
|
|
287
|
+
} catch (err) {
|
|
288
|
+
const error = err;
|
|
289
|
+
if (error.message) {
|
|
290
|
+
return sendAsyncErr(error.message);
|
|
308
291
|
}
|
|
292
|
+
}
|
|
309
293
|
};
|
|
310
294
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
//# sourceMappingURL=index.js.map
|
|
295
|
+
// index.ts
|
|
296
|
+
Plugin.create(() => ({
|
|
297
|
+
schema: "0.1",
|
|
298
|
+
version: "0.4.0",
|
|
299
|
+
alias: "pinata",
|
|
300
|
+
tasks: [
|
|
301
|
+
Task.create({
|
|
302
|
+
task: "publish",
|
|
303
|
+
command: "publish [path]",
|
|
304
|
+
description: "Upload and pin files using your pinata account.",
|
|
305
|
+
aliases: [],
|
|
306
|
+
handler: "proxy",
|
|
307
|
+
positionals: [
|
|
308
|
+
PositionalArg.create({
|
|
309
|
+
placeholder: "path",
|
|
310
|
+
description: "Directory or file path to publish",
|
|
311
|
+
type: "string"
|
|
312
|
+
})
|
|
313
|
+
],
|
|
314
|
+
encoding: "json"
|
|
315
|
+
}),
|
|
316
|
+
Task.create({
|
|
317
|
+
task: "pin",
|
|
318
|
+
command: "pin [hash]",
|
|
319
|
+
description: "Pin a file already on ipfs with your pinata account.",
|
|
320
|
+
aliases: [],
|
|
321
|
+
handler: "proxy",
|
|
322
|
+
positionals: [
|
|
323
|
+
PositionalArg.create({
|
|
324
|
+
placeholder: "hash",
|
|
325
|
+
description: "Ipfs hash of the file or directory that is already on the ipfs network.",
|
|
326
|
+
type: "string"
|
|
327
|
+
})
|
|
328
|
+
]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ACpB,QAAQ;QACR,SAAS;QACT,OAAO;QACP,OAAO;YACN,CAAA,GAAA,WAAG,EAAE,MAAM,CAAC;gBACX,MAAM;gBACN,SAAS;gBACT,aAAa;gBACb,SAAS,EAAE;gBACX,SAAS;gBACT,aAAa;oBACZ,CAAA,GAAA,oBAAY,EAAE,MAAM,CAAC;wBACpB,aAAa;wBACb,aAAa;wBACb,MAAM;oBACP;iBACA;gBACD,UAAU;YACX;YACA,CAAA,GAAA,WAAG,EAAE,MAAM,CAAC;gBACX,MAAM;gBACN,SAAS;gBACT,aAAa;gBACb,SAAS,EAAE;gBACX,SAAS;gBACT,aAAa;oBACZ,CAAA,GAAA,oBAAY,EAAE,MAAM,CAAC;wBACpB,aAAa;wBACb,aAAa;wBACb,MAAM;oBACP;iBACA;YACF;SACA;eACD;IACD,CAAA,GAAI,QAAQ,IAAI","sources":["taqueria-plugin-ipfs-pinata/index.ts","taqueria-plugin-ipfs-pinata/src/proxy.ts","taqueria-plugin-ipfs-pinata/src/file-processing.ts","taqueria-plugin-ipfs-pinata/src/pinata-api.ts","taqueria-plugin-ipfs-pinata/src/utils.ts"],"sourcesContent":["import { Option, Plugin, PositionalArg, Task } from '@taqueria/node-sdk';\nimport proxy from './src/proxy';\n\nPlugin.create(() => ({\n\tschema: '0.1',\n\tversion: '0.4.0',\n\talias: 'pinata',\n\ttasks: [\n\t\tTask.create({\n\t\t\ttask: 'publish',\n\t\t\tcommand: 'publish [path]',\n\t\t\tdescription: 'Upload and pin files using your pinata account.',\n\t\t\taliases: [],\n\t\t\thandler: 'proxy',\n\t\t\tpositionals: [\n\t\t\t\tPositionalArg.create({\n\t\t\t\t\tplaceholder: 'path',\n\t\t\t\t\tdescription: 'Directory or file path to publish',\n\t\t\t\t\ttype: 'string',\n\t\t\t\t}),\n\t\t\t],\n\t\t\tencoding: 'json',\n\t\t}),\n\t\tTask.create({\n\t\t\ttask: 'pin',\n\t\t\tcommand: 'pin [hash]',\n\t\t\tdescription: 'Pin a file already on ipfs with your pinata account.',\n\t\t\taliases: [],\n\t\t\thandler: 'proxy',\n\t\t\tpositionals: [\n\t\t\t\tPositionalArg.create({\n\t\t\t\t\tplaceholder: 'hash',\n\t\t\t\t\tdescription: 'Ipfs hash of the file or directory that is already on the ipfs network.',\n\t\t\t\t\ttype: 'string',\n\t\t\t\t}),\n\t\t\t],\n\t\t}),\n\t],\n\tproxy,\n}), process.argv);\n","import { sendAsyncErr, sendAsyncRes, sendErr, sendJsonRes } from '@taqueria/node-sdk';\nimport { RequestArgs } from '@taqueria/node-sdk';\nimport { LoadedConfig, SanitizedAbsPath } from '@taqueria/node-sdk/types';\nimport path from 'path';\nimport { processFiles } from './file-processing';\nimport { PinataAuth, pinHash, publishFileToIpfs } from './pinata-api';\nimport { createProcessBackoffController } from './utils';\n\n// Load .env for jwt token\n// TODO: How should this be stored in a secure way?\nimport 'dotenv/config';\n\n// TODO: What should this be, it was removed from the sdk\ntype PluginResponse =\n\t| void\n\t| {\n\t\trender: 'table';\n\t\tdata: unknown[];\n\t};\n\ninterface Opts extends RequestArgs.t {\n\treadonly path?: string;\n\treadonly hash?: string;\n\treadonly task?: string;\n}\n\nconst publishToIpfs = async (fileOrDirPath: undefined | string, auth: PinataAuth): Promise<PluginResponse> => {\n\tif (!fileOrDirPath) {\n\t\tthrow new Error(`path was not provided`);\n\t}\n\n\t// Pinata is limited to 180 requests per minute\n\t// So for the first 180 requests they can go fast\n\n\tconst { processWithBackoff } = createProcessBackoffController({\n\t\tretryCount: 5,\n\t\ttargetRequestsPerMinute: 180,\n\t});\n\n\tconst result = await processFiles({\n\t\tfileOrDirPath,\n\t\tparallelCount: 10,\n\t\tprocessFile: async filePath => {\n\t\t\t// // TEMP: Debug\n\t\t\t// console.log(`publishing: ${filePath}`);\n\n\t\t\treturn processWithBackoff(() =>\n\t\t\t\tpublishFileToIpfs({\n\t\t\t\t\tauth,\n\t\t\t\t\titem: { filePath, name: path.basename(filePath) },\n\t\t\t\t})\n\t\t\t);\n\t\t},\n\t\tonProgress: ({ processedFilesCount, estimateFileCount }) => {\n\t\t\tif (estimateFileCount && processedFilesCount % 10) {\n\t\t\t\tlet ratio = processedFilesCount / estimateFileCount;\n\t\t\t\tif (ratio > 1) ratio = 1;\n\n\t\t\t\t// // TODO: Call task sdk progress\n\t\t\t\t// console.log(`Progress: ${(ratio * 100).toFixed(0)}%`);\n\t\t\t}\n\t\t},\n\t});\n\n\t// // TEMP: DEBUG: Show error\n\t// if (result.failures.length) {\n\t// \tconsole.log('❗ Failures:\\n' + result.failures.map(f => `${f.filePath}: ${f.error}`).join('\\n'));\n\t// }\n\n\treturn {\n\t\trender: 'table',\n\t\tdata: [\n\t\t\t...result.failures.map(x => ({\n\t\t\t\t'?': '❌',\n\t\t\t\tfilePath: x.filePath,\n\t\t\t\tipfsHash: undefined,\n\t\t\t\terror: (x.error as { message?: string })?.message ?? JSON.stringify(x.error),\n\t\t\t})),\n\t\t\t...result.successes.map(x => ({\n\t\t\t\t'?': '✔',\n\t\t\t\tfilePath: x.filePath,\n\t\t\t\tipfsHash: x.result.ipfsHash,\n\t\t\t\terror: undefined,\n\t\t\t})),\n\t\t],\n\t};\n};\n\nconst pinToIpfs = async (hash: undefined | string, auth: PinataAuth): Promise<PluginResponse> => {\n\tif (!hash) {\n\t\tthrow new Error(`ipfs hash was not provided`);\n\t}\n\n\tawait pinHash({ ipfsHash: hash, auth });\n\n\treturn {\n\t\trender: 'table',\n\t\tdata: [{ ipfsHash: hash }],\n\t};\n};\n\nconst execute = async (opts: Opts): Promise<PluginResponse> => {\n\tconst {\n\t\ttask,\n\t\tpath,\n\t\thash,\n\t} = opts;\n\n\tconst auth: PinataAuth = {\n\t\t// TODO: Where should this be stored?\n\t\t// pinataJwtToken: (config as Record<string, any>).credentials.pinataJwtToken,\n\t\tpinataJwtToken: process.env['pinataJwtToken'] as string,\n\t};\n\n\tif (!auth.pinataJwtToken) {\n\t\tthrow new Error(`The 'credentials.pinataJwtToken' was not found in config`);\n\t}\n\n\tswitch (task) {\n\t\tcase 'publish':\n\t\t\treturn publishToIpfs(path, auth);\n\t\tcase 'pin':\n\t\t\treturn pinToIpfs(hash, auth);\n\t\tdefault:\n\t\t\tthrow new Error(`${task} is not an understood task by the ipfs-pinata plugin`);\n\t}\n};\n\nexport default async (args: RequestArgs.t): Promise<PluginResponse> => {\n\tconst opts = args as Opts;\n\n\ttry {\n\t\tconst resultRaw = await execute(opts) as Record<string, unknown>;\n\t\t// TODO: Fix deno parsing\n\t\t// Without this, `data.reduce is not a function`\n\t\tconst result = ('data' in resultRaw) ? resultRaw.data : resultRaw;\n\t\treturn sendJsonRes(result);\n\t} catch (err) {\n\t\tconst error = err as Error;\n\t\tif (error.message) {\n\t\t\treturn sendAsyncErr(error.message);\n\t\t}\n\t}\n};\n","import fs from 'fs/promises';\nimport path from 'path';\n\n// Async generator\n// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search\nasync function* getFiles(fileOrDirPath: string): AsyncGenerator<string, void, unknown> {\n\tconst dirInfo = await fs.stat(fileOrDirPath);\n\tif (dirInfo.isFile()) {\n\t\tyield fileOrDirPath;\n\t\treturn;\n\t}\n\n\tconst dirents = await fs.readdir(fileOrDirPath, { withFileTypes: true });\n\tfor (const dirent of dirents) {\n\t\tconst res = path.resolve(fileOrDirPath, dirent.name);\n\t\tif (dirent.isDirectory()) {\n\t\t\tyield* getFiles(res);\n\t\t} else {\n\t\t\tyield res;\n\t\t}\n\t}\n}\n\nconst createFileProvider = async ({\n\tfileOrDirPath,\n\tfilter,\n\tshouldEstimateFileCount,\n}: {\n\tfileOrDirPath: string;\n\tfilter?: (filePath: string) => boolean;\n\tshouldEstimateFileCount?: boolean;\n}) => {\n\tfileOrDirPath = path.resolve(fileOrDirPath);\n\tconst pathInfo = await fs.stat(fileOrDirPath);\n\tif (\n\t\t!pathInfo.isFile()\n\t\t&& !pathInfo.isDirectory()\n\t) {\n\t\tthrow new Error(`The path '${fileOrDirPath}' is not a file or directory`);\n\t}\n\n\tlet estimateFileCount = undefined as undefined | number;\n\tif (shouldEstimateFileCount) {\n\t\testimateFileCount = 0;\n\t\tfor await (const filePath of getFiles(fileOrDirPath)) {\n\t\t\tif (filter && !filter(filePath)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\testimateFileCount++;\n\t\t}\n\t}\n\n\tconst fileGenerator = getFiles(fileOrDirPath);\n\tconst getNextFile = async () => {\n\t\tlet nextFile = (await fileGenerator.next()).value;\n\t\tif (!filter) {\n\t\t\treturn nextFile;\n\t\t}\n\n\t\twhile (nextFile && !filter(nextFile)) {\n\t\t\tnextFile = await getNextFile();\n\t\t}\n\n\t\treturn nextFile;\n\t};\n\treturn {\n\t\tgetNextFile,\n\t\testimateFileCount,\n\t};\n};\n\ntype ProgressInfo = { processedFilesCount: number; estimateFileCount: undefined | number };\nexport const processFiles = async <TResult>({\n\tfileOrDirPath,\n\tprocessFile,\n\tfilter,\n\tparallelCount = 10,\n\tonProgress,\n}: {\n\tfileOrDirPath: string;\n\tprocessFile: (filePath: string, progress: ProgressInfo) => Promise<TResult>;\n\tfilter?: (filePath: string) => boolean;\n\tparallelCount?: number;\n\tonProgress?: (progress: ProgressInfo) => void;\n}) => {\n\tconst { getNextFile, estimateFileCount } = await createFileProvider({\n\t\tfileOrDirPath,\n\t\tfilter,\n\t\tshouldEstimateFileCount: true,\n\t});\n\n\tconst successes = [] as { filePath: string; result: TResult }[];\n\tconst failures = [] as { filePath: string; error: unknown }[];\n\n\tonProgress?.({\n\t\tprocessedFilesCount: 0,\n\t\testimateFileCount,\n\t});\n\n\tawait Promise.all([...new Array(parallelCount)].map(async x => {\n\t\tlet fileToProcess = await getNextFile();\n\t\twhile (fileToProcess) {\n\t\t\tconst progressInfo = {\n\t\t\t\tprocessedFilesCount: successes.length + failures.length,\n\t\t\t\testimateFileCount,\n\t\t\t};\n\t\t\tonProgress?.(progressInfo);\n\n\t\t\ttry {\n\t\t\t\tconst result = await processFile(fileToProcess, progressInfo);\n\t\t\t\tsuccesses.push({ filePath: fileToProcess, result });\n\t\t\t} catch (err) {\n\t\t\t\tfailures.push({ filePath: fileToProcess, error: err });\n\t\t\t}\n\n\t\t\tfileToProcess = await getNextFile();\n\t\t}\n\t}));\n\n\tonProgress?.({\n\t\tprocessedFilesCount: successes.length + failures.length,\n\t\testimateFileCount,\n\t});\n\n\treturn {\n\t\tsuccesses,\n\t\tfailures,\n\t};\n};\n","import FormData from 'form-data';\nimport fs from 'fs';\nimport Hash from 'ipfs-only-hash';\nimport fetch from 'node-fetch';\n\nexport type PinataAuth = {\n\tpinataJwtToken: string;\n};\n\nexport type PublishFileResult = {\n\tipfsHash: string;\n};\n\nexport const publishFileToIpfs = async ({\n\tauth,\n\titem,\n}: {\n\tauth: PinataAuth;\n\titem: {\n\t\tname: string;\n\t\tfilePath: string;\n\t};\n}): Promise<PublishFileResult> => {\n\t// The data api to check for existing file is limited to 30 requests per minute\n\t// While uploading allows 180 requests per minute\n\t// i.e. it's faster to just upload again\n\n\t// // Skip if already pinned\n\t// const { isPinned, ipfsHash } = await checkIfFileIsPinned({ auth, item });\n\t// if (isPinned) {\n\t// \treturn {\n\t// \t\tipfsHash,\n\t// \t};\n\t// }\n\n\tconst data = new FormData();\n\tdata.append('file', fs.createReadStream(item.filePath));\n\tdata.append(\n\t\t'pinataMetadata',\n\t\tJSON.stringify({\n\t\t\tname: item.name,\n\t\t}),\n\t);\n\n\tconst response = await fetch(`https://api.pinata.cloud/pinning/pinFileToIPFS`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t\t'Content-Type': `multipart/form-data; boundary=${(data as unknown as { _boundary: string })._boundary}`,\n\t\t},\n\t\tbody: data,\n\t\tmethod: 'post',\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to upload '${item.name}' to ipfs ${response.statusText}`);\n\t}\n\n\tconst uploadResult = await response.json() as {\n\t\tIpfsHash: string; // This is the IPFS multi-hash provided back for your content,\n\t\tPinSize: string; // This is how large (in bytes) the content you just pinned is,\n\t\tTimestamp: string; // This is the timestamp for your content pinning (represented in ISO 8601 format)\n\t};\n\n\treturn {\n\t\tipfsHash: uploadResult.IpfsHash,\n\t};\n};\n\nconst checkIfFileIsPinned = async ({\n\tauth,\n\titem,\n}: {\n\tauth: PinataAuth;\n\titem: {\n\t\tname: string;\n\t\tfilePath: string;\n\t};\n}) => {\n\tconst ipfsHash = await Hash.of(fs.createReadStream(item.filePath));\n\n\tconst response = await fetch(`https://api.pinata.cloud/data/pinList?status=pinned&hashContains=${ipfsHash}`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t},\n\t\tmethod: 'get',\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to query '${item.name}' status from pinata ${response.statusText}`);\n\t}\n\n\tconst pinResult = await response.json() as {\n\t\tcount: number;\n\t\trows: {\n\t\t\tid: string;\n\t\t\tipfs_pin_hash: string;\n\t\t\tsize: number;\n\t\t\tuser_id: string;\n\t\t\tdate_pinned: null | string;\n\t\t\tdate_unpinned: null | string;\n\t\t\tmetadata: {\n\t\t\t\tname: string;\n\t\t\t\tkeyvalues: null | string;\n\t\t\t};\n\t\t\tregions: {\n\t\t\t\tregionId: string;\n\t\t\t\tcurrentReplicationCount: number;\n\t\t\t\tdesiredReplicationCount: number;\n\t\t\t}[];\n\t\t}[];\n\t};\n\n\tconst isPinned = pinResult.rows.some(x =>\n\t\tx.ipfs_pin_hash === ipfsHash\n\t\t&& x.date_pinned\n\t\t&& !x.date_unpinned\n\t);\n\n\treturn {\n\t\tisPinned,\n\t\tipfsHash,\n\t};\n};\n\nexport const pinHash = async ({\n\tauth,\n\tipfsHash,\n}: {\n\tauth: PinataAuth;\n\tipfsHash: string;\n}) => {\n\tconst response = await fetch(`https://api.pinata.cloud/pinning/pinByHash`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t\t'Content-Type': 'application/json',\n\t\t},\n\t\tmethod: 'post',\n\t\tbody: JSON.stringify({\n\t\t\thashToPin: ipfsHash,\n\t\t}),\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to pin '${ipfsHash}' with pinata: ${response.statusText}`);\n\t}\n\n\t// Ok is the only response if successful\n\treturn;\n};\n","export async function delay(timeout: number): Promise<void> {\n\treturn await new Promise(resolve => {\n\t\tsetTimeout(resolve, timeout);\n\t});\n}\n\nexport const createProcessBackoffController = ({\n\tretryCount = 5,\n\ttargetRequestsPerMinute = 180,\n}: {\n\tretryCount?: number;\n\ttargetRequestsPerMinute?: number;\n}) => {\n\tlet averageTimePerRequest = 5000;\n\tlet targetTimePerRequest = 60000 / targetRequestsPerMinute;\n\tlet lastTime = Date.now();\n\n\tconst processWithBackoff = async <TResult>(process: () => Promise<TResult>) => {\n\t\tlet attempt = 0;\n\t\tlet lastError = undefined as unknown;\n\t\twhile (attempt < retryCount) {\n\t\t\ttry {\n\t\t\t\tlet delayTimeMs = Math.max(10, targetTimePerRequest - averageTimePerRequest);\n\n\t\t\t\t// Partially randomized delay to ensure parallel requests don't line up\n\t\t\t\tawait delay(Math.floor(delayTimeMs * (1 + 0.5 * Math.random())));\n\n\t\t\t\tconst result = await process();\n\n\t\t\t\tconst timeNow = Date.now();\n\t\t\t\tconst timeElapsed = timeNow - lastTime;\n\t\t\t\tlastTime = timeNow;\n\n\t\t\t\t// Running average\n\t\t\t\taverageTimePerRequest = averageTimePerRequest * 0.97 + timeElapsed * 0.03;\n\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tlastError = err;\n\t\t\t}\n\n\t\t\t// Quickly increase time to wait if failure (allow negatives to wait longer than target)\n\t\t\taverageTimePerRequest -= (attempt + 1) * 1000;\n\t\t\tattempt++;\n\t\t}\n\n\t\t// All attempts failed\n\t\tthrow lastError;\n\t};\n\n\treturn {\n\t\tprocessWithBackoff,\n\t};\n};\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../"}
|
|
1
|
+
{"version":3,"sources":["index.ts","src/proxy.ts","src/file-processing.ts","src/pinata-api.ts","src/utils.ts"],"sourcesContent":["import { Option, Plugin, PositionalArg, Task } from '@taqueria/node-sdk';\nimport proxy from './src/proxy';\n\nPlugin.create(() => ({\n\tschema: '0.1',\n\tversion: '0.4.0',\n\talias: 'pinata',\n\ttasks: [\n\t\tTask.create({\n\t\t\ttask: 'publish',\n\t\t\tcommand: 'publish [path]',\n\t\t\tdescription: 'Upload and pin files using your pinata account.',\n\t\t\taliases: [],\n\t\t\thandler: 'proxy',\n\t\t\tpositionals: [\n\t\t\t\tPositionalArg.create({\n\t\t\t\t\tplaceholder: 'path',\n\t\t\t\t\tdescription: 'Directory or file path to publish',\n\t\t\t\t\ttype: 'string',\n\t\t\t\t}),\n\t\t\t],\n\t\t\tencoding: 'json',\n\t\t}),\n\t\tTask.create({\n\t\t\ttask: 'pin',\n\t\t\tcommand: 'pin [hash]',\n\t\t\tdescription: 'Pin a file already on ipfs with your pinata account.',\n\t\t\taliases: [],\n\t\t\thandler: 'proxy',\n\t\t\tpositionals: [\n\t\t\t\tPositionalArg.create({\n\t\t\t\t\tplaceholder: 'hash',\n\t\t\t\t\tdescription: 'Ipfs hash of the file or directory that is already on the ipfs network.',\n\t\t\t\t\ttype: 'string',\n\t\t\t\t}),\n\t\t\t],\n\t\t}),\n\t],\n\tproxy,\n}), process.argv);\n","import { sendAsyncErr, sendAsyncRes, sendErr, sendJsonRes } from '@taqueria/node-sdk';\nimport { RequestArgs } from '@taqueria/node-sdk';\nimport { LoadedConfig, SanitizedAbsPath } from '@taqueria/node-sdk/types';\nimport path from 'path';\nimport { processFiles } from './file-processing';\nimport { PinataAuth, pinHash, publishFileToIpfs } from './pinata-api';\nimport { createProcessBackoffController } from './utils';\n\n// Load .env for jwt token\n// TODO: How should this be stored in a secure way?\nimport 'dotenv/config';\n\n// TODO: What should this be, it was removed from the sdk\ntype PluginResponse =\n\t| void\n\t| {\n\t\trender: 'table';\n\t\tdata: unknown[];\n\t};\n\ninterface Opts extends RequestArgs.t {\n\treadonly path?: string;\n\treadonly hash?: string;\n\treadonly task?: string;\n}\n\nconst publishToIpfs = async (fileOrDirPath: undefined | string, auth: PinataAuth): Promise<PluginResponse> => {\n\tif (!fileOrDirPath) {\n\t\tthrow new Error(`path was not provided`);\n\t}\n\n\t// Pinata is limited to 180 requests per minute\n\t// So for the first 180 requests they can go fast\n\n\tconst { processWithBackoff } = createProcessBackoffController({\n\t\tretryCount: 5,\n\t\ttargetRequestsPerMinute: 180,\n\t});\n\n\tconst result = await processFiles({\n\t\tfileOrDirPath,\n\t\tparallelCount: 10,\n\t\tprocessFile: async filePath => {\n\t\t\t// // TEMP: Debug\n\t\t\t// console.log(`publishing: ${filePath}`);\n\n\t\t\treturn processWithBackoff(() =>\n\t\t\t\tpublishFileToIpfs({\n\t\t\t\t\tauth,\n\t\t\t\t\titem: { filePath, name: path.basename(filePath) },\n\t\t\t\t})\n\t\t\t);\n\t\t},\n\t\tonProgress: ({ processedFilesCount, estimateFileCount }) => {\n\t\t\tif (estimateFileCount && processedFilesCount % 10) {\n\t\t\t\tlet ratio = processedFilesCount / estimateFileCount;\n\t\t\t\tif (ratio > 1) ratio = 1;\n\n\t\t\t\t// // TODO: Call task sdk progress\n\t\t\t\t// console.log(`Progress: ${(ratio * 100).toFixed(0)}%`);\n\t\t\t}\n\t\t},\n\t});\n\n\t// // TEMP: DEBUG: Show error\n\t// if (result.failures.length) {\n\t// \tconsole.log('❗ Failures:\\n' + result.failures.map(f => `${f.filePath}: ${f.error}`).join('\\n'));\n\t// }\n\n\treturn {\n\t\trender: 'table',\n\t\tdata: [\n\t\t\t...result.failures.map(x => ({\n\t\t\t\t'?': '❌',\n\t\t\t\tfilePath: x.filePath,\n\t\t\t\tipfsHash: undefined,\n\t\t\t\terror: (x.error as { message?: string })?.message ?? JSON.stringify(x.error),\n\t\t\t})),\n\t\t\t...result.successes.map(x => ({\n\t\t\t\t'?': '✔',\n\t\t\t\tfilePath: x.filePath,\n\t\t\t\tipfsHash: x.result.ipfsHash,\n\t\t\t\terror: undefined,\n\t\t\t})),\n\t\t],\n\t};\n};\n\nconst pinToIpfs = async (hash: undefined | string, auth: PinataAuth): Promise<PluginResponse> => {\n\tif (!hash) {\n\t\tthrow new Error(`ipfs hash was not provided`);\n\t}\n\n\tawait pinHash({ ipfsHash: hash, auth });\n\n\treturn {\n\t\trender: 'table',\n\t\tdata: [{ ipfsHash: hash }],\n\t};\n};\n\nconst execute = async (opts: Opts): Promise<PluginResponse> => {\n\tconst {\n\t\ttask,\n\t\tpath,\n\t\thash,\n\t} = opts;\n\n\tconst auth: PinataAuth = {\n\t\t// TODO: Where should this be stored?\n\t\t// pinataJwtToken: (config as Record<string, any>).credentials.pinataJwtToken,\n\t\tpinataJwtToken: process.env['pinataJwtToken'] as string,\n\t};\n\n\tif (!auth.pinataJwtToken) {\n\t\tthrow new Error(`The 'credentials.pinataJwtToken' was not found in config`);\n\t}\n\n\tswitch (task) {\n\t\tcase 'publish':\n\t\t\treturn publishToIpfs(path, auth);\n\t\tcase 'pin':\n\t\t\treturn pinToIpfs(hash, auth);\n\t\tdefault:\n\t\t\tthrow new Error(`${task} is not an understood task by the ipfs-pinata plugin`);\n\t}\n};\n\nexport default async (args: RequestArgs.t): Promise<PluginResponse> => {\n\tconst opts = args as Opts;\n\n\ttry {\n\t\tconst resultRaw = await execute(opts) as Record<string, unknown>;\n\t\t// TODO: Fix deno parsing\n\t\t// Without this, `data.reduce is not a function`\n\t\tconst result = ('data' in resultRaw) ? resultRaw.data : resultRaw;\n\t\treturn sendJsonRes(result);\n\t} catch (err) {\n\t\tconst error = err as Error;\n\t\tif (error.message) {\n\t\t\treturn sendAsyncErr(error.message);\n\t\t}\n\t}\n};\n","import fs from 'fs/promises';\nimport path from 'path';\n\n// Async generator\n// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search\nasync function* getFiles(fileOrDirPath: string): AsyncGenerator<string, void, unknown> {\n\tconst dirInfo = await fs.stat(fileOrDirPath);\n\tif (dirInfo.isFile()) {\n\t\tyield fileOrDirPath;\n\t\treturn;\n\t}\n\n\tconst dirents = await fs.readdir(fileOrDirPath, { withFileTypes: true });\n\tfor (const dirent of dirents) {\n\t\tconst res = path.resolve(fileOrDirPath, dirent.name);\n\t\tif (dirent.isDirectory()) {\n\t\t\tyield* getFiles(res);\n\t\t} else {\n\t\t\tyield res;\n\t\t}\n\t}\n}\n\nconst createFileProvider = async ({\n\tfileOrDirPath,\n\tfilter,\n\tshouldEstimateFileCount,\n}: {\n\tfileOrDirPath: string;\n\tfilter?: (filePath: string) => boolean;\n\tshouldEstimateFileCount?: boolean;\n}) => {\n\tfileOrDirPath = path.resolve(fileOrDirPath);\n\tconst pathInfo = await fs.stat(fileOrDirPath);\n\tif (\n\t\t!pathInfo.isFile()\n\t\t&& !pathInfo.isDirectory()\n\t) {\n\t\tthrow new Error(`The path '${fileOrDirPath}' is not a file or directory`);\n\t}\n\n\tlet estimateFileCount = undefined as undefined | number;\n\tif (shouldEstimateFileCount) {\n\t\testimateFileCount = 0;\n\t\tfor await (const filePath of getFiles(fileOrDirPath)) {\n\t\t\tif (filter && !filter(filePath)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\testimateFileCount++;\n\t\t}\n\t}\n\n\tconst fileGenerator = getFiles(fileOrDirPath);\n\tconst getNextFile = async () => {\n\t\tlet nextFile = (await fileGenerator.next()).value;\n\t\tif (!filter) {\n\t\t\treturn nextFile;\n\t\t}\n\n\t\twhile (nextFile && !filter(nextFile)) {\n\t\t\tnextFile = await getNextFile();\n\t\t}\n\n\t\treturn nextFile;\n\t};\n\treturn {\n\t\tgetNextFile,\n\t\testimateFileCount,\n\t};\n};\n\ntype ProgressInfo = { processedFilesCount: number; estimateFileCount: undefined | number };\nexport const processFiles = async <TResult>({\n\tfileOrDirPath,\n\tprocessFile,\n\tfilter,\n\tparallelCount = 10,\n\tonProgress,\n}: {\n\tfileOrDirPath: string;\n\tprocessFile: (filePath: string, progress: ProgressInfo) => Promise<TResult>;\n\tfilter?: (filePath: string) => boolean;\n\tparallelCount?: number;\n\tonProgress?: (progress: ProgressInfo) => void;\n}) => {\n\tconst { getNextFile, estimateFileCount } = await createFileProvider({\n\t\tfileOrDirPath,\n\t\tfilter,\n\t\tshouldEstimateFileCount: true,\n\t});\n\n\tconst successes = [] as { filePath: string; result: TResult }[];\n\tconst failures = [] as { filePath: string; error: unknown }[];\n\n\tonProgress?.({\n\t\tprocessedFilesCount: 0,\n\t\testimateFileCount,\n\t});\n\n\tawait Promise.all([...new Array(parallelCount)].map(async x => {\n\t\tlet fileToProcess = await getNextFile();\n\t\twhile (fileToProcess) {\n\t\t\tconst progressInfo = {\n\t\t\t\tprocessedFilesCount: successes.length + failures.length,\n\t\t\t\testimateFileCount,\n\t\t\t};\n\t\t\tonProgress?.(progressInfo);\n\n\t\t\ttry {\n\t\t\t\tconst result = await processFile(fileToProcess, progressInfo);\n\t\t\t\tsuccesses.push({ filePath: fileToProcess, result });\n\t\t\t} catch (err) {\n\t\t\t\tfailures.push({ filePath: fileToProcess, error: err });\n\t\t\t}\n\n\t\t\tfileToProcess = await getNextFile();\n\t\t}\n\t}));\n\n\tonProgress?.({\n\t\tprocessedFilesCount: successes.length + failures.length,\n\t\testimateFileCount,\n\t});\n\n\treturn {\n\t\tsuccesses,\n\t\tfailures,\n\t};\n};\n","import FormData from 'form-data';\nimport fs from 'fs';\nimport Hash from 'ipfs-only-hash';\nimport fetch from 'node-fetch';\n\nexport type PinataAuth = {\n\tpinataJwtToken: string;\n};\n\nexport type PublishFileResult = {\n\tipfsHash: string;\n};\n\nexport const publishFileToIpfs = async ({\n\tauth,\n\titem,\n}: {\n\tauth: PinataAuth;\n\titem: {\n\t\tname: string;\n\t\tfilePath: string;\n\t};\n}): Promise<PublishFileResult> => {\n\t// The data api to check for existing file is limited to 30 requests per minute\n\t// While uploading allows 180 requests per minute\n\t// i.e. it's faster to just upload again\n\n\t// // Skip if already pinned\n\t// const { isPinned, ipfsHash } = await checkIfFileIsPinned({ auth, item });\n\t// if (isPinned) {\n\t// \treturn {\n\t// \t\tipfsHash,\n\t// \t};\n\t// }\n\n\tconst data = new FormData();\n\tdata.append('file', fs.createReadStream(item.filePath));\n\tdata.append(\n\t\t'pinataMetadata',\n\t\tJSON.stringify({\n\t\t\tname: item.name,\n\t\t}),\n\t);\n\n\tconst response = await fetch(`https://api.pinata.cloud/pinning/pinFileToIPFS`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t\t'Content-Type': `multipart/form-data; boundary=${(data as unknown as { _boundary: string })._boundary}`,\n\t\t},\n\t\tbody: data,\n\t\tmethod: 'post',\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to upload '${item.name}' to ipfs ${response.statusText}`);\n\t}\n\n\tconst uploadResult = await response.json() as {\n\t\tIpfsHash: string; // This is the IPFS multi-hash provided back for your content,\n\t\tPinSize: string; // This is how large (in bytes) the content you just pinned is,\n\t\tTimestamp: string; // This is the timestamp for your content pinning (represented in ISO 8601 format)\n\t};\n\n\treturn {\n\t\tipfsHash: uploadResult.IpfsHash,\n\t};\n};\n\nconst checkIfFileIsPinned = async ({\n\tauth,\n\titem,\n}: {\n\tauth: PinataAuth;\n\titem: {\n\t\tname: string;\n\t\tfilePath: string;\n\t};\n}) => {\n\tconst ipfsHash = await Hash.of(fs.createReadStream(item.filePath));\n\n\tconst response = await fetch(`https://api.pinata.cloud/data/pinList?status=pinned&hashContains=${ipfsHash}`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t},\n\t\tmethod: 'get',\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to query '${item.name}' status from pinata ${response.statusText}`);\n\t}\n\n\tconst pinResult = await response.json() as {\n\t\tcount: number;\n\t\trows: {\n\t\t\tid: string;\n\t\t\tipfs_pin_hash: string;\n\t\t\tsize: number;\n\t\t\tuser_id: string;\n\t\t\tdate_pinned: null | string;\n\t\t\tdate_unpinned: null | string;\n\t\t\tmetadata: {\n\t\t\t\tname: string;\n\t\t\t\tkeyvalues: null | string;\n\t\t\t};\n\t\t\tregions: {\n\t\t\t\tregionId: string;\n\t\t\t\tcurrentReplicationCount: number;\n\t\t\t\tdesiredReplicationCount: number;\n\t\t\t}[];\n\t\t}[];\n\t};\n\n\tconst isPinned = pinResult.rows.some(x =>\n\t\tx.ipfs_pin_hash === ipfsHash\n\t\t&& x.date_pinned\n\t\t&& !x.date_unpinned\n\t);\n\n\treturn {\n\t\tisPinned,\n\t\tipfsHash,\n\t};\n};\n\nexport const pinHash = async ({\n\tauth,\n\tipfsHash,\n}: {\n\tauth: PinataAuth;\n\tipfsHash: string;\n}) => {\n\tconst response = await fetch(`https://api.pinata.cloud/pinning/pinByHash`, {\n\t\theaders: {\n\t\t\tAuthorization: `Bearer ${auth.pinataJwtToken}`,\n\t\t\t'Content-Type': 'application/json',\n\t\t},\n\t\tmethod: 'post',\n\t\tbody: JSON.stringify({\n\t\t\thashToPin: ipfsHash,\n\t\t}),\n\t});\n\n\tif (!response.ok) {\n\t\tthrow new Error(`Failed to pin '${ipfsHash}' with pinata: ${response.statusText}`);\n\t}\n\n\t// Ok is the only response if successful\n\treturn;\n};\n","export async function delay(timeout: number): Promise<void> {\n\treturn await new Promise(resolve => {\n\t\tsetTimeout(resolve, timeout);\n\t});\n}\n\nexport const createProcessBackoffController = ({\n\tretryCount = 5,\n\ttargetRequestsPerMinute = 180,\n}: {\n\tretryCount?: number;\n\ttargetRequestsPerMinute?: number;\n}) => {\n\tlet averageTimePerRequest = 5000;\n\tlet targetTimePerRequest = 60000 / targetRequestsPerMinute;\n\tlet lastTime = Date.now();\n\n\tconst processWithBackoff = async <TResult>(process: () => Promise<TResult>) => {\n\t\tlet attempt = 0;\n\t\tlet lastError = undefined as unknown;\n\t\twhile (attempt < retryCount) {\n\t\t\ttry {\n\t\t\t\tlet delayTimeMs = Math.max(10, targetTimePerRequest - averageTimePerRequest);\n\n\t\t\t\t// Partially randomized delay to ensure parallel requests don't line up\n\t\t\t\tawait delay(Math.floor(delayTimeMs * (1 + 0.5 * Math.random())));\n\n\t\t\t\tconst result = await process();\n\n\t\t\t\tconst timeNow = Date.now();\n\t\t\t\tconst timeElapsed = timeNow - lastTime;\n\t\t\t\tlastTime = timeNow;\n\n\t\t\t\t// Running average\n\t\t\t\taverageTimePerRequest = averageTimePerRequest * 0.97 + timeElapsed * 0.03;\n\n\t\t\t\treturn result;\n\t\t\t} catch (err) {\n\t\t\t\tlastError = err;\n\t\t\t}\n\n\t\t\t// Quickly increase time to wait if failure (allow negatives to wait longer than target)\n\t\t\taverageTimePerRequest -= (attempt + 1) * 1000;\n\t\t\tattempt++;\n\t\t}\n\n\t\t// All attempts failed\n\t\tthrow lastError;\n\t};\n\n\treturn {\n\t\tprocessWithBackoff,\n\t};\n};\n"],"mappings":";AAAA,SAAiB,QAAQ,eAAe,YAAY;;;ACApD,SAAS,cAAqC,mBAAmB;AAGjE,OAAOA,WAAU;;;ACHjB,OAAO,QAAQ;AACf,OAAO,UAAU;AAIjB,gBAAgB,SAAS,eAA8D;AACtF,QAAM,UAAU,MAAM,GAAG,KAAK,aAAa;AAC3C,MAAI,QAAQ,OAAO,GAAG;AACrB,UAAM;AACN;AAAA,EACD;AAEA,QAAM,UAAU,MAAM,GAAG,QAAQ,eAAe,EAAE,eAAe,KAAK,CAAC;AACvE,aAAW,UAAU,SAAS;AAC7B,UAAM,MAAM,KAAK,QAAQ,eAAe,OAAO,IAAI;AACnD,QAAI,OAAO,YAAY,GAAG;AACzB,aAAO,SAAS,GAAG;AAAA,IACpB,OAAO;AACN,YAAM;AAAA,IACP;AAAA,EACD;AACD;AAEA,IAAM,qBAAqB,OAAO;AAAA,EACjC;AAAA,EACA;AAAA,EACA;AACD,MAIM;AACL,kBAAgB,KAAK,QAAQ,aAAa;AAC1C,QAAM,WAAW,MAAM,GAAG,KAAK,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process","path"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taqueria/plugin-ipfs-pinata",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.19-rc",
|
|
4
4
|
"description": "A plugin for Taqueria providing ipfs publishing and pinning using the Pinata service",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"taqueria",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
|
-
"build": "npx tsc -noEmit -p ./tsconfig.json && npx
|
|
25
|
+
"build": "npx tsc -noEmit -p ./tsconfig.json && npx tsup"
|
|
26
26
|
},
|
|
27
27
|
"author": "ECAD Labs",
|
|
28
28
|
"license": "Apache-2.0",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"directory": "taqueria-plugin-ipfs-pinata"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@taqueria/node-sdk": "^0.25.
|
|
36
|
+
"@taqueria/node-sdk": "^0.25.19-rc",
|
|
37
37
|
"dotenv": "^16.0.0",
|
|
38
38
|
"form-data": "^4.0.0",
|
|
39
39
|
"ipfs-only-hash": "^4.0.0",
|
|
@@ -41,7 +41,23 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node-fetch": "^2.6.1",
|
|
44
|
-
"
|
|
44
|
+
"tsup": "^6.1.3",
|
|
45
45
|
"typescript": "4.7.2"
|
|
46
|
+
},
|
|
47
|
+
"tsup": {
|
|
48
|
+
"entry": [
|
|
49
|
+
"index.ts"
|
|
50
|
+
],
|
|
51
|
+
"sourcemap": true,
|
|
52
|
+
"target": "node16",
|
|
53
|
+
"outDir": "./",
|
|
54
|
+
"dts": true,
|
|
55
|
+
"clean": false,
|
|
56
|
+
"skipNodeModulesBundle": true,
|
|
57
|
+
"platform": "node",
|
|
58
|
+
"format": [
|
|
59
|
+
"esm",
|
|
60
|
+
"cjs"
|
|
61
|
+
]
|
|
46
62
|
}
|
|
47
63
|
}
|