Skip to content

Commit

Permalink
refactor(core-server-graphql): Complete redesign of ServerContext p…
Browse files Browse the repository at this point in the history
…rocessing and extension
  • Loading branch information
sullivanpj committed Sep 24, 2023
1 parent 3429c7f commit 6b85c0d
Show file tree
Hide file tree
Showing 107 changed files with 6,664 additions and 6,085 deletions.
42 changes: 42 additions & 0 deletions .github/actions/node/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Setup Node"
description: "This action install node and cache modules. It uses pnpm as package manager."
inputs:
node-version:
description: "The node version to install (Default: lts)"
default: "lts/*"
required: false
working-directory:
description: "The working directory of your node package"
default: "."
required: false

runs:
using: "composite"
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ inputs.node-version }}

- uses: pnpm/action-setup@v2.2.4
id: pnpm-install
with:
version: 8.6.10
run_install: false

- name: Get pnpm store directory
shell: bash
id: pnpm-cache
run: |
echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-8-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-8-store-
- name: setup pnpm config registry
run: pnpm config set registry https://registry.npmjs.org
shell: bash
5 changes: 5 additions & 0 deletions apps/workers/contact-api/.dev.vars
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ DEV_EDITOR_ID="vscode"
DEV_REPO_ROOT="C:\\Development\\open-system"

NODE_ENV="development"
ENVIRONMENT="development"

DOMAIN_NAME="contact"
SERVICE_NAME="Contact API"
SERVICE_ID="contact-api"
27 changes: 14 additions & 13 deletions apps/workers/contact-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ A Cloudflare Worker to be developed in the Open System monorepo.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

## Table of Contents

