Skip to content

Commit

Permalink
Support Node.js ESM.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenseric committed May 25, 2020
1 parent fa758f4 commit cab7737
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 23 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Major

- Updated Node.js support to `^10.13.0 || ^12.0.0 || >= 13.7.0`.
- Added a [package `exports` field](https://nodejs.org/api/esm.html#esm_package_entry_points) with [conditional exports](https://nodejs.org/api/esm.html#esm_conditional_exports) to support native ESM in Node.js and keep internal code private, [whilst avoiding the dual package hazard](https://nodejs.org/api/esm.html#esm_approach_1_use_an_es_module_wrapper). Published files have been reorganized, so previously undocumented deep imports will need to be rewritten according to the newly documented paths.
- Updated dev dependencies, some of which require newer Node.js versions than previously supported.

### Patch
Expand Down
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@
"server",
"koa",
"express",
"apollo"
"apollo",
"esm",
"mjs"
],
"files": [
"lib"
"private",
"public"
],
"main": "lib",
"main": "public",
"exports": {
".": {
"import": "./public/index.mjs",
"require": "./public/index.js"
},
"./public/": "./public/",
"./package": "./package.json",
"./package.json": "./package.json"
},
"engines": {
"node": "^10.13.0 || ^12.0.0 || >= 13.7.0"
},
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions lib/GraphQLUpload.js → public/GraphQLUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ const Upload = require('./Upload');
* [file upload details]{@link FileUpload} for processing and storage.
* @kind class
* @name GraphQLUpload
* @example <caption>Ways to `import`.</caption>
* ```js
* import { GraphQLUpload } from 'graphql-upload';
* ```
*
* ```js
* import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
* ```
* @example <caption>Ways to `require`.</caption>
* ```js
* const { GraphQLUpload } = require('graphql-upload');
* ```
*
* ```js
* const GraphQLUpload = require('graphql-upload/public/GraphQLUpload');
* ```
* @example <caption>Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema).</caption>
* ```js
* const { makeExecutableSchema } = require('graphql-tools');
Expand Down
16 changes: 16 additions & 0 deletions lib/Upload.js → public/Upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
* [`promise`]{@link Upload#promise} property.
* @kind class
* @name Upload
* @example <caption>Ways to `import`.</caption>
* ```js
* import { Upload } from 'graphql-upload';
* ```
*
* ```js
* import Upload from 'graphql-upload/public/Upload.js';
* ```
* @example <caption>Ways to `require`.</caption>
* ```js
* const { Upload } = require('graphql-upload');
* ```
*
* ```js
* const Upload = require('graphql-upload/public/Upload');
* ```
*/
module.exports = class Upload {
constructor() {
Expand Down
16 changes: 16 additions & 0 deletions lib/graphqlUploadExpress.js → public/graphqlUploadExpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ const defaultProcessRequest = require('./processRequest');
* @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
* @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
* @returns {Function} Express middleware.
* @example <caption>Ways to `import`.</caption>
* ```js
* import { graphqlUploadExpress } from 'graphql-upload';
* ```
*
* ```js
* import graphqlUploadExpress from 'graphql-upload/public/graphqlUploadExpress.js';
* ```
* @example <caption>Ways to `require`.</caption>
* ```js
* const { graphqlUploadExpress } = require('graphql-upload');
* ```
*
* ```js
* const graphqlUploadExpress = require('graphql-upload/public/graphqlUploadExpress');
* ```
* @example <caption>Basic [`express-graphql`](https://npm.im/express-graphql) setup.</caption>
* ```js
* const express = require('express');
Expand Down
16 changes: 16 additions & 0 deletions lib/graphqlUploadKoa.js → public/graphqlUploadKoa.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ const defaultProcessRequest = require('./processRequest');
* @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
* @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
* @returns {Function} Koa middleware.
* @example <caption>Ways to `import`.</caption>
* ```js
* import { graphqlUploadKoa } from 'graphql-upload';
* ```
*
* ```js
* import graphqlUploadKoa from 'graphql-upload/public/graphqlUploadKoa.js';
* ```
* @example <caption>Ways to `require`.</caption>
* ```js
* const { graphqlUploadKoa } = require('graphql-upload');
* ```
*
* ```js
* const graphqlUploadKoa = require('graphql-upload/public/graphqlUploadKoa');
* ```
* @example <caption>Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup.</caption>
* ```js
* const Koa = require('koa');
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions public/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default as GraphQLUpload } from './GraphQLUpload.js';
export { default as processRequest } from './processRequest.js';
export { default as graphqlUploadKoa } from './graphqlUploadKoa.js';
export { default as graphqlUploadExpress } from './graphqlUploadExpress.js';
export { default as Upload } from './Upload.js';
18 changes: 15 additions & 3 deletions lib/processRequest.js → public/processRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const { WriteStream } = require('fs-capacitor');
const createError = require('http-errors');
const isObject = require('isobject');
const objectPath = require('object-path');
const { SPEC_URL } = require('../private/constants');
const ignoreStream = require('../private/ignoreStream');
const Upload = require('./Upload');
const { SPEC_URL } = require('./constants');
const ignoreStream = require('./ignoreStream');

/**
* Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
Expand All @@ -24,10 +24,22 @@ const ignoreStream = require('./ignoreStream');
* @kind function
* @name processRequest
* @type {ProcessRequestFunction}
* @example <caption>How to import.</caption>
* @example <caption>Ways to `import`.</caption>
* ```js
* import { processRequest } from 'graphql-upload';
* ```
*
* ```js
* import processRequest from 'graphql-upload/public/processRequest.js';
* ```
* @example <caption>Ways to `require`.</caption>
* ```js
* const { processRequest } = require('graphql-upload');
* ```
*
* ```js
* const processRequest = require('graphql-upload/public/processRequest');
* ```
*/
module.exports = function processRequest(
request,
Expand Down
98 changes: 97 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ A GraphQL `Upload` scalar that can be used in a [`GraphQLSchema`](https://graphq

#### Examples

_Ways to `import`._

> ```js
> import { GraphQLUpload } from 'graphql-upload';
> ```
>
> ```js
> import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
> ```
_Ways to `require`._
> ```js
> const { GraphQLUpload } = require('graphql-upload');
> ```
>
> ```js
> const GraphQLUpload = require('graphql-upload/public/GraphQLUpload');
> ```
_Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema)._
> ```js
Expand Down Expand Up @@ -143,6 +163,28 @@ _A manually constructed schema with an image upload mutation._
A file expected to be uploaded as it has been declared in the `map` field of a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). The [`processRequest`](#function-processrequest) function places references to an instance of this class wherever the file is expected in the [GraphQL operation](#type-graphqloperation). The [`Upload` scalar](#class-graphqlupload) derives it’s value from the [`promise`](#upload-instance-property-promise) property.
#### Examples
_Ways to `import`._
> ```js
> import { Upload } from 'graphql-upload';
> ```
>
> ```js
> import Upload from 'graphql-upload/public/Upload.js';
> ```
_Ways to `require`._
> ```js
> const { Upload } = require('graphql-upload');
> ```
>
> ```js
> const Upload = require('graphql-upload/public/Upload');
> ```
#### Upload instance method reject
Rejects the upload promise with an error. This should only be utilized by [`processRequest`](#function-processrequest).
Expand Down Expand Up @@ -186,6 +228,26 @@ Creates [Express](https://expressjs.com) middleware that processes [GraphQL mult
#### Examples
_Ways to `import`._
> ```js
> import { graphqlUploadExpress } from 'graphql-upload';
> ```
>
> ```js
> import graphqlUploadExpress from 'graphql-upload/public/graphqlUploadExpress.js';
> ```
_Ways to `require`._
> ```js
> const { graphqlUploadExpress } = require('graphql-upload');
> ```
>
> ```js
> const graphqlUploadExpress = require('graphql-upload/public/graphqlUploadExpress');
> ```
_Basic [`express-graphql`](https://npm.im/express-graphql) setup._
> ```js
Expand Down Expand Up @@ -218,6 +280,26 @@ Creates [Koa](https://koajs.com) middleware that processes [GraphQL multipart re
#### Examples
_Ways to `import`._
> ```js
> import { graphqlUploadKoa } from 'graphql-upload';
> ```
>
> ```js
> import graphqlUploadKoa from 'graphql-upload/public/graphqlUploadKoa.js';
> ```
_Ways to `require`._
> ```js
> const { graphqlUploadKoa } = require('graphql-upload');
> ```
>
> ```js
> const graphqlUploadKoa = require('graphql-upload/public/graphqlUploadKoa');
> ```
_Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup._
> ```js
Expand Down Expand Up @@ -245,11 +327,25 @@ Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-m
#### Examples
_How to import._
_Ways to `import`._
> ```js
> import { processRequest } from 'graphql-upload';
> ```
>
> ```js
> import processRequest from 'graphql-upload/public/processRequest.js';
> ```
_Ways to `require`._
> ```js
> const { processRequest } = require('graphql-upload');
> ```
>
> ```js
> const processRequest = require('graphql-upload/public/processRequest');
> ```
---
Expand Down
12 changes: 6 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const { TestDirector } = require('test-director');

const tests = new TestDirector();

require('./lib/GraphQLUpload.test')(tests);
require('./lib/graphqlUploadExpress.test')(tests);
require('./lib/graphqlUploadKoa.test')(tests);
require('./lib/ignoreStream.test')(tests);
require('./lib/processRequest.test')(tests);
require('./lib/Upload.test')(tests);
require('./private/ignoreStream.test')(tests);
require('./public/GraphQLUpload.test')(tests);
require('./public/graphqlUploadExpress.test')(tests);
require('./public/graphqlUploadKoa.test')(tests);
require('./public/processRequest.test')(tests);
require('./public/Upload.test')(tests);

tests.run();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { doesNotThrow, strictEqual } = require('assert');
const ignoreStream = require('../../lib/ignoreStream');
const ignoreStream = require('../../private/ignoreStream');
const CountReadableStream = require('../CountReadableStream');

module.exports = (tests) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const { doesNotThrow, throws } = require('assert');
const { parseValue } = require('graphql');
const GraphQLUpload = require('../../lib/GraphQLUpload');
const Upload = require('../../lib/Upload');
const GraphQLUpload = require('../../public/GraphQLUpload');
const Upload = require('../../public/Upload');

module.exports = (tests) => {
tests.add('`GraphQLUpload` scalar `parseValue` with a valid value.', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/Upload.test.js → test/public/Upload.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { ok, rejects, strictEqual } = require('assert');
const Upload = require('../../lib/Upload');
const Upload = require('../../public/Upload');

module.exports = (tests) => {
tests.add('`Upload` class resolving a file.', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const express = require('express');
const FormData = require('form-data');
const createError = require('http-errors');
const fetch = require('node-fetch');
const graphqlUploadExpress = require('../../lib/graphqlUploadExpress');
const processRequest = require('../../lib/processRequest');
const graphqlUploadExpress = require('../../public/graphqlUploadExpress');
const processRequest = require('../../public/processRequest');
const listen = require('../listen');

module.exports = (tests) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { deepStrictEqual, ok, strictEqual } = require('assert');
const FormData = require('form-data');
const Koa = require('koa');
const fetch = require('node-fetch');
const graphqlUploadKoa = require('../../lib/graphqlUploadKoa');
const processRequest = require('../../lib/processRequest');
const graphqlUploadKoa = require('../../public/graphqlUploadKoa');
const processRequest = require('../../public/processRequest');
const listen = require('../listen');

module.exports = (tests) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const http = require('http');
const FormData = require('form-data');
const { ReadStream } = require('fs-capacitor');
const fetch = require('node-fetch');
const Upload = require('../../lib/Upload');
const processRequest = require('../../lib/processRequest');
const Upload = require('../../public/Upload');
const processRequest = require('../../public/processRequest');
const abortingMultipartRequest = require('../abortingMultipartRequest');
const listen = require('../listen');
const streamToString = require('../streamToString');
Expand Down

0 comments on commit cab7737

Please sign in to comment.