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 { WebResource } from "../webResource";
import { ServiceClientCredentials } from "./serviceClientCredentials";
import * as base64 from "../util/base64";
const HeaderConstants = Constants.HeaderConstants;
const DEFAULT_AUTHORIZATION_SCHEME = "Basic";
@ -40,7 +41,7 @@ export class BasicAuthenticationCredentials implements ServiceClientCredentials
*/
signRequest(webResource: WebResource) {
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();
webResource.headers.set(HeaderConstants.AUTHORIZATION, encodedCredentials);
return Promise.resolve(webResource);

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

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

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

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

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

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

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

@ -10,6 +10,14 @@ import { Mappers } from "./data/TestClient/lib/models/mappers";
let Serializer = new msRest.Serializer({});
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("serializeObject", function () {
it("should correctly serialize a Date Object", function (done) {
@ -25,10 +33,10 @@ describe("msrest", function () {
done();
});
it("should correctly serialize a Buffer Object", function (done) {
let bufferObj = new Buffer("Javascript");
it("should correctly serialize a Uint8Array Object", function (done) {
const byteArray = stringToByteArray("Javascript");
let base64str = "SmF2YXNjcmlwdA==";
msRest.serializeObject(bufferObj).should.equal(base64str);
msRest.serializeObject(byteArray).should.equal(base64str);
done();
});
@ -52,11 +60,11 @@ describe("msrest", function () {
let o1: any = {
"p1": "value1",
"p2": "value2",
"top-buf": new Buffer("top string", "utf-8"),
"top-buf": stringToByteArray("top string"),
"top-date": new Date("2014"),
"top-dates": [new Date("1900"), new Date("1901")],
"insider": {
"insider-buf": new Buffer("insider string", "utf-8"),
"insider-buf": stringToByteArray("insider string"),
"insider-date": new Date("2015"),
"insider-dates": [new Date("2100"), new Date("2101")],
"insider-dictionary": {
@ -71,7 +79,7 @@ describe("msrest", function () {
"male": true,
"birthday": "1992-01-01T00:00:00.000Z",
"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 bufferObj = new Buffer("Javascript");
let byteArray = stringToByteArray("Javascript");
let base64str = "SmF2YXNjcmlwdA==";
let serializedObject = Serializer.serialize(mapper, bufferObj, "stringBody");
let serializedObject = Serializer.serialize(mapper, byteArray, "stringBody");
serializedObject.should.equal(base64str);
done();
});
@ -509,7 +517,7 @@ describe("msrest", function () {
"birthday": new Date("2012-01-05T01:00:00Z"),
"species": "king",
"length": 1.0,
"picture": new Buffer([255, 255, 255, 255, 254]),
"picture": new Uint8Array([255, 255, 255, 255, 254]),
"siblings": [
{
"fishtype": "shark",
@ -523,7 +531,7 @@ describe("msrest", function () {
"age": 105,
"birthday": new Date("1900-01-05T01:00:00Z"),
"length": 10.0,
"picture": new Buffer([255, 255, 255, 255, 254]),
"picture": new Uint8Array([255, 255, 255, 255, 254]),
"species": "dangerous"
}
]
@ -592,7 +600,7 @@ describe("msrest", function () {
});
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 buf = Buffer.from("HelloWorld!");
let buf = [1, 2, 3];
let deserializedObject = Serializer.deserialize(mapper, buf, "anyBody");
deserializedObject.should.equal(buf);
done();
@ -764,7 +772,7 @@ describe("msrest", function () {
"birthday": new Date("2012-01-05T01:00:00Z").toISOString(),
"species": "king",
"length": 1.0,
"picture": new Buffer([255, 255, 255, 255, 254]).toString(),
"picture": Uint8Array.from([255, 255, 255, 255, 254]).toString(),
"siblings": [
{
"fish.type": "shark",
@ -778,7 +786,7 @@ describe("msrest", function () {
"age": 105,
"birthday": new Date("1900-01-05T01:00:00Z").toISOString(),
"length": 10.0,
"picture": new Buffer([255, 255, 255, 255, 254]).toString(),
"picture": Uint8Array.from([255, 255, 255, 255, 254]).toString(),
"species": "dangerous"
}
]
@ -865,14 +873,14 @@ describe("msrest", function () {
'birthday': new Date('2012-01-05T01:00:00Z').toISOString(),
'species': 'king',
'length': 1.0,
'picture': new Buffer([255, 255, 255, 255, 254]).toString(),
'picture': Uint8Array.from([255, 255, 255, 255, 254]).toString(),
'siblings': [
{
'fish.type': 'mutatedshark',
'age': 105,
'birthday': new Date('1900-01-05T01:00:00Z').toISOString(),
'length': 10.0,
'picture': new Buffer([255, 255, 255, 255, 254]).toString(),
'picture': Uint8Array.from([255, 255, 255, 255, 254]).toString(),
'species': 'dangerous',
'siblings': [
{

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

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

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

@ -10,18 +10,23 @@ const config: webpack.Configuration = {
libraryTarget: 'var',
library: 'msRest'
},
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
],
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /(node_modules|test)/,
options: { configFile: 'tsconfig.webpack.json' }
exclude: /(node_modules|test)/
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
extensions: [".tsx", ".ts", ".js"],
alias: {
"moment": path.resolve('./node_modules/moment/min/moment.min.js')
}
},
node: {
fs: false,
@ -31,7 +36,9 @@ const config: webpack.Configuration = {
tls: false,
tty: 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 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'))],
mode: 'development',
devtool: 'source-map',
@ -13,21 +13,23 @@ const config = {
filename: 'testBundle.js',
path: __dirname
},
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
],
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /(node_modules)/,
options: { configFile: "tsconfig.webpack.json" }
exclude: /(node_modules)/
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
externals: {
"ms-rest-js": "msRest"
extensions: [".tsx", ".ts", ".js"],
alias: {
"moment": path.join(__dirname, 'node_modules/moment/min/moment.min.js')
}
},
node: {
fs: false,
@ -37,7 +39,9 @@ const config = {
tls: false,
tty: false,
v8: false,
Buffer: true // TODO: find alternative to Buffer for tests
Buffer: false,
process: false,
stream: false
}
};