Skip to content

Commit

Permalink
Bugfix: add duplex. Refactoring imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed May 23, 2023
1 parent 2d45515 commit ef3305c
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 38 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 19
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
22 changes: 22 additions & 0 deletions @types/dom/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright © 2023 Exact Realty Limited. All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

export {};

declare global {
interface RequestInit {
duplex?: 'full' | 'half';
}
}
24 changes: 24 additions & 0 deletions SECURITY
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Security Policy
===============

We value your contributions and feedback, and we want to ensure the security and
reliability of our software for all users. As part of our commitment to
security, we have established this security policy to help you report any
vulnerabilities that you may discover.

If you believe you have found a security vulnerability in our software, please
let us know immediately. You may find our contact information at
<https://exact.realty/.well-known/security.txt>. Please include the following
information in your report:

* A brief description of the vulnerability
* Steps to reproduce the vulnerability
* The version of our software affected by the vulnerability
* Your contact information (email or other means of communication)

We kindly ask that you do not disclose the vulnerability to anyone else until we
have had the opportunity to investigate and address it. We will respond to your
report as soon as possible and work with you to resolve the issue. We may also
provide credit to you in our release notes or other public acknowledgments.

Thank you for helping to keep our software secure!
2 changes: 1 addition & 1 deletion esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const buildOptionsBase = {
minify: true,
entryNames: '[name]',
platform: 'node',
external: ['esbuild'],
external: ['firebase-functions'],
};

