Skip to content

Commit

Permalink
Remove node type ambient declaration (#618)
Browse files Browse the repository at this point in the history
* adding a shim for buffer

* adding node stream interface

* renaming shims

* remove dom-shim

* using uintarray
  • Loading branch information
nikithauc authored Feb 9, 2022
1 parent 37b0ed1 commit 720d6d5
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 28 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"prettier/prettier": "error",
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
],
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/naming-convention": [
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"files": [
"lib/",
"src/",
"authProviders/"
"authProviders/",
"shims.d.ts"
],
"scripts": {
"build": "npm run pre-build && npm run build:sub_cjs && npm run build:sub_es && rollup -c",
Expand Down
21 changes: 21 additions & 0 deletions shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/

/**
* Shim for Node stream interface.
*/
interface NodeStream {
/**
* Shim for Node stream interface when the environment does not use @types/node.
* Using @types/node appends the ambient Node definition to the global scope which is not desirable.
* https://github.com/microsoftgraph/msgraph-sdk-javascript/issues/600
*/
readable: boolean;
readableLength: number;
read(size?: number): any;
on(event: string | symbol, listener: (...args: any[]) => void): this;
}
2 changes: 2 additions & 0 deletions src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* -------------------------------------------------------------------------------------------
*/

// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path= "../../shims.d.ts" />
export { BatchRequestStep, BatchRequestData, BatchRequestContent, RequestData, BatchRequestBody } from "../content/BatchRequestContent";
export { BatchResponseBody, BatchResponseContent } from "../content/BatchResponseContent";

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* -------------------------------------------------------------------------------------------
*/

// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path= "./../shims.d.ts" />

export { BatchRequestBody, RequestData, BatchRequestContent, BatchRequestData, BatchRequestStep } from "./content/BatchRequestContent";
export { BatchResponseBody, BatchResponseContent } from "./content/BatchResponseContent";

Expand Down
6 changes: 2 additions & 4 deletions src/tasks/FileUploadTask/FileObjectClasses/StreamUpload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Readable } from "stream";

