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

Pass options to createReadStream #179

Merged
merged 24 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
03d8e24
Upgrade fs-capacitor to 6.0.0
mike-marcacci Jan 5, 2020
bde7a83
update engine to >=10.18
mike-marcacci Jan 5, 2020
c36dc27
Allow specification of encoding, highWaterMark in createReadStream
mike-marcacci Jan 5, 2020
566107f
Add changelog entry
mike-marcacci Jan 5, 2020
5a345b4
Merge branch 'master' into feature/highWaterMark
jaydenseric Jan 15, 2020
59493cf
Changelog entry tweaks.
jaydenseric Jan 15, 2020
a8239af
Add issue and PR links to the changelog entry.
jaydenseric Jan 15, 2020
eeeaf5f
JSDOC FileUploadCreateReadStream typedef WIP.
jaydenseric Jan 15, 2020
ff4732d
Document createReadStream options
mike-marcacci Jan 20, 2020
87591c2
test createReadStream options
mike-marcacci Jan 20, 2020
736b109
switch encodings
mike-marcacci Jan 20, 2020
3208b03
reference fs-capacitor docs
mike-marcacci Jan 20, 2020
512fe4a
copy descriptions to jsdoc
mike-marcacci Jan 20, 2020
52dea7f
Lint fixes.
jaydenseric Jan 26, 2020
bd053e9
Merge branch 'master' into feature/highWaterMark
jaydenseric Jan 26, 2020
6f367ba
Fix tests.
jaydenseric Jan 26, 2020
79a42b0
Fix an incorrect assertion.
jaydenseric Jan 27, 2020
a62c4b6
FileUploadCreateReadStream typedef documentation fixes.
jaydenseric Jan 27, 2020
f2dd44d
Use more variables in a test.
jaydenseric Jan 27, 2020
889220b
Add another test assertion.
jaydenseric Jan 27, 2020
29e5699
Test createReadStream default options.
jaydenseric Jan 27, 2020
91dbd3e
Better document createReadStream options.
jaydenseric Jan 27, 2020
1d79991
Use an API that works for Node.js < v12.7.0.
jaydenseric Jan 27, 2020
1c037a1
Remove a redundant test.
jaydenseric Jan 27, 2020
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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
### Major

