fix: make import() return an awaitable promise for the store - #636
Open
jeswr wants to merge 3 commits into
Open
Conversation
BREAKING CHANGE: the stream returned by Store#import() is now thenable; an async function that returns it awaits stream completion and resolves to the store instead of the stream. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
jeswr
commented
Aug 24, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Store#import() to be RDF/JS Dataset.import(stream): Promise<Dataset>-compatible by making the returned value awaitable (thenable), while still allowing existing Sink-style usage that treats the return value as a stream/EventEmitter.
Changes:
- Wrap the returned import stream in a
Proxythat exposesthen/catch/finallyto await completion and resolve to the store instance. - Lazily create the completion promise only when
then/catch/finallyis accessed to avoid eager listeners and unhandled rejections for non-awaiting callers. - Add tests covering awaiting behavior, repeated promise access, already-ended/destroyed inputs, and error/unhandled-rejection scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/N3Store.js |
Makes import() return a thenable stream wrapper that can be awaited to get the store on completion. |
test/N3Store-test.js |
Adds a dedicated test suite validating promise-like behavior and preserving EventEmitter usage expectations. |
Suppressed comments (1)
src/N3Store.js:516
- The Proxy wrapper does not preserve method
thisbinding. For example,imported.on('end', ...)will call the stream'sonwiththis === proxy, so listener state is stored on the proxy rather than the underlying stream; events emitted bystream(the target) won't reach listeners registered on the proxy. To truly “behave as the stream”, thegettrap should return functions bound totarget(or otherwise ensure calls execute withthis === target), ideally with caching so repeated gets return the same bound function.
}
return target[property];
},
});
}
// ### `removeQuad` removes a quad from the store if it exists
removeQuad(subject, predicate, object, graph) {
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+501
to
+504
| else { | ||
| stream.once('end', () => resolve(store)); | ||
| stream.once('error', reject); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes
Store#import()awaitable: the returned stream is wrapped in a proxy that forwardsthen/catch/finallyto a completion promise resolving to the store itself, satisfying the RDF/JSDataset.import(stream): Promise<Dataset>signature while keeping the existingSink.import(stream): EventEmitterbehaviour intact for callers that never await.Supersedes #503, reworked per the review notes there:
asyncfunction thatreturns the result ofimport()— it now awaits stream completion and resolves to the store instead of the stream. Labelledsemver.majoraccordingly.end/errorlisteners are only created upon first access ofthen/catch/finally. Non-awaiting Sink-style callers observe zero change: no listeners are added and an unlistenederrorevent still throws loudly (an eagerly attachederrorlistener — even guarded with a no-opcatch— was verified to silently swallow such errors instead), and no rejection ever exists to go unhandled.this, which is allPromise<Store>requires; the returned wrapper otherwise behaves as the stream.stream.erroredbefore the ended/destroyed/non-readable checks, so a stream that has already errored (e.g. viadestroy(error)) rejects instead of resolving as if it had ended.Refs #298 #435 #503
cc @jeswr