Skip to content

Commit 720d6d5

Browse files
authored
Remove node type ambient declaration (#618)
* adding a shim for buffer * adding node stream interface * renaming shims * remove dom-shim * using uintarray
1 parent 37b0ed1 commit 720d6d5

File tree

9 files changed

+58
-28
lines changed

9 files changed

+58
-28
lines changed

.eslintrc.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
"@typescript-eslint/no-unused-vars": "error",
1010
"@typescript-eslint/no-explicit-any": "off",
1111
"@typescript-eslint/explicit-module-boundary-types": "off",
12-
"prettier/prettier": "error",
12+
"prettier/prettier": [
13+
"error",
14+
{
15+
"endOfLine": "auto"
16+
}
17+
],
1318
"@typescript-eslint/no-var-requires": "error",
1419
"@typescript-eslint/no-non-null-assertion": "error",
1520
"@typescript-eslint/naming-convention": [

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"files": [
2626
"lib/",
2727
"src/",
28-
"authProviders/"
28+
"authProviders/",
29+
"shims.d.ts"
2930
],
3031
"scripts": {
3132
"build": "npm run pre-build && npm run build:sub_cjs && npm run build:sub_es && rollup -c",

shims.d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* -------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4+
* See License in the project root for license information.
5+
* -------------------------------------------------------------------------------------------
6+
*/
7+
8+
/**
9+
* Shim for Node stream interface.
10+
*/
11+
interface NodeStream {
12+
/**
13+
* Shim for Node stream interface when the environment does not use @types/node.
14+
* Using @types/node appends the ambient Node definition to the global scope which is not desirable.
15+
* https://github.com/microsoftgraph/msgraph-sdk-javascript/issues/600
16+
*/
17+
readable: boolean;
18+
readableLength: number;
19+
read(size?: number): any;
20+
on(event: string | symbol, listener: (...args: any[]) => void): this;
21+
}

src/browser/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

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

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

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

src/tasks/FileUploadTask/FileObjectClasses/StreamUpload.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Readable } from "stream";
2-
31
import { GraphClientError } from "../../../GraphClientError";
42
import { FileObject, SliceType } from "../../LargeFileUploadTask";
53
import { Range } from "../Range";
@@ -19,15 +17,15 @@ interface SliceRecord {
1917
* @class
2018
* FileObject class for Readable Stream upload
2119
*/
22-
export class StreamUpload implements FileObject<Readable> {
20+
export class StreamUpload implements FileObject<NodeStream> {
2321
/**
2422
* @private
2523
* Represents a cache of the last attempted upload slice.
2624
* This can be used when resuming a previously failed slice upload.
2725
*/
2826
private previousSlice: SliceRecord;
2927

30-
public constructor(public content: Readable, public name: string, public size: number) {
28+
public constructor(public content: NodeStream, public name: string, public size: number) {
3129
if (!content || !name || !size) {
3230
throw new GraphClientError("Please provide the Readable Stream content, name of the file and size of the file");
3331
}

src/tasks/LargeFileUploadTask.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export interface LargeFileUploadSession {
6464
* @type
6565
* Representing the return type of the sliceFile function that is type of the slice of a given range.
6666
*/
67-
export type SliceType = ArrayBuffer | Blob | Buffer;
67+
export type SliceType = ArrayBuffer | Blob | Uint8Array;
6868

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

239239
/**

src/tasks/OneDriveLargeFileUploadTask.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ interface OneDriveFileUploadSessionPayLoad {
5151
/**
5252
* @interface
5353
* Signature to define the file information when processing an upload task
54-
* @property {File | Buffer} content - The file content
54+
* @property {File | Uint8Array} content - The file content
5555
* @property {number} size - The size of file
5656
*/
5757
interface FileInfo {
58-
content: File | Buffer;
58+
content: File | Uint8Array;
5959
size: number;
6060
}
6161

@@ -103,11 +103,11 @@ export class OneDriveLargeFileUploadTask<T> extends LargeFileUploadTask<T> {
103103
* @private
104104
* @static
105105
* Get file information
106-
* @param {Blob | Buffer | File} file - The file entity
106+
* @param {Blob | Uint8Array | File} file - The file entity
107107
* @param {string} fileName - The file name
108108
* @returns {FileInfo} The file information
109109
*/
110-
private static getFileInfo(file: Blob | Buffer | File, fileName: string): FileInfo {
110+
private static getFileInfo(file: Blob | Uint8Array | File, fileName: string): FileInfo {
111111
let content;
112112
let size;
113113
if (typeof Blob !== "undefined" && file instanceof Blob) {
@@ -116,8 +116,8 @@ export class OneDriveLargeFileUploadTask<T> extends LargeFileUploadTask<T> {
116116
} else if (typeof File !== "undefined" && file instanceof File) {
117117
content = file as File;
118118
size = content.size;
119-
} else if (typeof Buffer !== "undefined" && file instanceof Buffer) {
120-
const b = file as Buffer;
119+
} else if (typeof Uint8Array !== "undefined" && file instanceof Uint8Array) {
120+
const b = file as Uint8Array;
121121
size = b.byteLength;
122122
content = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
123123
}
@@ -133,18 +133,18 @@ export class OneDriveLargeFileUploadTask<T> extends LargeFileUploadTask<T> {
133133
* @async
134134
* Creates a OneDriveLargeFileUploadTask
135135
* @param {Client} client - The GraphClient instance
136-
* @param {Blob | Buffer | File} file - File represented as Blob, Buffer or File
136+
* @param {Blob | Uint8Array | File} file - File represented as Blob, Uint8Array or File
137137
* @param {OneDriveLargeFileUploadOptions} options - The options for upload task
138138
* @returns The promise that will be resolves to OneDriveLargeFileUploadTask instance
139139
*/
140-
public static async create(client: Client, file: Blob | Buffer | File, options: OneDriveLargeFileUploadOptions): Promise<OneDriveLargeFileUploadTask<Blob | ArrayBuffer | Buffer>> {
140+
public static async create(client: Client, file: Blob | Uint8Array | File, options: OneDriveLargeFileUploadOptions): Promise<OneDriveLargeFileUploadTask<Blob | ArrayBuffer | Uint8Array>> {
141141
if (!client || !file || !options) {
142142
throw new GraphClientError("Please provide the Graph client instance, file object and OneDriveLargeFileUploadOptions value");
143143
}
144144
const fileName = options.fileName;
145145
const fileInfo = OneDriveLargeFileUploadTask.getFileInfo(file, fileName);
146146
const fileObj = new FileUpload(fileInfo.content, fileName, fileInfo.size);
147-
return this.createTaskWithFileObject<Blob | ArrayBuffer | Buffer>(client, fileObj, options);
147+
return this.createTaskWithFileObject<Blob | ArrayBuffer | Uint8Array>(client, fileObj, options);
148148
}
149149

150150
/**

tsconfig-base.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"compilerOptions": {
3-
"importHelpers": true,
4-
"noEmitOnError": true,
5-
"noImplicitAny": false,
6-
"moduleResolution": "node",
7-
"removeComments": false,
8-
"sourceMap": true,
9-
"declaration": true
10-
}
11-
}
2+
"compilerOptions": {
3+
"importHelpers": true,
4+
"noEmitOnError": true,
5+
"noImplicitAny": false,
6+
"moduleResolution": "node",
7+
"removeComments": false,
8+
"sourceMap": true,
9+
"declaration": true,
10+
}
11+
}

0 commit comments

Comments
 (0)