Skip to content

Commit

Permalink
convert utils and defaults to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpearson committed Sep 23, 2021
1 parent ed3209c commit 19721cb
Show file tree
Hide file tree
Showing 17 changed files with 619 additions and 626 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports = function (grunt) {
grunt.registerTask('set-library-version',
'Set the library version string used for loading dependencies',
function() {
var defaultsFile = gruntConfig.dirs.common + '/lib/util/defaults.js';
var defaultsFile = gruntConfig.dirs.common + '/lib/util/defaults.ts';
var defaultsText = grunt.file.read(defaultsFile).replace(/(version\s*=\s*)'([\w\.\-]+)'/, '$1\'' + gruntConfig.pkgVersion + '\'');
grunt.file.write(defaultsFile, defaultsText);

Expand Down
11 changes: 5 additions & 6 deletions common/lib/types/devicedetails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { decodeBody, encodeBody, Format } from '../util/encoding';
import isArray from '../util/isArray';
import Utils from '../util/utils';
import ErrorInfo from './errorinfo';

enum DeviceFormFactor {
Expand Down Expand Up @@ -81,14 +80,14 @@ class DeviceDetails {
return result;
}

static toRequestBody = encodeBody;
static toRequestBody = Utils.encodeBody;

static fromResponseBody(body: Array<Record<string, unknown>> | Record<string, unknown>, format: Format) {
static fromResponseBody(body: Array<Record<string, unknown>> | Record<string, unknown>, format: Utils.Format) {
if(format) {
body = decodeBody(body, format);
body = Utils.decodeBody(body, format);
}

if(isArray(body)) {
if(Utils.isArray(body)) {
return DeviceDetails.fromValuesArray(body);
} else {
return DeviceDetails.fromValues(body);
Expand Down
4 changes: 2 additions & 2 deletions common/lib/types/errorinfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import inspectError from '../util/inspectError';
import Utils from "../util/utils";

export default class ErrorInfo {
message?: string;
Expand All @@ -19,7 +19,7 @@ export default class ErrorInfo {
if(this.message) result += ': ' + this.message;
if(this.statusCode) result += '; statusCode=' + this.statusCode;
if(this.code) result += '; code=' + this.code;
if(this.cause) result += '; cause=' + inspectError(this.cause);
if(this.cause) result += '; cause=' + Utils.inspectError(this.cause);
if(this.href && !(this.message && this.message.indexOf('help.ably.io') > -1)) result += '; see ' + this.href + ' ';
result += ']';
return result;
Expand Down
15 changes: 6 additions & 9 deletions common/lib/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import BufferUtils from 'platform-bufferutils';
import Logger from '../util/logger';
import Crypto from 'platform-crypto';
import ErrorInfo from './errorinfo';
import { decodeBody, encodeBody, Format } from '../util/encoding';
import { ChannelOptions } from '../../types/channel';
import isArray from '../util/isArray';
import isObject from '../util/isObject';
import dataSizeBytes from '../util/dataSizeBytes';
import PresenceMessage from './presencemessage';
import Utils from '../util/utils';

export type CipherOptions = {
channelCipher: {
Expand Down Expand Up @@ -66,7 +63,7 @@ function getMessageSize(msg: Message) {
size += JSON.stringify(msg.extras).length;
}
if(msg.data) {
size += dataSizeBytes(msg.data);
size += Utils.dataSizeBytes(msg.data);
}
return size;
};
Expand Down Expand Up @@ -174,7 +171,7 @@ class Message {
let nativeDataType = typeof(data) == 'string' || BufferUtils.isBuffer(data) || data === null || data === undefined;

if (!nativeDataType) {
if (isObject(data) || isArray(data)) {
if (Utils.isObject(data) || Utils.isArray(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
} else {
Expand Down Expand Up @@ -205,7 +202,7 @@ class Message {
}
}

static serialize = encodeBody;
static serialize = Utils.encodeBody;

static decode(message: Message | PresenceMessage, inputContext: CipherOptions | EncodingDecodingContext | ChannelOptions) {
const context = normaliseContext(inputContext);
Expand Down Expand Up @@ -289,9 +286,9 @@ class Message {
context.baseEncodedPreviousPayload = lastPayload;
}

static fromResponseBody(body: Array<Message>, options: ChannelOptions | EncodingDecodingContext, format: Format): Message[] {
static fromResponseBody(body: Array<Message>, options: ChannelOptions | EncodingDecodingContext, format: Utils.Format): Message[] {
if(format) {
body = decodeBody(body, format);
body = Utils.decodeBody(body, format);
}

for(let i = 0; i < body.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions common/lib/types/presencemessage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Logger from '../util/logger';
import BufferUtils from 'platform-bufferutils';
import Message, { CipherOptions } from './message';
import { decodeBody, Format } from '../util/encoding';
import Utils from '../util/utils';

function toActionValue(actionString: string) {
return PresenceMessage.Actions.indexOf(actionString);
Expand Down Expand Up @@ -105,9 +105,9 @@ class PresenceMessage {
static encode = Message.encode;
static decode = Message.decode;

static fromResponseBody(body: unknown[], options: CipherOptions, format: Format) {
static fromResponseBody(body: unknown[], options: CipherOptions, format: Utils.Format) {
if(format) {
body = decodeBody(body, format);
body = Utils.decodeBody(body, format);
}

for(let i = 0; i < body.length; i++) {
Expand Down
11 changes: 5 additions & 6 deletions common/lib/types/protocolmessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { decodeBody, encodeBody, Format } from '../util/encoding';
import forInOwnNonNullProps from '../util/forInOwnNonNullProps';
import Utils from '../util/utils';
import ErrorInfo from './errorinfo';
import Message from './message';
import PresenceMessage from './presencemessage';
Expand Down Expand Up @@ -107,10 +106,10 @@ class ProtocolMessage {
return modes.length > 0 ? modes : undefined;
};

static serialize = encodeBody;
static serialize = Utils.encodeBody;

static deserialize = function(serialized: unknown, format: Format) {
const deserialized = decodeBody(serialized, format);
static deserialize = function(serialized: unknown, format: Utils.Format) {
const deserialized = Utils.decodeBody(serialized, format);
return ProtocolMessage.fromDeserialized(deserialized);
};

Expand Down Expand Up @@ -152,7 +151,7 @@ class ProtocolMessage {
result += '; flags=' + flagNames.filter(msg.hasFlag).join(',');
if(msg.params) {
let stringifiedParams = '';
forInOwnNonNullProps(msg.params, function(prop: string) {
Utils.forInOwnNonNullProps(msg.params, function(prop: string) {
if (stringifiedParams.length > 0) {
stringifiedParams += '; ';
}
Expand Down
11 changes: 5 additions & 6 deletions common/lib/types/pushchannelsubscription.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { decodeBody, encodeBody, Format } from "../util/encoding";
import isArray from "../util/isArray";
import Utils from "../util/utils";

class PushChannelSubscription {
channel?: string;
Expand Down Expand Up @@ -30,14 +29,14 @@ class PushChannelSubscription {
return result;
}

static toRequestBody = encodeBody;
static toRequestBody = Utils.encodeBody;

static fromResponseBody(body: Array<Record<string, unknown>> | Record<string, unknown>, format: Format) {
static fromResponseBody(body: Array<Record<string, unknown>> | Record<string, unknown>, format: Utils.Format) {
if(format) {
body = decodeBody(body, format);
body = Utils.decodeBody(body, format);
}

if(isArray(body)) {
if(Utils.isArray(body)) {
return PushChannelSubscription.fromValuesArray(body);
} else {
return PushChannelSubscription.fromValues(body);
Expand Down
6 changes: 3 additions & 3 deletions common/lib/types/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import forInOwnNonNullProps from '../util/forInOwnNonNullProps';
import Utils from "../util/utils"

type MessageValues = {
count?: number;
Expand Down Expand Up @@ -121,7 +121,7 @@ class MessageCategory extends MessageCount {
super(values);
if (values && values.category) {
this.category = {};
forInOwnNonNullProps(values.category, (prop: string) => {
Utils.forInOwnNonNullProps(values.category, (prop: string) => {
(this.category as Record<string, MessageCount>)[prop] = new MessageCount((values.category as Record<string, MessageCount>)[prop]);
});
}
Expand Down Expand Up @@ -263,7 +263,7 @@ class ProcessedMessages {
this.delta = undefined;
if (values && values.delta) {
this.delta = { };
forInOwnNonNullProps(values.delta, (prop: string) => {
Utils.forInOwnNonNullProps(values.delta, (prop: string) => {
(this.delta as Record<string, ProcessedCount>)[prop] = new ProcessedCount((values.delta as Record<string, ProcessedCountValues>)[prop]);
});
}
Expand Down
13 changes: 0 additions & 13 deletions common/lib/util/dataSizeBytes.ts

This file was deleted.

Loading

0 comments on commit 19721cb

Please sign in to comment.