- Updated Node.js support from v8.10+ to v10+, as earlier versions have reached end-of-life.
- Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v6, which now requires Node.js v10+, via [#179](https://github.com/jaydenseric/graphql-upload/pull/179).
- Updated dev dependencies, some of which now require Node.js v10+.
- Replaced the [`tap`](https://npm.im/tap) dev dependency with [`test-director`](https://npm.im/test-director), [`coverage-node`](https://npm.im/coverage-node), and [`hard-rejection`](https://npm.im/hard-rejection) to improve the dev experience and reduce the dev install size by ~75.7 MB. These new dev dependencies require Node.js v10+.
- Reorganized files. This is only a breaking change for projects using undocumented deep imports.
- Removed now redundant Node.js version compatibility logic in the `processRequest` function.
- The `processRequest` function now places references to instances of the now exported and documented `Upload` class in the GraphQL operation for the `GraphQLUpload` scalar to derive its value, and the `GraphQLUpload` scalar now throws a `GraphQLError` when it parses an invalid value, fixing [#175](https://github.com/jaydenseric/graphql-upload/issues/175) via [#181](https://github.com/jaydenseric/graphql-upload/pull/181).
- The `GraphQLUpload` scalar `parseLiteral` and `serialize` methods now throw `GraphQLError` (instead of `Error`) instances, with tweaked messages.

### Minor

- The `createReadStream` function in resolved file uploads now accepts options to configure the encoding and high water mark, fixing [#177](https://github.com/jaydenseric/graphql-upload/issues/177) via [#179](https://github.com/jaydenseric/graphql-upload/pull/179).

### Patch

- Removed the now redundant [`eslint-plugin-import-order-alphabetical`](https://npm.im/eslint-plugin-import-order-alphabetical) and [`express-async-handler`](https://npm.im/express-async-handler) dev dependencies.
Expand Down
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ exports.Upload = require('./Upload')
* @prop {string} filename File name.
* @prop {string} mimetype File MIME type. Provided by the client and can’t be trusted.
* @prop {string} encoding File stream transfer encoding.
* @prop {Function} createReadStream Returns a Node.js readable stream of the file contents, for processing and storing the file. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request.
* @prop {FileUploadCreateReadStream} createReadStream Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents, for processing and storage.
*/

/**
* Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of an [uploading file’s]{@link FileUpload} contents, for processing and storage. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request.
* @kind typedef
* @name FileUploadCreateReadStream
* @type {Function}
* @param {object} [options] [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions).
* @param {string} [options.encoding=null] Specify an encoding for the [`data`](https://nodejs.org/api/stream.html#stream_event_data) chunks to be strings (without splitting multi-byte characters across chunks) instead of Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) instances. Supported values depend on the [`Buffer` implementation](https://github.com/nodejs/node/blob/v13.7.0/lib/buffer.js#L587-L663) and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`, or `hex`.
* @param {number} [options.highWaterMark=16384] Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
* @returns {Readable} [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.
* @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
* @see [Node.js stream backpressure guide](https://nodejs.org/es/docs/guides/backpressuring-in-streams).
*/

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/processRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ module.exports = function processRequest(
filename,
mimetype,
encoding,
createReadStream() {
createReadStream(options) {
const error = fileError || (released ? exitError : null)
if (error) throw error
return capacitor.createReadStream()
return capacitor.createReadStream(options)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"busboy": "^0.3.1",
"fs-capacitor": "^4.0.1",
"fs-capacitor": "^6.0.0",
"http-errors": "^1.7.3",
"isobject": "^4.0.0",
"object-path": "^0.11.4"
Expand Down
24 changes: 23 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-mult
- [function graphqlUploadKoa](#function-graphqluploadkoa)
- [function processRequest](#function-processrequest)
- [type FileUpload](#type-fileupload)
- [type FileUploadCreateReadStream](#type-fileuploadcreatereadstream)
- [type GraphQLOperation](#type-graphqloperation)
- [type ProcessRequestFunction](#type-processrequestfunction)
- [type ProcessRequestOptions](#type-processrequestoptions)
Expand Down Expand Up @@ -263,7 +264,28 @@ File upload details that are only available after the file’s field in the [Gra
| `filename` | string | File name. |
| `mimetype` | string | File MIME type. Provided by the client and can’t be trusted. |
| `encoding` | string | File stream transfer encoding. |
| `createReadStream` | Function | Returns a Node.js readable stream of the file contents, for processing and storing the file. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request. |
| `createReadStream` | [FileUploadCreateReadStream](#type-fileuploadcreatereadstream) | Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents, for processing and storage. |

---

### type FileUploadCreateReadStream

Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of an [uploading file’s](#type-fileupload) contents, for processing and storage. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request.

**Type:** Function

| Parameter | Type | Description |
| :-- | :-- | :-- |
| `options` | object? | [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions). |
| `options.encoding` | string? = `null` | Specify an encoding for the [`data`](https://nodejs.org/api/stream.html#stream_event_data) chunks to be strings (without splitting multi-byte characters across chunks) instead of Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) instances. Supported values depend on the [`Buffer` implementation](https://github.com/nodejs/node/blob/v13.7.0/lib/buffer.js#L587-L663) and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`, or `hex`. |
| `options.highWaterMark` | number? = `16384` | Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. |

**Returns:** Readable — [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.

#### See

- [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
- [Node.js stream backpressure guide](https://nodejs.org/es/docs/guides/backpressuring-in-streams).

---

Expand Down
114 changes: 86 additions & 28 deletions test/lib/processRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,106 @@ const listen = require('../listen')
const streamToString = require('../streamToString')

module.exports = tests => {
tests.add('`processRequest` with a single file.', async () => {
let serverError
tests.add(
'`processRequest` with a single file and default `createReadStream` options.',
async () => {
let serverError

const server = http.createServer(async (request, response) => {
try {
const operation = await processRequest(request, response)
const server = http.createServer(async (request, response) => {
try {
const operation = await processRequest(request, response)

ok(operation.variables.file instanceof Upload)
ok(operation.variables.file instanceof Upload)

const upload = await operation.variables.file.promise
const upload = await operation.variables.file.promise

strictEqual(upload.filename, 'a.txt')
strictEqual(upload.mimetype, 'text/plain')
strictEqual(upload.encoding, '7bit')
strictEqual(upload.filename, 'a.txt')
strictEqual(upload.mimetype, 'text/plain')
strictEqual(upload.encoding, '7bit')

const stream = upload.createReadStream()
const stream = upload.createReadStream()

ok(stream instanceof ReadStream)
strictEqual(await streamToString(stream), 'a')
} catch (error) {
serverError = error
ok(stream instanceof ReadStream)
strictEqual(stream._readableState.encoding, null)
strictEqual(stream.readableHighWaterMark, 16384)
strictEqual(await streamToString(stream), 'a')
} catch (error) {
serverError = error
} finally {
response.end()
}
})

const { port, close } = await listen(server)

try {
const body = new FormData()

body.append('operations', JSON.stringify({ variables: { file: null } }))
body.append('map', JSON.stringify({ '1': ['variables.file'] }))
body.append('1', 'a', { filename: 'a.txt' })

await fetch(`http://localhost:${port}`, { method: 'POST', body })

if (serverError) throw serverError
} finally {
response.end()
close()
}
})
}
)

const { port, close } = await listen(server)
tests.add(
'`processRequest` with a single file and custom `createReadStream` options.',
async () => {
let serverError

try {
const body = new FormData()
const server = http.createServer(async (request, response) => {
try {
const operation = await processRequest(request, response)

body.append('operations', JSON.stringify({ variables: { file: null } }))
body.append('map', JSON.stringify({ '1': ['variables.file'] }))
body.append('1', 'a', { filename: 'a.txt' })
ok(operation.variables.file instanceof Upload)

await fetch(`http://localhost:${port}`, { method: 'POST', body })
const upload = await operation.variables.file.promise

if (serverError) throw serverError
} finally {
close()
strictEqual(upload.filename, 'a.txt')
strictEqual(upload.mimetype, 'text/plain')
strictEqual(upload.encoding, '7bit')

const encoding = 'base64'
const highWaterMark = 100
const stream = upload.createReadStream({ encoding, highWaterMark })

ok(stream instanceof ReadStream)
strictEqual(stream._readableState.encoding, encoding)
strictEqual(stream.readableHighWaterMark, highWaterMark)
strictEqual(
await streamToString(stream),
Buffer.from('a').toString(encoding)
)
} catch (error) {
serverError = error
} finally {
response.end()
}
})

const { port, close } = await listen(server)

try {
const body = new FormData()

body.append('operations', JSON.stringify({ variables: { file: null } }))
body.append('map', JSON.stringify({ '1': ['variables.file'] }))
body.append('1', 'a', { filename: 'a.txt' })

await fetch(`http://localhost:${port}`, { method: 'POST', body })

if (serverError) throw serverError
} finally {
close()
}
}
})
)

tests.add('`processRequest` with a single file, batched.', async () => {
let serverError
Expand Down