feat: Use end-of-stream to know when to release lock

This commit is contained in:
Joachim Van Herwegen 2021-02-10 15:44:41 +01:00
parent 230303e7d7
commit 0ffd332828
3 changed files with 14 additions and 5 deletions

8
package-lock.json generated
View File

@ -1180,6 +1180,14 @@
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.9.tgz", "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.9.tgz",
"integrity": "sha512-zurD1ibz21BRlAOIKP8yhrxlqKx6L9VCwkB5kMiP6nZAhoF5MvC7qS1qPA7nRcr1GJolfkQC7/EAL4hdYejLtg==" "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": { "@types/express": {
"version": "4.17.8", "version": "4.17.8",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.8.tgz", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.8.tgz",

View File

@ -79,6 +79,7 @@
"@types/arrayify-stream": "^1.0.0", "@types/arrayify-stream": "^1.0.0",
"@types/async-lock": "^1.1.2", "@types/async-lock": "^1.1.2",
"@types/cors": "^2.8.9", "@types/cors": "^2.8.9",
"@types/end-of-stream": "^1.4.0",
"@types/express": "^4.17.6", "@types/express": "^4.17.6",
"@types/mime-types": "^2.1.0", "@types/mime-types": "^2.1.0",
"@types/n3": "^1.4.4", "@types/n3": "^1.4.4",
@ -94,6 +95,7 @@
"async-lock": "^1.2.4", "async-lock": "^1.2.4",
"componentsjs": "^4.0.1", "componentsjs": "^4.0.1",
"cors": "^2.8.5", "cors": "^2.8.5",
"end-of-stream": "^1.4.4",
"escape-string-regexp": "^4.0.0", "escape-string-regexp": "^4.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"fetch-sparql-endpoint": "^1.8.0", "fetch-sparql-endpoint": "^1.8.0",

View File

@ -1,4 +1,6 @@
import type { Readable } from 'stream'; import type { Readable } from 'stream';
import { promisify } from 'util';
import eos from 'end-of-stream';
import type { Patch } from '../ldp/http/Patch'; import type { Patch } from '../ldp/http/Patch';
import { BasicRepresentation } from '../ldp/representation/BasicRepresentation'; import { BasicRepresentation } from '../ldp/representation/BasicRepresentation';
import type { Representation } from '../ldp/representation/Representation'; 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 { AtomicResourceStore } from './AtomicResourceStore';
import type { Conditions } from './Conditions'; import type { Conditions } from './Conditions';
import type { ResourceStore } from './ResourceStore'; import type { ResourceStore } from './ResourceStore';
const endOfStream = promisify(eos);
/** /**
* Store that for every call acquires a lock before executing it on the requested resource, * 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<void> { protected async waitForStreamToEnd(source: Readable): Promise<void> {
try { try {
await new Promise((resolve, reject): void => { await endOfStream(source);
source.on('error', reject);
source.on('end', resolve);
source.on('close', resolve);
});
} catch { } catch {
// Destroy the stream in case of errors // Destroy the stream in case of errors
if (!source.destroyed) { if (!source.destroyed) {