@contember/engine-http 1.0.0-rc.9 → 1.1.0-alpha.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-http",
3
- "version": "1.0.0-rc.9",
3
+ "version": "1.1.0-alpha.2",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/src/index.js",
6
6
  "typings": "dist/src/index.d.ts",
@@ -9,14 +9,14 @@
9
9
  },
10
10
  "dependencies": {
11
11
  "@apollographql/graphql-playground-html": "^1.6.24",
12
- "@contember/database": "^1.0.0-rc.9",
13
- "@contember/engine-content-api": "^1.0.0-rc.9",
14
- "@contember/engine-plugins": "^1.0.0-rc.9",
15
- "@contember/engine-system-api": "^1.0.0-rc.9",
16
- "@contember/engine-tenant-api": "^1.0.0-rc.9",
17
- "@contember/graphql-utils": "^1.0.0-rc.9",
18
- "@contember/schema": "^1.0.0-rc.9",
19
- "@contember/schema-utils": "^1.0.0-rc.9",
12
+ "@contember/database": "^1.1.0-alpha.2",
13
+ "@contember/engine-content-api": "^1.1.0-alpha.2",
14
+ "@contember/engine-plugins": "^1.1.0-alpha.2",
15
+ "@contember/engine-system-api": "^1.1.0-alpha.2",
16
+ "@contember/engine-tenant-api": "^1.1.0-alpha.2",
17
+ "@contember/graphql-utils": "^1.1.0-alpha.2",
18
+ "@contember/schema": "^1.1.0-alpha.2",
19
+ "@contember/schema-utils": "^1.1.0-alpha.2",
20
20
  "@graphql-tools/merge": "^6.2.14",
21
21
  "@graphql-tools/schema": "^7.1.5",
22
22
  "@koa/cors": "^3.1.0",
@@ -6,7 +6,7 @@ import {
6
6
  Identity,
7
7
  IntrospectionSchemaDefinitionFactory,
8
8
  PermissionsByIdentityFactory,
9
- StaticAuthorizator,
9
+ Authorizator,
10
10
  IntrospectionSchemaFactory,
11
11
  } from '@contember/engine-content-api'
12
12
  import { mergeSchemas } from '@graphql-tools/merge'
@@ -50,7 +50,7 @@ export class GraphQlSchemaFactory {
50
50
  }
51
51
  const { permissions, verifier } = this.permissionFactory.createPermissions(schema, identity)
52
52
 
53
- const authorizator = new StaticAuthorizator(permissions)
53
+ const authorizator = new Authorizator(permissions)
54
54
  const dataSchemaBuilder = this.graphqlSchemaBuilderFactory.create(schema.model, authorizator)
55
55
  const contentSchemaFactory = new IntrospectionSchemaDefinitionFactory(
56
56
  new IntrospectionSchemaFactory(
@@ -1,6 +1,7 @@
1
1
  import { DocumentNode, execute, GraphQLError, GraphQLSchema, parse, validate, validateSchema } from 'graphql'
2
2
  import { KoaContext } from '../koa'
3
3
  import LRUCache from 'lru-cache'
4
+ import { createHash } from 'crypto'
4
5
 
5
6
  export interface GraphQLListener<Context> {
6
7
  onStart?: (ctx: {}) => Omit<GraphQLListener<Context>, 'onStart'> | void
@@ -25,16 +26,36 @@ interface FactoryArgs<Context, KoaState> {
25
26
 
26
27
  export type GraphQLQueryHandler<KoaState> = (context: KoaContext<KoaState>) => any
27
28
 
29
+ const hitCacheMaxAgeSeconds = 10 * 60
30
+ const documentCacheMaxAgeSeconds = hitCacheMaxAgeSeconds * 10
31
+ const pruneIntervalSeconds = documentCacheMaxAgeSeconds / 2
32
+ const documentCacheMax = 100
33
+ const hitCacheMax = documentCacheMax * 2
34
+
28
35
  export const createGraphQLQueryHandler = <Context, KoaState>({
29
36
  schema,
30
37
  contextFactory,
31
38
  listeners,
32
39
  }: FactoryArgs<Context, KoaState>): GraphQLQueryHandler<KoaState> => {
33
40
  let schemaValidated = false
41
+ const hitCache = new LRUCache<string, true>({
42
+ max: hitCacheMax,
43
+ maxAge: hitCacheMaxAgeSeconds * 1000,
44
+ })
34
45
  const documentCache = new LRUCache<string, DocumentNode>({
35
- max: 200,
46
+ max: documentCacheMax,
47
+ maxAge: documentCacheMaxAgeSeconds * 1000,
36
48
  })
49
+ let lastPrune = Date.now()
50
+
37
51
  return async ctx => {
52
+
53
+ const now = Date.now()
54
+ if ((now - lastPrune) > pruneIntervalSeconds * 1000) {
55
+ documentCache.prune()
56
+ lastPrune = now
57
+ }
58
+
38
59
  const listenersQueue = [...listeners]
39
60
 
40
61
  listenersQueue.forEach(it => {
@@ -66,7 +87,8 @@ export const createGraphQLQueryHandler = <Context, KoaState>({
66
87
  errors: { message: 'Missing request query' },
67
88
  })
68
89
  }
69
- let doc = documentCache.get(request.query)
90
+ const queryHash = createHash('md5').update(request.query).digest('hex')
91
+ let doc = documentCache.get(queryHash)
70
92
  if (!doc) {
71
93
  try {
72
94
  doc = parse(request.query)
@@ -74,7 +96,10 @@ export const createGraphQLQueryHandler = <Context, KoaState>({
74
96
  if (validationResult.length) {
75
97
  return respond(400, { errors: validationResult })
76
98
  }
77
- documentCache.set(request.query, doc)
99
+ if (hitCache.get(queryHash)) {
100
+ documentCache.set(queryHash, doc)
101
+ }
102
+ hitCache.set(queryHash, true)
78
103
  } catch (e) {
79
104
  if (e instanceof GraphQLError) {
80
105
  return respond(400, { errors: [e] })