Skip to content

Commit

Permalink
ESM wip (#149)
Browse files Browse the repository at this point in the history
* ESM wip

* wip

* wip

* 0.27.1

* wip

* wip

* 0.27.2

* conect CJS to ESM, oh my god!

* 0.27.3

* fix npm pack & smoke testing

* ESM support done

* 0.27.4
  • Loading branch information
huan authored Aug 31, 2021
1 parent a3dbf69 commit 218ef0d
Show file tree
Hide file tree
Showing 43 changed files with 436 additions and 214 deletions.
File renamed without changes.
8 changes: 4 additions & 4 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh
- name: Generate Package JSON
run: ./scripts/generate-package-json.sh

- name: Pack Testing
run: ./scripts/npm-pack-testing.sh
Expand All @@ -80,8 +80,8 @@ jobs:

- name: Install Dependencies
run: npm install
- name: Generate Version
run: ./scripts/generate-version.sh
- name: Generate Package JSON
run: ./scripts/generate-package-json.sh
- name: Set Publish Config
run: ./scripts/package-publish-config-tag.sh
- run: npm run dist
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ TCP hole punching

### master v0.27

1. export generated protocol buffers class as `puppet`
1. ES Modules support
1. Export generated protocol buffers class as `puppet`

### v0.20 (Feb 21, 2021)

Expand Down
1 change: 1 addition & 0 deletions cjs/code-root.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const codeRoot: string
10 changes: 10 additions & 0 deletions cjs/code-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path')

const codeRoot = path.join(
__dirname,
'..',
)

module.exports = {
codeRoot,
}
9 changes: 9 additions & 0 deletions cjs/code-root.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env -S ts-node --project tsconfig.cjs.json

import { test } from 'tstest'

import { codeRoot } from './code-root'

test('CJS: codeRoot()', async t => {
t.ok(codeRoot, 'should exist codeRoot')
})
36 changes: 36 additions & 0 deletions cjs/generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Huan(202108)
*
* Re-exporting namespace declarations in ES6 ambient declaration #4336
* https://github.com/microsoft/TypeScript/issues/4336
*/

/**
* Huan(202108): I want to `declare namespace puppet {...}`
* but it seemss the `export * from '../generated/...js` is not working
*
* So I export them on the top level,
* then import them in another `puppet.js` file
* with the `puppet` namespace
*
* This is because the ESM module system need to do the following things
* when import a CJS module:
*
* ```ts
* import pkg from './cjs-pkg'
* const puppet = pkg['puppet']
* ```
*/
export * from '../generated/wechaty/puppet/base_pb'
export * from '../generated/wechaty/puppet/contact_pb.js'
export * from '../generated/wechaty/puppet/event_pb.js'
export * from '../generated/wechaty/puppet/file_box_pb.js'
export * from '../generated/wechaty/puppet/friendship_pb.js'
export * from '../generated/wechaty/puppet/message_pb.js'
export * from '../generated/wechaty/puppet/room_invitation_pb.js'
export * from '../generated/wechaty/puppet/room_member_pb.js'
export * from '../generated/wechaty/puppet/room_pb.js'
export * from '../generated/wechaty/puppet/tag_pb.js'

export * from '../generated/wechaty/puppet_grpc_pb.js'
export * from '../generated/wechaty/puppet_pb.js'
37 changes: 37 additions & 0 deletions cjs/generated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const pkgs = [
'../generated/wechaty/puppet/base_pb.js',
'../generated/wechaty/puppet/contact_pb.js',
'../generated/wechaty/puppet/event_pb.js',
'../generated/wechaty/puppet/file_box_pb.js',
'../generated/wechaty/puppet/friendship_pb.js',
'../generated/wechaty/puppet/message_pb.js',
'../generated/wechaty/puppet/room_invitation_pb.js',
'../generated/wechaty/puppet/room_member_pb.js',
'../generated/wechaty/puppet/room_pb.js',
'../generated/wechaty/puppet/tag_pb.js',

'../generated/wechaty/puppet_grpc_pb.js',
'../generated/wechaty/puppet_pb.js',
]

/**
* Huan(202108):
* if there's a "package.json" file in the `generated/` directory,
* then all the files in the `generated/` directory will be treated as one module,
* which means tht `require` each file under that directory will add methods to the same module.
*/
// for (const pkg of pkgs) {
// console.info('## pkg:', pkg)
// const module = require(pkg)
// console.info(Object.keys(module).length)
// // OOPS! The output number above will be keep increasing
// }

module.exports = pkgs.reduce((acc, pkg) => ({
...acc,
...require(pkg),
}), {}) // Huan(202108): must provide a `{}` as the initial value, or it will be `[]`

// for (const m of Object.keys(module.exports)) {
// console.info(m)
// }
23 changes: 23 additions & 0 deletions cjs/generated.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env -S ts-node --project tsconfig.cjs.json

import { test } from 'tstest'

import * as puppet from './generated.js'

test('CJS: "EventRequest"', async t => {
t.ok(puppet.EventRequest, 'should export EventRequest')
})

test('CJS: "PuppetService"', async t => {
t.ok(puppet.PuppetService, 'should export PuppetSevice')
})

test('CJS: "PuppetService"', async t => {
const map: puppet.EventTypeMap = {} as any
map.EVENT_TYPE_DIRTY = puppet.EventType.EVENT_TYPE_DIRTY
t.equal(Object.keys(map).length, 1, 'should export type EventTypeMap')
})

test('CJS: DingRequest', async t => {
t.ok(puppet.DingRequest, 'should exists "DingRequest"')
})
1 change: 1 addition & 0 deletions cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
1 change: 1 addition & 0 deletions cjs/puppet.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as puppet from './generated.js'
3 changes: 3 additions & 0 deletions cjs/puppet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
puppet: require('./generated.js')
}
19 changes: 19 additions & 0 deletions cjs/puppet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env -S ts-node --project tsconfig.cjs.json

