Skip to content

Commit

Permalink
TS types (#45)
Browse files Browse the repository at this point in the history
* Add TS types

* Fix npm script

* Replaces `files` with `.npmignore`

* Second iteration of TS types

* Add "the infamous triplet"

* Fix wording

* Improve assertion

* Make script entries consistent
  • Loading branch information
kibertoad authored May 21, 2021
1 parent 91250dd commit 4b09301
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
types/index.d.ts
types/index.test-d.ts
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- run: node --version
- run: npm --version
- run: npm install --ignore-scripts
- run: npm run test-only
- run: npm run test:unit
env:
CI: true

Expand All @@ -57,4 +57,4 @@ jobs:
steps:
- uses: fastify/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ package-lock.json

# node clinic
*clinic*
/.idea/
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.editorconfig
.gitattributes
.git
.DS_Store
.gitignore
.github
.dependabot
.clinic
tsconfig.json
types/index.test-d.ts
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,16 @@ function actualClose (sonic) {
sonic._buf = ''
}

/**
* These export configurations enable JS and TS developers
* to consumer SonicBoom in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const SonicBoom = require('SonicBoom')`
* - `const { SonicBoom } = require('SonicBoom')`
* - `import * as SonicBoom from 'SonicBoom'`
* - `import { SonicBoom } from 'SonicBoom'`
* - `import SonicBoom from 'SonicBoom'`
*/
SonicBoom.SonicBoom = SonicBoom
SonicBoom.default = SonicBoom
module.exports = SonicBoom
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"version": "1.4.1",
"description": "Extremely fast utf8 only stream implementation",
"main": "index.js",
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"test-only": "tap test.js",
"test": "standard && tap test.js",
"test": "npm run test:types && standard && tap test.js",
"test:unit": "tap test.js",
"test:types": "tsc && tsd",
"prepare": "husky install"
},
"repository": {
Expand All @@ -28,11 +31,14 @@
},
"homepage": "https://github.com/mcollina/sonic-boom#readme",
"devDependencies": {
"@types/node": "^15.3.0",
"fastbench": "^1.0.1",
"husky": "^6.0.0",
"proxyquire": "^2.1.0",
"standard": "^16.0.3",
"tap": "^15.0.1"
"tap": "^15.0.1",
"tsd": "^0.15.1",
"typescript": "^4.2.4"
},
"dependencies": {
"atomic-sleep": "^1.0.0",
Expand All @@ -42,5 +48,8 @@
"hooks": {
"pre-commit": "npm test"
}
},
"tsd": {
"directory": "./types"
}
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"lib": [ "es2015" ],
"module": "commonjs",
"noEmit": true,
"strict": true
},
"include": [
"./types/index.test-d.ts",
"./types/index.d.ts"
]
}
48 changes: 48 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Type definitions for sonic-boom 0.7
// Definitions by: Alex Ferrando <https://github.com/alferpal>
// Igor Savin <https://github.com/kibertoad>
/// <reference types="node"/>

import { EventEmitter } from 'events';

export default SonicBoom;

export class SonicBoom extends EventEmitter {
/**
* @param [fileDescriptor] File path or numerical file descriptor
* relative protocol is enabled. Default: process.stdout
* @returns a new sonic-boom instance
*/
constructor(fileDescriptor: string | number, minLength?: number, sync?: boolean)

/**
* Writes the string to the file. It will return false to signal the producer to slow down.
*/
write(string: string): void;

/**
* Writes the current buffer to the file if a write was not in progress.
* Do nothing if minLength is zero or if it is already writing.
*/
flush(): void;

/**
* Reopen the file in place, useful for log rotation.
*/
reopen(fileDescriptor?: string | number): void;

/**
* Flushes the buffered data synchronously. This is a costly operation.
*/
flushSync(): void;

/**
* Closes the stream, the data will be flushed down asynchronously
*/
end(): void;

/**
* Closes the stream immediately, the data is not flushed.
*/
destroy(): void;
}
34 changes: 34 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expectType } from "tsd";
import SonicBoom, { SonicBoom as SonicBoomNamed } from "../";
import SonicBoomDefault from "../";
import * as SonicBoomStar from "../";
import SonicBoomCjsImport = require ("../");
const SonicBoomCjs = require("../");
const { SonicBoom: SonicBoomCjsNamed } = require('SonicBoom')

const sonic = new SonicBoom(1);

expectType<SonicBoom>(new SonicBoomNamed(1));
expectType<SonicBoom>( new SonicBoomDefault(1));
expectType<SonicBoom>( new SonicBoomStar.SonicBoom(1));
expectType<SonicBoom>( new SonicBoomStar.default(1));
expectType<SonicBoom>( new SonicBoomCjsImport.SonicBoom(1));
expectType<SonicBoom>( new SonicBoomCjsImport.default(1));
expectType<any>( new SonicBoomCjs(1));
expectType<any>( new SonicBoomCjsNamed(1));

sonic.write('hello sonic\n');

sonic.flush();

sonic.flushSync();

sonic.reopen();

sonic.end();

sonic.destroy();

const extraSonic = new SonicBoom(1, 0, true);

extraSonic.write('extra sonic\n');

0 comments on commit 4b09301

Please sign in to comment.