Commit 7f499afd authored by Rohan Prabhu's avatar Rohan Prabhu

Fix some issues with rollup

parent 418a41b9
{
"name": "mitter-node",
"version": "0.0.0",
"name": "@mitter-io/node",
"version": "0.4.2",
"description": "",
"keywords": [],
"main": "dist/mitter-node.umd.js",
......@@ -111,9 +111,12 @@
"dependencies": {
"@types/md5": "^2.1.32",
"@types/randomstring": "^1.1.6",
"axios": "^0.18.0",
"crypto": "^1.0.1",
"http": "^0.0.0",
"md5": "^2.2.1",
"randomstring": "^1.1.5"
"randomstring": "^1.1.5",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0"
}
}
......@@ -4,6 +4,8 @@ import sourceMaps from 'rollup-plugin-sourcemaps'
import camelCase from 'lodash.camelcase'
import typescript from 'rollup-plugin-typescript2'
import json from 'rollup-plugin-json'
import builtins from 'rollup-plugin-node-builtins'
import globals from 'rollup-plugin-node-globals'
const pkg = require('./package.json')
......@@ -12,27 +14,45 @@ const libraryName = 'mitter-node'
export default {
input: `src/${libraryName}.ts`,
output: [
{ file: pkg.main, name: camelCase(libraryName), format: 'umd', sourcemap: true },
{
file: pkg.main,
name: camelCase(libraryName),
format: 'cjs',
sourcemap: true,
outro: 'module.exports = Object.assign({}, module.exports, exports)'
},
{ file: pkg.module, format: 'es', sourcemap: true },
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
external: [
'websocket',
'sockjs-client',
'axios',
'crypto'
],
watch: {
include: 'src/**',
include: [
'src/**',
'./../mitter-ts-core/dist/**',
'./../mitter-ts-models/dist/**'
]
},
plugins: [
globals(),
resolve(),
builtins(),
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
json(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),
// Resolve source maps to the original source
sourceMaps(),
sourceMaps()
],
}
......@@ -8,7 +8,8 @@ export default class AccessKeySigner {
static Headers = {
Nonce: 'Nonce',
Date: 'Date',
ContentMD5: 'Content-Md5'
ContentMD5: 'Content-Md5',
ContentType: 'Content-Type'
}
public signRequest(digestParts: DigestParts): DigestGenerationArtifacts {
......
......@@ -15,20 +15,25 @@ export class AccessKeySigningInterceptor {
}
getInterceptor(): GenericInterceptor {
return (mitter, requestParams) => {
return requestParams => {
const payload =
requestParams.data === null || requestParams.data === undefined
? ''
: requestParams.data
const wirePayload = typeof payload === 'object' ? JSON.stringify(payload) : payload
const contentType = payload == '' ? 'null' : 'application/json'
const payloadMd5 = crypto
.createHash('md5')
.update(payload)
.digest('hex')
.update(wirePayload)
.digest('base64')
const digestParts = new DigestParts(
requestParams.method,
requestParams.path,
payloadMd5
payloadMd5,
contentType
)
const digest = this.accessKeySigner.signRequest(digestParts)
......@@ -42,6 +47,8 @@ export class AccessKeySigningInterceptor {
requestParams.headers[AccessKeySigner.Headers.Date] = [digest.date]
requestParams.headers[AccessKeySigner.Headers.Nonce] = [digest.nonce]
requestParams.headers[AccessKeySigner.Headers.ContentMD5] = [payloadMd5]
requestParams.headers[AccessKeySigner.Headers.ContentType] = [contentType]
requestParams.data = wirePayload
}
}
}
......@@ -3,8 +3,8 @@ export class DigestParts {
public readonly method: string,
public readonly path: string,
public readonly payloadMd5: string,
public readonly nonce?: string | undefined,
public readonly contentType?: string | undefined,
public readonly nonce?: string | undefined,
public readonly date?: string | undefined
) {}
}
......
import AccessKeySigner from './auth/AccessKeySigner'
import http from 'http'
import md5 from 'md5'
import { DigestParts } from './auth/digest-objects'
import { IncomingMessage } from 'http'
type AccessCredentials = {
accessKey: {
key: string
}
accessSecret: {
secret: string
}
}
export function testFunction(accessCredential: AccessCredentials) {
const accessKeySigner = new AccessKeySigner(
accessCredential['accessKey']['key'],
accessCredential['accessSecret']['secret']
)
const path = '/test/application/by/accesskey'
const digestParts = new DigestParts('get', path, Buffer.from(md5(''), 'hex').toString('base64'))
const generatedDigest = accessKeySigner.signRequest(digestParts)
const headers = {
Date: generatedDigest.date,
'X-Mitter-Application-Access-Key': accessKeySigner.accessKey,
Authorization: generatedDigest.authorizationHeader,
Nonce: generatedDigest.nonce,
'Content-Md5': Buffer.from(md5(''), 'hex').toString('base64')
}
http.request(
{
host: 'localhost',
port: 8080,
path: path,
headers: headers
},
(res: IncomingMessage) => {
let data = ''
res.on('data', chunk => {
data += chunk
})
res.on('end', () => {
console.log(JSON.parse(data))
})
}
)
.on('error', e => {
console.error(`Got error: ${e.message}`)
})
.end()
}
// Import here Polyfills if needed. Recommended core-js (npm i -D core-js)
// import "core-js/fn/array.find"
// ...
export * from './main'
import {
MitterBase,
MitterAxiosInterceptionHost,
MitterAxiosApiInterceptor,
MitterConstants
} from 'mitter-core'
import { AxiosInstance } from 'axios'
import { AccessKeySigningInterceptor } from './auth/access-key-interceptor'
import { AccessKeyApplicationCredentials } from './auth/application-credentials'
export class MitterNode extends MitterBase implements MitterAxiosInterceptionHost {
private accessKeySigningInterceptor: AccessKeySigningInterceptor
constructor(
private applicationId: string,
private accessKey: AccessKeyApplicationCredentials,
public mitterApiBaseUrl: string
) {
super()
this.accessKeySigningInterceptor = new AccessKeySigningInterceptor(accessKey)
}
version() {
return '0.5.0'
}
enableAxiosInterceptor(axiosInstance: AxiosInstance) {
new MitterAxiosApiInterceptor(
/* the application id */
this.applicationId,
/* the default signing interceptor to use */
this.accessKeySigningInterceptor.getInterceptor(),
this.mitterApiBaseUrl
).enable(axiosInstance)
}
}
export const Mitter = {
forNode: function(
applicationId: string,
accessKey: AccessKeyApplicationCredentials,
mitterApiBaseUrl: string = MitterConstants.MitterApiUrl
): MitterNode {
return new MitterNode(applicationId, accessKey, mitterApiBaseUrl)
}
}
import DummyClass from '../src/mitter-node'
/**
* Dummy test
*/
describe('Dummy test', () => {
it('works if true is truthy', () => {
expect(true).toBeTruthy()
})
it('DummyClass is instantiable', () => {
expect(new DummyClass()).toBeInstanceOf(DummyClass)
})
})
const { cd, exec, echo, touch } = require("shelljs")
const { readFileSync } = require("fs")
const url = require("url")
let repoUrl
let pkg = JSON.parse(readFileSync("package.json") as any)
if (typeof pkg.repository === "object") {
if (!pkg.repository.hasOwnProperty("url")) {
throw new Error("URL does not exist in repository section")
}
repoUrl = pkg.repository.url
} else {
repoUrl = pkg.repository
}
let parsedUrl = url.parse(repoUrl)
let repository = (parsedUrl.host || "") + (parsedUrl.path || "")
let ghToken = process.env.GH_TOKEN
echo("Deploying docs!!!")
cd("docs")
touch(".nojekyll")
exec("git init")
exec("git add .")
exec('git config user.name "Rohan Prabhu"')
exec('git config user.email ""')
exec('git commit -m "docs(docs): update gh-pages"')
exec(
`git push --force --quiet "https://${ghToken}@${repository}" master:gh-pages`
)
echo("Docs deployed!!")
const path = require("path")
const { fork } = require("child_process")
const colors = require("colors")
const { readFileSync, writeFileSync } = require("fs")
const pkg = JSON.parse(
readFileSync(path.resolve(__dirname, "..", "package.json"))
)
pkg.scripts.prepush = "npm run test:prod && npm run build"
pkg.scripts.commitmsg = "validate-commit-msg"
writeFileSync(
path.resolve(__dirname, "..", "package.json"),
JSON.stringify(pkg, null, 2)
)
// Call husky to set up the hooks
fork(path.resolve(__dirname, "..", "node_modules", "husky", "bin", "install"))
console.log()
console.log(colors.green("Done!!"))
console.log()
if (pkg.repository.url.trim()) {
console.log(colors.cyan("Now run:"))
console.log(colors.cyan(" npm install -g semantic-release-cli"))
console.log(colors.cyan(" semantic-release-cli setup"))
console.log()
console.log(
colors.cyan('Important! Answer NO to "Generate travis.yml" question')
)
console.log()
console.log(
colors.gray(
'Note: Make sure "repository.url" in your package.json is correct before'
)
)
} else {
console.log(
colors.red(
'First you need to set the "repository.url" property in package.json'
)
)
console.log(colors.cyan("Then run:"))
console.log(colors.cyan(" npm install -g semantic-release-cli"))
console.log(colors.cyan(" semantic-release-cli setup"))
console.log()
console.log(
colors.cyan('Important! Answer NO to "Generate travis.yml" question')
)
}
console.log()
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment