-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: remove concat-stream dependency and modernize MemoryStorage #1356
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
Open
Phillip9587
wants to merge
1
commit into
expressjs:main
Choose a base branch
from
Phillip9587:memory-stream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,91 @@ | ||
var concat = require('concat-stream') | ||
const Writable = require('stream').Writable | ||
const Buffer = require('buffer').Buffer | ||
const isUint8Array = require('util').types.isUint8Array | ||
|
||
function MemoryStorage (opts) {} | ||
class MemoryStorage { | ||
_handleFile (req, file, cb) { | ||
file.stream.pipe( | ||
new ConcatStream(function (data) { | ||
cb(null, { | ||
buffer: data, | ||
size: data.length | ||
}) | ||
}) | ||
) | ||
} | ||
|
||
MemoryStorage.prototype._handleFile = function _handleFile (req, file, cb) { | ||
file.stream.pipe(concat({ encoding: 'buffer' }, function (data) { | ||
cb(null, { | ||
buffer: data, | ||
size: data.length | ||
_removeFile (req, file, cb) { | ||
delete file.buffer | ||
cb(null) | ||
} | ||
} | ||
|
||
module.exports = function () { | ||
return new MemoryStorage() | ||
} | ||
|
||
/** | ||
* Writable stream that concatenates all written chunks into a single Buffer. | ||
* | ||
* Implementation inspired by the concat-stream npm package (https://www.npmjs.com/package/concat-stream), | ||
* modified for our specific use case. | ||
*/ | ||
class ConcatStream extends Writable { | ||
/** | ||
* Creates a new ConcatStream instance. | ||
* @param {function(Buffer): void} cb - Callback invoked with the concatenated Buffer when the stream finishes. | ||
*/ | ||
constructor (cb) { | ||
super() | ||
this.body = [] | ||
this.on('finish', function () { | ||
cb(this.getBody()) | ||
}) | ||
})) | ||
} | ||
|
||
_write (chunk, enc, next) { | ||
this.body.push(chunk) | ||
next() | ||
} | ||
|
||
/** | ||
* Concatenates all collected chunks into a single Buffer. | ||
* @returns {Buffer} The concatenated buffer containing all written data. | ||
*/ | ||
getBody () { | ||
const bufs = [] | ||
for (const p of this.body) { | ||
// Buffer.concat can handle Buffer and Uint8Array (and subclasses) | ||
if (Buffer.isBuffer(p) || isUint8Array(p)) { | ||
bufs.push(p) | ||
} else if (isBufferish(p)) { | ||
bufs.push(Buffer.from(p)) | ||
} else { | ||
bufs.push(Buffer.from(String(p))) | ||
} | ||
} | ||
return Buffer.concat(bufs) | ||
} | ||
} | ||
|
||
MemoryStorage.prototype._removeFile = function _removeFile (req, file, cb) { | ||
delete file.buffer | ||
cb(null) | ||
/** | ||
* Checks if the given value is array-like. | ||
* @param {*} arr | ||
* @returns {boolean} | ||
*/ | ||
function isArrayish (arr) { | ||
return /Array\]$/.test(Object.prototype.toString.call(arr)) | ||
} | ||
|
||
module.exports = function (opts) { | ||
return new MemoryStorage(opts) | ||
/** | ||
* Checks if the given value is Buffer-like. | ||
* @param {*} p | ||
* @returns {boolean} | ||
*/ | ||
function isBufferish (p) { | ||
return ( | ||
typeof p === 'string' || | ||
isArrayish(p) || | ||
(p && typeof p.subarray === 'function') | ||
) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callback is invoked with
this.getBody()
butthis
context may not refer to the ConcatStream instance inside the 'finish' event handler. Use an arrow function or bind the context to ensurethis
refers to the ConcatStream instance.Copilot uses AI. Check for mistakes.