Added types for socket.io-file package (#40119)

* Added types for socket.io-file package

* Added TypeScript Version comment

* Fixed formatting

* Fixed formatting errors

* Fixed formatting errors
This commit is contained in:
Dief Bell
2019-11-05 21:47:21 +00:00
committed by Nathan Shively-Sanders
parent 677662da78
commit 5149044884
4 changed files with 128 additions and 0 deletions

38
types/socket.io-file/index.d.ts vendored Normal file
View File

@@ -0,0 +1,38 @@
// Type definitions for socket.io-file 2.0
// Project: https://github.com/rico345100/socket.io-file
// Definitions by: Dief Bell <https://github.com/merrickking>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { Socket } from 'socket.io';
export = SocketIOFile;
declare class SocketIOFile {
constructor(socket: Socket, options: Options);
on(event: string, cb: (fileInfo: FileInfo) => void): void;
}
interface Options {
uploadDir: string | { [dirId: string]: string };
maxFileSize?: number;
accepts?: string[];
chunkSize?: number;
transmissionDelay?: number;
overwrite?: boolean;
rename?: (fileName: string, fileInfo: FileInfo) => string | string;
resume?: boolean;
}
interface FileInfo {
name: string;
size: number;
path: string;
wrote: number;
uploadDir: string;
data: any[];
mime: string;
estimated: number;
uploadId: string;
originalFileName: string;
}

View File

@@ -0,0 +1,66 @@
"use strict";
import express = require('express');
const app = express();
import http = require('http');
const httpServer = new http.Server(app);
import io = require('socket.io');
const socket = io(httpServer);
import SocketIOFile = require('socket.io-file');
app.get('/', (req, res, next) => {
res.sendFile(__dirname + '/client/index.html');
return;
});
app.get('/app.js', (req, res, next) => {
res.sendFile(__dirname + '/client/app.js');
return;
});
app.get('/socket.io.js', (req, res, next) => {
res.sendFile(__dirname + '/node_modules/socket.io-client/dist/socket.io.js');
return;
});
app.get('/socket.io-file-client.js', (req, res, next) => {
res.sendFile(__dirname + '/node_modules/socket.io-file-client/socket.io-file-client.js');
return;
});
socket.on('connection', (socket: io.Socket) => {
console.log('Socket connected.');
const uploader = new SocketIOFile(socket, {
// uploadDir: { // multiple directories
// music: 'data/music',
// document: 'data/document'
// },
uploadDir: 'data', // simple directory
accepts: ['audio/mpeg', 'audio/mp3'], // chrome and some of browsers checking mp3 as 'audio/mp3', not 'audio/mpeg'
maxFileSize: 4194304, // 4 MB. default is undefined(no limit)
chunkSize: 10240, // default is 10240(1KB)
transmissionDelay: 0, // delay of each transmission, higher value saves more cpu resources, lower upload speed. default is 0(no delay)
overwrite: true // overwrite file if exists, default is true.
});
uploader.on('start', (fileInfo) => {
console.log('Start uploading');
console.log(fileInfo);
});
uploader.on('stream', (fileInfo) => {
console.log(`${fileInfo.wrote} / ${fileInfo.size} byte(s)`);
});
uploader.on('complete', (fileInfo) => {
console.log('Upload Complete.');
console.log(fileInfo);
});
uploader.on('error', (err) => {
console.log('Error!', err);
});
uploader.on('abort', (fileInfo) => {
console.log('Aborted: ', fileInfo);
});
});
httpServer.listen(3000, () => {
console.log('Server listening on port 3000');
});

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"socket.io-file-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }