Skip to content

Commit

Permalink
Use ESM instead of CJS and update the fs-capacitor dependency.
Browse files Browse the repository at this point in the history
Fixes #318 . Closes #290 .
  • Loading branch information
jaydenseric committed Jul 23, 2022
1 parent 8914950 commit 07b8b80
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// @ts-check

"use strict";

/**
* [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
* URL. Useful for error messages, etc.
*/
const GRAPHQL_MULTIPART_REQUEST_SPEC_URL =
"https://github.com/jaydenseric/graphql-multipart-request-spec";

module.exports = GRAPHQL_MULTIPART_REQUEST_SPEC_URL;
export default GRAPHQL_MULTIPART_REQUEST_SPEC_URL;
17 changes: 8 additions & 9 deletions GraphQLUpload.js → GraphQLUpload.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @ts-check

"use strict";
import { GraphQLError, GraphQLScalarType } from "graphql";

const { GraphQLScalarType, GraphQLError } = require("graphql");
const Upload = require("./Upload.js");
import Upload from "./Upload.mjs";

/** @typedef {import("./processRequest").FileUpload} FileUpload */
/** @typedef {import("./processRequest.mjs").FileUpload} FileUpload */

/**
* A GraphQL `Upload` scalar that can be used in a
Expand All @@ -18,8 +17,8 @@ const Upload = require("./Upload.js");
* from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema):
*
* ```js
* const { makeExecutableSchema } = require("@graphql-tools/schema");
* const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
* import { makeExecutableSchema } from "@graphql-tools/schema/makeExecutableSchema";
* import GraphQLUpload from "graphql-upload/GraphQLUpload.mjs";
*
* const schema = makeExecutableSchema({
* typeDefs: `
Expand All @@ -34,8 +33,8 @@ const Upload = require("./Upload.js");
* A manually constructed schema with an image upload mutation:
*
* ```js
* const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require("graphql");
* const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
* import { GraphQLBoolean, GraphQLObjectType, GraphQLSchema } from "graphql";
* import GraphQLUpload from "graphql-upload/GraphQLUpload.mjs";
*
* const schema = new GraphQLSchema({
* mutation: new GraphQLObjectType({
Expand Down Expand Up @@ -77,4 +76,4 @@ const GraphQLUpload = new GraphQLScalarType({
},
});

module.exports = GraphQLUpload;
export default GraphQLUpload;
4 changes: 2 additions & 2 deletions GraphQLUpload.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { doesNotThrow, throws } from "assert";
import { parseValue } from "graphql";

import GraphQLUpload from "./GraphQLUpload.js";
import Upload from "./Upload.js";
import GraphQLUpload from "./GraphQLUpload.mjs";
import Upload from "./Upload.mjs";

/**
* Adds `GraphQLUpload` tests.
Expand Down
16 changes: 6 additions & 10 deletions Upload.js → Upload.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @ts-check

"use strict";

/** @typedef {import("./GraphQLUpload.js")} GraphQLUpload */
/** @typedef {import("./processRequest.js")} processRequest */
/** @typedef {import("./GraphQLUpload.mjs").default} GraphQLUpload */
/** @typedef {import("./processRequest.mjs").default} processRequest */

