Skip to content

Commit 68842a0

Browse files
fix Body is unusable err message (calling await res.text() twice) (#42)
* fix Body is unusable err message (calling await res.text() twice) * bump version * Trigger pipeline * Remove console.log --------- Co-authored-by: Dimasik Kolezhniuk <[email protected]>
1 parent 30401f6 commit 68842a0

File tree

3 files changed

+70
-45
lines changed

3 files changed

+70
-45
lines changed

.github/workflows/ci.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ jobs:
5151
- name: Run Tests
5252
env:
5353
IPFS_URL: ${{ secrets.IPFS_URL }}
54+
IPFS_GATEWAY_URL: ${{ secrets.IPFS_GATEWAY_URL }}
5455
run: npm run test

src/loaders/jsonld-loader.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,7 @@ async function loadFromIPFSNode(url: string, ipfsNodeURL: string): Promise<Remot
199199
const { res, body } = await _fetch({ url: catRequestURL, method: 'POST' });
200200

201201
if (res.status != 200) {
202-
let errorBody: string;
203-
try {
204-
errorBody = await res.text();
205-
} catch (e) {
206-
// eslint-disable-next-line no-console
207-
console.warn(e);
208-
}
209-
210-
throw new Error(`Error calling IPFS node: [${res.status}] ${res.statusText}\n${errorBody}`);
202+
throw new Error(`Error calling IPFS node: [${res.status}] ${res.statusText}\n${body}`);
211203
}
212204

213205
return {

tests/merklization.test.ts

+68-36
Original file line numberDiff line numberDiff line change
@@ -1144,8 +1144,14 @@ describe('merklize document with ipfs context', () => {
11441144
// node --experimental-vm-modules node_modules/jest/bin/jest.js -t 'set kubo client' tests/merklization.test.ts
11451145

11461146
const ipfsNodeURL = process.env.IPFS_URL ?? null;
1147+
const ipfsGatewayURL = process.env.IPFS_GATEWAY_URL ?? null;
1148+
11471149
if (!ipfsNodeURL) {
1148-
throw new Error('IPFS_URL is not set, skipping IPFS Node test');
1150+
throw new Error('IPFS_URL is not set');
1151+
}
1152+
1153+
if (!ipfsGatewayURL) {
1154+
throw new Error('IPFS_GATEWAY_URL is not set');
11491155
}
11501156

11511157
beforeAll(async () => {
@@ -1168,7 +1174,7 @@ describe('merklize document with ipfs context', () => {
11681174
it('ipfsGatewayURL is set', async () => {
11691175
const opts = {
11701176
documentLoader: cacheLoader({
1171-
ipfsNodeURL
1177+
ipfsGatewayURL
11721178
})
11731179
};
11741180
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, opts);
@@ -1186,7 +1192,7 @@ describe('merklize document with ipfs context', () => {
11861192
it('TestExistenceProofIPFS', async () => {
11871193
const opts = {
11881194
documentLoader: cacheLoader({
1189-
ipfsGatewayURL: 'https://ipfs.io'
1195+
ipfsGatewayURL
11901196
})
11911197
};
11921198
const mz = await Merklizer.merklizeJSONLD(testDocumentIPFS, opts);
@@ -1206,42 +1212,68 @@ describe('merklize document with ipfs context', () => {
12061212
});
12071213

12081214
async function pushSchemasToIPFS(ipfsNodeURL: string): Promise<void> {
1209-
const citizenshipData = await readFile('tests/testdata/citizenship-v1.jsonld');
1210-
const bbsData = await readFile('tests/testdata/dir1/dir2/bbs-v2.jsonld');
1211-
const formData = new FormData();
1212-
formData.append(
1213-
'file',
1214-
new Blob([citizenshipData], { type: 'application/octet-stream' }),
1215-
'citizenship-v1.jsonld'
1216-
);
1217-
formData.append(
1218-
'file',
1219-
new Blob([bbsData], { type: 'application/octet-stream' }),
1220-
'dir1/dir2/bbs-v2.jsonld'
1221-
);
1222-
let url: string | URL = normalizeIPFSNodeURL(ipfsNodeURL, 'add');
1223-
url = new URL(url);
1224-
let headers = {};
1225-
if (url.username && url.password) {
1226-
headers = {
1227-
authorization: `Basic ${btoa(url.username + ':' + url.password)}`
1228-
};
1215+
const getUrl = (uri: string, method: string): { url: string; headers: unknown } => {
1216+
const url: string | URL = new URL(normalizeIPFSNodeURL(uri, method));
1217+
1218+
const headers =
1219+
url.username && url.password
1220+
? {
1221+
authorization: `Basic ${btoa(url.username + ':' + url.password)}`
1222+
}
1223+
: {};
12291224
url.username = '';
12301225
url.password = '';
1226+
1227+
return { url: url.toString(), headers };
1228+
};
1229+
1230+
const { url: catUrl, headers } = getUrl(ipfsNodeURL, 'cat');
1231+
const catOpts = { headers, method: 'POST' };
1232+
1233+
try {
1234+
const cat = await Promise.all([
1235+
fetch(`${catUrl}?arg=QmdP4MZkESEabRVB322r2xWm7TCi7LueMNWMJawYmSy7hp`, {
1236+
...catOpts
1237+
} as RequestInit).then((r) => r.text()),
1238+
fetch(`${catUrl}?arg=Qmbp4kwoHULnmK71abrxdksjPH5sAjxSAXU5PEp2XRMFNw/dir2/bbs-v2.jsonld`, {
1239+
...catOpts
1240+
} as RequestInit).then((r) => r.text())
1241+
]);
1242+
const records = cat.map((r) => JSON.parse(r)['@context']).filter(Boolean);
1243+
if (records.length !== 2) {
1244+
throw new Error('IPFS records not found');
1245+
}
1246+
} catch (e) {
1247+
console.warn('try to upload document', e);
1248+
const citizenshipData = await readFile('tests/testdata/citizenship-v1.jsonld');
1249+
const bbsData = await readFile('tests/testdata/dir1/dir2/bbs-v2.jsonld');
1250+
const formData = new FormData();
1251+
formData.append(
1252+
'file',
1253+
new Blob([citizenshipData], { type: 'application/octet-stream' }),
1254+
'citizenship-v1.jsonld'
1255+
);
1256+
formData.append(
1257+
'file',
1258+
new Blob([bbsData], { type: 'application/octet-stream' }),
1259+
'dir1/dir2/bbs-v2.jsonld'
1260+
);
1261+
const { url: addUrl, headers: addHeaders } = getUrl(ipfsNodeURL, 'add');
1262+
1263+
const res = await fetch(addUrl, {
1264+
method: 'POST',
1265+
body: formData,
1266+
headers: addHeaders as HeadersInit
1267+
});
1268+
const resBody = await res.text();
1269+
const records = resBody
1270+
.split('\n')
1271+
.filter((l) => l.trim().length > 0)
1272+
.map((l) => JSON.parse(l).Hash);
1273+
// Check that URLs from ipfsDocument are uploaded to IPFS
1274+
expect(records).toContain('QmdP4MZkESEabRVB322r2xWm7TCi7LueMNWMJawYmSy7hp');
1275+
expect(records).toContain('Qmbp4kwoHULnmK71abrxdksjPH5sAjxSAXU5PEp2XRMFNw');
12311276
}
1232-
const res = await fetch(url, {
1233-
method: 'POST',
1234-
body: formData,
1235-
headers
1236-
});
1237-
const resBody = await res.text();
1238-
const records = resBody
1239-
.split('\n')
1240-
.filter((l) => l.trim().length > 0)
1241-
.map((l) => JSON.parse(l).Hash);
1242-
// Check that URLs from ipfsDocument are uploaded to IPFS
1243-
expect(records).toContain('QmdP4MZkESEabRVB322r2xWm7TCi7LueMNWMJawYmSy7hp');
1244-
expect(records).toContain('Qmbp4kwoHULnmK71abrxdksjPH5sAjxSAXU5PEp2XRMFNw');
12451277
}
12461278

12471279
function strHash(str: string): string {

0 commit comments

Comments
 (0)