diff --git a/package-lock.json b/package-lock.json index b43fd1b2c..1ceccd0f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1180,6 +1180,14 @@ "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.9.tgz", "integrity": "sha512-zurD1ibz21BRlAOIKP8yhrxlqKx6L9VCwkB5kMiP6nZAhoF5MvC7qS1qPA7nRcr1GJolfkQC7/EAL4hdYejLtg==" }, + "@types/end-of-stream": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/@types/end-of-stream/-/end-of-stream-1.4.0.tgz", + "integrity": "sha512-d0FD2A4vpFI8wyQeQbr9VDVKtA1PmeGO3Ntn+6j626QTtAQ9HSqWFACP7rTHaV2cspVhLijl00Vvkf/U2UZGWA==", + "requires": { + "@types/node": "*" + } + }, "@types/express": { "version": "4.17.8", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.8.tgz", diff --git a/package.json b/package.json index 4fbbe8a60..0bdd0290e 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "@types/arrayify-stream": "^1.0.0", "@types/async-lock": "^1.1.2", "@types/cors": "^2.8.9", + "@types/end-of-stream": "^1.4.0", "@types/express": "^4.17.6", "@types/mime-types": "^2.1.0", "@types/n3": "^1.4.4", @@ -94,6 +95,7 @@ "async-lock": "^1.2.4", "componentsjs": "^4.0.1", "cors": "^2.8.5", + "end-of-stream": "^1.4.4", "escape-string-regexp": "^4.0.0", "express": "^4.17.1", "fetch-sparql-endpoint": "^1.8.0", diff --git a/src/storage/LockingResourceStore.ts b/src/storage/LockingResourceStore.ts index 0971c0916..2dadaf61c 100644 --- a/src/storage/LockingResourceStore.ts +++ b/src/storage/LockingResourceStore.ts @@ -1,4 +1,6 @@ import type { Readable } from 'stream'; +import { promisify } from 'util'; +import eos from 'end-of-stream'; import type { Patch } from '../ldp/http/Patch'; import { BasicRepresentation } from '../ldp/representation/BasicRepresentation'; import type { Representation } from '../ldp/representation/Representation'; @@ -9,6 +11,7 @@ import type { ExpiringReadWriteLocker } from '../util/locking/ExpiringReadWriteL import type { AtomicResourceStore } from './AtomicResourceStore'; import type { Conditions } from './Conditions'; import type { ResourceStore } from './ResourceStore'; +const endOfStream = promisify(eos); /** * Store that for every call acquires a lock before executing it on the requested resource, @@ -121,11 +124,7 @@ export class LockingResourceStore implements AtomicResourceStore { */ protected async waitForStreamToEnd(source: Readable): Promise { try { - await new Promise((resolve, reject): void => { - source.on('error', reject); - source.on('end', resolve); - source.on('close', resolve); - }); + await endOfStream(source); } catch { // Destroy the stream in case of errors if (!source.destroyed) {