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

TS types #45

Merged
merged 10 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
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
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 @@ -339,4 +339,16 @@ function actualClose (sonic) {
sonic._buf = ''
}

/**
* These export configurations enable JS and TS developers
* to consumer fastify 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
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"version": "1.3.0",
"description": "Extremely fast utf8 only stream implementation",
"main": "index.js",
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"test": "standard && tap --no-ts --no-jsx --no-esm -t 120 test.js"
"test": "npm run test:types && standard && tap --no-ts --no-jsx --no-esm -t 120 test.js",
"test:types": "tsc && tsd"
},
"repository": {
"type": "git",
Expand All @@ -26,11 +29,14 @@
},
"homepage": "https://github.com/mcollina/sonic-boom#readme",
"devDependencies": {
"@types/node": "^14.11.1",
"fastbench": "^1.0.1",
"husky": "^4.3.0",
"proxyquire": "^2.1.0",
"standard": "^14.0.0",
"tap": "^14.10.8"
"tap": "^14.10.8",
"tsd": "^0.13.1",
"typescript": "^4.0.3"
},
"dependencies": {
"atomic-sleep": "^1.0.0",
Expand All @@ -40,5 +46,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;
}
31 changes: 31 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SonicBoom as SonicBoomNamed } from "../";
import SonicBoomDefault from "../";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! We should add this import test in every package we maintain :)

import * as SonicBoomStar from "../";
import SonicBoomCjsImport = require ("../");
const SonicBoomCjs = require("../");
const { SonicBoom } = require('SonicBoom')

const sonic = new SonicBoomNamed(1)
const sonic2 = new SonicBoomDefault(1)
const sonic3 = new SonicBoomStar.SonicBoom(1)
const sonic4 = new SonicBoomStar.default(1)
const sonic5 = new SonicBoomCjsImport.SonicBoom(1)
const sonic6 = new SonicBoomCjsImport.default(1)
const sonic7 = new SonicBoomCjs(1);
const sonic8 = new SonicBoom(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');