Remove usages of Buffer from ms-rest-js

This commit is contained in:
Rikki Gibson 2018-05-10 11:20:38 -07:00
Родитель ead2fd350a
Коммит 3f497185a5
10 изменённых файлов: 114 добавлений и 60 удалений

Просмотреть файл

@ -5,6 +5,7 @@ import { HttpHeaders } from "../httpHeaders";
import { Constants } from "../util/constants"; import { Constants } from "../util/constants";
import { WebResource } from "../webResource"; import { WebResource } from "../webResource";
import { ServiceClientCredentials } from "./serviceClientCredentials"; import { ServiceClientCredentials } from "./serviceClientCredentials";
import * as base64 from "../util/base64";
const HeaderConstants = Constants.HeaderConstants; const HeaderConstants = Constants.HeaderConstants;
const DEFAULT_AUTHORIZATION_SCHEME = "Basic"; const DEFAULT_AUTHORIZATION_SCHEME = "Basic";
@ -40,7 +41,7 @@ export class BasicAuthenticationCredentials implements ServiceClientCredentials
*/ */
signRequest(webResource: WebResource) { signRequest(webResource: WebResource) {
const credentials = `${this.userName}:${this.password}`; const credentials = `${this.userName}:${this.password}`;
const encodedCredentials = `${this.authorizationScheme} ${Buffer.from(credentials).toString("base64")}`; const encodedCredentials = `${this.authorizationScheme} ${base64.encodeString(credentials)}`;
if (!webResource.headers) webResource.headers = new HttpHeaders(); if (!webResource.headers) webResource.headers = new HttpHeaders();
webResource.headers.set(HeaderConstants.AUTHORIZATION, encodedCredentials); webResource.headers.set(HeaderConstants.AUTHORIZATION, encodedCredentials);
return Promise.resolve(webResource); return Promise.resolve(webResource);

Просмотреть файл

@ -2,7 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information. // Licensed under the MIT License. See License.txt in the project root for license information.
import * as utils from "./util/utils"; import * as utils from "./util/utils";
const isBuffer: (obj: any) => boolean = require("is-buffer"); import * as base64 from "./util/base64";
export class Serializer { export class Serializer {
modelMappers?: { [key: string]: any }; modelMappers?: { [key: string]: any };
@ -79,26 +79,26 @@ export class Serializer {
if (!buffer) { if (!buffer) {
return undefined; return undefined;
} }
if (!isBuffer(buffer)) { if (!(buffer instanceof Uint8Array)) {
throw new Error(`Please provide an input of type Buffer for converting to Base64Url.`); throw new Error(`Please provide an input of type Uint8Array for converting to Base64Url.`);
} }
// Buffer to Base64. // Uint8Array to Base64.
const str = buffer.toString("base64"); const str = base64.encodeByteArray(buffer);
// Base64 to Base64Url. // Base64 to Base64Url.
return this.trimEnd(str, "=").replace(/\+/g, "-").replace(/\//g, "_"); return this.trimEnd(str, "=").replace(/\+/g, "-").replace(/\//g, "_");
} }
private base64UrlToBuffer(str: string): any { private base64UrlToByteArray(str: string): Uint8Array | undefined {
if (!str) { if (!str) {
return undefined; return undefined;
} }
if (str && typeof str.valueOf() !== "string") { if (str && typeof str.valueOf() !== "string") {
throw new Error("Please provide an input of type string for converting to Buffer"); throw new Error("Please provide an input of type string for converting to Uint8Array");
} }
// Base64Url to Base64. // Base64Url to Base64.
str = str.replace(/\-/g, "+").replace(/\_/g, "/"); str = str.replace(/\-/g, "+").replace(/\_/g, "/");
// Base64 to Buffer. // Base64 to Uint8Array.
return Buffer.from(str, "base64"); return base64.decodeString(str);
} }
private splitSerializeName(prop: string): Array<string> { private splitSerializeName(prop: string): Array<string> {
@ -185,20 +185,20 @@ export class Serializer {
return value; return value;
} }
private serializeBufferType(objectName: string, value: any): any { private serializeByteArrayType(objectName: string, value: any): any {
if (value !== null && value !== undefined) { if (value !== null && value !== undefined) {
if (!isBuffer(value)) { if (!(value instanceof Uint8Array)) {
throw new Error(`${objectName} must be of type Buffer.`); throw new Error(`${objectName} must be of type Uint8Array.`);
} }
value = value.toString("base64"); value = base64.encodeByteArray(value);
} }
return value; return value;
} }
private serializeBase64UrlType(objectName: string, value: any): any { private serializeBase64UrlType(objectName: string, value: any): any {
if (value !== null && value !== undefined) { if (value !== null && value !== undefined) {
if (!isBuffer(value)) { if (!(value instanceof Uint8Array)) {
throw new Error(`${objectName} must be of type Buffer.`); throw new Error(`${objectName} must be of type Uint8Array.`);
} }
value = this.bufferToBase64Url(value); value = this.bufferToBase64Url(value);
} }
@ -412,7 +412,7 @@ export class Serializer {
} else if (mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/ig) !== null) { } else if (mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/ig) !== null) {
payload = this.serializeDateTypes(mapperType, object, objectName); payload = this.serializeDateTypes(mapperType, object, objectName);
} else if (mapperType.match(/^ByteArray$/ig) !== null) { } else if (mapperType.match(/^ByteArray$/ig) !== null) {
payload = this.serializeBufferType(objectName, object); payload = this.serializeByteArrayType(objectName, object);
} else if (mapperType.match(/^Base64Url$/ig) !== null) { } else if (mapperType.match(/^Base64Url$/ig) !== null) {
payload = this.serializeBase64UrlType(objectName, object); payload = this.serializeBase64UrlType(objectName, object);
} else if (mapperType.match(/^Sequence$/ig) !== null) { } else if (mapperType.match(/^Sequence$/ig) !== null) {
@ -596,9 +596,9 @@ export class Serializer {
} else if (mapperType.match(/^UnixTime$/ig) !== null) { } else if (mapperType.match(/^UnixTime$/ig) !== null) {
payload = this.unixTimeToDate(responseBody); payload = this.unixTimeToDate(responseBody);
} else if (mapperType.match(/^ByteArray$/ig) !== null) { } else if (mapperType.match(/^ByteArray$/ig) !== null) {
payload = Buffer.from(responseBody, "base64"); payload = base64.decodeString(responseBody);
} else if (mapperType.match(/^Base64Url$/ig) !== null) { } else if (mapperType.match(/^Base64Url$/ig) !== null) {
payload = this.base64UrlToBuffer(responseBody); payload = this.base64UrlToByteArray(responseBody);
} else if (mapperType.match(/^Sequence$/ig) !== null) { } else if (mapperType.match(/^Sequence$/ig) !== null) {
payload = this.deserializeSequenceType(mapper as SequenceMapper, responseBody, objectName); payload = this.deserializeSequenceType(mapper as SequenceMapper, responseBody, objectName);
} else if (mapperType.match(/^Dictionary$/ig) !== null) { } else if (mapperType.match(/^Dictionary$/ig) !== null) {
@ -782,8 +782,8 @@ export interface UrlParameterValue {
export function serializeObject(toSerialize: any): any { export function serializeObject(toSerialize: any): any {
if (toSerialize === null || toSerialize === undefined) return undefined; if (toSerialize === null || toSerialize === undefined) return undefined;
if (isBuffer(toSerialize)) { if (toSerialize instanceof Uint8Array) {
toSerialize = toSerialize.toString("base64"); toSerialize = base64.encodeByteArray(toSerialize);
return toSerialize; return toSerialize;
} }
else if (toSerialize instanceof Date) { else if (toSerialize instanceof Date) {

39
lib/util/base64.ts Normal file
Просмотреть файл

@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
/**
* Encodes a string in base64 format.
* @param value the string to encode
*/
export function encodeString(value: string): string {
if (typeof Buffer === "undefined") {
return btoa(value);
} else {
return Buffer.from(value).toString("base64");
}
}
/**
* Encodes a byte array in base64 format.
* @param value the Uint8Aray to encode
*/
export function encodeByteArray(value: Uint8Array): string {
if (typeof Buffer === "undefined") {
return btoa(new TextDecoder().decode(value));
} else {
const bufferValue = (value instanceof Buffer) ? value : new Buffer(value);
return bufferValue.toString("base64");
}
}
/**
* Decodes a base64 string into a byte array.
* @param value the base64 string to decode
*/
export function decodeString(value: string): Uint8Array {
if (typeof Buffer === "undefined") {
return new TextEncoder().encode(atob(value));
} else {
return Buffer.from(value, "base64");
}
}

5
package-lock.json сгенерированный
Просмотреть файл

@ -4671,11 +4671,6 @@
"binary-extensions": "^1.0.0" "binary-extensions": "^1.0.0"
} }
}, },
"is-buffer": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.2.tgz",
"integrity": "sha512-imvkm8cOGKeZ/NwkAd+FAURi0hsL9gr3kvdi0r3MnqChcOdPaQRIOQiOU+sD40XzUIe6nFmSHYtQjbkDvaQbEg=="
},
"is-builtin-module": { "is-builtin-module": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz", "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz",

Просмотреть файл

@ -35,7 +35,6 @@
"@types/uuid": "^3.4.3", "@types/uuid": "^3.4.3",
"axios": "^0.18.0", "axios": "^0.18.0",
"form-data": "^2.3.2", "form-data": "^2.3.2",
"is-buffer": "^2.0.0",
"isomorphic-tough-cookie": "^0.0.1", "isomorphic-tough-cookie": "^0.0.1",
"isomorphic-xml2js": "^0.0.3", "isomorphic-xml2js": "^0.0.3",
"tslib": "^1.9.2", "tslib": "^1.9.2",

Просмотреть файл

@ -3,6 +3,7 @@
import * as should from "should"; import * as should from "should";
import * as msRest from "../../lib/msRest"; import * as msRest from "../../lib/msRest";
import * as base64 from "../../lib/util/base64";
const TokenCredentials = msRest.TokenCredentials; const TokenCredentials = msRest.TokenCredentials;
const BasicAuthenticationCredentials = msRest.BasicAuthenticationCredentials; const BasicAuthenticationCredentials = msRest.BasicAuthenticationCredentials;
const ApiKeyCredentials = msRest.ApiKeyCredentials; const ApiKeyCredentials = msRest.ApiKeyCredentials;
@ -58,7 +59,7 @@ describe("Token credentials", () => {
}); });
describe("Basic Authentication credentials", () => { describe("Basic Authentication credentials", () => {
const encodedCredentials = Buffer.from(dummyuserName + ":" + dummyPassword).toString("base64"); const encodedCredentials = base64.encodeString(dummyuserName + ":" + dummyPassword);
describe("usage", () => { describe("usage", () => {
it("should base64 encode the username and password and set auth header with baisc scheme in request", (done) => { it("should base64 encode the username and password and set auth header with baisc scheme in request", (done) => {
const creds = new BasicAuthenticationCredentials(dummyuserName, dummyPassword); const creds = new BasicAuthenticationCredentials(dummyuserName, dummyPassword);

Просмотреть файл

@ -10,6 +10,14 @@ import { Mappers } from "./data/TestClient/lib/models/mappers";
let Serializer = new msRest.Serializer({}); let Serializer = new msRest.Serializer({});
let valid_uuid = "ceaafd1e-f936-429f-bbfc-82ee75dddc33"; let valid_uuid = "ceaafd1e-f936-429f-bbfc-82ee75dddc33";
function stringToByteArray(str: string): Uint8Array {
if (typeof Buffer === "function") {
return new Buffer(str, "utf-8");
} else {
return new TextEncoder().encode(str);
}
}
describe("msrest", function () { describe("msrest", function () {
describe("serializeObject", function () { describe("serializeObject", function () {
it("should correctly serialize a Date Object", function (done) { it("should correctly serialize a Date Object", function (done) {
@ -25,10 +33,10 @@ describe("msrest", function () {
done(); done();
}); });
it("should correctly serialize a Buffer Object", function (done) { it("should correctly serialize a Uint8Array Object", function (done) {
let bufferObj = new Buffer("Javascript"); const byteArray = stringToByteArray("Javascript");
let base64str = "SmF2YXNjcmlwdA=="; let base64str = "SmF2YXNjcmlwdA==";
msRest.serializeObject(bufferObj).should.equal(base64str); msRest.serializeObject(byteArray).should.equal(base64str);
done(); done();
}); });
@ -52,11 +60,11 @@ describe("msrest", function () {
let o1: any = { let o1: any = {
"p1": "value1", "p1": "value1",
"p2": "value2", "p2": "value2",
"top-buf": new Buffer("top string", "utf-8"), "top-buf": stringToByteArray("top string"),
"top-date": new Date("2014"), "top-date": new Date("2014"),
"top-dates": [new Date("1900"), new Date("1901")], "top-dates": [new Date("1900"), new Date("1901")],
"insider": { "insider": {
"insider-buf": new Buffer("insider string", "utf-8"), "insider-buf": stringToByteArray("insider string"),
"insider-date": new Date("2015"), "insider-date": new Date("2015"),
"insider-dates": [new Date("2100"), new Date("2101")], "insider-dates": [new Date("2100"), new Date("2101")],
"insider-dictionary": { "insider-dictionary": {
@ -71,7 +79,7 @@ describe("msrest", function () {
"male": true, "male": true,
"birthday": "1992-01-01T00:00:00.000Z", "birthday": "1992-01-01T00:00:00.000Z",
"anniversary": new Date("2013-12-08"), "anniversary": new Date("2013-12-08"),
"memory": new Buffer("Yadadadada") "memory": stringToByteArray("Yadadadada")
} }
} }
}; };
@ -176,11 +184,11 @@ describe("msrest", function () {
} }
}); });
it("should correctly serialize a Buffer Object", function (done) { it("should correctly serialize a ByteArray Object", function (done) {
let mapper: msRest.Mapper = { type: { name: "ByteArray" }, required: false, serializedName: "ByteArray" }; let mapper: msRest.Mapper = { type: { name: "ByteArray" }, required: false, serializedName: "ByteArray" };
let bufferObj = new Buffer("Javascript"); let byteArray = stringToByteArray("Javascript");
let base64str = "SmF2YXNjcmlwdA=="; let base64str = "SmF2YXNjcmlwdA==";
let serializedObject = Serializer.serialize(mapper, bufferObj, "stringBody"); let serializedObject = Serializer.serialize(mapper, byteArray, "stringBody");
serializedObject.should.equal(base64str); serializedObject.should.equal(base64str);
done(); done();
}); });
@ -509,7 +517,7 @@ describe("msrest", function () {
"birthday": new Date("2012-01-05T01:00:00Z"), "birthday": new Date("2012-01-05T01:00:00Z"),
"species": "king", "species": "king",
"length": 1.0, "length": 1.0,
"picture": new Buffer([255, 255, 255, 255, 254]), "picture": new Uint8Array([255, 255, 255, 255, 254]),
"siblings": [ "siblings": [
{ {
"fishtype": "shark", "fishtype": "shark",
@ -523,7 +531,7 @@ describe("msrest", function () {
"age": 105, "age": 105,
"birthday": new Date("1900-01-05T01:00:00Z"), "birthday": new Date("1900-01-05T01:00:00Z"),
"length": 10.0, "length": 10.0,
"picture": new Buffer([255, 255, 255, 255, 254]), "picture": new Uint8Array([255, 255, 255, 255, 254]),
"species": "dangerous" "species": "dangerous"
} }
] ]
@ -592,7 +600,7 @@ describe("msrest", function () {
}); });
it("should correctly deserialize an array if the type is 'any'", function (done) { it("should correctly deserialize an array if the type is 'any'", function (done) {
let mapper: msRest.Mapper = { type: { name: "any" }, required: false, serializedName: "any" }; let mapper: msRest.Mapper = { type: { name: "any" }, required: false, serializedName: "any" };
let buf = Buffer.from("HelloWorld!"); let buf = [1, 2, 3];
let deserializedObject = Serializer.deserialize(mapper, buf, "anyBody"); let deserializedObject = Serializer.deserialize(mapper, buf, "anyBody");
deserializedObject.should.equal(buf); deserializedObject.should.equal(buf);
done(); done();
@ -764,7 +772,7 @@ describe("msrest", function () {
"birthday": new Date("2012-01-05T01:00:00Z").toISOString(), "birthday": new Date("2012-01-05T01:00:00Z").toISOString(),
"species": "king", "species": "king",
"length": 1.0, "length": 1.0,
"picture": new Buffer([255, 255, 255, 255, 254]).toString(), "picture": Uint8Array.from([255, 255, 255, 255, 254]).toString(),
"siblings": [ "siblings": [
{ {
"fish.type": "shark", "fish.type": "shark",
@ -778,7 +786,7 @@ describe("msrest", function () {
"age": 105, "age": 105,
"birthday": new Date("1900-01-05T01:00:00Z").toISOString(), "birthday": new Date("1900-01-05T01:00:00Z").toISOString(),
"length": 10.0, "length": 10.0,
"picture": new Buffer([255, 255, 255, 255, 254]).toString(), "picture": Uint8Array.from([255, 255, 255, 255, 254]).toString(),
"species": "dangerous" "species": "dangerous"
} }
] ]
@ -865,14 +873,14 @@ describe("msrest", function () {
'birthday': new Date('2012-01-05T01:00:00Z').toISOString(), 'birthday': new Date('2012-01-05T01:00:00Z').toISOString(),
'species': 'king', 'species': 'king',
'length': 1.0, 'length': 1.0,
'picture': new Buffer([255, 255, 255, 255, 254]).toString(), 'picture': Uint8Array.from([255, 255, 255, 255, 254]).toString(),
'siblings': [ 'siblings': [
{ {
'fish.type': 'mutatedshark', 'fish.type': 'mutatedshark',
'age': 105, 'age': 105,
'birthday': new Date('1900-01-05T01:00:00Z').toISOString(), 'birthday': new Date('1900-01-05T01:00:00Z').toISOString(),
'length': 10.0, 'length': 10.0,
'picture': new Buffer([255, 255, 255, 255, 254]).toString(), 'picture': Uint8Array.from([255, 255, 255, 255, 254]).toString(),
'species': 'dangerous', 'species': 'dangerous',
'siblings': [ 'siblings': [
{ {

Просмотреть файл

@ -2,7 +2,7 @@ import * as path from "path";
import * as webpackMiddleware from "webpack-dev-middleware"; import * as webpackMiddleware from "webpack-dev-middleware";
import webpack = require("webpack"); import webpack = require("webpack");
import express = require("express"); import express = require("express");
const testconfig: webpack.Configuration = require("../webpack.testconfig"); import testconfig = require("../webpack.testconfig");
const port = parseInt(process.env.PORT) || 3001; const port = parseInt(process.env.PORT) || 3001;
const app = express(); const app = express();

Просмотреть файл

@ -10,18 +10,23 @@ const config: webpack.Configuration = {
libraryTarget: 'var', libraryTarget: 'var',
library: 'msRest' library: 'msRest'
}, },
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
],
module: { module: {
rules: [ rules: [
{ {
test: /\.tsx?$/, test: /\.tsx?$/,
loader: 'ts-loader', loader: 'ts-loader',
exclude: /(node_modules|test)/, exclude: /(node_modules|test)/
options: { configFile: 'tsconfig.webpack.json' }
} }
] ]
}, },
resolve: { resolve: {
extensions: [".tsx", ".ts", ".js"] extensions: [".tsx", ".ts", ".js"],
alias: {
"moment": path.resolve('./node_modules/moment/min/moment.min.js')
}
}, },
node: { node: {
fs: false, fs: false,
@ -31,7 +36,9 @@ const config: webpack.Configuration = {
tls: false, tls: false,
tty: false, tty: false,
v8: false, v8: false,
Buffer: false Buffer: false,
process: false,
stream: false
} }
}; };

Просмотреть файл

@ -2,7 +2,7 @@ import * as webpack from 'webpack';
import * as glob from 'glob'; import * as glob from 'glob';
import * as path from 'path'; import * as path from 'path';
const config = { const config: webpack.Configuration = {
entry: [...glob.sync(path.join(__dirname, 'test/shared/**/*.ts')), ...glob.sync(path.join(__dirname, 'test/browser/**/*.ts'))], entry: [...glob.sync(path.join(__dirname, 'test/shared/**/*.ts')), ...glob.sync(path.join(__dirname, 'test/browser/**/*.ts'))],
mode: 'development', mode: 'development',
devtool: 'source-map', devtool: 'source-map',
@ -13,21 +13,23 @@ const config = {
filename: 'testBundle.js', filename: 'testBundle.js',
path: __dirname path: __dirname
}, },
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
],
module: { module: {
rules: [ rules: [
{ {
test: /\.tsx?$/, test: /\.tsx?$/,
loader: 'ts-loader', loader: 'ts-loader',
exclude: /(node_modules)/, exclude: /(node_modules)/
options: { configFile: "tsconfig.webpack.json" }
} }
] ]
}, },
resolve: { resolve: {
extensions: [".tsx", ".ts", ".js"] extensions: [".tsx", ".ts", ".js"],
}, alias: {
externals: { "moment": path.join(__dirname, 'node_modules/moment/min/moment.min.js')
"ms-rest-js": "msRest" }
}, },
node: { node: {
fs: false, fs: false,
@ -37,7 +39,9 @@ const config = {
tls: false, tls: false,
tty: false, tty: false,
v8: false, v8: false,
Buffer: true // TODO: find alternative to Buffer for tests Buffer: false,
process: false,
stream: false
} }
}; };