const formats = ['cjs', 'esm'];
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exact-realty/routemate",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"dist": "dist/index.js",
"main": "dist/index.js",
Expand All @@ -17,7 +17,7 @@
},
"./cloudflare-workers": {
"import": "./dist/cloudflare-workers.mjs",
"require": "./dist/cloudflare-workers.mjs"
"re5quire": "./dist/cloudflare-workers.mjs"
}
},
"files": [
Expand All @@ -44,6 +44,14 @@
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"peerDependencies": {
"firebase-functions": "^4.2.1"
},
"peerDependenciesMeta": {
"firebase-functions": {
"optional": true
}
},
"scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
Expand Down
2 changes: 1 addition & 1 deletion src/ResponseError/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { TErrorHandler } from '../types';
import { TErrorHandler } from '../types/index.js';
import errorResponseText from './errorResponseText.json';

class ResponseError_ extends Error {
Expand Down
2 changes: 1 addition & 1 deletion src/dotWellKnownHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export { default as setupDotWellKnownTime } from './time';
export { default as setupDotWellKnownTime } from './time.js';
2 changes: 1 addition & 1 deletion src/dotWellKnownHandlers/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { TRouter } from '../types';
import { TRouter } from '../types/index.js';

const setupDotWellKnownTime_ = (router: TRouter) =>
router.route(
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export * as dotWellKnownHandlers from './dotWellKnownHandlers';
export * as listeners from './listeners';
export * as dotWellKnownHandlers from './dotWellKnownHandlers/index.js';
export * as listeners from './listeners/index.js';
export {
default as ResponseError_,
handleResponseError as handleResponseError,
Expand Down
2 changes: 1 addition & 1 deletion src/listeners/aws-lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import type { TListener } from '../../types';
import type { TListener } from '../../types/index.js';

const createServer_: TListener = (r) => async () => {
void r;
Expand Down
2 changes: 1 addition & 1 deletion src/listeners/azure-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import type { TListener } from '../../types';
import type { TListener } from '../../types/index.js';

const createServer_: TListener = (r) => async () => {
void r;
Expand Down
21 changes: 18 additions & 3 deletions src/listeners/cloudflare-workers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

/// <reference types="@cloudflare/workers-types" />
// /// <reference types="@cloudflare/workers-types" />
import type {
EventListenerOrEventListenerObject,
EventTargetAddEventListenerOptions,
FetchEvent,
Response as CfResponse,
WorkerGlobalScopeEventMap,
} from '@cloudflare/workers-types';

import type { TListener } from '../../types';
import type { TListener } from '../../types/index.js';

declare function addEventListener<Type extends keyof WorkerGlobalScopeEventMap>(
type: Type,
handler: EventListenerOrEventListenerObject<
WorkerGlobalScopeEventMap[Type]
>,
options?: EventTargetAddEventListenerOptions | boolean,
): void;

const createServer_: TListener =
(r) =>
Expand Down Expand Up @@ -45,7 +60,7 @@ const createServer_: TListener =
);

try {
event.respondWith(response);
event.respondWith(response as unknown as Promise<CfResponse>);
} catch (e) {
void e;
// log error
Expand Down
4 changes: 2 additions & 2 deletions src/listeners/deno/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/// <reference types="deno-types" />

import type { TListener } from '../../types';
import type { TListener } from '../../types/index.js';

const createServer_: TListener =
(r) =>
Expand Down Expand Up @@ -78,7 +78,7 @@ const createServer_: TListener =

(async () => {
for await (const conn of server) {
serveHttp(conn);
serveHttp(conn).catch(Boolean);
}
})();

Expand Down
16 changes: 9 additions & 7 deletions src/listeners/dynamic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

/// <reference types="@cloudflare/workers-types" />
/// <reference types="deno-types" />
// import type { CacheStorage } from '@cloudflare/workers-types';

declare const caches: unknown;
declare const WebSocketPair: unknown;

const dynamic_ = async () => {
if (typeof process !== 'undefined' && process.release?.name === 'node') {
Expand All @@ -35,25 +37,25 @@ const dynamic_ = async () => {
process.env?.['AWS_LAMBDA_FUNCTION_VERSION'] &&
process.env?.['LAMBDA_TASK_ROOT']
) {
return import('../aws-lambda');
return import('../aws-lambda/index.js');
}

if (
['Development', 'Staging', 'Production'].includes(
process.env?.['AZURE_FUNCTIONS_ENVIRONMENT'] ?? '',
)
) {
return import('../azure-functions');
return import('../azure-functions/index.js');
}

return import('../node');
return import('../node/index.js');
} else if (
typeof caches !== 'undefined' &&
typeof WebSocketPair !== 'undefined'
) {
return import('../cloudflare-workers');
return import('../cloudflare-workers/index.js');
} else if (typeof Deno !== 'undefined') {
return import('../deno');
return import('../deno/index.js');
} else {
throw new Error('Unsupported environment');
}
Expand Down
4 changes: 2 additions & 2 deletions src/listeners/gcp-cloudfunction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/

import { https } from 'firebase-functions';
import { TListener } from '../../types';
import { nodeHandler_ as nodeHandler } from '../node';
import { TListener } from '../../types/index.js';
import { nodeHandler_ as nodeHandler } from '../node/index.js';

const createServer_: TListener =
(r) =>
Expand Down
8 changes: 4 additions & 4 deletions src/listeners/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export { default as cloudflareWorkers } from './cloudflare-workers';
export { default as deno } from './deno';
export { default as dynamic } from './dynamic';
export { default as node, nodeHandler_ as nodeHandler } from './node';
export { default as cloudflareWorkers } from './cloudflare-workers/index.js';
export { default as deno } from './deno/index.js';
export { default as dynamic } from './dynamic/index.js';
export { default as node, nodeHandler_ as nodeHandler } from './node/index.js';
19 changes: 16 additions & 3 deletions src/listeners/node/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*/

import assert from 'node:assert/strict';
import nodeListener from '.';
import server, { Router } from '../../server';
import { handleResponseError as handleResponseError } from '../../ResponseError';
import nodeListener from './index.js';
import server, { Router } from '../../server.js';
import { handleResponseError as handleResponseError } from '../../ResponseError/index.js';

const abortController = new AbortController();
const port = (Math.random() * (1 << 15)) | 0 | (1 << 11);
Expand Down Expand Up @@ -64,6 +64,9 @@ before(async () => {
r.use('/eh/406', () => {
throw 406;
});
r.post('/post', async (req) => {
return new Response('body: ' + (await req.text()));
});
r['use:error']('/eh/406', handleResponseError);

await r.listen(port, host, abortController.signal);
Expand Down Expand Up @@ -126,4 +129,14 @@ describe('Node.js handler', () => {
);
});
});

it('Test 8: /post', async () => {
await fetch(`http://${host}:${port}/post`, {
method: 'POST',
body: 'hello, world!',
}).then(async (v) => {
assert.equal(v.status, 200);
assert.equal(await v.text(), 'body: hello, world!');
});
});
});
3 changes: 2 additions & 1 deletion src/listeners/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import type { IncomingMessage, ServerResponse } from 'node:http';
import { createServer } from 'node:http';
import { TListen, TListener, TRouter } from '../../types';
import { TListen, TListener, TRouter } from '../../types/index.js';

const nodeHandler_ =
(r: TRouter) => (req: IncomingMessage, res: ServerResponse) => {
Expand Down Expand Up @@ -44,6 +44,7 @@ const nodeHandler_ =
`http://${req.headers['host'] ?? 'default.local.'}${req.url}`,
).toString(),
{
duplex: 'half',
method: req.method,
headers: new Headers(
Array.from(
Expand Down
2 changes: 1 addition & 1 deletion src/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import assert from 'node:assert/strict';
import Router from './router';
import Router from './router.js';

describe('Router', () => {
it('No handler results in 501', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
TResponse,
TRoute,
TRouter,
} from './types';
} from './types/index.js';

const isTPHandlerFullSetupMPH = <T>(
args: unknown[],
Expand Down
4 changes: 2 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import Router from './router';
import type { TListener, TServer } from './types';
import Router from './router.js';
import type { TListener, TServer } from './types/index.js';

const server_ = (listener: TListener): TServer => {
const router = Router();
Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
"resolveJsonModule": true,
"allowJs": true,
"checkJs": true,
"lib": ["ES2016", "ES2019.object", "dom"]
"lib": ["ES2016", "ES2019.object", "dom"],
"typeRoots": ["./node_modules/@types", "./@types"]
},
"include": ["src/**/*", "types/**/*"],
"ts-node": {
"experimentalResolver": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}

0 comments on commit ef3305c

Please sign in to comment.