Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update node-fetch in the API to fix connection drop issue #1397

Merged
merged 13 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
app/bundle.api.js*
app/stats.json
migrations
default-db.sqlite
5 changes: 2 additions & 3 deletions packages/api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fetch from 'node-fetch';

// eslint-disable-next-line import/extensions
import * as bundle from './app/bundle.api.js';
import * as injected from './injected';
Expand All @@ -18,7 +16,8 @@ export async function init(config = {}) {
return;
}

global.fetch = fetch;
global.fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));

await bundle.init(config);
actualApp = bundle.lib;
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"better-sqlite3": "^8.2.0",
"node-fetch": "^2.6.9",
"node-fetch": "^3.3.2",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/api/tsconfig.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// the latest Node 16.x release supports all of the features
"target": "es2021",
"module": "CommonJS",
"moduleResolution": "Node16",
"noEmit": false,
"declaration": true,
"outDir": "dist"
Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/platform/server/fetch/index.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fetch = self.fetch;
4 changes: 0 additions & 4 deletions packages/loot-core/src/platform/server/fetch/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ export function fetch(
input: RequestInfo | URL,
options?: unknown,
): Promise<Response>;
export function fetchBinary(
input: RequestInfo | URL,
filepath: string,
): Promise<unknown>;
20 changes: 1 addition & 19 deletions packages/loot-core/src/platform/server/fetch/index.electron.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
import * as fs from 'fs';

import fetch from 'node-fetch';

export { fetch };

export const fetchBinary = async function (url, filepath) {
const res = await fetch(url);
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(filepath);
res.body.pipe(fileStream);
res.body.on('error', err => {
reject(err);
});
fileStream.on('finish', function () {
resolve(undefined);
});
});
};
export { default as fetch } from 'node-fetch';
4 changes: 0 additions & 4 deletions packages/loot-core/src/platform/server/fetch/index.testing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export const fetch = function () {
throw new Error('fetch not implemented');
};

export const fetchBinary = function () {
throw new Error('fetchBinary not implemented');
};
4 changes: 0 additions & 4 deletions packages/loot-core/src/platform/server/fetch/index.web.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export const fetch = self.fetch;

export const fetchBinary = function () {
throw new Error('fetchBinary not implemented');
};
5 changes: 5 additions & 0 deletions packages/loot-core/src/server/cloud-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,15 @@ export async function download(fileId) {
},
});
} catch (err) {
console.log('Error fetching file info', err);
throw FileDownloadError('internal', { fileId });
}

if (res.status !== 'ok') {
console.log(
'Could not download file from the server. Are you sure you have the right file ID?',
res,
);
throw FileDownloadError('internal', { fileId });
}
let fileData = res.data;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1397.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [j-f1]
---

Update the API’s `node-fetch` dependency to fix a bug where connections could unexpectedly fail
53 changes: 52 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ __metadata:
dependencies:
"@types/uuid": ^9.0.2
better-sqlite3: ^8.2.0
node-fetch: ^2.6.9
node-fetch: ^3.3.2
typescript: ^5.0.2
uuid: ^9.0.0
languageName: unknown
Expand Down Expand Up @@ -7558,6 +7558,13 @@ __metadata:
languageName: node
linkType: hard

"data-uri-to-buffer@npm:^4.0.0":
version: 4.0.1
resolution: "data-uri-to-buffer@npm:4.0.1"
checksum: 0d0790b67ffec5302f204c2ccca4494f70b4e2d940fea3d36b09f0bb2b8539c2e86690429eb1f1dc4bcc9e4df0644193073e63d9ee48ac9fce79ec1506e4aa4c
languageName: node
linkType: hard

"data-urls@npm:^2.0.0":
version: 2.0.0
resolution: "data-urls@npm:2.0.0"
Expand Down Expand Up @@ -9395,6 +9402,16 @@ __metadata:
languageName: node
linkType: hard

"fetch-blob@npm:^3.1.2, fetch-blob@npm:^3.1.4":
version: 3.2.0
resolution: "fetch-blob@npm:3.2.0"
dependencies:
node-domexception: ^1.0.0
web-streams-polyfill: ^3.0.3
checksum: f19bc28a2a0b9626e69fd7cf3a05798706db7f6c7548da657cbf5026a570945f5eeaedff52007ea35c8bcd3d237c58a20bf1543bc568ab2422411d762dd3d5bf
languageName: node
linkType: hard

"file-entry-cache@npm:^6.0.1":
version: 6.0.1
resolution: "file-entry-cache@npm:6.0.1"
Expand Down Expand Up @@ -9628,6 +9645,15 @@ __metadata:
languageName: node
linkType: hard

"formdata-polyfill@npm:^4.0.10":
version: 4.0.10
resolution: "formdata-polyfill@npm:4.0.10"
dependencies:
fetch-blob: ^3.1.2
checksum: 82a34df292afadd82b43d4a740ce387bc08541e0a534358425193017bf9fb3567875dc5f69564984b1da979979b70703aa73dee715a17b6c229752ae736dd9db
languageName: node
linkType: hard

"forwarded@npm:0.2.0":
version: 0.2.0
resolution: "forwarded@npm:0.2.0"
Expand Down Expand Up @@ -13253,6 +13279,13 @@ __metadata:
languageName: node
linkType: hard

"node-domexception@npm:^1.0.0":
version: 1.0.0
resolution: "node-domexception@npm:1.0.0"
checksum: ee1d37dd2a4eb26a8a92cd6b64dfc29caec72bff5e1ed9aba80c294f57a31ba4895a60fd48347cf17dd6e766da0ae87d75657dfd1f384ebfa60462c2283f5c7f
languageName: node
linkType: hard

"node-fetch@npm:^1.0.1":
version: 1.7.3
resolution: "node-fetch@npm:1.7.3"
Expand All @@ -13277,6 +13310,17 @@ __metadata:
languageName: node
linkType: hard

"node-fetch@npm:^3.3.2":
version: 3.3.2
resolution: "node-fetch@npm:3.3.2"
dependencies:
data-uri-to-buffer: ^4.0.0
fetch-blob: ^3.1.4
formdata-polyfill: ^4.0.10
checksum: 06a04095a2ddf05b0830a0d5302699704d59bda3102894ea64c7b9d4c865ecdff2d90fd042df7f5bc40337266961cb6183dcc808ea4f3000d024f422b462da92
languageName: node
linkType: hard

"node-forge@npm:^1":
version: 1.3.1
resolution: "node-forge@npm:1.3.1"
Expand Down Expand Up @@ -18816,6 +18860,13 @@ __metadata:
languageName: node
linkType: hard

"web-streams-polyfill@npm:^3.0.3":
version: 3.2.1
resolution: "web-streams-polyfill@npm:3.2.1"
checksum: b119c78574b6d65935e35098c2afdcd752b84268e18746606af149e3c424e15621b6f1ff0b42b2676dc012fc4f0d313f964b41a4b5031e525faa03997457da02
languageName: node
linkType: hard

"webidl-conversions@npm:^3.0.0":
version: 3.0.1
resolution: "webidl-conversions@npm:3.0.1"
Expand Down