Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript migration changes for obj-multiplex #9

Merged
merged 7 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: 2.1

workflows:
build-test:
jobs:
- prep-deps
- test-build:
requires:
- prep-deps
- test-lint:
requires:
- prep-deps
- all-tests-pass:
requires:
- test-build
- test-lint
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved

jobs:
prep-deps:
docker:
- image: circleci/node:10
steps:
- checkout
- run:
name: Install deps
command: |
.circleci/scripts/deps-install.sh
- run:
name: Collect yarn install HAR logs
command: |
.circleci/scripts/collect-har-artifact.sh
- persist_to_workspace:
root: .
paths:
- node_modules
- build-artifacts

test-build:
docker:
- image: circleci/node:10
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Build project
command: yarn build

test-lint:
docker:
- image: circleci/node:10
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Lint
command: yarn lint

all-tests-pass:
docker:
- image: circleci/node:10
steps:
- run:
name: All tests passed
command: echo 'Great success'
9 changes: 9 additions & 0 deletions .circleci/scripts/collect-har-artifact.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set -e
set -u
set -o pipefail

mkdir -p build-artifacts/yarn-install-har
mv ./*.har build-artifacts/yarn-install-har/
8 changes: 8 additions & 0 deletions .circleci/scripts/deps-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -x
set -e
set -u
set -o pipefail

yarn --frozen-lockfile --ignore-scripts --har
32 changes: 32 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2017,
},
extends: [
'@metamask/eslint-config',
'@metamask/eslint-config/config/nodejs',
'@metamask/eslint-config/config/typescript',
],
plugins: [
'json',
],
overrides: [{
files: [
'*.js',
'*.json',
],
parserOptions: {
sourceType: 'script',
},
rules: {
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-var-requires': 'off',
},
}],
ignorePatterns: [
'!eslintrc.js',
'dist/',
'node_modules/',
],
};
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ jspm_packages/
# Typescript v1 declaration files
typings/

# Typscript build / generate output
dist/

# package-lock generated by npm
package-lock.json

# Optional npm cache directory
.npm

Expand All @@ -68,7 +74,7 @@ typings/
.LSOverride

# Icon must end with two \r
Icon
Icon

# Thumbnails
._*
Expand Down
Binary file added .nvmrc
Binary file not shown.
3 changes: 0 additions & 3 deletions circle.yml

This file was deleted.

108 changes: 0 additions & 108 deletions index.js

This file was deleted.

23 changes: 19 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@
"name": "obj-multiplex",
"version": "1.0.2",
"description": "",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "node test"
"build": "tsc --project .",
"lint": "eslint . --ext ts,js,json",
"lint:fix": "yarn lint --fix",
"test": "yarn build && node test"
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
},
"author": "",
"license": "ISC",
"dependencies": {
"end-of-stream": "^1.4.0",
"end-of-stream": "^1.4.4",
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
"once": "^1.4.0",
"readable-stream": "^2.3.3"
},
"devDependencies": {
"@metamask/eslint-config": "^4.1.0",
"@types/end-of-stream": "^1.4.0",
"@types/node": "^14.14.9",
"@types/once": "^1.4.0",
"@types/readable-stream": "^2.3.9",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"eslint": "^7.14.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-json": "^2.1.2",
"eslint-plugin-node": "^11.1.0",
"pump": "^1.0.2",
"tape": "^4.8.0"
"tape": "^4.8.0",
"typescript": "^4.1.2"
}
}
93 changes: 93 additions & 0 deletions src/ObjectMultiplex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Duplex } from 'readable-stream';
import eos from 'end-of-stream';
rekmarks marked this conversation as resolved.
Show resolved Hide resolved
import once from 'once';
import { Substream } from './Substream';

const IGNORE_SUBSTREAM: Record<string, unknown> = {};
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved

export class ObjectMultiplex extends Duplex {

private _substreams: Record<string, Substream | Record<string, any>>;
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved

constructor(_opts: Record<string, unknown> = {}) {
const opts = Object.assign({}, _opts, {
objectMode: true,
});
super(opts);
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
this._substreams = {};
}

createStream(name: string): Substream {
// validate name
if (!name) {
throw new Error('ObjectMultiplex - name must not be empty');
}

if (this._substreams[name]) {
throw new Error(`ObjectMultiplex - Substream for name "${name}" already exists`);
}

// create substream
const substream = new Substream({ parent: this, name });
this._substreams[name] = substream;

// listen for parent stream to end
anyStreamEnd(this, (_error?: Error | null) => {
substream.destroy();
});

return substream;
}

// ignore streams (dont display orphaned data warning)
ignoreStream(name: string): void {
// validate name
if (!name) {
throw new Error('ObjectMultiplex - name must not be empty');
}
if (this._substreams[name]) {
throw new Error(`ObjectMultiplex - Substream for name "${name}" already exists`);
}
// set
this._substreams[name] = IGNORE_SUBSTREAM;
}

_read(): void {
return undefined;
}

_write(
chunk: Record<string, any>,
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
_encoding: BufferEncoding,
callback: (error?: Error | null) => void,
): void {

const { name, data } = chunk;

if (!name) {
console.warn(`ObjectMultiplex - malformed chunk without name "${chunk}"`);
return callback();
}

// get corresponding substream
const substream = this._substreams[name];
if (!substream) {
console.warn(`ObjectMultiplex - orphaned data for stream "${name}"`);
return callback();
}

// push data into substream
if (substream !== IGNORE_SUBSTREAM) {
substream.push(data);
}

return callback();
}
}

// util
function anyStreamEnd(stream: ObjectMultiplex, _cb: (error?: Error | null) => void) {
const cb = once(_cb);
eos(stream, { readable: false }, cb);
eos(stream, { writable: false }, cb);
}
Loading