- [Build](#build)
- [Development server](#development-server)
- [Build](#build)
- [Development server](#development-server)
- [Environment Configuration Help](#environment-configuration-help)
- [Deploy an application](#deploy-an-application)
- [Running unit tests](#running-unit-tests)
Expand Down Expand Up @@ -99,17 +100,17 @@ nx graph

See the [open issues](https://github.com/sullivanpj/open-system/issues) for a list of proposed features (and known issues).

- [Top Feature Requests](https://github.com/sullivanpj/open-system/issues?q=label%3Aenhancement+is%3Aopen+sort%3Areactions-%2B1-desc) (Add your votes using the 👍 reaction)
- [Top Bugs](https://github.com/sullivanpj/open-system/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Areactions-%2B1-desc) (Add your votes using the 👍 reaction)
- [Newest Bugs](https://github.com/sullivanpj/open-system/issues?q=is%3Aopen+is%3Aissue+label%3Abug)
- [Top Feature Requests](https://github.com/sullivanpj/open-system/issues?q=label%3Aenhancement+is%3Aopen+sort%3Areactions-%2B1-desc) (Add your votes using the 👍 reaction)
- [Top Bugs](https://github.com/sullivanpj/open-system/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Areactions-%2B1-desc) (Add your votes using the 👍 reaction)
- [Newest Bugs](https://github.com/sullivanpj/open-system/issues?q=is%3Aopen+is%3Aissue+label%3Abug)

## Support

Reach out to the maintainer at one of the following places:

- [Contact](https://www.patsullivan.org/contact)
- [GitHub discussions](https://github.com/sullivanpj/open-system/discussions)
- <contact@patsullivan.org>
- [Contact](https://www.patsullivan.org/contact)
- [GitHub discussions](https://github.com/sullivanpj/open-system/discussions)
- <contact@patsullivan.org>

## License

Expand All @@ -127,10 +128,10 @@ First off, thanks for taking the time to contribute! Contributions are what make

Please try to create bug reports that are:

- _Reproducible._ Include steps to reproduce the problem.
- _Specific._ Include as much detail as possible: which version, what environment, etc.
- _Unique._ Do not duplicate existing opened issues.
- _Scoped to a Single Bug._ One bug per report.
- _Reproducible._ Include steps to reproduce the problem.
- _Specific._ Include as much detail as possible: which version, what environment, etc.
- _Unique._ Do not duplicate existing opened issues.
- _Scoped to a Single Bug._ One bug per report.

Please adhere to this project's [code of conduct](.github/CODE_OF_CONDUCT.md).

Expand Down Expand Up @@ -167,7 +168,7 @@ This project follows the [all-contributors](https://github.com/all-contributors/
<br />
<br />
<div align="center">
<img src="https://pub-e71cff0f90204755bc910518d63cacf8.r2.dev/logo-opengraph.png" width="100%"/>
<img src="https://pub-e71cff0f90204755bc910518d63cacf8.r2.dev/logo-opengraph.gif" width="100%"/>
</div>
<div align="center">
<a href="https://www.patsullivan.org" target="_blank">Website</a> | <a href="https://www.patsullivan.org/contact" target="_blank">Contact</a> | <a href="https://linkedin.com/in/patrick-sullivan-865526b0" target="_blank">LinkedIn</a> | <a href="https://medium.com/@pat.joseph.sullivan" target="_blank">Medium</a> | <a href="https://github.com/sullivanpj" target="_blank">GitHub</a> | <a href="https://keybase.io/sullivanp" target="_blank">OpenPGP Key</a>
Expand Down
3 changes: 2 additions & 1 deletion apps/workers/contact-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"version": "0.0.1",
"dependencies": {
"@cloudflare/workers-types": "^4.20230904.0",
"@whatwg-node/node-fetch": "^0.4.18",
"async_hooks": "^1.0.0",
"buffer": "^6.0.3",
"esbuild-plugin-polyfill-node": "^0.3.0",
"fs": "0.0.1-security",
"http": "0.0.1-security",
"process": "^0.11.10",
"reflect-metadata": "^0.1.13",
"wrangler": "^3.2.0",
"wrangler": "^3.9.0",
"zlib": "^1.0.5"
}
}
9 changes: 7 additions & 2 deletions apps/workers/contact-api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"output": "."
}
],
"skipTypeCheck": true,
"polyfillNode": true,
"skipTypeCheck": true
"platform": "node"
},
"configurations": {
"development": {
Expand Down Expand Up @@ -113,5 +114,9 @@
}
}
},
"tags": []
"tags": [],
"implicitDependencies": [
"core-server-cloudflare",
"contact-server-attachment"
]
}
68 changes: 55 additions & 13 deletions apps/workers/contact-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,74 @@
import "reflect-metadata";
import "./dependencies";

import { extendContactServerContext } from "@open-system/contact-server-attachment";
import { schema } from "@open-system/contact-server-attachment/api";
import { handleCloudflareGraphQLRequest } from "@open-system/core-server-cloudflare/server/handler";
import { isPonyfillBody } from "@open-system/core-server-utilities/type-checks";
import { ConsoleLogger } from "@open-system/core-shared-logging/console/console-logger";
import { formatErrorLog } from "@open-system/core-shared-logging/format/format-log";
import { isError } from "@open-system/core-shared-utilities/common/type-checks";
import { createServer } from "./server";
import {
HttpHeaderTypes,
HttpMediaTypes,
HttpMethod
} from "@open-system/core-shared-utilities/types";

export interface Env {
DB: any;
}

globalThis.Buffer = Buffer;
globalThis.TransformStream = TransformStream;
globalThis.ReadableStream = ReadableStream;
globalThis.WritableStream = WritableStream;

/*addEventListener("unhandledrejection", (event) => {
ConsoleLogger.error(globalContextStore.getStore(), "unhandled rejection!");
ConsoleLogger.error(event);
});*/

export default {
async fetch(request: Request, env: Env) {
async fetch(request: Request, env: Env): Promise<Response> {
try {
ConsoleLogger.debug("Server bindings", env);
const response = await handleCloudflareGraphQLRequest(
request,
{
schema,
serviceProvidersOptions: {
services: []
},
extendContextOptions: {
plugin: extendContactServerContext
}
},
env
);

/*let { readable, writable } = new TransformStream();
response.body.pipeTo(writable);*/

const server = await createServer();
const headers: HeadersInit = response.headers;
let contentType!: string;

const result = await server.fetch(request as any, env as any);
if (isError(result)) {
ConsoleLogger.error(result);
return new Response(formatErrorLog(result), {
status: 500,
...(result as Error)
});
if (isPonyfillBody(response)) {
contentType = response.contentType;
headers.set(HttpHeaderTypes.CONTENT_TYPE, response.contentType);
} else if (headers.has(HttpHeaderTypes.CONTENT_TYPE)) {
contentType = headers.get(HttpHeaderTypes.CONTENT_TYPE)!;
}

return result;
return new Response(
contentType === HttpMediaTypes.TEXT ||
contentType === HttpMediaTypes.HTML ||
request.method === HttpMethod.GET
? await response.text()
: await response.json(),
{
status: response.status,
statusText: response.statusText,
headers
}
);
} catch (e) {
ConsoleLogger.fatal(e);
return new Response(formatErrorLog(e as Error), {
Expand Down
86 changes: 0 additions & 86 deletions apps/workers/contact-api/src/server/handler.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/workers/contact-api/src/server/index.ts

This file was deleted.

27 changes: 0 additions & 27 deletions apps/workers/contact-api/src/server/server.ts

This file was deleted.

7 changes: 6 additions & 1 deletion apps/workers/contact-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
"moduleResolution": "node",
"sourceMap": true,
"esModuleInterop": true,
"types": ["node", "@cloudflare/workers-types", "reflect-metadata"]
"types": [
"node",
"@cloudflare/workers-types",
"@whatwg-node/node-fetch",
"reflect-metadata"
]
},
"exclude": [
"node_modules",
Expand Down
6 changes: 3 additions & 3 deletions apps/workers/contact-api/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ workers_dev = true
usage_model = "bundled"

compatibility_date = "2023-07-10"
# compatibility_flags = [ "nodejs_compat" ]
node_compat = true
compatibility_flags = [ "nodejs_compat" ]
# node_compat = true

# rules = [
# { type = "ESModule", globs = ["@whatwg-node/fetch"] }
Expand All @@ -23,7 +23,7 @@ account_id = "03700f13b61493bbc558dc93ea4980bc"
[build]
command = "pnpm exec nx run worker-contact-api:build:development"
cwd = "C:\\Development\\open-system"
watch_dir = "C:\\Development\\open-system\\apps\\worker-contact-api"
watch_dir = "./"

[[d1_databases]]
binding = "DB"
Expand Down
3 changes: 3 additions & 0 deletions assets/open-system.logo-green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/open-system.opengraph-green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6b85c0d

Please sign in to comment.