-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(saslprep): provide a browser-compatible version of saslprep pac…
…kage that doesn't use zlib (#202)
- Loading branch information
1 parent
eb7e757
commit a8e3821
Showing
9 changed files
with
175 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "dist/index.js", | ||
"main": "dist/node.js", | ||
"bugs": { | ||
"url": "https://jira.mongodb.org/projects/COMPASS/issues", | ||
"email": "[email protected]" | ||
|
@@ -28,18 +28,22 @@ | |
], | ||
"license": "MIT", | ||
"exports": { | ||
"browser": { | ||
"types": "./dist/browser.d.ts", | ||
"default": "./dist/browser.js" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"types": "./dist/node.d.ts", | ||
"default": "./dist/.esm-wrapper.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
"types": "./dist/node.d.ts", | ||
"default": "./dist/node.js" | ||
} | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"types": "./dist/node.d.ts", | ||
"scripts": { | ||
"gen-code-points": "ts-node src/generate-code-points.ts src/code-points-data.ts", | ||
"gen-code-points": "ts-node src/generate-code-points.ts src/code-points-data.ts src/code-points-data-browser.ts", | ||
"bootstrap": "npm run compile", | ||
"prepublishOnly": "npm run compile", | ||
"compile": "npm run gen-code-points && tsc -p tsconfig.json && gen-esm-wrapper . ./dist/.esm-wrapper.mjs", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import saslprep from './browser'; | ||
import { expect } from 'chai'; | ||
|
||
const chr = String.fromCodePoint; | ||
|
||
describe('saslprep (browser)', function () { | ||
it('should work with latin letters', function () { | ||
const str = 'user'; | ||
expect(saslprep(str)).to.equal(str); | ||
}); | ||
|
||
it('should work be case preserved', function () { | ||
const str = 'USER'; | ||
expect(saslprep(str)).to.equal(str); | ||
}); | ||
|
||
it('should work with high code points (> U+FFFF)', function () { | ||
const str = '\uD83D\uDE00'; | ||
expect(saslprep(str, { allowUnassigned: true })).to.equal(str); | ||
}); | ||
|
||
it('should remove `mapped to nothing` characters', function () { | ||
expect(saslprep('I\u00ADX')).to.equal('IX'); | ||
}); | ||
|
||
it('should replace `Non-ASCII space characters` with space', function () { | ||
expect(saslprep('a\u00A0b')).to.equal('a\u0020b'); | ||
}); | ||
|
||
it('should normalize as NFKC', function () { | ||
expect(saslprep('\u00AA')).to.equal('a'); | ||
expect(saslprep('\u2168')).to.equal('IX'); | ||
}); | ||
|
||
it('should throws when prohibited characters', function () { | ||
// C.2.1 ASCII control characters | ||
expect(() => saslprep('a\u007Fb')).to.throw(); | ||
|
||
// C.2.2 Non-ASCII control characters | ||
expect(() => saslprep('a\u06DDb')).to.throw(); | ||
|
||
// C.3 Private use | ||
expect(() => saslprep('a\uE000b')).to.throw(); | ||
|
||
// C.4 Non-character code points | ||
expect(() => saslprep(`a${chr(0x1fffe)}b`)).to.throw(); | ||
|
||
// C.5 Surrogate codes | ||
expect(() => saslprep('a\uD800b')).to.throw(); | ||
|
||
// C.6 Inappropriate for plain text | ||
expect(() => saslprep('a\uFFF9b')).to.throw(); | ||
|
||
// C.7 Inappropriate for canonical representation | ||
expect(() => saslprep('a\u2FF0b')).to.throw(); | ||
|
||
// C.8 Change display properties or are deprecated | ||
expect(() => saslprep('a\u200Eb')).to.throw(); | ||
|
||
// C.9 Tagging characters | ||
expect(() => saslprep(`a${chr(0xe0001)}b`)).to.throw(); | ||
}); | ||
|
||
it('should not containt RandALCat and LCat bidi', function () { | ||
expect(() => saslprep('a\u06DD\u00AAb')).to.throw(); | ||
}); | ||
|
||
it('RandALCat should be first and last', function () { | ||
expect(() => saslprep('\u0627\u0031\u0628')).not.to.throw(); | ||
expect(() => saslprep('\u0627\u0031')).to.throw(); | ||
}); | ||
|
||
it('should handle unassigned code points', function () { | ||
expect(() => saslprep('a\u0487')).to.throw(); | ||
expect(() => saslprep('a\u0487', { allowUnassigned: true })).not.to.throw(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import _saslprep from './index'; | ||
import { createMemoryCodePoints } from './memory-code-points'; | ||
import data from './code-points-data-browser'; | ||
|
||
const codePoints = createMemoryCodePoints(data); | ||
|
||
const saslprep = _saslprep.bind(null, codePoints); | ||
|
||
Object.assign(saslprep, { saslprep, default: saslprep }); | ||
|
||
export = saslprep; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
import bitfield from 'sparse-bitfield'; | ||
import memory from './code-points-data'; | ||
|
||
let offset = 0; | ||
export function createMemoryCodePoints(data: Buffer) { | ||
let offset = 0; | ||
|
||
/** | ||
* Loads each code points sequence from buffer. | ||
*/ | ||
function read(): bitfield.BitFieldInstance { | ||
const size = memory.readUInt32BE(offset); | ||
offset += 4; | ||
/** | ||
* Loads each code points sequence from buffer. | ||
*/ | ||
function read(): bitfield.BitFieldInstance { | ||
const size = data.readUInt32BE(offset); | ||
offset += 4; | ||
|
||
const codepoints = memory.slice(offset, offset + size); | ||
offset += size; | ||
const codepoints = data.slice(offset, offset + size); | ||
offset += size; | ||
|
||
return bitfield({ buffer: codepoints }); | ||
} | ||
return bitfield({ buffer: codepoints }); | ||
} | ||
|
||
const unassigned_code_points = read(); | ||
const commonly_mapped_to_nothing = read(); | ||
const non_ASCII_space_characters = read(); | ||
const prohibited_characters = read(); | ||
const bidirectional_r_al = read(); | ||
const bidirectional_l = read(); | ||
|
||
export const unassigned_code_points = read(); | ||
export const commonly_mapped_to_nothing = read(); | ||
export const non_ASCII_space_characters = read(); | ||
export const prohibited_characters = read(); | ||
export const bidirectional_r_al = read(); | ||
export const bidirectional_l = read(); | ||
return { | ||
unassigned_code_points, | ||
commonly_mapped_to_nothing, | ||
non_ASCII_space_characters, | ||
prohibited_characters, | ||
bidirectional_r_al, | ||
bidirectional_l, | ||
}; | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/saslprep/src/index.spec.ts → packages/saslprep/src/node.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import _saslprep from './index'; | ||
import { createMemoryCodePoints } from './memory-code-points'; | ||
import data from './code-points-data'; | ||
|
||
const codePoints = createMemoryCodePoints(data); | ||
|
||
const saslprep = _saslprep.bind(null, codePoints); | ||
|
||
Object.assign(saslprep, { saslprep, default: saslprep }); | ||
|
||
export = saslprep; |