Skip to content

Commit

Permalink
chore: fix build process
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Aug 29, 2024
1 parent cfba764 commit ecad9d3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 73 deletions.
1 change: 1 addition & 0 deletions .aegir.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
build: {
config: {
format: 'esm',
platform: 'node',
external: ['*.node'],
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"scripts": {
"artifacts": "napi artifacts",
"build": "yarn build:rs && scripts/postbuild.sh && yarn build:ts",
"build:ts": "aegir build",
"build:rs": "napi build --platform --release --dts src/napi.d.ts --js src/napi.cjs",
"build:rs:debug": "napi build --platform --dts src/napi.d.ts --js src/napi.cjs",
"build:ts": "aegir build -t node",
"build:rs": "napi build --platform --release --dts src/napi.d.ts --js src/napi.js",
"build:rs:debug": "napi build --platform --dts src/napi.d.ts --js src/napi.js",
"lint": "aegir lint",
"prepublishOnly": "napi prepublish -t npm",
"test": "aegir test",
Expand Down
5 changes: 5 additions & 0 deletions scripts/JS_GEN_PREFIX
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

const __dirname = new URL('.', import.meta.url).pathname;

12 changes: 8 additions & 4 deletions scripts/postbuild.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/bin/bash

# typescript dislikes the generated js code from napi-rs

# Typescript dislikes the generated js code from napi-rs
# so we need to disable type checking for that file
# Also, we're hacking the generated output to convert cjs to esm

# this script should be run once after running yarn build:rs

OUTPUT="src/napi.cjs"
OUTPUT="src/napi.js"

{ cat scripts/JS_GEN_PREFIX; cat $OUTPUT; } > ${OUTPUT}.tmp
mv ${OUTPUT}.tmp $OUTPUT
sed -i "s/\.\/libp2p/..\/libp2p/" $OUTPUT
sed -i "s/'libp2p/'..\/libp2p/" $OUTPUT

sed -i "s/\.\/libp2p/..\/..\/libp2p/" $OUTPUT
sed -i "s/'libp2p/'..\/..\/libp2p/" $OUTPUT
sed -i "s/module\.exports\.\(.*\) = \(.*\)/export { \2 }/g" $OUTPUT
44 changes: 21 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import type { Sink, Source } from 'it-stream-types'
import { Uint8ArrayList } from 'uint8arraylist'
import { dialFilter, listenFilter } from './filter.js'

import * as napii from './napi.cjs'
import type * as types from './napi'
const napi = napii as typeof types
import * as napi from './napi.js'

export function quic(options?: Partial<QuicOptions>): (components: QuicComponents) => Transport {
return (components) => new QuicTransport(components, {...defaultOptions, ...options})
Expand All @@ -22,7 +20,7 @@ export const defaultOptions: QuicOptions = {
maxConnectionData: 15_000_000,
}

export type QuicOptions = Omit<types.Config, "privateKeyProto"> & {}
export type QuicOptions = Omit<napi.Config, "privateKeyProto"> & {}
export type QuicComponents = {
metrics?: Metrics
logger: ComponentLogger
Expand All @@ -39,11 +37,11 @@ export class QuicTransport implements Transport {
readonly log: Logger
readonly components: QuicComponents

readonly #config: types.QuinnConfig
readonly #config: napi.QuinnConfig

readonly #clients: {
ip4: types.Client
ip6: types.Client
ip4: napi.Client
ip6: napi.Client
}

readonly listenFilter: MultiaddrFilter
Expand Down Expand Up @@ -95,25 +93,25 @@ export class QuicTransport implements Transport {

type QuicListenerInit = {
options: QuicCreateListenerOptions
config: types.QuinnConfig
config: napi.QuinnConfig
logger: ComponentLogger
}

type QuicListenerState = {
status: 'ready'
} | {
status: 'listening'
listener: types.Server
listener: napi.Server
listenAddr: Multiaddr
controller: AbortController
} | {
status: 'closed'
listener: types.Server
listener: napi.Server
listenAddr: Multiaddr
}

export class QuicListener extends TypedEventEmitter<ListenerEvents> implements Listener {
readonly #config: types.QuinnConfig
readonly #config: napi.QuinnConfig
readonly init: QuicListenerInit
readonly options: QuicCreateListenerOptions
readonly log: Logger
Expand Down Expand Up @@ -168,7 +166,7 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
const connection = await Promise.race([
aborted,
this.state.listener.inboundConnection(),
]) as types.Connection | undefined
]) as napi.Connection | undefined
if (connection == null) {
break
}
Expand All @@ -181,7 +179,7 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
}
}

async onInboundConnection(connection: types.Connection): Promise<void> {
async onInboundConnection(connection: napi.Connection): Promise<void> {
const maConn = new QuicConnection({
connection,
logger: this.init.logger,
Expand All @@ -203,20 +201,20 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
}

type QuicConnectionInit = {
connection: types.Connection
connection: napi.Connection
logger: ComponentLogger
}

type QuicStreamMuxerFactoryInit = {
connection: types.Connection
connection: napi.Connection
logger: ComponentLogger
}

/**
* Each stream muxer factory is only configured for a single connection
*/
export class QuicStreamMuxerFactory implements StreamMuxerFactory {
#connection: types.Connection
#connection: napi.Connection
init: QuicStreamMuxerFactoryInit
protocol: string = 'quic'

Expand All @@ -235,12 +233,12 @@ export class QuicStreamMuxerFactory implements StreamMuxerFactory {
}

type QuicStreamMuxerInit = StreamMuxerInit & {
connection: types.Connection
connection: napi.Connection
logger: ComponentLogger
}

class QuicStreamMuxer implements StreamMuxer {
#connection: types.Connection
#connection: napi.Connection
init: QuicStreamMuxerInit
log: Logger

Expand All @@ -264,7 +262,7 @@ class QuicStreamMuxer implements StreamMuxer {
const stream = await Promise.race([
aborted,
this.#connection.inboundStream()
]) as types.Stream | undefined
]) as napi.Stream | undefined
if (stream == null) {
break
}
Expand All @@ -277,7 +275,7 @@ class QuicStreamMuxer implements StreamMuxer {
}
}

private onInboundStream = (str: types.Stream) => {
private onInboundStream = (str: napi.Stream) => {
const stream = new QuicStream({
stream: str,
direction: 'inbound',
Expand Down Expand Up @@ -311,7 +309,7 @@ class QuicStreamMuxer implements StreamMuxer {
}

export class QuicConnection implements MultiaddrConnection {
readonly #connection: types.Connection
readonly #connection: napi.Connection

readonly log: Logger
readonly remoteAddr: Multiaddr
Expand All @@ -336,13 +334,13 @@ export class QuicConnection implements MultiaddrConnection {
}

type QuicStreamInit = {
stream: types.Stream
stream: napi.Stream
direction: Direction
logger: ComponentLogger
}

export class QuicStream implements Stream {
readonly #stream: types.Stream
readonly #stream: napi.Stream

readonly id: string
readonly direction: Direction
Expand Down
Loading

0 comments on commit ecad9d3

Please sign in to comment.