Skip to content

Commit

Permalink
Add @segment/analytics-generic-utils (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Oct 19, 2023
1 parent 2f1ae75 commit c879377
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 23 deletions.
15 changes: 15 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ steps:
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
paths: ['.yarn/cache/']

- label: '[Generic Utils] Lint + Test'
agents:
queue: v1
commands:
- npm config set "//registry.npmjs.org/:_authToken" $${NPM_TOKEN}
- echo "--- Install dependencies"
- PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 HUSKY=0 yarn install --immutable
- echo "+++ Run QA"
- yarn turbo run --filter=analytics-generic-utils lint
- yarn turbo run --filter=analytics-generic-utils test
plugins:
- ssh://[email protected]/segmentio/cache-buildkite-plugin#v2.0.0:
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
paths: ['.yarn/cache/']

- label: '[Consent] Lint + Test'
agents:
queue: v1
Expand Down
6 changes: 6 additions & 0 deletions .changeset/green-experts-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@segment/analytics-generic-utils': major
'@segment/analytics-node': patch
---

Refactor to get createDeferred from @segment/analytics-generic-utils lib
4 changes: 4 additions & 0 deletions packages/generic-utils/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type { import('eslint').Linter.Config } */
module.exports = {
extends: ['../../.eslintrc.isomorphic'],
}
1 change: 1 addition & 0 deletions packages/generic-utils/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@internal/config").lintStagedConfig
21 changes: 21 additions & 0 deletions packages/generic-utils/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2023 Segment

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions packages/generic-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @segment/analytics-generic-utils

This monorepo's version of "lodash". This package contains shared generic utilities that can be used within the ecosystem. This package should not have dependencies, and should not contain any references to the Analytics domain.
3 changes: 3 additions & 0 deletions packages/generic-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { createJestTSConfig } = require('@internal/config')

module.exports = createJestTSConfig(__dirname)
36 changes: 36 additions & 0 deletions packages/generic-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@segment/analytics-generic-utils",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "https://github.com/segmentio/analytics-next",
"directory": "packages/generic-utils"
},
"license": "MIT",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/",
"src/",
"!**/__tests__/**",
"!*.tsbuildinfo",
"LICENSE"
],
"sideEffects": false,
"scripts": {
".": "yarn run -T turbo run --filter=@segment/analytics-generic-utils",
"test": "yarn jest",
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
"build": "yarn concurrently 'yarn:build:*'",
"build:esm": "yarn tsc -p tsconfig.build.json",
"build:cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs",
"watch": "yarn build:esm --watch",
"watch:test": "yarn test --watch",
"tsc": "yarn run -T tsc",
"eslint": "yarn run -T eslint",
"concurrently": "yarn run -T concurrently",
"jest": "yarn run -T jest"
},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createDeferred } from '../create-deferred'

describe(createDeferred, () => {
it('should return a deferred object', async () => {
const o = createDeferred()
expect(o.promise).toBeInstanceOf(Promise)
expect(o.resolve).toBeInstanceOf(Function)
expect(o.reject).toBeInstanceOf(Function)
})
it('should resolve', async () => {
const { promise, resolve } = createDeferred<string>()
let isResolved = false
let isResolvedVal = undefined
void promise.then((value) => {
isResolvedVal = value
isResolved = true
})
expect(isResolved).toBe(false)
expect(isResolvedVal).toBeUndefined()
await resolve('foo')
expect(isResolved).toBe(true)
expect(isResolvedVal).toBe('foo')
})

it('should reject', async () => {
const { promise, reject } = createDeferred<string>()
let isRejected = false
let isRejectedVal = undefined
void promise.catch((value) => {
isRejectedVal = value
isRejected = true
})
expect(isRejected).toBe(false)
expect(isRejectedVal).toBeUndefined()
await reject('foo')
expect(isRejected).toBe(true)
expect(isRejectedVal).toBe('foo')
})
})
16 changes: 16 additions & 0 deletions packages/generic-utils/src/create-deferred/create-deferred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Return a promise that can be externally resolved
*/
export const createDeferred = <T>() => {
let resolve!: (value: T | PromiseLike<T>) => void
let reject!: (reason: any) => void
const promise = new Promise<T>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
})
return {
resolve,
reject,
promise,
}
}
1 change: 1 addition & 0 deletions packages/generic-utils/src/create-deferred/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './create-deferred'
1 change: 1 addition & 0 deletions packages/generic-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './create-deferred'
9 changes: 9 additions & 0 deletions packages/generic-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["**/__tests__/**", "**/*.test.*"],
"compilerOptions": {
"outDir": "./dist/esm",
"declarationDir": "./dist/types"
}
}
11 changes: 11 additions & 0 deletions packages/generic-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"module": "esnext",
"target": "ES5",
"moduleResolution": "node",
"resolveJsonModule": true,
"lib": ["es2020"]
}
}
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@lukeed/uuid": "^2.0.0",
"@segment/analytics-core": "1.3.2",
"@segment/analytics-generic-utils": "0.0.1",
"buffer": "^6.0.3",
"node-fetch": "^2.6.7",
"tslib": "^2.4.1"
Expand Down
21 changes: 0 additions & 21 deletions packages/node/src/lib/extract-promise-parts.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/node/src/plugins/segmentio/publisher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { backoff } from '@segment/analytics-core'
import type { Context } from '../../app/context'
import { tryCreateFormattedUrl } from '../../lib/create-url'
import { extractPromiseParts } from '../../lib/extract-promise-parts'
import { createDeferred } from '@segment/analytics-generic-utils'
import { ContextBatch } from './context-batch'
import { NodeEmitter } from '../../app/emitter'
import { b64encode } from '../../lib/base-64-encode'
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Publisher {
enqueue(ctx: Context): Promise<Context> {
const batch = this._batch ?? this.createBatch()

const { promise: ctxPromise, resolve } = extractPromiseParts<Context>()
const { promise: ctxPromise, resolve } = createDeferred<Context>()

const pendingItem: PendingItem = {
context: ctx,
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3747,6 +3747,12 @@ __metadata:
languageName: unknown
linkType: soft

"@segment/[email protected], @segment/analytics-generic-utils@workspace:packages/generic-utils":
version: 0.0.0-use.local
resolution: "@segment/analytics-generic-utils@workspace:packages/generic-utils"
languageName: unknown
linkType: soft

"@segment/analytics-next@workspace:*, @segment/analytics-next@workspace:packages/browser":
version: 0.0.0-use.local
resolution: "@segment/analytics-next@workspace:packages/browser"
Expand Down Expand Up @@ -3811,6 +3817,7 @@ __metadata:
"@internal/config": 0.0.0
"@lukeed/uuid": ^2.0.0
"@segment/analytics-core": 1.3.2
"@segment/analytics-generic-utils": 0.0.1
"@types/node": ^16
axios: ^1.4.0
buffer: ^6.0.3
Expand Down

0 comments on commit c879377

Please sign in to comment.