Skip to content

Commit

Permalink
fix: include type definitions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the module is now ESM
  • Loading branch information
wkillerud committed Jun 25, 2024
1 parent bde292a commit 3824adf
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 137 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage/
node_modules/
*.log
.vscode
types/
24 changes: 24 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import prettierConfig from 'eslint-config-prettier';
import prettierPlugin from 'eslint-plugin-prettier';
import globals from 'globals';
import js from '@eslint/js';

export default [
js.configs.recommended,
prettierConfig,
{
plugins: {
prettier: prettierPlugin,
},
languageOptions: {
globals: {
...globals.browser,
...globals.node,
global: true,
},
},
},
{
ignores: ['dist/*', 'coverage/*', 'example/*'],
},
];
23 changes: 18 additions & 5 deletions lib/http-request/request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
'use strict';
import { URL } from 'url';
import http from 'http';

const { URL } = require('url');
const http = require('http');
/**
* @typedef {object} RequestOptions
* @property {string} [pathname="/"]
* @property {string} [address=""]
* @property {Record<string, string>} [headers={}]
* @property {string} [method="GET"]
* @property {boolean} [json=false]
*/

const request = (
/**
* @template T
* @param {RequestOptions} options
* @param {T} payload
* @returns {Promise<{ headers: import('http').IncomingHttpHeaders, body: T}>}
*/
export const request = (
{
pathname = '/',
address = '',
Expand All @@ -19,6 +32,7 @@ const request = (
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
headers = Object.assign(headers, {
'Content-Type': 'application/x-www-form-urlencoded',
// @ts-expect-error It's fine
'Content-Length': Buffer.byteLength(payload),
});
}
Expand Down Expand Up @@ -60,4 +74,3 @@ const request = (

req.end();
});
module.exports = request;
26 changes: 13 additions & 13 deletions lib/http-server/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';
import enableDestroy from 'server-destroy';
import http from 'http';
import url from 'url';

const enableDestroy = require('server-destroy');
const http = require('http');
const url = require('url');
export class HttpServer {
address = '';
/** @type {import('http').RequestListener | undefined} */
request = undefined;
/** @type {import('http').Server | undefined} */
server = undefined;

class HttpServer {
constructor() {
this.address = '';
this.request = undefined;
this.server = undefined;
this.app = http.createServer((req, res) => {
if (this.request) {
this.request(req, res);
Expand All @@ -23,9 +24,10 @@ class HttpServer {
listen() {
return new Promise((resolve) => {
this.server = this.app.listen(0, '0.0.0.0', () => {
this.address = `http://${this.server.address().address}:${
this.server.address().port
}`;
let address = /** @type {import('net').AddressInfo} */ (
this.server.address()
);
this.address = `http://${address.address}:${address.port}`;
resolve(this.address);
});
enableDestroy(this.server);
Expand Down Expand Up @@ -75,5 +77,3 @@ class HttpServer {
});
}
}

module.exports = HttpServer;
30 changes: 15 additions & 15 deletions lib/https-server/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict';

const enableDestroy = require('server-destroy');
const selfsigned = require('selfsigned');
const { URL } = require('url');
const https = require('https');
import enableDestroy from 'server-destroy';
import selfsigned from 'selfsigned';
import { URL } from 'url';
import https from 'https';

const attrs = [{ name: 'commonName', value: 'podium-lib.io' }];
const PEMS = selfsigned.generate(attrs, { days: 365 });

class HttpsServer {
export class HttpsServer {
address = '';
/** @type {import('http').RequestListener | undefined} */
request = undefined;
/** @type {import('https').Server | undefined} */
server = undefined;

constructor() {
this.address = '';
this.request = undefined;
this.server = undefined;
this.app = https.createServer(
{
key: PEMS.private,
Expand All @@ -33,9 +34,10 @@ class HttpsServer {
listen() {
return new Promise((resolve) => {
this.server = this.app.listen(0, '0.0.0.0', () => {
this.address = `https://${this.server.address().address}:${
this.server.address().port
}`;
let address = /** @type {import('net').AddressInfo} */ (
this.server.address()
);
this.address = `https://${address.address}:${address.port}`;
resolve(this.address);
});
enableDestroy(this.server);
Expand Down Expand Up @@ -94,5 +96,3 @@ class HttpsServer {
});
}
}

module.exports = HttpsServer;
29 changes: 16 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';
import { PodletServer } from './podlet-server/server.js';
import { HttpsServer } from './https-server/server.js';
import { HttpServer } from './http-server/server.js';
import { request } from './http-request/request.js';
import {
destinationBufferStream,
destinationObjectStream,
} from './stream-utils/streams.js';

const PodletServer = require('./podlet-server/server');
const HttpsServer = require('./https-server/server');
const HttpServer = require('./http-server/server');
const request = require('./http-request/request');
const streams = require('./stream-utils/streams');

module.exports.destinationObjectStream = streams.destinationObjectStream;
module.exports.destinationBufferStream = streams.destinationBufferStream;
module.exports.PodletServer = PodletServer;
module.exports.HttpsServer = HttpsServer;
module.exports.HttpServer = HttpServer;
module.exports.request = request;
export {
PodletServer,
HttpsServer,
HttpServer,
request,
destinationBufferStream,
destinationObjectStream,
};
44 changes: 27 additions & 17 deletions lib/podlet-server/server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
/* eslint-disable no-plusplus */
/* eslint-disable no-underscore-dangle */

'use strict';

const { HttpIncoming } = require('@podium/utils');
const enableDestroy = require('server-destroy');
const EventEmitter = require('events');
const Podlet = require('@podium/podlet');
const http = require('http');
const url = require('url');

class PodletServer extends EventEmitter {
import { HttpIncoming } from '@podium/utils';
import Podlet from '@podium/podlet';
import enableDestroy from 'server-destroy';
import EventEmitter from 'events';
import http from 'http';
import url from 'url';

/**
* @typedef {object} PodletServerOptions
* @property {string} [manifest="/manifest.json"]
* @property {string} [fallback="/fallback.html"]
* @property {string} [content="/index.html"]
* @property {string} [version="1.0.0"]
* @property {string} [pathname="/"]
* @property {string} [name="component"]
* @property {{ js?: string; css?: string; }} [assets]
* @property {Record<string, string>} [proxy]
*/

export class PodletServer extends EventEmitter {
/**
* @constructor
* @param {PodletServerOptions} options
*/
constructor({
manifest = '/manifest.json',
fallback = '/fallback.html',
Expand All @@ -25,6 +36,7 @@ class PodletServer extends EventEmitter {

// Private
this._server = undefined;
this._manifest = {};

this._podlet = new Podlet({
manifest,
Expand Down Expand Up @@ -121,8 +133,8 @@ class PodletServer extends EventEmitter {
get: () =>
// TODO: remove workaround
({
css: this._podlet.css(),
js: this._podlet.js(),
css: this._podlet.css([]),
js: this._podlet.js([]),
}),
set: (value) => {
// TODO: does probably not work as is (not in use since no tests break)
Expand Down Expand Up @@ -381,5 +393,3 @@ class PodletServer extends EventEmitter {
return `1.0.0-beta.${index.toString()}`;
}
}

module.exports = PodletServer;
33 changes: 25 additions & 8 deletions lib/stream-utils/streams.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
'use strict';

const stream = require('readable-stream');

const destinationObjectStream = (done) => {
import stream from 'readable-stream';

/**
* @template [T=string]
* @param {(result: T[]) => void} done
* @returns
*/
export const destinationObjectStream = (done) => {
/** @type {T[]} */
const arr = [];

const dStream = new stream.Writable({
objectMode: true,
/**
* @param {T} chunk
* @param {string} encoding
* @param {() => void} callback
*/
write(chunk, encoding, callback) {
arr.push(chunk);
callback();
Expand All @@ -19,13 +28,22 @@ const destinationObjectStream = (done) => {

return dStream;
};
module.exports.destinationObjectStream = destinationObjectStream;

const destinationBufferStream = (done) => {
/**
* @param {(result: string) => void} done
* @returns
*/
export const destinationBufferStream = (done) => {
/** @type {string[]} */
const buffer = [];

const dStream = new stream.Writable({
objectMode: false,
/**
* @param {string} chunk
* @param {string} encoding
* @param {() => void} callback
*/
write(chunk, encoding, callback) {
buffer.push(chunk);
callback();
Expand All @@ -38,4 +56,3 @@ const destinationBufferStream = (done) => {

return dStream;
};
module.exports.destinationBufferStream = destinationBufferStream;
Loading

0 comments on commit 3824adf

Please sign in to comment.