/**
* A file expected to be uploaded as it was declared in the `map` field of a
Expand All @@ -12,26 +10,26 @@
* this class wherever the file is expected in the GraphQL operation. The scalar
* {@linkcode GraphQLUpload} derives it’s value from {@linkcode Upload.promise}.
*/
class Upload {
export default class Upload {
constructor() {
/**
* Promise that resolves file upload details. This should only be utilized
* by {@linkcode GraphQLUpload}.
* @type {Promise<import("./processRequest.js").FileUpload>}
* @type {Promise<import("./processRequest.mjs").FileUpload>}
*/
this.promise = new Promise((resolve, reject) => {
/**
* Resolves the upload promise with the file upload details. This should
* only be utilized by {@linkcode processRequest}.
* @param {import("./processRequest.js").FileUpload} file File upload
* @param {import("./processRequest.mjs").FileUpload} file File upload
* details.
*/
this.resolve = (file) => {
/**
* The file upload details, available when the
* {@linkcode Upload.promise} resolves. This should only be utilized by
* {@linkcode processRequest}.
* @type {import("./processRequest.js").FileUpload | undefined}
* @type {import("./processRequest.mjs").FileUpload | undefined}
*/
this.file = file;

Expand All @@ -51,5 +49,3 @@ class Upload {
this.promise.catch(() => {});
}
}

module.exports = Upload;
2 changes: 1 addition & 1 deletion Upload.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { ok, rejects, strictEqual } from "assert";

import Upload from "./Upload.js";
import Upload from "./Upload.mjs";

/**
* Adds `Upload` tests.
Expand Down
32 changes: 32 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,41 @@

## Next

### Major

- Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v8, fixing [#318](https://github.com/jaydenseric/graphql-upload/issues/318).
- The type `FileUploadCreateReadStreamOptions` from the `processRequest.mjs` module now uses types from [`fs-capacitor`](https://npm.im/fs-capacitor) that are slightly more specific.
- The API is now ESM in `.mjs` files instead of CJS in `.js` files, [accessible via `import` but not `require`](https://nodejs.org/dist/latest/docs/api/esm.html#require). To migrate imports:

```diff
- import GraphQLUpload from "graphql-upload/GraphQLUpload.js";
+ import GraphQLUpload from "graphql-upload/GraphQLUpload.mjs";
```

```diff
- import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.js";
+ import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.mjs";
```

```diff
- import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.js";
+ import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.mjs";
```

```diff
- import processRequest from "graphql-upload/processRequest.js";
+ import processRequest from "graphql-upload/processRequest.mjs";
```

```diff
- import Upload from "graphql-upload/Upload.js";
+ import Upload from "graphql-upload/Upload.mjs";
```

### Patch

- Updated dev dependencies.
- Updated examples in JSDoc comments.
- Updated the changelog entry for v14.0.0 to show how to migrate imports.

## 15.0.2
Expand Down
23 changes: 10 additions & 13 deletions graphqlUploadExpress.js → graphqlUploadExpress.mjs
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
// @ts-check

"use strict";

const defaultProcessRequest = require("./processRequest.js");
import defaultProcessRequest from "./processRequest.mjs";

/**
* Creates [Express](https://expressjs.com) middleware that processes incoming
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
* the request `body` to be similar to a conventional GraphQL POST request for
* following GraphQL middleware to consume.
* @param {import("./processRequest.js").ProcessRequestOptions & {
* processRequest?: import("./processRequest.js").ProcessRequestFunction
* @param {import("./processRequest.mjs").ProcessRequestOptions & {
* processRequest?: import("./processRequest.mjs").ProcessRequestFunction
* }} options Options.
* @returns Express middleware.
* @example
* Basic [`express-graphql`](https://npm.im/express-graphql) setup:
*
* ```js
* const express = require("express");
* const graphqlHTTP = require("express-graphql");
* const graphqlUploadExpress = require("graphql-upload/graphqlUploadExpress.js");
* const schema = require("./schema.js");
* import express from "express";
* import expressGraphQL from "express-graphql";
* import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.mjs";
*
* import schema from "./schema.mjs";
*
* express()
* .use(
* "/graphql",
* graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
* graphqlHTTP({ schema })
* expressGraphQL.graphqlHTTP({ schema })
* )
* .listen(3000);
* ```
*/
function graphqlUploadExpress({
export default function graphqlUploadExpress({
processRequest = defaultProcessRequest,
...processRequestOptions
} = {}) {
Expand Down Expand Up @@ -76,5 +75,3 @@ function graphqlUploadExpress({

return graphqlUploadExpressMiddleware;
}

module.exports = graphqlUploadExpress;
8 changes: 4 additions & 4 deletions graphqlUploadExpress.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { createServer } from "http";
import createError from "http-errors";
import fetch, { File, FormData } from "node-fetch";

import graphqlUploadExpress from "./graphqlUploadExpress.js";
import processRequest from "./processRequest.js";
import graphqlUploadExpress from "./graphqlUploadExpress.mjs";
import processRequest from "./processRequest.mjs";
import listen from "./test/listen.mjs";

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ export default (tests) => {
/**
* @type {{
* variables: {
* file: import("./Upload.js"),
* file: import("./Upload.mjs").default,
* },
* } | undefined}
*/
Expand Down Expand Up @@ -84,7 +84,7 @@ export default (tests) => {
/**
* @type {{
* variables: {
* file: import("./Upload.js"),
* file: import("./Upload.mjs").default,
* },
* } | undefined}
*/
Expand Down
24 changes: 11 additions & 13 deletions graphqlUploadKoa.js → graphqlUploadKoa.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// @ts-check

"use strict";

const defaultProcessRequest = require("./processRequest.js");
import defaultProcessRequest from "./processRequest.mjs";

/**
* Creates [Koa](https://koajs.com) middleware that processes incoming
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
* the request `body` to be similar to a conventional GraphQL POST request for
* following GraphQL middleware to consume.
* @param {import("./processRequest.js").ProcessRequestOptions & {
* processRequest?: import("./processRequest.js").ProcessRequestFunction
* @param {import("./processRequest.mjs").ProcessRequestOptions & {
* processRequest?: import("./processRequest.mjs").ProcessRequestFunction
* }} options Options.
* @returns Koa middleware.
* @example
* Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup:
*
* ```js
* const Koa = require("koa");
* const bodyParser = require("koa-bodyparser");
* const { errorHandler, execute } = require("graphql-api-koa");
* const graphqlUploadKoa = require("graphql-upload/graphqlUploadKoa.js");
* const schema = require("./schema.js");
* import errorHandler from "graphql-api-koa/errorHandler.mjs";
* import execute from "graphql-api-koa/execute.mjs";
* import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.mjs";
* import Koa from "koa";
* import bodyParser from "koa-bodyparser";
*
* import schema from "./schema.mjs";
*
* new Koa()
* .use(errorHandler())
Expand All @@ -32,7 +32,7 @@ const defaultProcessRequest = require("./processRequest.js");
* .listen(3000);
* ```
*/
function graphqlUploadKoa({
export default function graphqlUploadKoa({
processRequest = defaultProcessRequest,
...processRequestOptions
} = {}) {
Expand Down Expand Up @@ -65,5 +65,3 @@ function graphqlUploadKoa({

return graphqlUploadKoaMiddleware;
}

module.exports = graphqlUploadKoa;
8 changes: 4 additions & 4 deletions graphqlUploadKoa.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { createServer } from "http";
import Koa from "koa";
import fetch, { File, FormData } from "node-fetch";

import graphqlUploadKoa from "./graphqlUploadKoa.js";
import processRequest from "./processRequest.js";
import graphqlUploadKoa from "./graphqlUploadKoa.mjs";
import processRequest from "./processRequest.mjs";
import listen from "./test/listen.mjs";

/**
Expand Down Expand Up @@ -40,7 +40,7 @@ export default (tests) => {
/**
* @type {{
* variables: {
* file: import("./Upload.js"),
* file: import("./Upload.mjs").default,
* },
* } | undefined}
*/
Expand Down Expand Up @@ -80,7 +80,7 @@ export default (tests) => {
/**
* @type {{
* variables: {
* file: import("./Upload.js"),
* file: import("./Upload.mjs").default,
* },
* } | undefined}
*/
Expand Down
6 changes: 1 addition & 5 deletions ignoreStream.js → ignoreStream.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// @ts-check

"use strict";

/**
* Safely ignores a Node.js readable stream.
* @param {import("stream").Readable} stream Node.js readable stream.
*/
function ignoreStream(stream) {
export default function ignoreStream(stream) {
// Prevent an unhandled error from crashing the process.
stream.on("error", () => {});

// Waste the stream.
stream.resume();
}

module.exports = ignoreStream;
2 changes: 1 addition & 1 deletion ignoreStream.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { doesNotThrow, strictEqual } from "assert";

import ignoreStream from "./ignoreStream.js";
import ignoreStream from "./ignoreStream.mjs";
import CountReadableStream from "./test/CountReadableStream.mjs";

/**
Expand Down
Loading

0 comments on commit 07b8b80

Please sign in to comment.