import { GraphClientError } from "../../../GraphClientError";
import { FileObject, SliceType } from "../../LargeFileUploadTask";
import { Range } from "../Range";
Expand All @@ -19,15 +17,15 @@ interface SliceRecord {
* @class
* FileObject class for Readable Stream upload
*/
export class StreamUpload implements FileObject<Readable> {
export class StreamUpload implements FileObject<NodeStream> {
/**
* @private
* Represents a cache of the last attempted upload slice.
* This can be used when resuming a previously failed slice upload.
*/
private previousSlice: SliceRecord;

public constructor(public content: Readable, public name: string, public size: number) {
public constructor(public content: NodeStream, public name: string, public size: number) {
if (!content || !name || !size) {
throw new GraphClientError("Please provide the Readable Stream content, name of the file and size of the file");
}
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/LargeFileUploadTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface LargeFileUploadSession {
* @type
* Representing the return type of the sliceFile function that is type of the slice of a given range.
*/
export type SliceType = ArrayBuffer | Blob | Buffer;
export type SliceType = ArrayBuffer | Blob | Uint8Array;

/**
* @interface
Expand Down Expand Up @@ -230,10 +230,10 @@ export class LargeFileUploadTask<T> {
*/
public sliceFile(range: Range): ArrayBuffer | Blob {
console.warn("The LargeFileUploadTask.sliceFile() function has been deprecated and moved into the FileObject interface.");
if (this.file.content instanceof ArrayBuffer || this.file.content instanceof Blob || this.file.content instanceof Buffer) {
if (this.file.content instanceof ArrayBuffer || this.file.content instanceof Blob || this.file.content instanceof Uint8Array) {
return this.file.content.slice(range.minValue, range.maxValue + 1);
}
throw new GraphClientError("The LargeFileUploadTask.sliceFile() function expects only Blob, ArrayBuffer or Buffer file content. Please note that the sliceFile() function is deprecated.");
throw new GraphClientError("The LargeFileUploadTask.sliceFile() function expects only Blob, ArrayBuffer or Uint8Array file content. Please note that the sliceFile() function is deprecated.");
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/tasks/OneDriveLargeFileUploadTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ interface OneDriveFileUploadSessionPayLoad {
/**
* @interface
* Signature to define the file information when processing an upload task
* @property {File | Buffer} content - The file content
* @property {File | Uint8Array} content - The file content
* @property {number} size - The size of file
*/
interface FileInfo {
content: File | Buffer;
content: File | Uint8Array;
size: number;
}

Expand Down Expand Up @@ -103,11 +103,11 @@ export class OneDriveLargeFileUploadTask<T> extends LargeFileUploadTask<T> {
* @private
* @static
* Get file information
* @param {Blob | Buffer | File} file - The file entity
* @param {Blob | Uint8Array | File} file - The file entity
* @param {string} fileName - The file name
* @returns {FileInfo} The file information
*/
private static getFileInfo(file: Blob | Buffer | File, fileName: string): FileInfo {
private static getFileInfo(file: Blob | Uint8Array | File, fileName: string): FileInfo {
let content;
let size;
if (typeof Blob !== "undefined" && file instanceof Blob) {
Expand All @@ -116,8 +116,8 @@ export class OneDriveLargeFileUploadTask<T> extends LargeFileUploadTask<T> {
} else if (typeof File !== "undefined" && file instanceof File) {
content = file as File;
size = content.size;
} else if (typeof Buffer !== "undefined" && file instanceof Buffer) {
const b = file as Buffer;
} else if (typeof Uint8Array !== "undefined" && file instanceof Uint8Array) {
const b = file as Uint8Array;
size = b.byteLength;
content = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
}
Expand All @@ -133,18 +133,18 @@ export class OneDriveLargeFileUploadTask<T> extends LargeFileUploadTask<T> {
* @async
* Creates a OneDriveLargeFileUploadTask
* @param {Client} client - The GraphClient instance
* @param {Blob | Buffer | File} file - File represented as Blob, Buffer or File
* @param {Blob | Uint8Array | File} file - File represented as Blob, Uint8Array or File
* @param {OneDriveLargeFileUploadOptions} options - The options for upload task
* @returns The promise that will be resolves to OneDriveLargeFileUploadTask instance
*/
public static async create(client: Client, file: Blob | Buffer | File, options: OneDriveLargeFileUploadOptions): Promise<OneDriveLargeFileUploadTask<Blob | ArrayBuffer | Buffer>> {
public static async create(client: Client, file: Blob | Uint8Array | File, options: OneDriveLargeFileUploadOptions): Promise<OneDriveLargeFileUploadTask<Blob | ArrayBuffer | Uint8Array>> {
if (!client || !file || !options) {
throw new GraphClientError("Please provide the Graph client instance, file object and OneDriveLargeFileUploadOptions value");
}
const fileName = options.fileName;
const fileInfo = OneDriveLargeFileUploadTask.getFileInfo(file, fileName);
const fileObj = new FileUpload(fileInfo.content, fileName, fileInfo.size);
return this.createTaskWithFileObject<Blob | ArrayBuffer | Buffer>(client, fileObj, options);
return this.createTaskWithFileObject<Blob | ArrayBuffer | Uint8Array>(client, fileObj, options);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"compilerOptions": {
"importHelpers": true,
"noEmitOnError": true,
"noImplicitAny": false,
"moduleResolution": "node",
"removeComments": false,
"sourceMap": true,
"declaration": true
}
}
"compilerOptions": {
"importHelpers": true,
"noEmitOnError": true,
"noImplicitAny": false,
"moduleResolution": "node",
"removeComments": false,
"sourceMap": true,
"declaration": true,
}
}

0 comments on commit 720d6d5

Please sign in to comment.