import { test } from 'tstest'

import { puppet } from './puppet.js'

test('CJS: EventRequest', async t => {
t.ok(puppet.EventRequest, 'should export EventRequest')
})

test('CJS: PuppetService', async t => {
t.ok(puppet.PuppetService, 'should export PuppetSevice')
})

test('CJS: EventTypeMap', async t => {
const map: puppet.EventTypeMap = {} as any
map.EVENT_TYPE_DIRTY = puppet.EventType.EVENT_TYPE_DIRTY
t.equal(Object.keys(map).length, 1, 'should export type EventTypeMap')
})
15 changes: 7 additions & 8 deletions examples/auth/client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { CallMetadataGenerator } from '@grpc/grpc-js/build/src/call-credentials'
import type { CallMetadataGenerator } from '@grpc/grpc-js/build/src/call-credentials'
import fs from 'fs'

import {
grpc,
PuppetClient,
DingRequest,
} from '../../src/mod'
puppet,
} from '../../src/mod.js'

import { promisify } from '../promisify'
import { promisify } from '../promisify.js'

export async function testDing (client: PuppetClient) {
export async function testDing (client: puppet.PuppetClient) {
const ding = promisify(client.ding.bind(client))
const dingRequest = new DingRequest()
const dingRequest = new puppet.DingRequest()
dingRequest.setData('dingdong')
try {
// const metadata = new Metadata()
Expand Down Expand Up @@ -42,7 +41,7 @@ async function main () {
const callCred = grpc.credentials.createFromMetadataGenerator(metaCallback)
const combCreds = grpc.credentials.combineChannelCredentials(channelCred, callCred)

const client = new PuppetClient(
const client = new puppet.PuppetClient(
'localhost:8788',
combCreds,
{
Expand Down
6 changes: 3 additions & 3 deletions examples/auth/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import {

import {
puppetServerImpl,
} from '../../tests/puppet-server-impl'
} from '../../tests/puppet-server-impl.js'
import {
StatusBuilder,
Metadata,
UntypedHandleCall,
} from '@grpc/grpc-js'
// import { Http2SecureServer } from 'http2'
import {
import type {
sendUnaryData,
ServerUnaryCall,
} from '@grpc/grpc-js/build/src/server-call'

import http2 from 'http2'
import type http2 from 'http2'

function monkeyPatchMetadataFromHttp2Headers (
MetadataClass: typeof Metadata,
Expand Down
32 changes: 11 additions & 21 deletions examples/client.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
// tslint:disable:no-console
// tslint:disable:max-line-length
// tslint:disable:no-shadowed-variable
// tslint:disable:callable-types

// import { Metadata } from '@grpc/grpc-js'
import { StringValue } from 'google-protobuf/google/protobuf/wrappers_pb'

// import { CallMetadataGenerator } from '@grpc/grpc-js/build/src/call-credentials'

import {
grpc,
PuppetClient,
EventRequest,
EventResponse,
ContactAliasRequest,
DingRequest,
// EventType,
} from '../src/mod'
puppet,
} from '../src/mod.js'

import { promisify } from './promisify'
import { promisify } from './promisify.js'

export async function testAlias (client: PuppetClient) {
const request = new ContactAliasRequest()
export async function testAlias (client: puppet.PuppetClient) {
const request = new puppet.ContactAliasRequest()

const contactAlias = promisify(client.contactAlias.bind(client))

Expand Down Expand Up @@ -54,9 +44,9 @@ export async function testAlias (client: PuppetClient) {
}
}

export async function testDing (client: PuppetClient) {
export async function testDing (client: puppet.PuppetClient) {
const ding = promisify(client.ding.bind(client))
const dingRequest = new DingRequest()
const dingRequest = new puppet.DingRequest()
dingRequest.setData('dingdong')
try {
// const metadata = new Metadata()
Expand All @@ -67,11 +57,11 @@ export async function testDing (client: PuppetClient) {
}
}

export function testStream (client: PuppetClient) {
export function testStream (client: puppet.PuppetClient) {
// event(request: wechaty_puppet_event_pb.EventRequest, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<wechaty_puppet_event_pb.EventRequest>;
const eventStream = client.event(new EventRequest())
const eventStream = client.event(new puppet.EventRequest())
eventStream
.on('data', (chunk: EventResponse) => {
.on('data', (chunk: puppet.EventResponse) => {
// console.info('EventType:', EventType)
// console.info('type:', chunk.getType(), EventType[chunk.getType()], EventType[23])
console.info('payload:', chunk.getPayload())
Expand All @@ -96,7 +86,7 @@ async function main () {
// )
const creds = grpc.credentials.createInsecure()

const client = new PuppetClient(
const client = new puppet.PuppetClient(
'localhost:8788',
creds,
{
Expand Down
27 changes: 11 additions & 16 deletions examples/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ import util from 'util'

import {
grpc,
IPuppetServer,
PuppetService,
EventResponse,
EventType,
DingResponse,
EventRequest,
} from '../src/mod'
puppet,
} from '../src/mod.js'

// import { StringValue } from 'google-protobuf/google/protobuf/wrappers_pb'

import {
puppetServerImpl,
} from '../tests/puppet-server-impl'
} from '../tests/puppet-server-impl.js'

let eventStream: undefined | grpc.ServerWritableStream<EventRequest, EventResponse>
let eventStream: undefined | grpc.ServerWritableStream<puppet.EventRequest, puppet.EventResponse>

/**
* Huan(202003): gRPC Wait for Ready Semantics
Expand All @@ -28,7 +23,7 @@ const dingQueue = [] as string[]
/**
* Implements the SayHello RPC method.
*/
const puppetServerExample: IPuppetServer = {
const puppetServerExample: puppet.IPuppetServer = {
...puppetServerImpl,

event: (streammingCall) => {
Expand All @@ -43,8 +38,8 @@ const puppetServerExample: IPuppetServer = {
eventStream = streammingCall
while (dingQueue.length > 0) {
const data = dingQueue.shift()
const eventResponse = new EventResponse()
eventResponse.setType(EventType.EVENT_TYPE_DONG)
const eventResponse = new puppet.EventResponse()
eventResponse.setType(puppet.EventType.EVENT_TYPE_DONG)
eventResponse.setPayload(data!)
eventStream.write(eventResponse)
}
Expand All @@ -69,13 +64,13 @@ const puppetServerExample: IPuppetServer = {
if (!eventStream) {
dingQueue.push(data)
} else {
const eventResponse = new EventResponse()
eventResponse.setType(EventType.EVENT_TYPE_DONG)
const eventResponse = new puppet.EventResponse()
eventResponse.setType(puppet.EventType.EVENT_TYPE_DONG)
eventResponse.setPayload(data)
eventStream.write(eventResponse)
}

callback(null, new DingResponse())
callback(null, new puppet.DingResponse())
},
}

Expand All @@ -86,7 +81,7 @@ const puppetServerExample: IPuppetServer = {
async function main () {
const server = new grpc.Server()
server.addService(
PuppetService,
puppet.PuppetService,
puppetServerExample,
)
const serverBindPromise = util.promisify(
Expand Down
Loading

0 comments on commit 218ef0d

Please sign in to comment.