@envelop/parser-cache 4.4.0-alpha-aca44e1.0 → 4.4.0-alpha-385b86b.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,20 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const core = require('@envelop/core');
6
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useParserCache = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const graphql_1 = require("graphql");
6
+ const tiny_lru_1 = tslib_1.__importDefault(require("tiny-lru"));
7
+ const DEFAULT_MAX = 1000;
8
+ const DEFAULT_TTL = 3600000;
7
9
  const useParserCache = (pluginOptions = {}) => {
8
- const documentCache = pluginOptions.documentCache != null ? pluginOptions.documentCache : core.getInMemoryLRUCache();
9
- const errorCache = pluginOptions.errorCache != null ? pluginOptions.errorCache : core.getInMemoryLRUCache();
10
+ const documentCache = typeof pluginOptions.documentCache !== 'undefined'
11
+ ? pluginOptions.documentCache
12
+ : (0, tiny_lru_1.default)(DEFAULT_MAX, DEFAULT_TTL);
13
+ const errorCache = typeof pluginOptions.errorCache !== 'undefined' ? pluginOptions.errorCache : (0, tiny_lru_1.default)(DEFAULT_MAX, DEFAULT_TTL);
10
14
  return {
11
15
  onParse({ params, setParsedDocument }) {
12
- var _a;
13
16
  const { source } = params;
14
- const key = core.isSourceObject(source) ? source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString();
17
+ const key = source instanceof graphql_1.Source ? source.body : source;
15
18
  const cachedError = errorCache.get(key);
16
19
  if (cachedError !== undefined) {
17
20
  throw cachedError;
@@ -19,18 +22,16 @@ const useParserCache = (pluginOptions = {}) => {
19
22
  const cachedDocument = documentCache.get(key);
20
23
  if (cachedDocument !== undefined) {
21
24
  setParsedDocument(cachedDocument);
22
- return;
23
25
  }
24
26
  return ({ result }) => {
25
27
  if (result instanceof Error) {
26
28
  errorCache.set(key, result);
27
29
  }
28
- else if (result != null) {
30
+ else if (result !== null) {
29
31
  documentCache.set(key, result);
30
32
  }
31
33
  };
32
34
  },
33
35
  };
34
36
  };
35
-
36
37
  exports.useParserCache = useParserCache;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -1,13 +1,16 @@
1
- import { getInMemoryLRUCache, isSourceObject } from '@envelop/core';
2
-
3
- const useParserCache = (pluginOptions = {}) => {
4
- const documentCache = pluginOptions.documentCache != null ? pluginOptions.documentCache : getInMemoryLRUCache();
5
- const errorCache = pluginOptions.errorCache != null ? pluginOptions.errorCache : getInMemoryLRUCache();
1
+ import { Source } from 'graphql';
2
+ import lru from 'tiny-lru';
3
+ const DEFAULT_MAX = 1000;
4
+ const DEFAULT_TTL = 3600000;
5
+ export const useParserCache = (pluginOptions = {}) => {
6
+ const documentCache = typeof pluginOptions.documentCache !== 'undefined'
7
+ ? pluginOptions.documentCache
8
+ : lru(DEFAULT_MAX, DEFAULT_TTL);
9
+ const errorCache = typeof pluginOptions.errorCache !== 'undefined' ? pluginOptions.errorCache : lru(DEFAULT_MAX, DEFAULT_TTL);
6
10
  return {
7
11
  onParse({ params, setParsedDocument }) {
8
- var _a;
9
12
  const { source } = params;
10
- const key = isSourceObject(source) ? source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString();
13
+ const key = source instanceof Source ? source.body : source;
11
14
  const cachedError = errorCache.get(key);
12
15
  if (cachedError !== undefined) {
13
16
  throw cachedError;
@@ -15,18 +18,15 @@ const useParserCache = (pluginOptions = {}) => {
15
18
  const cachedDocument = documentCache.get(key);
16
19
  if (cachedDocument !== undefined) {
17
20
  setParsedDocument(cachedDocument);
18
- return;
19
21
  }
20
22
  return ({ result }) => {
21
23
  if (result instanceof Error) {
22
24
  errorCache.set(key, result);
23
25
  }
24
- else if (result != null) {
26
+ else if (result !== null) {
25
27
  documentCache.set(key, result);
26
28
  }
27
29
  };
28
30
  },
29
31
  };
30
32
  };
31
-
32
- export { useParserCache };
package/package.json CHANGED
@@ -1,33 +1,57 @@
1
1
  {
2
2
  "name": "@envelop/parser-cache",
3
- "version": "4.4.0-alpha-aca44e1.0",
3
+ "version": "4.4.0-alpha-385b86b.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "2.4.0-alpha-aca44e1.0",
6
+ "@envelop/core": "2.4.0-alpha-385b86b.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
+ "dependencies": {
10
+ "tiny-lru": "7.0.6"
11
+ },
9
12
  "repository": {
10
13
  "type": "git",
11
- "url": "https://github.com/dotansimha/envelop.git",
14
+ "url": "https://github.com/n1ru4l/envelop.git",
12
15
  "directory": "packages/plugins/parser-cache"
13
16
  },
14
17
  "author": "Dotan Simha <dotansimha@gmail.com>",
15
18
  "license": "MIT",
16
- "main": "index.js",
17
- "module": "index.mjs",
18
- "typings": "index.d.ts",
19
+ "main": "cjs/index.js",
20
+ "module": "esm/index.js",
21
+ "typings": "typings/index.d.ts",
19
22
  "typescript": {
20
- "definition": "index.d.ts"
23
+ "definition": "typings/index.d.ts"
21
24
  },
25
+ "type": "module",
22
26
  "exports": {
23
27
  ".": {
24
- "require": "./index.js",
25
- "import": "./index.mjs"
28
+ "require": {
29
+ "types": "./typings/index.d.ts",
30
+ "default": "./cjs/index.js"
31
+ },
32
+ "import": {
33
+ "types": "./typings/index.d.ts",
34
+ "default": "./esm/index.js"
35
+ },
36
+ "default": {
37
+ "types": "./typings/index.d.ts",
38
+ "default": "./esm/index.js"
39
+ }
26
40
  },
27
41
  "./*": {
28
- "require": "./*.js",
29
- "import": "./*.mjs"
42
+ "require": {
43
+ "types": "./typings/*.d.ts",
44
+ "default": "./cjs/*.js"
45
+ },
46
+ "import": {
47
+ "types": "./typings/*.d.ts",
48
+ "default": "./esm/*.js"
49
+ },
50
+ "default": {
51
+ "types": "./typings/*.d.ts",
52
+ "default": "./esm/*.js"
53
+ }
30
54
  },
31
55
  "./package.json": "./package.json"
32
56
  }
33
- }
57
+ }
@@ -0,0 +1,14 @@
1
+ import { Plugin } from '@envelop/core';
2
+ import { DocumentNode } from 'graphql';
3
+ interface Cache<T> {
4
+ get(key: string): T | undefined;
5
+ set(key: string, value: T): void;
6
+ }
7
+ export declare type DocumentCache = Cache<DocumentNode>;
8
+ export declare type ErrorCache = Cache<Error>;
9
+ export declare type ParserCacheOptions = {
10
+ documentCache?: DocumentCache;
11
+ errorCache?: ErrorCache;
12
+ };
13
+ export declare const useParserCache: (pluginOptions?: ParserCacheOptions) => Plugin;
14
+ export {};
package/README.md DELETED
@@ -1,37 +0,0 @@
1
- ## `@envelop/parser-cache`
2
-
3
- This plugins adds simple LRU caching to your `parse`, to improve performance by caching the parsed result.
4
-
5
- This plugins improves performance of parsing by ~60% (based on benchmarks).
6
-
7
- ## Getting Started
8
-
9
- ```
10
- yarn add @envelop/parser-cache
11
- ```
12
-
13
- ## Usage Example
14
-
15
- ```ts
16
- import { envelop } from '@envelop/core';
17
- import { useParserCache } from '@envelop/parser-cache';
18
-
19
- const getEnveloped = envelop({
20
- plugins: [
21
- // ... other plugins ...
22
- useParserCache({
23
- // options goes here
24
- }),
25
- ],
26
- });
27
- ```
28
-
29
- ### API Reference
30
-
31
- #### `documentCache`
32
-
33
- Set this to pass in a cache instance for caching documents. By default a new LRU cache is created using default `max` and `ttl`.
34
-
35
- #### `errorCache`
36
-
37
- Set this to pass in a cache instance for caching errors. By default a new LRU cache is created using default `max` and `ttl`.
package/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import { Plugin, BasicCache } from '@envelop/core';
2
- import { DocumentNode } from 'graphql';
3
- export declare type DocumentCache = BasicCache<DocumentNode>;
4
- export declare type ErrorCache = BasicCache<Error>;
5
- export declare type ParserCacheOptions = {
6
- documentCache?: DocumentCache;
7
- errorCache?: ErrorCache;
8
- };
9
- export declare const useParserCache: (pluginOptions?: ParserCacheOptions) => Plugin;